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
|
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Package: noscrypt
* File: impl/openssl.c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with noscrypt. If not, see http://www.gnu.org/licenses/.
*/
/* Setup openssl */
#ifdef OPENSSL_CRYPTO_LIB
#include <openssl/crypto.h>
#define _OSSL_FAIL(x) if(!(x)) return CSTATUS_FAIL;
#ifndef _IMPL_SECURE_ZERO_MEMSET
#define _IMPL_SECURE_ZERO_MEMSET _ossl_secure_zero_memset
_IMPLSTB void _ossl_secure_zero_memset(void* ptr, size_t size)
{
_overflow_check(size)
OPENSSL_cleanse(ptr, size);
}
#endif
#ifndef _IMPL_CRYPTO_FIXED_TIME_COMPARE
#define _IMPL_CRYPTO_FIXED_TIME_COMPARE _ossl_fixed_time_compare
_IMPLSTB uint32_t _ossl_fixed_time_compare(const uint8_t* a, const uint8_t* b, uint32_t size)
{
int result;
/* Size checks are required for platforms that have integer sizes under 32bit */
_overflow_check(size)
result = CRYPTO_memcmp(a, b, size);
return (uint32_t)result;
}
#endif /* _IMPL_CRYPTO_FIXED_TIME_COMPARE */
#ifndef _IMPL_CRYPTO_SHA256_DIGEST
#include <openssl/sha.h>
#define _IMPL_CRYPTO_SHA256_DIGEST _ossl_sha256_digest
_IMPLSTB cstatus_t _ossl_sha256_digest(const cspan_t* data, sha256_t digestOut32)
{
_overflow_check(data->size)
_OSSL_FAIL(SHA256(data->data, data->size, digestOut32))
return CSTATUS_OK;
}
#endif
#ifndef _IMPL_CRYPTO_SHA256_HMAC
#include <openssl/hmac.h>
/* Export function */
#define _IMPL_CRYPTO_SHA256_HMAC _ossl_hmac_sha256
_IMPLSTB cstatus_t _ossl_hmac_sha256(const cspan_t* key, const cspan_t* data, sha256_t hmacOut32)
{
unsigned int hmacLen;
_overflow_check(key->size)
_overflow_check(data->size)
hmacLen = sizeof(sha256_t);
_OSSL_FAIL(
HMAC(
EVP_sha256(),
key->data,
key->size,
data->data,
data->size,
hmacOut32,
&hmacLen
)
)
/* digest length should match the actual digest size */
DEBUG_ASSERT(hmacLen == sizeof(sha256_t))
return CSTATUS_OK;
}
#endif /* !_IMPL_CRYPTO_SHA256_HMAC */
#ifndef _IMPL_CRYPTO_SHA256_HKDF_EXPAND
#include <openssl/hmac.h>
#define _IMPL_CRYPTO_SHA256_HKDF_EXPAND _ossl_sha256_hkdf_expand
cstatus_t _ossl_hkdf_update(void* ctx, const cspan_t* data)
{
DEBUG_ASSERT(ctx != NULL)
_overflow_check(data->size)
_OSSL_FAIL(EVP_DigestUpdate((EVP_MD_CTX*)ctx, data->data, data->size))
return CSTATUS_OK;
}
cstatus_t _ossl_hkdf_finish(void* ctx, sha256_t hmacOut32)
{
unsigned int hmacSize;
DEBUG_ASSERT(ctx != NULL)
hmacSize = sizeof(sha256_t);
_OSSL_FAIL(EVP_DigestFinal_ex((EVP_MD_CTX*)ctx, hmacOut32, &hmacSize))
/* When configured for sha256, should always be the same size in/out */
DEBUG_ASSERT(hmacSize == sizeof(sha256_t))
return CSTATUS_OK;
}
_IMPLSTB cstatus_t _ossl_sha256_hkdf_expand(const cspan_t* prk, const cspan_t* info, span_t* okm)
{
EVP_MD_CTX* ctx;
cstatus_t result;
struct nc_hkdf_fn_cb_struct handler;
result = CSTATUS_FAIL;
/*
* NOTE! Hmac reusable flag must be set to allow for multiple
* calls to the finish function without losing the context.
*/
if ((ctx = EVP_MD_CTX_create()) == NULL)
{
return CSTATUS_FAIL;
}
if (!EVP_DigestInit_ex2(ctx, EVP_sha256(), NULL))
{
goto Cleanup;
}
if (!EVP_DigestUpdate(ctx, prk->data, prk->size))
{
goto Cleanup;
}
handler.update = _ossl_hkdf_update;
handler.finish = _ossl_hkdf_finish;
result = hkdfExpandProcess(&handler, ctx, info, okm);
Cleanup:
EVP_MD_CTX_destroy(ctx);
return result;
}
#endif /* !_IMPL_CRYPTO_SHA256_HKDF_EXPAND */
#ifndef _IMPL_CHACHA20_CRYPT
#include <openssl/evp.h>
#define _IMPL_CHACHA20_CRYPT _ossl_chacha20_crypt
_IMPLSTB cstatus_t _ossl_chacha20_crypt(
const uint8_t* key,
const uint8_t* nonce,
const uint8_t* input,
uint8_t* output,
uint32_t dataLen
)
{
cstatus_t result;
EVP_CIPHER_CTX* ctx;
result = CSTATUS_FAIL;
if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
{
return CSTATUS_FAIL;
}
if (!EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, key, nonce))
{
goto Cleanup;
}
if (!EVP_EncryptUpdate(ctx, output, (int*)&dataLen, input, dataLen))
{
goto Cleanup;
}
result = CSTATUS_OK;
Cleanup:
EVP_CIPHER_CTX_free(ctx);
return result;
}
#endif
#endif /*!OPENSSL_CRYPTO_LIB */
|