7 #include <openssl/evp.h>
8 #include <openssl/hmac.h>
11 #include "e_gost_err.h"
13 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
15 * Function expects that out is a preallocated buffer of length
16 * defined as sum of shared_len and mac length defined by mac_nid
18 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
19 int cipher_nid, const unsigned char *cipher_key,
20 int mac_nid, unsigned char *mac_key,
21 const unsigned char *iv, const size_t ivlen,
22 unsigned char *out, int *out_len)
24 unsigned char iv_full[16], mac_buf[16];
27 EVP_CIPHER_CTX *ciph = NULL;
28 EVP_MD_CTX *mac = NULL;
33 mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
34 (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
37 GOSTerr(GOST_F_GOST_KEXP15, GOST_R_INVALID_CIPHER);
41 /* we expect IV of half length */
42 memset(iv_full, 0, 16);
43 memcpy(iv_full, iv, ivlen);
45 mac = EVP_MD_CTX_new();
47 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
51 if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
52 || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
53 || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
54 || EVP_DigestUpdate(mac, iv, ivlen) <= 0
55 || EVP_DigestUpdate(mac, shared_key, shared_len) <= 0
56 /* As we set MAC length directly, we should not allow overwriting it */
57 || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
58 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
62 ciph = EVP_CIPHER_CTX_new();
64 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
69 (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 1) <= 0
70 || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 1) <= 0
71 || EVP_CipherUpdate(ciph, out, &len, shared_key, shared_len) <= 0
72 || EVP_CipherUpdate(ciph, out + shared_len, &len, mac_buf, mac_len) <= 0
73 || EVP_CipherFinal_ex(ciph, out + shared_len + len, out_len) <= 0) {
74 GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
78 *out_len = shared_len + mac_len;
83 OPENSSL_cleanse(mac_buf, mac_len);
85 EVP_CIPHER_CTX_free(ciph);
91 * Function expects that shared_key is a preallocated buffer
92 * with length defined as expkeylen + mac_len defined by mac_nid
94 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
95 int cipher_nid, const unsigned char *cipher_key,
96 int mac_nid, unsigned char *mac_key,
97 const unsigned char *iv, const size_t ivlen,
98 unsigned char *shared_key)
100 unsigned char iv_full[16], out[48], mac_buf[16];
101 unsigned int mac_len;
102 const size_t shared_len = 32;
104 EVP_CIPHER_CTX *ciph = NULL;
105 EVP_MD_CTX *mac = NULL;
110 mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
111 (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
114 GOSTerr(GOST_F_GOST_KIMP15, GOST_R_INVALID_CIPHER);
118 /* we expect IV of half length */
119 memset(iv_full, 0, 16);
120 memcpy(iv_full, iv, ivlen);
122 ciph = EVP_CIPHER_CTX_new();
124 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
128 if (EVP_CipherInit_ex
129 (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 0) <= 0
130 || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 0) <= 0
131 || EVP_CipherUpdate(ciph, out, &len, expkey, expkeylen) <= 0
132 || EVP_CipherFinal_ex(ciph, out + len, &len) <= 0) {
133 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
136 /*Now we have shared key and mac in out[] */
138 mac = EVP_MD_CTX_new();
140 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
144 if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
145 || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
146 || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
147 || EVP_DigestUpdate(mac, iv, ivlen) <= 0
148 || EVP_DigestUpdate(mac, out, shared_len) <= 0
149 /* As we set MAC length directly, we should not allow overwriting it */
150 || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
151 GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
155 if (CRYPTO_memcmp(mac_buf, out + shared_len, mac_len) != 0) {
156 GOSTerr(GOST_F_GOST_KIMP15, GOST_R_BAD_MAC);
160 memcpy(shared_key, out, shared_len);
164 OPENSSL_cleanse(out, sizeof(out));
165 EVP_MD_CTX_free(mac);
166 EVP_CIPHER_CTX_free(ciph);
170 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
171 const unsigned char *key, size_t keylen,
172 const unsigned char *label, size_t label_len,
173 const unsigned char *seed, size_t seed_len,
174 const size_t representation)
177 unsigned char zero = 0;
178 unsigned char *ptr = keyout;
179 HMAC_CTX *ctx = NULL;
180 unsigned char *len_ptr = NULL;
181 uint32_t len_repr = htonl(keyout_len * 8);
182 size_t len_repr_len = 4;
184 ctx = HMAC_CTX_new();
186 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
190 if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
191 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
194 iters = keyout_len / 32;
196 len_ptr = (unsigned char *)&len_repr;
197 while (*len_ptr == 0) {
202 for (i = 1; i <= iters; i++) {
203 uint32_t iter_net = htonl(i);
204 unsigned char *rep_ptr =
205 ((unsigned char *)&iter_net) + (4 - representation);
207 if (HMAC_Init_ex(ctx, key, keylen,
208 EVP_get_digestbynid(NID_id_GostR3411_2012_256),
210 || HMAC_Update(ctx, rep_ptr, representation) <= 0
211 || HMAC_Update(ctx, label, label_len) <= 0
212 || HMAC_Update(ctx, &zero, 1) <= 0
213 || HMAC_Update(ctx, seed, seed_len) <= 0
214 || HMAC_Update(ctx, len_ptr, len_repr_len) <= 0
215 || HMAC_Final(ctx, ptr, NULL) <= 0) {
216 GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
230 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
231 const unsigned char *tlsseq)
234 uint64_t gh_c1 = 0xFFFFFFFF00000000, gh_c2 = 0xFFFFFFFFFFF80000,
235 gh_c3 = 0xFFFFFFFFFFFFFFC0;
236 uint64_t mg_c1 = 0xFFFFFFC000000000, mg_c2 = 0xFFFFFFFFFE000000,
237 mg_c3 = 0xFFFFFFFFFFFFF000;
239 uint64_t gh_c1 = 0x00000000FFFFFFFF, gh_c2 = 0x0000F8FFFFFFFFFF,
240 gh_c3 = 0xC0FFFFFFFFFFFFFF;
241 uint64_t mg_c1 = 0x00000000C0FFFFFF, mg_c2 = 0x000000FEFFFFFFFF,
242 mg_c3 = 0x00F0FFFFFFFFFFFF;
245 uint64_t seed1, seed2, seed3;
247 unsigned char ko1[32], ko2[32];
249 switch (cipher_nid) {
255 case NID_grasshopper_cbc:
263 memcpy(&seq, tlsseq, 8);
268 if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
269 (const unsigned char *)&seed1, 8, 1) <= 0
270 || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
271 (const unsigned char *)&seed2, 8, 1) <= 0
272 || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
273 (const unsigned char *)&seed3, 8, 1) <= 0)