hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+test.ha (4818B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use crypto::mac;
      6 use encoding::hex;
      7 
      8 // example taken from the Poly1305-AES paper from D. J. Bernstein
      9 @test fn example1() void = {
     10 	const message: [_]u8 = [0xf3, 0xf6];
     11 
     12 	const key: [32]u8 = [
     13 		0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b,
     14 		0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00,
     15 		0x58, 0x0b, 0x3b, 0x0f, 0x94, 0x47, 0xbb, 0x1e,
     16 		0x69, 0xd0, 0x95, 0xb5, 0x92, 0x8b, 0x6d, 0xbc,
     17 	];
     18 
     19 	const expected: [_]u8 = [
     20 		0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45,
     21 		0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde,
     22 	];
     23 
     24 	let result: [16]u8 = [0...];
     25 
     26 	let p = poly1305();
     27 	init(&p, &key);
     28 	mac::write(&p, message);
     29 	mac::sum(&p, result);
     30 	mac::finish(&p);
     31 
     32 	assert(bytes::equal(expected, result));
     33 };
     34 
     35 // example taken from the Poly1305-AES paper from D. J. Bernstein
     36 @test fn example2() void = {
     37 	const key: [_]u8 = [
     38 		0xa0, 0xf3, 0x08, 0x00, 0x00, 0xf4, 0x64, 0x00,
     39 		0xd0, 0xc7, 0xe9, 0x07, 0x6c, 0x83, 0x44, 0x03,
     40 		0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7,
     41 		0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7,
     42 	];
     43 
     44 	const expected: [_]u8 = [
     45 		0xdd, 0x3f, 0xab, 0x22, 0x51, 0xf1, 0x1a, 0xc7,
     46 		0x59, 0xf0, 0x88, 0x71, 0x29, 0xcc, 0x2e, 0xe7,
     47 	];
     48 
     49 	let result: [16]u8 = [0...];
     50 
     51 	let p = poly1305();
     52 	init(&p, &key);
     53 	mac::sum(&p, result);
     54 	mac::finish(&p);
     55 
     56 	assert(bytes::equal(expected, result));
     57 };
     58 
     59 // example taken from the Poly1305-AES paper from D. J. Bernstein
     60 @test fn example3() void = {
     61 	const message: [_]u8 = [
     62 		0x66, 0x3c, 0xea, 0x19, 0x0f, 0xfb, 0x83, 0xd8,
     63 		0x95, 0x93, 0xf3, 0xf4, 0x76, 0xb6, 0xbc, 0x24,
     64 		0xd7, 0xe6, 0x79, 0x10, 0x7e, 0xa2, 0x6a, 0xdb,
     65 		0x8c, 0xaf, 0x66, 0x52, 0xd0, 0x65, 0x61, 0x36,
     66 	];
     67 
     68 	const expected: [_]u8 = [
     69 		0x0e, 0xe1, 0xc1, 0x6b, 0xb7, 0x3f, 0x0f, 0x4f,
     70 		0xd1, 0x98, 0x81, 0x75, 0x3c, 0x01, 0xcd, 0xbe,
     71 	];
     72 
     73 	const key: [32]u8 = [
     74 		0x48, 0x44, 0x3d, 0x0b, 0xb0, 0xd2, 0x11, 0x09,
     75 		0xc8, 0x9a, 0x10, 0x0b, 0x5c, 0xe2, 0xc2, 0x08,
     76 		0x83, 0x14, 0x9c, 0x69, 0xb5, 0x61, 0xdd, 0x88,
     77 		0x29, 0x8a, 0x17, 0x98, 0xb1, 0x07, 0x16, 0xef,
     78 	];
     79 
     80 	let result: [16]u8 = [0...];
     81 
     82 	let p = poly1305();
     83 	init(&p, &key);
     84 	mac::write(&p, message);
     85 	mac::sum(&p, result);
     86 	mac::finish(&p);
     87 
     88 	assert(bytes::equal(expected, result));
     89 };
     90 
     91 // example taken from the Poly1305-AES paper from D. J. Bernstein
     92 @test fn example4() void = {
     93 	const message: [_]u8 = [
     94 		0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42,
     95 		0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79,
     96 		0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91,
     97 		0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2,
     98 		0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83,
     99 		0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09,
    100 		0x5c, 0x1b, 0xf9,
    101 	];
    102 
    103 	const expected: [_]u8 = [
    104 		0x51, 0x54, 0xad, 0x0d, 0x2c, 0xb2, 0x6e, 0x01,
    105 		0x27, 0x4f, 0xc5, 0x11, 0x48, 0x49, 0x1f, 0x1b,
    106 	];
    107 
    108 	const key: [32]u8 = [
    109 		0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c,
    110 		0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07,
    111 		0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1,
    112 		0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57,
    113 	];
    114 
    115 	let result: [16]u8 = [0...];
    116 
    117 	let p = poly1305();
    118 	init(&p, &key);
    119 	mac::write(&p, message);
    120 	mac::sum(&p, result);
    121 	mac::finish(&p);
    122 
    123 	assert(bytes::equal(expected, result));
    124 };
    125 
    126 @test fn writepatterns() void = {
    127 	const message: [_]u8 = [
    128 		0xab, 0x08, 0x12, 0x72, 0x4a, 0x7f, 0x1e, 0x34, 0x27, 0x42,
    129 		0xcb, 0xed, 0x37, 0x4d, 0x94, 0xd1, 0x36, 0xc6, 0xb8, 0x79,
    130 		0x5d, 0x45, 0xb3, 0x81, 0x98, 0x30, 0xf2, 0xc0, 0x44, 0x91,
    131 		0xfa, 0xf0, 0x99, 0x0c, 0x62, 0xe4, 0x8b, 0x80, 0x18, 0xb2,
    132 		0xc3, 0xe4, 0xa0, 0xfa, 0x31, 0x34, 0xcb, 0x67, 0xfa, 0x83,
    133 		0xe1, 0x58, 0xc9, 0x94, 0xd9, 0x61, 0xc4, 0xcb, 0x21, 0x09,
    134 		0x5c, 0x1b, 0xf9,
    135 	];
    136 
    137 	const expected: [_]u8 = [
    138 		0x51, 0x54, 0xad, 0x0d, 0x2c, 0xb2, 0x6e, 0x01,
    139 		0x27, 0x4f, 0xc5, 0x11, 0x48, 0x49, 0x1f, 0x1b,
    140 	];
    141 
    142 	const key: [32]u8 = [
    143 		0x12, 0x97, 0x6a, 0x08, 0xc4, 0x42, 0x6d, 0x0c,
    144 		0xe8, 0xa8, 0x24, 0x07, 0xc4, 0xf4, 0x82, 0x07,
    145 		0x80, 0xf8, 0xc2, 0x0a, 0xa7, 0x12, 0x02, 0xd1,
    146 		0xe2, 0x91, 0x79, 0xcb, 0xcb, 0x55, 0x5a, 0x57,
    147 	];
    148 
    149 
    150 	patternwrite(&key, message[..], expected[..], [5, 20, 38]);
    151 	patternwrite(&key, message[..], expected[..], [1, 2, 8, 10]);
    152 	patternwrite(&key, message[..], expected[..], [16, 16]);
    153 	patternwrite(&key, message[..], expected[..], [12, 4, 14, 2, 8, 8]);
    154 };
    155 
    156 fn patternwrite(key: *key, msg: []u8, expected: []u8, pattern: []uint) void = {
    157 	let p = poly1305();
    158 	init(&p, key);
    159 
    160 	for (let i = 0z; i < len(pattern); i += 1) {
    161 		let n = pattern[i];
    162 		mac::write(&p, msg[..n]);
    163 		msg = msg[n..];
    164 	};
    165 
    166 	if (len(msg) > 0) {
    167 		mac::write(&p, msg);
    168 	};
    169 
    170 	let result: [16]u8 = [0...];
    171 	mac::sum(&p, result);
    172 	mac::finish(&p);
    173 
    174 	assert(bytes::equal(expected, result));
    175 };