blob: 17d79c579efa974f8b689383ab7832501d41b547 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/gcm.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker89e80c92012-03-20 13:50:09 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_GCM_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker89e80c92012-03-20 13:50:09 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Ron Eldor5a21fd62016-12-16 16:15:56 +020011void 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 ) );
Darryl Green11999bb2018-03-13 15:22:58 +000038
Ron Eldor5a21fd62016-12-16 16:15:56 +020039 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
48exit:
49 mbedtls_gcm_free( &ctx );
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010054void gcm_encrypt_and_tag( int cipher_id, uint8_t * key_str, uint32_t key_len,
55 uint8_t * src_str, uint32_t pt_len,
56 uint8_t * iv_str, uint32_t iv_len,
57 uint8_t * add_str, uint32_t add_len,
58 uint8_t * hex_dst_string,
59 uint32_t hex_dst_string_len, int tag_len_bits,
60 uint8_t * hex_tag_string,
61 uint32_t hex_tag_string_len, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000062{
Paul Bakker89e80c92012-03-20 13:50:09 +000063 unsigned char output[128];
64 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +010066 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +000067
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020068 mbedtls_gcm_init( &ctx );
69
Paul Bakker89e80c92012-03-20 13:50:09 +000070 memset(output, 0x00, 128);
71 memset(tag_output, 0x00, 16);
72
Paul Bakker89e80c92012-03-20 13:50:09 +000073
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +020074 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020075 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +000076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 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 Bakker89e80c92012-03-20 13:50:09 +000078
Azim Khanf1aaec92017-05-30 14:23:15 +010079 TEST_ASSERT( hexcmp( output, hex_dst_string, pt_len, hex_dst_string_len ) == 0 );
80 TEST_ASSERT( hexcmp( tag_output, hex_tag_string, tag_len, hex_tag_string_len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +000081 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +020082
Paul Bakkerbd51b262014-07-10 15:26:12 +020083exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +000085}
Paul Bakker33b43f12013-08-20 11:48:36 +020086/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +000087
Paul Bakker33b43f12013-08-20 11:48:36 +020088/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010089void gcm_decrypt_and_verify( int cipher_id, uint8_t * key_str,
90 uint32_t key_len, uint8_t * src_str,
91 uint32_t pt_len, uint8_t * iv_str,
92 uint32_t iv_len, uint8_t * add_str,
93 uint32_t add_len, int tag_len_bits,
94 uint8_t * tag_str, uint32_t tag_str_len,
Azim Khan46c9b1f2017-05-31 20:46:35 +010095 char * result, uint8_t * pt_result,
96 uint32_t pt_result_len, int init_result )
Paul Bakker89e80c92012-03-20 13:50:09 +000097{
Paul Bakker89e80c92012-03-20 13:50:09 +000098 unsigned char output[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +0000100 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +0100101 size_t tag_len = tag_len_bits / 8;
Paul Bakker89e80c92012-03-20 13:50:09 +0000102
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200103 mbedtls_gcm_init( &ctx );
104
Paul Bakker89e80c92012-03-20 13:50:09 +0000105 memset(output, 0x00, 128);
106
Paul Bakker89e80c92012-03-20 13:50:09 +0000107
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200108 TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200109 if( init_result == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output );
Paul Bakker89e80c92012-03-20 13:50:09 +0000112
Azim Khan46c9b1f2017-05-31 20:46:35 +0100113 if( strcmp( "FAIL", result ) == 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
Paul Bakker89e80c92012-03-20 13:50:09 +0000116 }
117 else
118 {
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200119 TEST_ASSERT( ret == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000120
Azim Khanf1aaec92017-05-30 14:23:15 +0100121 TEST_ASSERT( hexcmp( output, pt_result, pt_len, pt_result_len ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000122 }
123 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200124
Paul Bakkerbd51b262014-07-10 15:26:12 +0200125exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_gcm_free( &ctx );
Paul Bakker89e80c92012-03-20 13:50:09 +0000127}
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100131void gcm_selftest( )
Paul Bakker89e80c92012-03-20 13:50:09 +0000132{
Andres AG93012e82016-09-09 09:10:28 +0100133 TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
Paul Bakker89e80c92012-03-20 13:50:09 +0000134}
Paul Bakker33b43f12013-08-20 11:48:36 +0200135/* END_CASE */