1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #ifndef _BSV_CRYPTO_DRIVER_H
9 #define _BSV_CRYPTO_DRIVER_H
10 
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 #include "cc_sec_defs.h"
18 #include "bsv_crypto_defs.h"
19 
20 /*! @file
21 @brief This file contains crypto driver definitions: SH256, CMAC KDF, and CCM.
22 */
23 
24 /************************ Defines ******************************/
25 
26 /*! SHA256 digest result in words. */
27 #define CC_BSV_SHA256_DIGEST_SIZE_IN_WORDS  8
28 /*! SHA256 digest result in bytes. */
29 #define CC_BSV_SHA256_DIGEST_SIZE_IN_BYTES  CC_BSV_SHA256_DIGEST_SIZE_IN_WORDS*sizeof(uint32_t)
30 
31 /*! SHA256 maximal data size to be hashed */
32 #define CC_BSV_SHA256_MAX_DATA_SIZE_IN_BYTES    0x00010000 /* 64KB */
33 
34 
35 /*! The derived key size for 128 bits. */
36 #define CC_BSV_128BITS_KEY_SIZE_IN_BYTES    16
37 
38 /*! The derived key size for 256 bits. */
39 #define CC_BSV_256BITS_KEY_SIZE_IN_BYTES    32
40 
41 /*! Maximal label length in bytes. */
42 #define CC_BSV_MAX_LABEL_LENGTH_IN_BYTES    8
43 
44 /*! Maximal context length in bytes. */
45 #define CC_BSV_MAX_CONTEXT_LENGTH_IN_BYTES  32
46 
47 /*! KDF 128 bits key fixed data size in bytes. */
48 #define CC_BSV_KDF_DATA_128BITS_SIZE_IN_BYTES   3 /*!< \internal 0x01, 0x00, lengt(-0x80) */
49 /*! KDF 256 bits key fixed data size in bytes. */
50 #define CC_BSV_KDF_DATA_256BITS_SIZE_IN_BYTES   4 /*!< \internal 0x02, 0x00, lengt(-0x0100) */
51 
52 /*! KDF data maximal size in bytes. */
53 #define CC_BSV_KDF_MAX_SIZE_IN_BYTES (CC_BSV_KDF_DATA_256BITS_SIZE_IN_BYTES + CC_BSV_MAX_LABEL_LENGTH_IN_BYTES + CC_BSV_MAX_CONTEXT_LENGTH_IN_BYTES)
54 
55 
56 /*! Maximal AES CCM associated data size in bytes. */
57 #define CC_BSV_CCM_MAX_ASSOC_DATA_SIZE_IN_BYTES         0xff00  /* 2^16-2^8 */
58 
59 /*! Maximal AES CCM text data size in bytes. */
60 #define CC_BSV_CCM_MAX_TEXT_DATA_SIZE_IN_BYTES      0x00010000  /* 64KB */
61 
62 /*! AES block size in bytes. */
63 #define BSV_AES_BLOCK_SIZE_IN_BYTES 16
64 /*! AES IV size in bytes. */
65 #define BSV_AES_IV_SIZE_IN_BYTES        16
66 /*! AES IV size in words. */
67 #define BSV_AES_IV_SIZE_IN_WORDS        4
68 
69 /*! HASH SHA256 control value. */
70 #define BSV_HASH_CTL_SHA256_VAL         0x2UL
71 /*! HASH SHA256 padding configuration. */
72 #define BSV_HASH_PAD_CFG_VAL            0x4UL
73 
74 /************************ Typedefs  *****************************/
75 /*! Definitions of cryptographic mode. */
76 typedef enum bsvCryptoMode {
77     /*! AES.*/
78     BSV_CRYPTO_AES = 1,
79     /*! AES and HASH.*/
80     BSV_CRYPTO_AES_AND_HASH = 3,
81     /*! HASH.*/
82     BSV_CRYPTO_HASH = 7,
83     /*! AES to HASH and to DOUT.*/
84     BSV_CRYPTO_AES_TO_HASH_AND_DOUT = 10,
85     /*! Reserved.*/
86     BSV_CRYPTO_RESERVE32B = INT32_MAX
87 }bsvCryptoMode_t;
88 
89 /*! Definitions for AES modes. */
90 typedef enum bsvAesMode {
91     /*! AES CTR mode.*/
92     BSV_AES_CIPHER_CTR = 2,
93     /*! AES CBC MAC mode.*/
94     BSV_AES_CIPHER_CBC_MAC = 3,
95     /*! AES CMAC mode.*/
96     BSV_AES_CIPHER_CMAC = 7,
97     /*! AES CCM PE mode.*/
98     BSV_AES_CIPHER_CCMPE = 9,
99     /*! AES CCM PD mode.*/
100     BSV_AES_CIPHER_CCMPD = 10,
101     /*! Reserved.*/
102     BSV_AES_CIPHER_RESERVE32B = INT32_MAX
103 }bsvAesMode_t;
104 
105 /*! Definitions for AES directions. */
106 typedef enum bsvAesDirection {
107     /*! Encrypt.*/
108     BSV_AES_DIRECTION_ENCRYPT = 0,
109     /*! Decrypt.*/
110     BSV_AES_DIRECTION_DECRYPT = 1,
111     /*! Reserved.*/
112     BSV_AES_DIRECTION_RESERVE32B = INT32_MAX
113 }bsvAesDirection_t;
114 
115 /*! Defintions for AES key sizes. */
116 typedef enum bsvAesKeySize {
117     /*! 128 bits AES key. */
118     BSV_AES_KEY_SIZE_128BITS = 0,
119     /*! 256 bits AES key. */
120     BSV_AES_KEY_SIZE_256BITS = 2,
121     /*! Reserved.*/
122     BSV_AES_KEY_SIZE_RESERVE32B = INT32_MAX
123 }bsvAesKeySize_t;
124 
125 /***************************** function declaration **************************/
126 
127 
128 CCError_t BsvAes(unsigned long          hwBaseAddress,
129             bsvAesMode_t        mode,
130             CCBsvKeyType_t          keyType,
131             uint32_t            *pUserKey,
132             size_t              userKeySize,
133             uint32_t        *pIvBuf,
134             uint8_t         *pDataIn,
135             uint8_t         *pDataOut,
136             size_t                  dataSize,
137             CCBsvCmacResult_t   cmacResBuf);
138 
139 CCError_t BsvCryptoImageInit( unsigned long     hwBaseAddress,
140             bsvCryptoMode_t     mode,
141             CCBsvKeyType_t      keyType);
142 
143 CCError_t BsvCryptoImageUpdate( unsigned long   hwBaseAddress,
144             bsvCryptoMode_t     mode,
145             CCBsvKeyType_t          keyType,
146             uint32_t        *pCtrStateBuf,
147             uint8_t         *pDataIn,
148             uint8_t         *pDataOut,
149             size_t                  dataSize,
150             CCHashResult_t          hashBuff,
151             uint8_t         isLoadIV);
152 
153 CCError_t BsvCryptoImageFinish( unsigned long   hwBaseAddress,
154             bsvCryptoMode_t     mode,
155             CCHashResult_t          hashBuff);
156 
157 
158 /* SHA256 */
159 void InitBsvHash(unsigned long hwBaseAddress);
160 void FreeBsvHash(unsigned long hwBaseAddress);
161 CCError_t ProcessBsvHash(unsigned long hwBaseAddress, uint32_t inputDataAddr, uint32_t dataInSize);
162 void FinishBsvHash(unsigned long hwBaseAddress, CCHashResult_t  HashBuff);
163 
164 
165 /* AES (CTR, CMAC ) */
166 
167 void InitBsvAes(unsigned long hwBaseAddress);
168 void FreeBsvAes(unsigned long hwBaseAddress);
169 CCError_t ProcessBsvAes(unsigned long   hwBaseAddress,
170         bsvAesMode_t        mode,
171         CCBsvKeyType_t      keyType,
172         uint32_t            *pUserKey,
173         size_t              userKeySize,
174         uint32_t        *pCtrStateBuf,
175         uint32_t        inputDataAddr,
176         uint32_t        outputDataAddr,
177         uint32_t        blockSize,
178         uint8_t         isLoadIv);
179 
180 void FinishBsvAes(unsigned long hwBaseAddress,
181     bsvAesMode_t        mode,
182     CCBsvCmacResult_t   cmacResBuf);
183 
184 /* AES-CCM */
185 
186 CCError_t ProcessBsvAesCcm(unsigned long    hwBaseAddress,
187             bsvAesMode_t    mode,
188             uint32_t    *pKeyBuf,
189             uint32_t    *pIvBuf,
190             uint32_t    *pCtrStateBuf,
191             uint32_t    inputDataAddr,
192             uint32_t    outputDataAddr,
193             uint32_t    blockSize);
194 
195 void FinishBsvAesCcm(unsigned long hwBaseAddress,
196         bsvAesMode_t    mode,
197         uint32_t    *pIvBuf,
198         uint32_t    *pCtrStateBuf);
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif
205 
206 
207