ADuCM4x50 Device Drivers API Reference Manual  Release 4.0.0.0
adi_pkstor.c
1 
14 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT))
15 
16 
59 /*======== I N C L U D E ========*/
60 
61 /* This source file is meant to be included by adi_crypto.c CRYPTO device driver.
62  It is not meant to be compiled stand-alone. It depends on includes provided
63  by the CRYPTO device driver source file.
64 */
65 
66 
67 /* The entire content of this file is dedicated to supporting PKSTOR functionality,
68  and so it is all bracketed by a single ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT macro.
69 */
70 
73 /* common PKSTOR command dispatcher */
74 static ADI_CRYPTO_RESULT pkCmd (ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_CMD const cmd, uint8_t const index);
75 
103 ADI_CRYPTO_RESULT adi_crypto_pk_EnablePKSTOR (ADI_CRYPTO_HANDLE const hDevice, bool const bEnable)
104 {
105  static uint32_t savedConfig;
106  static bool bEnableState = false;
107 
108 #ifdef ADI_DEBUG
109  ADI_CRYPTO_RESULT result;
110 
111  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
112  return result;
113  }
114 #endif /* ADI_DEBUG */
115 
116  /* reconfigure Crypto Config Register for PKSTOR operation */
117  if (bEnable) {
118  if (true == bEnableState) {
119  return ADI_CRYPTO_PK_ALREADY_ENABLED;
120  } else {
121  savedConfig = hDevice->pDev->CFG;
122  hDevice->pDev->CFG = PK_CONFIG_BITS;
123  bEnableState = true;
124  }
125  } else {
126  if (true == bEnableState) {
127  hDevice->pDev->CFG = savedConfig;
128  bEnableState = false;
129  } else {
130  return ADI_CRYPTO_PK_ALREADY_DISABLED;
131  }
132  }
133 
134  return ADI_CRYPTO_SUCCESS;
135 }
136 
137 
167 ADI_CRYPTO_RESULT adi_crypto_pk_SetValString (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pValStr)
168 {
169  uint32_t volatile *pReg;
170  uint8_t *pData8;
171 
172 #ifdef ADI_DEBUG
173  ADI_CRYPTO_RESULT result;
174 
175  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
176  return result;
177  }
178  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
179  return ADI_CRYPTO_PK_NOT_ENABLED;
180  }
181 #endif /* ADI_DEBUG */
182 
183  /* store string into validation registers */
184  pReg = &hDevice->pDev->KUWVALSTR1;
185  pData8 = pValStr;
186  for (uint32_t count = 0u; count < NUM_PKSTOR_VAL_STRING_WORDS; count++, pReg++) {
187  *pReg = u32FromU8p(pData8);
188  pData8 += sizeof(uint32_t);
189  }
190 
191  return ADI_CRYPTO_SUCCESS;
192 }
193 
194 
212 ADI_CRYPTO_RESULT adi_crypto_pk_GetValString (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pValStr)
213 {
214  uint32_t volatile *pReg;
215  uint32_t data32;
216  uint8_t *pData8;
217 
218 #ifdef ADI_DEBUG
219  ADI_CRYPTO_RESULT result;
220 
221  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
222  return result;
223  }
224  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
225  return ADI_CRYPTO_PK_NOT_ENABLED;
226  }
227 #endif /* ADI_DEBUG */
228 
229  /* copy string out of validation registers */
230  pData8 = pValStr;
231  pReg = &hDevice->pDev->KUWVALSTR1;
232  for (uint32_t count = 0u; count < NUM_PKSTOR_VAL_STRING_WORDS; count++, pReg++) {
233  data32 = *pReg;
234  *pData8 = (uint8_t)((data32 & 0x000000ff) >> 0); pData8++;
235  *pData8 = (uint8_t)((data32 & 0x0000ff00) >> 8); pData8++;
236  *pData8 = (uint8_t)((data32 & 0x00ff0000) >> 16); pData8++;
237  *pData8 = (uint8_t)((data32 & 0xff000000) >> 24); pData8++;
238  }
239 
240  return ADI_CRYPTO_SUCCESS;
241 }
242 
243 
270 ADI_CRYPTO_RESULT adi_crypto_pk_SetKuwLen (ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_KUW_LEN const kuwDataLen)
271 {
272 #ifdef ADI_DEBUG
274  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
275  return result;
276  }
277  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
278  return ADI_CRYPTO_PK_NOT_ENABLED;
279  }
280  /* Note: 512-bit keys are not storable in flash... it is just here for HMAC support, so most other PKSTOR command exempt it... */
281  if ((ADI_PK_KUW_LEN_128 != kuwDataLen) && (ADI_PK_KUW_LEN_256 != kuwDataLen) && (ADI_PK_KUW_LEN_512 != kuwDataLen)) {
282  return ADI_CRYPTO_PK_INVALID_KUWLEN;
283  }
284 #endif
285 
286  /* set KUW key length register field */
287  CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_KUWKEYLEN);
288  SET_BITS(hDevice->pDev->CFG, (uint32_t)kuwDataLen); /* pre-shifted */
289 
290  return ADI_CRYPTO_SUCCESS;
291 }
292 
293 
313 ADI_CRYPTO_RESULT adi_crypto_pk_SetKuwReg (ADI_CRYPTO_HANDLE const hDevice, uint8_t * const pKuwData)
314 {
316  uint32_t volatile *pReg;
317  uint8_t *pData8;
318  uint32_t num32;
319  uint32_t count;
320 
321 #ifdef ADI_DEBUG
322  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
323  return result;
324  }
325  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
326  return ADI_CRYPTO_PK_NOT_ENABLED;
327  }
328 #endif /* ADI_DEBUG */
329 
330  /* set number of words to write to the 32-bit KUW registers
331  according to currently active KUW length value */
332  switch (hDevice->pDev->CFG & BITM_CRYPT_CFG_KUWKEYLEN) {
333  case ADI_PK_KUW_LEN_128:
334  num32 = 4u;
335  break;
336  case ADI_PK_KUW_LEN_256:
337  num32 = 8u;
338  break;
339  case ADI_PK_KUW_LEN_512:
340  num32 = 16u;
341  break;
342  default:
343 #ifdef ADI_DEBUG
344  return ADI_CRYPTO_PK_INVALID_KUWLEN;
345 #else
346  num32 = 0u;
347  break;
348 #endif /* ADI_DEBUG */
349  }
350 
351  /* write the specified KUW register set according to kuwDataLen */
352  pReg = &hDevice->pDev->KUW0;
353  pData8 = pKuwData;
354  for (count = 0u; count < num32; count++, pReg++) {
355  *pReg = u32FromU8p(pData8);
356  pData8 += sizeof(uint32_t);
357  }
358 
359  /* zero out unused KUW registers... so entire KUW register set is written */
360  for (; count < 16; count++, pReg++) {
361  *pReg = 0ul;
362  }
363 
364  return result;
365 }
366 
367 
410 ADI_CRYPTO_RESULT adi_crypto_pk_WrapKuwReg (ADI_CRYPTO_HANDLE const hDevice)
411 {
412 #ifdef ADI_DEBUG
413  ADI_CRYPTO_RESULT result;
414 
415  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
416  return result;
417  }
418  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
419  return ADI_CRYPTO_PK_NOT_ENABLED;
420  }
421 #endif /* ADI_DEBUG */
422 
423  /* issue the wrap command */
424  return pkCmd(hDevice, ADI_PK_CMD_WRAP_KUW, 0);
425 }
426 
427 
473 ADI_CRYPTO_RESULT adi_crypto_pk_UnwrapKuwReg (ADI_CRYPTO_HANDLE const hDevice)
474 {
475 #ifdef ADI_DEBUG
476  ADI_CRYPTO_RESULT result;
477 
478  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
479  return result;
480  }
481  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
482  return ADI_CRYPTO_PK_NOT_ENABLED;
483  }
484 #endif /* ADI_DEBUG */
485 
486  /* issue the unwrap command */
487  return pkCmd(hDevice, ADI_PK_CMD_UNWRAP_KUW, 0);
488 }
489 
490 
509 ADI_CRYPTO_RESULT adi_crypto_pk_ResetKuwReg (ADI_CRYPTO_HANDLE const hDevice)
510 {
511 #ifdef ADI_DEBUG
512  ADI_CRYPTO_RESULT result;
513 
514  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
515  return result;
516  }
517  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
518  return ADI_CRYPTO_PK_NOT_ENABLED;
519  }
520 #endif /* ADI_DEBUG */
521 
522  /* issue the reset KUW command */
523  return pkCmd(hDevice, ADI_PK_CMD_RESET_KUW, 0);
524 }
525 
526 
553 ADI_CRYPTO_RESULT adi_crypto_pk_UseDecryptedKey (ADI_CRYPTO_HANDLE const hDevice)
554 {
555 #ifdef ADI_DEBUG
556  ADI_CRYPTO_RESULT result;
557 
558  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
559  return result;
560  }
561  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
562  return ADI_CRYPTO_PK_NOT_ENABLED;
563  }
564 #endif /* ADI_DEBUG */
565 
566  /* issue the use decrypted key command */
567  return pkCmd(hDevice, ADI_PK_CMD_USE_KEY, 0);
568 }
569 
570 
594 ADI_CRYPTO_RESULT adi_crypto_pk_LoadDeviceKey (ADI_CRYPTO_HANDLE const hDevice)
595 {
596 #ifdef ADI_DEBUG
597  ADI_CRYPTO_RESULT result;
598 
599  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
600  return result;
601  }
602  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
603  return ADI_CRYPTO_PK_NOT_ENABLED;
604  }
605 #endif /* ADI_DEBUG */
606 
607  /* NOTE: "load" device key size is fixed at 256-bit key and
608  AESKEYLEN should reflect this after command completes */
609 
610  /* issue the use default device key command */
611  return pkCmd(hDevice, ADI_PK_CMD_USE_DEV_KEY, 0);
612 }
613 
614 
642 ADI_CRYPTO_RESULT adi_crypto_pk_RetrieveKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
643 {
644 #ifdef ADI_DEBUG
645  ADI_CRYPTO_RESULT result;
646 
647  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
648  return result;
649  }
650  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
651  return ADI_CRYPTO_PK_NOT_ENABLED;
652  }
653 #endif /* ADI_DEBUG */
654 
655  /* issue the retrieve key command */
656  return pkCmd(hDevice, ADI_PK_CMD_RETRIEVE_KEY, index);
657 }
658 
659 
687 ADI_CRYPTO_RESULT adi_crypto_pk_StoreKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
688 {
689 #ifdef ADI_DEBUG
690  ADI_CRYPTO_RESULT result;
691 
692  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
693  return result;
694  }
695  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
696  return ADI_CRYPTO_PK_NOT_ENABLED;
697  }
698 #endif /* ADI_DEBUG */
699 
700  /* issue the store key command */
701  return pkCmd(hDevice, ADI_PK_CMD_STORE_KEY, index);
702 }
703 
704 
740 ADI_CRYPTO_RESULT adi_crypto_pk_DestroyKey (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
741 {
742 #ifdef ADI_DEBUG
743  ADI_CRYPTO_RESULT result;
744 
745  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
746  return result;
747  }
748  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
749  return ADI_CRYPTO_PK_NOT_ENABLED;
750  }
751 #endif /* ADI_DEBUG */
752 
753  /* issue the erase key command */
754  return pkCmd(hDevice, ADI_PK_CMD_ERASE_KEY, index);
755 }
756 
757 
782 ADI_CRYPTO_RESULT adi_crypto_pk_ErasePage (ADI_CRYPTO_HANDLE const hDevice, uint8_t const index)
783 {
784 #ifdef ADI_DEBUG
785  ADI_CRYPTO_RESULT result;
786 
787  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
788  return result;
789  }
790  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
791  return ADI_CRYPTO_PK_NOT_ENABLED;
792  }
793 #endif /* ADI_DEBUG */
794 
795  /* issue the erase page command */
796  return pkCmd(hDevice, ADI_PK_CMD_ERASE_PAGE, index);
797 }
798 
799 
803 /* common dispatcher for simple PKSTOR commands */
804 static ADI_CRYPTO_RESULT pkCmd(ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_PK_CMD const cmd, uint8_t const index)
805 {
806  volatile uint32_t status;
807 
808 #ifdef ADI_DEBUG
809  ADI_CRYPTO_RESULT result;
810 
811  if ((result = ValidateHandle(hDevice)) != ADI_CRYPTO_SUCCESS) {
812  return result;
813  }
814  if (PK_CONFIG_BITS != (PK_CONFIG_BITS & hDevice->pDev->CFG)) {
815  return ADI_CRYPTO_PK_NOT_ENABLED;
816  }
817  if (~BITM_CRYPT_PRKSTORCFG_KEY_INDEX & index) {
818  return ADI_CRYPTO_PK_INVALID_KUWLEN;
819  }
820 #endif /* ADI_DEBUG */
821 
822  /* clear status */
823  hDevice->pDev->STAT = 0xffffffff;
824 
825  /* issue command (cmd is pre-shifted) */
826  hDevice->pDev->PRKSTORCFG = cmd | (index << BITP_CRYPT_PRKSTORCFG_KEY_INDEX);
827 
828  /* wait for PKSTOR command DONE status */
829  while (1) {
830  status = hDevice->pDev->STAT;
831  if (status & BITM_CRYPT_STAT_PRKSTOR_CMD_DONE)
832  break;
833 
834  /* fault checks... */
835  if (status & BITM_CRYPT_STAT_PRKSTOR_CMD_FAIL)
836  return ADI_CRYPTO_PK_CMD_FAULT;
837 
838  if (status & BITM_CRYPT_STAT_PRKSTOR_RET_STATUS)
839  return ADI_CRYPTO_PK_CMD_ECC_FAULT;
840  }
841 
842  return ADI_CRYPTO_SUCCESS;
843 }
844 
845 
848 #endif /* ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT */
849 
ADI_CRYPTO_RESULT
Definition: adi_crypto.h:38
struct __ADI_CRYPTO_DEV_DATA_TYPE * ADI_CRYPTO_HANDLE
Definition: adi_crypto.h:109