test_suite_gcm.function 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/gcm.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_GCM_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void gcm_encrypt_and_tag( int cipher_id,
  10. char *hex_key_string, char *hex_src_string,
  11. char *hex_iv_string, char *hex_add_string,
  12. char *hex_dst_string, int tag_len_bits,
  13. char *hex_tag_string, int init_result )
  14. {
  15. unsigned char key_str[128];
  16. unsigned char src_str[128];
  17. unsigned char dst_str[257];
  18. unsigned char iv_str[128];
  19. unsigned char add_str[128];
  20. unsigned char tag_str[128];
  21. unsigned char output[128];
  22. unsigned char tag_output[16];
  23. mbedtls_gcm_context ctx;
  24. unsigned int key_len;
  25. size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
  26. mbedtls_gcm_init( &ctx );
  27. memset(key_str, 0x00, 128);
  28. memset(src_str, 0x00, 128);
  29. memset(dst_str, 0x00, 257);
  30. memset(iv_str, 0x00, 128);
  31. memset(add_str, 0x00, 128);
  32. memset(tag_str, 0x00, 128);
  33. memset(output, 0x00, 128);
  34. memset(tag_output, 0x00, 16);
  35. key_len = unhexify( key_str, hex_key_string );
  36. pt_len = unhexify( src_str, hex_src_string );
  37. iv_len = unhexify( iv_str, hex_iv_string );
  38. add_len = unhexify( add_str, hex_add_string );
  39. TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
  40. if( init_result == 0 )
  41. {
  42. TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 );
  43. hexify( dst_str, output, pt_len );
  44. hexify( tag_str, tag_output, tag_len );
  45. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  46. TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 );
  47. }
  48. exit:
  49. mbedtls_gcm_free( &ctx );
  50. }
  51. /* END_CASE */
  52. /* BEGIN_CASE */
  53. void gcm_decrypt_and_verify( int cipher_id,
  54. char *hex_key_string, char *hex_src_string,
  55. char *hex_iv_string, char *hex_add_string,
  56. int tag_len_bits, char *hex_tag_string,
  57. char *pt_result, int init_result )
  58. {
  59. unsigned char key_str[128];
  60. unsigned char src_str[128];
  61. unsigned char dst_str[257];
  62. unsigned char iv_str[128];
  63. unsigned char add_str[128];
  64. unsigned char tag_str[128];
  65. unsigned char output[128];
  66. mbedtls_gcm_context ctx;
  67. unsigned int key_len;
  68. size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8;
  69. int ret;
  70. mbedtls_gcm_init( &ctx );
  71. memset(key_str, 0x00, 128);
  72. memset(src_str, 0x00, 128);
  73. memset(dst_str, 0x00, 257);
  74. memset(iv_str, 0x00, 128);
  75. memset(add_str, 0x00, 128);
  76. memset(tag_str, 0x00, 128);
  77. memset(output, 0x00, 128);
  78. key_len = unhexify( key_str, hex_key_string );
  79. pt_len = unhexify( src_str, hex_src_string );
  80. iv_len = unhexify( iv_str, hex_iv_string );
  81. add_len = unhexify( add_str, hex_add_string );
  82. unhexify( tag_str, hex_tag_string );
  83. TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
  84. if( init_result == 0 )
  85. {
  86. ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
  87. if( strcmp( "FAIL", pt_result ) == 0 )
  88. {
  89. TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
  90. }
  91. else
  92. {
  93. TEST_ASSERT( ret == 0 );
  94. hexify( dst_str, output, pt_len );
  95. TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 );
  96. }
  97. }
  98. exit:
  99. mbedtls_gcm_free( &ctx );
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  103. void gcm_selftest()
  104. {
  105. TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
  106. }
  107. /* END_CASE */