| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 2 | #include <polarssl/cipher.h> | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 3 |  | 
|  | 4 | #if defined(POLARSSL_GCM_C) | 
|  | 5 | #include <polarssl/gcm.h> | 
|  | 6 | #endif | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | /* END_HEADER */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 8 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 9 | /* BEGIN_DEPENDENCIES | 
|  | 10 | * depends_on:POLARSSL_CIPHER_C | 
|  | 11 | * END_DEPENDENCIES | 
|  | 12 | */ | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 13 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 14 | /* BEGIN_CASE */ | 
|  | 15 | void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, | 
|  | 16 | int length_val, int pad_mode ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 17 | { | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 18 | size_t length = length_val, outlen, total_len, i; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 19 | unsigned char key[32]; | 
|  | 20 | unsigned char iv[16]; | 
| Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 21 | unsigned char ad[13]; | 
|  | 22 | unsigned char tag[16]; | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 23 | unsigned char inbuf[64]; | 
|  | 24 | unsigned char encbuf[64]; | 
|  | 25 | unsigned char decbuf[64]; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 26 |  | 
|  | 27 | const cipher_info_t *cipher_info; | 
|  | 28 | cipher_context_t ctx_dec; | 
|  | 29 | cipher_context_t ctx_enc; | 
|  | 30 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 31 | /* | 
|  | 32 | * Prepare contexts | 
|  | 33 | */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 34 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); | 
|  | 35 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 36 |  | 
|  | 37 | memset( key, 0x2a, sizeof( key ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | /* Check and get info structures */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 40 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 41 | TEST_ASSERT( NULL != cipher_info ); | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 42 | TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 43 |  | 
|  | 44 | /* Initialise enc and dec contexts */ | 
|  | 45 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); | 
|  | 46 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 47 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 48 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); | 
|  | 49 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 50 |  | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 51 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 52 | if( -1 != pad_mode ) | 
| Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 53 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 54 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) ); | 
|  | 55 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 56 | } | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 57 | #else | 
|  | 58 | (void) pad_mode; | 
|  | 59 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 60 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 61 | /* | 
|  | 62 | * Do a few encode/decode cycles | 
|  | 63 | */ | 
|  | 64 | for( i = 0; i < 3; i++ ) | 
|  | 65 | { | 
|  | 66 | memset( iv , 0x00 + i, sizeof( iv ) ); | 
|  | 67 | memset( ad, 0x10 + i, sizeof( ad ) ); | 
|  | 68 | memset( inbuf, 0x20 + i, sizeof( inbuf ) ); | 
|  | 69 |  | 
|  | 70 | memset( encbuf, 0, sizeof( encbuf ) ); | 
|  | 71 | memset( decbuf, 0, sizeof( decbuf ) ); | 
|  | 72 | memset( tag, 0, sizeof( tag ) ); | 
|  | 73 |  | 
|  | 74 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) ); | 
|  | 75 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) ); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 76 |  | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 77 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); | 
|  | 78 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); | 
|  | 79 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 80 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 81 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) ); | 
|  | 82 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 83 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 84 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 85 | /* encode length number of bytes from inbuf */ | 
|  | 86 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 87 | total_len = outlen; | 
|  | 88 |  | 
|  | 89 | TEST_ASSERT( total_len == length || | 
|  | 90 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 91 | total_len < length && | 
|  | 92 | total_len + cipher_get_block_size( &ctx_enc ) > length ) ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 93 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 94 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 95 | total_len += outlen; | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 96 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 97 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 98 | TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 99 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 100 |  | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 101 | TEST_ASSERT( total_len == length || | 
|  | 102 | ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 103 | total_len > length && | 
|  | 104 | total_len <= length + cipher_get_block_size( &ctx_enc ) ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 105 |  | 
|  | 106 | /* decode the previously encoded string */ | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 107 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); | 
|  | 108 | total_len = outlen; | 
|  | 109 |  | 
|  | 110 | TEST_ASSERT( total_len == length || | 
|  | 111 | ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && | 
|  | 112 | total_len < length && | 
|  | 113 | total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 114 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 115 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 116 | total_len += outlen; | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 117 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 118 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 119 | TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 120 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 121 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 122 | /* check result */ | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 123 | TEST_ASSERT( total_len == length ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 124 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 125 | } | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 126 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 127 | /* | 
|  | 128 | * Done | 
|  | 129 | */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 130 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); | 
|  | 131 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 132 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 133 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 134 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 135 | /* BEGIN_CASE */ | 
|  | 136 | void enc_fail( int cipher_id, int pad_mode, int key_len, | 
|  | 137 | int length_val, int ret ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 138 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 139 | size_t length = length_val; | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 140 | unsigned char key[32]; | 
|  | 141 | unsigned char iv[16]; | 
|  | 142 |  | 
|  | 143 | const cipher_info_t *cipher_info; | 
|  | 144 | cipher_context_t ctx; | 
|  | 145 |  | 
|  | 146 | unsigned char inbuf[64]; | 
|  | 147 | unsigned char encbuf[64]; | 
|  | 148 |  | 
|  | 149 | size_t outlen = 0; | 
|  | 150 |  | 
|  | 151 | memset( key, 0, 32 ); | 
|  | 152 | memset( iv , 0, 16 ); | 
|  | 153 |  | 
|  | 154 | memset( &ctx, 0, sizeof( ctx ) ); | 
|  | 155 |  | 
|  | 156 | memset( inbuf, 5, 64 ); | 
|  | 157 | memset( encbuf, 0, 64 ); | 
|  | 158 |  | 
|  | 159 | /* Check and get info structures */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 160 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 161 | TEST_ASSERT( NULL != cipher_info ); | 
|  | 162 |  | 
|  | 163 | /* Initialise context */ | 
|  | 164 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 165 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 166 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 167 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 168 | #else | 
|  | 169 | (void) pad_mode; | 
|  | 170 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 171 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) ); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 172 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 173 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 174 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 175 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 176 |  | 
|  | 177 | /* encode length number of bytes from inbuf */ | 
|  | 178 | TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 179 | TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 180 |  | 
|  | 181 | /* done */ | 
|  | 182 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 183 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 184 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 185 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 186 | /* BEGIN_CASE */ | 
|  | 187 | void dec_empty_buf() | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 188 | { | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 189 | unsigned char key[32]; | 
|  | 190 | unsigned char iv[16]; | 
|  | 191 |  | 
|  | 192 | cipher_context_t ctx_dec; | 
|  | 193 | const cipher_info_t *cipher_info; | 
|  | 194 |  | 
|  | 195 | unsigned char encbuf[64]; | 
|  | 196 | unsigned char decbuf[64]; | 
|  | 197 |  | 
| Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 198 | size_t outlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 199 |  | 
|  | 200 | memset( key, 0, 32 ); | 
|  | 201 | memset( iv , 0, 16 ); | 
|  | 202 |  | 
|  | 203 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); | 
|  | 204 |  | 
|  | 205 | memset( encbuf, 0, 64 ); | 
|  | 206 | memset( decbuf, 0, 64 ); | 
|  | 207 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 208 | /* Initialise context */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 209 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); | 
|  | 210 | TEST_ASSERT( NULL != cipher_info); | 
|  | 211 |  | 
|  | 212 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); | 
|  | 213 |  | 
|  | 214 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); | 
|  | 215 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 216 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); | 
|  | 217 |  | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 218 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); | 
|  | 219 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 220 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 221 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 222 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 223 |  | 
|  | 224 | /* decode 0-byte string */ | 
|  | 225 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); | 
|  | 226 | TEST_ASSERT( 0 == outlen ); | 
| Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 227 | TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 228 | &ctx_dec, decbuf + outlen, &outlen ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 229 | TEST_ASSERT( 0 == outlen ); | 
|  | 230 |  | 
|  | 231 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 232 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 233 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 234 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 235 | /* BEGIN_CASE */ | 
|  | 236 | void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val, | 
|  | 237 | int second_length_val ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 238 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 239 | size_t first_length = first_length_val; | 
|  | 240 | size_t second_length = second_length_val; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 241 | size_t length = first_length + second_length; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 242 | unsigned char key[32]; | 
|  | 243 | unsigned char iv[16]; | 
|  | 244 |  | 
|  | 245 | cipher_context_t ctx_dec; | 
|  | 246 | cipher_context_t ctx_enc; | 
|  | 247 | const cipher_info_t *cipher_info; | 
|  | 248 |  | 
|  | 249 | unsigned char inbuf[64]; | 
|  | 250 | unsigned char encbuf[64]; | 
|  | 251 | unsigned char decbuf[64]; | 
|  | 252 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 253 | size_t outlen = 0; | 
|  | 254 | size_t totaloutlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 255 |  | 
|  | 256 | memset( key, 0, 32 ); | 
|  | 257 | memset( iv , 0, 16 ); | 
|  | 258 |  | 
|  | 259 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); | 
|  | 260 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); | 
|  | 261 |  | 
|  | 262 | memset( inbuf, 5, 64 ); | 
|  | 263 | memset( encbuf, 0, 64 ); | 
|  | 264 | memset( decbuf, 0, 64 ); | 
|  | 265 |  | 
|  | 266 | /* Initialise enc and dec contexts */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 267 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 268 | TEST_ASSERT( NULL != cipher_info); | 
|  | 269 |  | 
|  | 270 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); | 
|  | 271 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); | 
|  | 272 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 273 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); | 
|  | 274 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 275 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 276 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); | 
|  | 277 | TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); | 
|  | 278 |  | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 279 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) ); | 
|  | 280 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) ); | 
|  | 281 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 282 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 283 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) ); | 
|  | 284 | TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 285 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 286 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 287 | /* encode length number of bytes from inbuf */ | 
|  | 288 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); | 
|  | 289 | totaloutlen = outlen; | 
|  | 290 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); | 
|  | 291 | totaloutlen += outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 292 | TEST_ASSERT( totaloutlen == length || | 
|  | 293 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 294 | totaloutlen < length && | 
|  | 295 | totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); | 
|  | 296 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 297 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 298 | totaloutlen += outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 299 | TEST_ASSERT( totaloutlen == length || | 
|  | 300 | ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && | 
|  | 301 | totaloutlen > length && | 
|  | 302 | totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 303 |  | 
|  | 304 | /* decode the previously encoded string */ | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 305 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); | 
|  | 306 | totaloutlen = outlen; | 
|  | 307 |  | 
|  | 308 | TEST_ASSERT( totaloutlen == length || | 
|  | 309 | ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && | 
|  | 310 | totaloutlen < length && | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 311 | totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 312 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 313 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 314 | totaloutlen += outlen; | 
|  | 315 |  | 
|  | 316 | TEST_ASSERT( totaloutlen == length ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 317 |  | 
|  | 318 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); | 
|  | 319 |  | 
|  | 320 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); | 
|  | 321 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 322 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 323 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 324 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 325 | /* BEGIN_CASE */ | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 326 | void decrypt_test_vec( int cipher_id, int pad_mode, | 
|  | 327 | char *hex_key, char *hex_iv, | 
|  | 328 | char *hex_cipher, char *hex_clear, | 
|  | 329 | char *hex_ad, char *hex_tag, | 
|  | 330 | int finish_result, int tag_result ) | 
|  | 331 | { | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 332 | unsigned char key[50]; | 
|  | 333 | unsigned char iv[50]; | 
|  | 334 | unsigned char cipher[200]; | 
|  | 335 | unsigned char clear[200]; | 
|  | 336 | unsigned char ad[200]; | 
|  | 337 | unsigned char tag[20]; | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 338 | size_t key_len, iv_len, cipher_len, clear_len; | 
|  | 339 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
|  | 340 | size_t ad_len, tag_len; | 
|  | 341 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 342 | cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 343 | unsigned char output[200]; | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 344 | size_t outlen, total_len; | 
|  | 345 |  | 
|  | 346 | memset( key, 0x00, sizeof( key ) ); | 
|  | 347 | memset( iv, 0x00, sizeof( iv ) ); | 
|  | 348 | memset( cipher, 0x00, sizeof( cipher ) ); | 
|  | 349 | memset( clear, 0x00, sizeof( clear ) ); | 
|  | 350 | memset( ad, 0x00, sizeof( ad ) ); | 
|  | 351 | memset( tag, 0x00, sizeof( tag ) ); | 
|  | 352 | memset( output, 0x00, sizeof( output ) ); | 
|  | 353 |  | 
|  | 354 | key_len = unhexify( key, hex_key ); | 
|  | 355 | iv_len = unhexify( iv, hex_iv ); | 
|  | 356 | cipher_len = unhexify( cipher, hex_cipher ); | 
|  | 357 | clear_len = unhexify( clear, hex_clear ); | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 358 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 359 | ad_len = unhexify( ad, hex_ad ); | 
|  | 360 | tag_len = unhexify( tag, hex_tag ); | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 361 | #else | 
|  | 362 | ((void) hex_ad); | 
|  | 363 | ((void) hex_tag); | 
|  | 364 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 365 |  | 
|  | 366 | /* Prepare context */ | 
|  | 367 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, | 
|  | 368 | cipher_info_from_type( cipher_id ) ) ); | 
|  | 369 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 370 | #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 371 | if( pad_mode != -1 ) | 
|  | 372 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 373 | #else | 
|  | 374 | (void) pad_mode; | 
|  | 375 | #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 376 | TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) ); | 
|  | 377 | TEST_ASSERT( 0 == cipher_reset( &ctx ) ); | 
|  | 378 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
|  | 379 | TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) ); | 
|  | 380 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
|  | 381 |  | 
|  | 382 | /* decode buffer and check tag */ | 
|  | 383 | total_len = 0; | 
|  | 384 | TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) ); | 
|  | 385 | total_len += outlen; | 
|  | 386 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, | 
|  | 387 | &outlen ) ); | 
|  | 388 | total_len += outlen; | 
|  | 389 | #if defined(POLARSSL_CIPHER_MODE_AEAD) | 
|  | 390 | TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) ); | 
|  | 391 | #endif /* POLARSSL_CIPHER_MODE_AEAD */ | 
|  | 392 |  | 
|  | 393 | /* check plaintext only if everything went fine */ | 
|  | 394 | if( 0 == finish_result && 0 == tag_result ) | 
|  | 395 | { | 
|  | 396 | TEST_ASSERT( total_len == clear_len ); | 
|  | 397 | TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) ); | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | cipher_free_ctx( &ctx ); | 
|  | 401 | } | 
|  | 402 | /* END_CASE */ | 
|  | 403 |  | 
|  | 404 | /* BEGIN_CASE */ | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 405 | void test_vec_ecb( int cipher_id, int operation, char *hex_key, | 
|  | 406 | char *hex_input, char *hex_result, | 
|  | 407 | int finish_result ) | 
|  | 408 | { | 
|  | 409 | unsigned char key[50]; | 
|  | 410 | unsigned char input[16]; | 
|  | 411 | unsigned char result[16]; | 
|  | 412 | size_t key_len; | 
|  | 413 | cipher_context_t ctx; | 
|  | 414 | unsigned char output[32]; | 
|  | 415 | size_t outlen; | 
|  | 416 |  | 
|  | 417 | memset( key, 0x00, sizeof( key ) ); | 
|  | 418 | memset( input, 0x00, sizeof( input ) ); | 
|  | 419 | memset( result, 0x00, sizeof( result ) ); | 
|  | 420 | memset( output, 0x00, sizeof( output ) ); | 
|  | 421 |  | 
|  | 422 | /* Prepare context */ | 
|  | 423 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, | 
|  | 424 | cipher_info_from_type( cipher_id ) ) ); | 
|  | 425 |  | 
|  | 426 | key_len = unhexify( key, hex_key ); | 
|  | 427 | TEST_ASSERT( unhexify( input, hex_input ) == | 
|  | 428 | (int) cipher_get_block_size( &ctx ) ); | 
|  | 429 | TEST_ASSERT( unhexify( result, hex_result ) == | 
|  | 430 | (int) cipher_get_block_size( &ctx ) ); | 
|  | 431 |  | 
|  | 432 | TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) ); | 
|  | 433 |  | 
|  | 434 | TEST_ASSERT( 0 == cipher_update( &ctx, input, | 
|  | 435 | cipher_get_block_size( &ctx ), | 
|  | 436 | output, &outlen ) ); | 
|  | 437 | TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) ); | 
|  | 438 | TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen, | 
|  | 439 | &outlen ) ); | 
|  | 440 | TEST_ASSERT( 0 == outlen ); | 
|  | 441 |  | 
|  | 442 | /* check plaintext only if everything went fine */ | 
|  | 443 | if( 0 == finish_result ) | 
|  | 444 | TEST_ASSERT( 0 == memcmp( output, result, | 
|  | 445 | cipher_get_block_size( &ctx ) ) ); | 
|  | 446 |  | 
|  | 447 | cipher_free_ctx( &ctx ); | 
|  | 448 | } | 
|  | 449 | /* END_CASE */ | 
|  | 450 |  | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 451 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_WITH_PADDING */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 452 | void set_padding( int cipher_id, int pad_mode, int ret ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 453 | { | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 454 | const cipher_info_t *cipher_info; | 
|  | 455 | cipher_context_t ctx; | 
|  | 456 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 457 | cipher_info = cipher_info_from_type( cipher_id ); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 458 | TEST_ASSERT( NULL != cipher_info ); | 
|  | 459 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); | 
|  | 460 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 461 | TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 462 |  | 
|  | 463 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 464 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 465 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 466 |  | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 467 | /* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 468 | void check_padding( int pad_mode, char *input_str, int ret, int dlen_check ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 469 | { | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 470 | cipher_info_t cipher_info; | 
|  | 471 | cipher_context_t ctx; | 
|  | 472 | unsigned char input[16]; | 
|  | 473 | size_t ilen, dlen; | 
|  | 474 |  | 
|  | 475 | /* build a fake context just for getting access to get_padding */ | 
|  | 476 | memset( &ctx, 0, sizeof( ctx ) ); | 
|  | 477 | cipher_info.mode = POLARSSL_MODE_CBC; | 
|  | 478 | ctx.cipher_info = &cipher_info; | 
|  | 479 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 480 | TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 481 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 482 | ilen = unhexify( input, input_str ); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 483 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 484 | TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); | 
|  | 485 | if( 0 == ret ) | 
|  | 486 | TEST_ASSERT( dlen == (size_t) dlen_check ); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 487 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 488 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 489 |  | 
| Manuel Pégourié-Gonnard | 2014016 | 2013-10-10 12:48:03 +0200 | [diff] [blame] | 490 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 491 | void cipher_selftest() | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 492 | { | 
|  | 493 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); | 
|  | 494 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 495 | /* END_CASE */ |