test_suite_ccm.function 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/ccm.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_CCM_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
  9. void mbedtls_ccm_self_test( )
  10. {
  11. TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 );
  12. }
  13. /* END_CASE */
  14. /* BEGIN_CASE */
  15. void mbedtls_ccm_setkey( int cipher_id, int key_size, int result )
  16. {
  17. mbedtls_ccm_context ctx;
  18. unsigned char key[32];
  19. int ret;
  20. mbedtls_ccm_init( &ctx );
  21. memset( key, 0x2A, sizeof( key ) );
  22. TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
  23. ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size );
  24. TEST_ASSERT( ret == result );
  25. exit:
  26. mbedtls_ccm_free( &ctx );
  27. }
  28. /* END_CASE */
  29. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  30. void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res )
  31. {
  32. mbedtls_ccm_context ctx;
  33. unsigned char key[16];
  34. unsigned char msg[10];
  35. unsigned char iv[14];
  36. unsigned char *add = NULL;
  37. unsigned char out[10];
  38. unsigned char tag[18];
  39. int decrypt_ret;
  40. mbedtls_ccm_init( &ctx );
  41. ASSERT_ALLOC_WEAK( add, add_len );
  42. memset( key, 0, sizeof( key ) );
  43. memset( msg, 0, sizeof( msg ) );
  44. memset( iv, 0, sizeof( iv ) );
  45. memset( out, 0, sizeof( out ) );
  46. memset( tag, 0, sizeof( tag ) );
  47. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
  48. key, 8 * sizeof( key ) ) == 0 );
  49. TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
  50. msg, out, tag, tag_len ) == res );
  51. decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
  52. msg, out, tag, tag_len );
  53. if( res == 0 )
  54. TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
  55. else
  56. TEST_ASSERT( decrypt_ret == res );
  57. exit:
  58. mbedtls_free( add );
  59. mbedtls_ccm_free( &ctx );
  60. }
  61. /* END_CASE */
  62. /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
  63. void ccm_star_lengths( int msg_len, int iv_len, int add_len, int tag_len,
  64. int res )
  65. {
  66. mbedtls_ccm_context ctx;
  67. unsigned char key[16];
  68. unsigned char msg[10];
  69. unsigned char iv[14];
  70. unsigned char add[10];
  71. unsigned char out[10];
  72. unsigned char tag[18];
  73. int decrypt_ret;
  74. mbedtls_ccm_init( &ctx );
  75. memset( key, 0, sizeof( key ) );
  76. memset( msg, 0, sizeof( msg ) );
  77. memset( iv, 0, sizeof( iv ) );
  78. memset( add, 0, sizeof( add ) );
  79. memset( out, 0, sizeof( out ) );
  80. memset( tag, 0, sizeof( tag ) );
  81. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
  82. key, 8 * sizeof( key ) ) == 0 );
  83. TEST_ASSERT( mbedtls_ccm_star_encrypt_and_tag( &ctx, msg_len, iv, iv_len,
  84. add, add_len, msg, out, tag, tag_len ) == res );
  85. decrypt_ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg_len, iv, iv_len, add,
  86. add_len, msg, out, tag, tag_len );
  87. if( res == 0 && tag_len != 0 )
  88. TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
  89. else
  90. TEST_ASSERT( decrypt_ret == res );
  91. exit:
  92. mbedtls_ccm_free( &ctx );
  93. }
  94. /* END_CASE */
  95. /* BEGIN_CASE */
  96. void mbedtls_ccm_encrypt_and_tag( int cipher_id, data_t * key,
  97. data_t * msg, data_t * iv,
  98. data_t * add, data_t * result )
  99. {
  100. mbedtls_ccm_context ctx;
  101. size_t tag_len;
  102. uint8_t * msg_n_tag = (uint8_t *)malloc( result->len + 2 );
  103. mbedtls_ccm_init( &ctx );
  104. memset( msg_n_tag, 0, result->len + 2 );
  105. memcpy( msg_n_tag, msg->x, msg->len );
  106. tag_len = result->len - msg->len;
  107. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
  108. /* Test with input == output */
  109. TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg->len, iv->x, iv->len, add->x, add->len,
  110. msg_n_tag, msg_n_tag, msg_n_tag + msg->len, tag_len ) == 0 );
  111. TEST_ASSERT( memcmp( msg_n_tag, result->x, result->len ) == 0 );
  112. /* Check we didn't write past the end */
  113. TEST_ASSERT( msg_n_tag[result->len] == 0 && msg_n_tag[result->len + 1] == 0 );
  114. exit:
  115. mbedtls_ccm_free( &ctx );
  116. free( msg_n_tag );
  117. }
  118. /* END_CASE */
  119. /* BEGIN_CASE */
  120. void mbedtls_ccm_auth_decrypt( int cipher_id, data_t * key,
  121. data_t * msg, data_t * iv,
  122. data_t * add, int tag_len, int result,
  123. data_t * expected_msg )
  124. {
  125. unsigned char tag[16];
  126. mbedtls_ccm_context ctx;
  127. mbedtls_ccm_init( &ctx );
  128. memset( tag, 0x00, sizeof( tag ) );
  129. msg->len -= tag_len;
  130. memcpy( tag, msg->x + msg->len, tag_len );
  131. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
  132. /* Test with input == output */
  133. TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg->len, iv->x, iv->len, add->x, add->len,
  134. msg->x, msg->x, msg->x + msg->len, tag_len ) == result );
  135. if( result == 0 )
  136. {
  137. TEST_ASSERT( memcmp( msg->x, expected_msg->x, expected_msg->len ) == 0 );
  138. }
  139. else
  140. {
  141. size_t i;
  142. for( i = 0; i < msg->len; i++ )
  143. TEST_ASSERT( msg->x[i] == 0 );
  144. }
  145. /* Check we didn't write past the end (where the original tag is) */
  146. TEST_ASSERT( memcmp( msg->x + msg->len, tag, tag_len ) == 0 );
  147. exit:
  148. mbedtls_ccm_free( &ctx );
  149. }
  150. /* END_CASE */
  151. /* BEGIN_CASE */
  152. void mbedtls_ccm_star_encrypt_and_tag( int cipher_id,
  153. data_t *key, data_t *msg,
  154. data_t *source_address, data_t *frame_counter,
  155. int sec_level, data_t *add,
  156. data_t *expected_result, int output_ret )
  157. {
  158. unsigned char iv[13];
  159. unsigned char result[50];
  160. mbedtls_ccm_context ctx;
  161. size_t iv_len, tag_len;
  162. int ret;
  163. mbedtls_ccm_init( &ctx );
  164. memset( result, 0x00, sizeof( result ) );
  165. if( sec_level % 4 == 0)
  166. tag_len = 0;
  167. else
  168. tag_len = 1 << ( sec_level % 4 + 1);
  169. TEST_ASSERT( source_address->len == 8 );
  170. TEST_ASSERT( frame_counter->len == 4 );
  171. memcpy( iv, source_address->x, source_address->len );
  172. memcpy( iv + source_address->len, frame_counter->x, frame_counter->len );
  173. iv[source_address->len + frame_counter->len] = sec_level;
  174. iv_len = sizeof( iv );
  175. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id,
  176. key->x, key->len * 8 ) == 0 );
  177. ret = mbedtls_ccm_star_encrypt_and_tag( &ctx, msg->len, iv, iv_len,
  178. add->x, add->len, msg->x,
  179. result, result + msg->len, tag_len );
  180. TEST_ASSERT( ret == output_ret );
  181. TEST_ASSERT( memcmp( result,
  182. expected_result->x, expected_result->len ) == 0 );
  183. /* Check we didn't write past the end */
  184. TEST_ASSERT( result[expected_result->len] == 0 &&
  185. result[expected_result->len + 1] == 0 );
  186. exit:
  187. mbedtls_ccm_free( &ctx );
  188. }
  189. /* END_CASE */
  190. /* BEGIN_CASE */
  191. void mbedtls_ccm_star_auth_decrypt( int cipher_id,
  192. data_t *key, data_t *msg,
  193. data_t *source_address, data_t *frame_counter,
  194. int sec_level, data_t *add,
  195. data_t *expected_result, int output_ret )
  196. {
  197. unsigned char iv[13];
  198. unsigned char result[50];
  199. mbedtls_ccm_context ctx;
  200. size_t iv_len, tag_len;
  201. int ret;
  202. mbedtls_ccm_init( &ctx );
  203. memset( iv, 0x00, sizeof( iv ) );
  204. memset( result, '+', sizeof( result ) );
  205. if( sec_level % 4 == 0)
  206. tag_len = 0;
  207. else
  208. tag_len = 1 << ( sec_level % 4 + 1);
  209. TEST_ASSERT( source_address->len == 8 );
  210. TEST_ASSERT( frame_counter->len == 4 );
  211. memcpy( iv, source_address->x, source_address->len );
  212. memcpy( iv + source_address->len, frame_counter->x, frame_counter->len );
  213. iv[source_address->len + frame_counter->len] = sec_level;
  214. iv_len = sizeof( iv );
  215. TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
  216. ret = mbedtls_ccm_star_auth_decrypt( &ctx, msg->len - tag_len, iv, iv_len,
  217. add->x, add->len, msg->x, result,
  218. msg->x + msg->len - tag_len, tag_len );
  219. TEST_ASSERT( ret == output_ret );
  220. TEST_ASSERT( memcmp( result, expected_result->x,
  221. expected_result->len ) == 0 );
  222. /* Check we didn't write past the end (where the original tag is) */
  223. TEST_ASSERT( ( msg->len + 2 ) <= sizeof( result ) );
  224. TEST_EQUAL( result[msg->len], '+' );
  225. TEST_EQUAL( result[msg->len + 1], '+' );
  226. exit:
  227. mbedtls_ccm_free( &ctx );
  228. }
  229. /* END_CASE */
  230. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  231. void ccm_invalid_param( )
  232. {
  233. struct mbedtls_ccm_context ctx;
  234. unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
  235. mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
  236. int valid_len = sizeof(valid_buffer);
  237. int valid_bitlen = valid_len * 8;
  238. mbedtls_ccm_init( &ctx );
  239. /* mbedtls_ccm_init() */
  240. TEST_INVALID_PARAM( mbedtls_ccm_init( NULL ) );
  241. /* mbedtls_ccm_setkey() */
  242. TEST_INVALID_PARAM_RET(
  243. MBEDTLS_ERR_CCM_BAD_INPUT,
  244. mbedtls_ccm_setkey( NULL, valid_cipher, valid_buffer, valid_bitlen ) );
  245. TEST_INVALID_PARAM_RET(
  246. MBEDTLS_ERR_CCM_BAD_INPUT,
  247. mbedtls_ccm_setkey( &ctx, valid_cipher, NULL, valid_bitlen ) );
  248. /* mbedtls_ccm_encrypt_and_tag() */
  249. TEST_INVALID_PARAM_RET(
  250. MBEDTLS_ERR_CCM_BAD_INPUT,
  251. mbedtls_ccm_encrypt_and_tag( NULL, valid_len,
  252. valid_buffer, valid_len,
  253. valid_buffer, valid_len,
  254. valid_buffer, valid_buffer,
  255. valid_buffer, valid_len ) );
  256. TEST_INVALID_PARAM_RET(
  257. MBEDTLS_ERR_CCM_BAD_INPUT,
  258. mbedtls_ccm_encrypt_and_tag( &ctx, valid_len,
  259. NULL, valid_len,
  260. valid_buffer, valid_len,
  261. valid_buffer, valid_buffer,
  262. valid_buffer, valid_len ) );
  263. TEST_INVALID_PARAM_RET(
  264. MBEDTLS_ERR_CCM_BAD_INPUT,
  265. mbedtls_ccm_encrypt_and_tag( &ctx, valid_len,
  266. valid_buffer, valid_len,
  267. NULL, valid_len,
  268. valid_buffer, valid_buffer,
  269. valid_buffer, valid_len ) );
  270. TEST_INVALID_PARAM_RET(
  271. MBEDTLS_ERR_CCM_BAD_INPUT,
  272. mbedtls_ccm_encrypt_and_tag( &ctx, valid_len,
  273. valid_buffer, valid_len,
  274. valid_buffer, valid_len,
  275. NULL, valid_buffer,
  276. valid_buffer, valid_len ) );
  277. TEST_INVALID_PARAM_RET(
  278. MBEDTLS_ERR_CCM_BAD_INPUT,
  279. mbedtls_ccm_encrypt_and_tag( &ctx, valid_len,
  280. valid_buffer, valid_len,
  281. valid_buffer, valid_len,
  282. valid_buffer, NULL,
  283. valid_buffer, valid_len ) );
  284. TEST_INVALID_PARAM_RET(
  285. MBEDTLS_ERR_CCM_BAD_INPUT,
  286. mbedtls_ccm_encrypt_and_tag( &ctx, valid_len,
  287. valid_buffer, valid_len,
  288. valid_buffer, valid_len,
  289. valid_buffer, valid_buffer,
  290. NULL, valid_len ) );
  291. /* mbedtls_ccm_star_encrypt_and_tag() */
  292. TEST_INVALID_PARAM_RET(
  293. MBEDTLS_ERR_CCM_BAD_INPUT,
  294. mbedtls_ccm_star_encrypt_and_tag( NULL, valid_len,
  295. valid_buffer, valid_len,
  296. valid_buffer, valid_len,
  297. valid_buffer, valid_buffer,
  298. valid_buffer, valid_len) );
  299. TEST_INVALID_PARAM_RET(
  300. MBEDTLS_ERR_CCM_BAD_INPUT,
  301. mbedtls_ccm_star_encrypt_and_tag( &ctx, valid_len,
  302. NULL, valid_len,
  303. valid_buffer, valid_len,
  304. valid_buffer, valid_buffer,
  305. valid_buffer, valid_len ) );
  306. TEST_INVALID_PARAM_RET(
  307. MBEDTLS_ERR_CCM_BAD_INPUT,
  308. mbedtls_ccm_star_encrypt_and_tag( &ctx, valid_len,
  309. valid_buffer, valid_len,
  310. NULL, valid_len,
  311. valid_buffer, valid_buffer,
  312. valid_buffer, valid_len ) );
  313. TEST_INVALID_PARAM_RET(
  314. MBEDTLS_ERR_CCM_BAD_INPUT,
  315. mbedtls_ccm_star_encrypt_and_tag( &ctx, valid_len,
  316. valid_buffer, valid_len,
  317. valid_buffer, valid_len,
  318. NULL, valid_buffer,
  319. valid_buffer, valid_len ) );
  320. TEST_INVALID_PARAM_RET(
  321. MBEDTLS_ERR_CCM_BAD_INPUT,
  322. mbedtls_ccm_star_encrypt_and_tag( &ctx, valid_len,
  323. valid_buffer, valid_len,
  324. valid_buffer, valid_len,
  325. valid_buffer, NULL,
  326. valid_buffer, valid_len ) );
  327. TEST_INVALID_PARAM_RET(
  328. MBEDTLS_ERR_CCM_BAD_INPUT,
  329. mbedtls_ccm_star_encrypt_and_tag( &ctx, valid_len,
  330. valid_buffer, valid_len,
  331. valid_buffer, valid_len,
  332. valid_buffer, valid_buffer,
  333. NULL, valid_len ) );
  334. /* mbedtls_ccm_auth_decrypt() */
  335. TEST_INVALID_PARAM_RET(
  336. MBEDTLS_ERR_CCM_BAD_INPUT,
  337. mbedtls_ccm_auth_decrypt( NULL, valid_len,
  338. valid_buffer, valid_len,
  339. valid_buffer, valid_len,
  340. valid_buffer, valid_buffer,
  341. valid_buffer, valid_len ) );
  342. TEST_INVALID_PARAM_RET(
  343. MBEDTLS_ERR_CCM_BAD_INPUT,
  344. mbedtls_ccm_auth_decrypt( &ctx, valid_len,
  345. NULL, valid_len,
  346. valid_buffer, valid_len,
  347. valid_buffer, valid_buffer,
  348. valid_buffer, valid_len ) );
  349. TEST_INVALID_PARAM_RET(
  350. MBEDTLS_ERR_CCM_BAD_INPUT,
  351. mbedtls_ccm_auth_decrypt( &ctx, valid_len,
  352. valid_buffer, valid_len,
  353. NULL, valid_len,
  354. valid_buffer, valid_buffer,
  355. valid_buffer, valid_len ) );
  356. TEST_INVALID_PARAM_RET(
  357. MBEDTLS_ERR_CCM_BAD_INPUT,
  358. mbedtls_ccm_auth_decrypt( &ctx, valid_len,
  359. valid_buffer, valid_len,
  360. valid_buffer, valid_len,
  361. NULL, valid_buffer,
  362. valid_buffer, valid_len ) );
  363. TEST_INVALID_PARAM_RET(
  364. MBEDTLS_ERR_CCM_BAD_INPUT,
  365. mbedtls_ccm_auth_decrypt( &ctx, valid_len,
  366. valid_buffer, valid_len,
  367. valid_buffer, valid_len,
  368. valid_buffer, NULL,
  369. valid_buffer, valid_len ) );
  370. TEST_INVALID_PARAM_RET(
  371. MBEDTLS_ERR_CCM_BAD_INPUT,
  372. mbedtls_ccm_auth_decrypt( &ctx, valid_len,
  373. valid_buffer, valid_len,
  374. valid_buffer, valid_len,
  375. valid_buffer, valid_buffer,
  376. NULL, valid_len ) );
  377. /* mbedtls_ccm_star_auth_decrypt() */
  378. TEST_INVALID_PARAM_RET(
  379. MBEDTLS_ERR_CCM_BAD_INPUT,
  380. mbedtls_ccm_star_auth_decrypt( NULL, valid_len,
  381. valid_buffer, valid_len,
  382. valid_buffer, valid_len,
  383. valid_buffer, valid_buffer,
  384. valid_buffer, valid_len ) );
  385. TEST_INVALID_PARAM_RET(
  386. MBEDTLS_ERR_CCM_BAD_INPUT,
  387. mbedtls_ccm_star_auth_decrypt( &ctx, valid_len,
  388. NULL, valid_len,
  389. valid_buffer, valid_len,
  390. valid_buffer, valid_buffer,
  391. valid_buffer, valid_len ) );
  392. TEST_INVALID_PARAM_RET(
  393. MBEDTLS_ERR_CCM_BAD_INPUT,
  394. mbedtls_ccm_star_auth_decrypt( &ctx, valid_len,
  395. valid_buffer, valid_len,
  396. NULL, valid_len,
  397. valid_buffer, valid_buffer,
  398. valid_buffer, valid_len ) );
  399. TEST_INVALID_PARAM_RET(
  400. MBEDTLS_ERR_CCM_BAD_INPUT,
  401. mbedtls_ccm_star_auth_decrypt( &ctx, valid_len,
  402. valid_buffer, valid_len,
  403. valid_buffer, valid_len,
  404. NULL, valid_buffer,
  405. valid_buffer, valid_len ) );
  406. TEST_INVALID_PARAM_RET(
  407. MBEDTLS_ERR_CCM_BAD_INPUT,
  408. mbedtls_ccm_star_auth_decrypt( &ctx, valid_len,
  409. valid_buffer, valid_len,
  410. valid_buffer, valid_len,
  411. valid_buffer, NULL,
  412. valid_buffer, valid_len ) );
  413. TEST_INVALID_PARAM_RET(
  414. MBEDTLS_ERR_CCM_BAD_INPUT,
  415. mbedtls_ccm_star_auth_decrypt( &ctx, valid_len,
  416. valid_buffer, valid_len,
  417. valid_buffer, valid_len,
  418. valid_buffer, valid_buffer,
  419. NULL, valid_len ) );
  420. exit:
  421. mbedtls_ccm_free( &ctx );
  422. return;
  423. }
  424. /* END_CASE */
  425. /* BEGIN_CASE */
  426. void ccm_valid_param( )
  427. {
  428. TEST_VALID_PARAM( mbedtls_ccm_free( NULL ) );
  429. exit:
  430. return;
  431. }
  432. /* END_CASE */