Repository navigation
Expand file tree
/
Copy pathfscrypt.c
More file actions
277 lines (245 loc) · 7.53 KB
/
Copy pathfscrypt.c
File metadata and controls
277 lines (245 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*********************************************************************
* This file is a part of FItsSec2 project: Implementation of
* IEEE Std. 1609.2,
* ETSI TS 103 097 v1.3.1,
* ETSI TS 102 941 v1.3.1
* Copyright (C) 2020 Denis Filatov (denis.filatov()fillabs.com)
* This file is NOT a free or open source software and shall not me used
* in any way not explicitly authorized by the author.
*********************************************************************/
#include "fscrypt_plugin.h"
#include <e4c_lite.h>
#include <cstr.h>
#include <assert.h>
static FSCrypt* _e = NULL;
FSCRYPT_EXPORT
void FSCrypt_Register(FSCrypt* e)
{
e->_next = _e;
_e = e;
}
#ifndef FS_ECC_DEFAULT_ENGINE
#define FS_ECC_DEFAULT_ENGINE "openssl"
#endif
FSCRYPT_EXPORT
FSCrypt* FSCrypt_FindEngine(const char* name)
{
FSCrypt* e = _e;
if (name == NULL)
name = FS_ECC_DEFAULT_ENGINE;
for(; e; e=e->_next){
if(cstrequal(name, e->name))
break;
}
return e;
}
FSCRYPT_EXPORT
bool FSCrypt_InitEngine( FSCrypt* const e, const char * params)
{
if(e && e->Init){
return e->Init(e, params);
}
return true;
}
FSCRYPT_EXPORT
bool FSCrypt_DeinitEngine( FSCrypt* const e, const char * params)
{
if(e && e->Deinit){
return e->Deinit(e, params);
}
return true;
}
FSCRYPT_EXPORT
void FSKey_InitPublic (FSPublicKey * k, FSCurve curve, FSPointType pType, const uint8_t * x, const uint8_t * y)
{
k->k = NULL;
k->curve = curve;
k->point.type = pType;
k->point.x = (uint8_t*) x;
k->point.y = (uint8_t*) ((pType == FS_UNCOMPRESSED) ? y : NULL);
}
FSCRYPT_EXPORT
bool FSKey_ExportPublic (FSCrypt* e, const FSPrivateKey * pK, FSPublicKey * k)
{
return e->KeyOps->ExportPublic(e, pK, k);
}
FSCRYPT_EXPORT
void FSKey_CleanPublic (FSCrypt* e, FSPublicKey * k)
{
e->KeyOps->FreePublic(e, k);
}
FSCRYPT_EXPORT
FSPrivateKey* FSKey_ImportPrivate (FSCrypt* e, FSCurve curve, const uint8_t * data, size_t len)
{
if(data && len)
return e->KeyOps->Import(e, curve, data, len);
return NULL;
}
FSCRYPT_EXPORT
FSPrivateKey* FSKey_Generate (FSCrypt* e, FSCurve curve, FSPublicKey * k)
{
FSPrivateKey* pK = NULL;
if(!e->KeyOps->Generate(e, curve, &pK, k)) {
pK = NULL;
}
return pK;
}
FSCRYPT_EXPORT
void FSKey_FreePrivate (FSCrypt* e, FSPrivateKey* k)
{
e->KeyOps->FreePrivate(e, k);
}
FSCRYPT_EXPORT
void FS_Random(FSCrypt* e, void* ptr, size_t const len)
{
e->Random(e, ptr, len);
}
FSCRYPT_EXPORT
size_t FSKey_Derive(FSCrypt* e, const FSPublicKey* k, const FSPrivateKey* pK,
const void* salt, size_t salt_len,
void* digest, size_t digest_len)
{
#ifdef FSCRYPT_HAVE_ENCRYPTION
return e->KeyOps->Derive(e, k, pK, salt, salt_len, digest, digest_len);
#else
return 0;
#endif
}
FSCRYPT_EXPORT
bool FSKey_ReconstructPublic(FSCrypt* e, const FSPublicKey* rv, const FSPublicKey* ca, const unsigned char * hash)
{
return e->KeyOps->ReconstructPublic(e, (FSPublicKey*)rv, ca, hash);
}
FSCRYPT_EXPORT
size_t FSKey_ExportPrivate (FSCrypt* e, const FSPrivateKey * pK, uint8_t * buf)
{
return e->KeyOps->ExportPrivate(e, pK, buf);
}
/*
void FN_THROW(RuntimeException) FSEccPoint_Read(FSECPoint* p, FSCurve curve, const char** ptr, const char* end, int error)
{
uint8_t * s = (uint8_t *)(*ptr);
size_t psize = FSEccPoint_Size(curve);
if (*s < 0x80 || *s > (0x80 + FS_UNCOMPRESSED))
throw(RuntimeException, error | FSERR_ECC_POINT | FSERR_TYPE | FSERR_PARSEERROR, NULL);
p->type = (*s) & 0x3F;
p->x = s + 1;
if(p->type == FS_UNCOMPRESSED){
p->y = p->x + psize;
*ptr = (const char*)p->y + psize;
}else{
p->y = NULL;
*ptr = (const char*)p->x + psize;
}
if( (*ptr) > end){
throw(RuntimeException, error | FSERR_ECC_POINT | FSERR_NOSPACE, NULL);
}
}
void FN_THROW(RuntimeException) FSSignature_Read(FSSignature* s, const char** ptr, const char* end, int error)
{
const uint8_t * b = (const uint8_t*)*ptr;
s->curve = *(b++);
if (s->curve < 0x80 || s->curve >= (0x80 + FSCurve_Max)) {
throw(RuntimeException, error | FSERR_SIGNATURE | FSERR_PK_ALGORITHM | FSERR_PARSEERROR, NULL);
}
s->curve &= 0x3F;
if (s->curve > FS_BRAINPOOLP256R1) {
// skip extension length
size_t l = *(b++);
if ((b + l) > (const uint8_t*)end) {
throw(RuntimeException, error | FSERR_SIGNATURE | FSERR_NOSPACE, NULL);
}
}
FSEccPoint_Read(&s->point, s->curve, (const char**)&b, end, error);
s->s = (uint8_t*)b;
*ptr = (const char*)b + FSEccPoint_Size(s->curve);
}
*/
FSCRYPT_EXPORT
bool FSSignature_Sign_ex(FSCrypt* e, FSSignature * s, const FSPrivateKey* key, const uint8_t * digest, const uint8_t * k)
{
return e->SignatureOps->Sign(e, key, s, digest, k);
}
FSCRYPT_EXPORT
bool FSSignature_Sign(FSCrypt* e, FSSignature * s, const FSPrivateKey* key, const uint8_t * digest)
{
return e->SignatureOps->Sign(e, key, s, digest, NULL);
}
FSCRYPT_EXPORT
bool FSSignature_Verify(FSCrypt* e, const FSSignature * s, const FSPublicKey* pk, const uint8_t * digest)
{
return e->SignatureOps->Verify(e, pk, s, digest);
}
static const char * _sym_names[] = {
"AES128_CCM",
"SM4_CCM"
};
FSCRYPT_EXPORT
const char * FSSymm_AlgName(FSSymmAlg alg){
return (alg < arraysize(_sym_names)) ? _sym_names[alg] : "UNKNOWN";
}
FSCRYPT_EXPORT
size_t FSSymm_Encrypt(FSCrypt* e, FSSymmAlg alg,
const uint8_t* key, const uint8_t* nonce,
const uint8_t* in_buf, size_t in_size,
uint8_t* out_buf, size_t out_size)
{
#ifdef FSCRYPT_HAVE_ENCRYPTION
return e->SymmOps->Encrypt(e, alg, key, nonce, in_buf, in_size, out_buf, out_size);
#else
return 0;
#endif
}
FSCRYPT_EXPORT
size_t FSSymm_Decrypt(FSCrypt* e, FSSymmAlg alg,
const uint8_t* key, const uint8_t* nonce,
const uint8_t* in_buf, size_t in_size,
uint8_t* out_buf, size_t out_size)
{
#ifdef FSCRYPT_HAVE_ENCRYPTION
return e->SymmOps->Decrypt(e, alg, key, nonce, in_buf, in_size, out_buf, out_size);
#else
return 0;
#endif
}
FSCRYPT_EXPORT
size_t FSCrypt_MAC(FSCrypt* e, FSMAC alg, const uint8_t* data, size_t size, const uint8_t* key, size_t key_len, uint8_t* out)
{
#ifdef FSCRYPT_HAVE_ENCRYPTION
return e->MACOps->mac(e, alg, data, size, key, key_len, out);
#endif
return 0;
}
FSCRYPT_EXPORT
size_t FSHash_Calc(FSCrypt* e, FSHashAlg alg, const void* data, size_t len, uint8_t* md)
{
const FSHashOps* ops = e->HashOps;
if (ops && ops->Calc) {
return ops->Calc(e, alg, data, len, md);
}
return 0;
}
const uint8_t _h_empty_sha256[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
};
const uint8_t _h_empty_sha384[] = {
0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b
};
const uint8_t _h_empty_sm3[] = {
0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F, 0x8E, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74, 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
};
const uint8_t* _h_empty[] = {
&_h_empty_sha256[0],
&_h_empty_sha384[0],
&_h_empty_sm3[0]
};
FSCRYPT_EXPORT
const uint8_t* FSHash_EmptyString(FSHashAlg alg)
{
assert(alg < (sizeof(_h_empty) / sizeof(_h_empty[0])));
return _h_empty[alg];
}