| ... | @@ -0,0 +1,81 @@ |
| 1 | const std = @import("../index.zig"); |
| 2 | const crypto = std.crypto; |
| 3 | const debug = std.debug; |
| 4 | const mem = std.mem; |
| 5 | |
| 6 | pub const HmacMd5 = Hmac(crypto.Md5); |
| 7 | pub const HmacSha1 = Hmac(crypto.Sha1); |
| 8 | pub const HmacSha256 = Hmac(crypto.Sha256); |
| 9 | |
| 10 | pub fn Hmac(comptime H: type) type { |
| 11 | return struct { |
| 12 | const digest_size = H.digest_size; |
| 13 | |
| 14 | pub fn hash(output: []u8, key: []const u8, message: []const u8) void { |
| 15 | debug.assert(output.len >= H.digest_size); |
| 16 | debug.assert(H.digest_size <= H.block_size); // HMAC makes this assumption |
| 17 | var scratch: [H.block_size]u8 = undefined; |
| 18 | |
| 19 | // Normalize key length to block size of hash |
| 20 | if (key.len > H.block_size) { |
| 21 | H.hash(key, scratch[0..H.digest_size]); |
| 22 | mem.set(u8, scratch[H.digest_size..H.block_size], 0); |
| 23 | } else if (key.len < H.block_size) { |
| 24 | mem.copy(u8, scratch[0..key.len], key); |
| 25 | mem.set(u8, scratch[key.len..H.block_size], 0); |
| 26 | } else { |
| 27 | mem.copy(u8, scratch[0..], key); |
| 28 | } |
| 29 | |
| 30 | var o_key_pad: [H.block_size]u8 = undefined; |
| 31 | for (o_key_pad) |*b, i| { |
| 32 | *b = scratch[i] ^ 0x5c; |
| 33 | } |
| 34 | |
| 35 | var i_key_pad: [H.block_size]u8 = undefined; |
| 36 | for (i_key_pad) |*b, i| { |
| 37 | *b = scratch[i] ^ 0x36; |
| 38 | } |
| 39 | |
| 40 | // HMAC(k, m) = H(o_key_pad | H(i_key_pad | message)) where | is concatenation |
| 41 | var hmac = H.init(); |
| 42 | hmac.update(i_key_pad[0..]); |
| 43 | hmac.update(message); |
| 44 | hmac.final(scratch[0..H.digest_size]); |
| 45 | |
| 46 | hmac.reset(); |
| 47 | hmac.update(o_key_pad[0..]); |
| 48 | hmac.update(scratch[0..H.digest_size]); |
| 49 | hmac.final(output[0..H.digest_size]); |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | const htest = @import("test.zig"); |
| 55 | |
| 56 | test "hmac md5" { |
| 57 | var out: [crypto.Md5.digest_size]u8 = undefined; |
| 58 | HmacMd5.hash(out[0..], "", ""); |
| 59 | htest.assertEqual("74e6f7298a9c2d168935f58c001bad88", out[0..]); |
| 60 | |
| 61 | HmacMd5.hash(out[0..], "key", "The quick brown fox jumps over the lazy dog"); |
| 62 | htest.assertEqual("80070713463e7749b90c2dc24911e275", out[0..]); |
| 63 | } |
| 64 | |
| 65 | test "hmac sha1" { |
| 66 | var out: [crypto.Sha1.digest_size]u8 = undefined; |
| 67 | HmacSha1.hash(out[0..], "", ""); |
| 68 | htest.assertEqual("fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", out[0..]); |
| 69 | |
| 70 | HmacSha1.hash(out[0..], "key", "The quick brown fox jumps over the lazy dog"); |
| 71 | htest.assertEqual("de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9", out[0..]); |
| 72 | } |
| 73 | |
| 74 | test "hmac sha256" { |
| 75 | var out: [crypto.Sha256.digest_size]u8 = undefined; |
| 76 | HmacSha256.hash(out[0..], "", ""); |
| 77 | htest.assertEqual("b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", out[0..]); |
| 78 | |
| 79 | HmacSha256.hash(out[0..], "key", "The quick brown fox jumps over the lazy dog"); |
| 80 | htest.assertEqual("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8", out[0..]); |
| 81 | } |