From d899adaa20c3242200867b4a8b41af08c60f2abe Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 11 Jun 2015 03:24:02 +0300 Subject: add `assert`s --- include/aesni/block.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/common.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) diff --git a/include/aesni/block.h b/include/aesni/block.h index 7f8be7b..0909744 100644 --- a/include/aesni/block.h +++ b/include/aesni/block.h @@ -11,6 +11,8 @@ #include "data.h" #include "raw.h" +#include + #ifdef __cplusplus extern "C" { @@ -20,6 +22,8 @@ static __inline void __fastcall aesni_expand_key_schedule128( AesNI_Block128 key, AesNI_KeySchedule128* key_schedule) { + assert(key_schedule); + aesni_raw_expand_key_schedule128(key, key_schedule); } @@ -27,6 +31,9 @@ static __inline void __fastcall aesni_invert_key_schedule128( AesNI_KeySchedule128* key_schedule, AesNI_KeySchedule128* inverted_schedule) { + assert(key_schedule); + assert(inverted_schedule); + aesni_raw_invert_key_schedule128(key_schedule, inverted_schedule); } @@ -34,6 +41,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb128( AesNI_Block128 plain, AesNI_KeySchedule128* key_schedule) { + assert(key_schedule); + return aesni_raw_encrypt_block128(plain, key_schedule); } @@ -41,6 +50,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb128( AesNI_Block128 cipher, AesNI_KeySchedule128* inverted_schedule) { + assert(inverted_schedule); + return aesni_raw_decrypt_block128(cipher, inverted_schedule); } @@ -50,6 +61,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = aesni_raw_encrypt_block128(_mm_xor_si128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -61,6 +75,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(inverted_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block128(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; return plain; @@ -72,6 +89,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block128(init_vector, key_schedule), plain); *next_init_vector = cipher; return cipher; @@ -83,6 +103,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block128(init_vector, key_schedule), cipher); *next_init_vector = cipher; return plain; @@ -94,6 +117,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block128(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, plain); @@ -105,6 +131,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb128( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block128(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, cipher); @@ -116,6 +145,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr128( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); @@ -128,6 +159,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr128( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); @@ -138,6 +171,9 @@ static __inline void __fastcall aesni_expand_key_schedule192( AesNI_Block192* key, AesNI_KeySchedule192* key_schedule) { + assert(key); + assert(key_schedule); + aesni_raw_expand_key_schedule192(key->lo, key->hi, key_schedule); } @@ -145,6 +181,9 @@ static __inline void __fastcall aesni_invert_key_schedule192( AesNI_KeySchedule192* key_schedule, AesNI_KeySchedule192* inverted_schedule) { + assert(key_schedule); + assert(inverted_schedule); + aesni_raw_invert_key_schedule192(key_schedule, inverted_schedule); } @@ -152,6 +191,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb192( AesNI_Block128 plain, AesNI_KeySchedule192* key_schedule) { + assert(key_schedule); + return aesni_raw_encrypt_block192(plain, key_schedule); } @@ -159,6 +200,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb192( AesNI_Block128 cipher, AesNI_KeySchedule192* inverted_schedule) { + assert(inverted_schedule); + return aesni_raw_decrypt_block192(cipher, inverted_schedule); } @@ -168,6 +211,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = aesni_raw_encrypt_block192(_mm_xor_si128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -179,6 +225,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(inverted_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block192(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; return plain; @@ -190,6 +239,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block192(init_vector, key_schedule), plain); *next_init_vector = cipher; return cipher; @@ -201,6 +253,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block192(init_vector, key_schedule), cipher); *next_init_vector = cipher; return plain; @@ -212,6 +267,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block192(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, plain); @@ -223,6 +281,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb192( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block192(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, cipher); @@ -234,6 +295,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr192( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); @@ -246,6 +309,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr192( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); @@ -256,6 +321,9 @@ static __inline void __fastcall aesni_expand_key_schedule256( AesNI_Block256* key, AesNI_KeySchedule256* key_schedule) { + assert(key); + assert(key_schedule); + aesni_raw_expand_key_schedule256(key->lo, key->hi, key_schedule); } @@ -263,6 +331,9 @@ static __inline void __fastcall aesni_invert_key_schedule256( AesNI_KeySchedule256* key_schedule, AesNI_KeySchedule256* inverted_schedule) { + assert(key_schedule); + assert(inverted_schedule); + aesni_raw_invert_key_schedule256(key_schedule, inverted_schedule); } @@ -270,6 +341,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ecb256( AesNI_Block128 plain, AesNI_KeySchedule256* key_schedule) { + assert(key_schedule); + return aesni_raw_encrypt_block256(plain, key_schedule); } @@ -277,6 +350,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ecb256( AesNI_Block128 cipher, AesNI_KeySchedule256* inverted_schedule) { + assert(inverted_schedule); + return aesni_raw_decrypt_block256(cipher, inverted_schedule); } @@ -286,6 +361,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cbc256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = aesni_raw_encrypt_block256(_mm_xor_si128(plain, init_vector), key_schedule); *next_init_vector = cipher; return cipher; @@ -297,6 +375,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cbc256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(inverted_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_decrypt_block256(cipher, inverted_schedule), init_vector); *next_init_vector = cipher; return plain; @@ -308,6 +389,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_cfb256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 cipher = _mm_xor_si128(aesni_raw_encrypt_block256(init_vector, key_schedule), plain); *next_init_vector = cipher; return cipher; @@ -319,6 +403,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_cfb256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 plain = _mm_xor_si128(aesni_raw_encrypt_block256(init_vector, key_schedule), cipher); *next_init_vector = cipher; return plain; @@ -330,6 +417,9 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ofb256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block256(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, plain); @@ -341,6 +431,9 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ofb256( AesNI_Block128 init_vector, AesNI_Block128* next_init_vector) { + assert(key_schedule); + assert(next_init_vector); + AesNI_Block128 tmp = aesni_raw_encrypt_block256(init_vector, key_schedule); *next_init_vector = tmp; return _mm_xor_si128(tmp, cipher); @@ -352,6 +445,8 @@ static __inline AesNI_Block128 __fastcall aesni_encrypt_block_ctr256( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); @@ -364,6 +459,8 @@ static __inline AesNI_Block128 __fastcall aesni_decrypt_block_ctr256( AesNI_Block128 init_vector, int counter) { + assert(key_schedule); + init_vector = aesni_le2be128(init_vector); init_vector = _mm_add_epi32(init_vector, aesni_make_block128(0, 0, 0, counter)); init_vector = aesni_be2le128(init_vector); diff --git a/src/common.c b/src/common.c index e8d6e5b..3f64b80 100644 --- a/src/common.c +++ b/src/common.c @@ -10,11 +10,14 @@ #include +#include #include #include AesNI_BlockString128 aesni_format_block128(AesNI_Block128* block) { + assert(block); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_format_block128_le(block); #else @@ -24,6 +27,8 @@ AesNI_BlockString128 aesni_format_block128(AesNI_Block128* block) AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block) { + assert(block); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_format_block192_le(block); #else @@ -33,6 +38,8 @@ AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block) AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block) { + assert(block); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_format_block256_le(block); #else @@ -42,6 +49,8 @@ AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block) AesNI_BlockString128 aesni_format_block128_le(AesNI_Block128* block) { + assert(block); + AesNI_BlockString128 result; char *cursor = result.str; @@ -54,6 +63,8 @@ AesNI_BlockString128 aesni_format_block128_le(AesNI_Block128* block) AesNI_BlockString192 aesni_format_block192_le(AesNI_Block192* block) { + assert(block); + AesNI_BlockString192 result; char *cursor = result.str; @@ -68,6 +79,8 @@ AesNI_BlockString192 aesni_format_block192_le(AesNI_Block192* block) AesNI_BlockString256 aesni_format_block256_le(AesNI_Block256* block) { + assert(block); + AesNI_BlockString256 result; char *cursor = result.str; @@ -82,6 +95,8 @@ AesNI_BlockString256 aesni_format_block256_le(AesNI_Block256* block) AesNI_BlockString128 aesni_format_block128_be(AesNI_Block128* block) { + assert(block); + AesNI_BlockString128 result; char *cursor = result.str; @@ -94,6 +109,8 @@ AesNI_BlockString128 aesni_format_block128_be(AesNI_Block128* block) AesNI_BlockString192 aesni_format_block192_be(AesNI_Block192* block) { + assert(block); + AesNI_BlockString192 result; char *cursor = result.str; @@ -108,6 +125,8 @@ AesNI_BlockString192 aesni_format_block192_be(AesNI_Block192* block) AesNI_BlockString256 aesni_format_block256_be(AesNI_Block256* block) { + assert(block); + AesNI_BlockString256 result; char *cursor = result.str; @@ -122,21 +141,29 @@ AesNI_BlockString256 aesni_format_block256_be(AesNI_Block256* block) AesNI_BlockMatrixString128 aesni_format_block128_as_matrix(AesNI_Block128* block) { + assert(block); + return aesni_format_block128_be_as_matrix(block); } AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192* block) { + assert(block); + return aesni_format_block192_be_as_matrix(block); } AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256* block) { + assert(block); + return aesni_format_block256_be_as_matrix(block); } AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* block) { + assert(block); + __declspec(align(16)) unsigned char bytes[4][4]; AesNI_BlockMatrixString128 result; char* cursor = result.str; @@ -156,6 +183,8 @@ AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* bl AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* block) { + assert(block); + __declspec(align(16)) unsigned char bytes[8][4]; AesNI_BlockMatrixString192 result; char* cursor = result.str; @@ -176,6 +205,8 @@ AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* bl AesNI_BlockMatrixString256 aesni_format_block256_be_as_matrix(AesNI_Block256* block) { + assert(block); + __declspec(align(16)) unsigned char bytes[8][4]; AesNI_BlockMatrixString256 result; char* cursor = result.str; @@ -196,81 +227,114 @@ AesNI_BlockMatrixString256 aesni_format_block256_be_as_matrix(AesNI_Block256* bl void aesni_print_block128(AesNI_Block128* block) { + assert(block); + printf("%s\n", aesni_format_block128(block).str); } void aesni_print_block192(AesNI_Block192* block) { + assert(block); + printf("%s\n", aesni_format_block192(block).str); } void aesni_print_block256(AesNI_Block256* block) { + assert(block); + printf("%s\n", aesni_format_block256(block).str); } void aesni_print_block128_le(AesNI_Block128* block) { + assert(block); + printf("%s\n", aesni_format_block128_le(block).str); } void aesni_print_block192_le(AesNI_Block192* block) { + assert(block); + printf("%s\n", aesni_format_block192_le(block).str); } void aesni_print_block256_le(AesNI_Block256* block) { + assert(block); + printf("%s\n", aesni_format_block256_le(block).str); } void aesni_print_block128_be(AesNI_Block128* block) { + assert(block); + printf("%s\n", aesni_format_block128_be(block).str); } void aesni_print_block192_be(AesNI_Block192* block) { + assert(block); + printf("%s\n", aesni_format_block192_be(block).str); } void aesni_print_block256_be(AesNI_Block256* block) { + assert(block); + printf("%s\n", aesni_format_block256_be(block).str); } void aesni_print_block128_as_matrix(AesNI_Block128* block) { + assert(block); + printf("%s", aesni_format_block128_as_matrix(block).str); } void aesni_print_block192_as_matrix(AesNI_Block192* block) { + assert(block); + printf("%s", aesni_format_block192_as_matrix(block).str); } void aesni_print_block256_as_matrix(AesNI_Block256* block) { + assert(block); + printf("%s", aesni_format_block256_as_matrix(block).str); } void aesni_print_block128_be_as_matrix(AesNI_Block128* block) { + assert(block); + printf("%s", aesni_format_block128_be_as_matrix(block).str); } void aesni_print_block192_be_as_matrix(AesNI_Block192* block) { + assert(block); + printf("%s", aesni_format_block192_be_as_matrix(block).str); } void aesni_print_block256_be_as_matrix(AesNI_Block256* block) { + assert(block); + printf("%s", aesni_format_block256_be_as_matrix(block).str); } int aesni_parse_block128(AesNI_Block128* block, const char* src) { + assert(block); + assert(src); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_parse_block128_le(block, src); #else @@ -280,6 +344,9 @@ int aesni_parse_block128(AesNI_Block128* block, const char* src) int aesni_parse_block192(AesNI_Block192* block, const char* src) { + assert(block); + assert(src); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_parse_block192_le(block, src); #else @@ -289,6 +356,9 @@ int aesni_parse_block192(AesNI_Block192* block, const char* src) int aesni_parse_block256(AesNI_Block256* block, const char* src) { + assert(block); + assert(src); + #if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO return aesni_parse_block256_le(block, src); #else @@ -298,6 +368,9 @@ int aesni_parse_block256(AesNI_Block256* block, const char* src) int aesni_parse_block128_le(AesNI_Block128* block, const char* src) { + assert(block); + assert(src); + int n, xs[4]; if (sscanf(src, "%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &n) != 4 || n != strlen(src)) @@ -308,6 +381,9 @@ int aesni_parse_block128_le(AesNI_Block128* block, const char* src) int aesni_parse_block192_le(AesNI_Block192* block, const char* src) { + assert(block); + assert(src); + int n, xs[6]; if (sscanf(src, "%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &n) != 6 || n != strlen(src)) @@ -318,6 +394,9 @@ int aesni_parse_block192_le(AesNI_Block192* block, const char* src) int aesni_parse_block256_le(AesNI_Block256* block, const char* src) { + assert(block); + assert(src); + int n, xs[8]; if (sscanf(src, "%8x%8x%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &xs[6], &xs[7], &n) != 8 || n != strlen(src)) @@ -328,6 +407,9 @@ int aesni_parse_block256_le(AesNI_Block256* block, const char* src) int aesni_parse_block128_be(AesNI_Block128* block, const char* src) { + assert(block); + assert(src); + unsigned char bytes[16]; for (int i = 0; i < 16; ++i) @@ -346,6 +428,9 @@ int aesni_parse_block128_be(AesNI_Block128* block, const char* src) int aesni_parse_block192_be(AesNI_Block192* block, const char* src) { + assert(block); + assert(src); + AesNI_Block128 lo, hi; unsigned char lo_bytes[16], hi_bytes[16] = { 0 }; @@ -380,6 +465,9 @@ int aesni_parse_block192_be(AesNI_Block192* block, const char* src) int aesni_parse_block256_be(AesNI_Block256* block, const char* src) { + assert(block); + assert(src); + AesNI_Block128 lo, hi; unsigned char lo_bytes[16], hi_bytes[16]; -- cgit v1.2.3