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_CRYPTO_ASYM_H
8 #define _COMMON_CRYPTO_ASYM_H
9 
10 
11 #include <stdint.h>
12 
13 #define RSA_USE_PKCS_21_VERSION  0x01
14 #define RSA_USE_PKCS_15_VERSION  0x02
15 
16 #define RSA_SALT_LEN             32
17 /**
18  * @brief Verifies RSA signature.
19  *
20  * The function follows the steps:
21  * 1. Read RSA private key structure
22  * 2. Call function according to PKCS version to create RSA signature
23  *
24  * @param[in] pkcsVersion - the version used (according to global definitions of available versions)
25  * @param[in] pDataIn - the data to sign on
26  * @param[in] dataInSize - the data size
27  * @param[in] pPemEncryptedFileName - the private key file
28  * @param[in] pKeyPwd - the passphrase string
29  * @param[out] pSignature - the RSA signature
30  *
31  */
32 /*********************************************************/
33 int32_t CC_CommonRsaVerify(int32_t  pkcsVersion,
34              int8_t *pPubKey,
35              int8_t *pDataIn,
36              int32_t  dataInSize,
37              int8_t *pSignature);
38 
39 
40 /**
41  * @brief The CC_CommonRsaSign generates RSA signature and returns it.
42  *
43  * The function follows the steps:
44  * 1. Read RSA private key structure
45  * 2. Call function according to PKCS version to create RSA signature
46  *
47  * @param[in] pkcsVersion - the version used (according to global definitions of available versions)
48  * @param[in] DataIn_ptr - the data to sign on
49  * @param[in] DataInSize - the data size
50  * @param[in] PemEncryptedFileName_ptr - the private key file
51  * @param[in] Key_ptr - the passphrase string
52  * @param[out] Signature_ptr - the RSA signature
53  *
54  */
55 /*********************************************************/
56 int32_t CC_CommonRsaSign(int32_t pkcsVersion,
57                int8_t *DataIn_ptr,
58                uint32_t  DataInSize,
59                int8_t *PemEncryptedFileName_ptr,
60                int8_t *Key_ptr,
61                int8_t *Signature_ptr);
62 
63 
64 
65 /**
66  * @brief Encrypts data using RSA.
67  *
68  * The function follows the steps:
69  * 1. Read RSA private key structure
70  * 2. Call function according to PKCS version to create RSA signature
71  *
72  * @param[in] pkcsVersion - the version used (according to global definitions of available versions)
73  * @param[in] pPemEncryptedFileName - the private key file
74  * @param[in] pKeyPwd - the passphrase string
75  * @param[in] pDataIn - the data to encrypt
76  * @param[in] dataInSize - the data size
77  * @param[out] pEncData - the encrypted data
78  *
79  */
80 /*********************************************************/
81 int32_t CC_CommonRsaEncrypt(int32_t pkcsVersion,
82               int8_t *pPubKey,
83               int8_t *pDataIn,
84               int32_t  dataInSize,
85               int8_t *pEncData);
86 
87 
88 /**
89  * @brief Decrypts data using RSA.
90  *
91  * The function follows the steps:
92  * 1. Read RSA private key structure
93  * 2. Call function according to PKCS version to create RSA signature
94  *
95  * @param[in] pkcsVersion - the version used (according to global definitions of available versions)
96  * @param[in] pPemEncryptedFileName - the private key file
97  * @param[in] pKeyPwd - the passphrase string
98  * @param[in] pEnDataIn - the data to decrypt
99  * @param[in] enDataInSize - the encrypted data size
100  * @param[out] pData - the decrypted data
101  *
102  */
103 /*********************************************************/
104 int32_t CC_CommonRsaDecrypt(int32_t pkcsVersion,
105               int8_t *pPemEncryptedFileName,
106               int8_t *pKeyPwd,
107               int8_t *pEnDataIn,
108               int32_t  enDataInSize,
109               int8_t *pData);
110 
111 
112 /**
113 * @brief The function CC_CommonRsaCalculateH calculates the H it returns it as binary string
114 *
115 * @param[in] N_ptr - public key N, represented as array of ascii's (0xbc is translated
116 *                    to 0x62 0x63)
117 * @param[out] H_ptr - The H result. H size is N_SIZE_IN_BYTES*2 + 1
118 *
119 */
120 /*********************************************************/
121 int32_t CC_CommonRsaCalculateH(const int8_t *N_ptr, int8_t *H_ptr);
122 
123 
124 
125 
126 /**
127 * @brief The CC_CommonRsaLoadKey reads RSA key from the file using passphrase
128 *        and returns its decrypted value.
129 *
130 * @param[in] PemEncryptedFileName_ptr - file name
131 * @param[in] Key_ptr - passphrase
132 */
133 /*********************************************************/
134 int32_t CC_CommonRsaLoadKey(int8_t *PemEncryptedFileName_ptr, int8_t *Key_ptr, int8_t *PemDecryted);
135 
136 
137 /**
138 * @brief The CC_CommonRandBytes reads RSA key from the file using passphrase
139 *        and returns its decrypted value.
140 *
141 * @param[in] PemEncryptedFileName_ptr - file name
142 * @param[in] Key_ptr - passphrase
143 */
144 /*********************************************************/
145 int32_t CC_CommonRandBytes(int32_t numBytes, int8_t *buf);
146 
147 
148 #endif
149