+test.ha (3374B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use bytes; 5 use crypto::mac; 6 use crypto::sha1; 7 use crypto::sha256; 8 use encoding::hex; 9 use hash; 10 use io; 11 use strings; 12 13 fn assert_hmac_sha1(keystr: str, vectors: [](str, [20]u8)) void = { 14 let key = strings::toutf8(keystr); 15 let h = sha1::sha1(); 16 let buf: [sha1::BLOCKSZ]u8 = [0...]; 17 18 for (let i = 0z; i < len(vectors); i += 1) { 19 hash::reset(&h); 20 let hmac = hmac(&h, key, buf); 21 defer mac::finish(&hmac); 22 23 const vector = vectors[i]; 24 25 mac::write(&hmac, strings::toutf8(vector.0)); 26 27 let sum: [sha1::SZ]u8 = [0...]; 28 mac::sum(&hmac, sum); 29 30 assert(bytes::equal(vector.1, sum)); 31 }; 32 }; 33 34 @test fn hmac_sha1_empty_key() void = { 35 const vectors: [_](str, [20]u8) = [ 36 ( 37 "", 38 [ 39 0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08, 40 0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63, 41 0x70, 0x69, 0x0e, 0x1d, 42 ] 43 ), 44 ( 45 "abc", 46 [ 47 0x9b, 0x4a, 0x91, 0x8f, 0x39, 0x8d, 0x74, 0xd3, 48 0xe3, 0x67, 0x97, 0x0a, 0xba, 0x3c, 0xbe, 0x54, 49 0xe4, 0xd2, 0xb5, 0xd9, 50 ] 51 ), 52 ( 53 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl" 54 "mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq" 55 "rstu", 56 [ 57 0xf4, 0x50, 0x13, 0xac, 0xc6, 0xa6, 0xa5, 0x3a, 58 0x49, 0xbf, 0xc0, 0x7b, 0x1e, 0xd8, 0xf5, 0x39, 59 0x5b, 0xf3, 0x9d, 0x96, 60 ] 61 ), 62 ]; 63 assert_hmac_sha1("", vectors); 64 }; 65 66 @test fn hmac_sha1() void = { 67 const vectors: [_](str, [20]u8) = [ 68 ( 69 "", 70 [ 71 0xc5, 0x89, 0xa2, 0x7a, 0xb0, 0xaf, 0x9f, 0xe7, 72 0xe0, 0x38, 0x68, 0x24, 0x73, 0x5d, 0x22, 0x0d, 73 0x04, 0x1b, 0x70, 0xf0, 74 ] 75 ), 76 ( 77 "abc", 78 [ 79 0x85, 0x2d, 0x02, 0xc6, 0xfd, 0x3b, 0x51, 0xdf, 80 0x64, 0x4a, 0xfb, 0x13, 0x81, 0x76, 0xb3, 0x07, 81 0x61, 0xca, 0x95, 0x27, 82 ] 83 ), 84 ( 85 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl" 86 "mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq" 87 "rstu", 88 [ 89 0x41, 0x29, 0x43, 0xf0, 0x84, 0x0d, 0xc1, 0xa1, 90 0x33, 0x8a, 0x16, 0xec, 0x3d, 0x73, 0x0b, 0x76, 91 0x25, 0xac, 0xf0, 0xaa, 92 ] 93 ), 94 ]; 95 96 assert_hmac_sha1("trustno1", vectors); 97 }; 98 99 @test fn hmac_sha1_large_key() void = { 100 const vectors: [_](str, [20]u8) = [ 101 ( 102 "", 103 [ 104 0x24, 0xe4, 0x38, 0xcb, 0x07, 0x54, 0xef, 0x48, 105 0x97, 0x20, 0x05, 0x65, 0xd5, 0xf2, 0x7f, 0x6e, 106 0x62, 0x77, 0xfc, 0x95 107 ], 108 ), 109 ( 110 "abc", 111 [ 112 0xe3, 0x1a, 0x77, 0xff, 0xf1, 0x4b, 0xbf, 0xba, 113 0x5d, 0x2f, 0x8b, 0x44, 0xa6, 0x33, 0xd4, 0xc9, 114 0xfc, 0x8a, 0xd0, 0xe9, 115 ] 116 ), 117 ( 118 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijkl" 119 "mnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopq" 120 "rstu", 121 [ 122 0x5a, 0x64, 0x69, 0x18, 0xa0, 0xe3, 0xaf, 0xe9, 123 0xab, 0x45, 0x1d, 0xf3, 0xcd, 0xdd, 0x1e, 0x7e, 124 0xb9, 0x19, 0xa7, 0x4b 125 ] 126 ), 127 ]; 128 129 let key = "xxabcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi" 130 "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; 131 assert_hmac_sha1(key, vectors); 132 }; 133 134 @test fn sha256() void = { 135 const expected: [_]u8 = [ 136 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, 0x77, 0x2f, 137 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, 0xff, 0x16, 0x97, 0xc4, 138 0x93, 0x71, 0x56, 0x53, 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 139 0xc5, 0xad, 140 ]; 141 142 let hmac = sha256([]); 143 defer mac::finish(&hmac); 144 145 let sum: [sha256::SZ]u8 = [0...]; 146 mac::sum(&hmac, sum); 147 148 assert(bytes::equal(expected, sum)); 149 };