1 /* 2 * Copyright (c) 2015, Freescale Semiconductor, Inc. 3 * All rights reserved. 4 * 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 #ifndef _CAU_API_H_ 9 #define _CAU_API_H_ 10 11 /******************************************************************************* 12 * Definitions 13 ******************************************************************************/ 14 15 /*! 16 * @addtogroup mmcau MMCAU Peripheral Driver 17 * @{ 18 */ 19 #define cau_aes_set_key mmcau_aes_set_key 20 #define cau_aes_encrypt mmcau_aes_encrypt 21 #define cau_aes_decrypt mmcau_aes_decrypt 22 #define cau_des_chk_parity mmcau_des_chk_parity 23 #define cau_des_encrypt mmcau_des_encrypt 24 #define cau_des_decrypt mmcau_des_decrypt 25 #define cau_md5_initialize_output mmcau_md5_initialize_output 26 #define cau_md5_hash_n mmcau_md5_hash_n 27 #define cau_md5_update mmcau_md5_update 28 #define cau_md5_hash mmcau_md5_hash 29 #define cau_sha1_initialize_output mmcau_sha1_initialize_output 30 #define cau_sha1_hash_n mmcau_sha1_hash_n 31 #define cau_sha1_update mmcau_sha1_update 32 #define cau_sha1_hash mmcau_sha1_hash 33 #define cau_sha256_initialize_output mmcau_sha256_initialize_output 34 #define cau_sha256_hash_n mmcau_sha256_hash_n 35 #define cau_sha256_update mmcau_sha256_update 36 #define cau_sha256_hash mmcau_sha256_hash 37 38 /******************************************************************************* 39 * API 40 ******************************************************************************/ 41 #if defined(__cplusplus) 42 extern "C" { 43 #endif 44 45 /*! 46 * @brief AES: Performs an AES key expansion 47 * 48 * This function performs an AES key expansion 49 * 50 * @param key Pointer to input key (128, 192, 256 bits in length). 51 * @param key_size Key size in bits (128, 192, 256) 52 * @param[out] key_sch Pointer to key schedule output (44, 52, 60 longwords) 53 * @note All pointers must have word (4 bytes) alignment 54 * @note Table below shows the requirements for the cau_aes_set_key() function when using AES128, AES192 or AES256. 55 * | [in] Key Size (bits) | [out] Key Schedule Size (32 bit data values) | 56 * | :---------------------: | :--------------------------------------------: | 57 * | 128 | 44 | 58 * | 192 | 52 | 59 * | 256 | 60 | 60 */ 61 void cau_aes_set_key(const unsigned char *key, const int key_size, unsigned char *key_sch); 62 63 /*! 64 * @brief AES: Encrypts a single 16 byte block 65 * 66 * This function encrypts a single 16-byte block for AES128, AES192 and AES256 67 * 68 * @param in Pointer to 16-byte block of input plaintext 69 * @param key_sch Pointer to key schedule (44, 52, 60 longwords) 70 * @param nr Number of AES rounds (10, 12, 14 = f(key_schedule)) 71 * @param[out] out Pointer to 16-byte block of output ciphertext 72 * @note All pointers must have word (4 bytes) alignment 73 * @note Input and output blocks may overlap.@n 74 * Table below shows the requirements for the cau_aes_encrypt()/cau_aes_decrypt() 75 * function when using AES128, AES192 or AES256. 76 * | Block Cipher | [in] Key Schedule Size (longwords) | [in] Number of AES rounds | 77 * | :----------: | :--------------------------------: | :-----------------------: | 78 * | AES128 | 44 | 10 | 79 * | AES192 | 52 | 12 | 80 * | AES256 | 60 | 14 | 81 */ 82 void cau_aes_encrypt(const unsigned char *in, const unsigned char *key_sch, const int nr, unsigned char *out); 83 84 /*! 85 * @brief AES: Decrypts a single 16-byte block 86 * 87 * This function decrypts a single 16-byte block for AES128, AES192 and AES256 88 * 89 * @param in Pointer to 16-byte block of input ciphertext 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 plaintext 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_decrypt(const unsigned char *in, const unsigned char *key_sch, const int nr, unsigned char *out); 104 105 /*! 106 * @brief DES: Checks key parity 107 * 108 * This function checks the parity of a DES key 109 * 110 * @param key 64-bit DES key with parity bits. Must have word (4 bytes) alignment. 111 * @return 0 no error 112 * @return -1 parity error 113 */ 114 int cau_des_chk_parity(const unsigned char *key); 115 116 /*! 117 * @brief DES: Encrypts a single 8-byte block 118 * 119 * This function encrypts a single 8-byte block with DES algorithm. 120 * 121 * @param in Pointer to 8-byte block of input plaintext 122 * @param key Pointer to 64-bit DES key with parity bits 123 * @param[out] out Pointer to 8-byte block of output ciphertext 124 * @note All pointers must have word (4 bytes) alignment 125 * @note Input and output blocks may overlap. 126 */ 127 void cau_des_encrypt(const unsigned char *in, const unsigned char *key, unsigned char *out); 128 129 /*! 130 * @brief DES: Decrypts a single 8-byte block 131 * 132 * This function decrypts a single 8-byte block with DES algorithm. 133 * 134 * @param in Pointer to 8-byte block of input ciphertext 135 * @param key Pointer to 64-bit DES key with parity bits 136 * @param[out] out Pointer to 8-byte block of output plaintext 137 * @note All pointers must have word (4 bytes) alignment 138 * @note Input and output blocks may overlap. 139 */ 140 void cau_des_decrypt(const unsigned char *in, const unsigned char *key, unsigned char *out); 141 142 /*! 143 * @brief MD5: Initializes the MD5 state variables 144 * 145 * This function initializes the MD5 state variables. 146 * The output can be used as input to cau_md5_hash() and cau_md5_hash_n(). 147 * 148 * @param[out] md5_state Pointer to 128-bit block of md5 state variables: a,b,c,d 149 * @note All pointers must have word (4 bytes) alignment 150 */ 151 void cau_md5_initialize_output(const unsigned char *md5_state); 152 153 /*! 154 * @brief MD5: Updates MD5 state variables with n message blocks 155 * 156 * This function updates MD5 state variables for one or more input message blocks 157 * 158 * @param msg_data Pointer to start of input message data 159 * @param num_blks Number of 512-bit blocks to process 160 * @param[in,out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d 161 * @note All pointers must have word (4 bytes) alignment 162 * @note Input message and digest output blocks must not overlap. 163 * The cau_md5_initialize_output() function must be called when starting a new hash. 164 * Useful when handling non-contiguous input message blocks. 165 */ 166 void cau_md5_hash_n(const unsigned char *msg_data, const int num_blks, unsigned char *md5_state); 167 168 /*! 169 * @brief MD5: Updates MD5 state variables 170 * 171 * This function updates MD5 state variables for one or more input message blocks. 172 * It starts a new hash as it internally calls cau_md5_initialize_output() first. 173 * 174 * @param msg_data Pointer to start of input message data 175 * @param num_blks Number of 512-bit blocks to process 176 * @param[out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d 177 * @note All pointers must have word (4 bytes) alignment 178 * @note Input message and digest output blocks must not overlap. 179 * The cau_md5_initialize_output() function is not required to be called 180 * as it is called internally to start a new hash. 181 * All input message blocks must be contiguous. 182 */ 183 void cau_md5_update(const unsigned char *msg_data, const int num_blks, unsigned char *md5_state); 184 185 /*! 186 * @brief MD5: Updates MD5 state variables with one message block 187 * 188 * This function updates MD5 state variables for one input message block 189 * 190 * @param msg_data Pointer to start of 512-bits of input message data 191 * @param[in,out] md5_state Pointer to 128-bit block of MD5 state variables: a,b,c,d 192 * @note All pointers must have word (4 bytes) alignment 193 * @note Input message and digest output blocks must not overlap. 194 * The cau_md5_initialize_output() function must be called when starting a new hash. 195 */ 196 void cau_md5_hash(const unsigned char *msg_data, unsigned char *md5_state); 197 198 /*! 199 * @brief SHA1: Initializes the SHA1 state variables 200 * 201 * This function initializes the SHA1 state variables. 202 * The output can be used as input to cau_sha1_hash() and cau_sha1_hash_n(). 203 * 204 * @param[out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e 205 * @note All pointers must have word (4 bytes) alignment 206 */ 207 void cau_sha1_initialize_output(const unsigned int *sha1_state); 208 209 /*! 210 * @brief SHA1: Updates SHA1 state variables with n message blocks 211 * 212 * This function updates SHA1 state variables for one or more input message blocks 213 * 214 * @param msg_data Pointer to start of input message data 215 * @param num_blks Number of 512-bit blocks to process 216 * @param[in,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 * @note Input message and digest output blocks must not overlap. 219 * The cau_sha1_initialize_output() function must be called when starting a new hash. 220 * Useful when handling non-contiguous input message blocks. 221 */ 222 void cau_sha1_hash_n(const unsigned char *msg_data, const int num_blks, unsigned int *sha1_state); 223 224 /*! 225 * @brief SHA1: Updates SHA1 state variables 226 * 227 * This function updates SHA1 state variables for one or more input message blocks. 228 * It starts a new hash as it internally calls cau_sha1_initialize_output() first. 229 * 230 * @param msg_data Pointer to start of input message data 231 * @param num_blks Number of 512-bit blocks to process 232 * @param[out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e 233 * @note All pointers must have word (4 bytes) alignment 234 * @note Input message and digest output blocks must not overlap. 235 * The cau_sha1_initialize_output() function is not required to be called 236 * as it is called internally to start a new hash. 237 * All input message blocks must be contiguous. 238 */ 239 void cau_sha1_update(const unsigned char *msg_data, const int num_blks, unsigned int *sha1_state); 240 241 /*! 242 * @brief SHA1: Updates SHA1 state variables with one message block 243 * 244 * This function updates SHA1 state variables for one input message block 245 * 246 * @param msg_data Pointer to start of 512-bits of input message data 247 * @param[in,out] sha1_state Pointer to 160-bit block of SHA1 state variables: a,b,c,d,e 248 * @note All pointers must have word (4 bytes) alignment 249 * @note Input message and digest output blocks must not overlap. 250 * The cau_sha1_initialize_output() function must be called when starting a new hash. 251 */ 252 void cau_sha1_hash(const unsigned char *msg_data, unsigned int *sha1_state); 253 254 /*! 255 * @brief SHA256: Initializes the SHA256 state variables 256 * 257 * This function initializes the SHA256 state variables. 258 * The output can be used as input to cau_sha256_hash() and cau_sha256_hash_n(). 259 * 260 * @param[out] output Pointer to 256-bit block of SHA2 state variables a,b,c,d,e,f,g,h 261 * @note All pointers must have word (4 bytes) alignment 262 * @return 0 No error. CAU hardware support for SHA256 is present. 263 * @return -1 Error. CAU hardware support for SHA256 is not present. 264 */ 265 int cau_sha256_initialize_output(const unsigned int *output); 266 267 /*! 268 * @brief SHA256: Updates SHA256 state variables with n message blocks 269 * 270 * This function updates SHA256 state variables for one or more input message blocks 271 * 272 * @param input Pointer to start of input message data 273 * @param num_blks Number of 512-bit blocks to process 274 * @param[in,out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h 275 * @note All pointers must have word (4 bytes) alignment 276 * @note Input message and digest output blocks must not overlap. 277 * The cau_sha256_initialize_output() function must be called when starting a new hash. 278 * Useful when handling non-contiguous input message blocks. 279 */ 280 void cau_sha256_hash_n(const unsigned char *input, const int num_blks, unsigned int *output); 281 282 /*! 283 * @brief SHA256: Updates SHA256 state variables 284 * 285 * This function updates SHA256 state variables for one or more input message blocks. 286 * It starts a new hash as it internally calls cau_sha256_initialize_output() first. 287 * 288 * @param input Pointer to start of input message data 289 * @param num_blks Number of 512-bit blocks to process 290 * @param[out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h 291 * @note All pointers must have word (4 bytes) alignment 292 * @note Input message and digest output blocks must not overlap. 293 * The cau_sha256_initialize_output() function is not required to be called 294 * as it is called internally to start a new hash. 295 * All input message blocks must be contiguous. 296 */ 297 void cau_sha256_update(const unsigned char *input, const int num_blks, unsigned int *output); 298 299 /*! 300 * @brief SHA256: Updates SHA256 state variables with one message block 301 * 302 * This function updates SHA256 state variables for one input message block 303 * 304 * @param input Pointer to start of 512-bits of input message data 305 * @param[in,out] output Pointer to 256-bit block of SHA2 state variables: a,b,c,d,e,f,g,h 306 * @note All pointers must have word (4 bytes) alignment 307 * @note Input message and digest output blocks must not overlap. 308 * The cau_sha256_initialize_output() function must be called when starting a new hash. 309 */ 310 void cau_sha256_hash(const unsigned char *input, unsigned int *output); 311 312 #if defined(__cplusplus) 313 } 314 #endif 315 316 /*! @}*/ 317 #endif /* _CAU_API_H_ */ 318