• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: crypto pke struct header. \n
16  *
17  * History: \n
18  * 2023-03-22, Create file. \n
19  */
20 #ifndef CRYPTO_PKE_STRUCT_H
21 #define CRYPTO_PKE_STRUCT_H
22 
23 #include "crypto_common_struct.h"
24 
25 typedef enum {
26     DRV_PKE_LEN_192 = 24,
27     DRV_PKE_LEN_224 = 32,
28     DRV_PKE_LEN_256 = 32,
29     DRV_PKE_LEN_384 = 48,
30     DRV_PKE_LEN_448 = 56,
31     DRV_PKE_LEN_512 = 64,
32     DRV_PKE_LEN_521 = 68,
33     DRV_PKE_LEN_576 = 72,
34     DRV_PKE_LEN_1024 = 128,
35     DRV_PKE_LEN_1536 = 192,
36     DRV_PKE_LEN_2048 = 256,
37     DRV_PKE_LEN_3072 = 384,
38     DRV_PKE_LEN_4096 = 512,
39     DRV_PKE_LEN_MAX,
40     DRV_PKE_LEN_INVALID = 0xffffffff,
41 } drv_pke_len;
42 
43 typedef enum {
44     DRV_PKE_ECC_TYPE_RFC5639_P256 = 0,      /* RFC 5639 - Brainpool P256/384/512 */
45     DRV_PKE_ECC_TYPE_RFC5639_P384,          /* RFC 5639 - Brainpool P256/384/512 */
46     DRV_PKE_ECC_TYPE_RFC5639_P512,          /* RFC 5639 - Brainpool P256/384/512 */
47     DRV_PKE_ECC_TYPE_FIPS_P256K,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
48     DRV_PKE_ECC_TYPE_FIPS_P192R,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
49     DRV_PKE_ECC_TYPE_FIPS_P224R,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
50     DRV_PKE_ECC_TYPE_FIPS_P256R,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
51     DRV_PKE_ECC_TYPE_FIPS_P384R,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
52     DRV_PKE_ECC_TYPE_FIPS_P521R,            /* NIST FIPS 186-4 P192/224/256/384/521, suggest not to use */
53     DRV_PKE_ECC_TYPE_RFC7748,               /* RFC 7748 - Curve25519 */
54     DRV_PKE_ECC_TYPE_RFC7748_448,           /* RFC 7748 - Curve448 */
55     DRV_PKE_ECC_TYPE_RFC8032,               /* RFC 8032 - ED25519 */
56     DRV_PKE_ECC_TYPE_SM2,                   /* GMT 0003.2-2012 */
57     DRV_PKE_ECC_TYPE_MAX,
58     DRV_PKE_ECC_TYPE_INVALID = 0xffffffff,
59 } drv_pke_ecc_curve_type;
60 
61 typedef enum {
62     DRV_PKE_RSA_SCHEME_PKCS1_V15 = 0x00,  /* PKCS#1 V15 */
63     DRV_PKE_RSA_SCHEME_PKCS1_V21,         /* PKCS#1 V21, PSS for signning, OAEP for encryption */
64     DRV_PKE_RSA_SCHEME_MAX,
65     DRV_PKE_RSA_SCHEME_INVALID = 0xffffffff,
66 } drv_pke_rsa_scheme;
67 
68 typedef enum {
69     DRV_PKE_HASH_TYPE_SHA1 = 0x00,   /* Suggest Not to use */
70     DRV_PKE_HASH_TYPE_SHA224,
71     DRV_PKE_HASH_TYPE_SHA256,
72     DRV_PKE_HASH_TYPE_SHA384,
73     DRV_PKE_HASH_TYPE_SHA512,
74     DRV_PKE_HASH_TYPE_SM3,
75     DRV_PKE_HASH_TYPE_MAX,
76     DRV_PKE_HASH_TYPE_INVALID = 0xffffffff,
77 } drv_pke_hash_type;
78 
79 typedef enum {
80     DRV_PKE_BUF_NONSECURE = 0x00,
81     DRV_PKE_BUF_SECURE,
82     DRV_PKE_BUF_INVALID = 0xffffffff,
83 } drv_pke_buffer_secure;
84 
85 typedef struct {
86     td_u32  length;
87     td_u8  *data;
88 } drv_pke_data;
89 
90 /* * struct of ecc point */
91 typedef struct {
92     td_u8 *x;   /* X coordinates of the generated public key, the caller ensures it is padded with leading
93                    zeros if the effective size of this key is smaller than ecc key size. */
94     td_u8 *y;   /* Y coordinates of the generated public key, the caller ensures it is padded with leading
95                    zeros if the effective size of this key is smaller than ecc key size. */
96     td_u32 length;
97 } drv_pke_ecc_point;
98 
99 /* * struct of ecc signature */
100 typedef struct {
101     td_u8 *r;   /* r component of the signature. */
102     td_u8 *s;   /* s component of the signature. */
103     td_u32 length;
104 } drv_pke_ecc_sig;
105 
106 /* * struct of ecc curves parameters. */
107 typedef struct {
108     const td_u8 *p;   /* prime specifying the base field. It is p (RFC5639), p (FIPS), p (RFC7748). */
109     const td_u8 *a;   /* Curve parameter a. It is A (RFC5639), c (FIPS), A24 (RFC7748), d(RFC8032). */
110     const td_u8 *b;   /* Curve parameter b. It is B (RFC5639), b (FIPS), N/A (RFC7748, RFC8032). */
111     const td_u8 *gx;  /* X coordinates of G which is a base point on the curve.
112                          It is x (RFC5639), Gx (FIPS), U(P) (RFC7748). */
113     const td_u8 *gy;  /* Y coordinates of G which is a base point on the curve.
114                          It is y (RFC5639), Gy (FIPS), N/A (RFC7748). */
115     const td_u8 *n;   /* Prime which is the order of G point. It is q (RFC5639), n (FIPS, RFC7748). */
116     td_u32 h;         /* Cofactor, which is the order of the elliptic curve divided by the order of the point G.
117                          It is h (RFC5639), h (FIPS), Cofactor (RFC7748). */
118     drv_pke_len ksize;         /* Ecc key size in bytes. It corresponds to the size in bytes of the prime. */
119     drv_pke_ecc_curve_type ecc_type; /* Type of ECC curve */
120 } drv_pke_ecc_curve;
121 
122 typedef struct {
123     td_u32  length;
124     td_u8  *data;
125     drv_pke_buffer_secure buf_sec;
126 } drv_pke_msg;
127 
128 /* * RSA private key struct */
129 typedef struct {
130     td_u8 *n;          /* *< public modulus */
131     td_u8 *e;          /* *< public exponent */
132     td_u8 *d;          /* *< private exponent */
133     td_u8 *p;          /* *< 1st prime factor */
134     td_u8 *q;          /* *< 2nd prime factor */
135     td_u8 *dp;         /* *< D % (P - 1) */
136     td_u8 *dq;         /* *< D % (Q - 1) */
137     td_u8 *qp;         /* *< 1 / (Q % P) */
138     td_u16 n_len;      /* *< length of public modulus */
139     td_u16 e_len;      /* *< length of public exponent */
140     td_u16 d_len;      /* *< length of private exponent */
141     td_u16 p_len;      /* *< length of 1st prime factor,should be half of u16NLen */
142     td_u16 q_len;      /* *< length of 2nd prime factor,should be half of u16NLen */
143     td_u16 dp_len;     /* *< length of D % (P - 1),should be half of u16NLen */
144     td_u16 dq_len;     /* *< length of D % (Q - 1),should be half of u16NLen */
145     td_u16 qp_len;     /* *< length of 1 / (Q % P),should be half of u16NLen */
146 } drv_pke_rsa_priv_key;
147 
148 /* * struct of RSA public key */
149 typedef struct {
150     td_u8  *n;            /* point to public modulus */
151     td_u8  *e;            /* point to public exponent */
152     td_u16 len;           /* length of public modulus, max value is 512Byte */
153 } drv_pke_rsa_pub_key;
154 
155 typedef struct {
156     const td_u8 *mont_a;    /* the montgomerized of parameter a(RFC5639, FIPS, SM2), a24(RFC7748), d(RFC8032). */
157     const td_u8 *mont_b;    /* the montgomerized of parameter b(RFC5639, FIPS, SM2), N/A(RFC7748), sqrt_m1(RFC8032). */
158     const td_u8 *mont_1_p;  /* the montgomerized of const value 1 (modp) */
159     const td_u8 *mont_1_n;  /* the montgomerized of const value 1 (modn) */
160     const td_u8 *rrp;  /* the montgomery parameter when modulur is p */
161     const td_u8 *rrn;  /* the montgomery parameter when modulur is n */
162     const td_u8 *const_1;
163     const td_u8 *const_0;
164     const td_u32 *mont_param_n;  /* the montgomerized parameter when the modulur is n. */
165     const td_u32 *mont_param_p;  /* the montgomerized parameter when the modulur is p. */
166 } pke_ecc_init_param;
167 
168 typedef struct {
169     const drv_pke_ecc_curve *curve_param;
170     const pke_ecc_init_param *default_param;
171 } pke_default_parameters;
172 
173 #endif