From a11c940b6367eef51027fcb82d70216323d5844a Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Sun, 22 Apr 2018 22:57:58 +0100 Subject: [PATCH] Add test cases for AES OFB block mode Adds test cases from NIST SP800-38A for OFB block mode to AES-128/192/256, for the configuration of MBEDTLS_CIPHER_MODE_OFB. --- tests/Makefile | 11 +++++- tests/suites/test_suite_aes.function | 52 ++++++++++++++++++++++++++++ tests/suites/test_suite_aes.ofb.data | 35 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/suites/test_suite_aes.ofb.data diff --git a/tests/Makefile b/tests/Makefile index d85617fdc..e15d94576 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -46,7 +46,8 @@ LOCAL_LDFLAGS += -lz endif APPS = test_suite_aes.ecb$(EXEXT) test_suite_aes.cbc$(EXEXT) \ - test_suite_aes.cfb$(EXEXT) test_suite_aes.rest$(EXEXT) \ + test_suite_aes.cfb$(EXEXT) test_suite_aes.ofb$(EXEXT) \ + test_suite_aes.rest$(EXEXT) \ test_suite_arc4$(EXEXT) test_suite_asn1write$(EXEXT) \ test_suite_base64$(EXEXT) test_suite_blowfish$(EXEXT) \ test_suite_camellia$(EXEXT) test_suite_ccm$(EXEXT) \ @@ -109,6 +110,10 @@ test_suite_aes.cfb.c : suites/test_suite_aes.function suites/test_suite_aes.cfb. echo " Gen $@" perl scripts/generate_code.pl suites test_suite_aes test_suite_aes.cfb +test_suite_aes.ofb.c : suites/test_suite_aes.function suites/test_suite_aes.ofb.data scripts/generate_code.pl suites/helpers.function suites/main_test.function + echo " Gen $@" + perl scripts/generate_code.pl suites test_suite_aes test_suite_aes.ofb + test_suite_aes.rest.c : suites/test_suite_aes.function suites/test_suite_aes.rest.data scripts/generate_code.pl suites/helpers.function suites/main_test.function echo " Gen $@" perl scripts/generate_code.pl suites test_suite_aes test_suite_aes.rest @@ -209,6 +214,10 @@ test_suite_aes.cfb$(EXEXT): test_suite_aes.cfb.c $(DEP) echo " CC $<" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ +test_suite_aes.ofb$(EXEXT): test_suite_aes.ofb.c $(DEP) + echo " CC $<" + $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ + test_suite_aes.rest$(EXEXT): test_suite_aes.rest.c $(DEP) echo " CC $<" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) $< $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ diff --git a/tests/suites/test_suite_aes.function b/tests/suites/test_suite_aes.function index c5f0eaac9..e1792dd5a 100644 --- a/tests/suites/test_suite_aes.function +++ b/tests/suites/test_suite_aes.function @@ -289,6 +289,58 @@ exit: } /* END_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ +void aes_encrypt_ofb( int fragment_size, char *hex_key_string, + char *hex_iv_string, char *hex_src_string, + char *hex_dst_string ) +{ + unsigned char key_str[100]; + unsigned char iv_str[100]; + unsigned char src_str[200]; + unsigned char dst_str[200]; + unsigned char output[200]; + mbedtls_aes_context ctx; + size_t iv_offset = 0; + int in_buffer_len; + unsigned char* src_str_next; + int key_len, iv_len; + + memset(key_str, 0x00, 100); + memset(iv_str, 0x00, 100); + memset(src_str, 0x00, 200); + memset(dst_str, 0x00, 200); + memset(output, 0x00, 200); + mbedtls_aes_init( &ctx ); + + key_len = unhexify( key_str, hex_key_string ); + iv_len = unhexify( iv_str, hex_iv_string ); + in_buffer_len = unhexify( src_str, hex_src_string ); + + mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); + src_str_next = src_str; + + while( in_buffer_len > 0 ) + { + TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, + iv_str, src_str_next, output ) == 0 ); + + hexify( dst_str, output, fragment_size ); + TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string, + ( 2 * fragment_size) ) == 0 ); + + in_buffer_len -= fragment_size; + hex_dst_string += ( fragment_size * 2 ); + src_str_next += fragment_size; + + if( in_buffer_len < fragment_size ) + fragment_size = in_buffer_len; + } + +exit: + mbedtls_aes_free( &ctx ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ void aes_selftest() { diff --git a/tests/suites/test_suite_aes.ofb.data b/tests/suites/test_suite_aes.ofb.data new file mode 100644 index 000000000..4b9d80e8d --- /dev/null +++ b/tests/suites/test_suite_aes.ofb.data @@ -0,0 +1,35 @@ +# NIST Special Publication 800-38A +# Recommendation for Block Cipher Modes of Operation +# Test Vectors - Appendix F, Section F.4 +OFB-AES128.Encrypt - Single block +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"2b7e151628aed2a6abf7158809cf4f3c":"000102030405060708090a0b0c0d0e0f":"6bc1bee22e409f96e93d7e117393172a":"3b3fd92eb72dad20333449f8e83cfb4a" + +OFB-AES128.Encrypt - Partial blocks - 7 bytes +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:5:"2b7e151628aed2a6abf7158809cf4f3c":"000102030405060708090a0b0c0d0e0f":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":"3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e" + +OFB-AES128.Encrypt - Test NIST SP800-38A - F.4.1 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"2b7e151628aed2a6abf7158809cf4f3c":"000102030405060708090a0b0c0d0e0f":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":"3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e" + +OFB-AES128.Decrypt - Test NIST SP800-38A - F.4.2 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"2b7e151628aed2a6abf7158809cf4f3c":"000102030405060708090a0b0c0d0e0f":"3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710" + +OFB-AES192.Encrypt - Test NIST SP800-38A - F.4.3 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b":"000102030405060708090a0b0c0d0e0f":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":"cdc80d6fddf18cab34c25909c99a4174fcc28b8d4c63837c09e81700c11004018d9a9aeac0f6596f559c6d4daf59a5f26d9f200857ca6c3e9cac524bd9acc92a" + +OFB-AES192.Decrypt - Test NIST SP800-38A - F.4.4 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b":"000102030405060708090a0b0c0d0e0f":"cdc80d6fddf18cab34c25909c99a4174fcc28b8d4c63837c09e81700c11004018d9a9aeac0f6596f559c6d4daf59a5f26d9f200857ca6c3e9cac524bd9acc92a":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710" + +OFB-AES256.Encrypt - Test NIST SP800-38A - F.4.5 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4":"000102030405060708090a0b0c0d0e0f":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710":"dc7e84bfda79164b7ecd8486985d38604febdc6740d20b3ac88f6ad82a4fb08d71ab47a086e86eedf39d1c5bba97c4080126141d67f37be8538f5a8be740e484" + +OFB-AES256.Decrypt - Test NIST SP800-38A - F.4.6 +depends_on:MBEDTLS_CIPHER_MODE_OFB +aes_encrypt_ofb:16:"603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4":"000102030405060708090a0b0c0d0e0f":"dc7e84bfda79164b7ecd8486985d38604febdc6740d20b3ac88f6ad82a4fb08d71ab47a086e86eedf39d1c5bba97c4080126141d67f37be8538f5a8be740e484":"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710" +