mbed TLS Build
tests/suites/test_suite_ccm.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
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/ccm.h" |
markrad | 0:cdf462088d13 | 3 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 6 | * depends_on:MBEDTLS_CCM_C |
markrad | 0:cdf462088d13 | 7 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 8 | */ |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */ |
markrad | 0:cdf462088d13 | 11 | void mbedtls_ccm_self_test( ) |
markrad | 0:cdf462088d13 | 12 | { |
markrad | 0:cdf462088d13 | 13 | TEST_ASSERT( mbedtls_ccm_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 14 | } |
markrad | 0:cdf462088d13 | 15 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 16 | |
markrad | 0:cdf462088d13 | 17 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 18 | void mbedtls_ccm_setkey( int cipher_id, int key_size, int result ) |
markrad | 0:cdf462088d13 | 19 | { |
markrad | 0:cdf462088d13 | 20 | mbedtls_ccm_context ctx; |
markrad | 0:cdf462088d13 | 21 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 22 | int ret; |
markrad | 0:cdf462088d13 | 23 | |
markrad | 0:cdf462088d13 | 24 | mbedtls_ccm_init( &ctx ); |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | memset( key, 0x2A, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 27 | TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) ); |
markrad | 0:cdf462088d13 | 28 | |
markrad | 0:cdf462088d13 | 29 | ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size ); |
markrad | 0:cdf462088d13 | 30 | TEST_ASSERT( ret == result ); |
markrad | 0:cdf462088d13 | 31 | |
markrad | 0:cdf462088d13 | 32 | exit: |
markrad | 0:cdf462088d13 | 33 | mbedtls_ccm_free( &ctx ); |
markrad | 0:cdf462088d13 | 34 | } |
markrad | 0:cdf462088d13 | 35 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
markrad | 0:cdf462088d13 | 38 | void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res ) |
markrad | 0:cdf462088d13 | 39 | { |
markrad | 0:cdf462088d13 | 40 | mbedtls_ccm_context ctx; |
markrad | 0:cdf462088d13 | 41 | unsigned char key[16]; |
markrad | 0:cdf462088d13 | 42 | unsigned char msg[10]; |
markrad | 0:cdf462088d13 | 43 | unsigned char iv[14]; |
markrad | 0:cdf462088d13 | 44 | unsigned char add[10]; |
markrad | 0:cdf462088d13 | 45 | unsigned char out[10]; |
markrad | 0:cdf462088d13 | 46 | unsigned char tag[18]; |
markrad | 0:cdf462088d13 | 47 | int decrypt_ret; |
markrad | 0:cdf462088d13 | 48 | |
markrad | 0:cdf462088d13 | 49 | mbedtls_ccm_init( &ctx ); |
markrad | 0:cdf462088d13 | 50 | |
markrad | 0:cdf462088d13 | 51 | memset( key, 0, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 52 | memset( msg, 0, sizeof( msg ) ); |
markrad | 0:cdf462088d13 | 53 | memset( iv, 0, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 54 | memset( add, 0, sizeof( add ) ); |
markrad | 0:cdf462088d13 | 55 | memset( out, 0, sizeof( out ) ); |
markrad | 0:cdf462088d13 | 56 | memset( tag, 0, sizeof( tag ) ); |
markrad | 0:cdf462088d13 | 57 | |
markrad | 0:cdf462088d13 | 58 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, |
markrad | 0:cdf462088d13 | 59 | key, 8 * sizeof( key ) ) == 0 ); |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, |
markrad | 0:cdf462088d13 | 62 | msg, out, tag, tag_len ) == res ); |
markrad | 0:cdf462088d13 | 63 | |
markrad | 0:cdf462088d13 | 64 | decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len, |
markrad | 0:cdf462088d13 | 65 | msg, out, tag, tag_len ); |
markrad | 0:cdf462088d13 | 66 | |
markrad | 0:cdf462088d13 | 67 | if( res == 0 ) |
markrad | 0:cdf462088d13 | 68 | TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED ); |
markrad | 0:cdf462088d13 | 69 | else |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( decrypt_ret == res ); |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | exit: |
markrad | 0:cdf462088d13 | 73 | mbedtls_ccm_free( &ctx ); |
markrad | 0:cdf462088d13 | 74 | } |
markrad | 0:cdf462088d13 | 75 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 76 | |
markrad | 0:cdf462088d13 | 77 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 78 | void mbedtls_ccm_encrypt_and_tag( int cipher_id, |
markrad | 0:cdf462088d13 | 79 | char *key_hex, char *msg_hex, |
markrad | 0:cdf462088d13 | 80 | char *iv_hex, char *add_hex, |
markrad | 0:cdf462088d13 | 81 | char *result_hex ) |
markrad | 0:cdf462088d13 | 82 | { |
markrad | 0:cdf462088d13 | 83 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 84 | unsigned char msg[50]; |
markrad | 0:cdf462088d13 | 85 | unsigned char iv[13]; |
markrad | 0:cdf462088d13 | 86 | unsigned char add[32]; |
markrad | 0:cdf462088d13 | 87 | unsigned char result[50]; |
markrad | 0:cdf462088d13 | 88 | mbedtls_ccm_context ctx; |
markrad | 0:cdf462088d13 | 89 | size_t key_len, msg_len, iv_len, add_len, tag_len, result_len; |
markrad | 0:cdf462088d13 | 90 | |
markrad | 0:cdf462088d13 | 91 | mbedtls_ccm_init( &ctx ); |
markrad | 0:cdf462088d13 | 92 | |
markrad | 0:cdf462088d13 | 93 | memset( key, 0x00, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 94 | memset( msg, 0x00, sizeof( msg ) ); |
markrad | 0:cdf462088d13 | 95 | memset( iv, 0x00, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 96 | memset( add, 0x00, sizeof( add ) ); |
markrad | 0:cdf462088d13 | 97 | memset( result, 0x00, sizeof( result ) ); |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | key_len = unhexify( key, key_hex ); |
markrad | 0:cdf462088d13 | 100 | msg_len = unhexify( msg, msg_hex ); |
markrad | 0:cdf462088d13 | 101 | iv_len = unhexify( iv, iv_hex ); |
markrad | 0:cdf462088d13 | 102 | add_len = unhexify( add, add_hex ); |
markrad | 0:cdf462088d13 | 103 | result_len = unhexify( result, result_hex ); |
markrad | 0:cdf462088d13 | 104 | tag_len = result_len - msg_len; |
markrad | 0:cdf462088d13 | 105 | |
markrad | 0:cdf462088d13 | 106 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
markrad | 0:cdf462088d13 | 107 | |
markrad | 0:cdf462088d13 | 108 | /* Test with input == output */ |
markrad | 0:cdf462088d13 | 109 | TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, |
markrad | 0:cdf462088d13 | 110 | msg, msg, msg + msg_len, tag_len ) == 0 ); |
markrad | 0:cdf462088d13 | 111 | |
markrad | 0:cdf462088d13 | 112 | TEST_ASSERT( memcmp( msg, result, result_len ) == 0 ); |
markrad | 0:cdf462088d13 | 113 | |
markrad | 0:cdf462088d13 | 114 | /* Check we didn't write past the end */ |
markrad | 0:cdf462088d13 | 115 | TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 ); |
markrad | 0:cdf462088d13 | 116 | |
markrad | 0:cdf462088d13 | 117 | exit: |
markrad | 0:cdf462088d13 | 118 | mbedtls_ccm_free( &ctx ); |
markrad | 0:cdf462088d13 | 119 | } |
markrad | 0:cdf462088d13 | 120 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 121 | |
markrad | 0:cdf462088d13 | 122 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 123 | void mbedtls_ccm_auth_decrypt( int cipher_id, |
markrad | 0:cdf462088d13 | 124 | char *key_hex, char *msg_hex, |
markrad | 0:cdf462088d13 | 125 | char *iv_hex, char *add_hex, |
markrad | 0:cdf462088d13 | 126 | int tag_len, char *result_hex ) |
markrad | 0:cdf462088d13 | 127 | { |
markrad | 0:cdf462088d13 | 128 | unsigned char key[32]; |
markrad | 0:cdf462088d13 | 129 | unsigned char msg[50]; |
markrad | 0:cdf462088d13 | 130 | unsigned char iv[13]; |
markrad | 0:cdf462088d13 | 131 | unsigned char add[32]; |
markrad | 0:cdf462088d13 | 132 | unsigned char tag[16]; |
markrad | 0:cdf462088d13 | 133 | unsigned char result[50]; |
markrad | 0:cdf462088d13 | 134 | mbedtls_ccm_context ctx; |
markrad | 0:cdf462088d13 | 135 | size_t key_len, msg_len, iv_len, add_len, result_len; |
markrad | 0:cdf462088d13 | 136 | int ret; |
markrad | 0:cdf462088d13 | 137 | |
markrad | 0:cdf462088d13 | 138 | mbedtls_ccm_init( &ctx ); |
markrad | 0:cdf462088d13 | 139 | |
markrad | 0:cdf462088d13 | 140 | memset( key, 0x00, sizeof( key ) ); |
markrad | 0:cdf462088d13 | 141 | memset( msg, 0x00, sizeof( msg ) ); |
markrad | 0:cdf462088d13 | 142 | memset( iv, 0x00, sizeof( iv ) ); |
markrad | 0:cdf462088d13 | 143 | memset( add, 0x00, sizeof( add ) ); |
markrad | 0:cdf462088d13 | 144 | memset( tag, 0x00, sizeof( tag ) ); |
markrad | 0:cdf462088d13 | 145 | memset( result, 0x00, sizeof( result ) ); |
markrad | 0:cdf462088d13 | 146 | |
markrad | 0:cdf462088d13 | 147 | key_len = unhexify( key, key_hex ); |
markrad | 0:cdf462088d13 | 148 | msg_len = unhexify( msg, msg_hex ); |
markrad | 0:cdf462088d13 | 149 | iv_len = unhexify( iv, iv_hex ); |
markrad | 0:cdf462088d13 | 150 | add_len = unhexify( add, add_hex ); |
markrad | 0:cdf462088d13 | 151 | msg_len -= tag_len; |
markrad | 0:cdf462088d13 | 152 | memcpy( tag, msg + msg_len, tag_len ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | if( strcmp( "FAIL", result_hex ) == 0 ) |
markrad | 0:cdf462088d13 | 155 | { |
markrad | 0:cdf462088d13 | 156 | ret = MBEDTLS_ERR_CCM_AUTH_FAILED; |
markrad | 0:cdf462088d13 | 157 | result_len = -1; |
markrad | 0:cdf462088d13 | 158 | } |
markrad | 0:cdf462088d13 | 159 | else |
markrad | 0:cdf462088d13 | 160 | { |
markrad | 0:cdf462088d13 | 161 | ret = 0; |
markrad | 0:cdf462088d13 | 162 | result_len = unhexify( result, result_hex ); |
markrad | 0:cdf462088d13 | 163 | } |
markrad | 0:cdf462088d13 | 164 | |
markrad | 0:cdf462088d13 | 165 | TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
markrad | 0:cdf462088d13 | 166 | |
markrad | 0:cdf462088d13 | 167 | /* Test with input == output */ |
markrad | 0:cdf462088d13 | 168 | TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len, |
markrad | 0:cdf462088d13 | 169 | msg, msg, msg + msg_len, tag_len ) == ret ); |
markrad | 0:cdf462088d13 | 170 | |
markrad | 0:cdf462088d13 | 171 | if( ret == 0 ) |
markrad | 0:cdf462088d13 | 172 | { |
markrad | 0:cdf462088d13 | 173 | TEST_ASSERT( memcmp( msg, result, result_len ) == 0 ); |
markrad | 0:cdf462088d13 | 174 | } |
markrad | 0:cdf462088d13 | 175 | else |
markrad | 0:cdf462088d13 | 176 | { |
markrad | 0:cdf462088d13 | 177 | size_t i; |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | for( i = 0; i < msg_len; i++ ) |
markrad | 0:cdf462088d13 | 180 | TEST_ASSERT( msg[i] == 0 ); |
markrad | 0:cdf462088d13 | 181 | } |
markrad | 0:cdf462088d13 | 182 | |
markrad | 0:cdf462088d13 | 183 | /* Check we didn't write past the end (where the original tag is) */ |
markrad | 0:cdf462088d13 | 184 | TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 ); |
markrad | 0:cdf462088d13 | 185 | |
markrad | 0:cdf462088d13 | 186 | exit: |
markrad | 0:cdf462088d13 | 187 | mbedtls_ccm_free( &ctx ); |
markrad | 0:cdf462088d13 | 188 | } |
markrad | 0:cdf462088d13 | 189 | /* END_CASE */ |