]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_keyexpimp.c
Get rid of EVP_MD_CTRL_MAC_LEN
[openssl-gost/engine.git] / gost_keyexpimp.c
1 #include <arpa/inet.h>
2 #include <string.h>
3 #include <openssl/evp.h>
4 #include <openssl/hmac.h>
5
6 #include "gost_lcl.h"
7 #include "e_gost_err.h"
8
9 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
10 /*
11  * Function expects that out is a preallocated buffer of length
12  * defined as sum of shared_len and mac length defined by mac_nid
13  * */
14 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
15                 int cipher_nid, const unsigned char *cipher_key,
16                 int mac_nid, unsigned char *mac_key,
17                 const unsigned char *iv, const size_t ivlen,
18                 unsigned char *out, int *out_len)
19 {
20     unsigned char iv_full[16], mac_buf[16];
21     unsigned int mac_len;
22
23     EVP_CIPHER_CTX *ciph = NULL;
24     EVP_MD_CTX *mac = NULL;
25
26     int ret = 0;
27     int len;
28
29     mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
30         (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
31
32     if (mac_len == 0) {
33         GOSTerr(GOST_F_GOST_KEXP15, GOST_R_INVALID_CIPHER);
34         goto err;
35     }
36
37     /* we expect IV of half length */
38     memset(iv_full, 0, 16);
39     memcpy(iv_full, iv, ivlen);
40
41     mac = EVP_MD_CTX_new();
42     if (mac == NULL) {
43         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
44         goto err;
45     }
46
47     if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
48         || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
49         || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
50         || EVP_DigestUpdate(mac, iv, ivlen) <= 0
51         || EVP_DigestUpdate(mac, shared_key, shared_len) <= 0
52         /* As we set MAC length directly, we should not allow overwriting it */
53         || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
54         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
55         goto err;
56     }
57
58     ciph = EVP_CIPHER_CTX_new();
59     if (ciph == NULL) {
60         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
61         goto err;
62     }
63
64     if (EVP_CipherInit_ex
65         (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 1) <= 0
66         || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 1) <= 0
67         || EVP_CipherUpdate(ciph, out, &len, shared_key, shared_len) <= 0
68         || EVP_CipherUpdate(ciph, out + shared_len, &len, mac_buf, mac_len) <= 0
69         || EVP_CipherFinal_ex(ciph, out + shared_len + len, out_len) <= 0) {
70         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
71         goto err;
72     }
73
74     *out_len = shared_len + mac_len;
75
76     ret = 1;
77
78  err:
79     OPENSSL_cleanse(mac_buf, mac_len);
80     EVP_MD_CTX_free(mac);
81     EVP_CIPHER_CTX_free(ciph);
82
83     return ret;
84 }
85
86 /*
87  * Function expects that shared_key is a preallocated buffer
88  * with length defined as expkeylen + mac_len defined by mac_nid
89  * */
90 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
91                 int cipher_nid, const unsigned char *cipher_key,
92                 int mac_nid, unsigned char *mac_key,
93                 const unsigned char *iv, const size_t ivlen,
94                 unsigned char *shared_key)
95 {
96     unsigned char iv_full[16], out[48], mac_buf[16];
97     unsigned int mac_len;
98     const size_t shared_len = 32;
99
100     EVP_CIPHER_CTX *ciph = NULL;
101     EVP_MD_CTX *mac = NULL;
102
103     int ret = 0;
104     int len;
105
106     mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
107         (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
108
109     if (mac_len == 0) {
110         GOSTerr(GOST_F_GOST_KIMP15, GOST_R_INVALID_CIPHER);
111         goto err;
112     }
113
114     /* we expect IV of half length */
115     memset(iv_full, 0, 16);
116     memcpy(iv_full, iv, ivlen);
117
118     ciph = EVP_CIPHER_CTX_new();
119     if (ciph == NULL) {
120         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
121         goto err;
122     }
123
124     if (EVP_CipherInit_ex
125         (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 0) <= 0
126         || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 0) <= 0
127         || EVP_CipherUpdate(ciph, out, &len, expkey, expkeylen) <= 0
128         || EVP_CipherFinal_ex(ciph, out + len, &len) <= 0) {
129         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
130         goto err;
131     }
132     /*Now we have shared key and mac in out[] */
133
134     mac = EVP_MD_CTX_new();
135     if (mac == NULL) {
136         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
137         goto err;
138     }
139
140     if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
141         || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
142         || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
143         || EVP_DigestUpdate(mac, iv, ivlen) <= 0
144         || EVP_DigestUpdate(mac, out, shared_len) <= 0
145         /* As we set MAC length directly, we should not allow overwriting it */
146         || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
147         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
148         goto err;
149     }
150
151     if (CRYPTO_memcmp(mac_buf, out + shared_len, mac_len) != 0) {
152         GOSTerr(GOST_F_GOST_KIMP15, GOST_R_BAD_MAC);
153         goto err;
154     }
155
156     memcpy(shared_key, out, shared_len);
157     ret = 1;
158
159  err:
160     OPENSSL_cleanse(out, sizeof(out));
161     EVP_MD_CTX_free(mac);
162     EVP_CIPHER_CTX_free(ciph);
163     return ret;
164 }
165
166 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
167                          const unsigned char *key, size_t keylen,
168                          const unsigned char *label, size_t label_len,
169                          const unsigned char *seed, size_t seed_len,
170                          const size_t representation)
171 {
172     int iters, i = 0;
173     unsigned char zero = 0;
174     unsigned char *ptr = keyout;
175     HMAC_CTX *ctx = NULL;
176     unsigned char *len_ptr = NULL;
177     uint32_t len_repr = htonl(keyout_len * 8);
178     size_t len_repr_len = 4;
179
180     ctx = HMAC_CTX_new();
181     if (ctx == NULL) {
182         GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
183         return 0;
184     }
185
186     if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
187         GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
188         return 0;
189     }
190     iters = keyout_len / 32;
191
192     len_ptr = (unsigned char *)&len_repr;
193     while (*len_ptr == 0) {
194         len_ptr++;
195         len_repr_len--;
196     }
197
198     for (i = 1; i <= iters; i++) {
199         uint32_t iter_net = htonl(i);
200         unsigned char *rep_ptr =
201             ((unsigned char *)&iter_net) + (4 - representation);
202
203         if (HMAC_Init_ex(ctx, key, keylen,
204                          EVP_get_digestbynid(NID_id_GostR3411_2012_256),
205                          NULL) <= 0
206             || HMAC_Update(ctx, rep_ptr, representation) <= 0
207             || HMAC_Update(ctx, label, label_len) <= 0
208             || HMAC_Update(ctx, &zero, 1) <= 0
209             || HMAC_Update(ctx, seed, seed_len) <= 0
210             || HMAC_Update(ctx, len_ptr, len_repr_len) <= 0
211             || HMAC_Final(ctx, ptr, NULL) <= 0) {
212             GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
213             HMAC_CTX_free(ctx);
214             return 0;
215         }
216
217         HMAC_CTX_reset(ctx);
218         ptr += 32;
219     }
220
221     HMAC_CTX_free(ctx);
222
223     return 1;
224 }
225
226 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
227                  const unsigned char *tlsseq)
228 {
229 #ifndef L_ENDIAN
230     uint64_t gh_c1 = 0xFFFFFFFF00000000, gh_c2 = 0xFFFFFFFFFFF80000,
231         gh_c3 = 0xFFFFFFFFFFFFFFC0;
232     uint64_t mg_c1 = 0xFFFFFFC000000000, mg_c2 = 0xFFFFFFFFFE000000,
233         mg_c3 = 0xFFFFFFFFFFFFF000;
234 #else
235     uint64_t gh_c1 = 0x00000000FFFFFFFF, gh_c2 = 0x0000F8FFFFFFFFFF,
236         gh_c3 = 0xC0FFFFFFFFFFFFFF;
237     uint64_t mg_c1 = 0x00000000C0FFFFFF, mg_c2 = 0x000000FEFFFFFFFF,
238         mg_c3 = 0x00F0FFFFFFFFFFFF;
239 #endif
240     uint64_t c1, c2, c3;
241     uint64_t seed1, seed2, seed3;
242     uint64_t seq;
243     unsigned char ko1[32], ko2[32];
244
245     switch (cipher_nid) {
246     case NID_magma_cbc:
247         c1 = mg_c1;
248         c2 = mg_c2;
249         c3 = mg_c3;
250         break;
251     case NID_grasshopper_cbc:
252         c1 = gh_c1;
253         c2 = gh_c2;
254         c3 = gh_c3;
255         break;
256     default:
257         return 0;
258     }
259     memcpy(&seq, tlsseq, 8);
260     seed1 = seq & c1;
261     seed2 = seq & c2;
262     seed3 = seq & c3;
263
264     if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
265                          (const unsigned char *)&seed1, 8, 1) <= 0
266                           || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
267                          (const unsigned char *)&seed2, 8, 1) <= 0
268         || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
269                          (const unsigned char *)&seed3, 8, 1) <= 0)
270                         return 0;
271
272     return 1;
273 }