Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedtls by
tests/suites/test_suite_gcm.function@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
- Child:
- 2:bbdeda018a3c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/gcm.h" |
markrad | 0:cdf462088d13 | 3 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 6 | * depends_on:MBEDTLS_GCM_C |
markrad | 0:cdf462088d13 | 7 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 8 | */ |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 11 | void gcm_encrypt_and_tag( int cipher_id, |
markrad | 0:cdf462088d13 | 12 | char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 13 | char *hex_iv_string, char *hex_add_string, |
markrad | 0:cdf462088d13 | 14 | char *hex_dst_string, int tag_len_bits, |
markrad | 0:cdf462088d13 | 15 | char *hex_tag_string, int init_result ) |
markrad | 0:cdf462088d13 | 16 | { |
markrad | 0:cdf462088d13 | 17 | unsigned char key_str[128]; |
markrad | 0:cdf462088d13 | 18 | unsigned char src_str[128]; |
markrad | 0:cdf462088d13 | 19 | unsigned char dst_str[257]; |
markrad | 0:cdf462088d13 | 20 | unsigned char iv_str[128]; |
markrad | 0:cdf462088d13 | 21 | unsigned char add_str[128]; |
markrad | 0:cdf462088d13 | 22 | unsigned char tag_str[128]; |
markrad | 0:cdf462088d13 | 23 | unsigned char output[128]; |
markrad | 0:cdf462088d13 | 24 | unsigned char tag_output[16]; |
markrad | 0:cdf462088d13 | 25 | mbedtls_gcm_context ctx; |
markrad | 0:cdf462088d13 | 26 | unsigned int key_len; |
markrad | 0:cdf462088d13 | 27 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
markrad | 0:cdf462088d13 | 28 | |
markrad | 0:cdf462088d13 | 29 | mbedtls_gcm_init( &ctx ); |
markrad | 0:cdf462088d13 | 30 | |
markrad | 0:cdf462088d13 | 31 | memset(key_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 32 | memset(src_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 33 | memset(dst_str, 0x00, 257); |
markrad | 0:cdf462088d13 | 34 | memset(iv_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 35 | memset(add_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 36 | memset(tag_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 37 | memset(output, 0x00, 128); |
markrad | 0:cdf462088d13 | 38 | memset(tag_output, 0x00, 16); |
markrad | 0:cdf462088d13 | 39 | |
markrad | 0:cdf462088d13 | 40 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 41 | pt_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 42 | iv_len = unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 43 | add_len = unhexify( add_str, hex_add_string ); |
markrad | 0:cdf462088d13 | 44 | |
markrad | 0:cdf462088d13 | 45 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
markrad | 0:cdf462088d13 | 46 | if( init_result == 0 ) |
markrad | 0:cdf462088d13 | 47 | { |
markrad | 0:cdf462088d13 | 48 | 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 ); |
markrad | 0:cdf462088d13 | 49 | hexify( dst_str, output, pt_len ); |
markrad | 0:cdf462088d13 | 50 | hexify( tag_str, tag_output, tag_len ); |
markrad | 0:cdf462088d13 | 51 | |
markrad | 0:cdf462088d13 | 52 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 53 | TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 ); |
markrad | 0:cdf462088d13 | 54 | } |
markrad | 0:cdf462088d13 | 55 | |
markrad | 0:cdf462088d13 | 56 | exit: |
markrad | 0:cdf462088d13 | 57 | mbedtls_gcm_free( &ctx ); |
markrad | 0:cdf462088d13 | 58 | } |
markrad | 0:cdf462088d13 | 59 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 62 | void gcm_decrypt_and_verify( int cipher_id, |
markrad | 0:cdf462088d13 | 63 | char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 64 | char *hex_iv_string, char *hex_add_string, |
markrad | 0:cdf462088d13 | 65 | int tag_len_bits, char *hex_tag_string, |
markrad | 0:cdf462088d13 | 66 | char *pt_result, int init_result ) |
markrad | 0:cdf462088d13 | 67 | { |
markrad | 0:cdf462088d13 | 68 | unsigned char key_str[128]; |
markrad | 0:cdf462088d13 | 69 | unsigned char src_str[128]; |
markrad | 0:cdf462088d13 | 70 | unsigned char dst_str[257]; |
markrad | 0:cdf462088d13 | 71 | unsigned char iv_str[128]; |
markrad | 0:cdf462088d13 | 72 | unsigned char add_str[128]; |
markrad | 0:cdf462088d13 | 73 | unsigned char tag_str[128]; |
markrad | 0:cdf462088d13 | 74 | unsigned char output[128]; |
markrad | 0:cdf462088d13 | 75 | mbedtls_gcm_context ctx; |
markrad | 0:cdf462088d13 | 76 | unsigned int key_len; |
markrad | 0:cdf462088d13 | 77 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
markrad | 0:cdf462088d13 | 78 | int ret; |
markrad | 0:cdf462088d13 | 79 | |
markrad | 0:cdf462088d13 | 80 | mbedtls_gcm_init( &ctx ); |
markrad | 0:cdf462088d13 | 81 | |
markrad | 0:cdf462088d13 | 82 | memset(key_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 83 | memset(src_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 84 | memset(dst_str, 0x00, 257); |
markrad | 0:cdf462088d13 | 85 | memset(iv_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 86 | memset(add_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 87 | memset(tag_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 88 | memset(output, 0x00, 128); |
markrad | 0:cdf462088d13 | 89 | |
markrad | 0:cdf462088d13 | 90 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 91 | pt_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 92 | iv_len = unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 93 | add_len = unhexify( add_str, hex_add_string ); |
markrad | 0:cdf462088d13 | 94 | unhexify( tag_str, hex_tag_string ); |
markrad | 0:cdf462088d13 | 95 | |
markrad | 0:cdf462088d13 | 96 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
markrad | 0:cdf462088d13 | 97 | if( init_result == 0 ) |
markrad | 0:cdf462088d13 | 98 | { |
markrad | 0:cdf462088d13 | 99 | ret = mbedtls_gcm_auth_decrypt( &ctx, pt_len, iv_str, iv_len, add_str, add_len, tag_str, tag_len, src_str, output ); |
markrad | 0:cdf462088d13 | 100 | |
markrad | 0:cdf462088d13 | 101 | if( strcmp( "FAIL", pt_result ) == 0 ) |
markrad | 0:cdf462088d13 | 102 | { |
markrad | 0:cdf462088d13 | 103 | TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ); |
markrad | 0:cdf462088d13 | 104 | } |
markrad | 0:cdf462088d13 | 105 | else |
markrad | 0:cdf462088d13 | 106 | { |
markrad | 0:cdf462088d13 | 107 | TEST_ASSERT( ret == 0 ); |
markrad | 0:cdf462088d13 | 108 | hexify( dst_str, output, pt_len ); |
markrad | 0:cdf462088d13 | 109 | |
markrad | 0:cdf462088d13 | 110 | TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 ); |
markrad | 0:cdf462088d13 | 111 | } |
markrad | 0:cdf462088d13 | 112 | } |
markrad | 0:cdf462088d13 | 113 | |
markrad | 0:cdf462088d13 | 114 | exit: |
markrad | 0:cdf462088d13 | 115 | mbedtls_gcm_free( &ctx ); |
markrad | 0:cdf462088d13 | 116 | } |
markrad | 0:cdf462088d13 | 117 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 118 | |
markrad | 0:cdf462088d13 | 119 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 120 | void gcm_selftest() |
markrad | 0:cdf462088d13 | 121 | { |
markrad | 0:cdf462088d13 | 122 | TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 123 | } |
markrad | 0:cdf462088d13 | 124 | /* END_CASE */ |