- fixed gmp initialization bugs
[strongswan.git] / Source / charon / transforms / rsa / rsa_private_key.c
1 /**
2 * @file rsa_private_key.c
3 *
4 * @brief Implementation of rsa_private_key_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <gmp.h>
24
25 #include "rsa_private_key.h"
26
27 #include <daemon.h>
28 #include <utils/allocator.h>
29
30
31 /*
32 * Oids for hash algorithms are defined in
33 * rsa_public_key.c.
34 */
35 extern u_int8_t md2_oid[18];
36 extern u_int8_t md5_oid[18];
37 extern u_int8_t sha1_oid[15];
38 extern u_int8_t sha256_oid[19];
39 extern u_int8_t sha384_oid[19];
40 extern u_int8_t sha512_oid[19];
41
42 /**
43 * Public exponent to use for key generation.
44 */
45 #define PUBLIC_EXPONENT 0x10001
46
47
48 typedef struct private_rsa_private_key_t private_rsa_private_key_t;
49
50 /**
51 * Private data of a rsa_private_key_t object.
52 */
53 struct private_rsa_private_key_t {
54 /**
55 * Public interface for this signer.
56 */
57 rsa_private_key_t public;
58
59 /**
60 * Is the key already set ?
61 */
62 bool is_key_set;
63
64 /**
65 * Public modulus.
66 */
67 mpz_t n;
68
69 /**
70 * Public exponent.
71 */
72 mpz_t e;
73
74 /**
75 * Private prime 1.
76 */
77 mpz_t p;
78
79 /**
80 * Private Prime 2.
81 */
82 mpz_t q;
83
84 /**
85 * Private exponent.
86 */
87 mpz_t d;
88
89 /**
90 * Private exponent 1.
91 */
92 mpz_t exp1;
93
94 /**
95 * Private exponent 2.
96 */
97 mpz_t exp2;
98
99 /**
100 * Private coefficient.
101 */
102 mpz_t coeff;
103
104 /**
105 * Keysize in bytes.
106 */
107 size_t k;
108
109 /**
110 * @brief Implements the RSADP algorithm specified in PKCS#1.
111 *
112 * @param this calling object
113 * @param data data to process
114 * @return processed data
115 */
116 chunk_t (*rsadp) (private_rsa_private_key_t *this, chunk_t data);
117
118 /**
119 * @brief Implements the RSASP1 algorithm specified in PKCS#1.
120 * @param this calling object
121 * @param data data to process
122 * @return processed data
123 */
124 chunk_t (*rsasp1) (private_rsa_private_key_t *this, chunk_t data);
125
126 };
127
128 /**
129 * Implementation of private_rsa_private_key_t.rsadp and private_rsa_private_key_t.rsasp1.
130 */
131 static chunk_t rsadp(private_rsa_private_key_t *this, chunk_t data)
132 {
133 mpz_t t1, t2;
134 chunk_t decrypted;
135
136 mpz_init(t1);
137 mpz_init(t2);
138
139 mpz_import(t1, data.len, 1, 1, 1, 0, data.ptr);
140
141 mpz_powm(t2, t1, this->exp1, this->p); /* m1 = c^dP mod p */
142 mpz_powm(t1, t1, this->exp2, this->q); /* m2 = c^dQ mod Q */
143 mpz_sub(t2, t2, t1); /* h = qInv (m1 - m2) mod p */
144 mpz_mod(t2, t2, this->p);
145 mpz_mul(t2, t2, this->coeff);
146 mpz_mod(t2, t2, this->p);
147
148 mpz_mul(t2, t2, this->q); /* m = m2 + h q */
149 mpz_add(t1, t1, t2);
150
151 decrypted.len = this->k;
152 decrypted.ptr = mpz_export(NULL, NULL, 1, decrypted.len, 1, 0, t1);
153
154 mpz_clear(t1);
155 mpz_clear(t2);
156
157 return decrypted;
158 }
159
160 /**
161 * Implementation of rsa_private_key.build_emsa_signature.
162 */
163 static status_t build_emsa_pkcs1_signature(private_rsa_private_key_t *this, hash_algorithm_t hash_algorithm, chunk_t data, chunk_t *signature)
164 {
165 hasher_t *hasher;
166 chunk_t hash;
167 chunk_t oid;
168 chunk_t em;
169
170 /* get oid string prepended to hash */
171 switch (hash_algorithm)
172 {
173 case HASH_MD2:
174 {
175 oid.ptr = md2_oid;
176 oid.len = sizeof(md2_oid);
177 break;
178 }
179 case HASH_MD5:
180 {
181 oid.ptr = md5_oid;
182 oid.len = sizeof(md5_oid);
183 break;
184 }
185 case HASH_SHA1:
186 {
187 oid.ptr = sha1_oid;
188 oid.len = sizeof(sha1_oid);
189 break;
190 }
191 case HASH_SHA256:
192 {
193 oid.ptr = sha256_oid;
194 oid.len = sizeof(sha256_oid);
195 break;
196 }
197 case HASH_SHA384:
198 {
199 oid.ptr = sha384_oid;
200 oid.len = sizeof(sha384_oid);
201 break;
202 }
203 case HASH_SHA512:
204 {
205 oid.ptr = sha512_oid;
206 oid.len = sizeof(sha512_oid);
207 break;
208 }
209 default:
210 {
211 return NOT_SUPPORTED;
212 }
213 }
214
215 /* get hasher */
216 hasher = hasher_create(hash_algorithm);
217 if (hasher == NULL)
218 {
219 return NOT_SUPPORTED;
220 }
221
222 /* build hash */
223 hasher->allocate_hash(hasher, data, &hash);
224 hasher->destroy(hasher);
225
226 /* build chunk to rsa-decrypt:
227 * EM = 0x00 || 0x01 || PS || 0x00 || T.
228 * PS = 0xFF padding, with length to fill em
229 * T = oid || hash
230 */
231 em.len = this->k;
232 em.ptr = allocator_alloc(em.len);
233
234 /* fill em with padding */
235 memset(em.ptr, 0xFF, em.len);
236 /* set magic bytes */
237 *(em.ptr) = 0x00;
238 *(em.ptr+1) = 0x01;
239 *(em.ptr + em.len - hash.len - oid.len - 1) = 0x00;
240 /* set hash */
241 memcpy(em.ptr + em.len - hash.len, hash.ptr, hash.len);
242 /* set oid */
243 memcpy(em.ptr + em.len - hash.len - oid.len, oid.ptr, oid.len);
244
245
246 /* build signature */
247 *signature = this->rsasp1(this, em);
248
249 allocator_free(hash.ptr);
250 allocator_free(em.ptr);
251
252 return SUCCESS;
253 }
254
255
256 /**
257 * Implementation of rsa_private_key.set_key.
258 */
259 static status_t set_key(private_rsa_private_key_t *this, chunk_t key)
260 {
261 chunk_t n, e, p, q, d, exp1, exp2, coeff;
262 this->k = key.len / 8;
263
264 n.len = this->k;
265 e.len = this->k;
266 p.len = this->k;
267 q.len = this->k;
268 d.len = this->k;
269 exp1.len = this->k;
270 exp2.len = this->k;
271 coeff.len = this->k;
272
273 n.ptr = key.ptr + this->k * 0;
274 e.ptr = key.ptr + this->k * 1;
275 p.ptr = key.ptr + this->k * 2;
276 q.ptr = key.ptr + this->k * 3;
277 d.ptr = key.ptr + this->k * 4;
278 exp1.ptr = key.ptr + this->k * 5;
279 exp2.ptr = key.ptr + this->k * 6;
280 coeff.ptr = key.ptr + this->k * 7;
281
282 mpz_init(this->n);
283 mpz_init(this->e);
284 mpz_init(this->p);
285 mpz_init(this->q);
286 mpz_init(this->d);
287 mpz_init(this->exp1);
288 mpz_init(this->exp2);
289 mpz_init(this->coeff);
290
291 mpz_import(this->n, this->k, 1, 1, 1, 0, n.ptr);
292 mpz_import(this->e, this->k, 1, 1, 1, 0, e.ptr);
293 mpz_import(this->p, this->k, 1, 1, 1, 0, p.ptr);
294 mpz_import(this->q, this->k, 1, 1, 1, 0, q.ptr);
295 mpz_import(this->d, this->k, 1, 1, 1, 0, d.ptr);
296 mpz_import(this->exp1, this->k, 1, 1, 1, 0, exp1.ptr);
297 mpz_import(this->exp2, this->k, 1, 1, 1, 0, exp2.ptr);
298 mpz_import(this->coeff, this->k, 1, 1, 1, 0, coeff.ptr);
299
300 this->is_key_set = TRUE;
301
302 return SUCCESS;
303
304 }
305
306 /**
307 * Implementation of rsa_private_key.get_key.
308 */
309 static status_t get_key(private_rsa_private_key_t *this, chunk_t *key)
310 {
311 if (!this->is_key_set)
312 {
313 return INVALID_STATE;
314 }
315
316 chunk_t n, e, p, q, d, exp1, exp2, coeff;
317
318 n.len = this->k;
319 n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
320 e.len = this->k;
321 e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
322 p.len = this->k;
323 p.ptr = mpz_export(NULL, NULL, 1, p.len, 1, 0, this->p);
324 q.len = this->k;
325 q.ptr = mpz_export(NULL, NULL, 1, q.len, 1, 0, this->q);
326 d.len = this->k;
327 d.ptr = mpz_export(NULL, NULL, 1, d.len, 1, 0, this->d);
328 exp1.len = this->k;
329 exp1.ptr = mpz_export(NULL, NULL, 1, exp1.len, 1, 0, this->exp1);
330 exp2.len = this->k;
331 exp2.ptr = mpz_export(NULL, NULL, 1, exp2.len, 1, 0, this->exp2);
332 coeff.len = this->k;
333 coeff.ptr = mpz_export(NULL, NULL, 1, coeff.len, 1, 0, this->coeff);
334
335 key->len = this->k * 8;
336 key->ptr = allocator_alloc(key->len);
337 memcpy(key->ptr + this->k * 0, n.ptr , n.len);
338 memcpy(key->ptr + this->k * 1, e.ptr, e.len);
339 memcpy(key->ptr + this->k * 2, p.ptr, p.len);
340 memcpy(key->ptr + this->k * 3, q.ptr, q.len);
341 memcpy(key->ptr + this->k * 4, d.ptr, d.len);
342 memcpy(key->ptr + this->k * 5, exp1.ptr, exp1.len);
343 memcpy(key->ptr + this->k * 6, exp2.ptr, exp2.len);
344 memcpy(key->ptr + this->k * 7, coeff.ptr, coeff.len);
345
346 allocator_free(n.ptr);
347 allocator_free(e.ptr);
348 allocator_free(p.ptr);
349 allocator_free(q.ptr);
350 allocator_free(d.ptr);
351 allocator_free(exp1.ptr);
352 allocator_free(exp2.ptr);
353 allocator_free(coeff.ptr);
354
355 return SUCCESS;
356 }
357
358 /**
359 * Implementation of rsa_private_key.load_key.
360 */
361 static status_t load_key(private_rsa_private_key_t *this, char *file)
362 {
363 return NOT_SUPPORTED;
364 }
365
366 /**
367 * Implementation of rsa_private_key.save_key.
368 */
369 static status_t save_key(private_rsa_private_key_t *this, char *file)
370 {
371 return NOT_SUPPORTED;
372 }
373
374 /**
375 * Implementation of rsa_private_key.generate_key.
376 */
377 static status_t generate_key(private_rsa_private_key_t *this, size_t key_size)
378 {
379 mpz_t p, q, n, e, d, exp1, exp2, coeff;
380 mpz_t m, q1, t;
381
382 if (key_size < 0)
383 {
384 return INVALID_ARG;
385 }
386
387 if (this->is_key_set)
388 {
389 mpz_clear(this->n);
390 mpz_clear(this->e);
391 mpz_clear(this->p);
392 mpz_clear(this->q);
393 mpz_clear(this->d);
394 mpz_clear(this->exp1);
395 mpz_clear(this->exp2);
396 mpz_clear(this->coeff);
397 }
398
399 key_size = key_size / 8;
400
401 mpz_init(t);
402 mpz_init(n);
403 mpz_init(d);
404 mpz_init(exp1);
405 mpz_init(exp2);
406 mpz_init(coeff);
407
408 /* Get values of primes p and q */
409 charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &p);
410 charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &q);
411
412 /* Swapping Primes so p is larger then q */
413 if (mpz_cmp(p, q) < 0)
414 {
415 mpz_set(t, p);
416 mpz_set(p, q);
417 mpz_set(q, t);
418 }
419
420 mpz_mul(n, p, q); /* n = p*q */
421 mpz_init_set_ui(e, PUBLIC_EXPONENT); /* assign public exponent */
422 mpz_init_set(m, p); /* m = p */
423 mpz_sub_ui(m, m, 1); /* m = m -1 */
424 mpz_init_set(q1, q); /* q1 = q */
425 mpz_sub_ui(q1, q1, 1); /* q1 = q1 -1 */
426 mpz_gcd(t, m, q1); /* t = gcd(p-1, q-1) */
427 mpz_mul(m, m, q1); /* m = (p-1)*(q-1) */
428 mpz_divexact(m, m, t); /* m = m / t */
429 mpz_gcd(t, m, e); /* t = gcd(m, e) (greatest common divisor) */
430
431 mpz_invert(d, e, m); /* e has an inverse mod m */
432 if (mpz_cmp_ui(d, 0) < 0) /* make sure d is positive */
433 {
434 mpz_add(d, d, m);
435 }
436 mpz_sub_ui(t, p, 1); /* t = p-1 */
437 mpz_mod(exp1, d, t); /* exp1 = d mod p-1 */
438 mpz_sub_ui(t, q, 1); /* t = q-1 */
439 mpz_mod(exp2, d, t); /* exp2 = d mod q-1 */
440
441 mpz_invert(coeff, q, p); /* coeff = q^-1 mod p */
442 if (mpz_cmp_ui(coeff, 0) < 0) /* make coeff d is positive */
443 {
444 mpz_add(coeff, coeff, p);
445 }
446
447 mpz_clear(q1);
448 mpz_clear(m);
449 mpz_clear(t);
450
451 /* apply values */
452 *(this->p) = *p;
453 *(this->q) = *q;
454 *(this->n) = *n;
455 *(this->e) = *e;
456 *(this->d) = *d;
457 *(this->exp1) = *exp1;
458 *(this->exp2) = *exp2;
459 *(this->coeff) = *coeff;
460
461 /* set key size in bytes */
462
463 this->is_key_set = TRUE;
464 this->k = key_size;
465
466 return SUCCESS;
467 }
468
469 /**
470 * Implementation of rsa_private_key.get_public_key.
471 */
472 rsa_public_key_t *get_public_key(private_rsa_private_key_t *this)
473 {
474 rsa_public_key_t *public_key;
475 //chunk_t key;
476
477 public_key = rsa_public_key_create();
478
479 if (this->is_key_set)
480 {
481
482 chunk_t n, e, key;
483
484 n.len = this->k;
485 n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
486 e.len = this->k;
487 e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
488
489 key.len = this->k * 2;
490 key.ptr = allocator_alloc(key.len);
491 memcpy(key.ptr, n.ptr, n.len);
492 memcpy(key.ptr + n.len, e.ptr, e.len);
493 allocator_free(n.ptr);
494 allocator_free(e.ptr);
495
496 public_key->set_key(public_key, key);
497 allocator_free(key.ptr);
498
499 }
500
501 return public_key;
502 }
503
504
505 /**
506 * Implementation of rsa_private_key.destroy.
507 */
508 static void destroy(private_rsa_private_key_t *this)
509 {
510 if (this->is_key_set)
511 {
512 mpz_clear(this->n);
513 mpz_clear(this->e);
514 mpz_clear(this->p);
515 mpz_clear(this->q);
516 mpz_clear(this->d);
517 mpz_clear(this->exp1);
518 mpz_clear(this->exp2);
519 mpz_clear(this->coeff);
520 }
521 allocator_free(this);
522 }
523
524 /*
525 * Described in header.
526 */
527 rsa_private_key_t *rsa_private_key_create(hash_algorithm_t hash_algoritm)
528 {
529 private_rsa_private_key_t *this = allocator_alloc_thing(private_rsa_private_key_t);
530
531 /* public functions */
532 this->public.build_emsa_pkcs1_signature = (status_t (*) (rsa_private_key_t*,hash_algorithm_t,chunk_t,chunk_t*))build_emsa_pkcs1_signature;
533 this->public.set_key = (status_t (*) (rsa_private_key_t*,chunk_t))set_key;
534 this->public.get_key = (status_t (*) (rsa_private_key_t*,chunk_t*))get_key;
535 this->public.load_key = (status_t (*) (rsa_private_key_t*,char*))load_key;
536 this->public.save_key = (status_t (*) (rsa_private_key_t*,char*))save_key;
537 this->public.generate_key = (status_t (*) (rsa_private_key_t*,size_t))generate_key;
538 this->public.get_public_key = (rsa_public_key_t *(*) (rsa_private_key_t*))get_public_key;
539 this->public.destroy = (void (*) (rsa_private_key_t*))destroy;
540
541 /* private functions */
542 this->rsadp = rsadp;
543 this->rsasp1 = rsadp; /* same algorithm */
544
545 this->is_key_set = FALSE;
546
547 return &(this->public);
548 }