1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _COMMON_CRYPTO_SYM_H
8 #define _COMMON_CRYPTO_SYM_H
9 
10 
11 #include <stdint.h>
12 #include "cc_crypto_defs.h"
13 
14 #define CC_COMMON_CALC_CBC_ENCODE_SIZE(size)  (AES_BLOCK_SIZE + (((size + AES_BLOCK_SIZE)/AES_BLOCK_SIZE)*AES_BLOCK_SIZE))
15 
16 
17 /**
18 * @brief The CC_CommonAesCtrEncrypt encrypts (AES CTR) a given data and returns it.
19 *
20 * @param[in] DataIn_ptr - the data to encrypt
21 * @param[in] DataInSize - the data size
22 * @param[in] Key_ptr - the AES key
23 * @param[in] KeySize - AES key size (must be one of the allowed AES key sizes)
24 * @param[in] IV_ptr - IV (AES IV size is constant)
25 * @param[in] Output_ptr - Output buffer
26 */
27 /*********************************************************/
28 int32_t CC_CommonAesCtrEncrypt(int8_t  *pDataIn,
29                   int32_t    dataInSize,
30                   int8_t   *pKey,
31                   int32_t    keySize,
32                   int8_t   *pIV,
33                   int8_t   *pEncBuff);
34 
35 
36 /**
37 * @brief The CC_CommonAesCbcDecrypt decrypts (AES CBC) a given data
38 *               and returns the decrypted buffer.
39 *
40 * @param[in] pwdFileName - file name for passsword to generate key and IV from
41 * @param[in] pEncBuff - the encrypted buffer- input buffer
42 * @param[in] encBuffSize - the encrypted buffer size
43 * @param[out] pDecBuff -the decrypted buffer.
44 *
45 * NOTE: pDecBuff - alocated size must be multiple of 16 bytes. same as encBuffSize
46 */
47 /*********************************************************/
48 int32_t CC_CommonAesCbcDecrypt(int8_t *pwdFileName,
49                   int8_t *pEncBuff,
50                   int32_t   encBuffSize,
51                   int8_t *pDecBuff);
52 
53 /**
54  * @brief This function
55  *
56  * @param[in]
57  *
58  * @param[out]
59  *
60  * @return uint8_t -
61 
62  */
63 int32_t CC_CommonAesCcmEncrypt(uint8_t *keyBuf,
64                   uint8_t *nonce, uint32_t nonceLen,
65                   uint8_t *aData, uint32_t aDatalen,
66                   uint8_t *plainTxt, uint32_t plainTxtLen,
67                   uint8_t *enBuff, uint32_t *enBuffLen,
68                   uint8_t *tagBuff, uint32_t tagBuffLen);
69 
70 /**
71 * @brief Encrypts (AES CMAC) a given data and returns it.
72 *
73 * @param[in] pDataIn - the data to encrypt
74 * @param[in] dataInSize - the data size
75 * @param[in] pKey - the AES key
76 * @param[in] keySize - the key size in bytes
77 * @param[in] pOutput - Output buffer
78 */
79 /*********************************************************/
80 int32_t CC_CommonAesCmacEncrypt(int8_t *pDataIn,
81                    int32_t  dataInSize,
82                    int8_t *pKey,
83                    int32_t  keySize,
84                    int8_t *pOutput);
85 
86 
87 
88 /**
89  * @brief The Common_CalcHash calculates HASH on the public key and Np using OpenSSL.
90  *
91  * @param[in] pPemDecryted - the decrypted public key (input data for HASH)
92  * @param[out] pHash - the HASH SHA 256 calculated on the data
93  *
94  */
95 /*********************************************************/
96 int32_t CC_CommonCalcHash(uint8_t *pPemDecryted, int32_t pemDecryptedSize, uint8_t *pHash, int32_t hashSize );
97 
98 /**
99  * @brief The Common_CalcHash calculates HASH on the public key and Np using OpenSSL.
100  *
101  * @param[in] pPemDecryted - the decrypted public key (input data for HASH)
102  * @param[out] pHash - the HASH SHA 256 calculated on the data
103  *
104  */
105 /*********************************************************/
106 int32_t CC_CommonCalcSha1(uint8_t *pDataIn, int32_t dataInSize, uint8_t *pHash);
107 
108 
109 /**
110 * @brief Encrypts (AES ECB) a given data and returns it.
111 *
112 * @param[in] pDataIn - the data to encrypt
113 * @param[in] dataInSize - the data size
114 * @param[in] pKey - the AES key
115 * @param[in] keySize - AES key size (must be one of the allowed AES key sizes)
116 * @param[out] pEncBuff - the encrypted buffer
117 */
118 /*********************************************************/
119 int32_t CC_CommonAesEcbEncrypt(uint8_t  *pDataIn,
120                   uint32_t    dataInSize,
121                   uint8_t   *pKey,
122                   uint32_t    keySize,
123                   uint8_t   *pEncBuff);
124 
125 #endif
126