This is a fork of ChaCha20 from rbx-cryptography library.
Code
--!nolint MultiLineStatement
--!strict
--!optimize 2
--!native
--[[
Cryptography library: ChaCha21
Sizes:
Nonce: 12/24 bytes
Key: 16/32 bytes
Return type: buffer
Example usage:
local Data = buffer.fromstring("Hello World")
local Key = buffer.fromstring(string.rep("k", 32))
local Nonce = buffer.fromstring(string.rep("n", 12))
--------Usage Case 1--------
local Encrypted = ChaCha21(Data, Key, Nonce)
--]]
local DWORD = 4
local BLOCK_SIZE = 64
local STATE_SIZE = 16
local CHACHA20_NONCE_SIZE = 12
local CHACHA20_KEY_SIZE_16 = 16
local CHACHA20_KEY_SIZE_32 = 32
local SIGMA_CONSTANTS = buffer.create(16) do
local SigmaBytes = { string.byte("expand 32-byte k", 1, -1) }
for Index, ByteValue in ipairs(SigmaBytes) do
buffer.writeu8(SIGMA_CONSTANTS, Index - 1, ByteValue)
end
end
local TAU_CONSTANTS = buffer.create(16) do
local TauBytes = { string.byte("expand 16-byte k", 1, -1) }
for Index, ByteValue in ipairs(TauBytes) do
buffer.writeu8(TAU_CONSTANTS, Index - 1, ByteValue)
end
end
local ProcessBlock = function(InitialState: buffer, Rounds: number)
local S0: number, S1: number, S2: number, S3: number, S4: number, S5: number, S6: number, S7: number, S8: number, S9: number, S10: number, S11: number, S12: number, S13: number, S14: number, S15: number =
buffer.readu32(InitialState, 0), buffer.readu32(InitialState, 4),
buffer.readu32(InitialState, 8), buffer.readu32(InitialState, 12),
buffer.readu32(InitialState, 16), buffer.readu32(InitialState, 20),
buffer.readu32(InitialState, 24), buffer.readu32(InitialState, 28),
buffer.readu32(InitialState, 32), buffer.readu32(InitialState, 36),
buffer.readu32(InitialState, 40), buffer.readu32(InitialState, 44),
buffer.readu32(InitialState, 48), buffer.readu32(InitialState, 52),
buffer.readu32(InitialState, 56), buffer.readu32(InitialState, 60)
for Round = 1, Rounds do
local RoundType = Round % 3
if RoundType == 1 then
S0 = bit32.bor(S0 + S4, 0); S12 = bit32.lrotate(bit32.bxor(S12, S0), 16)
S8 = bit32.bor(S8 + S12, 0); S4 = bit32.lrotate(bit32.bxor(S4, S8), 12)
S0 = bit32.bor(S0 + S4, 0); S12 = bit32.lrotate(bit32.bxor(S12, S0), 8)
S8 = bit32.bor(S8 + S12, 0); S4 = bit32.lrotate(bit32.bxor(S4, S8), 7)
S1 = bit32.bor(S1 + S5, 0); S13 = bit32.lrotate(bit32.bxor(S13, S1), 16)
S9 = bit32.bor(S9 + S13, 0); S5 = bit32.lrotate(bit32.bxor(S5, S9), 12)
S1 = bit32.bor(S1 + S5, 0); S13 = bit32.lrotate(bit32.bxor(S13, S1), 8)
S9 = bit32.bor(S9 + S13, 0); S5 = bit32.lrotate(bit32.bxor(S5, S9), 7)
S2 = bit32.bor(S2 + S6, 0); S14 = bit32.lrotate(bit32.bxor(S14, S2), 16)
S10 = bit32.bor(S10 + S14, 0); S6 = bit32.lrotate(bit32.bxor(S6, S10), 12)
S2 = bit32.bor(S2 + S6, 0); S14 = bit32.lrotate(bit32.bxor(S14, S2), 8)
S10 = bit32.bor(S10 + S14, 0); S6 = bit32.lrotate(bit32.bxor(S6, S10), 7)
S3 = bit32.bor(S3 + S7, 0); S15 = bit32.lrotate(bit32.bxor(S15, S3), 16)
S11 = bit32.bor(S11 + S15, 0); S7 = bit32.lrotate(bit32.bxor(S7, S11), 12)
S3 = bit32.bor(S3 + S7, 0); S15 = bit32.lrotate(bit32.bxor(S15, S3), 8)
S11 = bit32.bor(S11 + S15, 0); S7 = bit32.lrotate(bit32.bxor(S7, S11), 7)
elseif RoundType == 2 then
S0 = bit32.bor(S0 + S5, 0); S15 = bit32.lrotate(bit32.bxor(S15, S0), 16)
S10 = bit32.bor(S10 + S15, 0); S5 = bit32.lrotate(bit32.bxor(S5, S10), 12)
S0 = bit32.bor(S0 + S5, 0); S15 = bit32.lrotate(bit32.bxor(S15, S0), 8)
S10 = bit32.bor(S10 + S15, 0); S5 = bit32.lrotate(bit32.bxor(S5, S10), 7)
S1 = bit32.bor(S1 + S6, 0); S12 = bit32.lrotate(bit32.bxor(S12, S1), 16)
S11 = bit32.bor(S11 + S12, 0); S6 = bit32.lrotate(bit32.bxor(S6, S11), 12)
S1 = bit32.bor(S1 + S6, 0); S12 = bit32.lrotate(bit32.bxor(S12, S1), 8)
S11 = bit32.bor(S11 + S12, 0); S6 = bit32.lrotate(bit32.bxor(S6, S11), 7)
S2 = bit32.bor(S2 + S7, 0); S13 = bit32.lrotate(bit32.bxor(S13, S2), 16)
S8 = bit32.bor(S8 + S13, 0); S7 = bit32.lrotate(bit32.bxor(S7, S8), 12)
S2 = bit32.bor(S2 + S7, 0); S13 = bit32.lrotate(bit32.bxor(S13, S2), 8)
S8 = bit32.bor(S8 + S13, 0); S7 = bit32.lrotate(bit32.bxor(S7, S8), 7)
S3 = bit32.bor(S3 + S4, 0); S14 = bit32.lrotate(bit32.bxor(S14, S3), 16)
S9 = bit32.bor(S9 + S14, 0); S4 = bit32.lrotate(bit32.bxor(S4, S9), 12)
S3 = bit32.bor(S3 + S4, 0); S14 = bit32.lrotate(bit32.bxor(S14, S3), 8)
S9 = bit32.bor(S9 + S14, 0); S4 = bit32.lrotate(bit32.bxor(S4, S9), 7)
else
S3 = bit32.bor(S3 + S6, 0); S12 = bit32.lrotate(bit32.bxor(S12, S3), 16)
S9 = bit32.bor(S9 + S12, 0); S6 = bit32.lrotate(bit32.bxor(S6, S9), 12)
S3 = bit32.bor(S3 + S6, 0); S12 = bit32.lrotate(bit32.bxor(S12, S3), 8)
S9 = bit32.bor(S9 + S12, 0); S6 = bit32.lrotate(bit32.bxor(S6, S9), 7)
S0 = bit32.bor(S0 + S7, 0); S13 = bit32.lrotate(bit32.bxor(S13, S0), 16)
S10 = bit32.bor(S10 + S13, 0); S7 = bit32.lrotate(bit32.bxor(S7, S10), 12)
S0 = bit32.bor(S0 + S7, 0); S13 = bit32.lrotate(bit32.bxor(S13, S0), 8)
S10 = bit32.bor(S10 + S13, 0); S7 = bit32.lrotate(bit32.bxor(S7, S10), 7)
S1 = bit32.bor(S1 + S4, 0); S14 = bit32.lrotate(bit32.bxor(S14, S1), 16)
S11 = bit32.bor(S11 + S14, 0); S4 = bit32.lrotate(bit32.bxor(S4, S11), 12)
S1 = bit32.bor(S1 + S4, 0); S14 = bit32.lrotate(bit32.bxor(S14, S1), 8)
S11 = bit32.bor(S11 + S14, 0); S4 = bit32.lrotate(bit32.bxor(S4, S11), 7)
S2 = bit32.bor(S2 + S5, 0); S15 = bit32.lrotate(bit32.bxor(S15, S2), 16)
S8 = bit32.bor(S8 + S15, 0); S5 = bit32.lrotate(bit32.bxor(S5, S8), 12)
S2 = bit32.bor(S2 + S5, 0); S15 = bit32.lrotate(bit32.bxor(S15, S2), 8)
S8 = bit32.bor(S8 + S15, 0); S5 = bit32.lrotate(bit32.bxor(S5, S8), 7)
end
end
buffer.writeu32(InitialState, 0, buffer.readu32(InitialState, 0) + S0)
buffer.writeu32(InitialState, 4, buffer.readu32(InitialState, 4) + S1)
buffer.writeu32(InitialState, 8, buffer.readu32(InitialState, 8) + S2)
buffer.writeu32(InitialState, 12, buffer.readu32(InitialState, 12) + S3)
buffer.writeu32(InitialState, 16, buffer.readu32(InitialState, 16) + S4)
buffer.writeu32(InitialState, 20, buffer.readu32(InitialState, 20) + S5)
buffer.writeu32(InitialState, 24, buffer.readu32(InitialState, 24) + S6)
buffer.writeu32(InitialState, 28, buffer.readu32(InitialState, 28) + S7)
buffer.writeu32(InitialState, 32, buffer.readu32(InitialState, 32) + S8)
buffer.writeu32(InitialState, 36, buffer.readu32(InitialState, 36) + S9)
buffer.writeu32(InitialState, 40, buffer.readu32(InitialState, 40) + S10)
buffer.writeu32(InitialState, 44, buffer.readu32(InitialState, 44) + S11)
buffer.writeu32(InitialState, 48, buffer.readu32(InitialState, 48) + S12)
buffer.writeu32(InitialState, 52, buffer.readu32(InitialState, 52) + S13)
buffer.writeu32(InitialState, 56, buffer.readu32(InitialState, 56) + S14)
buffer.writeu32(InitialState, 60, buffer.readu32(InitialState, 60) + S15)
end
local InitializeState = function(Key: buffer, Nonce: buffer, Counter: number): buffer
local KeyLength = buffer.len(Key)
local State = buffer.create(STATE_SIZE * DWORD)
local Sigma, Tau = SIGMA_CONSTANTS, TAU_CONSTANTS
local Constants = KeyLength == 32 and Sigma or Tau
buffer.copy(State, 0, Constants, 0, 16)
buffer.copy(State, 16, Key, 0, math.min(KeyLength, 16))
if KeyLength == 32 then
buffer.copy(State, 32, Key, 16, 16)
else
buffer.copy(State, 32, Key, 0, 16)
end
buffer.writeu32(State, 48, Counter)
buffer.copy(State, 52, Nonce, 0, 12)
return State
end
local ChaCha21 = function(Data: buffer, Key: buffer, Nonce: buffer, Counter: number?, Rounds: number?): buffer
local BlockCounter = Counter or 1
local BlockRounds = Rounds or 21
if BlockRounds % 3 ~= 0 then
BlockRounds = math.ceil(BlockRounds / 3) * 3
end
local DataLength = buffer.len(Data)
if DataLength == 0 then
return buffer.create(0)
end
local Output = buffer.create(DataLength)
local DataOffset = 0
local State = InitializeState(Key, Nonce, BlockCounter)
local StateBackup = buffer.create(64)
buffer.copy(StateBackup, 0, State, 0)
while DataOffset < DataLength do
ProcessBlock(State, BlockRounds)
local BytesToProcess = math.min(BLOCK_SIZE, DataLength - DataOffset)
for Index = 0, BytesToProcess - 1 do
local DataByte = buffer.readu8(Data, DataOffset + Index)
local KeystreamByte = buffer.readu8(State, Index)
buffer.writeu8(Output, DataOffset + Index, bit32.bxor(DataByte, KeystreamByte))
end
DataOffset += BytesToProcess
BlockCounter += 1
buffer.copy(State, 0, StateBackup, 0)
buffer.writeu32(State, 48, BlockCounter)
end
return Output
end
return ChaCha21
Example use:
local encrypted = require(workspace.ChaCha21)(buffer.fromstring("hihihihi"), buffer.fromstring(string.rep("k", 32)), buffer.fromstring(string.rep("n", 12)))
local decrypted = require(workspace.ChaCha21)(encrypted, buffer.fromstring(string.rep("k", 32)), buffer.fromstring(string.rep("n", 12)))
print(buffer.tostring(decrypted))