Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 2 | #include <polarssl/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 |
| 6 | * depends_on:POLARSSL_GCM_C |
| 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 */ |
| 11 | void gcm_encrypt_and_tag( char *hex_key_string, char *hex_src_string, |
| 12 | char *hex_iv_string, char *hex_add_string, |
| 13 | char *hex_dst_string, int tag_len_bits, |
| 14 | char *hex_tag_string, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 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 | gcm_context ctx; |
| 25 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 26 | 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] | 27 | |
| 28 | memset(key_str, 0x00, 128); |
| 29 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 30 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 31 | memset(iv_str, 0x00, 128); |
| 32 | memset(add_str, 0x00, 128); |
| 33 | memset(tag_str, 0x00, 128); |
| 34 | memset(output, 0x00, 128); |
| 35 | memset(tag_output, 0x00, 16); |
| 36 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 37 | key_len = unhexify( key_str, hex_key_string ); |
| 38 | pt_len = unhexify( src_str, hex_src_string ); |
| 39 | iv_len = unhexify( iv_str, hex_iv_string ); |
| 40 | add_len = unhexify( add_str, hex_add_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 41 | |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 42 | TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 43 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 44 | { |
| 45 | TEST_ASSERT( gcm_crypt_and_tag( &ctx, GCM_ENCRYPT, pt_len, iv_str, iv_len, add_str, add_len, src_str, output, tag_len, tag_output ) == 0 ); |
| 46 | hexify( dst_str, output, pt_len ); |
| 47 | hexify( tag_str, tag_output, tag_len ); |
| 48 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 49 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 50 | TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 51 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 52 | |
| 53 | gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 54 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 55 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 56 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 57 | /* BEGIN_CASE */ |
| 58 | void gcm_decrypt_and_verify( char *hex_key_string, char *hex_src_string, |
| 59 | char *hex_iv_string, char *hex_add_string, |
| 60 | int tag_len_bits, char *hex_tag_string, |
| 61 | char *pt_result, int init_result ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 62 | { |
| 63 | unsigned char key_str[128]; |
| 64 | unsigned char src_str[128]; |
| 65 | unsigned char dst_str[257]; |
| 66 | unsigned char iv_str[128]; |
| 67 | unsigned char add_str[128]; |
| 68 | unsigned char tag_str[128]; |
| 69 | unsigned char output[128]; |
| 70 | gcm_context ctx; |
| 71 | unsigned int key_len; |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 72 | 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] | 73 | int ret; |
| 74 | |
| 75 | memset(key_str, 0x00, 128); |
| 76 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 77 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 78 | memset(iv_str, 0x00, 128); |
| 79 | memset(add_str, 0x00, 128); |
| 80 | memset(tag_str, 0x00, 128); |
| 81 | memset(output, 0x00, 128); |
| 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 ); |
| 87 | unhexify( tag_str, hex_tag_string ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 88 | |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 89 | TEST_ASSERT( gcm_init( &ctx, POLARSSL_CIPHER_ID_AES, key_str, key_len * 8 ) == init_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 90 | if( init_result == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 91 | { |
| 92 | ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output ); |
| 93 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 94 | if( strcmp( "FAIL", pt_result ) == 0 ) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 95 | { |
| 96 | TEST_ASSERT( ret == POLARSSL_ERR_GCM_AUTH_FAILED ); |
| 97 | } |
| 98 | else |
| 99 | { |
Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 100 | TEST_ASSERT( ret == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 101 | hexify( dst_str, output, pt_len ); |
| 102 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 103 | TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 106 | |
| 107 | gcm_free( &ctx ); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 108 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 109 | /* END_CASE */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 387a211 | 2013-09-18 18:54:01 +0200 | [diff] [blame] | 111 | /* BEGIN_CASE depends_on:POLARSSL_SELFTEST_C */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 112 | void gcm_selftest() |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 113 | { |
| 114 | TEST_ASSERT( gcm_self_test( 0 ) == 0 ); |
| 115 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 116 | /* END_CASE */ |