hare

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

commit 7ba94eb1491d75716be79ea5a11a3d30fdea4ebb
parent 46fe35af85c8fb3cc9512712f03d205b4ce94dd5
Author: Armin Preiml <apreiml@strohwolke.at>
Date:   Sun, 14 Nov 2021 18:16:07 +0100

add in place encryption tests for aes and cbc

Signed-off-by: Armin Preiml <apreiml@strohwolke.at>

Diffstat:
Mcrypto/aes/cbc+test.ha | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Mcrypto/aes/ct64+test.ha | 27+++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/crypto/aes/cbc+test.ha b/crypto/aes/cbc+test.ha @@ -49,3 +49,52 @@ use bytes; cipher::cbc_decrypt(&cbcd, result, cipher); assert(bytes::equal(plain, result)); }; + +@test fn cbc_encrypt_decrypt_in_place() void = { + const key: [_]u8 = [ + 0xde, 0x05, 0x73, 0x76, 0xbf, 0x1b, 0x0c, 0xd1, + 0x26, 0x66, 0xa9, 0x63, 0x30, 0xce, 0x1c, 0xde, + ]; + + const iv: [_]u8 = [ + 0x2c, 0x80, 0x9e, 0x1e, 0xc3, 0x02, 0x28, 0xae, + 0x0a, 0x19, 0xea, 0xef, 0x73, 0x6c, 0x9a, 0xbb, + ]; + + const plain: [_]u8 = [ + 0x50, 0xe6, 0xd6, 0x87, 0xeb, 0xb4, 0x65, 0x9c, + 0xfc, 0x70, 0x7e, 0x00, 0xe6, 0xd6, 0xef, 0x23, + 0xd6, 0x11, 0xca, 0x11, 0x08, 0x2a, 0xde, 0x07, + 0xf2, 0xe4, 0x09, 0x17, 0x5a, 0xac, 0xdf, 0x6c, + 0x9b, 0x08, 0x82, 0x92, 0x7e, 0x2c, 0xbc, 0xb7, + 0x74, 0xe0, 0xe8, 0x7b, 0xe5, 0x72, 0x86, 0x6f, + 0x92, 0x37, 0x85, 0xb0, 0x5e, 0x53, 0xd0, 0xcb, + 0x42, 0x70, 0x3d, 0x49, 0x6a, 0x48, 0x47, 0x36, + ]; + + const cipher: [_]u8 = [ + 0xb4, 0x6b, 0x0b, 0x36, 0xe1, 0xd7, 0x84, 0x1a, + 0xf4, 0x3e, 0x74, 0x29, 0x72, 0x9c, 0x60, 0x60, + 0xe1, 0x8a, 0xfc, 0x87, 0x3b, 0xe3, 0x18, 0x89, + 0x8c, 0x5b, 0x5f, 0x12, 0xe6, 0x71, 0x0d, 0x7e, + 0xf3, 0xfd, 0xa6, 0x82, 0x4e, 0xa8, 0x86, 0x9f, + 0xaf, 0x60, 0xe9, 0x16, 0x05, 0xda, 0xe1, 0xa5, + 0xaa, 0xc0, 0x57, 0x20, 0xf8, 0xbf, 0x17, 0x37, + 0x74, 0x8d, 0xd5, 0x3d, 0xf8, 0x0c, 0x97, 0x05, + ]; + + let result: [64]u8 = plain; + + let b = ct64(key); + let cbc = cipher::cbc_encryptor(&b, iv[..]); + defer cipher::cbc_finish(&cbc); + + cipher::cbc_encrypt(&cbc, result, result); + assert(bytes::equal(cipher, result)); + + let cbcd = cipher::cbc_decryptor(&b, iv[..]); + defer cipher::cbc_finish(&cbcd); + + cipher::cbc_decrypt(&cbcd, result, result); + assert(bytes::equal(plain, result)); +}; diff --git a/crypto/aes/ct64+test.ha b/crypto/aes/ct64+test.ha @@ -161,6 +161,33 @@ use bytes; assert(bytes::equal(plain, result)); }; +@test fn test_example_vector1_in_place() void = { + const key: [_]u8 = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + ]; + + const plain: [_]u8 = [ + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, + ]; + + const cipher: [_]u8 = [ + 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, + 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a, + ]; + + let result: [16]u8 = plain; + + let block = ct64(key[..]); + + cipher::encrypt(&block, result[..], result[..]); + assert(bytes::equal(cipher, result)); + + cipher::decrypt(&block, result[..], result[..]); + assert(bytes::equal(plain, result)); +}; + // fips-197.pdf Appendix C.2 @test fn test_example_vector2() void = { const key: [_]u8 = [