1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_MMCAU_H_
10 #define _FSL_MMCAU_H_
11 
12 #include "fsl_common.h"
13 
14 /*******************************************************************************
15  * API
16  ******************************************************************************/
17 
18 /*!
19  * @addtogroup mmcau MMCAU Peripheral Driver
20  * @{
21  */
22 
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26 
27 /*!
28  * @brief AES: Performs an AES key expansion.
29  *
30  * This function performs an AES key expansion.
31  *
32  * @param key Pointer to input key (128, 192, 256 bits in length).
33  * @param keySize Key size in bytes (16, 24, 32)
34  * @param[out] keySch Pointer to key schedule output (44, 52, 60 longwords)
35  * @note Table below shows the requirements for the MMCAU_AES_SetKey() function when using AES128, AES192, or AES256.
36  * | [in] Key Size (bits)    |  [out] Key Schedule Size (32 bit data values)  |
37  * | :---------------------: | :--------------------------------------------: |
38  * |          128            |                      44                        |
39  * |          192            |                      52                        |
40  * |          256            |                      60                        |
41  * @return Status of the operation. (kStatus_Success, kStatus_InvalidArgument, kStatus_Fail)
42  */
43 status_t MMCAU_AES_SetKey(const uint8_t *key, const size_t keySize, uint8_t *keySch);
44 
45 /*!
46  * @brief AES: Encrypts a single 16 byte block.
47  *
48  *  This function encrypts a single 16-byte block for AES128, AES192, and AES256.
49  *
50  * @param in Pointer to 16-byte block of input plaintext.
51  * @param keySch Pointer to key schedule (44, 52, 60 longwords).
52  * @param aesRounds Number of AES rounds (10, 12, 14 = f(key_schedule)).
53  * @param[out] out Pointer to 16-byte block of output ciphertext.
54  * @note   Input and output blocks may overlap.@n
55  *         Table below shows the requirements for the MMCAU_AES_EncryptEcb()/MMCAU_AES_DecryptEcb()
56  *         function when using AES128, AES192 or AES256.
57  * | Block Cipher | [in] Key Schedule Size (longwords) | [in] Number of AES rounds |
58  * | :----------: | :--------------------------------: | :-----------------------: |
59  * |    AES128    |               44                   |             10            |
60  * |    AES192    |               52                   |             12            |
61  * |    AES256    |               60                   |             14            |
62  * @return Status of the operation. (kStatus_Success, kStatus_InvalidArgument, kStatus_Fail)
63  */
64 status_t MMCAU_AES_EncryptEcb(const uint8_t *in, const uint8_t *keySch, uint32_t aesRounds, uint8_t *out);
65 
66 /*!
67  * @brief AES: Decrypts a single 16-byte block.
68  *
69  * This function decrypts a single 16-byte block for AES128, AES192, and AES256.
70  *
71  * @param in Pointer to 16-byte block of input ciphertext.
72  * @param keySch Pointer to key schedule (44, 52, 60 longwords).
73  * @param aesRounds Number of AES rounds (10, 12, 14 = f(key_schedule)).
74  * @param[out] out Pointer to 16-byte block of output plaintext.
75  * @note   Input and output blocks may overlap.@n
76  *         Table below shows the requirements for the cau_aes_encrypt()/cau_aes_decrypt().
77  *         function when using AES128, AES192 or AES256.
78  * | Block Cipher | [in] Key Schedule Size (longwords) | [in] Number of AES rounds |
79  * | :----------: | :--------------------------------: | :-----------------------: |
80  * |    AES128    |               44                   |             10            |
81  * |    AES192    |               52                   |             12            |
82  * |    AES256    |               60                   |             14            |
83  * @return Status of the operation. (kStatus_Success, kStatus_InvalidArgument, kStatus_Fail)
84  */
85 status_t MMCAU_AES_DecryptEcb(const uint8_t *in, const uint8_t *keySch, uint32_t aesRounds, uint8_t *out);
86 
87 /*!
88  * @brief DES: Checks the key parity.
89  *
90  * This function checks the parity of a DES key.
91  *
92  * @param key 64-bit DES key with parity bits.
93  * @return kStatus_Success No error.
94  * @return kStatus_Fail Parity error.
95  * @return kStatus_InvalidArgument Key argument is NULL.
96  */
97 status_t MMCAU_DES_ChkParity(const uint8_t *key);
98 
99 /*!
100  * @brief DES: Encrypts a single 8-byte block.
101  *
102  * This function encrypts a single 8-byte block with the DES algorithm.
103  *
104  * @param in Pointer to 8-byte block of input plaintext.
105  * @param key Pointer to 64-bit DES key with parity bits.
106  * @param[out] out Pointer to 8-byte block of output ciphertext.
107  * @note Input and output blocks may overlap.
108  * @return Status of the operation. (kStatus_Success, kStatus_InvalidArgument, kStatus_Fail)
109  */
110 status_t MMCAU_DES_EncryptEcb(const uint8_t *in, const uint8_t *key, uint8_t *out);
111 
112 /*!
113  * @brief DES: Decrypts a single 8-byte block.
114  *
115  * This function decrypts a single 8-byte block with the DES algorithm.
116  *
117  * @param in Pointer to 8-byte block of input ciphertext.
118  * @param key Pointer to 64-bit DES key with parity bits.
119  * @param[out] out Pointer to 8-byte block of output plaintext.
120  * @note  Input and output blocks may overlap.
121  * @return Status of the operation. (kStatus_Success, kStatus_InvalidArgument, kStatus_Fail)
122  */
123 status_t MMCAU_DES_DecryptEcb(const uint8_t *in, const uint8_t *key, uint8_t *out);
124 
125 /*!
126  * @brief MD5: Initializes the MD5 state variables.
127  *
128  * This function initializes the MD5 state variables.
129  * The output can be used as input to MMCAU_MD5_HashN().
130  *
131  * @param[out] md5State Pointer to 128-bit block of md5 state variables: a,b,c,d
132  */
133 status_t MMCAU_MD5_InitializeOutput(uint32_t *md5State);
134 
135 /*!
136  * @brief MD5: Updates the MD5 state variables with n message blocks.
137  *
138  * This function updates the MD5 state variables for one or more input message blocks.
139  *
140  * @param msgData Pointer to start of input message data.
141  * @param numBlocks Number of 512-bit blocks to process.
142  * @param[in,out] md5State Pointer to 128-bit block of MD5 state variables: a, b, c, d.
143  * @note  Input message and digest output blocks must not overlap.
144  *        The MMCAU_MD5_InitializeOutput() function must be called when starting a new hash.
145  *        Useful when handling non-contiguous input message blocks.
146  */
147 status_t MMCAU_MD5_HashN(const uint8_t *msgData, uint32_t numBlocks, uint32_t *md5State);
148 
149 /*!
150  * @brief MD5: Updates the MD5 state variables.
151  *
152  * This function updates the MD5 state variables for one or more input message blocks.
153  * It starts a new hash as it internally calls MMCAU_MD5_InitializeOutput() first.
154  *
155  * @param msgData Pointer to start of input message data.
156  * @param numBlocks Number of 512-bit blocks to process.
157  * @param[out] md5State Pointer to 128-bit block of MD5 state variables: a, b, c, d.
158  * @note  Input message and digest output blocks must not overlap.
159  *        The MMCAU_MD5_InitializeOutput() function is not required to be called
160  *        as it is called internally to start a new hash.
161  *        All input message blocks must be contiguous.
162  */
163 status_t MMCAU_MD5_Update(const uint8_t *msgData, uint32_t numBlocks, uint32_t *md5State);
164 
165 /*!
166  * @brief SHA1: Initializes the SHA1 state variables.
167  *
168  * This function initializes the SHA1 state variables.
169  * The output can be used as input to MMCAU_SHA1_HashN().
170  *
171  * @param[out] sha1State Pointer to 160-bit block of SHA1 state variables: a, b, c, d, e.
172  */
173 status_t MMCAU_SHA1_InitializeOutput(uint32_t *sha1State);
174 
175 /*!
176  * @brief SHA1: Updates the SHA1 state variables with n message blocks.
177  *
178  * This function updates the SHA1 state variables for one or more input message blocks.
179  *
180  * @param msgData Pointer to start of input message data.
181  * @param numBlocks Number of 512-bit blocks to process.
182  * @param[in,out] sha1State Pointer to 160-bit block of SHA1 state variables: a, b, c, d, e.
183  * @note  Input message and digest output blocks must not overlap.
184  *        The MMCAU_SHA1_InitializeOutput() function must be called when starting a new hash.
185  *        Useful when handling non-contiguous input message blocks.
186  */
187 status_t MMCAU_SHA1_HashN(const uint8_t *msgData, uint32_t numBlocks, uint32_t *sha1State);
188 
189 /*!
190  * @brief SHA1: Updates the SHA1 state variables.
191  *
192  * This function updates the SHA1 state variables for one or more input message blocks.
193  * It starts a new hash as it internally calls MMCAU_SHA1_InitializeOutput() first.
194  *
195  * @param msgData Pointer to start of input message data.
196  * @param numBlocks Number of 512-bit blocks to process.
197  * @param[out] sha1State Pointer to 160-bit block of SHA1 state variables: a, b, c, d, e.
198  * @note  Input message and digest output blocks must not overlap.
199  *        The MMCAU_SHA1_InitializeOutput() function is not required to be called
200  *        as it is called internally to start a new hash.
201  *        All input message blocks must be contiguous.
202  */
203 status_t MMCAU_SHA1_Update(const uint8_t *msgData, uint32_t numBlocks, uint32_t *sha1State);
204 
205 /*!
206  * @brief SHA256: Initializes the SHA256 state variables.
207  *
208  * This function initializes the SHA256 state variables.
209  * The output can be used as input to MMCAU_SHA256_HashN().
210  *
211  * @param[out] sha256State Pointer to 256-bit block of SHA2 state variables a, b, c, d, e, f, g, h.
212  * @return kStatus_Success No error. CAU hardware support for SHA256 is present.
213  * @return kStatus_Fail Error. CAU hardware support for SHA256 is not present.
214  * @return kStatus_InvalidArgument Error. sha256State is NULL.
215  */
216 status_t MMCAU_SHA256_InitializeOutput(uint32_t *sha256State);
217 
218 /*!
219  * @brief SHA256: Updates the SHA256 state variables with n message blocks.
220  *
221  * This function updates SHA256 state variables for one or more input message blocks.
222  *
223  * @param input Pointer to start of input message data.
224  * @param numBlocks Number of 512-bit blocks to process.
225  * @param[in,out] sha256State Pointer to 256-bit block of SHA2 state variables: a, b, c, d, e, f, g, h.
226  * @note  Input message and digest output blocks must not overlap.
227  *        The MMCAU_SHA256_InitializeOutput() function must be called when starting a new hash.
228  *        Useful when handling non-contiguous input message blocks.
229  */
230 status_t MMCAU_SHA256_HashN(const uint8_t *input, uint32_t numBlocks, uint32_t *sha256State);
231 
232 /*!
233  * @brief SHA256: Updates SHA256 state variables.
234  *
235  * This function updates the SHA256 state variables for one or more input message blocks.
236  * It starts a new hash as it internally calls cau_sha256_initialize_output() first.
237  *
238  * @param input Pointer to start of input message data.
239  * @param numBlocks Number of 512-bit blocks to process.
240  * @param[out] sha256State Pointer to 256-bit block of SHA2 state variables: a, b, c, d, e, f, g, h.
241  * @note  Input message and digest output blocks must not overlap.
242  *        The MMCAU_SHA256_InitializeOutput() function is not required to be called.
243  *        as it is called internally to start a new hash.
244  *        All input message blocks must be contiguous.
245  */
246 status_t MMCAU_SHA256_Update(const uint8_t *input, uint32_t numBlocks, uint32_t *sha256State);
247 
248 #if defined(__cplusplus)
249 }
250 #endif
251 
252 /*! @}*/
253 
254 #endif /* _FSL_MMCAU_H_ */
255