/src/gnutls/lib/nettle/gost_keywrap.c
Line | Count | Source (jump to first uncovered line) |
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 <https://www.gnu.org/licenses/> |
19 | | */ |
20 | | |
21 | | #include "gnutls_int.h" |
22 | | #include "gost/gost28147.h" |
23 | | |
24 | | static const struct gost28147_param |
25 | | *_gnutls_gost_get_param(gnutls_gost_paramset_t param) |
26 | 0 | { |
27 | 0 | if (param == GNUTLS_GOST_PARAMSET_TC26_Z) |
28 | 0 | return &gost28147_param_TC26_Z; |
29 | 0 | else if (param == GNUTLS_GOST_PARAMSET_CP_A) |
30 | 0 | return &gost28147_param_CryptoPro_A; |
31 | 0 | else if (param == GNUTLS_GOST_PARAMSET_CP_B) |
32 | 0 | return &gost28147_param_CryptoPro_B; |
33 | 0 | else if (param == GNUTLS_GOST_PARAMSET_CP_C) |
34 | 0 | return &gost28147_param_CryptoPro_C; |
35 | 0 | else if (param == GNUTLS_GOST_PARAMSET_CP_D) |
36 | 0 | return &gost28147_param_CryptoPro_D; |
37 | | |
38 | 0 | gnutls_assert(); |
39 | |
|
40 | 0 | return NULL; |
41 | 0 | } |
42 | | |
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, gnutls_datum_t * imit) |
48 | 0 | { |
49 | 0 | const struct gost28147_param *gp; |
50 | |
|
51 | 0 | gp = _gnutls_gost_get_param(gost_params); |
52 | 0 | if (gp == NULL) { |
53 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
54 | 0 | } |
55 | | |
56 | 0 | if (kek->size != GOST28147_KEY_SIZE || |
57 | 0 | cek->size != GOST28147_KEY_SIZE || |
58 | 0 | ukm->size < GOST28147_IMIT_BLOCK_SIZE) { |
59 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
60 | 0 | } |
61 | | |
62 | 0 | enc->size = GOST28147_KEY_SIZE; |
63 | 0 | enc->data = gnutls_malloc(enc->size); |
64 | 0 | if (enc->data == NULL) { |
65 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
66 | 0 | } |
67 | | |
68 | 0 | imit->size = GOST28147_IMIT_DIGEST_SIZE; |
69 | 0 | imit->data = gnutls_malloc(imit->size); |
70 | 0 | if (imit->data == NULL) { |
71 | 0 | _gnutls_free_datum(enc); |
72 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
73 | 0 | } |
74 | | |
75 | 0 | gost28147_key_wrap_cryptopro(gp, kek->data, ukm->data, ukm->size, |
76 | 0 | cek->data, enc->data, imit->data); |
77 | |
|
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | | int _gnutls_gost_key_unwrap(gnutls_gost_paramset_t gost_params, |
82 | | const gnutls_datum_t * kek, |
83 | | const gnutls_datum_t * ukm, |
84 | | const gnutls_datum_t * enc, |
85 | | const gnutls_datum_t * imit, gnutls_datum_t * cek) |
86 | 0 | { |
87 | 0 | const struct gost28147_param *gp; |
88 | 0 | int ret; |
89 | |
|
90 | 0 | gp = _gnutls_gost_get_param(gost_params); |
91 | 0 | if (gp == NULL) { |
92 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
93 | 0 | } |
94 | | |
95 | 0 | if (kek->size != GOST28147_KEY_SIZE || |
96 | 0 | enc->size != GOST28147_KEY_SIZE || |
97 | 0 | imit->size != GOST28147_IMIT_DIGEST_SIZE || |
98 | 0 | ukm->size < GOST28147_IMIT_BLOCK_SIZE) { |
99 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
100 | 0 | } |
101 | | |
102 | 0 | cek->size = GOST28147_KEY_SIZE; |
103 | 0 | cek->data = gnutls_malloc(cek->size); |
104 | 0 | if (cek->data == NULL) { |
105 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
106 | 0 | } |
107 | | |
108 | 0 | ret = gost28147_key_unwrap_cryptopro(gp, kek->data, |
109 | 0 | ukm->data, ukm->size, |
110 | 0 | enc->data, imit->data, cek->data); |
111 | 0 | if (ret == 0) { |
112 | 0 | gnutls_assert(); |
113 | 0 | _gnutls_free_temp_key_datum(cek); |
114 | 0 | return GNUTLS_E_DECRYPTION_FAILED; |
115 | 0 | } |
116 | | |
117 | 0 | return 0; |
118 | 0 | } |