aboutsummaryrefslogtreecommitdiff
path: root/src/providers/openssl.c
blob: 4b41f1ab58d97a7524e9535470a0cc13850a3d81 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Package: noscrypt
* File: providers/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-helpers.c"

#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	

	#define _IMPL_CRYPTO_SHA256_DIGEST			_ossl_sha256_digest	

	_IMPLSTB cstatus_t _ossl_sha256_digest(cspan_t data, sha256_t digestOut32)
	{
		cstatus_t result;
		span_t digestSpan;
		struct ossl_evp_state evpState;
		

		DEBUG_ASSERT(digestOut32 != NULL);
		DEBUG_ASSERT(ncSpanIsValidC(data));

		result = CSTATUS_FAIL;

		_overflow_check(data.size);

		ncSpanInit(&digestSpan, digestOut32, sizeof(sha256_t));

		/*
		* Allocate and initalize the context
		*/
		if (!_osslEvpInit(&evpState, EvpStateTypeDigest, OSSL_SHA256))
		{
			goto Cleanup;
		}

		if (!_osslEvpUpdate(&evpState, data))
		{
			goto Cleanup;
		}

		if (!_osslEvpFinal(&evpState, digestSpan))
		{
			goto Cleanup;
		}

		result = CSTATUS_OK;

	Cleanup:

		_osslEvpFree(&evpState);

		return result;
	}

#endif

#ifndef _IMPL_CRYPTO_SHA256_HMAC

	/* Export function */
	#define _IMPL_CRYPTO_SHA256_HMAC			_ossl_hmac_sha256	
	
	_IMPLSTB cstatus_t _ossl_hmac_sha256(cspan_t key, cspan_t data, sha256_t hmacOut32)
	{
		cstatus_t result;
		span_t digestSpan;		
		struct ossl_evp_state evpState;

		result = CSTATUS_FAIL;

		_overflow_check(key.size)
		_overflow_check(data.size)

		ncSpanInit(&digestSpan, hmacOut32, sizeof(sha256_t));

		/*
		* Allocate and initalize the context
		*/
		if (!_osslEvpInit(&evpState, EvpStateTypeMac, OSSL_HMAC))
		{
			goto Cleanup;
		}

		/*
		* To use HMAC the digest parameters must be set
		* before the context can be initialized
		*/

		evpState.params[0] = OSSL_PARAM_construct_utf8_string("digest", "sha256", 0);
		evpState.params[1] = OSSL_PARAM_construct_end();

		/*
		* PRK Data must be assigned before the hmac 
		* can be initialized
		*/

		_osslEvpSetPrk(&evpState, key);
	
		if (!_osslEvpMacInit(&evpState))
		{
			goto Cleanup;
		}

		if (!_osslEvpUpdate(&evpState, data))
		{
			goto Cleanup;
		}

		if (!_osslEvpFinal(&evpState, digestSpan))
		{
			goto Cleanup;
		}
		
		result = CSTATUS_OK;

	Cleanup:

		_osslEvpFree(&evpState);

		return result;

	}

#endif /* !_IMPL_CRYPTO_SHA256_HMAC */

#ifndef _IMPL_CRYPTO_SHA256_HKDF_EXPAND

	#define _IMPL_CRYPTO_SHA256_HKDF_EXPAND		_ossl_sha256_hkdf_expand

	static cstatus_t _ossl_hkdf_update(void* ctx, cspan_t data)
	{
		DEBUG_ASSERT(ctx != NULL);
		_overflow_check(data.size);

		return _osslEvpUpdate(
			(const struct ossl_evp_state*)ctx, 
			data
		);
	}

	static cstatus_t _ossl_hkdf_finish(void* ctx, sha256_t hmacOut32)
	{
		span_t hmacSpan;

		DEBUG_ASSERT(ctx != NULL);
		DEBUG_ASSERT(hmacOut32 != NULL);

		ncSpanInit(&hmacSpan, hmacOut32, sizeof(sha256_t));

		if (!_osslEvpFinal((const struct ossl_evp_state*)ctx, hmacSpan))
		{
			return CSTATUS_FAIL;
		}

		/* 
		* Context must be re-initalized after finalize
		* See lifecycle https://docs.openssl.org/3.0/man7/life_cycle-mac/#copyright
		*/

		return _osslEvpMacInit((const struct ossl_evp_state*)ctx);
	}
	

	_IMPLSTB cstatus_t _ossl_sha256_hkdf_expand(cspan_t prk, cspan_t info, span_t okm)
	{
		cstatus_t result;
		struct ossl_evp_state hkdfState;
		struct nc_hkdf_fn_cb_struct handler;		

		result = CSTATUS_FAIL;

		handler.update = _ossl_hkdf_update;
		handler.finish = _ossl_hkdf_finish;
	
		_overflow_check(prk.size);
		_overflow_check(info.size);
		_overflow_check(okm.size);

		/*
		* PRK Must be set before any call to MacInit
		*
		* Params must also be set for sha256 digest for mac
		*/
		_osslEvpSetPrk(&hkdfState, prk);

		hkdfState.params[0] = OSSL_PARAM_construct_utf8_string("digest", "sha256", 0);
		hkdfState.params[1] = OSSL_PARAM_construct_end();		

		if (!_osslEvpInit(&hkdfState, EvpStateTypeMac, OSSL_HMAC))
		{
			goto Cleanup;
		}

		/*
		* Silly openssl stuff. Enable hmac with sha256 using the system default
		* security provider. The one-shot flag must also be disabled (0) because
		* we need to call update multiple times.
		*/

		if (_osslEvpMacInit(&hkdfState) != CSTATUS_OK)
		{
			goto Cleanup;
		}

		DEBUG_ASSERT(EVP_MAC_CTX_get_mac_size(_osslEvpGetMacContext(&hkdfState)) == sizeof(sha256_t));

		/* Pass the library  */
		result = hkdfExpandProcess(&handler, &hkdfState, info, okm);

	Cleanup:

		_osslEvpFree(&hkdfState);

		return result;
	}

