ADuCM302x Device Drivers API Reference Manual  Release 3.1.2.0
adi_pkstor.c
1 
46 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT))
47 
48 
91 /*======== I N C L U D E ========*/
92 
93 /* This source file is meant to be included by adi_crypto.c CRYPTO device driver.
94  It is not meant to be compiled stand-alone. It depends on includes provided
95  by the CRYPTO device driver source file.
96 */
97 
98 
99 /* The entire content of this file is dedicated to supporting PKSTOR functionality,
100  and so it is all bracketed by a single ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT macro.
101 */
102 
105 /* common PKSTOR command dispatcher */
106 static ADI_CRYPTO_RESULT pkCmd (ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_CMD const cmd, uint8_t const index);
107 
135 ADI_CRYPTO_RESULT adi_crypto_pk_EnablePKSTOR (ADI_CRYPTO_HANDLE const hDevice, bool const bEnable)
136 {
137  static uint32_t savedConfig;
138  static bool bEnableState = false;
139 
140 #ifdef ADI_DEBUG
141  ADI_CRYPTO_RESULT result;
142 
143  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
144  return result;
145  }
146 #endif /* ADI_DEBUG */
147 
148  /* reconfigure Crypto Config Register for PKSTOR operation */
149  if (bEnable) {
150  if (true == bEnableState) {
151  return ADI_CRYPTO_PK_ALREADY_ENABLED;
152  } else {
153  savedConfig = hDevice->pDev->CFG;
154  hDevice->pDev->CFG = PK_CONFIG_BITS;
155  bEnableState = true;
156  }
157  } else {
158  if (true == bEnableState) {
159  hDevice->pDev->CFG = savedConfig;
160  bEnableState = false;
161  } else {
162  return ADI_CRYPTO_PK_ALREADY_DISABLED;
163  }
164  }
165 
166  return ADI_CRYPTO_SUCCESS;
167 }
168 
169 
199 ADI_CRYPTO_RESULT adi_crypto_pk_SetValString (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pValStr)
200 {
201  uint32_t volatile *pReg;
202  uint8_t *pData8;
203 
204 #ifdef ADI_DEBUG
205  ADI_CRYPTO_RESULT result;
206 
207  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
208  return result;
209  }
210  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
211  return ADI_CRYPTO_PK_NOT_ENABLED;
212  }
213 #endif /* ADI_DEBUG */
214 
215  /* store string into validation registers */
216  pReg = &hDevice->pDev->KUWVALSTR1;
217  pData8 = pValStr;
218  for (uint32_t count = 0u; count < NUM_PKSTOR_VAL_STRING_WORDS; count++, pReg++) {
219  *pReg = u32FromU8p(pData8);
220  pData8 += sizeof(uint32_t);
221  }
222 
223  return ADI_CRYPTO_SUCCESS;
224 }
225 
226 
244 ADI_CRYPTO_RESULT adi_crypto_pk_GetValString (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pValStr)
245 {
246  uint32_t volatile *pReg;
247  uint32_t data32;
248  uint8_t *pData8;
249 
250 #ifdef ADI_DEBUG
251  ADI_CRYPTO_RESULT result;
252 
253  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
254  return result;
255  }
256  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
257  return ADI_CRYPTO_PK_NOT_ENABLED;
258  }
259 #endif /* ADI_DEBUG */
260 
261  /* copy string out of validation registers */
262  pData8 = pValStr;
263  pReg = &hDevice->pDev->KUWVALSTR1;
264  for (uint32_t count = 0u; count < NUM_PKSTOR_VAL_STRING_WORDS; count++, pReg++) {
265  data32 = *pReg;
266  *pData8 = (uint8_t)((data32 & 0x000000ff) >> 0); pData8++;
267  *pData8 = (uint8_t)((data32 & 0x0000ff00) >> 8); pData8++;
268  *pData8 = (uint8_t)((data32 & 0x00ff0000) >> 16); pData8++;
269  *pData8 = (uint8_t)((data32 & 0xff000000) >> 24); pData8++;
270  }
271 
272  return ADI_CRYPTO_SUCCESS;
273 }
274 
275 
302 ADI_CRYPTO_RESULT adi_crypto_pk_SetKuwLen (ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_KUW_LEN const kuwDataLen)
303 {
304 #ifdef ADI_DEBUG
306  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
307  return result;
308  }
309  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
310  return ADI_CRYPTO_PK_NOT_ENABLED;
311  }
312  /* Note: 512-bit keys are not storable in flash... it is just here for HMAC support, so most other PKSTOR command exempt it... */
313  if ((ADI_PK_KUW_LEN_128 != kuwDataLen) && (ADI_PK_KUW_LEN_256 != kuwDataLen) && (ADI_PK_KUW_LEN_512 != kuwDataLen)) {
314  return ADI_CRYPTO_PK_INVALID_KUWLEN;
315  }
316 #endif
317 
318  /* set KUW key length register field */
319  CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_KUWKEYLEN);
320  SET_BITS(hDevice->pDev->CFG, (uint32_t)kuwDataLen); /* pre-shifted */
321 
322  return ADI_CRYPTO_SUCCESS;
323 }
324 
325 
345 ADI_CRYPTO_RESULT adi_crypto_pk_SetKuwReg (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pKuwData)
346 {
348  uint32_t volatile *pReg;
349  uint8_t *pData8;
350  uint32_t num32;
351  uint32_t count;
352 
353 #ifdef ADI_DEBUG
354  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
355  return result;
356  }
357  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
358  return ADI_CRYPTO_PK_NOT_ENABLED;
359  }
360 #endif /* ADI_DEBUG */
361 
362  /* set number of words to write to the 32-bit KUW registers
363  according to currently active KUW length value */
364  switch (hDevice->pDev->CFG & BITM_CRYPT_CFG_KUWKEYLEN) {
365  case ADI_PK_KUW_LEN_128:
366  num32 = 4u;
367  break;
368  case ADI_PK_KUW_LEN_256:
369  num32 = 8u;
370  break;
371  case ADI_PK_KUW_LEN_512:
372  num32 = 16u;
373  break;
374  default:
375 #ifdef ADI_DEBUG
376  return ADI_CRYPTO_PK_INVALID_KUWLEN;
377 #else
378  num32 = 0u;
379  break;
380 #endif /* ADI_DEBUG */
381  }
382 
383  /* write the specified KUW register set according to kuwDataLen */
384  pReg = &hDevice->pDev->KUW0;
385  pData8 = pKuwData;
386  for (count = 0u; count < num32; count++, pReg++) {
387  *pReg = u32FromU8p(pData8);
388  pData8 += sizeof(uint32_t);
389  }
390 
391  /* zero out unused KUW registers... so entire KUW register set is written */
392  for (; count < 16; count++, pReg++) {
393  *pReg = 0ul;
394  }
395 
396  return result;
397 }
398 
399 
442 ADI_CRYPTO_RESULT adi_crypto_pk_WrapKuwReg (ADI_CRYPTO_HANDLE const hDevice)
443 {
444 #ifdef ADI_DEBUG
445  ADI_CRYPTO_RESULT result;
446 
447  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
448  return result;
449  }
450  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
451  return ADI_CRYPTO_PK_NOT_ENABLED;
452  }
453 #endif /* ADI_DEBUG */
454 
455  /* issue the wrap command */
456  return pkCmd(hDevice, ADI_PK_CMD_WRAP_KUW, 0);
457 }
458 
459 
505 ADI_CRYPTO_RESULT adi_crypto_pk_UnwrapKuwReg (ADI_CRYPTO_HANDLE const hDevice)
506 {
507 #ifdef ADI_DEBUG
508  ADI_CRYPTO_RESULT result;
509 
510  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
511  return result;
512  }
513  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
514  return ADI_CRYPTO_PK_NOT_ENABLED;
515  }
516 #endif /* ADI_DEBUG */
517 
518  /* issue the unwrap command */
519  return pkCmd(hDevice, ADI_PK_CMD_UNWRAP_KUW, 0);
520 }
521 
522 
541 ADI_CRYPTO_RESULT adi_crypto_pk_ResetKuwReg (ADI_CRYPTO_HANDLE const hDevice)
542 {
543 #ifdef ADI_DEBUG
544  ADI_CRYPTO_RESULT result;
545 
546  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
547  return result;
548  }
549  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
550  return ADI_CRYPTO_PK_NOT_ENABLED;
551  }
552 #endif /* ADI_DEBUG */
553 
554  /* issue the reset KUW command */
555  return pkCmd(hDevice, ADI_PK_CMD_RESET_KUW, 0);
556 }
557 
558 
585 ADI_CRYPTO_RESULT adi_crypto_pk_UseDecryptedKey (ADI_CRYPTO_HANDLE const hDevice)
586 {
587 #ifdef ADI_DEBUG
588  ADI_CRYPTO_RESULT result;
589 
590  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
591  return result;
592  }
593  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
594  return ADI_CRYPTO_PK_NOT_ENABLED;
595  }
596 #endif /* ADI_DEBUG */
597 
598  /* issue the use decrypted key command */
599  return pkCmd(hDevice, ADI_PK_CMD_USE_KEY, 0);
600 }
601 
602 
626 ADI_CRYPTO_RESULT adi_crypto_pk_LoadDeviceKey (ADI_CRYPTO_HANDLE const hDevice)
627 {
628 #ifdef ADI_DEBUG
629  ADI_CRYPTO_RESULT result;
630 
631  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
632  return result;
633  }
634  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
635  return ADI_CRYPTO_PK_NOT_ENABLED;
636  }
637 #endif /* ADI_DEBUG */
638 
639  /* NOTE: "load" device key size is fixed at 256-bit key and
640  AESKEYLEN should reflect this after command completes */
641 
642  /* issue the use default device key command */
643  return pkCmd(hDevice, ADI_PK_CMD_USE_DEV_KEY, 0);
644 }
645 
646 
674 ADI_CRYPTO_RESULT adi_crypto_pk_RetrieveKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
675 {
676 #ifdef ADI_DEBUG
677  ADI_CRYPTO_RESULT result;
678 
679  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
680  return result;
681  }
682  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
683  return ADI_CRYPTO_PK_NOT_ENABLED;
684  }
685 #endif /* ADI_DEBUG */
686 
687  /* issue the retrieve key command */
688  return pkCmd(hDevice, ADI_PK_CMD_RETRIEVE_KEY, index);
689 }
690 
691 
719 ADI_CRYPTO_RESULT adi_crypto_pk_StoreKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
720 {
721 #ifdef ADI_DEBUG
722  ADI_CRYPTO_RESULT result;
723 
724  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
725  return result;
726  }
727  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
728  return ADI_CRYPTO_PK_NOT_ENABLED;
729  }
730 #endif /* ADI_DEBUG */
731 
732  /* issue the store key command */
733  return pkCmd(hDevice, ADI_PK_CMD_STORE_KEY, index);
734 }
735 
736 
772 ADI_CRYPTO_RESULT adi_crypto_pk_DestroyKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
773 {
774 #ifdef ADI_DEBUG
775  ADI_CRYPTO_RESULT result;
776 
777  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
778  return result;
779  }
780  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
781  return ADI_CRYPTO_PK_NOT_ENABLED;
782  }
783 #endif /* ADI_DEBUG */
784 
785  /* issue the erase key command */
786  return pkCmd(hDevice, ADI_PK_CMD_ERASE_KEY, index);
787 }
788 
789 
814 ADI_CRYPTO_RESULT adi_crypto_pk_ErasePage (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
815 {
816 #ifdef ADI_DEBUG
817  ADI_CRYPTO_RESULT result;
818 
819  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
820  return result;
821  }
822  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
823  return ADI_CRYPTO_PK_NOT_ENABLED;
824  }
825 #endif /* ADI_DEBUG */
826 
827  /* issue the erase page command */
828  return pkCmd(hDevice, ADI_PK_CMD_ERASE_PAGE, index);
829 }
830 
831 
835 /* common dispatcher for simple PKSTOR commands */
836 static ADI_CRYPTO_RESULT pkCmd(ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_CMD const cmd, uint8_t const index)
837 {
838  volatile uint32_t status;
839 
840 #ifdef ADI_DEBUG
841  ADI_CRYPTO_RESULT result;
842 
843  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
844  return result;
845  }
846  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
847  return ADI_CRYPTO_PK_NOT_ENABLED;
848  }
849  if (~BITM_CRYPT_PRKSTORCFG_KEY_INDEX & index) {
850  return ADI_CRYPTO_PK_INVALID_KUWLEN;
851  }
852 #endif /* ADI_DEBUG */
853 
854  /* clear status */
855  hDevice->pDev->STAT = 0xffffffff;
856 
857  /* issue command (cmd is pre-shifted) */
858  hDevice->pDev->PRKSTORCFG = cmd | (index << BITP_CRYPT_PRKSTORCFG_KEY_INDEX);
859 
860  /* wait for PKSTOR command DONE status */
861  while (1) {
862  status = hDevice->pDev->STAT;
863  if (status & BITM_CRYPT_STAT_PRKSTOR_CMD_DONE)
864  break;
865 
866  /* fault checks... */
867  if (status & BITM_CRYPT_STAT_PRKSTOR_CMD_FAIL)
868  return ADI_CRYPTO_PK_CMD_FAULT;
869 
870  if (status & BITM_CRYPT_STAT_PRKSTOR_RET_STATUS)
871  return ADI_CRYPTO_PK_CMD_ECC_FAULT;
872  }
873 
874  return ADI_CRYPTO_SUCCESS;
875 }
876 
877 
880 #endif /* ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT */
881 
ADI_CRYPTO_RESULT
Definition: adi_crypto.h:69
struct __ADI_CRYPTO_DEV_DATA_TYPE * ADI_CRYPTO_HANDLE
Definition: adi_crypto.h:140