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_RSA_KEYPAIR_H
8 #define _COMMON_RSA_KEYPAIR_H
9 
10 #include <stdint.h>
11 
12 #include <openssl/objects.h>
13 #include <openssl/pem.h>
14 #include "cc_pka_hw_plat_defs.h"
15 
16 #ifdef WIN32
17 #define SBUEXPORT_C __declspec(dllexport)
18 #else
19 #define SBUEXPORT_C
20 #endif
21 
22 typedef enum NP_RESULT_TYPE {
23     NP_BIN = 0,
24     NP_HEX = 1
25 }NP_RESULT_TYPE_t;
26 
27 
28 /* Global defines */
29 #define RSA_PRIVATE_KEY_SIZE     SB_CERT_RSA_KEY_SIZE_IN_BITS
30 #define NP_SIZE_IN_BYTES         20
31 #define NEW_PKA_WORD_SIZE_BITS   CC_PKA_WORD_SIZE_IN_BITS
32 #define NEW_PAK_ADDITIONAL_BITS  8
33 #define SNP                      SB_CERT_RSA_KEY_SIZE_IN_BITS + NEW_PKA_WORD_SIZE_BITS + NEW_PAK_ADDITIONAL_BITS -1
34 
35 
36 /**
37  * @brief The CC_CommonGetKeyPair reads RSA private key from the file, along with retrieving the private key,
38  *    it also retrieves the public key.
39  *
40  * The function
41  * 1. Build RSA public key structure
42  * @param[out] pRsaPrivKey - the private key
43  * @param[in] PemEncryptedFileName_ptr - private key file
44  * @param[in] Key_ptr - passphrase string
45  *
46  */
47 /*********************************************************/
48 int32_t CC_CommonGetKeyPair (RSA **pRsaKeyPair, int8_t *PemEncryptedFileName_ptr, int8_t *Key_ptr);
49 
50 /**
51  * @brief The CC_CommonGetPubKey reads RSA public key from the file.
52  *
53  * The function
54  * 1. Build RSA public key structure
55  * @param[out] pRsaPrivKey - the rsa key
56  * @param[in] PemEncryptedFileName_ptr - public key file name
57  *
58  */
59 /*********************************************************/
60 int32_t CC_CommonGetPubKey (RSA **pRsaKeyPair, int8_t *PemEncryptedFileName_ptr);
61 
62 /**
63  * @brief The function calculates Np when given N as hex data.
64  *
65  * @param[in] n - modulus as hex data
66  * @param[out] NP_ptr - the Np
67  *
68  */
69 /*********************************************************/
70 SBUEXPORT_C int32_t CC_CommonRsaCalculateNp(const int8_t* N_ptr,
71                       int8_t *NP_ptr);
72 
73 
74 /**
75  * @brief The function calculates Np when given N as BIGNUM.
76  *
77  * @param[in] n - modulus as BIGNUM ptr
78  * @param[out] NP_ptr - the Np
79  *
80  */
81 /*********************************************************/
82 SBUEXPORT_C int32_t CC_CommonRSACalculateNpInt(BIGNUM *n,
83                          uint8_t *NP_ptr,
84                          NP_RESULT_TYPE_t resultType);
85 
86 #endif
87