/src/mbedtls/library/ccm.c
Line | Count | Source |
1 | | /* |
2 | | * NIST SP800-38C compliant CCM implementation |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
6 | | */ |
7 | | |
8 | | /* |
9 | | * Definition of CCM: |
10 | | * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf |
11 | | * RFC 3610 "Counter with CBC-MAC (CCM)" |
12 | | * |
13 | | * Related: |
14 | | * RFC 5116 "An Interface and Algorithms for Authenticated Encryption" |
15 | | */ |
16 | | |
17 | | #include "common.h" |
18 | | |
19 | | #if defined(MBEDTLS_CCM_C) |
20 | | |
21 | | #include "mbedtls/ccm.h" |
22 | | #include "mbedtls/platform_util.h" |
23 | | #include "mbedtls/error.h" |
24 | | #include "mbedtls/constant_time.h" |
25 | | |
26 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
27 | | #include "block_cipher_internal.h" |
28 | | #endif |
29 | | |
30 | | #include <string.h> |
31 | | |
32 | | #if defined(MBEDTLS_PLATFORM_C) |
33 | | #include "mbedtls/platform.h" |
34 | | #else |
35 | | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
36 | | #include <stdio.h> |
37 | | #define mbedtls_printf printf |
38 | | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
39 | | #endif /* MBEDTLS_PLATFORM_C */ |
40 | | |
41 | | #if !defined(MBEDTLS_CCM_ALT) |
42 | | |
43 | | |
44 | | /* |
45 | | * Initialize context |
46 | | */ |
47 | | void mbedtls_ccm_init(mbedtls_ccm_context *ctx) |
48 | 160 | { |
49 | 160 | memset(ctx, 0, sizeof(mbedtls_ccm_context)); |
50 | 160 | } |
51 | | |
52 | | int mbedtls_ccm_setkey(mbedtls_ccm_context *ctx, |
53 | | mbedtls_cipher_id_t cipher, |
54 | | const unsigned char *key, |
55 | | unsigned int keybits) |
56 | 160 | { |
57 | 160 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
58 | | |
59 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
60 | | mbedtls_block_cipher_free(&ctx->block_cipher_ctx); |
61 | | |
62 | | if ((ret = mbedtls_block_cipher_setup(&ctx->block_cipher_ctx, cipher)) != 0) { |
63 | | return MBEDTLS_ERR_CCM_BAD_INPUT; |
64 | | } |
65 | | |
66 | | if ((ret = mbedtls_block_cipher_setkey(&ctx->block_cipher_ctx, key, keybits)) != 0) { |
67 | | return MBEDTLS_ERR_CCM_BAD_INPUT; |
68 | | } |
69 | | #else |
70 | 160 | const mbedtls_cipher_info_t *cipher_info; |
71 | | |
72 | 160 | cipher_info = mbedtls_cipher_info_from_values(cipher, keybits, |
73 | 160 | MBEDTLS_MODE_ECB); |
74 | 160 | if (cipher_info == NULL) { |
75 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
76 | 0 | } |
77 | | |
78 | 160 | if (mbedtls_cipher_info_get_block_size(cipher_info) != 16) { |
79 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
80 | 0 | } |
81 | | |
82 | 160 | mbedtls_cipher_free(&ctx->cipher_ctx); |
83 | | |
84 | 160 | if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) { |
85 | 0 | return ret; |
86 | 0 | } |
87 | | |
88 | 160 | if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits, |
89 | 160 | MBEDTLS_ENCRYPT)) != 0) { |
90 | 0 | return ret; |
91 | 0 | } |
92 | 160 | #endif |
93 | | |
94 | 160 | return ret; |
95 | 160 | } |
96 | | |
97 | | /* |
98 | | * Free context |
99 | | */ |
100 | | void mbedtls_ccm_free(mbedtls_ccm_context *ctx) |
101 | 160 | { |
102 | 160 | if (ctx == NULL) { |
103 | 0 | return; |
104 | 0 | } |
105 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
106 | | mbedtls_block_cipher_free(&ctx->block_cipher_ctx); |
107 | | #else |
108 | 160 | mbedtls_cipher_free(&ctx->cipher_ctx); |
109 | 160 | #endif |
110 | 160 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ccm_context)); |
111 | 160 | } |
112 | | |
113 | 601 | #define CCM_STATE__CLEAR 0 |
114 | 2.40k | #define CCM_STATE__STARTED (1 << 0) |
115 | 2.40k | #define CCM_STATE__LENGTHS_SET (1 << 1) |
116 | 1.20k | #define CCM_STATE__AUTH_DATA_STARTED (1 << 2) |
117 | 1.80k | #define CCM_STATE__AUTH_DATA_FINISHED (1 << 3) |
118 | 1.80k | #define CCM_STATE__ERROR (1 << 4) |
119 | | |
120 | | /* |
121 | | * Encrypt or decrypt a partial block with CTR |
122 | | */ |
123 | | static int mbedtls_ccm_crypt(mbedtls_ccm_context *ctx, |
124 | | size_t offset, size_t use_len, |
125 | | const unsigned char *input, |
126 | | unsigned char *output) |
127 | 6.55k | { |
128 | 6.55k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
129 | 6.55k | unsigned char tmp_buf[16] = { 0 }; |
130 | | |
131 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
132 | | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->ctr, tmp_buf); |
133 | | #else |
134 | 6.55k | size_t olen = 0; |
135 | 6.55k | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->ctr, 16, tmp_buf, &olen); |
136 | 6.55k | #endif |
137 | 6.55k | if (ret != 0) { |
138 | 0 | ctx->state |= CCM_STATE__ERROR; |
139 | 0 | mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); |
140 | 0 | return ret; |
141 | 0 | } |
142 | | |
143 | 6.55k | mbedtls_xor(output, input, tmp_buf + offset, use_len); |
144 | | |
145 | 6.55k | mbedtls_platform_zeroize(tmp_buf, sizeof(tmp_buf)); |
146 | 6.55k | return ret; |
147 | 6.55k | } |
148 | | |
149 | | static void mbedtls_ccm_clear_state(mbedtls_ccm_context *ctx) |
150 | 601 | { |
151 | 601 | ctx->state = CCM_STATE__CLEAR; |
152 | 601 | memset(ctx->y, 0, 16); |
153 | 601 | memset(ctx->ctr, 0, 16); |
154 | 601 | } |
155 | | |
156 | | static int ccm_calculate_first_block_if_ready(mbedtls_ccm_context *ctx) |
157 | 1.20k | { |
158 | 1.20k | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
159 | 1.20k | unsigned char i; |
160 | 1.20k | size_t len_left; |
161 | 1.20k | #if !defined(MBEDTLS_BLOCK_CIPHER_C) |
162 | 1.20k | size_t olen; |
163 | 1.20k | #endif |
164 | | |
165 | | /* length calculation can be done only after both |
166 | | * mbedtls_ccm_starts() and mbedtls_ccm_set_lengths() have been executed |
167 | | */ |
168 | 1.20k | if (!(ctx->state & CCM_STATE__STARTED) || !(ctx->state & CCM_STATE__LENGTHS_SET)) { |
169 | 601 | return 0; |
170 | 601 | } |
171 | | |
172 | | /* CCM expects non-empty tag. |
173 | | * CCM* allows empty tag. For CCM* without tag, the tag calculation is skipped. |
174 | | */ |
175 | 601 | if (ctx->tag_len == 0) { |
176 | 0 | if (ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT || ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) { |
177 | 0 | ctx->plaintext_len = 0; |
178 | 0 | return 0; |
179 | 0 | } else { |
180 | 0 | ctx->state |= CCM_STATE__ERROR; |
181 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * First block: |
187 | | * 0 .. 0 flags |
188 | | * 1 .. iv_len nonce (aka iv) - set by: mbedtls_ccm_starts() |
189 | | * iv_len+1 .. 15 length |
190 | | * |
191 | | * With flags as (bits): |
192 | | * 7 0 |
193 | | * 6 add present? |
194 | | * 5 .. 3 (t - 2) / 2 |
195 | | * 2 .. 0 q - 1 |
196 | | */ |
197 | 601 | ctx->y[0] |= (ctx->add_len > 0) << 6; |
198 | 601 | ctx->y[0] |= ((ctx->tag_len - 2) / 2) << 3; |
199 | 601 | ctx->y[0] |= ctx->q - 1; |
200 | | |
201 | 2.40k | for (i = 0, len_left = ctx->plaintext_len; i < ctx->q; i++, len_left >>= 8) { |
202 | 1.80k | ctx->y[15-i] = MBEDTLS_BYTE_0(len_left); |
203 | 1.80k | } |
204 | | |
205 | 601 | if (len_left > 0) { |
206 | 0 | ctx->state |= CCM_STATE__ERROR; |
207 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
208 | 0 | } |
209 | | |
210 | | /* Start CBC-MAC with first block*/ |
211 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
212 | | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
213 | | #else |
214 | 601 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
215 | 601 | #endif |
216 | 601 | if (ret != 0) { |
217 | 0 | ctx->state |= CCM_STATE__ERROR; |
218 | 0 | return ret; |
219 | 0 | } |
220 | | |
221 | 601 | return 0; |
222 | 601 | } |
223 | | |
224 | | int mbedtls_ccm_starts(mbedtls_ccm_context *ctx, |
225 | | int mode, |
226 | | const unsigned char *iv, |
227 | | size_t iv_len) |
228 | 601 | { |
229 | | /* Also implies q is within bounds */ |
230 | 601 | if (iv_len < 7 || iv_len > 13) { |
231 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
232 | 0 | } |
233 | | |
234 | 601 | ctx->mode = mode; |
235 | 601 | ctx->q = 16 - 1 - (unsigned char) iv_len; |
236 | | |
237 | | /* |
238 | | * Prepare counter block for encryption: |
239 | | * 0 .. 0 flags |
240 | | * 1 .. iv_len nonce (aka iv) |
241 | | * iv_len+1 .. 15 counter (initially 1) |
242 | | * |
243 | | * With flags as (bits): |
244 | | * 7 .. 3 0 |
245 | | * 2 .. 0 q - 1 |
246 | | */ |
247 | 601 | memset(ctx->ctr, 0, 16); |
248 | 601 | ctx->ctr[0] = ctx->q - 1; |
249 | 601 | memcpy(ctx->ctr + 1, iv, iv_len); |
250 | 601 | memset(ctx->ctr + 1 + iv_len, 0, ctx->q); |
251 | 601 | ctx->ctr[15] = 1; |
252 | | |
253 | | /* |
254 | | * See ccm_calculate_first_block_if_ready() for block layout description |
255 | | */ |
256 | 601 | memcpy(ctx->y + 1, iv, iv_len); |
257 | | |
258 | 601 | ctx->state |= CCM_STATE__STARTED; |
259 | 601 | return ccm_calculate_first_block_if_ready(ctx); |
260 | 601 | } |
261 | | |
262 | | int mbedtls_ccm_set_lengths(mbedtls_ccm_context *ctx, |
263 | | size_t total_ad_len, |
264 | | size_t plaintext_len, |
265 | | size_t tag_len) |
266 | 601 | { |
267 | | /* |
268 | | * Check length requirements: SP800-38C A.1 |
269 | | * Additional requirement: a < 2^16 - 2^8 to simplify the code. |
270 | | * 'length' checked later (when writing it to the first block) |
271 | | * |
272 | | * Also, loosen the requirements to enable support for CCM* (IEEE 802.15.4). |
273 | | */ |
274 | 601 | if (tag_len == 2 || tag_len > 16 || tag_len % 2 != 0) { |
275 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
276 | 0 | } |
277 | | |
278 | 601 | if (total_ad_len >= 0xFF00) { |
279 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
280 | 0 | } |
281 | | |
282 | 601 | ctx->plaintext_len = plaintext_len; |
283 | 601 | ctx->add_len = total_ad_len; |
284 | 601 | ctx->tag_len = tag_len; |
285 | 601 | ctx->processed = 0; |
286 | | |
287 | 601 | ctx->state |= CCM_STATE__LENGTHS_SET; |
288 | 601 | return ccm_calculate_first_block_if_ready(ctx); |
289 | 601 | } |
290 | | |
291 | | int mbedtls_ccm_update_ad(mbedtls_ccm_context *ctx, |
292 | | const unsigned char *add, |
293 | | size_t add_len) |
294 | 601 | { |
295 | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
296 | 601 | size_t use_len, offset; |
297 | 601 | #if !defined(MBEDTLS_BLOCK_CIPHER_C) |
298 | 601 | size_t olen; |
299 | 601 | #endif |
300 | | |
301 | 601 | if (ctx->state & CCM_STATE__ERROR) { |
302 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
303 | 0 | } |
304 | | |
305 | 601 | if (add_len > 0) { |
306 | 601 | if (ctx->state & CCM_STATE__AUTH_DATA_FINISHED) { |
307 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
308 | 0 | } |
309 | | |
310 | 601 | if (!(ctx->state & CCM_STATE__AUTH_DATA_STARTED)) { |
311 | 601 | if (add_len > ctx->add_len) { |
312 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
313 | 0 | } |
314 | | |
315 | 601 | ctx->y[0] ^= (unsigned char) ((ctx->add_len >> 8) & 0xFF); |
316 | 601 | ctx->y[1] ^= (unsigned char) ((ctx->add_len) & 0xFF); |
317 | | |
318 | 601 | ctx->state |= CCM_STATE__AUTH_DATA_STARTED; |
319 | 601 | } else if (ctx->processed + add_len > ctx->add_len) { |
320 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
321 | 0 | } |
322 | | |
323 | 1.20k | while (add_len > 0) { |
324 | 601 | offset = (ctx->processed + 2) % 16; /* account for y[0] and y[1] |
325 | | * holding total auth data length */ |
326 | 601 | use_len = 16 - offset; |
327 | | |
328 | 601 | if (use_len > add_len) { |
329 | 601 | use_len = add_len; |
330 | 601 | } |
331 | | |
332 | 601 | mbedtls_xor(ctx->y + offset, ctx->y + offset, add, use_len); |
333 | | |
334 | 601 | ctx->processed += use_len; |
335 | 601 | add_len -= use_len; |
336 | 601 | add += use_len; |
337 | | |
338 | 601 | if (use_len + offset == 16 || ctx->processed == ctx->add_len) { |
339 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
340 | | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
341 | | #else |
342 | 601 | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
343 | 601 | #endif |
344 | 601 | if (ret != 0) { |
345 | 0 | ctx->state |= CCM_STATE__ERROR; |
346 | 0 | return ret; |
347 | 0 | } |
348 | 601 | } |
349 | 601 | } |
350 | | |
351 | 601 | if (ctx->processed == ctx->add_len) { |
352 | 601 | ctx->state |= CCM_STATE__AUTH_DATA_FINISHED; |
353 | 601 | ctx->processed = 0; // prepare for mbedtls_ccm_update() |
354 | 601 | } |
355 | 601 | } |
356 | | |
357 | 601 | return 0; |
358 | 601 | } |
359 | | |
360 | | int mbedtls_ccm_update(mbedtls_ccm_context *ctx, |
361 | | const unsigned char *input, size_t input_len, |
362 | | unsigned char *output, size_t output_size, |
363 | | size_t *output_len) |
364 | 601 | { |
365 | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
366 | 601 | unsigned char i; |
367 | 601 | size_t use_len, offset; |
368 | 601 | #if !defined(MBEDTLS_BLOCK_CIPHER_C) |
369 | 601 | size_t olen; |
370 | 601 | #endif |
371 | | |
372 | 601 | unsigned char local_output[16]; |
373 | | |
374 | 601 | if (ctx->state & CCM_STATE__ERROR) { |
375 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
376 | 0 | } |
377 | | |
378 | | /* Check against plaintext length only if performing operation with |
379 | | * authentication |
380 | | */ |
381 | 601 | if (ctx->tag_len != 0 && ctx->processed + input_len > ctx->plaintext_len) { |
382 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
383 | 0 | } |
384 | | |
385 | 601 | if (output_size < input_len) { |
386 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
387 | 0 | } |
388 | 601 | *output_len = input_len; |
389 | | |
390 | 601 | ret = 0; |
391 | | |
392 | 6.55k | while (input_len > 0) { |
393 | 5.95k | offset = ctx->processed % 16; |
394 | | |
395 | 5.95k | use_len = 16 - offset; |
396 | | |
397 | 5.95k | if (use_len > input_len) { |
398 | 595 | use_len = input_len; |
399 | 595 | } |
400 | | |
401 | 5.95k | ctx->processed += use_len; |
402 | | |
403 | 5.95k | if (ctx->mode == MBEDTLS_CCM_ENCRYPT || \ |
404 | 4.86k | ctx->mode == MBEDTLS_CCM_STAR_ENCRYPT) { |
405 | 1.08k | mbedtls_xor(ctx->y + offset, ctx->y + offset, input, use_len); |
406 | | |
407 | 1.08k | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
408 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
409 | | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
410 | | #else |
411 | 1.08k | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
412 | 1.08k | #endif |
413 | 1.08k | if (ret != 0) { |
414 | 0 | ctx->state |= CCM_STATE__ERROR; |
415 | 0 | goto exit; |
416 | 0 | } |
417 | 1.08k | } |
418 | | |
419 | 1.08k | ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, output); |
420 | 1.08k | if (ret != 0) { |
421 | 0 | goto exit; |
422 | 0 | } |
423 | 1.08k | } |
424 | | |
425 | 5.95k | if (ctx->mode == MBEDTLS_CCM_DECRYPT || \ |
426 | 4.86k | ctx->mode == MBEDTLS_CCM_STAR_DECRYPT) { |
427 | | /* Since output may be in shared memory, we cannot be sure that |
428 | | * it will contain what we wrote to it. Therefore, we should avoid using |
429 | | * it as input to any operations. |
430 | | * Write decrypted data to local_output to avoid using output variable as |
431 | | * input in the XOR operation for Y. |
432 | | */ |
433 | 4.86k | ret = mbedtls_ccm_crypt(ctx, offset, use_len, input, local_output); |
434 | 4.86k | if (ret != 0) { |
435 | 0 | goto exit; |
436 | 0 | } |
437 | | |
438 | 4.86k | mbedtls_xor(ctx->y + offset, ctx->y + offset, local_output, use_len); |
439 | | |
440 | 4.86k | memcpy(output, local_output, use_len); |
441 | | |
442 | 4.86k | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
443 | | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
444 | | ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, ctx->y, ctx->y); |
445 | | #else |
446 | 4.86k | ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ctx->y, &olen); |
447 | 4.86k | #endif |
448 | 4.86k | if (ret != 0) { |
449 | 0 | ctx->state |= CCM_STATE__ERROR; |
450 | 0 | goto exit; |
451 | 0 | } |
452 | 4.86k | } |
453 | 4.86k | } |
454 | | |
455 | 5.95k | if (use_len + offset == 16 || ctx->processed == ctx->plaintext_len) { |
456 | 5.96k | for (i = 0; i < ctx->q; i++) { |
457 | 5.96k | if (++(ctx->ctr)[15-i] != 0) { |
458 | 5.95k | break; |
459 | 5.95k | } |
460 | 5.96k | } |
461 | 5.95k | } |
462 | | |
463 | 5.95k | input_len -= use_len; |
464 | 5.95k | input += use_len; |
465 | 5.95k | output += use_len; |
466 | 5.95k | } |
467 | | |
468 | 601 | exit: |
469 | 601 | mbedtls_platform_zeroize(local_output, 16); |
470 | | |
471 | 601 | return ret; |
472 | 601 | } |
473 | | |
474 | | int mbedtls_ccm_finish(mbedtls_ccm_context *ctx, |
475 | | unsigned char *tag, size_t tag_len) |
476 | 601 | { |
477 | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
478 | 601 | unsigned char i; |
479 | | |
480 | 601 | if (ctx->state & CCM_STATE__ERROR) { |
481 | 0 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
482 | 0 | } |
483 | | |
484 | 601 | if (!(ctx->state & CCM_STATE__STARTED)) { |
485 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
486 | 0 | } |
487 | | |
488 | 601 | if (!(ctx->state & CCM_STATE__LENGTHS_SET)) { |
489 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
490 | 0 | } |
491 | | |
492 | 601 | if (ctx->add_len > 0 && !(ctx->state & CCM_STATE__AUTH_DATA_FINISHED)) { |
493 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
494 | 0 | } |
495 | | |
496 | 601 | if (ctx->plaintext_len > 0 && ctx->processed != ctx->plaintext_len) { |
497 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
498 | 0 | } |
499 | | |
500 | 601 | if (tag_len != ctx->tag_len) { |
501 | 0 | return MBEDTLS_ERR_CCM_BAD_INPUT; |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * Authentication: reset counter and crypt/mask internal tag |
506 | | */ |
507 | 2.40k | for (i = 0; i < ctx->q; i++) { |
508 | 1.80k | ctx->ctr[15-i] = 0; |
509 | 1.80k | } |
510 | | |
511 | 601 | ret = mbedtls_ccm_crypt(ctx, 0, 16, ctx->y, ctx->y); |
512 | 601 | if (ret != 0) { |
513 | 0 | return ret; |
514 | 0 | } |
515 | 601 | if (tag != NULL) { |
516 | 601 | memcpy(tag, ctx->y, tag_len); |
517 | 601 | } |
518 | 601 | mbedtls_ccm_clear_state(ctx); |
519 | | |
520 | 601 | return 0; |
521 | 601 | } |
522 | | |
523 | | /* |
524 | | * Authenticated encryption or decryption |
525 | | */ |
526 | | static int ccm_auth_crypt(mbedtls_ccm_context *ctx, int mode, size_t length, |
527 | | const unsigned char *iv, size_t iv_len, |
528 | | const unsigned char *add, size_t add_len, |
529 | | const unsigned char *input, unsigned char *output, |
530 | | unsigned char *tag, size_t tag_len) |
531 | 601 | { |
532 | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
533 | 601 | size_t olen; |
534 | | |
535 | 601 | if ((ret = mbedtls_ccm_starts(ctx, mode, iv, iv_len)) != 0) { |
536 | 0 | return ret; |
537 | 0 | } |
538 | | |
539 | 601 | if ((ret = mbedtls_ccm_set_lengths(ctx, add_len, length, tag_len)) != 0) { |
540 | 0 | return ret; |
541 | 0 | } |
542 | | |
543 | 601 | if ((ret = mbedtls_ccm_update_ad(ctx, add, add_len)) != 0) { |
544 | 0 | return ret; |
545 | 0 | } |
546 | | |
547 | 601 | if ((ret = mbedtls_ccm_update(ctx, input, length, |
548 | 601 | output, length, &olen)) != 0) { |
549 | 0 | return ret; |
550 | 0 | } |
551 | | |
552 | 601 | if ((ret = mbedtls_ccm_finish(ctx, tag, tag_len)) != 0) { |
553 | 0 | return ret; |
554 | 0 | } |
555 | | |
556 | 601 | return 0; |
557 | 601 | } |
558 | | |
559 | | /* |
560 | | * Authenticated encryption |
561 | | */ |
562 | | int mbedtls_ccm_star_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, |
563 | | const unsigned char *iv, size_t iv_len, |
564 | | const unsigned char *add, size_t add_len, |
565 | | const unsigned char *input, unsigned char *output, |
566 | | unsigned char *tag, size_t tag_len) |
567 | 0 | { |
568 | 0 | return ccm_auth_crypt(ctx, MBEDTLS_CCM_STAR_ENCRYPT, length, iv, iv_len, |
569 | 0 | add, add_len, input, output, tag, tag_len); |
570 | 0 | } |
571 | | |
572 | | int mbedtls_ccm_encrypt_and_tag(mbedtls_ccm_context *ctx, size_t length, |
573 | | const unsigned char *iv, size_t iv_len, |
574 | | const unsigned char *add, size_t add_len, |
575 | | const unsigned char *input, unsigned char *output, |
576 | | unsigned char *tag, size_t tag_len) |
577 | 563 | { |
578 | 563 | return ccm_auth_crypt(ctx, MBEDTLS_CCM_ENCRYPT, length, iv, iv_len, |
579 | 563 | add, add_len, input, output, tag, tag_len); |
580 | 563 | } |
581 | | |
582 | | /* |
583 | | * Authenticated decryption |
584 | | */ |
585 | | static int mbedtls_ccm_compare_tags(const unsigned char *tag1, |
586 | | const unsigned char *tag2, |
587 | | size_t tag_len) |
588 | 38 | { |
589 | | /* Check tag in "constant-time" */ |
590 | 38 | int diff = mbedtls_ct_memcmp(tag1, tag2, tag_len); |
591 | | |
592 | 38 | if (diff != 0) { |
593 | 38 | return MBEDTLS_ERR_CCM_AUTH_FAILED; |
594 | 38 | } |
595 | | |
596 | 0 | return 0; |
597 | 38 | } |
598 | | |
599 | | static int ccm_auth_decrypt(mbedtls_ccm_context *ctx, int mode, size_t length, |
600 | | const unsigned char *iv, size_t iv_len, |
601 | | const unsigned char *add, size_t add_len, |
602 | | const unsigned char *input, unsigned char *output, |
603 | | const unsigned char *tag, size_t tag_len) |
604 | 38 | { |
605 | 38 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
606 | 38 | unsigned char check_tag[16]; |
607 | | |
608 | 38 | if ((ret = ccm_auth_crypt(ctx, mode, length, |
609 | 38 | iv, iv_len, add, add_len, |
610 | 38 | input, output, check_tag, tag_len)) != 0) { |
611 | 0 | return ret; |
612 | 0 | } |
613 | | |
614 | 38 | if ((ret = mbedtls_ccm_compare_tags(tag, check_tag, tag_len)) != 0) { |
615 | 38 | mbedtls_platform_zeroize(output, length); |
616 | 38 | return ret; |
617 | 38 | } |
618 | | |
619 | 0 | return 0; |
620 | 38 | } |
621 | | |
622 | | int mbedtls_ccm_star_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, |
623 | | const unsigned char *iv, size_t iv_len, |
624 | | const unsigned char *add, size_t add_len, |
625 | | const unsigned char *input, unsigned char *output, |
626 | | const unsigned char *tag, size_t tag_len) |
627 | 0 | { |
628 | 0 | return ccm_auth_decrypt(ctx, MBEDTLS_CCM_STAR_DECRYPT, length, |
629 | 0 | iv, iv_len, add, add_len, |
630 | 0 | input, output, tag, tag_len); |
631 | 0 | } |
632 | | |
633 | | int mbedtls_ccm_auth_decrypt(mbedtls_ccm_context *ctx, size_t length, |
634 | | const unsigned char *iv, size_t iv_len, |
635 | | const unsigned char *add, size_t add_len, |
636 | | const unsigned char *input, unsigned char *output, |
637 | | const unsigned char *tag, size_t tag_len) |
638 | 38 | { |
639 | 38 | return ccm_auth_decrypt(ctx, MBEDTLS_CCM_DECRYPT, length, |
640 | 38 | iv, iv_len, add, add_len, |
641 | 38 | input, output, tag, tag_len); |
642 | 38 | } |
643 | | #endif /* !MBEDTLS_CCM_ALT */ |
644 | | |
645 | | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_CCM_GCM_CAN_AES) |
646 | | /* |
647 | | * Examples 1 to 3 from SP800-38C Appendix C |
648 | | */ |
649 | | |
650 | 0 | #define NB_TESTS 3 |
651 | 0 | #define CCM_SELFTEST_PT_MAX_LEN 24 |
652 | 0 | #define CCM_SELFTEST_CT_MAX_LEN 32 |
653 | | /* |
654 | | * The data is the same for all tests, only the used length changes |
655 | | */ |
656 | | static const unsigned char key_test_data[] = { |
657 | | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
658 | | 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f |
659 | | }; |
660 | | |
661 | | static const unsigned char iv_test_data[] = { |
662 | | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
663 | | 0x18, 0x19, 0x1a, 0x1b |
664 | | }; |
665 | | |
666 | | static const unsigned char ad_test_data[] = { |
667 | | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
668 | | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
669 | | 0x10, 0x11, 0x12, 0x13 |
670 | | }; |
671 | | |
672 | | static const unsigned char msg_test_data[CCM_SELFTEST_PT_MAX_LEN] = { |
673 | | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
674 | | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
675 | | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
676 | | }; |
677 | | |
678 | | static const size_t iv_len_test_data[NB_TESTS] = { 7, 8, 12 }; |
679 | | static const size_t add_len_test_data[NB_TESTS] = { 8, 16, 20 }; |
680 | | static const size_t msg_len_test_data[NB_TESTS] = { 4, 16, 24 }; |
681 | | static const size_t tag_len_test_data[NB_TESTS] = { 4, 6, 8 }; |
682 | | |
683 | | static const unsigned char res_test_data[NB_TESTS][CCM_SELFTEST_CT_MAX_LEN] = { |
684 | | { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, |
685 | | { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, |
686 | | 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, |
687 | | 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, |
688 | | { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, |
689 | | 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, |
690 | | 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, |
691 | | 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } |
692 | | }; |
693 | | |
694 | | int mbedtls_ccm_self_test(int verbose) |
695 | 0 | { |
696 | 0 | mbedtls_ccm_context ctx; |
697 | | /* |
698 | | * Some hardware accelerators require the input and output buffers |
699 | | * would be in RAM, because the flash is not accessible. |
700 | | * Use buffers on the stack to hold the test vectors data. |
701 | | */ |
702 | 0 | unsigned char plaintext[CCM_SELFTEST_PT_MAX_LEN]; |
703 | 0 | unsigned char ciphertext[CCM_SELFTEST_CT_MAX_LEN]; |
704 | 0 | size_t i; |
705 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
706 | |
|
707 | 0 | mbedtls_ccm_init(&ctx); |
708 | |
|
709 | 0 | if (mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key_test_data, |
710 | 0 | 8 * sizeof(key_test_data)) != 0) { |
711 | 0 | if (verbose != 0) { |
712 | 0 | mbedtls_printf(" CCM: setup failed"); |
713 | 0 | } |
714 | |
|
715 | 0 | return 1; |
716 | 0 | } |
717 | | |
718 | 0 | for (i = 0; i < NB_TESTS; i++) { |
719 | 0 | if (verbose != 0) { |
720 | 0 | mbedtls_printf(" CCM-AES #%u: ", (unsigned int) i + 1); |
721 | 0 | } |
722 | |
|
723 | 0 | memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN); |
724 | 0 | memset(ciphertext, 0, CCM_SELFTEST_CT_MAX_LEN); |
725 | 0 | memcpy(plaintext, msg_test_data, msg_len_test_data[i]); |
726 | |
|
727 | 0 | ret = mbedtls_ccm_encrypt_and_tag(&ctx, msg_len_test_data[i], |
728 | 0 | iv_test_data, iv_len_test_data[i], |
729 | 0 | ad_test_data, add_len_test_data[i], |
730 | 0 | plaintext, ciphertext, |
731 | 0 | ciphertext + msg_len_test_data[i], |
732 | 0 | tag_len_test_data[i]); |
733 | |
|
734 | 0 | if (ret != 0 || |
735 | 0 | memcmp(ciphertext, res_test_data[i], |
736 | 0 | msg_len_test_data[i] + tag_len_test_data[i]) != 0) { |
737 | 0 | if (verbose != 0) { |
738 | 0 | mbedtls_printf("failed\n"); |
739 | 0 | } |
740 | |
|
741 | 0 | return 1; |
742 | 0 | } |
743 | 0 | memset(plaintext, 0, CCM_SELFTEST_PT_MAX_LEN); |
744 | |
|
745 | 0 | ret = mbedtls_ccm_auth_decrypt(&ctx, msg_len_test_data[i], |
746 | 0 | iv_test_data, iv_len_test_data[i], |
747 | 0 | ad_test_data, add_len_test_data[i], |
748 | 0 | ciphertext, plaintext, |
749 | 0 | ciphertext + msg_len_test_data[i], |
750 | 0 | tag_len_test_data[i]); |
751 | |
|
752 | 0 | if (ret != 0 || |
753 | 0 | memcmp(plaintext, msg_test_data, msg_len_test_data[i]) != 0) { |
754 | 0 | if (verbose != 0) { |
755 | 0 | mbedtls_printf("failed\n"); |
756 | 0 | } |
757 | |
|
758 | 0 | return 1; |
759 | 0 | } |
760 | | |
761 | 0 | if (verbose != 0) { |
762 | 0 | mbedtls_printf("passed\n"); |
763 | 0 | } |
764 | 0 | } |
765 | | |
766 | 0 | mbedtls_ccm_free(&ctx); |
767 | |
|
768 | 0 | if (verbose != 0) { |
769 | 0 | mbedtls_printf("\n"); |
770 | 0 | } |
771 | |
|
772 | 0 | return 0; |
773 | 0 | } |
774 | | |
775 | | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
776 | | |
777 | | #endif /* MBEDTLS_CCM_C */ |