hare

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

cbc+test.ha (3252B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use crypto::cipher;
      6 
      7 @test fn cbc_encrypt_decrypt() void = {
      8 	const key: [_]u8 = [
      9 		0xde, 0x05, 0x73, 0x76, 0xbf, 0x1b, 0x0c, 0xd1,
     10 		0x26, 0x66, 0xa9, 0x63, 0x30, 0xce, 0x1c, 0xde,
     11 	];
     12 
     13 	const iv: [_]u8 = [
     14 		0x2c, 0x80, 0x9e, 0x1e, 0xc3, 0x02, 0x28, 0xae,
     15 		0x0a, 0x19, 0xea, 0xef, 0x73, 0x6c, 0x9a, 0xbb,
     16 	];
     17 
     18 	const plain: [_]u8 = [
     19 		0x50, 0xe6, 0xd6, 0x87, 0xeb, 0xb4, 0x65, 0x9c,
     20 		0xfc, 0x70, 0x7e, 0x00, 0xe6, 0xd6, 0xef, 0x23,
     21 		0xd6, 0x11, 0xca, 0x11, 0x08, 0x2a, 0xde, 0x07,
     22 		0xf2, 0xe4, 0x09, 0x17, 0x5a, 0xac, 0xdf, 0x6c,
     23 		0x9b, 0x08, 0x82, 0x92, 0x7e, 0x2c, 0xbc, 0xb7,
     24 		0x74, 0xe0, 0xe8, 0x7b, 0xe5, 0x72, 0x86, 0x6f,
     25 		0x92, 0x37, 0x85, 0xb0, 0x5e, 0x53, 0xd0, 0xcb,
     26 		0x42, 0x70, 0x3d, 0x49, 0x6a, 0x48, 0x47, 0x36,
     27 	];
     28 
     29 	const cipher: [_]u8 = [
     30 		0xb4, 0x6b, 0x0b, 0x36, 0xe1, 0xd7, 0x84, 0x1a,
     31 		0xf4, 0x3e, 0x74, 0x29, 0x72, 0x9c, 0x60, 0x60,
     32 		0xe1, 0x8a, 0xfc, 0x87, 0x3b, 0xe3, 0x18, 0x89,
     33 		0x8c, 0x5b, 0x5f, 0x12, 0xe6, 0x71, 0x0d, 0x7e,
     34 		0xf3, 0xfd, 0xa6, 0x82, 0x4e, 0xa8, 0x86, 0x9f,
     35 		0xaf, 0x60, 0xe9, 0x16, 0x05, 0xda, 0xe1, 0xa5,
     36 		0xaa, 0xc0, 0x57, 0x20, 0xf8, 0xbf, 0x17, 0x37,
     37 		0x74, 0x8d, 0xd5, 0x3d, 0xf8, 0x0c, 0x97, 0x05,
     38 	];
     39 
     40 	let result: [64]u8 = [0...];
     41 	let buf: [CBC_BUFSZ]u8 = [0...];
     42 
     43 	let b = ct64();
     44 	ct64_init(&b, key);
     45 	defer cipher::finish(&b);
     46 
     47 	let cbc = cipher::cbc_encryptor(&b, iv[..], buf[..]);
     48 
     49 	cipher::cbc_encrypt(&cbc, result, plain);
     50 	assert(bytes::equal(cipher, result));
     51 
     52 	let cbcd = cipher::cbc_decryptor(&b, iv[..], buf[..]);
     53 
     54 	cipher::cbc_decrypt(&cbcd, result, cipher);
     55 	assert(bytes::equal(plain, result));
     56 };
     57 
     58 @test fn cbc_encrypt_decrypt_in_place() void = {
     59 	const key: [_]u8 = [
     60 		0xde, 0x05, 0x73, 0x76, 0xbf, 0x1b, 0x0c, 0xd1,
     61 		0x26, 0x66, 0xa9, 0x63, 0x30, 0xce, 0x1c, 0xde,
     62 	];
     63 
     64 	const iv: [_]u8 = [
     65 		0x2c, 0x80, 0x9e, 0x1e, 0xc3, 0x02, 0x28, 0xae,
     66 		0x0a, 0x19, 0xea, 0xef, 0x73, 0x6c, 0x9a, 0xbb,
     67 	];
     68 
     69 	const plain: [_]u8 = [
     70 		0x50, 0xe6, 0xd6, 0x87, 0xeb, 0xb4, 0x65, 0x9c,
     71 		0xfc, 0x70, 0x7e, 0x00, 0xe6, 0xd6, 0xef, 0x23,
     72 		0xd6, 0x11, 0xca, 0x11, 0x08, 0x2a, 0xde, 0x07,
     73 		0xf2, 0xe4, 0x09, 0x17, 0x5a, 0xac, 0xdf, 0x6c,
     74 		0x9b, 0x08, 0x82, 0x92, 0x7e, 0x2c, 0xbc, 0xb7,
     75 		0x74, 0xe0, 0xe8, 0x7b, 0xe5, 0x72, 0x86, 0x6f,
     76 		0x92, 0x37, 0x85, 0xb0, 0x5e, 0x53, 0xd0, 0xcb,
     77 		0x42, 0x70, 0x3d, 0x49, 0x6a, 0x48, 0x47, 0x36,
     78 	];
     79 
     80 	const cipher: [_]u8 = [
     81 		0xb4, 0x6b, 0x0b, 0x36, 0xe1, 0xd7, 0x84, 0x1a,
     82 		0xf4, 0x3e, 0x74, 0x29, 0x72, 0x9c, 0x60, 0x60,
     83 		0xe1, 0x8a, 0xfc, 0x87, 0x3b, 0xe3, 0x18, 0x89,
     84 		0x8c, 0x5b, 0x5f, 0x12, 0xe6, 0x71, 0x0d, 0x7e,
     85 		0xf3, 0xfd, 0xa6, 0x82, 0x4e, 0xa8, 0x86, 0x9f,
     86 		0xaf, 0x60, 0xe9, 0x16, 0x05, 0xda, 0xe1, 0xa5,
     87 		0xaa, 0xc0, 0x57, 0x20, 0xf8, 0xbf, 0x17, 0x37,
     88 		0x74, 0x8d, 0xd5, 0x3d, 0xf8, 0x0c, 0x97, 0x05,
     89 	];
     90 
     91 	let result: [64]u8 = plain;
     92 	let buf: [CBC_BUFSZ]u8 = [0...];
     93 
     94 	let b = ct64();
     95 	ct64_init(&b, key);
     96 	defer cipher::finish(&b);
     97 
     98 	let cbc = cipher::cbc_encryptor(&b, iv[..], buf[..]);
     99 
    100 	cipher::cbc_encrypt(&cbc, result, result);
    101 	assert(bytes::equal(cipher, result));
    102 
    103 	let cbcd = cipher::cbc_decryptor(&b, iv[..], buf[..]);
    104 
    105 	cipher::cbc_decrypt(&cbcd, result, result);
    106 	assert(bytes::equal(plain, result));
    107 };