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
38 #include <krb5-types.h>
50 BN2mpz(mpz_t s
, const BIGNUM
*bn
)
55 len
= BN_num_bytes(bn
);
59 mpz_import(s
, len
, 1, 1, 1, 0, p
);
72 mpz_export(NULL
, &size
, 1, 1, 1, 0, s
);
74 if (p
== NULL
&& size
!= 0)
76 mpz_export(p
, &size
, 1, 1, 1, 0, s
);
77 bn
= BN_bin2bn(p
, size
, NULL
);
83 rsa_private_calculate(mpz_t in
, mpz_t p
, mpz_t q
,
84 mpz_t dmp1
, mpz_t dmq1
, mpz_t iqmp
,
88 mpz_init(vp
); mpz_init(vq
); mpz_init(u
);
90 /* vq = c ^ (d mod (q - 1)) mod q */
91 /* vp = c ^ (d mod (p - 1)) mod p */
92 mpz_fdiv_r(vp
, in
, p
);
93 mpz_powm(vp
, vp
, dmp1
, p
);
94 mpz_fdiv_r(vq
, in
, q
);
95 mpz_powm(vq
, vq
, dmq1
, q
);
97 /* C2 = 1/q mod p (iqmp) */
98 /* u = (vp - vq)C2 mod p. */
101 if (mp_int_compare_zero(&u
) < 0)
102 mp_int_add(&u
, p
, &u
);
107 /* c ^ d mod n = vq + u q */
123 gmp_rsa_public_encrypt(int flen
, const unsigned char* from
,
124 unsigned char* to
, RSA
* rsa
, int padding
)
126 unsigned char *p
, *p0
;
128 mpz_t enc
, dec
, n
, e
;
130 if (padding
!= RSA_PKCS1_PADDING
)
133 size
= RSA_size(rsa
);
135 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
141 p
= p0
= malloc(size
- 1);
148 padlen
= size
- flen
- 3;
152 if (RAND_bytes(p
, padlen
) != 1) {
165 memcpy(p
, from
, flen
);
167 assert((p
- p0
) == size
- 1);
171 mpz_import(dec
, size
- 1, 1, 1, 1, 0, p0
);
174 mpz_powm(enc
, dec
, e
, n
);
181 mpz_export(to
, &ssize
, 1, 1, 1, 0, enc
);
182 assert(size
>= ssize
);
191 gmp_rsa_public_decrypt(int flen
, const unsigned char* from
,
192 unsigned char* to
, RSA
* rsa
, int padding
)
198 if (padding
!= RSA_PKCS1_PADDING
)
201 if (flen
> RSA_size(rsa
))
208 /* Check that the exponent is larger then 3 */
209 if (mp_int_compare_value(&e
, 3) <= 0) {
218 mpz_import(s
, flen
, 1, 1, 1, 0, rk_UNCONST(from
));
220 if (mpz_cmp(s
, n
) >= 0) {
226 mpz_powm(us
, s
, e
, n
);
234 mpz_export(p
, &size
, 1, 1, 1, 0, us
);
235 assert(size
<= RSA_size(rsa
));
239 /* head zero was skipped by mp_int_to_unsigned */
245 while (size
&& *p
== 0xff) {
248 if (size
== 0 || *p
!= 0)
252 memmove(to
, p
, size
);
258 gmp_rsa_private_encrypt(int flen
, const unsigned char* from
,
259 unsigned char* to
, RSA
* rsa
, int padding
)
261 unsigned char *p
, *p0
;
265 if (padding
!= RSA_PKCS1_PADDING
)
268 size
= RSA_size(rsa
);
270 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
273 p0
= p
= malloc(size
);
276 memset(p
, 0xff, size
- flen
- 3);
277 p
+= size
- flen
- 3;
279 memcpy(p
, from
, flen
);
281 assert((p
- p0
) == size
);
288 mpz_import(in
, size
, 1, 1, 1, 0, p0
);
292 if(mp_int_compare_zero(&in
) < 0 ||
293 mp_int_compare(&in
, &n
) >= 0) {
299 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
300 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
304 BN2mpz(dmp1
, rsa
->dmp1
);
305 BN2mpz(dmq1
, rsa
->dmq1
);
306 BN2mpz(iqmp
, rsa
->iqmp
);
308 rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
319 mpz_powm(out
, in
, d
, n
);
325 mpz_export(to
, &ssize
, 1, 1, 1, 0, out
);
326 assert(size
>= ssize
);
339 gmp_rsa_private_decrypt(int flen
, const unsigned char* from
,
340 unsigned char* to
, RSA
* rsa
, int padding
)
346 if (padding
!= RSA_PKCS1_PADDING
)
349 size
= RSA_size(rsa
);
359 mpz_import(in
, flen
, 1, 1, 1, 0, from
);
361 if(mpz_cmp_ui(in
, 0) < 0 ||
362 mpz_cmp(in
, n
) >= 0) {
367 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
368 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
372 BN2mpz(dmp1
, rsa
->dmp1
);
373 BN2mpz(dmq1
, rsa
->dmq1
);
374 BN2mpz(iqmp
, rsa
->iqmp
);
376 rsa_private_calculate(in
, p
, q
, dmp1
, dmq1
, iqmp
, out
);
387 if(mp_int_compare_zero(&in
) < 0 ||
388 mp_int_compare(&in
, &n
) >= 0)
393 mpz_powm(out
, in
, d
, n
);
400 mpz_export(ptr
, &ssize
, 1, 1, 1, 0, out
);
401 assert(size
>= ssize
);
405 /* head zero was skipped by mp_int_to_unsigned */
409 while (size
&& *ptr
!= 0) {
416 memmove(to
, ptr
, size
);
428 random_num(mpz_t num
, size_t len
)
436 if (RAND_bytes(p
, len
) != 1) {
440 mpz_import(num
, len
, 1, 1, 1, 0, p
);
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 random_num(p
, bits
/ 2 + 1);
479 mpz_sub_ui(t1
, p
, 1);
481 } while(mpz_cmp_ui(t2
, 1) != 0);
483 BN_GENCB_call(cb
, 3, 0);
487 BN_GENCB_call(cb
, 2, counter
++);
488 random_num(q
, bits
/ 2 + 1);
491 mpz_sub_ui(t1
, q
, 1);
493 } while(mpz_cmp_ui(t2
, 1) != 0);
496 if (mpz_cmp(p
, q
) < 0)
499 BN_GENCB_call(cb
, 3, 1);
501 /* calculate n, n = p * q */
504 /* calculate d, d = 1/e mod (p - 1)(q - 1) */
505 mpz_sub_ui(t1
, p
, 1);
506 mpz_sub_ui(t2
, q
, 1);
508 mpz_invert(d
, el
, t3
);
510 /* calculate dmp1 dmp1 = d mod (p-1) */
511 mpz_mod(dmp1
, d
, t1
);
512 /* calculate dmq1 dmq1 = d mod (q-1) */
513 mpz_mod(dmq1
, d
, t2
);
514 /* calculate iqmp iqmp = 1/q mod p */
515 mpz_invert(iqmp
, q
, p
);
517 /* fill in RSA key */
524 rsa
->dmp1
= mpz2BN(dmp1
);
525 rsa
->dmq1
= mpz2BN(dmq1
);
526 rsa
->iqmp
= mpz2BN(iqmp
);
546 gmp_rsa_init(RSA
*rsa
)
552 gmp_rsa_finish(RSA
*rsa
)
557 const RSA_METHOD hc_rsa_gmp_method
= {
559 gmp_rsa_public_encrypt
,
560 gmp_rsa_public_decrypt
,
561 gmp_rsa_private_encrypt
,
562 gmp_rsa_private_decrypt
,
574 #endif /* HAVE_GMP */
577 * RSA implementation using Gnu Multipresistion Library.
584 return &hc_rsa_gmp_method
;