forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccm.c
635 lines (532 loc) · 21.5 KB
/
ccm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//
// CCM.c implementation of the CCM block cipher mode
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
#include "precomp.h"
#define CCM_MIN_NONCE_SIZE (7)
#define CCM_MAX_NONCE_SIZE (13)
#define CCM_MIN_TAG_SIZE (4)
#define CCM_MAX_TAG_SIZE (16)
#define CCM_MAX_COUNTER_SIZE (SYMCRYPT_CCM_BLOCK_SIZE - 1 - CCM_MIN_NONCE_SIZE)
#define AUTHDATA_16BIT_LIMIT ((1<<16) - (1<<8))
#define AUTHDATA_32BIT_LIMIT (1ull << 32)
// Compile time BOOL statically determines if we need to check cbAuthData < AUTHDATA_32BIT_LIMIT
// Used to suppress MSVC C4127 and clang Wtautological-constant-out-of-range-compare on 32b platforms
const BOOL fcbAuthDataLt32bitLimitStatic = SIZE_T_MAX < AUTHDATA_32BIT_LIMIT;
#define CCM_BLOCK_MOD_MASK (SYMCRYPT_CCM_BLOCK_SIZE - 1)
#define CCM_BLOCK_ROUND_MASK (~CCM_BLOCK_MOD_MASK)
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptCcmValidateParameters(
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_ SIZE_T cbNonce,
_In_ SIZE_T cbAssociatedData,
_In_ UINT64 cbData,
_In_ SIZE_T cbTag
)
{
SIZE_T cbCounter;
UNREFERENCED_PARAMETER( cbAssociatedData );
if( pBlockCipher->blockSize != SYMCRYPT_CCM_BLOCK_SIZE )
{
return SYMCRYPT_WRONG_BLOCK_SIZE;
}
//
// Test against limits in SP800-38C appendix A
//
if( cbNonce < CCM_MIN_NONCE_SIZE || cbNonce > CCM_MAX_NONCE_SIZE )
{
return SYMCRYPT_WRONG_NONCE_SIZE;
}
//
// cbAssociatedData is limited to <2^64
// We don't test for this. None of our platforms has a SIZE_T that is
// large enough to violate this condition. And the test
// is of a form that the compiler cannot optimize away.
//
//
// The counter block consists of a single flag byte, the nonce, and the counter field.
//
cbCounter = SYMCRYPT_CCM_BLOCK_SIZE - cbNonce - 1;
//
// per SP800-38C cbData is limited to 2^{8*cbCounter}
// There is no way to do this test in a single comparison.
// We don't have to worry about side-channels in the && because
// cbCounter depends only on the length of the nonce, and we do not
// try to hide any lengths.
//
if( cbCounter < sizeof( UINT64 ) &&
cbData >= ((UINT64)1 << (8*cbCounter)) )
{
return SYMCRYPT_WRONG_DATA_SIZE;
}
if( cbTag < CCM_MIN_TAG_SIZE ||
cbTag > CCM_MAX_TAG_SIZE ||
(cbTag & 1) == 1 // valid tag lengths are [4, 6, 8, ..., 16]
)
{
return SYMCRYPT_WRONG_TAG_SIZE;
}
return SYMCRYPT_NO_ERROR;
}
VOID
SYMCRYPT_CALL
SymCryptCcmEncryptDecryptPart(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
SIZE_T cbToDo = cbData;
SIZE_T bytesToProcess;
//
// Use any left-over key stream
//
while( (pState->bytesProcessed & CCM_BLOCK_MOD_MASK) != 0 && cbToDo > 0 )
{
*pbDst = *pbSrc ^ pState->keystreamBlock[ pState->bytesProcessed & CCM_BLOCK_MOD_MASK ];
pbDst++;
pbSrc++;
cbToDo--;
pState->bytesProcessed++;
}
//
// Bulk process the main part of the input and output
//
if( cbToDo >= SYMCRYPT_CCM_BLOCK_SIZE )
{
bytesToProcess = cbToDo & CCM_BLOCK_ROUND_MASK;
SYMCRYPT_ASSERT( bytesToProcess <= cbToDo );
SYMCRYPT_ASSERT( pState->pBlockCipher->blockSize == SYMCRYPT_CCM_BLOCK_SIZE );
SymCryptCtrMsb64( pState->pBlockCipher,
pState->pExpandedKey,
&pState->counterBlock[0],
pbSrc,
pbDst,
bytesToProcess );
pbSrc += bytesToProcess;
pbDst += bytesToProcess;
pState->bytesProcessed += bytesToProcess;
cbToDo -= bytesToProcess;
}
if( cbToDo > 0 )
{
//
// Encrypt an all-zero key stream block to get the key stream.
//
SymCryptWipeKnownSize( &pState->keystreamBlock[0], SYMCRYPT_CCM_BLOCK_SIZE );
SYMCRYPT_ASSERT( pState->pBlockCipher->blockSize == SYMCRYPT_CCM_BLOCK_SIZE );
SymCryptCtrMsb64( pState->pBlockCipher,
pState->pExpandedKey,
&pState->counterBlock[0],
&pState->keystreamBlock[0],
&pState->keystreamBlock[0],
SYMCRYPT_CCM_BLOCK_SIZE );
while( cbToDo > 0 )
{
*pbDst = *pbSrc ^ pState->keystreamBlock[ pState->bytesProcessed & CCM_BLOCK_MOD_MASK ];
pbDst++;
pbSrc++;
cbToDo--;
pState->bytesProcessed++;
}
}
}
VOID
SYMCRYPT_CALL
SymCryptCcmAddMacData(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
SIZE_T bytesToProcess;
if( pState->bytesInMacBlock > 0 )
{
bytesToProcess = SYMCRYPT_MIN( cbData, SYMCRYPT_CCM_BLOCK_SIZE - pState->bytesInMacBlock );
SymCryptXorBytes( &pState->macBlock[pState->bytesInMacBlock], pbData, &pState->macBlock[pState->bytesInMacBlock], bytesToProcess );
pbData += bytesToProcess;
cbData -= bytesToProcess;
pState->bytesInMacBlock += bytesToProcess;
if( pState->bytesInMacBlock == SYMCRYPT_CCM_BLOCK_SIZE )
{
pState->pBlockCipher->encryptFunc( pState->pExpandedKey, &pState->macBlock[0], &pState->macBlock[0] );
pState->bytesInMacBlock = 0;
}
}
if( cbData >= SYMCRYPT_CCM_BLOCK_SIZE )
{
bytesToProcess = cbData & CCM_BLOCK_ROUND_MASK;
SYMCRYPT_ASSERT( pState->pBlockCipher->blockSize == SYMCRYPT_CCM_BLOCK_SIZE );
SymCryptCbcMac( pState->pBlockCipher,
pState->pExpandedKey,
&pState->macBlock[0],
pbData,
bytesToProcess );
pbData += bytesToProcess;
cbData -= bytesToProcess;
}
if( cbData > 0 )
{
SymCryptXorBytes( &pState->macBlock[0], pbData, &pState->macBlock[0], cbData );
pState->bytesInMacBlock = cbData;
}
}
VOID
SYMCRYPT_CALL
SymCryptCcmPadMacData( _Inout_ PSYMCRYPT_CCM_STATE pState )
{
//
// Pad the MAC data with zeroes until we hit the block size.
// The data is xorred into macBlock, so we don't have to update that.
// All we do is apply the block cipher if there was any data remaining in the macBlock.
//
if( pState->bytesInMacBlock > 0 )
{
pState->pBlockCipher->encryptFunc( pState->pExpandedKey, &pState->macBlock[0], &pState->macBlock[0] );
pState->bytesInMacBlock = 0;
}
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptCcmEncrypt(
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_ PCVOID pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce,
_In_reads_opt_( cbAuthData ) PCBYTE pbAuthData,
SIZE_T cbAuthData,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData,
_Out_writes_( cbTag ) PBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_CCM_STATE state;
SymCryptCcmInit( &state,
pBlockCipher,
pExpandedKey,
pbNonce, cbNonce,
pbAuthData, cbAuthData,
cbData, cbTag );
SymCryptCcmEncryptPart( &state, pbSrc, pbDst, cbData );
SymCryptCcmEncryptFinal( &state, pbTag, cbTag );
}
SYMCRYPT_NOINLINE
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptCcmDecrypt(
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_ PCVOID pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce,
_In_reads_opt_( cbAuthData ) PCBYTE pbAuthData,
SIZE_T cbAuthData,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData,
_In_reads_( cbTag ) PCBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_CCM_STATE state;
SYMCRYPT_ERROR status;
SymCryptCcmInit( &state,
pBlockCipher,
pExpandedKey,
pbNonce, cbNonce,
pbAuthData, cbAuthData,
cbData, cbTag );
SymCryptCcmDecryptPart( &state, pbSrc, pbDst, cbData );
status = SymCryptCcmDecryptFinal( &state, pbTag, cbTag );
//
// If we failed for any reason we wipe our output buffer to avoid returning
// decrypted but unauthenticated data.
//
if( status != SYMCRYPT_NO_ERROR )
{
SymCryptWipe( pbDst, cbData );
}
return status;
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptCcmInit(
_Out_ PSYMCRYPT_CCM_STATE pState,
_In_ PCSYMCRYPT_BLOCKCIPHER pBlockCipher,
_In_ PCVOID pExpandedKey,
_In_reads_( cbNonce ) PCBYTE pbNonce,
SIZE_T cbNonce,
_In_reads_opt_( cbAuthData ) PCBYTE pbAuthData,
SIZE_T cbAuthData,
UINT64 cbData,
SIZE_T cbTag )
{
BYTE flags;
BYTE tmpBuf[ SYMCRYPT_CCM_BLOCK_SIZE ];
SIZE_T cbCounter;
SYMCRYPT_SET_MAGIC( pState );
//
// Validate parameters in checked builds
//
SYMCRYPT_ASSERT( SymCryptCcmValidateParameters( pBlockCipher, cbNonce, cbAuthData, cbData, cbTag ) == SYMCRYPT_NO_ERROR );
//
// compute # bytes in the counter field
// We limit cbNonce to 15 so that cbCounter + cbNonce = 15 will always hold
// This is much cheaper than full parameter validation, and it is enough to
// avoid any buffer overflows.
//
cbNonce &= SYMCRYPT_CCM_BLOCK_SIZE - 1;
cbCounter = SYMCRYPT_CCM_BLOCK_SIZE - 1 - cbNonce;
pState->pBlockCipher = pBlockCipher;
pState->pExpandedKey = pExpandedKey;
pState->cbNonce = cbNonce;
pState->cbData = cbData;
pState->cbTag = cbTag;
pState->cbCounter = cbCounter;
pState->bytesProcessed = 0;
pState->bytesInMacBlock = 0;
//
// Build the initial blocks for authentication and en/decryption
//
// Per Sp800-38c the flag byte is made up of four fields:
// Bits 0-2 are cbCounter - 1
// Bits 3-5 are (cbTag-2)/2
// Bit 6 is 1 if cbAuthData > 0
// Bit 7 is reserved and set to 0.
flags = (BYTE) (pState->cbCounter - 1);
flags |= ((cbTag-2)/2) << 3;
if( cbAuthData > 0 )
{
//
// No side-channel concerns with this if statements as we don't try to hide the
// data length or presence of authdata.
//
flags |= (1 << 6);
}
//
// The MAC starting block consists of three fields:
// the flag byte, the nonce, and cbData encoded into cbCounter bytes.
//
pState->macBlock[0] = flags;
memcpy( &pState->macBlock[1], pbNonce, cbNonce );
SYMCRYPT_STORE_MSBFIRST64( &tmpBuf[0], cbData );
memcpy( &pState->macBlock[1+cbNonce], &tmpBuf[ 8 - cbCounter ], cbCounter );
//
// The counter block is similar in layout, but with two changes:
// Bits 3-7 of the flag bytes are set to 0.
// The counter field is set to one (first counter value used for data encryption).
// Wiping the whole block first is probably faster, as the size is known and the
// block is aligned.
// We also copy the nonce from the mac block to follow the read-once rule.
//
SymCryptWipeKnownSize( &pState->counterBlock[0], SYMCRYPT_CCM_BLOCK_SIZE );
pState->counterBlock[0] = (BYTE)(flags & 0x7);
memcpy( &pState->counterBlock[1], &pState->macBlock[1], cbNonce );
pState->counterBlock[ SYMCRYPT_CCM_BLOCK_SIZE - 1] = 1;
//
// Encrypt the current MAC block; our CBC convention is to do the encryption
// as soon as we have enough data.
//
pBlockCipher->encryptFunc( pExpandedKey, &pState->macBlock[0], &pState->macBlock[0] );
//
// Next we process the associated data
// See the CCM specs for the details
//
if( cbAuthData <= 0 )
{
//
// cbAuthData == 0, nothing needs to be done.
//
} else if( cbAuthData < AUTHDATA_16BIT_LIMIT )
{
//
// 16-bit length encoding.
//
SYMCRYPT_STORE_MSBFIRST16( &tmpBuf[0], (UINT16) cbAuthData );
SymCryptCcmAddMacData( pState, &tmpBuf[0], 2 );
} else if( fcbAuthDataLt32bitLimitStatic || cbAuthData < AUTHDATA_32BIT_LIMIT )
{
//
// 32-bit length
//
tmpBuf[0] = 0xff;
tmpBuf[1] = 0xfe; // Magic prefix as per SP 800-38c
SYMCRYPT_STORE_MSBFIRST32( &tmpBuf[2], (UINT32) cbAuthData );
SymCryptCcmAddMacData( pState, &tmpBuf[0], 2 + sizeof( UINT32 ) );
} else
{
//
// 64-bit length
//
tmpBuf[0] = 0xff;
tmpBuf[1] = 0xff; // Magic prefix as per SP 800-38c
SYMCRYPT_STORE_MSBFIRST64( &tmpBuf[2], cbAuthData );
SymCryptCcmAddMacData( pState, &tmpBuf[0], 2 + sizeof( UINT64 ) );
}
SymCryptCcmAddMacData( pState, pbAuthData, cbAuthData );
SymCryptCcmPadMacData( pState ); // Pad MAC data with zeroes until the next block size boundary
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptCcmEncryptPart(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
UINT64 bytesProcessedAfterThisCall;
SYMCRYPT_CHECK_MAGIC( pState );
bytesProcessedAfterThisCall = cbData + pState->bytesProcessed;
SYMCRYPT_ASSERT( bytesProcessedAfterThisCall >= cbData &&
bytesProcessedAfterThisCall <= pState->cbData );
//
// We are violating the read-once implementation rule here. We read the data twice:
// once for MACing and once for encryption.
// In this particular situation this is safe to do.
// We consider the read for the MAC operation as reading the 'real' value.
// The encryption code reads the data, but all it does is XOR the key stream into
// it. (CCM encryption uses CTR mode for the encryption part.)
// We don't care if the attacker modifies the data before the encryption.
// We are revealing the key stream anyway (from the plaintext and ciphertext) and
// the exact byte value that we xor the key stream into is irrelevant.
//
SymCryptCcmAddMacData( pState, pbSrc, cbData );
SymCryptCcmEncryptDecryptPart( pState, pbSrc, pbDst, cbData );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptCcmEncryptFinal(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_Out_writes_( cbTag ) PBYTE pbTag,
SIZE_T cbTag )
{
//
// Check invariants in checked builds
//
SYMCRYPT_CHECK_MAGIC( pState );
SYMCRYPT_ASSERT( cbTag == pState->cbTag && pState->bytesProcessed == pState->cbData );
SymCryptCcmPadMacData( pState );
//
// Set the counter value to zero to get the counter value that encrypts the tag,
// and then encrypt the tag.
// We reset bytesProcessed so that the partial encrypt/decrypt function will do the right thing
//
SymCryptWipe( &pState->counterBlock[1 + pState->cbNonce], pState->cbCounter );
pState->bytesProcessed = 0;
SymCryptCcmEncryptDecryptPart( pState, &pState->macBlock[0], &pState->macBlock[0], SYMCRYPT_CCM_BLOCK_SIZE );
memcpy( pbTag, &pState->macBlock[0], cbTag );
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
SYMCRYPT_ASSERT( pState->bytesInMacBlock == 0 );
}
SYMCRYPT_NOINLINE
VOID
SYMCRYPT_CALL
SymCryptCcmDecryptPart(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
UINT64 bytesProcessedAfterThisCall;
SYMCRYPT_CHECK_MAGIC( pState );
bytesProcessedAfterThisCall = cbData + pState->bytesProcessed;
SYMCRYPT_ASSERT( bytesProcessedAfterThisCall >= cbData &&
bytesProcessedAfterThisCall <= pState->cbData );
//
// We are violating the read-once/write-once implementation rule here.
// We write the decrypted data and then read it back for the authentication function.
// In this particular situation this is safe to do.
//
// Anyone who can access the memory space that contains the source and destination of this
// function can recover the key stream used for this (key,nonce) combination.
// We can think of the decryption function as merely exposing the key stream, and then the
// caller picking the ciphertext (and by implication the plaintext) to be authenticated.
// Thus the data we read during authentication is the 'real' plaintext, and the
// decryption function merely made the key stream available.
//
// Note that this would not safe in general, it is only safe because CTR mode decryption already
// reveals the key stream.
//
SymCryptCcmEncryptDecryptPart( pState, pbSrc, pbDst, cbData );
SymCryptCcmAddMacData( pState, pbDst, cbData );
}
SYMCRYPT_NOINLINE
SYMCRYPT_ERROR
SYMCRYPT_CALL
SymCryptCcmDecryptFinal(
_Inout_ PSYMCRYPT_CCM_STATE pState,
_In_reads_( cbTag ) PCBYTE pbTag,
SIZE_T cbTag )
{
SYMCRYPT_ERROR status;
//
// Check invariants in checked builds
//
SYMCRYPT_CHECK_MAGIC( pState );
SYMCRYPT_ASSERT( cbTag == pState->cbTag && pState->bytesProcessed == pState->cbData );
SymCryptCcmPadMacData( pState );
//
// Set the counter value to zero to get the counter value that encrypts the tag,
// and then encrypt the tag
// We reset bytesProcessed so that the partial encrypt/decrypt function will do the right thing
//
SymCryptWipe( &pState->counterBlock[1 + pState->cbNonce], pState->cbCounter );
pState->bytesProcessed = 0;
SymCryptCcmEncryptDecryptPart( pState, &pState->macBlock[0], &pState->macBlock[0], SYMCRYPT_CCM_BLOCK_SIZE );
if( !SymCryptEqual( pbTag, &pState->macBlock[0], cbTag ) )
{
status = SYMCRYPT_AUTHENTICATION_FAILURE;
}
else
{
status = SYMCRYPT_NO_ERROR;
}
SymCryptWipeKnownSize( pState, sizeof( *pState ) );
SYMCRYPT_ASSERT( pState->bytesInMacBlock == 0 );
return status;
}
static const BYTE SymCryptCcmSelftestResult[3 + SYMCRYPT_AES_BLOCK_SIZE ] =
{
0x42, 0xd7, 0xda,
0x3d, 0x9e, 0x95, 0x82, 0x29, 0x3c, 0x10, 0x9c, 0xa3, 0x39, 0x31, 0x3f, 0x18, 0xf3, 0x10, 0xf6
};
VOID
SYMCRYPT_CALL
SymCryptCcmSelftest(void)
{
BYTE buf[ 3 + SYMCRYPT_AES_BLOCK_SIZE ];
SYMCRYPT_AES_EXPANDED_KEY key;
SYMCRYPT_ERROR err;
if( SymCryptAesExpandKey( &key, SymCryptTestKey32, 16 ) != SYMCRYPT_NO_ERROR )
{
SymCryptFatal( 'ccm0' );
}
SymCryptCcmEncrypt( SymCryptAesBlockCipher,
&key,
&SymCryptTestKey32[16], 12,
NULL, 0,
&SymCryptTestMsg3[0], buf, 3,
&buf[3], SYMCRYPT_AES_BLOCK_SIZE );
SymCryptInjectError( buf, sizeof( buf ) );
if( memcmp( buf, SymCryptCcmSelftestResult, sizeof( buf ) ) != 0 )
{
SymCryptFatal( 'ccm1' );
}
// inject error into the ciphertext or tag
SymCryptInjectError( buf, sizeof( buf ) );
err = SymCryptCcmDecrypt( SymCryptAesBlockCipher,
&key,
&SymCryptTestKey32[16], 12,
NULL, 0,
buf, buf, 3,
&buf[3], SYMCRYPT_AES_BLOCK_SIZE );
SymCryptInjectError( buf, 3 );
if( err != SYMCRYPT_NO_ERROR || memcmp( buf, SymCryptTestMsg3, 3 ) != 0 )
{
SymCryptFatal( 'ccm2' );
}
}