hare

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

+test.ha (4799B)


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