From f194c758f2ec4a5058d48b8dd9e8f0466206421c Mon Sep 17 00:00:00 2001 From: jsing <> Date: Sat, 5 Jul 2025 14:32:47 +0000 Subject: Add minimal regress coverage for AES-XTS --- src/regress/lib/libcrypto/aes/aes_test.c | 185 ++++++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/regress/lib/libcrypto/aes/aes_test.c b/src/regress/lib/libcrypto/aes/aes_test.c index 564fadb01c..8d5947a031 100644 --- a/src/regress/lib/libcrypto/aes/aes_test.c +++ b/src/regress/lib/libcrypto/aes/aes_test.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aes_test.c,v 1.4 2025/06/03 08:19:29 kenjiro Exp $ */ +/* $OpenBSD: aes_test.c,v 1.5 2025/07/05 14:32:47 jsing Exp $ */ /* * Copyright (c) 2022 Joshua Sing * @@ -524,6 +524,161 @@ static const struct aes_test aes_tests[] = { }, .out_len = 64, }, + + /* XTS128 - Test vectors from NIST SP 800-38A */ + { + /* XTSGenAES128 1 */ + .mode = NID_aes_128_xts, + .key = { + 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35, + 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62, + 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18, + 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f, + }, + .iv = { + 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6, + 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5, + }, + .iv_len = 16, + .in = { + 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d, + 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c, + }, + .in_len = 16, + .out = { + 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a, + 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63, + }, + .out_len = 16, + }, + { + /* XTSGenAES128 385 */ + .mode = NID_aes_128_xts, + .key = { + 0xb8, 0xdb, 0x0b, 0x9e, 0x63, 0xf5, 0xf0, 0xe6, + 0x60, 0x97, 0x98, 0xa6, 0xcb, 0x42, 0xbb, 0x5b, + 0x5d, 0x71, 0x39, 0xbb, 0x95, 0x57, 0x99, 0xf5, + 0x2a, 0x7c, 0x58, 0x1f, 0x84, 0x63, 0x31, 0x76, + }, + .iv = { + 0x8d, 0x46, 0xf9, 0x67, 0x01, 0x16, 0x7a, 0x1d, + 0x77, 0xcd, 0x1e, 0x44, 0xda, 0x92, 0xf3, 0xa8, + }, + .iv_len = 16, + .in = { + 0xb4, 0x64, 0x4d, 0xc1, 0xb3, 0x8d, 0xd5, 0x98, + 0xca, 0x84, 0x0a, 0x82, 0xd4, 0xd9, 0xc0, 0x65, + 0x67, 0x23, 0xb1, 0x58, 0x01, 0xaa, 0x18, 0xe6, + 0x6e, + }, + .in_len = 25, + .out = { + 0x09, 0x28, 0x8c, 0xf5, 0x1f, 0x1e, 0xb4, 0xad, + 0xb8, 0x54, 0x23, 0xd0, 0xe0, 0xd6, 0xe9, 0x58, + 0x18, 0x87, 0x06, 0xaf, 0x26, 0x0e, 0x24, 0x67, + 0x4e, + }, + .out_len = 25, + }, + { + /* XTSGenAES128 404 */ + .mode = NID_aes_128_xts, + .key = { + 0xbe, 0x5c, 0xf1, 0xf9, 0x9d, 0x51, 0x59, 0xf2, + 0x11, 0xdb, 0xc4, 0xc1, 0x47, 0xf7, 0x9c, 0x55, + 0x6b, 0x2d, 0xa5, 0xc6, 0x91, 0xde, 0xed, 0x74, + 0x0d, 0x01, 0x57, 0xea, 0xb8, 0xc9, 0xc8, 0x9a, + }, + .iv = { + 0x89, 0x24, 0x86, 0x24, 0xb6, 0x96, 0xcf, 0x9c, + 0xb1, 0xb5, 0x77, 0x9c, 0xdc, 0xbc, 0xfe, 0x1c, + }, + .iv_len = 16, + .in = { + 0x3b, 0x80, 0xf8, 0x22, 0xc4, 0xee, 0xe1, 0x31, + 0x3f, 0x79, 0xca, 0x3d, 0xb1, 0x34, 0xd9, 0xca, + 0x8b, 0x09, 0xa3, 0x53, 0x4d, 0x4e, 0x18, 0xe6, + 0x43, 0x9e, 0x1c, 0xdb, 0x86, 0x18, 0x2a, 0x4f, + }, + .in_len = 32, + .out = { + 0x4b, 0x6a, 0xf4, 0x3a, 0x88, 0xb6, 0x33, 0xeb, + 0xd1, 0xe1, 0x27, 0xc1, 0xec, 0x90, 0xcc, 0x47, + 0xa2, 0xf1, 0x6e, 0x3b, 0xc7, 0x9f, 0x88, 0x45, + 0xe3, 0xbd, 0x00, 0x25, 0xda, 0x87, 0x26, 0x45, + }, + .out_len = 32, + }, + { + /* XTSGenAES256 1 */ + .mode = NID_aes_256_xts, + .key = { + 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e, + 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14, + 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7, + 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c, + 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d, + 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3, + 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58, + 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08, + }, + .iv = { + 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2, + 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64, + }, + .iv_len = 16, + .in = { + 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1, + 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64, + 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3, + 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e, + }, + .in_len = 32, + .out = { + 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5, + 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13, + 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a, + 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb, + }, + .out_len = 32, + }, + { + /* XTSGenAES256 172 */ + .mode = NID_aes_256_xts, + .key= { + 0x5c, 0x7f, 0x7a, 0x36, 0x08, 0x01, 0x78, 0x43, + 0x00, 0x83, 0xff, 0x54, 0x92, 0xef, 0x77, 0x26, + 0x0f, 0x68, 0x0a, 0x15, 0xa7, 0x66, 0x24, 0xb8, + 0x9e, 0x85, 0x4c, 0x94, 0xf0, 0x48, 0x8a, 0x9e, + 0x7d, 0xaa, 0x4f, 0x33, 0x01, 0x1f, 0x91, 0xdf, + 0x5e, 0x33, 0x80, 0x53, 0xf4, 0x6c, 0xee, 0x65, + 0x0f, 0xb0, 0xee, 0x69, 0xf8, 0xc2, 0x15, 0x75, + 0x5a, 0x4a, 0x63, 0xcd, 0x42, 0x28, 0xc2, 0x19, + }, + .iv = { + 0xa4, 0x01, 0xd7, 0x3c, 0x88, 0x75, 0xe7, 0x59, + 0xaa, 0x3e, 0xef, 0x53, 0xe0, 0xfb, 0x62, 0x63, + }, + .iv_len = 16, + .in = { + 0xb1, 0xe6, 0x29, 0xa6, 0x2a, 0x03, 0xca, 0x96, + 0x9b, 0x16, 0x91, 0x52, 0x02, 0xbc, 0xaa, 0x09, + 0xe7, 0x8a, 0xe1, 0x85, 0x1b, 0xc8, 0x85, 0x81, + 0x16, 0x49, 0x68, 0xa5, 0x65, 0x6c, 0x82, 0xc0, + 0xe5, 0xc4, 0x03, 0xba, 0x54, 0xb9, 0xb5, 0xed, + 0x9b, 0xab, 0xe8, 0xb0, 0x75, 0x1d, 0x1b, 0x34, + }, + .in_len = 48, + .out = { + 0xf5, 0xbc, 0xa6, 0x0f, 0xb9, 0x35, 0x2b, 0x1d, + 0xe0, 0x4d, 0x71, 0x29, 0x40, 0x56, 0x26, 0xb3, + 0xa4, 0x74, 0xa2, 0x64, 0xfb, 0xac, 0x2d, 0x6b, + 0xe1, 0x19, 0xe1, 0xd5, 0x7a, 0xa9, 0x98, 0xd0, + 0xe0, 0xe4, 0xd9, 0xf9, 0xc9, 0x76, 0x21, 0x0d, + 0x93, 0xc4, 0x65, 0xa3, 0xe3, 0x60, 0xcd, 0x92, + }, + .out_len = 48, + }, }; #define N_AES_TESTS (sizeof(aes_tests) / sizeof(aes_tests[0])) @@ -751,6 +906,10 @@ aes_evp_test(size_t test_number, const struct aes_test *at, const char *label, if (in_len > at->in_len - i) in_len = at->in_len - i; + /* XTS needs to be single shot. */ + if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_XTS_MODE) + in_len = at->in_len; + if (!EVP_EncryptUpdate(ctx, out + total_len, &out_len, at->in + i, in_len)) { fprintf(stderr, @@ -817,6 +976,10 @@ aes_evp_test(size_t test_number, const struct aes_test *at, const char *label, if (in_len > at->out_len - i) in_len = at->out_len - i; + /* XTS needs to be single shot. */ + if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_XTS_MODE) + in_len = at->in_len; + if (!EVP_DecryptUpdate(ctx, out + total_len, &out_len, at->out + i, in_len)) { fprintf(stderr, @@ -983,6 +1146,16 @@ aes_cipher_from_nid(int nid, const char **out_label, *out_cipher = EVP_aes_256_ccm(); break; + /* XTS */ + case NID_aes_128_xts: + *out_label = SN_aes_128_xts; + *out_cipher = EVP_aes_128_xts(); + break; + case NID_aes_256_xts: + *out_label = SN_aes_256_xts; + *out_cipher = EVP_aes_256_xts(); + break; + /* Unknown */ default: return 0; @@ -1004,8 +1177,10 @@ aes_test(void) for (i = 0; i < N_AES_TESTS; i++) { at = &aes_tests[i]; key_bits = aes_key_bits_from_nid(at->mode); - if (!aes_cipher_from_nid(at->mode, &label, &cipher)) + if (!aes_cipher_from_nid(at->mode, &label, &cipher)) { + fprintf(stderr, "unknown cipher\n"); goto failed; + } switch (at->mode) { /* ECB */ @@ -1054,6 +1229,12 @@ aes_test(void) /* CCM is EVP-only */ break; + /* XTS */ + case NID_aes_128_xts: + case NID_aes_256_xts: + /* XTS is EVP-only */ + break; + /* Unknown */ default: fprintf(stderr, "FAIL: unknown mode (%d)\n", -- cgit v1.2.3-55-g6feb