hal_crypto.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.h
  15. * @brief Cryptographic Driver macros and structures.
  16. *
  17. * @addtogroup CRYPTO
  18. * @{
  19. */
  20. #ifndef HAL_CRYPTO_H
  21. #define HAL_CRYPTO_H
  22. #if (HAL_USE_CRY == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver pre-compile time settings. */
  28. /*===========================================================================*/
  29. /**
  30. * @brief Enables the SW fall-back of the cryptographic driver.
  31. * @details When enabled, this option, activates a fall-back software
  32. * implementation for algorithms not supported by the underlying
  33. * hardware.
  34. * @note Fall-back implementations may not be present for all algorithms.
  35. */
  36. #if !defined(HAL_CRY_USE_FALLBACK) || defined(__DOXYGEN__)
  37. #define HAL_CRY_USE_FALLBACK FALSE
  38. #endif
  39. /**
  40. * @brief Makes the driver forcibly use the fall-back implementations.
  41. * @note If enabled then the LLD driver is not included at all.
  42. */
  43. #if !defined(HAL_CRY_ENFORCE_FALLBACK) || defined(__DOXYGEN__)
  44. #define HAL_CRY_ENFORCE_FALLBACK FALSE
  45. #endif
  46. /*===========================================================================*/
  47. /* Derived constants and error checks. */
  48. /*===========================================================================*/
  49. #if HAL_CRY_ENFORCE_FALLBACK == TRUE
  50. #undef HAL_CRY_USE_FALLBACK
  51. #define HAL_CRY_USE_FALLBACK TRUE
  52. #endif
  53. /*===========================================================================*/
  54. /* Driver data structures and types. */
  55. /*===========================================================================*/
  56. /**
  57. * @brief Size, in bits, of a crypto field or message.
  58. * @note It is assumed, for simplicity, that this type is equivalent to
  59. * a @p size_t.
  60. */
  61. typedef size_t bitsize_t;
  62. /**
  63. * @brief Driver state machine possible states.
  64. */
  65. typedef enum {
  66. CRY_UNINIT = 0, /**< Not initialized. */
  67. CRY_STOP = 1, /**< Stopped. */
  68. CRY_READY = 2 /**< Ready. */
  69. } crystate_t;
  70. /**
  71. * @brief Driver error codes.
  72. */
  73. typedef enum {
  74. CRY_NOERROR = 0, /**< No error. */
  75. CRY_ERR_INV_ALGO = 1, /**< Invalid cypher/mode. */
  76. CRY_ERR_INV_KEY_SIZE = 2, /**< Invalid key size. */
  77. CRY_ERR_INV_KEY_TYPE = 3, /**< Invalid key type. */
  78. CRY_ERR_INV_KEY_ID = 4, /**< Invalid key identifier. */
  79. CRY_ERR_OP_FAILURE = 5 /**< Requested operation failed.*/
  80. } cryerror_t;
  81. /**
  82. * @brief Type of an algorithm identifier.
  83. * @note It is only used to determine the key required for operations.
  84. */
  85. typedef enum {
  86. cry_algo_none = 0,
  87. cry_algo_aes, /**< AES 128, 192, 256 bits. */
  88. cry_algo_des, /**< DES 56, TDES 112, 168 bits.*/
  89. cry_algo_hmac /**< HMAC variable size. */
  90. } cryalgorithm_t;
  91. #if HAL_CRY_ENFORCE_FALLBACK == FALSE
  92. /* Use the defined low level driver.*/
  93. #include "hal_crypto_lld.h"
  94. #if !defined(CRY_LLD_SUPPORTS_AES) || \
  95. !defined(CRY_LLD_SUPPORTS_AES_ECB) || \
  96. !defined(CRY_LLD_SUPPORTS_AES_CBC) || \
  97. !defined(CRY_LLD_SUPPORTS_AES_CFB) || \
  98. !defined(CRY_LLD_SUPPORTS_AES_CTR) || \
  99. !defined(CRY_LLD_SUPPORTS_AES_GCM) || \
  100. !defined(CRY_LLD_SUPPORTS_DES) || \
  101. !defined(CRY_LLD_SUPPORTS_DES_ECB) || \
  102. !defined(CRY_LLD_SUPPORTS_DES_CBC) || \
  103. !defined(CRY_LLD_SUPPORTS_SHA1) || \
  104. !defined(CRY_LLD_SUPPORTS_SHA256) || \
  105. !defined(CRY_LLD_SUPPORTS_SHA512) || \
  106. !defined(CRY_LLD_SUPPORTS_HMAC_SHA256) || \
  107. !defined(CRY_LLD_SUPPORTS_HMAC_SHA512)
  108. #error "CRYPTO LLD does not export the required switches"
  109. #endif
  110. #else /* HAL_CRY_ENFORCE_FALLBACK == TRUE */
  111. /* No LLD at all, using the standalone mode.*/
  112. #define CRY_LLD_SUPPORTS_AES FALSE
  113. #define CRY_LLD_SUPPORTS_AES_ECB FALSE
  114. #define CRY_LLD_SUPPORTS_AES_CBC FALSE
  115. #define CRY_LLD_SUPPORTS_AES_CFB FALSE
  116. #define CRY_LLD_SUPPORTS_AES_CTR FALSE
  117. #define CRY_LLD_SUPPORTS_AES_GCM FALSE
  118. #define CRY_LLD_SUPPORTS_DES FALSE
  119. #define CRY_LLD_SUPPORTS_DES_ECB FALSE
  120. #define CRY_LLD_SUPPORTS_DES_CBC FALSE
  121. #define CRY_LLD_SUPPORTS_SHA1 FALSE
  122. #define CRY_LLD_SUPPORTS_SHA256 FALSE
  123. #define CRY_LLD_SUPPORTS_SHA512 FALSE
  124. #define CRY_LLD_SUPPORTS_HMAC_SHA256 FALSE
  125. #define CRY_LLD_SUPPORTS_HMAC_SHA512 FALSE
  126. typedef uint_fast8_t crykey_t;
  127. typedef struct CRYDriver CRYDriver;
  128. typedef struct {
  129. uint32_t dummy;
  130. } CRYConfig;
  131. struct CRYDriver {
  132. crystate_t state;
  133. const CRYConfig *config;
  134. };
  135. #endif /* HAL_CRY_ENFORCE_FALLBACK == TRUE */
  136. /* The fallback header is included only if required by settings.*/
  137. #if HAL_CRY_USE_FALLBACK == TRUE
  138. #include "hal_crypto_fallback.h"
  139. #endif
  140. #if (HAL_CRY_USE_FALLBACK == FALSE) && (CRY_LLD_SUPPORTS_SHA1 == FALSE)
  141. /* Stub @p SHA1Context structure type declaration. It is not provided by
  142. the LLD and the fallback is not enabled.*/
  143. typedef struct {
  144. uint32_t dummy;
  145. } SHA1Context;
  146. #endif
  147. #if (HAL_CRY_USE_FALLBACK == FALSE) && (CRY_LLD_SUPPORTS_SHA256 == FALSE)
  148. /* Stub @p SHA256Context structure type declaration. It is not provided by
  149. the LLD and the fallback is not enabled.*/
  150. typedef struct {
  151. uint32_t dummy;
  152. } SHA256Context;
  153. #endif
  154. #if (HAL_CRY_USE_FALLBACK == FALSE) && (CRY_LLD_SUPPORTS_SHA512 == FALSE)
  155. /* Stub @p SHA512Context structure type declaration. It is not provided by
  156. the LLD and the fallback is not enabled.*/
  157. typedef struct {
  158. uint32_t dummy;
  159. } SHA512Context;
  160. #endif
  161. #if (HAL_CRY_USE_FALLBACK == FALSE) && (CRY_LLD_SUPPORTS_HMAC_SHA256 == FALSE)
  162. /* Stub @p HMACSHA256Context structure type declaration. It is not provided by
  163. the LLD and the fallback is not enabled.*/
  164. typedef struct {
  165. uint32_t dummy;
  166. } HMACSHA256Context;
  167. #endif
  168. #if (HAL_CRY_USE_FALLBACK == FALSE) && (CRY_LLD_SUPPORTS_HMAC_SHA512 == FALSE)
  169. /* Stub @p HMACSHA512Context structure type declaration. It is not provided by
  170. the LLD and the fallback is not enabled.*/
  171. typedef struct {
  172. uint32_t dummy;
  173. } HMACSHA512Context;
  174. #endif
  175. /*===========================================================================*/
  176. /* Driver macros. */
  177. /*===========================================================================*/
  178. /**
  179. * @name Low level driver helper macros
  180. * @{
  181. */
  182. /** @} */
  183. /*===========================================================================*/
  184. /* External declarations. */
  185. /*===========================================================================*/
  186. #ifdef __cplusplus
  187. extern "C" {
  188. #endif
  189. void cryInit(void);
  190. void cryObjectInit(CRYDriver *cryp);
  191. void cryStart(CRYDriver *cryp, const CRYConfig *config);
  192. void cryStop(CRYDriver *cryp);
  193. cryerror_t cryLoadAESTransientKey(CRYDriver *cryp,
  194. size_t size,
  195. const uint8_t *keyp);
  196. cryerror_t cryEncryptAES(CRYDriver *cryp,
  197. crykey_t key_id,
  198. const uint8_t *in,
  199. uint8_t *out);
  200. cryerror_t cryDecryptAES(CRYDriver *cryp,
  201. crykey_t key_id,
  202. const uint8_t *in,
  203. uint8_t *out);
  204. cryerror_t cryEncryptAES_ECB(CRYDriver *cryp,
  205. crykey_t key_id,
  206. size_t size,
  207. const uint8_t *in,
  208. uint8_t *out);
  209. cryerror_t cryDecryptAES_ECB(CRYDriver *cryp,
  210. crykey_t key_id,
  211. size_t size,
  212. const uint8_t *in,
  213. uint8_t *out);
  214. cryerror_t cryEncryptAES_CBC(CRYDriver *cryp,
  215. crykey_t key_id,
  216. size_t size,
  217. const uint8_t *in,
  218. uint8_t *out,
  219. const uint8_t *iv);
  220. cryerror_t cryDecryptAES_CBC(CRYDriver *cryp,
  221. crykey_t key_id,
  222. size_t size,
  223. const uint8_t *in,
  224. uint8_t *out,
  225. const uint8_t *iv);
  226. cryerror_t cryEncryptAES_CFB(CRYDriver *cryp,
  227. crykey_t key_id,
  228. size_t size,
  229. const uint8_t *in,
  230. uint8_t *out,
  231. const uint8_t *iv);
  232. cryerror_t cryDecryptAES_CFB(CRYDriver *cryp,
  233. crykey_t key_id,
  234. size_t size,
  235. const uint8_t *in,
  236. uint8_t *out,
  237. const uint8_t *iv);
  238. cryerror_t cryEncryptAES_CTR(CRYDriver *cryp,
  239. crykey_t key_id,
  240. size_t size,
  241. const uint8_t *in,
  242. uint8_t *out,
  243. const uint8_t *iv);
  244. cryerror_t cryDecryptAES_CTR(CRYDriver *cryp,
  245. crykey_t key_id,
  246. size_t size,
  247. const uint8_t *in,
  248. uint8_t *out,
  249. const uint8_t *iv);
  250. cryerror_t cryEncryptAES_GCM(CRYDriver *cryp,
  251. crykey_t key_id,
  252. size_t size,
  253. const uint8_t *in,
  254. uint8_t *out,
  255. const uint8_t *iv,
  256. size_t aadsize,
  257. const uint8_t *aad,
  258. uint8_t *authtag);
  259. cryerror_t cryDecryptAES_GCM(CRYDriver *cryp,
  260. crykey_t key_id,
  261. size_t size,
  262. const uint8_t *in,
  263. uint8_t *out,
  264. const uint8_t *iv,
  265. size_t aadsize,
  266. const uint8_t *aad,
  267. uint8_t *authtag);
  268. cryerror_t cryLoadDESTransientKey(CRYDriver *cryp,
  269. size_t size,
  270. const uint8_t *keyp);
  271. cryerror_t cryEncryptDES(CRYDriver *cryp,
  272. crykey_t key_id,
  273. const uint8_t *in,
  274. uint8_t *out);
  275. cryerror_t cryDecryptDES(CRYDriver *cryp,
  276. crykey_t key_id,
  277. const uint8_t *in,
  278. uint8_t *out);
  279. cryerror_t cryEncryptDES_ECB(CRYDriver *cryp,
  280. crykey_t key_id,
  281. size_t size,
  282. const uint8_t *in,
  283. uint8_t *out);
  284. cryerror_t cryDecryptDES_ECB(CRYDriver *cryp,
  285. crykey_t key_id,
  286. size_t size,
  287. const uint8_t *in,
  288. uint8_t *out);
  289. cryerror_t cryEncryptDES_CBC(CRYDriver *cryp,
  290. crykey_t key_id,
  291. size_t size,
  292. const uint8_t *in,
  293. uint8_t *out,
  294. const uint8_t *iv);
  295. cryerror_t cryDecryptDES_CBC(CRYDriver *cryp,
  296. crykey_t key_id,
  297. size_t size,
  298. const uint8_t *in,
  299. uint8_t *out,
  300. const uint8_t *iv);
  301. cryerror_t crySHA1Init(CRYDriver *cryp, SHA1Context *sha1ctxp);
  302. cryerror_t crySHA1Update(CRYDriver *cryp, SHA1Context *sha1ctxp,
  303. size_t size, const uint8_t *in);
  304. cryerror_t crySHA1Final(CRYDriver *cryp, SHA1Context *sha1ctxp,
  305. uint8_t *out);
  306. cryerror_t crySHA256Init(CRYDriver *cryp, SHA256Context *sha256ctxp);
  307. cryerror_t crySHA256Update(CRYDriver *cryp, SHA256Context *sha256ctxp,
  308. size_t size, const uint8_t *in);
  309. cryerror_t crySHA256Final(CRYDriver *cryp, SHA256Context *sha256ctxp,
  310. uint8_t *out);
  311. cryerror_t crySHA512Init(CRYDriver *cryp, SHA512Context *sha512ctxp);
  312. cryerror_t crySHA512Update(CRYDriver *cryp, SHA512Context *sha512ctxp,
  313. size_t size, const uint8_t *in);
  314. cryerror_t crySHA512Final(CRYDriver *cryp, SHA512Context *sha512ctxp,
  315. uint8_t *out);
  316. cryerror_t cryLoadHMACTransientKey(CRYDriver *cryp,
  317. size_t size,
  318. const uint8_t *keyp);
  319. cryerror_t cryHMACSHA256Init(CRYDriver *cryp,
  320. HMACSHA256Context *hmacsha256ctxp);
  321. cryerror_t cryHMACSHA256Update(CRYDriver *cryp,
  322. HMACSHA256Context *hmacsha256ctxp,
  323. size_t size,
  324. const uint8_t *in);
  325. cryerror_t cryHMACSHA256Final(CRYDriver *cryp,
  326. HMACSHA256Context *hmacsha256ctxp,
  327. uint8_t *out);
  328. cryerror_t cryHMACSHA512Init(CRYDriver *cryp,
  329. HMACSHA512Context *hmacsha512ctxp);
  330. cryerror_t cryHMACSHA512Update(CRYDriver *cryp,
  331. HMACSHA512Context *hmacsha512ctxp,
  332. size_t size,
  333. const uint8_t *in);
  334. cryerror_t cryHMACSHA512Final(CRYDriver *cryp,
  335. HMACSHA512Context *hmacsha512ctxp,
  336. uint8_t *out);
  337. #ifdef __cplusplus
  338. }
  339. #endif
  340. #endif /* HAL_USE_CRYPTO == TRUE */
  341. #endif /* HAL_CRYPTO_H */
  342. /** @} */