2 * Copyright (c) 2019 Dmitry Belyavskiy <beldmit@gmail.com>
3 * Copyright (c) 2020 Vitaly Chikunov <vt@altlinux.org>
5 * Contents licensed under the terms of the OpenSSL license
6 * See https://www.openssl.org/source/license.html for details
10 #include <openssl/evp.h>
11 #include <openssl/hmac.h>
12 #include <openssl/buffer.h>
15 #include "e_gost_err.h"
17 static uint32_t be32(uint32_t host)
20 return (host & 0xff000000) >> 24 |
21 (host & 0x00ff0000) >> 8 |
22 (host & 0x0000ff00) << 8 |
23 (host & 0x000000ff) << 24;
29 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
31 * Function expects that out is a preallocated buffer of length
32 * defined as sum of shared_len and mac length defined by mac_nid
34 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
35 int cipher_nid, const unsigned char *cipher_key,
36 int mac_nid, unsigned char *mac_key,
37 const unsigned char *iv, const size_t ivlen,
38 unsigned char *out, int *out_len)
40 unsigned char iv_full[16], mac_buf[16];
43 EVP_CIPHER_CTX *ciph = NULL;
44 EVP_MD_CTX *mac = NULL;
49 mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
50 (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
53 GOSTerr(GOST_F_GOST_KEXP15, GOST_R_INVALID_CIPHER);
57 if (shared_len + mac_len > (unsigned int)(*out_len)) {
58 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
62 /* we expect IV of half length */
63 memset(iv_full, 0, 16);
64 memcpy(iv_full, iv, ivlen);
66 mac = EVP_MD_CTX_new();
68 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
72 if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
73 || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
74 || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
75 || EVP_DigestUpdate(mac, iv, ivlen) <= 0
76 || EVP_DigestUpdate(mac, shared_key, shared_len) <= 0
77 /* As we set MAC length directly, we should not allow overwriting it */
78 || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
79 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
83 ciph = EVP_CIPHER_CTX_new();
85 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
90 (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 1) <= 0
91 || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 1) <= 0
92 || EVP_CipherUpdate(ciph, out, &len, shared_key, shared_len) <= 0
93 || EVP_CipherUpdate(ciph, out + shared_len, &len, mac_buf, mac_len) <= 0
94 || EVP_CipherFinal_ex(ciph, out + shared_len + len, out_len) <= 0) {
95 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
99 *out_len = shared_len + mac_len;
104 OPENSSL_cleanse(mac_buf, mac_len);
105 EVP_MD_CTX_free(mac);
106 EVP_CIPHER_CTX_free(ciph);
112 * Function expects that shared_key is a preallocated buffer
113 * with length defined as expkeylen + mac_len defined by mac_nid
115 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
116 int cipher_nid, const unsigned char *cipher_key,
117 int mac_nid, unsigned char *mac_key,
118 const unsigned char *iv, const size_t ivlen,
119 unsigned char *shared_key)
121 unsigned char iv_full[16], out[48], mac_buf[16];
122 unsigned int mac_len;
123 const size_t shared_len = 32;
125 EVP_CIPHER_CTX *ciph = NULL;
126 EVP_MD_CTX *mac = NULL;
131 mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
132 (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
135 GOSTerr(GOST_F_GOST_KIMP15, GOST_R_INVALID_CIPHER);
139 if (expkeylen > sizeof(out)) {
140 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
145 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
149 /* we expect IV of half length */
150 memset(iv_full, 0, 16);
151 memcpy(iv_full, iv, ivlen);
153 ciph = EVP_CIPHER_CTX_new();
155 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
159 if (EVP_CipherInit_ex
160 (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 0) <= 0
161 || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 0) <= 0
162 || EVP_CipherUpdate(ciph, out, &len, expkey, expkeylen) <= 0
163 || EVP_CipherFinal_ex(ciph, out + len, &len) <= 0) {
164 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
167 /*Now we have shared key and mac in out[] */
169 mac = EVP_MD_CTX_new();
171 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
175 if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
176 || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
177 || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
178 || EVP_DigestUpdate(mac, iv, ivlen) <= 0
179 || EVP_DigestUpdate(mac, out, shared_len) <= 0
180 /* As we set MAC length directly, we should not allow overwriting it */
181 || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
182 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
186 if (CRYPTO_memcmp(mac_buf, out + shared_len, mac_len) != 0) {
187 GOSTerr(GOST_F_GOST_KIMP15, GOST_R_BAD_MAC);
191 memcpy(shared_key, out, shared_len);
195 OPENSSL_cleanse(out, sizeof(out));
196 EVP_MD_CTX_free(mac);
197 EVP_CIPHER_CTX_free(ciph);
201 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
202 const unsigned char *key, size_t keylen,
203 const unsigned char *label, size_t label_len,
204 const unsigned char *seed, size_t seed_len,
205 const size_t representation)
208 unsigned char zero = 0;
209 unsigned char *ptr = keyout;
211 unsigned char *len_ptr = NULL;
212 uint32_t len_repr = be32(keyout_len * 8);
213 size_t len_repr_len = 4;
215 ctx = HMAC_CTX_new();
217 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
221 if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
222 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
225 iters = keyout_len / 32;
227 len_ptr = (unsigned char *)&len_repr;
228 while (*len_ptr == 0) {
233 for (i = 1; i <= iters; i++) {
234 uint32_t iter_net = be32(i);
235 unsigned char *rep_ptr =
236 ((unsigned char *)&iter_net) + (4 - representation);
238 if (HMAC_Init_ex(ctx, key, keylen,
239 EVP_get_digestbynid(NID_id_GostR3411_2012_256),
241 || HMAC_Update(ctx, rep_ptr, representation) <= 0
242 || HMAC_Update(ctx, label, label_len) <= 0
243 || HMAC_Update(ctx, &zero, 1) <= 0
244 || HMAC_Update(ctx, seed, seed_len) <= 0
245 || HMAC_Update(ctx, len_ptr, len_repr_len) <= 0
246 || HMAC_Final(ctx, ptr, NULL) <= 0) {
247 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
261 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
262 const unsigned char *tlsseq)
264 uint64_t gh_c1 = 0x00000000FFFFFFFF, gh_c2 = 0x0000F8FFFFFFFFFF,
265 gh_c3 = 0xC0FFFFFFFFFFFFFF;
266 uint64_t mg_c1 = 0x00000000C0FFFFFF, mg_c2 = 0x000000FEFFFFFFFF,
267 mg_c3 = 0x00F0FFFFFFFFFFFF;
269 uint64_t seed1, seed2, seed3;
271 unsigned char ko1[32], ko2[32];
273 switch (cipher_nid) {
279 case NID_grasshopper_cbc:
288 BUF_reverse((unsigned char *)&seq, tlsseq, 8);
290 memcpy(&seq, tlsseq, 8);
296 if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
297 (const unsigned char *)&seed1, 8, 1) <= 0
298 || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
299 (const unsigned char *)&seed2, 8, 1) <= 0
300 || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
301 (const unsigned char *)&seed3, 8, 1) <= 0)
307 #define GOST_WRAP_FLAGS EVP_CIPH_CTRL_INIT | EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1
309 #define MAGMA_MAC_WRAP_LEN 8
310 #define KUZNYECHIK_MAC_WRAP_LEN 16
311 #define MAX_MAC_WRAP_LEN KUZNYECHIK_MAC_WRAP_LEN
312 #define GOSTKEYLEN 32
313 #define MAGMA_WRAPPED_KEY_LEN GOSTKEYLEN + MAGMA_MAC_WRAP_LEN
314 #define KUZNYECHIK_WRAPPED_KEY_LEN GOSTKEYLEN + KUZNYECHIK_MAC_WRAP_LEN
315 #define MAX_WRAPPED_KEY_LEN KUZNYECHIK_WRAPPED_KEY_LEN
318 unsigned char iv[8]; /* Max IV size is half of base cipher block length */
319 unsigned char key[GOSTKEYLEN*2]; /* Combined cipher and mac keys */
320 unsigned char wrapped[MAX_WRAPPED_KEY_LEN]; /* Max size */
324 static int magma_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
325 const unsigned char *iv, int enc)
327 GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
328 memset(cctx->wrapped, 0, MAX_WRAPPED_KEY_LEN);
329 cctx->wrap_count = 0;
332 memset(cctx->iv, 0, 8);
333 memcpy(cctx->iv, iv, 4);
337 memcpy(cctx->key, key, GOSTKEYLEN*2);
342 static int magma_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
343 const unsigned char *in, size_t inl)
345 GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
346 int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
351 if (inl <= MAGMA_WRAPPED_KEY_LEN) {
352 if (cctx->wrap_count + inl > MAGMA_WRAPPED_KEY_LEN)
355 if (cctx->wrap_count + inl <= MAGMA_WRAPPED_KEY_LEN)
357 memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
358 cctx->wrap_count += inl;
362 if (cctx->wrap_count < MAGMA_WRAPPED_KEY_LEN)
367 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
368 cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
372 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_magma_ctr,
373 cctx->key+GOSTKEYLEN, NID_magma_mac, cctx->key, cctx->iv, 4, out) > 0 ? GOSTKEYLEN : 0;
377 static int kuznyechik_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
378 const unsigned char *iv, int enc)
380 GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
381 memset(cctx->wrapped, 0, KUZNYECHIK_WRAPPED_KEY_LEN);
382 cctx->wrap_count = 0;
385 memset(cctx->iv, 0, 8);
386 memcpy(cctx->iv, iv, 8);
390 memcpy(cctx->key, key, GOSTKEYLEN*2);
395 static int kuznyechik_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
396 const unsigned char *in, size_t inl)
398 GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
399 int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
404 if (inl <= KUZNYECHIK_WRAPPED_KEY_LEN) {
405 if (cctx->wrap_count + inl > KUZNYECHIK_WRAPPED_KEY_LEN)
408 if (cctx->wrap_count + inl <= KUZNYECHIK_WRAPPED_KEY_LEN)
410 memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
411 cctx->wrap_count += inl;
415 if (cctx->wrap_count < KUZNYECHIK_WRAPPED_KEY_LEN)
420 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
421 cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
425 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_kuznyechik_ctr,
426 cctx->key+GOSTKEYLEN, NID_kuznyechik_mac, cctx->key, cctx->iv, 8, out) > 0 ? GOSTKEYLEN : 0;
430 static int wrap_ctrl (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
435 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
442 static GOST_cipher wrap_template_cipher = {
443 .key_len = GOSTKEYLEN * 2,
444 .flags = GOST_WRAP_FLAGS,
445 .ctx_size = sizeof(GOST_WRAP_CTX),
449 GOST_cipher magma_kexp15_cipher = {
450 .template = &wrap_template_cipher,
451 .nid = NID_magma_kexp15,
454 .init = magma_wrap_init,
455 .do_cipher = magma_wrap_do,
458 GOST_cipher kuznyechik_kexp15_cipher = {
459 .template = &wrap_template_cipher,
460 .nid = NID_kuznyechik_kexp15,
463 .init = kuznyechik_wrap_init,
464 .do_cipher = kuznyechik_wrap_do,
466 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */