cau_api.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * All rights reserved.
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _CAU_API_H_
  35. #define _CAU_API_H_
  36. /*******************************************************************************
  37. * Definitions
  38. ******************************************************************************/
  39. /*!
  40. * @addtogroup mmcau MMCAU Peripheral Driver
  41. * @{
  42. */
  43. #define cau_aes_set_key mmcau_aes_set_key
  44. #define cau_aes_encrypt mmcau_aes_encrypt
  45. #define cau_aes_decrypt mmcau_aes_decrypt
  46. #define cau_des_chk_parity mmcau_des_chk_parity
  47. #define cau_des_encrypt mmcau_des_encrypt
  48. #define cau_des_decrypt mmcau_des_decrypt
  49. #define cau_md5_initialize_output mmcau_md5_initialize_output
  50. #define cau_md5_hash_n mmcau_md5_hash_n
  51. #define cau_md5_update mmcau_md5_update
  52. #define cau_md5_hash mmcau_md5_hash
  53. #define cau_sha1_initialize_output mmcau_sha1_initialize_output
  54. #define cau_sha1_hash_n mmcau_sha1_hash_n
  55. #define cau_sha1_update mmcau_sha1_update
  56. #define cau_sha1_hash mmcau_sha1_hash
  57. #define cau_sha256_initialize_output mmcau_sha256_initialize_output
  58. #define cau_sha256_hash_n mmcau_sha256_hash_n
  59. #define cau_sha256_update mmcau_sha256_update
  60. #define cau_sha256_hash mmcau_sha256_hash
  61. /*******************************************************************************
  62. * API
  63. ******************************************************************************/
  64. #if defined(__cplusplus)
  65. extern "C" {
  66. #endif
  67. /*!
  68. * @brief AES: Performs an AES key expansion
  69. *
  70. * This function performs an AES key expansion
  71. *
  72. * @param key Pointer to input key (128, 192, 256 bits in length).
  73. * @param key_size Key size in bits (128, 192, 256)
  74. * @param[out] key_sch Pointer to key schedule output (44, 52, 60 longwords)
  75. * @note All pointers must have word (4 bytes) alignment
  76. * @note Table below shows the requirements for the cau_aes_set_key() function when using AES128, AES192 or AES256.
  77. * | [in] Key Size (bits) | [out] Key Schedule Size (32 bit data values) |
  78. * | :---------------------: | :--------------------------------------------: |
  79. * | 128 | 44 |
  80. * | 192 | 52 |
  81. * | 256 | 60 |
  82. */
  83. void cau_aes_set_key(const unsigned char *key, const int key_size, unsigned char *key_sch);
  84. /*!
  85. * @brief AES: Encrypts a single 16 byte block
  86. *
  87. * This function encrypts a single 16-byte block for AES128, AES192 and AES256
  88. *
  89. * @param in Pointer to 16-byte block of input plaintext
  90. * @param key_sch Pointer to key schedule (44, 52, 60 longwords)
  91. * @param nr Number of AES rounds (10, 12, 14 = f(key_schedule))
  92. * @param[out] out Pointer to 16-byte block of output ciphertext
  93. * @note All pointers must have word (4 bytes) alignment
  94. * @note Input and output blocks may overlap.@n
  95. * Table below shows the requirements for the cau_aes_encrypt()/cau_aes_decrypt()
  96. * function when using AES128, AES192 or AES256.
  97. * | Block Cipher | [in] Key Schedule Size (longwords) | [in] Number of AES rounds |
  98. * | :----------: | :--------------------------------: | :-----------------------: |
  99. * | AES128 | 44 | 10 |
  100. * | AES192 | 52 | 12 |
  101. * | AES256 | 60 | 14 |
  102. */
  103. void cau_aes_encrypt(const unsigned char *in, const unsigned char *key_sch, const int nr, unsigned char *out);
  104. /*!
  105. * @brief AES: Decrypts a single 16-byte block
  106. *
  107. * This function decrypts a single 16-byte block for AES128, AES192 and AES256
  108. *
  109. * @param in Pointer to 16-byte block of input ciphertext
  110. * @param key_sch Pointer to key schedule (44, 52, 60 longwords)
  111. * @param nr Number of AES rounds (10, 12, 14 = f(key_schedule))
  112. * @param[out] out Pointer to 16-byte block of output plaintext
  113. * @note All pointers must have word (4 bytes) alignment
  114. * @note Input and output blocks may overlap.@n
  115. * Table below shows the requirements for the cau_aes_encrypt()/cau_aes_decrypt()
  116. * function when using AES128, AES192 or AES256.
  117. * | Block Cipher | [in] Key Schedule Size (longwords) | [in] Number of AES rounds |
  118. * | :----------: | :--------------------------------: | :-----------------------: |
  119. * | AES128 | 44 | 10 |
  120. * | AES192 | 52 | 12 |
  121. * | AES256 | 60 | 14 |
  122. */
  123. void cau_aes_decrypt(const unsigned char *in, const unsigned char *key_sch, const int nr, unsigned char *out);
  124. /*!
  125. * @brief DES: Checks key parity
  126. *
  127. * This function checks the parity of a DES key
  128. *
  129. * @param key 64-bit DES key with parity bits. Must have word (4 bytes) alignment.
  130. * @return 0 no error
  131. * @return -1 parity error
  132. */
  133. int cau_des_chk_parity(const unsigned char *key);
  134. /*!
  135. * @brief DES: Encrypts a single 8-byte block
  136. *
  137. * This function encrypts a single 8-byte block with DES algorithm.
  138. *
  139. * @param in Pointer to 8-byte block of input plaintext
  140. * @param key Pointer to 64-bit DES key with parity bits
  141. * @param[out] out Pointer to 8-byte block of output ciphertext
  142. * @note All pointers must have word (4 bytes) alignment
  143. * @note Input and output blocks may overlap.
  144. */
  145. void cau_des_encrypt(const unsigned char *in, const unsigned char *key, unsigned char *out);
  146. /*!
  147. * @brief DES: Decrypts a single 8-byte block
  148. *
  149. * This function decrypts a single 8-byte block with DES algorithm.
  150. *
  151. * @param in Pointer to 8-byte block of input ciphertext
  152. * @param key Pointer to 64-bit DES key with parity bits
  153. * @param[out] out Pointer to 8-byte block of output plaintext
  154. * @note All pointers must have word (4 bytes) alignment
  155. * @note Input and output blocks may overlap.
  156. */
  157. void cau_des_decrypt(const unsigned char *in, const unsigned char *key, unsigned char *out);
  158. /*!
  159. * @brief MD5: Initializes the MD5 state variables
  160. *
  161. * This function initializes the MD5 state variables.
  162. * The output can be used as input to cau_md5_hash() and cau_md5_hash_n().
  163. *
  164. * @param[out] md5_state Pointer to 128-bit block of md5 state variables: a,b,c,d
  165. * @note All pointers must have word (4 bytes) alignment
  166. */
  167. void cau_md5_initialize_output(const unsigned char *md5_state);
  168. /*!
  169. * @brief MD5: Updates MD5 state variables with n message blocks
  170. *
  171. * This function updates MD5 state variables for one or more input message blocks
  172. *
  173. * @param msg_data Pointer to start of input message data
  174. * @param num_blks Number of 512-bit blocks to process
  175. * @param[in,out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d
  176. * @note All pointers must have word (4 bytes) alignment
  177. * @note Input message and digest output blocks must not overlap.
  178. * The cau_md5_initialize_output() function must be called when starting a new hash.
  179. * Useful when handling non-contiguous input message blocks.
  180. */
  181. void cau_md5_hash_n(const unsigned char *msg_data, const int num_blks, unsigned char *md5_state);
  182. /*!
  183. * @brief MD5: Updates MD5 state variables
  184. *
  185. * This function updates MD5 state variables for one or more input message blocks.
  186. * It starts a new hash as it internally calls cau_md5_initialize_output() first.
  187. *
  188. * @param msg_data Pointer to start of input message data
  189. * @param num_blks Number of 512-bit blocks to process
  190. * @param[out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d
  191. * @note All pointers must have word (4 bytes) alignment
  192. * @note Input message and digest output blocks must not overlap.
  193. * The cau_md5_initialize_output() function is not required to be called
  194. * as it is called internally to start a new hash.
  195. * All input message blocks must be contiguous.
  196. */
  197. void cau_md5_update(const unsigned char *msg_data, const int num_blks, unsigned char *md5_state);
  198. /*!
  199. * @brief MD5: Updates MD5 state variables with one message block
  200. *
  201. * This function updates MD5 state variables for one input message block
  202. *
  203. * @param msg_data Pointer to start of 512-bits of input message data
  204. * @param[in,out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d
  205. * @note All pointers must have word (4 bytes) alignment
  206. * @note Input message and digest output blocks must not overlap.
  207. * The cau_md5_initialize_output() function must be called when starting a new hash.
  208. */
  209. void cau_md5_hash(const unsigned char *msg_data, unsigned char *md5_state);
  210. /*!
  211. * @brief SHA1: Initializes the SHA1 state variables
  212. *
  213. * This function initializes the SHA1 state variables.
  214. * The output can be used as input to cau_sha1_hash() and cau_sha1_hash_n().
  215. *
  216. * @param[out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e
  217. * @note All pointers must have word (4 bytes) alignment
  218. */
  219. void cau_sha1_initialize_output(const unsigned int *sha1_state);
  220. /*!
  221. * @brief SHA1: Updates SHA1 state variables with n message blocks
  222. *
  223. * This function updates SHA1 state variables for one or more input message blocks
  224. *
  225. * @param msg_data Pointer to start of input message data
  226. * @param num_blks Number of 512-bit blocks to process
  227. * @param[in,out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e
  228. * @note All pointers must have word (4 bytes) alignment
  229. * @note Input message and digest output blocks must not overlap.
  230. * The cau_sha1_initialize_output() function must be called when starting a new hash.
  231. * Useful when handling non-contiguous input message blocks.
  232. */
  233. void cau_sha1_hash_n(const unsigned char *msg_data, const int num_blks, unsigned int *sha1_state);
  234. /*!
  235. * @brief SHA1: Updates SHA1 state variables
  236. *
  237. * This function updates SHA1 state variables for one or more input message blocks.
  238. * It starts a new hash as it internally calls cau_sha1_initialize_output() first.
  239. *
  240. * @param msg_data Pointer to start of input message data
  241. * @param num_blks Number of 512-bit blocks to process
  242. * @param[out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e
  243. * @note All pointers must have word (4 bytes) alignment
  244. * @note Input message and digest output blocks must not overlap.
  245. * The cau_sha1_initialize_output() function is not required to be called
  246. * as it is called internally to start a new hash.
  247. * All input message blocks must be contiguous.
  248. */
  249. void cau_sha1_update(const unsigned char *msg_data, const int num_blks, unsigned int *sha1_state);
  250. /*!
  251. * @brief SHA1: Updates SHA1 state variables with one message block
  252. *
  253. * This function updates SHA1 state variables for one input message block
  254. *
  255. * @param msg_data Pointer to start of 512-bits of input message data
  256. * @param[in,out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e
  257. * @note All pointers must have word (4 bytes) alignment
  258. * @note Input message and digest output blocks must not overlap.
  259. * The cau_sha1_initialize_output() function must be called when starting a new hash.
  260. */
  261. void cau_sha1_hash(const unsigned char *msg_data, unsigned int *sha1_state);
  262. /*!
  263. * @brief SHA256: Initializes the SHA256 state variables
  264. *
  265. * This function initializes the SHA256 state variables.
  266. * The output can be used as input to cau_sha256_hash() and cau_sha256_hash_n().
  267. *
  268. * @param[out] output Pointer to 256-bit block of SHA2 state variables a,b,c,d,e,f,g,h
  269. * @note All pointers must have word (4 bytes) alignment
  270. * @return 0 No error. CAU hardware support for SHA256 is present.
  271. * @return -1 Error. CAU hardware support for SHA256 is not present.
  272. */
  273. int cau_sha256_initialize_output(const unsigned int *output);
  274. /*!
  275. * @brief SHA256: Updates SHA256 state variables with n message blocks
  276. *
  277. * This function updates SHA256 state variables for one or more input message blocks
  278. *
  279. * @param input Pointer to start of input message data
  280. * @param num_blks Number of 512-bit blocks to process
  281. * @param[in,out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h
  282. * @note All pointers must have word (4 bytes) alignment
  283. * @note Input message and digest output blocks must not overlap.
  284. * The cau_sha256_initialize_output() function must be called when starting a new hash.
  285. * Useful when handling non-contiguous input message blocks.
  286. */
  287. void cau_sha256_hash_n(const unsigned char *input, const int num_blks, unsigned int *output);
  288. /*!
  289. * @brief SHA256: Updates SHA256 state variables
  290. *
  291. * This function updates SHA256 state variables for one or more input message blocks.
  292. * It starts a new hash as it internally calls cau_sha256_initialize_output() first.
  293. *
  294. * @param input Pointer to start of input message data
  295. * @param num_blks Number of 512-bit blocks to process
  296. * @param[out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h
  297. * @note All pointers must have word (4 bytes) alignment
  298. * @note Input message and digest output blocks must not overlap.
  299. * The cau_sha256_initialize_output() function is not required to be called
  300. * as it is called internally to start a new hash.
  301. * All input message blocks must be contiguous.
  302. */
  303. void cau_sha256_update(const unsigned char *input, const int num_blks, unsigned int *output);
  304. /*!
  305. * @brief SHA256: Updates SHA256 state variables with one message block
  306. *
  307. * This function updates SHA256 state variables for one input message block
  308. *
  309. * @param input Pointer to start of 512-bits of input message data
  310. * @param[in,out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h
  311. * @note All pointers must have word (4 bytes) alignment
  312. * @note Input message and digest output blocks must not overlap.
  313. * The cau_sha256_initialize_output() function must be called when starting a new hash.
  314. */
  315. void cau_sha256_hash(const unsigned char *input, unsigned int *output);
  316. #if defined(__cplusplus)
  317. }
  318. #endif
  319. /*! @}*/
  320. #endif /* _CAU_API_H_ */