test_suite_chachapoly.function 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/chachapoly.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_CHACHAPOLY_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void mbedtls_chachapoly_enc( data_t *key_str, data_t *nonce_str, data_t *aad_str, data_t *input_str, data_t *output_str, data_t *mac_str )
  10. {
  11. unsigned char output[265];
  12. unsigned char mac[16]; /* size set by the standard */
  13. mbedtls_chachapoly_context ctx;
  14. TEST_ASSERT( key_str->len == 32 );
  15. TEST_ASSERT( nonce_str->len == 12 );
  16. TEST_ASSERT( mac_str->len == 16 );
  17. mbedtls_chachapoly_init( &ctx );
  18. TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, key_str->x ) == 0 );
  19. TEST_ASSERT( mbedtls_chachapoly_encrypt_and_tag( &ctx,
  20. input_str->len, nonce_str->x,
  21. aad_str->x, aad_str->len,
  22. input_str->x, output, mac ) == 0 );
  23. TEST_ASSERT( memcmp( output_str->x, output, output_str->len ) == 0 );
  24. TEST_ASSERT( memcmp( mac_str->x, mac, 16U ) == 0 );
  25. exit:
  26. mbedtls_chachapoly_free( &ctx );
  27. }
  28. /* END_CASE */
  29. /* BEGIN_CASE */
  30. void mbedtls_chachapoly_dec( data_t *key_str, data_t *nonce_str, data_t *aad_str, data_t *input_str, data_t *output_str, data_t *mac_str, int ret_exp )
  31. {
  32. unsigned char output[265];
  33. int ret;
  34. mbedtls_chachapoly_context ctx;
  35. TEST_ASSERT( key_str->len == 32 );
  36. TEST_ASSERT( nonce_str->len == 12 );
  37. TEST_ASSERT( mac_str->len == 16 );
  38. mbedtls_chachapoly_init( &ctx );
  39. TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, key_str->x ) == 0 );
  40. ret = mbedtls_chachapoly_auth_decrypt( &ctx,
  41. input_str->len, nonce_str->x,
  42. aad_str->x, aad_str->len,
  43. mac_str->x, input_str->x, output );
  44. TEST_ASSERT( ret == ret_exp );
  45. if( ret_exp == 0 )
  46. {
  47. TEST_ASSERT( memcmp( output_str->x, output, output_str->len ) == 0 );
  48. }
  49. exit:
  50. mbedtls_chachapoly_free( &ctx );
  51. }
  52. /* END_CASE */
  53. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  54. void chachapoly_bad_params()
  55. {
  56. unsigned char key[32];
  57. unsigned char nonce[12];
  58. unsigned char aad[1];
  59. unsigned char input[1];
  60. unsigned char output[1];
  61. unsigned char mac[16];
  62. size_t input_len = sizeof( input );
  63. size_t aad_len = sizeof( aad );
  64. mbedtls_chachapoly_context ctx;
  65. memset( key, 0x00, sizeof( key ) );
  66. memset( nonce, 0x00, sizeof( nonce ) );
  67. memset( aad, 0x00, sizeof( aad ) );
  68. memset( input, 0x00, sizeof( input ) );
  69. memset( output, 0x00, sizeof( output ) );
  70. memset( mac, 0x00, sizeof( mac ) );
  71. TEST_INVALID_PARAM( mbedtls_chachapoly_init( NULL ) );
  72. TEST_VALID_PARAM( mbedtls_chachapoly_free( NULL ) );
  73. /* setkey */
  74. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  75. mbedtls_chachapoly_setkey( NULL, key ) );
  76. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  77. mbedtls_chachapoly_setkey( &ctx, NULL ) );
  78. /* encrypt_and_tag */
  79. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  80. mbedtls_chachapoly_encrypt_and_tag( NULL,
  81. 0, nonce,
  82. aad, 0,
  83. input, output, mac ) );
  84. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  85. mbedtls_chachapoly_encrypt_and_tag( &ctx,
  86. 0, NULL,
  87. aad, 0,
  88. input, output, mac ) );
  89. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  90. mbedtls_chachapoly_encrypt_and_tag( &ctx,
  91. 0, nonce,
  92. NULL, aad_len,
  93. input, output, mac ) );
  94. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  95. mbedtls_chachapoly_encrypt_and_tag( &ctx,
  96. input_len, nonce,
  97. aad, 0,
  98. NULL, output, mac ) );
  99. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  100. mbedtls_chachapoly_encrypt_and_tag( &ctx,
  101. input_len, nonce,
  102. aad, 0,
  103. input, NULL, mac ) );
  104. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  105. mbedtls_chachapoly_encrypt_and_tag( &ctx,
  106. 0, nonce,
  107. aad, 0,
  108. input, output, NULL ) );
  109. /* auth_decrypt */
  110. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  111. mbedtls_chachapoly_auth_decrypt( NULL,
  112. 0, nonce,
  113. aad, 0,
  114. mac, input, output ) );
  115. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  116. mbedtls_chachapoly_auth_decrypt( &ctx,
  117. 0, NULL,
  118. aad, 0,
  119. mac, input, output ) );
  120. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  121. mbedtls_chachapoly_auth_decrypt( &ctx,
  122. 0, nonce,
  123. NULL, aad_len,
  124. mac, input, output ) );
  125. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  126. mbedtls_chachapoly_auth_decrypt( &ctx,
  127. 0, nonce,
  128. aad, 0,
  129. NULL, input, output ) );
  130. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  131. mbedtls_chachapoly_auth_decrypt( &ctx,
  132. input_len, nonce,
  133. aad, 0,
  134. mac, NULL, output ) );
  135. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  136. mbedtls_chachapoly_auth_decrypt( &ctx,
  137. input_len, nonce,
  138. aad, 0,
  139. mac, input, NULL ) );
  140. /* starts */
  141. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  142. mbedtls_chachapoly_starts( NULL, nonce,
  143. MBEDTLS_CHACHAPOLY_ENCRYPT ) );
  144. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  145. mbedtls_chachapoly_starts( &ctx, NULL,
  146. MBEDTLS_CHACHAPOLY_ENCRYPT ) );
  147. /* update_aad */
  148. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  149. mbedtls_chachapoly_update_aad( NULL, aad,
  150. aad_len ) );
  151. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  152. mbedtls_chachapoly_update_aad( &ctx, NULL,
  153. aad_len ) );
  154. /* update */
  155. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  156. mbedtls_chachapoly_update( NULL, input_len,
  157. input, output ) );
  158. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  159. mbedtls_chachapoly_update( &ctx, input_len,
  160. NULL, output ) );
  161. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  162. mbedtls_chachapoly_update( &ctx, input_len,
  163. input, NULL ) );
  164. /* finish */
  165. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  166. mbedtls_chachapoly_finish( NULL, mac ) );
  167. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA,
  168. mbedtls_chachapoly_finish( &ctx, NULL ) );
  169. exit:
  170. return;
  171. }
  172. /* END_CASE */
  173. /* BEGIN_CASE */
  174. void chachapoly_state()
  175. {
  176. unsigned char key[32];
  177. unsigned char nonce[12];
  178. unsigned char aad[1];
  179. unsigned char input[1];
  180. unsigned char output[1];
  181. unsigned char mac[16];
  182. size_t input_len = sizeof( input );
  183. size_t aad_len = sizeof( aad );
  184. mbedtls_chachapoly_context ctx;
  185. memset( key, 0x00, sizeof( key ) );
  186. memset( nonce, 0x00, sizeof( nonce ) );
  187. memset( aad, 0x00, sizeof( aad ) );
  188. memset( input, 0x00, sizeof( input ) );
  189. memset( output, 0x00, sizeof( output ) );
  190. memset( mac, 0x00, sizeof( mac ) );
  191. /* Initial state: finish, update, update_aad forbidden */
  192. mbedtls_chachapoly_init( &ctx );
  193. TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, mac )
  194. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  195. TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, output )
  196. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  197. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  198. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  199. /* Still initial state: finish, update, update_aad forbidden */
  200. TEST_ASSERT( mbedtls_chachapoly_setkey( &ctx, key )
  201. == 0 );
  202. TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, mac )
  203. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  204. TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, output )
  205. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  206. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  207. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  208. /* Starts -> finish OK */
  209. TEST_ASSERT( mbedtls_chachapoly_starts( &ctx, nonce, MBEDTLS_CHACHAPOLY_ENCRYPT )
  210. == 0 );
  211. TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, mac )
  212. == 0 );
  213. /* After finish: update, update_aad forbidden */
  214. TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, output )
  215. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  216. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  217. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  218. /* Starts -> update* OK */
  219. TEST_ASSERT( mbedtls_chachapoly_starts( &ctx, nonce, MBEDTLS_CHACHAPOLY_ENCRYPT )
  220. == 0 );
  221. TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, output )
  222. == 0 );
  223. TEST_ASSERT( mbedtls_chachapoly_update( &ctx, input_len, input, output )
  224. == 0 );
  225. /* After update: update_aad forbidden */
  226. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  227. == MBEDTLS_ERR_CHACHAPOLY_BAD_STATE );
  228. /* Starts -> update_aad* -> finish OK */
  229. TEST_ASSERT( mbedtls_chachapoly_starts( &ctx, nonce, MBEDTLS_CHACHAPOLY_ENCRYPT )
  230. == 0 );
  231. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  232. == 0 );
  233. TEST_ASSERT( mbedtls_chachapoly_update_aad( &ctx, aad, aad_len )
  234. == 0 );
  235. TEST_ASSERT( mbedtls_chachapoly_finish( &ctx, mac )
  236. == 0 );
  237. exit:
  238. mbedtls_chachapoly_free( &ctx );
  239. }
  240. /* END_CASE */
  241. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  242. void chachapoly_selftest()
  243. {
  244. TEST_ASSERT( mbedtls_chachapoly_self_test( 1 ) == 0 );
  245. }
  246. /* END_CASE */