]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/opencrypto/cryptosoft.c
cryptosoft: Fix support for variable tag lengths in AES-CCM.
[FreeBSD/FreeBSD.git] / sys / opencrypto / cryptosoft.c
1 /*      $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
2
3 /*-
4  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5  * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
6  *
7  * This code was written by Angelos D. Keromytis in Athens, Greece, in
8  * February 2000. Network Security Technologies Inc. (NSTI) kindly
9  * supported the development of this code.
10  *
11  * Copyright (c) 2000, 2001 Angelos D. Keromytis
12  * Copyright (c) 2014-2021 The FreeBSD Foundation
13  * All rights reserved.
14  *
15  * Portions of this software were developed by John-Mark Gurney
16  * under sponsorship of the FreeBSD Foundation and
17  * Rubicon Communications, LLC (Netgate).
18  *
19  * Portions of this software were developed by Ararat River
20  * Consulting, LLC under sponsorship of the FreeBSD Foundation.
21  *
22  * Permission to use, copy, and modify this software with or without fee
23  * is hereby granted, provided that this entire notice is included in
24  * all source code copies of any software which is or includes a copy or
25  * modification of this software.
26  *
27  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
28  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
29  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
30  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
31  * PURPOSE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/sysctl.h>
43 #include <sys/errno.h>
44 #include <sys/random.h>
45 #include <sys/kernel.h>
46 #include <sys/uio.h>
47 #include <sys/lock.h>
48 #include <sys/rwlock.h>
49 #include <sys/endian.h>
50 #include <sys/limits.h>
51 #include <sys/mutex.h>
52
53 #include <crypto/sha1.h>
54 #include <opencrypto/rmd160.h>
55
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/xform.h>
58
59 #include <sys/kobj.h>
60 #include <sys/bus.h>
61 #include "cryptodev_if.h"
62
63 struct swcr_auth {
64         void            *sw_ictx;
65         void            *sw_octx;
66         const struct auth_hash *sw_axf;
67         uint16_t        sw_mlen;
68 };
69
70 struct swcr_encdec {
71         void            *sw_kschedule;
72         const struct enc_xform *sw_exf;
73 };
74
75 struct swcr_compdec {
76         const struct comp_algo *sw_cxf;
77 };
78
79 struct swcr_session {
80         struct mtx      swcr_lock;
81         int     (*swcr_process)(struct swcr_session *, struct cryptop *);
82
83         struct swcr_auth swcr_auth;
84         struct swcr_encdec swcr_encdec;
85         struct swcr_compdec swcr_compdec;
86 };
87
88 static  int32_t swcr_id;
89
90 static  void swcr_freesession(device_t dev, crypto_session_t cses);
91
92 /* Used for CRYPTO_NULL_CBC. */
93 static int
94 swcr_null(struct swcr_session *ses, struct cryptop *crp)
95 {
96
97         return (0);
98 }
99
100 /*
101  * Apply a symmetric encryption/decryption algorithm.
102  */
103 static int
104 swcr_encdec(struct swcr_session *ses, struct cryptop *crp)
105 {
106         unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN];
107         unsigned char *ivp, *nivp, iv2[EALG_MAX_BLOCK_LEN];
108         const struct crypto_session_params *csp;
109         const struct enc_xform *exf;
110         struct swcr_encdec *sw;
111         size_t inlen, outlen;
112         int i, blks, resid;
113         struct crypto_buffer_cursor cc_in, cc_out;
114         const unsigned char *inblk;
115         unsigned char *outblk;
116         int error;
117         bool encrypting;
118
119         error = 0;
120
121         sw = &ses->swcr_encdec;
122         exf = sw->sw_exf;
123         csp = crypto_get_params(crp->crp_session);
124
125         if (exf->native_blocksize == 0) {
126                 /* Check for non-padded data */
127                 if ((crp->crp_payload_length % exf->blocksize) != 0)
128                         return (EINVAL);
129
130                 blks = exf->blocksize;
131         } else
132                 blks = exf->native_blocksize;
133
134         if (exf == &enc_xform_aes_icm &&
135             (crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
136                 return (EINVAL);
137
138         if (crp->crp_cipher_key != NULL) {
139                 error = exf->setkey(sw->sw_kschedule,
140                     crp->crp_cipher_key, csp->csp_cipher_klen);
141                 if (error)
142                         return (error);
143         }
144
145         crypto_read_iv(crp, iv);
146
147         if (exf->reinit) {
148                 /*
149                  * xforms that provide a reinit method perform all IV
150                  * handling themselves.
151                  */
152                 exf->reinit(sw->sw_kschedule, iv, csp->csp_ivlen);
153         }
154
155         ivp = iv;
156
157         crypto_cursor_init(&cc_in, &crp->crp_buf);
158         crypto_cursor_advance(&cc_in, crp->crp_payload_start);
159         inblk = crypto_cursor_segment(&cc_in, &inlen);
160         if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
161                 crypto_cursor_init(&cc_out, &crp->crp_obuf);
162                 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
163         } else
164                 cc_out = cc_in;
165         outblk = crypto_cursor_segment(&cc_out, &outlen);
166
167         resid = crp->crp_payload_length;
168         encrypting = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
169
170         /*
171          * Loop through encrypting blocks.  'inlen' is the remaining
172          * length of the current segment in the input buffer.
173          * 'outlen' is the remaining length of current segment in the
174          * output buffer.
175          */
176         while (resid >= blks) {
177                 /*
178                  * If the current block is not contained within the
179                  * current input/output segment, use 'blk' as a local
180                  * buffer.
181                  */
182                 if (inlen < blks) {
183                         crypto_cursor_copydata(&cc_in, blks, blk);
184                         inblk = blk;
185                 }
186                 if (outlen < blks)
187                         outblk = blk;
188
189                 /*
190                  * Ciphers without a 'reinit' hook are assumed to be
191                  * used in CBC mode where the chaining is done here.
192                  */
193                 if (exf->reinit != NULL) {
194                         if (encrypting)
195                                 exf->encrypt(sw->sw_kschedule, inblk, outblk);
196                         else
197                                 exf->decrypt(sw->sw_kschedule, inblk, outblk);
198                 } else if (encrypting) {
199                         /* XOR with previous block */
200                         for (i = 0; i < blks; i++)
201                                 outblk[i] = inblk[i] ^ ivp[i];
202
203                         exf->encrypt(sw->sw_kschedule, outblk, outblk);
204
205                         /*
206                          * Keep encrypted block for XOR'ing
207                          * with next block
208                          */
209                         memcpy(iv, outblk, blks);
210                         ivp = iv;
211                 } else {        /* decrypt */
212                         /*
213                          * Keep encrypted block for XOR'ing
214                          * with next block
215                          */
216                         nivp = (ivp == iv) ? iv2 : iv;
217                         memcpy(nivp, inblk, blks);
218
219                         exf->decrypt(sw->sw_kschedule, inblk, outblk);
220
221                         /* XOR with previous block */
222                         for (i = 0; i < blks; i++)
223                                 outblk[i] ^= ivp[i];
224
225                         ivp = nivp;
226                 }
227
228                 if (inlen < blks) {
229                         inblk = crypto_cursor_segment(&cc_in, &inlen);
230                 } else {
231                         crypto_cursor_advance(&cc_in, blks);
232                         inlen -= blks;
233                         inblk += blks;
234                 }
235
236                 if (outlen < blks) {
237                         crypto_cursor_copyback(&cc_out, blks, blk);
238                         outblk = crypto_cursor_segment(&cc_out, &outlen);
239                 } else {
240                         crypto_cursor_advance(&cc_out, blks);
241                         outlen -= blks;
242                         outblk += blks;
243                 }
244
245                 resid -= blks;
246         }
247
248         /* Handle trailing partial block for stream ciphers. */
249         if (resid > 0) {
250                 KASSERT(exf->native_blocksize != 0,
251                     ("%s: partial block of %d bytes for cipher %s",
252                     __func__, i, exf->name));
253                 KASSERT(exf->reinit != NULL,
254                     ("%s: partial block cipher %s without reinit hook",
255                     __func__, exf->name));
256                 KASSERT(resid < blks, ("%s: partial block too big", __func__));
257
258                 inblk = crypto_cursor_segment(&cc_in, &inlen);
259                 outblk = crypto_cursor_segment(&cc_out, &outlen);
260                 if (inlen < resid) {
261                         crypto_cursor_copydata(&cc_in, resid, blk);
262                         inblk = blk;
263                 }
264                 if (outlen < resid)
265                         outblk = blk;
266                 if (encrypting)
267                         exf->encrypt_last(sw->sw_kschedule, inblk, outblk,
268                             resid);
269                 else
270                         exf->decrypt_last(sw->sw_kschedule, inblk, outblk,
271                             resid);
272                 if (outlen < resid)
273                         crypto_cursor_copyback(&cc_out, resid, blk);
274         }
275
276         explicit_bzero(blk, sizeof(blk));
277         explicit_bzero(iv, sizeof(iv));
278         explicit_bzero(iv2, sizeof(iv2));
279         return (0);
280 }
281
282 static void
283 swcr_authprepare(const struct auth_hash *axf, struct swcr_auth *sw,
284     const uint8_t *key, int klen)
285 {
286
287         switch (axf->type) {
288         case CRYPTO_SHA1_HMAC:
289         case CRYPTO_SHA2_224_HMAC:
290         case CRYPTO_SHA2_256_HMAC:
291         case CRYPTO_SHA2_384_HMAC:
292         case CRYPTO_SHA2_512_HMAC:
293         case CRYPTO_NULL_HMAC:
294         case CRYPTO_RIPEMD160_HMAC:
295                 hmac_init_ipad(axf, key, klen, sw->sw_ictx);
296                 hmac_init_opad(axf, key, klen, sw->sw_octx);
297                 break;
298         case CRYPTO_POLY1305:
299         case CRYPTO_BLAKE2B:
300         case CRYPTO_BLAKE2S:
301                 axf->Setkey(sw->sw_ictx, key, klen);
302                 axf->Init(sw->sw_ictx);
303                 break;
304         default:
305                 panic("%s: algorithm %d doesn't use keys", __func__, axf->type);
306         }
307 }
308
309 /*
310  * Compute or verify hash.
311  */
312 static int
313 swcr_authcompute(struct swcr_session *ses, struct cryptop *crp)
314 {
315         u_char aalg[HASH_MAX_LEN];
316         const struct crypto_session_params *csp;
317         struct swcr_auth *sw;
318         const struct auth_hash *axf;
319         union authctx ctx;
320         int err;
321
322         sw = &ses->swcr_auth;
323
324         axf = sw->sw_axf;
325
326         csp = crypto_get_params(crp->crp_session);
327         if (crp->crp_auth_key != NULL) {
328                 swcr_authprepare(axf, sw, crp->crp_auth_key,
329                     csp->csp_auth_klen);
330         }
331
332         bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
333
334         if (crp->crp_aad != NULL)
335                 err = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
336         else
337                 err = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length,
338                     axf->Update, &ctx);
339         if (err)
340                 goto out;
341
342         if (CRYPTO_HAS_OUTPUT_BUFFER(crp) &&
343             CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
344                 err = crypto_apply_buf(&crp->crp_obuf,
345                     crp->crp_payload_output_start, crp->crp_payload_length,
346                     axf->Update, &ctx);
347         else
348                 err = crypto_apply(crp, crp->crp_payload_start,
349                     crp->crp_payload_length, axf->Update, &ctx);
350         if (err)
351                 goto out;
352
353         if (csp->csp_flags & CSP_F_ESN)
354                 axf->Update(&ctx, crp->crp_esn, 4);
355
356         axf->Final(aalg, &ctx);
357         if (sw->sw_octx != NULL) {
358                 bcopy(sw->sw_octx, &ctx, axf->ctxsize);
359                 axf->Update(&ctx, aalg, axf->hashsize);
360                 axf->Final(aalg, &ctx);
361         }
362
363         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
364                 u_char uaalg[HASH_MAX_LEN];
365
366                 crypto_copydata(crp, crp->crp_digest_start, sw->sw_mlen, uaalg);
367                 if (timingsafe_bcmp(aalg, uaalg, sw->sw_mlen) != 0)
368                         err = EBADMSG;
369                 explicit_bzero(uaalg, sizeof(uaalg));
370         } else {
371                 /* Inject the authentication data */
372                 crypto_copyback(crp, crp->crp_digest_start, sw->sw_mlen, aalg);
373         }
374         explicit_bzero(aalg, sizeof(aalg));
375 out:
376         explicit_bzero(&ctx, sizeof(ctx));
377         return (err);
378 }
379
380 CTASSERT(INT_MAX <= (1ll<<39) - 256);   /* GCM: plain text < 2^39-256 */
381 CTASSERT(INT_MAX <= (uint64_t)-1);      /* GCM: associated data <= 2^64-1 */
382
383 static int
384 swcr_gmac(struct swcr_session *ses, struct cryptop *crp)
385 {
386         uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
387         u_char *blk = (u_char *)blkbuf;
388         u_char tag[GMAC_DIGEST_LEN];
389         u_char iv[AES_BLOCK_LEN];
390         struct crypto_buffer_cursor cc;
391         const u_char *inblk;
392         union authctx ctx;
393         struct swcr_auth *swa;
394         const struct auth_hash *axf;
395         uint32_t *blkp;
396         size_t len;
397         int blksz, error, ivlen, resid;
398
399         swa = &ses->swcr_auth;
400         axf = swa->sw_axf;
401
402         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
403         blksz = GMAC_BLOCK_LEN;
404         KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
405             __func__));
406
407         /* Initialize the IV */
408         ivlen = AES_GCM_IV_LEN;
409         crypto_read_iv(crp, iv);
410
411         axf->Reinit(&ctx, iv, ivlen);
412         crypto_cursor_init(&cc, &crp->crp_buf);
413         crypto_cursor_advance(&cc, crp->crp_payload_start);
414         for (resid = crp->crp_payload_length; resid >= blksz; resid -= len) {
415                 inblk = crypto_cursor_segment(&cc, &len);
416                 if (len >= blksz) {
417                         len = rounddown(MIN(len, resid), blksz);
418                         crypto_cursor_advance(&cc, len);
419                 } else {
420                         len = blksz;
421                         crypto_cursor_copydata(&cc, len, blk);
422                         inblk = blk;
423                 }
424                 axf->Update(&ctx, inblk, len);
425         }
426         if (resid > 0) {
427                 memset(blk, 0, blksz);
428                 crypto_cursor_copydata(&cc, resid, blk);
429                 axf->Update(&ctx, blk, blksz);
430         }
431
432         /* length block */
433         memset(blk, 0, blksz);
434         blkp = (uint32_t *)blk + 1;
435         *blkp = htobe32(crp->crp_payload_length * 8);
436         axf->Update(&ctx, blk, blksz);
437
438         /* Finalize MAC */
439         axf->Final(tag, &ctx);
440
441         error = 0;
442         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
443                 u_char tag2[GMAC_DIGEST_LEN];
444
445                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
446                     tag2);
447                 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
448                         error = EBADMSG;
449                 explicit_bzero(tag2, sizeof(tag2));
450         } else {
451                 /* Inject the authentication data */
452                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
453         }
454         explicit_bzero(blkbuf, sizeof(blkbuf));
455         explicit_bzero(tag, sizeof(tag));
456         explicit_bzero(iv, sizeof(iv));
457         return (error);
458 }
459
460 static int
461 swcr_gcm(struct swcr_session *ses, struct cryptop *crp)
462 {
463         uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
464         u_char *blk = (u_char *)blkbuf;
465         u_char tag[GMAC_DIGEST_LEN];
466         struct crypto_buffer_cursor cc_in, cc_out;
467         const u_char *inblk;
468         u_char *outblk;
469         union authctx ctx;
470         struct swcr_auth *swa;
471         struct swcr_encdec *swe;
472         const struct auth_hash *axf;
473         const struct enc_xform *exf;
474         uint32_t *blkp;
475         size_t len;
476         int blksz, error, ivlen, r, resid;
477
478         swa = &ses->swcr_auth;
479         axf = swa->sw_axf;
480
481         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
482         blksz = GMAC_BLOCK_LEN;
483         KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
484             __func__));
485
486         swe = &ses->swcr_encdec;
487         exf = swe->sw_exf;
488         KASSERT(axf->blocksize == exf->native_blocksize,
489             ("%s: blocksize mismatch", __func__));
490
491         if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
492                 return (EINVAL);
493
494         ivlen = AES_GCM_IV_LEN;
495
496         /* Supply MAC with IV */
497         axf->Reinit(&ctx, crp->crp_iv, ivlen);
498
499         /* Supply MAC with AAD */
500         if (crp->crp_aad != NULL) {
501                 len = rounddown(crp->crp_aad_length, blksz);
502                 if (len != 0)
503                         axf->Update(&ctx, crp->crp_aad, len);
504                 if (crp->crp_aad_length != len) {
505                         memset(blk, 0, blksz);
506                         memcpy(blk, (char *)crp->crp_aad + len,
507                             crp->crp_aad_length - len);
508                         axf->Update(&ctx, blk, blksz);
509                 }
510         } else {
511                 crypto_cursor_init(&cc_in, &crp->crp_buf);
512                 crypto_cursor_advance(&cc_in, crp->crp_aad_start);
513                 for (resid = crp->crp_aad_length; resid >= blksz;
514                      resid -= len) {
515                         inblk = crypto_cursor_segment(&cc_in, &len);
516                         if (len >= blksz) {
517                                 len = rounddown(MIN(len, resid), blksz);
518                                 crypto_cursor_advance(&cc_in, len);
519                         } else {
520                                 len = blksz;
521                                 crypto_cursor_copydata(&cc_in, len, blk);
522                                 inblk = blk;
523                         }
524                         axf->Update(&ctx, inblk, len);
525                 }
526                 if (resid > 0) {
527                         memset(blk, 0, blksz);
528                         crypto_cursor_copydata(&cc_in, resid, blk);
529                         axf->Update(&ctx, blk, blksz);
530                 }
531         }
532
533         if (crp->crp_cipher_key != NULL)
534                 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
535                     crypto_get_params(crp->crp_session)->csp_cipher_klen);
536         exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
537
538         /* Do encryption with MAC */
539         crypto_cursor_init(&cc_in, &crp->crp_buf);
540         crypto_cursor_advance(&cc_in, crp->crp_payload_start);
541         if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
542                 crypto_cursor_init(&cc_out, &crp->crp_obuf);
543                 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
544         } else
545                 cc_out = cc_in;
546         for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
547                 inblk = crypto_cursor_segment(&cc_in, &len);
548                 if (len < blksz) {
549                         crypto_cursor_copydata(&cc_in, blksz, blk);
550                         inblk = blk;
551                 } else {
552                         crypto_cursor_advance(&cc_in, blksz);
553                 }
554                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
555                         outblk = crypto_cursor_segment(&cc_out, &len);
556                         if (len < blksz)
557                                 outblk = blk;
558                         exf->encrypt(swe->sw_kschedule, inblk, outblk);
559                         axf->Update(&ctx, outblk, blksz);
560                         if (outblk == blk)
561                                 crypto_cursor_copyback(&cc_out, blksz, blk);
562                         else
563                                 crypto_cursor_advance(&cc_out, blksz);
564                 } else {
565                         axf->Update(&ctx, inblk, blksz);
566                 }
567         }
568         if (resid > 0) {
569                 crypto_cursor_copydata(&cc_in, resid, blk);
570                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
571                         exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
572                         crypto_cursor_copyback(&cc_out, resid, blk);
573                 }
574                 axf->Update(&ctx, blk, resid);
575         }
576
577         /* length block */
578         memset(blk, 0, blksz);
579         blkp = (uint32_t *)blk + 1;
580         *blkp = htobe32(crp->crp_aad_length * 8);
581         blkp = (uint32_t *)blk + 3;
582         *blkp = htobe32(crp->crp_payload_length * 8);
583         axf->Update(&ctx, blk, blksz);
584
585         /* Finalize MAC */
586         axf->Final(tag, &ctx);
587
588         /* Validate tag */
589         error = 0;
590         if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
591                 u_char tag2[GMAC_DIGEST_LEN];
592
593                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen, tag2);
594
595                 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
596                 explicit_bzero(tag2, sizeof(tag2));
597                 if (r != 0) {
598                         error = EBADMSG;
599                         goto out;
600                 }
601
602                 /* tag matches, decrypt data */
603                 crypto_cursor_init(&cc_in, &crp->crp_buf);
604                 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
605                 for (resid = crp->crp_payload_length; resid > blksz;
606                      resid -= blksz) {
607                         inblk = crypto_cursor_segment(&cc_in, &len);
608                         if (len < blksz) {
609                                 crypto_cursor_copydata(&cc_in, blksz, blk);
610                                 inblk = blk;
611                         } else
612                                 crypto_cursor_advance(&cc_in, blksz);
613                         outblk = crypto_cursor_segment(&cc_out, &len);
614                         if (len < blksz)
615                                 outblk = blk;
616                         exf->decrypt(swe->sw_kschedule, inblk, outblk);
617                         if (outblk == blk)
618                                 crypto_cursor_copyback(&cc_out, blksz, blk);
619                         else
620                                 crypto_cursor_advance(&cc_out, blksz);
621                 }
622                 if (resid > 0) {
623                         crypto_cursor_copydata(&cc_in, resid, blk);
624                         exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
625                         crypto_cursor_copyback(&cc_out, resid, blk);
626                 }
627         } else {
628                 /* Inject the authentication data */
629                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
630         }
631
632 out:
633         explicit_bzero(blkbuf, sizeof(blkbuf));
634         explicit_bzero(tag, sizeof(tag));
635
636         return (error);
637 }
638
639 static void
640 build_ccm_b0(const char *nonce, u_int nonce_length, u_int aad_length,
641     u_int data_length, u_int tag_length, uint8_t *b0)
642 {
643         uint8_t *bp;
644         uint8_t flags, L;
645
646         KASSERT(nonce_length >= 7 && nonce_length <= 13,
647             ("nonce_length must be between 7 and 13 bytes"));
648
649         /*
650          * Need to determine the L field value.  This is the number of
651          * bytes needed to specify the length of the message; the length
652          * is whatever is left in the 16 bytes after specifying flags and
653          * the nonce.
654          */
655         L = 15 - nonce_length;
656
657         flags = ((aad_length > 0) << 6) +
658             (((tag_length - 2) / 2) << 3) +
659             L - 1;
660
661         /*
662          * Now we need to set up the first block, which has flags, nonce,
663          * and the message length.
664          */
665         b0[0] = flags;
666         memcpy(b0 + 1, nonce, nonce_length);
667         bp = b0 + 1 + nonce_length;
668
669         /* Need to copy L' [aka L-1] bytes of data_length */
670         for (uint8_t *dst = b0 + CCM_CBC_BLOCK_LEN - 1; dst >= bp; dst--) {
671                 *dst = data_length;
672                 data_length >>= 8;
673         }
674 }
675
676 /* NB: OCF only supports AAD lengths < 2^32. */
677 static int
678 build_ccm_aad_length(u_int aad_length, uint8_t *blk)
679 {
680         if (aad_length < ((1 << 16) - (1 << 8))) {
681                 be16enc(blk, aad_length);
682                 return (sizeof(uint16_t));
683         } else {
684                 blk[0] = 0xff;
685                 blk[1] = 0xfe;
686                 be32enc(blk + 2, aad_length);
687                 return (2 + sizeof(uint32_t));
688         }
689 }
690
691 static int
692 swcr_ccm_cbc_mac(struct swcr_session *ses, struct cryptop *crp)
693 {
694         u_char iv[AES_BLOCK_LEN];
695         u_char blk[CCM_CBC_BLOCK_LEN];
696         u_char tag[AES_CBC_MAC_HASH_LEN];
697         union authctx ctx;
698         const struct crypto_session_params *csp;
699         struct swcr_auth *swa;
700         const struct auth_hash *axf;
701         int error, ivlen, len;
702
703         csp = crypto_get_params(crp->crp_session);
704         swa = &ses->swcr_auth;
705         axf = swa->sw_axf;
706
707         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
708
709         /* Initialize the IV */
710         ivlen = csp->csp_ivlen;
711         crypto_read_iv(crp, iv);
712
713         /* Supply MAC with IV */
714         axf->Reinit(&ctx, crp->crp_iv, ivlen);
715
716         /* Supply MAC with b0. */
717         build_ccm_b0(crp->crp_iv, ivlen, crp->crp_payload_length, 0,
718             swa->sw_mlen, blk);
719         axf->Update(&ctx, blk, CCM_CBC_BLOCK_LEN);
720
721         len = build_ccm_aad_length(crp->crp_payload_length, blk);
722         axf->Update(&ctx, blk, len);
723
724         crypto_apply(crp, crp->crp_payload_start, crp->crp_payload_length,
725             axf->Update, &ctx);
726
727         /* Finalize MAC */
728         axf->Final(tag, &ctx);
729
730         error = 0;
731         if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
732                 u_char tag2[AES_CBC_MAC_HASH_LEN];
733
734                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
735                     tag2);
736                 if (timingsafe_bcmp(tag, tag2, swa->sw_mlen) != 0)
737                         error = EBADMSG;
738                 explicit_bzero(tag2, sizeof(tag));
739         } else {
740                 /* Inject the authentication data */
741                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
742         }
743         explicit_bzero(tag, sizeof(tag));
744         explicit_bzero(blk, sizeof(blk));
745         explicit_bzero(iv, sizeof(iv));
746         return (error);
747 }
748
749 static int
750 swcr_ccm(struct swcr_session *ses, struct cryptop *crp)
751 {
752         const struct crypto_session_params *csp;
753         uint32_t blkbuf[howmany(AES_BLOCK_LEN, sizeof(uint32_t))];
754         u_char *blk = (u_char *)blkbuf;
755         u_char tag[AES_CBC_MAC_HASH_LEN];
756         struct crypto_buffer_cursor cc_in, cc_out;
757         const u_char *inblk;
758         u_char *outblk;
759         union authctx ctx;
760         struct swcr_auth *swa;
761         struct swcr_encdec *swe;
762         const struct auth_hash *axf;
763         const struct enc_xform *exf;
764         size_t len;
765         int blksz, error, ivlen, r, resid;
766
767         csp = crypto_get_params(crp->crp_session);
768         swa = &ses->swcr_auth;
769         axf = swa->sw_axf;
770
771         bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
772         blksz = AES_BLOCK_LEN;
773         KASSERT(axf->blocksize == blksz, ("%s: axf block size mismatch",
774             __func__));
775
776         swe = &ses->swcr_encdec;
777         exf = swe->sw_exf;
778         KASSERT(axf->blocksize == exf->native_blocksize,
779             ("%s: blocksize mismatch", __func__));
780
781         if (crp->crp_payload_length > ccm_max_payload_length(csp))
782                 return (EMSGSIZE);
783
784         if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
785                 return (EINVAL);
786
787         ivlen = csp->csp_ivlen;
788
789         /* Supply MAC with IV */
790         axf->Reinit(&ctx, crp->crp_iv, ivlen);
791
792         /* Supply MAC with b0. */
793         _Static_assert(sizeof(blkbuf) >= CCM_CBC_BLOCK_LEN,
794             "blkbuf too small for b0");
795         build_ccm_b0(crp->crp_iv, ivlen, crp->crp_aad_length,
796             crp->crp_payload_length, swa->sw_mlen, blk);
797         axf->Update(&ctx, blk, CCM_CBC_BLOCK_LEN);
798
799         /* Supply MAC with AAD */
800         if (crp->crp_aad_length != 0) {
801                 len = build_ccm_aad_length(crp->crp_aad_length, blk);
802                 axf->Update(&ctx, blk, len);
803                 if (crp->crp_aad != NULL)
804                         axf->Update(&ctx, crp->crp_aad,
805                             crp->crp_aad_length);
806                 else
807                         crypto_apply(crp, crp->crp_aad_start,
808                             crp->crp_aad_length, axf->Update, &ctx);
809
810                 /* Pad the AAD (including length field) to a full block. */
811                 len = (len + crp->crp_aad_length) % CCM_CBC_BLOCK_LEN;
812                 if (len != 0) {
813                         len = CCM_CBC_BLOCK_LEN - len;
814                         memset(blk, 0, CCM_CBC_BLOCK_LEN);
815                         axf->Update(&ctx, blk, len);
816                 }
817         }
818
819         if (crp->crp_cipher_key != NULL)
820                 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
821                     crypto_get_params(crp->crp_session)->csp_cipher_klen);
822         exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
823
824         /* Do encryption/decryption with MAC */
825         crypto_cursor_init(&cc_in, &crp->crp_buf);
826         crypto_cursor_advance(&cc_in, crp->crp_payload_start);
827         if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
828                 crypto_cursor_init(&cc_out, &crp->crp_obuf);
829                 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
830         } else
831                 cc_out = cc_in;
832         for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
833                 inblk = crypto_cursor_segment(&cc_in, &len);
834                 if (len < blksz) {
835                         crypto_cursor_copydata(&cc_in, blksz, blk);
836                         inblk = blk;
837                 } else
838                         crypto_cursor_advance(&cc_in, blksz);
839                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
840                         outblk = crypto_cursor_segment(&cc_out, &len);
841                         if (len < blksz)
842                                 outblk = blk;
843                         axf->Update(&ctx, inblk, blksz);
844                         exf->encrypt(swe->sw_kschedule, inblk, outblk);
845                         if (outblk == blk)
846                                 crypto_cursor_copyback(&cc_out, blksz, blk);
847                         else
848                                 crypto_cursor_advance(&cc_out, blksz);
849                 } else {
850                         /*
851                          * One of the problems with CCM+CBC is that
852                          * the authentication is done on the
853                          * unencrypted data.  As a result, we have to
854                          * decrypt the data twice: once to generate
855                          * the tag and a second time after the tag is
856                          * verified.
857                          */
858                         exf->decrypt(swe->sw_kschedule, inblk, blk);
859                         axf->Update(&ctx, blk, blksz);
860                 }
861         }
862         if (resid > 0) {
863                 crypto_cursor_copydata(&cc_in, resid, blk);
864                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
865                         axf->Update(&ctx, blk, resid);
866                         exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
867                         crypto_cursor_copyback(&cc_out, resid, blk);
868                 } else {
869                         exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
870                         axf->Update(&ctx, blk, resid);
871                 }
872         }
873
874         /* Finalize MAC */
875         axf->Final(tag, &ctx);
876
877         /* Validate tag */
878         error = 0;
879         if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
880                 u_char tag2[AES_CBC_MAC_HASH_LEN];
881
882                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen,
883                     tag2);
884
885                 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
886                 explicit_bzero(tag2, sizeof(tag2));
887                 if (r != 0) {
888                         error = EBADMSG;
889                         goto out;
890                 }
891
892                 /* tag matches, decrypt data */
893                 exf->reinit(swe->sw_kschedule, crp->crp_iv, ivlen);
894                 crypto_cursor_init(&cc_in, &crp->crp_buf);
895                 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
896                 for (resid = crp->crp_payload_length; resid > blksz;
897                      resid -= blksz) {
898                         inblk = crypto_cursor_segment(&cc_in, &len);
899                         if (len < blksz) {
900                                 crypto_cursor_copydata(&cc_in, blksz, blk);
901                                 inblk = blk;
902                         } else
903                                 crypto_cursor_advance(&cc_in, blksz);
904                         outblk = crypto_cursor_segment(&cc_out, &len);
905                         if (len < blksz)
906                                 outblk = blk;
907                         exf->decrypt(swe->sw_kschedule, inblk, outblk);
908                         if (outblk == blk)
909                                 crypto_cursor_copyback(&cc_out, blksz, blk);
910                         else
911                                 crypto_cursor_advance(&cc_out, blksz);
912                 }
913                 if (resid > 0) {
914                         crypto_cursor_copydata(&cc_in, resid, blk);
915                         exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
916                         crypto_cursor_copyback(&cc_out, resid, blk);
917                 }
918         } else {
919                 /* Inject the authentication data */
920                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
921         }
922
923 out:
924         explicit_bzero(blkbuf, sizeof(blkbuf));
925         explicit_bzero(tag, sizeof(tag));
926         return (error);
927 }
928
929 static int
930 swcr_chacha20_poly1305(struct swcr_session *ses, struct cryptop *crp)
931 {
932         const struct crypto_session_params *csp;
933         uint64_t blkbuf[howmany(CHACHA20_NATIVE_BLOCK_LEN, sizeof(uint64_t))];
934         u_char *blk = (u_char *)blkbuf;
935         u_char tag[POLY1305_HASH_LEN];
936         struct crypto_buffer_cursor cc_in, cc_out;
937         const u_char *inblk;
938         u_char *outblk;
939         uint64_t *blkp;
940         union authctx ctx;
941         struct swcr_auth *swa;
942         struct swcr_encdec *swe;
943         const struct auth_hash *axf;
944         const struct enc_xform *exf;
945         size_t len;
946         int blksz, error, r, resid;
947
948         swa = &ses->swcr_auth;
949         axf = swa->sw_axf;
950
951         swe = &ses->swcr_encdec;
952         exf = swe->sw_exf;
953         blksz = exf->native_blocksize;
954         KASSERT(blksz <= sizeof(blkbuf), ("%s: blocksize mismatch", __func__));
955
956         if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
957                 return (EINVAL);
958
959         csp = crypto_get_params(crp->crp_session);
960
961         /* Generate Poly1305 key. */
962         if (crp->crp_cipher_key != NULL)
963                 axf->Setkey(&ctx, crp->crp_cipher_key, csp->csp_cipher_klen);
964         else
965                 axf->Setkey(&ctx, csp->csp_cipher_key, csp->csp_cipher_klen);
966         axf->Reinit(&ctx, crp->crp_iv, csp->csp_ivlen);
967
968         /* Supply MAC with AAD */
969         if (crp->crp_aad != NULL)
970                 axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length);
971         else
972                 crypto_apply(crp, crp->crp_aad_start,
973                     crp->crp_aad_length, axf->Update, &ctx);
974         if (crp->crp_aad_length % 16 != 0) {
975                 /* padding1 */
976                 memset(blk, 0, 16);
977                 axf->Update(&ctx, blk, 16 - crp->crp_aad_length % 16);
978         }
979
980         if (crp->crp_cipher_key != NULL)
981                 exf->setkey(swe->sw_kschedule, crp->crp_cipher_key,
982                     csp->csp_cipher_klen);
983         exf->reinit(swe->sw_kschedule, crp->crp_iv, csp->csp_ivlen);
984
985         /* Do encryption with MAC */
986         crypto_cursor_init(&cc_in, &crp->crp_buf);
987         crypto_cursor_advance(&cc_in, crp->crp_payload_start);
988         if (CRYPTO_HAS_OUTPUT_BUFFER(crp)) {
989                 crypto_cursor_init(&cc_out, &crp->crp_obuf);
990                 crypto_cursor_advance(&cc_out, crp->crp_payload_output_start);
991         } else
992                 cc_out = cc_in;
993         for (resid = crp->crp_payload_length; resid >= blksz; resid -= blksz) {
994                 inblk = crypto_cursor_segment(&cc_in, &len);
995                 if (len < blksz) {
996                         crypto_cursor_copydata(&cc_in, blksz, blk);
997                         inblk = blk;
998                 } else
999                         crypto_cursor_advance(&cc_in, blksz);
1000                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1001                         outblk = crypto_cursor_segment(&cc_out, &len);
1002                         if (len < blksz)
1003                                 outblk = blk;
1004                         exf->encrypt(swe->sw_kschedule, inblk, outblk);
1005                         axf->Update(&ctx, outblk, blksz);
1006                         if (outblk == blk)
1007                                 crypto_cursor_copyback(&cc_out, blksz, blk);
1008                         else
1009                                 crypto_cursor_advance(&cc_out, blksz);
1010                 } else {
1011                         axf->Update(&ctx, inblk, blksz);
1012                 }
1013         }
1014         if (resid > 0) {
1015                 crypto_cursor_copydata(&cc_in, resid, blk);
1016                 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1017                         exf->encrypt_last(swe->sw_kschedule, blk, blk, resid);
1018                         crypto_cursor_copyback(&cc_out, resid, blk);
1019                 }
1020                 axf->Update(&ctx, blk, resid);
1021                 if (resid % 16 != 0) {
1022                         /* padding2 */
1023                         memset(blk, 0, 16);
1024                         axf->Update(&ctx, blk, 16 - resid % 16);
1025                 }
1026         }
1027
1028         /* lengths */
1029         blkp = (uint64_t *)blk;
1030         blkp[0] = htole64(crp->crp_aad_length);
1031         blkp[1] = htole64(crp->crp_payload_length);
1032         axf->Update(&ctx, blk, sizeof(uint64_t) * 2);
1033
1034         /* Finalize MAC */
1035         axf->Final(tag, &ctx);
1036
1037         /* Validate tag */
1038         error = 0;
1039         if (!CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1040                 u_char tag2[POLY1305_HASH_LEN];
1041
1042                 crypto_copydata(crp, crp->crp_digest_start, swa->sw_mlen, tag2);
1043
1044                 r = timingsafe_bcmp(tag, tag2, swa->sw_mlen);
1045                 explicit_bzero(tag2, sizeof(tag2));
1046                 if (r != 0) {
1047                         error = EBADMSG;
1048                         goto out;
1049                 }
1050
1051                 /* tag matches, decrypt data */
1052                 crypto_cursor_init(&cc_in, &crp->crp_buf);
1053                 crypto_cursor_advance(&cc_in, crp->crp_payload_start);
1054                 for (resid = crp->crp_payload_length; resid > blksz;
1055                      resid -= blksz) {
1056                         inblk = crypto_cursor_segment(&cc_in, &len);
1057                         if (len < blksz) {
1058                                 crypto_cursor_copydata(&cc_in, blksz, blk);
1059                                 inblk = blk;
1060                         } else
1061                                 crypto_cursor_advance(&cc_in, blksz);
1062                         outblk = crypto_cursor_segment(&cc_out, &len);
1063                         if (len < blksz)
1064                                 outblk = blk;
1065                         exf->decrypt(swe->sw_kschedule, inblk, outblk);
1066                         if (outblk == blk)
1067                                 crypto_cursor_copyback(&cc_out, blksz, blk);
1068                         else
1069                                 crypto_cursor_advance(&cc_out, blksz);
1070                 }
1071                 if (resid > 0) {
1072                         crypto_cursor_copydata(&cc_in, resid, blk);
1073                         exf->decrypt_last(swe->sw_kschedule, blk, blk, resid);
1074                         crypto_cursor_copyback(&cc_out, resid, blk);
1075                 }
1076         } else {
1077                 /* Inject the authentication data */
1078                 crypto_copyback(crp, crp->crp_digest_start, swa->sw_mlen, tag);
1079         }
1080
1081 out:
1082         explicit_bzero(blkbuf, sizeof(blkbuf));
1083         explicit_bzero(tag, sizeof(tag));
1084         explicit_bzero(&ctx, sizeof(ctx));
1085         return (error);
1086 }
1087
1088 /*
1089  * Apply a cipher and a digest to perform EtA.
1090  */
1091 static int
1092 swcr_eta(struct swcr_session *ses, struct cryptop *crp)
1093 {
1094         int error;
1095
1096         if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
1097                 error = swcr_encdec(ses, crp);
1098                 if (error == 0)
1099                         error = swcr_authcompute(ses, crp);
1100         } else {
1101                 error = swcr_authcompute(ses, crp);
1102                 if (error == 0)
1103                         error = swcr_encdec(ses, crp);
1104         }
1105         return (error);
1106 }
1107
1108 /*
1109  * Apply a compression/decompression algorithm
1110  */
1111 static int
1112 swcr_compdec(struct swcr_session *ses, struct cryptop *crp)
1113 {
1114         const struct comp_algo *cxf;
1115         uint8_t *data, *out;
1116         int adj;
1117         uint32_t result;
1118
1119         cxf = ses->swcr_compdec.sw_cxf;
1120
1121         /* We must handle the whole buffer of data in one time
1122          * then if there is not all the data in the mbuf, we must
1123          * copy in a buffer.
1124          */
1125
1126         data = malloc(crp->crp_payload_length, M_CRYPTO_DATA,  M_NOWAIT);
1127         if (data == NULL)
1128                 return (EINVAL);
1129         crypto_copydata(crp, crp->crp_payload_start, crp->crp_payload_length,
1130             data);
1131
1132         if (CRYPTO_OP_IS_COMPRESS(crp->crp_op))
1133                 result = cxf->compress(data, crp->crp_payload_length, &out);
1134         else
1135                 result = cxf->decompress(data, crp->crp_payload_length, &out);
1136
1137         free(data, M_CRYPTO_DATA);
1138         if (result == 0)
1139                 return (EINVAL);
1140         crp->crp_olen = result;
1141
1142         /* Check the compressed size when doing compression */
1143         if (CRYPTO_OP_IS_COMPRESS(crp->crp_op)) {
1144                 if (result >= crp->crp_payload_length) {
1145                         /* Compression was useless, we lost time */
1146                         free(out, M_CRYPTO_DATA);
1147                         return (0);
1148                 }
1149         }
1150
1151         /* Copy back the (de)compressed data. m_copyback is
1152          * extending the mbuf as necessary.
1153          */
1154         crypto_copyback(crp, crp->crp_payload_start, result, out);
1155         if (result < crp->crp_payload_length) {
1156                 switch (crp->crp_buf.cb_type) {
1157                 case CRYPTO_BUF_MBUF:
1158                 case CRYPTO_BUF_SINGLE_MBUF:
1159                         adj = result - crp->crp_payload_length;
1160                         m_adj(crp->crp_buf.cb_mbuf, adj);
1161                         break;
1162                 case CRYPTO_BUF_UIO: {
1163                         struct uio *uio = crp->crp_buf.cb_uio;
1164                         int ind;
1165
1166                         adj = crp->crp_payload_length - result;
1167                         ind = uio->uio_iovcnt - 1;
1168
1169                         while (adj > 0 && ind >= 0) {
1170                                 if (adj < uio->uio_iov[ind].iov_len) {
1171                                         uio->uio_iov[ind].iov_len -= adj;
1172                                         break;
1173                                 }
1174
1175                                 adj -= uio->uio_iov[ind].iov_len;
1176                                 uio->uio_iov[ind].iov_len = 0;
1177                                 ind--;
1178                                 uio->uio_iovcnt--;
1179                         }
1180                         }
1181                         break;
1182                 case CRYPTO_BUF_VMPAGE:
1183                         adj = crp->crp_payload_length - result;
1184                         crp->crp_buf.cb_vm_page_len -= adj;
1185                         break;
1186                 default:
1187                         break;
1188                 }
1189         }
1190         free(out, M_CRYPTO_DATA);
1191         return 0;
1192 }
1193
1194 static int
1195 swcr_setup_cipher(struct swcr_session *ses,
1196     const struct crypto_session_params *csp)
1197 {
1198         struct swcr_encdec *swe;
1199         const struct enc_xform *txf;
1200         int error;
1201
1202         swe = &ses->swcr_encdec;
1203         txf = crypto_cipher(csp);
1204         if (txf->ctxsize != 0) {
1205                 swe->sw_kschedule = malloc(txf->ctxsize, M_CRYPTO_DATA,
1206                     M_NOWAIT);
1207                 if (swe->sw_kschedule == NULL)
1208                         return (ENOMEM);
1209         }
1210         if (csp->csp_cipher_key != NULL) {
1211                 error = txf->setkey(swe->sw_kschedule,
1212                     csp->csp_cipher_key, csp->csp_cipher_klen);
1213                 if (error)
1214                         return (error);
1215         }
1216         swe->sw_exf = txf;
1217         return (0);
1218 }
1219
1220 static int
1221 swcr_setup_auth(struct swcr_session *ses,
1222     const struct crypto_session_params *csp)
1223 {
1224         struct swcr_auth *swa;
1225         const struct auth_hash *axf;
1226
1227         swa = &ses->swcr_auth;
1228
1229         axf = crypto_auth_hash(csp);
1230         swa->sw_axf = axf;
1231         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1232                 return (EINVAL);
1233         if (csp->csp_auth_mlen == 0)
1234                 swa->sw_mlen = axf->hashsize;
1235         else
1236                 swa->sw_mlen = csp->csp_auth_mlen;
1237         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1238         if (swa->sw_ictx == NULL)
1239                 return (ENOBUFS);
1240
1241         switch (csp->csp_auth_alg) {
1242         case CRYPTO_SHA1_HMAC:
1243         case CRYPTO_SHA2_224_HMAC:
1244         case CRYPTO_SHA2_256_HMAC:
1245         case CRYPTO_SHA2_384_HMAC:
1246         case CRYPTO_SHA2_512_HMAC:
1247         case CRYPTO_NULL_HMAC:
1248         case CRYPTO_RIPEMD160_HMAC:
1249                 swa->sw_octx = malloc(axf->ctxsize, M_CRYPTO_DATA,
1250                     M_NOWAIT);
1251                 if (swa->sw_octx == NULL)
1252                         return (ENOBUFS);
1253
1254                 if (csp->csp_auth_key != NULL) {
1255                         swcr_authprepare(axf, swa, csp->csp_auth_key,
1256                             csp->csp_auth_klen);
1257                 }
1258
1259                 if (csp->csp_mode == CSP_MODE_DIGEST)
1260                         ses->swcr_process = swcr_authcompute;
1261                 break;
1262         case CRYPTO_SHA1:
1263         case CRYPTO_SHA2_224:
1264         case CRYPTO_SHA2_256:
1265         case CRYPTO_SHA2_384:
1266         case CRYPTO_SHA2_512:
1267                 axf->Init(swa->sw_ictx);
1268                 if (csp->csp_mode == CSP_MODE_DIGEST)
1269                         ses->swcr_process = swcr_authcompute;
1270                 break;
1271         case CRYPTO_AES_NIST_GMAC:
1272                 axf->Init(swa->sw_ictx);
1273                 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1274                     csp->csp_auth_klen);
1275                 if (csp->csp_mode == CSP_MODE_DIGEST)
1276                         ses->swcr_process = swcr_gmac;
1277                 break;
1278         case CRYPTO_POLY1305:
1279         case CRYPTO_BLAKE2B:
1280         case CRYPTO_BLAKE2S:
1281                 /*
1282                  * Blake2b and Blake2s support an optional key but do
1283                  * not require one.
1284                  */
1285                 if (csp->csp_auth_klen == 0 || csp->csp_auth_key != NULL)
1286                         axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1287                             csp->csp_auth_klen);
1288                 axf->Init(swa->sw_ictx);
1289                 if (csp->csp_mode == CSP_MODE_DIGEST)
1290                         ses->swcr_process = swcr_authcompute;
1291                 break;
1292         case CRYPTO_AES_CCM_CBC_MAC:
1293                 axf->Init(swa->sw_ictx);
1294                 axf->Setkey(swa->sw_ictx, csp->csp_auth_key,
1295                     csp->csp_auth_klen);
1296                 if (csp->csp_mode == CSP_MODE_DIGEST)
1297                         ses->swcr_process = swcr_ccm_cbc_mac;
1298                 break;
1299         }
1300
1301         return (0);
1302 }
1303
1304 static int
1305 swcr_setup_gcm(struct swcr_session *ses,
1306     const struct crypto_session_params *csp)
1307 {
1308         struct swcr_auth *swa;
1309         const struct auth_hash *axf;
1310
1311         if (csp->csp_ivlen != AES_GCM_IV_LEN)
1312                 return (EINVAL);
1313
1314         /* First, setup the auth side. */
1315         swa = &ses->swcr_auth;
1316         switch (csp->csp_cipher_klen * 8) {
1317         case 128:
1318                 axf = &auth_hash_nist_gmac_aes_128;
1319                 break;
1320         case 192:
1321                 axf = &auth_hash_nist_gmac_aes_192;
1322                 break;
1323         case 256:
1324                 axf = &auth_hash_nist_gmac_aes_256;
1325                 break;
1326         default:
1327                 return (EINVAL);
1328         }
1329         swa->sw_axf = axf;
1330         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1331                 return (EINVAL);
1332         if (csp->csp_auth_mlen == 0)
1333                 swa->sw_mlen = axf->hashsize;
1334         else
1335                 swa->sw_mlen = csp->csp_auth_mlen;
1336         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1337         if (swa->sw_ictx == NULL)
1338                 return (ENOBUFS);
1339         axf->Init(swa->sw_ictx);
1340         if (csp->csp_cipher_key != NULL)
1341                 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1342                     csp->csp_cipher_klen);
1343
1344         /* Second, setup the cipher side. */
1345         return (swcr_setup_cipher(ses, csp));
1346 }
1347
1348 static int
1349 swcr_setup_ccm(struct swcr_session *ses,
1350     const struct crypto_session_params *csp)
1351 {
1352         struct swcr_auth *swa;
1353         const struct auth_hash *axf;
1354
1355         /* First, setup the auth side. */
1356         swa = &ses->swcr_auth;
1357         switch (csp->csp_cipher_klen * 8) {
1358         case 128:
1359                 axf = &auth_hash_ccm_cbc_mac_128;
1360                 break;
1361         case 192:
1362                 axf = &auth_hash_ccm_cbc_mac_192;
1363                 break;
1364         case 256:
1365                 axf = &auth_hash_ccm_cbc_mac_256;
1366                 break;
1367         default:
1368                 return (EINVAL);
1369         }
1370         swa->sw_axf = axf;
1371         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1372                 return (EINVAL);
1373         if (csp->csp_auth_mlen == 0)
1374                 swa->sw_mlen = axf->hashsize;
1375         else
1376                 swa->sw_mlen = csp->csp_auth_mlen;
1377         swa->sw_ictx = malloc(axf->ctxsize, M_CRYPTO_DATA, M_NOWAIT);
1378         if (swa->sw_ictx == NULL)
1379                 return (ENOBUFS);
1380         axf->Init(swa->sw_ictx);
1381         if (csp->csp_cipher_key != NULL)
1382                 axf->Setkey(swa->sw_ictx, csp->csp_cipher_key,
1383                     csp->csp_cipher_klen);
1384
1385         /* Second, setup the cipher side. */
1386         return (swcr_setup_cipher(ses, csp));
1387 }
1388
1389 static int
1390 swcr_setup_chacha20_poly1305(struct swcr_session *ses,
1391     const struct crypto_session_params *csp)
1392 {
1393         struct swcr_auth *swa;
1394         const struct auth_hash *axf;
1395
1396         if (csp->csp_ivlen != CHACHA20_POLY1305_IV_LEN)
1397                 return (EINVAL);
1398
1399         /* First, setup the auth side. */
1400         swa = &ses->swcr_auth;
1401         axf = &auth_hash_chacha20_poly1305;
1402         swa->sw_axf = axf;
1403         if (csp->csp_auth_mlen < 0 || csp->csp_auth_mlen > axf->hashsize)
1404                 return (EINVAL);
1405         if (csp->csp_auth_mlen == 0)
1406                 swa->sw_mlen = axf->hashsize;
1407         else
1408                 swa->sw_mlen = csp->csp_auth_mlen;
1409
1410         /* The auth state is regenerated for each nonce. */
1411
1412         /* Second, setup the cipher side. */
1413         return (swcr_setup_cipher(ses, csp));
1414 }
1415
1416 static bool
1417 swcr_auth_supported(const struct crypto_session_params *csp)
1418 {
1419         const struct auth_hash *axf;
1420
1421         axf = crypto_auth_hash(csp);
1422         if (axf == NULL)
1423                 return (false);
1424         switch (csp->csp_auth_alg) {
1425         case CRYPTO_SHA1_HMAC:
1426         case CRYPTO_SHA2_224_HMAC:
1427         case CRYPTO_SHA2_256_HMAC:
1428         case CRYPTO_SHA2_384_HMAC:
1429         case CRYPTO_SHA2_512_HMAC:
1430         case CRYPTO_NULL_HMAC:
1431         case CRYPTO_RIPEMD160_HMAC:
1432                 break;
1433         case CRYPTO_AES_NIST_GMAC:
1434                 switch (csp->csp_auth_klen * 8) {
1435                 case 128:
1436                 case 192:
1437                 case 256:
1438                         break;
1439                 default:
1440                         return (false);
1441                 }
1442                 if (csp->csp_auth_key == NULL)
1443                         return (false);
1444                 if (csp->csp_ivlen != AES_GCM_IV_LEN)
1445                         return (false);
1446                 break;
1447         case CRYPTO_POLY1305:
1448                 if (csp->csp_auth_klen != POLY1305_KEY_LEN)
1449                         return (false);
1450                 break;
1451         case CRYPTO_AES_CCM_CBC_MAC:
1452                 switch (csp->csp_auth_klen * 8) {
1453                 case 128:
1454                 case 192:
1455                 case 256:
1456                         break;
1457                 default:
1458                         return (false);
1459                 }
1460                 if (csp->csp_auth_key == NULL)
1461                         return (false);
1462                 break;
1463         }
1464         return (true);
1465 }
1466
1467 static bool
1468 swcr_cipher_supported(const struct crypto_session_params *csp)
1469 {
1470         const struct enc_xform *txf;
1471
1472         txf = crypto_cipher(csp);
1473         if (txf == NULL)
1474                 return (false);
1475         if (csp->csp_cipher_alg != CRYPTO_NULL_CBC &&
1476             txf->ivsize != csp->csp_ivlen)
1477                 return (false);
1478         return (true);
1479 }
1480
1481 #define SUPPORTED_SES (CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD | CSP_F_ESN)
1482
1483 static int
1484 swcr_probesession(device_t dev, const struct crypto_session_params *csp)
1485 {
1486         if ((csp->csp_flags & ~(SUPPORTED_SES)) != 0)
1487                 return (EINVAL);
1488         switch (csp->csp_mode) {
1489         case CSP_MODE_COMPRESS:
1490                 switch (csp->csp_cipher_alg) {
1491                 case CRYPTO_DEFLATE_COMP:
1492                         break;
1493                 default:
1494                         return (EINVAL);
1495                 }
1496                 break;
1497         case CSP_MODE_CIPHER:
1498                 switch (csp->csp_cipher_alg) {
1499                 case CRYPTO_AES_NIST_GCM_16:
1500                 case CRYPTO_AES_CCM_16:
1501                 case CRYPTO_CHACHA20_POLY1305:
1502                         return (EINVAL);
1503                 default:
1504                         if (!swcr_cipher_supported(csp))
1505                                 return (EINVAL);
1506                         break;
1507                 }
1508                 break;
1509         case CSP_MODE_DIGEST:
1510                 if (!swcr_auth_supported(csp))
1511                         return (EINVAL);
1512                 break;
1513         case CSP_MODE_AEAD:
1514                 switch (csp->csp_cipher_alg) {
1515                 case CRYPTO_AES_NIST_GCM_16:
1516                 case CRYPTO_AES_CCM_16:
1517                 case CRYPTO_CHACHA20_POLY1305:
1518                         break;
1519                 default:
1520                         return (EINVAL);
1521                 }
1522                 break;
1523         case CSP_MODE_ETA:
1524                 /* AEAD algorithms cannot be used for EtA. */
1525                 switch (csp->csp_cipher_alg) {
1526                 case CRYPTO_AES_NIST_GCM_16:
1527                 case CRYPTO_AES_CCM_16:
1528                 case CRYPTO_CHACHA20_POLY1305:
1529                         return (EINVAL);
1530                 }
1531                 switch (csp->csp_auth_alg) {
1532                 case CRYPTO_AES_NIST_GMAC:
1533                 case CRYPTO_AES_CCM_CBC_MAC:
1534                         return (EINVAL);
1535                 }
1536
1537                 if (!swcr_cipher_supported(csp) ||
1538                     !swcr_auth_supported(csp))
1539                         return (EINVAL);
1540                 break;
1541         default:
1542                 return (EINVAL);
1543         }
1544
1545         return (CRYPTODEV_PROBE_SOFTWARE);
1546 }
1547
1548 /*
1549  * Generate a new software session.
1550  */
1551 static int
1552 swcr_newsession(device_t dev, crypto_session_t cses,
1553     const struct crypto_session_params *csp)
1554 {
1555         struct swcr_session *ses;
1556         struct swcr_encdec *swe;
1557         struct swcr_auth *swa;
1558         const struct comp_algo *cxf;
1559         int error;
1560
1561         ses = crypto_get_driver_session(cses);
1562         mtx_init(&ses->swcr_lock, "swcr session lock", NULL, MTX_DEF);
1563
1564         error = 0;
1565         swe = &ses->swcr_encdec;
1566         swa = &ses->swcr_auth;
1567         switch (csp->csp_mode) {
1568         case CSP_MODE_COMPRESS:
1569                 switch (csp->csp_cipher_alg) {
1570                 case CRYPTO_DEFLATE_COMP:
1571                         cxf = &comp_algo_deflate;
1572                         break;
1573 #ifdef INVARIANTS
1574                 default:
1575                         panic("bad compression algo");
1576 #endif
1577                 }
1578                 ses->swcr_compdec.sw_cxf = cxf;
1579                 ses->swcr_process = swcr_compdec;
1580                 break;
1581         case CSP_MODE_CIPHER:
1582                 switch (csp->csp_cipher_alg) {
1583                 case CRYPTO_NULL_CBC:
1584                         ses->swcr_process = swcr_null;
1585                         break;
1586 #ifdef INVARIANTS
1587                 case CRYPTO_AES_NIST_GCM_16:
1588                 case CRYPTO_AES_CCM_16:
1589                 case CRYPTO_CHACHA20_POLY1305:
1590                         panic("bad cipher algo");
1591 #endif
1592                 default:
1593                         error = swcr_setup_cipher(ses, csp);
1594                         if (error == 0)
1595                                 ses->swcr_process = swcr_encdec;
1596                 }
1597                 break;
1598         case CSP_MODE_DIGEST:
1599                 error = swcr_setup_auth(ses, csp);
1600                 break;
1601         case CSP_MODE_AEAD:
1602                 switch (csp->csp_cipher_alg) {
1603                 case CRYPTO_AES_NIST_GCM_16:
1604                         error = swcr_setup_gcm(ses, csp);
1605                         if (error == 0)
1606                                 ses->swcr_process = swcr_gcm;
1607                         break;
1608                 case CRYPTO_AES_CCM_16:
1609                         error = swcr_setup_ccm(ses, csp);
1610                         if (error == 0)
1611                                 ses->swcr_process = swcr_ccm;
1612                         break;
1613                 case CRYPTO_CHACHA20_POLY1305:
1614                         error = swcr_setup_chacha20_poly1305(ses, csp);
1615                         if (error == 0)
1616                                 ses->swcr_process = swcr_chacha20_poly1305;
1617                         break;
1618 #ifdef INVARIANTS
1619                 default:
1620                         panic("bad aead algo");
1621 #endif
1622                 }
1623                 break;
1624         case CSP_MODE_ETA:
1625 #ifdef INVARIANTS
1626                 switch (csp->csp_cipher_alg) {
1627                 case CRYPTO_AES_NIST_GCM_16:
1628                 case CRYPTO_AES_CCM_16:
1629                 case CRYPTO_CHACHA20_POLY1305:
1630                         panic("bad eta cipher algo");
1631                 }
1632                 switch (csp->csp_auth_alg) {
1633                 case CRYPTO_AES_NIST_GMAC:
1634                 case CRYPTO_AES_CCM_CBC_MAC:
1635                         panic("bad eta auth algo");
1636                 }
1637 #endif
1638
1639                 error = swcr_setup_auth(ses, csp);
1640                 if (error)
1641                         break;
1642                 if (csp->csp_cipher_alg == CRYPTO_NULL_CBC) {
1643                         /* Effectively degrade to digest mode. */
1644                         ses->swcr_process = swcr_authcompute;
1645                         break;
1646                 }
1647
1648                 error = swcr_setup_cipher(ses, csp);
1649                 if (error == 0)
1650                         ses->swcr_process = swcr_eta;
1651                 break;
1652         default:
1653                 error = EINVAL;
1654         }
1655
1656         if (error)
1657                 swcr_freesession(dev, cses);
1658         return (error);
1659 }
1660
1661 static void
1662 swcr_freesession(device_t dev, crypto_session_t cses)
1663 {
1664         struct swcr_session *ses;
1665
1666         ses = crypto_get_driver_session(cses);
1667
1668         mtx_destroy(&ses->swcr_lock);
1669
1670         zfree(ses->swcr_encdec.sw_kschedule, M_CRYPTO_DATA);
1671         zfree(ses->swcr_auth.sw_ictx, M_CRYPTO_DATA);
1672         zfree(ses->swcr_auth.sw_octx, M_CRYPTO_DATA);
1673 }
1674
1675 /*
1676  * Process a software request.
1677  */
1678 static int
1679 swcr_process(device_t dev, struct cryptop *crp, int hint)
1680 {
1681         struct swcr_session *ses;
1682
1683         ses = crypto_get_driver_session(crp->crp_session);
1684         mtx_lock(&ses->swcr_lock);
1685
1686         crp->crp_etype = ses->swcr_process(ses, crp);
1687
1688         mtx_unlock(&ses->swcr_lock);
1689         crypto_done(crp);
1690         return (0);
1691 }
1692
1693 static void
1694 swcr_identify(driver_t *drv, device_t parent)
1695 {
1696         /* NB: order 10 is so we get attached after h/w devices */
1697         if (device_find_child(parent, "cryptosoft", -1) == NULL &&
1698             BUS_ADD_CHILD(parent, 10, "cryptosoft", 0) == 0)
1699                 panic("cryptosoft: could not attach");
1700 }
1701
1702 static int
1703 swcr_probe(device_t dev)
1704 {
1705         device_set_desc(dev, "software crypto");
1706         device_quiet(dev);
1707         return (BUS_PROBE_NOWILDCARD);
1708 }
1709
1710 static int
1711 swcr_attach(device_t dev)
1712 {
1713
1714         swcr_id = crypto_get_driverid(dev, sizeof(struct swcr_session),
1715                         CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC);
1716         if (swcr_id < 0) {
1717                 device_printf(dev, "cannot initialize!");
1718                 return (ENXIO);
1719         }
1720
1721         return (0);
1722 }
1723
1724 static int
1725 swcr_detach(device_t dev)
1726 {
1727         crypto_unregister_all(swcr_id);
1728         return 0;
1729 }
1730
1731 static device_method_t swcr_methods[] = {
1732         DEVMETHOD(device_identify,      swcr_identify),
1733         DEVMETHOD(device_probe,         swcr_probe),
1734         DEVMETHOD(device_attach,        swcr_attach),
1735         DEVMETHOD(device_detach,        swcr_detach),
1736
1737         DEVMETHOD(cryptodev_probesession, swcr_probesession),
1738         DEVMETHOD(cryptodev_newsession, swcr_newsession),
1739         DEVMETHOD(cryptodev_freesession,swcr_freesession),
1740         DEVMETHOD(cryptodev_process,    swcr_process),
1741
1742         {0, 0},
1743 };
1744
1745 static driver_t swcr_driver = {
1746         "cryptosoft",
1747         swcr_methods,
1748         0,              /* NB: no softc */
1749 };
1750 static devclass_t swcr_devclass;
1751
1752 /*
1753  * NB: We explicitly reference the crypto module so we
1754  * get the necessary ordering when built as a loadable
1755  * module.  This is required because we bundle the crypto
1756  * module code together with the cryptosoft driver (otherwise
1757  * normal module dependencies would handle things).
1758  */
1759 extern int crypto_modevent(struct module *, int, void *);
1760 /* XXX where to attach */
1761 DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,0);
1762 MODULE_VERSION(cryptosoft, 1);
1763 MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);