/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, const gnutls_datum_t *ukm, |
45 | | const gnutls_datum_t *cek, gnutls_datum_t *enc, |
46 | | gnutls_datum_t *imit) |
47 | 0 | { |
48 | 0 | const struct gost28147_param *gp; |
49 | |
|
50 | 0 | gp = _gnutls_gost_get_param(gost_params); |
51 | 0 | if (gp == NULL) { |
52 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
53 | 0 | } |
54 | | |
55 | 0 | if (kek->size != GOST28147_KEY_SIZE || |
56 | 0 | cek->size != GOST28147_KEY_SIZE || |
57 | 0 | ukm->size < GOST28147_IMIT_BLOCK_SIZE) { |
58 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
59 | 0 | } |
60 | | |
61 | 0 | enc->size = GOST28147_KEY_SIZE; |
62 | 0 | enc->data = gnutls_malloc(enc->size); |
63 | 0 | if (enc->data == NULL) { |
64 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
65 | 0 | } |
66 | | |
67 | 0 | imit->size = GOST28147_IMIT_DIGEST_SIZE; |
68 | 0 | imit->data = gnutls_malloc(imit->size); |
69 | 0 | if (imit->data == NULL) { |
70 | 0 | _gnutls_free_datum(enc); |
71 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
72 | 0 | } |
73 | | |
74 | 0 | gost28147_key_wrap_cryptopro(gp, kek->data, ukm->data, ukm->size, |
75 | 0 | cek->data, enc->data, imit->data); |
76 | |
|
77 | 0 | return 0; |
78 | 0 | } |
79 | | |
80 | | int _gnutls_gost_key_unwrap(gnutls_gost_paramset_t gost_params, |
81 | | const gnutls_datum_t *kek, |
82 | | const gnutls_datum_t *ukm, |
83 | | const gnutls_datum_t *enc, |
84 | | const gnutls_datum_t *imit, gnutls_datum_t *cek) |
85 | 0 | { |
86 | 0 | const struct gost28147_param *gp; |
87 | 0 | int ret; |
88 | |
|
89 | 0 | gp = _gnutls_gost_get_param(gost_params); |
90 | 0 | if (gp == NULL) { |
91 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
92 | 0 | } |
93 | | |
94 | 0 | if (kek->size != GOST28147_KEY_SIZE || |
95 | 0 | enc->size != GOST28147_KEY_SIZE || |
96 | 0 | imit->size != GOST28147_IMIT_DIGEST_SIZE || |
97 | 0 | ukm->size < GOST28147_IMIT_BLOCK_SIZE) { |
98 | 0 | return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER); |
99 | 0 | } |
100 | | |
101 | 0 | cek->size = GOST28147_KEY_SIZE; |
102 | 0 | cek->data = gnutls_malloc(cek->size); |
103 | 0 | if (cek->data == NULL) { |
104 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
105 | 0 | } |
106 | | |
107 | 0 | ret = gost28147_key_unwrap_cryptopro(gp, kek->data, ukm->data, |
108 | 0 | ukm->size, enc->data, imit->data, |
109 | 0 | cek->data); |
110 | 0 | if (ret == 0) { |
111 | 0 | gnutls_assert(); |
112 | 0 | _gnutls_free_temp_key_datum(cek); |
113 | 0 | return GNUTLS_E_DECRYPTION_FAILED; |
114 | 0 | } |
115 | | |
116 | 0 | return 0; |
117 | 0 | } |