David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame^] | 1 | /* test_ccm_mode.c - TinyCrypt AES-CCM tests (RFC 3610 tests) */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2015 by Intel Corporation, All Rights Reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of Intel Corporation nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * DESCRIPTION |
| 35 | * This module tests the following AES-CCM Mode routines: |
| 36 | * |
| 37 | * Scenarios tested include: |
| 38 | * - AES128 CCM mode encryption RFC 3610 test vector #1 |
| 39 | * - AES128 CCM mode encryption RFC 3610 test vector #2 |
| 40 | * - AES128 CCM mode encryption RFC 3610 test vector #3 |
| 41 | * - AES128 CCM mode encryption RFC 3610 test vector #7 |
| 42 | * - AES128 CCM mode encryption RFC 3610 test vector #8 |
| 43 | * - AES128 CCM mode encryption RFC 3610 test vector #9 |
| 44 | * - AES128 CCM mode encryption No associated data |
| 45 | * - AES128 CCM mode encryption No payload data |
| 46 | */ |
| 47 | |
| 48 | #include <tinycrypt/ccm_mode.h> |
| 49 | #include <tinycrypt/constants.h> |
| 50 | #include <test_utils.h> |
| 51 | |
| 52 | #include <string.h> |
| 53 | |
| 54 | #define CIPHERTEXT_LEN 50 |
| 55 | #define DECRYPTED_LEN 25 |
| 56 | #define NUM_NIST_KEYS 16 |
| 57 | #define NONCE_LEN 13 |
| 58 | #define HEADER_LEN 8 |
| 59 | #define M_LEN8 8 |
| 60 | #define M_LEN10 10 |
| 61 | #define DATA_BUF_LEN23 23 |
| 62 | #define DATA_BUF_LEN24 24 |
| 63 | #define DATA_BUF_LEN25 25 |
| 64 | #define EXPECTED_BUF_LEN31 31 |
| 65 | #define EXPECTED_BUF_LEN32 32 |
| 66 | #define EXPECTED_BUF_LEN33 33 |
| 67 | #define EXPECTED_BUF_LEN34 34 |
| 68 | #define EXPECTED_BUF_LEN35 35 |
| 69 | |
| 70 | int do_test(const uint8_t *key, |
| 71 | uint8_t *nonce, size_t nlen, |
| 72 | const uint8_t *hdr, size_t hlen, |
| 73 | const uint8_t *data, size_t dlen, |
| 74 | const uint8_t *expected, size_t elen, |
| 75 | const int mlen) |
| 76 | { |
| 77 | int result = TC_PASS; |
| 78 | uint8_t ciphertext[CIPHERTEXT_LEN]; |
| 79 | uint8_t decrypted[DECRYPTED_LEN]; |
| 80 | struct tc_ccm_mode_struct c; |
| 81 | struct tc_aes_key_sched_struct sched; |
| 82 | |
| 83 | tc_aes128_set_encrypt_key(&sched, key); |
| 84 | |
| 85 | result = tc_ccm_config(&c, &sched, nonce, nlen, mlen); |
| 86 | if (result == 0) { |
| 87 | TC_ERROR("CCM config failed in %s.\n", __func__); |
| 88 | |
| 89 | result = TC_FAIL; |
| 90 | goto exitTest1; |
| 91 | } |
| 92 | |
| 93 | result = tc_ccm_generation_encryption(ciphertext, hdr, hlen, |
| 94 | data, dlen, &c); |
| 95 | if (result == 0) { |
| 96 | TC_ERROR("ccm_encrypt failed in %s.\n", __func__); |
| 97 | |
| 98 | result = TC_FAIL; |
| 99 | goto exitTest1; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | if (memcmp(expected, ciphertext, elen) != 0) { |
| 104 | TC_ERROR("ccm_encrypt produced wrong ciphertext in %s.\n", |
| 105 | __func__); |
| 106 | show_str("\t\tExpected", expected, elen); |
| 107 | show_str("\t\tComputed", ciphertext, elen); |
| 108 | |
| 109 | result = TC_FAIL; |
| 110 | goto exitTest1; |
| 111 | } |
| 112 | |
| 113 | result = tc_ccm_decryption_verification(decrypted, hdr, hlen, |
| 114 | ciphertext, dlen + mlen, &c); |
| 115 | if (result == 0) { |
| 116 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 117 | show_str("\t\tExpected", data, dlen); |
| 118 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 119 | |
| 120 | result = TC_FAIL; |
| 121 | goto exitTest1; |
| 122 | } |
| 123 | |
| 124 | result = TC_PASS; |
| 125 | |
| 126 | exitTest1: |
| 127 | TC_END_RESULT(result); |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | int test_vector_1(void) |
| 132 | { |
| 133 | int result = TC_PASS; |
| 134 | /* RFC 3610 test vector #1 */ |
| 135 | const uint8_t key[NUM_NIST_KEYS] = { |
| 136 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 137 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 138 | }; |
| 139 | uint8_t nonce[NONCE_LEN] = { |
| 140 | 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, |
| 141 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 142 | }; |
| 143 | const uint8_t hdr[HEADER_LEN] = { |
| 144 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 145 | }; |
| 146 | const uint8_t data[DATA_BUF_LEN23] = { |
| 147 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 148 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 149 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e |
| 150 | }; |
| 151 | const uint8_t expected[EXPECTED_BUF_LEN31] = { |
| 152 | 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, |
| 153 | 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80, |
| 154 | 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17, |
| 155 | 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 |
| 156 | }; |
| 157 | uint16_t mlen = M_LEN8; |
| 158 | |
| 159 | TC_PRINT("%s: Performing CCM test #1 (RFC 3610 test vector #1):\n", |
| 160 | __func__); |
| 161 | |
| 162 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 163 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 164 | |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | int test_vector_2(void) |
| 169 | { |
| 170 | int result = TC_PASS; |
| 171 | /* RFC 3610 test vector #2 */ |
| 172 | const uint8_t key[NUM_NIST_KEYS] = { |
| 173 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 174 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 175 | }; |
| 176 | uint8_t nonce[NONCE_LEN] = { |
| 177 | 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0xa0, |
| 178 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 179 | }; |
| 180 | const uint8_t hdr[HEADER_LEN] = { |
| 181 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 182 | }; |
| 183 | const uint8_t data[DATA_BUF_LEN24] = { |
| 184 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 185 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 186 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 187 | }; |
| 188 | const uint8_t expected[EXPECTED_BUF_LEN32] = { |
| 189 | 0x72, 0xc9, 0x1a, 0x36, 0xe1, 0x35, 0xf8, 0xcf, |
| 190 | 0x29, 0x1c, 0xa8, 0x94, 0x08, 0x5c, 0x87, 0xe3, |
| 191 | 0xcc, 0x15, 0xc4, 0x39, 0xc9, 0xe4, 0x3a, 0x3b, |
| 192 | 0xa0, 0x91, 0xd5, 0x6e, 0x10, 0x40, 0x09, 0x16 |
| 193 | }; |
| 194 | uint16_t mlen = M_LEN8; |
| 195 | |
| 196 | TC_PRINT("%s: Performing CCM test #2 (RFC 3610 test vector #2):\n", |
| 197 | __func__); |
| 198 | |
| 199 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 200 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 201 | |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | int test_vector_3(void) |
| 206 | { |
| 207 | int result = TC_PASS; |
| 208 | /* RFC 3610 test vector #3 */ |
| 209 | const uint8_t key[NUM_NIST_KEYS] = { |
| 210 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 211 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 212 | }; |
| 213 | uint8_t nonce[NONCE_LEN] = { |
| 214 | 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x02, 0xa0, |
| 215 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 216 | }; |
| 217 | const uint8_t hdr[HEADER_LEN] = { |
| 218 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 219 | }; |
| 220 | const uint8_t data[DATA_BUF_LEN25] = { |
| 221 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 222 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 223 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 224 | 0x20 |
| 225 | }; |
| 226 | const uint8_t expected[EXPECTED_BUF_LEN33] = { |
| 227 | 0x51, 0xb1, 0xe5, 0xf4, 0x4a, 0x19, 0x7d, 0x1d, |
| 228 | 0xa4, 0x6b, 0x0f, 0x8e, 0x2d, 0x28, 0x2a, 0xe8, |
| 229 | 0x71, 0xe8, 0x38, 0xbb, 0x64, 0xda, 0x85, 0x96, |
| 230 | 0x57, 0x4a, 0xda, 0xa7, 0x6f, 0xbd, 0x9f, 0xb0, |
| 231 | 0xc5 |
| 232 | }; |
| 233 | uint16_t mlen = M_LEN8; |
| 234 | |
| 235 | TC_PRINT("%s: Performing CCM test #3 (RFC 3610 test vector #3):\n", |
| 236 | __func__); |
| 237 | |
| 238 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), data, |
| 239 | sizeof(data), expected, sizeof(expected), mlen); |
| 240 | |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | int test_vector_4(void) |
| 245 | { |
| 246 | int result = TC_PASS; |
| 247 | /* RFC 3610 test vector #7 */ |
| 248 | const uint8_t key[NUM_NIST_KEYS] = { |
| 249 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 250 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 251 | }; |
| 252 | uint8_t nonce[NONCE_LEN] = { |
| 253 | 0x00, 0x00, 0x00, 0x09, 0x08, 0x07, 0x06, 0xa0, |
| 254 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 255 | }; |
| 256 | const uint8_t hdr[HEADER_LEN] = { |
| 257 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 258 | }; |
| 259 | const uint8_t data[DATA_BUF_LEN23] = { |
| 260 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 261 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 262 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e |
| 263 | }; |
| 264 | const uint8_t expected[EXPECTED_BUF_LEN33] = { |
| 265 | 0x01, 0x35, 0xD1, 0xB2, 0xC9, 0x5F, 0x41, 0xD5, |
| 266 | 0xD1, 0xD4, 0xFE, 0xC1, 0x85, 0xD1, 0x66, 0xB8, |
| 267 | 0x09, 0x4E, 0x99, 0x9D, 0xFE, 0xD9, 0x6C, 0x04, |
| 268 | 0x8C, 0x56, 0x60, 0x2C, 0x97, 0xAC, 0xBB, 0x74, |
| 269 | 0x90 |
| 270 | }; |
| 271 | uint16_t mlen = M_LEN10; |
| 272 | |
| 273 | TC_PRINT("%s: Performing CCM test #4 (RFC 3610 test vector #7):\n", |
| 274 | __func__); |
| 275 | |
| 276 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 277 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 278 | |
| 279 | return result; |
| 280 | } |
| 281 | |
| 282 | int test_vector_5(void) |
| 283 | { |
| 284 | int result = TC_PASS; |
| 285 | /* RFC 3610 test vector #8 */ |
| 286 | const uint8_t key[NUM_NIST_KEYS] = { |
| 287 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 288 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 289 | }; |
| 290 | uint8_t nonce[NONCE_LEN] = { |
| 291 | 0x00, 0x00, 0x00, 0x0A, 0x09, 0x08, 0x07, 0xA0, |
| 292 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 293 | }; |
| 294 | const uint8_t hdr[HEADER_LEN] = { |
| 295 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 296 | }; |
| 297 | const uint8_t data[DATA_BUF_LEN24] = { |
| 298 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 299 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 300 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 301 | }; |
| 302 | const uint8_t expected[EXPECTED_BUF_LEN34] = { |
| 303 | 0x7B, 0x75, 0x39, 0x9A, 0xC0, 0x83, 0x1D, 0xD2, |
| 304 | 0xF0, 0xBB, 0xD7, 0x58, 0x79, 0xA2, 0xFD, 0x8F, |
| 305 | 0x6C, 0xAE, 0x6B, 0x6C, 0xD9, 0xB7, 0xDB, 0x24, |
| 306 | 0xC1, 0x7B, 0x44, 0x33, 0xF4, 0x34, 0x96, 0x3F, |
| 307 | 0x34, 0xB4 |
| 308 | }; |
| 309 | uint16_t mlen = M_LEN10; |
| 310 | |
| 311 | TC_PRINT("%s: Performing CCM test #5 (RFC 3610 test vector #8):\n", |
| 312 | __func__); |
| 313 | |
| 314 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 315 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 316 | |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | int test_vector_6(void) |
| 321 | { |
| 322 | int result = TC_PASS; |
| 323 | /* RFC 3610 test vector #9 */ |
| 324 | const uint8_t key[NUM_NIST_KEYS] = { |
| 325 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 326 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 327 | }; |
| 328 | uint8_t nonce[NONCE_LEN] = { |
| 329 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 330 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 331 | }; |
| 332 | const uint8_t hdr[HEADER_LEN] = { |
| 333 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 334 | }; |
| 335 | const uint8_t data[DATA_BUF_LEN25] = { |
| 336 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 337 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 338 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 339 | 0x20 |
| 340 | }; |
| 341 | const uint8_t expected[EXPECTED_BUF_LEN35] = { |
| 342 | 0x82, 0x53, 0x1a, 0x60, 0xCC, 0x24, 0x94, 0x5a, |
| 343 | 0x4b, 0x82, 0x79, 0x18, 0x1a, 0xb5, 0xc8, 0x4d, |
| 344 | 0xf2, 0x1c, 0xe7, 0xf9, 0xb7, 0x3f, 0x42, 0xe1, |
| 345 | 0x97, 0xea, 0x9c, 0x07, 0xe5, 0x6b, 0x5e, 0xb1, |
| 346 | 0x7e, 0x5f, 0x4e |
| 347 | }; |
| 348 | uint16_t mlen = M_LEN10; |
| 349 | |
| 350 | TC_PRINT("%s: Performing CCM test #6 (RFC 3610 test vector #9):\n", |
| 351 | __func__); |
| 352 | |
| 353 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 354 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 355 | |
| 356 | return result; |
| 357 | } |
| 358 | |
| 359 | int test_vector_7(void) |
| 360 | { |
| 361 | int result = TC_PASS; |
| 362 | /* Test based on RFC 3610 test vector #9 but with no associated data */ |
| 363 | const uint8_t key[NUM_NIST_KEYS] = { |
| 364 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 365 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 366 | }; |
| 367 | uint8_t nonce[NONCE_LEN] = { |
| 368 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 369 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 370 | }; |
| 371 | uint8_t *hdr = NULL; |
| 372 | |
| 373 | uint8_t data[DATA_BUF_LEN25] = { |
| 374 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 375 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 376 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 377 | 0x20 |
| 378 | }; |
| 379 | struct tc_ccm_mode_struct c; |
| 380 | struct tc_aes_key_sched_struct sched; |
| 381 | uint8_t decrypted[DECRYPTED_LEN]; |
| 382 | uint8_t ciphertext[CIPHERTEXT_LEN]; |
| 383 | uint16_t mlen = M_LEN10; |
| 384 | |
| 385 | TC_PRINT("%s: Performing CCM test #7 (no associated data):\n", |
| 386 | __func__); |
| 387 | |
| 388 | tc_aes128_set_encrypt_key(&sched, key); |
| 389 | if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) { |
| 390 | TC_ERROR("ccm_config failed in %s.\n", __func__); |
| 391 | |
| 392 | result = TC_FAIL; |
| 393 | goto exitTest1; |
| 394 | } |
| 395 | |
| 396 | result = tc_ccm_generation_encryption(ciphertext, hdr, 0, |
| 397 | data, sizeof(data), &c); |
| 398 | if (result == 0) { |
| 399 | TC_ERROR("ccm_encryption failed in %s.\n", __func__); |
| 400 | |
| 401 | result = TC_FAIL; |
| 402 | goto exitTest1; |
| 403 | } |
| 404 | |
| 405 | result = tc_ccm_decryption_verification(decrypted, hdr, 0, ciphertext, |
| 406 | sizeof(data) + mlen, &c); |
| 407 | if (result == 0) { |
| 408 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 409 | show_str("\t\tExpected", data, sizeof(data)); |
| 410 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 411 | |
| 412 | result = TC_FAIL; |
| 413 | goto exitTest1; |
| 414 | } |
| 415 | |
| 416 | result = TC_PASS; |
| 417 | |
| 418 | exitTest1: |
| 419 | TC_END_RESULT(result); |
| 420 | return result; |
| 421 | |
| 422 | } |
| 423 | |
| 424 | int test_vector_8(void) |
| 425 | { |
| 426 | int result = TC_PASS; |
| 427 | /* Test based on RFC 3610 test vector #9 but with no payload data */ |
| 428 | const uint8_t key[NUM_NIST_KEYS] = { |
| 429 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 430 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 431 | }; |
| 432 | uint8_t nonce[NONCE_LEN] = { |
| 433 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 434 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 435 | }; |
| 436 | const uint8_t hdr[8] = { |
| 437 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 438 | }; |
| 439 | uint8_t data[] = {}; |
| 440 | struct tc_ccm_mode_struct c; |
| 441 | struct tc_aes_key_sched_struct sched; |
| 442 | uint8_t decrypted[DECRYPTED_LEN]; |
| 443 | uint8_t ciphertext[CIPHERTEXT_LEN]; |
| 444 | uint16_t mlen = M_LEN10; |
| 445 | |
| 446 | TC_PRINT("%s: Performing CCM test #8 (no payload data):\n", __func__); |
| 447 | |
| 448 | tc_aes128_set_encrypt_key(&sched, key); |
| 449 | if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) { |
| 450 | TC_ERROR("CCM config failed in %s.\n", __func__); |
| 451 | |
| 452 | result = TC_FAIL; |
| 453 | goto exitTest1; |
| 454 | } |
| 455 | |
| 456 | result = tc_ccm_generation_encryption(ciphertext, hdr, sizeof(hdr), |
| 457 | data, sizeof(data), &c); |
| 458 | if (result == 0) { |
| 459 | TC_ERROR("ccm_encrypt failed in %s.\n", __func__); |
| 460 | |
| 461 | result = TC_FAIL; |
| 462 | goto exitTest1; |
| 463 | } |
| 464 | |
| 465 | result = tc_ccm_decryption_verification(decrypted, hdr, sizeof(hdr), |
| 466 | ciphertext, mlen, &c); |
| 467 | if (result == 0) { |
| 468 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 469 | show_str("\t\tExpected", data, sizeof(data)); |
| 470 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 471 | |
| 472 | result = TC_FAIL; |
| 473 | goto exitTest1; |
| 474 | } |
| 475 | |
| 476 | result = TC_PASS; |
| 477 | |
| 478 | exitTest1: |
| 479 | TC_END_RESULT(result); |
| 480 | return result; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Main task to test CCM |
| 485 | */ |
| 486 | int main(void) |
| 487 | { |
| 488 | int result = TC_PASS; |
| 489 | |
| 490 | TC_START("Performing CCM tests:"); |
| 491 | |
| 492 | result = test_vector_1(); |
| 493 | if (result == TC_FAIL) { /* terminate test */ |
| 494 | TC_ERROR("CCM test #1 (RFC 3610 test vector #1) failed.\n"); |
| 495 | goto exitTest; |
| 496 | } |
| 497 | result = test_vector_2(); |
| 498 | if (result == TC_FAIL) { /* terminate test */ |
| 499 | TC_ERROR("CCM test #2 failed.\n"); |
| 500 | goto exitTest; |
| 501 | } |
| 502 | result = test_vector_3(); |
| 503 | if (result == TC_FAIL) { /* terminate test */ |
| 504 | TC_ERROR("CCM test #3 failed.\n"); |
| 505 | goto exitTest; |
| 506 | } |
| 507 | result = test_vector_4(); |
| 508 | if (result == TC_FAIL) { /* terminate test */ |
| 509 | TC_ERROR("CCM test #4 failed.\n"); |
| 510 | goto exitTest; |
| 511 | } |
| 512 | result = test_vector_5(); |
| 513 | if (result == TC_FAIL) { /* terminate test */ |
| 514 | TC_ERROR("CCM test #5 failed.\n"); |
| 515 | goto exitTest; |
| 516 | } |
| 517 | result = test_vector_6(); |
| 518 | if (result == TC_FAIL) { /* terminate test */ |
| 519 | TC_ERROR("CCM test #6 failed.\n"); |
| 520 | goto exitTest; |
| 521 | } |
| 522 | result = test_vector_7(); |
| 523 | if (result == TC_FAIL) { /* terminate test */ |
| 524 | TC_ERROR("CCM test #7 failed.\n"); |
| 525 | goto exitTest; |
| 526 | } |
| 527 | result = test_vector_8(); |
| 528 | if (result == TC_FAIL) { /* terminate test */ |
| 529 | TC_ERROR("CCM test #8 (no payload data) failed.\n"); |
| 530 | goto exitTest; |
| 531 | } |
| 532 | |
| 533 | TC_PRINT("All CCM tests succeeded!\n"); |
| 534 | |
| 535 | exitTest: |
| 536 | TC_END_RESULT(result); |
| 537 | TC_END_REPORT(result); |
| 538 | |
| 539 | return result; |
| 540 | } |