1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2016 Dmitry Eremin-Solenikov
4 *
5 * This file is part of GnuTLS.
6 *
7 * The GnuTLS is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>
19 */
20
21 #include "gnutls_int.h"
22 #include "gost/gost28147.h"
23
24 static const struct gost28147_param *
_gnutls_gost_get_param(gnutls_gost_paramset_t param)25 _gnutls_gost_get_param(gnutls_gost_paramset_t param)
26 {
27 if (param == GNUTLS_GOST_PARAMSET_TC26_Z)
28 return &gost28147_param_TC26_Z;
29 else if (param == GNUTLS_GOST_PARAMSET_CP_A)
30 return &gost28147_param_CryptoPro_A;
31 else if (param == GNUTLS_GOST_PARAMSET_CP_B)
32 return &gost28147_param_CryptoPro_B;
33 else if (param == GNUTLS_GOST_PARAMSET_CP_C)
34 return &gost28147_param_CryptoPro_C;
35 else if (param == GNUTLS_GOST_PARAMSET_CP_D)
36 return &gost28147_param_CryptoPro_D;
37
38 gnutls_assert();
39
40 return NULL;
41 }
42
_gnutls_gost_key_wrap(gnutls_gost_paramset_t gost_params,const gnutls_datum_t * kek,const gnutls_datum_t * ukm,const gnutls_datum_t * cek,gnutls_datum_t * enc,gnutls_datum_t * imit)43 int _gnutls_gost_key_wrap(gnutls_gost_paramset_t gost_params,
44 const gnutls_datum_t *kek,
45 const gnutls_datum_t *ukm,
46 const gnutls_datum_t *cek,
47 gnutls_datum_t *enc,
48 gnutls_datum_t *imit)
49 {
50 const struct gost28147_param *gp;
51
52 gp = _gnutls_gost_get_param(gost_params);
53 if (gp == NULL) {
54 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
55 }
56
57 if (kek->size != GOST28147_KEY_SIZE ||
58 cek->size != GOST28147_KEY_SIZE ||
59 ukm->size < GOST28147_IMIT_BLOCK_SIZE) {
60 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
61 }
62
63 enc->size = GOST28147_KEY_SIZE;
64 enc->data = gnutls_malloc(enc->size);
65 if (enc->data == NULL) {
66 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
67 }
68
69 imit->size = GOST28147_IMIT_DIGEST_SIZE;
70 imit->data = gnutls_malloc(imit->size);
71 if (imit->data == NULL) {
72 _gnutls_free_datum(enc);
73 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
74 }
75
76 gost28147_key_wrap_cryptopro(gp, kek->data, ukm->data, ukm->size,
77 cek->data, enc->data, imit->data);
78
79 return 0;
80 }
81
_gnutls_gost_key_unwrap(gnutls_gost_paramset_t gost_params,const gnutls_datum_t * kek,const gnutls_datum_t * ukm,const gnutls_datum_t * enc,const gnutls_datum_t * imit,gnutls_datum_t * cek)82 int _gnutls_gost_key_unwrap(gnutls_gost_paramset_t gost_params,
83 const gnutls_datum_t *kek,
84 const gnutls_datum_t *ukm,
85 const gnutls_datum_t *enc,
86 const gnutls_datum_t *imit,
87 gnutls_datum_t *cek)
88 {
89 const struct gost28147_param *gp;
90 int ret;
91
92 gp = _gnutls_gost_get_param(gost_params);
93 if (gp == NULL) {
94 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
95 }
96
97 if (kek->size != GOST28147_KEY_SIZE ||
98 enc->size != GOST28147_KEY_SIZE ||
99 imit->size != GOST28147_IMIT_DIGEST_SIZE ||
100 ukm->size < GOST28147_IMIT_BLOCK_SIZE) {
101 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
102 }
103
104 cek->size = GOST28147_KEY_SIZE;
105 cek->data = gnutls_malloc(cek->size);
106 if (cek->data == NULL) {
107 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
108 }
109
110 ret = gost28147_key_unwrap_cryptopro(gp, kek->data,
111 ukm->data, ukm->size,
112 enc->data, imit->data,
113 cek->data);
114 if (ret == 0) {
115 gnutls_assert();
116 _gnutls_free_temp_key_datum(cek);
117 return GNUTLS_E_DECRYPTION_FAILED;
118 }
119
120 return 0;
121 }
122