caam_driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* caam_driver.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef CAAM_DRIVER_H
  22. #define CAAM_DRIVER_H
  23. #if (defined(__QNX__) || defined(__QNXNTO__))
  24. int InitCAAM(void);
  25. #include "caam_qnx.h"
  26. #endif
  27. #ifndef CAAM_BASE
  28. #if (defined(__INTEGRITY) || defined(INTEGRITY))
  29. #define CAAM_BASE 0xf2100000
  30. #define CAAM_PAGE 0xf0100000
  31. #elif defined(__aarch64__)
  32. /* if on an AArch64 system make assumption that it is an i.MX8 QXP */
  33. /* use block of memory set aside for job ring 2 */
  34. #define CAAM_BASE 0x31400000
  35. #define CAAM_PAGE 0x31800000
  36. #elif defined(WOLFSSL_CAAM_IMX6Q)
  37. /* IMX6Q */
  38. #define CAAM_BASE 0x02100000
  39. #define CAAM_PAGE 0x00100000
  40. #else
  41. /* IMX6UL */
  42. #define CAAM_BASE 0x02140000
  43. #define CAAM_PAGE 0x00100000
  44. #endif
  45. #endif /* !CAAM_BASE */
  46. #ifdef WOLFSSL_CAAM_PRINT
  47. #include <stdio.h>
  48. #define WOLFSSL_MSG(in) printf("%s\n", (in))
  49. void DEBUG_PRINT_ARRAY(void* a, int aSz, char* str);
  50. #else
  51. #define WOLFSSL_MSG(in) do {} while (0)
  52. #define DEBUG_PRINT_ARRAY(a,aSz,str) do {} while (0)
  53. #endif
  54. #define CAAM_PAGE_MAX 6
  55. /* max size of 64 word32's */
  56. #define CAAM_DESC_MAX 256
  57. #ifndef CAAM_JOBRING_SIZE
  58. #define CAAM_JOBRING_SIZE 1
  59. #endif
  60. /* black key stored in secure memory location */
  61. #define CAAM_BLACK_KEY_SM 1
  62. /* black key encrypted with AES-CCM (has MAC) */
  63. #define CAAM_BLACK_KEY_CCM 2
  64. /* black key encrypted with AES-ECB (no MAC) */
  65. #define CAAM_BLACK_KEY_ECB 3
  66. /******************************************************************************
  67. Basic Descriptors
  68. ****************************************************************************/
  69. /* descriptor commands */
  70. #define CAAM_KEY 0x00000000
  71. #define CAAM_LOAD 0x10000000
  72. #define CAAM_LOAD_CTX 0x10200000
  73. #define CAAM_IMM 0x00800000
  74. #define CAAM_FIFO_L 0x20000000
  75. #define CAAM_FIFO_S 0x60000000
  76. #define CAAM_FIFO_S_SKEY 0x60260000
  77. #define CAAM_STORE 0x50000000
  78. #define CAAM_STORE_CTX 0x50200000
  79. #define CAAM_MOVE 0x78000000
  80. #define CAAM_OP 0x80000000
  81. #define CAAM_SIG 0x90000000
  82. #define CAAM_JUMP 0xA0000000
  83. #define CAAM_SEQI 0xF0000000/* SEQ in */
  84. #define CAAM_SEQO 0xF8000000/* SEQ out */
  85. #define CAAM_HEAD 0xB0800000
  86. #define CAAM_NWB 0x00200000
  87. /* PROTOCOL OPERATION command */
  88. #define CAAM_PROT_UNIDI 0
  89. #define CAAM_BLOB_ENCAP 0x07000000
  90. #define CAAM_BLOB_DECAP 0x06000000
  91. #define CAAM_PKHA_OP 0x01000000
  92. #define CAAM_OPID_BLOB 0x000D0000
  93. /* algorithms modes and types */
  94. #define CAAM_CLASS1 0x02000000/* i.e. AES, PKHA */
  95. #define CAAM_CLASS2 0x04000000/* i.e. hash algos */
  96. #define CAAM_ENC 0x00000001
  97. #define CAAM_DEC 0x00000000
  98. #define CAAM_ALG_INIT 0x00000004
  99. #define CAAM_ALG_INITF 0x0000000C
  100. #define CAAM_ALG_UPDATE 0x00000000
  101. #define CAAM_ALG_FINAL 0x00000008
  102. #define CAAM_ALG_IVC 0x00000002
  103. /* AES 10h */
  104. #define CAAM_AESCTR 0x00100000
  105. #define CAAM_AESCBC 0x00100100
  106. #define CAAM_AESECB 0x00100200
  107. #define CAAM_AESCFB 0x00100300
  108. #define CAAM_AESOFB 0x00100400
  109. #define CAAM_CMAC 0x00100600
  110. #define CAAM_AESCCM 0x00100800
  111. #define CAAM_AESGCM 0x00100900
  112. /* HASH 40h */
  113. #define CAAM_MD5 0x00400000
  114. #define CAAM_SHA 0x00410000
  115. #define CAAM_SHA224 0x00420000
  116. #define CAAM_SHA256 0x00430000
  117. #define CAAM_SHA384 0x00440000
  118. #define CAAM_SHA512 0x00450000
  119. /* HMAC 40h + 10 AAI */
  120. #define CAAM_HMAC_MD5 0x00400010
  121. #define CAAM_HMAC_SHA 0x00410010
  122. #define CAAM_HMAC_SHA224 0x00420010
  123. #define CAAM_HMAC_SHA256 0x00430010
  124. #define CAAM_HMAC_SHA384 0x00440010
  125. #define CAAM_HMAC_SHA512 0x00450010
  126. /* ECDSA ECDSEL (pre defined flags for ECDSA parameters i.e. order) */
  127. #define CAAM_ECDSEL_SHIFT 7
  128. #define CAAM_ECDSA_PD 0x00400000
  129. #define CAAM_ECDSA_KEYGEN_PD 0x02000000
  130. #define CAAM_ECDSA_P192 (0x00 << CAAM_ECDSEL_SHIFT)
  131. #define CAAM_ECDSA_P224 (0x01 << CAAM_ECDSEL_SHIFT)
  132. #define CAAM_ECDSA_P256 (0x02 << CAAM_ECDSEL_SHIFT)
  133. #define CAAM_ECDSA_P384 (0x03 << CAAM_ECDSEL_SHIFT)
  134. #define CAAM_ECDSA_P521 (0x04 << CAAM_ECDSEL_SHIFT)
  135. #define CAAM_ECDSA_BRAINPOOL_P256 (0x0B << CAAM_ECDSEL_SHIFT)
  136. /* PKHA Operation ID ECDSA */
  137. #define CAAM_ECDSA_KEYPAIR 0x00140000
  138. #define CAAM_ECDSA_SIGN 0x00150000
  139. #define CAAM_ECDSA_VERIFY 0x00160000
  140. #define CAAM_ECDSA_ECDH 0x00170000
  141. #define CAAM_ECDSA_MESREP_HASHED (0x10 << 7)
  142. /* key encryption bit */
  143. #define CAAM_PKHA_ECC 0x00000002
  144. #define CAAM_PKHA_ENC_PRI_AESECB 0x00000004
  145. #define CAAM_PKHA_ENC_PRI_AESCCM (0x00000010 | CAAM_PKHA_ENC_PRI_AESECB)
  146. #define CAAM_PKHA_NO_TIMING_RESISTANCE 0x40000000
  147. #define CAAM_LOAD_BLACK_KEY 0x500000
  148. /* PKHA RSA */
  149. #define CAAM_OPID_RSA_ENCRYPT 0x00180000
  150. #define CAAM_OPID_RSA_DECRYPT 0x00190000
  151. #define CAAM_MD5_CTXSZ (16 + 8)
  152. #define CAAM_SHA_CTXSZ (20 + 8)
  153. #define CAAM_SHA224_CTXSZ (32 + 8)
  154. #define CAAM_SHA256_CTXSZ (32 + 8)
  155. #define CAAM_SHA384_CTXSZ (64 + 8)
  156. #define CAAM_SHA512_CTXSZ (64 + 8)
  157. /* RNG 50h */
  158. #define CAAM_RNG 0x00500000
  159. /* Used to get raw entropy from TRNG */
  160. #define CAAM_ENTROPY 0x00500001
  161. #define FIFOL_TYPE_MSG 0x00100000
  162. #define FIFOL_TYPE_IV 0x00200000
  163. #define FIFOL_TYPE_IVC 0x00380000
  164. #define FIFOL_TYPE_AAD 0x00300000
  165. #define FIFOL_TYPE_FC1 0x00010000
  166. #define FIFOL_TYPE_LC1 0x00020000
  167. #define FIFOL_TYPE_LC2 0x00040000
  168. #define FIFOS_TYPE_MSG 0x00300000
  169. #define FIFOS_TYPE_RNG 0x00340000
  170. #define FIFOS_EXT 0x00400000
  171. /* continue bit set if more output is expected */
  172. #define CAAM_FIFOS_CONT 0x00800000
  173. #define CAAM_PAGE_SZ 4096
  174. /* RNG Registers */
  175. #define CAAM_RTMCTL 0X0600
  176. #define CAAM_RTSDCTL 0X0610
  177. #define CAAM_RTFRQMIN 0X0618
  178. #define CAAM_RTFRQMAX 0X061C
  179. #define CAAM_RDSTA 0X06C0
  180. #define CAAM_RTSTATUS 0x063C
  181. #define CAAM_RDINT0 0x06D0
  182. /* each of the following 11 RTENT registers are an offset of 4 from RTENT0 */
  183. #define CAAM_RTENT0 0x0640
  184. #define CAAM_RTENT11 0x066C /* Max RTENT register */
  185. #define CAAM_RTENT_MAX 0x067C
  186. /* RNG Masks/Values */
  187. #ifndef CAAM_ENT_DLY
  188. /* Less than half the default value to try and increase entropy collection.
  189. * Value is system clock cycles. */
  190. #define CAAM_ENT_DLY 1200
  191. #endif
  192. #ifndef CAAM_ENT_DLY_INCREMENT
  193. #define CAAM_ENT_DLY_INCREMENT 500
  194. #endif
  195. #ifndef CAAM_ENT_SAMPLE
  196. /* default sample value from reference manual */
  197. #define CAAM_ENT_SAMPLE 0x09C4
  198. #endif
  199. #ifndef CAAM_ENT_DLY_MAX
  200. #define CAAM_ENT_DLY_MAX 12000
  201. #endif
  202. #ifndef CAAM_ENT_MINSHIFT
  203. /* default to the minimum entropy delay of 1/4 */
  204. #define CAAM_ENT_MINSHIFT 2
  205. #endif
  206. #ifndef CAAM_ENT_MAXSHIFT
  207. /* default to the maximum entropy delay of 16 times */
  208. #define CAAM_ENT_MAXSHIFT 4
  209. #endif
  210. #define CAAM_PRGM 0x00010000 /* Set RTMCTL to program state */
  211. #define CAAM_TRNG 0x00000020 /* Set TRNG access */
  212. #define CAAM_RTMCTL_RESET 0x40 /* TRNG reset to defaults */
  213. #define CAAM_CTLERR 0x00001000
  214. #define CAAM_ENTVAL 0x00000400 /* checking RTMCTL for entropy ready */
  215. /* Input Job Ring Registers */
  216. #define CAAM_IRBAR0 0x0004
  217. #define CAAM_IRSR0 0x000C
  218. #define CAAM_IRJAR0 0x001C
  219. #define CAAM_IRBAR2 0x3004
  220. #define CAAM_IRSR2 0x300C
  221. #define CAAM_IRJAR2 0x301C
  222. #define CAAM_IRSAR_JR 0x0014
  223. /* Output Job Ring Registers */
  224. #define CAAM_ORBAR 0x0024
  225. #define CAAM_ORSR0 0x002C
  226. #define CAAM_ORJAR 0x003C
  227. #define CAAM_ORJRR 0x0034
  228. #define JRCFGR_JR 0x0054
  229. #define JRCR_JR 0x006C
  230. /* Status Registers */
  231. #define CAAM_STATUS 0x0FD4
  232. #define CAAM_CHA_VERSION_MS 0x0FE8
  233. #define CAAM_CHA_VERSION_LS 0x0FEC
  234. #define CAMM_CHA_SUPPORT_MS 0x0FF0
  235. #define CAMM_CHA_SUPPORT_LS 0x0FF4
  236. #define CAAM_VERSION_MS 0x0FF8
  237. #define CAAM_VERSION_LS 0x0FFC
  238. #define CAAM_CHA_CCBVID 0x0FE4
  239. /* high performance AES module includes XTS, GCM */
  240. #define CAAM_AES_HIGH_PERFORMANCE 0x4
  241. /* low power AES module includes ECB, CBC, CTR, CCM, CMAC, CBC */
  242. #define CAAM_AES_LOW_POWER 0x3
  243. #define CAAM_SM_CMD 0x0BE4
  244. #define CAAM_SM_SMPO 0x0FBC
  245. #define CAAM_SMAPR 0x0A04
  246. #define CAAM_SM_SMVID_MS 0x0FD8
  247. #define CAAM_SM_SMVID_LS 0x0FDC
  248. #define CAAM_SM_STATUS 0x0BEC
  249. #define CAAM_SM_CSP 0x00008000
  250. #define CAAM_SM_SMAP_LOCK 0x00002000
  251. #define CAAM_SM_SMAG_LOCK 0x00001000
  252. #define CAAM_SM_ALL_RW 0x000000FF
  253. #define CAAM_C1DSR_LS 0x8014
  254. #define CAAM_C1MR 0x8004
  255. #define CAAM_RSTA 0x0004
  256. #define CAAM_DRR 0x0124
  257. #define CAAM_CRNR_LS 0x0FA4
  258. /* output FIFO is 16 entries deep and each entry has a two 4 byte registers */
  259. #define CAAM_FIFOO_MS 0x87F0
  260. #define CAAM_FIFOO_LS 0x87F4
  261. /* input FIFO is 16 entries deep with each entry having two 4 byte registers
  262. All data written to it from IP bus should be in big endian format */
  263. #define CAAM_FIFOI_LS 0x87E0
  264. /* offset of 4 with range 0 .. 13 */
  265. #define CAAM_CTX1 0x8100
  266. #define CAAM_CTRIV CAAM_CTX1 + 8 /* AES-CTR iv is in 2 and 3 */
  267. #define CAAM_CBCIV CAAM_CTX1 /* AES-CBC iv is in 1 and 2 */
  268. /* debugging registers */
  269. #define CAAM_DECORR 0x009C /* used to set DECO into debug mode */
  270. #define CAAM_DODJR 0x8E00 /* for hung operations */
  271. #define CAAM_DOJQCR_MS 0x8800
  272. #define CAAM_DOOPSTA_MS 0x8810 /* DECO operation status register */
  273. #define CAAM_DODAR 0x8808 /* address of current descriptor */
  274. #define CAAM_DODESB 0x8A00 /* 64 registers that hold the current descriptor buffer */
  275. #define JRINTR_JR 0x004C
  276. #define CAAM_SINGLE_STEP_MODE 0x40000000
  277. #define CAAM_STEP 0x80000000
  278. /* Port layer for CAAM driver, functions defined in caam_<env>.c */
  279. unsigned int CAAM_READ(CAAM_ADDRESS reg);
  280. void CAAM_WRITE(CAAM_ADDRESS reg, unsigned int in);
  281. int CAAM_SET_BASEADDR(CAAM_ADDRESS* baseAddr);
  282. void CAAM_UNSET_BASEADDR(CAAM_ADDRESS baseAddr);
  283. void CAAM_UNSET_JOBRING_ADDR(CAAM_ADDRESS base, CAAM_ADDRESS ringInPhy,
  284. void* ringInVir);
  285. int CAAM_SET_JOBRING_ADDR(CAAM_ADDRESS* base, CAAM_ADDRESS* ringInPhy,
  286. void** ringInVir);
  287. CAAM_ADDRESS CAAM_ADR_TO_PHYSICAL(void* in, int inSz);
  288. void* CAAM_ADR_MAP(CAAM_ADDRESS in, int inSz, unsigned char copy);
  289. void CAAM_ADR_UNMAP(void* vaddr, CAAM_ADDRESS out, int outSz,
  290. unsigned char copy);
  291. int CAAM_ADR_SYNC(void* vaddr, int sz);
  292. CAAM_ADDRESS CAAM_ADR_TO_VIRTUAL(CAAM_ADDRESS in, int length);
  293. #ifndef WOLFSSL_CAAM_BUFFER
  294. #define WOLFSSL_CAAM_BUFFER
  295. typedef struct CAAM_BUFFER {
  296. int BufferType;
  297. CAAM_ADDRESS TheAddress;
  298. int Length;
  299. } CAAM_BUFFER;
  300. #endif
  301. unsigned int caamReadRegister(unsigned int reg);
  302. void caamWriteRegister(unsigned int reg, unsigned int in);
  303. int SynchronousSendRequest(int type, unsigned int args[4], CAAM_BUFFER *buf, int sz);
  304. int CleanupCAAM(void);
  305. /* Driver API that can be called by caam_<env>.c port layers */
  306. typedef struct DESCSTRUCT DESCSTRUCT;
  307. int caamKeyCover(DESCSTRUCT *desc, int sz, unsigned int args[4]);
  308. int caamEntropy(unsigned char *out, int outSz);
  309. int caamECDSA_ECDH(DESCSTRUCT *desc, int sz, unsigned int args[4]);
  310. int caamECDSASign(DESCSTRUCT *desc, int sz, unsigned int args[4]);
  311. int caamECDSAVerify(DESCSTRUCT *desc, CAAM_BUFFER *buf, int sz,
  312. unsigned int args[4]);
  313. int caamECDSAMake(DESCSTRUCT *desc, CAAM_BUFFER *buf, unsigned int args[4]);
  314. int caamAes(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4]);
  315. int caamAead(DESCSTRUCT* desc, CAAM_BUFFER* buf, unsigned int args[4]);
  316. int caamAesCmac(DESCSTRUCT *desc, int sz, unsigned int args[4]);
  317. int caamBlob(DESCSTRUCT *desc);
  318. CAAM_ADDRESS caamGetPartition(unsigned int part, int partSz, unsigned int flag);
  319. int caamFreePart(unsigned int part);
  320. int caamFindUnusedPartition(void);
  321. void caamDescInit(DESCSTRUCT* desc, int type, unsigned int args[4],
  322. CAAM_BUFFER* buf, int sz);
  323. /* CAAM descriptor */
  324. #define DESC_COUNT 1
  325. #define MAX_BUF 20
  326. #define BUFFER_COUNT (MAX_BUF * DESC_COUNT)
  327. /* CAAM descriptors can only be 64 unsigned ints */
  328. #define MAX_DESC_SZ 64
  329. /* 64 byte buffer for when data crosses a page boundary */
  330. #define ALIGN_BUF 16
  331. /* MAX_CTX is 64 bytes (sha512 digest) + 8 bytes (CAAM length value) */
  332. #define MAX_CTX 18
  333. #define MIN_READ_REG CAAM_BASE
  334. #define MAX_READ_REG (CAAM_BASE + 0x00010000)
  335. struct buffer {
  336. CAAM_ADDRESS data;
  337. CAAM_ADDRESS dataSz;
  338. };
  339. struct DESCSTRUCT {
  340. #if defined(__INTEGRITY) || defined(INTEGRITY)
  341. struct IORequestStruct TheIORequest;
  342. #endif
  343. struct CAAM_DEVICE* caam;
  344. struct buffer buf[MAX_BUF]; /* buffers holding data input address */
  345. unsigned int desc[MAX_DESC_SZ]; /* max size of 64 word32 */
  346. unsigned int aadSzBuf[4]; /* Formatted AAD size for CCM */
  347. unsigned int alignBuf[ALIGN_BUF]; /* 64 byte buffer for non page
  348. align */
  349. unsigned int iv[MAX_CTX]; /* AES IV and also hash state */
  350. unsigned int ctxBuf[MAX_CTX]; /* key */
  351. CAAM_ADDRESS output; /* address to output buffer */
  352. CAAM_ADDRESS ctxOut; /* address to update buffer holding state */
  353. Value alignIdx;/* index for align buffer */
  354. Value idx; /* index for descriptor buffer */
  355. Value headIdx; /* for first portion of descriptor buffer */
  356. Value lastIdx; /* for last portion of descriptor buffer */
  357. Value outputIdx; /* idx to output buffer in "buf" */
  358. Value inputSz; /* size of input buffer */
  359. Value ctxSz; /* size of CTX/Key buffer */
  360. Value aadSz; /* AAD size for CCM */
  361. Value startIdx; /* for telling header where to start */
  362. Value lastFifo;
  363. Value type;
  364. Value state;
  365. Value DescriptorCount;
  366. Boolean running; /* True if building/running descriptor is
  367. in process */
  368. };
  369. /* wolfSSL specific flags */
  370. #define CAAM_FIND_PART 0xFFFFFFFF
  371. #define CAAM_GET_PART 0xFFFFFFFE
  372. #define CAAM_FREE_PART 0xFFFFFFFD
  373. #define CAAM_READ_PART 0xFFFFFFFC
  374. #define CAAM_WRITE_PART 0xFFFFFFFB
  375. #define MAX_ECDSA_VERIFY_ADDR 8
  376. #define MAX_ECDSA_SIGN_ADDR 8
  377. #define BLACK_KEY_MAC_SZ 16
  378. #define BLACK_BLOB_KEYMOD_SZ 16
  379. #define RED_BLOB_KEYMOD_SZ 16
  380. #define SM_BLOB_KEYMOD_SZ 8
  381. #endif /* CAAM_DRIVER_H */