Print this page
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/common/crypto/modes/ccm.c
+++ new/usr/src/common/crypto/modes/ccm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #ifndef _KERNEL
27 27 #include <strings.h>
28 28 #include <limits.h>
29 29 #include <assert.h>
30 30 #include <security/cryptoki.h>
31 31 #endif
32 32
33 33 #include <sys/types.h>
34 34 #include <sys/kmem.h>
35 35 #include <modes/modes.h>
36 36 #include <sys/crypto/common.h>
37 37 #include <sys/crypto/impl.h>
38 38
39 39 #if defined(__i386) || defined(__amd64)
40 40 #include <sys/byteorder.h>
41 41 #define UNALIGNED_POINTERS_PERMITTED
42 42 #endif
43 43
44 44 /*
45 45 * Encrypt multiple blocks of data in CCM mode. Decrypt for CCM mode
46 46 * is done in another function.
47 47 */
48 48 int
49 49 ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
50 50 crypto_data_t *out, size_t block_size,
51 51 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
52 52 void (*copy_block)(uint8_t *, uint8_t *),
53 53 void (*xor_block)(uint8_t *, uint8_t *))
54 54 {
55 55 size_t remainder = length;
56 56 size_t need;
57 57 uint8_t *datap = (uint8_t *)data;
58 58 uint8_t *blockp;
59 59 uint8_t *lastp;
60 60 void *iov_or_mp;
61 61 offset_t offset;
62 62 uint8_t *out_data_1;
63 63 uint8_t *out_data_2;
64 64 size_t out_data_1_len;
65 65 uint64_t counter;
66 66 uint8_t *mac_buf;
67 67
68 68 if (length + ctx->ccm_remainder_len < block_size) {
69 69 /* accumulate bytes here and return */
70 70 bcopy(datap,
71 71 (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
72 72 length);
73 73 ctx->ccm_remainder_len += length;
74 74 ctx->ccm_copy_to = datap;
75 75 return (CRYPTO_SUCCESS);
76 76 }
77 77
78 78 lastp = (uint8_t *)ctx->ccm_cb;
79 79 if (out != NULL)
80 80 crypto_init_ptrs(out, &iov_or_mp, &offset);
81 81
82 82 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
83 83
84 84 do {
85 85 /* Unprocessed data from last call. */
86 86 if (ctx->ccm_remainder_len > 0) {
87 87 need = block_size - ctx->ccm_remainder_len;
88 88
89 89 if (need > remainder)
90 90 return (CRYPTO_DATA_LEN_RANGE);
91 91
92 92 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
93 93 [ctx->ccm_remainder_len], need);
94 94
95 95 blockp = (uint8_t *)ctx->ccm_remainder;
96 96 } else {
97 97 blockp = datap;
98 98 }
99 99
100 100 /*
101 101 * do CBC MAC
102 102 *
103 103 * XOR the previous cipher block current clear block.
104 104 * mac_buf always contain previous cipher block.
105 105 */
106 106 xor_block(blockp, mac_buf);
107 107 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
108 108
109 109 /* ccm_cb is the counter block */
110 110 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb,
111 111 (uint8_t *)ctx->ccm_tmp);
112 112
113 113 lastp = (uint8_t *)ctx->ccm_tmp;
114 114
115 115 /*
116 116 * Increment counter. Counter bits are confined
117 117 * to the bottom 64 bits of the counter block.
118 118 */
119 119 #ifdef _LITTLE_ENDIAN
120 120 counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
121 121 counter = htonll(counter + 1);
122 122 #else
123 123 counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
124 124 counter++;
125 125 #endif /* _LITTLE_ENDIAN */
126 126 counter &= ctx->ccm_counter_mask;
127 127 ctx->ccm_cb[1] =
128 128 (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
129 129
130 130 /*
131 131 * XOR encrypted counter block with the current clear block.
132 132 */
133 133 xor_block(blockp, lastp);
134 134
135 135 ctx->ccm_processed_data_len += block_size;
136 136
137 137 if (out == NULL) {
138 138 if (ctx->ccm_remainder_len > 0) {
139 139 bcopy(blockp, ctx->ccm_copy_to,
140 140 ctx->ccm_remainder_len);
141 141 bcopy(blockp + ctx->ccm_remainder_len, datap,
142 142 need);
143 143 }
144 144 } else {
145 145 crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
146 146 &out_data_1_len, &out_data_2, block_size);
147 147
148 148 /* copy block to where it belongs */
149 149 if (out_data_1_len == block_size) {
150 150 copy_block(lastp, out_data_1);
151 151 } else {
152 152 bcopy(lastp, out_data_1, out_data_1_len);
153 153 if (out_data_2 != NULL) {
154 154 bcopy(lastp + out_data_1_len,
155 155 out_data_2,
156 156 block_size - out_data_1_len);
157 157 }
158 158 }
159 159 /* update offset */
160 160 out->cd_offset += block_size;
161 161 }
162 162
163 163 /* Update pointer to next block of data to be processed. */
164 164 if (ctx->ccm_remainder_len != 0) {
165 165 datap += need;
166 166 ctx->ccm_remainder_len = 0;
167 167 } else {
168 168 datap += block_size;
169 169 }
170 170
171 171 remainder = (size_t)&data[length] - (size_t)datap;
172 172
173 173 /* Incomplete last block. */
174 174 if (remainder > 0 && remainder < block_size) {
175 175 bcopy(datap, ctx->ccm_remainder, remainder);
176 176 ctx->ccm_remainder_len = remainder;
177 177 ctx->ccm_copy_to = datap;
178 178 goto out;
179 179 }
180 180 ctx->ccm_copy_to = NULL;
181 181
182 182 } while (remainder > 0);
183 183
184 184 out:
185 185 return (CRYPTO_SUCCESS);
186 186 }
187 187
188 188 void
189 189 calculate_ccm_mac(ccm_ctx_t *ctx, uint8_t *ccm_mac,
190 190 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
191 191 {
192 192 uint64_t counter;
193 193 uint8_t *counterp, *mac_buf;
194 194 int i;
195 195
196 196 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
197 197
198 198 /* first counter block start with index 0 */
199 199 counter = 0;
200 200 ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
201 201
202 202 counterp = (uint8_t *)ctx->ccm_tmp;
203 203 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
204 204
205 205 /* calculate XOR of MAC with first counter block */
206 206 for (i = 0; i < ctx->ccm_mac_len; i++) {
207 207 ccm_mac[i] = mac_buf[i] ^ counterp[i];
208 208 }
209 209 }
210 210
211 211 /* ARGSUSED */
212 212 int
213 213 ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
214 214 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
215 215 void (*xor_block)(uint8_t *, uint8_t *))
216 216 {
217 217 uint8_t *lastp, *mac_buf, *ccm_mac_p, *macp;
218 218 void *iov_or_mp;
219 219 offset_t offset;
220 220 uint8_t *out_data_1;
221 221 uint8_t *out_data_2;
222 222 size_t out_data_1_len;
223 223 int i;
224 224
225 225 if (out->cd_length < (ctx->ccm_remainder_len + ctx->ccm_mac_len)) {
226 226 return (CRYPTO_DATA_LEN_RANGE);
227 227 }
228 228
229 229 /*
230 230 * When we get here, the number of bytes of payload processed
231 231 * plus whatever data remains, if any,
232 232 * should be the same as the number of bytes that's being
233 233 * passed in the argument during init time.
234 234 */
235 235 if ((ctx->ccm_processed_data_len + ctx->ccm_remainder_len)
236 236 != (ctx->ccm_data_len)) {
237 237 return (CRYPTO_DATA_LEN_RANGE);
238 238 }
239 239
240 240 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
241 241
242 242 if (ctx->ccm_remainder_len > 0) {
243 243
244 244 /* ccm_mac_input_buf is not used for encryption */
245 245 macp = (uint8_t *)ctx->ccm_mac_input_buf;
246 246 bzero(macp, block_size);
247 247
248 248 /* copy remainder to temporary buffer */
249 249 bcopy(ctx->ccm_remainder, macp, ctx->ccm_remainder_len);
250 250
251 251 /* calculate the CBC MAC */
252 252 xor_block(macp, mac_buf);
253 253 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
254 254
255 255 /* calculate the counter mode */
256 256 lastp = (uint8_t *)ctx->ccm_tmp;
257 257 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, lastp);
258 258
259 259 /* XOR with counter block */
260 260 for (i = 0; i < ctx->ccm_remainder_len; i++) {
261 261 macp[i] ^= lastp[i];
262 262 }
263 263 ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
264 264 }
265 265
266 266 /* Calculate the CCM MAC */
267 267 ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
268 268 calculate_ccm_mac(ctx, ccm_mac_p, encrypt_block);
269 269
270 270 crypto_init_ptrs(out, &iov_or_mp, &offset);
271 271 crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
272 272 &out_data_1_len, &out_data_2,
273 273 ctx->ccm_remainder_len + ctx->ccm_mac_len);
274 274
275 275 if (ctx->ccm_remainder_len > 0) {
276 276
277 277 /* copy temporary block to where it belongs */
278 278 if (out_data_2 == NULL) {
279 279 /* everything will fit in out_data_1 */
280 280 bcopy(macp, out_data_1, ctx->ccm_remainder_len);
281 281 bcopy(ccm_mac_p, out_data_1 + ctx->ccm_remainder_len,
282 282 ctx->ccm_mac_len);
283 283 } else {
284 284
285 285 if (out_data_1_len < ctx->ccm_remainder_len) {
286 286
287 287 size_t data_2_len_used;
288 288
289 289 bcopy(macp, out_data_1, out_data_1_len);
290 290
291 291 data_2_len_used = ctx->ccm_remainder_len
292 292 - out_data_1_len;
293 293
294 294 bcopy((uint8_t *)macp + out_data_1_len,
295 295 out_data_2, data_2_len_used);
296 296 bcopy(ccm_mac_p, out_data_2 + data_2_len_used,
297 297 ctx->ccm_mac_len);
298 298 } else {
299 299 bcopy(macp, out_data_1, out_data_1_len);
300 300 if (out_data_1_len == ctx->ccm_remainder_len) {
301 301 /* mac will be in out_data_2 */
302 302 bcopy(ccm_mac_p, out_data_2,
303 303 ctx->ccm_mac_len);
304 304 } else {
305 305 size_t len_not_used = out_data_1_len -
306 306 ctx->ccm_remainder_len;
307 307 /*
308 308 * part of mac in will be in
309 309 * out_data_1, part of the mac will be
310 310 * in out_data_2
311 311 */
312 312 bcopy(ccm_mac_p,
313 313 out_data_1 + ctx->ccm_remainder_len,
314 314 len_not_used);
315 315 bcopy(ccm_mac_p + len_not_used,
316 316 out_data_2,
317 317 ctx->ccm_mac_len - len_not_used);
318 318
319 319 }
320 320 }
321 321 }
322 322 } else {
323 323 /* copy block to where it belongs */
324 324 bcopy(ccm_mac_p, out_data_1, out_data_1_len);
325 325 if (out_data_2 != NULL) {
326 326 bcopy(ccm_mac_p + out_data_1_len, out_data_2,
327 327 block_size - out_data_1_len);
328 328 }
329 329 }
330 330 out->cd_offset += ctx->ccm_remainder_len + ctx->ccm_mac_len;
331 331 ctx->ccm_remainder_len = 0;
332 332 return (CRYPTO_SUCCESS);
333 333 }
334 334
335 335 /*
336 336 * This will only deal with decrypting the last block of the input that
337 337 * might not be a multiple of block length.
338 338 */
339 339 void
340 340 ccm_decrypt_incomplete_block(ccm_ctx_t *ctx,
341 341 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
342 342 {
343 343 uint8_t *datap, *outp, *counterp;
344 344 int i;
345 345
346 346 datap = (uint8_t *)ctx->ccm_remainder;
347 347 outp = &((ctx->ccm_pt_buf)[ctx->ccm_processed_data_len]);
348 348
349 349 counterp = (uint8_t *)ctx->ccm_tmp;
350 350 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
351 351
352 352 /* XOR with counter block */
353 353 for (i = 0; i < ctx->ccm_remainder_len; i++) {
354 354 outp[i] = datap[i] ^ counterp[i];
355 355 }
356 356 }
357 357
358 358 /*
359 359 * This will decrypt the cipher text. However, the plaintext won't be
360 360 * returned to the caller. It will be returned when decrypt_final() is
361 361 * called if the MAC matches
362 362 */
363 363 /* ARGSUSED */
364 364 int
365 365 ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
366 366 crypto_data_t *out, size_t block_size,
367 367 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
368 368 void (*copy_block)(uint8_t *, uint8_t *),
369 369 void (*xor_block)(uint8_t *, uint8_t *))
370 370 {
371 371 size_t remainder = length;
372 372 size_t need;
373 373 uint8_t *datap = (uint8_t *)data;
374 374 uint8_t *blockp;
375 375 uint8_t *cbp;
376 376 uint64_t counter;
377 377 size_t pt_len, total_decrypted_len, mac_len, pm_len, pd_len;
378 378 uint8_t *resultp;
379 379
380 380
381 381 pm_len = ctx->ccm_processed_mac_len;
382 382
383 383 if (pm_len > 0) {
384 384 uint8_t *tmp;
385 385 /*
386 386 * all ciphertext has been processed, just waiting for
387 387 * part of the value of the mac
388 388 */
389 389 if ((pm_len + length) > ctx->ccm_mac_len) {
390 390 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
391 391 }
392 392 tmp = (uint8_t *)ctx->ccm_mac_input_buf;
393 393
394 394 bcopy(datap, tmp + pm_len, length);
395 395
396 396 ctx->ccm_processed_mac_len += length;
397 397 return (CRYPTO_SUCCESS);
398 398 }
399 399
400 400 /*
401 401 * If we decrypt the given data, what total amount of data would
402 402 * have been decrypted?
403 403 */
404 404 pd_len = ctx->ccm_processed_data_len;
405 405 total_decrypted_len = pd_len + length + ctx->ccm_remainder_len;
406 406
407 407 if (total_decrypted_len >
408 408 (ctx->ccm_data_len + ctx->ccm_mac_len)) {
409 409 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
410 410 }
411 411
412 412 pt_len = ctx->ccm_data_len;
413 413
414 414 if (total_decrypted_len > pt_len) {
415 415 /*
416 416 * part of the input will be the MAC, need to isolate that
417 417 * to be dealt with later. The left-over data in
418 418 * ccm_remainder_len from last time will not be part of the
419 419 * MAC. Otherwise, it would have already been taken out
420 420 * when this call is made last time.
421 421 */
422 422 size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len;
423 423
424 424 mac_len = length - pt_part;
425 425
426 426 ctx->ccm_processed_mac_len = mac_len;
427 427 bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len);
|
↓ open down ↓ |
427 lines elided |
↑ open up ↑ |
428 428
429 429 if (pt_part + ctx->ccm_remainder_len < block_size) {
430 430 /*
431 431 * since this is last of the ciphertext, will
432 432 * just decrypt with it here
433 433 */
434 434 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
435 435 [ctx->ccm_remainder_len], pt_part);
436 436 ctx->ccm_remainder_len += pt_part;
437 437 ccm_decrypt_incomplete_block(ctx, encrypt_block);
438 + ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
438 439 ctx->ccm_remainder_len = 0;
439 - ctx->ccm_processed_data_len += pt_part;
440 440 return (CRYPTO_SUCCESS);
441 441 } else {
442 442 /* let rest of the code handle this */
443 443 length = pt_part;
444 444 }
445 445 } else if (length + ctx->ccm_remainder_len < block_size) {
446 446 /* accumulate bytes here and return */
447 447 bcopy(datap,
448 448 (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
449 449 length);
450 450 ctx->ccm_remainder_len += length;
451 451 ctx->ccm_copy_to = datap;
452 452 return (CRYPTO_SUCCESS);
453 453 }
454 454
455 455 do {
456 456 /* Unprocessed data from last call. */
457 457 if (ctx->ccm_remainder_len > 0) {
458 458 need = block_size - ctx->ccm_remainder_len;
459 459
460 460 if (need > remainder)
461 461 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
462 462
463 463 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
464 464 [ctx->ccm_remainder_len], need);
465 465
466 466 blockp = (uint8_t *)ctx->ccm_remainder;
467 467 } else {
468 468 blockp = datap;
469 469 }
470 470
471 471 /* Calculate the counter mode, ccm_cb is the counter block */
472 472 cbp = (uint8_t *)ctx->ccm_tmp;
473 473 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp);
474 474
475 475 /*
476 476 * Increment counter.
477 477 * Counter bits are confined to the bottom 64 bits
478 478 */
479 479 #ifdef _LITTLE_ENDIAN
480 480 counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
481 481 counter = htonll(counter + 1);
482 482 #else
483 483 counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
484 484 counter++;
485 485 #endif /* _LITTLE_ENDIAN */
486 486 counter &= ctx->ccm_counter_mask;
487 487 ctx->ccm_cb[1] =
488 488 (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
489 489
490 490 /* XOR with the ciphertext */
491 491 xor_block(blockp, cbp);
492 492
493 493 /* Copy the plaintext to the "holding buffer" */
494 494 resultp = (uint8_t *)ctx->ccm_pt_buf +
495 495 ctx->ccm_processed_data_len;
496 496 copy_block(cbp, resultp);
497 497
498 498 ctx->ccm_processed_data_len += block_size;
499 499
500 500 ctx->ccm_lastp = blockp;
501 501
502 502 /* Update pointer to next block of data to be processed. */
503 503 if (ctx->ccm_remainder_len != 0) {
504 504 datap += need;
505 505 ctx->ccm_remainder_len = 0;
506 506 } else {
507 507 datap += block_size;
508 508 }
509 509
510 510 remainder = (size_t)&data[length] - (size_t)datap;
511 511
512 512 /* Incomplete last block */
513 513 if (remainder > 0 && remainder < block_size) {
514 514 bcopy(datap, ctx->ccm_remainder, remainder);
515 515 ctx->ccm_remainder_len = remainder;
516 516 ctx->ccm_copy_to = datap;
517 517 if (ctx->ccm_processed_mac_len > 0) {
518 518 /*
519 519 * not expecting anymore ciphertext, just
520 520 * compute plaintext for the remaining input
521 521 */
522 522 ccm_decrypt_incomplete_block(ctx,
523 523 encrypt_block);
524 524 ctx->ccm_processed_data_len += remainder;
525 525 ctx->ccm_remainder_len = 0;
526 526 }
527 527 goto out;
528 528 }
529 529 ctx->ccm_copy_to = NULL;
530 530
531 531 } while (remainder > 0);
532 532
533 533 out:
534 534 return (CRYPTO_SUCCESS);
535 535 }
536 536
537 537 int
538 538 ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
539 539 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
540 540 void (*copy_block)(uint8_t *, uint8_t *),
541 541 void (*xor_block)(uint8_t *, uint8_t *))
542 542 {
543 543 size_t mac_remain, pt_len;
544 544 uint8_t *pt, *mac_buf, *macp, *ccm_mac_p;
545 545 int rv;
546 546
547 547 pt_len = ctx->ccm_data_len;
548 548
549 549 /* Make sure output buffer can fit all of the plaintext */
550 550 if (out->cd_length < pt_len) {
551 551 return (CRYPTO_DATA_LEN_RANGE);
552 552 }
553 553
554 554 pt = ctx->ccm_pt_buf;
555 555 mac_remain = ctx->ccm_processed_data_len;
556 556 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
557 557
558 558 macp = (uint8_t *)ctx->ccm_tmp;
559 559
560 560 while (mac_remain > 0) {
561 561
562 562 if (mac_remain < block_size) {
563 563 bzero(macp, block_size);
564 564 bcopy(pt, macp, mac_remain);
565 565 mac_remain = 0;
566 566 } else {
567 567 copy_block(pt, macp);
568 568 mac_remain -= block_size;
569 569 pt += block_size;
570 570 }
571 571
572 572 /* calculate the CBC MAC */
573 573 xor_block(macp, mac_buf);
574 574 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
575 575 }
576 576
577 577 /* Calculate the CCM MAC */
578 578 ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
579 579 calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block);
580 580
581 581 /* compare the input CCM MAC value with what we calculated */
582 582 if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) {
583 583 /* They don't match */
584 584 return (CRYPTO_INVALID_MAC);
585 585 } else {
586 586 rv = crypto_put_output_data(ctx->ccm_pt_buf, out, pt_len);
587 587 if (rv != CRYPTO_SUCCESS)
588 588 return (rv);
589 589 out->cd_offset += pt_len;
590 590 }
591 591 return (CRYPTO_SUCCESS);
592 592 }
593 593
594 594 int
595 595 ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init)
596 596 {
597 597 size_t macSize, nonceSize;
598 598 uint8_t q;
599 599 uint64_t maxValue;
600 600
601 601 /*
602 602 * Check the length of the MAC. The only valid
603 603 * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16
604 604 */
605 605 macSize = ccm_param->ulMACSize;
606 606 if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) {
607 607 return (CRYPTO_MECHANISM_PARAM_INVALID);
608 608 }
609 609
610 610 /* Check the nonce length. Valid values are 7, 8, 9, 10, 11, 12, 13 */
611 611 nonceSize = ccm_param->ulNonceSize;
612 612 if ((nonceSize < 7) || (nonceSize > 13)) {
613 613 return (CRYPTO_MECHANISM_PARAM_INVALID);
614 614 }
615 615
616 616 /* q is the length of the field storing the length, in bytes */
617 617 q = (uint8_t)((15 - nonceSize) & 0xFF);
618 618
619 619
620 620 /*
621 621 * If it is decrypt, need to make sure size of ciphertext is at least
622 622 * bigger than MAC len
623 623 */
624 624 if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) {
625 625 return (CRYPTO_MECHANISM_PARAM_INVALID);
626 626 }
627 627
628 628 /*
629 629 * Check to make sure the length of the payload is within the
630 630 * range of values allowed by q
631 631 */
632 632 if (q < 8) {
633 633 maxValue = (1ULL << (q * 8)) - 1;
634 634 } else {
635 635 maxValue = ULONG_MAX;
636 636 }
637 637
638 638 if (ccm_param->ulDataSize > maxValue) {
639 639 return (CRYPTO_MECHANISM_PARAM_INVALID);
640 640 }
641 641 return (CRYPTO_SUCCESS);
642 642 }
643 643
644 644 /*
645 645 * Format the first block used in CBC-MAC (B0) and the initial counter
646 646 * block based on formatting functions and counter generation functions
647 647 * specified in RFC 3610 and NIST publication 800-38C, appendix A
648 648 *
649 649 * b0 is the first block used in CBC-MAC
650 650 * cb0 is the first counter block
651 651 *
652 652 * It's assumed that the arguments b0 and cb0 are preallocated AES blocks
653 653 *
654 654 */
655 655 static void
656 656 ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize,
657 657 ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx)
658 658 {
659 659 uint64_t payloadSize;
660 660 uint8_t t, q, have_adata = 0;
661 661 size_t limit;
662 662 int i, j, k;
663 663 uint64_t mask = 0;
664 664 uint8_t *cb;
665 665
666 666 q = (uint8_t)((15 - nonceSize) & 0xFF);
667 667 t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF);
668 668
669 669 /* Construct the first octet of b0 */
670 670 if (authDataSize > 0) {
671 671 have_adata = 1;
672 672 }
673 673 b0[0] = (have_adata << 6) | (((t - 2) / 2) << 3) | (q - 1);
674 674
675 675 /* copy the nonce value into b0 */
676 676 bcopy(nonce, &(b0[1]), nonceSize);
677 677
678 678 /* store the length of the payload into b0 */
679 679 bzero(&(b0[1+nonceSize]), q);
680 680
681 681 payloadSize = aes_ctx->ccm_data_len;
682 682 limit = 8 < q ? 8 : q;
683 683
684 684 for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) {
685 685 b0[k] = (uint8_t)((payloadSize >> j) & 0xFF);
686 686 }
687 687
688 688 /* format the counter block */
689 689
690 690 cb = (uint8_t *)aes_ctx->ccm_cb;
691 691
692 692 cb[0] = 0x07 & (q-1); /* first byte */
693 693
694 694 /* copy the nonce value into the counter block */
695 695 bcopy(nonce, &(cb[1]), nonceSize);
696 696
697 697 bzero(&(cb[1+nonceSize]), q);
698 698
699 699 /* Create the mask for the counter field based on the size of nonce */
700 700 q <<= 3;
701 701 while (q-- > 0) {
702 702 mask |= (1ULL << q);
703 703 }
704 704
705 705 #ifdef _LITTLE_ENDIAN
706 706 mask = htonll(mask);
707 707 #endif
708 708 aes_ctx->ccm_counter_mask = mask;
709 709
710 710 /*
711 711 * During calculation, we start using counter block 1, we will
712 712 * set it up right here.
713 713 * We can just set the last byte to have the value 1, because
714 714 * even with the biggest nonce of 13, the last byte of the
715 715 * counter block will be used for the counter value.
716 716 */
717 717 cb[15] = 0x01;
718 718 }
719 719
720 720 /*
721 721 * Encode the length of the associated data as
722 722 * specified in RFC 3610 and NIST publication 800-38C, appendix A
723 723 */
724 724 static void
725 725 encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len)
726 726 {
727 727 #ifdef UNALIGNED_POINTERS_PERMITTED
728 728 uint32_t *lencoded_ptr;
729 729 #ifdef _LP64
730 730 uint64_t *llencoded_ptr;
731 731 #endif
732 732 #endif /* UNALIGNED_POINTERS_PERMITTED */
733 733
734 734 if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) {
735 735 /* 0 < a < (2^16-2^8) */
736 736 *encoded_len = 2;
737 737 encoded[0] = (auth_data_len & 0xff00) >> 8;
738 738 encoded[1] = auth_data_len & 0xff;
739 739
740 740 } else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) &&
741 741 (auth_data_len < (1ULL << 31))) {
742 742 /* (2^16-2^8) <= a < 2^32 */
743 743 *encoded_len = 6;
744 744 encoded[0] = 0xff;
745 745 encoded[1] = 0xfe;
746 746 #ifdef UNALIGNED_POINTERS_PERMITTED
747 747 lencoded_ptr = (uint32_t *)&encoded[2];
748 748 *lencoded_ptr = htonl(auth_data_len);
749 749 #else
750 750 encoded[2] = (auth_data_len & 0xff000000) >> 24;
751 751 encoded[3] = (auth_data_len & 0xff0000) >> 16;
752 752 encoded[4] = (auth_data_len & 0xff00) >> 8;
753 753 encoded[5] = auth_data_len & 0xff;
754 754 #endif /* UNALIGNED_POINTERS_PERMITTED */
755 755
756 756 #ifdef _LP64
757 757 } else {
758 758 /* 2^32 <= a < 2^64 */
759 759 *encoded_len = 10;
760 760 encoded[0] = 0xff;
761 761 encoded[1] = 0xff;
762 762 #ifdef UNALIGNED_POINTERS_PERMITTED
763 763 llencoded_ptr = (uint64_t *)&encoded[2];
764 764 *llencoded_ptr = htonl(auth_data_len);
765 765 #else
766 766 encoded[2] = (auth_data_len & 0xff00000000000000) >> 56;
767 767 encoded[3] = (auth_data_len & 0xff000000000000) >> 48;
768 768 encoded[4] = (auth_data_len & 0xff0000000000) >> 40;
769 769 encoded[5] = (auth_data_len & 0xff00000000) >> 32;
770 770 encoded[6] = (auth_data_len & 0xff000000) >> 24;
771 771 encoded[7] = (auth_data_len & 0xff0000) >> 16;
772 772 encoded[8] = (auth_data_len & 0xff00) >> 8;
773 773 encoded[9] = auth_data_len & 0xff;
774 774 #endif /* UNALIGNED_POINTERS_PERMITTED */
775 775 #endif /* _LP64 */
776 776 }
777 777 }
778 778
779 779 /*
780 780 * The following function should be call at encrypt or decrypt init time
781 781 * for AES CCM mode.
782 782 */
783 783 int
784 784 ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len,
785 785 unsigned char *auth_data, size_t auth_data_len, size_t block_size,
786 786 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
787 787 void (*xor_block)(uint8_t *, uint8_t *))
788 788 {
789 789 uint8_t *mac_buf, *datap, *ivp, *authp;
790 790 size_t remainder, processed;
791 791 uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */
792 792 size_t encoded_a_len = 0;
793 793
794 794 mac_buf = (uint8_t *)&(ctx->ccm_mac_buf);
795 795
796 796 /*
797 797 * Format the 1st block for CBC-MAC and construct the
798 798 * 1st counter block.
799 799 *
800 800 * aes_ctx->ccm_iv is used for storing the counter block
801 801 * mac_buf will store b0 at this time.
802 802 */
803 803 ccm_format_initial_blocks(nonce, nonce_len,
804 804 auth_data_len, mac_buf, ctx);
805 805
806 806 /* The IV for CBC MAC for AES CCM mode is always zero */
807 807 ivp = (uint8_t *)ctx->ccm_tmp;
808 808 bzero(ivp, block_size);
809 809
810 810 xor_block(ivp, mac_buf);
811 811
812 812 /* encrypt the nonce */
813 813 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
814 814
815 815 /* take care of the associated data, if any */
816 816 if (auth_data_len == 0) {
817 817 return (CRYPTO_SUCCESS);
818 818 }
819 819
820 820 encode_adata_len(auth_data_len, encoded_a, &encoded_a_len);
821 821
822 822 remainder = auth_data_len;
823 823
824 824 /* 1st block: it contains encoded associated data, and some data */
825 825 authp = (uint8_t *)ctx->ccm_tmp;
826 826 bzero(authp, block_size);
827 827 bcopy(encoded_a, authp, encoded_a_len);
828 828 processed = block_size - encoded_a_len;
829 829 if (processed > auth_data_len) {
830 830 /* in case auth_data is very small */
831 831 processed = auth_data_len;
832 832 }
833 833 bcopy(auth_data, authp+encoded_a_len, processed);
834 834 /* xor with previous buffer */
835 835 xor_block(authp, mac_buf);
836 836 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
837 837 remainder -= processed;
838 838 if (remainder == 0) {
839 839 /* a small amount of associated data, it's all done now */
840 840 return (CRYPTO_SUCCESS);
841 841 }
842 842
843 843 do {
844 844 if (remainder < block_size) {
845 845 /*
846 846 * There's not a block full of data, pad rest of
847 847 * buffer with zero
848 848 */
849 849 bzero(authp, block_size);
850 850 bcopy(&(auth_data[processed]), authp, remainder);
851 851 datap = (uint8_t *)authp;
852 852 remainder = 0;
853 853 } else {
854 854 datap = (uint8_t *)(&(auth_data[processed]));
855 855 processed += block_size;
856 856 remainder -= block_size;
857 857 }
858 858
859 859 xor_block(datap, mac_buf);
860 860 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
861 861
862 862 } while (remainder > 0);
863 863
864 864 return (CRYPTO_SUCCESS);
865 865 }
866 866
867 867 int
868 868 ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag,
869 869 boolean_t is_encrypt_init, size_t block_size,
870 870 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
871 871 void (*xor_block)(uint8_t *, uint8_t *))
872 872 {
873 873 int rv;
874 874 CK_AES_CCM_PARAMS *ccm_param;
875 875
876 876 if (param != NULL) {
877 877 ccm_param = (CK_AES_CCM_PARAMS *)param;
878 878
879 879 if ((rv = ccm_validate_args(ccm_param,
880 880 is_encrypt_init)) != 0) {
881 881 return (rv);
882 882 }
883 883
884 884 ccm_ctx->ccm_mac_len = ccm_param->ulMACSize;
885 885 if (is_encrypt_init) {
886 886 ccm_ctx->ccm_data_len = ccm_param->ulDataSize;
887 887 } else {
888 888 ccm_ctx->ccm_data_len =
889 889 ccm_param->ulDataSize - ccm_ctx->ccm_mac_len;
890 890 ccm_ctx->ccm_processed_mac_len = 0;
891 891 }
892 892 ccm_ctx->ccm_processed_data_len = 0;
893 893
894 894 ccm_ctx->ccm_flags |= CCM_MODE;
895 895 } else {
896 896 rv = CRYPTO_MECHANISM_PARAM_INVALID;
897 897 goto out;
898 898 }
899 899
900 900 if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize,
901 901 ccm_param->authData, ccm_param->ulAuthDataSize, block_size,
902 902 encrypt_block, xor_block) != 0) {
903 903 rv = CRYPTO_MECHANISM_PARAM_INVALID;
904 904 goto out;
905 905 }
906 906 if (!is_encrypt_init) {
907 907 /* allocate buffer for storing decrypted plaintext */
908 908 #ifdef _KERNEL
909 909 ccm_ctx->ccm_pt_buf = kmem_alloc(ccm_ctx->ccm_data_len,
910 910 kmflag);
911 911 #else
912 912 ccm_ctx->ccm_pt_buf = malloc(ccm_ctx->ccm_data_len);
913 913 #endif
914 914 if (ccm_ctx->ccm_pt_buf == NULL) {
915 915 rv = CRYPTO_HOST_MEMORY;
916 916 }
917 917 }
918 918 out:
919 919 return (rv);
920 920 }
921 921
922 922 void *
923 923 ccm_alloc_ctx(int kmflag)
924 924 {
925 925 ccm_ctx_t *ccm_ctx;
926 926
927 927 #ifdef _KERNEL
928 928 if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL)
929 929 #else
930 930 if ((ccm_ctx = calloc(1, sizeof (ccm_ctx_t))) == NULL)
931 931 #endif
932 932 return (NULL);
933 933
934 934 ccm_ctx->ccm_flags = CCM_MODE;
935 935 return (ccm_ctx);
936 936 }
|
↓ open down ↓ |
487 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX