raz / site / Sha256.raz

98 lines · 4.3 KiB · raw · history · last change fe3051dc

1(* Sha256: the SHA-256 of some bytes, as 64 lower-case hex digits, to
2 check the site's archives against the sums published beside them.
3 It needs only Buf and Str. It leaves out a streaming interface: the
4 whole message is given at once. *)
5module Sha256;
6import Buf, Str, Heap, Check;
7
8const
9 BlockLen = 64; (* bytes in a block, the unit the hash works on *)
10 Digits = "0123456789abcdef";
11 (* the first 32 bits of the cube roots of the first 64 primes *)
12 K: [64]U32 = [
13 0428A2F98H, 071374491H, 0B5C0FBCFH, 0E9B5DBA5H, 03956C25BH, 059F111F1H, 0923F82A4H, 0AB1C5ED5H,
14 0D807AA98H, 012835B01H, 0243185BEH, 0550C7DC3H, 072BE5D74H, 080DEB1FEH, 09BDC06A7H, 0C19BF174H,
15 0E49B69C1H, 0EFBE4786H, 00FC19DC6H, 0240CA1CCH, 02DE92C6FH, 04A7484AAH, 05CB0A9DCH, 076F988DAH,
16 0983E5152H, 0A831C66DH, 0B00327C8H, 0BF597FC7H, 0C6E00BF3H, 0D5A79147H, 006CA6351H, 014292967H,
17 027B70A85H, 02E1B2138H, 04D2C6DFCH, 053380D13H, 0650A7354H, 0766A0ABBH, 081C2C92EH, 092722C85H,
18 0A2BFE8A1H, 0A81A664BH, 0C24B8B70H, 0C76C51A3H, 0D192E819H, 0D6990624H, 0F40E3585H, 0106AA070H,
19 019A4C116H, 01E376C08H, 02748774CH, 034B0BCB5H, 0391C0CB3H, 04ED8AA4AH, 05B9CCA4FH, 0682E6FF3H,
20 0748F82EEH, 078A5636FH, 084C87814H, 08CC70208H, 090BEFFFAH, 0A4506CEBH, 0BEF9A3F7H, 0C67178F2H];
21
22type
23 (* The eight words of the running hash. *)
24 State = [8]U32;
25
26(* Mixes the 64 bytes p into the state h. *)
27proc block(var h: State; p: []const U8);
28var
29 w: [64]U32; a, b, c, d, e, f, g, hh, t1, t2, s0, s1: U32; i: INT;
30begin
31 for i := 0 to 15 do w[i] := U32(Str.be(p[4 * i..4 * i + 4])); end;
32 for i := 16 to 63 do
33 s0 := ROR(w[i - 15], 7) xor ROR(w[i - 15], 18) xor (w[i - 15] shr 3);
34 s1 := ROR(w[i - 2], 17) xor ROR(w[i - 2], 19) xor (w[i - 2] shr 10);
35 w[i] := w[i - 16] + s0 + w[i - 7] + s1;
36 end;
37 a := h[0]; b := h[1]; c := h[2]; d := h[3];
38 e := h[4]; f := h[5]; g := h[6]; hh := h[7];
39 for i := 0 to 63 do
40 s1 := ROR(e, 6) xor ROR(e, 11) xor ROR(e, 25);
41 t1 := hh + s1 + ((e & f) xor (~e & g)) + K[i] + w[i];
42 s0 := ROR(a, 2) xor ROR(a, 13) xor ROR(a, 22);
43 t2 := s0 + ((a & b) xor (a & c) xor (b & c));
44 hh := g; g := f; f := e; e := d + t1;
45 d := c; c := b; b := a; a := t1 + t2;
46 end;
47 h[0] := h[0] + a; h[1] := h[1] + b; h[2] := h[2] + c; h[3] := h[3] + d;
48 h[4] := h[4] + e; h[5] := h[5] + f; h[6] := h[6] + g; h[7] := h[7] + hh;
49end;
50
51(* Adds the SHA-256 of data to b, as 64 hex digits. *)
52proc hex*(data: []const U8; var b: Buf.Buf);
53var
54 h: State; tail: [2 * BlockLen]U8; whole, rest, n, i, j: INT; word: U32;
55begin
56 h := [06A09E667H, 0BB67AE85H, 03C6EF372H, 0A54FF53AH, 0510E527FH, 09B05688CH, 01F83D9ABH, 05BE0CD19H];
57 whole := LEN(data) div BlockLen * BlockLen;
58 for i := 0 to whole - 1 by BlockLen do block(h, data[i..i + BlockLen]); end;
59 (* the rest, a 1 bit, zeros, and the length in bits, in one or two blocks *)
60 rest := LEN(data) - whole;
61 FILL(tail, 0);
62 COPY(tail[0..rest], data[whole..LEN(data)]);
63 tail[rest] := 80H;
64 n := BlockLen;
65 if rest >= BlockLen - 8 then n := 2 * BlockLen; end;
66 Str.putBE(tail[n - 8..n], U64(LEN(data)) * 8);
67 for i := 0 to n - 1 by BlockLen do block(h, tail[i..i + BlockLen]); end;
68 for i := 0 to 7 do
69 word := h[i];
70 for j := 7 to 0 by -1 do
71 Buf.add(b, Digits[INT(word shr U32(4 * j)) & 0FH]);
72 end;
73 end;
74end;
75
76(* The module's test: the standard examples, one block, two blocks,
77 and a message whose padding needs a second block. *)
78proc main*;
79var b: Buf.Buf; m: [1000]U8;
80begin
81 Buf.init(b, Heap.heap);
82 hex("", b);
83 Check.str("empty", Buf.bytes(b), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
84 Buf.reset(b);
85 hex("abc", b);
86 Check.str("abc", Buf.bytes(b), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
87 Buf.reset(b);
88 hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", b);
89 Check.str("two blocks", Buf.bytes(b), "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
90 Buf.reset(b);
91 FILL(m, 'a');
92 hex(m[0..55], b);
93 Check.str("55 bytes", Buf.bytes(b), "9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318");
94 Buf.reset(b);
95 hex(m[0..1000], b);
96 Check.str("1000 bytes", Buf.bytes(b), "41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3");
97end;
98end Sha256.