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 LLF_RSA_H 9 #define LLF_RSA_H 10 11 12 #include "cc_rsa_types.h" 13 #include "cc_rnd_common.h" 14 #ifdef FIPS_CERTIFICATION 15 #include "fips_tests_rsa_def.h" 16 #include "cc_common.h" 17 #endif 18 19 #ifdef __cplusplus 20 extern "C" 21 { 22 #endif 23 24 /************************ Defines ******************************/ 25 26 #define PKA_MAX_RSA_KEY_GENERATION_SIZE_BITS CC_RSA_MAX_KEY_GENERATION_SIZE_BITS 27 /* max allowed size of pprimes P, Q in RSA KG */ 28 #define PKA_RSA_KG_MAX_PQ_SIZE_BITS (PKA_MAX_RSA_KEY_GENERATION_SIZE_BITS/2) 29 /* max. total count of avaliable PKA registers in RSA KG */ 30 #define PKA_RSA_KG_MAX_COUNT_REGS \ 31 CC_MIN(PKA_MAX_COUNT_OF_PHYS_MEM_REGS, \ 32 (8*CC_SRAM_PKA_SIZE_IN_BYTES) / (PKA_RSA_KG_MAX_PQ_SIZE_BITS + CC_PKA_WORD_SIZE_IN_BITS)) 33 /* max. count of avaliable registers in RSA KG without auxiliary regs. 30,31 */ 34 #define PKA_RSA_KG_MAX_REG_ID (PKA_RSA_KG_MAX_COUNT_REGS - 2) 35 36 /* define size of auxiliary prime numbers for RSA * 37 * (see FIPS 186-4 C3 tab.C3) */ 38 #define PKA_RSA_KEY_1024_AUX_PRIME_SIZE_BITS 104 /* for P,Q size 1024 bit aux.size > 100 bits */ 39 #define PKA_RSA_KEY_2048_AUX_PRIME_SIZE_BITS 144 /* for P,Q size 2048 bit aux.size > 140 bits */ 40 #define PKA_RSA_KEY_3072_AUX_PRIME_SIZE_BITS 176 /* for P,Q size 3072 bit aux.size > 170 bits */ 41 42 #define PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS 8 /* max size of temp buffer for auxiliary prime */ 43 44 /* define count of Miller-Rabin tests for P,Q and auxiliary prime numbers * 45 * for RSA key generation (see FIPS 186-4 C3 tab.C3) */ 46 #define PKA_RSA_KEY_1024_AUX_PRIME_RM_TST_COUNT 38 47 #define PKA_RSA_KEY_1024_PQ_PRIME_RM_TST_COUNT 7 48 #define PKA_RSA_KEY_2048_AUX_PRIME_RM_TST_COUNT 32 49 #define PKA_RSA_KEY_2048_PQ_PRIME_RM_TST_COUNT 4 50 #define PKA_RSA_KEY_3072_AUX_PRIME_RM_TST_COUNT 27 51 #define PKA_RSA_KEY_3072_PQ_PRIME_RM_TST_COUNT 3 52 53 #define CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_WORDS CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS / CC_BITS_IN_32BIT_WORD 54 55 #define LLF_PKI_PKA_DEBUG 1 56 57 #ifdef FIPS_CERTIFICATION 58 rsaKgInternalDataStruct_t rsaKgOutParams; 59 #endif 60 61 62 /* RSA key generation parameters structure */ 63 typedef struct 64 { 65 uint32_t auxPrimesSizeInBits; 66 uint32_t pqPrimesMilRabTestsCount; 67 uint32_t auxPrimesMilRabTestsCount; 68 }RsaKgParams_t; 69 70 /* RSA Barrett tags for modular operations with N, P, Q */ 71 typedef struct 72 { 73 uint32_t tmpBuf[3*CC_PKA_MAXIMUM_MOD_BUFFER_SIZE_IN_WORDS]; 74 uint32_t barN[CC_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS]; /*!< Barrett tag for modulus N */ 75 uint32_t barP[CC_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS]; /*!< Barrett tag for modulus P */ 76 uint32_t barQ[CC_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS]; /*!< Barrett tag for modulus Q */ 77 }RsaKgIntBuff_t; 78 79 CCError_t RsaGenPandQ(CCRndContext_t *rndContext_ptr, 80 size_t KeySize, 81 uint32_t eSizeInBits, 82 uint32_t *pPubExp, 83 CCRsaKgData_t *KeyGenData_ptr); 84 85 CCError_t RsaPrimeTestCall(CCRndContext_t *rndContext_ptr, 86 uint32_t *P_ptr, 87 int32_t sizeWords, 88 int32_t rabinTestsCount, 89 int8_t *isPrime_ptr, 90 uint32_t *TempBuff_ptr, 91 CCRsaDhPrimeTestMode_t primeTestMode); 92 93 CCError_t RsaCalculateNandD(CCRsaPubKey_t *pCcPubKey, /*!< [in] pointer to the public key structure */ 94 CCRsaPrivKey_t *pCcPrivKey, /*!< [in] pointer to the private key structure */ 95 CCRsaKgData_t *KeyGenData_ptr, /*!< [in] pointer to a structure required for the KeyGen operation, holding P and Q */ 96 uint32_t primeSizeInBits); /*!< [in] Size of the prime factors in bits. */ 97 98 99 CCError_t RsaCalculateCrtParams(uint32_t *pPubExp, /*!< [in] Pointer to the public exponent. */ 100 uint32_t eSizeInBits, /*!< [in] Public exponent size in bits. */ 101 uint32_t nSizeInBits, /*!< [in] Size of the key modulus in bits. */ 102 uint32_t *pPrimeP, /*!< [out] First factor pointer - p. */ 103 uint32_t *pPrimeQ, /*!< [out] Second factor pointer - Q. */ 104 uint32_t *pPrivExp1dp, /*!< [out] Private exponent for first factor - dP. */ 105 uint32_t *pPrivExp2dq, /*!< [out] Private exponent for second factor - dQ. */ 106 uint32_t *pQInv); 107 108 109 //#if (defined RSA_KG_FIND_BAD_RND || defined RSA_KG_NO_RND) && defined DEBUG 110 extern uint8_t RSA_KG_debugPvect[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES/2]; 111 extern uint8_t RSA_KG_debugQvect[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BYTES/2]; 112 extern uint8_t PQindex; 113 extern uint8_t rBuff1[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS * sizeof(uint32_t) ]; 114 extern uint8_t rBuff2[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS * sizeof(uint32_t) ]; 115 extern uint8_t rBuff3[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS * sizeof(uint32_t) ]; 116 extern uint8_t rBuff4[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS * sizeof(uint32_t) ]; 117 /* temp buffers for output results of generation P1,P2 for P and Q */ 118 extern uint32_t P1pR[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 119 extern uint32_t P2pR[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 120 extern uint32_t P1qR[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 121 extern uint32_t P2qR[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 122 /* final values of P1,P2 for P and Q */ 123 extern uint32_t P1pPr[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 124 extern uint32_t P2pPr[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 125 extern uint32_t P1qPr[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 126 extern uint32_t P2qPr[PKA_RSA_AUX_PRIME_BUFF_SIZE_IN_32BIT_WORDS]; 127 extern uint32_t *P1R_ptr; 128 extern uint32_t *P2R_ptr; 129 extern uint32_t *P1Pr_ptr; 130 extern uint32_t *P2Pr_ptr; 131 132 /* temp buffers and pointer for output the P,Q after generation */ 133 extern uint32_t rBuffP[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_WORDS / 2]; 134 extern uint32_t rBuffQ[CC_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_WORDS / 2]; 135 extern uint32_t *PQ_ptr; 136 //#endif 137 138 139 #ifdef __cplusplus 140 } 141 #endif 142 143 #endif 144