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" |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 3 | |
| 4 | /* Use the multipart interface to process the encrypted data in two parts |
| 5 | * and check that the output matches the expected output. |
| 6 | * The context must have been set up with the key. */ |
| 7 | static int check_multipart( mbedtls_gcm_context *ctx, |
| 8 | int mode, |
| 9 | const data_t *iv, |
| 10 | const data_t *add, |
| 11 | const data_t *input, |
| 12 | const data_t *expected_output, |
| 13 | const data_t *tag, |
| 14 | size_t n1 ) |
| 15 | { |
| 16 | int ok = 0; |
| 17 | uint8_t *output = NULL; |
| 18 | size_t n2 = input->len - n1; |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 19 | size_t olen; |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 20 | |
| 21 | /* Sanity checks on the test data */ |
| 22 | TEST_ASSERT( n1 <= input->len ); |
| 23 | TEST_EQUAL( input->len, expected_output->len ); |
| 24 | |
| 25 | TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode, |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 26 | iv->x, iv->len ) ); |
| 27 | TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x, add->len ) ); |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 28 | |
| 29 | /* Allocate a tight buffer for each update call. This way, if the function |
| 30 | * tries to write beyond the advertised required buffer size, this will |
| 31 | * count as an overflow for memory sanitizers and static checkers. */ |
| 32 | ASSERT_ALLOC( output, n1 ); |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 33 | olen = 0xdeadbeef; |
| 34 | TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x, n1, output, n1, &olen ) ); |
| 35 | TEST_EQUAL( n1, olen ); |
| 36 | ASSERT_COMPARE( output, olen, expected_output->x, n1 ); |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 37 | mbedtls_free( output ); |
| 38 | output = NULL; |
| 39 | |
| 40 | ASSERT_ALLOC( output, n2 ); |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 41 | olen = 0xdeadbeef; |
| 42 | TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x + n1, n2, output, n2, &olen ) ); |
| 43 | TEST_EQUAL( n2, olen ); |
| 44 | ASSERT_COMPARE( output, olen, expected_output->x + n1, n2 ); |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 45 | mbedtls_free( output ); |
| 46 | output = NULL; |
| 47 | |
| 48 | ASSERT_ALLOC( output, tag->len ); |
Gilles Peskine | 9461e45 | 2021-04-15 16:48:32 +0200 | [diff] [blame] | 49 | TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, output, tag->len ) ); |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 50 | ASSERT_COMPARE( output, tag->len, tag->x, tag->len ); |
| 51 | mbedtls_free( output ); |
| 52 | output = NULL; |
| 53 | |
| 54 | ok = 1; |
| 55 | exit: |
| 56 | mbedtls_free( output ); |
| 57 | return( ok ); |
| 58 | } |
| 59 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 60 | /* END_HEADER */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 61 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 62 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | * depends_on:MBEDTLS_GCM_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 64 | * END_DEPENDENCIES |
| 65 | */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 66 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 67 | /* BEGIN_CASE */ |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 68 | void gcm_bad_parameters( int cipher_id, int direction, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 69 | data_t *key_str, data_t *src_str, |
| 70 | data_t *iv_str, data_t *add_str, |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 71 | int tag_len_bits, int gcm_result ) |
| 72 | { |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 73 | unsigned char output[128]; |
| 74 | unsigned char tag_output[16]; |
| 75 | mbedtls_gcm_context ctx; |
Azim Khan | 317efe8 | 2017-08-02 17:33:54 +0100 | [diff] [blame] | 76 | size_t tag_len = tag_len_bits / 8; |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 77 | |
| 78 | mbedtls_gcm_init( &ctx ); |
| 79 | |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 80 | memset( output, 0x00, sizeof( output ) ); |
| 81 | memset( tag_output, 0x00, sizeof( tag_output ) ); |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 82 | |
Azim Khan | 317efe8 | 2017-08-02 17:33:54 +0100 | [diff] [blame] | 83 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 ); |
| 84 | TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, src_str->len, iv_str->x, iv_str->len, |
| 85 | add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == gcm_result ); |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 86 | |
| 87 | exit: |
| 88 | mbedtls_gcm_free( &ctx ); |
| 89 | } |
| 90 | /* END_CASE */ |
| 91 | |
| 92 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 93 | void gcm_encrypt_and_tag( int cipher_id, data_t * key_str, |
| 94 | data_t * src_str, data_t * iv_str, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 95 | data_t * add_str, data_t * dst, |
| 96 | int tag_len_bits, data_t * tag, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 97 | int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 98 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 99 | unsigned char output[128]; |
| 100 | unsigned char tag_output[16]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | mbedtls_gcm_context ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 102 | size_t tag_len = tag_len_bits / 8; |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 103 | size_t n1; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 105 | mbedtls_gcm_init( &ctx ); |
| 106 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 107 | memset(output, 0x00, 128); |
| 108 | memset(tag_output, 0x00, 16); |
| 109 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 110 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 111 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 112 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 113 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 114 | TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 115 | |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 116 | ASSERT_COMPARE( output, src_str->len, dst->x, dst->len ); |
| 117 | ASSERT_COMPARE( tag_output, tag_len, tag->x, tag->len ); |
| 118 | |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 119 | for( n1 = 0; n1 <= src_str->len; n1 += 1 ) |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 120 | { |
| 121 | mbedtls_test_set_step( n1 ); |
| 122 | if( !check_multipart( &ctx, MBEDTLS_GCM_ENCRYPT, |
| 123 | iv_str, add_str, src_str, |
| 124 | dst, tag, |
| 125 | n1 ) ) |
| 126 | goto exit; |
| 127 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 128 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 129 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 130 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 132 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 133 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 134 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 135 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 136 | void gcm_decrypt_and_verify( int cipher_id, data_t * key_str, |
| 137 | data_t * src_str, data_t * iv_str, |
| 138 | data_t * add_str, int tag_len_bits, |
| 139 | data_t * tag_str, char * result, |
| 140 | data_t * pt_result, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 141 | { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 142 | unsigned char output[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 144 | int ret; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 145 | size_t tag_len = tag_len_bits / 8; |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 146 | size_t n1; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 148 | mbedtls_gcm_init( &ctx ); |
| 149 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 150 | memset(output, 0x00, 128); |
| 151 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 152 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 153 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 154 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 155 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 156 | ret = mbedtls_gcm_auth_decrypt( &ctx, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, tag_str->x, tag_len, src_str->x, output ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 157 | |
Azim Khan | 46c9b1f | 2017-05-31 20:46:35 +0100 | [diff] [blame] | 158 | if( strcmp( "FAIL", result ) == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 159 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 161 | } |
| 162 | else |
| 163 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 164 | TEST_ASSERT( ret == 0 ); |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 165 | ASSERT_COMPARE( output, src_str->len, pt_result->x, pt_result->len ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 166 | |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 167 | for( n1 = 0; n1 <= src_str->len; n1 += 1 ) |
Gilles Peskine | 36dd93e | 2021-04-13 13:02:03 +0200 | [diff] [blame] | 168 | { |
| 169 | mbedtls_test_set_step( n1 ); |
| 170 | if( !check_multipart( &ctx, MBEDTLS_GCM_DECRYPT, |
| 171 | iv_str, add_str, src_str, |
| 172 | pt_result, tag_str, |
| 173 | n1 ) ) |
| 174 | goto exit; |
| 175 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 178 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 179 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | mbedtls_gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 181 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 182 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 183 | |
TRodziewicz | 062f353 | 2021-05-25 15:15:57 +0200 | [diff] [blame^] | 184 | /* BEGIN_CASE depends_on:NOT_DEFINED */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 185 | void gcm_invalid_param( ) |
| 186 | { |
| 187 | mbedtls_gcm_context ctx; |
| 188 | unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
| 189 | mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES; |
Ronald Cron | 875b5fb | 2021-05-21 08:50:00 +0200 | [diff] [blame] | 190 | int invalid_bitlen = 1; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 191 | |
| 192 | mbedtls_gcm_init( &ctx ); |
| 193 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 194 | /* mbedtls_gcm_setkey */ |
Ronald Cron | 875b5fb | 2021-05-21 08:50:00 +0200 | [diff] [blame] | 195 | TEST_EQUAL( |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 196 | MBEDTLS_ERR_GCM_BAD_INPUT, |
| 197 | mbedtls_gcm_setkey( &ctx, valid_cipher, valid_buffer, invalid_bitlen ) ); |
| 198 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 199 | exit: |
| 200 | mbedtls_gcm_free( &ctx ); |
| 201 | } |
| 202 | /* END_CASE */ |
| 203 | |
| 204 | /* BEGIN_CASE */ |
| 205 | void gcm_valid_param( ) |
| 206 | { |
| 207 | TEST_VALID_PARAM( mbedtls_gcm_free( NULL ) ); |
| 208 | exit: |
| 209 | return; |
| 210 | } |
| 211 | /* END_CASE */ |
| 212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 214 | void gcm_selftest( ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 215 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 216 | TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 217 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 218 | /* END_CASE */ |