34 #if defined(CRYPTO_COUNT) && (CRYPTO_COUNT > 0)
55 #define CRYPTO_INSTRUCTIONS_PER_REG (4)
56 #define CRYPTO_INSTRUCTIONS_MAX (12)
57 #define CRYPTO_INSTRUCTION_REGS (CRYPTO_INSTRUCTIONS_MAX/CRYPTO_INSTRUCTIONS_PER_REG)
59 #define CRYPTO_SHA1_BLOCK_SIZE_IN_BITS (512)
60 #define CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES (CRYPTO_SHA1_BLOCK_SIZE_IN_BITS/8)
61 #define CRYPTO_SHA1_BLOCK_SIZE_IN_32BIT_WORDS (CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES/sizeof(uint32_t))
62 #define CRYPTO_SHA1_DIGEST_SIZE_IN_32BIT_WORDS (CRYPTO_SHA1_DIGEST_SIZE_IN_BYTES/sizeof(uint32_t))
64 #define CRYPTO_SHA256_BLOCK_SIZE_IN_BITS (512)
65 #define CRYPTO_SHA256_BLOCK_SIZE_IN_BYTES (CRYPTO_SHA256_BLOCK_SIZE_IN_BITS/8)
66 #define CRYPTO_SHA256_BLOCK_SIZE_IN_32BIT_WORDS (CRYPTO_SHA256_BLOCK_SIZE_IN_BYTES/sizeof(uint32_t))
68 #define CRYPTO_SHA256_DIGEST_SIZE_IN_32BIT_WORDS (CRYPTO_SHA256_DIGEST_SIZE_IN_BYTES/sizeof(uint32_t))
70 #define PARTIAL_OPERAND_WIDTH_LOG2 (7)
71 #define PARTIAL_OPERAND_WIDTH (1<<PARTIAL_OPERAND_WIDTH_LOG2)
72 #define PARTIAL_OPERAND_WIDTH_MASK (PARTIAL_OPERAND_WIDTH-1)
73 #define PARTIAL_OPERAND_WIDTH_IN_BYTES (PARTIAL_OPERAND_WIDTH/8)
74 #define PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS (PARTIAL_OPERAND_WIDTH_IN_BYTES/sizeof(uint32_t))
76 #define SWAP32(x) (__REV(x))
78 #define CRYPTO_AES_BLOCKSIZE (16)
84 static inline void CRYPTO_AES_ProcessLoop(CRYPTO_TypeDef *crypto,
86 CRYPTO_DataReg_TypeDef inReg,
88 CRYPTO_DataReg_TypeDef outReg,
91 static void CRYPTO_AES_CBCx(CRYPTO_TypeDef *crypto,
98 CRYPTO_KeyWidth_TypeDef keyWidth);
100 static void CRYPTO_AES_CFBx(CRYPTO_TypeDef *crypto,
107 CRYPTO_KeyWidth_TypeDef keyWidth);
109 static void CRYPTO_AES_CTRx(CRYPTO_TypeDef *crypto,
115 CRYPTO_AES_CtrFuncPtr_TypeDef ctrFunc,
116 CRYPTO_KeyWidth_TypeDef keyWidth);
118 static void CRYPTO_AES_ECBx(CRYPTO_TypeDef *crypto,
124 CRYPTO_KeyWidth_TypeDef keyWidth);
126 static void CRYPTO_AES_OFBx(CRYPTO_TypeDef *crypto,
132 CRYPTO_KeyWidth_TypeDef keyWidth);
134 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
148 void CRYPTO_DataWriteVariableSize(CRYPTO_DataReg_TypeDef dataReg,
149 const CRYPTO_Data_TypeDef val,
153 volatile uint32_t * reg = (
volatile uint32_t *) dataReg;
158 for (i = 0; i < valSize; i++)
165 CRYPTO_BurstToCrypto(reg, &val[0]);
190 void CRYPTO_ModulusSet(CRYPTO_TypeDef * crypto,
191 CRYPTO_ModulusId_TypeDef modulusId)
193 uint32_t temp = crypto->WAC & (~(_CRYPTO_WAC_MODULUS_MASK | _CRYPTO_WAC_MODOP_MASK));
197 case cryptoModulusBin256:
198 case cryptoModulusBin128:
199 case cryptoModulusGcmBin128:
200 case cryptoModulusEccB233:
201 case cryptoModulusEccB163:
202 #ifdef _CRYPTO_WAC_MODULUS_ECCBIN233N
203 case cryptoModulusEccB233Order:
204 case cryptoModulusEccB233KOrder:
205 case cryptoModulusEccB163Order:
206 case cryptoModulusEccB163KOrder:
208 crypto->WAC = temp | modulusId | CRYPTO_WAC_MODOP_BINARY;
211 case cryptoModulusEccP256:
212 case cryptoModulusEccP224:
213 case cryptoModulusEccP192:
214 #ifdef _CRYPTO_WAC_MODULUS_ECCPRIME256P
215 case cryptoModulusEccP256Order:
216 case cryptoModulusEccP224Order:
217 case cryptoModulusEccP192Order:
219 crypto->WAC = temp | modulusId | CRYPTO_WAC_MODOP_REGULAR;
244 void CRYPTO_KeyRead(CRYPTO_TypeDef * crypto,
245 CRYPTO_KeyBuf_TypeDef val,
246 CRYPTO_KeyWidth_TypeDef keyWidth)
250 CRYPTO_BurstFromCrypto(&crypto->KEY, &val[0]);
251 if (keyWidth == cryptoKey256Bits)
253 CRYPTO_BurstFromCrypto(&crypto->KEY, &val[4]);
277 void CRYPTO_SHA_1(CRYPTO_TypeDef * crypto,
280 CRYPTO_SHA1_Digest_TypeDef msgDigest)
285 uint32_t shaBlock[CRYPTO_SHA1_BLOCK_SIZE_IN_32BIT_WORDS]=
288 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
290 uint8_t * p8ShaBlock = (uint8_t *) shaBlock;
293 crypto->CTRL = CRYPTO_CTRL_SHA_SHA1;
295 crypto->SEQCTRLB = 0;
298 CRYPTO_ResultWidthSet(crypto, cryptoResult256Bits);
301 CRYPTO_DDataWrite(&crypto->DDATA1, shaBlock);
304 CRYPTO_EXECUTE_2(crypto,
305 CRYPTO_CMD_INSTR_DDATA1TODDATA0,
306 CRYPTO_CMD_INSTR_SELDDATA0DDATA1);
310 while (len >= CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES)
313 CRYPTO_QDataWrite(&crypto->QDATA1BIG, (uint32_t *) msg);
316 CRYPTO_EXECUTE_3(crypto,
317 CRYPTO_CMD_INSTR_SHA,
318 CRYPTO_CMD_INSTR_MADD32,
319 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
321 len -= CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES;
322 msg += CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES;
329 p8ShaBlock[blockLen++] = *msg++;
332 p8ShaBlock[blockLen++] = 0x80;
340 while (blockLen < 64)
341 p8ShaBlock[blockLen++] = 0;
344 CRYPTO_QDataWrite(&crypto->QDATA1BIG, shaBlock);
347 CRYPTO_EXECUTE_3(crypto,
348 CRYPTO_CMD_INSTR_SHA,
349 CRYPTO_CMD_INSTR_MADD32,
350 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
355 while (blockLen < 56)
356 p8ShaBlock[blockLen++] = 0;
360 uint64_t msgLenInBits = msgLen << 3;
361 temp = msgLenInBits >> 32;
362 *(uint32_t*)&p8ShaBlock[56] = SWAP32(temp);
363 temp = msgLenInBits & 0xFFFFFFFF;
364 *(uint32_t*)&p8ShaBlock[60] = SWAP32(temp);
368 CRYPTO_QDataWrite(&crypto->QDATA1BIG, shaBlock);
371 CRYPTO_EXECUTE_3(crypto,
372 CRYPTO_CMD_INSTR_SHA,
373 CRYPTO_CMD_INSTR_MADD32,
374 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
377 ((uint32_t*)msgDigest)[0] = crypto->DDATA0BIG;
378 ((uint32_t*)msgDigest)[1] = crypto->DDATA0BIG;
379 ((uint32_t*)msgDigest)[2] = crypto->DDATA0BIG;
380 ((uint32_t*)msgDigest)[3] = crypto->DDATA0BIG;
381 ((uint32_t*)msgDigest)[4] = crypto->DDATA0BIG;
382 temp = crypto->DDATA0BIG;
383 temp = crypto->DDATA0BIG;
384 temp = crypto->DDATA0BIG;
407 void CRYPTO_SHA_256(CRYPTO_TypeDef * crypto,
410 CRYPTO_SHA256_Digest_TypeDef msgDigest)
415 uint32_t shaBlock[CRYPTO_SHA256_BLOCK_SIZE_IN_32BIT_WORDS]=
418 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
419 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
421 uint8_t * p8ShaBlock = (uint8_t *) shaBlock;
424 crypto->CTRL = CRYPTO_CTRL_SHA_SHA2;
426 crypto->SEQCTRLB = 0;
429 CRYPTO_ResultWidthSet(crypto, cryptoResult256Bits);
432 CRYPTO_DDataWrite(&crypto->DDATA1, shaBlock);
435 CRYPTO_EXECUTE_2(crypto,
436 CRYPTO_CMD_INSTR_DDATA1TODDATA0,
437 CRYPTO_CMD_INSTR_SELDDATA0DDATA1);
440 while (len >= CRYPTO_SHA256_BLOCK_SIZE_IN_BYTES)
443 CRYPTO_QDataWrite(&crypto->QDATA1BIG, (uint32_t *) msg);
446 CRYPTO_EXECUTE_3(crypto,
447 CRYPTO_CMD_INSTR_SHA,
448 CRYPTO_CMD_INSTR_MADD32,
449 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
451 len -= CRYPTO_SHA256_BLOCK_SIZE_IN_BYTES;
452 msg += CRYPTO_SHA256_BLOCK_SIZE_IN_BYTES;
459 p8ShaBlock[blockLen++] = *msg++;
462 p8ShaBlock[blockLen++] = 0x80;
470 while (blockLen < 64)
471 p8ShaBlock[blockLen++] = 0;
474 CRYPTO_QDataWrite(&crypto->QDATA1BIG, shaBlock);
477 CRYPTO_EXECUTE_3(crypto,
478 CRYPTO_CMD_INSTR_SHA,
479 CRYPTO_CMD_INSTR_MADD32,
480 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
485 while (blockLen < 56)
486 p8ShaBlock[blockLen++] = 0;
490 uint64_t msgLenInBits = msgLen << 3;
491 temp = msgLenInBits >> 32;
492 *(uint32_t *)&p8ShaBlock[56] = SWAP32(temp);
493 temp = msgLenInBits & 0xFFFFFFFF;
494 *(uint32_t *)&p8ShaBlock[60] = SWAP32(temp);
498 CRYPTO_QDataWrite(&crypto->QDATA1BIG, shaBlock);
501 CRYPTO_EXECUTE_3(crypto,
502 CRYPTO_CMD_INSTR_SHA,
503 CRYPTO_CMD_INSTR_MADD32,
504 CRYPTO_CMD_INSTR_DDATA0TODDATA1);
507 CRYPTO_DDataRead(&crypto->DDATA0BIG, (uint32_t *)msgDigest);
517 __STATIC_INLINE
void cryptoBigintZeroize(uint32_t * words32bits,
520 while (num32bitWords--)
531 __STATIC_INLINE
void cryptoBigintIncrement(uint32_t * words32bits,
535 for (i=0; i<num32bitWords; i++)
536 if (++words32bits[i] != 0)
558 void CRYPTO_Mul(CRYPTO_TypeDef * crypto,
559 uint32_t * A,
int aSize,
560 uint32_t * B,
int bSize,
561 uint32_t * R,
int rSize)
567 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
568 int numWordsLastOperandA = (aSize&PARTIAL_OPERAND_WIDTH_MASK)>>5;
569 int numPartialOperandsA = numWordsLastOperandA ?
570 (aSize >> PARTIAL_OPERAND_WIDTH_LOG2) + 1 :
571 aSize >> PARTIAL_OPERAND_WIDTH_LOG2;
572 int numWordsLastOperandB = (bSize&PARTIAL_OPERAND_WIDTH_MASK)>>5;
573 int numPartialOperandsB = numWordsLastOperandB ?
574 (bSize >> PARTIAL_OPERAND_WIDTH_LOG2) + 1 :
575 bSize >> PARTIAL_OPERAND_WIDTH_LOG2;
576 int numWordsLastOperandR = (rSize&PARTIAL_OPERAND_WIDTH_MASK)>>5;
577 int numPartialOperandsR = numWordsLastOperandR ?
578 (rSize >> PARTIAL_OPERAND_WIDTH_LOG2) + 1 :
579 rSize >> PARTIAL_OPERAND_WIDTH_LOG2;
580 EFM_ASSERT(numPartialOperandsA + numPartialOperandsB <= numPartialOperandsR);
582 int numPartialOperandsA = aSize >> PARTIAL_OPERAND_WIDTH_LOG2;
583 int numPartialOperandsB = bSize >> PARTIAL_OPERAND_WIDTH_LOG2;
584 EFM_ASSERT((aSize & PARTIAL_OPERAND_WIDTH_MASK) == 0);
585 EFM_ASSERT((bSize & PARTIAL_OPERAND_WIDTH_MASK) == 0);
587 EFM_ASSERT(aSize + bSize <= rSize);
590 cryptoBigintZeroize(R, rSize >> 5);
593 crypto->WAC = CRYPTO_WAC_MULWIDTH_MUL128 | CRYPTO_WAC_RESULTWIDTH_256BIT;
600 CRYPTO_CTRL_DMA0RSEL_DATA0 | CRYPTO_CTRL_DMA0MODE_FULL |
601 CRYPTO_CTRL_DMA1RSEL_DATA1 | CRYPTO_CTRL_DMA1MODE_FULL;
603 CRYPTO_EXECUTE_4(crypto,
604 CRYPTO_CMD_INSTR_CCLR,
605 CRYPTO_CMD_INSTR_CLR,
607 CRYPTO_CMD_INSTR_DDATA0TODDATA2,
608 CRYPTO_CMD_INSTR_SELDDATA1DDATA3);
617 CRYPTO_SEQ_LOAD_10(crypto,
620 CRYPTO_CMD_INSTR_DMA0TODATA,
622 CRYPTO_CMD_INSTR_DDATA2TODDATA1,
625 CRYPTO_CMD_INSTR_DATA1TODATA0,
628 CRYPTO_CMD_INSTR_DMA1TODATA,
631 CRYPTO_CMD_INSTR_MULO,
636 CRYPTO_CMD_INSTR_SELDDATA0DDATA2,
637 CRYPTO_CMD_INSTR_ADDIC,
640 CRYPTO_CMD_INSTR_DDATA0TODDATA2,
641 CRYPTO_CMD_INSTR_DATATODMA0,
643 CRYPTO_CMD_INSTR_SELDDATA2DDATA3
648 for(i=0; i<numPartialOperandsA; i++)
651 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
652 if ( (numWordsLastOperandA != 0) && ( i == numPartialOperandsA-1 ) )
653 CRYPTO_DataWriteVariableSize(&crypto->DATA2,
654 &A[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS],
655 numWordsLastOperandA);
657 CRYPTO_DataWrite(&crypto->DATA2, &A[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
659 CRYPTO_DataWrite(&crypto->DATA2, &A[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
663 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
664 if ( (numWordsLastOperandR != 0) && ( i == numPartialOperandsR-1 ) )
665 CRYPTO_DataWriteVariableSize(&crypto->DATA1,
666 &R[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS],
667 numWordsLastOperandR);
669 CRYPTO_DataWrite(&crypto->DATA1, &R[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
671 CRYPTO_DataWrite(&crypto->DATA1, &R[i*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
675 crypto->CMD = CRYPTO_CMD_INSTR_CCLR;
678 crypto->SEQCTRL = CRYPTO_SEQCTRL_BLOCKSIZE_16BYTES
679 | (PARTIAL_OPERAND_WIDTH_IN_BYTES * numPartialOperandsB);
682 CRYPTO_InstructionSequenceExecute(crypto);
684 for (j=0; j<numPartialOperandsB; j++)
688 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
689 if ( (numWordsLastOperandB != 0) && ( j == numPartialOperandsB-1 ) )
690 CRYPTO_DataWriteVariableSize(&crypto->DATA0,
691 &B[j*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS],
692 numWordsLastOperandB);
694 CRYPTO_DataWrite(&crypto->DATA0,
695 &B[j*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
697 CRYPTO_DataWrite(&crypto->DATA0,
698 &B[j*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
703 #ifdef USE_VARIABLE_SIZED_DATA_LOADS
704 if ( (numWordsLastOperandR != 0) && ( (i+j+1) == numPartialOperandsR-1 ) )
705 CRYPTO_DataWriteVariableSize(&crypto->DATA1,
706 &R[(i+j+1)*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS],
707 numWordsLastOperandR);
709 CRYPTO_DataWrite(&crypto->DATA1,
710 &R[(i+j+1)*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
712 CRYPTO_DataWrite(&crypto->DATA1,
713 &R[(i+j+1)*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
716 CRYPTO_DataRead(&crypto->DATA0,
717 &R[(i+j)*PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
722 if (CRYPTO_CarryIsSet(crypto))
723 cryptoBigintIncrement(&R[(i+numPartialOperandsB+1)
724 *PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS],
725 (numPartialOperandsA-i-1)
726 *PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS);
728 CRYPTO_DataRead(&crypto->DATA1,
729 &R[(i+numPartialOperandsB)
730 * PARTIAL_OPERAND_WIDTH_IN_32BIT_WORDS]);
800 void CRYPTO_AES_CBC128(CRYPTO_TypeDef * crypto,
808 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
809 CRYPTO_AES_CBCx(crypto, out, in, len, key, iv, encrypt, cryptoKey128Bits);
846 void CRYPTO_AES_CBC256(CRYPTO_TypeDef * crypto,
854 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
855 CRYPTO_AES_CBCx(crypto, out, in, len, key, iv, encrypt, cryptoKey256Bits);
919 void CRYPTO_AES_CFB128(CRYPTO_TypeDef * crypto,
927 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
928 CRYPTO_AES_CFBx(crypto, out, in, len, key, iv, encrypt, cryptoKey128Bits);
962 void CRYPTO_AES_CFB256(CRYPTO_TypeDef * crypto,
970 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
971 CRYPTO_AES_CFBx(crypto, out, in, len, key, iv, encrypt, cryptoKey256Bits);
1038 void CRYPTO_AES_CTR128(CRYPTO_TypeDef * crypto,
1042 const uint8_t * key,
1044 CRYPTO_AES_CtrFuncPtr_TypeDef ctrFunc)
1046 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
1047 CRYPTO_AES_CTRx(crypto, out, in, len, key, ctr, ctrFunc, cryptoKey128Bits);
1084 void CRYPTO_AES_CTR256(CRYPTO_TypeDef * crypto,
1088 const uint8_t * key,
1090 CRYPTO_AES_CtrFuncPtr_TypeDef ctrFunc)
1092 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
1093 CRYPTO_AES_CTRx(crypto, out, in, len, key, ctr, ctrFunc, cryptoKey256Bits);
1110 void CRYPTO_AES_CTRUpdate32Bit(uint8_t * ctr)
1112 uint32_t * _ctr = (uint32_t *) ctr;
1114 _ctr[3] = __REV(__REV(_ctr[3]) + 1);
1135 void CRYPTO_AES_DecryptKey128(CRYPTO_TypeDef * crypto,
1139 uint32_t * _out = (uint32_t *) out;
1140 const uint32_t * _in = (
const uint32_t *) in;
1143 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
1146 CRYPTO_BurstToCrypto(&crypto->KEYBUF, &_in[0]);
1149 crypto->CMD = CRYPTO_CMD_INSTR_AESENC;
1152 CRYPTO_BurstFromCrypto(&crypto->KEY, &_out[0]);
1173 void CRYPTO_AES_DecryptKey256(CRYPTO_TypeDef * crypto,
1177 uint32_t * _out = (uint32_t *) out;
1178 const uint32_t * _in = (
const uint32_t *) in;
1181 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
1184 CRYPTO_BurstToCrypto(&crypto->KEYBUF, &_in[0]);
1185 CRYPTO_BurstToCrypto(&crypto->KEYBUF, &_in[4]);
1188 crypto->CMD = CRYPTO_CMD_INSTR_AESENC;
1191 CRYPTO_BurstFromCrypto(&crypto->KEY, &_out[0]);
1192 CRYPTO_BurstFromCrypto(&crypto->KEY, &_out[4]);
1250 void CRYPTO_AES_ECB128(CRYPTO_TypeDef * crypto,
1254 const uint8_t * key,
1257 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
1258 CRYPTO_AES_ECBx(crypto, out, in, len, key, encrypt, cryptoKey128Bits);
1292 void CRYPTO_AES_ECB256(CRYPTO_TypeDef * crypto,
1296 const uint8_t * key,
1299 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
1300 CRYPTO_AES_ECBx(crypto, out, in, len, key, encrypt, cryptoKey256Bits);
1363 void CRYPTO_AES_OFB128(CRYPTO_TypeDef * crypto,
1367 const uint8_t * key,
1370 crypto->CTRL = CRYPTO_CTRL_AES_AES128;
1371 CRYPTO_AES_OFBx(crypto, out, in, len, key, iv, cryptoKey128Bits);
1402 void CRYPTO_AES_OFB256(CRYPTO_TypeDef * crypto,
1406 const uint8_t * key,
1409 crypto->CTRL = CRYPTO_CTRL_AES_AES256;
1410 CRYPTO_AES_OFBx(crypto, out, in, len, key, iv, cryptoKey256Bits);
1453 static void CRYPTO_AES_CBCx(CRYPTO_TypeDef * crypto,
1457 const uint8_t * key,
1460 CRYPTO_KeyWidth_TypeDef keyWidth)
1462 EFM_ASSERT(!(len % CRYPTO_AES_BLOCKSIZE));
1467 CRYPTO_KeyBufWrite(crypto, (uint32_t *)key, keyWidth);
1471 CRYPTO_DataWrite(&crypto->DATA0, (uint32_t *)iv);
1474 CRYPTO_CMD_INSTR_DATA1TODATA0XOR << _CRYPTO_SEQ0_INSTR0_SHIFT |
1475 CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR1_SHIFT;
1477 CRYPTO_AES_ProcessLoop(crypto, len,
1478 &crypto->DATA1, (uint32_t *) in,
1479 &crypto->DATA0, (uint32_t *) out);
1483 CRYPTO_DataWrite(&crypto->DATA2, (uint32_t *) iv);
1486 CRYPTO_CMD_INSTR_DATA1TODATA0 << _CRYPTO_SEQ0_INSTR0_SHIFT |
1487 CRYPTO_CMD_INSTR_AESDEC << _CRYPTO_SEQ0_INSTR1_SHIFT |
1488 CRYPTO_CMD_INSTR_DATA2TODATA0XOR << _CRYPTO_SEQ0_INSTR2_SHIFT |
1489 CRYPTO_CMD_INSTR_DATA1TODATA2 << _CRYPTO_SEQ0_INSTR3_SHIFT;
1497 CRYPTO_AES_ProcessLoop(crypto, len,
1498 &crypto->DATA1, (uint32_t *) in,
1499 &crypto->DATA0, (uint32_t *) out);
1537 static void CRYPTO_AES_CFBx(CRYPTO_TypeDef * crypto,
1541 const uint8_t * key,
1544 CRYPTO_KeyWidth_TypeDef keyWidth)
1546 EFM_ASSERT(!(len % CRYPTO_AES_BLOCKSIZE));
1552 CRYPTO_KeyBufWrite(crypto, (uint32_t *)key, keyWidth);
1558 CRYPTO_DataWrite(&crypto->DATA0, (uint32_t *)iv);
1561 CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR0_SHIFT |
1562 CRYPTO_CMD_INSTR_DATA1TODATA0XOR << _CRYPTO_SEQ0_INSTR1_SHIFT;
1564 CRYPTO_AES_ProcessLoop(crypto, len,
1565 &crypto->DATA1, (uint32_t *)in,
1566 &crypto->DATA0, (uint32_t *)out
1572 CRYPTO_DataWrite(&crypto->DATA2, (uint32_t *)iv);
1575 CRYPTO_CMD_INSTR_DATA2TODATA0 << _CRYPTO_SEQ0_INSTR0_SHIFT |
1576 CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR1_SHIFT |
1577 CRYPTO_CMD_INSTR_DATA1TODATA0XOR << _CRYPTO_SEQ0_INSTR2_SHIFT |
1578 CRYPTO_CMD_INSTR_DATA1TODATA2 << _CRYPTO_SEQ0_INSTR3_SHIFT;
1581 CRYPTO_AES_ProcessLoop(crypto, len,
1582 &crypto->DATA1, (uint32_t *)in,
1583 &crypto->DATA0, (uint32_t *)out
1625 static void CRYPTO_AES_CTRx(CRYPTO_TypeDef * crypto,
1629 const uint8_t * key,
1631 CRYPTO_AES_CtrFuncPtr_TypeDef ctrFunc,
1632 CRYPTO_KeyWidth_TypeDef keyWidth)
1636 EFM_ASSERT(!(len % CRYPTO_AES_BLOCKSIZE));
1639 crypto->CTRL |= CRYPTO_CTRL_INCWIDTH_INCWIDTH4;
1642 CRYPTO_KeyBufWrite(crypto, (uint32_t *)key, keyWidth);
1644 CRYPTO_DataWrite(&crypto->DATA1, (uint32_t *) ctr);
1646 crypto->SEQ0 = CRYPTO_CMD_INSTR_DATA1TODATA0 << _CRYPTO_SEQ0_INSTR0_SHIFT |
1647 CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR1_SHIFT |
1648 CRYPTO_CMD_INSTR_DATA0TODATA3 << _CRYPTO_SEQ0_INSTR2_SHIFT |
1649 CRYPTO_CMD_INSTR_DATA1INC << _CRYPTO_SEQ0_INSTR3_SHIFT;
1651 crypto->SEQ1 = CRYPTO_CMD_INSTR_DATA2TODATA0XOR << _CRYPTO_SEQ1_INSTR4_SHIFT;
1653 CRYPTO_AES_ProcessLoop(crypto, len,
1654 &crypto->DATA2, (uint32_t *) in,
1655 &crypto->DATA0, (uint32_t *) out);
1657 CRYPTO_DataRead(&crypto->DATA1, (uint32_t *) ctr);
1693 static void CRYPTO_AES_ECBx(CRYPTO_TypeDef * crypto,
1697 const uint8_t * key,
1699 CRYPTO_KeyWidth_TypeDef keyWidth)
1701 EFM_ASSERT(!(len % CRYPTO_AES_BLOCKSIZE));
1705 CRYPTO_KeyBufWrite(crypto, (uint32_t *)key, keyWidth);
1710 (CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR0_SHIFT |
1711 CRYPTO_CMD_INSTR_DATA0TODATA1 << _CRYPTO_SEQ0_INSTR1_SHIFT);
1716 (CRYPTO_CMD_INSTR_AESDEC << _CRYPTO_SEQ0_INSTR0_SHIFT |
1717 CRYPTO_CMD_INSTR_DATA0TODATA1 << _CRYPTO_SEQ0_INSTR1_SHIFT);
1720 CRYPTO_AES_ProcessLoop(crypto, len,
1721 &crypto->DATA0, (uint32_t *) in,
1722 &crypto->DATA1, (uint32_t *) out);
1756 static void CRYPTO_AES_OFBx(CRYPTO_TypeDef * crypto,
1760 const uint8_t * key,
1762 CRYPTO_KeyWidth_TypeDef keyWidth)
1764 EFM_ASSERT(!(len % CRYPTO_AES_BLOCKSIZE));
1768 CRYPTO_KeyBufWrite(crypto, (uint32_t *)key, keyWidth);
1770 CRYPTO_DataWrite(&crypto->DATA2, (uint32_t *)iv);
1773 CRYPTO_CMD_INSTR_DATA0TODATA1 << _CRYPTO_SEQ0_INSTR0_SHIFT |
1774 CRYPTO_CMD_INSTR_DATA2TODATA0 << _CRYPTO_SEQ0_INSTR1_SHIFT |
1775 CRYPTO_CMD_INSTR_AESENC << _CRYPTO_SEQ0_INSTR2_SHIFT |
1776 CRYPTO_CMD_INSTR_DATA0TODATA2 << _CRYPTO_SEQ0_INSTR3_SHIFT;
1778 CRYPTO_CMD_INSTR_DATA1TODATA0XOR << _CRYPTO_SEQ1_INSTR4_SHIFT |
1779 CRYPTO_CMD_INSTR_DATA0TODATA1 << _CRYPTO_SEQ1_INSTR5_SHIFT;
1781 CRYPTO_AES_ProcessLoop(crypto, len,
1782 &crypto->DATA0, (uint32_t *) in,
1783 &crypto->DATA1, (uint32_t *) out);
1814 static inline void CRYPTO_AES_ProcessLoop(CRYPTO_TypeDef * crypto,
1816 CRYPTO_DataReg_TypeDef inReg,
1818 CRYPTO_DataReg_TypeDef outReg,
1821 len /= CRYPTO_AES_BLOCKSIZE;
1822 crypto->SEQCTRL = 16 << _CRYPTO_SEQCTRL_LENGTHA_SHIFT;
1827 CRYPTO_DataWrite(inReg, (uint32_t *)in);
1829 crypto->CMD = CRYPTO_CMD_SEQSTART;
1832 CRYPTO_DataRead(outReg, (uint32_t *)out);
Emlib peripheral API "assert" implementation.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
Cryptography accelerator peripheral API.