Repository navigation
Expand file tree
/
Copy pathChaCha20.pas
More file actions
210 lines (178 loc) · 6.44 KB
/
Copy pathChaCha20.pas
File metadata and controls
210 lines (178 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
unit ChaCha20;
{
Inno Setup
Copyright (C) 1997-2026 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
ChaCha20 and XChaCha20 encryption/decryption
Initially based on https://github.com/Ginurx/chacha20-c/tree/master
}
interface
uses
System.SysUtils;
type
TChaCha20Ctx = array[0..15] of Cardinal;
TChaCha20Context = record
ctx, keystream: TChaCha20Ctx;
position: 0..64;
exhausted: Boolean;
count64: Boolean;
end;
procedure ChaCha20Init(var Context: TChaCha20Context; const Key;
const KeyLength: Cardinal; const Nonce; const NonceLength: Cardinal;
const Count: Cardinal);
procedure ChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;
var OutBuffer; const Length: Cardinal);
procedure HChaCha20(const Key; const KeyLength: Cardinal; const Nonce;
const NonceLength: Cardinal; out SubKey: TBytes);
procedure XChaCha20Init(var Context: TChaCha20Context; const Key;
const KeyLength: Cardinal; const Nonce; const NonceLength: Cardinal;
const Count: Cardinal);
procedure XChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;
var OutBuffer; const Length: Cardinal);
implementation
uses
UnsignedFunc;
{$C+}
{$Q-} { ChaCha20 arithmetic relies on Cardinal wraparound }
procedure ChaCha20InitCtx(var ctx: TChaCha20Ctx; const Key;
const KeyLength: Cardinal; const Nonce; const NonceLength: Cardinal;
const Count: Cardinal);
begin
Assert(KeyLength = 32);
Assert(NonceLength in [0, 8, 12]);
{$IFDEF DEBUG}
FillChar(ctx[0], SizeOf(ctx), 1);
{$ENDIF}
ctx[0] := $61707865;
ctx[1] := $3320646e;
ctx[2] := $79622d32;
ctx[3] := $6b206574;
UMove(Key, ctx[4], KeyLength);
ctx[12] := Count;
if NonceLength = 12 then
Move(Nonce, ctx[13], 12)
else if NonceLength = 8 then begin
ctx[13] := 0;
Move(Nonce, ctx[14], 8)
end else
FillChar(ctx[13], 12, 0);
end;
procedure ChaCha20Init(var Context: TChaCha20Context; const Key;
const KeyLength: Cardinal; const Nonce; const NonceLength: Cardinal;
const Count: Cardinal);
begin
ChaCha20InitCtx(Context.ctx, Key, KeyLength, Nonce, NonceLength, Count);
Context.position := 64;
Context.exhausted := False;
Context.count64 := NonceLength <> 12;
end;
procedure ChaCha20RunRounds(var ctx, keystream: TChaCha20Ctx);
function ROTL(const x: Cardinal; const n: Byte): Cardinal;
begin
Result := (x shl n) or (x shr (32 - n));
end;
procedure CHACHA20_QR(var a, b, c, d: Cardinal);
begin
Inc(a, b); d := d xor a; d := ROTL(d, 16);
Inc(c, d); b := b xor c; b := ROTL(b, 12);
Inc(a, b); d := d xor a; d := ROTL(d, 8);
Inc(c, d); b := b xor c; b := ROTL(b, 7);
end;
begin
Move(ctx, keystream, SizeOf(ctx));
for var i := 0 to 9 do begin
CHACHA20_QR(keystream[0], keystream[4], keystream[8], keystream[12]); // column 0
CHACHA20_QR(keystream[1], keystream[5], keystream[9], keystream[13]); // column 1
CHACHA20_QR(keystream[2], keystream[6], keystream[10], keystream[14]); // column 2
CHACHA20_QR(keystream[3], keystream[7], keystream[11], keystream[15]); // column 3
CHACHA20_QR(keystream[0], keystream[5], keystream[10], keystream[15]); // diagonal 1 (main diagonal)
CHACHA20_QR(keystream[1], keystream[6], keystream[11], keystream[12]); // diagonal 2
CHACHA20_QR(keystream[2], keystream[7], keystream[8], keystream[13]); // diagonal 3
CHACHA20_QR(keystream[3], keystream[4], keystream[9], keystream[14]); // diagonal 4
end;
end;
procedure ChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;
var OutBuffer; const Length: Cardinal);
procedure ChaCha20BlockNext(var ctx, keystream: TChaCha20Ctx;
var exhausted: Boolean; const count64: Boolean);
begin
{ The counter is advanced only after the block for it has been generated,
so the last valid block (counter at its maximum) must still be usable.
The context is marked exhausted instead, which makes the assert fire on
the next call rather than one block too early }
Assert(not exhausted);
ChaCha20RunRounds(ctx, keystream);
for var i := 0 to 15 do
keystream[i] := keystream[i] + ctx[i];
if count64 then begin
if ctx[12] < High(Cardinal) then
ctx[12] := ctx[12] + 1
else if ctx[13] < High(Cardinal) then begin
ctx[12] := 0;
ctx[13] := ctx[13] + 1;
end else
exhausted := True;
end else begin
if ctx[12] < High(Cardinal) then
ctx[12] := ctx[12] + 1
else
exhausted := True;
end;
end;
begin
if Length = 0 then
Exit;
var InBuf: PByte := @InBuffer;
var OutBuf: PByte := @OutBuffer;
var KeyStream := PByte(@Context.keystream);
for var I := 0 to Length-1 do begin
if Context.position >= 64 then begin
ChaCha20BlockNext(Context.ctx, Context.keystream, Context.exhausted, Context.count64);
Context.position := 0;
end;
OutBuf[I] := InBuf[I] xor KeyStream[Context.position];
Inc(Context.position);
end;
end;
procedure HChaCha20(const Key; const KeyLength: Cardinal; const Nonce;
const NonceLength: Cardinal; out SubKey: TBytes);
begin
Assert(NonceLength = 16);
var NonceBytes: PByte := @Nonce;
var ctx: TChaCha20Ctx;
ChaCha20InitCtx(ctx, Key, KeyLength, NonceBytes[4], 12, PCardinal(NonceBytes)^);
var keystream: TChaCha20Ctx;
ChaCha20RunRounds(ctx, keystream);
try
SetLength(SubKey, 32);
Move(keystream[0], SubKey[0], 16);
Move(keystream[12], SubKey[16], 16);
finally
{ Security: don't leave key material on the stack }
FillChar(ctx, SizeOf(ctx), 0);
FillChar(keystream, SizeOf(keystream), 0);
end;
end;
procedure XChaCha20Init(var Context: TChaCha20Context; const Key;
const KeyLength: Cardinal; const Nonce; const NonceLength: Cardinal;
const Count: Cardinal);
begin
Assert(NonceLength = 24);
var SubKey: TBytes;
HChaCha20(Key, KeyLength, Nonce, 16, SubKey);
try
var NonceBytes: PByte := @Nonce;
ChaCha20Init(Context, SubKey[0], ULength(SubKey), NonceBytes[16], 8, Count);
finally
{ Security: don't leave derived key in heap memory }
if Length(SubKey) > 0 then
FillChar(SubKey[0], Length(SubKey), 0);
end;
end;
procedure XChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;
var OutBuffer; const Length: Cardinal);
begin
ChaCha20Crypt(Context, InBuffer, OutBuffer, Length);
end;
end.