authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-08-31 18:45:45+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-08-31 18:45:45+12:00
log763845f95c33140859aa0297c9495d5f08f9afea
treead2fc6f4b8dcbb7f67d306209e2dfaf144fca8e0
parent38399941d41f8edba9c297a7c0e3da6deb44766c

std/crypto: zig fmt


2 files changed, 39 insertions(+), 109 deletions(-)

std/crypto/chacha20.zig+30-27
...@@ -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];
3333
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 };
4444
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 cycles47 // 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 }
5559
...@@ -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't174 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 };
212215
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 };
252255
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 };
286289
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 };
320323
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 };
354357
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));
std/crypto/sha3.zig+9-82
...@@ -87,97 +87,24 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {...@@ -87,97 +87,24 @@ fn Keccak(comptime bits: usize, comptime delim: u8) type {
87}87}
8888
89const RC = []const u64{89const RC = []const u64{
90 0x0000000000000001,90 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
91 0x0000000000008082,91 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
92 0x800000000000808a,92 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
93 0x8000000080008000,93 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
94 0x000000000000808b,94 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
95 0x0000000080000001,95 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
96 0x8000000080008081,
97 0x8000000000008009,
98 0x000000000000008a,
99 0x0000000000000088,
100 0x0000000080008009,
101 0x000000008000000a,
102 0x000000008000808b,
103 0x800000000000008b,
104 0x8000000000008089,
105 0x8000000000008003,
106 0x8000000000008002,
107 0x8000000000000080,
108 0x000000000000800a,
109 0x800000008000000a,
110 0x8000000080008081,
111 0x8000000000008080,
112 0x0000000080000001,
113 0x8000000080008008,
114};96};
11597
116const ROTC = []const usize{98const ROTC = []const usize{
117 1,99 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
118 3,
119 6,
120 10,
121 15,
122 21,
123 28,
124 36,
125 45,
126 55,
127 2,
128 14,
129 27,
130 41,
131 56,
132 8,
133 25,
134 43,
135 62,
136 18,
137 39,
138 61,
139 20,
140 44,
141};100};
142101
143const PIL = []const usize{102const PIL = []const usize{
144 10,103 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
145 7,
146 11,
147 17,
148 18,
149 3,
150 5,
151 16,
152 8,
153 21,
154 24,
155 4,
156 15,
157 23,
158 19,
159 13,
160 12,
161 2,
162 20,
163 14,
164 22,
165 9,
166 6,
167 1,
168};104};
169105
170const M5 = []const usize{106const M5 = []const usize{
171 0,107 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
172 1,
173 2,
174 3,
175 4,
176 0,
177 1,
178 2,
179 3,
180 4,
181};108};
182109
183fn keccak_f(comptime F: usize, d: []u8) void {110fn keccak_f(comptime F: usize, d: []u8) void {