1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /******************************************************************************
19  * @file     drv/rsa.h
20  * @brief    Header File for RSA Driver
21  * @version  V1.0
22  * @date     02. June 2020
23  * @model    rsa
24  ******************************************************************************/
25 #ifndef _DRV_RSA_H_
26 #define _DRV_RSA_H_
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <stdint.h>
33 #include <drv/common.h>
34 
35 /*----- RSA Control Codes: Mode Parameters: Key Bits -----*/
36 /****** RSA Key bits Type *****/
37 typedef enum {
38     RSA_KEY_BITS_192  = 0U,                    /* 192 Key bits */
39     RSA_KEY_BITS_256,                      /* 256 Key bits */
40     RSA_KEY_BITS_512,                      /* 512 Key bits */
41     RSA_KEY_BITS_1024,                     /* 1024 Key bits */
42     RSA_KEY_BITS_2048,                     /* 2048 Key bits */
43     RSA_KEY_BITS_3072,                     /* 3072 Key bits */
44     RSA_KEY_BITS_4096                      /* 4096 Key bits */
45 } csi_rsa_key_bits_t;
46 
47 /****** RSA Padding Type *****/
48 typedef enum {
49     RSA_PADDING_MODE_NO  = 0,              /* RSA NO Padding Mode */
50     RSA_PADDING_MODE_PKCS1,                /* RSA PKCS1 Padding Mode */
51     RSA_PADDING_MODE_PKCS1_OAEP,           /* RSA PKCS1 OAEP Padding Mode */
52     RSA_PADDING_MODE_SSLV23,               /* RSA SSLV23 Padding Mode */
53     RSA_PADDING_MODE_X931,                 /* RSA X931 Padding Mode */
54     RSA_PADDING_MODE_PSS                   /* RSA PSS Padding Mode */
55 } csi_rsa_padding_type_t;
56 
57 /****** RSA Hash Type *****/
58 typedef enum {
59     RSA_HASH_TYPE_MD5  = 0,
60     RSA_HASH_TYPE_SHA1,
61     RSA_HASH_TYPE_SHA224,
62     RSA_HASH_TYPE_SHA256,
63     RSA_HASH_TYPE_SHA384,
64     RSA_HASH_TYPE_SHA512
65 } csi_rsa_hash_type_t;
66 
67 /****** RSA Context *****/
68 typedef struct {
69     void *n;                                /* Pointer to the public modulus */
70     void *e;                                /* Pointer to the public exponent */
71     void *d;                                /* Pointer to the private exponent */
72     csi_rsa_key_bits_t  key_bits;           /* RSA KEY BITS */
73     csi_rsa_padding_type_t padding_type;    /* RSA PADDING TYPE */
74 } csi_rsa_context_t;
75 
76 /****** RSA State *****/
77 typedef struct {
78     uint8_t busy             : 1;           /* Calculate busy flag */
79     uint8_t error            : 1;           /* Calculate error flag */
80 } csi_rsa_state_t;
81 
82 /****** RSA Ctrl *****/
83 typedef struct {
84     csi_dev_t           dev;
85     void                *cb;
86     void                *arg;
87     csi_rsa_state_t     state;
88     void                *prim;
89 } csi_rsa_t;
90 
91 /****** RSA Moddle *****/
92 typedef struct {
93   uint32_t pout[64];
94   uint8_t  *pouts;
95   uint32_t *pout_size;
96   uint32_t u32keywords;
97   uint8_t  *pdst;
98   uint32_t u32padding;
99   uint32_t u32dst_words;
100   uint32_t u32type;
101   uint32_t rsa_state;
102 }rsa_middle_t;
103 
104 /****** RSA Event *****/
105 typedef enum {
106     RSA_EVENT_COMPLETE    = 0,            /* rsa event completed */
107     RSA_EVENT_VERIFY_SUCCESS,             /* rsa event verify success */
108     RSA_EVENT_VERIFY_FAILED,              /* rsa event verify failed */
109     RSA_EVENT_ERROR,                      /* rsa event error */
110 } csi_rsa_event_t;
111 
112 typedef void (*csi_rsa_callback_t)(csi_rsa_t *rsa, csi_rsa_event_t event, void *arg);   ///< Pointer to \ref csi_rsa_callback_t : RSA Event call back.
113 
114 /**
115   \brief       Initialize RSA Interface. 1. Initializes the resources needed for the RSA interface 2.registers event callback function
116   \param[in]   rsa  RSA handle to operate.
117   \param[in]   idx  Device id
118   \return      Error code \ref csi_error_t
119 */
120 csi_error_t csi_rsa_init(csi_rsa_t *rsa, uint32_t idx);
121 
122 /**
123   \brief       De-initialize RSA Interface. stops operation and releases the software resources used by the interface
124   \param[in]   rsa  RSA handle to operate.
125   \return      none
126 */
127 void csi_rsa_uninit(csi_rsa_t *rsa);
128 
129 /**
130   \brief       Generate rsa key pair.
131   \param[in]   rsa       RSA handle to operate.
132   \param[out]  context   Pointer to the rsa context
133   \return      Error code \ref csi_error_t
134 */
135 csi_error_t csi_rsa_gen_key(csi_rsa_t *rsa, csi_rsa_context_t *context);
136 
137 /**
138   \brief       Encrypt
139   \param[in]   rsa       RSA handle to operate.
140   \param[in]   context   Pointer to the rsa context
141   \param[in]   src       Pointer to the source data.
142   \param[in]   src_size  The source data len
143   \param[out]  out       Pointer to the result buffer
144   \return      Error code \ref csi_error_t
145 */
146 csi_error_t csi_rsa_encrypt(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out);
147 
148 /**
149   \brief       decrypt
150   \param[in]   rsa       RSA handle to operate.
151   \param[in]   context   Pointer to the rsa context
152   \param[in]   src       Pointer to the source data.
153   \param[in]   src_size  The source data len
154   \param[out]  out       Pointer to the result buffer
155   \param[out]  out_size  The result size
156   \return      Error code \ref csi_error_t
157 */
158 csi_error_t csi_rsa_decrypt(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *out, uint32_t *out_size);
159 
160 /**
161   \brief       RSA sign
162   \param[in]   rsa       RSA handle to operate.
163   \param[in]   context   Pointer to the rsa context
164   \param[in]   src       Pointer to the source data.
165   \param[in]   src_size  The source data len
166   \param[out]  signature Pointer to the signature
167   \param[in]   hash_type The source data hash type
168   \return      Error code \ref csi_error_t
169 */
170 csi_error_t csi_rsa_sign(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, csi_rsa_hash_type_t hash_type);
171 
172 /**
173   \brief       RSA verify
174   \param[in]   rsa       RSA handle to operate.
175   \param[in]   context   Pointer to the rsa context
176   \param[in]   src       Pointer to the source data.
177   \param[in]   src_size  The source data len
178   \param[in]   signature Pointer to the signature
179   \param[in]   sig_size  The signature size
180   \param[in]   hash_type The source data hash type
181   \return      Verify result
182 */
183 bool csi_rsa_verify(csi_rsa_t *rsa, csi_rsa_context_t *context, void *src, uint32_t src_size, void *signature, uint32_t sig_size, csi_rsa_hash_type_t hash_type);
184 
185 /**
186   \brief       Get big prime data
187   \param[in]   rsa          RSA handle to operate.
188   \param[in]   p            Pointer to the prime
189   \param[in]   bit_length   Pointer to the prime bit length
190   \return      Error code \ref csi_error_t
191 */
192 csi_error_t csi_rsa_get_prime(csi_rsa_t *rsa, void *p, uint32_t bit_length);
193 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* _DRV_RSA_H_ */
199