hal_crypto_lld.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_crypto_lld.h
  15. * @brief PLATFORM cryptographic subsystem low level driver header.
  16. *
  17. * @addtogroup CRYPTO
  18. * @{
  19. */
  20. #ifndef HAL_CRYPTO_LLD_H
  21. #define HAL_CRYPTO_LLD_H
  22. #if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /**
  27. * @name Driver capability switches
  28. * @{
  29. */
  30. #define CRY_LLD_SUPPORTS_AES TRUE
  31. #define CRY_LLD_SUPPORTS_AES_ECB TRUE
  32. #define CRY_LLD_SUPPORTS_AES_CBC TRUE
  33. #define CRY_LLD_SUPPORTS_AES_CFB TRUE
  34. #define CRY_LLD_SUPPORTS_AES_CTR TRUE
  35. #define CRY_LLD_SUPPORTS_AES_GCM TRUE
  36. #define CRY_LLD_SUPPORTS_DES TRUE
  37. #define CRY_LLD_SUPPORTS_DES_ECB TRUE
  38. #define CRY_LLD_SUPPORTS_DES_CBC TRUE
  39. #define CRY_LLD_SUPPORTS_SHA1 TRUE
  40. #define CRY_LLD_SUPPORTS_SHA256 TRUE
  41. #define CRY_LLD_SUPPORTS_SHA512 TRUE
  42. #define CRY_LLD_SUPPORTS_HMAC_SHA256 TRUE
  43. #define CRY_LLD_SUPPORTS_HMAC_SHA512 TRUE
  44. /** @} */
  45. /*===========================================================================*/
  46. /* Driver pre-compile time settings. */
  47. /*===========================================================================*/
  48. /**
  49. * @name PLATFORM configuration options
  50. * @{
  51. */
  52. /**
  53. * @brief CRY1 driver enable switch.
  54. * @details If set to @p TRUE the support for CRY1 is included.
  55. * @note The default is @p FALSE.
  56. */
  57. #if !defined(PLATFORM_CRY_USE_CRY1) || defined(__DOXYGEN__)
  58. #define PLATFORM_CRY_USE_CRY1 FALSE
  59. #endif
  60. /** @} */
  61. /*===========================================================================*/
  62. /* Derived constants and error checks. */
  63. /*===========================================================================*/
  64. /*===========================================================================*/
  65. /* Driver data structures and types. */
  66. /*===========================================================================*/
  67. /**
  68. * @brief CRY key identifier type.
  69. */
  70. typedef uint32_t crykey_t;
  71. /**
  72. * @brief Type of a structure representing an CRY driver.
  73. */
  74. typedef struct CRYDriver CRYDriver;
  75. /**
  76. * @brief Driver configuration structure.
  77. * @note It could be empty on some architectures.
  78. */
  79. typedef struct {
  80. uint32_t dummy;
  81. } CRYConfig;
  82. /**
  83. * @brief Structure representing an CRY driver.
  84. */
  85. struct CRYDriver {
  86. /**
  87. * @brief Driver state.
  88. */
  89. crystate_t state;
  90. /**
  91. * @brief Current configuration data.
  92. */
  93. const CRYConfig *config;
  94. #if defined(CRY_DRIVER_EXT_FIELDS)
  95. CRY_DRIVER_EXT_FIELDS
  96. #endif
  97. /* End of the mandatory fields.*/
  98. };
  99. #if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
  100. /**
  101. * @brief Type of a SHA1 context.
  102. */
  103. typedef struct {
  104. uint32_t dummy;
  105. } SHA1Context;
  106. #endif
  107. #if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
  108. /**
  109. * @brief Type of a SHA256 context.
  110. */
  111. typedef struct {
  112. uint32_t dummy;
  113. } SHA256Context;
  114. #endif
  115. #if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
  116. /**
  117. * @brief Type of a SHA512 context.
  118. */
  119. typedef struct {
  120. uint32_t dummy;
  121. } SHA512Context;
  122. #endif
  123. #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || defined(__DOXYGEN__)
  124. /**
  125. * @brief Type of a HMAC_SHA256 context.
  126. */
  127. typedef struct {
  128. uint32_t dummy;
  129. } HMACSHA256Context;
  130. #endif
  131. #if (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || defined(__DOXYGEN__)
  132. /**
  133. * @brief Type of a HMAC_SHA512 context.
  134. */
  135. typedef struct {
  136. uint32_t dummy;
  137. } HMACSHA512Context;
  138. #endif
  139. /*===========================================================================*/
  140. /* Driver macros. */
  141. /*===========================================================================*/
  142. /*===========================================================================*/
  143. /* External declarations. */
  144. /*===========================================================================*/
  145. #if (PLATFORM_CRY_USE_CRY1 == TRUE) && !defined(__DOXYGEN__)
  146. extern CRYDriver CRYD1;
  147. #endif
  148. #ifdef __cplusplus
  149. extern "C" {
  150. #endif
  151. void cry_lld_init(void);
  152. void cry_lld_start(CRYDriver *cryp);
  153. void cry_lld_stop(CRYDriver *cryp);
  154. #if (CRY_LLD_SUPPORTS_AES == TRUE) || \
  155. (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || \
  156. (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || \
  157. (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || \
  158. (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || \
  159. (CRY_LLD_SUPPORTS_AES_GCM == TRUE) || \
  160. defined(__DOXYGEN__)
  161. cryerror_t cry_lld_aes_loadkey(CRYDriver *cryp,
  162. size_t size,
  163. const uint8_t *keyp);
  164. #endif
  165. #if (CRY_LLD_SUPPORTS_AES == TRUE) || defined(__DOXYGEN__)
  166. cryerror_t cry_lld_encrypt_AES(CRYDriver *cryp,
  167. crykey_t key_id,
  168. const uint8_t *in,
  169. uint8_t *out);
  170. cryerror_t cry_lld_decrypt_AES(CRYDriver *cryp,
  171. crykey_t key_id,
  172. const uint8_t *in,
  173. uint8_t *out);
  174. #endif
  175. #if (CRY_LLD_SUPPORTS_AES_ECB == TRUE) || defined(__DOXYGEN__)
  176. cryerror_t cry_lld_encrypt_AES_ECB(CRYDriver *cryp,
  177. crykey_t key_id,
  178. size_t size,
  179. const uint8_t *in,
  180. uint8_t *out);
  181. cryerror_t cry_lld_decrypt_AES_ECB(CRYDriver *cryp,
  182. crykey_t key_id,
  183. size_t size,
  184. const uint8_t *in,
  185. uint8_t *out);
  186. #endif
  187. #if (CRY_LLD_SUPPORTS_AES_CBC == TRUE) || defined(__DOXYGEN__)
  188. cryerror_t cry_lld_encrypt_AES_CBC(CRYDriver *cryp,
  189. crykey_t key_id,
  190. size_t size,
  191. const uint8_t *in,
  192. uint8_t *out,
  193. const uint8_t *iv);
  194. cryerror_t cry_lld_decrypt_AES_CBC(CRYDriver *cryp,
  195. crykey_t key_id,
  196. size_t size,
  197. const uint8_t *in,
  198. uint8_t *out,
  199. const uint8_t *iv);
  200. #endif
  201. #if (CRY_LLD_SUPPORTS_AES_CFB == TRUE) || defined(__DOXYGEN__)
  202. cryerror_t cry_lld_encrypt_AES_CFB(CRYDriver *cryp,
  203. crykey_t key_id,
  204. size_t size,
  205. const uint8_t *in,
  206. uint8_t *out,
  207. const uint8_t *iv);
  208. cryerror_t cry_lld_decrypt_AES_CFB(CRYDriver *cryp,
  209. crykey_t key_id,
  210. size_t size,
  211. const uint8_t *in,
  212. uint8_t *out,
  213. const uint8_t *iv);
  214. #endif
  215. #if (CRY_LLD_SUPPORTS_AES_CTR == TRUE) || defined(__DOXYGEN__)
  216. cryerror_t cry_lld_encrypt_AES_CTR(CRYDriver *cryp,
  217. crykey_t key_id,
  218. size_t size,
  219. const uint8_t *in,
  220. uint8_t *out,
  221. const uint8_t *iv);
  222. cryerror_t cry_lld_decrypt_AES_CTR(CRYDriver *cryp,
  223. crykey_t key_id,
  224. size_t size,
  225. const uint8_t *in,
  226. uint8_t *out,
  227. const uint8_t *iv);
  228. #endif
  229. #if (CRY_LLD_SUPPORTS_AES_GCM == TRUE) || defined(__DOXYGEN__)
  230. cryerror_t cry_lld_encrypt_AES_GCM(CRYDriver *cryp,
  231. crykey_t key_id,
  232. size_t size,
  233. const uint8_t *in,
  234. uint8_t *out,
  235. const uint8_t *iv,
  236. size_t aadsize,
  237. const uint8_t *aad,
  238. uint8_t *authtag);
  239. cryerror_t cry_lld_decrypt_AES_GCM(CRYDriver *cryp,
  240. crykey_t key_id,
  241. size_t size,
  242. const uint8_t *in,
  243. uint8_t *out,
  244. const uint8_t *iv,
  245. size_t aadsize,
  246. const uint8_t *aad,
  247. uint8_t *authtag);
  248. #endif
  249. #if (CRY_LLD_SUPPORTS_DES == TRUE) || \
  250. (CRY_LLD_SUPPORTS_DES_ECB == TRUE) || \
  251. (CRY_LLD_SUPPORTS_DES_CBC == TRUE) || \
  252. defined(__DOXYGEN__)
  253. cryerror_t cry_lld_des_loadkey(CRYDriver *cryp,
  254. size_t size,
  255. const uint8_t *keyp);
  256. #endif
  257. #if (CRY_LLD_SUPPORTS_DES == TRUE) || defined(__DOXYGEN__)
  258. cryerror_t cry_lld_encrypt_DES(CRYDriver *cryp,
  259. crykey_t key_id,
  260. const uint8_t *in,
  261. uint8_t *out);
  262. cryerror_t cry_lld_decrypt_DES(CRYDriver *cryp,
  263. crykey_t key_id,
  264. const uint8_t *in,
  265. uint8_t *out);
  266. #endif
  267. #if (CRY_LLD_SUPPORTS_DES_ECB == TRUE) || defined(__DOXYGEN__)
  268. cryerror_t cry_lld_encrypt_DES_ECB(CRYDriver *cryp,
  269. crykey_t key_id,
  270. size_t size,
  271. const uint8_t *in,
  272. uint8_t *out);
  273. cryerror_t cry_lld_decrypt_DES_ECB(CRYDriver *cryp,
  274. crykey_t key_id,
  275. size_t size,
  276. const uint8_t *in,
  277. uint8_t *out);
  278. #endif
  279. #if (CRY_LLD_SUPPORTS_DES_CBC == TRUE) || defined(__DOXYGEN__)
  280. cryerror_t cry_lld_encrypt_DES_CBC(CRYDriver *cryp,
  281. crykey_t key_id,
  282. size_t size,
  283. const uint8_t *in,
  284. uint8_t *out,
  285. const uint8_t *iv);
  286. cryerror_t cry_lld_decrypt_DES_CBC(CRYDriver *cryp,
  287. crykey_t key_id,
  288. size_t size,
  289. const uint8_t *in,
  290. uint8_t *out,
  291. const uint8_t *iv);
  292. #endif
  293. #if (CRY_LLD_SUPPORTS_SHA1 == TRUE) || defined(__DOXYGEN__)
  294. cryerror_t cry_lld_SHA1_init(CRYDriver *cryp, SHA1Context *sha1ctxp);
  295. cryerror_t cry_lld_SHA1_update(CRYDriver *cryp, SHA1Context *sha1ctxp,
  296. size_t size, const uint8_t *in);
  297. cryerror_t cry_lld_SHA1_final(CRYDriver *cryp, SHA1Context *sha1ctxp,
  298. uint8_t *out);
  299. #endif
  300. #if (CRY_LLD_SUPPORTS_SHA256 == TRUE) || defined(__DOXYGEN__)
  301. cryerror_t cry_lld_SHA256_init(CRYDriver *cryp, SHA256Context *sha256ctxp);
  302. cryerror_t cry_lld_SHA256_update(CRYDriver *cryp, SHA256Context *sha256ctxp,
  303. size_t size, const uint8_t *in);
  304. cryerror_t cry_lld_SHA256_final(CRYDriver *cryp, SHA256Context *sha256ctxp,
  305. uint8_t *out);
  306. #endif
  307. #if (CRY_LLD_SUPPORTS_SHA512 == TRUE) || defined(__DOXYGEN__)
  308. cryerror_t cry_lld_SHA512_init(CRYDriver *cryp, SHA512Context *sha512ctxp);
  309. cryerror_t cry_lld_SHA512_update(CRYDriver *cryp, SHA512Context *sha512ctxp,
  310. size_t size, const uint8_t *in);
  311. cryerror_t cry_lld_SHA512_final(CRYDriver *cryp, SHA512Context *sha512ctxp,
  312. uint8_t *out);
  313. #endif
  314. #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || \
  315. (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || \
  316. defined(__DOXYGEN__)
  317. cryerror_t cry_lld_hmac_loadkey(CRYDriver *cryp,
  318. size_t size,
  319. const uint8_t *keyp);
  320. #endif
  321. #if (CRY_LLD_SUPPORTS_HMAC_SHA256 == TRUE) || defined(__DOXYGEN__)
  322. cryerror_t cry_lld_HMACSHA256_init(CRYDriver *cryp,
  323. HMACSHA256Context *hmacsha256ctxp);
  324. cryerror_t cry_lld_HMACSHA256_update(CRYDriver *cryp,
  325. HMACSHA256Context *hmacsha256ctxp,
  326. size_t size, const uint8_t *in);
  327. cryerror_t cry_lld_HMACSHA256_final(CRYDriver *cryp,
  328. HMACSHA256Context *hmacsha256ctxp,
  329. uint8_t *out);
  330. #endif
  331. #if (CRY_LLD_SUPPORTS_HMAC_SHA512 == TRUE) || defined(__DOXYGEN__)
  332. cryerror_t cry_lld_HMACSHA512_init(CRYDriver *cryp,
  333. HMACSHA512Context *hmacsha512ctxp);
  334. cryerror_t cry_lld_HMACSHA512_update(CRYDriver *cryp,
  335. HMACSHA512Context *hmacsha512ctxp,
  336. size_t size, const uint8_t *in);
  337. cryerror_t cry_lld_HMACSHA512_final(CRYDriver *cryp,
  338. HMACSHA512Context *hmacsha512ctxp,
  339. uint8_t *out);
  340. #endif
  341. #ifdef __cplusplus
  342. }
  343. #endif
  344. #endif /* HAL_USE_CRY == TRUE */
  345. #endif /* HAL_CRYPTO_LLD_H */
  346. /** @} */