| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bytes; |
| 5 |
use crypto::mac; |
| 6 |
use hash; |
| 7 |
use io; |
| 8 |
|
| 9 |
export type state = struct { |
| 10 |
mac::mac, |
| 11 |
h: *hash::hash, |
| 12 |
keypad: []u8, |
| 13 |
}; |
| 14 |
|
| 15 |
const hmac_vtable: io::vtable = io::vtable { |
| 16 |
writer = &write, |
| 17 |
... |
| 18 |
}; |
| 19 |
|
| 20 |
// Creates a [[crypto::mac::mac]] that computes an HMAC using the provided hash |
| 21 |
// function 'h' with given 'key'. The caller must provide a 'buf' of |
| 22 |
// [[hash::bsz]] bytes. Use the BLOCKSZ constant of the given hash function to |
| 23 |
// allocate the memory statically. |
| 24 |
// |
| 25 |
// The caller must take extra care to call [[crypto::mac::finish]] when they are |
| 26 |
// finished using the MAC function, which, in addition to freeing state |
| 27 |
// associated with the MAC, will securely erase state which contains secret |
| 28 |
// information. |
| 29 |
export fn hmac(h: *hash::hash, key: const []u8, buf: []u8) state = { |
| 30 |
const bsz = hash::bsz(h); |
| 31 |
|
| 32 |
assert(len(buf) >= bsz, "buf must be at least the size of one " |
| 33 |
"block of the given hash function"); |
| 34 |
let keypad = buf[..bsz]; |
| 35 |
|
| 36 |
init(h, key, keypad); |
| 37 |
|
| 38 |
return state { |
| 39 |
h = h, |
| 40 |
stream = &hmac_vtable, |
| 41 |
sz = hash::sz(h), |
| 42 |
bsz = bsz, |
| 43 |
sum = &gensum, |
| 44 |
finish = &finish, |
| 45 |
keypad = keypad, |
| 46 |
... |
| 47 |
}; |
| 48 |
}; |
| 49 |
|
| 50 |
fn init(h: *hash::hash, key: []u8, keypad: []u8) void = { |
| 51 |
const bsz = hash::bsz(h); |
| 52 |
|
| 53 |
keypad[..] = [0...]; |
| 54 |
if (len(key) > bsz) { |
| 55 |
hash::write(h, key); |
| 56 |
hash::sum(h, keypad); |
| 57 |
hash::reset(h); |
| 58 |
} else { |
| 59 |
keypad[..len(key)] = key[..]; |
| 60 |
}; |
| 61 |
|
| 62 |
for (let i = 0z; i < bsz; i += 1) { |
| 63 |
keypad[i] = 0x36 ^ keypad[i]; |
| 64 |
}; |
| 65 |
|
| 66 |
hash::write(h, keypad); |
| 67 |
|
| 68 |
for (let i = 0z; i < bsz; i += 1) { |
| 69 |
// keypad for the outer hash is xored with 0x5c instead of 0x36 |
| 70 |
keypad[i] = keypad[i] ^ 0x36 ^ 0x5c; |
| 71 |
}; |
| 72 |
}; |
| 73 |
|
| 74 |
fn write(st: *io::stream, buf: const []u8) (size | io::error) = { |
| 75 |
let hm = st: *state; |
| 76 |
return hash::write(hm.h, buf); |
| 77 |
}; |
| 78 |
|
| 79 |
fn sum(h: *hash::hash, keypad: []u8, dest: []u8) void = { |
| 80 |
hash::sum(h, dest); |
| 81 |
|
| 82 |
hash::reset(h); |
| 83 |
hash::write(h, keypad); |
| 84 |
hash::write(h, dest); |
| 85 |
hash::sum(h, dest); |
| 86 |
}; |
| 87 |
|
| 88 |
fn gensum(mac: *mac::mac, dest: []u8) void = { |
| 89 |
let hm = mac: *state; |
| 90 |
sum(hm.h, hm.keypad, dest); |
| 91 |
}; |
| 92 |
|
| 93 |
fn finish(mac: *mac::mac) void = { |
| 94 |
let hm = mac: *state; |
| 95 |
bytes::zero(hm.keypad); |
| 96 |
hash::close(hm.h); |
| 97 |
}; |