2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #include <krb5-types.h>
56 BN2mpz(mpz_t s
, const BIGNUM
*bn
)
61 len
= BN_num_bytes(bn
);
64 mpz_import(s
, len
, 1, 1, 1, 0, p
);
76 mpz_export(NULL
, &size
, 1, 1, 1, 0, s
);
78 if (p
== NULL
&& size
!= 0)
80 mpz_export(p
, &size
, 1, 1, 1, 0, s
);
82 bn
= BN_bin2bn(p
, size
, NULL
);
88 rsa_private_calculate(mpz_t in
, mpz_t p
, mpz_t q
,
89 mpz_t dmp1
, mpz_t dmq1
, mpz_t iqmp
,
93 mpz_init(vp
); mpz_init(vq
); mpz_init(u
);
95 /* vq = c ^ (d mod (q - 1)) mod q */
96 /* vp = c ^ (d mod (p - 1)) mod p */
98 mpz_powm(vp
, vp
, dmp1
, p
);
100 mpz_powm(vq
, vq
, dmq1
, q
);
102 /* C2 = 1/q mod p (iqmp) */
103 /* u = (vp - vq)C2 mod p. */
106 if (mp_int_compare_zero(&u
) < 0)
107 mp_int_add(&u
, p
, &u
);
112 /* c ^ d mod n = vq + u q */
128 gmp_rsa_public_encrypt(int flen
, const unsigned char* from
,
129 unsigned char* to
, RSA
* rsa
, int padding
)
131 unsigned char *p
, *p0
;
134 mpz_t enc
, dec
, n
, e
;
136 if (padding
!= RSA_PKCS1_PADDING
)
139 size
= RSA_size(rsa
);
141 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
147 p
= p0
= malloc(size
- 1);
154 padlen
= size
- flen
- 3;
158 if (RAND_bytes(p
, padlen
) != 1) {
171 memcpy(p
, from
, flen
);
173 assert((p
- p0
) == size
- 1);
177 mpz_import(dec
, size
- 1, 1, 1, 1, 0, p0
);
180 mpz_powm(enc
, dec
, e
, n
);
187 mpz_export(to
, &ssize
, 1, 1, 1, 0, enc
);
188 assert(size
>= ssize
);
197 gmp_rsa_public_decrypt(int flen
, const unsigned char* from
,
198 unsigned char* to
, RSA
* rsa
, int padding
)
204 if (padding
!= RSA_PKCS1_PADDING
)
207 if (flen
> RSA_size(rsa
))
214 /* Check that the exponent is larger then 3 */
215 if (mp_int_compare_value(&e
, 3) <= 0) {
224 mpz_import(s
, flen
, 1, 1, 1, 0, rk_UNCONST(from
));
226 if (mpz_cmp(s
, n
) >= 0) {
232 mpz_powm(us
, s
, e
, n
);
240 mpz_export(p
, &size
, 1, 1, 1, 0, us
);
241 assert(size
<= RSA_size(rsa
));
245 /* head zero was skipped by mp_int_to_unsigned */
251 while (size
&& *p
== 0xff) {
254 if (size
== 0 || *p
!= 0)
258 memmove(to
, p
, size
);
264 gmp_rsa_private_encrypt(int flen
, const unsigned char* from
,
265 unsigned char* to
, RSA
* rsa
, int padding
)
267 unsigned char *p
, *p0
;
269 mpz_t in
, out
, n
, e
, b
, bi
;
271 if (padding
!= RSA_PKCS1_PADDING
)
274 size
= RSA_size(rsa
);
276 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
279 p0
= p
= malloc(size
);
282 memset(p
, 0xff, size
- flen
- 3);
283 p
+= size
- flen
- 3;
285 memcpy(p
, from
, flen
);
287 assert((p
- p0
) == size
);
294 mpz_import(in
, size
, 1, 1, 1, 0, p0
);
298 if(mp_int_compare_zero(&in
) < 0 ||
299 mp_int_compare(&in
, &n
) >= 0) {
305 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
306 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
310 BN2mpz(dmp1
, rsa
->dmp1
);
311 BN2mpz(dmq1
, rsa
->dmq1
);
312 BN2mpz(iqmp
, rsa
->iqmp
);
314 rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
325 mpz_powm(out
, in
, d
, n
);
335 mpz_export(to
, &ssize
, 1, 1, 1, 0, out
);
336 assert(size
>= ssize
);
350 gmp_rsa_private_decrypt(int flen
, const unsigned char* from
,
351 unsigned char* to
, RSA
* rsa
, int padding
)
355 mpz_t in
, out
, n
, e
, b
, bi
;
357 if (padding
!= RSA_PKCS1_PADDING
)
360 size
= RSA_size(rsa
);
370 res
= mp_int_read_unsigned(&in
, rk_UNCONST(from
), flen
);
376 if(mp_int_compare_zero(&in
) < 0 ||
377 mp_int_compare(&in
, &n
) >= 0) {
382 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
383 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
387 BN2mpz(dmp1
, rsa
->dmp1
);
388 BN2mpz(dmq1
, rsa
->dmq1
);
389 BN2mpz(iqmp
, rsa
->iqmp
);
391 res
= rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
402 if(mp_int_compare_zero(&in
) < 0 ||
403 mp_int_compare(&in
, &n
) >= 0)
408 mpz_powm(out
, in
, d
, n
);
419 mpz_export(ptr
, &ssize
, 1, 1, 1, 0, out
);
420 assert(size
>= ssize
);
424 /* head zero was skipped by mp_int_to_unsigned */
428 while (size
&& *ptr
!= 0) {
435 memmove(to
, ptr
, size
);
447 gmp_rsa_generate_key(RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
)
449 mpz_t el
, p
, q
, n
, d
, dmp1
, dmq1
, iqmp
, t1
, t2
, t3
;
471 /* generate p and q so that p != q and bits(pq) ~ bits */
475 BN_GENCB_call(cb
, 2, counter
++);
476 CHECK(random_num(&p
, bits
/ 2 + 1), 0);
477 CHECK(mp_int_find_prime(&p
), MP_TRUE
);
479 CHECK(mp_int_sub_value(&p
, 1, &t1
), MP_OK
);
480 CHECK(mp_int_gcd(&t1
, &el
, &t2
), MP_OK
);
481 } while(mp_int_compare_value(&t2
, 1) != 0);
483 BN_GENCB_call(cb
, 3, 0);
487 BN_GENCB_call(cb
, 2, counter
++);
488 CHECK(random_num(&q
, bits
/ 2 + 1), 0);
489 CHECK(mp_int_find_prime(&q
), MP_TRUE
);
491 if (mp_int_compare(&p
, &q
) == 0) /* don't let p and q be the same */
494 CHECK(mp_int_sub_value(&q
, 1, &t1
), MP_OK
);
495 CHECK(mp_int_gcd(&t1
, &el
, &t2
), MP_OK
);
496 } while(mp_int_compare_value(&t2
, 1) != 0);
499 if (mp_int_compare(&p
, &q
) < 0)
502 BN_GENCB_call(cb
, 3, 1);
504 /* calculate n, n = p * q */
505 CHECK(mp_int_mul(&p
, &q
, &n
), MP_OK
);
507 /* calculate d, d = 1/e mod (p - 1)(q - 1) */
508 CHECK(mp_int_sub_value(&p
, 1, &t1
), MP_OK
);
509 CHECK(mp_int_sub_value(&q
, 1, &t2
), MP_OK
);
510 CHECK(mp_int_mul(&t1
, &t2
, &t3
), MP_OK
);
511 CHECK(mp_int_invmod(&el
, &t3
, &d
), MP_OK
);
513 /* calculate dmp1 dmp1 = d mod (p-1) */
514 CHECK(mp_int_mod(&d
, &t1
, &dmp1
), MP_OK
);
515 /* calculate dmq1 dmq1 = d mod (q-1) */
516 CHECK(mp_int_mod(&d
, &t2
, &dmq1
), MP_OK
);
517 /* calculate iqmp iqmp = 1/q mod p */
518 CHECK(mp_int_invmod(&q
, &p
, &iqmp
), MP_OK
);
520 /* fill in RSA key */
522 rsa
->e
= mpz2BN(&el
);
527 rsa
->dmp1
= mpz2BN(&dmp1
);
528 rsa
->dmq1
= mpz2BN(&dmq1
);
529 rsa
->iqmp
= mpz2BN(&iqmp
);
550 gmp_rsa_init(RSA
*rsa
)
556 gmp_rsa_finish(RSA
*rsa
)
561 const RSA_METHOD hc_rsa_gmp_method
= {
563 gmp_rsa_public_encrypt
,
564 gmp_rsa_public_decrypt
,
565 gmp_rsa_private_encrypt
,
566 gmp_rsa_private_decrypt
,
578 #endif /* HAVE_GMP */
581 * RSA implementation using Gnu Multipresistion Library.
588 return &hc_rsa_gmp_method
;