Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/gcm.h> |
| 3 | END_HEADER |
| 4 | |
| 5 | BEGIN_DEPENDENCIES |
| 6 | depends_on:POLARSSL_GCM_C |
| 7 | END_DEPENDENCIES |
| 8 | |
| 9 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 10 | gcm_encrypt_and_tag:hex_key_string:hex_src_string:hex_iv_string:hex_add_string:hex_dst_string:#tag_len_bits:hex_tag_string:#init_result |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 11 | { |
| 12 | unsigned char key_str[128]; |
| 13 | unsigned char src_str[128]; |
| 14 | unsigned char dst_str[257]; |
| 15 | unsigned char iv_str[128]; |
| 16 | unsigned char add_str[128]; |
| 17 | unsigned char tag_str[128]; |
| 18 | unsigned char output[128]; |
| 19 | unsigned char tag_output[16]; |
| 20 | gcm_context ctx; |
| 21 | unsigned int key_len; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 22 | 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] | 23 | |
| 24 | memset(key_str, 0x00, 128); |
| 25 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 26 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 27 | memset(iv_str, 0x00, 128); |
| 28 | memset(add_str, 0x00, 128); |
| 29 | memset(tag_str, 0x00, 128); |
| 30 | memset(output, 0x00, 128); |
| 31 | memset(tag_output, 0x00, 16); |
| 32 | |
| 33 | key_len = unhexify( key_str, {hex_key_string} ); |
| 34 | pt_len = unhexify( src_str, {hex_src_string} ); |
| 35 | iv_len = unhexify( iv_str, {hex_iv_string} ); |
| 36 | add_len = unhexify( add_str, {hex_add_string} ); |
| 37 | |
| 38 | TEST_ASSERT( gcm_init( &ctx, key_str, key_len * 8 ) == {init_result} ); |
| 39 | if( {init_result} == 0 ) |
| 40 | { |
| 41 | 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 ); |
| 42 | hexify( dst_str, output, pt_len ); |
| 43 | hexify( tag_str, tag_output, tag_len ); |
| 44 | |
| 45 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 46 | TEST_ASSERT( strcmp( (char *) tag_str, {hex_tag_string} ) == 0 ); |
| 47 | } |
| 48 | } |
| 49 | END_CASE |
| 50 | |
| 51 | BEGIN_CASE |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 52 | gcm_decrypt_and_verify:hex_key_string:hex_src_string:hex_iv_string:hex_add_string:#tag_len_bits:hex_tag_string:pt_result:#init_result |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 53 | { |
| 54 | unsigned char key_str[128]; |
| 55 | unsigned char src_str[128]; |
| 56 | unsigned char dst_str[257]; |
| 57 | unsigned char iv_str[128]; |
| 58 | unsigned char add_str[128]; |
| 59 | unsigned char tag_str[128]; |
| 60 | unsigned char output[128]; |
| 61 | gcm_context ctx; |
| 62 | unsigned int key_len; |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 63 | 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] | 64 | int ret; |
| 65 | |
| 66 | memset(key_str, 0x00, 128); |
| 67 | memset(src_str, 0x00, 128); |
Paul Bakker | 68b6d88 | 2012-09-08 14:04:13 +0000 | [diff] [blame] | 68 | memset(dst_str, 0x00, 257); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 69 | memset(iv_str, 0x00, 128); |
| 70 | memset(add_str, 0x00, 128); |
| 71 | memset(tag_str, 0x00, 128); |
| 72 | memset(output, 0x00, 128); |
| 73 | |
| 74 | key_len = unhexify( key_str, {hex_key_string} ); |
| 75 | pt_len = unhexify( src_str, {hex_src_string} ); |
| 76 | iv_len = unhexify( iv_str, {hex_iv_string} ); |
| 77 | add_len = unhexify( add_str, {hex_add_string} ); |
| 78 | unhexify( tag_str, {hex_tag_string} ); |
| 79 | |
| 80 | TEST_ASSERT( gcm_init( &ctx, key_str, key_len * 8 ) == {init_result} ); |
| 81 | if( {init_result} == 0 ) |
| 82 | { |
| 83 | ret = gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output ); |
| 84 | |
| 85 | if( strcmp( "FAIL", {pt_result} ) == 0 ) |
| 86 | { |
| 87 | TEST_ASSERT( ret == POLARSSL_ERR_GCM_AUTH_FAILED ); |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | hexify( dst_str, output, pt_len ); |
| 92 | |
| 93 | TEST_ASSERT( strcmp( (char *) dst_str, {pt_result} ) == 0 ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | END_CASE |
| 98 | |
| 99 | BEGIN_CASE |
| 100 | gcm_selftest: |
| 101 | { |
| 102 | TEST_ASSERT( gcm_self_test( 0 ) == 0 ); |
| 103 | } |
| 104 | END_CASE |