|
| 1 | +///| |
| 2 | +/// The 64 SHA-256 round constants (§4.2.2 of FIPS 180-4): the first 32 bits of |
| 3 | +/// the fractional parts of the cube roots of the first 64 primes. |
| 4 | +let sha256_k : Array[UInt] = [ |
| 5 | + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, |
| 6 | + 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, |
| 7 | + 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, |
| 8 | + 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 9 | + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, |
| 10 | + 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, |
| 11 | + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, |
| 12 | + 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 13 | + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, |
| 14 | + 0xc67178f2, |
| 15 | +] |
| 16 | + |
| 17 | +///| |
| 18 | +/// A 32-bit right-rotation, the diffusion operator SHA-256 is built from. |
| 19 | +fn rotr32(x : UInt, n : Int) -> UInt { |
| 20 | + (x >> n) | (x << (32 - n)) |
| 21 | +} |
| 22 | + |
| 23 | +///| |
| 24 | +/// SHA-256 (FIPS 180-4): hash an arbitrary byte string to a 32-byte digest. A |
| 25 | +/// self-built primitive — MoonBit's core ships no `crypto` — implementing the |
| 26 | +/// full message schedule and 64-round compression over 512-bit blocks with the |
| 27 | +/// standard length-padding. Verified against the NIST vectors (`""`, `"abc"`). |
| 28 | +/// The building block for `hmac_sha256`, and through it for JWT HS256 signing. |
| 29 | +pub fn sha256(msg : Bytes) -> Bytes { |
| 30 | + let mut h0 : UInt = 0x6a09e667 |
| 31 | + let mut h1 : UInt = 0xbb67ae85 |
| 32 | + let mut h2 : UInt = 0x3c6ef372 |
| 33 | + let mut h3 : UInt = 0xa54ff53a |
| 34 | + let mut h4 : UInt = 0x510e527f |
| 35 | + let mut h5 : UInt = 0x9b05688c |
| 36 | + let mut h6 : UInt = 0x1f83d9ab |
| 37 | + let mut h7 : UInt = 0x5be0cd19 |
| 38 | + let bitlen = (msg.length() * 8).to_uint64() |
| 39 | + let buf = Buffer() |
| 40 | + buf.write_bytes(msg[:]) |
| 41 | + buf.write_byte(b'\x80') |
| 42 | + while buf.length() % 64 != 56 { |
| 43 | + buf.write_byte(b'\x00') |
| 44 | + } |
| 45 | + for i = 7; i >= 0; i = i - 1 { |
| 46 | + buf.write_byte(((bitlen >> (i * 8)) & 0xFF).to_byte()) |
| 47 | + } |
| 48 | + let data = buf.to_bytes() |
| 49 | + let w : Array[UInt] = Array::make(64, 0U) |
| 50 | + let nblocks = data.length() / 64 |
| 51 | + for b = 0; b < nblocks; b = b + 1 { |
| 52 | + let off = b * 64 |
| 53 | + for i = 0; i < 16; i = i + 1 { |
| 54 | + let j = off + i * 4 |
| 55 | + w[i] = (data[j].to_int().reinterpret_as_uint() << 24) | |
| 56 | + (data[j + 1].to_int().reinterpret_as_uint() << 16) | |
| 57 | + (data[j + 2].to_int().reinterpret_as_uint() << 8) | |
| 58 | + data[j + 3].to_int().reinterpret_as_uint() |
| 59 | + } |
| 60 | + for i = 16; i < 64; i = i + 1 { |
| 61 | + let s0 = rotr32(w[i - 15], 7) ^ rotr32(w[i - 15], 18) ^ (w[i - 15] >> 3) |
| 62 | + let s1 = rotr32(w[i - 2], 17) ^ rotr32(w[i - 2], 19) ^ (w[i - 2] >> 10) |
| 63 | + w[i] = w[i - 16] + s0 + w[i - 7] + s1 |
| 64 | + } |
| 65 | + let mut a = h0 |
| 66 | + let mut bb = h1 |
| 67 | + let mut c = h2 |
| 68 | + let mut d = h3 |
| 69 | + let mut e = h4 |
| 70 | + let mut f = h5 |
| 71 | + let mut g = h6 |
| 72 | + let mut hh = h7 |
| 73 | + for i = 0; i < 64; i = i + 1 { |
| 74 | + let s1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25) |
| 75 | + let ch = (e & f) ^ (e.lnot() & g) |
| 76 | + let t1 = hh + s1 + ch + sha256_k[i] + w[i] |
| 77 | + let s0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22) |
| 78 | + let maj = (a & bb) ^ (a & c) ^ (bb & c) |
| 79 | + let t2 = s0 + maj |
| 80 | + hh = g |
| 81 | + g = f |
| 82 | + f = e |
| 83 | + e = d + t1 |
| 84 | + d = c |
| 85 | + c = bb |
| 86 | + bb = a |
| 87 | + a = t1 + t2 |
| 88 | + } |
| 89 | + h0 = h0 + a |
| 90 | + h1 = h1 + bb |
| 91 | + h2 = h2 + c |
| 92 | + h3 = h3 + d |
| 93 | + h4 = h4 + e |
| 94 | + h5 = h5 + f |
| 95 | + h6 = h6 + g |
| 96 | + h7 = h7 + hh |
| 97 | + } |
| 98 | + let out = Buffer() |
| 99 | + for hv in [h0, h1, h2, h3, h4, h5, h6, h7] { |
| 100 | + out.write_byte((hv >> 24).to_byte()) |
| 101 | + out.write_byte((hv >> 16).to_byte()) |
| 102 | + out.write_byte((hv >> 8).to_byte()) |
| 103 | + out.write_byte(hv.to_byte()) |
| 104 | + } |
| 105 | + out.to_bytes() |
| 106 | +} |
| 107 | + |
| 108 | +///| |
| 109 | +/// HMAC-SHA256 (RFC 2104): a keyed message-authentication code over `sha256`. |
| 110 | +/// A key longer than the 64-byte block is hashed first; a shorter key is |
| 111 | +/// zero-padded. The message is authenticated as |
| 112 | +/// `H((K ⊕ opad) ∥ H((K ⊕ ipad) ∥ msg))`. Verified against RFC 4231 test case |
| 113 | +/// 2. This is the signature function behind JWT HS256. |
| 114 | +pub fn hmac_sha256(key : Bytes, msg : Bytes) -> Bytes { |
| 115 | + let block = 64 |
| 116 | + let k0 = Buffer() |
| 117 | + if key.length() > block { |
| 118 | + k0.write_bytes(sha256(key)[:]) |
| 119 | + } else { |
| 120 | + k0.write_bytes(key[:]) |
| 121 | + } |
| 122 | + while k0.length() < block { |
| 123 | + k0.write_byte(b'\x00') |
| 124 | + } |
| 125 | + let kb = k0.to_bytes() |
| 126 | + let ipad = Buffer() |
| 127 | + let opad = Buffer() |
| 128 | + for i = 0; i < block; i = i + 1 { |
| 129 | + ipad.write_byte((kb[i].to_int() ^ 0x36).to_byte()) |
| 130 | + opad.write_byte((kb[i].to_int() ^ 0x5c).to_byte()) |
| 131 | + } |
| 132 | + ipad.write_bytes(msg[:]) |
| 133 | + let inner = sha256(ipad.to_bytes()) |
| 134 | + opad.write_bytes(inner[:]) |
| 135 | + sha256(opad.to_bytes()) |
| 136 | +} |
| 137 | + |
| 138 | +///| |
| 139 | +/// A constant-time byte-string equality: it inspects every byte of both inputs |
| 140 | +/// regardless of where they first differ, so an attacker cannot recover a valid |
| 141 | +/// signature byte-by-byte from response timing. Unequal lengths return `false` |
| 142 | +/// immediately (length is not secret). Used to compare JWT signatures. |
| 143 | +pub fn constant_time_eq(a : Bytes, b : Bytes) -> Bool { |
| 144 | + if a.length() != b.length() { |
| 145 | + return false |
| 146 | + } |
| 147 | + let mut diff = 0 |
| 148 | + for i = 0; i < a.length(); i = i + 1 { |
| 149 | + diff = diff | (a[i].to_int() ^ b[i].to_int()) |
| 150 | + } |
| 151 | + diff == 0 |
| 152 | +} |
0 commit comments