xref: /minix3/crypto/external/bsd/netpgp/dist/src/librsa/rsa.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*-
2*ebfedea0SLionel Sambuc  * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3*ebfedea0SLionel Sambuc  * All rights reserved.
4*ebfedea0SLionel Sambuc  *
5*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
7*ebfedea0SLionel Sambuc  * are met:
8*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
10*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
11*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
12*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
13*ebfedea0SLionel Sambuc  *
14*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*ebfedea0SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*ebfedea0SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*ebfedea0SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*ebfedea0SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*ebfedea0SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*ebfedea0SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*ebfedea0SLionel Sambuc  */
25*ebfedea0SLionel Sambuc #include <sys/types.h>
26*ebfedea0SLionel Sambuc #include <sys/syslog.h>
27*ebfedea0SLionel Sambuc 
28*ebfedea0SLionel Sambuc #ifdef _KERNEL
29*ebfedea0SLionel Sambuc # include <sys/kmem.h>
30*ebfedea0SLionel Sambuc # define logmessage	log
31*ebfedea0SLionel Sambuc #else
32*ebfedea0SLionel Sambuc # include <stdio.h>
33*ebfedea0SLionel Sambuc # include <stdlib.h>
34*ebfedea0SLionel Sambuc # include <string.h>
35*ebfedea0SLionel Sambuc # include <unistd.h>
36*ebfedea0SLionel Sambuc #endif
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc #include "misc.h"
39*ebfedea0SLionel Sambuc #include "digest.h"
40*ebfedea0SLionel Sambuc #include "rsa.h"
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc #ifndef USE_ARG
43*ebfedea0SLionel Sambuc #define USE_ARG(x)	/*LINTED*/(void)&(x)
44*ebfedea0SLionel Sambuc #endif
45*ebfedea0SLionel Sambuc 
46*ebfedea0SLionel Sambuc #define RSA_MAX_MODULUS_BITS	16384
47*ebfedea0SLionel Sambuc #define RSA_SMALL_MODULUS_BITS	3072
48*ebfedea0SLionel Sambuc #define RSA_MAX_PUBEXP_BITS	64 /* exponent limit enforced for "large" modulus only */
49*ebfedea0SLionel Sambuc 
50*ebfedea0SLionel Sambuc static int
rsa_padding_check_none(uint8_t * to,int tlen,const uint8_t * from,int flen,int num)51*ebfedea0SLionel Sambuc rsa_padding_check_none(uint8_t *to, int tlen, const uint8_t *from, int flen, int num)
52*ebfedea0SLionel Sambuc {
53*ebfedea0SLionel Sambuc 	USE_ARG(num);
54*ebfedea0SLionel Sambuc 	if (flen > tlen) {
55*ebfedea0SLionel Sambuc 		printf("r too large\n");
56*ebfedea0SLionel Sambuc 		return -1;
57*ebfedea0SLionel Sambuc 	}
58*ebfedea0SLionel Sambuc 	(void) memset(to, 0x0, tlen - flen);
59*ebfedea0SLionel Sambuc 	(void) memcpy(to + tlen - flen, from, flen);
60*ebfedea0SLionel Sambuc 	return tlen;
61*ebfedea0SLionel Sambuc }
62*ebfedea0SLionel Sambuc 
63*ebfedea0SLionel Sambuc static int
lowlevel_rsa_private_encrypt(int plainc,const unsigned char * plain,unsigned char * encbuf,RSA * rsa)64*ebfedea0SLionel Sambuc lowlevel_rsa_private_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa)
65*ebfedea0SLionel Sambuc {
66*ebfedea0SLionel Sambuc 	BIGNUM	*decbn;
67*ebfedea0SLionel Sambuc 	BIGNUM	*signedbn;
68*ebfedea0SLionel Sambuc 	uint8_t	*decbuf;
69*ebfedea0SLionel Sambuc 	int	 nbytes;
70*ebfedea0SLionel Sambuc 	int	 signc;
71*ebfedea0SLionel Sambuc 	int	 signedbytes;
72*ebfedea0SLionel Sambuc 	int	 r;
73*ebfedea0SLionel Sambuc 
74*ebfedea0SLionel Sambuc 	decbuf = NULL;
75*ebfedea0SLionel Sambuc 	r = -1;
76*ebfedea0SLionel Sambuc 	decbn = BN_new();
77*ebfedea0SLionel Sambuc 	signedbn = BN_new();
78*ebfedea0SLionel Sambuc 	nbytes = BN_num_bytes(rsa->n);
79*ebfedea0SLionel Sambuc 	decbuf = netpgp_allocate(1, nbytes);
80*ebfedea0SLionel Sambuc 	/* add no padding */
81*ebfedea0SLionel Sambuc 	memcpy(decbuf, plain, plainc);
82*ebfedea0SLionel Sambuc 	BN_bin2bn(decbuf, nbytes, decbn);
83*ebfedea0SLionel Sambuc 	if (BN_cmp(decbn, rsa->n) >= 0) {
84*ebfedea0SLionel Sambuc 		printf("decbn too big\n");
85*ebfedea0SLionel Sambuc 		goto err;
86*ebfedea0SLionel Sambuc 	}
87*ebfedea0SLionel Sambuc 	if (!BN_mod_exp(signedbn, decbn, rsa->d, rsa->n, NULL)) {
88*ebfedea0SLionel Sambuc 		printf("bad mod_exp\n");
89*ebfedea0SLionel Sambuc 		goto err;
90*ebfedea0SLionel Sambuc 	}
91*ebfedea0SLionel Sambuc 	signedbytes = BN_num_bytes(signedbn);
92*ebfedea0SLionel Sambuc 	signc = BN_bn2bin(signedbn, &encbuf[nbytes - signedbytes]);
93*ebfedea0SLionel Sambuc 	memset(encbuf, 0x0, nbytes - signc);
94*ebfedea0SLionel Sambuc 	r = nbytes;
95*ebfedea0SLionel Sambuc err:
96*ebfedea0SLionel Sambuc 	netpgp_deallocate(decbuf, nbytes);
97*ebfedea0SLionel Sambuc 	BN_clear_free(decbn);
98*ebfedea0SLionel Sambuc 	BN_clear_free(signedbn);
99*ebfedea0SLionel Sambuc 	return r;
100*ebfedea0SLionel Sambuc }
101*ebfedea0SLionel Sambuc 
102*ebfedea0SLionel Sambuc static int
lowlevel_rsa_public_encrypt(int plainc,const unsigned char * plain,unsigned char * encbuf,RSA * rsa)103*ebfedea0SLionel Sambuc lowlevel_rsa_public_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa)
104*ebfedea0SLionel Sambuc {
105*ebfedea0SLionel Sambuc 	BIGNUM	*decbn;
106*ebfedea0SLionel Sambuc 	BIGNUM	*encbn;
107*ebfedea0SLionel Sambuc 	uint8_t	*decbuf;
108*ebfedea0SLionel Sambuc 	int	 nbytes;
109*ebfedea0SLionel Sambuc 	int	 encc;
110*ebfedea0SLionel Sambuc 	int	 r;
111*ebfedea0SLionel Sambuc 	int	 i;
112*ebfedea0SLionel Sambuc 
113*ebfedea0SLionel Sambuc 	r = -1;
114*ebfedea0SLionel Sambuc 	decbn = BN_new();
115*ebfedea0SLionel Sambuc 	encbn = BN_new();
116*ebfedea0SLionel Sambuc 	nbytes = BN_num_bytes(rsa->n);
117*ebfedea0SLionel Sambuc 	decbuf = netpgp_allocate(1, nbytes);
118*ebfedea0SLionel Sambuc 	(void) memcpy(decbuf, plain, plainc);
119*ebfedea0SLionel Sambuc 	if (BN_bin2bn(decbuf, nbytes, decbn) == NULL) {
120*ebfedea0SLionel Sambuc 		printf("bin2bn failed\n");
121*ebfedea0SLionel Sambuc 		goto err;
122*ebfedea0SLionel Sambuc 	}
123*ebfedea0SLionel Sambuc 	if (BN_cmp(decbn, rsa->n) >= 0) {
124*ebfedea0SLionel Sambuc 		printf("BN_cmp failed\n");
125*ebfedea0SLionel Sambuc 		goto err;
126*ebfedea0SLionel Sambuc 	}
127*ebfedea0SLionel Sambuc 	if (!BN_mod_exp(encbn, decbn, rsa->e, rsa->n, NULL)) {
128*ebfedea0SLionel Sambuc 		printf("BN_mod_exp failed\n");
129*ebfedea0SLionel Sambuc 		goto err;
130*ebfedea0SLionel Sambuc 	}
131*ebfedea0SLionel Sambuc 	encc = BN_num_bytes(encbn);
132*ebfedea0SLionel Sambuc 	i = BN_bn2bin(encbn, &encbuf[nbytes - encc]);
133*ebfedea0SLionel Sambuc 	(void) memset(encbuf, 0x0, nbytes - i);
134*ebfedea0SLionel Sambuc 	r = nbytes;
135*ebfedea0SLionel Sambuc err:
136*ebfedea0SLionel Sambuc 	if (decbuf) {
137*ebfedea0SLionel Sambuc 		memset(decbuf, 0x0, nbytes);
138*ebfedea0SLionel Sambuc 		netpgp_deallocate(decbuf, nbytes);
139*ebfedea0SLionel Sambuc 	}
140*ebfedea0SLionel Sambuc 	BN_clear_free(decbn);
141*ebfedea0SLionel Sambuc 	BN_clear_free(encbn);
142*ebfedea0SLionel Sambuc 	return r;
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc 
145*ebfedea0SLionel Sambuc static int
lowlevel_rsa_private_decrypt(int enclen,const unsigned char * encbuf,unsigned char * to,RSA * rsa)146*ebfedea0SLionel Sambuc lowlevel_rsa_private_decrypt(int enclen, const unsigned char *encbuf, unsigned char *to, RSA *rsa)
147*ebfedea0SLionel Sambuc {
148*ebfedea0SLionel Sambuc 	BIGNUM	*encbn;
149*ebfedea0SLionel Sambuc 	BIGNUM	*decbn;
150*ebfedea0SLionel Sambuc 	uint8_t	*buf;
151*ebfedea0SLionel Sambuc 	int	 nbytes;
152*ebfedea0SLionel Sambuc 	int	 j;
153*ebfedea0SLionel Sambuc 	int	 r;
154*ebfedea0SLionel Sambuc 
155*ebfedea0SLionel Sambuc 	r = -1;
156*ebfedea0SLionel Sambuc 	decbn = encbn = NULL;
157*ebfedea0SLionel Sambuc 	buf = NULL;
158*ebfedea0SLionel Sambuc 	if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) {
159*ebfedea0SLionel Sambuc 		return -1;
160*ebfedea0SLionel Sambuc 	}
161*ebfedea0SLionel Sambuc 	if (BN_cmp(rsa->n, rsa->e) <= 0) {
162*ebfedea0SLionel Sambuc 		return -1;
163*ebfedea0SLionel Sambuc 	}
164*ebfedea0SLionel Sambuc 	encbn = BN_new();
165*ebfedea0SLionel Sambuc 	decbn = BN_new();
166*ebfedea0SLionel Sambuc 	nbytes = BN_num_bytes(rsa->n);
167*ebfedea0SLionel Sambuc 	buf = netpgp_allocate(1, nbytes);
168*ebfedea0SLionel Sambuc 	if (enclen > nbytes) {
169*ebfedea0SLionel Sambuc 		printf("bad enclen\n");
170*ebfedea0SLionel Sambuc 		goto err;
171*ebfedea0SLionel Sambuc 	}
172*ebfedea0SLionel Sambuc 	BN_bin2bn(encbuf, enclen, encbn);
173*ebfedea0SLionel Sambuc 	if (BN_cmp(encbn, rsa->n) >= 0) {
174*ebfedea0SLionel Sambuc 		printf("bad encbn\n");
175*ebfedea0SLionel Sambuc 		goto err;
176*ebfedea0SLionel Sambuc 	}
177*ebfedea0SLionel Sambuc 	BN_mod_exp(decbn, encbn, rsa->d, rsa->n, NULL);
178*ebfedea0SLionel Sambuc 	j = BN_bn2bin(decbn, buf);
179*ebfedea0SLionel Sambuc 	r = rsa_padding_check_none(to, nbytes, buf, j, nbytes);
180*ebfedea0SLionel Sambuc err:
181*ebfedea0SLionel Sambuc 	BN_clear_free(encbn);
182*ebfedea0SLionel Sambuc 	BN_clear_free(decbn);
183*ebfedea0SLionel Sambuc 	netpgp_deallocate(buf, nbytes);
184*ebfedea0SLionel Sambuc 	return r;
185*ebfedea0SLionel Sambuc }
186*ebfedea0SLionel Sambuc 
187*ebfedea0SLionel Sambuc static int
lowlevel_rsa_public_decrypt(const uint8_t * encbuf,int enclen,uint8_t * dec,const rsa_pubkey_t * rsa)188*ebfedea0SLionel Sambuc lowlevel_rsa_public_decrypt(const uint8_t *encbuf, int enclen, uint8_t *dec, const rsa_pubkey_t *rsa)
189*ebfedea0SLionel Sambuc {
190*ebfedea0SLionel Sambuc 	uint8_t		*decbuf;
191*ebfedea0SLionel Sambuc 	BIGNUM		*decbn;
192*ebfedea0SLionel Sambuc 	BIGNUM		*encbn;
193*ebfedea0SLionel Sambuc 	int		 decbytes;
194*ebfedea0SLionel Sambuc 	int		 nbytes;
195*ebfedea0SLionel Sambuc 	int		 r;
196*ebfedea0SLionel Sambuc 
197*ebfedea0SLionel Sambuc 	nbytes = 0;
198*ebfedea0SLionel Sambuc 	r = -1;
199*ebfedea0SLionel Sambuc 	decbuf = NULL;
200*ebfedea0SLionel Sambuc 	decbn = encbn = NULL;
201*ebfedea0SLionel Sambuc 	if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) {
202*ebfedea0SLionel Sambuc 		printf("rsa r modulus too large\n");
203*ebfedea0SLionel Sambuc 		goto err;
204*ebfedea0SLionel Sambuc 	}
205*ebfedea0SLionel Sambuc 	if (BN_cmp(rsa->n, rsa->e) <= 0) {
206*ebfedea0SLionel Sambuc 		printf("rsa r bad n value\n");
207*ebfedea0SLionel Sambuc 		goto err;
208*ebfedea0SLionel Sambuc 	}
209*ebfedea0SLionel Sambuc 	if (BN_num_bits(rsa->n) > RSA_SMALL_MODULUS_BITS &&
210*ebfedea0SLionel Sambuc 	    BN_num_bits(rsa->e) > RSA_MAX_PUBEXP_BITS) {
211*ebfedea0SLionel Sambuc 		printf("rsa r bad exponent limit\n");
212*ebfedea0SLionel Sambuc 		goto err;
213*ebfedea0SLionel Sambuc 	}
214*ebfedea0SLionel Sambuc 	if ((encbn = BN_new()) == NULL ||
215*ebfedea0SLionel Sambuc 	    (decbn = BN_new()) == NULL ||
216*ebfedea0SLionel Sambuc 	    (decbuf = netpgp_allocate(1, nbytes = BN_num_bytes(rsa->n))) == NULL) {
217*ebfedea0SLionel Sambuc 		printf("allocation failure\n");
218*ebfedea0SLionel Sambuc 		goto err;
219*ebfedea0SLionel Sambuc 	}
220*ebfedea0SLionel Sambuc 	if (enclen > nbytes) {
221*ebfedea0SLionel Sambuc 		printf("rsa r > mod len\n");
222*ebfedea0SLionel Sambuc 		goto err;
223*ebfedea0SLionel Sambuc 	}
224*ebfedea0SLionel Sambuc 	if (BN_bin2bn(encbuf, enclen, encbn) == NULL) {
225*ebfedea0SLionel Sambuc 		printf("null encrypted BN\n");
226*ebfedea0SLionel Sambuc 		goto err;
227*ebfedea0SLionel Sambuc 	}
228*ebfedea0SLionel Sambuc 	if (BN_cmp(encbn, rsa->n) >= 0) {
229*ebfedea0SLionel Sambuc 		printf("rsa r data too large for modulus\n");
230*ebfedea0SLionel Sambuc 		goto err;
231*ebfedea0SLionel Sambuc 	}
232*ebfedea0SLionel Sambuc 	if (BN_mod_exp(decbn, encbn, rsa->e, rsa->n, NULL) < 0) {
233*ebfedea0SLionel Sambuc 		printf("BN_mod_exp < 0\n");
234*ebfedea0SLionel Sambuc 		goto err;
235*ebfedea0SLionel Sambuc 	}
236*ebfedea0SLionel Sambuc 	decbytes = BN_num_bytes(decbn);
237*ebfedea0SLionel Sambuc 	(void) BN_bn2bin(decbn, decbuf);
238*ebfedea0SLionel Sambuc 	if ((r = rsa_padding_check_none(dec, nbytes, decbuf, decbytes, 0)) < 0) {
239*ebfedea0SLionel Sambuc 		printf("rsa r padding check failed\n");
240*ebfedea0SLionel Sambuc 	}
241*ebfedea0SLionel Sambuc err:
242*ebfedea0SLionel Sambuc 	BN_free(encbn);
243*ebfedea0SLionel Sambuc 	BN_free(decbn);
244*ebfedea0SLionel Sambuc 	if (decbuf != NULL) {
245*ebfedea0SLionel Sambuc 		(void) memset(decbuf, 0x0, nbytes);
246*ebfedea0SLionel Sambuc 		netpgp_deallocate(decbuf, nbytes);
247*ebfedea0SLionel Sambuc 	}
248*ebfedea0SLionel Sambuc 	return r;
249*ebfedea0SLionel Sambuc }
250*ebfedea0SLionel Sambuc 
251*ebfedea0SLionel Sambuc #if 0
252*ebfedea0SLionel Sambuc /**
253*ebfedea0SLionel Sambuc   @file rsa_make_key.c
254*ebfedea0SLionel Sambuc   RSA key generation, Tom St Denis
255*ebfedea0SLionel Sambuc */
256*ebfedea0SLionel Sambuc 
257*ebfedea0SLionel Sambuc /**
258*ebfedea0SLionel Sambuc    Create an RSA key
259*ebfedea0SLionel Sambuc    @param prng     An active PRNG state
260*ebfedea0SLionel Sambuc    @param wprng    The index of the PRNG desired
261*ebfedea0SLionel Sambuc    @param size     The size of the modulus (key size) desired (octets)
262*ebfedea0SLionel Sambuc    @param e        The "e" value (public key).  e==65537 is a good choice
263*ebfedea0SLionel Sambuc    @param key      [out] Destination of a newly created private key pair
264*ebfedea0SLionel Sambuc    @return CRYPT_OK if successful, upon error all allocated ram is freed
265*ebfedea0SLionel Sambuc */
266*ebfedea0SLionel Sambuc static int
267*ebfedea0SLionel Sambuc rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key)
268*ebfedea0SLionel Sambuc {
269*ebfedea0SLionel Sambuc 	void *p, *q, *tmp1, *tmp2, *tmp3;
270*ebfedea0SLionel Sambuc 	int    err;
271*ebfedea0SLionel Sambuc 
272*ebfedea0SLionel Sambuc 	LTC_ARGCHK(ltc_mp.name != NULL);
273*ebfedea0SLionel Sambuc 	LTC_ARGCHK(key         != NULL);
274*ebfedea0SLionel Sambuc 
275*ebfedea0SLionel Sambuc 	if ((size < (MIN_RSA_SIZE/8)) || (size > (MAX_RSA_SIZE/8))) {
276*ebfedea0SLionel Sambuc 		return CRYPT_INVALID_KEYSIZE;
277*ebfedea0SLionel Sambuc 	}
278*ebfedea0SLionel Sambuc 
279*ebfedea0SLionel Sambuc 	if ((e < 3) || ((e & 1) == 0)) {
280*ebfedea0SLionel Sambuc 		return CRYPT_INVALID_ARG;
281*ebfedea0SLionel Sambuc 	}
282*ebfedea0SLionel Sambuc 
283*ebfedea0SLionel Sambuc 	if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
284*ebfedea0SLionel Sambuc 		return err;
285*ebfedea0SLionel Sambuc 	}
286*ebfedea0SLionel Sambuc 
287*ebfedea0SLionel Sambuc 	if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != CRYPT_OK) {
288*ebfedea0SLionel Sambuc 		return err;
289*ebfedea0SLionel Sambuc 	}
290*ebfedea0SLionel Sambuc 
291*ebfedea0SLionel Sambuc 	/* make primes p and q (optimization provided by Wayne Scott) */
292*ebfedea0SLionel Sambuc 		/* tmp3 = e */
293*ebfedea0SLionel Sambuc 	if ((err = mp_set_int(tmp3, e)) != CRYPT_OK) {
294*ebfedea0SLionel Sambuc 		goto errkey;
295*ebfedea0SLionel Sambuc 	}
296*ebfedea0SLionel Sambuc 
297*ebfedea0SLionel Sambuc 	/* make prime "p" */
298*ebfedea0SLionel Sambuc 	do {
299*ebfedea0SLionel Sambuc 		if ((err = rand_prime( p, size/2, prng, wprng)) != CRYPT_OK) {
300*ebfedea0SLionel Sambuc 			goto errkey;
301*ebfedea0SLionel Sambuc 		}
302*ebfedea0SLionel Sambuc 		/* tmp1 = p-1 */
303*ebfedea0SLionel Sambuc 		if ((err = mp_sub_d( p, 1,  tmp1)) != CRYPT_OK) {
304*ebfedea0SLionel Sambuc 			goto errkey;
305*ebfedea0SLionel Sambuc 		}
306*ebfedea0SLionel Sambuc 		/* tmp2 = gcd(p-1, e) */
307*ebfedea0SLionel Sambuc 		if ((err = mp_gcd( tmp1,  tmp3,  tmp2)) != CRYPT_OK) {
308*ebfedea0SLionel Sambuc 			goto errkey;
309*ebfedea0SLionel Sambuc 		}
310*ebfedea0SLionel Sambuc 	} while (mp_cmp_d( tmp2, 1) != 0);
311*ebfedea0SLionel Sambuc 	/* while e divides p-1 */
312*ebfedea0SLionel Sambuc 
313*ebfedea0SLionel Sambuc 	/* make prime "q" */
314*ebfedea0SLionel Sambuc 	do {
315*ebfedea0SLionel Sambuc 		if ((err = rand_prime( q, size/2, prng, wprng)) != CRYPT_OK) {
316*ebfedea0SLionel Sambuc 			goto errkey;
317*ebfedea0SLionel Sambuc 		}
318*ebfedea0SLionel Sambuc 		/* tmp1 = q-1 */
319*ebfedea0SLionel Sambuc 		if ((err = mp_sub_d( q, 1,  tmp1)) != CRYPT_OK) {
320*ebfedea0SLionel Sambuc 			goto errkey;
321*ebfedea0SLionel Sambuc 		}
322*ebfedea0SLionel Sambuc 		/* tmp2 = gcd(q-1, e) */
323*ebfedea0SLionel Sambuc 		if ((err = mp_gcd( tmp1,  tmp3,  tmp2)) != CRYPT_OK) {
324*ebfedea0SLionel Sambuc 			goto errkey;
325*ebfedea0SLionel Sambuc 		}
326*ebfedea0SLionel Sambuc 	} while (mp_cmp_d( tmp2, 1) != 0);
327*ebfedea0SLionel Sambuc 	/* while e divides q-1 */
328*ebfedea0SLionel Sambuc 
329*ebfedea0SLionel Sambuc 	/* tmp1 = lcm(p-1, q-1) */
330*ebfedea0SLionel Sambuc 		/* tmp2 = p-1 */
331*ebfedea0SLionel Sambuc 	if ((err = mp_sub_d( p, 1,  tmp2)) != CRYPT_OK) {
332*ebfedea0SLionel Sambuc 		goto errkey;
333*ebfedea0SLionel Sambuc 	}
334*ebfedea0SLionel Sambuc 	/* tmp1 = q-1 (previous do/while loop) */
335*ebfedea0SLionel Sambuc 		/* tmp1 = lcm(p-1, q-1) */
336*ebfedea0SLionel Sambuc 	if ((err = mp_lcm( tmp1,  tmp2,  tmp1)) != CRYPT_OK) {
337*ebfedea0SLionel Sambuc 		goto errkey;
338*ebfedea0SLionel Sambuc 	}
339*ebfedea0SLionel Sambuc 
340*ebfedea0SLionel Sambuc 	/* make key */
341*ebfedea0SLionel Sambuc 	if ((err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL)) != CRYPT_OK) {
342*ebfedea0SLionel Sambuc 		goto errkey;
343*ebfedea0SLionel Sambuc 	}
344*ebfedea0SLionel Sambuc 
345*ebfedea0SLionel Sambuc 	/* key->e =  e */
346*ebfedea0SLionel Sambuc 	if ((err = mp_set_int( key->e, e)) != CRYPT_OK) {
347*ebfedea0SLionel Sambuc 		goto errkey;
348*ebfedea0SLionel Sambuc 	}
349*ebfedea0SLionel Sambuc 	/* key->d = 1/e mod lcm(p-1,q-1) */
350*ebfedea0SLionel Sambuc 	if ((err = mp_invmod( key->e,  tmp1,  key->d)) != CRYPT_OK) {
351*ebfedea0SLionel Sambuc 		goto errkey;
352*ebfedea0SLionel Sambuc 	}
353*ebfedea0SLionel Sambuc 	/* key->N = pq */
354*ebfedea0SLionel Sambuc 	if ((err = mp_mul( p,  q,  key->N)) != CRYPT_OK) {
355*ebfedea0SLionel Sambuc 		goto errkey;
356*ebfedea0SLionel Sambuc 	}
357*ebfedea0SLionel Sambuc 
358*ebfedea0SLionel Sambuc 	/* optimize for CRT now */
359*ebfedea0SLionel Sambuc 	/* find d mod q-1 and d mod p-1 */
360*ebfedea0SLionel Sambuc 	/* tmp1 = q-1 */
361*ebfedea0SLionel Sambuc 	if ((err = mp_sub_d( p, 1,  tmp1)) != CRYPT_OK) {
362*ebfedea0SLionel Sambuc 		goto errkey;
363*ebfedea0SLionel Sambuc 	}
364*ebfedea0SLionel Sambuc 	/* tmp2 = p-1 */
365*ebfedea0SLionel Sambuc 	if ((err = mp_sub_d( q, 1,  tmp2)) != CRYPT_OK) {
366*ebfedea0SLionel Sambuc 		goto errkey;
367*ebfedea0SLionel Sambuc 	}
368*ebfedea0SLionel Sambuc 	/* dP = d mod p-1 */
369*ebfedea0SLionel Sambuc 	if ((err = mp_mod( key->d,  tmp1,  key->dP)) != CRYPT_OK) {
370*ebfedea0SLionel Sambuc 		goto errkey;
371*ebfedea0SLionel Sambuc 	}
372*ebfedea0SLionel Sambuc 	/* dQ = d mod q-1 */
373*ebfedea0SLionel Sambuc 	if ((err = mp_mod( key->d,  tmp2,  key->dQ)) != CRYPT_OK) {
374*ebfedea0SLionel Sambuc 		goto errkey;
375*ebfedea0SLionel Sambuc 	}
376*ebfedea0SLionel Sambuc 	/* qP = 1/q mod p */
377*ebfedea0SLionel Sambuc 	if ((err = mp_invmod( q,  p,  key->qP)) != CRYPT_OK) {
378*ebfedea0SLionel Sambuc 		got oerrkey;
379*ebfedea0SLionel Sambuc 	}
380*ebfedea0SLionel Sambuc 
381*ebfedea0SLionel Sambuc 	if ((err = mp_copy( p,  key->p)) != CRYPT_OK) {
382*ebfedea0SLionel Sambuc 		goto errkey;
383*ebfedea0SLionel Sambuc 		}
384*ebfedea0SLionel Sambuc 	if ((err = mp_copy( q,  key->q)) != CRYPT_OK) {
385*ebfedea0SLionel Sambuc 		goto errkey;
386*ebfedea0SLionel Sambuc 	}
387*ebfedea0SLionel Sambuc 
388*ebfedea0SLionel Sambuc 	/* set key type (in this case it's CRT optimized) */
389*ebfedea0SLionel Sambuc 	key->type = PK_PRIVATE;
390*ebfedea0SLionel Sambuc 
391*ebfedea0SLionel Sambuc 	/* return ok and free temps */
392*ebfedea0SLionel Sambuc 	err = CRYPT_OK;
393*ebfedea0SLionel Sambuc 	goto cleanup;
394*ebfedea0SLionel Sambuc errkey:
395*ebfedea0SLionel Sambuc 	mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL);
396*ebfedea0SLionel Sambuc cleanup:
397*ebfedea0SLionel Sambuc 	mp_clear_multi(tmp3, tmp2, tmp1, p, q, NULL);
398*ebfedea0SLionel Sambuc 	return err;
399*ebfedea0SLionel Sambuc }
400*ebfedea0SLionel Sambuc #endif
401*ebfedea0SLionel Sambuc 
402*ebfedea0SLionel Sambuc #define HASHBUF_LEN	512
403*ebfedea0SLionel Sambuc 
404*ebfedea0SLionel Sambuc #define DSA_MAX_MODULUS_BITS	10000
405*ebfedea0SLionel Sambuc 
406*ebfedea0SLionel Sambuc static int
dsa_do_verify(const unsigned char * calculated,int dgst_len,const dsasig_t * sig,mpi_dsa_t * dsa)407*ebfedea0SLionel Sambuc dsa_do_verify(const unsigned char *calculated, int dgst_len, const dsasig_t *sig, mpi_dsa_t *dsa)
408*ebfedea0SLionel Sambuc {
409*ebfedea0SLionel Sambuc 	BIGNUM		 *M;
410*ebfedea0SLionel Sambuc 	BIGNUM		 *W;
411*ebfedea0SLionel Sambuc 	BIGNUM		 *t1;
412*ebfedea0SLionel Sambuc 	int		 ret = -1;
413*ebfedea0SLionel Sambuc 	int		 qbits;
414*ebfedea0SLionel Sambuc 
415*ebfedea0SLionel Sambuc 	if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
416*ebfedea0SLionel Sambuc 		return 0;
417*ebfedea0SLionel Sambuc 	}
418*ebfedea0SLionel Sambuc 	M = W = t1 = NULL;
419*ebfedea0SLionel Sambuc 	qbits = BN_num_bits(dsa->q);
420*ebfedea0SLionel Sambuc 	switch(qbits) {
421*ebfedea0SLionel Sambuc 	case 160:
422*ebfedea0SLionel Sambuc 	case 224:
423*ebfedea0SLionel Sambuc 	case 256:
424*ebfedea0SLionel Sambuc 		/* openssl sources say these are the valid values */
425*ebfedea0SLionel Sambuc 		/* according to FIPS 186-3 */
426*ebfedea0SLionel Sambuc 		break;
427*ebfedea0SLionel Sambuc 	default:
428*ebfedea0SLionel Sambuc 		printf("dsa: bad # of Q bits\n");
429*ebfedea0SLionel Sambuc 		return 0;
430*ebfedea0SLionel Sambuc 	}
431*ebfedea0SLionel Sambuc 	if (BN_num_bits(dsa->p) > DSA_MAX_MODULUS_BITS) {
432*ebfedea0SLionel Sambuc 		printf("dsa: p too large\n");
433*ebfedea0SLionel Sambuc 		return 0;
434*ebfedea0SLionel Sambuc 	}
435*ebfedea0SLionel Sambuc 	/* no love for SHA512? */
436*ebfedea0SLionel Sambuc 	if (dgst_len > SHA256_DIGEST_LENGTH) {
437*ebfedea0SLionel Sambuc 		printf("dsa: digest too long\n");
438*ebfedea0SLionel Sambuc 		return 0;
439*ebfedea0SLionel Sambuc 	}
440*ebfedea0SLionel Sambuc 	ret = 0;
441*ebfedea0SLionel Sambuc 	if ((M = BN_new()) == NULL ||
442*ebfedea0SLionel Sambuc 	    (W = BN_new()) == NULL ||
443*ebfedea0SLionel Sambuc 	    (t1 = BN_new()) == NULL) {
444*ebfedea0SLionel Sambuc 		goto err;
445*ebfedea0SLionel Sambuc 	}
446*ebfedea0SLionel Sambuc 	if (BN_is_zero(sig->r) ||
447*ebfedea0SLionel Sambuc 	    BN_is_negative(sig->r) ||
448*ebfedea0SLionel Sambuc 	    BN_cmp(sig->r, dsa->q) >= 0) {
449*ebfedea0SLionel Sambuc 		goto err;
450*ebfedea0SLionel Sambuc 	}
451*ebfedea0SLionel Sambuc 	if (BN_is_zero(sig->s) ||
452*ebfedea0SLionel Sambuc 	    BN_is_negative(sig->s) ||
453*ebfedea0SLionel Sambuc 	    BN_cmp(sig->s, dsa->q) >= 0) {
454*ebfedea0SLionel Sambuc 		goto err;
455*ebfedea0SLionel Sambuc 	}
456*ebfedea0SLionel Sambuc 	if (BN_mod_inverse(W, sig->s, dsa->q, NULL) != MP_OKAY) {
457*ebfedea0SLionel Sambuc 		goto err;
458*ebfedea0SLionel Sambuc 	}
459*ebfedea0SLionel Sambuc 	if (dgst_len > qbits / 8) {
460*ebfedea0SLionel Sambuc 		dgst_len = qbits / 8;
461*ebfedea0SLionel Sambuc 	}
462*ebfedea0SLionel Sambuc 	if (BN_bin2bn(calculated, dgst_len, M) == NULL) {
463*ebfedea0SLionel Sambuc 		goto err;
464*ebfedea0SLionel Sambuc 	}
465*ebfedea0SLionel Sambuc 	if (!BN_mod_mul(M, M, W, dsa->q, NULL)) {
466*ebfedea0SLionel Sambuc 		goto err;
467*ebfedea0SLionel Sambuc 	}
468*ebfedea0SLionel Sambuc 	if (!BN_mod_mul(W, sig->r, W, dsa->q, NULL)) {
469*ebfedea0SLionel Sambuc 		goto err;
470*ebfedea0SLionel Sambuc 	}
471*ebfedea0SLionel Sambuc 	if (!BN_mod_exp(dsa->p, t1, dsa->g, M, NULL)) {
472*ebfedea0SLionel Sambuc 		goto err;
473*ebfedea0SLionel Sambuc 	}
474*ebfedea0SLionel Sambuc 	if (!BN_div(NULL, M, t1, dsa->q, NULL)) {
475*ebfedea0SLionel Sambuc 		goto err;
476*ebfedea0SLionel Sambuc 	}
477*ebfedea0SLionel Sambuc 	ret = (BN_cmp(M, sig->r) == 0);
478*ebfedea0SLionel Sambuc err:
479*ebfedea0SLionel Sambuc 	if (M) {
480*ebfedea0SLionel Sambuc 		BN_free(M);
481*ebfedea0SLionel Sambuc 	}
482*ebfedea0SLionel Sambuc 	if (W) {
483*ebfedea0SLionel Sambuc 		BN_free(W);
484*ebfedea0SLionel Sambuc 	}
485*ebfedea0SLionel Sambuc 	if (t1) {
486*ebfedea0SLionel Sambuc 		BN_free(t1);
487*ebfedea0SLionel Sambuc 	}
488*ebfedea0SLionel Sambuc 	return ret;
489*ebfedea0SLionel Sambuc }
490*ebfedea0SLionel Sambuc 
491*ebfedea0SLionel Sambuc /*************************************************************************/
492*ebfedea0SLionel Sambuc 
493*ebfedea0SLionel Sambuc int
RSA_size(const RSA * rsa)494*ebfedea0SLionel Sambuc RSA_size(const RSA *rsa)
495*ebfedea0SLionel Sambuc {
496*ebfedea0SLionel Sambuc 	return (rsa == NULL) ? 0 : BN_num_bits(rsa->n);
497*ebfedea0SLionel Sambuc }
498*ebfedea0SLionel Sambuc 
499*ebfedea0SLionel Sambuc int
DSA_size(const DSA * dsa)500*ebfedea0SLionel Sambuc DSA_size(const DSA *dsa)
501*ebfedea0SLionel Sambuc {
502*ebfedea0SLionel Sambuc 	return (dsa == NULL) ? 0 : BN_num_bits(dsa->p);
503*ebfedea0SLionel Sambuc }
504*ebfedea0SLionel Sambuc 
505*ebfedea0SLionel Sambuc unsigned
dsa_verify(const signature_t * signature,const dsa_pubkey_t * pubdsa,const uint8_t * calculated,size_t hash_length)506*ebfedea0SLionel Sambuc dsa_verify(const signature_t *signature, const dsa_pubkey_t *pubdsa, const uint8_t *calculated, size_t hash_length)
507*ebfedea0SLionel Sambuc {
508*ebfedea0SLionel Sambuc 	mpi_dsa_t	odsa;
509*ebfedea0SLionel Sambuc 	dsasig_t	osig;
510*ebfedea0SLionel Sambuc 	unsigned	qlen;
511*ebfedea0SLionel Sambuc 	int             ret;
512*ebfedea0SLionel Sambuc 
513*ebfedea0SLionel Sambuc 	if (signature == NULL || pubdsa == NULL || calculated == NULL) {
514*ebfedea0SLionel Sambuc 		return -1;
515*ebfedea0SLionel Sambuc 	}
516*ebfedea0SLionel Sambuc 	(void) memset(&osig, 0x0, sizeof(osig));
517*ebfedea0SLionel Sambuc 	(void) memset(&odsa, 0x0, sizeof(odsa));
518*ebfedea0SLionel Sambuc 	BN_copy(osig.r, signature->dsa.r);
519*ebfedea0SLionel Sambuc 	BN_copy(osig.s, signature->dsa.s);
520*ebfedea0SLionel Sambuc 	odsa.p = pubdsa->p;
521*ebfedea0SLionel Sambuc 	odsa.q = pubdsa->q;
522*ebfedea0SLionel Sambuc 	odsa.g = pubdsa->g;
523*ebfedea0SLionel Sambuc 	odsa.pub_key = pubdsa->y;
524*ebfedea0SLionel Sambuc 	if ((qlen = BN_num_bytes(odsa.q)) < hash_length) {
525*ebfedea0SLionel Sambuc 		hash_length = qlen;
526*ebfedea0SLionel Sambuc 	}
527*ebfedea0SLionel Sambuc 	ret = dsa_do_verify(calculated, (int)hash_length, &signature->dsa, &odsa);
528*ebfedea0SLionel Sambuc 	if (ret < 0) {
529*ebfedea0SLionel Sambuc 		return 0;
530*ebfedea0SLionel Sambuc 	}
531*ebfedea0SLionel Sambuc 	BN_free(odsa.p);
532*ebfedea0SLionel Sambuc 	BN_free(odsa.q);
533*ebfedea0SLionel Sambuc 	BN_free(odsa.g);
534*ebfedea0SLionel Sambuc 	BN_free(odsa.pub_key);
535*ebfedea0SLionel Sambuc 	odsa.p = odsa.q = odsa.g = odsa.pub_key = NULL;
536*ebfedea0SLionel Sambuc 	BN_free(osig.r);
537*ebfedea0SLionel Sambuc 	BN_free(osig.s);
538*ebfedea0SLionel Sambuc 	osig.r = osig.s = NULL;
539*ebfedea0SLionel Sambuc 	return (unsigned)ret;
540*ebfedea0SLionel Sambuc }
541*ebfedea0SLionel Sambuc 
542*ebfedea0SLionel Sambuc RSA *
RSA_new(void)543*ebfedea0SLionel Sambuc RSA_new(void)
544*ebfedea0SLionel Sambuc {
545*ebfedea0SLionel Sambuc 	return netpgp_allocate(1, sizeof(RSA));
546*ebfedea0SLionel Sambuc }
547*ebfedea0SLionel Sambuc 
548*ebfedea0SLionel Sambuc void
RSA_free(RSA * rsa)549*ebfedea0SLionel Sambuc RSA_free(RSA *rsa)
550*ebfedea0SLionel Sambuc {
551*ebfedea0SLionel Sambuc 	if (rsa) {
552*ebfedea0SLionel Sambuc 		netpgp_deallocate(rsa, sizeof(*rsa));
553*ebfedea0SLionel Sambuc 	}
554*ebfedea0SLionel Sambuc }
555*ebfedea0SLionel Sambuc 
556*ebfedea0SLionel Sambuc int
RSA_check_key(RSA * rsa)557*ebfedea0SLionel Sambuc RSA_check_key(RSA *rsa)
558*ebfedea0SLionel Sambuc {
559*ebfedea0SLionel Sambuc 	BIGNUM	*calcn;
560*ebfedea0SLionel Sambuc 	int	 ret;
561*ebfedea0SLionel Sambuc 
562*ebfedea0SLionel Sambuc 	ret = 0;
563*ebfedea0SLionel Sambuc 	if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->n == NULL) {
564*ebfedea0SLionel Sambuc 		return -1;
565*ebfedea0SLionel Sambuc 	}
566*ebfedea0SLionel Sambuc 	/* check that p and q are coprime, and that n = p*q. */
567*ebfedea0SLionel Sambuc 	if (!BN_is_prime(rsa->p, 1, NULL, NULL, NULL) ||
568*ebfedea0SLionel Sambuc 	    !BN_is_prime(rsa->q, 1, NULL, NULL, NULL)) {
569*ebfedea0SLionel Sambuc 		return 0;
570*ebfedea0SLionel Sambuc 	}
571*ebfedea0SLionel Sambuc 	calcn = BN_new();
572*ebfedea0SLionel Sambuc         BN_mul(calcn, rsa->p, rsa->q, NULL);
573*ebfedea0SLionel Sambuc 	if (BN_cmp(calcn, rsa->n) != 0) {
574*ebfedea0SLionel Sambuc 		goto errout;
575*ebfedea0SLionel Sambuc 	}
576*ebfedea0SLionel Sambuc 	/* XXX - check that d*e = 1 mod (p-1*q-1) */
577*ebfedea0SLionel Sambuc 	ret = 1;
578*ebfedea0SLionel Sambuc errout:
579*ebfedea0SLionel Sambuc 	BN_clear_free(calcn);
580*ebfedea0SLionel Sambuc 	return ret;
581*ebfedea0SLionel Sambuc }
582*ebfedea0SLionel Sambuc 
583*ebfedea0SLionel Sambuc RSA *
RSA_generate_key(int num,unsigned long e,void (* callback)(int,int,void *),void * cb_arg)584*ebfedea0SLionel Sambuc RSA_generate_key(int num, unsigned long e, void (*callback)(int,int,void *), void *cb_arg)
585*ebfedea0SLionel Sambuc {
586*ebfedea0SLionel Sambuc 	/* STUBBED */
587*ebfedea0SLionel Sambuc 	USE_ARG(num);
588*ebfedea0SLionel Sambuc 	USE_ARG(e);
589*ebfedea0SLionel Sambuc 	USE_ARG(callback);
590*ebfedea0SLionel Sambuc 	USE_ARG(cb_arg);
591*ebfedea0SLionel Sambuc 	printf("RSA_generate_key stubbed\n");
592*ebfedea0SLionel Sambuc 	return RSA_new();
593*ebfedea0SLionel Sambuc }
594*ebfedea0SLionel Sambuc 
595*ebfedea0SLionel Sambuc /* encrypt */
596*ebfedea0SLionel Sambuc int
RSA_public_encrypt(int plainc,const unsigned char * plain,unsigned char * encbuf,RSA * rsa,int padding)597*ebfedea0SLionel Sambuc RSA_public_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa, int padding)
598*ebfedea0SLionel Sambuc {
599*ebfedea0SLionel Sambuc 	USE_ARG(padding);
600*ebfedea0SLionel Sambuc 	if (plain == NULL || encbuf == NULL || rsa == NULL) {
601*ebfedea0SLionel Sambuc 		return -1;
602*ebfedea0SLionel Sambuc 	}
603*ebfedea0SLionel Sambuc 	return lowlevel_rsa_public_encrypt(plainc, plain, encbuf, rsa);
604*ebfedea0SLionel Sambuc }
605*ebfedea0SLionel Sambuc 
606*ebfedea0SLionel Sambuc /* decrypt */
607*ebfedea0SLionel Sambuc int
RSA_private_decrypt(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding)608*ebfedea0SLionel Sambuc RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
609*ebfedea0SLionel Sambuc {
610*ebfedea0SLionel Sambuc 	USE_ARG(padding);
611*ebfedea0SLionel Sambuc 	if (from == NULL || to == NULL || rsa == NULL) {
612*ebfedea0SLionel Sambuc 		return -1;
613*ebfedea0SLionel Sambuc 	}
614*ebfedea0SLionel Sambuc 	return lowlevel_rsa_private_decrypt(flen, from, to, rsa);
615*ebfedea0SLionel Sambuc }
616*ebfedea0SLionel Sambuc 
617*ebfedea0SLionel Sambuc /* sign */
618*ebfedea0SLionel Sambuc int
RSA_private_encrypt(int plainc,const unsigned char * plain,unsigned char * encbuf,RSA * rsa,int padding)619*ebfedea0SLionel Sambuc RSA_private_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa, int padding)
620*ebfedea0SLionel Sambuc {
621*ebfedea0SLionel Sambuc 	USE_ARG(padding);
622*ebfedea0SLionel Sambuc 	if (plain == NULL || encbuf == NULL || rsa == NULL) {
623*ebfedea0SLionel Sambuc 		return -1;
624*ebfedea0SLionel Sambuc 	}
625*ebfedea0SLionel Sambuc 	return lowlevel_rsa_private_encrypt(plainc, plain, encbuf, rsa);
626*ebfedea0SLionel Sambuc }
627*ebfedea0SLionel Sambuc 
628*ebfedea0SLionel Sambuc /* verify */
629*ebfedea0SLionel Sambuc int
RSA_public_decrypt(int enclen,const unsigned char * enc,unsigned char * dec,RSA * rsa,int padding)630*ebfedea0SLionel Sambuc RSA_public_decrypt(int enclen, const unsigned char *enc, unsigned char *dec, RSA *rsa, int padding)
631*ebfedea0SLionel Sambuc {
632*ebfedea0SLionel Sambuc 	rsa_pubkey_t	pub;
633*ebfedea0SLionel Sambuc 	int		ret;
634*ebfedea0SLionel Sambuc 
635*ebfedea0SLionel Sambuc 	if (enc == NULL || dec == NULL || rsa == NULL) {
636*ebfedea0SLionel Sambuc 		return 0;
637*ebfedea0SLionel Sambuc 	}
638*ebfedea0SLionel Sambuc 	USE_ARG(padding);
639*ebfedea0SLionel Sambuc 	(void) memset(&pub, 0x0, sizeof(pub));
640*ebfedea0SLionel Sambuc 	pub.n = BN_dup(rsa->n);
641*ebfedea0SLionel Sambuc 	pub.e = BN_dup(rsa->e);
642*ebfedea0SLionel Sambuc 	ret = lowlevel_rsa_public_decrypt(enc, enclen, dec, &pub);
643*ebfedea0SLionel Sambuc 	BN_free(pub.n);
644*ebfedea0SLionel Sambuc 	BN_free(pub.e);
645*ebfedea0SLionel Sambuc 	return ret;
646*ebfedea0SLionel Sambuc }
647*ebfedea0SLionel Sambuc 
648*ebfedea0SLionel Sambuc /***********************************************************************/
649*ebfedea0SLionel Sambuc 
650*ebfedea0SLionel Sambuc DSA *
DSA_new(void)651*ebfedea0SLionel Sambuc DSA_new(void)
652*ebfedea0SLionel Sambuc {
653*ebfedea0SLionel Sambuc 	return netpgp_allocate(1, sizeof(DSA));
654*ebfedea0SLionel Sambuc }
655*ebfedea0SLionel Sambuc 
656*ebfedea0SLionel Sambuc void
DSA_free(DSA * dsa)657*ebfedea0SLionel Sambuc DSA_free(DSA *dsa)
658*ebfedea0SLionel Sambuc {
659*ebfedea0SLionel Sambuc 	if (dsa) {
660*ebfedea0SLionel Sambuc 		netpgp_deallocate(dsa, sizeof(*dsa));
661*ebfedea0SLionel Sambuc 	}
662*ebfedea0SLionel Sambuc }
663*ebfedea0SLionel Sambuc 
664*ebfedea0SLionel Sambuc DSA_SIG *
DSA_SIG_new(void)665*ebfedea0SLionel Sambuc DSA_SIG_new(void)
666*ebfedea0SLionel Sambuc {
667*ebfedea0SLionel Sambuc 	return netpgp_allocate(1, sizeof(DSA_SIG));
668*ebfedea0SLionel Sambuc }
669*ebfedea0SLionel Sambuc 
670*ebfedea0SLionel Sambuc void
DSA_SIG_free(DSA_SIG * sig)671*ebfedea0SLionel Sambuc DSA_SIG_free(DSA_SIG *sig)
672*ebfedea0SLionel Sambuc {
673*ebfedea0SLionel Sambuc 	if (sig) {
674*ebfedea0SLionel Sambuc 		netpgp_deallocate(sig, sizeof(*sig));
675*ebfedea0SLionel Sambuc 	}
676*ebfedea0SLionel Sambuc }
677*ebfedea0SLionel Sambuc 
678*ebfedea0SLionel Sambuc DSA_SIG *
DSA_do_sign(const unsigned char * dgst,int dlen,DSA * dsa)679*ebfedea0SLionel Sambuc DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
680*ebfedea0SLionel Sambuc {
681*ebfedea0SLionel Sambuc 	/* STUBBED */
682*ebfedea0SLionel Sambuc 	USE_ARG(dgst);
683*ebfedea0SLionel Sambuc 	USE_ARG(dlen);
684*ebfedea0SLionel Sambuc 	USE_ARG(dsa);
685*ebfedea0SLionel Sambuc 	printf("DSA_do_sign stubbed\n");
686*ebfedea0SLionel Sambuc 	return DSA_SIG_new();
687*ebfedea0SLionel Sambuc }
688*ebfedea0SLionel Sambuc 
689*ebfedea0SLionel Sambuc int
DSA_do_verify(const unsigned char * dgst,int dgst_len,DSA_SIG * sig,DSA * dsa)690*ebfedea0SLionel Sambuc DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa)
691*ebfedea0SLionel Sambuc {
692*ebfedea0SLionel Sambuc 	if (dgst == NULL || dgst_len == 0 || sig == NULL || dsa == NULL) {
693*ebfedea0SLionel Sambuc 		return -1;
694*ebfedea0SLionel Sambuc 	}
695*ebfedea0SLionel Sambuc 	return dsa_do_verify(dgst, dgst_len, sig, dsa);
696*ebfedea0SLionel Sambuc }
697