summaryrefslogtreecommitdiff
path: root/lib/nettle/gost/gost-wrap.c
blob: e4a616ed2cedfec1a93ea398c1c27cfb77e61592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* GOST 28147-89 (Magma) implementation
 *
 * Copyright: 2015, 2016 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Copyright: 2009-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnutls_int.h>

#include <string.h>

#include <nettle/macros.h>
#include "gost28147.h"
#include <nettle/cfb.h>
#include <nettle/memops.h>

void
gost28147_kdf_cryptopro(const struct gost28147_param *param,
		       const uint8_t *in,
		       const uint8_t *ukm,
		       uint8_t *out)
{
	struct gost28147_ctx ctx;
	int i;

	memcpy(out, in, GOST28147_KEY_SIZE);
	for (i = 0; i < 8; i++) {
		uint8_t mask;
		uint8_t *p;
		uint8_t iv[GOST28147_BLOCK_SIZE];
		uint32_t block[2] = {0, 0};
		uint32_t t;

		for (p = out, mask = 1; mask; mask <<= 1) {
			t = LE_READ_UINT32(p);
			p += 4;
			if (mask & ukm[i])
				block[0] += t;
			else
				block[1] += t;
		}

		LE_WRITE_UINT32(iv + 0, block[0]);
		LE_WRITE_UINT32(iv + 4, block[1]);

		gost28147_set_key(&ctx, out);
		gost28147_set_param(&ctx, param);
		cfb_encrypt(&ctx,
			    (nettle_cipher_func*)gost28147_encrypt_for_cfb,
			    GOST28147_BLOCK_SIZE, iv,
			    GOST28147_KEY_SIZE, out, out);
	}
}

void
gost28147_key_wrap_cryptopro(const struct gost28147_param *param,
			     const uint8_t *kek,
			     const uint8_t *ukm, size_t ukm_size,
			     const uint8_t *cek,
			     uint8_t *enc,
			     uint8_t *imit)
{
	uint8_t kd[GOST28147_KEY_SIZE];
	struct gost28147_ctx ctx;
	struct gost28147_imit_ctx ictx;

	assert(ukm_size >= GOST28147_IMIT_BLOCK_SIZE);

	gost28147_kdf_cryptopro(param, kek, ukm, kd);
	gost28147_set_key(&ctx, kd);
	gost28147_set_param(&ctx, param);
	gost28147_encrypt(&ctx, GOST28147_KEY_SIZE, enc, cek);

	gost28147_imit_set_key(&ictx, GOST28147_KEY_SIZE, kd);
	gost28147_imit_set_param(&ictx, param);
	gost28147_imit_set_nonce(&ictx, ukm);
	gost28147_imit_update(&ictx, GOST28147_KEY_SIZE, cek);
	gost28147_imit_digest(&ictx, GOST28147_IMIT_DIGEST_SIZE, imit);
}

int
gost28147_key_unwrap_cryptopro(const struct gost28147_param *param,
			       const uint8_t *kek,
			       const uint8_t *ukm, size_t ukm_size,
			       const uint8_t *enc,
			       const uint8_t *imit,
			       uint8_t *cek)
{
	uint8_t kd[GOST28147_KEY_SIZE];
	uint8_t mac[GOST28147_IMIT_DIGEST_SIZE];
	struct gost28147_ctx ctx;
	struct gost28147_imit_ctx ictx;

	assert(ukm_size >= GOST28147_IMIT_BLOCK_SIZE);

	gost28147_kdf_cryptopro(param, kek, ukm, kd);
	gost28147_set_key(&ctx, kd);
	gost28147_set_param(&ctx, param);
	gost28147_decrypt(&ctx, GOST28147_KEY_SIZE, cek, enc);

	gost28147_imit_set_key(&ictx, GOST28147_KEY_SIZE, kd);
	gost28147_imit_set_param(&ictx, param);
	gost28147_imit_set_nonce(&ictx, ukm);
	gost28147_imit_update(&ictx, GOST28147_KEY_SIZE, cek);
	gost28147_imit_digest(&ictx, GOST28147_IMIT_DIGEST_SIZE, mac);

	return memeql_sec(mac, imit, GOST28147_IMIT_DIGEST_SIZE);
}