1 /* $NetBSD: rsa-tfm.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>
47 #ifdef USE_HCRYPTO_TFM
52 BN2mpz(fp_int
*s
, const BIGNUM
*bn
)
59 len
= BN_num_bytes(bn
);
62 fp_read_unsigned_bin(s
, p
, len
);
67 tfm_rsa_private_calculate(fp_int
* in
, fp_int
* p
, fp_int
* q
,
68 fp_int
* dmp1
, fp_int
* dmq1
, fp_int
* iqmp
,
73 fp_init_multi(&vp
, &vq
, &u
, NULL
);
75 /* vq = c ^ (d mod (q - 1)) mod q */
76 /* vp = c ^ (d mod (p - 1)) mod p */
78 fp_exptmod(&u
, dmp1
, p
, &vp
);
80 fp_exptmod(&u
, dmq1
, q
, &vq
);
82 /* C2 = 1/q mod p (iqmp) */
83 /* u = (vp - vq)C2 mod p. */
90 /* c ^ d mod n = vq + u q */
94 fp_zero_multi(&vp
, &vq
, &u
, NULL
);
104 tfm_rsa_public_encrypt(int flen
, const unsigned char* from
,
105 unsigned char* to
, RSA
* rsa
, int padding
)
107 unsigned char *p
, *p0
;
110 fp_int enc
, dec
, n
, e
;
112 if (padding
!= RSA_PKCS1_PADDING
)
115 size
= RSA_size(rsa
);
117 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
123 p
= p0
= malloc(size
- 1);
125 fp_zero_multi(&e
, &n
, NULL
);
129 padlen
= size
- flen
- 3;
132 if (RAND_bytes(p
, padlen
) != 1) {
133 fp_zero_multi(&e
, &n
, NULL
);
144 memcpy(p
, from
, flen
);
146 assert((p
- p0
) == size
- 1);
148 fp_init_multi(&enc
, &dec
, NULL
);
149 fp_read_unsigned_bin(&dec
, p0
, size
- 1);
152 res
= fp_exptmod(&dec
, &e
, &n
, &enc
);
154 fp_zero_multi(&dec
, &e
, &n
, NULL
);
161 ssize
= fp_unsigned_bin_size(&enc
);
162 assert(size
>= ssize
);
163 fp_to_unsigned_bin(&enc
, to
);
172 tfm_rsa_public_decrypt(int flen
, const unsigned char* from
,
173 unsigned char* to
, RSA
* rsa
, int padding
)
180 if (padding
!= RSA_PKCS1_PADDING
)
183 if (flen
> RSA_size(rsa
))
190 /* Check that the exponent is larger then 3 */
191 if (mp_int_compare_value(&e
, 3) <= 0) {
192 fp_zero_multi(&e
, &n
, NULL
);
197 fp_init_multi(&s
, &us
, NULL
);
198 fp_read_unsigned_bin(&s
, rk_UNCONST(from
), flen
);
200 if (fp_cmp(&s
, &n
) >= 0) {
201 fp_zero_multi(&e
, &n
, NULL
);
205 res
= fp_exptmod(&s
, &e
, &n
, &us
);
207 fp_zero_multi(&s
, &e
, &n
, NULL
);
214 size
= fp_unsigned_bin_size(&us
);
215 assert(size
<= RSA_size(rsa
));
216 fp_to_unsigned_bin(&us
, p
);
220 /* head zero was skipped by fp_to_unsigned_bin */
226 while (size
&& *p
== 0xff) {
229 if (size
== 0 || *p
!= 0)
233 memmove(to
, p
, size
);
239 tfm_rsa_private_encrypt(int flen
, const unsigned char* from
,
240 unsigned char* to
, RSA
* rsa
, int padding
)
242 unsigned char *p
, *p0
;
245 fp_int in
, out
, n
, e
;
247 if (padding
!= RSA_PKCS1_PADDING
)
250 size
= RSA_size(rsa
);
252 if (size
< RSA_PKCS1_PADDING_SIZE
|| size
- RSA_PKCS1_PADDING_SIZE
< flen
)
255 p0
= p
= malloc(size
);
258 memset(p
, 0xff, size
- flen
- 3);
259 p
+= size
- flen
- 3;
261 memcpy(p
, from
, flen
);
263 assert((p
- p0
) == size
);
268 fp_init_multi(&in
, &out
, NULL
);
269 fp_read_unsigned_bin(&in
, p0
, size
);
272 if(fp_isneg(&in
) || fp_cmp(&in
, &n
) >= 0) {
277 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
278 fp_int p
, q
, dmp1
, dmq1
, iqmp
;
282 BN2mpz(&dmp1
, rsa
->dmp1
);
283 BN2mpz(&dmq1
, rsa
->dmq1
);
284 BN2mpz(&iqmp
, rsa
->iqmp
);
286 res
= tfm_rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
288 fp_zero_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
298 res
= fp_exptmod(&in
, &d
, &n
, &out
);
308 ssize
= fp_unsigned_bin_size(&out
);
309 assert(size
>= ssize
);
310 fp_to_unsigned_bin(&out
, to
);
315 fp_zero_multi(&e
, &n
, &in
, &out
, NULL
);
321 tfm_rsa_private_decrypt(int flen
, const unsigned char* from
,
322 unsigned char* to
, RSA
* rsa
, int padding
)
327 fp_int in
, out
, n
, e
;
329 if (padding
!= RSA_PKCS1_PADDING
)
332 size
= RSA_size(rsa
);
336 fp_init_multi(&in
, &out
, NULL
);
341 fp_read_unsigned_bin(&in
, rk_UNCONST(from
), flen
);
343 if(fp_isneg(&in
) || fp_cmp(&in
, &n
) >= 0) {
348 if (rsa
->p
&& rsa
->q
&& rsa
->dmp1
&& rsa
->dmq1
&& rsa
->iqmp
) {
349 fp_int p
, q
, dmp1
, dmq1
, iqmp
;
353 BN2mpz(&dmp1
, rsa
->dmp1
);
354 BN2mpz(&dmq1
, rsa
->dmq1
);
355 BN2mpz(&iqmp
, rsa
->iqmp
);
357 res
= tfm_rsa_private_calculate(&in
, &p
, &q
, &dmp1
, &dmq1
, &iqmp
, &out
);
359 fp_zero_multi(&p
, &q
, &dmp1
, &dmq1
, &iqmp
, NULL
);
369 if(fp_isneg(&in
) || fp_cmp(&in
, &n
) >= 0)
373 res
= fp_exptmod(&in
, &d
, &n
, &out
);
384 ssize
= fp_unsigned_bin_size(&out
);
385 assert(size
>= ssize
);
386 fp_to_unsigned_bin(&out
, ptr
);
390 /* head zero was skipped by mp_int_to_unsigned */
396 while (size
&& *ptr
!= 0) {
403 memmove(to
, ptr
, size
);
406 fp_zero_multi(&e
, &n
, &in
, &out
, NULL
);
418 size
= fp_unsigned_bin_size(s
);
420 if (p
== NULL
&& size
!= 0)
423 fp_to_unsigned_bin(s
, p
);
425 bn
= BN_bin2bn(p
, size
, NULL
);
431 random_num(fp_int
*num
, size_t len
)
439 if (RAND_bytes(p
, len
) != 1) {
443 fp_read_unsigned_bin(num
, p
, len
);
448 #define CHECK(f, v) if ((f) != (v)) { goto out; }
451 tfm_rsa_generate_key(RSA
*rsa
, int bits
, BIGNUM
*e
, BN_GENCB
*cb
)
453 fp_int el
, p
, q
, n
, d
, dmp1
, dmq1
, iqmp
, t1
, t2
, t3
;
454 int counter
, ret
, bitsp
;
459 bitsp
= (bits
+ 1) / 2;
463 fp_init_multi(&el
, &p
, &q
, &n
, &n
, &d
, &dmp1
, &dmq1
, &iqmp
, &t1
, &t2
, &t3
, NULL
);
467 /* generate p and q so that p != q and bits(pq) ~ bits */
470 BN_GENCB_call(cb
, 2, counter
++);
471 CHECK(random_num(&p
, bitsp
), 0);
472 CHECK(fp_find_prime(&p
), FP_YES
);
474 fp_sub_d(&p
, 1, &t1
);
475 fp_gcd(&t1
, &el
, &t2
);
476 } while(fp_cmp_d(&t2
, 1) != 0);
478 BN_GENCB_call(cb
, 3, 0);
482 BN_GENCB_call(cb
, 2, counter
++);
483 CHECK(random_num(&q
, bits
- bitsp
), 0);
484 CHECK(fp_find_prime(&q
), FP_YES
);
486 if (fp_cmp(&p
, &q
) == 0) /* don't let p and q be the same */
489 fp_sub_d(&q
, 1, &t1
);
490 fp_gcd(&t1
, &el
, &t2
);
491 } while(fp_cmp_d(&t2
, 1) != 0);
494 if (fp_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 fp_sub_d(&p
, 1, &t1
);
508 fp_sub_d(&q
, 1, &t2
);
509 fp_mul(&t1
, &t2
, &t3
);
510 fp_invmod(&el
, &t3
, &d
);
512 /* calculate dmp1 dmp1 = d mod (p-1) */
513 fp_mod(&d
, &t1
, &dmp1
);
514 /* calculate dmq1 dmq1 = d mod (q-1) */
515 fp_mod(&d
, &t2
, &dmq1
);
516 /* calculate iqmp iqmp = 1/q mod p */
517 fp_invmod(&q
, &p
, &iqmp
);
519 /* fill in RSA key */
521 rsa
->e
= mpz2BN(&el
);
526 rsa
->dmp1
= mpz2BN(&dmp1
);
527 rsa
->dmq1
= mpz2BN(&dmq1
);
528 rsa
->iqmp
= mpz2BN(&iqmp
);
533 fp_zero_multi(&el
, &p
, &q
, &n
, &d
, &dmp1
,
534 &dmq1
, &iqmp
, &t1
, &t2
, &t3
, NULL
);
540 tfm_rsa_init(RSA
*rsa
)
546 tfm_rsa_finish(RSA
*rsa
)
551 const RSA_METHOD hc_rsa_tfm_method
= {
553 tfm_rsa_public_encrypt
,
554 tfm_rsa_public_decrypt
,
555 tfm_rsa_private_encrypt
,
556 tfm_rsa_private_decrypt
,
573 #ifdef USE_HCRYPTO_TFM
574 return &hc_rsa_tfm_method
;