1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include "skf_int.h"
17 #include "skf_wisec.h"
18
19
20 typedef struct {
21 ULONG std_id;
22 ULONG vendor_id;
23 } SKF_ALGOR_PAIR;
24
25 static SKF_ALGOR_PAIR wisec_ciphers[] = {
26 { SGD_SM1, WISEC_SM1 },
27 { SGD_SM1_ECB, WISEC_SM1_ECB },
28 { SGD_SM1_CBC, WISEC_SM1_CBC },
29 { SGD_SM1_CFB, WISEC_SM1_CFB },
30 { SGD_SM1_OFB, WISEC_SM1_OFB },
31 { SGD_SM1_MAC, WISEC_SM1_MAC },
32 { SGD_SM4, WISEC_SM4 },
33 { SGD_SM4_ECB, WISEC_SM4_ECB },
34 { SGD_SM4_CBC, WISEC_SM4_CBC },
35 { SGD_SM4_CFB, WISEC_SM4_CFB },
36 { SGD_SM4_OFB, WISEC_SM4_OFB },
37 { SGD_SM4_MAC, WISEC_SM4_MAC },
38 { SGD_SSF33, WISEC_SSF33 },
39 { SGD_SSF33_ECB, WISEC_SSF33_ECB },
40 { SGD_SSF33_CBC, WISEC_SSF33_CBC },
41 { SGD_SSF33_CFB, WISEC_SSF33_CFB },
42 { SGD_SSF33_OFB, WISEC_SSF33_OFB },
43 { SGD_SSF33_MAC, WISEC_SSF33_MAC },
44 };
45
wisec_get_cipher_algor(ULONG vendor_id)46 static ULONG wisec_get_cipher_algor(ULONG vendor_id)
47 {
48 size_t i;
49 for (i = 0; i < sizeof(wisec_ciphers)/sizeof(wisec_ciphers[0]); i++) {
50 if (vendor_id == wisec_ciphers[i].vendor_id) {
51 return wisec_ciphers[i].std_id;
52 }
53 }
54 return 0;
55 }
56
wisec_get_cipher_cap(ULONG vendor_cap)57 static ULONG wisec_get_cipher_cap(ULONG vendor_cap)
58 {
59 ULONG std_cap = 0;
60 size_t i;
61 for (i = 0; i < sizeof(wisec_ciphers)/sizeof(wisec_ciphers[0]); i++) {
62 if (vendor_cap & wisec_ciphers[i].vendor_id) {
63 std_cap |= wisec_ciphers[i].std_id;
64 }
65 }
66 return std_cap;
67 }
68
69 static SKF_ALGOR_PAIR wisec_digests[] = {
70 { SGD_SM3, WISEC_SM3 },
71 { SGD_SHA1, WISEC_SHA1 },
72 { SGD_SHA256, WISEC_SHA256 },
73 };
74
wisec_get_digest_algor(ULONG vendor_id)75 static ULONG wisec_get_digest_algor(ULONG vendor_id)
76 {
77 size_t i;
78 for (i = 0; i < sizeof(wisec_digests)/sizeof(wisec_digests[0]); i++) {
79 if (vendor_id == wisec_digests[i].vendor_id) {
80 return wisec_digests[i].std_id;
81 }
82 }
83 return 0;
84 }
85
wisec_get_digest_cap(ULONG vendor_cap)86 static ULONG wisec_get_digest_cap(ULONG vendor_cap)
87 {
88 ULONG std_cap = 0;
89 size_t i;
90 for (i = 0; i < sizeof(wisec_digests)/sizeof(wisec_digests[0]); i++) {
91 if (vendor_cap & wisec_digests[i].vendor_id) {
92 std_cap |= wisec_digests[i].std_id;
93 }
94 }
95 return std_cap;
96 }
97
98 static SKF_ALGOR_PAIR wisec_pkeys[] = {
99 { SGD_RSA, WISEC_RSA },
100 { SGD_RSA_SIGN, WISEC_RSA_SIGN },
101 { SGD_RSA_ENC, WISEC_RSA_ENC },
102 { SGD_SM2, WISEC_SM2 },
103 { SGD_SM2_1, WISEC_SM2_1 },
104 { SGD_SM2_2, WISEC_SM2_2 },
105 { SGD_SM2_3, WISEC_SM2_3 },
106 };
107
wisec_get_pkey_algor(ULONG vendor_id)108 static ULONG wisec_get_pkey_algor(ULONG vendor_id)
109 {
110 size_t i;
111 for (i = 0; i < sizeof(wisec_pkeys)/sizeof(wisec_pkeys[0]); i++) {
112 if (vendor_id == wisec_pkeys[i].vendor_id) {
113 return wisec_pkeys[i].std_id;
114 }
115 }
116 return 0;
117 }
118
wisec_get_pkey_cap(ULONG vendor_cap)119 static ULONG wisec_get_pkey_cap(ULONG vendor_cap)
120 {
121 ULONG std_cap = 0;
122 size_t i;
123 for (i = 0; i < sizeof(wisec_pkeys)/sizeof(wisec_pkeys[0]); i++) {
124 if (vendor_cap & wisec_pkeys[i].vendor_id) {
125 std_cap |= wisec_pkeys[i].std_id;
126 }
127 }
128 return std_cap;
129 }
130
wisec_get_error_reason(ULONG err)131 static unsigned long wisec_get_error_reason(ULONG err)
132 {
133 return 0;
134 }
135
136 SKF_VENDOR skf_wisec = {
137 "wisec",
138 16,
139 wisec_get_cipher_algor,
140 wisec_get_cipher_cap,
141 wisec_get_digest_algor,
142 wisec_get_digest_cap,
143 wisec_get_pkey_algor,
144 wisec_get_pkey_cap,
145 wisec_get_error_reason,
146 };
147