1 /* $NetBSD: rsa-ltm.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
4 * Copyright (c) 2006 - 2007, 2010 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>
50 random_num(mp_int
*num
, size_t len
)
58 if (RAND_bytes(p
, len
) != 1) {
62 mp_read_unsigned_bin(num
, p
, len
);
68 BN2mpz(mp_int
*s
, const BIGNUM
*bn
)
73 len
= BN_num_bytes(bn
);
76 mp_read_unsigned_bin(s
, p
, len
);
81 setup_blind(mp_int
*n
, mp_int
*b
, mp_int
*bi
)
83 random_num(b
, mp_count_bits(n
));
89 blind(mp_int
*in
, mp_int
*b
, mp_int
*e
, mp_int
*n
)
93 /* in' = (in * b^e) mod n */
94 mp_exptmod(b
, e
, n
, &t1
);
101 unblind(mp_int
*out
, mp_int
*bi
, mp_int
*n
)
103 /* out' = (out * 1/b) mod n */
104 mp_mul(out
, bi
, out
);
109 ltm_rsa_private_calculate(mp_int
* in
, mp_int
* p
, mp_int
* q
,
110 mp_int
* dmp1
, mp_int
* dmq1
, mp_int
* iqmp
,
115 mp_init_multi(&vp
, &vq
, &u
, NULL
);
117 /* vq = c ^ (d mod (q - 1)) mod q */
118 /* vp = c ^ (d mod (p - 1)) mod p */
120 mp_exptmod(&u
, dmp1
, p
, &vp
);
122 mp_exptmod(&u
, dmq1
, q
, &vq
);
124 /* C2 = 1/q mod p (iqmp) */
125 /* u = (vp - vq)C2 mod p. */
126 mp_sub(&vp
, &vq
, &u
);
129 mp_mul(&u
, iqmp
, &u
);
132 /* c ^ d mod n = vq + u q */
134 mp_add(&u
, &vq
, out
);
136 mp_clear_multi(&vp
, &vq
, &u
, NULL
);
146 ltm_rsa_public_encrypt(int flen
, const unsigned char* from
,
147 unsigned char* to
, RSA
* rsa
, int padding
)
149 unsigned char *p
, *p0
;
152 mp_int enc
, dec
, n
, e
;
154 if (padding
!= RSA_PKCS1_PADDING
)
157 mp_init_multi(&n
, &e
, &enc
, &dec
, NULL
);
159 size
= RSA_size(rsa
);
161 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
) {
162 mp_clear_multi(&n
, &e
, &enc
, &dec
);
169 p
= p0
= malloc(size
- 1);
171 mp_clear_multi(&e
, &n
, &enc
, &dec
, NULL
);
175 padlen
= size
- flen
- 3;
178 if (RAND_bytes(p
, padlen
) != 1) {
179 mp_clear_multi(&e
, &n
, &enc
, &dec
, NULL
);
190 memcpy(p
, from
, flen
);
192 assert((p
- p0
) == size
- 1);
194 mp_read_unsigned_bin(&dec
, p0
, size
- 1);
197 res
= mp_exptmod(&dec
, &e
, &n
, &enc
);
199 mp_clear_multi(&dec
, &e
, &n
, NULL
);
208 ssize
= mp_unsigned_bin_size(&enc
);
209 assert(size
>= ssize
);
210 mp_to_unsigned_bin(&enc
, to
);
219 ltm_rsa_public_decrypt(int flen
, const unsigned char* from
,
220 unsigned char* to
, RSA
* rsa
, int padding
)
227 if (padding
!= RSA_PKCS1_PADDING
)
230 if (flen
> RSA_size(rsa
))
233 mp_init_multi(&e
, &n
, &s
, &us
, NULL
);
239 /* Check that the exponent is larger then 3 */
240 if (mp_int_compare_value(&e
, 3) <= 0) {
241 mp_clear_multi(&e
, &n
, &s
, &us
, NULL
);
246 mp_read_unsigned_bin(&s
, rk_UNCONST(from
), flen
);
248 if (mp_cmp(&s
, &n
) >= 0) {
249 mp_clear_multi(&e
, &n
, &s
, &us
, NULL
);
253 res
= mp_exptmod(&s
, &e
, &n
, &us
);
255 mp_clear_multi(&e
, &n
, &s
, NULL
);
264 size
= mp_unsigned_bin_size(&us
);
265 assert(size
<= RSA_size(rsa
));
266 mp_to_unsigned_bin(&us
, p
);
270 /* head zero was skipped by mp_to_unsigned_bin */
276 while (size
&& *p
== 0xff) {
279 if (size
== 0 || *p
!= 0)
283 memmove(to
, p
, size
);
289 ltm_rsa_private_encrypt(int flen
, const unsigned char* from
,
290 unsigned char* to
, RSA
* rsa
, int padding
)
292 unsigned char *p
, *p0
;
295 mp_int in
, out
, n
, e
;
297 int blinding
= (rsa
->flags
& RSA_FLAG_NO_BLINDING
) == 0;
300 if (padding
!= RSA_PKCS1_PADDING
)
303 mp_init_multi(&e
, &n
, &in
, &out
, &b
, &bi
, NULL
);
305 size
= RSA_size(rsa
);
307 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
310 p0
= p
= malloc(size
);
313 memset(p
, 0xff, size
- flen
- 3);
314 p
+= size
- flen
- 3;
316 memcpy(p
, from
, flen
);
318 assert((p
- p0
) == size
);
323 mp_read_unsigned_bin(&in
, p0
, size
);
326 if(mp_isneg(&in
) || mp_cmp(&in
, &n
) >= 0) {
332 setup_blind(&n
, &b
, &bi
);
333 blind(&in
, &b
, &e
, &n
);
337 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
338 mp_int p
, q
, dmp1
, dmq1
, iqmp
;
340 mp_init_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
344 BN2mpz(&dmp1
, rsa
->dmp1
);
345 BN2mpz(&dmq1
, rsa
->dmq1
);
346 BN2mpz(&iqmp
, rsa
->iqmp
);
348 res
= ltm_rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
350 mp_clear_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
360 res
= mp_exptmod(&in
, &d
, &n
, &out
);
369 unblind(&out
, &bi
, &n
);
373 ssize
= mp_unsigned_bin_size(&out
);
374 assert(size
>= ssize
);
375 mp_to_unsigned_bin(&out
, to
);
380 mp_clear_multi(&e
, &n
, &in
, &out
, &b
, &bi
, NULL
);
386 ltm_rsa_private_decrypt(int flen
, const unsigned char* from
,
387 unsigned char* to
, RSA
* rsa
, int padding
)
391 mp_int in
, out
, n
, e
, b
, bi
;
392 int blinding
= (rsa
->flags
& RSA_FLAG_NO_BLINDING
) == 0;
395 if (padding
!= RSA_PKCS1_PADDING
)
398 size
= RSA_size(rsa
);
402 mp_init_multi(&in
, &n
, &e
, &out
, &b
, &bi
, NULL
);
407 mp_read_unsigned_bin(&in
, rk_UNCONST(from
), flen
);
409 if(mp_isneg(&in
) || mp_cmp(&in
, &n
) >= 0) {
415 setup_blind(&n
, &b
, &bi
);
416 blind(&in
, &b
, &e
, &n
);
420 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
421 mp_int p
, q
, dmp1
, dmq1
, iqmp
;
423 mp_init_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
427 BN2mpz(&dmp1
, rsa
->dmp1
);
428 BN2mpz(&dmq1
, rsa
->dmq1
);
429 BN2mpz(&iqmp
, rsa
->iqmp
);
431 res
= ltm_rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
433 mp_clear_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
443 if(mp_isneg(&in
) || mp_cmp(&in
, &n
) >= 0)
447 res
= mp_exptmod(&in
, &d
, &n
, &out
);
456 unblind(&out
, &bi
, &n
);
461 ssize
= mp_unsigned_bin_size(&out
);
462 assert(size
>= ssize
);
463 mp_to_unsigned_bin(&out
, ptr
);
467 /* head zero was skipped by mp_int_to_unsigned */
473 while (size
&& *ptr
!= 0) {
480 memmove(to
, ptr
, size
);
483 mp_clear_multi(&e
, &n
, &in
, &out
, &b
, &bi
, NULL
);
495 size
= mp_unsigned_bin_size(s
);
497 if (p
== NULL
&& size
!= 0)
500 mp_to_unsigned_bin(s
, p
);
502 bn
= BN_bin2bn(p
, size
, NULL
);
507 #define CHECK(f, v) if ((f) != (v)) { goto out; }
510 ltm_rsa_generate_key(RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
)
512 mp_int el
, p
, q
, n
, d
, dmp1
, dmq1
, iqmp
, t1
, t2
, t3
;
513 int counter
, ret
, bitsp
;
518 bitsp
= (bits
+ 1) / 2;
522 mp_init_multi(&el
, &p
, &q
, &n
, &d
,
524 &t1
, &t2
, &t3
, NULL
);
528 /* generate p and q so that p != q and bits(pq) ~ bits */
531 BN_GENCB_call(cb
, 2, counter
++);
532 CHECK(random_num(&p
, bitsp
), 0);
533 CHECK(mp_find_prime(&p
), MP_YES
);
535 mp_sub_d(&p
, 1, &t1
);
536 mp_gcd(&t1
, &el
, &t2
);
537 } while(mp_cmp_d(&t2
, 1) != 0);
539 BN_GENCB_call(cb
, 3, 0);
543 BN_GENCB_call(cb
, 2, counter
++);
544 CHECK(random_num(&q
, bits
- bitsp
), 0);
545 CHECK(mp_find_prime(&q
), MP_YES
);
547 if (mp_cmp(&p
, &q
) == 0) /* don't let p and q be the same */
550 mp_sub_d(&q
, 1, &t1
);
551 mp_gcd(&t1
, &el
, &t2
);
552 } while(mp_cmp_d(&t2
, 1) != 0);
555 if (mp_cmp(&p
, &q
) < 0) {
562 BN_GENCB_call(cb
, 3, 1);
564 /* calculate n, n = p * q */
567 /* calculate d, d = 1/e mod (p - 1)(q - 1) */
568 mp_sub_d(&p
, 1, &t1
);
569 mp_sub_d(&q
, 1, &t2
);
570 mp_mul(&t1
, &t2
, &t3
);
571 mp_invmod(&el
, &t3
, &d
);
573 /* calculate dmp1 dmp1 = d mod (p-1) */
574 mp_mod(&d
, &t1
, &dmp1
);
575 /* calculate dmq1 dmq1 = d mod (q-1) */
576 mp_mod(&d
, &t2
, &dmq1
);
577 /* calculate iqmp iqmp = 1/q mod p */
578 mp_invmod(&q
, &p
, &iqmp
);
580 /* fill in RSA key */
582 rsa
->e
= mpz2BN(&el
);
587 rsa
->dmp1
= mpz2BN(&dmp1
);
588 rsa
->dmq1
= mpz2BN(&dmq1
);
589 rsa
->iqmp
= mpz2BN(&iqmp
);
594 mp_clear_multi(&el
, &p
, &q
, &n
, &d
,
596 &t1
, &t2
, &t3
, NULL
);
602 ltm_rsa_init(RSA
*rsa
)
608 ltm_rsa_finish(RSA
*rsa
)
613 const RSA_METHOD hc_rsa_ltm_method
= {
615 ltm_rsa_public_encrypt
,
616 ltm_rsa_public_decrypt
,
617 ltm_rsa_private_encrypt
,
618 ltm_rsa_private_decrypt
,
633 return &hc_rsa_ltm_method
;