| ... | @@ -32,24 +32,28 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void { | ... | @@ -32,24 +32,28 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void { |
| 32 | x[i] = input[i]; | 32 | x[i] = input[i]; |
| 33 | | 33 | |
| 34 | const rounds = comptime []QuarterRound{ | 34 | const rounds = comptime []QuarterRound{ |
| 35 | Rp( 0, 4, 8,12), | 35 | Rp(0, 4, 8, 12), |
| 36 | Rp( 1, 5, 9,13), | 36 | Rp(1, 5, 9, 13), |
| 37 | Rp( 2, 6,10,14), | 37 | Rp(2, 6, 10, 14), |
| 38 | Rp( 3, 7,11,15), | 38 | Rp(3, 7, 11, 15), |
| 39 | Rp( 0, 5,10,15), | 39 | Rp(0, 5, 10, 15), |
| 40 | Rp( 1, 6,11,12), | 40 | Rp(1, 6, 11, 12), |
| 41 | Rp( 2, 7, 8,13), | 41 | Rp(2, 7, 8, 13), |
| 42 | Rp( 3, 4, 9,14), | 42 | Rp(3, 4, 9, 14), |
| 43 | }; | 43 | }; |
| 44 | | 44 | |
| 45 | comptime var j: usize = 0; | 45 | comptime var j: usize = 0; |
| 46 | inline while (j < 20) : (j += 2) { | 46 | inline while (j < 20) : (j += 2) { |
| 47 | // two-round cycles | 47 | // two-round cycles |
| 48 | inline for (rounds) |r| { | 48 | inline for (rounds) |r| { |
| 49 | x[r.a] +%= x[r.b]; x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(16)); | 49 | x[r.a] +%= x[r.b]; |
| 50 | x[r.c] +%= x[r.d]; x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(12)); | 50 | x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(16)); |
| 51 | x[r.a] +%= x[r.b]; x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(8)); | 51 | x[r.c] +%= x[r.d]; |
| 52 | x[r.c] +%= x[r.d]; x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(7)); | 52 | x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(12)); |
| | 53 | x[r.a] +%= x[r.b]; |
| | 54 | x[r.d] = std.math.rotl(u32, x[r.d] ^ x[r.a], u32(8)); |
| | 55 | x[r.c] +%= x[r.d]; |
| | 56 | x[r.b] = std.math.rotl(u32, x[r.b] ^ x[r.c], u32(7)); |
| 53 | } | 57 | } |
| 54 | } | 58 | } |
| 55 | | 59 | |
| ... | @@ -166,9 +170,8 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32] | ... | @@ -166,9 +170,8 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32] |
| 166 | var remaining_blocks: u32 = @intCast(u32, (in.len / big_block)); | 170 | var remaining_blocks: u32 = @intCast(u32, (in.len / big_block)); |
| 167 | var i: u32 = 0; | 171 | var i: u32 = 0; |
| 168 | while (remaining_blocks > 0) : (remaining_blocks -= 1) { | 172 | while (remaining_blocks > 0) : (remaining_blocks -= 1) { |
| 169 | chaCha20_internal(out[cursor..cursor + big_block], in[cursor..cursor + big_block], k, c); | 173 | chaCha20_internal(out[cursor .. cursor + big_block], in[cursor .. cursor + big_block], k, c); |
| 170 | c[1] += 1; // upper 32-bit of counter, generic chaCha20_internal() doesn't | 174 | c[1] += 1; // upper 32-bit of counter, generic chaCha20_internal() doesn't know about this. |
| 171 | // know about this. | | |
| 172 | cursor += big_block; | 175 | cursor += big_block; |
| 173 | } | 176 | } |
| 174 | } | 177 | } |
| ... | @@ -199,16 +202,16 @@ test "crypto.chacha20 test vector sunscreen" { | ... | @@ -199,16 +202,16 @@ test "crypto.chacha20 test vector sunscreen" { |
| 199 | const input = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."; | 202 | const input = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."; |
| 200 | var result: [114]u8 = undefined; | 203 | var result: [114]u8 = undefined; |
| 201 | const key = []u8{ | 204 | const key = []u8{ |
| 202 | 0, 1, 2, 3, 4, 5, 6, 7, | 205 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 203 | 8, 9,10,11,12,13,14,15, | 206 | 8, 9, 10, 11, 12, 13, 14, 15, |
| 204 | 16,17,18,19,20,21,22,23, | 207 | 16, 17, 18, 19, 20, 21, 22, 23, |
| 205 | 24,25,26,27,28,29,30,31, | 208 | 24, 25, 26, 27, 28, 29, 30, 31, |
| 206 | }; | 209 | }; |
| 207 | const nonce = []u8{ | 210 | const nonce = []u8{ |
| 208 | 0, 0, 0, 0, | 211 | 0, 0, 0, 0, |
| 209 | 0, 0, 0, 0x4a, | 212 | 0, 0, 0, 0x4a, |
| 210 | 0, 0, 0, 0, | 213 | 0, 0, 0, 0, |
| 211 | }; | 214 | }; |
| 212 | | 215 | |
| 213 | chaCha20IETF(result[0..], input[0..], 1, key, nonce); | 216 | chaCha20IETF(result[0..], input[0..], 1, key, nonce); |
| 214 | assert(mem.eql(u8, expected_result, result)); | 217 | assert(mem.eql(u8, expected_result, result)); |
| ... | @@ -248,7 +251,7 @@ test "crypto.chacha20 test vector 1" { | ... | @@ -248,7 +251,7 @@ test "crypto.chacha20 test vector 1" { |
| 248 | 0, 0, 0, 0, 0, 0, 0, 0, | 251 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 249 | 0, 0, 0, 0, 0, 0, 0, 0, | 252 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 250 | }; | 253 | }; |
| 251 | const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 0}; | 254 | const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 252 | | 255 | |
| 253 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); | 256 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); |
| 254 | assert(mem.eql(u8, expected_result, result)); | 257 | assert(mem.eql(u8, expected_result, result)); |
| ... | @@ -282,7 +285,7 @@ test "crypto.chacha20 test vector 2" { | ... | @@ -282,7 +285,7 @@ test "crypto.chacha20 test vector 2" { |
| 282 | 0, 0, 0, 0, 0, 0, 0, 0, | 285 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 283 | 0, 0, 0, 0, 0, 0, 0, 1, | 286 | 0, 0, 0, 0, 0, 0, 0, 1, |
| 284 | }; | 287 | }; |
| 285 | const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 0}; | 288 | const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 286 | | 289 | |
| 287 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); | 290 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); |
| 288 | assert(mem.eql(u8, expected_result, result)); | 291 | assert(mem.eql(u8, expected_result, result)); |
| ... | @@ -316,7 +319,7 @@ test "crypto.chacha20 test vector 3" { | ... | @@ -316,7 +319,7 @@ test "crypto.chacha20 test vector 3" { |
| 316 | 0, 0, 0, 0, 0, 0, 0, 0, | 319 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 317 | 0, 0, 0, 0, 0, 0, 0, 0, | 320 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 318 | }; | 321 | }; |
| 319 | const nonce = []u8{0, 0, 0, 0, 0, 0, 0, 1}; | 322 | const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 320 | | 323 | |
| 321 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); | 324 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); |
| 322 | assert(mem.eql(u8, expected_result, result)); | 325 | assert(mem.eql(u8, expected_result, result)); |
| ... | @@ -350,7 +353,7 @@ test "crypto.chacha20 test vector 4" { | ... | @@ -350,7 +353,7 @@ test "crypto.chacha20 test vector 4" { |
| 350 | 0, 0, 0, 0, 0, 0, 0, 0, | 353 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 351 | 0, 0, 0, 0, 0, 0, 0, 0, | 354 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 352 | }; | 355 | }; |
| 353 | const nonce = []u8{1, 0, 0, 0, 0, 0, 0, 0}; | 356 | const nonce = []u8{ 1, 0, 0, 0, 0, 0, 0, 0 }; |
| 354 | | 357 | |
| 355 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); | 358 | chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); |
| 356 | assert(mem.eql(u8, expected_result, result)); | 359 | assert(mem.eql(u8, expected_result, result)); |