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 */ |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 11 | void gcm_bad_parameters( int cipher_id, int direction, |
| 12 | char *hex_key_string, char *hex_src_string, |
| 13 | char *hex_iv_string, char *hex_add_string, |
| 14 | int tag_len_bits, int gcm_result ) |
| 15 | { |
| 16 | unsigned char key_str[128]; |
| 17 | unsigned char src_str[128]; |
| 18 | unsigned char dst_str[257]; |
| 19 | unsigned char iv_str[128]; |
| 20 | unsigned char add_str[128]; |
| 21 | unsigned char tag_str[128]; |
| 22 | unsigned char output[128]; |
| 23 | unsigned char tag_output[16]; |
| 24 | mbedtls_gcm_context ctx; |
| 25 | unsigned int key_len; |
| 26 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
| 27 | |
| 28 | mbedtls_gcm_init( &ctx ); |
| 29 | |
| 30 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 31 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 32 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
| 33 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
| 34 | memset( add_str, 0x00, sizeof( add_str ) ); |
| 35 | memset( tag_str, 0x00, sizeof( tag_str ) ); |
| 36 | memset( output, 0x00, sizeof( output ) ); |
| 37 | memset( tag_output, 0x00, sizeof( tag_output ) ); |
| 38 | |
| 39 | key_len = unhexify( key_str, hex_key_string ); |
| 40 | pt_len = unhexify( src_str, hex_src_string ); |
| 41 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 42 | add_len = unhexify( add_str, hex_add_string ); |
| 43 | |
| 44 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == 0 ); |
| 45 | TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, pt_len, iv_str, iv_len, |
| 46 | add_str, add_len, src_str, output, tag_len, tag_output ) == gcm_result ); |
| 47 | |
| 48 | exit: |
| 49 | mbedtls_gcm_free( &ctx ); |
| 50 | } |
| 51 | /* END_CASE */ |
| 52 | |
| 53 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 083d668 | 2013-10-24 12:06:54 +0200 | [diff] [blame] | 54 | void gcm_encrypt_and_tag( int cipher_id, |
| 55 | char *hex_key_string, char *hex_src_string, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 56 | char *hex_iv_string, char *hex_add_string, |
| 57 | char *hex_dst_string, int tag_len_bits, |
| 58 | char *hex_tag_string, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned char key_str[128]; |
| 61 | unsigned char src_str[128]; |
| 62 | unsigned char dst_str[257]; |
| 63 | unsigned char iv_str[128]; |
| 64 | unsigned char add_str[128]; |
| 65 | unsigned char tag_str[128]; |
| 66 | unsigned char output[128]; |
| 67 | unsigned char tag_output[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 69 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 70 | 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] | 71 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 72 | mbedtls_gcm_init( &ctx ); |
| 73 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 74 | memset(key_str, 0x00, 128); |
| 75 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 76 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 77 | memset(iv_str, 0x00, 128); |
| 78 | memset(add_str, 0x00, 128); |
| 79 | memset(tag_str, 0x00, 128); |
| 80 | memset(output, 0x00, 128); |
| 81 | memset(tag_output, 0x00, 16); |
| 82 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 83 | key_len = unhexify( key_str, hex_key_string ); |
| 84 | pt_len = unhexify( src_str, hex_src_string ); |
| 85 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 86 | add_len = unhexify( add_str, hex_add_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 88 | 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] | 89 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 90 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | 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] | 92 | hexify( dst_str, output, pt_len ); |
| 93 | hexify( tag_str, tag_output, tag_len ); |
| 94 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 95 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 96 | TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 97 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 98 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 99 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 101 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 102 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 103 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 104 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 083d668 | 2013-10-24 12:06:54 +0200 | [diff] [blame] | 105 | void gcm_decrypt_and_verify( int cipher_id, |
| 106 | char *hex_key_string, char *hex_src_string, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 107 | char *hex_iv_string, char *hex_add_string, |
| 108 | int tag_len_bits, char *hex_tag_string, |
| 109 | char *pt_result, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 110 | { |
| 111 | unsigned char key_str[128]; |
| 112 | unsigned char src_str[128]; |
| 113 | unsigned char dst_str[257]; |
| 114 | unsigned char iv_str[128]; |
| 115 | unsigned char add_str[128]; |
| 116 | unsigned char tag_str[128]; |
| 117 | unsigned char output[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 119 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 120 | 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] | 121 | int ret; |
| 122 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 123 | mbedtls_gcm_init( &ctx ); |
| 124 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 125 | memset(key_str, 0x00, 128); |
| 126 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 127 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 128 | memset(iv_str, 0x00, 128); |
| 129 | memset(add_str, 0x00, 128); |
| 130 | memset(tag_str, 0x00, 128); |
| 131 | memset(output, 0x00, 128); |
| 132 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 133 | key_len = unhexify( key_str, hex_key_string ); |
| 134 | pt_len = unhexify( src_str, hex_src_string ); |
| 135 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 136 | add_len = unhexify( add_str, hex_add_string ); |
| 137 | unhexify( tag_str, hex_tag_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 138 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 139 | 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] | 140 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 141 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | 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] | 143 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 144 | if( strcmp( "FAIL", pt_result ) == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 145 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 150 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 151 | hexify( dst_str, output, pt_len ); |
| 152 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 153 | TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 156 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 157 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 159 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 160 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 163 | void gcm_selftest() |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 164 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 165 | TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 166 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 167 | /* END_CASE */ |