1*2139Sjp161948 /*
2*2139Sjp161948 * Copyright (c) 2002 Bob Beck <beck@openbsd.org>
3*2139Sjp161948 * Copyright (c) 2002 Theo de Raadt
4*2139Sjp161948 * Copyright (c) 2002 Markus Friedl
5*2139Sjp161948 * All rights reserved.
6*2139Sjp161948 *
7*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
8*2139Sjp161948 * modification, are permitted provided that the following conditions
9*2139Sjp161948 * are met:
10*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
11*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
12*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
13*2139Sjp161948 * notice, this list of conditions and the following disclaimer in the
14*2139Sjp161948 * documentation and/or other materials provided with the distribution.
15*2139Sjp161948 *
16*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17*2139Sjp161948 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*2139Sjp161948 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*2139Sjp161948 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
20*2139Sjp161948 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*2139Sjp161948 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23*2139Sjp161948 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*2139Sjp161948 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*2139Sjp161948 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*2139Sjp161948 *
27*2139Sjp161948 */
28*2139Sjp161948
29*2139Sjp161948 #include <openssl/objects.h>
30*2139Sjp161948 #include <openssl/engine.h>
31*2139Sjp161948 #include <openssl/evp.h>
32*2139Sjp161948 #include <openssl/bn.h>
33*2139Sjp161948
34*2139Sjp161948 #if (defined(__unix__) || defined(unix)) && !defined(USG) && \
35*2139Sjp161948 (defined(OpenBSD) || defined(__FreeBSD_version))
36*2139Sjp161948 #include <sys/param.h>
37*2139Sjp161948 # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041)
38*2139Sjp161948 # define HAVE_CRYPTODEV
39*2139Sjp161948 # endif
40*2139Sjp161948 # if (OpenBSD >= 200110)
41*2139Sjp161948 # define HAVE_SYSLOG_R
42*2139Sjp161948 # endif
43*2139Sjp161948 #endif
44*2139Sjp161948
45*2139Sjp161948 #ifndef HAVE_CRYPTODEV
46*2139Sjp161948
47*2139Sjp161948 void
ENGINE_load_cryptodev(void)48*2139Sjp161948 ENGINE_load_cryptodev(void)
49*2139Sjp161948 {
50*2139Sjp161948 /* This is a NOP on platforms without /dev/crypto */
51*2139Sjp161948 return;
52*2139Sjp161948 }
53*2139Sjp161948
54*2139Sjp161948 #else
55*2139Sjp161948
56*2139Sjp161948 #include <sys/types.h>
57*2139Sjp161948 #include <crypto/cryptodev.h>
58*2139Sjp161948 #include <sys/ioctl.h>
59*2139Sjp161948 #include <errno.h>
60*2139Sjp161948 #include <stdio.h>
61*2139Sjp161948 #include <unistd.h>
62*2139Sjp161948 #include <fcntl.h>
63*2139Sjp161948 #include <stdarg.h>
64*2139Sjp161948 #include <syslog.h>
65*2139Sjp161948 #include <errno.h>
66*2139Sjp161948 #include <string.h>
67*2139Sjp161948
68*2139Sjp161948 struct dev_crypto_state {
69*2139Sjp161948 struct session_op d_sess;
70*2139Sjp161948 int d_fd;
71*2139Sjp161948 };
72*2139Sjp161948
73*2139Sjp161948 static u_int32_t cryptodev_asymfeat = 0;
74*2139Sjp161948
75*2139Sjp161948 static int get_asym_dev_crypto(void);
76*2139Sjp161948 static int open_dev_crypto(void);
77*2139Sjp161948 static int get_dev_crypto(void);
78*2139Sjp161948 static int cryptodev_max_iv(int cipher);
79*2139Sjp161948 static int cryptodev_key_length_valid(int cipher, int len);
80*2139Sjp161948 static int cipher_nid_to_cryptodev(int nid);
81*2139Sjp161948 static int get_cryptodev_ciphers(const int **cnids);
82*2139Sjp161948 static int get_cryptodev_digests(const int **cnids);
83*2139Sjp161948 static int cryptodev_usable_ciphers(const int **nids);
84*2139Sjp161948 static int cryptodev_usable_digests(const int **nids);
85*2139Sjp161948 static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
86*2139Sjp161948 const unsigned char *in, unsigned int inl);
87*2139Sjp161948 static int cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
88*2139Sjp161948 const unsigned char *iv, int enc);
89*2139Sjp161948 static int cryptodev_cleanup(EVP_CIPHER_CTX *ctx);
90*2139Sjp161948 static int cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
91*2139Sjp161948 const int **nids, int nid);
92*2139Sjp161948 static int cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
93*2139Sjp161948 const int **nids, int nid);
94*2139Sjp161948 static int bn2crparam(const BIGNUM *a, struct crparam *crp);
95*2139Sjp161948 static int crparam2bn(struct crparam *crp, BIGNUM *a);
96*2139Sjp161948 static void zapparams(struct crypt_kop *kop);
97*2139Sjp161948 static int cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r,
98*2139Sjp161948 int slen, BIGNUM *s);
99*2139Sjp161948
100*2139Sjp161948 static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a,
101*2139Sjp161948 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
102*2139Sjp161948 static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I,
103*2139Sjp161948 RSA *rsa);
104*2139Sjp161948 static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
105*2139Sjp161948 static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a,
106*2139Sjp161948 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
107*2139Sjp161948 static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
108*2139Sjp161948 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
109*2139Sjp161948 BN_CTX *ctx, BN_MONT_CTX *mont);
110*2139Sjp161948 static DSA_SIG *cryptodev_dsa_do_sign(const unsigned char *dgst,
111*2139Sjp161948 int dlen, DSA *dsa);
112*2139Sjp161948 static int cryptodev_dsa_verify(const unsigned char *dgst, int dgst_len,
113*2139Sjp161948 DSA_SIG *sig, DSA *dsa);
114*2139Sjp161948 static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
115*2139Sjp161948 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
116*2139Sjp161948 BN_MONT_CTX *m_ctx);
117*2139Sjp161948 static int cryptodev_dh_compute_key(unsigned char *key,
118*2139Sjp161948 const BIGNUM *pub_key, DH *dh);
119*2139Sjp161948 static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p,
120*2139Sjp161948 void (*f)());
121*2139Sjp161948 void ENGINE_load_cryptodev(void);
122*2139Sjp161948
123*2139Sjp161948 static const ENGINE_CMD_DEFN cryptodev_defns[] = {
124*2139Sjp161948 { 0, NULL, NULL, 0 }
125*2139Sjp161948 };
126*2139Sjp161948
127*2139Sjp161948 static struct {
128*2139Sjp161948 int id;
129*2139Sjp161948 int nid;
130*2139Sjp161948 int ivmax;
131*2139Sjp161948 int keylen;
132*2139Sjp161948 } ciphers[] = {
133*2139Sjp161948 { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, },
134*2139Sjp161948 { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, },
135*2139Sjp161948 { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, },
136*2139Sjp161948 { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, },
137*2139Sjp161948 { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, },
138*2139Sjp161948 { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, },
139*2139Sjp161948 { 0, NID_undef, 0, 0, },
140*2139Sjp161948 };
141*2139Sjp161948
142*2139Sjp161948 static struct {
143*2139Sjp161948 int id;
144*2139Sjp161948 int nid;
145*2139Sjp161948 } digests[] = {
146*2139Sjp161948 { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, },
147*2139Sjp161948 { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, },
148*2139Sjp161948 { CRYPTO_MD5_KPDK, NID_undef, },
149*2139Sjp161948 { CRYPTO_SHA1_KPDK, NID_undef, },
150*2139Sjp161948 { CRYPTO_MD5, NID_md5, },
151*2139Sjp161948 { CRYPTO_SHA1, NID_undef, },
152*2139Sjp161948 { 0, NID_undef, },
153*2139Sjp161948 };
154*2139Sjp161948
155*2139Sjp161948 /*
156*2139Sjp161948 * Return a fd if /dev/crypto seems usable, 0 otherwise.
157*2139Sjp161948 */
158*2139Sjp161948 static int
open_dev_crypto(void)159*2139Sjp161948 open_dev_crypto(void)
160*2139Sjp161948 {
161*2139Sjp161948 static int fd = -1;
162*2139Sjp161948
163*2139Sjp161948 if (fd == -1) {
164*2139Sjp161948 if ((fd = open("/dev/crypto", O_RDWR, 0)) == -1)
165*2139Sjp161948 return (-1);
166*2139Sjp161948 /* close on exec */
167*2139Sjp161948 if (fcntl(fd, F_SETFD, 1) == -1) {
168*2139Sjp161948 close(fd);
169*2139Sjp161948 fd = -1;
170*2139Sjp161948 return (-1);
171*2139Sjp161948 }
172*2139Sjp161948 }
173*2139Sjp161948 return (fd);
174*2139Sjp161948 }
175*2139Sjp161948
176*2139Sjp161948 static int
get_dev_crypto(void)177*2139Sjp161948 get_dev_crypto(void)
178*2139Sjp161948 {
179*2139Sjp161948 int fd, retfd;
180*2139Sjp161948
181*2139Sjp161948 if ((fd = open_dev_crypto()) == -1)
182*2139Sjp161948 return (-1);
183*2139Sjp161948 if (ioctl(fd, CRIOGET, &retfd) == -1)
184*2139Sjp161948 return (-1);
185*2139Sjp161948
186*2139Sjp161948 /* close on exec */
187*2139Sjp161948 if (fcntl(retfd, F_SETFD, 1) == -1) {
188*2139Sjp161948 close(retfd);
189*2139Sjp161948 return (-1);
190*2139Sjp161948 }
191*2139Sjp161948 return (retfd);
192*2139Sjp161948 }
193*2139Sjp161948
194*2139Sjp161948 /* Caching version for asym operations */
195*2139Sjp161948 static int
get_asym_dev_crypto(void)196*2139Sjp161948 get_asym_dev_crypto(void)
197*2139Sjp161948 {
198*2139Sjp161948 static int fd = -1;
199*2139Sjp161948
200*2139Sjp161948 if (fd == -1)
201*2139Sjp161948 fd = get_dev_crypto();
202*2139Sjp161948 return fd;
203*2139Sjp161948 }
204*2139Sjp161948
205*2139Sjp161948 /*
206*2139Sjp161948 * XXXX this needs to be set for each alg - and determined from
207*2139Sjp161948 * a running card.
208*2139Sjp161948 */
209*2139Sjp161948 static int
cryptodev_max_iv(int cipher)210*2139Sjp161948 cryptodev_max_iv(int cipher)
211*2139Sjp161948 {
212*2139Sjp161948 int i;
213*2139Sjp161948
214*2139Sjp161948 for (i = 0; ciphers[i].id; i++)
215*2139Sjp161948 if (ciphers[i].id == cipher)
216*2139Sjp161948 return (ciphers[i].ivmax);
217*2139Sjp161948 return (0);
218*2139Sjp161948 }
219*2139Sjp161948
220*2139Sjp161948 /*
221*2139Sjp161948 * XXXX this needs to be set for each alg - and determined from
222*2139Sjp161948 * a running card. For now, fake it out - but most of these
223*2139Sjp161948 * for real devices should return 1 for the supported key
224*2139Sjp161948 * sizes the device can handle.
225*2139Sjp161948 */
226*2139Sjp161948 static int
cryptodev_key_length_valid(int cipher,int len)227*2139Sjp161948 cryptodev_key_length_valid(int cipher, int len)
228*2139Sjp161948 {
229*2139Sjp161948 int i;
230*2139Sjp161948
231*2139Sjp161948 for (i = 0; ciphers[i].id; i++)
232*2139Sjp161948 if (ciphers[i].id == cipher)
233*2139Sjp161948 return (ciphers[i].keylen == len);
234*2139Sjp161948 return (0);
235*2139Sjp161948 }
236*2139Sjp161948
237*2139Sjp161948 /* convert libcrypto nids to cryptodev */
238*2139Sjp161948 static int
cipher_nid_to_cryptodev(int nid)239*2139Sjp161948 cipher_nid_to_cryptodev(int nid)
240*2139Sjp161948 {
241*2139Sjp161948 int i;
242*2139Sjp161948
243*2139Sjp161948 for (i = 0; ciphers[i].id; i++)
244*2139Sjp161948 if (ciphers[i].nid == nid)
245*2139Sjp161948 return (ciphers[i].id);
246*2139Sjp161948 return (0);
247*2139Sjp161948 }
248*2139Sjp161948
249*2139Sjp161948 /*
250*2139Sjp161948 * Find out what ciphers /dev/crypto will let us have a session for.
251*2139Sjp161948 * XXX note, that some of these openssl doesn't deal with yet!
252*2139Sjp161948 * returning them here is harmless, as long as we return NULL
253*2139Sjp161948 * when asked for a handler in the cryptodev_engine_ciphers routine
254*2139Sjp161948 */
255*2139Sjp161948 static int
get_cryptodev_ciphers(const int ** cnids)256*2139Sjp161948 get_cryptodev_ciphers(const int **cnids)
257*2139Sjp161948 {
258*2139Sjp161948 static int nids[CRYPTO_ALGORITHM_MAX];
259*2139Sjp161948 struct session_op sess;
260*2139Sjp161948 int fd, i, count = 0;
261*2139Sjp161948
262*2139Sjp161948 if ((fd = get_dev_crypto()) < 0) {
263*2139Sjp161948 *cnids = NULL;
264*2139Sjp161948 return (0);
265*2139Sjp161948 }
266*2139Sjp161948 memset(&sess, 0, sizeof(sess));
267*2139Sjp161948 sess.key = (caddr_t)"123456781234567812345678";
268*2139Sjp161948
269*2139Sjp161948 for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
270*2139Sjp161948 if (ciphers[i].nid == NID_undef)
271*2139Sjp161948 continue;
272*2139Sjp161948 sess.cipher = ciphers[i].id;
273*2139Sjp161948 sess.keylen = ciphers[i].keylen;
274*2139Sjp161948 sess.mac = 0;
275*2139Sjp161948 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
276*2139Sjp161948 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
277*2139Sjp161948 nids[count++] = ciphers[i].nid;
278*2139Sjp161948 }
279*2139Sjp161948 close(fd);
280*2139Sjp161948
281*2139Sjp161948 if (count > 0)
282*2139Sjp161948 *cnids = nids;
283*2139Sjp161948 else
284*2139Sjp161948 *cnids = NULL;
285*2139Sjp161948 return (count);
286*2139Sjp161948 }
287*2139Sjp161948
288*2139Sjp161948 /*
289*2139Sjp161948 * Find out what digests /dev/crypto will let us have a session for.
290*2139Sjp161948 * XXX note, that some of these openssl doesn't deal with yet!
291*2139Sjp161948 * returning them here is harmless, as long as we return NULL
292*2139Sjp161948 * when asked for a handler in the cryptodev_engine_digests routine
293*2139Sjp161948 */
294*2139Sjp161948 static int
get_cryptodev_digests(const int ** cnids)295*2139Sjp161948 get_cryptodev_digests(const int **cnids)
296*2139Sjp161948 {
297*2139Sjp161948 static int nids[CRYPTO_ALGORITHM_MAX];
298*2139Sjp161948 struct session_op sess;
299*2139Sjp161948 int fd, i, count = 0;
300*2139Sjp161948
301*2139Sjp161948 if ((fd = get_dev_crypto()) < 0) {
302*2139Sjp161948 *cnids = NULL;
303*2139Sjp161948 return (0);
304*2139Sjp161948 }
305*2139Sjp161948 memset(&sess, 0, sizeof(sess));
306*2139Sjp161948 for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) {
307*2139Sjp161948 if (digests[i].nid == NID_undef)
308*2139Sjp161948 continue;
309*2139Sjp161948 sess.mac = digests[i].id;
310*2139Sjp161948 sess.cipher = 0;
311*2139Sjp161948 if (ioctl(fd, CIOCGSESSION, &sess) != -1 &&
312*2139Sjp161948 ioctl(fd, CIOCFSESSION, &sess.ses) != -1)
313*2139Sjp161948 nids[count++] = digests[i].nid;
314*2139Sjp161948 }
315*2139Sjp161948 close(fd);
316*2139Sjp161948
317*2139Sjp161948 if (count > 0)
318*2139Sjp161948 *cnids = nids;
319*2139Sjp161948 else
320*2139Sjp161948 *cnids = NULL;
321*2139Sjp161948 return (count);
322*2139Sjp161948 }
323*2139Sjp161948
324*2139Sjp161948 /*
325*2139Sjp161948 * Find the useable ciphers|digests from dev/crypto - this is the first
326*2139Sjp161948 * thing called by the engine init crud which determines what it
327*2139Sjp161948 * can use for ciphers from this engine. We want to return
328*2139Sjp161948 * only what we can do, anythine else is handled by software.
329*2139Sjp161948 *
330*2139Sjp161948 * If we can't initialize the device to do anything useful for
331*2139Sjp161948 * any reason, we want to return a NULL array, and 0 length,
332*2139Sjp161948 * which forces everything to be done is software. By putting
333*2139Sjp161948 * the initalization of the device in here, we ensure we can
334*2139Sjp161948 * use this engine as the default, and if for whatever reason
335*2139Sjp161948 * /dev/crypto won't do what we want it will just be done in
336*2139Sjp161948 * software
337*2139Sjp161948 *
338*2139Sjp161948 * This can (should) be greatly expanded to perhaps take into
339*2139Sjp161948 * account speed of the device, and what we want to do.
340*2139Sjp161948 * (although the disabling of particular alg's could be controlled
341*2139Sjp161948 * by the device driver with sysctl's.) - this is where we
342*2139Sjp161948 * want most of the decisions made about what we actually want
343*2139Sjp161948 * to use from /dev/crypto.
344*2139Sjp161948 */
345*2139Sjp161948 static int
cryptodev_usable_ciphers(const int ** nids)346*2139Sjp161948 cryptodev_usable_ciphers(const int **nids)
347*2139Sjp161948 {
348*2139Sjp161948 return (get_cryptodev_ciphers(nids));
349*2139Sjp161948 }
350*2139Sjp161948
351*2139Sjp161948 static int
cryptodev_usable_digests(const int ** nids)352*2139Sjp161948 cryptodev_usable_digests(const int **nids)
353*2139Sjp161948 {
354*2139Sjp161948 /*
355*2139Sjp161948 * XXXX just disable all digests for now, because it sucks.
356*2139Sjp161948 * we need a better way to decide this - i.e. I may not
357*2139Sjp161948 * want digests on slow cards like hifn on fast machines,
358*2139Sjp161948 * but might want them on slow or loaded machines, etc.
359*2139Sjp161948 * will also want them when using crypto cards that don't
360*2139Sjp161948 * suck moose gonads - would be nice to be able to decide something
361*2139Sjp161948 * as reasonable default without having hackery that's card dependent.
362*2139Sjp161948 * of course, the default should probably be just do everything,
363*2139Sjp161948 * with perhaps a sysctl to turn algoritms off (or have them off
364*2139Sjp161948 * by default) on cards that generally suck like the hifn.
365*2139Sjp161948 */
366*2139Sjp161948 *nids = NULL;
367*2139Sjp161948 return (0);
368*2139Sjp161948 }
369*2139Sjp161948
370*2139Sjp161948 static int
cryptodev_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)371*2139Sjp161948 cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
372*2139Sjp161948 const unsigned char *in, unsigned int inl)
373*2139Sjp161948 {
374*2139Sjp161948 struct crypt_op cryp;
375*2139Sjp161948 struct dev_crypto_state *state = ctx->cipher_data;
376*2139Sjp161948 struct session_op *sess = &state->d_sess;
377*2139Sjp161948 void *iiv;
378*2139Sjp161948 unsigned char save_iv[EVP_MAX_IV_LENGTH];
379*2139Sjp161948
380*2139Sjp161948 if (state->d_fd < 0)
381*2139Sjp161948 return (0);
382*2139Sjp161948 if (!inl)
383*2139Sjp161948 return (1);
384*2139Sjp161948 if ((inl % ctx->cipher->block_size) != 0)
385*2139Sjp161948 return (0);
386*2139Sjp161948
387*2139Sjp161948 memset(&cryp, 0, sizeof(cryp));
388*2139Sjp161948
389*2139Sjp161948 cryp.ses = sess->ses;
390*2139Sjp161948 cryp.flags = 0;
391*2139Sjp161948 cryp.len = inl;
392*2139Sjp161948 cryp.src = (caddr_t) in;
393*2139Sjp161948 cryp.dst = (caddr_t) out;
394*2139Sjp161948 cryp.mac = 0;
395*2139Sjp161948
396*2139Sjp161948 cryp.op = ctx->encrypt ? COP_ENCRYPT : COP_DECRYPT;
397*2139Sjp161948
398*2139Sjp161948 if (ctx->cipher->iv_len) {
399*2139Sjp161948 cryp.iv = (caddr_t) ctx->iv;
400*2139Sjp161948 if (!ctx->encrypt) {
401*2139Sjp161948 iiv = (void *) in + inl - ctx->cipher->iv_len;
402*2139Sjp161948 memcpy(save_iv, iiv, ctx->cipher->iv_len);
403*2139Sjp161948 }
404*2139Sjp161948 } else
405*2139Sjp161948 cryp.iv = NULL;
406*2139Sjp161948
407*2139Sjp161948 if (ioctl(state->d_fd, CIOCCRYPT, &cryp) == -1) {
408*2139Sjp161948 /* XXX need better errror handling
409*2139Sjp161948 * this can fail for a number of different reasons.
410*2139Sjp161948 */
411*2139Sjp161948 return (0);
412*2139Sjp161948 }
413*2139Sjp161948
414*2139Sjp161948 if (ctx->cipher->iv_len) {
415*2139Sjp161948 if (ctx->encrypt)
416*2139Sjp161948 iiv = (void *) out + inl - ctx->cipher->iv_len;
417*2139Sjp161948 else
418*2139Sjp161948 iiv = save_iv;
419*2139Sjp161948 memcpy(ctx->iv, iiv, ctx->cipher->iv_len);
420*2139Sjp161948 }
421*2139Sjp161948 return (1);
422*2139Sjp161948 }
423*2139Sjp161948
424*2139Sjp161948 static int
cryptodev_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)425*2139Sjp161948 cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
426*2139Sjp161948 const unsigned char *iv, int enc)
427*2139Sjp161948 {
428*2139Sjp161948 struct dev_crypto_state *state = ctx->cipher_data;
429*2139Sjp161948 struct session_op *sess = &state->d_sess;
430*2139Sjp161948 int cipher;
431*2139Sjp161948
432*2139Sjp161948 if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef)
433*2139Sjp161948 return (0);
434*2139Sjp161948
435*2139Sjp161948 if (ctx->cipher->iv_len > cryptodev_max_iv(cipher))
436*2139Sjp161948 return (0);
437*2139Sjp161948
438*2139Sjp161948 if (!cryptodev_key_length_valid(cipher, ctx->key_len))
439*2139Sjp161948 return (0);
440*2139Sjp161948
441*2139Sjp161948 memset(sess, 0, sizeof(struct session_op));
442*2139Sjp161948
443*2139Sjp161948 if ((state->d_fd = get_dev_crypto()) < 0)
444*2139Sjp161948 return (0);
445*2139Sjp161948
446*2139Sjp161948 sess->key = (unsigned char *)key;
447*2139Sjp161948 sess->keylen = ctx->key_len;
448*2139Sjp161948 sess->cipher = cipher;
449*2139Sjp161948
450*2139Sjp161948 if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) {
451*2139Sjp161948 close(state->d_fd);
452*2139Sjp161948 state->d_fd = -1;
453*2139Sjp161948 return (0);
454*2139Sjp161948 }
455*2139Sjp161948 return (1);
456*2139Sjp161948 }
457*2139Sjp161948
458*2139Sjp161948 /*
459*2139Sjp161948 * free anything we allocated earlier when initting a
460*2139Sjp161948 * session, and close the session.
461*2139Sjp161948 */
462*2139Sjp161948 static int
cryptodev_cleanup(EVP_CIPHER_CTX * ctx)463*2139Sjp161948 cryptodev_cleanup(EVP_CIPHER_CTX *ctx)
464*2139Sjp161948 {
465*2139Sjp161948 int ret = 0;
466*2139Sjp161948 struct dev_crypto_state *state = ctx->cipher_data;
467*2139Sjp161948 struct session_op *sess = &state->d_sess;
468*2139Sjp161948
469*2139Sjp161948 if (state->d_fd < 0)
470*2139Sjp161948 return (0);
471*2139Sjp161948
472*2139Sjp161948 /* XXX if this ioctl fails, someting's wrong. the invoker
473*2139Sjp161948 * may have called us with a bogus ctx, or we could
474*2139Sjp161948 * have a device that for whatever reason just doesn't
475*2139Sjp161948 * want to play ball - it's not clear what's right
476*2139Sjp161948 * here - should this be an error? should it just
477*2139Sjp161948 * increase a counter, hmm. For right now, we return
478*2139Sjp161948 * 0 - I don't believe that to be "right". we could
479*2139Sjp161948 * call the gorpy openssl lib error handlers that
480*2139Sjp161948 * print messages to users of the library. hmm..
481*2139Sjp161948 */
482*2139Sjp161948
483*2139Sjp161948 if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) == -1) {
484*2139Sjp161948 ret = 0;
485*2139Sjp161948 } else {
486*2139Sjp161948 ret = 1;
487*2139Sjp161948 }
488*2139Sjp161948 close(state->d_fd);
489*2139Sjp161948 state->d_fd = -1;
490*2139Sjp161948
491*2139Sjp161948 return (ret);
492*2139Sjp161948 }
493*2139Sjp161948
494*2139Sjp161948 /*
495*2139Sjp161948 * libcrypto EVP stuff - this is how we get wired to EVP so the engine
496*2139Sjp161948 * gets called when libcrypto requests a cipher NID.
497*2139Sjp161948 */
498*2139Sjp161948
499*2139Sjp161948 /* DES CBC EVP */
500*2139Sjp161948 const EVP_CIPHER cryptodev_des_cbc = {
501*2139Sjp161948 NID_des_cbc,
502*2139Sjp161948 8, 8, 8,
503*2139Sjp161948 EVP_CIPH_CBC_MODE,
504*2139Sjp161948 cryptodev_init_key,
505*2139Sjp161948 cryptodev_cipher,
506*2139Sjp161948 cryptodev_cleanup,
507*2139Sjp161948 sizeof(struct dev_crypto_state),
508*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
509*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
510*2139Sjp161948 NULL
511*2139Sjp161948 };
512*2139Sjp161948
513*2139Sjp161948 /* 3DES CBC EVP */
514*2139Sjp161948 const EVP_CIPHER cryptodev_3des_cbc = {
515*2139Sjp161948 NID_des_ede3_cbc,
516*2139Sjp161948 8, 24, 8,
517*2139Sjp161948 EVP_CIPH_CBC_MODE,
518*2139Sjp161948 cryptodev_init_key,
519*2139Sjp161948 cryptodev_cipher,
520*2139Sjp161948 cryptodev_cleanup,
521*2139Sjp161948 sizeof(struct dev_crypto_state),
522*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
523*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
524*2139Sjp161948 NULL
525*2139Sjp161948 };
526*2139Sjp161948
527*2139Sjp161948 const EVP_CIPHER cryptodev_bf_cbc = {
528*2139Sjp161948 NID_bf_cbc,
529*2139Sjp161948 8, 16, 8,
530*2139Sjp161948 EVP_CIPH_CBC_MODE,
531*2139Sjp161948 cryptodev_init_key,
532*2139Sjp161948 cryptodev_cipher,
533*2139Sjp161948 cryptodev_cleanup,
534*2139Sjp161948 sizeof(struct dev_crypto_state),
535*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
536*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
537*2139Sjp161948 NULL
538*2139Sjp161948 };
539*2139Sjp161948
540*2139Sjp161948 const EVP_CIPHER cryptodev_cast_cbc = {
541*2139Sjp161948 NID_cast5_cbc,
542*2139Sjp161948 8, 16, 8,
543*2139Sjp161948 EVP_CIPH_CBC_MODE,
544*2139Sjp161948 cryptodev_init_key,
545*2139Sjp161948 cryptodev_cipher,
546*2139Sjp161948 cryptodev_cleanup,
547*2139Sjp161948 sizeof(struct dev_crypto_state),
548*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
549*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
550*2139Sjp161948 NULL
551*2139Sjp161948 };
552*2139Sjp161948
553*2139Sjp161948 const EVP_CIPHER cryptodev_aes_cbc = {
554*2139Sjp161948 NID_aes_128_cbc,
555*2139Sjp161948 16, 16, 16,
556*2139Sjp161948 EVP_CIPH_CBC_MODE,
557*2139Sjp161948 cryptodev_init_key,
558*2139Sjp161948 cryptodev_cipher,
559*2139Sjp161948 cryptodev_cleanup,
560*2139Sjp161948 sizeof(struct dev_crypto_state),
561*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
562*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
563*2139Sjp161948 NULL
564*2139Sjp161948 };
565*2139Sjp161948
566*2139Sjp161948 /*
567*2139Sjp161948 * Registered by the ENGINE when used to find out how to deal with
568*2139Sjp161948 * a particular NID in the ENGINE. this says what we'll do at the
569*2139Sjp161948 * top level - note, that list is restricted by what we answer with
570*2139Sjp161948 */
571*2139Sjp161948 static int
cryptodev_engine_ciphers(ENGINE * e,const EVP_CIPHER ** cipher,const int ** nids,int nid)572*2139Sjp161948 cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
573*2139Sjp161948 const int **nids, int nid)
574*2139Sjp161948 {
575*2139Sjp161948 if (!cipher)
576*2139Sjp161948 return (cryptodev_usable_ciphers(nids));
577*2139Sjp161948
578*2139Sjp161948 switch (nid) {
579*2139Sjp161948 case NID_des_ede3_cbc:
580*2139Sjp161948 *cipher = &cryptodev_3des_cbc;
581*2139Sjp161948 break;
582*2139Sjp161948 case NID_des_cbc:
583*2139Sjp161948 *cipher = &cryptodev_des_cbc;
584*2139Sjp161948 break;
585*2139Sjp161948 case NID_bf_cbc:
586*2139Sjp161948 *cipher = &cryptodev_bf_cbc;
587*2139Sjp161948 break;
588*2139Sjp161948 case NID_cast5_cbc:
589*2139Sjp161948 *cipher = &cryptodev_cast_cbc;
590*2139Sjp161948 break;
591*2139Sjp161948 case NID_aes_128_cbc:
592*2139Sjp161948 *cipher = &cryptodev_aes_cbc;
593*2139Sjp161948 break;
594*2139Sjp161948 default:
595*2139Sjp161948 *cipher = NULL;
596*2139Sjp161948 break;
597*2139Sjp161948 }
598*2139Sjp161948 return (*cipher != NULL);
599*2139Sjp161948 }
600*2139Sjp161948
601*2139Sjp161948 static int
cryptodev_engine_digests(ENGINE * e,const EVP_MD ** digest,const int ** nids,int nid)602*2139Sjp161948 cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest,
603*2139Sjp161948 const int **nids, int nid)
604*2139Sjp161948 {
605*2139Sjp161948 if (!digest)
606*2139Sjp161948 return (cryptodev_usable_digests(nids));
607*2139Sjp161948
608*2139Sjp161948 switch (nid) {
609*2139Sjp161948 case NID_md5:
610*2139Sjp161948 *digest = NULL; /* need to make a clean md5 critter */
611*2139Sjp161948 break;
612*2139Sjp161948 default:
613*2139Sjp161948 *digest = NULL;
614*2139Sjp161948 break;
615*2139Sjp161948 }
616*2139Sjp161948 return (*digest != NULL);
617*2139Sjp161948 }
618*2139Sjp161948
619*2139Sjp161948 /*
620*2139Sjp161948 * Convert a BIGNUM to the representation that /dev/crypto needs.
621*2139Sjp161948 * Upon completion of use, the caller is responsible for freeing
622*2139Sjp161948 * crp->crp_p.
623*2139Sjp161948 */
624*2139Sjp161948 static int
bn2crparam(const BIGNUM * a,struct crparam * crp)625*2139Sjp161948 bn2crparam(const BIGNUM *a, struct crparam *crp)
626*2139Sjp161948 {
627*2139Sjp161948 int i, j, k;
628*2139Sjp161948 ssize_t words, bytes, bits;
629*2139Sjp161948 u_char *b;
630*2139Sjp161948
631*2139Sjp161948 crp->crp_p = NULL;
632*2139Sjp161948 crp->crp_nbits = 0;
633*2139Sjp161948
634*2139Sjp161948 bits = BN_num_bits(a);
635*2139Sjp161948 bytes = (bits + 7) / 8;
636*2139Sjp161948
637*2139Sjp161948 b = malloc(bytes);
638*2139Sjp161948 if (b == NULL)
639*2139Sjp161948 return (1);
640*2139Sjp161948
641*2139Sjp161948 crp->crp_p = b;
642*2139Sjp161948 crp->crp_nbits = bits;
643*2139Sjp161948
644*2139Sjp161948 for (i = 0, j = 0; i < a->top; i++) {
645*2139Sjp161948 for (k = 0; k < BN_BITS2 / 8; k++) {
646*2139Sjp161948 if ((j + k) >= bytes)
647*2139Sjp161948 return (0);
648*2139Sjp161948 b[j + k] = a->d[i] >> (k * 8);
649*2139Sjp161948 }
650*2139Sjp161948 j += BN_BITS2 / 8;
651*2139Sjp161948 }
652*2139Sjp161948 return (0);
653*2139Sjp161948 }
654*2139Sjp161948
655*2139Sjp161948 /* Convert a /dev/crypto parameter to a BIGNUM */
656*2139Sjp161948 static int
crparam2bn(struct crparam * crp,BIGNUM * a)657*2139Sjp161948 crparam2bn(struct crparam *crp, BIGNUM *a)
658*2139Sjp161948 {
659*2139Sjp161948 u_int8_t *pd;
660*2139Sjp161948 int i, bytes;
661*2139Sjp161948
662*2139Sjp161948 bytes = (crp->crp_nbits + 7) / 8;
663*2139Sjp161948
664*2139Sjp161948 if (bytes == 0)
665*2139Sjp161948 return (-1);
666*2139Sjp161948
667*2139Sjp161948 if ((pd = (u_int8_t *) malloc(bytes)) == NULL)
668*2139Sjp161948 return (-1);
669*2139Sjp161948
670*2139Sjp161948 for (i = 0; i < bytes; i++)
671*2139Sjp161948 pd[i] = crp->crp_p[bytes - i - 1];
672*2139Sjp161948
673*2139Sjp161948 BN_bin2bn(pd, bytes, a);
674*2139Sjp161948 free(pd);
675*2139Sjp161948
676*2139Sjp161948 return (0);
677*2139Sjp161948 }
678*2139Sjp161948
679*2139Sjp161948 static void
zapparams(struct crypt_kop * kop)680*2139Sjp161948 zapparams(struct crypt_kop *kop)
681*2139Sjp161948 {
682*2139Sjp161948 int i;
683*2139Sjp161948
684*2139Sjp161948 for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) {
685*2139Sjp161948 if (kop->crk_param[i].crp_p)
686*2139Sjp161948 free(kop->crk_param[i].crp_p);
687*2139Sjp161948 kop->crk_param[i].crp_p = NULL;
688*2139Sjp161948 kop->crk_param[i].crp_nbits = 0;
689*2139Sjp161948 }
690*2139Sjp161948 }
691*2139Sjp161948
692*2139Sjp161948 static int
cryptodev_asym(struct crypt_kop * kop,int rlen,BIGNUM * r,int slen,BIGNUM * s)693*2139Sjp161948 cryptodev_asym(struct crypt_kop *kop, int rlen, BIGNUM *r, int slen, BIGNUM *s)
694*2139Sjp161948 {
695*2139Sjp161948 int fd, ret = -1;
696*2139Sjp161948
697*2139Sjp161948 if ((fd = get_asym_dev_crypto()) < 0)
698*2139Sjp161948 return (ret);
699*2139Sjp161948
700*2139Sjp161948 if (r) {
701*2139Sjp161948 kop->crk_param[kop->crk_iparams].crp_p = calloc(rlen, sizeof(char));
702*2139Sjp161948 kop->crk_param[kop->crk_iparams].crp_nbits = rlen * 8;
703*2139Sjp161948 kop->crk_oparams++;
704*2139Sjp161948 }
705*2139Sjp161948 if (s) {
706*2139Sjp161948 kop->crk_param[kop->crk_iparams+1].crp_p = calloc(slen, sizeof(char));
707*2139Sjp161948 kop->crk_param[kop->crk_iparams+1].crp_nbits = slen * 8;
708*2139Sjp161948 kop->crk_oparams++;
709*2139Sjp161948 }
710*2139Sjp161948
711*2139Sjp161948 if (ioctl(fd, CIOCKEY, kop) == 0) {
712*2139Sjp161948 if (r)
713*2139Sjp161948 crparam2bn(&kop->crk_param[kop->crk_iparams], r);
714*2139Sjp161948 if (s)
715*2139Sjp161948 crparam2bn(&kop->crk_param[kop->crk_iparams+1], s);
716*2139Sjp161948 ret = 0;
717*2139Sjp161948 }
718*2139Sjp161948
719*2139Sjp161948 return (ret);
720*2139Sjp161948 }
721*2139Sjp161948
722*2139Sjp161948 static int
cryptodev_bn_mod_exp(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * in_mont)723*2139Sjp161948 cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
724*2139Sjp161948 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
725*2139Sjp161948 {
726*2139Sjp161948 struct crypt_kop kop;
727*2139Sjp161948 int ret = 1;
728*2139Sjp161948
729*2139Sjp161948 /* Currently, we know we can do mod exp iff we can do any
730*2139Sjp161948 * asymmetric operations at all.
731*2139Sjp161948 */
732*2139Sjp161948 if (cryptodev_asymfeat == 0) {
733*2139Sjp161948 ret = BN_mod_exp(r, a, p, m, ctx);
734*2139Sjp161948 return (ret);
735*2139Sjp161948 }
736*2139Sjp161948
737*2139Sjp161948 memset(&kop, 0, sizeof kop);
738*2139Sjp161948 kop.crk_op = CRK_MOD_EXP;
739*2139Sjp161948
740*2139Sjp161948 /* inputs: a^p % m */
741*2139Sjp161948 if (bn2crparam(a, &kop.crk_param[0]))
742*2139Sjp161948 goto err;
743*2139Sjp161948 if (bn2crparam(p, &kop.crk_param[1]))
744*2139Sjp161948 goto err;
745*2139Sjp161948 if (bn2crparam(m, &kop.crk_param[2]))
746*2139Sjp161948 goto err;
747*2139Sjp161948 kop.crk_iparams = 3;
748*2139Sjp161948
749*2139Sjp161948 if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) {
750*2139Sjp161948 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
751*2139Sjp161948 ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont);
752*2139Sjp161948 }
753*2139Sjp161948 err:
754*2139Sjp161948 zapparams(&kop);
755*2139Sjp161948 return (ret);
756*2139Sjp161948 }
757*2139Sjp161948
758*2139Sjp161948 static int
cryptodev_rsa_nocrt_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa)759*2139Sjp161948 cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
760*2139Sjp161948 {
761*2139Sjp161948 int r;
762*2139Sjp161948 BN_CTX *ctx;
763*2139Sjp161948
764*2139Sjp161948 ctx = BN_CTX_new();
765*2139Sjp161948 r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL);
766*2139Sjp161948 BN_CTX_free(ctx);
767*2139Sjp161948 return (r);
768*2139Sjp161948 }
769*2139Sjp161948
770*2139Sjp161948 static int
cryptodev_rsa_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa,BN_CTX * ctx)771*2139Sjp161948 cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
772*2139Sjp161948 {
773*2139Sjp161948 struct crypt_kop kop;
774*2139Sjp161948 int ret = 1;
775*2139Sjp161948
776*2139Sjp161948 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
777*2139Sjp161948 /* XXX 0 means failure?? */
778*2139Sjp161948 return (0);
779*2139Sjp161948 }
780*2139Sjp161948
781*2139Sjp161948 memset(&kop, 0, sizeof kop);
782*2139Sjp161948 kop.crk_op = CRK_MOD_EXP_CRT;
783*2139Sjp161948 /* inputs: rsa->p rsa->q I rsa->dmp1 rsa->dmq1 rsa->iqmp */
784*2139Sjp161948 if (bn2crparam(rsa->p, &kop.crk_param[0]))
785*2139Sjp161948 goto err;
786*2139Sjp161948 if (bn2crparam(rsa->q, &kop.crk_param[1]))
787*2139Sjp161948 goto err;
788*2139Sjp161948 if (bn2crparam(I, &kop.crk_param[2]))
789*2139Sjp161948 goto err;
790*2139Sjp161948 if (bn2crparam(rsa->dmp1, &kop.crk_param[3]))
791*2139Sjp161948 goto err;
792*2139Sjp161948 if (bn2crparam(rsa->dmq1, &kop.crk_param[4]))
793*2139Sjp161948 goto err;
794*2139Sjp161948 if (bn2crparam(rsa->iqmp, &kop.crk_param[5]))
795*2139Sjp161948 goto err;
796*2139Sjp161948 kop.crk_iparams = 6;
797*2139Sjp161948
798*2139Sjp161948 if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) {
799*2139Sjp161948 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
800*2139Sjp161948 ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
801*2139Sjp161948 }
802*2139Sjp161948 err:
803*2139Sjp161948 zapparams(&kop);
804*2139Sjp161948 return (ret);
805*2139Sjp161948 }
806*2139Sjp161948
807*2139Sjp161948 static RSA_METHOD cryptodev_rsa = {
808*2139Sjp161948 "cryptodev RSA method",
809*2139Sjp161948 NULL, /* rsa_pub_enc */
810*2139Sjp161948 NULL, /* rsa_pub_dec */
811*2139Sjp161948 NULL, /* rsa_priv_enc */
812*2139Sjp161948 NULL, /* rsa_priv_dec */
813*2139Sjp161948 NULL,
814*2139Sjp161948 NULL,
815*2139Sjp161948 NULL, /* init */
816*2139Sjp161948 NULL, /* finish */
817*2139Sjp161948 0, /* flags */
818*2139Sjp161948 NULL, /* app_data */
819*2139Sjp161948 NULL, /* rsa_sign */
820*2139Sjp161948 NULL /* rsa_verify */
821*2139Sjp161948 };
822*2139Sjp161948
823*2139Sjp161948 static int
cryptodev_dsa_bn_mod_exp(DSA * dsa,BIGNUM * r,BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)824*2139Sjp161948 cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
825*2139Sjp161948 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
826*2139Sjp161948 {
827*2139Sjp161948 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
828*2139Sjp161948 }
829*2139Sjp161948
830*2139Sjp161948 static int
cryptodev_dsa_dsa_mod_exp(DSA * dsa,BIGNUM * t1,BIGNUM * g,BIGNUM * u1,BIGNUM * pub_key,BIGNUM * u2,BIGNUM * p,BN_CTX * ctx,BN_MONT_CTX * mont)831*2139Sjp161948 cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g,
832*2139Sjp161948 BIGNUM *u1, BIGNUM *pub_key, BIGNUM *u2, BIGNUM *p,
833*2139Sjp161948 BN_CTX *ctx, BN_MONT_CTX *mont)
834*2139Sjp161948 {
835*2139Sjp161948 BIGNUM t2;
836*2139Sjp161948 int ret = 0;
837*2139Sjp161948
838*2139Sjp161948 BN_init(&t2);
839*2139Sjp161948
840*2139Sjp161948 /* v = ( g^u1 * y^u2 mod p ) mod q */
841*2139Sjp161948 /* let t1 = g ^ u1 mod p */
842*2139Sjp161948 ret = 0;
843*2139Sjp161948
844*2139Sjp161948 if (!dsa->meth->bn_mod_exp(dsa,t1,dsa->g,u1,dsa->p,ctx,mont))
845*2139Sjp161948 goto err;
846*2139Sjp161948
847*2139Sjp161948 /* let t2 = y ^ u2 mod p */
848*2139Sjp161948 if (!dsa->meth->bn_mod_exp(dsa,&t2,dsa->pub_key,u2,dsa->p,ctx,mont))
849*2139Sjp161948 goto err;
850*2139Sjp161948 /* let u1 = t1 * t2 mod p */
851*2139Sjp161948 if (!BN_mod_mul(u1,t1,&t2,dsa->p,ctx))
852*2139Sjp161948 goto err;
853*2139Sjp161948
854*2139Sjp161948 BN_copy(t1,u1);
855*2139Sjp161948
856*2139Sjp161948 ret = 1;
857*2139Sjp161948 err:
858*2139Sjp161948 BN_free(&t2);
859*2139Sjp161948 return(ret);
860*2139Sjp161948 }
861*2139Sjp161948
862*2139Sjp161948 static DSA_SIG *
cryptodev_dsa_do_sign(const unsigned char * dgst,int dlen,DSA * dsa)863*2139Sjp161948 cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
864*2139Sjp161948 {
865*2139Sjp161948 struct crypt_kop kop;
866*2139Sjp161948 BIGNUM *r = NULL, *s = NULL;
867*2139Sjp161948 DSA_SIG *dsaret = NULL;
868*2139Sjp161948
869*2139Sjp161948 if ((r = BN_new()) == NULL)
870*2139Sjp161948 goto err;
871*2139Sjp161948 if ((s = BN_new()) == NULL) {
872*2139Sjp161948 BN_free(r);
873*2139Sjp161948 goto err;
874*2139Sjp161948 }
875*2139Sjp161948
876*2139Sjp161948 memset(&kop, 0, sizeof kop);
877*2139Sjp161948 kop.crk_op = CRK_DSA_SIGN;
878*2139Sjp161948
879*2139Sjp161948 /* inputs: dgst dsa->p dsa->q dsa->g dsa->priv_key */
880*2139Sjp161948 kop.crk_param[0].crp_p = (caddr_t)dgst;
881*2139Sjp161948 kop.crk_param[0].crp_nbits = dlen * 8;
882*2139Sjp161948 if (bn2crparam(dsa->p, &kop.crk_param[1]))
883*2139Sjp161948 goto err;
884*2139Sjp161948 if (bn2crparam(dsa->q, &kop.crk_param[2]))
885*2139Sjp161948 goto err;
886*2139Sjp161948 if (bn2crparam(dsa->g, &kop.crk_param[3]))
887*2139Sjp161948 goto err;
888*2139Sjp161948 if (bn2crparam(dsa->priv_key, &kop.crk_param[4]))
889*2139Sjp161948 goto err;
890*2139Sjp161948 kop.crk_iparams = 5;
891*2139Sjp161948
892*2139Sjp161948 if (cryptodev_asym(&kop, BN_num_bytes(dsa->q), r,
893*2139Sjp161948 BN_num_bytes(dsa->q), s) == 0) {
894*2139Sjp161948 dsaret = DSA_SIG_new();
895*2139Sjp161948 dsaret->r = r;
896*2139Sjp161948 dsaret->s = s;
897*2139Sjp161948 } else {
898*2139Sjp161948 const DSA_METHOD *meth = DSA_OpenSSL();
899*2139Sjp161948 BN_free(r);
900*2139Sjp161948 BN_free(s);
901*2139Sjp161948 dsaret = (meth->dsa_do_sign)(dgst, dlen, dsa);
902*2139Sjp161948 }
903*2139Sjp161948 err:
904*2139Sjp161948 kop.crk_param[0].crp_p = NULL;
905*2139Sjp161948 zapparams(&kop);
906*2139Sjp161948 return (dsaret);
907*2139Sjp161948 }
908*2139Sjp161948
909*2139Sjp161948 static int
cryptodev_dsa_verify(const unsigned char * dgst,int dlen,DSA_SIG * sig,DSA * dsa)910*2139Sjp161948 cryptodev_dsa_verify(const unsigned char *dgst, int dlen,
911*2139Sjp161948 DSA_SIG *sig, DSA *dsa)
912*2139Sjp161948 {
913*2139Sjp161948 struct crypt_kop kop;
914*2139Sjp161948 int dsaret = 1;
915*2139Sjp161948
916*2139Sjp161948 memset(&kop, 0, sizeof kop);
917*2139Sjp161948 kop.crk_op = CRK_DSA_VERIFY;
918*2139Sjp161948
919*2139Sjp161948 /* inputs: dgst dsa->p dsa->q dsa->g dsa->pub_key sig->r sig->s */
920*2139Sjp161948 kop.crk_param[0].crp_p = (caddr_t)dgst;
921*2139Sjp161948 kop.crk_param[0].crp_nbits = dlen * 8;
922*2139Sjp161948 if (bn2crparam(dsa->p, &kop.crk_param[1]))
923*2139Sjp161948 goto err;
924*2139Sjp161948 if (bn2crparam(dsa->q, &kop.crk_param[2]))
925*2139Sjp161948 goto err;
926*2139Sjp161948 if (bn2crparam(dsa->g, &kop.crk_param[3]))
927*2139Sjp161948 goto err;
928*2139Sjp161948 if (bn2crparam(dsa->pub_key, &kop.crk_param[4]))
929*2139Sjp161948 goto err;
930*2139Sjp161948 if (bn2crparam(sig->r, &kop.crk_param[5]))
931*2139Sjp161948 goto err;
932*2139Sjp161948 if (bn2crparam(sig->s, &kop.crk_param[6]))
933*2139Sjp161948 goto err;
934*2139Sjp161948 kop.crk_iparams = 7;
935*2139Sjp161948
936*2139Sjp161948 if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) {
937*2139Sjp161948 dsaret = kop.crk_status;
938*2139Sjp161948 } else {
939*2139Sjp161948 const DSA_METHOD *meth = DSA_OpenSSL();
940*2139Sjp161948
941*2139Sjp161948 dsaret = (meth->dsa_do_verify)(dgst, dlen, sig, dsa);
942*2139Sjp161948 }
943*2139Sjp161948 err:
944*2139Sjp161948 kop.crk_param[0].crp_p = NULL;
945*2139Sjp161948 zapparams(&kop);
946*2139Sjp161948 return (dsaret);
947*2139Sjp161948 }
948*2139Sjp161948
949*2139Sjp161948 static DSA_METHOD cryptodev_dsa = {
950*2139Sjp161948 "cryptodev DSA method",
951*2139Sjp161948 NULL,
952*2139Sjp161948 NULL, /* dsa_sign_setup */
953*2139Sjp161948 NULL,
954*2139Sjp161948 NULL, /* dsa_mod_exp */
955*2139Sjp161948 NULL,
956*2139Sjp161948 NULL, /* init */
957*2139Sjp161948 NULL, /* finish */
958*2139Sjp161948 0, /* flags */
959*2139Sjp161948 NULL /* app_data */
960*2139Sjp161948 };
961*2139Sjp161948
962*2139Sjp161948 static int
cryptodev_mod_exp_dh(const DH * dh,BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)963*2139Sjp161948 cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
964*2139Sjp161948 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
965*2139Sjp161948 BN_MONT_CTX *m_ctx)
966*2139Sjp161948 {
967*2139Sjp161948 return (cryptodev_bn_mod_exp(r, a, p, m, ctx, m_ctx));
968*2139Sjp161948 }
969*2139Sjp161948
970*2139Sjp161948 static int
cryptodev_dh_compute_key(unsigned char * key,const BIGNUM * pub_key,DH * dh)971*2139Sjp161948 cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
972*2139Sjp161948 {
973*2139Sjp161948 struct crypt_kop kop;
974*2139Sjp161948 int dhret = 1;
975*2139Sjp161948 int fd, keylen;
976*2139Sjp161948
977*2139Sjp161948 if ((fd = get_asym_dev_crypto()) < 0) {
978*2139Sjp161948 const DH_METHOD *meth = DH_OpenSSL();
979*2139Sjp161948
980*2139Sjp161948 return ((meth->compute_key)(key, pub_key, dh));
981*2139Sjp161948 }
982*2139Sjp161948
983*2139Sjp161948 keylen = BN_num_bits(dh->p);
984*2139Sjp161948
985*2139Sjp161948 memset(&kop, 0, sizeof kop);
986*2139Sjp161948 kop.crk_op = CRK_DH_COMPUTE_KEY;
987*2139Sjp161948
988*2139Sjp161948 /* inputs: dh->priv_key pub_key dh->p key */
989*2139Sjp161948 if (bn2crparam(dh->priv_key, &kop.crk_param[0]))
990*2139Sjp161948 goto err;
991*2139Sjp161948 if (bn2crparam(pub_key, &kop.crk_param[1]))
992*2139Sjp161948 goto err;
993*2139Sjp161948 if (bn2crparam(dh->p, &kop.crk_param[2]))
994*2139Sjp161948 goto err;
995*2139Sjp161948 kop.crk_iparams = 3;
996*2139Sjp161948
997*2139Sjp161948 kop.crk_param[3].crp_p = key;
998*2139Sjp161948 kop.crk_param[3].crp_nbits = keylen * 8;
999*2139Sjp161948 kop.crk_oparams = 1;
1000*2139Sjp161948
1001*2139Sjp161948 if (ioctl(fd, CIOCKEY, &kop) == -1) {
1002*2139Sjp161948 const DH_METHOD *meth = DH_OpenSSL();
1003*2139Sjp161948
1004*2139Sjp161948 dhret = (meth->compute_key)(key, pub_key, dh);
1005*2139Sjp161948 }
1006*2139Sjp161948 err:
1007*2139Sjp161948 kop.crk_param[3].crp_p = NULL;
1008*2139Sjp161948 zapparams(&kop);
1009*2139Sjp161948 return (dhret);
1010*2139Sjp161948 }
1011*2139Sjp161948
1012*2139Sjp161948 static DH_METHOD cryptodev_dh = {
1013*2139Sjp161948 "cryptodev DH method",
1014*2139Sjp161948 NULL, /* cryptodev_dh_generate_key */
1015*2139Sjp161948 NULL,
1016*2139Sjp161948 NULL,
1017*2139Sjp161948 NULL,
1018*2139Sjp161948 NULL,
1019*2139Sjp161948 0, /* flags */
1020*2139Sjp161948 NULL /* app_data */
1021*2139Sjp161948 };
1022*2139Sjp161948
1023*2139Sjp161948 /*
1024*2139Sjp161948 * ctrl right now is just a wrapper that doesn't do much
1025*2139Sjp161948 * but I expect we'll want some options soon.
1026*2139Sjp161948 */
1027*2139Sjp161948 static int
cryptodev_ctrl(ENGINE * e,int cmd,long i,void * p,void (* f)())1028*2139Sjp161948 cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
1029*2139Sjp161948 {
1030*2139Sjp161948 #ifdef HAVE_SYSLOG_R
1031*2139Sjp161948 struct syslog_data sd = SYSLOG_DATA_INIT;
1032*2139Sjp161948 #endif
1033*2139Sjp161948
1034*2139Sjp161948 switch (cmd) {
1035*2139Sjp161948 default:
1036*2139Sjp161948 #ifdef HAVE_SYSLOG_R
1037*2139Sjp161948 syslog_r(LOG_ERR, &sd,
1038*2139Sjp161948 "cryptodev_ctrl: unknown command %d", cmd);
1039*2139Sjp161948 #else
1040*2139Sjp161948 syslog(LOG_ERR, "cryptodev_ctrl: unknown command %d", cmd);
1041*2139Sjp161948 #endif
1042*2139Sjp161948 break;
1043*2139Sjp161948 }
1044*2139Sjp161948 return (1);
1045*2139Sjp161948 }
1046*2139Sjp161948
1047*2139Sjp161948 void
ENGINE_load_cryptodev(void)1048*2139Sjp161948 ENGINE_load_cryptodev(void)
1049*2139Sjp161948 {
1050*2139Sjp161948 ENGINE *engine = ENGINE_new();
1051*2139Sjp161948 int fd;
1052*2139Sjp161948
1053*2139Sjp161948 if (engine == NULL)
1054*2139Sjp161948 return;
1055*2139Sjp161948 if ((fd = get_dev_crypto()) < 0) {
1056*2139Sjp161948 ENGINE_free(engine);
1057*2139Sjp161948 return;
1058*2139Sjp161948 }
1059*2139Sjp161948
1060*2139Sjp161948 /*
1061*2139Sjp161948 * find out what asymmetric crypto algorithms we support
1062*2139Sjp161948 */
1063*2139Sjp161948 if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) {
1064*2139Sjp161948 close(fd);
1065*2139Sjp161948 ENGINE_free(engine);
1066*2139Sjp161948 return;
1067*2139Sjp161948 }
1068*2139Sjp161948 close(fd);
1069*2139Sjp161948
1070*2139Sjp161948 if (!ENGINE_set_id(engine, "cryptodev") ||
1071*2139Sjp161948 !ENGINE_set_name(engine, "BSD cryptodev engine") ||
1072*2139Sjp161948 !ENGINE_set_ciphers(engine, cryptodev_engine_ciphers) ||
1073*2139Sjp161948 !ENGINE_set_digests(engine, cryptodev_engine_digests) ||
1074*2139Sjp161948 !ENGINE_set_ctrl_function(engine, cryptodev_ctrl) ||
1075*2139Sjp161948 !ENGINE_set_cmd_defns(engine, cryptodev_defns)) {
1076*2139Sjp161948 ENGINE_free(engine);
1077*2139Sjp161948 return;
1078*2139Sjp161948 }
1079*2139Sjp161948
1080*2139Sjp161948 if (ENGINE_set_RSA(engine, &cryptodev_rsa)) {
1081*2139Sjp161948 const RSA_METHOD *rsa_meth = RSA_PKCS1_SSLeay();
1082*2139Sjp161948
1083*2139Sjp161948 cryptodev_rsa.bn_mod_exp = rsa_meth->bn_mod_exp;
1084*2139Sjp161948 cryptodev_rsa.rsa_mod_exp = rsa_meth->rsa_mod_exp;
1085*2139Sjp161948 cryptodev_rsa.rsa_pub_enc = rsa_meth->rsa_pub_enc;
1086*2139Sjp161948 cryptodev_rsa.rsa_pub_dec = rsa_meth->rsa_pub_dec;
1087*2139Sjp161948 cryptodev_rsa.rsa_priv_enc = rsa_meth->rsa_priv_enc;
1088*2139Sjp161948 cryptodev_rsa.rsa_priv_dec = rsa_meth->rsa_priv_dec;
1089*2139Sjp161948 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1090*2139Sjp161948 cryptodev_rsa.bn_mod_exp = cryptodev_bn_mod_exp;
1091*2139Sjp161948 if (cryptodev_asymfeat & CRF_MOD_EXP_CRT)
1092*2139Sjp161948 cryptodev_rsa.rsa_mod_exp =
1093*2139Sjp161948 cryptodev_rsa_mod_exp;
1094*2139Sjp161948 else
1095*2139Sjp161948 cryptodev_rsa.rsa_mod_exp =
1096*2139Sjp161948 cryptodev_rsa_nocrt_mod_exp;
1097*2139Sjp161948 }
1098*2139Sjp161948 }
1099*2139Sjp161948
1100*2139Sjp161948 if (ENGINE_set_DSA(engine, &cryptodev_dsa)) {
1101*2139Sjp161948 const DSA_METHOD *meth = DSA_OpenSSL();
1102*2139Sjp161948
1103*2139Sjp161948 memcpy(&cryptodev_dsa, meth, sizeof(DSA_METHOD));
1104*2139Sjp161948 if (cryptodev_asymfeat & CRF_DSA_SIGN)
1105*2139Sjp161948 cryptodev_dsa.dsa_do_sign = cryptodev_dsa_do_sign;
1106*2139Sjp161948 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1107*2139Sjp161948 cryptodev_dsa.bn_mod_exp = cryptodev_dsa_bn_mod_exp;
1108*2139Sjp161948 cryptodev_dsa.dsa_mod_exp = cryptodev_dsa_dsa_mod_exp;
1109*2139Sjp161948 }
1110*2139Sjp161948 if (cryptodev_asymfeat & CRF_DSA_VERIFY)
1111*2139Sjp161948 cryptodev_dsa.dsa_do_verify = cryptodev_dsa_verify;
1112*2139Sjp161948 }
1113*2139Sjp161948
1114*2139Sjp161948 if (ENGINE_set_DH(engine, &cryptodev_dh)){
1115*2139Sjp161948 const DH_METHOD *dh_meth = DH_OpenSSL();
1116*2139Sjp161948
1117*2139Sjp161948 cryptodev_dh.generate_key = dh_meth->generate_key;
1118*2139Sjp161948 cryptodev_dh.compute_key = dh_meth->compute_key;
1119*2139Sjp161948 cryptodev_dh.bn_mod_exp = dh_meth->bn_mod_exp;
1120*2139Sjp161948 if (cryptodev_asymfeat & CRF_MOD_EXP) {
1121*2139Sjp161948 cryptodev_dh.bn_mod_exp = cryptodev_mod_exp_dh;
1122*2139Sjp161948 if (cryptodev_asymfeat & CRF_DH_COMPUTE_KEY)
1123*2139Sjp161948 cryptodev_dh.compute_key =
1124*2139Sjp161948 cryptodev_dh_compute_key;
1125*2139Sjp161948 }
1126*2139Sjp161948 }
1127*2139Sjp161948
1128*2139Sjp161948 ENGINE_add(engine);
1129*2139Sjp161948 ENGINE_free(engine);
1130*2139Sjp161948 ERR_clear_error();
1131*2139Sjp161948 }
1132*2139Sjp161948
1133*2139Sjp161948 #endif /* HAVE_CRYPTODEV */
1134