• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <gmssl/error.h>
16 #include "skf.h"
17 #include "skf_int.h"
18 #include "skf_ext.h"
19 
20 
21 
skf_algor_name(ULONG ulAlgID)22 static char *skf_algor_name(ULONG ulAlgID)
23 {
24 	switch (ulAlgID) {
25 	case SGD_SM1_ECB: return "sm1-ecb";
26 	case SGD_SM1_CBC: return "sm1-cbc";
27 	case SGD_SM1_CFB: return "sm1-cfb";
28 	case SGD_SM1_OFB: return "sm1-ofb128";
29 	case SGD_SM1_MAC: return "sm1-mac";
30 	case SGD_SM4_ECB: return "sms4-ecb";
31 	case SGD_SM4_CBC: return "sms4-cbc";
32 	case SGD_SM4_CFB: return "sms4-cfb";
33 	case SGD_SM4_OFB: return "sms4-ofb128";
34 	case SGD_SM4_MAC: return "sms4-mac";
35 	case SGD_SSF33_ECB: return "ssf33-ecb";
36 	case SGD_SSF33_CBC: return "ssf33-cbc";
37 	case SGD_SSF33_CFB: return "ssf33-cfb";
38 	case SGD_SSF33_OFB: return "ssf33-ofb128";
39 	case SGD_SSF33_MAC: return "ssf33-mac";
40 	case SGD_RSA: return "rsa";
41 	case SGD_SM2_1: return "sm2sign";
42 	case SGD_SM2_2: return "sm2encrypt";
43 	case SGD_SM2_3: return "sm2keyagreement";
44 	case SGD_SM3: return "sm3";
45 	case SGD_SHA1: return "sha1";
46 	case SGD_SHA256: return "sha256";
47 	}
48 	return NULL;
49 }
50 
SKF_GetDevStateName(ULONG ulDevState,LPSTR * szDevStateName)51 ULONG SKF_GetDevStateName(ULONG ulDevState, LPSTR *szDevStateName)
52 {
53 	if (!szDevStateName) {
54 		return SAR_INDATALENERR;
55 	}
56 
57 	switch (ulDevState) {
58 	case SKF_DEV_STATE_ABSENT:
59 		*szDevStateName = (LPSTR)"Absent";
60 		break;
61 	case SKF_DEV_STATE_PRESENT:
62 		*szDevStateName = (LPSTR)"Present";
63 		break;
64 	case SKF_DEV_STATE_UNKNOW:
65 		*szDevStateName = (LPSTR)"Unknown";
66 		break;
67 	default:
68 		*szDevStateName = (LPSTR)"(Error)";
69 		return SAR_INDATALENERR;
70 	}
71 
72 	return SAR_OK;
73 }
74 
SKF_GetContainerTypeName(ULONG ulContainerType,LPSTR * szName)75 ULONG SKF_GetContainerTypeName(ULONG ulContainerType, LPSTR *szName)
76 {
77 	switch (ulContainerType) {
78 	case SKF_CONTAINER_TYPE_UNDEF:
79 		*szName = (LPSTR)"(undef)";
80 		break;
81 	case SKF_CONTAINER_TYPE_RSA:
82 		*szName = (LPSTR)"RSA";
83 		break;
84 	case SKF_CONTAINER_TYPE_ECC:
85 		*szName = (LPSTR)"EC";
86 		break;
87 	default:
88 		*szName = (LPSTR)"(unknown)";
89 	}
90 	/* always success for help functions */
91 	return SAR_OK;
92 }
93 
94 typedef struct {
95 	ULONG id;
96 	char *name;
97 } table_item_t;
98 
99 static table_item_t skf_cipher_caps[] = {
100 	{ SGD_SM1_ECB, "sm1-ecb" },
101 	{ SGD_SM1_CBC, "sm1-cbc" },
102 	{ SGD_SM1_CFB, "sm1-cfb" },
103 	{ SGD_SM1_OFB, "sm1-ofb128" },
104 	{ SGD_SM1_MAC, "cbcmac-sm1" },
105 	{ SGD_SSF33_ECB, "ssf33-ecb" },
106 	{ SGD_SSF33_CBC, "ssf33-cbc" },
107 	{ SGD_SSF33_CFB, "ssf33-cfb" },
108 	{ SGD_SSF33_OFB, "ssf33-ofb128" },
109 	{ SGD_SSF33_MAC, "cbcmac-ssf33" },
110 	{ SGD_SM4_ECB, "sms4-ecb" },
111 	{ SGD_SM4_CBC, "sms4-cbc" },
112 	{ SGD_SM4_CFB, "sms4-cfb" },
113 	{ SGD_SM4_OFB, "sms4-ofb128" },
114 	{ SGD_SM4_MAC, "cbcmac-sms4" },
115 	{ SGD_ZUC_EEA3, "zuc_128eea3" },
116 	{ SGD_ZUC_EIA3, "zuc_128eia3" }
117 };
118 
119 static table_item_t skf_digest_caps[] = {
120 	{ SGD_SM3,  "sm3" },
121 	{ SGD_SHA1, "sha1" },
122 	{ SGD_SHA256, "sha256" },
123 };
124 
125 static table_item_t skf_pkey_caps[] = {
126 	{ SGD_RSA_SIGN, "rsa" },
127 	{ SGD_RSA_ENC, "rsaEncryption" },
128 	{ SGD_SM2_1, "sm2sign" },
129 	{ SGD_SM2_2, "sm2exchange" },
130 	{ SGD_SM2_3, "sm2encrypt" }
131 };
132 
SKF_PrintDevInfo(FILE * fp,const DEVINFO * devInfo)133 ULONG SKF_PrintDevInfo(FILE *fp, const DEVINFO *devInfo)
134 {
135 	size_t i, n;
136 	int fmt = 0, ind = 4;
137 
138 	format_print(fp, fmt, ind, "Version: %d.%d\n", devInfo->Version.major, devInfo->Version.minor);
139 	format_print(fp, fmt, ind, "Manufacturer: %s\n", devInfo->Manufacturer);
140 	format_print(fp, fmt, ind, "Issuer: %s\n", devInfo->Issuer);
141 	format_print(fp, fmt, ind, "Label: %s\n", devInfo->Label);
142 	format_bytes(fp, fmt, ind, "SerialNumber", devInfo->SerialNumber, strlen((char *)devInfo->SerialNumber));
143 	format_print(fp, fmt, ind, "FirmwareVersion: %d.%d\n", devInfo->HWVersion.major, devInfo->HWVersion.minor);
144 
145 	format_print(fp, fmt, ind, "Ciphers: ");
146 	for (i = n = 0; i < sizeof(skf_cipher_caps)/sizeof(skf_cipher_caps[0]); i++) {
147 		if ((devInfo->AlgSymCap & skf_cipher_caps[i].id) ==
148 			skf_cipher_caps[i].id) {
149 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", skf_cipher_caps[i].name);
150 			n++;
151 		}
152 	}
153 	format_print(fp, fmt, 0, "\n");
154 
155 	format_print(fp, fmt, ind, "Public Keys: ");
156 	for (i = n = 0; i < sizeof(skf_pkey_caps)/sizeof(skf_pkey_caps[0]); i++) {
157 		if ((devInfo->AlgAsymCap & skf_pkey_caps[i].id) ==
158 			skf_pkey_caps[i].id) {
159 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", skf_pkey_caps[i].name);
160 			n++;
161 		}
162 	}
163 	format_print(fp, fmt, 0, "\n");
164 
165 	format_print(fp, fmt, ind, "Digests: ");
166 	for (i = n = 0; i < sizeof(skf_digest_caps)/sizeof(skf_digest_caps[0]); i++) {
167 		if ((devInfo->AlgHashCap & skf_digest_caps[i].id) ==
168 			skf_digest_caps[i].id) {
169 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", skf_digest_caps[i].name);
170 			n++;
171 		}
172 	}
173 	format_print(fp, fmt, 0, "\n");
174 
175 	format_print(fp, fmt, ind, "AuthCipher");
176 	for (i = 0; i < sizeof(skf_cipher_caps)/sizeof(skf_cipher_caps[0]); i++) {
177 		if (devInfo->DevAuthAlgId == skf_cipher_caps[i].id) {
178 			format_print(fp, fmt, 0, "%s\n", skf_cipher_caps[i].name);
179 			break;
180 		}
181 	}
182 	if (i == sizeof(skf_cipher_caps)/sizeof(skf_cipher_caps[0])) {
183 		format_print(fp, fmt, 0, "(unknown)\n");
184 	}
185 	format_print(fp, fmt, 0, "\n");
186 
187 
188 
189 	if (devInfo->TotalSpace == UINT_MAX)
190 		format_print(fp, fmt, ind, "Total Sapce: %s\n", "(unlimited)");
191 	else	format_print(fp, fmt, ind, "Total Sapce: %u\n", devInfo->TotalSpace);
192 
193 	if (devInfo->FreeSpace == UINT_MAX)
194 		format_print(fp, fmt, ind, "Free Space: %s\n", "(unlimited)");
195 	else	format_print(fp, fmt, ind, "Free Space: %u\n", devInfo->FreeSpace);
196 
197 	if (devInfo->MaxECCBufferSize == UINT_MAX)
198 		format_print(fp, fmt, ind, "MAX ECC Input: %s\n", "(unlimited)");
199 	else	format_print(fp, fmt, ind, "MAX ECC Input: %u\n",  devInfo->MaxECCBufferSize);
200 
201 	if (devInfo->MaxBufferSize == UINT_MAX)
202 		format_print(fp, fmt, ind, "MAX Cipher Input: %s\n", "(unlimited)");
203 	else	format_print(fp, fmt, ind, "MAX Cipher Input: %u\n", devInfo->MaxBufferSize);
204 
205 	return SAR_OK;
206 }
207 
SKF_PrintRSAPublicKey(FILE * fp,const RSAPUBLICKEYBLOB * blob)208 ULONG SKF_PrintRSAPublicKey(FILE *fp, const RSAPUBLICKEYBLOB *blob)
209 {
210 	int fmt = 0, ind = 4;
211 	format_print(fp, fmt, ind, "AlgID: %s\n", skf_algor_name(blob->AlgID));
212 	format_print(fp, fmt, ind, "BitLen: %u\n", blob->BitLen);
213 	format_bytes(fp, fmt, ind, "Modulus", blob->Modulus, MAX_RSA_MODULUS_LEN);
214 	format_bytes(fp, fmt, ind, "PublicExponent", blob->PublicExponent, MAX_RSA_EXPONENT_LEN);
215 	return SAR_OK;
216 }
217 
SKF_PrintRSAPrivateKey(FILE * fp,const RSAPRIVATEKEYBLOB * blob)218 ULONG SKF_PrintRSAPrivateKey(FILE *fp, const RSAPRIVATEKEYBLOB *blob)
219 {
220 	int fmt = 0, ind = 4;
221 	format_print(fp, fmt, ind, "AlgID: %s\n", skf_algor_name(blob->AlgID));
222 	format_print(fp, fmt, ind, "BitLen: %u\n", blob->BitLen);
223 	format_bytes(fp, fmt, ind, "Modulus", blob->Modulus, MAX_RSA_MODULUS_LEN);
224 	format_bytes(fp, fmt, ind, "PublicExponent", blob->PublicExponent, MAX_RSA_EXPONENT_LEN);
225 	format_bytes(fp, fmt, ind, "PrivateExponent", blob->PrivateExponent, MAX_RSA_MODULUS_LEN);
226 	format_bytes(fp, fmt, ind, "Prime1", blob->Prime1, MAX_RSA_MODULUS_LEN/2);
227 	format_bytes(fp, fmt, ind, "Prime2", blob->Prime2, MAX_RSA_MODULUS_LEN/2);
228 	format_bytes(fp, fmt, ind, "Prime1Exponent", blob->Prime1Exponent, MAX_RSA_MODULUS_LEN/2);
229 	format_bytes(fp, fmt, ind, "Prime2Exponent", blob->Prime2Exponent, MAX_RSA_MODULUS_LEN/2);
230 	format_bytes(fp, fmt, ind, "Coefficient", blob->Coefficient, MAX_RSA_MODULUS_LEN/2);
231 	return SAR_OK;
232 }
233 
SKF_PrintECCPublicKey(FILE * fp,const ECCPUBLICKEYBLOB * blob)234 ULONG SKF_PrintECCPublicKey(FILE *fp, const ECCPUBLICKEYBLOB *blob)
235 {
236 	int fmt = 0, ind = 4;
237 	format_print(fp, fmt, ind, "BitLen: %u\n", blob->BitLen);
238 	format_bytes(fp, fmt, ind, "XCoordinate", blob->XCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8);
239 	format_bytes(fp, fmt, ind, "YCoordinate", blob->YCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8);
240 	return SAR_OK;
241 }
242 
SKF_PrintECCPrivateKey(FILE * fp,const ECCPRIVATEKEYBLOB * blob)243 ULONG SKF_PrintECCPrivateKey(FILE *fp, const ECCPRIVATEKEYBLOB *blob)
244 {
245 	int fmt = 0, ind = 4;
246 	format_print(fp, fmt, ind, "BitLen: %u\n", blob->BitLen);
247 	format_bytes(fp, fmt, ind, "PrivateKey", blob->PrivateKey, ECC_MAX_MODULUS_BITS_LEN/8);
248 	return SAR_OK;
249 }
250 
SKF_PrintECCCipher(FILE * fp,const ECCCIPHERBLOB * blob)251 ULONG SKF_PrintECCCipher(FILE *fp, const ECCCIPHERBLOB *blob)
252 {
253 	int fmt = 0, ind = 4;
254 	format_bytes(fp, fmt, ind, "XCoordinate", blob->XCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8);
255 	format_bytes(fp, fmt, ind, "YCoordinate", blob->YCoordinate, ECC_MAX_XCOORDINATE_BITS_LEN/8);
256 	format_bytes(fp, fmt, ind, "HASH", blob->HASH, 32);
257 	format_print(fp, fmt, ind, "CipherLen: %u\n", blob->CipherLen);
258 	format_bytes(fp, fmt, ind, "Cipher", blob->Cipher, blob->CipherLen);
259 	return SAR_OK;
260 }
261 
SKF_PrintECCSignature(FILE * fp,const ECCSIGNATUREBLOB * blob)262 ULONG SKF_PrintECCSignature(FILE *fp, const ECCSIGNATUREBLOB *blob)
263 {
264 	int fmt = 0, ind = 4;
265 	format_bytes(fp, fmt, ind, "r", blob->r, ECC_MAX_XCOORDINATE_BITS_LEN/8);
266 	format_bytes(fp, fmt, ind, "s", blob->s, ECC_MAX_XCOORDINATE_BITS_LEN/8);
267 	return SAR_OK;
268 }
269 
SKF_GetAlgorName(ULONG ulAlgID,LPSTR * szName)270 ULONG DEVAPI SKF_GetAlgorName(ULONG ulAlgID, LPSTR *szName)
271 {
272 	char *name;
273 	if ((name = skf_algor_name(ulAlgID)) != NULL) {
274 		*szName = (LPSTR)&name;
275 		return SAR_OK;
276 	}
277 	return SAR_FAIL;
278 }
279 
SKF_PrintErrorString(FILE * fp,ULONG ulError)280 ULONG DEVAPI SKF_PrintErrorString(FILE *fp, ULONG ulError)
281 {
282 	LPSTR str = NULL;
283 	SKF_GetErrorString(ulError, &str);
284 	fprintf(fp, "SKF Error: %s\n", (char *)str);
285 	return SAR_OK;
286 }
287