2 * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/types.h>
26 #include <sys/syslog.h>
29 # include <sys/kmem.h>
30 # define logmessage log
43 #define USE_ARG(x) /*LINTED*/(void)&(x)
46 #define RSA_MAX_MODULUS_BITS 16384
47 #define RSA_SMALL_MODULUS_BITS 3072
48 #define RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */
51 rsa_padding_check_none(uint8_t *to
, int tlen
, const uint8_t *from
, int flen
, int num
)
55 printf("r too large\n");
58 (void) memset(to
, 0x0, tlen
- flen
);
59 (void) memcpy(to
+ tlen
- flen
, from
, flen
);
64 lowlevel_rsa_private_encrypt(int plainc
, const unsigned char *plain
, unsigned char *encbuf
, RSA
*rsa
)
78 nbytes
= BN_num_bytes(rsa
->n
);
79 decbuf
= netpgp_allocate(1, nbytes
);
81 memcpy(decbuf
, plain
, plainc
);
82 BN_bin2bn(decbuf
, nbytes
, decbn
);
83 if (BN_cmp(decbn
, rsa
->n
) >= 0) {
84 printf("decbn too big\n");
87 if (!BN_mod_exp(signedbn
, decbn
, rsa
->d
, rsa
->n
, NULL
)) {
88 printf("bad mod_exp\n");
91 signedbytes
= BN_num_bytes(signedbn
);
92 signc
= BN_bn2bin(signedbn
, &encbuf
[nbytes
- signedbytes
]);
93 memset(encbuf
, 0x0, nbytes
- signc
);
96 netpgp_deallocate(decbuf
, nbytes
);
98 BN_clear_free(signedbn
);
103 lowlevel_rsa_public_encrypt(int plainc
, const unsigned char *plain
, unsigned char *encbuf
, RSA
*rsa
)
116 nbytes
= BN_num_bytes(rsa
->n
);
117 decbuf
= netpgp_allocate(1, nbytes
);
118 (void) memcpy(decbuf
, plain
, plainc
);
119 if (BN_bin2bn(decbuf
, nbytes
, decbn
) == NULL
) {
120 printf("bin2bn failed\n");
123 if (BN_cmp(decbn
, rsa
->n
) >= 0) {
124 printf("BN_cmp failed\n");
127 if (!BN_mod_exp(encbn
, decbn
, rsa
->e
, rsa
->n
, NULL
)) {
128 printf("BN_mod_exp failed\n");
131 encc
= BN_num_bytes(encbn
);
132 i
= BN_bn2bin(encbn
, &encbuf
[nbytes
- encc
]);
133 (void) memset(encbuf
, 0x0, nbytes
- i
);
137 memset(decbuf
, 0x0, nbytes
);
138 netpgp_deallocate(decbuf
, nbytes
);
140 BN_clear_free(decbn
);
141 BN_clear_free(encbn
);
146 lowlevel_rsa_private_decrypt(int enclen
, const unsigned char *encbuf
, unsigned char *to
, RSA
*rsa
)
156 decbn
= encbn
= NULL
;
158 if (BN_num_bits(rsa
->n
) > RSA_MAX_MODULUS_BITS
) {
161 if (BN_cmp(rsa
->n
, rsa
->e
) <= 0) {
166 nbytes
= BN_num_bytes(rsa
->n
);
167 buf
= netpgp_allocate(1, nbytes
);
168 if (enclen
> nbytes
) {
169 printf("bad enclen\n");
172 BN_bin2bn(encbuf
, enclen
, encbn
);
173 if (BN_cmp(encbn
, rsa
->n
) >= 0) {
174 printf("bad encbn\n");
177 BN_mod_exp(decbn
, encbn
, rsa
->d
, rsa
->n
, NULL
);
178 j
= BN_bn2bin(decbn
, buf
);
179 r
= rsa_padding_check_none(to
, nbytes
, buf
, j
, nbytes
);
181 BN_clear_free(encbn
);
182 BN_clear_free(decbn
);
183 netpgp_deallocate(buf
, nbytes
);
188 lowlevel_rsa_public_decrypt(const uint8_t *encbuf
, int enclen
, uint8_t *dec
, const rsa_pubkey_t
*rsa
)
200 decbn
= encbn
= NULL
;
201 if (BN_num_bits(rsa
->n
) > RSA_MAX_MODULUS_BITS
) {
202 printf("rsa r modulus too large\n");
205 if (BN_cmp(rsa
->n
, rsa
->e
) <= 0) {
206 printf("rsa r bad n value\n");
209 if (BN_num_bits(rsa
->n
) > RSA_SMALL_MODULUS_BITS
&&
210 BN_num_bits(rsa
->e
) > RSA_MAX_PUBEXP_BITS
) {
211 printf("rsa r bad exponent limit\n");
214 if ((encbn
= BN_new()) == NULL
||
215 (decbn
= BN_new()) == NULL
||
216 (decbuf
= netpgp_allocate(1, nbytes
= BN_num_bytes(rsa
->n
))) == NULL
) {
217 printf("allocation failure\n");
220 if (enclen
> nbytes
) {
221 printf("rsa r > mod len\n");
224 if (BN_bin2bn(encbuf
, enclen
, encbn
) == NULL
) {
225 printf("null encrypted BN\n");
228 if (BN_cmp(encbn
, rsa
->n
) >= 0) {
229 printf("rsa r data too large for modulus\n");
232 if (BN_mod_exp(decbn
, encbn
, rsa
->e
, rsa
->n
, NULL
) < 0) {
233 printf("BN_mod_exp < 0\n");
236 decbytes
= BN_num_bytes(decbn
);
237 (void) BN_bn2bin(decbn
, decbuf
);
238 if ((r
= rsa_padding_check_none(dec
, nbytes
, decbuf
, decbytes
, 0)) < 0) {
239 printf("rsa r padding check failed\n");
244 if (decbuf
!= NULL
) {
245 (void) memset(decbuf
, 0x0, nbytes
);
246 netpgp_deallocate(decbuf
, nbytes
);
254 RSA key generation, Tom St Denis
259 @param prng An active PRNG state
260 @param wprng The index of the PRNG desired
261 @param size The size of the modulus (key size) desired (octets)
262 @param e The "e" value (public key). e==65537 is a good choice
263 @param key [out] Destination of a newly created private key pair
264 @return CRYPT_OK if successful, upon error all allocated ram is freed
267 rsa_make_key(prng_state
*prng
, int wprng
, int size
, long e
, rsa_key
*key
)
269 void *p
, *q
, *tmp1
, *tmp2
, *tmp3
;
272 LTC_ARGCHK(ltc_mp
.name
!= NULL
);
273 LTC_ARGCHK(key
!= NULL
);
275 if ((size
< (MIN_RSA_SIZE
/8)) || (size
> (MAX_RSA_SIZE
/8))) {
276 return CRYPT_INVALID_KEYSIZE
;
279 if ((e
< 3) || ((e
& 1) == 0)) {
280 return CRYPT_INVALID_ARG
;
283 if ((err
= prng_is_valid(wprng
)) != CRYPT_OK
) {
287 if ((err
= mp_init_multi(&p
, &q
, &tmp1
, &tmp2
, &tmp3
, NULL
)) != CRYPT_OK
) {
291 /* make primes p and q (optimization provided by Wayne Scott) */
293 if ((err
= mp_set_int(tmp3
, e
)) != CRYPT_OK
) {
299 if ((err
= rand_prime( p
, size
/2, prng
, wprng
)) != CRYPT_OK
) {
303 if ((err
= mp_sub_d( p
, 1, tmp1
)) != CRYPT_OK
) {
306 /* tmp2 = gcd(p-1, e) */
307 if ((err
= mp_gcd( tmp1
, tmp3
, tmp2
)) != CRYPT_OK
) {
310 } while (mp_cmp_d( tmp2
, 1) != 0);
311 /* while e divides p-1 */
315 if ((err
= rand_prime( q
, size
/2, prng
, wprng
)) != CRYPT_OK
) {
319 if ((err
= mp_sub_d( q
, 1, tmp1
)) != CRYPT_OK
) {
322 /* tmp2 = gcd(q-1, e) */
323 if ((err
= mp_gcd( tmp1
, tmp3
, tmp2
)) != CRYPT_OK
) {
326 } while (mp_cmp_d( tmp2
, 1) != 0);
327 /* while e divides q-1 */
329 /* tmp1 = lcm(p-1, q-1) */
331 if ((err
= mp_sub_d( p
, 1, tmp2
)) != CRYPT_OK
) {
334 /* tmp1 = q-1 (previous do/while loop) */
335 /* tmp1 = lcm(p-1, q-1) */
336 if ((err
= mp_lcm( tmp1
, tmp2
, tmp1
)) != CRYPT_OK
) {
341 if ((err
= mp_init_multi(&key
->e
, &key
->d
, &key
->N
, &key
->dQ
, &key
->dP
, &key
->qP
, &key
->p
, &key
->q
, NULL
)) != CRYPT_OK
) {
346 if ((err
= mp_set_int( key
->e
, e
)) != CRYPT_OK
) {
349 /* key->d = 1/e mod lcm(p-1,q-1) */
350 if ((err
= mp_invmod( key
->e
, tmp1
, key
->d
)) != CRYPT_OK
) {
354 if ((err
= mp_mul( p
, q
, key
->N
)) != CRYPT_OK
) {
358 /* optimize for CRT now */
359 /* find d mod q-1 and d mod p-1 */
361 if ((err
= mp_sub_d( p
, 1, tmp1
)) != CRYPT_OK
) {
365 if ((err
= mp_sub_d( q
, 1, tmp2
)) != CRYPT_OK
) {
369 if ((err
= mp_mod( key
->d
, tmp1
, key
->dP
)) != CRYPT_OK
) {
373 if ((err
= mp_mod( key
->d
, tmp2
, key
->dQ
)) != CRYPT_OK
) {
377 if ((err
= mp_invmod( q
, p
, key
->qP
)) != CRYPT_OK
) {
381 if ((err
= mp_copy( p
, key
->p
)) != CRYPT_OK
) {
384 if ((err
= mp_copy( q
, key
->q
)) != CRYPT_OK
) {
388 /* set key type (in this case it's CRT optimized) */
389 key
->type
= PK_PRIVATE
;
391 /* return ok and free temps */
395 mp_clear_multi(key
->d
, key
->e
, key
->N
, key
->dQ
, key
->dP
, key
->qP
, key
->p
, key
->q
, NULL
);
397 mp_clear_multi(tmp3
, tmp2
, tmp1
, p
, q
, NULL
);
402 #define HASHBUF_LEN 512
404 #define DSA_MAX_MODULUS_BITS 10000
407 dsa_do_verify(const unsigned char *calculated
, int dgst_len
, const dsasig_t
*sig
, mpi_dsa_t
*dsa
)
415 if (dsa
->p
== NULL
|| dsa
->q
== NULL
|| dsa
->g
== NULL
) {
419 qbits
= BN_num_bits(dsa
->q
);
424 /* openssl sources say these are the valid values */
425 /* according to FIPS 186-3 */
428 printf("dsa: bad # of Q bits\n");
431 if (BN_num_bits(dsa
->p
) > DSA_MAX_MODULUS_BITS
) {
432 printf("dsa: p too large\n");
435 /* no love for SHA512? */
436 if (dgst_len
> SHA256_DIGEST_LENGTH
) {
437 printf("dsa: digest too long\n");
441 if ((M
= BN_new()) == NULL
||
442 (W
= BN_new()) == NULL
||
443 (t1
= BN_new()) == NULL
) {
446 if (BN_is_zero(sig
->r
) ||
447 BN_is_negative(sig
->r
) ||
448 BN_cmp(sig
->r
, dsa
->q
) >= 0) {
451 if (BN_is_zero(sig
->s
) ||
452 BN_is_negative(sig
->s
) ||
453 BN_cmp(sig
->s
, dsa
->q
) >= 0) {
456 if (BN_mod_inverse(W
, sig
->s
, dsa
->q
, NULL
) != MP_OKAY
) {
459 if (dgst_len
> qbits
/ 8) {
460 dgst_len
= qbits
/ 8;
462 if (BN_bin2bn(calculated
, dgst_len
, M
) == NULL
) {
465 if (!BN_mod_mul(M
, M
, W
, dsa
->q
, NULL
)) {
468 if (!BN_mod_mul(W
, sig
->r
, W
, dsa
->q
, NULL
)) {
471 if (!BN_mod_exp(dsa
->p
, t1
, dsa
->g
, M
, NULL
)) {
474 if (!BN_div(NULL
, M
, t1
, dsa
->q
, NULL
)) {
477 ret
= (BN_cmp(M
, sig
->r
) == 0);
491 /*************************************************************************/
494 RSA_size(const RSA
*rsa
)
496 return (rsa
== NULL
) ? 0 : BN_num_bits(rsa
->n
);
500 DSA_size(const DSA
*dsa
)
502 return (dsa
== NULL
) ? 0 : BN_num_bits(dsa
->p
);
506 dsa_verify(const signature_t
*signature
, const dsa_pubkey_t
*pubdsa
, const uint8_t *calculated
, size_t hash_length
)
513 if (signature
== NULL
|| pubdsa
== NULL
|| calculated
== NULL
) {
516 (void) memset(&osig
, 0x0, sizeof(osig
));
517 (void) memset(&odsa
, 0x0, sizeof(odsa
));
518 BN_copy(osig
.r
, signature
->dsa
.r
);
519 BN_copy(osig
.s
, signature
->dsa
.s
);
523 odsa
.pub_key
= pubdsa
->y
;
524 if ((qlen
= BN_num_bytes(odsa
.q
)) < hash_length
) {
527 ret
= dsa_do_verify(calculated
, (int)hash_length
, &signature
->dsa
, &odsa
);
534 BN_free(odsa
.pub_key
);
535 odsa
.p
= odsa
.q
= odsa
.g
= odsa
.pub_key
= NULL
;
538 osig
.r
= osig
.s
= NULL
;
539 return (unsigned)ret
;
545 return netpgp_allocate(1, sizeof(RSA
));
552 netpgp_deallocate(rsa
, sizeof(*rsa
));
557 RSA_check_key(RSA
*rsa
)
563 if (rsa
== NULL
|| rsa
->p
== NULL
|| rsa
->q
== NULL
|| rsa
->n
== NULL
) {
566 /* check that p and q are coprime, and that n = p*q. */
567 if (!BN_is_prime(rsa
->p
, 1, NULL
, NULL
, NULL
) ||
568 !BN_is_prime(rsa
->q
, 1, NULL
, NULL
, NULL
)) {
572 BN_mul(calcn
, rsa
->p
, rsa
->q
, NULL
);
573 if (BN_cmp(calcn
, rsa
->n
) != 0) {
576 /* XXX - check that d*e = 1 mod (p-1*q-1) */
579 BN_clear_free(calcn
);
584 RSA_generate_key(int num
, unsigned long e
, void (*callback
)(int,int,void *), void *cb_arg
)
591 printf("RSA_generate_key stubbed\n");
597 RSA_public_encrypt(int plainc
, const unsigned char *plain
, unsigned char *encbuf
, RSA
*rsa
, int padding
)
600 if (plain
== NULL
|| encbuf
== NULL
|| rsa
== NULL
) {
603 return lowlevel_rsa_public_encrypt(plainc
, plain
, encbuf
, rsa
);
608 RSA_private_decrypt(int flen
, const unsigned char *from
, unsigned char *to
, RSA
*rsa
, int padding
)
611 if (from
== NULL
|| to
== NULL
|| rsa
== NULL
) {
614 return lowlevel_rsa_private_decrypt(flen
, from
, to
, rsa
);
619 RSA_private_encrypt(int plainc
, const unsigned char *plain
, unsigned char *encbuf
, RSA
*rsa
, int padding
)
622 if (plain
== NULL
|| encbuf
== NULL
|| rsa
== NULL
) {
625 return lowlevel_rsa_private_encrypt(plainc
, plain
, encbuf
, rsa
);
630 RSA_public_decrypt(int enclen
, const unsigned char *enc
, unsigned char *dec
, RSA
*rsa
, int padding
)
635 if (enc
== NULL
|| dec
== NULL
|| rsa
== NULL
) {
639 (void) memset(&pub
, 0x0, sizeof(pub
));
640 pub
.n
= BN_dup(rsa
->n
);
641 pub
.e
= BN_dup(rsa
->e
);
642 ret
= lowlevel_rsa_public_decrypt(enc
, enclen
, dec
, &pub
);
648 /***********************************************************************/
653 return netpgp_allocate(1, sizeof(DSA
));
660 netpgp_deallocate(dsa
, sizeof(*dsa
));
667 return netpgp_allocate(1, sizeof(DSA_SIG
));
671 DSA_SIG_free(DSA_SIG
*sig
)
674 netpgp_deallocate(sig
, sizeof(*sig
));
679 DSA_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
685 printf("DSA_do_sign stubbed\n");
686 return DSA_SIG_new();
690 DSA_do_verify(const unsigned char *dgst
, int dgst_len
, DSA_SIG
*sig
, DSA
*dsa
)
692 if (dgst
== NULL
|| dgst_len
== 0 || sig
== NULL
|| dsa
== NULL
) {
695 return dsa_do_verify(dgst
, dgst_len
, sig
, dsa
);