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
|
// crypto.c --- cryptography utilities
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
// Commentary:
//
// Code:
#include "crypto.h"
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include "common.h"
static EVP_PKEY *pkey_local = NULL;
static EVP_PKEY_CTX *pkey_derive_ctx = NULL;
uint8_t local_pubkey[X25519_PUBKEY_LEN];
static EVP_MD *md_sha1 = NULL;
static EVP_MD_CTX *md_sha1_ctx = NULL;
static EVP_KDF *hkdf = NULL;
static EVP_KDF_CTX *hkdf_ctx = NULL;
static EVP_CIPHER *cipher_aes_cbc = NULL;
static EVP_CIPHER_CTX *cipher_ctx = NULL;
bool
crypto_init (void)
{
EVP_PKEY_CTX *pkey_gen_ctx =
EVP_PKEY_CTX_new_from_name (NULL, "X25519", NULL);
if (pkey_gen_ctx == NULL) goto error;
if (EVP_PKEY_keygen_init (pkey_gen_ctx) <= 0) {
EVP_PKEY_CTX_free (pkey_gen_ctx);
goto error;
}
if (EVP_PKEY_keygen (pkey_gen_ctx, &pkey_local) <= 0) {
EVP_PKEY_CTX_free (pkey_gen_ctx);
goto error;
} EVP_PKEY_CTX_free (pkey_gen_ctx);
pkey_derive_ctx = EVP_PKEY_CTX_new_from_pkey (NULL, pkey_local, NULL);
if (pkey_derive_ctx == NULL) goto error;
size_t len;
if (EVP_PKEY_get_raw_public_key (pkey_local, local_pubkey, &len) <= 0)
goto error;
if (len != X25519_PUBKEY_LEN) goto error_pkey_derive_ctx;
md_sha1 = EVP_MD_fetch (NULL, "SHA-1", NULL);
if (md_sha1 == NULL) goto error_pkey_derive_ctx;
md_sha1_ctx = EVP_MD_CTX_new ();
if (md_sha1_ctx == NULL) goto error_md_sha1;
if (!EVP_DigestInit_ex2 (md_sha1_ctx, md_sha1, NULL))
goto error_md_sha1_ctx;
hkdf = EVP_KDF_fetch (NULL, "HKDF", NULL);
if (!hkdf) goto error_md_sha1_ctx;
hkdf_ctx = EVP_KDF_CTX_new (hkdf);
if (!hkdf_ctx) goto error_hkdf;
cipher_aes_cbc = EVP_CIPHER_fetch (NULL, "AES-256-CBC", NULL);
if (cipher_aes_cbc == NULL) goto error_hkdf_ctx;
cipher_ctx = EVP_CIPHER_CTX_new ();
if (cipher_ctx == NULL) goto error_cipher_aes_cbc;
return true;
error_cipher_aes_cbc:
EVP_CIPHER_free (cipher_aes_cbc);
error_hkdf_ctx:
EVP_KDF_CTX_free (hkdf_ctx);
error_hkdf:
EVP_KDF_free (hkdf);
error_pkey_derive_ctx:
EVP_PKEY_CTX_free (pkey_derive_ctx);
error_md_sha1_ctx:
EVP_MD_CTX_free (md_sha1_ctx);
error_md_sha1:
EVP_MD_free (md_sha1);
error:
ERR_print_errors_fp (stderr);
return false;
}
ssize_t
sha1_hash (uint8_t out[SHA_DIGEST_SIZE], struct memory_view in)
{
unsigned int length;
if (EVP_DigestInit_ex2 (md_sha1_ctx, NULL, NULL) != 1) goto error;
if (EVP_DigestUpdate (md_sha1_ctx, in.data, in.size) != 1) goto error;
if (EVP_DigestFinal_ex (md_sha1_ctx, out, &length) != 1) goto error;
return length;
error:
ERR_reason_error_string (ERR_get_error ());
return -1;
}
bool
hkdf_sha256 (const uint8_t peer_pubkey[X25519_PUBKEY_LEN], aes_256_key out)
{
EVP_PKEY *peer_pkey =
EVP_PKEY_new_raw_public_key_ex (NULL, "X25519", NULL,
peer_pubkey, X25519_PUBKEY_LEN);
if (peer_pkey == NULL) goto error;
if (EVP_PKEY_derive_init (pkey_derive_ctx) <= 0)
goto error;
if (EVP_PKEY_derive_set_peer (pkey_derive_ctx, peer_pkey) <= 0)
goto error;
size_t ikm_len = 0;
if (EVP_PKEY_derive (pkey_derive_ctx, NULL, &ikm_len) <= 0) goto error;
uint8_t *ikm = OPENSSL_malloc (ikm_len);
if (!ikm) goto error;
if (EVP_PKEY_derive (pkey_derive_ctx, ikm, &ikm_len) <= 0) goto error;
EVP_PKEY_free (peer_pkey);
uint8_t salt[] = { 0xcd };
size_t salt_len = sizeof (salt);
uint8_t info[] = { 0xcd };
size_t info_len = sizeof (info);
OSSL_PARAM params[] = {
OSSL_PARAM_construct_utf8_string (OSSL_KDF_PARAM_DIGEST, "SHA256", 0),
OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_KEY, (void *) ikm, ikm_len),
OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_SALT, (void *) salt, salt_len),
OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_INFO, (void *) info, info_len),
OSSL_PARAM_construct_end (),
};
EVP_KDF_CTX_reset (hkdf_ctx);
if (EVP_KDF_derive (hkdf_ctx, out, AES_256_KEY_SIZE, params) <= 0)
goto error;
OPENSSL_free (ikm);
return true;
error:
EVP_PKEY_free (peer_pkey);
OPENSSL_free (ikm);
ERR_print_errors_fp (stderr);
return false;
}
ssize_t
aes_256_cbc_encrypt (struct memory_span out,
struct memory_view in, const aes_256_key key, bool salt)
{
ASSERT (out.size >= AES_IV_SIZE + in.size + AES_BLOCK_SIZE - 1);
uint8_t *head = out.data;
uint8_t *iv = NULL;
if (salt) {
iv = head;
if (RAND_bytes (iv, AES_IV_SIZE) != 1)
goto error;
head += AES_IV_SIZE;
}
if (!EVP_CIPHER_CTX_reset (cipher_ctx))
goto error;
if (EVP_EncryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1)
goto error;
int bytes_written = 0;
if (EVP_EncryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1)
goto error;
head += bytes_written;
if (EVP_EncryptFinal_ex (cipher_ctx, head, &bytes_written) != 1)
goto error;
head += bytes_written;
return head - out.data;
error:
ERR_print_errors_fp (stderr);
return -1;
}
ssize_t
aes_256_cbc_decrypt (struct memory_span out,
struct memory_view in, const aes_256_key key, bool salt)
{
uint8_t *head = out.data;
const uint8_t *iv = NULL;
if (salt) {
const uint8_t *iv = in.data;
in.data += AES_IV_SIZE;
in.size -= AES_IV_SIZE;
}
if (!EVP_CIPHER_CTX_reset (cipher_ctx))
goto error;
if (EVP_DecryptInit_ex2 (cipher_ctx, cipher_aes_cbc, key, iv, NULL) != 1)
goto error;
int bytes_written = 0;
if (EVP_DecryptUpdate (cipher_ctx, head, &bytes_written, in.data, in.size) != 1)
goto error;
head += bytes_written;
if (EVP_DecryptFinal_ex (cipher_ctx, head, &bytes_written) != 1)
goto error;
head += bytes_written;
return head - out.data;
error:
// ERR_print_errors_fp (stderr);
return -1;
}
// crypto.c ends here
|