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@2:bbdeda018a3c, 2017-09-29 (annotated)
- Committer:
- Jasper Wallace
- Date:
- Fri Sep 29 19:50:30 2017 +0100
- Revision:
- 2:bbdeda018a3c
- Parent:
- 0:cdf462088d13
Update to mbedtls 2.6.0, many changes.
Changes to mbedtls sources made:
in include/mbedtls/config.h comment out:
#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C
uncomment:
#define MBEDTLS_NO_PLATFORM_ENTROPY
remove the following directorys:
programs
yotta
visualc
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 */ |
Jasper Wallace |
2:bbdeda018a3c | 11 | void gcm_bad_parameters( int cipher_id, int direction, |
Jasper Wallace |
2:bbdeda018a3c | 12 | char *hex_key_string, char *hex_src_string, |
Jasper Wallace |
2:bbdeda018a3c | 13 | char *hex_iv_string, char *hex_add_string, |
Jasper Wallace |
2:bbdeda018a3c | 14 | int tag_len_bits, int gcm_result ) |
Jasper Wallace |
2:bbdeda018a3c | 15 | { |
Jasper Wallace |
2:bbdeda018a3c | 16 | unsigned char key_str[128]; |
Jasper Wallace |
2:bbdeda018a3c | 17 | unsigned char src_str[128]; |
Jasper Wallace |
2:bbdeda018a3c | 18 | unsigned char dst_str[257]; |
Jasper Wallace |
2:bbdeda018a3c | 19 | unsigned char iv_str[128]; |
Jasper Wallace |
2:bbdeda018a3c | 20 | unsigned char add_str[128]; |
Jasper Wallace |
2:bbdeda018a3c | 21 | unsigned char tag_str[128]; |
Jasper Wallace |
2:bbdeda018a3c | 22 | unsigned char output[128]; |
Jasper Wallace |
2:bbdeda018a3c | 23 | unsigned char tag_output[16]; |
Jasper Wallace |
2:bbdeda018a3c | 24 | mbedtls_gcm_context ctx; |
Jasper Wallace |
2:bbdeda018a3c | 25 | unsigned int key_len; |
Jasper Wallace |
2:bbdeda018a3c | 26 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
Jasper Wallace |
2:bbdeda018a3c | 27 | |
Jasper Wallace |
2:bbdeda018a3c | 28 | mbedtls_gcm_init( &ctx ); |
Jasper Wallace |
2:bbdeda018a3c | 29 | |
Jasper Wallace |
2:bbdeda018a3c | 30 | memset( key_str, 0x00, sizeof( key_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 31 | memset( src_str, 0x00, sizeof( src_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 32 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 33 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 34 | memset( add_str, 0x00, sizeof( add_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 35 | memset( tag_str, 0x00, sizeof( tag_str ) ); |
Jasper Wallace |
2:bbdeda018a3c | 36 | memset( output, 0x00, sizeof( output ) ); |
Jasper Wallace |
2:bbdeda018a3c | 37 | memset( tag_output, 0x00, sizeof( tag_output ) ); |
Jasper Wallace |
2:bbdeda018a3c | 38 | |
Jasper Wallace |
2:bbdeda018a3c | 39 | key_len = unhexify( key_str, hex_key_string ); |
Jasper Wallace |
2:bbdeda018a3c | 40 | pt_len = unhexify( src_str, hex_src_string ); |
Jasper Wallace |
2:bbdeda018a3c | 41 | iv_len = unhexify( iv_str, hex_iv_string ); |
Jasper Wallace |
2:bbdeda018a3c | 42 | add_len = unhexify( add_str, hex_add_string ); |
Jasper Wallace |
2:bbdeda018a3c | 43 | |
Jasper Wallace |
2:bbdeda018a3c | 44 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == 0 ); |
Jasper Wallace |
2:bbdeda018a3c | 45 | TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, pt_len, iv_str, iv_len, |
Jasper Wallace |
2:bbdeda018a3c | 46 | add_str, add_len, src_str, output, tag_len, tag_output ) == gcm_result ); |
Jasper Wallace |
2:bbdeda018a3c | 47 | |
Jasper Wallace |
2:bbdeda018a3c | 48 | exit: |
Jasper Wallace |
2:bbdeda018a3c | 49 | mbedtls_gcm_free( &ctx ); |
Jasper Wallace |
2:bbdeda018a3c | 50 | } |
Jasper Wallace |
2:bbdeda018a3c | 51 | /* END_CASE */ |
Jasper Wallace |
2:bbdeda018a3c | 52 | |
Jasper Wallace |
2:bbdeda018a3c | 53 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 54 | void gcm_encrypt_and_tag( int cipher_id, |
markrad | 0:cdf462088d13 | 55 | char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 56 | char *hex_iv_string, char *hex_add_string, |
markrad | 0:cdf462088d13 | 57 | char *hex_dst_string, int tag_len_bits, |
markrad | 0:cdf462088d13 | 58 | char *hex_tag_string, int init_result ) |
markrad | 0:cdf462088d13 | 59 | { |
markrad | 0:cdf462088d13 | 60 | unsigned char key_str[128]; |
markrad | 0:cdf462088d13 | 61 | unsigned char src_str[128]; |
markrad | 0:cdf462088d13 | 62 | unsigned char dst_str[257]; |
markrad | 0:cdf462088d13 | 63 | unsigned char iv_str[128]; |
markrad | 0:cdf462088d13 | 64 | unsigned char add_str[128]; |
markrad | 0:cdf462088d13 | 65 | unsigned char tag_str[128]; |
markrad | 0:cdf462088d13 | 66 | unsigned char output[128]; |
markrad | 0:cdf462088d13 | 67 | unsigned char tag_output[16]; |
markrad | 0:cdf462088d13 | 68 | mbedtls_gcm_context ctx; |
markrad | 0:cdf462088d13 | 69 | unsigned int key_len; |
markrad | 0:cdf462088d13 | 70 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | mbedtls_gcm_init( &ctx ); |
markrad | 0:cdf462088d13 | 73 | |
markrad | 0:cdf462088d13 | 74 | memset(key_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 75 | memset(src_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 76 | memset(dst_str, 0x00, 257); |
markrad | 0:cdf462088d13 | 77 | memset(iv_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 78 | memset(add_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 79 | memset(tag_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 80 | memset(output, 0x00, 128); |
markrad | 0:cdf462088d13 | 81 | memset(tag_output, 0x00, 16); |
markrad | 0:cdf462088d13 | 82 | |
markrad | 0:cdf462088d13 | 83 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 84 | pt_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 85 | iv_len = unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 86 | add_len = unhexify( add_str, hex_add_string ); |
markrad | 0:cdf462088d13 | 87 | |
markrad | 0:cdf462088d13 | 88 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
markrad | 0:cdf462088d13 | 89 | if( init_result == 0 ) |
markrad | 0:cdf462088d13 | 90 | { |
markrad | 0:cdf462088d13 | 91 | 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 | 92 | hexify( dst_str, output, pt_len ); |
markrad | 0:cdf462088d13 | 93 | hexify( tag_str, tag_output, tag_len ); |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
markrad | 0:cdf462088d13 | 96 | TEST_ASSERT( strcmp( (char *) tag_str, hex_tag_string ) == 0 ); |
markrad | 0:cdf462088d13 | 97 | } |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | exit: |
markrad | 0:cdf462088d13 | 100 | mbedtls_gcm_free( &ctx ); |
markrad | 0:cdf462088d13 | 101 | } |
markrad | 0:cdf462088d13 | 102 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 103 | |
markrad | 0:cdf462088d13 | 104 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 105 | void gcm_decrypt_and_verify( int cipher_id, |
markrad | 0:cdf462088d13 | 106 | char *hex_key_string, char *hex_src_string, |
markrad | 0:cdf462088d13 | 107 | char *hex_iv_string, char *hex_add_string, |
markrad | 0:cdf462088d13 | 108 | int tag_len_bits, char *hex_tag_string, |
markrad | 0:cdf462088d13 | 109 | char *pt_result, int init_result ) |
markrad | 0:cdf462088d13 | 110 | { |
markrad | 0:cdf462088d13 | 111 | unsigned char key_str[128]; |
markrad | 0:cdf462088d13 | 112 | unsigned char src_str[128]; |
markrad | 0:cdf462088d13 | 113 | unsigned char dst_str[257]; |
markrad | 0:cdf462088d13 | 114 | unsigned char iv_str[128]; |
markrad | 0:cdf462088d13 | 115 | unsigned char add_str[128]; |
markrad | 0:cdf462088d13 | 116 | unsigned char tag_str[128]; |
markrad | 0:cdf462088d13 | 117 | unsigned char output[128]; |
markrad | 0:cdf462088d13 | 118 | mbedtls_gcm_context ctx; |
markrad | 0:cdf462088d13 | 119 | unsigned int key_len; |
markrad | 0:cdf462088d13 | 120 | size_t pt_len, iv_len, add_len, tag_len = tag_len_bits / 8; |
markrad | 0:cdf462088d13 | 121 | int ret; |
markrad | 0:cdf462088d13 | 122 | |
markrad | 0:cdf462088d13 | 123 | mbedtls_gcm_init( &ctx ); |
markrad | 0:cdf462088d13 | 124 | |
markrad | 0:cdf462088d13 | 125 | memset(key_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 126 | memset(src_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 127 | memset(dst_str, 0x00, 257); |
markrad | 0:cdf462088d13 | 128 | memset(iv_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 129 | memset(add_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 130 | memset(tag_str, 0x00, 128); |
markrad | 0:cdf462088d13 | 131 | memset(output, 0x00, 128); |
markrad | 0:cdf462088d13 | 132 | |
markrad | 0:cdf462088d13 | 133 | key_len = unhexify( key_str, hex_key_string ); |
markrad | 0:cdf462088d13 | 134 | pt_len = unhexify( src_str, hex_src_string ); |
markrad | 0:cdf462088d13 | 135 | iv_len = unhexify( iv_str, hex_iv_string ); |
markrad | 0:cdf462088d13 | 136 | add_len = unhexify( add_str, hex_add_string ); |
markrad | 0:cdf462088d13 | 137 | unhexify( tag_str, hex_tag_string ); |
markrad | 0:cdf462088d13 | 138 | |
markrad | 0:cdf462088d13 | 139 | TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str, key_len * 8 ) == init_result ); |
markrad | 0:cdf462088d13 | 140 | if( init_result == 0 ) |
markrad | 0:cdf462088d13 | 141 | { |
markrad | 0:cdf462088d13 | 142 | 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 | 143 | |
markrad | 0:cdf462088d13 | 144 | if( strcmp( "FAIL", pt_result ) == 0 ) |
markrad | 0:cdf462088d13 | 145 | { |
markrad | 0:cdf462088d13 | 146 | TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ); |
markrad | 0:cdf462088d13 | 147 | } |
markrad | 0:cdf462088d13 | 148 | else |
markrad | 0:cdf462088d13 | 149 | { |
markrad | 0:cdf462088d13 | 150 | TEST_ASSERT( ret == 0 ); |
markrad | 0:cdf462088d13 | 151 | hexify( dst_str, output, pt_len ); |
markrad | 0:cdf462088d13 | 152 | |
markrad | 0:cdf462088d13 | 153 | TEST_ASSERT( strcmp( (char *) dst_str, pt_result ) == 0 ); |
markrad | 0:cdf462088d13 | 154 | } |
markrad | 0:cdf462088d13 | 155 | } |
markrad | 0:cdf462088d13 | 156 | |
markrad | 0:cdf462088d13 | 157 | exit: |
markrad | 0:cdf462088d13 | 158 | mbedtls_gcm_free( &ctx ); |
markrad | 0:cdf462088d13 | 159 | } |
markrad | 0:cdf462088d13 | 160 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 161 | |
markrad | 0:cdf462088d13 | 162 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 163 | void gcm_selftest() |
markrad | 0:cdf462088d13 | 164 | { |
markrad | 0:cdf462088d13 | 165 | TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 166 | } |
markrad | 0:cdf462088d13 | 167 | /* END_CASE */ |