test_suite_aes.function 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/aes.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_AES_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void aes_encrypt_ecb( char *hex_key_string, char *hex_src_string,
  10. char *hex_dst_string, int setkey_result )
  11. {
  12. unsigned char key_str[100];
  13. unsigned char src_str[100];
  14. unsigned char dst_str[100];
  15. unsigned char output[100];
  16. mbedtls_aes_context ctx;
  17. int key_len;
  18. memset(key_str, 0x00, 100);
  19. memset(src_str, 0x00, 100);
  20. memset(dst_str, 0x00, 100);
  21. memset(output, 0x00, 100);
  22. mbedtls_aes_init( &ctx );
  23. key_len = unhexify( key_str, hex_key_string );
  24. unhexify( src_str, hex_src_string );
  25. TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
  26. if( setkey_result == 0 )
  27. {
  28. TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 );
  29. hexify( dst_str, output, 16 );
  30. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  31. }
  32. exit:
  33. mbedtls_aes_free( &ctx );
  34. }
  35. /* END_CASE */
  36. /* BEGIN_CASE */
  37. void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
  38. char *hex_dst_string, int setkey_result )
  39. {
  40. unsigned char key_str[100];
  41. unsigned char src_str[100];
  42. unsigned char dst_str[100];
  43. unsigned char output[100];
  44. mbedtls_aes_context ctx;
  45. int key_len;
  46. memset(key_str, 0x00, 100);
  47. memset(src_str, 0x00, 100);
  48. memset(dst_str, 0x00, 100);
  49. memset(output, 0x00, 100);
  50. mbedtls_aes_init( &ctx );
  51. key_len = unhexify( key_str, hex_key_string );
  52. unhexify( src_str, hex_src_string );
  53. TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
  54. if( setkey_result == 0 )
  55. {
  56. TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 );
  57. hexify( dst_str, output, 16 );
  58. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  59. }
  60. exit:
  61. mbedtls_aes_free( &ctx );
  62. }
  63. /* END_CASE */
  64. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  65. void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
  66. char *hex_src_string, char *hex_dst_string,
  67. int cbc_result )
  68. {
  69. unsigned char key_str[100];
  70. unsigned char iv_str[100];
  71. unsigned char src_str[100];
  72. unsigned char dst_str[100];
  73. unsigned char output[100];
  74. mbedtls_aes_context ctx;
  75. int key_len, data_len;
  76. memset(key_str, 0x00, 100);
  77. memset(iv_str, 0x00, 100);
  78. memset(src_str, 0x00, 100);
  79. memset(dst_str, 0x00, 100);
  80. memset(output, 0x00, 100);
  81. mbedtls_aes_init( &ctx );
  82. key_len = unhexify( key_str, hex_key_string );
  83. unhexify( iv_str, hex_iv_string );
  84. data_len = unhexify( src_str, hex_src_string );
  85. mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
  86. TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
  87. if( cbc_result == 0 )
  88. {
  89. hexify( dst_str, output, data_len );
  90. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  91. }
  92. exit:
  93. mbedtls_aes_free( &ctx );
  94. }
  95. /* END_CASE */
  96. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  97. void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
  98. char *hex_src_string, char *hex_dst_string,
  99. int cbc_result )
  100. {
  101. unsigned char key_str[100];
  102. unsigned char iv_str[100];
  103. unsigned char src_str[100];
  104. unsigned char dst_str[100];
  105. unsigned char output[100];
  106. mbedtls_aes_context ctx;
  107. int key_len, data_len;
  108. memset(key_str, 0x00, 100);
  109. memset(iv_str, 0x00, 100);
  110. memset(src_str, 0x00, 100);
  111. memset(dst_str, 0x00, 100);
  112. memset(output, 0x00, 100);
  113. mbedtls_aes_init( &ctx );
  114. key_len = unhexify( key_str, hex_key_string );
  115. unhexify( iv_str, hex_iv_string );
  116. data_len = unhexify( src_str, hex_src_string );
  117. mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 );
  118. TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
  119. if( cbc_result == 0)
  120. {
  121. hexify( dst_str, output, data_len );
  122. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  123. }
  124. exit:
  125. mbedtls_aes_free( &ctx );
  126. }
  127. /* END_CASE */
  128. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  129. void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
  130. char *hex_src_string, char *hex_dst_string )
  131. {
  132. unsigned char key_str[100];
  133. unsigned char iv_str[100];
  134. unsigned char src_str[100];
  135. unsigned char dst_str[100];
  136. unsigned char output[100];
  137. mbedtls_aes_context ctx;
  138. size_t iv_offset = 0;
  139. int key_len;
  140. memset(key_str, 0x00, 100);
  141. memset(iv_str, 0x00, 100);
  142. memset(src_str, 0x00, 100);
  143. memset(dst_str, 0x00, 100);
  144. memset(output, 0x00, 100);
  145. mbedtls_aes_init( &ctx );
  146. key_len = unhexify( key_str, hex_key_string );
  147. unhexify( iv_str, hex_iv_string );
  148. unhexify( src_str, hex_src_string );
  149. mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
  150. TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
  151. hexify( dst_str, output, 16 );
  152. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  153. exit:
  154. mbedtls_aes_free( &ctx );
  155. }
  156. /* END_CASE */
  157. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  158. void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
  159. char *hex_src_string, char *hex_dst_string )
  160. {
  161. unsigned char key_str[100];
  162. unsigned char iv_str[100];
  163. unsigned char src_str[100];
  164. unsigned char dst_str[100];
  165. unsigned char output[100];
  166. mbedtls_aes_context ctx;
  167. size_t iv_offset = 0;
  168. int key_len;
  169. memset(key_str, 0x00, 100);
  170. memset(iv_str, 0x00, 100);
  171. memset(src_str, 0x00, 100);
  172. memset(dst_str, 0x00, 100);
  173. memset(output, 0x00, 100);
  174. mbedtls_aes_init( &ctx );
  175. key_len = unhexify( key_str, hex_key_string );
  176. unhexify( iv_str, hex_iv_string );
  177. unhexify( src_str, hex_src_string );
  178. mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
  179. TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
  180. hexify( dst_str, output, 16 );
  181. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  182. exit:
  183. mbedtls_aes_free( &ctx );
  184. }
  185. /* END_CASE */
  186. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  187. void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
  188. char *hex_src_string, char *hex_dst_string )
  189. {
  190. unsigned char key_str[100];
  191. unsigned char iv_str[100];
  192. unsigned char src_str[100];
  193. unsigned char dst_str[100];
  194. unsigned char output[100];
  195. mbedtls_aes_context ctx;
  196. int key_len, src_len;
  197. memset(key_str, 0x00, 100);
  198. memset(iv_str, 0x00, 100);
  199. memset(src_str, 0x00, 100);
  200. memset(dst_str, 0x00, 100);
  201. memset(output, 0x00, 100);
  202. mbedtls_aes_init( &ctx );
  203. key_len = unhexify( key_str, hex_key_string );
  204. unhexify( iv_str, hex_iv_string );
  205. src_len = unhexify( src_str, hex_src_string );
  206. mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
  207. TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 );
  208. hexify( dst_str, output, src_len );
  209. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  210. exit:
  211. mbedtls_aes_free( &ctx );
  212. }
  213. /* END_CASE */
  214. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  215. void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
  216. char *hex_src_string, char *hex_dst_string )
  217. {
  218. unsigned char key_str[100];
  219. unsigned char iv_str[100];
  220. unsigned char src_str[100];
  221. unsigned char dst_str[100];
  222. unsigned char output[100];
  223. mbedtls_aes_context ctx;
  224. int key_len, src_len;
  225. memset(key_str, 0x00, 100);
  226. memset(iv_str, 0x00, 100);
  227. memset(src_str, 0x00, 100);
  228. memset(dst_str, 0x00, 100);
  229. memset(output, 0x00, 100);
  230. mbedtls_aes_init( &ctx );
  231. key_len = unhexify( key_str, hex_key_string );
  232. unhexify( iv_str, hex_iv_string );
  233. src_len = unhexify( src_str, hex_src_string );
  234. mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
  235. TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 );
  236. hexify( dst_str, output, src_len );
  237. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  238. exit:
  239. mbedtls_aes_free( &ctx );
  240. }
  241. /* END_CASE */
  242. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  243. void aes_selftest()
  244. {
  245. TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
  246. }
  247. /* END_CASE */