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 CCSW_RSA_TYPES_H
8 #define CCSW_RSA_TYPES_H
9 
10 
11 #include "cc_pal_types.h"
12 #include "cc_hash.h"
13 #include "cc_rsa_types.h"
14 #include "ccsw_rsa_shared_types.h"
15 
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20 
21 
22 /************************ Defines ******************************/
23 
24 #define   PLS_FALSE  0UL
25 #define   PLS_TRUE   1UL
26 
27 /************************************************************************/
28 /* the following definitions are only relevant for RSA code on SW */
29 /************************************************************************/
30 /* Define the maximal allowed width of the exponentiation sliding window
31 in range 2...6. This define is actual for projects on soft platform.
32 To minimize code size use the minimum value. To optimize performance
33 choose the maximum value */
34 /* Define the size of the exponentiation temp buffer, used in LLF_PKI and NON DEPENDED on
35 width of the sliding window. The size defined in units equaled to maximal RSA modulus size */
36 #define PKI_CONV_CRT_CONST_TEMP_BUFF_SIZE_IN_MODULUS_UNITS  16
37 
38 /**************  Calculation of buffers sizes in words *******************************/
39 
40 /* Size of buffers for sliding window exponents */
41 #if (PKI_EXP_SLIDING_WINDOW_MAX_VALUE == 6)
42 #define PKI_EXP_WINDOW_TEMP_BUFFER_SIZE_IN_MODULUS_UNITS  34
43 #else
44 #define PKI_EXP_WINDOW_TEMP_BUFFER_SIZE_IN_MODULUS_UNITS  (3 + (1 << (PKI_EXP_SLIDING_WINDOW_MAX_VALUE-1)))
45 #endif
46 
47 
48 
49 
50 /* Define the size of the temp buffer, used in LLF_PKI_CONVERT_TO_CRT and DEPENDED on
51    width of the sliding window in words */
52 #if (PKI_CONV_CRT_CONST_TEMP_BUFF_SIZE_IN_MODULUS_UNITS > PKI_EXP_WINDOW_TEMP_BUFFER_SIZE_IN_MODULUS_UNITS )
53 #define PKI_CONV_CRT_TEMP_BUFFER_SIZE_IN_WORDS  \
54     (PKI_CONV_CRT_CONST_TEMP_BUFF_SIZE_IN_MODULUS_UNITS * SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS + 2 )
55 #else
56 #define PKI_CONV_CRT_TEMP_BUFFER_SIZE_IN_WORDS  \
57     (PKI_EXP_WINDOW_TEMP_BUFFER_SIZE_IN_MODULUS_UNITS * SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS + 2 )
58 #endif
59 
60 #define SW_CC_RSA_VALID_KEY_SIZE_MULTIPLE_VALUE_IN_BITS     256
61 
62 /* maximal allowed key size in words */
63 #define SW_CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES    (SW_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS / 8)
64 
65 
66 
67 
68 /************************ Public and private key database Structs ******************************/
69 
70 /* .................. The public key definitions ...................... */
71 /* --------------------------------------------------------------------- */
72 
73 
74 
75 /* The public key data structure */
76 typedef struct {
77     /* The RSA modulus buffer and its size in bits */
78     uint32_t n[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
79     uint32_t nSizeInBits;
80 
81     /* The RSA public exponent buffer and its size in bits */
82     uint32_t e[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
83     uint32_t eSizeInBits;
84     /* # added for compatibility with size of CC CCRsaPubKey_t type */
85     uint32_t ccRSAIntBuff[CC_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS];
86 
87 }SwRsaPubKey_t;
88 
89 /* The user structure prototype used as an input to the CC_RsaPrimEncrypt */
90 typedef struct CCSwRsaUserPubKey_t {
91 
92         uint32_t valid_tag;
93         uint32_t  PublicKeyDbBuff[sizeof(SwRsaPubKey_t)/sizeof(uint32_t)+1];
94 }CCSwRsaUserPubKey_t;
95 
96 /* .................. The private key definitions ...................... */
97 /* --------------------------------------------------------------------- */
98 
99 /* The private key on non-CRT mode data structure */
100 typedef struct {
101     /* The RSA private exponent buffer and its size in bits */
102     uint32_t d[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
103     uint32_t dSizeInBits;
104 
105     /* The RSA public exponent buffer and its size in bits */
106     uint32_t e[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
107     uint32_t eSizeInBits;
108 
109 }CCSwRsaPrivNonCRTKey_t;
110 
111 /* The private key on CRT mode data structure */
112 #ifndef CC_NO_RSA_SMALL_CRT_BUFFERS_SUPPORT
113 /* use small CRT buffers */
114 typedef struct {
115     /* The first factor buffer and size in bits */
116     uint32_t P[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS/2];
117     uint32_t PSizeInBits;
118 
119     /* The second factor buffer and its size in bits */
120     uint32_t Q[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS/2];
121     uint32_t QSizeInBits;
122 
123     /* The first CRT exponent buffer and its size in bits */
124     uint32_t dP[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS/2];
125     uint32_t dPSizeInBits;
126 
127     /* The second CRT exponent buffer and its size in bits */
128     uint32_t dQ[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS/2];
129     uint32_t dQSizeInBits;
130 
131     /* The first CRT coefficient buffer and its size in bits */
132     uint32_t qInv[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS/2];
133     uint32_t qInvSizeInBits;
134 
135 }CCSwRsaPrivCrtKey_t;
136 
137 
138 #else /* use large CRT buffers */
139 typedef struct {
140     /* The first factor buffer and size in bits */
141     uint32_t P[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
142     uint32_t PSizeInBits;
143 
144     /* The second factor buffer and its size in bits */
145     uint32_t Q[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
146     uint32_t QSizeInBits;
147 
148     /* The first CRT exponent buffer and its size in bits */
149     uint32_t dP[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
150     uint32_t dPSizeInBits;
151 
152     /* The second CRT exponent buffer and its size in bits */
153     uint32_t dQ[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
154     uint32_t dQSizeInBits;
155 
156     /* The first CRT coefficient buffer and its size in bits */
157     uint32_t qInv[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
158     uint32_t qInvSizeInBits;
159 
160 }CCSwRsaPrivCrtKey_t;
161 
162 #endif
163 
164 /* The private key data structure: */
165 typedef struct {
166     /* The RSA modulus buffer and its size in bits */
167     uint32_t n[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
168     uint32_t nSizeInBits;
169 
170     /* The decryption operation mode */
171     CCRsaDecryptionMode_t OperationMode;
172 
173     /* the source flag: 1 - External;  2 - Internal generation */
174     CCRsaKeySource_t KeySource;
175 
176     /* The union between the CRT and non-CRT data structures */
177     union {
178         CCSwRsaPrivNonCRTKey_t NonCrt;
179         CCSwRsaPrivCrtKey_t    Crt;
180     }PriveKeyDb;
181 
182        /* # added for compatibility with size of CC CCRsaPrivKey_t type */
183        uint32_t ccRSAPrivKeyIntBuff[CC_PKA_PRIV_KEY_BUFF_SIZE_IN_WORDS];
184 
185 }SwRsaPrivKey_t;
186 
187 /* Define the size of SwRsaPrivKey_t structure for using in temp buffers allocation */
188 
189 /* The users Key structure prototype, used as an input to the
190 CC_RsaPrimDecrypt*/
191 typedef struct CCSwRsaUserPrivKey_t {
192 
193     uint32_t valid_tag;
194     uint32_t  PrivateKeyDbBuff[sizeof(SwRsaPrivKey_t)/sizeof(uint32_t)+1];
195 }CCSwRsaUserPrivKey_t;
196 
197 /* the RSA data type */
198 typedef struct CCSwRsaPrimeData_t {
199     /* The aligned input and output data buffers */
200     uint32_t DataIn[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
201     uint32_t DataOut[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
202 
203     /* #include specific fields that are used by the low level */
204     struct {
205         union {
206             struct { /* Temporary buffers used for the exponent calculation */
207                 uint32_t Tempbuff1[PKI_EXP_TEMP_BUFFER_SIZE_IN_WORDS];
208                 uint32_t Tempbuff2[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS * 2];
209                 /* Temporary buffer for self-test support */
210                 uint32_t TempBuffer[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS];
211             }NonCrt;
212 
213             struct { /* Temporary buffers used for the exponent calculation */
214                 uint32_t Tempbuff1[PKI_EXP_TEMP_BUFFER_SIZE_IN_WORDS];
215                 uint32_t Tempbuff2[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS * 2];
216             }Crt;
217         }Data;
218     }LLF;
219 
220 }CCSwRsaPrimeData_t;
221 
222 /* the KG data type */
223 typedef union CCSwRsaKgData_t {
224     struct {
225         /* The aligned input and output data buffers */
226         uint32_t p[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS / 2];
227         uint32_t q[SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS / 2];
228         /* Temporary buffers used for the exponent calculation */
229         uint32_t TempbuffExp[PKI_KEY_GEN_TEMP_BUFF_SIZE_WORDS];
230 
231     }KGData;
232 
233     CCSwRsaPrimeData_t PrimData;
234 
235 }CCSwRsaKgData_t;
236 
237 /* .......................... Temp buff definition  ........................ */
238 /* ------------------------------------------------------------------------- */
239 
240 /* the RSA Convert Key to CRT data type */
241 typedef struct CCSwRsaConvertKeyToCrtBuffers_t {
242     /* #include specific fields that are used by the low level */
243     struct {
244         uint32_t TempBuffers[ 7*SW_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS + PKI_EXP_TEMP_BUFFER_SIZE_IN_WORDS ];
245 
246     } LLF;
247 }CCSwRsaConvertKeyToCrtBuffers_t;
248 
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 #endif
254