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 __RCSID("$Heimdal: rsa-imath.c 21154 2007-06-18 21:58:12Z lha $"
43 #include <krb5-types.h>
50 #include "imath/imath.h"
51 #include "imath/iprime.h"
54 BN2mpz(mpz_t
*s
, const BIGNUM
*bn
)
61 len
= BN_num_bytes(bn
);
64 mp_int_read_unsigned(s
, p
, len
);
75 size
= mp_int_unsigned_len(s
);
77 if (p
== NULL
&& size
!= 0)
79 mp_int_to_unsigned(s
, p
, size
);
81 bn
= BN_bin2bn(p
, size
, NULL
);
86 static int random_num(mp_int
, size_t);
89 setup_blind(mp_int n
, mp_int b
, mp_int bi
)
93 random_num(b
, mp_int_count_bits(n
));
95 mp_int_invmod(b
, n
, bi
);
99 blind(mp_int in
, mp_int b
, mp_int e
, mp_int n
)
103 /* in' = (in * b^e) mod n */
104 mp_int_exptmod(b
, e
, n
, &t1
);
105 mp_int_mul(&t1
, in
, in
);
106 mp_int_mod(in
, n
, in
);
111 unblind(mp_int out
, mp_int bi
, mp_int n
)
113 /* out' = (out * 1/b) mod n */
114 mp_int_mul(out
, bi
, out
);
115 mp_int_mod(out
, n
, out
);
119 rsa_private_calculate(mp_int in
, mp_int p
, mp_int q
,
120 mp_int dmp1
, mp_int dmq1
, mp_int iqmp
,
124 mp_int_init(&vp
); mp_int_init(&vq
); mp_int_init(&u
);
126 /* vq = c ^ (d mod (q - 1)) mod q */
127 /* vp = c ^ (d mod (p - 1)) mod p */
128 mp_int_mod(in
, p
, &u
);
129 mp_int_exptmod(&u
, dmp1
, p
, &vp
);
130 mp_int_mod(in
, q
, &u
);
131 mp_int_exptmod(&u
, dmq1
, q
, &vq
);
133 /* C2 = 1/q mod p (iqmp) */
134 /* u = (vp - vq)C2 mod p. */
135 mp_int_sub(&vp
, &vq
, &u
);
136 if (mp_int_compare_zero(&u
) < 0)
137 mp_int_add(&u
, p
, &u
);
138 mp_int_mul(&u
, iqmp
, &u
);
139 mp_int_mod(&u
, p
, &u
);
141 /* c ^ d mod n = vq + u q */
142 mp_int_mul(&u
, q
, &u
);
143 mp_int_add(&u
, &vq
, out
);
157 imath_rsa_public_encrypt(int flen
, const unsigned char* from
,
158 unsigned char* to
, RSA
* rsa
, int padding
)
160 unsigned char *p
, *p0
;
163 mpz_t enc
, dec
, n
, e
;
165 if (padding
!= RSA_PKCS1_PADDING
)
168 size
= RSA_size(rsa
);
170 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
176 p
= p0
= malloc(size
- 1);
183 padlen
= size
- flen
- 3;
186 if (RAND_bytes(p
, padlen
) != 1) {
199 memcpy(p
, from
, flen
);
201 assert((p
- p0
) == size
- 1);
205 mp_int_read_unsigned(&dec
, p0
, size
- 1);
208 res
= mp_int_exptmod(&dec
, &e
, &n
, &enc
);
215 ssize
= mp_int_unsigned_len(&enc
);
216 assert(size
>= ssize
);
217 mp_int_to_unsigned(&enc
, to
, ssize
);
226 imath_rsa_public_decrypt(int flen
, const unsigned char* from
,
227 unsigned char* to
, RSA
* rsa
, int padding
)
234 if (padding
!= RSA_PKCS1_PADDING
)
237 if (flen
> RSA_size(rsa
))
244 /* Check that the exponent is larger then 3 */
245 if (mp_int_compare_value(&e
, 3) <= 0) {
254 mp_int_read_unsigned(&s
, rk_UNCONST(from
), flen
);
256 if (mp_int_compare(&s
, &n
) >= 0) {
262 res
= mp_int_exptmod(&s
, &e
, &n
, &us
);
273 size
= mp_int_unsigned_len(&us
);
274 assert(size
<= RSA_size(rsa
));
275 mp_int_to_unsigned(&us
, p
, size
);
279 /* head zero was skipped by mp_int_to_unsigned */
285 while (size
&& *p
== 0xff) {
288 if (size
== 0 || *p
!= 0)
292 memmove(to
, p
, size
);
298 imath_rsa_private_encrypt(int flen
, const unsigned char* from
,
299 unsigned char* to
, RSA
* rsa
, int padding
)
301 unsigned char *p
, *p0
;
304 mpz_t in
, out
, n
, e
, b
, bi
;
305 int blinding
= (rsa
->flags
& RSA_FLAG_NO_BLINDING
) == 0;
307 if (padding
!= RSA_PKCS1_PADDING
)
310 size
= RSA_size(rsa
);
312 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
315 p0
= p
= malloc(size
);
318 memset(p
, 0xff, size
- flen
- 3);
319 p
+= size
- flen
- 3;
321 memcpy(p
, from
, flen
);
323 assert((p
- p0
) == size
);
330 mp_int_read_unsigned(&in
, p0
, size
);
333 if(mp_int_compare_zero(&in
) < 0 ||
334 mp_int_compare(&in
, &n
) >= 0) {
340 setup_blind(&n
, &b
, &bi
);
341 blind(&in
, &b
, &e
, &n
);
344 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
345 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
349 BN2mpz(&dmp1
, rsa
->dmp1
);
350 BN2mpz(&dmq1
, rsa
->dmq1
);
351 BN2mpz(&iqmp
, rsa
->iqmp
);
353 res
= rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
364 res
= mp_int_exptmod(&in
, &d
, &n
, &out
);
373 unblind(&out
, &bi
, &n
);
380 ssize
= mp_int_unsigned_len(&out
);
381 assert(size
>= ssize
);
382 mp_int_to_unsigned(&out
, to
, size
);
396 imath_rsa_private_decrypt(int flen
, const unsigned char* from
,
397 unsigned char* to
, RSA
* rsa
, int padding
)
402 mpz_t in
, out
, n
, e
, b
, bi
;
403 int blinding
= (rsa
->flags
& RSA_FLAG_NO_BLINDING
) == 0;
405 if (padding
!= RSA_PKCS1_PADDING
)
408 size
= RSA_size(rsa
);
418 res
= mp_int_read_unsigned(&in
, rk_UNCONST(from
), flen
);
424 if(mp_int_compare_zero(&in
) < 0 ||
425 mp_int_compare(&in
, &n
) >= 0) {
431 setup_blind(&n
, &b
, &bi
);
432 blind(&in
, &b
, &e
, &n
);
435 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
436 mpz_t p
, q
, dmp1
, dmq1
, iqmp
;
440 BN2mpz(&dmp1
, rsa
->dmp1
);
441 BN2mpz(&dmq1
, rsa
->dmq1
);
442 BN2mpz(&iqmp
, rsa
->iqmp
);
444 res
= rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
454 if(mp_int_compare_zero(&in
) < 0 ||
455 mp_int_compare(&in
, &n
) >= 0)
459 res
= mp_int_exptmod(&in
, &d
, &n
, &out
);
468 unblind(&out
, &bi
, &n
);
476 ssize
= mp_int_unsigned_len(&out
);
477 assert(size
>= ssize
);
478 mp_int_to_unsigned(&out
, ptr
, ssize
);
482 /* head zero was skipped by mp_int_to_unsigned */
486 while (size
&& *ptr
!= 0) {
493 memmove(to
, ptr
, size
);
505 random_num(mp_int num
, size_t len
)
514 if (RAND_bytes(p
, len
) != 1) {
518 res
= mp_int_read_unsigned(num
, p
, len
);
525 #define CHECK(f, v) if ((f) != (v)) { goto out; }
528 imath_rsa_generate_key(RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
)
530 mpz_t el
, p
, q
, n
, d
, dmp1
, dmq1
, iqmp
, t1
, t2
, t3
;
552 /* generate p and q so that p != q and bits(pq) ~ bits */
555 BN_GENCB_call(cb
, 2, counter
++);
556 CHECK(random_num(&p
, bits
/ 2 + 1), 0);
557 CHECK(mp_int_find_prime(&p
), MP_TRUE
);
559 CHECK(mp_int_sub_value(&p
, 1, &t1
), MP_OK
);
560 CHECK(mp_int_gcd(&t1
, &el
, &t2
), MP_OK
);
561 } while(mp_int_compare_value(&t2
, 1) != 0);
563 BN_GENCB_call(cb
, 3, 0);
567 BN_GENCB_call(cb
, 2, counter
++);
568 CHECK(random_num(&q
, bits
/ 2 + 1), 0);
569 CHECK(mp_int_find_prime(&q
), MP_TRUE
);
571 if (mp_int_compare(&p
, &q
) == 0) /* don't let p and q be the same */
574 CHECK(mp_int_sub_value(&q
, 1, &t1
), MP_OK
);
575 CHECK(mp_int_gcd(&t1
, &el
, &t2
), MP_OK
);
576 } while(mp_int_compare_value(&t2
, 1) != 0);
579 if (mp_int_compare(&p
, &q
) < 0)
582 BN_GENCB_call(cb
, 3, 1);
584 /* calculate n, n = p * q */
585 CHECK(mp_int_mul(&p
, &q
, &n
), MP_OK
);
587 /* calculate d, d = 1/e mod (p - 1)(q - 1) */
588 CHECK(mp_int_sub_value(&p
, 1, &t1
), MP_OK
);
589 CHECK(mp_int_sub_value(&q
, 1, &t2
), MP_OK
);
590 CHECK(mp_int_mul(&t1
, &t2
, &t3
), MP_OK
);
591 CHECK(mp_int_invmod(&el
, &t3
, &d
), MP_OK
);
593 /* calculate dmp1 dmp1 = d mod (p-1) */
594 CHECK(mp_int_mod(&d
, &t1
, &dmp1
), MP_OK
);
595 /* calculate dmq1 dmq1 = d mod (q-1) */
596 CHECK(mp_int_mod(&d
, &t2
, &dmq1
), MP_OK
);
597 /* calculate iqmp iqmp = 1/q mod p */
598 CHECK(mp_int_invmod(&q
, &p
, &iqmp
), MP_OK
);
600 /* fill in RSA key */
602 rsa
->e
= mpz2BN(&el
);
607 rsa
->dmp1
= mpz2BN(&dmp1
);
608 rsa
->dmq1
= mpz2BN(&dmq1
);
609 rsa
->iqmp
= mpz2BN(&iqmp
);
629 imath_rsa_init(RSA
*rsa
)
635 imath_rsa_finish(RSA
*rsa
)
640 const RSA_METHOD hc_rsa_imath_method
= {
642 imath_rsa_public_encrypt
,
643 imath_rsa_public_decrypt
,
644 imath_rsa_private_encrypt
,
645 imath_rsa_private_decrypt
,
654 imath_rsa_generate_key
658 RSA_imath_method(void)
660 return &hc_rsa_imath_method
;