authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-10-16 19:10:20+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-17 18:53:08-04:00
logfa17447090500b67c515c023376ab66201f8f088
tree5521ad832ded1b9c38a24fec0cc02871739bd630
parent0011def2b24f63233f2ee24909701f92264c2ef5

std/crypto: make the whole APIs more consistent

- use `PascalCase` for all types. So, AES256GCM is now Aes256Gcm. - consistently use `_length` instead of mixing `_size` and `_length` for the constants we expose - Use `minimum_key_length` when it represents an actual minimum length. Otherwise, use `key_length`. - Require output buffers (for ciphertexts, macs, hashes) to be of the right size, not at least of that size in some functions, and the exact size elsewhere. - Use a `_bits` suffix instead of `_length` when a size is represented as a number of bits to avoid confusion. - Functions returning a constant-sized slice are now defined as a slice instead of a pointer + a runtime assertion. This is the case for most hash functions. - Use `camelCase` for all functions instead of `snake_case`. No functional changes, but these are breaking API changes.

24 files changed, 743 insertions(+), 758 deletions(-)

lib/std/crypto.zig+9-6
...@@ -11,10 +11,10 @@ pub const aead = struct {...@@ -11,10 +11,10 @@ pub const aead = struct {
11 pub const Gimli = @import("crypto/gimli.zig").Aead;11 pub const Gimli = @import("crypto/gimli.zig").Aead;
12 pub const ChaCha20Poly1305 = chacha20.Chacha20Poly1305;12 pub const ChaCha20Poly1305 = chacha20.Chacha20Poly1305;
13 pub const XChaCha20Poly1305 = chacha20.XChacha20Poly1305;13 pub const XChaCha20Poly1305 = chacha20.XChacha20Poly1305;
14 pub const AEGIS128L = @import("crypto/aegis.zig").AEGIS128L;14 pub const Aegis128L = @import("crypto/aegis.zig").Aegis128L;
15 pub const AEGIS256 = @import("crypto/aegis.zig").AEGIS256;15 pub const Aegis256 = @import("crypto/aegis.zig").Aegis256;
16 pub const AES128GCM = @import("crypto/aes_gcm.zig").AES128GCM;16 pub const Aes128Gcm = @import("crypto/aes_gcm.zig").Aes128Gcm;
17 pub const AES256GCM = @import("crypto/aes_gcm.zig").AES256GCM;17 pub const Aes256Gcm = @import("crypto/aes_gcm.zig").Aes256Gcm;
18};18};
1919
20/// Authentication (MAC) functions.20/// Authentication (MAC) functions.
...@@ -156,8 +156,11 @@ test "issue #4532: no index out of bounds" {...@@ -156,8 +156,11 @@ test "issue #4532: no index out of bounds" {
156 hash.sha3.Sha3_256,156 hash.sha3.Sha3_256,
157 hash.sha3.Sha3_384,157 hash.sha3.Sha3_384,
158 hash.sha3.Sha3_512,158 hash.sha3.Sha3_512,
159 hash.blake2.Blake2s128,
159 hash.blake2.Blake2s224,160 hash.blake2.Blake2s224,
160 hash.blake2.Blake2s256,161 hash.blake2.Blake2s256,
162 hash.blake2.Blake2b128,
163 hash.blake2.Blake2b256,
161 hash.blake2.Blake2b384,164 hash.blake2.Blake2b384,
162 hash.blake2.Blake2b512,165 hash.blake2.Blake2b512,
163 hash.Gimli,166 hash.Gimli,
...@@ -170,11 +173,11 @@ test "issue #4532: no index out of bounds" {...@@ -170,11 +173,11 @@ test "issue #4532: no index out of bounds" {
170 const h0 = Hasher.init(.{});173 const h0 = Hasher.init(.{});
171 var h = h0;174 var h = h0;
172 h.update(block[0..]);175 h.update(block[0..]);
173 h.final(out1[0..]);176 h.final(&out1);
174 h = h0;177 h = h0;
175 h.update(block[0..1]);178 h.update(block[0..1]);
176 h.update(block[1..]);179 h.update(block[1..]);
177 h.final(out2[0..]);180 h.final(&out2);
178181
179 std.testing.expectEqual(out1, out2);182 std.testing.expectEqual(out1, out2);
180 }183 }
lib/std/crypto/25519/x25519.zig+5-5
...@@ -14,12 +14,12 @@ pub const X25519 = struct {...@@ -14,12 +14,12 @@ pub const X25519 = struct {
14 /// Length (in bytes) of a secret key.14 /// Length (in bytes) of a secret key.
15 pub const secret_length = 32;15 pub const secret_length = 32;
16 /// Length (in bytes) of the output of the DH function.16 /// Length (in bytes) of the output of the DH function.
17 pub const minimum_key_length = 32;17 pub const key_length = 32;
1818
19 /// Compute the public key for a given private key.19 /// Compute the public key for a given private key.
20 pub fn createPublicKey(public_key: []u8, private_key: []const u8) bool {20 pub fn createPublicKey(public_key: []u8, private_key: []const u8) bool {
21 std.debug.assert(private_key.len >= minimum_key_length);21 std.debug.assert(private_key.len >= key_length);
22 std.debug.assert(public_key.len >= minimum_key_length);22 std.debug.assert(public_key.len >= key_length);
23 var s: [32]u8 = undefined;23 var s: [32]u8 = undefined;
24 mem.copy(u8, &s, private_key[0..32]);24 mem.copy(u8, &s, private_key[0..32]);
25 if (Curve.basePoint.clampedMul(s)) |q| {25 if (Curve.basePoint.clampedMul(s)) |q| {
...@@ -35,8 +35,8 @@ pub const X25519 = struct {...@@ -35,8 +35,8 @@ pub const X25519 = struct {
35 /// hashing it first.35 /// hashing it first.
36 pub fn create(out: []u8, private_key: []const u8, public_key: []const u8) bool {36 pub fn create(out: []u8, private_key: []const u8, public_key: []const u8) bool {
37 std.debug.assert(out.len >= secret_length);37 std.debug.assert(out.len >= secret_length);
38 std.debug.assert(private_key.len >= minimum_key_length);38 std.debug.assert(private_key.len >= key_length);
39 std.debug.assert(public_key.len >= minimum_key_length);39 std.debug.assert(public_key.len >= key_length);
40 var s: [32]u8 = undefined;40 var s: [32]u8 = undefined;
41 var b: [32]u8 = undefined;41 var b: [32]u8 = undefined;
42 mem.copy(u8, &s, private_key[0..32]);42 mem.copy(u8, &s, private_key[0..32]);
lib/std/crypto/aegis.zig+70-70
...@@ -1,17 +1,17 @@...@@ -1,17 +1,17 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;2const mem = std.mem;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const AESBlock = std.crypto.core.aes.Block;4const AesBlock = std.crypto.core.aes.Block;
55
6const State128L = struct {6const State128L = struct {
7 blocks: [8]AESBlock,7 blocks: [8]AesBlock,
88
9 fn init(key: [16]u8, nonce: [16]u8) State128L {9 fn init(key: [16]u8, nonce: [16]u8) State128L {
10 const c1 = AESBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd });10 const c1 = AesBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd });
11 const c2 = AESBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 });11 const c2 = AesBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 });
12 const key_block = AESBlock.fromBytes(&key);12 const key_block = AesBlock.fromBytes(&key);
13 const nonce_block = AESBlock.fromBytes(&nonce);13 const nonce_block = AesBlock.fromBytes(&nonce);
14 const blocks = [8]AESBlock{14 const blocks = [8]AesBlock{
15 key_block.xorBlocks(nonce_block),15 key_block.xorBlocks(nonce_block),
16 c1,16 c1,
17 c2,17 c2,
...@@ -29,7 +29,7 @@ const State128L = struct {...@@ -29,7 +29,7 @@ const State128L = struct {
29 return state;29 return state;
30 }30 }
3131
32 inline fn update(state: *State128L, d1: AESBlock, d2: AESBlock) void {32 inline fn update(state: *State128L, d1: AesBlock, d2: AesBlock) void {
33 const blocks = &state.blocks;33 const blocks = &state.blocks;
34 const tmp = blocks[7];34 const tmp = blocks[7];
35 comptime var i: usize = 7;35 comptime var i: usize = 7;
...@@ -43,8 +43,8 @@ const State128L = struct {...@@ -43,8 +43,8 @@ const State128L = struct {
4343
44 fn enc(state: *State128L, dst: *[32]u8, src: *const [32]u8) void {44 fn enc(state: *State128L, dst: *[32]u8, src: *const [32]u8) void {
45 const blocks = &state.blocks;45 const blocks = &state.blocks;
46 const msg0 = AESBlock.fromBytes(src[0..16]);46 const msg0 = AesBlock.fromBytes(src[0..16]);
47 const msg1 = AESBlock.fromBytes(src[16..32]);47 const msg1 = AesBlock.fromBytes(src[16..32]);
48 var tmp0 = msg0.xorBlocks(blocks[6]).xorBlocks(blocks[1]);48 var tmp0 = msg0.xorBlocks(blocks[6]).xorBlocks(blocks[1]);
49 var tmp1 = msg1.xorBlocks(blocks[2]).xorBlocks(blocks[5]);49 var tmp1 = msg1.xorBlocks(blocks[2]).xorBlocks(blocks[5]);
50 tmp0 = tmp0.xorBlocks(blocks[2].andBlocks(blocks[3]));50 tmp0 = tmp0.xorBlocks(blocks[2].andBlocks(blocks[3]));
...@@ -56,8 +56,8 @@ const State128L = struct {...@@ -56,8 +56,8 @@ const State128L = struct {
5656
57 fn dec(state: *State128L, dst: *[32]u8, src: *const [32]u8) void {57 fn dec(state: *State128L, dst: *[32]u8, src: *const [32]u8) void {
58 const blocks = &state.blocks;58 const blocks = &state.blocks;
59 var msg0 = AESBlock.fromBytes(src[0..16]).xorBlocks(blocks[6]).xorBlocks(blocks[1]);59 var msg0 = AesBlock.fromBytes(src[0..16]).xorBlocks(blocks[6]).xorBlocks(blocks[1]);
60 var msg1 = AESBlock.fromBytes(src[16..32]).xorBlocks(blocks[2]).xorBlocks(blocks[5]);60 var msg1 = AesBlock.fromBytes(src[16..32]).xorBlocks(blocks[2]).xorBlocks(blocks[5]);
61 msg0 = msg0.xorBlocks(blocks[2].andBlocks(blocks[3]));61 msg0 = msg0.xorBlocks(blocks[2].andBlocks(blocks[3]));
62 msg1 = msg1.xorBlocks(blocks[6].andBlocks(blocks[7]));62 msg1 = msg1.xorBlocks(blocks[6].andBlocks(blocks[7]));
63 dst[0..16].* = msg0.toBytes();63 dst[0..16].* = msg0.toBytes();
...@@ -70,7 +70,7 @@ const State128L = struct {...@@ -70,7 +70,7 @@ const State128L = struct {
70 var sizes: [16]u8 = undefined;70 var sizes: [16]u8 = undefined;
71 mem.writeIntLittle(u64, sizes[0..8], adlen * 8);71 mem.writeIntLittle(u64, sizes[0..8], adlen * 8);
72 mem.writeIntLittle(u64, sizes[8..16], mlen * 8);72 mem.writeIntLittle(u64, sizes[8..16], mlen * 8);
73 const tmp = AESBlock.fromBytes(&sizes).xorBlocks(blocks[2]);73 const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[2]);
74 var i: usize = 0;74 var i: usize = 0;
75 while (i < 7) : (i += 1) {75 while (i < 7) : (i += 1) {
76 state.update(tmp, tmp);76 state.update(tmp, tmp);
...@@ -86,7 +86,7 @@ const State128L = struct {...@@ -86,7 +86,7 @@ const State128L = struct {
86/// It was designed to fully exploit the parallelism and built-in AES support of recent Intel and ARM CPUs.86/// It was designed to fully exploit the parallelism and built-in AES support of recent Intel and ARM CPUs.
87///87///
88/// https://competitions.cr.yp.to/round3/aegisv11.pdf88/// https://competitions.cr.yp.to/round3/aegisv11.pdf
89pub const AEGIS128L = struct {89pub const Aegis128L = struct {
90 pub const tag_length = 16;90 pub const tag_length = 16;
91 pub const nonce_length = 16;91 pub const nonce_length = 16;
92 pub const key_length = 16;92 pub const key_length = 16;
...@@ -155,8 +155,8 @@ pub const AEGIS128L = struct {...@@ -155,8 +155,8 @@ pub const AEGIS128L = struct {
155 mem.copy(u8, m[i .. i + m.len % 32], dst[0 .. m.len % 32]);155 mem.copy(u8, m[i .. i + m.len % 32], dst[0 .. m.len % 32]);
156 mem.set(u8, dst[0 .. m.len % 32], 0);156 mem.set(u8, dst[0 .. m.len % 32], 0);
157 const blocks = &state.blocks;157 const blocks = &state.blocks;
158 blocks[0] = blocks[0].xorBlocks(AESBlock.fromBytes(dst[0..16]));158 blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(dst[0..16]));
159 blocks[4] = blocks[4].xorBlocks(AESBlock.fromBytes(dst[16..32]));159 blocks[4] = blocks[4].xorBlocks(AesBlock.fromBytes(dst[16..32]));
160 }160 }
161 const computed_tag = state.mac(ad.len, m.len);161 const computed_tag = state.mac(ad.len, m.len);
162 var acc: u8 = 0;162 var acc: u8 = 0;
...@@ -171,18 +171,18 @@ pub const AEGIS128L = struct {...@@ -171,18 +171,18 @@ pub const AEGIS128L = struct {
171};171};
172172
173const State256 = struct {173const State256 = struct {
174 blocks: [6]AESBlock,174 blocks: [6]AesBlock,
175175
176 fn init(key: [32]u8, nonce: [32]u8) State256 {176 fn init(key: [32]u8, nonce: [32]u8) State256 {
177 const c1 = AESBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd });177 const c1 = AesBlock.fromBytes(&[16]u8{ 0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1, 0x20, 0x11, 0x31, 0x42, 0x73, 0xb5, 0x28, 0xdd });
178 const c2 = AESBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 });178 const c2 = AesBlock.fromBytes(&[16]u8{ 0x0, 0x1, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d, 0x15, 0x22, 0x37, 0x59, 0x90, 0xe9, 0x79, 0x62 });
179 const key_block1 = AESBlock.fromBytes(key[0..16]);179 const key_block1 = AesBlock.fromBytes(key[0..16]);
180 const key_block2 = AESBlock.fromBytes(key[16..32]);180 const key_block2 = AesBlock.fromBytes(key[16..32]);
181 const nonce_block1 = AESBlock.fromBytes(nonce[0..16]);181 const nonce_block1 = AesBlock.fromBytes(nonce[0..16]);
182 const nonce_block2 = AESBlock.fromBytes(nonce[16..32]);182 const nonce_block2 = AesBlock.fromBytes(nonce[16..32]);
183 const kxn1 = key_block1.xorBlocks(nonce_block1);183 const kxn1 = key_block1.xorBlocks(nonce_block1);
184 const kxn2 = key_block2.xorBlocks(nonce_block2);184 const kxn2 = key_block2.xorBlocks(nonce_block2);
185 const blocks = [6]AESBlock{185 const blocks = [6]AesBlock{
186 kxn1,186 kxn1,
187 kxn2,187 kxn2,
188 c1,188 c1,
...@@ -201,7 +201,7 @@ const State256 = struct {...@@ -201,7 +201,7 @@ const State256 = struct {
201 return state;201 return state;
202 }202 }
203203
204 inline fn update(state: *State256, d: AESBlock) void {204 inline fn update(state: *State256, d: AesBlock) void {
205 const blocks = &state.blocks;205 const blocks = &state.blocks;
206 const tmp = blocks[5].encrypt(blocks[0]);206 const tmp = blocks[5].encrypt(blocks[0]);
207 comptime var i: usize = 5;207 comptime var i: usize = 5;
...@@ -213,7 +213,7 @@ const State256 = struct {...@@ -213,7 +213,7 @@ const State256 = struct {
213213
214 fn enc(state: *State256, dst: *[16]u8, src: *const [16]u8) void {214 fn enc(state: *State256, dst: *[16]u8, src: *const [16]u8) void {
215 const blocks = &state.blocks;215 const blocks = &state.blocks;
216 const msg = AESBlock.fromBytes(src);216 const msg = AesBlock.fromBytes(src);
217 var tmp = msg.xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]);217 var tmp = msg.xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]);
218 tmp = tmp.xorBlocks(blocks[2].andBlocks(blocks[3]));218 tmp = tmp.xorBlocks(blocks[2].andBlocks(blocks[3]));
219 dst.* = tmp.toBytes();219 dst.* = tmp.toBytes();
...@@ -222,7 +222,7 @@ const State256 = struct {...@@ -222,7 +222,7 @@ const State256 = struct {
222222
223 fn dec(state: *State256, dst: *[16]u8, src: *const [16]u8) void {223 fn dec(state: *State256, dst: *[16]u8, src: *const [16]u8) void {
224 const blocks = &state.blocks;224 const blocks = &state.blocks;
225 var msg = AESBlock.fromBytes(src).xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]);225 var msg = AesBlock.fromBytes(src).xorBlocks(blocks[5]).xorBlocks(blocks[4]).xorBlocks(blocks[1]);
226 msg = msg.xorBlocks(blocks[2].andBlocks(blocks[3]));226 msg = msg.xorBlocks(blocks[2].andBlocks(blocks[3]));
227 dst.* = msg.toBytes();227 dst.* = msg.toBytes();
228 state.update(msg);228 state.update(msg);
...@@ -233,7 +233,7 @@ const State256 = struct {...@@ -233,7 +233,7 @@ const State256 = struct {
233 var sizes: [16]u8 = undefined;233 var sizes: [16]u8 = undefined;
234 mem.writeIntLittle(u64, sizes[0..8], adlen * 8);234 mem.writeIntLittle(u64, sizes[0..8], adlen * 8);
235 mem.writeIntLittle(u64, sizes[8..16], mlen * 8);235 mem.writeIntLittle(u64, sizes[8..16], mlen * 8);
236 const tmp = AESBlock.fromBytes(&sizes).xorBlocks(blocks[3]);236 const tmp = AesBlock.fromBytes(&sizes).xorBlocks(blocks[3]);
237 var i: usize = 0;237 var i: usize = 0;
238 while (i < 7) : (i += 1) {238 while (i < 7) : (i += 1) {
239 state.update(tmp);239 state.update(tmp);
...@@ -248,7 +248,7 @@ const State256 = struct {...@@ -248,7 +248,7 @@ const State256 = struct {
248/// The 256 bit variant of AEGIS has a 256 bit key, a 256 bit nonce, and processes 128 bit message blocks.248/// The 256 bit variant of AEGIS has a 256 bit key, a 256 bit nonce, and processes 128 bit message blocks.
249///249///
250/// https://competitions.cr.yp.to/round3/aegisv11.pdf250/// https://competitions.cr.yp.to/round3/aegisv11.pdf
251pub const AEGIS256 = struct {251pub const Aegis256 = struct {
252 pub const tag_length = 16;252 pub const tag_length = 16;
253 pub const nonce_length = 32;253 pub const nonce_length = 32;
254 pub const key_length = 32;254 pub const key_length = 32;
...@@ -317,7 +317,7 @@ pub const AEGIS256 = struct {...@@ -317,7 +317,7 @@ pub const AEGIS256 = struct {
317 mem.copy(u8, m[i .. i + m.len % 16], dst[0 .. m.len % 16]);317 mem.copy(u8, m[i .. i + m.len % 16], dst[0 .. m.len % 16]);
318 mem.set(u8, dst[0 .. m.len % 16], 0);318 mem.set(u8, dst[0 .. m.len % 16], 0);
319 const blocks = &state.blocks;319 const blocks = &state.blocks;
320 blocks[0] = blocks[0].xorBlocks(AESBlock.fromBytes(&dst));320 blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(&dst));
321 }321 }
322 const computed_tag = state.mac(ad.len, m.len);322 const computed_tag = state.mac(ad.len, m.len);
323 var acc: u8 = 0;323 var acc: u8 = 0;
...@@ -334,113 +334,113 @@ pub const AEGIS256 = struct {...@@ -334,113 +334,113 @@ pub const AEGIS256 = struct {
334const htest = @import("test.zig");334const htest = @import("test.zig");
335const testing = std.testing;335const testing = std.testing;
336336
337test "AEGIS128L test vector 1" {337test "Aegis128L test vector 1" {
338 const key: [AEGIS128L.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 14;338 const key: [Aegis128L.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 14;
339 const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 13;339 const nonce: [Aegis128L.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 13;
340 const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };340 const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
341 const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };341 const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
342 var c: [m.len]u8 = undefined;342 var c: [m.len]u8 = undefined;
343 var m2: [m.len]u8 = undefined;343 var m2: [m.len]u8 = undefined;
344 var tag: [AEGIS128L.tag_length]u8 = undefined;344 var tag: [Aegis128L.tag_length]u8 = undefined;
345345
346 AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key);346 Aegis128L.encrypt(&c, &tag, &m, &ad, nonce, key);
347 try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key);347 try Aegis128L.decrypt(&m2, &c, tag, &ad, nonce, key);
348 testing.expectEqualSlices(u8, &m, &m2);348 testing.expectEqualSlices(u8, &m, &m2);
349349
350 htest.assertEqual("79d94593d8c2119d7e8fd9b8fc77845c5c077a05b2528b6ac54b563aed8efe84", &c);350 htest.assertEqual("79d94593d8c2119d7e8fd9b8fc77845c5c077a05b2528b6ac54b563aed8efe84", &c);
351 htest.assertEqual("cc6f3372f6aa1bb82388d695c3962d9a", &tag);351 htest.assertEqual("cc6f3372f6aa1bb82388d695c3962d9a", &tag);
352352
353 c[0] +%= 1;353 c[0] +%= 1;
354 testing.expectError(error.AuthenticationFailed, AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key));354 testing.expectError(error.AuthenticationFailed, Aegis128L.decrypt(&m2, &c, tag, &ad, nonce, key));
355 c[0] -%= 1;355 c[0] -%= 1;
356 tag[0] +%= 1;356 tag[0] +%= 1;
357 testing.expectError(error.AuthenticationFailed, AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key));357 testing.expectError(error.AuthenticationFailed, Aegis128L.decrypt(&m2, &c, tag, &ad, nonce, key));
358}358}
359359
360test "AEGIS128L test vector 2" {360test "Aegis128L test vector 2" {
361 const key: [AEGIS128L.key_length]u8 = [_]u8{0x00} ** 16;361 const key: [Aegis128L.key_length]u8 = [_]u8{0x00} ** 16;
362 const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{0x00} ** 16;362 const nonce: [Aegis128L.nonce_length]u8 = [_]u8{0x00} ** 16;
363 const ad = [_]u8{};363 const ad = [_]u8{};
364 const m = [_]u8{0x00} ** 16;364 const m = [_]u8{0x00} ** 16;
365 var c: [m.len]u8 = undefined;365 var c: [m.len]u8 = undefined;
366 var m2: [m.len]u8 = undefined;366 var m2: [m.len]u8 = undefined;
367 var tag: [AEGIS128L.tag_length]u8 = undefined;367 var tag: [Aegis128L.tag_length]u8 = undefined;
368368
369 AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key);369 Aegis128L.encrypt(&c, &tag, &m, &ad, nonce, key);
370 try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key);370 try Aegis128L.decrypt(&m2, &c, tag, &ad, nonce, key);
371 testing.expectEqualSlices(u8, &m, &m2);371 testing.expectEqualSlices(u8, &m, &m2);
372372
373 htest.assertEqual("41de9000a7b5e40e2d68bb64d99ebb19", &c);373 htest.assertEqual("41de9000a7b5e40e2d68bb64d99ebb19", &c);
374 htest.assertEqual("f4d997cc9b94227ada4fe4165422b1c8", &tag);374 htest.assertEqual("f4d997cc9b94227ada4fe4165422b1c8", &tag);
375}375}
376376
377test "AEGIS128L test vector 3" {377test "Aegis128L test vector 3" {
378 const key: [AEGIS128L.key_length]u8 = [_]u8{0x00} ** 16;378 const key: [Aegis128L.key_length]u8 = [_]u8{0x00} ** 16;
379 const nonce: [AEGIS128L.nonce_length]u8 = [_]u8{0x00} ** 16;379 const nonce: [Aegis128L.nonce_length]u8 = [_]u8{0x00} ** 16;
380 const ad = [_]u8{};380 const ad = [_]u8{};
381 const m = [_]u8{};381 const m = [_]u8{};
382 var c: [m.len]u8 = undefined;382 var c: [m.len]u8 = undefined;
383 var m2: [m.len]u8 = undefined;383 var m2: [m.len]u8 = undefined;
384 var tag: [AEGIS128L.tag_length]u8 = undefined;384 var tag: [Aegis128L.tag_length]u8 = undefined;
385385
386 AEGIS128L.encrypt(&c, &tag, &m, &ad, nonce, key);386 Aegis128L.encrypt(&c, &tag, &m, &ad, nonce, key);
387 try AEGIS128L.decrypt(&m2, &c, tag, &ad, nonce, key);387 try Aegis128L.decrypt(&m2, &c, tag, &ad, nonce, key);
388 testing.expectEqualSlices(u8, &m, &m2);388 testing.expectEqualSlices(u8, &m, &m2);
389389
390 htest.assertEqual("83cc600dc4e3e7e62d4055826174f149", &tag);390 htest.assertEqual("83cc600dc4e3e7e62d4055826174f149", &tag);
391}391}
392392
393test "AEGIS256 test vector 1" {393test "Aegis256 test vector 1" {
394 const key: [AEGIS256.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 30;394 const key: [Aegis256.key_length]u8 = [_]u8{ 0x10, 0x01 } ++ [_]u8{0x00} ** 30;
395 const nonce: [AEGIS256.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 29;395 const nonce: [Aegis256.nonce_length]u8 = [_]u8{ 0x10, 0x00, 0x02 } ++ [_]u8{0x00} ** 29;
396 const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };396 const ad = [8]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
397 const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };397 const m = [32]u8{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
398 var c: [m.len]u8 = undefined;398 var c: [m.len]u8 = undefined;
399 var m2: [m.len]u8 = undefined;399 var m2: [m.len]u8 = undefined;
400 var tag: [AEGIS256.tag_length]u8 = undefined;400 var tag: [Aegis256.tag_length]u8 = undefined;
401401
402 AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key);402 Aegis256.encrypt(&c, &tag, &m, &ad, nonce, key);
403 try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key);403 try Aegis256.decrypt(&m2, &c, tag, &ad, nonce, key);
404 testing.expectEqualSlices(u8, &m, &m2);404 testing.expectEqualSlices(u8, &m, &m2);
405405
406 htest.assertEqual("f373079ed84b2709faee373584585d60accd191db310ef5d8b11833df9dec711", &c);406 htest.assertEqual("f373079ed84b2709faee373584585d60accd191db310ef5d8b11833df9dec711", &c);
407 htest.assertEqual("8d86f91ee606e9ff26a01b64ccbdd91d", &tag);407 htest.assertEqual("8d86f91ee606e9ff26a01b64ccbdd91d", &tag);
408408
409 c[0] +%= 1;409 c[0] +%= 1;
410 testing.expectError(error.AuthenticationFailed, AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key));410 testing.expectError(error.AuthenticationFailed, Aegis256.decrypt(&m2, &c, tag, &ad, nonce, key));
411 c[0] -%= 1;411 c[0] -%= 1;
412 tag[0] +%= 1;412 tag[0] +%= 1;
413 testing.expectError(error.AuthenticationFailed, AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key));413 testing.expectError(error.AuthenticationFailed, Aegis256.decrypt(&m2, &c, tag, &ad, nonce, key));
414}414}
415415
416test "AEGIS256 test vector 2" {416test "Aegis256 test vector 2" {
417 const key: [AEGIS256.key_length]u8 = [_]u8{0x00} ** 32;417 const key: [Aegis256.key_length]u8 = [_]u8{0x00} ** 32;
418 const nonce: [AEGIS256.nonce_length]u8 = [_]u8{0x00} ** 32;418 const nonce: [Aegis256.nonce_length]u8 = [_]u8{0x00} ** 32;
419 const ad = [_]u8{};419 const ad = [_]u8{};
420 const m = [_]u8{0x00} ** 16;420 const m = [_]u8{0x00} ** 16;
421 var c: [m.len]u8 = undefined;421 var c: [m.len]u8 = undefined;
422 var m2: [m.len]u8 = undefined;422 var m2: [m.len]u8 = undefined;
423 var tag: [AEGIS256.tag_length]u8 = undefined;423 var tag: [Aegis256.tag_length]u8 = undefined;
424424
425 AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key);425 Aegis256.encrypt(&c, &tag, &m, &ad, nonce, key);
426 try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key);426 try Aegis256.decrypt(&m2, &c, tag, &ad, nonce, key);
427 testing.expectEqualSlices(u8, &m, &m2);427 testing.expectEqualSlices(u8, &m, &m2);
428428
429 htest.assertEqual("b98f03a947807713d75a4fff9fc277a6", &c);429 htest.assertEqual("b98f03a947807713d75a4fff9fc277a6", &c);
430 htest.assertEqual("478f3b50dc478ef7d5cf2d0f7cc13180", &tag);430 htest.assertEqual("478f3b50dc478ef7d5cf2d0f7cc13180", &tag);
431}431}
432432
433test "AEGIS256 test vector 3" {433test "Aegis256 test vector 3" {
434 const key: [AEGIS256.key_length]u8 = [_]u8{0x00} ** 32;434 const key: [Aegis256.key_length]u8 = [_]u8{0x00} ** 32;
435 const nonce: [AEGIS256.nonce_length]u8 = [_]u8{0x00} ** 32;435 const nonce: [Aegis256.nonce_length]u8 = [_]u8{0x00} ** 32;
436 const ad = [_]u8{};436 const ad = [_]u8{};
437 const m = [_]u8{};437 const m = [_]u8{};
438 var c: [m.len]u8 = undefined;438 var c: [m.len]u8 = undefined;
439 var m2: [m.len]u8 = undefined;439 var m2: [m.len]u8 = undefined;
440 var tag: [AEGIS256.tag_length]u8 = undefined;440 var tag: [Aegis256.tag_length]u8 = undefined;
441441
442 AEGIS256.encrypt(&c, &tag, &m, &ad, nonce, key);442 Aegis256.encrypt(&c, &tag, &m, &ad, nonce, key);
443 try AEGIS256.decrypt(&m2, &c, tag, &ad, nonce, key);443 try Aegis256.decrypt(&m2, &c, tag, &ad, nonce, key);
444 testing.expectEqualSlices(u8, &m, &m2);444 testing.expectEqualSlices(u8, &m, &m2);
445445
446 htest.assertEqual("f7a0878f68bd083e8065354071fc27c3", &tag);446 htest.assertEqual("f7a0878f68bd083e8065354071fc27c3", &tag);
lib/std/crypto/aes.zig+14-14
...@@ -21,10 +21,10 @@ impl: {...@@ -21,10 +21,10 @@ impl: {
21};21};
2222
23pub const Block = impl.Block;23pub const Block = impl.Block;
24pub const AESEncryptCtx = impl.AESEncryptCtx;24pub const AesEncryptCtx = impl.AesEncryptCtx;
25pub const AESDecryptCtx = impl.AESDecryptCtx;25pub const AesDecryptCtx = impl.AesDecryptCtx;
26pub const AES128 = impl.AES128;26pub const Aes128 = impl.Aes128;
27pub const AES256 = impl.AES256;27pub const Aes256 = impl.Aes256;
2828
29test "ctr" {29test "ctr" {
30 // NIST SP 800-38A pp 55-5830 // NIST SP 800-38A pp 55-58
...@@ -46,8 +46,8 @@ test "ctr" {...@@ -46,8 +46,8 @@ test "ctr" {
46 };46 };
4747
48 var out: [exp_out.len]u8 = undefined;48 var out: [exp_out.len]u8 = undefined;
49 var ctx = AES128.initEnc(key);49 var ctx = Aes128.initEnc(key);
50 ctr(AESEncryptCtx(AES128), ctx, out[0..], in[0..], iv, builtin.Endian.Big);50 ctr(AesEncryptCtx(Aes128), ctx, out[0..], in[0..], iv, builtin.Endian.Big);
51 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);51 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
52}52}
5353
...@@ -59,7 +59,7 @@ test "encrypt" {...@@ -59,7 +59,7 @@ test "encrypt" {
59 const exp_out = [_]u8{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 };59 const exp_out = [_]u8{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 };
6060
61 var out: [exp_out.len]u8 = undefined;61 var out: [exp_out.len]u8 = undefined;
62 var ctx = AES128.initEnc(key);62 var ctx = Aes128.initEnc(key);
63 ctx.encrypt(out[0..], in[0..]);63 ctx.encrypt(out[0..], in[0..]);
64 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);64 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
65 }65 }
...@@ -74,7 +74,7 @@ test "encrypt" {...@@ -74,7 +74,7 @@ test "encrypt" {
74 const exp_out = [_]u8{ 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };74 const exp_out = [_]u8{ 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };
7575
76 var out: [exp_out.len]u8 = undefined;76 var out: [exp_out.len]u8 = undefined;
77 var ctx = AES256.initEnc(key);77 var ctx = Aes256.initEnc(key);
78 ctx.encrypt(out[0..], in[0..]);78 ctx.encrypt(out[0..], in[0..]);
79 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);79 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
80 }80 }
...@@ -88,7 +88,7 @@ test "decrypt" {...@@ -88,7 +88,7 @@ test "decrypt" {
88 const exp_out = [_]u8{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 };88 const exp_out = [_]u8{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 };
8989
90 var out: [exp_out.len]u8 = undefined;90 var out: [exp_out.len]u8 = undefined;
91 var ctx = AES128.initDec(key);91 var ctx = Aes128.initDec(key);
92 ctx.decrypt(out[0..], in[0..]);92 ctx.decrypt(out[0..], in[0..]);
93 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);93 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
94 }94 }
...@@ -103,7 +103,7 @@ test "decrypt" {...@@ -103,7 +103,7 @@ test "decrypt" {
103 const exp_out = [_]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };103 const exp_out = [_]u8{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
104104
105 var out: [exp_out.len]u8 = undefined;105 var out: [exp_out.len]u8 = undefined;
106 var ctx = AES256.initDec(key);106 var ctx = Aes256.initDec(key);
107 ctx.decrypt(out[0..], in[0..]);107 ctx.decrypt(out[0..], in[0..]);
108 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);108 testing.expectEqualSlices(u8, exp_out[0..], out[0..]);
109 }109 }
...@@ -117,8 +117,8 @@ test "expand 128-bit key" {...@@ -117,8 +117,8 @@ test "expand 128-bit key" {
117 const exp_dec = [_]*const [32:0]u8{117 const exp_dec = [_]*const [32:0]u8{
118 "2b7e151628aed2a6abf7158809cf4f3c", "a0fafe1788542cb123a339392a6c7605", "f2c295f27a96b9435935807a7359f67f", "3d80477d4716fe3e1e237e446d7a883b", "ef44a541a8525b7fb671253bdb0bad00", "d4d1c6f87c839d87caf2b8bc11f915bc", "6d88a37a110b3efddbf98641ca0093fd", "4e54f70e5f5fc9f384a64fb24ea6dc4f", "ead27321b58dbad2312bf5607f8d292f", "ac7766f319fadc2128d12941575c006e", "d014f9a8c9ee2589e13f0cc8b6630ca6",118 "2b7e151628aed2a6abf7158809cf4f3c", "a0fafe1788542cb123a339392a6c7605", "f2c295f27a96b9435935807a7359f67f", "3d80477d4716fe3e1e237e446d7a883b", "ef44a541a8525b7fb671253bdb0bad00", "d4d1c6f87c839d87caf2b8bc11f915bc", "6d88a37a110b3efddbf98641ca0093fd", "4e54f70e5f5fc9f384a64fb24ea6dc4f", "ead27321b58dbad2312bf5607f8d292f", "ac7766f319fadc2128d12941575c006e", "d014f9a8c9ee2589e13f0cc8b6630ca6",
119 };119 };
120 const enc = AES128.initEnc(key);120 const enc = Aes128.initEnc(key);
121 const dec = AES128.initDec(key);121 const dec = Aes128.initDec(key);
122 var exp: [16]u8 = undefined;122 var exp: [16]u8 = undefined;
123123
124 for (enc.key_schedule.round_keys) |round_key, i| {124 for (enc.key_schedule.round_keys) |round_key, i| {
...@@ -139,8 +139,8 @@ test "expand 256-bit key" {...@@ -139,8 +139,8 @@ test "expand 256-bit key" {
139 const exp_dec = [_]*const [32:0]u8{139 const exp_dec = [_]*const [32:0]u8{
140 "fe4890d1e6188d0b046df344706c631e", "ada23f4963e23b2455427c8a5c709104", "57c96cf6074f07c0706abb07137f9241", "b668b621ce40046d36a047ae0932ed8e", "34ad1e4450866b367725bcc763152946", "32526c367828b24cf8e043c33f92aa20", "c440b289642b757227a3d7f114309581", "d669a7334a7ade7a80c8f18fc772e9e3", "25ba3c22a06bc7fb4388a28333934270", "54fb808b9c137949cab22ff547ba186c", "6c3d632985d1fbd9e3e36578701be0f3", "4a7459f9c8e8f9c256a156bc8d083799", "42107758e9ec98f066329ea193f8858b", "8ec6bff6829ca03b9e49af7edba96125", "603deb1015ca71be2b73aef0857d7781",140 "fe4890d1e6188d0b046df344706c631e", "ada23f4963e23b2455427c8a5c709104", "57c96cf6074f07c0706abb07137f9241", "b668b621ce40046d36a047ae0932ed8e", "34ad1e4450866b367725bcc763152946", "32526c367828b24cf8e043c33f92aa20", "c440b289642b757227a3d7f114309581", "d669a7334a7ade7a80c8f18fc772e9e3", "25ba3c22a06bc7fb4388a28333934270", "54fb808b9c137949cab22ff547ba186c", "6c3d632985d1fbd9e3e36578701be0f3", "4a7459f9c8e8f9c256a156bc8d083799", "42107758e9ec98f066329ea193f8858b", "8ec6bff6829ca03b9e49af7edba96125", "603deb1015ca71be2b73aef0857d7781",
141 };141 };
142 const enc = AES256.initEnc(key);142 const enc = Aes256.initEnc(key);
143 const dec = AES256.initDec(key);143 const dec = Aes256.initDec(key);
144 var exp: [16]u8 = undefined;144 var exp: [16]u8 = undefined;
145145
146 for (enc.key_schedule.round_keys) |round_key, i| {146 for (enc.key_schedule.round_keys) |round_key, i| {
lib/std/crypto/aes/aesni.zig+33-33
...@@ -13,7 +13,7 @@ const BlockVec = Vector(2, u64);...@@ -13,7 +13,7 @@ const BlockVec = Vector(2, u64);
1313
14/// A single AES block.14/// A single AES block.
15pub const Block = struct {15pub const Block = struct {
16 pub const block_size: usize = 16;16 pub const block_length: usize = 16;
1717
18 /// Internal representation of a block.18 /// Internal representation of a block.
19 repr: BlockVec,19 repr: BlockVec,
...@@ -165,9 +165,9 @@ pub const Block = struct {...@@ -165,9 +165,9 @@ pub const Block = struct {
165 };165 };
166};166};
167167
168fn KeySchedule(comptime AES: type) type {168fn KeySchedule(comptime Aes: type) type {
169 std.debug.assert(AES.rounds == 10 or AES.rounds == 14);169 std.debug.assert(Aes.rounds == 10 or Aes.rounds == 14);
170 const rounds = AES.rounds;170 const rounds = Aes.rounds;
171171
172 return struct {172 return struct {
173 const Self = @This();173 const Self = @This();
...@@ -243,24 +243,24 @@ fn KeySchedule(comptime AES: type) type {...@@ -243,24 +243,24 @@ fn KeySchedule(comptime AES: type) type {
243}243}
244244
245/// A context to perform encryption using the standard AES key schedule.245/// A context to perform encryption using the standard AES key schedule.
246pub fn AESEncryptCtx(comptime AES: type) type {246pub fn AesEncryptCtx(comptime Aes: type) type {
247 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);247 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
248 const rounds = AES.rounds;248 const rounds = Aes.rounds;
249249
250 return struct {250 return struct {
251 const Self = @This();251 const Self = @This();
252 pub const block = AES.block;252 pub const block = Aes.block;
253 pub const block_size = block.block_size;253 pub const block_length = block.block_length;
254 key_schedule: KeySchedule(AES),254 key_schedule: KeySchedule(Aes),
255255
256 /// Create a new encryption context with the given key.256 /// Create a new encryption context with the given key.
257 pub fn init(key: [AES.key_bits / 8]u8) Self {257 pub fn init(key: [Aes.key_bits / 8]u8) Self {
258 var t1 = Block.fromBytes(key[0..16]);258 var t1 = Block.fromBytes(key[0..16]);
259 const key_schedule = if (AES.key_bits == 128) ks: {259 const key_schedule = if (Aes.key_bits == 128) ks: {
260 break :ks KeySchedule(AES).expand128(&t1);260 break :ks KeySchedule(Aes).expand128(&t1);
261 } else ks: {261 } else ks: {
262 var t2 = Block.fromBytes(key[16..32]);262 var t2 = Block.fromBytes(key[16..32]);
263 break :ks KeySchedule(AES).expand256(&t1, &t2);263 break :ks KeySchedule(Aes).expand256(&t1, &t2);
264 };264 };
265 return Self{265 return Self{
266 .key_schedule = key_schedule,266 .key_schedule = key_schedule,
...@@ -335,26 +335,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {...@@ -335,26 +335,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {
335}335}
336336
337/// A context to perform decryption using the standard AES key schedule.337/// A context to perform decryption using the standard AES key schedule.
338pub fn AESDecryptCtx(comptime AES: type) type {338pub fn AesDecryptCtx(comptime Aes: type) type {
339 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);339 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
340 const rounds = AES.rounds;340 const rounds = Aes.rounds;
341341
342 return struct {342 return struct {
343 const Self = @This();343 const Self = @This();
344 pub const block = AES.block;344 pub const block = Aes.block;
345 pub const block_size = block.block_size;345 pub const block_length = block.block_length;
346 key_schedule: KeySchedule(AES),346 key_schedule: KeySchedule(Aes),
347347
348 /// Create a decryption context from an existing encryption context.348 /// Create a decryption context from an existing encryption context.
349 pub fn initFromEnc(ctx: AESEncryptCtx(AES)) Self {349 pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self {
350 return Self{350 return Self{
351 .key_schedule = ctx.key_schedule.invert(),351 .key_schedule = ctx.key_schedule.invert(),
352 };352 };
353 }353 }
354354
355 /// Create a new decryption context with the given key.355 /// Create a new decryption context with the given key.
356 pub fn init(key: [AES.key_bits / 8]u8) Self {356 pub fn init(key: [Aes.key_bits / 8]u8) Self {
357 const enc_ctx = AESEncryptCtx(AES).init(key);357 const enc_ctx = AesEncryptCtx(Aes).init(key);
358 return initFromEnc(enc_ctx);358 return initFromEnc(enc_ctx);
359 }359 }
360360
...@@ -395,35 +395,35 @@ pub fn AESDecryptCtx(comptime AES: type) type {...@@ -395,35 +395,35 @@ pub fn AESDecryptCtx(comptime AES: type) type {
395}395}
396396
397/// AES-128 with the standard key schedule.397/// AES-128 with the standard key schedule.
398pub const AES128 = struct {398pub const Aes128 = struct {
399 pub const key_bits: usize = 128;399 pub const key_bits: usize = 128;
400 pub const rounds = ((key_bits - 64) / 32 + 8);400 pub const rounds = ((key_bits - 64) / 32 + 8);
401 pub const block = Block;401 pub const block = Block;
402402
403 /// Create a new context for encryption.403 /// Create a new context for encryption.
404 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES128) {404 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128) {
405 return AESEncryptCtx(AES128).init(key);405 return AesEncryptCtx(Aes128).init(key);
406 }406 }
407407
408 /// Create a new context for decryption.408 /// Create a new context for decryption.
409 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES128) {409 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128) {
410 return AESDecryptCtx(AES128).init(key);410 return AesDecryptCtx(Aes128).init(key);
411 }411 }
412};412};
413413
414/// AES-256 with the standard key schedule.414/// AES-256 with the standard key schedule.
415pub const AES256 = struct {415pub const Aes256 = struct {
416 pub const key_bits: usize = 256;416 pub const key_bits: usize = 256;
417 pub const rounds = ((key_bits - 64) / 32 + 8);417 pub const rounds = ((key_bits - 64) / 32 + 8);
418 pub const block = Block;418 pub const block = Block;
419419
420 /// Create a new context for encryption.420 /// Create a new context for encryption.
421 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES256) {421 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256) {
422 return AESEncryptCtx(AES256).init(key);422 return AesEncryptCtx(Aes256).init(key);
423 }423 }
424424
425 /// Create a new context for decryption.425 /// Create a new context for decryption.
426 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES256) {426 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256) {
427 return AESDecryptCtx(AES256).init(key);427 return AesDecryptCtx(Aes256).init(key);
428 }428 }
429};429};
lib/std/crypto/aes/armcrypto.zig+33-33
...@@ -13,7 +13,7 @@ const BlockVec = Vector(2, u64);...@@ -13,7 +13,7 @@ const BlockVec = Vector(2, u64);
1313
14/// A single AES block.14/// A single AES block.
15pub const Block = struct {15pub const Block = struct {
16 pub const block_size: usize = 16;16 pub const block_length: usize = 16;
1717
18 /// Internal representation of a block.18 /// Internal representation of a block.
19 repr: BlockVec,19 repr: BlockVec,
...@@ -181,9 +181,9 @@ pub const Block = struct {...@@ -181,9 +181,9 @@ pub const Block = struct {
181 };181 };
182};182};
183183
184fn KeySchedule(comptime AES: type) type {184fn KeySchedule(comptime Aes: type) type {
185 std.debug.assert(AES.rounds == 10 or AES.rounds == 14);185 std.debug.assert(Aes.rounds == 10 or Aes.rounds == 14);
186 const rounds = AES.rounds;186 const rounds = Aes.rounds;
187187
188 return struct {188 return struct {
189 const Self = @This();189 const Self = @This();
...@@ -304,24 +304,24 @@ fn KeySchedule(comptime AES: type) type {...@@ -304,24 +304,24 @@ fn KeySchedule(comptime AES: type) type {
304}304}
305305
306/// A context to perform encryption using the standard AES key schedule.306/// A context to perform encryption using the standard AES key schedule.
307pub fn AESEncryptCtx(comptime AES: type) type {307pub fn AesEncryptCtx(comptime Aes: type) type {
308 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);308 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
309 const rounds = AES.rounds;309 const rounds = Aes.rounds;
310310
311 return struct {311 return struct {
312 const Self = @This();312 const Self = @This();
313 pub const block = AES.block;313 pub const block = Aes.block;
314 pub const block_size = block.block_size;314 pub const block_length = block.block_length;
315 key_schedule: KeySchedule(AES),315 key_schedule: KeySchedule(Aes),
316316
317 /// Create a new encryption context with the given key.317 /// Create a new encryption context with the given key.
318 pub fn init(key: [AES.key_bits / 8]u8) Self {318 pub fn init(key: [Aes.key_bits / 8]u8) Self {
319 var t1 = Block.fromBytes(key[0..16]);319 var t1 = Block.fromBytes(key[0..16]);
320 const key_schedule = if (AES.key_bits == 128) ks: {320 const key_schedule = if (Aes.key_bits == 128) ks: {
321 break :ks KeySchedule(AES).expand128(&t1);321 break :ks KeySchedule(Aes).expand128(&t1);
322 } else ks: {322 } else ks: {
323 var t2 = Block.fromBytes(key[16..32]);323 var t2 = Block.fromBytes(key[16..32]);
324 break :ks KeySchedule(AES).expand256(&t1, &t2);324 break :ks KeySchedule(Aes).expand256(&t1, &t2);
325 };325 };
326 return Self{326 return Self{
327 .key_schedule = key_schedule,327 .key_schedule = key_schedule,
...@@ -396,26 +396,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {...@@ -396,26 +396,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {
396}396}
397397
398/// A context to perform decryption using the standard AES key schedule.398/// A context to perform decryption using the standard AES key schedule.
399pub fn AESDecryptCtx(comptime AES: type) type {399pub fn AesDecryptCtx(comptime Aes: type) type {
400 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);400 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
401 const rounds = AES.rounds;401 const rounds = Aes.rounds;
402402
403 return struct {403 return struct {
404 const Self = @This();404 const Self = @This();
405 pub const block = AES.block;405 pub const block = Aes.block;
406 pub const block_size = block.block_size;406 pub const block_length = block.block_length;
407 key_schedule: KeySchedule(AES),407 key_schedule: KeySchedule(Aes),
408408
409 /// Create a decryption context from an existing encryption context.409 /// Create a decryption context from an existing encryption context.
410 pub fn initFromEnc(ctx: AESEncryptCtx(AES)) Self {410 pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self {
411 return Self{411 return Self{
412 .key_schedule = ctx.key_schedule.invert(),412 .key_schedule = ctx.key_schedule.invert(),
413 };413 };
414 }414 }
415415
416 /// Create a new decryption context with the given key.416 /// Create a new decryption context with the given key.
417 pub fn init(key: [AES.key_bits / 8]u8) Self {417 pub fn init(key: [Aes.key_bits / 8]u8) Self {
418 const enc_ctx = AESEncryptCtx(AES).init(key);418 const enc_ctx = AesEncryptCtx(Aes).init(key);
419 return initFromEnc(enc_ctx);419 return initFromEnc(enc_ctx);
420 }420 }
421421
...@@ -456,35 +456,35 @@ pub fn AESDecryptCtx(comptime AES: type) type {...@@ -456,35 +456,35 @@ pub fn AESDecryptCtx(comptime AES: type) type {
456}456}
457457
458/// AES-128 with the standard key schedule.458/// AES-128 with the standard key schedule.
459pub const AES128 = struct {459pub const Aes128 = struct {
460 pub const key_bits: usize = 128;460 pub const key_bits: usize = 128;
461 pub const rounds = ((key_bits - 64) / 32 + 8);461 pub const rounds = ((key_bits - 64) / 32 + 8);
462 pub const block = Block;462 pub const block = Block;
463463
464 /// Create a new context for encryption.464 /// Create a new context for encryption.
465 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES128) {465 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128) {
466 return AESEncryptCtx(AES128).init(key);466 return AesEncryptCtx(Aes128).init(key);
467 }467 }
468468
469 /// Create a new context for decryption.469 /// Create a new context for decryption.
470 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES128) {470 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128) {
471 return AESDecryptCtx(AES128).init(key);471 return AesDecryptCtx(Aes128).init(key);
472 }472 }
473};473};
474474
475/// AES-256 with the standard key schedule.475/// AES-256 with the standard key schedule.
476pub const AES256 = struct {476pub const Aes256 = struct {
477 pub const key_bits: usize = 256;477 pub const key_bits: usize = 256;
478 pub const rounds = ((key_bits - 64) / 32 + 8);478 pub const rounds = ((key_bits - 64) / 32 + 8);
479 pub const block = Block;479 pub const block = Block;
480480
481 /// Create a new context for encryption.481 /// Create a new context for encryption.
482 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES256) {482 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256) {
483 return AESEncryptCtx(AES256).init(key);483 return AesEncryptCtx(Aes256).init(key);
484 }484 }
485485
486 /// Create a new context for decryption.486 /// Create a new context for decryption.
487 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES256) {487 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256) {
488 return AESDecryptCtx(AES256).init(key);488 return AesDecryptCtx(Aes256).init(key);
489 }489 }
490};490};
lib/std/crypto/aes/soft.zig+34-34
...@@ -12,7 +12,7 @@ const BlockVec = [4]u32;...@@ -12,7 +12,7 @@ const BlockVec = [4]u32;
1212
13/// A single AES block.13/// A single AES block.
14pub const Block = struct {14pub const Block = struct {
15 pub const block_size: usize = 16;15 pub const block_length: usize = 16;
1616
17 /// Internal representation of a block.17 /// Internal representation of a block.
18 repr: BlockVec align(16),18 repr: BlockVec align(16),
...@@ -222,19 +222,19 @@ pub const Block = struct {...@@ -222,19 +222,19 @@ pub const Block = struct {
222 };222 };
223};223};
224224
225fn KeySchedule(comptime AES: type) type {225fn KeySchedule(comptime Aes: type) type {
226 std.debug.assert(AES.rounds == 10 or AES.rounds == 14);226 std.debug.assert(Aes.rounds == 10 or Aes.rounds == 14);
227 const key_size = AES.key_bits / 8;227 const key_length = Aes.key_bits / 8;
228 const rounds = AES.rounds;228 const rounds = Aes.rounds;
229229
230 return struct {230 return struct {
231 const Self = @This();231 const Self = @This();
232 const words_in_key = key_size / 4;232 const words_in_key = key_length / 4;
233233
234 round_keys: [rounds + 1]Block,234 round_keys: [rounds + 1]Block,
235235
236 // Key expansion algorithm. See FIPS-197, Figure 11.236 // Key expansion algorithm. See FIPS-197, Figure 11.
237 fn expandKey(key: [key_size]u8) Self {237 fn expandKey(key: [key_length]u8) Self {
238 const subw = struct {238 const subw = struct {
239 // Apply sbox0 to each byte in w.239 // Apply sbox0 to each byte in w.
240 fn func(w: u32) u32 {240 fn func(w: u32) u32 {
...@@ -282,19 +282,19 @@ fn KeySchedule(comptime AES: type) type {...@@ -282,19 +282,19 @@ fn KeySchedule(comptime AES: type) type {
282}282}
283283
284/// A context to perform encryption using the standard AES key schedule.284/// A context to perform encryption using the standard AES key schedule.
285pub fn AESEncryptCtx(comptime AES: type) type {285pub fn AesEncryptCtx(comptime Aes: type) type {
286 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);286 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
287 const rounds = AES.rounds;287 const rounds = Aes.rounds;
288288
289 return struct {289 return struct {
290 const Self = @This();290 const Self = @This();
291 pub const block = AES.block;291 pub const block = Aes.block;
292 pub const block_size = block.block_size;292 pub const block_length = block.block_length;
293 key_schedule: KeySchedule(AES),293 key_schedule: KeySchedule(Aes),
294294
295 /// Create a new encryption context with the given key.295 /// Create a new encryption context with the given key.
296 pub fn init(key: [AES.key_bits / 8]u8) Self {296 pub fn init(key: [Aes.key_bits / 8]u8) Self {
297 const key_schedule = KeySchedule(AES).expandKey(key);297 const key_schedule = KeySchedule(Aes).expandKey(key);
298 return Self{298 return Self{
299 .key_schedule = key_schedule,299 .key_schedule = key_schedule,
300 };300 };
...@@ -343,26 +343,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {...@@ -343,26 +343,26 @@ pub fn AESEncryptCtx(comptime AES: type) type {
343}343}
344344
345/// A context to perform decryption using the standard AES key schedule.345/// A context to perform decryption using the standard AES key schedule.
346pub fn AESDecryptCtx(comptime AES: type) type {346pub fn AesDecryptCtx(comptime Aes: type) type {
347 std.debug.assert(AES.key_bits == 128 or AES.key_bits == 256);347 std.debug.assert(Aes.key_bits == 128 or Aes.key_bits == 256);
348 const rounds = AES.rounds;348 const rounds = Aes.rounds;
349349
350 return struct {350 return struct {
351 const Self = @This();351 const Self = @This();
352 pub const block = AES.block;352 pub const block = Aes.block;
353 pub const block_size = block.block_size;353 pub const block_length = block.block_length;
354 key_schedule: KeySchedule(AES),354 key_schedule: KeySchedule(Aes),
355355
356 /// Create a decryption context from an existing encryption context.356 /// Create a decryption context from an existing encryption context.
357 pub fn initFromEnc(ctx: AESEncryptCtx(AES)) Self {357 pub fn initFromEnc(ctx: AesEncryptCtx(Aes)) Self {
358 return Self{358 return Self{
359 .key_schedule = ctx.key_schedule.invert(),359 .key_schedule = ctx.key_schedule.invert(),
360 };360 };
361 }361 }
362362
363 /// Create a new decryption context with the given key.363 /// Create a new decryption context with the given key.
364 pub fn init(key: [AES.key_bits / 8]u8) Self {364 pub fn init(key: [Aes.key_bits / 8]u8) Self {
365 const enc_ctx = AESEncryptCtx(AES).init(key);365 const enc_ctx = AesEncryptCtx(Aes).init(key);
366 return initFromEnc(enc_ctx);366 return initFromEnc(enc_ctx);
367 }367 }
368368
...@@ -389,36 +389,36 @@ pub fn AESDecryptCtx(comptime AES: type) type {...@@ -389,36 +389,36 @@ pub fn AESDecryptCtx(comptime AES: type) type {
389}389}
390390
391/// AES-128 with the standard key schedule.391/// AES-128 with the standard key schedule.
392pub const AES128 = struct {392pub const Aes128 = struct {
393 pub const key_bits: usize = 128;393 pub const key_bits: usize = 128;
394 pub const rounds = ((key_bits - 64) / 32 + 8);394 pub const rounds = ((key_bits - 64) / 32 + 8);
395 pub const block = Block;395 pub const block = Block;
396396
397 /// Create a new context for encryption.397 /// Create a new context for encryption.
398 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES128) {398 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes128) {
399 return AESEncryptCtx(AES128).init(key);399 return AesEncryptCtx(Aes128).init(key);
400 }400 }
401401
402 /// Create a new context for decryption.402 /// Create a new context for decryption.
403 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES128) {403 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes128) {
404 return AESDecryptCtx(AES128).init(key);404 return AesDecryptCtx(Aes128).init(key);
405 }405 }
406};406};
407407
408/// AES-256 with the standard key schedule.408/// AES-256 with the standard key schedule.
409pub const AES256 = struct {409pub const Aes256 = struct {
410 pub const key_bits: usize = 256;410 pub const key_bits: usize = 256;
411 pub const rounds = ((key_bits - 64) / 32 + 8);411 pub const rounds = ((key_bits - 64) / 32 + 8);
412 pub const block = Block;412 pub const block = Block;
413413
414 /// Create a new context for encryption.414 /// Create a new context for encryption.
415 pub fn initEnc(key: [key_bits / 8]u8) AESEncryptCtx(AES256) {415 pub fn initEnc(key: [key_bits / 8]u8) AesEncryptCtx(Aes256) {
416 return AESEncryptCtx(AES256).init(key);416 return AesEncryptCtx(Aes256).init(key);
417 }417 }
418418
419 /// Create a new context for decryption.419 /// Create a new context for decryption.
420 pub fn initDec(key: [key_bits / 8]u8) AESDecryptCtx(AES256) {420 pub fn initDec(key: [key_bits / 8]u8) AesDecryptCtx(Aes256) {
421 return AESDecryptCtx(AES256).init(key);421 return AesDecryptCtx(Aes256).init(key);
422 }422 }
423};423};
424424
lib/std/crypto/aes_gcm.zig+29-29
...@@ -7,16 +7,16 @@ const Ghash = std.crypto.onetimeauth.Ghash;...@@ -7,16 +7,16 @@ const Ghash = std.crypto.onetimeauth.Ghash;
7const mem = std.mem;7const mem = std.mem;
8const modes = crypto.core.modes;8const modes = crypto.core.modes;
99
10pub const AES128GCM = AESGCM(crypto.core.aes.AES128);10pub const Aes128Gcm = AesGcm(crypto.core.aes.Aes128);
11pub const AES256GCM = AESGCM(crypto.core.aes.AES256);11pub const Aes256Gcm = AesGcm(crypto.core.aes.Aes256);
1212
13fn AESGCM(comptime AES: anytype) type {13fn AesGcm(comptime Aes: anytype) type {
14 debug.assert(AES.block.block_size == 16);14 debug.assert(Aes.block.block_length == 16);
1515
16 return struct {16 return struct {
17 pub const tag_length = 16;17 pub const tag_length = 16;
18 pub const nonce_length = 12;18 pub const nonce_length = 12;
19 pub const key_length = AES.key_bits / 8;19 pub const key_length = Aes.key_bits / 8;
2020
21 const zeros = [_]u8{0} ** 16;21 const zeros = [_]u8{0} ** 16;
2222
...@@ -24,7 +24,7 @@ fn AESGCM(comptime AES: anytype) type {...@@ -24,7 +24,7 @@ fn AESGCM(comptime AES: anytype) type {
24 debug.assert(c.len == m.len);24 debug.assert(c.len == m.len);
25 debug.assert(m.len <= 16 * ((1 << 32) - 2));25 debug.assert(m.len <= 16 * ((1 << 32) - 2));
2626
27 const aes = AES.initEnc(key);27 const aes = Aes.initEnc(key);
28 var h: [16]u8 = undefined;28 var h: [16]u8 = undefined;
29 aes.encrypt(&h, &zeros);29 aes.encrypt(&h, &zeros);
3030
...@@ -56,7 +56,7 @@ fn AESGCM(comptime AES: anytype) type {...@@ -56,7 +56,7 @@ fn AESGCM(comptime AES: anytype) type {
56 pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) !void {56 pub fn decrypt(m: []u8, c: []const u8, tag: [tag_length]u8, ad: []const u8, npub: [nonce_length]u8, key: [key_length]u8) !void {
57 assert(c.len == m.len);57 assert(c.len == m.len);
5858
59 const aes = AES.initEnc(key);59 const aes = Aes.initEnc(key);
60 var h: [16]u8 = undefined;60 var h: [16]u8 = undefined;
61 aes.encrypt(&h, &zeros);61 aes.encrypt(&h, &zeros);
6262
...@@ -101,59 +101,59 @@ fn AESGCM(comptime AES: anytype) type {...@@ -101,59 +101,59 @@ fn AESGCM(comptime AES: anytype) type {
101const htest = @import("test.zig");101const htest = @import("test.zig");
102const testing = std.testing;102const testing = std.testing;
103103
104test "AES256GCM - Empty message and no associated data" {104test "Aes256Gcm - Empty message and no associated data" {
105 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;105 const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
106 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;106 const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
107 const ad = "";107 const ad = "";
108 const m = "";108 const m = "";
109 var c: [m.len]u8 = undefined;109 var c: [m.len]u8 = undefined;
110 var m2: [m.len]u8 = undefined;110 var m2: [m.len]u8 = undefined;
111 var tag: [AES256GCM.tag_length]u8 = undefined;111 var tag: [Aes256Gcm.tag_length]u8 = undefined;
112112
113 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);113 Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
114 htest.assertEqual("6b6ff610a16fa4cd59f1fb7903154e92", &tag);114 htest.assertEqual("6b6ff610a16fa4cd59f1fb7903154e92", &tag);
115}115}
116116
117test "AES256GCM - Associated data only" {117test "Aes256Gcm - Associated data only" {
118 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;118 const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
119 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;119 const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
120 const m = "";120 const m = "";
121 const ad = "Test with associated data";121 const ad = "Test with associated data";
122 var c: [m.len]u8 = undefined;122 var c: [m.len]u8 = undefined;
123 var tag: [AES256GCM.tag_length]u8 = undefined;123 var tag: [Aes256Gcm.tag_length]u8 = undefined;
124124
125 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);125 Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
126 htest.assertEqual("262ed164c2dfb26e080a9d108dd9dd4c", &tag);126 htest.assertEqual("262ed164c2dfb26e080a9d108dd9dd4c", &tag);
127}127}
128128
129test "AES256GCM - Message only" {129test "Aes256Gcm - Message only" {
130 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;130 const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
131 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;131 const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
132 const m = "Test with message only";132 const m = "Test with message only";
133 const ad = "";133 const ad = "";
134 var c: [m.len]u8 = undefined;134 var c: [m.len]u8 = undefined;
135 var m2: [m.len]u8 = undefined;135 var m2: [m.len]u8 = undefined;
136 var tag: [AES256GCM.tag_length]u8 = undefined;136 var tag: [Aes256Gcm.tag_length]u8 = undefined;
137137
138 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);138 Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
139 try AES256GCM.decrypt(&m2, &c, tag, ad, nonce, key);139 try Aes256Gcm.decrypt(&m2, &c, tag, ad, nonce, key);
140 testing.expectEqualSlices(u8, m[0..], m2[0..]);140 testing.expectEqualSlices(u8, m[0..], m2[0..]);
141141
142 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01d539472f7c", &c);142 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01d539472f7c", &c);
143 htest.assertEqual("07cd7fc9103e2f9e9bf2dfaa319caff4", &tag);143 htest.assertEqual("07cd7fc9103e2f9e9bf2dfaa319caff4", &tag);
144}144}
145145
146test "AES256GCM - Message and associated data" {146test "Aes256Gcm - Message and associated data" {
147 const key: [AES256GCM.key_length]u8 = [_]u8{0x69} ** AES256GCM.key_length;147 const key: [Aes256Gcm.key_length]u8 = [_]u8{0x69} ** Aes256Gcm.key_length;
148 const nonce: [AES256GCM.nonce_length]u8 = [_]u8{0x42} ** AES256GCM.nonce_length;148 const nonce: [Aes256Gcm.nonce_length]u8 = [_]u8{0x42} ** Aes256Gcm.nonce_length;
149 const m = "Test with message";149 const m = "Test with message";
150 const ad = "Test with associated data";150 const ad = "Test with associated data";
151 var c: [m.len]u8 = undefined;151 var c: [m.len]u8 = undefined;
152 var m2: [m.len]u8 = undefined;152 var m2: [m.len]u8 = undefined;
153 var tag: [AES256GCM.tag_length]u8 = undefined;153 var tag: [Aes256Gcm.tag_length]u8 = undefined;
154154
155 AES256GCM.encrypt(&c, &tag, m, ad, nonce, key);155 Aes256Gcm.encrypt(&c, &tag, m, ad, nonce, key);
156 try AES256GCM.decrypt(&m2, &c, tag, ad, nonce, key);156 try Aes256Gcm.decrypt(&m2, &c, tag, ad, nonce, key);
157 testing.expectEqualSlices(u8, m[0..], m2[0..]);157 testing.expectEqualSlices(u8, m[0..], m2[0..]);
158158
159 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01", &c);159 htest.assertEqual("5ca1642d90009fea33d01f78cf6eefaf01", &c);
lib/std/crypto/benchmark.zig+20-20
...@@ -73,7 +73,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {...@@ -73,7 +73,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
73 var in: [512 * KiB]u8 = undefined;73 var in: [512 * KiB]u8 = undefined;
74 prng.random.bytes(in[0..]);74 prng.random.bytes(in[0..]);
7575
76 const key_length = if (Mac.minimum_key_length == 0) 32 else Mac.minimum_key_length;76 const key_length = if (Mac.key_length == 0) 32 else Mac.key_length;
77 var key: [key_length]u8 = undefined;77 var key: [key_length]u8 = undefined;
78 prng.random.bytes(key[0..]);78 prng.random.bytes(key[0..]);
7979
...@@ -96,12 +96,12 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {...@@ -96,12 +96,12 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
96const exchanges = [_]Crypto{Crypto{ .ty = crypto.dh.X25519, .name = "x25519" }};96const exchanges = [_]Crypto{Crypto{ .ty = crypto.dh.X25519, .name = "x25519" }};
9797
98pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_count: comptime_int) !u64 {98pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_count: comptime_int) !u64 {
99 std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length);99 std.debug.assert(DhKeyExchange.key_length >= DhKeyExchange.secret_length);
100100
101 var in: [DhKeyExchange.minimum_key_length]u8 = undefined;101 var in: [DhKeyExchange.key_length]u8 = undefined;
102 prng.random.bytes(in[0..]);102 prng.random.bytes(in[0..]);
103103
104 var out: [DhKeyExchange.minimum_key_length]u8 = undefined;104 var out: [DhKeyExchange.key_length]u8 = undefined;
105 prng.random.bytes(out[0..]);105 prng.random.bytes(out[0..]);
106106
107 var timer = try Timer.start();107 var timer = try Timer.start();
...@@ -150,10 +150,10 @@ const aeads = [_]Crypto{...@@ -150,10 +150,10 @@ const aeads = [_]Crypto{
150 Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" },150 Crypto{ .ty = crypto.aead.ChaCha20Poly1305, .name = "chacha20Poly1305" },
151 Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" },151 Crypto{ .ty = crypto.aead.XChaCha20Poly1305, .name = "xchacha20Poly1305" },
152 Crypto{ .ty = crypto.aead.Gimli, .name = "gimli-aead" },152 Crypto{ .ty = crypto.aead.Gimli, .name = "gimli-aead" },
153 Crypto{ .ty = crypto.aead.AEGIS128L, .name = "aegis-128l" },153 Crypto{ .ty = crypto.aead.Aegis128L, .name = "aegis-128l" },
154 Crypto{ .ty = crypto.aead.AEGIS256, .name = "aegis-256" },154 Crypto{ .ty = crypto.aead.Aegis256, .name = "aegis-256" },
155 Crypto{ .ty = crypto.aead.AES128GCM, .name = "aes128-gcm" },155 Crypto{ .ty = crypto.aead.Aes128Gcm, .name = "aes128-gcm" },
156 Crypto{ .ty = crypto.aead.AES256GCM, .name = "aes256-gcm" },156 Crypto{ .ty = crypto.aead.Aes256Gcm, .name = "aes256-gcm" },
157};157};
158158
159pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 {159pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 {
...@@ -185,14 +185,14 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64...@@ -185,14 +185,14 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64
185}185}
186186
187const aes = [_]Crypto{187const aes = [_]Crypto{
188 Crypto{ .ty = crypto.core.aes.AES128, .name = "aes128-single" },188 Crypto{ .ty = crypto.core.aes.Aes128, .name = "aes128-single" },
189 Crypto{ .ty = crypto.core.aes.AES256, .name = "aes256-single" },189 Crypto{ .ty = crypto.core.aes.Aes256, .name = "aes256-single" },
190};190};
191191
192pub fn benchmarkAES(comptime AES: anytype, comptime count: comptime_int) !u64 {192pub fn benchmarkAes(comptime Aes: anytype, comptime count: comptime_int) !u64 {
193 var key: [AES.key_bits / 8]u8 = undefined;193 var key: [Aes.key_bits / 8]u8 = undefined;
194 prng.random.bytes(key[0..]);194 prng.random.bytes(key[0..]);
195 const ctx = AES.initEnc(key);195 const ctx = Aes.initEnc(key);
196196
197 var in = [_]u8{0} ** 16;197 var in = [_]u8{0} ** 16;
198198
...@@ -214,14 +214,14 @@ pub fn benchmarkAES(comptime AES: anytype, comptime count: comptime_int) !u64 {...@@ -214,14 +214,14 @@ pub fn benchmarkAES(comptime AES: anytype, comptime count: comptime_int) !u64 {
214}214}
215215
216const aes8 = [_]Crypto{216const aes8 = [_]Crypto{
217 Crypto{ .ty = crypto.core.aes.AES128, .name = "aes128-8" },217 Crypto{ .ty = crypto.core.aes.Aes128, .name = "aes128-8" },
218 Crypto{ .ty = crypto.core.aes.AES256, .name = "aes256-8" },218 Crypto{ .ty = crypto.core.aes.Aes256, .name = "aes256-8" },
219};219};
220220
221pub fn benchmarkAES8(comptime AES: anytype, comptime count: comptime_int) !u64 {221pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
222 var key: [AES.key_bits / 8]u8 = undefined;222 var key: [Aes.key_bits / 8]u8 = undefined;
223 prng.random.bytes(key[0..]);223 prng.random.bytes(key[0..]);
224 const ctx = AES.initEnc(key);224 const ctx = Aes.initEnc(key);
225225
226 var in = [_]u8{0} ** (8 * 16);226 var in = [_]u8{0} ** (8 * 16);
227227
...@@ -335,14 +335,14 @@ pub fn main() !void {...@@ -335,14 +335,14 @@ pub fn main() !void {
335335
336 inline for (aes) |E| {336 inline for (aes) |E| {
337 if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) {337 if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) {
338 const throughput = try benchmarkAES(E.ty, mode(100000000));338 const throughput = try benchmarkAes(E.ty, mode(100000000));
339 try stdout.print("{:>17}: {:10} ops/s\n", .{ E.name, throughput });339 try stdout.print("{:>17}: {:10} ops/s\n", .{ E.name, throughput });
340 }340 }
341 }341 }
342342
343 inline for (aes8) |E| {343 inline for (aes8) |E| {
344 if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) {344 if (filter == null or std.mem.indexOf(u8, E.name, filter.?) != null) {
345 const throughput = try benchmarkAES8(E.ty, mode(10000000));345 const throughput = try benchmarkAes8(E.ty, mode(10000000));
346 try stdout.print("{:>17}: {:10} ops/s\n", .{ E.name, throughput });346 try stdout.print("{:>17}: {:10} ops/s\n", .{ E.name, throughput });
347 }347 }
348 }348 }
lib/std/crypto/blake2.zig+39-33
...@@ -18,7 +18,7 @@ const RoundParam = struct {...@@ -18,7 +18,7 @@ const RoundParam = struct {
18 y: usize,18 y: usize,
19};19};
2020
21fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {21fn roundParam(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
22 return RoundParam{22 return RoundParam{
23 .a = a,23 .a = a,
24 .b = b,24 .b = b,
...@@ -32,14 +32,18 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {...@@ -32,14 +32,18 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
32/////////////////////32/////////////////////
33// Blake2s33// Blake2s
3434
35pub const Blake2s128 = Blake2s(128);
35pub const Blake2s224 = Blake2s(224);36pub const Blake2s224 = Blake2s(224);
36pub const Blake2s256 = Blake2s(256);37pub const Blake2s256 = Blake2s(256);
3738
38pub fn Blake2s(comptime out_len: usize) type {39pub fn Blake2s(comptime out_bits: usize) type {
39 return struct {40 return struct {
40 const Self = @This();41 const Self = @This();
41 pub const block_length = 64;42 pub const block_length = 64;
42 pub const digest_length = out_len / 8;43 pub const digest_length = out_bits / 8;
44 pub const key_length_min = 0;
45 pub const key_length_max = 32;
46 pub const key_length = 32; // recommended key length
43 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null };47 pub const Options = struct { key: ?[]const u8 = null, salt: ?[8]u8 = null, context: ?[8]u8 = null };
4448
45 const iv = [8]u32{49 const iv = [8]u32{
...@@ -73,14 +77,14 @@ pub fn Blake2s(comptime out_len: usize) type {...@@ -73,14 +77,14 @@ pub fn Blake2s(comptime out_len: usize) type {
73 buf_len: u8,77 buf_len: u8,
7478
75 pub fn init(options: Options) Self {79 pub fn init(options: Options) Self {
76 debug.assert(8 <= out_len and out_len <= 512);80 debug.assert(8 <= out_bits and out_bits <= 256);
7781
78 var d: Self = undefined;82 var d: Self = undefined;
79 mem.copy(u32, d.h[0..], iv[0..]);83 mem.copy(u32, d.h[0..], iv[0..]);
8084
81 const key_len = if (options.key) |key| key.len else 0;85 const key_len = if (options.key) |key| key.len else 0;
82 // default parameters86 // default parameters
83 d.h[0] ^= 0x01010000 ^ @truncate(u32, key_len << 8) ^ @intCast(u32, out_len >> 3);87 d.h[0] ^= 0x01010000 ^ @truncate(u32, key_len << 8) ^ @intCast(u32, out_bits >> 3);
84 d.t = 0;88 d.t = 0;
85 d.buf_len = 0;89 d.buf_len = 0;
8690
...@@ -100,7 +104,7 @@ pub fn Blake2s(comptime out_len: usize) type {...@@ -100,7 +104,7 @@ pub fn Blake2s(comptime out_len: usize) type {
100 return d;104 return d;
101 }105 }
102106
103 pub fn hash(b: []const u8, out: []u8, options: Options) void {107 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
104 var d = Self.init(options);108 var d = Self.init(options);
105 d.update(b);109 d.update(b);
106 d.final(out);110 d.final(out);
...@@ -129,14 +133,12 @@ pub fn Blake2s(comptime out_len: usize) type {...@@ -129,14 +133,12 @@ pub fn Blake2s(comptime out_len: usize) type {
129 d.buf_len += @intCast(u8, b[off..].len);133 d.buf_len += @intCast(u8, b[off..].len);
130 }134 }
131135
132 pub fn final(d: *Self, out: []u8) void {136 pub fn final(d: *Self, out: *[digest_length]u8) void {
133 debug.assert(out.len >= out_len / 8);
134
135 mem.set(u8, d.buf[d.buf_len..], 0);137 mem.set(u8, d.buf[d.buf_len..], 0);
136 d.t += d.buf_len;138 d.t += d.buf_len;
137 d.round(d.buf[0..], true);139 d.round(d.buf[0..], true);
138140
139 const rr = d.h[0 .. out_len / 32];141 const rr = d.h[0 .. digest_length / 4];
140142
141 for (rr) |s, j| {143 for (rr) |s, j| {
142 mem.writeIntSliceLittle(u32, out[4 * j ..], s);144 mem.writeIntSliceLittle(u32, out[4 * j ..], s);
...@@ -164,14 +166,14 @@ pub fn Blake2s(comptime out_len: usize) type {...@@ -164,14 +166,14 @@ pub fn Blake2s(comptime out_len: usize) type {
164 if (last) v[14] = ~v[14];166 if (last) v[14] = ~v[14];
165167
166 const rounds = comptime [_]RoundParam{168 const rounds = comptime [_]RoundParam{
167 Rp(0, 4, 8, 12, 0, 1),169 roundParam(0, 4, 8, 12, 0, 1),
168 Rp(1, 5, 9, 13, 2, 3),170 roundParam(1, 5, 9, 13, 2, 3),
169 Rp(2, 6, 10, 14, 4, 5),171 roundParam(2, 6, 10, 14, 4, 5),
170 Rp(3, 7, 11, 15, 6, 7),172 roundParam(3, 7, 11, 15, 6, 7),
171 Rp(0, 5, 10, 15, 8, 9),173 roundParam(0, 5, 10, 15, 8, 9),
172 Rp(1, 6, 11, 12, 10, 11),174 roundParam(1, 6, 11, 12, 10, 11),
173 Rp(2, 7, 8, 13, 12, 13),175 roundParam(2, 7, 8, 13, 12, 13),
174 Rp(3, 4, 9, 14, 14, 15),176 roundParam(3, 4, 9, 14, 14, 15),
175 };177 };
176178
177 comptime var j: usize = 0;179 comptime var j: usize = 0;
...@@ -372,15 +374,19 @@ test "comptime blake2s256" {...@@ -372,15 +374,19 @@ test "comptime blake2s256" {
372/////////////////////374/////////////////////
373// Blake2b375// Blake2b
374376
377pub const Blake2b128 = Blake2b(128);
375pub const Blake2b256 = Blake2b(256);378pub const Blake2b256 = Blake2b(256);
376pub const Blake2b384 = Blake2b(384);379pub const Blake2b384 = Blake2b(384);
377pub const Blake2b512 = Blake2b(512);380pub const Blake2b512 = Blake2b(512);
378381
379pub fn Blake2b(comptime out_len: usize) type {382pub fn Blake2b(comptime out_bits: usize) type {
380 return struct {383 return struct {
381 const Self = @This();384 const Self = @This();
382 pub const block_length = 128;385 pub const block_length = 128;
383 pub const digest_length = out_len / 8;386 pub const digest_length = out_bits / 8;
387 pub const key_length_min = 0;
388 pub const key_length_max = 64;
389 pub const key_length = 32; // recommended key length
384 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null };390 pub const Options = struct { key: ?[]const u8 = null, salt: ?[16]u8 = null, context: ?[16]u8 = null };
385391
386 const iv = [8]u64{392 const iv = [8]u64{
...@@ -416,14 +422,14 @@ pub fn Blake2b(comptime out_len: usize) type {...@@ -416,14 +422,14 @@ pub fn Blake2b(comptime out_len: usize) type {
416 buf_len: u8,422 buf_len: u8,
417423
418 pub fn init(options: Options) Self {424 pub fn init(options: Options) Self {
419 debug.assert(8 <= out_len and out_len <= 512);425 debug.assert(8 <= out_bits and out_bits <= 512);
420426
421 var d: Self = undefined;427 var d: Self = undefined;
422 mem.copy(u64, d.h[0..], iv[0..]);428 mem.copy(u64, d.h[0..], iv[0..]);
423429
424 const key_len = if (options.key) |key| key.len else 0;430 const key_len = if (options.key) |key| key.len else 0;
425 // default parameters431 // default parameters
426 d.h[0] ^= 0x01010000 ^ (key_len << 8) ^ (out_len >> 3);432 d.h[0] ^= 0x01010000 ^ (key_len << 8) ^ (out_bits >> 3);
427 d.t = 0;433 d.t = 0;
428 d.buf_len = 0;434 d.buf_len = 0;
429435
...@@ -443,7 +449,7 @@ pub fn Blake2b(comptime out_len: usize) type {...@@ -443,7 +449,7 @@ pub fn Blake2b(comptime out_len: usize) type {
443 return d;449 return d;
444 }450 }
445451
446 pub fn hash(b: []const u8, out: []u8, options: Options) void {452 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
447 var d = Self.init(options);453 var d = Self.init(options);
448 d.update(b);454 d.update(b);
449 d.final(out);455 d.final(out);
...@@ -472,12 +478,12 @@ pub fn Blake2b(comptime out_len: usize) type {...@@ -472,12 +478,12 @@ pub fn Blake2b(comptime out_len: usize) type {
472 d.buf_len += @intCast(u8, b[off..].len);478 d.buf_len += @intCast(u8, b[off..].len);
473 }479 }
474480
475 pub fn final(d: *Self, out: []u8) void {481 pub fn final(d: *Self, out: *[digest_length]u8) void {
476 mem.set(u8, d.buf[d.buf_len..], 0);482 mem.set(u8, d.buf[d.buf_len..], 0);
477 d.t += d.buf_len;483 d.t += d.buf_len;
478 d.round(d.buf[0..], true);484 d.round(d.buf[0..], true);
479485
480 const rr = d.h[0 .. out_len / 64];486 const rr = d.h[0 .. digest_length / 8];
481487
482 for (rr) |s, j| {488 for (rr) |s, j| {
483 mem.writeIntSliceLittle(u64, out[8 * j ..], s);489 mem.writeIntSliceLittle(u64, out[8 * j ..], s);
...@@ -505,14 +511,14 @@ pub fn Blake2b(comptime out_len: usize) type {...@@ -505,14 +511,14 @@ pub fn Blake2b(comptime out_len: usize) type {
505 if (last) v[14] = ~v[14];511 if (last) v[14] = ~v[14];
506512
507 const rounds = comptime [_]RoundParam{513 const rounds = comptime [_]RoundParam{
508 Rp(0, 4, 8, 12, 0, 1),514 roundParam(0, 4, 8, 12, 0, 1),
509 Rp(1, 5, 9, 13, 2, 3),515 roundParam(1, 5, 9, 13, 2, 3),
510 Rp(2, 6, 10, 14, 4, 5),516 roundParam(2, 6, 10, 14, 4, 5),
511 Rp(3, 7, 11, 15, 6, 7),517 roundParam(3, 7, 11, 15, 6, 7),
512 Rp(0, 5, 10, 15, 8, 9),518 roundParam(0, 5, 10, 15, 8, 9),
513 Rp(1, 6, 11, 12, 10, 11),519 roundParam(1, 6, 11, 12, 10, 11),
514 Rp(2, 7, 8, 13, 12, 13),520 roundParam(2, 7, 8, 13, 12, 13),
515 Rp(3, 4, 9, 14, 14, 15),521 roundParam(3, 4, 9, 14, 14, 15),
516 };522 };
517523
518 comptime var j: usize = 0;524 comptime var j: usize = 0;
lib/std/crypto/blake3.zig+35-34
...@@ -124,11 +124,11 @@ fn compress(...@@ -124,11 +124,11 @@ fn compress(
124 return state;124 return state;
125}125}
126126
127fn first_8_words(words: [16]u32) [8]u32 {127fn first8Words(words: [16]u32) [8]u32 {
128 return @ptrCast(*const [8]u32, &words).*;128 return @ptrCast(*const [8]u32, &words).*;
129}129}
130130
131fn words_from_little_endian_bytes(words: []u32, bytes: []const u8) void {131fn wordsFromLittleEndianBytes(words: []u32, bytes: []const u8) void {
132 var byte_slice = bytes;132 var byte_slice = bytes;
133 for (words) |*word| {133 for (words) |*word| {
134 word.* = mem.readIntSliceLittle(u32, byte_slice);134 word.* = mem.readIntSliceLittle(u32, byte_slice);
...@@ -146,8 +146,8 @@ const Output = struct {...@@ -146,8 +146,8 @@ const Output = struct {
146 counter: u64,146 counter: u64,
147 flags: u8,147 flags: u8,
148148
149 fn chaining_value(self: *const Output) [8]u32 {149 fn chainingValue(self: *const Output) [8]u32 {
150 return first_8_words(compress(150 return first8Words(compress(
151 self.input_chaining_value,151 self.input_chaining_value,
152 self.block_words,152 self.block_words,
153 self.block_len,153 self.block_len,
...@@ -156,7 +156,7 @@ const Output = struct {...@@ -156,7 +156,7 @@ const Output = struct {
156 ));156 ));
157 }157 }
158158
159 fn root_output_bytes(self: *const Output, output: []u8) void {159 fn rootOutputBytes(self: *const Output, output: []u8) void {
160 var out_block_it = ChunkIterator.init(output, 2 * OUT_LEN);160 var out_block_it = ChunkIterator.init(output, 2 * OUT_LEN);
161 var output_block_counter: usize = 0;161 var output_block_counter: usize = 0;
162 while (out_block_it.next()) |out_block| {162 while (out_block_it.next()) |out_block| {
...@@ -200,7 +200,7 @@ const ChunkState = struct {...@@ -200,7 +200,7 @@ const ChunkState = struct {
200 return BLOCK_LEN * @as(usize, self.blocks_compressed) + @as(usize, self.block_len);200 return BLOCK_LEN * @as(usize, self.blocks_compressed) + @as(usize, self.block_len);
201 }201 }
202202
203 fn fill_block_buf(self: *ChunkState, input: []const u8) []const u8 {203 fn fillBlockBuf(self: *ChunkState, input: []const u8) []const u8 {
204 const want = BLOCK_LEN - self.block_len;204 const want = BLOCK_LEN - self.block_len;
205 const take = math.min(want, input.len);205 const take = math.min(want, input.len);
206 mem.copy(u8, self.block[self.block_len..][0..take], input[0..take]);206 mem.copy(u8, self.block[self.block_len..][0..take], input[0..take]);
...@@ -208,7 +208,7 @@ const ChunkState = struct {...@@ -208,7 +208,7 @@ const ChunkState = struct {
208 return input[take..];208 return input[take..];
209 }209 }
210210
211 fn start_flag(self: *const ChunkState) u8 {211 fn startFlag(self: *const ChunkState) u8 {
212 return if (self.blocks_compressed == 0) CHUNK_START else 0;212 return if (self.blocks_compressed == 0) CHUNK_START else 0;
213 }213 }
214214
...@@ -219,13 +219,13 @@ const ChunkState = struct {...@@ -219,13 +219,13 @@ const ChunkState = struct {
219 // input is coming, so this compression is not CHUNK_END.219 // input is coming, so this compression is not CHUNK_END.
220 if (self.block_len == BLOCK_LEN) {220 if (self.block_len == BLOCK_LEN) {
221 var block_words: [16]u32 = undefined;221 var block_words: [16]u32 = undefined;
222 words_from_little_endian_bytes(block_words[0..], self.block[0..]);222 wordsFromLittleEndianBytes(block_words[0..], self.block[0..]);
223 self.chaining_value = first_8_words(compress(223 self.chaining_value = first8Words(compress(
224 self.chaining_value,224 self.chaining_value,
225 block_words,225 block_words,
226 BLOCK_LEN,226 BLOCK_LEN,
227 self.chunk_counter,227 self.chunk_counter,
228 self.flags | self.start_flag(),228 self.flags | self.startFlag(),
229 ));229 ));
230 self.blocks_compressed += 1;230 self.blocks_compressed += 1;
231 self.block = [_]u8{0} ** BLOCK_LEN;231 self.block = [_]u8{0} ** BLOCK_LEN;
...@@ -233,24 +233,24 @@ const ChunkState = struct {...@@ -233,24 +233,24 @@ const ChunkState = struct {
233 }233 }
234234
235 // Copy input bytes into the block buffer.235 // Copy input bytes into the block buffer.
236 input = self.fill_block_buf(input);236 input = self.fillBlockBuf(input);
237 }237 }
238 }238 }
239239
240 fn output(self: *const ChunkState) Output {240 fn output(self: *const ChunkState) Output {
241 var block_words: [16]u32 = undefined;241 var block_words: [16]u32 = undefined;
242 words_from_little_endian_bytes(block_words[0..], self.block[0..]);242 wordsFromLittleEndianBytes(block_words[0..], self.block[0..]);
243 return Output{243 return Output{
244 .input_chaining_value = self.chaining_value,244 .input_chaining_value = self.chaining_value,
245 .block_words = block_words,245 .block_words = block_words,
246 .block_len = self.block_len,246 .block_len = self.block_len,
247 .counter = self.chunk_counter,247 .counter = self.chunk_counter,
248 .flags = self.flags | self.start_flag() | CHUNK_END,248 .flags = self.flags | self.startFlag() | CHUNK_END,
249 };249 };
250 }250 }
251};251};
252252
253fn parent_output(253fn parentOutput(
254 left_child_cv: [8]u32,254 left_child_cv: [8]u32,
255 right_child_cv: [8]u32,255 right_child_cv: [8]u32,
256 key: [8]u32,256 key: [8]u32,
...@@ -268,18 +268,18 @@ fn parent_output(...@@ -268,18 +268,18 @@ fn parent_output(
268 };268 };
269}269}
270270
271fn parent_cv(271fn parentCv(
272 left_child_cv: [8]u32,272 left_child_cv: [8]u32,
273 right_child_cv: [8]u32,273 right_child_cv: [8]u32,
274 key: [8]u32,274 key: [8]u32,
275 flags: u8,275 flags: u8,
276) [8]u32 {276) [8]u32 {
277 return parent_output(left_child_cv, right_child_cv, key, flags).chaining_value();277 return parentOutput(left_child_cv, right_child_cv, key, flags).chainingValue();
278}278}
279279
280/// An incremental hasher that can accept any number of writes.280/// An incremental hasher that can accept any number of writes.
281pub const Blake3 = struct {281pub const Blake3 = struct {
282 pub const Options = struct { key: ?[KEY_LEN]u8 = null };282 pub const Options = struct { key: ?[digest_length]u8 = null };
283 pub const KdfOptions = struct {};283 pub const KdfOptions = struct {};
284284
285 chunk_state: ChunkState,285 chunk_state: ChunkState,
...@@ -288,8 +288,9 @@ pub const Blake3 = struct {...@@ -288,8 +288,9 @@ pub const Blake3 = struct {
288 cv_stack_len: u8 = 0, // 2^54 * CHUNK_LEN = 2^64288 cv_stack_len: u8 = 0, // 2^54 * CHUNK_LEN = 2^64
289 flags: u8,289 flags: u8,
290290
291 pub const digest_length = OUT_LEN;
292 pub const block_length = BLOCK_LEN;291 pub const block_length = BLOCK_LEN;
292 pub const digest_length = OUT_LEN;
293 pub const key_length = KEY_LEN;
293294
294 fn init_internal(key: [8]u32, flags: u8) Blake3 {295 fn init_internal(key: [8]u32, flags: u8) Blake3 {
295 return Blake3{296 return Blake3{
...@@ -303,7 +304,7 @@ pub const Blake3 = struct {...@@ -303,7 +304,7 @@ pub const Blake3 = struct {
303 pub fn init(options: Options) Blake3 {304 pub fn init(options: Options) Blake3 {
304 if (options.key) |key| {305 if (options.key) |key| {
305 var key_words: [8]u32 = undefined;306 var key_words: [8]u32 = undefined;
306 words_from_little_endian_bytes(key_words[0..], key[0..]);307 wordsFromLittleEndianBytes(key_words[0..], key[0..]);
307 return Blake3.init_internal(key_words, KEYED_HASH);308 return Blake3.init_internal(key_words, KEYED_HASH);
308 } else {309 } else {
309 return Blake3.init_internal(IV, 0);310 return Blake3.init_internal(IV, 0);
...@@ -318,7 +319,7 @@ pub const Blake3 = struct {...@@ -318,7 +319,7 @@ pub const Blake3 = struct {
318 var context_key: [KEY_LEN]u8 = undefined;319 var context_key: [KEY_LEN]u8 = undefined;
319 context_hasher.final(context_key[0..]);320 context_hasher.final(context_key[0..]);
320 var context_key_words: [8]u32 = undefined;321 var context_key_words: [8]u32 = undefined;
321 words_from_little_endian_bytes(context_key_words[0..], context_key[0..]);322 wordsFromLittleEndianBytes(context_key_words[0..], context_key[0..]);
322 return Blake3.init_internal(context_key_words, DERIVE_KEY_MATERIAL);323 return Blake3.init_internal(context_key_words, DERIVE_KEY_MATERIAL);
323 }324 }
324325
...@@ -328,18 +329,18 @@ pub const Blake3 = struct {...@@ -328,18 +329,18 @@ pub const Blake3 = struct {
328 hasher.final(out);329 hasher.final(out);
329 }330 }
330331
331 fn push_cv(self: *Blake3, cv: [8]u32) void {332 fn pushCv(self: *Blake3, cv: [8]u32) void {
332 self.cv_stack[self.cv_stack_len] = cv;333 self.cv_stack[self.cv_stack_len] = cv;
333 self.cv_stack_len += 1;334 self.cv_stack_len += 1;
334 }335 }
335336
336 fn pop_cv(self: *Blake3) [8]u32 {337 fn popCv(self: *Blake3) [8]u32 {
337 self.cv_stack_len -= 1;338 self.cv_stack_len -= 1;
338 return self.cv_stack[self.cv_stack_len];339 return self.cv_stack[self.cv_stack_len];
339 }340 }
340341
341 // Section 5.1.2 of the BLAKE3 spec explains this algorithm in more detail.342 // Section 5.1.2 of the BLAKE3 spec explains this algorithm in more detail.
342 fn add_chunk_chaining_value(self: *Blake3, first_cv: [8]u32, total_chunks: u64) void {343 fn addChunkChainingValue(self: *Blake3, first_cv: [8]u32, total_chunks: u64) void {
343 // This chunk might complete some subtrees. For each completed subtree,344 // This chunk might complete some subtrees. For each completed subtree,
344 // its left child will be the current top entry in the CV stack, and345 // its left child will be the current top entry in the CV stack, and
345 // its right child will be the current value of `new_cv`. Pop each left346 // its right child will be the current value of `new_cv`. Pop each left
...@@ -350,10 +351,10 @@ pub const Blake3 = struct {...@@ -350,10 +351,10 @@ pub const Blake3 = struct {
350 var new_cv = first_cv;351 var new_cv = first_cv;
351 var chunk_counter = total_chunks;352 var chunk_counter = total_chunks;
352 while (chunk_counter & 1 == 0) {353 while (chunk_counter & 1 == 0) {
353 new_cv = parent_cv(self.pop_cv(), new_cv, self.key, self.flags);354 new_cv = parentCv(self.popCv(), new_cv, self.key, self.flags);
354 chunk_counter >>= 1;355 chunk_counter >>= 1;
355 }356 }
356 self.push_cv(new_cv);357 self.pushCv(new_cv);
357 }358 }
358359
359 /// Add input to the hash state. This can be called any number of times.360 /// Add input to the hash state. This can be called any number of times.
...@@ -363,9 +364,9 @@ pub const Blake3 = struct {...@@ -363,9 +364,9 @@ pub const Blake3 = struct {
363 // If the current chunk is complete, finalize it and reset the364 // If the current chunk is complete, finalize it and reset the
364 // chunk state. More input is coming, so this chunk is not ROOT.365 // chunk state. More input is coming, so this chunk is not ROOT.
365 if (self.chunk_state.len() == CHUNK_LEN) {366 if (self.chunk_state.len() == CHUNK_LEN) {
366 const chunk_cv = self.chunk_state.output().chaining_value();367 const chunk_cv = self.chunk_state.output().chainingValue();
367 const total_chunks = self.chunk_state.chunk_counter + 1;368 const total_chunks = self.chunk_state.chunk_counter + 1;
368 self.add_chunk_chaining_value(chunk_cv, total_chunks);369 self.addChunkChainingValue(chunk_cv, total_chunks);
369 self.chunk_state = ChunkState.init(self.key, total_chunks, self.flags);370 self.chunk_state = ChunkState.init(self.key, total_chunks, self.flags);
370 }371 }
371372
...@@ -386,14 +387,14 @@ pub const Blake3 = struct {...@@ -386,14 +387,14 @@ pub const Blake3 = struct {
386 var parent_nodes_remaining: usize = self.cv_stack_len;387 var parent_nodes_remaining: usize = self.cv_stack_len;
387 while (parent_nodes_remaining > 0) {388 while (parent_nodes_remaining > 0) {
388 parent_nodes_remaining -= 1;389 parent_nodes_remaining -= 1;
389 output = parent_output(390 output = parentOutput(
390 self.cv_stack[parent_nodes_remaining],391 self.cv_stack[parent_nodes_remaining],
391 output.chaining_value(),392 output.chainingValue(),
392 self.key,393 self.key,
393 self.flags,394 self.flags,
394 );395 );
395 }396 }
396 output.root_output_bytes(out_slice);397 output.rootOutputBytes(out_slice);
397 }398 }
398};399};
399400
...@@ -561,7 +562,7 @@ const reference_test = ReferenceTest{...@@ -561,7 +562,7 @@ const reference_test = ReferenceTest{
561 },562 },
562};563};
563564
564fn test_blake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void {565fn testBlake3(hasher: *Blake3, input_len: usize, expected_hex: [262]u8) void {
565 // Save initial state566 // Save initial state
566 const initial_state = hasher.*;567 const initial_state = hasher.*;
567568
...@@ -596,8 +597,8 @@ test "BLAKE3 reference test cases" {...@@ -596,8 +597,8 @@ test "BLAKE3 reference test cases" {
596 var derive_key = &Blake3.initKdf(reference_test.context_string, .{});597 var derive_key = &Blake3.initKdf(reference_test.context_string, .{});
597598
598 for (reference_test.cases) |t| {599 for (reference_test.cases) |t| {
599 test_blake3(hash, t.input_len, t.hash.*);600 testBlake3(hash, t.input_len, t.hash.*);
600 test_blake3(keyed_hash, t.input_len, t.keyed_hash.*);601 testBlake3(keyed_hash, t.input_len, t.keyed_hash.*);
601 test_blake3(derive_key, t.input_len, t.derive_key.*);602 testBlake3(derive_key, t.input_len, t.derive_key.*);
602 }603 }
603}604}
lib/std/crypto/chacha20.zig+17-17
...@@ -317,7 +317,7 @@ fn keyToWords(key: [32]u8) [8]u32 {...@@ -317,7 +317,7 @@ fn keyToWords(key: [32]u8) [8]u32 {
317/// counter, nonce, and key.317/// counter, nonce, and key.
318pub const ChaCha20IETF = struct {318pub const ChaCha20IETF = struct {
319 pub fn xor(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce: [12]u8) void {319 pub fn xor(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce: [12]u8) void {
320 assert(in.len >= out.len);320 assert(in.len == out.len);
321 assert((in.len >> 6) + counter <= maxInt(u32));321 assert((in.len >> 6) + counter <= maxInt(u32));
322322
323 var c: [4]u32 = undefined;323 var c: [4]u32 = undefined;
...@@ -334,7 +334,7 @@ pub const ChaCha20IETF = struct {...@@ -334,7 +334,7 @@ pub const ChaCha20IETF = struct {
334/// exceed the 256 GiB limit of the 96-bit nonce version.334/// exceed the 256 GiB limit of the 96-bit nonce version.
335pub const ChaCha20With64BitNonce = struct {335pub const ChaCha20With64BitNonce = struct {
336 pub fn xor(out: []u8, in: []const u8, counter: u64, key: [32]u8, nonce: [8]u8) void {336 pub fn xor(out: []u8, in: []const u8, counter: u64, key: [32]u8, nonce: [8]u8) void {
337 assert(in.len >= out.len);337 assert(in.len == out.len);
338 assert(counter +% (in.len >> 6) >= counter);338 assert(counter +% (in.len >> 6) >= counter);
339339
340 var cursor: usize = 0;340 var cursor: usize = 0;
...@@ -345,9 +345,9 @@ pub const ChaCha20With64BitNonce = struct {...@@ -345,9 +345,9 @@ pub const ChaCha20With64BitNonce = struct {
345 c[2] = mem.readIntLittle(u32, nonce[0..4]);345 c[2] = mem.readIntLittle(u32, nonce[0..4]);
346 c[3] = mem.readIntLittle(u32, nonce[4..8]);346 c[3] = mem.readIntLittle(u32, nonce[4..8]);
347347
348 const block_size = (1 << 6);348 const block_length = (1 << 6);
349 // The full block size is greater than the address space on a 32bit machine349 // The full block size is greater than the address space on a 32bit machine
350 const big_block = if (@sizeOf(usize) > 4) (block_size << 32) else maxInt(usize);350 const big_block = if (@sizeOf(usize) > 4) (block_length << 32) else maxInt(usize);
351351
352 // first partial big block352 // first partial big block
353 if (((@intCast(u64, maxInt(u32) - @truncate(u32, counter)) + 1) << 6) < in.len) {353 if (((@intCast(u64, maxInt(u32) - @truncate(u32, counter)) + 1) << 6) < in.len) {
...@@ -621,10 +621,10 @@ test "crypto.chacha20 test vector 5" {...@@ -621,10 +621,10 @@ test "crypto.chacha20 test vector 5" {
621 testing.expectEqualSlices(u8, &expected_result, &result);621 testing.expectEqualSlices(u8, &expected_result, &result);
622}622}
623623
624pub const chacha20poly1305_tag_size = 16;624pub const chacha20poly1305_tag_length = 16;
625625
626fn chacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_size]u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) void {626fn chacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_length]u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) void {
627 assert(ciphertext.len >= plaintext.len);627 assert(ciphertext.len == plaintext.len);
628628
629 // derive poly1305 key629 // derive poly1305 key
630 var polyKey = [_]u8{0} ** 32;630 var polyKey = [_]u8{0} ** 32;
...@@ -655,13 +655,13 @@ fn chacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_si...@@ -655,13 +655,13 @@ fn chacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_si
655}655}
656656
657fn chacha20poly1305Seal(ciphertextAndTag: []u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) void {657fn chacha20poly1305Seal(ciphertextAndTag: []u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) void {
658 return chacha20poly1305SealDetached(ciphertextAndTag[0..plaintext.len], ciphertextAndTag[plaintext.len..][0..chacha20poly1305_tag_size], plaintext, data, key, nonce);658 return chacha20poly1305SealDetached(ciphertextAndTag[0..plaintext.len], ciphertextAndTag[plaintext.len..][0..chacha20poly1305_tag_length], plaintext, data, key, nonce);
659}659}
660660
661/// Verifies and decrypts an authenticated message produced by chacha20poly1305SealDetached.661/// Verifies and decrypts an authenticated message produced by chacha20poly1305SealDetached.
662fn chacha20poly1305OpenDetached(dst: []u8, ciphertext: []const u8, tag: *const [chacha20poly1305_tag_size]u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {662fn chacha20poly1305OpenDetached(dst: []u8, ciphertext: []const u8, tag: *const [chacha20poly1305_tag_length]u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {
663 // split ciphertext and tag663 // split ciphertext and tag
664 assert(dst.len >= ciphertext.len);664 assert(dst.len == ciphertext.len);
665665
666 // derive poly1305 key666 // derive poly1305 key
667 var polyKey = [_]u8{0} ** 32;667 var polyKey = [_]u8{0} ** 32;
...@@ -706,11 +706,11 @@ fn chacha20poly1305OpenDetached(dst: []u8, ciphertext: []const u8, tag: *const [...@@ -706,11 +706,11 @@ fn chacha20poly1305OpenDetached(dst: []u8, ciphertext: []const u8, tag: *const [
706706
707/// Verifies and decrypts an authenticated message produced by chacha20poly1305Seal.707/// Verifies and decrypts an authenticated message produced by chacha20poly1305Seal.
708fn chacha20poly1305Open(dst: []u8, ciphertextAndTag: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {708fn chacha20poly1305Open(dst: []u8, ciphertextAndTag: []const u8, data: []const u8, key: [32]u8, nonce: [12]u8) !void {
709 if (ciphertextAndTag.len < chacha20poly1305_tag_size) {709 if (ciphertextAndTag.len < chacha20poly1305_tag_length) {
710 return error.InvalidMessage;710 return error.InvalidMessage;
711 }711 }
712 const ciphertextLen = ciphertextAndTag.len - chacha20poly1305_tag_size;712 const ciphertextLen = ciphertextAndTag.len - chacha20poly1305_tag_length;
713 return try chacha20poly1305OpenDetached(dst, ciphertextAndTag[0..ciphertextLen], ciphertextAndTag[ciphertextLen..][0..chacha20poly1305_tag_size], data, key, nonce);713 return try chacha20poly1305OpenDetached(dst, ciphertextAndTag[0..ciphertextLen], ciphertextAndTag[ciphertextLen..][0..chacha20poly1305_tag_length], data, key, nonce);
714}714}
715715
716fn extend(key: [32]u8, nonce: [24]u8) struct { key: [32]u8, nonce: [12]u8 } {716fn extend(key: [32]u8, nonce: [24]u8) struct { key: [32]u8, nonce: [12]u8 } {
...@@ -730,9 +730,9 @@ pub const XChaCha20IETF = struct {...@@ -730,9 +730,9 @@ pub const XChaCha20IETF = struct {
730 }730 }
731};731};
732732
733pub const xchacha20poly1305_tag_size = 16;733pub const xchacha20poly1305_tag_length = 16;
734734
735fn xchacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_size]u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [24]u8) void {735fn xchacha20poly1305SealDetached(ciphertext: []u8, tag: *[chacha20poly1305_tag_length]u8, plaintext: []const u8, data: []const u8, key: [32]u8, nonce: [24]u8) void {
736 const extended = extend(key, nonce);736 const extended = extend(key, nonce);
737 return chacha20poly1305SealDetached(ciphertext, tag, plaintext, data, extended.key, extended.nonce);737 return chacha20poly1305SealDetached(ciphertext, tag, plaintext, data, extended.key, extended.nonce);
738}738}
...@@ -743,7 +743,7 @@ fn xchacha20poly1305Seal(ciphertextAndTag: []u8, plaintext: []const u8, data: []...@@ -743,7 +743,7 @@ fn xchacha20poly1305Seal(ciphertextAndTag: []u8, plaintext: []const u8, data: []
743}743}
744744
745/// Verifies and decrypts an authenticated message produced by xchacha20poly1305SealDetached.745/// Verifies and decrypts an authenticated message produced by xchacha20poly1305SealDetached.
746fn xchacha20poly1305OpenDetached(plaintext: []u8, ciphertext: []const u8, tag: *const [chacha20poly1305_tag_size]u8, data: []const u8, key: [32]u8, nonce: [24]u8) !void {746fn xchacha20poly1305OpenDetached(plaintext: []u8, ciphertext: []const u8, tag: *const [chacha20poly1305_tag_length]u8, data: []const u8, key: [32]u8, nonce: [24]u8) !void {
747 const extended = extend(key, nonce);747 const extended = extend(key, nonce);
748 return try chacha20poly1305OpenDetached(plaintext, ciphertext, tag, data, extended.key, extended.nonce);748 return try chacha20poly1305OpenDetached(plaintext, ciphertext, tag, data, extended.key, extended.nonce);
749}749}
...@@ -883,7 +883,7 @@ test "crypto.xchacha20" {...@@ -883,7 +883,7 @@ test "crypto.xchacha20" {
883 }883 }
884 {884 {
885 const data = "Additional data";885 const data = "Additional data";
886 var ciphertext: [input.len + xchacha20poly1305_tag_size]u8 = undefined;886 var ciphertext: [input.len + xchacha20poly1305_tag_length]u8 = undefined;
887 xchacha20poly1305Seal(ciphertext[0..], input, data, key, nonce);887 xchacha20poly1305Seal(ciphertext[0..], input, data, key, nonce);
888 var out: [input.len]u8 = undefined;888 var out: [input.len]u8 = undefined;
889 try xchacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce);889 try xchacha20poly1305Open(out[0..], ciphertext[0..], data, key, nonce);
lib/std/crypto/ghash.zig+10-10
...@@ -18,9 +18,9 @@ const mem = std.mem;...@@ -18,9 +18,9 @@ const mem = std.mem;
18///18///
19/// GHASH is typically used to compute the authentication tag in the AES-GCM construction.19/// GHASH is typically used to compute the authentication tag in the AES-GCM construction.
20pub const Ghash = struct {20pub const Ghash = struct {
21 pub const block_size: usize = 16;21 pub const block_length: usize = 16;
22 pub const mac_length = 16;22 pub const mac_length = 16;
23 pub const minimum_key_length = 16;23 pub const key_length = 16;
2424
25 y0: u64 = 0,25 y0: u64 = 0,
26 y1: u64 = 0,26 y1: u64 = 0,
...@@ -39,9 +39,9 @@ pub const Ghash = struct {...@@ -39,9 +39,9 @@ pub const Ghash = struct {
39 hh2r: u64 = undefined,39 hh2r: u64 = undefined,
4040
41 leftover: usize = 0,41 leftover: usize = 0,
42 buf: [block_size]u8 align(16) = undefined,42 buf: [block_length]u8 align(16) = undefined,
4343
44 pub fn init(key: *const [minimum_key_length]u8) Ghash {44 pub fn init(key: *const [key_length]u8) Ghash {
45 const h1 = mem.readIntBig(u64, key[0..8]);45 const h1 = mem.readIntBig(u64, key[0..8]);
46 const h0 = mem.readIntBig(u64, key[8..16]);46 const h0 = mem.readIntBig(u64, key[8..16]);
47 const h1r = @bitReverse(u64, h1);47 const h1r = @bitReverse(u64, h1);
...@@ -261,21 +261,21 @@ pub const Ghash = struct {...@@ -261,21 +261,21 @@ pub const Ghash = struct {
261 var mb = m;261 var mb = m;
262262
263 if (st.leftover > 0) {263 if (st.leftover > 0) {
264 const want = math.min(block_size - st.leftover, mb.len);264 const want = math.min(block_length - st.leftover, mb.len);
265 const mc = mb[0..want];265 const mc = mb[0..want];
266 for (mc) |x, i| {266 for (mc) |x, i| {
267 st.buf[st.leftover + i] = x;267 st.buf[st.leftover + i] = x;
268 }268 }
269 mb = mb[want..];269 mb = mb[want..];
270 st.leftover += want;270 st.leftover += want;
271 if (st.leftover < block_size) {271 if (st.leftover < block_length) {
272 return;272 return;
273 }273 }
274 st.blocks(&st.buf);274 st.blocks(&st.buf);
275 st.leftover = 0;275 st.leftover = 0;
276 }276 }
277 if (mb.len >= block_size) {277 if (mb.len >= block_length) {
278 const want = mb.len & ~(block_size - 1);278 const want = mb.len & ~(block_length - 1);
279 st.blocks(mb[0..want]);279 st.blocks(mb[0..want]);
280 mb = mb[want..];280 mb = mb[want..];
281 }281 }
...@@ -293,7 +293,7 @@ pub const Ghash = struct {...@@ -293,7 +293,7 @@ pub const Ghash = struct {
293 return;293 return;
294 }294 }
295 var i = st.leftover;295 var i = st.leftover;
296 while (i < block_size) : (i += 1) {296 while (i < block_length) : (i += 1) {
297 st.buf[i] = 0;297 st.buf[i] = 0;
298 }298 }
299 st.blocks(&st.buf);299 st.blocks(&st.buf);
...@@ -308,7 +308,7 @@ pub const Ghash = struct {...@@ -308,7 +308,7 @@ pub const Ghash = struct {
308 mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Ghash)]);308 mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Ghash)]);
309 }309 }
310310
311 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [minimum_key_length]u8) void {311 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
312 var st = Ghash.init(key);312 var st = Ghash.init(key);
313 st.update(msg);313 st.update(msg);
314 st.final(out);314 st.final(out);
lib/std/crypto/gimli.zig+3-4
...@@ -200,6 +200,7 @@ pub const Hash = struct {...@@ -200,6 +200,7 @@ pub const Hash = struct {
200 buf_off: usize,200 buf_off: usize,
201201
202 pub const block_length = State.RATE;202 pub const block_length = State.RATE;
203 pub const digest_length = 32;
203 pub const Options = struct {};204 pub const Options = struct {};
204205
205 const Self = @This();206 const Self = @This();
...@@ -231,15 +232,13 @@ pub const Hash = struct {...@@ -231,15 +232,13 @@ pub const Hash = struct {
231 }232 }
232 }233 }
233234
234 pub const digest_length = 32;
235
236 /// Finish the current hashing operation, writing the hash to `out`235 /// Finish the current hashing operation, writing the hash to `out`
237 ///236 ///
238 /// From 4.9 "Application to hashing"237 /// From 4.9 "Application to hashing"
239 /// By default, Gimli-Hash provides a fixed-length output of 32 bytes238 /// By default, Gimli-Hash provides a fixed-length output of 32 bytes
240 /// (the concatenation of two 16-byte blocks). However, Gimli-Hash can239 /// (the concatenation of two 16-byte blocks). However, Gimli-Hash can
241 /// be used as an “extendable one-way function” (XOF).240 /// be used as an “extendable one-way function” (XOF).
242 pub fn final(self: *Self, out: []u8) void {241 pub fn final(self: *Self, out: *[digest_length]u8) void {
243 const buf = self.state.toSlice();242 const buf = self.state.toSlice();
244243
245 // XOR 1 into the next byte of the state244 // XOR 1 into the next byte of the state
...@@ -251,7 +250,7 @@ pub const Hash = struct {...@@ -251,7 +250,7 @@ pub const Hash = struct {
251 }250 }
252};251};
253252
254pub fn hash(out: []u8, in: []const u8, options: Hash.Options) void {253pub fn hash(out: *[Hash.digest_length]u8, in: []const u8, options: Hash.Options) void {
255 var st = Hash.init(options);254 var st = Hash.init(options);
256 st.update(in);255 st.update(in);
257 st.final(out);256 st.final(out);
lib/std/crypto/hmac.zig+3-2
...@@ -22,14 +22,15 @@ pub fn Hmac(comptime Hash: type) type {...@@ -22,14 +22,15 @@ pub fn Hmac(comptime Hash: type) type {
22 return struct {22 return struct {
23 const Self = @This();23 const Self = @This();
24 pub const mac_length = Hash.digest_length;24 pub const mac_length = Hash.digest_length;
25 pub const minimum_key_length = 0;25 pub const key_length_min = 0;
26 pub const key_length = 32; // recommended key length
2627
27 o_key_pad: [Hash.block_length]u8,28 o_key_pad: [Hash.block_length]u8,
28 i_key_pad: [Hash.block_length]u8,29 i_key_pad: [Hash.block_length]u8,
29 scratch: [Hash.block_length]u8,30 scratch: [Hash.block_length]u8,
30 hash: Hash,31 hash: Hash,
3132
32 // HMAC(k, m) = H(o_key_pad | H(i_key_pad | msg)) where | is concatenation33 // HMAC(k, m) = H(o_key_pad || H(i_key_pad || msg)) where || is concatenation
33 pub fn create(out: []u8, msg: []const u8, key: []const u8) void {34 pub fn create(out: []u8, msg: []const u8, key: []const u8) void {
34 var ctx = Self.init(key);35 var ctx = Self.init(key);
35 ctx.update(msg);36 ctx.update(msg);
lib/std/crypto/md5.zig+70-75
...@@ -6,7 +6,6 @@...@@ -6,7 +6,6 @@
6const std = @import("../std.zig");6const std = @import("../std.zig");
7const mem = std.mem;7const mem = std.mem;
8const math = std.math;8const math = std.math;
9const debug = std.debug;
109
11const RoundParam = struct {10const RoundParam = struct {
12 a: usize,11 a: usize,
...@@ -18,7 +17,7 @@ const RoundParam = struct {...@@ -18,7 +17,7 @@ const RoundParam = struct {
18 t: u32,17 t: u32,
19};18};
2019
21fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) RoundParam {20fn roundParam(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) RoundParam {
22 return RoundParam{21 return RoundParam{
23 .a = a,22 .a = a,
24 .b = b,23 .b = b,
...@@ -59,7 +58,7 @@ pub const Md5 = struct {...@@ -59,7 +58,7 @@ pub const Md5 = struct {
59 };58 };
60 }59 }
6160
62 pub fn hash(b: []const u8, out: []u8, options: Options) void {61 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
63 var d = Md5.init(options);62 var d = Md5.init(options);
64 d.update(b);63 d.update(b);
65 d.final(out);64 d.final(out);
...@@ -73,13 +72,13 @@ pub const Md5 = struct {...@@ -73,13 +72,13 @@ pub const Md5 = struct {
73 off += 64 - d.buf_len;72 off += 64 - d.buf_len;
74 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);73 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
7574
76 d.round(d.buf[0..]);75 d.round(&d.buf);
77 d.buf_len = 0;76 d.buf_len = 0;
78 }77 }
7978
80 // Full middle blocks.79 // Full middle blocks.
81 while (off + 64 <= b.len) : (off += 64) {80 while (off + 64 <= b.len) : (off += 64) {
82 d.round(b[off .. off + 64]);81 d.round(b[off..][0..64]);
83 }82 }
8483
85 // Copy any remainder for next pass.84 // Copy any remainder for next pass.
...@@ -90,9 +89,7 @@ pub const Md5 = struct {...@@ -90,9 +89,7 @@ pub const Md5 = struct {
90 d.total_len +%= b.len;89 d.total_len +%= b.len;
91 }90 }
9291
93 pub fn final(d: *Self, out: []u8) void {92 pub fn final(d: *Self, out: *[digest_length]u8) void {
94 debug.assert(out.len >= 16);
95
96 // The buffer here will never be completely full.93 // The buffer here will never be completely full.
97 mem.set(u8, d.buf[d.buf_len..], 0);94 mem.set(u8, d.buf[d.buf_len..], 0);
9895
...@@ -122,9 +119,7 @@ pub const Md5 = struct {...@@ -122,9 +119,7 @@ pub const Md5 = struct {
122 }119 }
123 }120 }
124121
125 fn round(d: *Self, b: []const u8) void {122 fn round(d: *Self, b: *const [64]u8) void {
126 debug.assert(b.len == 64);
127
128 var s: [16]u32 = undefined;123 var s: [16]u32 = undefined;
129124
130 var i: usize = 0;125 var i: usize = 0;
...@@ -145,22 +140,22 @@ pub const Md5 = struct {...@@ -145,22 +140,22 @@ pub const Md5 = struct {
145 };140 };
146141
147 const round0 = comptime [_]RoundParam{142 const round0 = comptime [_]RoundParam{
148 Rp(0, 1, 2, 3, 0, 7, 0xD76AA478),143 roundParam(0, 1, 2, 3, 0, 7, 0xD76AA478),
149 Rp(3, 0, 1, 2, 1, 12, 0xE8C7B756),144 roundParam(3, 0, 1, 2, 1, 12, 0xE8C7B756),
150 Rp(2, 3, 0, 1, 2, 17, 0x242070DB),145 roundParam(2, 3, 0, 1, 2, 17, 0x242070DB),
151 Rp(1, 2, 3, 0, 3, 22, 0xC1BDCEEE),146 roundParam(1, 2, 3, 0, 3, 22, 0xC1BDCEEE),
152 Rp(0, 1, 2, 3, 4, 7, 0xF57C0FAF),147 roundParam(0, 1, 2, 3, 4, 7, 0xF57C0FAF),
153 Rp(3, 0, 1, 2, 5, 12, 0x4787C62A),148 roundParam(3, 0, 1, 2, 5, 12, 0x4787C62A),
154 Rp(2, 3, 0, 1, 6, 17, 0xA8304613),149 roundParam(2, 3, 0, 1, 6, 17, 0xA8304613),
155 Rp(1, 2, 3, 0, 7, 22, 0xFD469501),150 roundParam(1, 2, 3, 0, 7, 22, 0xFD469501),
156 Rp(0, 1, 2, 3, 8, 7, 0x698098D8),151 roundParam(0, 1, 2, 3, 8, 7, 0x698098D8),
157 Rp(3, 0, 1, 2, 9, 12, 0x8B44F7AF),152 roundParam(3, 0, 1, 2, 9, 12, 0x8B44F7AF),
158 Rp(2, 3, 0, 1, 10, 17, 0xFFFF5BB1),153 roundParam(2, 3, 0, 1, 10, 17, 0xFFFF5BB1),
159 Rp(1, 2, 3, 0, 11, 22, 0x895CD7BE),154 roundParam(1, 2, 3, 0, 11, 22, 0x895CD7BE),
160 Rp(0, 1, 2, 3, 12, 7, 0x6B901122),155 roundParam(0, 1, 2, 3, 12, 7, 0x6B901122),
161 Rp(3, 0, 1, 2, 13, 12, 0xFD987193),156 roundParam(3, 0, 1, 2, 13, 12, 0xFD987193),
162 Rp(2, 3, 0, 1, 14, 17, 0xA679438E),157 roundParam(2, 3, 0, 1, 14, 17, 0xA679438E),
163 Rp(1, 2, 3, 0, 15, 22, 0x49B40821),158 roundParam(1, 2, 3, 0, 15, 22, 0x49B40821),
164 };159 };
165 inline for (round0) |r| {160 inline for (round0) |r| {
166 v[r.a] = v[r.a] +% (v[r.d] ^ (v[r.b] & (v[r.c] ^ v[r.d]))) +% r.t +% s[r.k];161 v[r.a] = v[r.a] +% (v[r.d] ^ (v[r.b] & (v[r.c] ^ v[r.d]))) +% r.t +% s[r.k];
...@@ -168,22 +163,22 @@ pub const Md5 = struct {...@@ -168,22 +163,22 @@ pub const Md5 = struct {
168 }163 }
169164
170 const round1 = comptime [_]RoundParam{165 const round1 = comptime [_]RoundParam{
171 Rp(0, 1, 2, 3, 1, 5, 0xF61E2562),166 roundParam(0, 1, 2, 3, 1, 5, 0xF61E2562),
172 Rp(3, 0, 1, 2, 6, 9, 0xC040B340),167 roundParam(3, 0, 1, 2, 6, 9, 0xC040B340),
173 Rp(2, 3, 0, 1, 11, 14, 0x265E5A51),168 roundParam(2, 3, 0, 1, 11, 14, 0x265E5A51),
174 Rp(1, 2, 3, 0, 0, 20, 0xE9B6C7AA),169 roundParam(1, 2, 3, 0, 0, 20, 0xE9B6C7AA),
175 Rp(0, 1, 2, 3, 5, 5, 0xD62F105D),170 roundParam(0, 1, 2, 3, 5, 5, 0xD62F105D),
176 Rp(3, 0, 1, 2, 10, 9, 0x02441453),171 roundParam(3, 0, 1, 2, 10, 9, 0x02441453),
177 Rp(2, 3, 0, 1, 15, 14, 0xD8A1E681),172 roundParam(2, 3, 0, 1, 15, 14, 0xD8A1E681),
178 Rp(1, 2, 3, 0, 4, 20, 0xE7D3FBC8),173 roundParam(1, 2, 3, 0, 4, 20, 0xE7D3FBC8),
179 Rp(0, 1, 2, 3, 9, 5, 0x21E1CDE6),174 roundParam(0, 1, 2, 3, 9, 5, 0x21E1CDE6),
180 Rp(3, 0, 1, 2, 14, 9, 0xC33707D6),175 roundParam(3, 0, 1, 2, 14, 9, 0xC33707D6),
181 Rp(2, 3, 0, 1, 3, 14, 0xF4D50D87),176 roundParam(2, 3, 0, 1, 3, 14, 0xF4D50D87),
182 Rp(1, 2, 3, 0, 8, 20, 0x455A14ED),177 roundParam(1, 2, 3, 0, 8, 20, 0x455A14ED),
183 Rp(0, 1, 2, 3, 13, 5, 0xA9E3E905),178 roundParam(0, 1, 2, 3, 13, 5, 0xA9E3E905),
184 Rp(3, 0, 1, 2, 2, 9, 0xFCEFA3F8),179 roundParam(3, 0, 1, 2, 2, 9, 0xFCEFA3F8),
185 Rp(2, 3, 0, 1, 7, 14, 0x676F02D9),180 roundParam(2, 3, 0, 1, 7, 14, 0x676F02D9),
186 Rp(1, 2, 3, 0, 12, 20, 0x8D2A4C8A),181 roundParam(1, 2, 3, 0, 12, 20, 0x8D2A4C8A),
187 };182 };
188 inline for (round1) |r| {183 inline for (round1) |r| {
189 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.d] & (v[r.b] ^ v[r.c]))) +% r.t +% s[r.k];184 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.d] & (v[r.b] ^ v[r.c]))) +% r.t +% s[r.k];
...@@ -191,22 +186,22 @@ pub const Md5 = struct {...@@ -191,22 +186,22 @@ pub const Md5 = struct {
191 }186 }
192187
193 const round2 = comptime [_]RoundParam{188 const round2 = comptime [_]RoundParam{
194 Rp(0, 1, 2, 3, 5, 4, 0xFFFA3942),189 roundParam(0, 1, 2, 3, 5, 4, 0xFFFA3942),
195 Rp(3, 0, 1, 2, 8, 11, 0x8771F681),190 roundParam(3, 0, 1, 2, 8, 11, 0x8771F681),
196 Rp(2, 3, 0, 1, 11, 16, 0x6D9D6122),191 roundParam(2, 3, 0, 1, 11, 16, 0x6D9D6122),
197 Rp(1, 2, 3, 0, 14, 23, 0xFDE5380C),192 roundParam(1, 2, 3, 0, 14, 23, 0xFDE5380C),
198 Rp(0, 1, 2, 3, 1, 4, 0xA4BEEA44),193 roundParam(0, 1, 2, 3, 1, 4, 0xA4BEEA44),
199 Rp(3, 0, 1, 2, 4, 11, 0x4BDECFA9),194 roundParam(3, 0, 1, 2, 4, 11, 0x4BDECFA9),
200 Rp(2, 3, 0, 1, 7, 16, 0xF6BB4B60),195 roundParam(2, 3, 0, 1, 7, 16, 0xF6BB4B60),
201 Rp(1, 2, 3, 0, 10, 23, 0xBEBFBC70),196 roundParam(1, 2, 3, 0, 10, 23, 0xBEBFBC70),
202 Rp(0, 1, 2, 3, 13, 4, 0x289B7EC6),197 roundParam(0, 1, 2, 3, 13, 4, 0x289B7EC6),
203 Rp(3, 0, 1, 2, 0, 11, 0xEAA127FA),198 roundParam(3, 0, 1, 2, 0, 11, 0xEAA127FA),
204 Rp(2, 3, 0, 1, 3, 16, 0xD4EF3085),199 roundParam(2, 3, 0, 1, 3, 16, 0xD4EF3085),
205 Rp(1, 2, 3, 0, 6, 23, 0x04881D05),200 roundParam(1, 2, 3, 0, 6, 23, 0x04881D05),
206 Rp(0, 1, 2, 3, 9, 4, 0xD9D4D039),201 roundParam(0, 1, 2, 3, 9, 4, 0xD9D4D039),
207 Rp(3, 0, 1, 2, 12, 11, 0xE6DB99E5),202 roundParam(3, 0, 1, 2, 12, 11, 0xE6DB99E5),
208 Rp(2, 3, 0, 1, 15, 16, 0x1FA27CF8),203 roundParam(2, 3, 0, 1, 15, 16, 0x1FA27CF8),
209 Rp(1, 2, 3, 0, 2, 23, 0xC4AC5665),204 roundParam(1, 2, 3, 0, 2, 23, 0xC4AC5665),
210 };205 };
211 inline for (round2) |r| {206 inline for (round2) |r| {
212 v[r.a] = v[r.a] +% (v[r.b] ^ v[r.c] ^ v[r.d]) +% r.t +% s[r.k];207 v[r.a] = v[r.a] +% (v[r.b] ^ v[r.c] ^ v[r.d]) +% r.t +% s[r.k];
...@@ -214,22 +209,22 @@ pub const Md5 = struct {...@@ -214,22 +209,22 @@ pub const Md5 = struct {
214 }209 }
215210
216 const round3 = comptime [_]RoundParam{211 const round3 = comptime [_]RoundParam{
217 Rp(0, 1, 2, 3, 0, 6, 0xF4292244),212 roundParam(0, 1, 2, 3, 0, 6, 0xF4292244),
218 Rp(3, 0, 1, 2, 7, 10, 0x432AFF97),213 roundParam(3, 0, 1, 2, 7, 10, 0x432AFF97),
219 Rp(2, 3, 0, 1, 14, 15, 0xAB9423A7),214 roundParam(2, 3, 0, 1, 14, 15, 0xAB9423A7),
220 Rp(1, 2, 3, 0, 5, 21, 0xFC93A039),215 roundParam(1, 2, 3, 0, 5, 21, 0xFC93A039),
221 Rp(0, 1, 2, 3, 12, 6, 0x655B59C3),216 roundParam(0, 1, 2, 3, 12, 6, 0x655B59C3),
222 Rp(3, 0, 1, 2, 3, 10, 0x8F0CCC92),217 roundParam(3, 0, 1, 2, 3, 10, 0x8F0CCC92),
223 Rp(2, 3, 0, 1, 10, 15, 0xFFEFF47D),218 roundParam(2, 3, 0, 1, 10, 15, 0xFFEFF47D),
224 Rp(1, 2, 3, 0, 1, 21, 0x85845DD1),219 roundParam(1, 2, 3, 0, 1, 21, 0x85845DD1),
225 Rp(0, 1, 2, 3, 8, 6, 0x6FA87E4F),220 roundParam(0, 1, 2, 3, 8, 6, 0x6FA87E4F),
226 Rp(3, 0, 1, 2, 15, 10, 0xFE2CE6E0),221 roundParam(3, 0, 1, 2, 15, 10, 0xFE2CE6E0),
227 Rp(2, 3, 0, 1, 6, 15, 0xA3014314),222 roundParam(2, 3, 0, 1, 6, 15, 0xA3014314),
228 Rp(1, 2, 3, 0, 13, 21, 0x4E0811A1),223 roundParam(1, 2, 3, 0, 13, 21, 0x4E0811A1),
229 Rp(0, 1, 2, 3, 4, 6, 0xF7537E82),224 roundParam(0, 1, 2, 3, 4, 6, 0xF7537E82),
230 Rp(3, 0, 1, 2, 11, 10, 0xBD3AF235),225 roundParam(3, 0, 1, 2, 11, 10, 0xBD3AF235),
231 Rp(2, 3, 0, 1, 2, 15, 0x2AD7D2BB),226 roundParam(2, 3, 0, 1, 2, 15, 0x2AD7D2BB),
232 Rp(1, 2, 3, 0, 9, 21, 0xEB86D391),227 roundParam(1, 2, 3, 0, 9, 21, 0xEB86D391),
233 };228 };
234 inline for (round3) |r| {229 inline for (round3) |r| {
235 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.b] | ~v[r.d])) +% r.t +% s[r.k];230 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.b] | ~v[r.d])) +% r.t +% s[r.k];
lib/std/crypto/modes.zig+10-10
...@@ -16,34 +16,34 @@ const debug = std.debug;...@@ -16,34 +16,34 @@ const debug = std.debug;
16///16///
17/// Important: the counter mode doesn't provide authenticated encryption: the ciphertext can be trivially modified without this being detected.17/// Important: the counter mode doesn't provide authenticated encryption: the ciphertext can be trivially modified without this being detected.
18/// As a result, applications should generally never use it directly, but only in a construction that includes a MAC.18/// As a result, applications should generally never use it directly, but only in a construction that includes a MAC.
19pub fn ctr(comptime BlockCipher: anytype, block_cipher: BlockCipher, dst: []u8, src: []const u8, iv: [BlockCipher.block_size]u8, endian: comptime builtin.Endian) void {19pub fn ctr(comptime BlockCipher: anytype, block_cipher: BlockCipher, dst: []u8, src: []const u8, iv: [BlockCipher.block_length]u8, endian: comptime builtin.Endian) void {
20 debug.assert(dst.len >= src.len);20 debug.assert(dst.len >= src.len);
21 const block_size = BlockCipher.block_size;21 const block_length = BlockCipher.block_length;
22 var counter: [BlockCipher.block_size]u8 = undefined;22 var counter: [BlockCipher.block_length]u8 = undefined;
23 var counterInt = mem.readInt(u128, &iv, endian);23 var counterInt = mem.readInt(u128, &iv, endian);
24 var i: usize = 0;24 var i: usize = 0;
2525
26 const parallel_count = BlockCipher.block.parallel.optimal_parallel_blocks;26 const parallel_count = BlockCipher.block.parallel.optimal_parallel_blocks;
27 const wide_block_size = parallel_count * 16;27 const wide_block_length = parallel_count * 16;
28 if (src.len >= wide_block_size) {28 if (src.len >= wide_block_length) {
29 var counters: [parallel_count * 16]u8 = undefined;29 var counters: [parallel_count * 16]u8 = undefined;
30 while (i + wide_block_size <= src.len) : (i += wide_block_size) {30 while (i + wide_block_length <= src.len) : (i += wide_block_length) {
31 comptime var j = 0;31 comptime var j = 0;
32 inline while (j < parallel_count) : (j += 1) {32 inline while (j < parallel_count) : (j += 1) {
33 mem.writeInt(u128, counters[j * 16 .. j * 16 + 16], counterInt, endian);33 mem.writeInt(u128, counters[j * 16 .. j * 16 + 16], counterInt, endian);
34 counterInt +%= 1;34 counterInt +%= 1;
35 }35 }
36 block_cipher.xorWide(parallel_count, dst[i .. i + wide_block_size][0..wide_block_size], src[i .. i + wide_block_size][0..wide_block_size], counters);36 block_cipher.xorWide(parallel_count, dst[i .. i + wide_block_length][0..wide_block_length], src[i .. i + wide_block_length][0..wide_block_length], counters);
37 }37 }
38 }38 }
39 while (i + block_size <= src.len) : (i += block_size) {39 while (i + block_length <= src.len) : (i += block_length) {
40 mem.writeInt(u128, &counter, counterInt, endian);40 mem.writeInt(u128, &counter, counterInt, endian);
41 counterInt +%= 1;41 counterInt +%= 1;
42 block_cipher.xor(dst[i .. i + block_size][0..block_size], src[i .. i + block_size][0..block_size], counter);42 block_cipher.xor(dst[i .. i + block_length][0..block_length], src[i .. i + block_length][0..block_length], counter);
43 }43 }
44 if (i < src.len) {44 if (i < src.len) {
45 mem.writeInt(u128, &counter, counterInt, endian);45 mem.writeInt(u128, &counter, counterInt, endian);
46 var pad = [_]u8{0} ** block_size;46 var pad = [_]u8{0} ** block_length;
47 mem.copy(u8, &pad, src[i..]);47 mem.copy(u8, &pad, src[i..]);
48 block_cipher.xor(&pad, &pad, counter);48 block_cipher.xor(&pad, &pad, counter);
49 mem.copy(u8, dst[i..], pad[0 .. src.len - i]);49 mem.copy(u8, dst[i..], pad[0 .. src.len - i]);
lib/std/crypto/poly1305.zig+12-12
...@@ -7,9 +7,9 @@ const std = @import("../std.zig");...@@ -7,9 +7,9 @@ const std = @import("../std.zig");
7const mem = std.mem;7const mem = std.mem;
88
9pub const Poly1305 = struct {9pub const Poly1305 = struct {
10 pub const block_size: usize = 16;10 pub const block_length: usize = 16;
11 pub const mac_length = 16;11 pub const mac_length = 16;
12 pub const minimum_key_length = 32;12 pub const key_length = 32;
1313
14 // constant multiplier (from the secret key)14 // constant multiplier (from the secret key)
15 r: [3]u64,15 r: [3]u64,
...@@ -20,9 +20,9 @@ pub const Poly1305 = struct {...@@ -20,9 +20,9 @@ pub const Poly1305 = struct {
20 // how many bytes are waiting to be processed in a partial block20 // how many bytes are waiting to be processed in a partial block
21 leftover: usize = 0,21 leftover: usize = 0,
22 // partial block buffer22 // partial block buffer
23 buf: [block_size]u8 align(16) = undefined,23 buf: [block_length]u8 align(16) = undefined,
2424
25 pub fn init(key: *const [minimum_key_length]u8) Poly1305 {25 pub fn init(key: *const [key_length]u8) Poly1305 {
26 const t0 = mem.readIntLittle(u64, key[0..8]);26 const t0 = mem.readIntLittle(u64, key[0..8]);
27 const t1 = mem.readIntLittle(u64, key[8..16]);27 const t1 = mem.readIntLittle(u64, key[8..16]);
28 return Poly1305{28 return Poly1305{
...@@ -49,7 +49,7 @@ pub const Poly1305 = struct {...@@ -49,7 +49,7 @@ pub const Poly1305 = struct {
49 const s1 = r1 * (5 << 2);49 const s1 = r1 * (5 << 2);
50 const s2 = r2 * (5 << 2);50 const s2 = r2 * (5 << 2);
51 var i: usize = 0;51 var i: usize = 0;
52 while (i + block_size <= m.len) : (i += block_size) {52 while (i + block_length <= m.len) : (i += block_length) {
53 // h += m[i]53 // h += m[i]
54 const t0 = mem.readIntLittle(u64, m[i..][0..8]);54 const t0 = mem.readIntLittle(u64, m[i..][0..8]);
55 const t1 = mem.readIntLittle(u64, m[i + 8 ..][0..8]);55 const t1 = mem.readIntLittle(u64, m[i + 8 ..][0..8]);
...@@ -84,14 +84,14 @@ pub const Poly1305 = struct {...@@ -84,14 +84,14 @@ pub const Poly1305 = struct {
8484
85 // handle leftover85 // handle leftover
86 if (st.leftover > 0) {86 if (st.leftover > 0) {
87 const want = std.math.min(block_size - st.leftover, mb.len);87 const want = std.math.min(block_length - st.leftover, mb.len);
88 const mc = mb[0..want];88 const mc = mb[0..want];
89 for (mc) |x, i| {89 for (mc) |x, i| {
90 st.buf[st.leftover + i] = x;90 st.buf[st.leftover + i] = x;
91 }91 }
92 mb = mb[want..];92 mb = mb[want..];
93 st.leftover += want;93 st.leftover += want;
94 if (st.leftover < block_size) {94 if (st.leftover < block_length) {
95 return;95 return;
96 }96 }
97 st.blocks(&st.buf, false);97 st.blocks(&st.buf, false);
...@@ -99,8 +99,8 @@ pub const Poly1305 = struct {...@@ -99,8 +99,8 @@ pub const Poly1305 = struct {
99 }99 }
100100
101 // process full blocks101 // process full blocks
102 if (mb.len >= block_size) {102 if (mb.len >= block_length) {
103 const want = mb.len & ~(block_size - 1);103 const want = mb.len & ~(block_length - 1);
104 st.blocks(mb[0..want], false);104 st.blocks(mb[0..want], false);
105 mb = mb[want..];105 mb = mb[want..];
106 }106 }
...@@ -120,7 +120,7 @@ pub const Poly1305 = struct {...@@ -120,7 +120,7 @@ pub const Poly1305 = struct {
120 return;120 return;
121 }121 }
122 var i = st.leftover;122 var i = st.leftover;
123 while (i < block_size) : (i += 1) {123 while (i < block_length) : (i += 1) {
124 st.buf[i] = 0;124 st.buf[i] = 0;
125 }125 }
126 st.blocks(&st.buf);126 st.blocks(&st.buf);
...@@ -132,7 +132,7 @@ pub const Poly1305 = struct {...@@ -132,7 +132,7 @@ pub const Poly1305 = struct {
132 var i = st.leftover;132 var i = st.leftover;
133 st.buf[i] = 1;133 st.buf[i] = 1;
134 i += 1;134 i += 1;
135 while (i < block_size) : (i += 1) {135 while (i < block_length) : (i += 1) {
136 st.buf[i] = 0;136 st.buf[i] = 0;
137 }137 }
138 st.blocks(&st.buf, true);138 st.blocks(&st.buf, true);
...@@ -198,7 +198,7 @@ pub const Poly1305 = struct {...@@ -198,7 +198,7 @@ pub const Poly1305 = struct {
198 std.mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Poly1305)]);198 std.mem.secureZero(u8, @ptrCast([*]u8, st)[0..@sizeOf(Poly1305)]);
199 }199 }
200200
201 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [minimum_key_length]u8) void {201 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
202 var st = Poly1305.init(key);202 var st = Poly1305.init(key);
203 st.update(msg);203 st.update(msg);
204 st.final(out);204 st.final(out);
lib/std/crypto/sha1.zig+88-93
...@@ -6,7 +6,6 @@...@@ -6,7 +6,6 @@
6const std = @import("../std.zig");6const std = @import("../std.zig");
7const mem = std.mem;7const mem = std.mem;
8const math = std.math;8const math = std.math;
9const debug = std.debug;
109
11const RoundParam = struct {10const RoundParam = struct {
12 a: usize,11 a: usize,
...@@ -17,7 +16,7 @@ const RoundParam = struct {...@@ -17,7 +16,7 @@ const RoundParam = struct {
17 i: u32,16 i: u32,
18};17};
1918
20fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) RoundParam {19fn roundParam(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) RoundParam {
21 return RoundParam{20 return RoundParam{
22 .a = a,21 .a = a,
23 .b = b,22 .b = b,
...@@ -55,7 +54,7 @@ pub const Sha1 = struct {...@@ -55,7 +54,7 @@ pub const Sha1 = struct {
55 };54 };
56 }55 }
5756
58 pub fn hash(b: []const u8, out: []u8, options: Options) void {57 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
59 var d = Sha1.init(options);58 var d = Sha1.init(options);
60 d.update(b);59 d.update(b);
61 d.final(out);60 d.final(out);
...@@ -75,7 +74,7 @@ pub const Sha1 = struct {...@@ -75,7 +74,7 @@ pub const Sha1 = struct {
7574
76 // Full middle blocks.75 // Full middle blocks.
77 while (off + 64 <= b.len) : (off += 64) {76 while (off + 64 <= b.len) : (off += 64) {
78 d.round(b[off .. off + 64]);77 d.round(b[off..][0..64]);
79 }78 }
8079
81 // Copy any remainder for next pass.80 // Copy any remainder for next pass.
...@@ -85,9 +84,7 @@ pub const Sha1 = struct {...@@ -85,9 +84,7 @@ pub const Sha1 = struct {
85 d.total_len += b.len;84 d.total_len += b.len;
86 }85 }
8786
88 pub fn final(d: *Self, out: []u8) void {87 pub fn final(d: *Self, out: *[digest_length]u8) void {
89 debug.assert(out.len >= 20);
90
91 // The buffer here will never be completely full.88 // The buffer here will never be completely full.
92 mem.set(u8, d.buf[d.buf_len..], 0);89 mem.set(u8, d.buf[d.buf_len..], 0);
9390
...@@ -117,9 +114,7 @@ pub const Sha1 = struct {...@@ -117,9 +114,7 @@ pub const Sha1 = struct {
117 }114 }
118 }115 }
119116
120 fn round(d: *Self, b: []const u8) void {117 fn round(d: *Self, b: *const [64]u8) void {
121 debug.assert(b.len == 64);
122
123 var s: [16]u32 = undefined;118 var s: [16]u32 = undefined;
124119
125 var v: [5]u32 = [_]u32{120 var v: [5]u32 = [_]u32{
...@@ -131,22 +126,22 @@ pub const Sha1 = struct {...@@ -131,22 +126,22 @@ pub const Sha1 = struct {
131 };126 };
132127
133 const round0a = comptime [_]RoundParam{128 const round0a = comptime [_]RoundParam{
134 Rp(0, 1, 2, 3, 4, 0),129 roundParam(0, 1, 2, 3, 4, 0),
135 Rp(4, 0, 1, 2, 3, 1),130 roundParam(4, 0, 1, 2, 3, 1),
136 Rp(3, 4, 0, 1, 2, 2),131 roundParam(3, 4, 0, 1, 2, 2),
137 Rp(2, 3, 4, 0, 1, 3),132 roundParam(2, 3, 4, 0, 1, 3),
138 Rp(1, 2, 3, 4, 0, 4),133 roundParam(1, 2, 3, 4, 0, 4),
139 Rp(0, 1, 2, 3, 4, 5),134 roundParam(0, 1, 2, 3, 4, 5),
140 Rp(4, 0, 1, 2, 3, 6),135 roundParam(4, 0, 1, 2, 3, 6),
141 Rp(3, 4, 0, 1, 2, 7),136 roundParam(3, 4, 0, 1, 2, 7),
142 Rp(2, 3, 4, 0, 1, 8),137 roundParam(2, 3, 4, 0, 1, 8),
143 Rp(1, 2, 3, 4, 0, 9),138 roundParam(1, 2, 3, 4, 0, 9),
144 Rp(0, 1, 2, 3, 4, 10),139 roundParam(0, 1, 2, 3, 4, 10),
145 Rp(4, 0, 1, 2, 3, 11),140 roundParam(4, 0, 1, 2, 3, 11),
146 Rp(3, 4, 0, 1, 2, 12),141 roundParam(3, 4, 0, 1, 2, 12),
147 Rp(2, 3, 4, 0, 1, 13),142 roundParam(2, 3, 4, 0, 1, 13),
148 Rp(1, 2, 3, 4, 0, 14),143 roundParam(1, 2, 3, 4, 0, 14),
149 Rp(0, 1, 2, 3, 4, 15),144 roundParam(0, 1, 2, 3, 4, 15),
150 };145 };
151 inline for (round0a) |r| {146 inline for (round0a) |r| {
152 s[r.i] = (@as(u32, b[r.i * 4 + 0]) << 24) | (@as(u32, b[r.i * 4 + 1]) << 16) | (@as(u32, b[r.i * 4 + 2]) << 8) | (@as(u32, b[r.i * 4 + 3]) << 0);147 s[r.i] = (@as(u32, b[r.i * 4 + 0]) << 24) | (@as(u32, b[r.i * 4 + 1]) << 16) | (@as(u32, b[r.i * 4 + 2]) << 8) | (@as(u32, b[r.i * 4 + 3]) << 0);
...@@ -156,10 +151,10 @@ pub const Sha1 = struct {...@@ -156,10 +151,10 @@ pub const Sha1 = struct {
156 }151 }
157152
158 const round0b = comptime [_]RoundParam{153 const round0b = comptime [_]RoundParam{
159 Rp(4, 0, 1, 2, 3, 16),154 roundParam(4, 0, 1, 2, 3, 16),
160 Rp(3, 4, 0, 1, 2, 17),155 roundParam(3, 4, 0, 1, 2, 17),
161 Rp(2, 3, 4, 0, 1, 18),156 roundParam(2, 3, 4, 0, 1, 18),
162 Rp(1, 2, 3, 4, 0, 19),157 roundParam(1, 2, 3, 4, 0, 19),
163 };158 };
164 inline for (round0b) |r| {159 inline for (round0b) |r| {
165 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];160 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
...@@ -170,26 +165,26 @@ pub const Sha1 = struct {...@@ -170,26 +165,26 @@ pub const Sha1 = struct {
170 }165 }
171166
172 const round1 = comptime [_]RoundParam{167 const round1 = comptime [_]RoundParam{
173 Rp(0, 1, 2, 3, 4, 20),168 roundParam(0, 1, 2, 3, 4, 20),
174 Rp(4, 0, 1, 2, 3, 21),169 roundParam(4, 0, 1, 2, 3, 21),
175 Rp(3, 4, 0, 1, 2, 22),170 roundParam(3, 4, 0, 1, 2, 22),
176 Rp(2, 3, 4, 0, 1, 23),171 roundParam(2, 3, 4, 0, 1, 23),
177 Rp(1, 2, 3, 4, 0, 24),172 roundParam(1, 2, 3, 4, 0, 24),
178 Rp(0, 1, 2, 3, 4, 25),173 roundParam(0, 1, 2, 3, 4, 25),
179 Rp(4, 0, 1, 2, 3, 26),174 roundParam(4, 0, 1, 2, 3, 26),
180 Rp(3, 4, 0, 1, 2, 27),175 roundParam(3, 4, 0, 1, 2, 27),
181 Rp(2, 3, 4, 0, 1, 28),176 roundParam(2, 3, 4, 0, 1, 28),
182 Rp(1, 2, 3, 4, 0, 29),177 roundParam(1, 2, 3, 4, 0, 29),
183 Rp(0, 1, 2, 3, 4, 30),178 roundParam(0, 1, 2, 3, 4, 30),
184 Rp(4, 0, 1, 2, 3, 31),179 roundParam(4, 0, 1, 2, 3, 31),
185 Rp(3, 4, 0, 1, 2, 32),180 roundParam(3, 4, 0, 1, 2, 32),
186 Rp(2, 3, 4, 0, 1, 33),181 roundParam(2, 3, 4, 0, 1, 33),
187 Rp(1, 2, 3, 4, 0, 34),182 roundParam(1, 2, 3, 4, 0, 34),
188 Rp(0, 1, 2, 3, 4, 35),183 roundParam(0, 1, 2, 3, 4, 35),
189 Rp(4, 0, 1, 2, 3, 36),184 roundParam(4, 0, 1, 2, 3, 36),
190 Rp(3, 4, 0, 1, 2, 37),185 roundParam(3, 4, 0, 1, 2, 37),
191 Rp(2, 3, 4, 0, 1, 38),186 roundParam(2, 3, 4, 0, 1, 38),
192 Rp(1, 2, 3, 4, 0, 39),187 roundParam(1, 2, 3, 4, 0, 39),
193 };188 };
194 inline for (round1) |r| {189 inline for (round1) |r| {
195 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];190 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
...@@ -200,26 +195,26 @@ pub const Sha1 = struct {...@@ -200,26 +195,26 @@ pub const Sha1 = struct {
200 }195 }
201196
202 const round2 = comptime [_]RoundParam{197 const round2 = comptime [_]RoundParam{
203 Rp(0, 1, 2, 3, 4, 40),198 roundParam(0, 1, 2, 3, 4, 40),
204 Rp(4, 0, 1, 2, 3, 41),199 roundParam(4, 0, 1, 2, 3, 41),
205 Rp(3, 4, 0, 1, 2, 42),200 roundParam(3, 4, 0, 1, 2, 42),
206 Rp(2, 3, 4, 0, 1, 43),201 roundParam(2, 3, 4, 0, 1, 43),
207 Rp(1, 2, 3, 4, 0, 44),202 roundParam(1, 2, 3, 4, 0, 44),
208 Rp(0, 1, 2, 3, 4, 45),203 roundParam(0, 1, 2, 3, 4, 45),
209 Rp(4, 0, 1, 2, 3, 46),204 roundParam(4, 0, 1, 2, 3, 46),
210 Rp(3, 4, 0, 1, 2, 47),205 roundParam(3, 4, 0, 1, 2, 47),
211 Rp(2, 3, 4, 0, 1, 48),206 roundParam(2, 3, 4, 0, 1, 48),
212 Rp(1, 2, 3, 4, 0, 49),207 roundParam(1, 2, 3, 4, 0, 49),
213 Rp(0, 1, 2, 3, 4, 50),208 roundParam(0, 1, 2, 3, 4, 50),
214 Rp(4, 0, 1, 2, 3, 51),209 roundParam(4, 0, 1, 2, 3, 51),
215 Rp(3, 4, 0, 1, 2, 52),210 roundParam(3, 4, 0, 1, 2, 52),
216 Rp(2, 3, 4, 0, 1, 53),211 roundParam(2, 3, 4, 0, 1, 53),
217 Rp(1, 2, 3, 4, 0, 54),212 roundParam(1, 2, 3, 4, 0, 54),
218 Rp(0, 1, 2, 3, 4, 55),213 roundParam(0, 1, 2, 3, 4, 55),
219 Rp(4, 0, 1, 2, 3, 56),214 roundParam(4, 0, 1, 2, 3, 56),
220 Rp(3, 4, 0, 1, 2, 57),215 roundParam(3, 4, 0, 1, 2, 57),
221 Rp(2, 3, 4, 0, 1, 58),216 roundParam(2, 3, 4, 0, 1, 58),
222 Rp(1, 2, 3, 4, 0, 59),217 roundParam(1, 2, 3, 4, 0, 59),
223 };218 };
224 inline for (round2) |r| {219 inline for (round2) |r| {
225 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];220 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
...@@ -230,26 +225,26 @@ pub const Sha1 = struct {...@@ -230,26 +225,26 @@ pub const Sha1 = struct {
230 }225 }
231226
232 const round3 = comptime [_]RoundParam{227 const round3 = comptime [_]RoundParam{
233 Rp(0, 1, 2, 3, 4, 60),228 roundParam(0, 1, 2, 3, 4, 60),
234 Rp(4, 0, 1, 2, 3, 61),229 roundParam(4, 0, 1, 2, 3, 61),
235 Rp(3, 4, 0, 1, 2, 62),230 roundParam(3, 4, 0, 1, 2, 62),
236 Rp(2, 3, 4, 0, 1, 63),231 roundParam(2, 3, 4, 0, 1, 63),
237 Rp(1, 2, 3, 4, 0, 64),232 roundParam(1, 2, 3, 4, 0, 64),
238 Rp(0, 1, 2, 3, 4, 65),233 roundParam(0, 1, 2, 3, 4, 65),
239 Rp(4, 0, 1, 2, 3, 66),234 roundParam(4, 0, 1, 2, 3, 66),
240 Rp(3, 4, 0, 1, 2, 67),235 roundParam(3, 4, 0, 1, 2, 67),
241 Rp(2, 3, 4, 0, 1, 68),236 roundParam(2, 3, 4, 0, 1, 68),
242 Rp(1, 2, 3, 4, 0, 69),237 roundParam(1, 2, 3, 4, 0, 69),
243 Rp(0, 1, 2, 3, 4, 70),238 roundParam(0, 1, 2, 3, 4, 70),
244 Rp(4, 0, 1, 2, 3, 71),239 roundParam(4, 0, 1, 2, 3, 71),
245 Rp(3, 4, 0, 1, 2, 72),240 roundParam(3, 4, 0, 1, 2, 72),
246 Rp(2, 3, 4, 0, 1, 73),241 roundParam(2, 3, 4, 0, 1, 73),
247 Rp(1, 2, 3, 4, 0, 74),242 roundParam(1, 2, 3, 4, 0, 74),
248 Rp(0, 1, 2, 3, 4, 75),243 roundParam(0, 1, 2, 3, 4, 75),
249 Rp(4, 0, 1, 2, 3, 76),244 roundParam(4, 0, 1, 2, 3, 76),
250 Rp(3, 4, 0, 1, 2, 77),245 roundParam(3, 4, 0, 1, 2, 77),
251 Rp(2, 3, 4, 0, 1, 78),246 roundParam(2, 3, 4, 0, 1, 78),
252 Rp(1, 2, 3, 4, 0, 79),247 roundParam(1, 2, 3, 4, 0, 79),
253 };248 };
254 inline for (round3) |r| {249 inline for (round3) |r| {
255 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];250 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
...@@ -279,19 +274,19 @@ test "sha1 streaming" {...@@ -279,19 +274,19 @@ test "sha1 streaming" {
279 var h = Sha1.init(.{});274 var h = Sha1.init(.{});
280 var out: [20]u8 = undefined;275 var out: [20]u8 = undefined;
281276
282 h.final(out[0..]);277 h.final(&out);
283 htest.assertEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709", out[0..]);278 htest.assertEqual("da39a3ee5e6b4b0d3255bfef95601890afd80709", out[0..]);
284279
285 h = Sha1.init(.{});280 h = Sha1.init(.{});
286 h.update("abc");281 h.update("abc");
287 h.final(out[0..]);282 h.final(&out);
288 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);283 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);
289284
290 h = Sha1.init(.{});285 h = Sha1.init(.{});
291 h.update("a");286 h.update("a");
292 h.update("b");287 h.update("b");
293 h.update("c");288 h.update("c");
294 h.final(out[0..]);289 h.final(&out);
295 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);290 htest.assertEqual("a9993e364706816aba3e25717850c26c9cd0d89d", out[0..]);
296}291}
297292
lib/std/crypto/sha2.zig+178-187
...@@ -6,7 +6,6 @@...@@ -6,7 +6,6 @@
6const std = @import("../std.zig");6const std = @import("../std.zig");
7const mem = std.mem;7const mem = std.mem;
8const math = std.math;8const math = std.math;
9const debug = std.debug;
10const htest = @import("test.zig");9const htest = @import("test.zig");
1110
12/////////////////////11/////////////////////
...@@ -25,7 +24,7 @@ const RoundParam256 = struct {...@@ -25,7 +24,7 @@ const RoundParam256 = struct {
25 k: u32,24 k: u32,
26};25};
2726
28fn Rp256(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u32) RoundParam256 {27fn roundParam256(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u32) RoundParam256 {
29 return RoundParam256{28 return RoundParam256{
30 .a = a,29 .a = a,
31 .b = b,30 .b = b,
...@@ -49,7 +48,7 @@ const Sha2Params32 = struct {...@@ -49,7 +48,7 @@ const Sha2Params32 = struct {
49 iv5: u32,48 iv5: u32,
50 iv6: u32,49 iv6: u32,
51 iv7: u32,50 iv7: u32,
52 out_len: usize,51 digest_bits: usize,
53};52};
5453
55const Sha224Params = Sha2Params32{54const Sha224Params = Sha2Params32{
...@@ -61,7 +60,7 @@ const Sha224Params = Sha2Params32{...@@ -61,7 +60,7 @@ const Sha224Params = Sha2Params32{
61 .iv5 = 0x68581511,60 .iv5 = 0x68581511,
62 .iv6 = 0x64F98FA7,61 .iv6 = 0x64F98FA7,
63 .iv7 = 0xBEFA4FA4,62 .iv7 = 0xBEFA4FA4,
64 .out_len = 224,63 .digest_bits = 224,
65};64};
6665
67const Sha256Params = Sha2Params32{66const Sha256Params = Sha2Params32{
...@@ -73,20 +72,20 @@ const Sha256Params = Sha2Params32{...@@ -73,20 +72,20 @@ const Sha256Params = Sha2Params32{
73 .iv5 = 0x9B05688C,72 .iv5 = 0x9B05688C,
74 .iv6 = 0x1F83D9AB,73 .iv6 = 0x1F83D9AB,
75 .iv7 = 0x5BE0CD19,74 .iv7 = 0x5BE0CD19,
76 .out_len = 256,75 .digest_bits = 256,
77};76};
7877
79/// SHA-22478/// SHA-224
80pub const Sha224 = Sha2_32(Sha224Params);79pub const Sha224 = Sha2x32(Sha224Params);
8180
82/// SHA-25681/// SHA-256
83pub const Sha256 = Sha2_32(Sha256Params);82pub const Sha256 = Sha2x32(Sha256Params);
8483
85fn Sha2_32(comptime params: Sha2Params32) type {84fn Sha2x32(comptime params: Sha2Params32) type {
86 return struct {85 return struct {
87 const Self = @This();86 const Self = @This();
88 pub const block_length = 64;87 pub const block_length = 64;
89 pub const digest_length = params.out_len / 8;88 pub const digest_length = params.digest_bits / 8;
90 pub const Options = struct {};89 pub const Options = struct {};
9190
92 s: [8]u32,91 s: [8]u32,
...@@ -110,7 +109,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -110,7 +109,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
110 };109 };
111 }110 }
112111
113 pub fn hash(b: []const u8, out: []u8, options: Options) void {112 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
114 var d = Self.init(options);113 var d = Self.init(options);
115 d.update(b);114 d.update(b);
116 d.final(out);115 d.final(out);
...@@ -124,13 +123,13 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -124,13 +123,13 @@ fn Sha2_32(comptime params: Sha2Params32) type {
124 off += 64 - d.buf_len;123 off += 64 - d.buf_len;
125 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);124 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
126125
127 d.round(d.buf[0..]);126 d.round(&d.buf);
128 d.buf_len = 0;127 d.buf_len = 0;
129 }128 }
130129
131 // Full middle blocks.130 // Full middle blocks.
132 while (off + 64 <= b.len) : (off += 64) {131 while (off + 64 <= b.len) : (off += 64) {
133 d.round(b[off .. off + 64]);132 d.round(b[off..][0..64]);
134 }133 }
135134
136 // Copy any remainder for next pass.135 // Copy any remainder for next pass.
...@@ -140,9 +139,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -140,9 +139,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
140 d.total_len += b.len;139 d.total_len += b.len;
141 }140 }
142141
143 pub fn final(d: *Self, out: []u8) void {142 pub fn final(d: *Self, out: *[digest_length]u8) void {
144 debug.assert(out.len >= params.out_len / 8);
145
146 // The buffer here will never be completely full.143 // The buffer here will never be completely full.
147 mem.set(u8, d.buf[d.buf_len..], 0);144 mem.set(u8, d.buf[d.buf_len..], 0);
148145
...@@ -152,7 +149,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -152,7 +149,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
152149
153 // > 448 mod 512 so need to add an extra round to wrap around.150 // > 448 mod 512 so need to add an extra round to wrap around.
154 if (64 - d.buf_len < 8) {151 if (64 - d.buf_len < 8) {
155 d.round(d.buf[0..]);152 d.round(&d.buf);
156 mem.set(u8, d.buf[0..], 0);153 mem.set(u8, d.buf[0..], 0);
157 }154 }
158155
...@@ -165,19 +162,17 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -165,19 +162,17 @@ fn Sha2_32(comptime params: Sha2Params32) type {
165 len >>= 8;162 len >>= 8;
166 }163 }
167164
168 d.round(d.buf[0..]);165 d.round(&d.buf);
169166
170 // May truncate for possible 224 output167 // May truncate for possible 224 output
171 const rr = d.s[0 .. params.out_len / 32];168 const rr = d.s[0 .. params.digest_bits / 32];
172169
173 for (rr) |s, j| {170 for (rr) |s, j| {
174 mem.writeIntBig(u32, out[4 * j ..][0..4], s);171 mem.writeIntBig(u32, out[4 * j ..][0..4], s);
175 }172 }
176 }173 }
177174
178 fn round(d: *Self, b: []const u8) void {175 fn round(d: *Self, b: *const [64]u8) void {
179 debug.assert(b.len == 64);
180
181 var s: [64]u32 = undefined;176 var s: [64]u32 = undefined;
182177
183 var i: usize = 0;178 var i: usize = 0;
...@@ -204,70 +199,70 @@ fn Sha2_32(comptime params: Sha2Params32) type {...@@ -204,70 +199,70 @@ fn Sha2_32(comptime params: Sha2Params32) type {
204 };199 };
205200
206 const round0 = comptime [_]RoundParam256{201 const round0 = comptime [_]RoundParam256{
207 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98),202 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98),
208 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x71374491),203 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x71374491),
209 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCF),204 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCF),
210 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA5),205 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA5),
211 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25B),206 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25B),
212 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1),207 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1),
213 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4),208 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4),
214 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5),209 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5),
215 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98),210 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98),
216 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B01),211 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B01),
217 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE),212 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE),
218 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3),213 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3),
219 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74),214 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74),
220 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE),215 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE),
221 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A7),216 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A7),
222 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174),217 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174),
223 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C1),218 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C1),
224 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786),219 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786),
225 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC6),220 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC6),
226 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC),221 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC),
227 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F),222 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F),
228 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA),223 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA),
229 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DC),224 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DC),
230 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA),225 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA),
231 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152),226 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152),
232 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D),227 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D),
233 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C8),228 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C8),
234 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7),229 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7),
235 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF3),230 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF3),
236 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147),231 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147),
237 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351),232 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351),
238 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x14292967),233 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x14292967),
239 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A85),234 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A85),
240 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B2138),235 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B2138),
241 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC),236 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC),
242 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D13),237 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D13),
243 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A7354),238 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A7354),
244 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB),239 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB),
245 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E),240 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E),
246 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C85),241 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C85),
247 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A1),242 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A1),
248 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664B),243 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664B),
249 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70),244 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70),
250 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A3),245 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A3),
251 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819),246 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819),
252 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD6990624),247 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD6990624),
253 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E3585),248 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E3585),
254 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA070),249 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA070),
255 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116),250 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116),
256 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C08),251 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C08),
257 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774C),252 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774C),
258 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5),253 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5),
259 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3),254 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3),
260 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4A),255 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4A),
261 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F),256 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F),
262 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3),257 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3),
263 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE),258 roundParam256(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE),
264 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F),259 roundParam256(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F),
265 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814),260 roundParam256(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814),
266 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC70208),261 roundParam256(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC70208),
267 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA),262 roundParam256(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA),
268 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEB),263 roundParam256(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEB),
269 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7),264 roundParam256(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7),
270 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2),265 roundParam256(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2),
271 };266 };
272 inline for (round0) |r| {267 inline for (round0) |r| {
273 v[r.h] = v[r.h] +% (math.rotr(u32, v[r.e], @as(u32, 6)) ^ math.rotr(u32, v[r.e], @as(u32, 11)) ^ math.rotr(u32, v[r.e], @as(u32, 25))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];268 v[r.h] = v[r.h] +% (math.rotr(u32, v[r.e], @as(u32, 6)) ^ math.rotr(u32, v[r.e], @as(u32, 11)) ^ math.rotr(u32, v[r.e], @as(u32, 25))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];
...@@ -366,7 +361,7 @@ const RoundParam512 = struct {...@@ -366,7 +361,7 @@ const RoundParam512 = struct {
366 k: u64,361 k: u64,
367};362};
368363
369fn Rp512(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u64) RoundParam512 {364fn roundParam512(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u64) RoundParam512 {
370 return RoundParam512{365 return RoundParam512{
371 .a = a,366 .a = a,
372 .b = b,367 .b = b,
...@@ -390,7 +385,7 @@ const Sha2Params64 = struct {...@@ -390,7 +385,7 @@ const Sha2Params64 = struct {
390 iv5: u64,385 iv5: u64,
391 iv6: u64,386 iv6: u64,
392 iv7: u64,387 iv7: u64,
393 out_len: usize,388 digest_bits: usize,
394};389};
395390
396const Sha384Params = Sha2Params64{391const Sha384Params = Sha2Params64{
...@@ -402,7 +397,7 @@ const Sha384Params = Sha2Params64{...@@ -402,7 +397,7 @@ const Sha384Params = Sha2Params64{
402 .iv5 = 0x8EB44A8768581511,397 .iv5 = 0x8EB44A8768581511,
403 .iv6 = 0xDB0C2E0D64F98FA7,398 .iv6 = 0xDB0C2E0D64F98FA7,
404 .iv7 = 0x47B5481DBEFA4FA4,399 .iv7 = 0x47B5481DBEFA4FA4,
405 .out_len = 384,400 .digest_bits = 384,
406};401};
407402
408const Sha512Params = Sha2Params64{403const Sha512Params = Sha2Params64{
...@@ -414,7 +409,7 @@ const Sha512Params = Sha2Params64{...@@ -414,7 +409,7 @@ const Sha512Params = Sha2Params64{
414 .iv5 = 0x9B05688C2B3E6C1F,409 .iv5 = 0x9B05688C2B3E6C1F,
415 .iv6 = 0x1F83D9ABFB41BD6B,410 .iv6 = 0x1F83D9ABFB41BD6B,
416 .iv7 = 0x5BE0CD19137E2179,411 .iv7 = 0x5BE0CD19137E2179,
417 .out_len = 512,412 .digest_bits = 512,
418};413};
419414
420const Sha512256Params = Sha2Params64{415const Sha512256Params = Sha2Params64{
...@@ -426,7 +421,7 @@ const Sha512256Params = Sha2Params64{...@@ -426,7 +421,7 @@ const Sha512256Params = Sha2Params64{
426 .iv5 = 0xBE5E1E2553863992,421 .iv5 = 0xBE5E1E2553863992,
427 .iv6 = 0x2B0199FC2C85B8AA,422 .iv6 = 0x2B0199FC2C85B8AA,
428 .iv7 = 0x0EB72DDC81C52CA2,423 .iv7 = 0x0EB72DDC81C52CA2,
429 .out_len = 256,424 .digest_bits = 256,
430};425};
431426
432const Sha512T256Params = Sha2Params64{427const Sha512T256Params = Sha2Params64{
...@@ -438,26 +433,26 @@ const Sha512T256Params = Sha2Params64{...@@ -438,26 +433,26 @@ const Sha512T256Params = Sha2Params64{
438 .iv5 = 0x9B05688C2B3E6C1F,433 .iv5 = 0x9B05688C2B3E6C1F,
439 .iv6 = 0x1F83D9ABFB41BD6B,434 .iv6 = 0x1F83D9ABFB41BD6B,
440 .iv7 = 0x5BE0CD19137E2179,435 .iv7 = 0x5BE0CD19137E2179,
441 .out_len = 256,436 .digest_bits = 256,
442};437};
443438
444/// SHA-384439/// SHA-384
445pub const Sha384 = Sha2_64(Sha384Params);440pub const Sha384 = Sha2x64(Sha384Params);
446441
447/// SHA-512442/// SHA-512
448pub const Sha512 = Sha2_64(Sha512Params);443pub const Sha512 = Sha2x64(Sha512Params);
449444
450/// SHA-512/256445/// SHA-512/256
451pub const Sha512256 = Sha2_64(Sha512256Params);446pub const Sha512256 = Sha2x64(Sha512256Params);
452447
453/// Truncated SHA-512448/// Truncated SHA-512
454pub const Sha512T256 = Sha2_64(Sha512T256Params);449pub const Sha512T256 = Sha2x64(Sha512T256Params);
455450
456fn Sha2_64(comptime params: Sha2Params64) type {451fn Sha2x64(comptime params: Sha2Params64) type {
457 return struct {452 return struct {
458 const Self = @This();453 const Self = @This();
459 pub const block_length = 128;454 pub const block_length = 128;
460 pub const digest_length = params.out_len / 8;455 pub const digest_length = params.digest_bits / 8;
461 pub const Options = struct {};456 pub const Options = struct {};
462457
463 s: [8]u64,458 s: [8]u64,
...@@ -481,7 +476,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -481,7 +476,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
481 };476 };
482 }477 }
483478
484 pub fn hash(b: []const u8, out: []u8, options: Options) void {479 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
485 var d = Self.init(options);480 var d = Self.init(options);
486 d.update(b);481 d.update(b);
487 d.final(out);482 d.final(out);
...@@ -495,13 +490,13 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -495,13 +490,13 @@ fn Sha2_64(comptime params: Sha2Params64) type {
495 off += 128 - d.buf_len;490 off += 128 - d.buf_len;
496 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);491 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
497492
498 d.round(d.buf[0..]);493 d.round(&d.buf);
499 d.buf_len = 0;494 d.buf_len = 0;
500 }495 }
501496
502 // Full middle blocks.497 // Full middle blocks.
503 while (off + 128 <= b.len) : (off += 128) {498 while (off + 128 <= b.len) : (off += 128) {
504 d.round(b[off .. off + 128]);499 d.round(b[off..][0..128]);
505 }500 }
506501
507 // Copy any remainder for next pass.502 // Copy any remainder for next pass.
...@@ -511,9 +506,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -511,9 +506,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
511 d.total_len += b.len;506 d.total_len += b.len;
512 }507 }
513508
514 pub fn final(d: *Self, out: []u8) void {509 pub fn final(d: *Self, out: *[digest_length]u8) void {
515 debug.assert(out.len >= params.out_len / 8);
516
517 // The buffer here will never be completely full.510 // The buffer here will never be completely full.
518 mem.set(u8, d.buf[d.buf_len..], 0);511 mem.set(u8, d.buf[d.buf_len..], 0);
519512
...@@ -539,16 +532,14 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -539,16 +532,14 @@ fn Sha2_64(comptime params: Sha2Params64) type {
539 d.round(d.buf[0..]);532 d.round(d.buf[0..]);
540533
541 // May truncate for possible 384 output534 // May truncate for possible 384 output
542 const rr = d.s[0 .. params.out_len / 64];535 const rr = d.s[0 .. params.digest_bits / 64];
543536
544 for (rr) |s, j| {537 for (rr) |s, j| {
545 mem.writeIntBig(u64, out[8 * j ..][0..8], s);538 mem.writeIntBig(u64, out[8 * j ..][0..8], s);
546 }539 }
547 }540 }
548541
549 fn round(d: *Self, b: []const u8) void {542 fn round(d: *Self, b: *const [128]u8) void {
550 debug.assert(b.len == 128);
551
552 var s: [80]u64 = undefined;543 var s: [80]u64 = undefined;
553544
554 var i: usize = 0;545 var i: usize = 0;
...@@ -581,86 +572,86 @@ fn Sha2_64(comptime params: Sha2Params64) type {...@@ -581,86 +572,86 @@ fn Sha2_64(comptime params: Sha2Params64) type {
581 };572 };
582573
583 const round0 = comptime [_]RoundParam512{574 const round0 = comptime [_]RoundParam512{
584 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98D728AE22),575 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98D728AE22),
585 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x7137449123EF65CD),576 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x7137449123EF65CD),
586 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCFEC4D3B2F),577 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCFEC4D3B2F),
587 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA58189DBBC),578 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA58189DBBC),
588 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25BF348B538),579 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25BF348B538),
589 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1B605D019),580 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1B605D019),
590 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4AF194F9B),581 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4AF194F9B),
591 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5DA6D8118),582 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5DA6D8118),
592 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98A3030242),583 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98A3030242),
593 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B0145706FBE),584 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B0145706FBE),
594 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE4EE4B28C),585 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE4EE4B28C),
595 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3D5FFB4E2),586 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3D5FFB4E2),
596 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74F27B896F),587 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74F27B896F),
597 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE3B1696B1),588 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE3B1696B1),
598 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A725C71235),589 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A725C71235),
599 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174CF692694),590 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174CF692694),
600 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C19EF14AD2),591 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C19EF14AD2),
601 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786384F25E3),592 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786384F25E3),
602 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC68B8CD5B5),593 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC68B8CD5B5),
603 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC77AC9C65),594 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC77AC9C65),
604 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F592B0275),595 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F592B0275),
605 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA6EA6E483),596 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA6EA6E483),
606 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DCBD41FBD4),597 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DCBD41FBD4),
607 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA831153B5),598 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA831153B5),
608 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152EE66DFAB),599 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152EE66DFAB),
609 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D2DB43210),600 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D2DB43210),
610 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C898FB213F),601 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C898FB213F),
611 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7BEEF0EE4),602 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7BEEF0EE4),
612 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF33DA88FC2),603 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF33DA88FC2),
613 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147930AA725),604 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147930AA725),
614 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351E003826F),605 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351E003826F),
615 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x142929670A0E6E70),606 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x142929670A0E6E70),
616 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A8546D22FFC),607 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A8546D22FFC),
617 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B21385C26C926),608 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B21385C26C926),
618 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC5AC42AED),609 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC5AC42AED),
619 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D139D95B3DF),610 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D139D95B3DF),
620 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A73548BAF63DE),611 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A73548BAF63DE),
621 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB3C77B2A8),612 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB3C77B2A8),
622 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E47EDAEE6),613 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E47EDAEE6),
623 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C851482353B),614 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C851482353B),
624 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A14CF10364),615 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A14CF10364),
625 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664BBC423001),616 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664BBC423001),
626 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70D0F89791),617 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70D0F89791),
627 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A30654BE30),618 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A30654BE30),
628 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819D6EF5218),619 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819D6EF5218),
629 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD69906245565A910),620 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD69906245565A910),
630 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E35855771202A),621 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E35855771202A),
631 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA07032BBD1B8),622 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA07032BBD1B8),
632 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116B8D2D0C8),623 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116B8D2D0C8),
633 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C085141AB53),624 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C085141AB53),
634 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774CDF8EEB99),625 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774CDF8EEB99),
635 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5E19B48A8),626 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5E19B48A8),
636 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3C5C95A63),627 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3C5C95A63),
637 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4AE3418ACB),628 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4AE3418ACB),
638 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F7763E373),629 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F7763E373),
639 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3D6B2B8A3),630 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3D6B2B8A3),
640 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE5DEFB2FC),631 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE5DEFB2FC),
641 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F43172F60),632 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F43172F60),
642 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814A1F0AB72),633 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814A1F0AB72),
643 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC702081A6439EC),634 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC702081A6439EC),
644 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA23631E28),635 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA23631E28),
645 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEBDE82BDE9),636 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEBDE82BDE9),
646 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7B2C67915),637 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7B2C67915),
647 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2E372532B),638 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2E372532B),
648 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 64, 0xCA273ECEEA26619C),639 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 64, 0xCA273ECEEA26619C),
649 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 65, 0xD186B8C721C0C207),640 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 65, 0xD186B8C721C0C207),
650 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 66, 0xEADA7DD6CDE0EB1E),641 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 66, 0xEADA7DD6CDE0EB1E),
651 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 67, 0xF57D4F7FEE6ED178),642 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 67, 0xF57D4F7FEE6ED178),
652 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 68, 0x06F067AA72176FBA),643 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 68, 0x06F067AA72176FBA),
653 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 69, 0x0A637DC5A2C898A6),644 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 69, 0x0A637DC5A2C898A6),
654 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 70, 0x113F9804BEF90DAE),645 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 70, 0x113F9804BEF90DAE),
655 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 71, 0x1B710B35131C471B),646 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 71, 0x1B710B35131C471B),
656 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 72, 0x28DB77F523047D84),647 roundParam512(0, 1, 2, 3, 4, 5, 6, 7, 72, 0x28DB77F523047D84),
657 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 73, 0x32CAAB7B40C72493),648 roundParam512(7, 0, 1, 2, 3, 4, 5, 6, 73, 0x32CAAB7B40C72493),
658 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 74, 0x3C9EBE0A15C9BEBC),649 roundParam512(6, 7, 0, 1, 2, 3, 4, 5, 74, 0x3C9EBE0A15C9BEBC),
659 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 75, 0x431D67C49C100D4C),650 roundParam512(5, 6, 7, 0, 1, 2, 3, 4, 75, 0x431D67C49C100D4C),
660 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 76, 0x4CC5D4BECB3E42B6),651 roundParam512(4, 5, 6, 7, 0, 1, 2, 3, 76, 0x4CC5D4BECB3E42B6),
661 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 77, 0x597F299CFC657E2A),652 roundParam512(3, 4, 5, 6, 7, 0, 1, 2, 77, 0x597F299CFC657E2A),
662 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 78, 0x5FCB6FAB3AD6FAEC),653 roundParam512(2, 3, 4, 5, 6, 7, 0, 1, 78, 0x5FCB6FAB3AD6FAEC),
663 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 79, 0x6C44198C4A475817),654 roundParam512(1, 2, 3, 4, 5, 6, 7, 0, 79, 0x6C44198C4A475817),
664 };655 };
665 inline for (round0) |r| {656 inline for (round0) |r| {
666 v[r.h] = v[r.h] +% (math.rotr(u64, v[r.e], @as(u64, 14)) ^ math.rotr(u64, v[r.e], @as(u64, 18)) ^ math.rotr(u64, v[r.e], @as(u64, 41))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];657 v[r.h] = v[r.h] +% (math.rotr(u64, v[r.e], @as(u64, 14)) ^ math.rotr(u64, v[r.e], @as(u64, 18)) ^ math.rotr(u64, v[r.e], @as(u64, 41))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];
lib/std/crypto/sha3.zig+6-8
...@@ -29,7 +29,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {...@@ -29,7 +29,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
29 return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) };29 return Self{ .s = [_]u8{0} ** 200, .offset = 0, .rate = 200 - (bits / 4) };
30 }30 }
3131
32 pub fn hash(b: []const u8, out: []u8, options: Options) void {32 pub fn hash(b: []const u8, out: *[digest_length]u8, options: Options) void {
33 var d = Self.init(options);33 var d = Self.init(options);
34 d.update(b);34 d.update(b);
35 d.final(out);35 d.final(out);
...@@ -46,7 +46,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {...@@ -46,7 +46,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
46 for (d.s[offset .. offset + rate]) |*r, i|46 for (d.s[offset .. offset + rate]) |*r, i|
47 r.* ^= b[ip..][i];47 r.* ^= b[ip..][i];
4848
49 keccak_f(1600, d.s[0..]);49 keccakF(1600, &d.s);
5050
51 ip += rate;51 ip += rate;
52 len -= rate;52 len -= rate;
...@@ -60,12 +60,12 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {...@@ -60,12 +60,12 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
60 d.offset = offset + len;60 d.offset = offset + len;
61 }61 }
6262
63 pub fn final(d: *Self, out: []u8) void {63 pub fn final(d: *Self, out: *[digest_length]u8) void {
64 // padding64 // padding
65 d.s[d.offset] ^= delim;65 d.s[d.offset] ^= delim;
66 d.s[d.rate - 1] ^= 0x80;66 d.s[d.rate - 1] ^= 0x80;
6767
68 keccak_f(1600, d.s[0..]);68 keccakF(1600, &d.s);
6969
70 // squeeze70 // squeeze
71 var op: usize = 0;71 var op: usize = 0;
...@@ -73,7 +73,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {...@@ -73,7 +73,7 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
7373
74 while (len >= d.rate) {74 while (len >= d.rate) {
75 mem.copy(u8, out[op..], d.s[0..d.rate]);75 mem.copy(u8, out[op..], d.s[0..d.rate]);
76 keccak_f(1600, d.s[0..]);76 keccakF(1600, &d.s);
77 op += d.rate;77 op += d.rate;
78 len -= d.rate;78 len -= d.rate;
79 }79 }
...@@ -104,9 +104,7 @@ const M5 = [_]usize{...@@ -104,9 +104,7 @@ const M5 = [_]usize{
104 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,104 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
105};105};
106106
107fn keccak_f(comptime F: usize, d: []u8) void {107fn keccakF(comptime F: usize, d: *[F / 8]u8) void {
108 debug.assert(d.len == F / 8);
109
110 const B = F / 25;108 const B = F / 25;
111 const no_rounds = comptime x: {109 const no_rounds = comptime x: {
112 break :x 12 + 2 * math.log2(B);110 break :x 12 + 2 * math.log2(B);
lib/std/crypto/siphash.zig+15-19
...@@ -51,8 +51,9 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -51,8 +51,9 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
5151
52 return struct {52 return struct {
53 const Self = @This();53 const Self = @This();
54 const digest_size = 64;54 const block_length = 64;
55 const block_size = 64;55 const digest_length = 64;
56 const key_length = 16;
5657
57 v0: u64,58 v0: u64,
58 v1: u64,59 v1: u64,
...@@ -60,9 +61,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -60,9 +61,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
60 v3: u64,61 v3: u64,
61 msg_len: u8,62 msg_len: u8,
6263
63 pub fn init(key: []const u8) Self {64 pub fn init(key: *const [key_length]u8) Self {
64 assert(key.len >= 16);
65
66 const k0 = mem.readIntLittle(u64, key[0..8]);65 const k0 = mem.readIntLittle(u64, key[0..8]);
67 const k1 = mem.readIntLittle(u64, key[8..16]);66 const k1 = mem.readIntLittle(u64, key[8..16]);
6867
...@@ -86,7 +85,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -86,7 +85,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
8685
87 var off: usize = 0;86 var off: usize = 0;
88 while (off < b.len) : (off += 8) {87 while (off < b.len) : (off += 8) {
89 @call(.{ .modifier = .always_inline }, self.round, .{b[off .. off + 8]});88 @call(.{ .modifier = .always_inline }, self.round, .{b[off..][0..8].*});
90 }89 }
9190
92 self.msg_len +%= @truncate(u8, b.len);91 self.msg_len +%= @truncate(u8, b.len);
...@@ -100,7 +99,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -100,7 +99,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
100 var buf = [_]u8{0} ** 8;99 var buf = [_]u8{0} ** 8;
101 mem.copy(u8, buf[0..], b[0..]);100 mem.copy(u8, buf[0..], b[0..]);
102 buf[7] = self.msg_len;101 buf[7] = self.msg_len;
103 self.round(buf[0..]);102 self.round(buf);
104103
105 if (T == u128) {104 if (T == u128) {
106 self.v2 ^= 0xee;105 self.v2 ^= 0xee;
...@@ -132,9 +131,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -132,9 +131,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
132 return (@as(u128, b2) << 64) | b1;131 return (@as(u128, b2) << 64) | b1;
133 }132 }
134133
135 fn round(self: *Self, b: []const u8) void {134 fn round(self: *Self, b: [8]u8) void {
136 assert(b.len == 8);
137
138 const m = mem.readIntLittle(u64, b[0..8]);135 const m = mem.readIntLittle(u64, b[0..8]);
139 self.v3 ^= m;136 self.v3 ^= m;
140137
...@@ -165,7 +162,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round...@@ -165,7 +162,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
165 d.v2 = math.rotl(u64, d.v2, @as(u64, 32));162 d.v2 = math.rotl(u64, d.v2, @as(u64, 32));
166 }163 }
167164
168 pub fn hash(msg: []const u8, key: []const u8) T {165 pub fn hash(msg: []const u8, key: *const [key_length]u8) T {
169 const aligned_len = msg.len - (msg.len % 8);166 const aligned_len = msg.len - (msg.len % 8);
170 var c = Self.init(key);167 var c = Self.init(key);
171 @call(.{ .modifier = .always_inline }, c.update, .{msg[0..aligned_len]});168 @call(.{ .modifier = .always_inline }, c.update, .{msg[0..aligned_len]});
...@@ -181,7 +178,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)...@@ -181,7 +178,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
181 return struct {178 return struct {
182 const State = SipHashStateless(T, c_rounds, d_rounds);179 const State = SipHashStateless(T, c_rounds, d_rounds);
183 const Self = @This();180 const Self = @This();
184 pub const minimum_key_length = 16;181 pub const key_length = 16;
185 pub const mac_length = @sizeOf(T);182 pub const mac_length = @sizeOf(T);
186 pub const block_length = 8;183 pub const block_length = 8;
187184
...@@ -190,7 +187,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)...@@ -190,7 +187,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
190 buf_len: usize,187 buf_len: usize,
191188
192 /// Initialize a state for a SipHash function189 /// Initialize a state for a SipHash function
193 pub fn init(key: []const u8) Self {190 pub fn init(key: *const [key_length]u8) Self {
194 return Self{191 return Self{
195 .state = State.init(key),192 .state = State.init(key),
196 .buf = undefined,193 .buf = undefined,
...@@ -219,16 +216,15 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)...@@ -219,16 +216,15 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
219216
220 /// Return an authentication tag for the current state217 /// Return an authentication tag for the current state
221 /// Assumes `out` is less than or equal to `mac_length`.218 /// Assumes `out` is less than or equal to `mac_length`.
222 pub fn final(self: *Self, out: []u8) void {219 pub fn final(self: *Self, out: *[mac_length]u8) void {
223 std.debug.assert(out.len <= mac_length);220 mem.writeIntLittle(T, out, self.state.final(self.buf[0..self.buf_len]));
224 mem.writeIntLittle(T, out[0..mac_length], self.state.final(self.buf[0..self.buf_len]));
225 }221 }
226222
227 /// Return an authentication tag for a message and a key223 /// Return an authentication tag for a message and a key
228 pub fn create(out: []u8, msg: []const u8, key: []const u8) void {224 pub fn create(out: *[mac_length]u8, msg: []const u8, key: *const [key_length]u8) void {
229 var ctx = Self.init(key);225 var ctx = Self.init(key);
230 ctx.update(msg);226 ctx.update(msg);
231 ctx.final(out[0..]);227 ctx.final(out);
232 }228 }
233229
234 /// Return an authentication tag for the current state, as an integer230 /// Return an authentication tag for the current state, as an integer
...@@ -237,7 +233,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)...@@ -237,7 +233,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
237 }233 }
238234
239 /// Return an authentication tag for a message and a key, as an integer235 /// Return an authentication tag for a message and a key, as an integer
240 pub fn toInt(msg: []const u8, key: []const u8) T {236 pub fn toInt(msg: []const u8, key: *const [key_length]u8) T {
241 return State.hash(msg, key);237 return State.hash(msg, key);
242 }238 }
243 };239 };
lib/std/crypto/test.zig+8-8
...@@ -8,18 +8,18 @@ const testing = std.testing;...@@ -8,18 +8,18 @@ const testing = std.testing;
8const fmt = std.fmt;8const fmt = std.fmt;
99
10// Hash using the specified hasher `H` asserting `expected == H(input)`.10// Hash using the specified hasher `H` asserting `expected == H(input)`.
11pub fn assertEqualHash(comptime Hasher: anytype, comptime expected: []const u8, input: []const u8) void {11pub fn assertEqualHash(comptime Hasher: anytype, comptime expected_hex: *const [Hasher.digest_length * 2:0]u8, input: []const u8) void {
12 var h: [expected.len / 2]u8 = undefined;12 var h: [Hasher.digest_length]u8 = undefined;
13 Hasher.hash(input, h[0..], .{});13 Hasher.hash(input, &h, .{});
1414
15 assertEqual(expected, &h);15 assertEqual(expected_hex, &h);
16}16}
1717
18// Assert `expected` == `input` where `input` is a bytestring.18// Assert `expected` == hex(`input`) where `input` is a bytestring
19pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {19pub fn assertEqual(comptime expected_hex: [:0]const u8, input: []const u8) void {
20 var expected_bytes: [expected.len / 2]u8 = undefined;20 var expected_bytes: [expected_hex.len / 2]u8 = undefined;
21 for (expected_bytes) |*r, i| {21 for (expected_bytes) |*r, i| {
22 r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;22 r.* = fmt.parseInt(u8, expected_hex[2 * i .. 2 * i + 2], 16) catch unreachable;
23 }23 }
2424
25 testing.expectEqualSlices(u8, &expected_bytes, input);25 testing.expectEqualSlices(u8, &expected_bytes, input);
src/Cache.zig+2-2
...@@ -35,7 +35,7 @@ const manifest_file_size_max = 50 * 1024 * 1024;...@@ -35,7 +35,7 @@ const manifest_file_size_max = 50 * 1024 * 1024;
35pub const Hasher = crypto.auth.siphash.SipHash128(1, 3);35pub const Hasher = crypto.auth.siphash.SipHash128(1, 3);
3636
37/// Initial state, that can be copied.37/// Initial state, that can be copied.
38pub const hasher_init: Hasher = Hasher.init(&[_]u8{0} ** Hasher.minimum_key_length);38pub const hasher_init: Hasher = Hasher.init(&[_]u8{0} ** Hasher.key_length);
3939
40pub const File = struct {40pub const File = struct {
41 path: ?[]const u8,41 path: ?[]const u8,
...@@ -600,7 +600,7 @@ pub fn writeSmallFile(dir: fs.Dir, sub_path: []const u8, data: []const u8) !void...@@ -600,7 +600,7 @@ pub fn writeSmallFile(dir: fs.Dir, sub_path: []const u8, data: []const u8) !void
600 }600 }
601}601}
602602
603fn hashFile(file: fs.File, bin_digest: []u8) !void {603fn hashFile(file: fs.File, bin_digest: *[Hasher.mac_length]u8) !void {
604 var buf: [1024]u8 = undefined;604 var buf: [1024]u8 = undefined;
605605
606 var hasher = hasher_init;606 var hasher = hasher_init;