60673d74609fdc84e93bc15af2cc16180e8347dd
[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_import(this->n, this->k, 1, 1, 1, 0, n.ptr);
283 mpz_import(this->e, this->k, 1, 1, 1, 0, e.ptr);
284 mpz_import(this->p, this->k, 1, 1, 1, 0, p.ptr);
285 mpz_import(this->q, this->k, 1, 1, 1, 0, q.ptr);
286 mpz_import(this->d, this->k, 1, 1, 1, 0, d.ptr);
287 mpz_import(this->exp1, this->k, 1, 1, 1, 0, exp1.ptr);
288 mpz_import(this->exp2, this->k, 1, 1, 1, 0, exp2.ptr);
289 mpz_import(this->coeff, this->k, 1, 1, 1, 0, coeff.ptr);
290
291 this->is_key_set = TRUE;
292
293 return SUCCESS;
294
295 }
296
297 /**
298 * Implementation of rsa_private_key.get_key.
299 */
300 static status_t get_key(private_rsa_private_key_t *this, chunk_t *key)
301 {
302 if (!this->is_key_set)
303 {
304 return INVALID_STATE;
305 }
306
307 chunk_t n, e, p, q, d, exp1, exp2, coeff;
308
309 n.len = this->k;
310 n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
311 e.len = this->k;
312 e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
313 p.len = this->k;
314 p.ptr = mpz_export(NULL, NULL, 1, p.len, 1, 0, this->p);
315 q.len = this->k;
316 q.ptr = mpz_export(NULL, NULL, 1, q.len, 1, 0, this->q);
317 d.len = this->k;
318 d.ptr = mpz_export(NULL, NULL, 1, d.len, 1, 0, this->d);
319 exp1.len = this->k;
320 exp1.ptr = mpz_export(NULL, NULL, 1, exp1.len, 1, 0, this->exp1);
321 exp2.len = this->k;
322 exp2.ptr = mpz_export(NULL, NULL, 1, exp2.len, 1, 0, this->exp2);
323 coeff.len = this->k;
324 coeff.ptr = mpz_export(NULL, NULL, 1, coeff.len, 1, 0, this->coeff);
325
326 key->len = this->k * 8;
327 key->ptr = allocator_alloc(key->len);
328 memcpy(key->ptr + this->k * 0, n.ptr , n.len);
329 memcpy(key->ptr + this->k * 1, e.ptr, e.len);
330 memcpy(key->ptr + this->k * 2, p.ptr, p.len);
331 memcpy(key->ptr + this->k * 3, q.ptr, q.len);
332 memcpy(key->ptr + this->k * 4, d.ptr, d.len);
333 memcpy(key->ptr + this->k * 5, exp1.ptr, exp1.len);
334 memcpy(key->ptr + this->k * 6, exp2.ptr, exp2.len);
335 memcpy(key->ptr + this->k * 7, coeff.ptr, coeff.len);
336
337 allocator_free(n.ptr);
338 allocator_free(e.ptr);
339 allocator_free(p.ptr);
340 allocator_free(q.ptr);
341 allocator_free(d.ptr);
342 allocator_free(exp1.ptr);
343 allocator_free(exp2.ptr);
344 allocator_free(coeff.ptr);
345
346 return SUCCESS;
347 }
348
349 /**
350 * Implementation of rsa_private_key.load_key.
351 */
352 static status_t load_key(private_rsa_private_key_t *this, char *file)
353 {
354 return NOT_SUPPORTED;
355 }
356
357 /**
358 * Implementation of rsa_private_key.save_key.
359 */
360 static status_t save_key(private_rsa_private_key_t *this, char *file)
361 {
362 return NOT_SUPPORTED;
363 }
364
365 /**
366 * Implementation of rsa_private_key.generate_key.
367 */
368 static status_t generate_key(private_rsa_private_key_t *this, size_t key_size)
369 {
370 mpz_t p, q, n, e, d, exp1, exp2, coeff;
371 mpz_t m, q1, t;
372
373 if (key_size < 0)
374 {
375 return INVALID_ARG;
376 }
377
378 if (this->is_key_set)
379 {
380 mpz_clear(this->n);
381 mpz_clear(this->e);
382 mpz_clear(this->p);
383 mpz_clear(this->q);
384 mpz_clear(this->d);
385 mpz_clear(this->exp1);
386 mpz_clear(this->exp2);
387 mpz_clear(this->coeff);
388 }
389
390 key_size = key_size / 8;
391
392 mpz_init(t);
393 mpz_init(n);
394 mpz_init(d);
395 mpz_init(exp1);
396 mpz_init(exp2);
397 mpz_init(coeff);
398
399 /* Get values of primes p and q */
400 charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &p);
401 charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &q);
402
403 /* Swapping Primes so p is larger then q */
404 if (mpz_cmp(p, q) < 0)
405 {
406 mpz_set(t, p);
407 mpz_set(p, q);
408 mpz_set(q, t);
409 }
410
411 mpz_mul(n, p, q); /* n = p*q */
412 mpz_init_set_ui(e, PUBLIC_EXPONENT); /* assign public exponent */
413 mpz_init_set(m, p); /* m = p */
414 mpz_sub_ui(m, m, 1); /* m = m -1 */
415 mpz_init_set(q1, q); /* q1 = q */
416 mpz_sub_ui(q1, q1, 1); /* q1 = q1 -1 */
417 mpz_gcd(t, m, q1); /* t = gcd(p-1, q-1) */
418 mpz_mul(m, m, q1); /* m = (p-1)*(q-1) */
419 mpz_divexact(m, m, t); /* m = m / t */
420 mpz_gcd(t, m, e); /* t = gcd(m, e) (greatest common divisor) */
421
422 mpz_invert(d, e, m); /* e has an inverse mod m */
423 if (mpz_cmp_ui(d, 0) < 0) /* make sure d is positive */
424 {
425 mpz_add(d, d, m);
426 }
427 mpz_sub_ui(t, p, 1); /* t = p-1 */
428 mpz_mod(exp1, d, t); /* exp1 = d mod p-1 */
429 mpz_sub_ui(t, q, 1); /* t = q-1 */
430 mpz_mod(exp2, d, t); /* exp2 = d mod q-1 */
431
432 mpz_invert(coeff, q, p); /* coeff = q^-1 mod p */
433 if (mpz_cmp_ui(coeff, 0) < 0) /* make coeff d is positive */
434 {
435 mpz_add(coeff, coeff, p);
436 }
437
438 mpz_clear(q1);
439 mpz_clear(m);
440 mpz_clear(t);
441
442 /* apply values */
443 *(this->p) = *p;
444 *(this->q) = *q;
445 *(this->n) = *n;
446 *(this->e) = *e;
447 *(this->d) = *d;
448 *(this->exp1) = *exp1;
449 *(this->exp2) = *exp2;
450 *(this->coeff) = *coeff;
451
452 /* set key size in bytes */
453
454 this->is_key_set = TRUE;
455 this->k = key_size;
456
457 return SUCCESS;
458 }
459
460 /**
461 * Implementation of rsa_private_key.get_public_key.
462 */
463 rsa_public_key_t *get_public_key(private_rsa_private_key_t *this)
464 {
465 rsa_public_key_t *public_key;
466 //chunk_t key;
467
468 public_key = rsa_public_key_create();
469
470 if (this->is_key_set)
471 {
472
473 chunk_t n, e, key;
474
475 n.len = this->k;
476 n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
477 e.len = this->k;
478 e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
479
480 key.len = this->k * 2;
481 key.ptr = allocator_alloc(key.len);
482 memcpy(key.ptr, n.ptr, n.len);
483 memcpy(key.ptr + n.len, e.ptr, e.len);
484 allocator_free(n.ptr);
485 allocator_free(e.ptr);
486
487 public_key->set_key(public_key, key);
488 allocator_free(key.ptr);
489
490 }
491
492 return public_key;
493 }
494
495
496 /**
497 * Implementation of rsa_private_key.destroy.
498 */
499 static void destroy(private_rsa_private_key_t *this)
500 {
501 if (this->is_key_set)
502 {
503 mpz_clear(this->n);
504 mpz_clear(this->e);
505 mpz_clear(this->p);
506 mpz_clear(this->q);
507 mpz_clear(this->d);
508 mpz_clear(this->exp1);
509 mpz_clear(this->exp2);
510 mpz_clear(this->coeff);
511 }
512 allocator_free(this);
513 }
514
515 /*
516 * Described in header.
517 */
518 rsa_private_key_t *rsa_private_key_create(hash_algorithm_t hash_algoritm)
519 {
520 private_rsa_private_key_t *this = allocator_alloc_thing(private_rsa_private_key_t);
521
522 /* public functions */
523 this->public.build_emsa_pkcs1_signature = (status_t (*) (rsa_private_key_t*,hash_algorithm_t,chunk_t,chunk_t*))build_emsa_pkcs1_signature;
524 this->public.set_key = (status_t (*) (rsa_private_key_t*,chunk_t))set_key;
525 this->public.get_key = (status_t (*) (rsa_private_key_t*,chunk_t*))get_key;
526 this->public.load_key = (status_t (*) (rsa_private_key_t*,char*))load_key;
527 this->public.save_key = (status_t (*) (rsa_private_key_t*,char*))save_key;
528 this->public.generate_key = (status_t (*) (rsa_private_key_t*,size_t))generate_key;
529 this->public.get_public_key = (rsa_public_key_t *(*) (rsa_private_key_t*))get_public_key;
530 this->public.destroy = (void (*) (rsa_private_key_t*))destroy;
531
532 /* private functions */
533 this->rsadp = rsadp;
534 this->rsasp1 = rsadp; /* same algorithm */
535
536 this->is_key_set = FALSE;
537
538 return &(this->public);
539 }