Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/gcm.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 4 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6 | * depends_on:MBEDTLS_GCM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 9 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 083d668 | 2013-10-24 12:06:54 +0200 | [diff] [blame] | 11 | void gcm_encrypt_and_tag( int cipher_id, |
| 12 | char *hex_key_string, char *hex_src_string, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 13 | char *hex_iv_string, char *hex_add_string, |
| 14 | char *hex_dst_string, int tag_len_bits, |
| 15 | char *hex_tag_string, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 16 | { |
| 17 | unsigned char key_str[128]; |
| 18 | unsigned char src_str[128]; |
| 19 | unsigned char dst_str[257]; |
| 20 | unsigned char iv_str[128]; |
| 21 | unsigned char add_str[128]; |
| 22 | unsigned char tag_str[128]; |
| 23 | unsigned char output[128]; |
| 24 | unsigned char tag_output[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 26 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 27 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame^] | 29 | mbedtls_gcm_init( &ctx ); |
| 30 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 31 | memset(key_str, 0x00, 128); |
| 32 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 33 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 34 | memset(iv_str, 0x00, 128); |
| 35 | memset(add_str, 0x00, 128); |
| 36 | memset(tag_str, 0x00, 128); |
| 37 | memset(output, 0x00, 128); |
| 38 | memset(tag_output, 0x00, 16); |
| 39 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 40 | key_len = unhexify( key_str, hex_key_string ); |
| 41 | pt_len = unhexify( src_str, hex_src_string ); |
| 42 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 43 | add_len = unhexify( add_str, hex_add_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame^] | 45 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 46 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 47 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | 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 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 49 | hexify( dst_str, output, pt_len ); |
| 50 | hexify( tag_str, tag_output, tag_len ); |
| 51 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 52 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 53 | TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 54 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 55 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 56 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 58 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 59 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 60 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 61 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 083d668 | 2013-10-24 12:06:54 +0200 | [diff] [blame] | 62 | void gcm_decrypt_and_verify( int cipher_id, |
| 63 | char *hex_key_string, char *hex_src_string, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 64 | char *hex_iv_string, char *hex_add_string, |
| 65 | int tag_len_bits, char *hex_tag_string, |
| 66 | char *pt_result, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 67 | { |
| 68 | unsigned char key_str[128]; |
| 69 | unsigned char src_str[128]; |
| 70 | unsigned char dst_str[257]; |
| 71 | unsigned char iv_str[128]; |
| 72 | unsigned char add_str[128]; |
| 73 | unsigned char tag_str[128]; |
| 74 | unsigned char output[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 76 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 77 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 78 | int ret; |
| 79 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame^] | 80 | mbedtls_gcm_init( &ctx ); |
| 81 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 82 | memset(key_str, 0x00, 128); |
| 83 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 84 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 85 | memset(iv_str, 0x00, 128); |
| 86 | memset(add_str, 0x00, 128); |
| 87 | memset(tag_str, 0x00, 128); |
| 88 | memset(output, 0x00, 128); |
| 89 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 90 | key_len = unhexify( key_str, hex_key_string ); |
| 91 | pt_len = unhexify( src_str, hex_src_string ); |
| 92 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 93 | add_len = unhexify( add_str, hex_add_string ); |
| 94 | unhexify( tag_str, hex_tag_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame^] | 96 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 97 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 98 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 100 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 101 | if( strcmp( "FAIL", pt_result ) == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 107 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 108 | hexify( dst_str, output, pt_len ); |
| 109 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 110 | TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 113 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 114 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 116 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 117 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 120 | void gcm_selftest() |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | TEST_ASSERT( mbedtls_gcm_self_test( 0 ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 123 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 124 | /* END_CASE */ |