#endif /* !_IMPL_CRYPTO_SHA256_HKDF_EXPAND */

#ifndef _IMPL_CHACHA20_CRYPT

	#define _IMPL_CHACHA20_CRYPT _ossl_chacha20_crypt

	_IMPLSTB cstatus_t _ossl_cipher_core(
		const EVP_CIPHER* cipher,
		cspan_t key,
		cspan_t iv,
		cspan_t input,
		span_t output
	)
	{
		cstatus_t result;
		EVP_CIPHER_CTX* ctx;
		int tempLen, osslResult;

		DEBUG_ASSERT2(ncSpanGetSize(output) <= ncSpanGetSizeC(input), "Output buffer must be equal or larger than the input buffer");
		DEBUG_ASSERT(cipher != NULL);

		DEBUG_ASSERT((uint32_t)EVP_CIPHER_get_key_length(cipher) == ncSpanGetSizeC(key));
		DEBUG_ASSERT((uint32_t)EVP_CIPHER_iv_length(cipher) == ncSpanGetSizeC(iv));

		result = CSTATUS_FAIL;

		ctx = EVP_CIPHER_CTX_new();

		if (ctx == NULL)
		{
			goto Cleanup;
		}

		osslResult = EVP_EncryptInit_ex2(
			ctx, 
			cipher, 
			ncSpanGetOffsetC(key, 0),
			ncSpanGetOffsetC(iv, 0),
			NULL
		);

		if (!osslResult)
		{
			goto Cleanup;
		}

		osslResult = EVP_EncryptUpdate(
			ctx,
			ncSpanGetOffset(output, 0),
			&tempLen,
			ncSpanGetOffsetC(input, 0),
			ncSpanGetSizeC(input)
		);

		if (!osslResult)
		{
			goto Cleanup;
		}

		/*
		* We can't get a pointer outside the range of the 
		* output buffer
		*/
		if (((uint32_t)tempLen) < ncSpanGetSize(output))
		{
			if (!EVP_EncryptFinal_ex(ctx, ncSpanGetOffset(output, tempLen), &tempLen))
			{
				goto Cleanup;
			}
		}

		result = CSTATUS_OK;

	Cleanup:

		if (ctx) EVP_CIPHER_CTX_free(ctx);

		return result;
	}

	_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* cipher;
		uint8_t chaChaIv[CHACHA_NONCE_SIZE + 4];
		cspan_t keySpan, nonceSpan, inputSpan;
		span_t outputSpan;

		result = CSTATUS_FAIL;

		/*
		* RFC 7539 ChaCha20 requires a 16 byte initialization vector. A 
		* counter value is preprended to the nonce to make up the 16 byte 
		* size.
		*
		* The counter is always set to 0 for the nonce.
		*/

		ncCryptoSecureZero(chaChaIv, sizeof(chaChaIv));
		MEMMOV(chaChaIv + 4, nonce, CHACHA_NONCE_SIZE);

		ncSpanInitC(&keySpan, key, CHACHA_KEY_SIZE);
		ncSpanInitC(&nonceSpan, chaChaIv, sizeof(chaChaIv));
		ncSpanInitC(&inputSpan, input, dataLen);
		ncSpanInit(&outputSpan, output, dataLen);

		cipher = ossl_evp_fetch_chacha20();

		if (cipher == NULL)
		{
			goto Cleanup;
		}

		result = _ossl_cipher_core(
			cipher, 
			keySpan, 
			nonceSpan, 
			inputSpan, 
			outputSpan
		);

	Cleanup:
		
		if (cipher) EVP_CIPHER_free(cipher);

		return result;
	}

#endif

#endif	/*!OPENSSL_CRYPTO_LIB */