1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /************* Include Files ****************/
8 
9 #include "cc_pal_mem.h"
10 #include "cc_common.h"
11 #include "cc_common_math.h"
12 #include "cc_rnd_common.h"
13 #include "cc_rnd_local.h"
14 #include "cc_rsa_error.h"
15 #include "cc_rsa_local.h"
16 #include "sw_llf_pki_rsa.h"
17 #include "ccsw_cc_rsa_types.h"
18 
19 
20 /************************ Defines ******************************/
21 
22 /************************ Enums ************************************/
23 
24 /************************ Typedefs *********************************/
25 
26 /************************ Global Data ******************************/
27 
28 /*
29  For debugging the RSA_KG module define the following flags in project properties
30  and perform the following:
31    1. Compile project in DEBUG=1 mode.
32    2. Define LLF_PKI_PKA_DEBUG.
33    3. For findingthe bad random factors (P,Q,P1pR,P2pR,P1qR,P2qR):
34       define RSA_KG_FIND_BAD_RND flag, perform test and save (from memory)
35       the found bad vectors.
36    4. For repeat the testing of found bad vectors, write they as HW
37       initialization of the following buffers:
38       P=>RSA_KG_debugPvect, Q=>RSA_KG_debugQvect - in the cc_rsa_kg.c
39       file, and P1pR=>rBuff1, P2pR=>rBuff1, P1qR=>rBuff3, P2qR=>rBuff4 in the
40       LLF_PKI_GenKeyX931FindPrime.c file. Define the flag RSA_KG_NO_RND instead
41       previously defined RSA_KG_FIND_BAD_RND flag and perform the test.
42    5. For ordinary ATP or other tests (without debug) undef all the named flags.
43 */
44 
45 #if ((defined RSA_KG_FIND_BAD_RND || defined RSA_KG_NO_RND) && defined DEBUG)
46 uint8_t   RSA_KG_debugPvect[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES] =
47 { 0x78,0x71,0xDF,0xC5,0x36,0x98,0x12,0x21,0xCA,0xAC,0x48,0x22,0x01,0x94,0xF7,0x1A,
48     0x1C,0xBF,0x82,0xE9,0x8A,0xE4,0x2C,0x84,0x43,0x46,0xCF,0x6D,0x60,0xFB,0x5B,0xD3};
49 uint8_t   RSA_KG_debugQvect[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES] =
50 { 0x46,0x13,0x9F,0xBA,0xBC,0x8E,0x21,0x13,0x35,0x8C,0x2C,0x2D,0xA8,0xD6,0x59,0x78,
51     0x8A,0x14,0x17,0x5F,0xA5,0xEC,0x22,0xD5,0x87,0xF9,0x99,0x45,0x1B,0x38,0xA3,0xF0};
52 #endif
53 
54 
55 /************* Private function prototype **************************/
56 
57 
58 /************************ Public Functions ******************************/
59 
60 
61 /***********************************************************************************************/
62 #ifndef _INTERNAL_CC_NO_RSA_KG_SUPPORT
63 /**
64    @brief CC_RsaKgKeyPairGenerate generates a Pair of public and private keys on non CRT mode.
65 
66    @param [in/out] rndContext_ptr  - Pointer to the RND context buffer.
67    @param [in] PubExp_ptr - The pointer to the public exponent (public key)
68    @param [in] PubExpSizeInBytes - The public exponent size in bytes.
69    @param [in] KeySize  - The size of the key, in bits. Supported sizes are:
70                 - for PKI without PKA HW: all 256 bit multiples between 512 - 2048;
71                 - for PKI with PKA: HW all 32 bit multiples between 512 - 2112;
72    @param [out] UserPrivKey_ptr - A pointer to the private key structure.
73                This structure is used as input to the CC_RsaPrimDecrypt API.
74    @param [out] UserPubKey_ptr - A pointer to the public key structure.
75                This structure is used as input to the CC_RsaPrimEncrypt API.
76    @param [in] KeyGenData_ptr - a pointer to a structure required for the KeyGen
77       operation.
78  * @param rndCtx_ptr - The pointer to structure, containing context ID and void
79  *              pointer to RND State structure, which should be converted to
80  *              actual type inside of the function according to used platform
81  *                  (External or CC). also containig the generate random vector pointer
82 
83    @return CCError_t - CC_OK,
84              CC_RSA_INVALID_EXPONENT_POINTER_ERROR,
85              CC_RSA_INVALID_PRIV_KEY_STRUCT_POINTER_ERROR,
86              CC_RSA_INVALID_PUB_KEY_STRUCT_POINTER_ERROR,
87              CC_RSA_KEY_GEN_DATA_STRUCT_POINTER_INVALID,
88              CC_RSA_INVALID_MODULUS_SIZE,
89              CC_RSA_INVALID_EXPONENT_SIZE
90 */
CC_SwRsaKgGenerateKeyPair(CCRndContext_t * rndContext_ptr,uint8_t * PubExp_ptr,uint16_t PubExpSizeInBytes,uint32_t KeySize,CCSwRsaUserPrivKey_t * UserPrivKey_ptr,CCSwRsaUserPubKey_t * UserPubKey_ptr,CCSwRsaKgData_t * KeyGenData_ptr)91 CEXPORT_C CCError_t CC_SwRsaKgGenerateKeyPair(CCRndContext_t *rndContext_ptr,
92                             uint8_t             *PubExp_ptr,
93                             uint16_t             PubExpSizeInBytes,
94                             uint32_t             KeySize,
95                             CCSwRsaUserPrivKey_t *UserPrivKey_ptr,
96                             CCSwRsaUserPubKey_t  *UserPubKey_ptr,
97                             CCSwRsaKgData_t      *KeyGenData_ptr)
98 {
99     /* LOCAL INITIALIZATIONS AND DECLERATIONS */
100 
101     /* the error identifier */
102     CCError_t Error;
103 
104     /* the pointers to the key structures */
105     SwRsaPubKey_t  *PubKey_ptr;
106     SwRsaPrivKey_t *PrivKey_ptr;
107 
108     /* a temp definition to solve a problem on release mode on VC++ */
109     volatile uint32_t dummy = PubExpSizeInBytes;
110 
111     uint32_t  KeySizeInWords = KeySize/32;
112 
113 
114     /* FUNCTION LOGIC */
115     /* ................ initializaions and local declarations ............ */
116     /* ------------------------------------------------------------------- */
117 
118     /* to avoid compilers warnings */
119     dummy = dummy;
120 
121     /* initialize the error identifier to O.K */
122     Error = CC_OK;
123 
124 
125     /* ................. checking the validity of the pointer arguments ....... */
126     /* ------------------------------------------------------------------------ */
127 
128     /* ...... checking the key database handle pointer .................... */
129     if (PubExp_ptr == NULL)
130         return CC_RSA_INVALID_EXPONENT_POINTER_ERROR;
131 
132     /* ...... checking the validity of the exponent pointer ............... */
133     if (UserPrivKey_ptr == NULL)
134         return CC_RSA_INVALID_PRIV_KEY_STRUCT_POINTER_ERROR;
135 
136     /* ...... checking the validity of the modulus pointer .............. */
137     if (UserPubKey_ptr == NULL)
138         return CC_RSA_INVALID_PUB_KEY_STRUCT_POINTER_ERROR;
139 
140     /* ...... checking the validity of the keygen data .................. */
141     if (KeyGenData_ptr == NULL)
142         return CC_RSA_KEY_GEN_DATA_STRUCT_POINTER_INVALID;
143 
144     /* ...... checking the exponent size .................. */
145     if (PubExpSizeInBytes > CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES)
146         return CC_RSA_INVALID_EXPONENT_SIZE;
147 
148     /* ...... checking the required key size ............................ */
149     if (( KeySize < CC_RSA_MIN_VALID_KEY_SIZE_VALUE_IN_BITS ) ||
150         ( KeySize > CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS ) ||
151         ( KeySize % CC_RSA_VALID_KEY_SIZE_MULTIPLE_VALUE_IN_BITS )) {
152         return CC_RSA_INVALID_MODULUS_SIZE;
153     }
154 
155     /* set the public and private key structure pointers */
156     PubKey_ptr  = ( SwRsaPubKey_t *)UserPubKey_ptr->PublicKeyDbBuff;
157     PrivKey_ptr = ( SwRsaPrivKey_t *)UserPrivKey_ptr->PrivateKeyDbBuff;
158 
159     CC_PalMemSet( UserPrivKey_ptr, 0, sizeof(CCSwRsaUserPrivKey_t) );
160     CC_PalMemSet( UserPubKey_ptr, 0, sizeof(CCSwRsaUserPubKey_t) );
161     CC_PalMemSet( KeyGenData_ptr , 0, sizeof(CCSwRsaKgData_t) );
162 
163     /* ................ loading the public exponent to the structure .......... */
164     /* ------------------------------------------------------------------------- */
165     /* loading the buffers to start from LS word to MS word */
166     CC_CommonReverseMemcpy( (uint8_t*)PubKey_ptr->e , PubExp_ptr , PubExpSizeInBytes );
167     /* .......... initializing the effective counters size in bits .......... */
168     PubKey_ptr->eSizeInBits = CC_CommonGetBytesCounterEffectiveSizeInBits( (uint8_t*)PubKey_ptr->e,PubExpSizeInBytes );
169 
170     /* if the size in bits is 0 - return error */
171     if (PubKey_ptr->eSizeInBits == 0) {
172         Error = CC_RSA_INVALID_EXPONENT_SIZE;
173         goto End;
174     }
175 
176     /* verifying the exponent has legal value (currently only 0x3,0x11 and 0x10001) */
177     if (PubKey_ptr->e[0] != 0x3  &&
178         PubKey_ptr->e[0] != 0x11 &&
179         PubKey_ptr->e[0] != 0x010001) {
180         Error = CC_RSA_INVALID_EXPONENT_VAL;
181         goto End;
182     }
183     /* .......... initialize the public key on the private structure ............... */
184     CC_PalMemCopy( PrivKey_ptr->PriveKeyDb.NonCrt.e , PubKey_ptr->e , 4*((PubExpSizeInBytes+3)/4) );
185     PrivKey_ptr->PriveKeyDb.NonCrt.eSizeInBits = PubKey_ptr->eSizeInBits;
186 
187     /* .......... initializing the key size in bits ......................... */
188 
189     /* this initialization is required for the low level function (LLF) - indicates the required
190        size of the key to be found */
191     PubKey_ptr->nSizeInBits  = KeySize;
192     PrivKey_ptr->nSizeInBits = KeySize;
193 
194     /* .......... set the private mode to non CRT .............................. */
195     /* ------------------------------------------------------------------------- */
196 
197     /* set the mode to non CRT */
198     PrivKey_ptr->OperationMode = CC_RSA_NoCrt;
199 
200     /* set the key source as internal */
201     PrivKey_ptr->KeySource = CC_RSA_InternalKey;
202 
203     /* ................ executing the key generation ........................... */
204     /* ------------------------------------------------------------------------- */
205     /* generate the random */
206 
207 #if ( (!defined RSA_KG_FIND_BAD_RND && !defined RSA_KG_NO_RND) || defined RSA_KG_FIND_BAD_RND || !defined DEBUG)
208     Error = CC_RsaGenerateVectorInRangeX931(rndContext_ptr, KeySizeInWords / 2, KeyGenData_ptr->KGData.p);
209 
210     if (Error != CC_OK)
211         goto End;
212 
213     Error = CC_RsaGenerateVectorInRangeX931(rndContext_ptr, KeySizeInWords / 2, KeyGenData_ptr->KGData.q);
214 
215     if (Error != CC_OK)
216         goto End;
217 #endif
218 
219 #if (defined RSA_KG_FIND_BAD_RND && defined DEBUG)
220     CC_PalMemCopy( RSA_KG_debugPvect, (uint8_t*)KeyGenData_ptr->KGData.p, KeySizeInBytes / 2 );
221     CC_PalMemCopy( RSA_KG_debugQvect, (uint8_t*)KeyGenData_ptr->KGData.q, KeySizeInBytes / 2);
222 #endif
223 
224 #if (defined RSA_KG_NO_RND  && defined DEBUG)
225     CC_PalMemCopy( (uint8_t*)KeyGenData_ptr->KGData.p, RSA_KG_debugPvect, KeySizeInBytes / 2 );
226     CC_PalMemCopy( (uint8_t*)KeyGenData_ptr->KGData.q, RSA_KG_debugQvect, KeySizeInBytes / 2 );
227 #endif
228 
229 #if ((defined RSA_KG_FIND_BAD_RND || defined RSA_KG_NO_RND) && defined DEBUG)
230   #ifdef BIG__ENDIAN
231     /* for big endiannes machine reverse bytes order in words according to Big Endian  */
232     CC_COMMON_INVERSE_UINT32_IN_ARRAY( KeyGenData_ptr->KGData.p, KeySizeInWords / 2);
233     CC_COMMON_INVERSE_UINT32_IN_ARRAY( KeyGenData_ptr->KGData.q, KeySizeInWords / 2 );
234   #endif
235 #endif
236     /* clean the n-buffer */
237     CC_PalMemSetZero( PrivKey_ptr->n, 4*CC_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS );
238 
239     /* ................ execute the low level keygen ........................... */
240     Error = SW_LLF_PKI_RSA_GenerateKeyPair( PubKey_ptr,
241                         PrivKey_ptr,
242                         KeyGenData_ptr);
243 
244     /* on failure exit the function */
245     if (Error != CC_OK)
246         goto End;
247 
248     /* ................ initialize the low level key structures ................ */
249     /* ------------------------------------------------------------------------- */
250     Error = SW_LLF_PKI_RSA_InitPubKeyDb(PubKey_ptr);
251 
252     if (Error != CC_OK)
253         goto End;
254 
255     Error = SW_LLF_PKI_RSA_InitPrivKeyDb(PrivKey_ptr);
256 
257     if (Error != CC_OK)
258         goto End;
259 
260     /* ................ set the key valid tags ................................. */
261     /* ------------------------------------------------------------------------- */
262     UserPrivKey_ptr->valid_tag = CC_RSA_PRIV_KEY_VALIDATION_TAG;
263     UserPubKey_ptr->valid_tag  = CC_RSA_PUB_KEY_VALIDATION_TAG;
264 
265 
266 
267 End:
268         /* clear the KG data structure and keys */
269     CC_PalMemSetZero(KeyGenData_ptr, sizeof(CCSwRsaKgData_t));
270         /*............  end on error  .............*/
271         if(Error) {
272                 CC_PalMemSetZero(PubKey_ptr, sizeof(CCSwRsaUserPubKey_t));
273                 CC_PalMemSetZero(PrivKey_ptr, sizeof(CCSwRsaUserPrivKey_t));
274         }
275 
276         return Error;
277 
278 }/* END OF CC_SwRsaKgGenerateKeyPair */
279 
280 
281 /***********************************************************************************************/
282 /**
283    @brief CC_SwRsaKgGenerateKeyPairCRT generates a Pair of public and private keys on CRT mode.
284 
285    @param [in/out] rndContext_ptr  - Pointer to the RND context buffer.
286    @param [in] PubExp_ptr - The pointer to the public exponent (public key)
287    @param [in] PubExpSizeInBytes - The public exponent size in bits.
288    @param [in] KeySize  - The size of the key, in bits. Supported sizes are:
289                 - for PKI without PKA HW: all 256 bit multiples between 512 - 2048;
290                 - for PKI with PKA: HW all 32 bit multiples between 512 - 2112;
291    @param [out] UserPrivKey_ptr - A pointer to the private key structure.
292                This structure is used as input to the CC_RsaPrimDecrypt API.
293    @param [out] UserPubKey_ptr - A pointer to the public key structure.
294                This structure is used as input to the CC_RsaPrimEncryped API.
295    @param [in] KeyGenData_ptr - a pointer to a structure required for the KeyGen operation.
296  * @param [in/out] RndCtx_ptr - The pointer to structure, containing context ID and void
297  *              pointer to RND State structure, which should be converted to
298  *              actual type inside of the function according to used platform
299  *                  (External or CC).
300 
301    @return CCError_t - CC_OK,
302              CC_RSA_INVALID_EXPONENT_POINTER_ERROR,
303              CC_RSA_INVALID_PRIV_KEY_STRUCT_POINTER_ERROR,
304              CC_RSA_INVALID_PUB_KEY_STRUCT_POINTER_ERROR,
305              CC_RSA_KEY_GEN_DATA_STRUCT_POINTER_INVALID,
306              CC_RSA_INVALID_MODULUS_SIZE,
307              CC_RSA_INVALID_EXPONENT_SIZE
308 */
309 
CC_SwRsaKgGenerateKeyPairCRT(CCRndContext_t * rndContext_ptr,uint8_t * PubExp_ptr,uint16_t PubExpSizeInBytes,uint32_t KeySize,CCSwRsaUserPrivKey_t * UserPrivKey_ptr,CCSwRsaUserPubKey_t * UserPubKey_ptr,CCSwRsaKgData_t * KeyGenData_ptr)310 CEXPORT_C CCError_t CC_SwRsaKgGenerateKeyPairCRT(CCRndContext_t *rndContext_ptr,
311                                uint8_t  *PubExp_ptr,
312                                uint16_t  PubExpSizeInBytes,
313                                uint32_t  KeySize,
314                                CCSwRsaUserPrivKey_t *UserPrivKey_ptr,
315                                CCSwRsaUserPubKey_t  *UserPubKey_ptr,
316                                CCSwRsaKgData_t      *KeyGenData_ptr)
317 {
318     /* LOCAL INITIALIZATIONS AND DECLERATIONS */
319 
320     /* the error identifier */
321     CCError_t Error = CC_OK;
322 
323     /* the pointers to the key structures */
324     SwRsaPubKey_t  *PubKey_ptr;
325     SwRsaPrivKey_t *PrivKey_ptr;
326 
327     /* FUNCTION LOGIC */
328     uint32_t  KeySizeInBytes = KeySize/8;
329     uint32_t  KeySizeInWords = KeySize/32;
330 
331     /* ................ initializations and local declarations ............ */
332     /* ------------------------------------------------------------------- */
333 
334 
335 
336     /* ................. checking the validity of the pointer arguments ....... */
337     /* ------------------------------------------------------------------------ */
338     /* ...... checking the key database handle pointer .................... */
339     if (PubExp_ptr == NULL)
340         return CC_RSA_INVALID_EXPONENT_POINTER_ERROR;
341 
342     /* ...... checking the validity of the exponent pointer ............... */
343     if (UserPrivKey_ptr == NULL)
344         return CC_RSA_INVALID_PRIV_KEY_STRUCT_POINTER_ERROR;
345 
346     /* ...... checking the validity of the modulus pointer .............. */
347     if (UserPubKey_ptr == NULL)
348         return CC_RSA_INVALID_PUB_KEY_STRUCT_POINTER_ERROR;
349 
350     /* ...... checking the validity of the keygen data .................. */
351     if (KeyGenData_ptr == NULL)
352         return CC_RSA_KEY_GEN_DATA_STRUCT_POINTER_INVALID;
353 
354     /* ...... checking the required key size ............................ */
355     if (( KeySize < CC_RSA_MIN_VALID_KEY_SIZE_VALUE_IN_BITS ) ||
356         ( KeySize > CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS ) ||
357         ( KeySize % CC_RSA_VALID_KEY_SIZE_MULTIPLE_VALUE_IN_BITS ))
358         return CC_RSA_INVALID_MODULUS_SIZE;
359 
360     if (PubExpSizeInBytes > CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES)
361         return CC_RSA_INVALID_EXPONENT_SIZE;
362 
363 
364     /* set the public and private key structure pointers */
365     PubKey_ptr  = ( SwRsaPubKey_t *)UserPubKey_ptr->PublicKeyDbBuff;
366     PrivKey_ptr = ( SwRsaPrivKey_t *)UserPrivKey_ptr->PrivateKeyDbBuff;
367 
368 
369     /* ................ clear all input structures ............................. */
370     /* ------------------------------------------------------------------------- */
371     /* RL Clean buffers  */
372     CC_PalMemSet( UserPrivKey_ptr , 0 , sizeof(CCSwRsaUserPrivKey_t) );
373     CC_PalMemSet( UserPubKey_ptr  , 0 , sizeof(CCSwRsaUserPubKey_t) );
374     CC_PalMemSet( KeyGenData_ptr  , 0 , sizeof(CCSwRsaKgData_t) );
375 
376     /* ................ loading the public exponent to the structure .......... */
377     /* ------------------------------------------------------------------------- */
378 
379     /* loading the buffers to start from LS word to MS word */
380     CC_CommonReverseMemcpy( (uint8_t*)PubKey_ptr->e , PubExp_ptr , PubExpSizeInBytes );
381 
382     /* .......... initializing the effective counters size in bits .......... */
383     PubKey_ptr->eSizeInBits = CC_CommonGetBytesCounterEffectiveSizeInBits( (uint8_t*)PubKey_ptr->e, PubExpSizeInBytes );
384 
385     /* if the size in bits is 0 - return error */
386     if (PubKey_ptr->eSizeInBits == 0) {
387         Error = CC_RSA_INVALID_EXPONENT_SIZE;
388         goto End;
389     }
390 
391     /* verifing the exponent has legal value (currently only 0x3,0x11 and 0x10001) */
392         if(PubKey_ptr->e[0] != 0x3   &&
393            PubKey_ptr->e[0] != 0x11  &&
394            PubKey_ptr->e[0] != 0x010001){
395             Error = CC_RSA_INVALID_EXPONENT_VAL;
396             goto End;
397     }
398 
399 
400     /* .......... initializing the key size in bits ......................... */
401 
402     /* this initialization is required for the low level function (LLF) - indicates the required
403        size of the key to be found */
404     PubKey_ptr->nSizeInBits  = KeySize;
405     PrivKey_ptr->nSizeInBits = KeySize;
406 
407     /* .......... set the private mode to CRT .................................. */
408     /* ------------------------------------------------------------------------- */
409 
410     /* set the mode to CRT */
411     PrivKey_ptr->OperationMode = CC_RSA_Crt;
412 
413     /* set the key source as internal */
414     PrivKey_ptr->KeySource = CC_RSA_InternalKey;
415 
416     /* ................ executing the key generation ........................... */
417     /* ------------------------------------------------------------------------- */
418 
419     /* ................ generate the prime1 and prime2 random numbers .......... */
420 
421     /* generate the random */
422     Error = CC_RsaGenerateVectorInRangeX931(rndContext_ptr, KeySizeInWords / 2, KeyGenData_ptr->KGData.p);
423 
424     if (Error != CC_OK)
425         goto End;
426 
427     Error = CC_RsaGenerateVectorInRangeX931(rndContext_ptr, KeySizeInWords / 2, KeyGenData_ptr->KGData.q);
428 
429     if (Error != CC_OK)
430         goto End;
431 
432   #ifdef BIG__ENDIAN
433     /* for big endianness machine reverse bytes order according to Big Endian words */
434     CC_COMMON_INVERSE_UINT32_IN_ARRAY( KeyGenData_ptr->KGData.p, KeySizeInWords /2 );
435     CC_COMMON_INVERSE_UINT32_IN_ARRAY( KeyGenData_ptr->KGData.q, KeySizeInWords /2 );
436   #endif
437 
438     /* clean the n-buffer */
439     CC_PalMemSetZero( PrivKey_ptr->n, 4*CC_RSA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS );
440 
441     /* ................ execute the low level key gen ........................... */
442     Error = SW_LLF_PKI_RSA_GenerateKeyPair( PubKey_ptr,
443                         PrivKey_ptr,
444                         KeyGenData_ptr );
445 
446     /* on failure exit the function */
447     if (Error != CC_OK)
448         goto End;
449 
450     /* ................ set the vector sizes ................................... */
451     /* ------------------------------------------------------------------------- */
452 
453     PrivKey_ptr->PriveKeyDb.Crt.PSizeInBits =
454     CC_CommonGetBytesCounterEffectiveSizeInBits((uint8_t*)PrivKey_ptr->PriveKeyDb.Crt.P, KeySizeInBytes / 2);
455 
456     PrivKey_ptr->PriveKeyDb.Crt.QSizeInBits =
457     CC_CommonGetBytesCounterEffectiveSizeInBits((uint8_t*)PrivKey_ptr->PriveKeyDb.Crt.Q, KeySizeInBytes / 2);
458 
459     PrivKey_ptr->PriveKeyDb.Crt.dPSizeInBits =
460     CC_CommonGetBytesCounterEffectiveSizeInBits((uint8_t*)PrivKey_ptr->PriveKeyDb.Crt.dP, KeySizeInBytes / 2);
461 
462     PrivKey_ptr->PriveKeyDb.Crt.dQSizeInBits =
463     CC_CommonGetBytesCounterEffectiveSizeInBits((uint8_t*)PrivKey_ptr->PriveKeyDb.Crt.dQ, KeySizeInBytes / 2);
464 
465     PrivKey_ptr->PriveKeyDb.Crt.qInvSizeInBits =
466     CC_CommonGetBytesCounterEffectiveSizeInBits((uint8_t*)PrivKey_ptr->PriveKeyDb.Crt.qInv, KeySizeInBytes / 2);
467 
468     /* ................ initialize the low level key structures ................ */
469     /* ------------------------------------------------------------------------- */
470 
471     Error = SW_LLF_PKI_RSA_InitPubKeyDb( PubKey_ptr );
472 
473     if (Error != CC_OK)
474         goto End;
475 
476     Error = SW_LLF_PKI_RSA_InitPrivKeyDb( PrivKey_ptr );
477 
478     if (Error != CC_OK)
479         goto End;
480 
481     /* ................ set the key valid tags ................................. */
482     /* ------------------------------------------------------------------------- */
483 
484     UserPrivKey_ptr->valid_tag = CC_RSA_PRIV_KEY_VALIDATION_TAG;
485     UserPubKey_ptr->valid_tag  = CC_RSA_PUB_KEY_VALIDATION_TAG;
486 
487     End:
488 
489     /* clear the KG data structure */
490     CC_PalMemSetZero ( KeyGenData_ptr, sizeof(CCSwRsaKgData_t));
491 
492         /* on error clear the keys */
493         if (Error != CC_OK) {
494                 CC_PalMemSetZero(UserPrivKey_ptr, sizeof(CCRsaUserPrivKey_t));
495                 CC_PalMemSetZero(UserPubKey_ptr, sizeof(CCRsaUserPubKey_t));
496         }
497 
498     return Error;
499 
500 }/* END OF CC_RsaKgKeyPairCrtGenerate */
501 
502 #endif /*_INTERNAL_CC_NO_RSA_KG_SUPPORT*/
503