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 | /* |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 4 | * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 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 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 54 | #define TC_CCM_MAX_CT_SIZE 50 |
| 55 | #define TC_CCM_MAX_PT_SIZE 25 |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 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 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 70 | int do_test(const uint8_t *key, uint8_t *nonce, |
| 71 | size_t nlen, const uint8_t *hdr, |
| 72 | size_t hlen, const uint8_t *data, |
| 73 | size_t dlen, const uint8_t *expected, |
| 74 | size_t elen, const int mlen) |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 75 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 76 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 77 | int result = TC_PASS; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 78 | |
| 79 | uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; |
| 80 | uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 81 | struct tc_ccm_mode_struct c; |
| 82 | struct tc_aes_key_sched_struct sched; |
| 83 | |
| 84 | tc_aes128_set_encrypt_key(&sched, key); |
| 85 | |
| 86 | result = tc_ccm_config(&c, &sched, nonce, nlen, mlen); |
| 87 | if (result == 0) { |
| 88 | TC_ERROR("CCM config failed in %s.\n", __func__); |
| 89 | |
| 90 | result = TC_FAIL; |
| 91 | goto exitTest1; |
| 92 | } |
| 93 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 94 | result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, |
| 95 | hlen, data, dlen, &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 96 | if (result == 0) { |
| 97 | TC_ERROR("ccm_encrypt failed in %s.\n", __func__); |
| 98 | |
| 99 | result = TC_FAIL; |
| 100 | goto exitTest1; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | if (memcmp(expected, ciphertext, elen) != 0) { |
| 105 | TC_ERROR("ccm_encrypt produced wrong ciphertext in %s.\n", |
| 106 | __func__); |
| 107 | show_str("\t\tExpected", expected, elen); |
| 108 | show_str("\t\tComputed", ciphertext, elen); |
| 109 | |
| 110 | result = TC_FAIL; |
| 111 | goto exitTest1; |
| 112 | } |
| 113 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 114 | result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr, |
| 115 | hlen, ciphertext, dlen+mlen, &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 116 | if (result == 0) { |
| 117 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 118 | show_str("\t\tExpected", data, dlen); |
| 119 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 120 | |
| 121 | result = TC_FAIL; |
| 122 | goto exitTest1; |
| 123 | } |
| 124 | |
| 125 | result = TC_PASS; |
| 126 | |
| 127 | exitTest1: |
| 128 | TC_END_RESULT(result); |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | int test_vector_1(void) |
| 133 | { |
| 134 | int result = TC_PASS; |
| 135 | /* RFC 3610 test vector #1 */ |
| 136 | const uint8_t key[NUM_NIST_KEYS] = { |
| 137 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 138 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 139 | }; |
| 140 | uint8_t nonce[NONCE_LEN] = { |
| 141 | 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, |
| 142 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 143 | }; |
| 144 | const uint8_t hdr[HEADER_LEN] = { |
| 145 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 146 | }; |
| 147 | const uint8_t data[DATA_BUF_LEN23] = { |
| 148 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 149 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 150 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e |
| 151 | }; |
| 152 | const uint8_t expected[EXPECTED_BUF_LEN31] = { |
| 153 | 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, |
| 154 | 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80, |
| 155 | 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17, |
| 156 | 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 |
| 157 | }; |
| 158 | uint16_t mlen = M_LEN8; |
| 159 | |
| 160 | TC_PRINT("%s: Performing CCM test #1 (RFC 3610 test vector #1):\n", |
| 161 | __func__); |
| 162 | |
| 163 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 164 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 165 | |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | int test_vector_2(void) |
| 170 | { |
| 171 | int result = TC_PASS; |
| 172 | /* RFC 3610 test vector #2 */ |
| 173 | const uint8_t key[NUM_NIST_KEYS] = { |
| 174 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 175 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 176 | }; |
| 177 | uint8_t nonce[NONCE_LEN] = { |
| 178 | 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01, 0xa0, |
| 179 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 180 | }; |
| 181 | const uint8_t hdr[HEADER_LEN] = { |
| 182 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 183 | }; |
| 184 | const uint8_t data[DATA_BUF_LEN24] = { |
| 185 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 186 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 187 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 188 | }; |
| 189 | const uint8_t expected[EXPECTED_BUF_LEN32] = { |
| 190 | 0x72, 0xc9, 0x1a, 0x36, 0xe1, 0x35, 0xf8, 0xcf, |
| 191 | 0x29, 0x1c, 0xa8, 0x94, 0x08, 0x5c, 0x87, 0xe3, |
| 192 | 0xcc, 0x15, 0xc4, 0x39, 0xc9, 0xe4, 0x3a, 0x3b, |
| 193 | 0xa0, 0x91, 0xd5, 0x6e, 0x10, 0x40, 0x09, 0x16 |
| 194 | }; |
| 195 | uint16_t mlen = M_LEN8; |
| 196 | |
| 197 | TC_PRINT("%s: Performing CCM test #2 (RFC 3610 test vector #2):\n", |
| 198 | __func__); |
| 199 | |
| 200 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 201 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 202 | |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | int test_vector_3(void) |
| 207 | { |
| 208 | int result = TC_PASS; |
| 209 | /* RFC 3610 test vector #3 */ |
| 210 | const uint8_t key[NUM_NIST_KEYS] = { |
| 211 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 212 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 213 | }; |
| 214 | uint8_t nonce[NONCE_LEN] = { |
| 215 | 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x02, 0xa0, |
| 216 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 217 | }; |
| 218 | const uint8_t hdr[HEADER_LEN] = { |
| 219 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 220 | }; |
| 221 | const uint8_t data[DATA_BUF_LEN25] = { |
| 222 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 223 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 224 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 225 | 0x20 |
| 226 | }; |
| 227 | const uint8_t expected[EXPECTED_BUF_LEN33] = { |
| 228 | 0x51, 0xb1, 0xe5, 0xf4, 0x4a, 0x19, 0x7d, 0x1d, |
| 229 | 0xa4, 0x6b, 0x0f, 0x8e, 0x2d, 0x28, 0x2a, 0xe8, |
| 230 | 0x71, 0xe8, 0x38, 0xbb, 0x64, 0xda, 0x85, 0x96, |
| 231 | 0x57, 0x4a, 0xda, 0xa7, 0x6f, 0xbd, 0x9f, 0xb0, |
| 232 | 0xc5 |
| 233 | }; |
| 234 | uint16_t mlen = M_LEN8; |
| 235 | |
| 236 | TC_PRINT("%s: Performing CCM test #3 (RFC 3610 test vector #3):\n", |
| 237 | __func__); |
| 238 | |
| 239 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), data, |
| 240 | sizeof(data), expected, sizeof(expected), mlen); |
| 241 | |
| 242 | return result; |
| 243 | } |
| 244 | |
| 245 | int test_vector_4(void) |
| 246 | { |
| 247 | int result = TC_PASS; |
| 248 | /* RFC 3610 test vector #7 */ |
| 249 | const uint8_t key[NUM_NIST_KEYS] = { |
| 250 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 251 | 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf |
| 252 | }; |
| 253 | uint8_t nonce[NONCE_LEN] = { |
| 254 | 0x00, 0x00, 0x00, 0x09, 0x08, 0x07, 0x06, 0xa0, |
| 255 | 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 |
| 256 | }; |
| 257 | const uint8_t hdr[HEADER_LEN] = { |
| 258 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 259 | }; |
| 260 | const uint8_t data[DATA_BUF_LEN23] = { |
| 261 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 262 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 263 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e |
| 264 | }; |
| 265 | const uint8_t expected[EXPECTED_BUF_LEN33] = { |
| 266 | 0x01, 0x35, 0xD1, 0xB2, 0xC9, 0x5F, 0x41, 0xD5, |
| 267 | 0xD1, 0xD4, 0xFE, 0xC1, 0x85, 0xD1, 0x66, 0xB8, |
| 268 | 0x09, 0x4E, 0x99, 0x9D, 0xFE, 0xD9, 0x6C, 0x04, |
| 269 | 0x8C, 0x56, 0x60, 0x2C, 0x97, 0xAC, 0xBB, 0x74, |
| 270 | 0x90 |
| 271 | }; |
| 272 | uint16_t mlen = M_LEN10; |
| 273 | |
| 274 | TC_PRINT("%s: Performing CCM test #4 (RFC 3610 test vector #7):\n", |
| 275 | __func__); |
| 276 | |
| 277 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 278 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 279 | |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | int test_vector_5(void) |
| 284 | { |
| 285 | int result = TC_PASS; |
| 286 | /* RFC 3610 test vector #8 */ |
| 287 | const uint8_t key[NUM_NIST_KEYS] = { |
| 288 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 289 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 290 | }; |
| 291 | uint8_t nonce[NONCE_LEN] = { |
| 292 | 0x00, 0x00, 0x00, 0x0A, 0x09, 0x08, 0x07, 0xA0, |
| 293 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 294 | }; |
| 295 | const uint8_t hdr[HEADER_LEN] = { |
| 296 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 297 | }; |
| 298 | const uint8_t data[DATA_BUF_LEN24] = { |
| 299 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 300 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 301 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 302 | }; |
| 303 | const uint8_t expected[EXPECTED_BUF_LEN34] = { |
| 304 | 0x7B, 0x75, 0x39, 0x9A, 0xC0, 0x83, 0x1D, 0xD2, |
| 305 | 0xF0, 0xBB, 0xD7, 0x58, 0x79, 0xA2, 0xFD, 0x8F, |
| 306 | 0x6C, 0xAE, 0x6B, 0x6C, 0xD9, 0xB7, 0xDB, 0x24, |
| 307 | 0xC1, 0x7B, 0x44, 0x33, 0xF4, 0x34, 0x96, 0x3F, |
| 308 | 0x34, 0xB4 |
| 309 | }; |
| 310 | uint16_t mlen = M_LEN10; |
| 311 | |
| 312 | TC_PRINT("%s: Performing CCM test #5 (RFC 3610 test vector #8):\n", |
| 313 | __func__); |
| 314 | |
| 315 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 316 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 317 | |
| 318 | return result; |
| 319 | } |
| 320 | |
| 321 | int test_vector_6(void) |
| 322 | { |
| 323 | int result = TC_PASS; |
| 324 | /* RFC 3610 test vector #9 */ |
| 325 | const uint8_t key[NUM_NIST_KEYS] = { |
| 326 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 327 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 328 | }; |
| 329 | uint8_t nonce[NONCE_LEN] = { |
| 330 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 331 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 332 | }; |
| 333 | const uint8_t hdr[HEADER_LEN] = { |
| 334 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 335 | }; |
| 336 | const uint8_t data[DATA_BUF_LEN25] = { |
| 337 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 338 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 339 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 340 | 0x20 |
| 341 | }; |
| 342 | const uint8_t expected[EXPECTED_BUF_LEN35] = { |
| 343 | 0x82, 0x53, 0x1a, 0x60, 0xCC, 0x24, 0x94, 0x5a, |
| 344 | 0x4b, 0x82, 0x79, 0x18, 0x1a, 0xb5, 0xc8, 0x4d, |
| 345 | 0xf2, 0x1c, 0xe7, 0xf9, 0xb7, 0x3f, 0x42, 0xe1, |
| 346 | 0x97, 0xea, 0x9c, 0x07, 0xe5, 0x6b, 0x5e, 0xb1, |
| 347 | 0x7e, 0x5f, 0x4e |
| 348 | }; |
| 349 | uint16_t mlen = M_LEN10; |
| 350 | |
| 351 | TC_PRINT("%s: Performing CCM test #6 (RFC 3610 test vector #9):\n", |
| 352 | __func__); |
| 353 | |
| 354 | result = do_test(key, nonce, sizeof(nonce), hdr, sizeof(hdr), |
| 355 | data, sizeof(data), expected, sizeof(expected), mlen); |
| 356 | |
| 357 | return result; |
| 358 | } |
| 359 | |
| 360 | int test_vector_7(void) |
| 361 | { |
| 362 | int result = TC_PASS; |
| 363 | /* Test based on RFC 3610 test vector #9 but with no associated data */ |
| 364 | const uint8_t key[NUM_NIST_KEYS] = { |
| 365 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 366 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 367 | }; |
| 368 | uint8_t nonce[NONCE_LEN] = { |
| 369 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 370 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 371 | }; |
| 372 | uint8_t *hdr = NULL; |
| 373 | |
| 374 | uint8_t data[DATA_BUF_LEN25] = { |
| 375 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 376 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 377 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 378 | 0x20 |
| 379 | }; |
| 380 | struct tc_ccm_mode_struct c; |
| 381 | struct tc_aes_key_sched_struct sched; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 382 | uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; |
| 383 | uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 384 | uint16_t mlen = M_LEN10; |
| 385 | |
| 386 | TC_PRINT("%s: Performing CCM test #7 (no associated data):\n", |
| 387 | __func__); |
| 388 | |
| 389 | tc_aes128_set_encrypt_key(&sched, key); |
| 390 | if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) { |
| 391 | TC_ERROR("ccm_config failed in %s.\n", __func__); |
| 392 | |
| 393 | result = TC_FAIL; |
| 394 | goto exitTest1; |
| 395 | } |
| 396 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 397 | result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, |
| 398 | 0, data, sizeof(data), &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 399 | if (result == 0) { |
| 400 | TC_ERROR("ccm_encryption failed in %s.\n", __func__); |
| 401 | |
| 402 | result = TC_FAIL; |
| 403 | goto exitTest1; |
| 404 | } |
| 405 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 406 | result = tc_ccm_decryption_verification (decrypted, TC_CCM_MAX_PT_SIZE, hdr, |
| 407 | 0, ciphertext, sizeof(data)+mlen, &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 408 | if (result == 0) { |
| 409 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 410 | show_str("\t\tExpected", data, sizeof(data)); |
| 411 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 412 | |
| 413 | result = TC_FAIL; |
| 414 | goto exitTest1; |
| 415 | } |
| 416 | |
| 417 | result = TC_PASS; |
| 418 | |
| 419 | exitTest1: |
| 420 | TC_END_RESULT(result); |
| 421 | return result; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | int test_vector_8(void) |
| 426 | { |
| 427 | int result = TC_PASS; |
| 428 | /* Test based on RFC 3610 test vector #9 but with no payload data */ |
| 429 | const uint8_t key[NUM_NIST_KEYS] = { |
| 430 | 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, |
| 431 | 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF |
| 432 | }; |
| 433 | uint8_t nonce[NONCE_LEN] = { |
| 434 | 0x00, 0x00, 0x00, 0x0B, 0x0A, 0x09, 0x08, 0xA0, |
| 435 | 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 |
| 436 | }; |
| 437 | const uint8_t hdr[8] = { |
| 438 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 |
| 439 | }; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 440 | |
| 441 | uint8_t *data = NULL; |
| 442 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 443 | struct tc_ccm_mode_struct c; |
| 444 | struct tc_aes_key_sched_struct sched; |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 445 | |
| 446 | uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; |
| 447 | uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; |
| 448 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 449 | uint16_t mlen = M_LEN10; |
| 450 | |
| 451 | TC_PRINT("%s: Performing CCM test #8 (no payload data):\n", __func__); |
| 452 | |
| 453 | tc_aes128_set_encrypt_key(&sched, key); |
| 454 | if (tc_ccm_config(&c, &sched, nonce, sizeof(nonce), mlen) == 0) { |
| 455 | TC_ERROR("CCM config failed in %s.\n", __func__); |
| 456 | |
| 457 | result = TC_FAIL; |
| 458 | goto exitTest1; |
| 459 | } |
| 460 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 461 | result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, |
| 462 | sizeof(hdr), data, 0, &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 463 | if (result == 0) { |
| 464 | TC_ERROR("ccm_encrypt failed in %s.\n", __func__); |
| 465 | |
| 466 | result = TC_FAIL; |
| 467 | goto exitTest1; |
| 468 | } |
| 469 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 470 | result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr, |
| 471 | sizeof(hdr), ciphertext, mlen, &c); |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 472 | if (result == 0) { |
| 473 | TC_ERROR("ccm_decrypt failed in %s.\n", __func__); |
| 474 | show_str("\t\tExpected", data, sizeof(data)); |
| 475 | show_str("\t\tComputed", decrypted, sizeof(decrypted)); |
| 476 | |
| 477 | result = TC_FAIL; |
| 478 | goto exitTest1; |
| 479 | } |
| 480 | |
| 481 | result = TC_PASS; |
| 482 | |
| 483 | exitTest1: |
| 484 | TC_END_RESULT(result); |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Main task to test CCM |
| 490 | */ |
| 491 | int main(void) |
| 492 | { |
| 493 | int result = TC_PASS; |
| 494 | |
| 495 | TC_START("Performing CCM tests:"); |
| 496 | |
| 497 | result = test_vector_1(); |
| 498 | if (result == TC_FAIL) { /* terminate test */ |
| 499 | TC_ERROR("CCM test #1 (RFC 3610 test vector #1) failed.\n"); |
| 500 | goto exitTest; |
| 501 | } |
| 502 | result = test_vector_2(); |
| 503 | if (result == TC_FAIL) { /* terminate test */ |
| 504 | TC_ERROR("CCM test #2 failed.\n"); |
| 505 | goto exitTest; |
| 506 | } |
| 507 | result = test_vector_3(); |
| 508 | if (result == TC_FAIL) { /* terminate test */ |
| 509 | TC_ERROR("CCM test #3 failed.\n"); |
| 510 | goto exitTest; |
| 511 | } |
| 512 | result = test_vector_4(); |
| 513 | if (result == TC_FAIL) { /* terminate test */ |
| 514 | TC_ERROR("CCM test #4 failed.\n"); |
| 515 | goto exitTest; |
| 516 | } |
| 517 | result = test_vector_5(); |
| 518 | if (result == TC_FAIL) { /* terminate test */ |
| 519 | TC_ERROR("CCM test #5 failed.\n"); |
| 520 | goto exitTest; |
| 521 | } |
| 522 | result = test_vector_6(); |
| 523 | if (result == TC_FAIL) { /* terminate test */ |
| 524 | TC_ERROR("CCM test #6 failed.\n"); |
| 525 | goto exitTest; |
| 526 | } |
| 527 | result = test_vector_7(); |
| 528 | if (result == TC_FAIL) { /* terminate test */ |
| 529 | TC_ERROR("CCM test #7 failed.\n"); |
| 530 | goto exitTest; |
| 531 | } |
| 532 | result = test_vector_8(); |
| 533 | if (result == TC_FAIL) { /* terminate test */ |
| 534 | TC_ERROR("CCM test #8 (no payload data) failed.\n"); |
| 535 | goto exitTest; |
| 536 | } |
| 537 | |
| 538 | TC_PRINT("All CCM tests succeeded!\n"); |
| 539 | |
| 540 | exitTest: |
| 541 | TC_END_RESULT(result); |
| 542 | TC_END_REPORT(result); |
| 543 | |
| 544 | return result; |
| 545 | } |