summaryrefslogtreecommitdiffstats
path: root/crypto/userspace/ncr-dh.c
blob: e743c4b89940f90fdaeace8023c75ada17b0a8a4 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * New driver for /dev/crypto device (aka CryptoDev)

 * Copyright (c) 2010 Katholieke Universiteit Leuven
 *
 * Author: Nikos Mavrogiannopoulos <nmav@gnutls.org>
 *
 * This file is part of linux cryptodev.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/ioctl.h>
#include <linux/mm.h>
#include <linux/ncr.h>
#include <linux/slab.h>
#include <linux/random.h>
#include <linux/uaccess.h>
#include <linux/scatterlist.h>
#include <ncr-int.h>
#include <tomcrypt.h>
#include <ncr-dh.h>

void dh_free(dh_key * key)
{
	mp_clear_multi(&key->p, &key->g, &key->x, NULL);
}

int dh_import_params(dh_key * key, uint8_t * p, size_t p_size, uint8_t * g,
		     size_t g_size)
{
	int ret;
	int err;

	if ((err =
	     mp_init_multi(&key->p, &key->g, &key->x, &key->y,
			   NULL)) != CRYPT_OK) {
		err();
		return -ENOMEM;
	}

	if ((err =
	     mp_read_unsigned_bin(&key->p, (unsigned char *) p,
				  p_size)) != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	if ((err =
	     mp_read_unsigned_bin(&key->g, (unsigned char *) g,
				  g_size)) != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	return 0;
      fail:
	mp_clear_multi(&key->p, &key->g, &key->x, &key->y, NULL);

	return ret;
}

int dh_generate_key(dh_key * key)
{
	void *buf;
	int size;
	int err, ret;

	size = mp_unsigned_bin_size(&key->p);
	if (size == 0) {
		err();
		return -EINVAL;
	}

	buf = kmalloc(size, GFP_KERNEL);
	if (buf == NULL) {
		err();
		return -ENOMEM;
	}

	get_random_bytes(buf, size);

	if ((err = mp_read_unsigned_bin(&key->x, buf, size)) != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	err = mp_mod(&key->x, &key->p, &key->x);
	if (err != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	key->type = PK_PRIVATE;

	ret = 0;
      fail:
	kfree(buf);

	return ret;

}

int dh_generate_public(dh_key * public, dh_key * private)
{
	int err, ret;

	err =
	    mp_exptmod(&private->g, &private->x, &private->p, &public->y);
	if (err != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	public->type = PK_PUBLIC;

	ret = 0;
      fail:

	return ret;
}

int dh_export(uint8_t * out, unsigned long * outlen, int type, dh_key * key)
{
	unsigned long zero = 0;
	int err;

	if (out == NULL || outlen == NULL || key == NULL) {
		err();
		return -EINVAL;
	}

	/* can we store the static header?  */
	if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
		return -EINVAL;
	}

	if (type != PK_PUBLIC && type != PK_PRIVATE) {
		return -EINVAL;
	}

	/* This encoding is different from the one in original
	 * libtomcrypt. It uses a compatible encoding with gnutls
	 * and openssl 
	 */
	if (type == PK_PRIVATE) {
		err = der_encode_sequence_multi(out, outlen,
					LTC_ASN1_SHORT_INTEGER, 1UL, &zero, 
					LTC_ASN1_INTEGER, 1UL, &key->p, 
					LTC_ASN1_INTEGER, 1UL, &key->g,
					LTC_ASN1_INTEGER, 1UL, &key->x, 
					LTC_ASN1_EOL, 0UL, NULL);
	} else {
		err = mp_unsigned_bin_size(&key->y);
		if (err > *outlen) {
			err();
			return -EOVERFLOW;
		}
		
		*outlen = err;

		err = mp_to_unsigned_bin(&key->y, out);
	}
	
	if (err != CRYPT_OK) {
		err();
		return _ncr_tomerr(err);
	}
	
	return 0;
}

int dh_import(const uint8_t * in, size_t inlen, dh_key * key)
{
	int err;
	unsigned long zero = 0;

	if (in == NULL || key == NULL) {
		err();
		return -EINVAL;
	}

	/* init key */
	if (mp_init_multi
	    (&key->p, &key->g, &key->x, &key->y, NULL) != CRYPT_OK) {
		return -ENOMEM;
	}

	/* get key type */
	if ((err = der_decode_sequence_multi(in, inlen,
				LTC_ASN1_SHORT_INTEGER, 1UL, &zero, 
				LTC_ASN1_INTEGER, 1UL, &key->p, 
				LTC_ASN1_INTEGER, 1UL, &key->g,
				LTC_ASN1_INTEGER, 1UL, &key->x,
				LTC_ASN1_EOL, 0UL, NULL)) == CRYPT_OK) {
		key->type = PK_PRIVATE;
	} else {		/* public */
		err = mp_read_unsigned_bin(&key->y, in, inlen);
		key->type = PK_PUBLIC;
	}

	if (err != CRYPT_OK) {
		goto LBL_ERR;
	}

	return 0;

      LBL_ERR:
	mp_clear_multi(&key->p, &key->g, &key->x, &key->y, NULL);
	return _ncr_tomerr(err);
}

int dh_derive_gxy(struct key_item_st* newkey, dh_key * key,
	void* pk, size_t pk_size)
{
int ret, err;
mp_int y, gxy;
	/* newkey will be a secret key with value of g^{xy}
	 */
	
	if (mp_init_multi(&y, &gxy, NULL) != CRYPT_OK) {
		err();
		return -ENOMEM;
	}
	
	if (key->type != PK_PRIVATE) {
		err();
		return -EINVAL;
	}

	if ((err=mp_read_unsigned_bin(&y, pk, pk_size)) != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}
	
	if ((err=mp_exptmod(&y, &key->x, &key->p, &gxy))!= CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}
	
	err = mp_unsigned_bin_size(&gxy);
	if (err > NCR_CIPHER_MAX_KEY_LEN) {
		err();
		ret = -EOVERFLOW;
		goto fail;
	}
	newkey->key.secret.size = err;

	err = mp_to_unsigned_bin(&gxy, newkey->key.secret.data);
	if (err != CRYPT_OK) {
		err();
		ret = _ncr_tomerr(err);
		goto fail;
	}

	newkey->type = NCR_KEY_TYPE_SECRET;

	ret = 0;
fail:
	mp_clear_multi(&y, &gxy, NULL);
	
	return ret;
}	

int ncr_pk_get_dh_size( dh_key* key)
{
int ret;
	ret = mp_count_bits(&key->p);
	if (ret <= 0) {
		err();
		return -EINVAL;
	}
	
	return ret;
}