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 <string.h>
13 #include <stdlib.h>
14 #include <gmssl/sdf.h>
15 #include <gmssl/sm2.h>
16 #include <gmssl/error.h>
17 #include "sdf.h"
18 #include "sdf_ext.h"
19
20
21
22 static const uint8_t zeros[ECCref_MAX_LEN - 32] = {0};
23
SDF_ECCrefPublicKey_to_SM2_KEY(const ECCrefPublicKey * ref,SM2_KEY * sm2_key)24 static int SDF_ECCrefPublicKey_to_SM2_KEY(const ECCrefPublicKey *ref, SM2_KEY *sm2_key)
25 {
26 SM2_POINT point;
27
28 if (ref->bits != 256) {
29 error_print();
30 return -1;
31 }
32 if (memcmp(ref->x, zeros, sizeof(zeros)) != 0
33 || memcmp(ref->y, zeros, sizeof(zeros)) != 0) {
34 error_print();
35 return -1;
36 }
37
38 if (sm2_point_from_xy(&point, ref->x + ECCref_MAX_LEN - 32, ref->y + ECCref_MAX_LEN - 32) != 1
39 || sm2_key_set_public_key(sm2_key, &point) != 1) {
40 error_print();
41 return -1;
42 }
43 return SDR_OK;
44 }
45
SDF_ECCSignature_to_SM2_SIGNATURE(const ECCSignature * ref,SM2_SIGNATURE * sig)46 static int SDF_ECCSignature_to_SM2_SIGNATURE(const ECCSignature *ref, SM2_SIGNATURE *sig)
47 {
48 if (memcmp(ref->r, zeros, sizeof(zeros)) != 0
49 || memcmp(ref->s, zeros, sizeof(zeros)) != 0) {
50 error_print();
51 return -1;
52 }
53 memset(sig, 0, sizeof(SM2_SIGNATURE));
54 memcpy(sig->r, ref->r + ECCref_MAX_LEN - 32, 32);
55 memcpy(sig->s, ref->s + ECCref_MAX_LEN - 32, 32);
56 return SDR_OK;
57 }
58
sdf_load_library(const char * so_path,const char * vendor)59 int sdf_load_library(const char *so_path, const char *vendor)
60 {
61 if (SDF_LoadLibrary((char *)so_path, (char *)vendor) != SDR_OK) {
62 error_print();
63 return -1;
64 }
65 return 1;
66 }
67
sdf_unload_library(void)68 void sdf_unload_library(void)
69 {
70 SDF_UnloadLibrary();
71 }
72
sdf_open_device(SDF_DEVICE * dev)73 int sdf_open_device(SDF_DEVICE *dev)
74 {
75 int ret = -1;
76 void *hDevice = NULL;
77 void *hSession = NULL;
78 DEVICEINFO devInfo;
79
80 if (SDF_OpenDevice(&hDevice) != SDR_OK
81 || SDF_OpenSession(hDevice, &hSession) != SDR_OK
82 || SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
83 error_print();
84 goto end;
85 }
86
87 memset(dev, 0, sizeof(SDF_DEVICE));
88 dev->handle = hDevice;
89 hDevice = NULL;
90 memcpy(dev->issuer, devInfo.IssuerName, 40);
91 memcpy(dev->name, devInfo.DeviceName, 16);
92 memcpy(dev->serial, devInfo.DeviceSerial, 16);
93 ret = 1;
94 end:
95 if (hSession) SDF_CloseSession(hSession);
96 if (hDevice) SDF_CloseDevice(hDevice);
97 return ret;
98 }
99
sdf_print_device_info(FILE * fp,int fmt,int ind,const char * lable,SDF_DEVICE * dev)100 int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev)
101 {
102 int ret = -1;
103 void *hSession = NULL;
104 DEVICEINFO devInfo;
105
106 if (SDF_OpenSession(dev->handle, hSession) != SDR_OK
107 || SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
108 error_print();
109 goto end;
110 }
111 SDF_PrintDeviceInfo(fp, &devInfo);
112 ret = 1;
113 end:
114 if (hSession) SDF_CloseSession(hSession);
115 return ret;
116 }
117
sdf_rand_bytes(SDF_DEVICE * dev,uint8_t * buf,size_t len)118 int sdf_rand_bytes(SDF_DEVICE *dev, uint8_t *buf, size_t len)
119 {
120 int ret = -1;
121 void *hSession = NULL;
122
123 if (!dev || !buf || !len) {
124 error_print();
125 return -1;
126 }
127 if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
128 || SDF_GenerateRandom(hSession, len, buf) != SDR_OK) {
129 error_print();
130 goto end;
131 }
132 ret = 1;
133 end:
134 if (hSession) SDF_CloseSession(hSession);
135 return ret;
136 }
137
sdf_load_sign_key(SDF_DEVICE * dev,SDF_KEY * key,int index,const char * pass)138 int sdf_load_sign_key(SDF_DEVICE *dev, SDF_KEY *key, int index, const char *pass)
139 {
140 int ret = -1;
141 void *hSession = NULL;
142 ECCrefPublicKey eccPublicKey;
143 SM2_KEY public_key;
144
145 if (!dev || !key || !pass) {
146 error_print();
147 return -1;
148 }
149 if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
150 || SDF_ExportSignPublicKey_ECC(hSession, index, &eccPublicKey) != SDR_OK
151 || SDF_ECCrefPublicKey_to_SM2_KEY(&eccPublicKey, &public_key) != SDR_OK
152 || SDF_GetPrivateKeyAccessRight(hSession, index, (unsigned char *)pass, strlen(pass)) != SDR_OK) {
153 error_print();
154 goto end;
155 }
156
157 memset(key, 0, sizeof(SDF_KEY));
158 key->public_key = public_key;
159 key->session = hSession;
160 key->index = index;
161 hSession = NULL;
162 ret = 1;
163 end:
164 if (hSession) SDF_CloseSession(hSession);
165 return ret;
166 }
167
sdf_sign(SDF_KEY * key,const uint8_t dgst[32],uint8_t * sig,size_t * siglen)168 int sdf_sign(SDF_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen)
169 {
170 ECCSignature ecc_sig;
171 SM2_SIGNATURE sm2_sig;
172
173 if (!key || !dgst || !sig || !siglen) {
174 error_print();
175 return -1;
176 }
177 if (SDF_InternalSign_ECC(key->session, key->index, (unsigned char *)dgst, 32, &ecc_sig) != SDR_OK
178 || SDF_ECCSignature_to_SM2_SIGNATURE(&ecc_sig, &sm2_sig) != SDR_OK) {
179 error_print();
180 return -1;
181 }
182
183
184
185
186 *siglen = 0;
187 if (sm2_signature_to_der(&sm2_sig, &sig, siglen) != 1) {
188 error_print();
189 return -1;
190 }
191 return 1;
192 }
193
sdf_release_key(SDF_KEY * key)194 int sdf_release_key(SDF_KEY *key)
195 {
196 if (SDF_ReleasePrivateKeyAccessRight(key->session, key->index) != SDR_OK
197 || SDF_CloseSession(key->session) != SDR_OK) {
198 error_print();
199 return -1;
200 }
201 memset(key, 0, sizeof(SDF_KEY));
202 return 1;
203 }
204
sdf_close_device(SDF_DEVICE * dev)205 int sdf_close_device(SDF_DEVICE *dev)
206 {
207 if (SDF_CloseDevice(dev->handle) != SDR_OK) {
208 error_print();
209 return -1;
210 }
211 memset(dev, 0, sizeof(SDF_DEVICE));
212 return 1;
213 }
214