1 /* $NetBSD: rsa-gmp.c,v 1.1.1.1 2011/04/13 18:14:51 elric Exp $ */
4 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <krb5/krb5-types.h>
45 #include <krb5/roken.h>
52 BN2mpz(mpz_t s
, const BIGNUM
*bn
)
57 len
= BN_num_bytes(bn
);
61 mpz_import(s
, len
, 1, 1, 1, 0, p
);
74 mpz_export(NULL
, &size
, 1, 1, 1, 0, s
);
76 if (p
== NULL
&& size
!= 0)
78 mpz_export(p
, &size
, 1, 1, 1, 0, s
);
79 bn
= BN_bin2bn(p
, size
, NULL
);
85 rsa_private_calculate(mpz_t in
, mpz_t p
, mpz_t q
,
86 mpz_t dmp1
, mpz_t dmq1
, mpz_t iqmp
,
90 mpz_init(vp
); mpz_init(vq
); mpz_init(u
);
92 /* vq = c ^ (d mod (q - 1)) mod q */
93 /* vp = c ^ (d mod (p - 1)) mod p */
94 mpz_fdiv_r(vp
, in
, p
);
95 mpz_powm(vp
, vp
, dmp1
, p
);
96 mpz_fdiv_r(vq
, in
, q
);
97 mpz_powm(vq
, vq
, dmq1
, q
);
99 /* C2 = 1/q mod p (iqmp) */
100 /* u = (vp - vq)C2 mod p. */
103 if (mp_int_compare_zero(&u
) < 0)
104 mp_int_add(&u
, p
, &u
);
109 /* c ^ d mod n = vq + u q */
125 gmp_rsa_public_encrypt(int flen
, const unsigned char* from
,
126 unsigned char* to
, RSA
* rsa
, int padding
)
128 unsigned char *p
, *p0
;
130 mpz_t enc
, dec
, n
, e
;
132 if (padding
!= RSA_PKCS1_PADDING
)
135 size
= RSA_size(rsa
);
137 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
143 p
= p0
= malloc(size
- 1);
150 padlen
= size
- flen
- 3;
154 if (RAND_bytes(p
, padlen
) != 1) {
167 memcpy(p
, from
, flen
);
169 assert((p
- p0
) == size
- 1);
173 mpz_import(dec
, size
- 1, 1, 1, 1, 0, p0
);
176 mpz_powm(enc
, dec
, e
, n
);
183 mpz_export(to
, &ssize
, 1, 1, 1, 0, enc
);
184 assert(size
>= ssize
);
193 gmp_rsa_public_decrypt(int flen
, const unsigned char* from
,
194 unsigned char* to
, RSA
* rsa
, int padding
)
200 if (padding
!= RSA_PKCS1_PADDING
)
203 if (flen
> RSA_size(rsa
))
210 /* Check that the exponent is larger then 3 */
211 if (mp_int_compare_value(&e
, 3) <= 0) {
220 mpz_import(s
, flen
, 1, 1, 1, 0, rk_UNCONST(from
));
222 if (mpz_cmp(s
, n
) >= 0) {
228 mpz_powm(us
, s
, e
, n
);
236 mpz_export(p
, &size
, 1, 1, 1, 0, us
);
237 assert(size
<= RSA_size(rsa
));
241 /* head zero was skipped by mp_int_to_unsigned */
247 while (size
&& *p
== 0xff) {
250 if (size
== 0 || *p
!= 0)
254 memmove(to
, p
, size
);
260 gmp_rsa_private_encrypt(int flen
, const unsigned char* from
,
261 unsigned char* to
, RSA
* rsa
, int padding
)
263 unsigned char *p
, *p0
;
267 if (padding
!= RSA_PKCS1_PADDING
)
270 size
= RSA_size(rsa
);
272 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
275 p0
= p
= malloc(size
);
278 memset(p
, 0xff, size
- flen
- 3);
279 p
+= size
- flen
- 3;
281 memcpy(p
, from
, flen
);
283 assert((p
- p0
) == size
);
290 mpz_import(in
, size
, 1, 1, 1, 0, p0
);
294 if(mp_int_compare_zero(&in
) < 0 ||
295 mp_int_compare(&in
, &n
) >= 0) {
301 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
302 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
306 BN2mpz(dmp1
, rsa
->dmp1
);
307 BN2mpz(dmq1
, rsa
->dmq1
);
308 BN2mpz(iqmp
, rsa
->iqmp
);
310 rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
321 mpz_powm(out
, in
, d
, n
);
327 mpz_export(to
, &ssize
, 1, 1, 1, 0, out
);
328 assert(size
>= ssize
);
341 gmp_rsa_private_decrypt(int flen
, const unsigned char* from
,
342 unsigned char* to
, RSA
* rsa
, int padding
)
348 if (padding
!= RSA_PKCS1_PADDING
)
351 size
= RSA_size(rsa
);
361 mpz_import(in
, flen
, 1, 1, 1, 0, from
);
363 if(mpz_cmp_ui(in
, 0) < 0 ||
364 mpz_cmp(in
, n
) >= 0) {
369 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
370 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
374 BN2mpz(dmp1
, rsa
->dmp1
);
375 BN2mpz(dmq1
, rsa
->dmq1
);
376 BN2mpz(iqmp
, rsa
->iqmp
);
378 rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
389 if(mp_int_compare_zero(&in
) < 0 ||
390 mp_int_compare(&in
, &n
) >= 0)
395 mpz_powm(out
, in
, d
, n
);
402 mpz_export(ptr
, &ssize
, 1, 1, 1, 0, out
);
403 assert(size
>= ssize
);
407 /* head zero was skipped by mp_int_to_unsigned */
411 while (size
&& *ptr
!= 0) {
418 memmove(to
, ptr
, size
);
430 random_num(mpz_t num
, size_t len
)
438 if (RAND_bytes(p
, len
) != 1) {
442 mpz_import(num
, len
, 1, 1, 1, 0, p
);
449 gmp_rsa_generate_key(RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
)
451 mpz_t el
, p
, q
, n
, d
, dmp1
, dmq1
, iqmp
, t1
, t2
, t3
;
473 /* generate p and q so that p != q and bits(pq) ~ bits */
477 BN_GENCB_call(cb
, 2, counter
++);
478 random_num(p
, bits
/ 2 + 1);
481 mpz_sub_ui(t1
, p
, 1);
483 } while(mpz_cmp_ui(t2
, 1) != 0);
485 BN_GENCB_call(cb
, 3, 0);
489 BN_GENCB_call(cb
, 2, counter
++);
490 random_num(q
, bits
/ 2 + 1);
493 mpz_sub_ui(t1
, q
, 1);
495 } while(mpz_cmp_ui(t2
, 1) != 0);
498 if (mpz_cmp(p
, q
) < 0)
501 BN_GENCB_call(cb
, 3, 1);
503 /* calculate n, n = p * q */
506 /* calculate d, d = 1/e mod (p - 1)(q - 1) */
507 mpz_sub_ui(t1
, p
, 1);
508 mpz_sub_ui(t2
, q
, 1);
510 mpz_invert(d
, el
, t3
);
512 /* calculate dmp1 dmp1 = d mod (p-1) */
513 mpz_mod(dmp1
, d
, t1
);
514 /* calculate dmq1 dmq1 = d mod (q-1) */
515 mpz_mod(dmq1
, d
, t2
);
516 /* calculate iqmp iqmp = 1/q mod p */
517 mpz_invert(iqmp
, q
, p
);
519 /* fill in RSA key */
526 rsa
->dmp1
= mpz2BN(dmp1
);
527 rsa
->dmq1
= mpz2BN(dmq1
);
528 rsa
->iqmp
= mpz2BN(iqmp
);
548 gmp_rsa_init(RSA
*rsa
)
554 gmp_rsa_finish(RSA
*rsa
)
559 const RSA_METHOD hc_rsa_gmp_method
= {
561 gmp_rsa_public_encrypt
,
562 gmp_rsa_public_decrypt
,
563 gmp_rsa_private_encrypt
,
564 gmp_rsa_private_decrypt
,
576 #endif /* HAVE_GMP */
579 * RSA implementation using Gnu Multipresistion Library.
586 return &hc_rsa_gmp_method
;