• 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 <stdint.h>
15 #include <limits.h>
16 #include <gmssl/error.h>
17 #include "sdf_int.h"
18 #include "sdf_sansec.h"
19 
20 
21 #define SDFerr(a,b)
22 
23 
24 typedef struct {
25 	ULONG id;
26 	char *name;
27 } table_item_t;
28 
29 static table_item_t sdf_cipher_caps[] = {
30 	{ SGD_SM1_ECB, "sm1-ecb" },
31 	{ SGD_SM1_CBC, "sm1-cbc" },
32 	{ SGD_SM1_CFB, "sm1-cfb" },
33 	{ SGD_SM1_OFB, "sm1-ofb128" },
34 	{ SGD_SM1_MAC, "cbcmac-sm1" },
35 	{ SGD_SSF33_ECB, "ssf33-ecb" },
36 	{ SGD_SSF33_CBC, "ssf33-cbc" },
37 	{ SGD_SSF33_CFB, "ssf33-cfb" },
38 	{ SGD_SSF33_OFB, "ssf33-ofb128" },
39 	{ SGD_SSF33_MAC, "cbcmac-ssf33" },
40 	{ SGD_SM4_ECB, "sms4-ecb" },
41 	{ SGD_SM4_CBC, "sms4-cbc" },
42 	{ SGD_SM4_CFB, "sms4-cfb" },
43 	{ SGD_SM4_OFB, "sms4-ofb128" },
44 	{ SGD_SM4_MAC, "cbcmac-sms4" },
45 	{ SGD_ZUC_EEA3, "zuc_128eea3" },
46 	{ SGD_ZUC_EIA3, "zuc_128eia3" }
47 };
48 
49 static table_item_t sdf_digest_caps[] = {
50 	{ SGD_SM3,  "sm3" },
51 	{ SGD_SHA1, "sha1" },
52 	{ SGD_SHA256, "sha256" },
53 };
54 
55 static table_item_t sdf_pkey_caps[] = {
56 	{ SGD_RSA_SIGN, "rsa" },
57 	{ SGD_RSA_ENC, "rsaEncryption" },
58 	{ SGD_SM2_1, "sm2sign" },
59 	{ SGD_SM2_2, "sm2exchange" },
60 	{ SGD_SM2_3, "sm2encrypt" }
61 };
62 
SDF_PrintDeviceInfo(FILE * fp,const DEVICEINFO * pstDeviceInfo)63 int SDF_PrintDeviceInfo(FILE *fp, const DEVICEINFO *pstDeviceInfo)
64 {
65 	size_t i, n;
66 	DEVICEINFO buf;
67 	DEVICEINFO *devInfo = &buf;
68 	int fmt = 0, ind = 4;
69 
70 	memcpy(devInfo, pstDeviceInfo, sizeof(DEVICEINFO));
71 	devInfo->IssuerName[39] = 0;
72 	devInfo->DeviceName[15] = 0;
73 	devInfo->DeviceSerial[15] = 0;
74 
75 	format_print(fp, fmt, ind, "%-18s: %s\n", "Device Name", devInfo->DeviceName);
76 	format_print(fp, fmt, ind, "%-18s: %s\n", "Serial Number", devInfo->DeviceSerial);
77 	format_print(fp, fmt, ind, "%-18s: %s\n", "Issuer", devInfo->IssuerName);
78 	format_print(fp, fmt, ind, "%-18s: %u\n", "Hardware Version", devInfo->DeviceVersion);
79 	format_print(fp, fmt, ind, "%-18s: %u\n", "Standard Version", devInfo->StandardVersion);
80 	format_print(fp, fmt, ind, "%-18s: ", "Public Key Algors");
81 	for (i = n = 0; i < sizeof(sdf_pkey_caps)/sizeof(sdf_pkey_caps[0]); i++) {
82 		if ((devInfo->AsymAlgAbility[0] & sdf_pkey_caps[i].id) ==
83 			sdf_pkey_caps[i].id) {
84 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_pkey_caps[i].name);
85 			n++;
86 		}
87 	}
88 	format_print(fp, fmt, 0, "\n");
89 
90 	format_print(fp, fmt, ind, "%-18s: ", "Ciphers");
91 	for (i = n = 0; i < sizeof(sdf_cipher_caps)/sizeof(sdf_cipher_caps[0]); i++) {
92 		if ((devInfo->SymAlgAbility & sdf_cipher_caps[i].id) ==
93 			sdf_cipher_caps[i].id) {
94 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_cipher_caps[i].name);
95 			n++;
96 		}
97 	}
98 	format_print(fp, fmt, 0, "\n");
99 
100 	format_print(fp, fmt, ind, "%-18s: ", "Digests");
101 	for (i = n = 0; i < sizeof(sdf_digest_caps)/sizeof(sdf_digest_caps[0]); i++) {
102 		if ((devInfo->HashAlgAbility & sdf_digest_caps[i].id) ==
103 			sdf_digest_caps[i].id) {
104 			format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_digest_caps[i].name);
105 			n++;
106 		}
107 	}
108 	format_print(fp, fmt, 0, "\n");
109 	return SDR_OK;
110 }
111 
SDF_PrintRSAPublicKey(FILE * fp,const RSArefPublicKey * blob)112 int SDF_PrintRSAPublicKey(FILE *fp, const RSArefPublicKey *blob)
113 {
114 	int fmt = 0, ind = 4;
115 	(void)format_print(fp, fmt, ind, "bits: %d\n", blob->bits);
116 	(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
117 	(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
118 	return SDR_OK;
119 }
120 
SDF_PrintRSAPrivateKey(FILE * fp,const RSArefPrivateKey * blob)121 int SDF_PrintRSAPrivateKey(FILE *fp, const RSArefPrivateKey *blob)
122 {
123 	int fmt = 0, ind = 4;
124 	(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
125 	(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
126 	(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
127 	(void)format_bytes(fp, fmt, ind, "d", blob->d, sizeof(blob->d));
128 	(void)format_bytes(fp, fmt, ind, "prime[0]", blob->prime[0], sizeof(blob->prime[0]));
129 	(void)format_bytes(fp, fmt, ind, "prime[1]", blob->prime[1], sizeof(blob->prime[1]));
130 	(void)format_bytes(fp, fmt, ind, "pexp[0]", blob->pexp[0], sizeof(blob->pexp[0]));
131 	(void)format_bytes(fp, fmt, ind, "pexp[1]", blob->pexp[1], sizeof(blob->pexp[1]));
132 	(void)format_bytes(fp, fmt, ind, "coef", blob->coef, sizeof(blob->coef));
133 	return SDR_OK;
134 }
135 
SDF_PrintECCPublicKey(FILE * fp,const ECCrefPublicKey * blob)136 int SDF_PrintECCPublicKey(FILE *fp, const ECCrefPublicKey *blob)
137 {
138 	int fmt = 0, ind = 4;
139 	(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
140 	(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
141 	(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
142 	return SDR_OK;
143 }
144 
SDF_PrintECCPrivateKey(FILE * fp,const ECCrefPrivateKey * blob)145 int SDF_PrintECCPrivateKey(FILE *fp, const ECCrefPrivateKey *blob)
146 {
147 	int fmt = 0, ind = 4;
148 	(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
149 	(void)format_bytes(fp, fmt, ind, "K", blob->K, sizeof(blob->K));
150 	return SDR_OK;
151 }
152 
SDF_PrintECCCipher(FILE * fp,const ECCCipher * blob)153 int SDF_PrintECCCipher(FILE *fp, const ECCCipher *blob)
154 {
155 	int fmt = 0, ind = 4;
156 	(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
157 	(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
158 	(void)format_bytes(fp, fmt, ind, "M", blob->M, sizeof(blob->M));
159 	(void)format_print(fp, fmt, ind, "L: %d", blob->L);
160 	(void)format_bytes(fp, fmt, ind, "C", blob->C, sizeof(blob->C));
161 	return SDR_OK;
162 }
163 
SDF_PrintECCSignature(FILE * fp,const ECCSignature * blob)164 int SDF_PrintECCSignature(FILE *fp, const ECCSignature *blob)
165 {
166 	int fmt = 0, ind = 4;
167 	(void)format_bytes(fp, fmt, ind, "r", blob->r, sizeof(blob->r));
168 	(void)format_bytes(fp, fmt, ind, "s", blob->s, sizeof(blob->s));
169 	return SDR_OK;
170 }
171 
SDF_ImportKey(void * hSessionHandle,unsigned char * pucKey,unsigned int uiKeyLength,void ** phKeyHandle)172 int SDF_ImportKey(
173 	void *hSessionHandle,
174 	unsigned char *pucKey,
175 	unsigned int uiKeyLength,
176 	void **phKeyHandle)
177 {
178 	(void)hSessionHandle;
179 	(void)pucKey;
180 	(void)uiKeyLength;
181 	(void)phKeyHandle;
182 	SDFerr(SDF_F_SDF_IMPORTKEY, SDF_R_NOT_IMPLEMENTED);
183 	return SDR_NOTSUPPORT;
184 }
185 
SDF_NewECCCipher(ECCCipher ** cipher,size_t ulDataLen)186 int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen)
187 {
188 	ECCCipher *ecc_cipher = NULL;
189 	size_t len;
190 
191 	if (!cipher) {
192 		SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_PASSED_NULL_PARAMETER);
193 		return SDR_INARGERR;
194 	}
195 
196 	if (!ulDataLen || ulDataLen > INT_MAX) {
197 		SDFerr(SDF_F_SDF_NEWECCCIPHER,
198 			SDF_R_INVALID_SM2_CIPHERTEXT_LENGTH);
199 		return SDR_INARGERR;
200 	}
201 
202 	len = sizeof(ECCCipher) - 1 + ulDataLen;
203 	if (len < sizeof(SANSEC_ECCCipher)) {
204 		len = sizeof(SANSEC_ECCCipher);
205 	}
206 
207 	if (!(ecc_cipher = malloc(len))) {
208 		SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_MALLOC_FAILURE);
209 		return SDR_NOBUFFER;
210 	}
211 	memset(ecc_cipher, 0, sizeof(*ecc_cipher));
212 
213 	ecc_cipher->L = (unsigned int)ulDataLen;
214 
215 	*cipher = ecc_cipher;
216 	return SDR_OK;
217 }
218 
SDF_FreeECCCipher(ECCCipher * cipher)219 int SDF_FreeECCCipher(ECCCipher *cipher)
220 {
221 	free(cipher);
222 	return SDR_OK;
223 }
224 
SDF_GetErrorReason(int err)225 const char *SDF_GetErrorReason(int err)
226 {
227 	switch (err) {
228 	case SDR_OK: return "SDR_OK";
229 	case SDR_BASE: return "SDR_BASE";
230 	case SDR_UNKNOWERR: return "SDR_UNKNOWERR";
231 	case SDR_NOTSUPPORT: return "SDR_NOTSUPPORT";
232 	case SDR_COMMFAIL: return "SDR_COMMFAIL";
233 	case SDR_HARDFAIL: return "SDR_HARDFAIL";
234 	case SDR_OPENDEVICE: return "SDR_OPENDEVICE";
235 	case SDR_OPENSESSION: return "SDR_OPENSESSION";
236 	case SDR_PARDENY: return "SDR_PARDENY";
237 	case SDR_KEYNOTEXIST: return "SDR_KEYNOTEXIST";
238 	case SDR_ALGNOTSUPPORT: return "SDR_ALGNOTSUPPORT";
239 	case SDR_ALGMODNOTSUPPORT: return "SDR_ALGMODNOTSUPPORT";
240 	case SDR_PKOPERR: return "SDR_PKOPERR";
241 	case SDR_SKOPERR: return "SDR_SKOPERR";
242 	case SDR_SIGNERR: return "SDR_SIGNERR";
243 	case SDR_VERIFYERR: return "SDR_VERIFYERR";
244 	case SDR_SYMOPERR: return "SDR_SYMOPERR";
245 	case SDR_STEPERR: return "SDR_STEPERR";
246 	case SDR_FILESIZEERR: return "SDR_FILESIZEERR";
247 	case SDR_FILENOEXIST: return "SDR_FILENOEXIST";
248 	case SDR_FILEOFSERR: return "SDR_FILEOFSERR";
249 	case SDR_KEYTYPEERR: return "SDR_KEYTYPEERR";
250 	case SDR_KEYERR: return "SDR_KEYERR";
251 	case SDR_ENCDATAERR: return "SDR_ENCDATAERR";
252 	case SDR_RANDERR: return "SDR_RANDERR";
253 	case SDR_PRKRERR: return "SDR_PRKRERR";
254 	case SDR_MACERR: return "SDR_MACERR";
255 	case SDR_FILEEXSITS: return "SDR_FILEEXSITS";
256 	case SDR_FILEWERR: return "SDR_FILEWERR";
257 	case SDR_NOBUFFER: return "SDR_NOBUFFER";
258 	case SDR_INARGERR: return "SDR_INARGERR";
259 	case SDR_OUTARGERR: return "SDR_OUTARGERR";
260 	}
261 	return "(unknown)";
262 }
263