• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef __GMSSL_INTERNAL_H__
13 #define __GMSSL_INTERNAL_H__
14 
15 #include <crypto_driver_adaptor.h>
16 #include <crypto/sm2.h>
17 #ifdef OPENSSL3_ENABLE
18 #include <internal/sm3.h>
19 #else
20 #include <crypto/sm3.h>
21 #endif
22 #ifndef OPENSSL_NO_SM4
23 #include <crypto/sm4.h>
24 #endif
25 #include <crypto/evp.h>
26 #include <openssl/evp.h>
27 #ifdef CRYPTO_SUPPORT_SOFT_ECC
28 #include <openssl/ec.h>
29 #include <ec/ec_local.h>
30 #endif
31 #include "crypto/ec.h"
32 #include <openssl/ossl_typ.h>
33 #include <openssl/crypto.h>
34 #include <openssl/bn.h>
35 #include <openssl/modes.h>
36 #include <openssl/rand.h>
37 #include <openssl/asn1.h>
38 #include <openssl/asn1t.h>
39 #include <bn/bn_local.h>
40 #include <tee_log.h>
41 #include <tee_crypto_api.h>
42 
43 #define KEY_SIZE                 32
44 #define KEY_SIZE_2               64
45 #define HEX_LEN                  2
46 #define RAND_SIZE                64
47 #define SM4_BLOCK                16
48 #define MOD_LEN                  65 /* 256/8*2+1 */
49 #define ONLY_PUBLIC_LEN          3
50 #define KEYPAIR_LEN              4
51 #define GMSSL_OK                 1
52 #define GMSSL_ERR                0
53 #define STR_END_ZERO             1
54 #define HASH_SIZE                32
55 #define COORDINATE_LEN           32
56 #define COORDINATE_NUM           2
57 #define SM2_CIPHER_START         0x04
58 #define SM2_CIPHER_START_LEN     1
59 #define SIG_COMPONENT_SIZE       32
60 #define SIG_COMPONENT_NUM        2
61 #define SM2_CIPHER_INCREASE      97
62 #define BYTE_TO_BIT              8
63 #define SM2_INCREASE_MAX         110
64 #define STR_TO_HEX               2
65 #define HEX_FLAG                 16
66 #define SM2_GROUP_NOSTANDARD     0x12
67 #define SM2_ENCRYPTED_LEN        200
68 #define SM2_INCREASE_MIN         106
69 #define SM2_SIGN_MAX             72
70 #define SM2_SIGN_MIN             70
71 #define SM2_DIGEST_LEN           32
72 #define SM2_KEYPAIR_ATTR_COUNT   4
73 #define SM2_KEY_SIZE_BIT         256
74 #define SM2_SIG_LEN              64
75 #define SM2_MAX_PLAINTEXT_LENGTH 1024
76 
77 #ifdef CRYPTO_SUPPORT_SOFT_SM2
78 struct sm2_public_key {
79     uint8_t sm2_x[KEY_SIZE_2 + 1];
80     uint8_t sm2_y[KEY_SIZE_2 + 1];
81     uint32_t group;
82 };
83 
84 struct sm2_public_key_2 {
85     uint8_t sm2_x[KEY_SIZE];
86     uint8_t sm2_y[KEY_SIZE];
87     uint32_t group;
88 };
89 
90 typedef struct sm2_key_pair_s {
91     char x[KEY_SIZE_2];
92     char y[KEY_SIZE_2];
93     char d[KEY_SIZE_2];
94 } sm2_key_pair;
95 
96 struct ec_key_pair_bignum_t {
97     EC_GROUP *group;
98     EC_POINT *point;
99     BIGNUM *big_p;
100     BIGNUM *big_a;
101     BIGNUM *big_b;
102     BIGNUM *big_d;
103     BIGNUM *big_x;
104     BIGNUM *big_y;
105     BIGNUM *big_n;
106     BIGNUM *big_h;
107     BN_CTX *ctx;
108 };
109 
110 struct sm2_eckey_get_dxy_t {
111     char *d;
112     char *x;
113     char *y;
114     int32_t d_len;
115     int32_t x_len;
116     int32_t y_len;
117 };
118 
119 struct sm2_new_ec_group_t {
120     const char *p_hex;
121     const char *a_hex;
122     const char *b_hex;
123     const char *x_hex;
124     const char *y_hex;
125     const char *n_hex;
126     const char *h_hex;
127 };
128 
129 #endif
130 #endif
131