1 /* $NetBSD: cryptosoft.c,v 1.24 2009/03/25 01:26:13 darran Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $ */
3 /* $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
6 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
8 * This code was written by Angelos D. Keromytis in Athens, Greece, in
9 * February 2000. Network Security Technologies Inc. (NSTI) kindly
10 * supported the development of this code.
12 * Copyright (c) 2000, 2001 Angelos D. Keromytis
14 * Permission to use, copy, and modify this software with or without fee
15 * is hereby granted, provided that this entire notice is included in
16 * all source code copies of any software which is or includes a copy or
17 * modification of this software.
19 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
21 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
22 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.24 2009/03/25 01:26:13 darran Exp $");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
33 #include <sys/sysctl.h>
34 #include <sys/errno.h>
37 #include <opencrypto/cryptodev.h>
38 #include <opencrypto/cryptosoft.h>
39 #include <opencrypto/xform.h>
41 #include <opencrypto/cryptosoft_xform.c>
52 struct swcr_data
**swcr_sessions
= NULL
;
53 u_int32_t swcr_sesnum
= 0;
56 #define COPYBACK(x, a, b, c, d) \
57 (x) == CRYPTO_BUF_MBUF ? m_copyback((struct mbuf *)a,b,c,d) \
58 : cuio_copyback((struct uio *)a,b,c,d)
59 #define COPYDATA(x, a, b, c, d) \
60 (x) == CRYPTO_BUF_MBUF ? m_copydata((struct mbuf *)a,b,c,d) \
61 : cuio_copydata((struct uio *)a,b,c,d)
63 static int swcr_encdec(struct cryptodesc
*, struct swcr_data
*, void *, int);
64 static int swcr_compdec(struct cryptodesc
*, struct swcr_data
*, void *, int);
65 static int swcr_process(void *, struct cryptop
*, int);
66 static int swcr_newsession(void *, u_int32_t
*, struct cryptoini
*);
67 static int swcr_freesession(void *, u_int64_t
);
70 * Apply a symmetric encryption/decryption algorithm.
73 swcr_encdec(struct cryptodesc
*crd
, struct swcr_data
*sw
, void *bufv
,
77 unsigned char iv
[EALG_MAX_BLOCK_LEN
], blk
[EALG_MAX_BLOCK_LEN
], *idat
;
78 unsigned char *ivp
, piv
[EALG_MAX_BLOCK_LEN
];
79 const struct swcr_enc_xform
*exf
;
84 blks
= exf
->enc_xform
->blocksize
;
86 /* Check for non-padded data */
87 if (crd
->crd_len
% blks
)
90 /* Initialize the IV */
91 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
92 /* IV explicitly provided ? */
93 if (crd
->crd_flags
& CRD_F_IV_EXPLICIT
)
94 memcpy(iv
, crd
->crd_iv
, blks
);
98 i
+ sizeof (u_int32_t
) < EALG_MAX_BLOCK_LEN
;
99 i
+= sizeof (u_int32_t
)) {
100 u_int32_t temp
= arc4random();
102 memcpy(iv
+ i
, &temp
, sizeof(u_int32_t
));
105 * What if the block size is not a multiple
106 * of sizeof (u_int32_t), which is the size of
107 * what arc4random() returns ?
109 if (EALG_MAX_BLOCK_LEN
% sizeof (u_int32_t
) != 0) {
110 u_int32_t temp
= arc4random();
112 bcopy (&temp
, iv
+ i
,
113 EALG_MAX_BLOCK_LEN
- i
);
117 /* Do we need to write the IV */
118 if (!(crd
->crd_flags
& CRD_F_IV_PRESENT
)) {
119 COPYBACK(outtype
, buf
, crd
->crd_inject
, blks
, iv
);
122 } else { /* Decryption */
123 /* IV explicitly provided ? */
124 if (crd
->crd_flags
& CRD_F_IV_EXPLICIT
)
125 memcpy(iv
, crd
->crd_iv
, blks
);
128 COPYDATA(outtype
, buf
, crd
->crd_inject
, blks
, iv
);
134 if (outtype
== CRYPTO_BUF_CONTIG
) {
135 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
136 for (i
= crd
->crd_skip
;
137 i
< crd
->crd_skip
+ crd
->crd_len
; i
+= blks
) {
138 /* XOR with the IV/previous block, as appropriate. */
139 if (i
== crd
->crd_skip
)
140 for (k
= 0; k
< blks
; k
++)
141 buf
[i
+ k
] ^= ivp
[k
];
143 for (k
= 0; k
< blks
; k
++)
144 buf
[i
+ k
] ^= buf
[i
+ k
- blks
];
145 exf
->encrypt(sw
->sw_kschedule
, buf
+ i
);
147 } else { /* Decrypt */
149 * Start at the end, so we don't need to keep the encrypted
150 * block as the IV for the next block.
152 for (i
= crd
->crd_skip
+ crd
->crd_len
- blks
;
153 i
>= crd
->crd_skip
; i
-= blks
) {
154 exf
->decrypt(sw
->sw_kschedule
, buf
+ i
);
156 /* XOR with the IV/previous block, as appropriate */
157 if (i
== crd
->crd_skip
)
158 for (k
= 0; k
< blks
; k
++)
159 buf
[i
+ k
] ^= ivp
[k
];
161 for (k
= 0; k
< blks
; k
++)
162 buf
[i
+ k
] ^= buf
[i
+ k
- blks
];
167 } else if (outtype
== CRYPTO_BUF_MBUF
) {
168 struct mbuf
*m
= (struct mbuf
*) buf
;
170 /* Find beginning of data */
171 m
= m_getptr(m
, crd
->crd_skip
, &k
);
179 * If there's insufficient data at the end of
180 * an mbuf, we have to do some copying.
182 if (m
->m_len
< k
+ blks
&& m
->m_len
!= k
) {
183 m_copydata(m
, k
, blks
, blk
);
185 /* Actual encryption/decryption */
186 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
187 /* XOR with previous block */
188 for (j
= 0; j
< blks
; j
++)
191 exf
->encrypt(sw
->sw_kschedule
, blk
);
194 * Keep encrypted block for XOR'ing
197 memcpy(iv
, blk
, blks
);
199 } else { /* decrypt */
201 * Keep encrypted block for XOR'ing
205 memcpy(piv
, blk
, blks
);
207 memcpy(iv
, blk
, blks
);
209 exf
->decrypt(sw
->sw_kschedule
, blk
);
211 /* XOR with previous block */
212 for (j
= 0; j
< blks
; j
++)
216 memcpy(iv
, piv
, blks
);
221 /* Copy back decrypted block */
222 m_copyback(m
, k
, blks
, blk
);
224 /* Advance pointer */
225 m
= m_getptr(m
, k
+ blks
, &k
);
231 /* Could be done... */
236 /* Skip possibly empty mbufs */
238 for (m
= m
->m_next
; m
&& m
->m_len
== 0;
249 * Warning: idat may point to garbage here, but
250 * we only use it in the while() loop, only if
251 * there are indeed enough data.
253 idat
= mtod(m
, unsigned char *) + k
;
255 while (m
->m_len
>= k
+ blks
&& i
> 0) {
256 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
257 /* XOR with previous block/IV */
258 for (j
= 0; j
< blks
; j
++)
261 exf
->encrypt(sw
->sw_kschedule
, idat
);
263 } else { /* decrypt */
265 * Keep encrypted block to be used
266 * in next block's processing.
269 memcpy(piv
, idat
, blks
);
271 memcpy(iv
, idat
, blks
);
273 exf
->decrypt(sw
->sw_kschedule
, idat
);
275 /* XOR with previous block/IV */
276 for (j
= 0; j
< blks
; j
++)
280 memcpy(iv
, piv
, blks
);
291 return 0; /* Done with mbuf encryption/decryption */
292 } else if (outtype
== CRYPTO_BUF_IOV
) {
293 struct uio
*uio
= (struct uio
*) buf
;
295 /* Find beginning of data */
296 count
= crd
->crd_skip
;
297 ind
= cuio_getptr(uio
, count
, &k
);
305 * If there's insufficient data at the end,
306 * we have to do some copying.
308 if (uio
->uio_iov
[ind
].iov_len
< k
+ blks
&&
309 uio
->uio_iov
[ind
].iov_len
!= k
) {
310 cuio_copydata(uio
, k
, blks
, blk
);
312 /* Actual encryption/decryption */
313 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
314 /* XOR with previous block */
315 for (j
= 0; j
< blks
; j
++)
318 exf
->encrypt(sw
->sw_kschedule
, blk
);
321 * Keep encrypted block for XOR'ing
324 memcpy(iv
, blk
, blks
);
326 } else { /* decrypt */
328 * Keep encrypted block for XOR'ing
332 memcpy(piv
, blk
, blks
);
334 memcpy(iv
, blk
, blks
);
336 exf
->decrypt(sw
->sw_kschedule
, blk
);
338 /* XOR with previous block */
339 for (j
= 0; j
< blks
; j
++)
343 memcpy(iv
, piv
, blks
);
348 /* Copy back decrypted block */
349 cuio_copyback(uio
, k
, blks
, blk
);
353 /* Advance pointer */
354 ind
= cuio_getptr(uio
, count
, &k
);
360 /* Could be done... */
366 * Warning: idat may point to garbage here, but
367 * we only use it in the while() loop, only if
368 * there are indeed enough data.
370 idat
= ((char *)uio
->uio_iov
[ind
].iov_base
) + k
;
372 while (uio
->uio_iov
[ind
].iov_len
>= k
+ blks
&&
374 if (crd
->crd_flags
& CRD_F_ENCRYPT
) {
375 /* XOR with previous block/IV */
376 for (j
= 0; j
< blks
; j
++)
379 exf
->encrypt(sw
->sw_kschedule
, idat
);
381 } else { /* decrypt */
383 * Keep encrypted block to be used
384 * in next block's processing.
387 memcpy(piv
, idat
, blks
);
389 memcpy(iv
, idat
, blks
);
391 exf
->decrypt(sw
->sw_kschedule
, idat
);
393 /* XOR with previous block/IV */
394 for (j
= 0; j
< blks
; j
++)
398 memcpy(iv
, piv
, blks
);
409 return 0; /* Done with mbuf encryption/decryption */
417 * Compute keyed-hash authenticator.
420 swcr_authcompute(struct cryptop
*crp
, struct cryptodesc
*crd
,
421 struct swcr_data
*sw
, void *buf
, int outtype
)
423 unsigned char aalg
[AALG_MAX_RESULT_LEN
];
424 const struct swcr_auth_hash
*axf
;
428 if (sw
->sw_ictx
== 0)
433 memcpy(&ctx
, sw
->sw_ictx
, axf
->auth_hash
->ctxsize
);
436 case CRYPTO_BUF_CONTIG
:
437 axf
->Update(&ctx
, (char *)buf
+ crd
->crd_skip
, crd
->crd_len
);
439 case CRYPTO_BUF_MBUF
:
440 err
= m_apply((struct mbuf
*) buf
, crd
->crd_skip
, crd
->crd_len
,
441 (int (*)(void*, void *, unsigned int)) axf
->Update
,
447 err
= cuio_apply((struct uio
*) buf
, crd
->crd_skip
,
449 (int (*)(void *, void *, unsigned int)) axf
->Update
,
459 switch (sw
->sw_alg
) {
460 case CRYPTO_MD5_HMAC
:
461 case CRYPTO_MD5_HMAC_96
:
462 case CRYPTO_SHA1_HMAC
:
463 case CRYPTO_SHA1_HMAC_96
:
464 case CRYPTO_SHA2_HMAC
:
465 case CRYPTO_RIPEMD160_HMAC
:
466 case CRYPTO_RIPEMD160_HMAC_96
:
467 if (sw
->sw_octx
== NULL
)
470 axf
->Final(aalg
, &ctx
);
471 memcpy(&ctx
, sw
->sw_octx
, axf
->auth_hash
->ctxsize
);
472 axf
->Update(&ctx
, aalg
, axf
->auth_hash
->hashsize
);
473 axf
->Final(aalg
, &ctx
);
476 case CRYPTO_MD5_KPDK
:
477 case CRYPTO_SHA1_KPDK
:
478 if (sw
->sw_octx
== NULL
)
481 axf
->Update(&ctx
, sw
->sw_octx
, sw
->sw_klen
);
482 axf
->Final(aalg
, &ctx
);
485 case CRYPTO_NULL_HMAC
:
488 axf
->Final(aalg
, &ctx
);
492 /* Inject the authentication data */
494 case CRYPTO_BUF_CONTIG
:
495 (void)memcpy((char *)buf
+ crd
->crd_inject
, aalg
,
496 axf
->auth_hash
->authsize
);
498 case CRYPTO_BUF_MBUF
:
499 m_copyback((struct mbuf
*) buf
, crd
->crd_inject
,
500 axf
->auth_hash
->authsize
, aalg
);
503 memcpy(crp
->crp_mac
, aalg
, axf
->auth_hash
->authsize
);
512 * Apply a compression/decompression algorithm
515 swcr_compdec(struct cryptodesc
*crd
, struct swcr_data
*sw
,
516 void *buf
, int outtype
)
518 u_int8_t
*data
, *out
;
519 const struct swcr_comp_algo
*cxf
;
525 /* We must handle the whole buffer of data in one time
526 * then if there is not all the data in the mbuf, we must
530 data
= malloc(crd
->crd_len
, M_CRYPTO_DATA
, M_NOWAIT
);
533 COPYDATA(outtype
, buf
, crd
->crd_skip
, crd
->crd_len
, data
);
535 if (crd
->crd_flags
& CRD_F_COMP
)
536 result
= cxf
->compress(data
, crd
->crd_len
, &out
);
538 result
= cxf
->decompress(data
, crd
->crd_len
, &out
);
540 free(data
, M_CRYPTO_DATA
);
544 /* Copy back the (de)compressed data. m_copyback is
545 * extending the mbuf as necessary.
547 sw
->sw_size
= result
;
548 /* Check the compressed size when doing compression */
549 if (crd
->crd_flags
& CRD_F_COMP
) {
550 if (result
> crd
->crd_len
) {
551 /* Compression was useless, we lost time */
552 free(out
, M_CRYPTO_DATA
);
557 COPYBACK(outtype
, buf
, crd
->crd_skip
, result
, out
);
558 if (result
< crd
->crd_len
) {
559 adj
= result
- crd
->crd_len
;
560 if (outtype
== CRYPTO_BUF_MBUF
) {
561 adj
= result
- crd
->crd_len
;
562 m_adj((struct mbuf
*)buf
, adj
);
564 /* Don't adjust the iov_len, it breaks the kmem_free */
566 free(out
, M_CRYPTO_DATA
);
571 * Generate a new software session.
574 swcr_newsession(void *arg
, u_int32_t
*sid
, struct cryptoini
*cri
)
576 struct swcr_data
**swd
;
577 const struct swcr_auth_hash
*axf
;
578 const struct swcr_enc_xform
*txf
;
579 const struct swcr_comp_algo
*cxf
;
583 if (sid
== NULL
|| cri
== NULL
)
587 for (i
= 1; i
< swcr_sesnum
; i
++)
588 if (swcr_sessions
[i
] == NULL
)
591 i
= 1; /* NB: to silence compiler warning */
593 if (swcr_sessions
== NULL
|| i
== swcr_sesnum
) {
594 if (swcr_sessions
== NULL
) {
595 i
= 1; /* We leave swcr_sessions[0] empty */
596 swcr_sesnum
= CRYPTO_SW_SESSIONS
;
600 swd
= malloc(swcr_sesnum
* sizeof(struct swcr_data
*),
601 M_CRYPTO_DATA
, M_NOWAIT
);
603 /* Reset session number */
604 if (swcr_sesnum
== CRYPTO_SW_SESSIONS
)
611 memset(swd
, 0, swcr_sesnum
* sizeof(struct swcr_data
*));
613 /* Copy existing sessions */
615 memcpy(swd
, swcr_sessions
,
616 (swcr_sesnum
/ 2) * sizeof(struct swcr_data
*));
617 free(swcr_sessions
, M_CRYPTO_DATA
);
623 swd
= &swcr_sessions
[i
];
627 *swd
= malloc(sizeof **swd
, M_CRYPTO_DATA
, M_NOWAIT
);
629 swcr_freesession(NULL
, i
);
632 memset(*swd
, 0, sizeof(struct swcr_data
));
634 switch (cri
->cri_alg
) {
636 txf
= &swcr_enc_xform_des
;
638 case CRYPTO_3DES_CBC
:
639 txf
= &swcr_enc_xform_3des
;
642 txf
= &swcr_enc_xform_blf
;
644 case CRYPTO_CAST_CBC
:
645 txf
= &swcr_enc_xform_cast5
;
647 case CRYPTO_SKIPJACK_CBC
:
648 txf
= &swcr_enc_xform_skipjack
;
650 case CRYPTO_RIJNDAEL128_CBC
:
651 txf
= &swcr_enc_xform_rijndael128
;
653 case CRYPTO_NULL_CBC
:
654 txf
= &swcr_enc_xform_null
;
657 error
= txf
->setkey(&((*swd
)->sw_kschedule
),
658 cri
->cri_key
, cri
->cri_klen
/ 8);
660 swcr_freesession(NULL
, i
);
663 (*swd
)->sw_exf
= txf
;
666 case CRYPTO_MD5_HMAC
:
667 axf
= &swcr_auth_hash_hmac_md5
;
669 case CRYPTO_MD5_HMAC_96
:
670 axf
= &swcr_auth_hash_hmac_md5_96
;
672 case CRYPTO_SHA1_HMAC
:
673 axf
= &swcr_auth_hash_hmac_sha1
;
675 case CRYPTO_SHA1_HMAC_96
:
676 axf
= &swcr_auth_hash_hmac_sha1_96
;
678 case CRYPTO_SHA2_HMAC
:
679 if (cri
->cri_klen
== 256)
680 axf
= &swcr_auth_hash_hmac_sha2_256
;
681 else if (cri
->cri_klen
== 384)
682 axf
= &swcr_auth_hash_hmac_sha2_384
;
683 else if (cri
->cri_klen
== 512)
684 axf
= &swcr_auth_hash_hmac_sha2_512
;
686 swcr_freesession(NULL
, i
);
690 case CRYPTO_NULL_HMAC
:
691 axf
= &swcr_auth_hash_null
;
693 case CRYPTO_RIPEMD160_HMAC
:
694 axf
= &swcr_auth_hash_hmac_ripemd_160
;
696 case CRYPTO_RIPEMD160_HMAC_96
:
697 axf
= &swcr_auth_hash_hmac_ripemd_160_96
;
698 goto authcommon
; /* leave this for safety */
700 (*swd
)->sw_ictx
= malloc(axf
->auth_hash
->ctxsize
,
701 M_CRYPTO_DATA
, M_NOWAIT
);
702 if ((*swd
)->sw_ictx
== NULL
) {
703 swcr_freesession(NULL
, i
);
707 (*swd
)->sw_octx
= malloc(axf
->auth_hash
->ctxsize
,
708 M_CRYPTO_DATA
, M_NOWAIT
);
709 if ((*swd
)->sw_octx
== NULL
) {
710 swcr_freesession(NULL
, i
);
714 for (k
= 0; k
< cri
->cri_klen
/ 8; k
++)
715 cri
->cri_key
[k
] ^= HMAC_IPAD_VAL
;
717 axf
->Init((*swd
)->sw_ictx
);
718 axf
->Update((*swd
)->sw_ictx
, cri
->cri_key
,
720 axf
->Update((*swd
)->sw_ictx
, hmac_ipad_buffer
,
721 HMAC_BLOCK_LEN
- (cri
->cri_klen
/ 8));
723 for (k
= 0; k
< cri
->cri_klen
/ 8; k
++)
724 cri
->cri_key
[k
] ^= (HMAC_IPAD_VAL
^ HMAC_OPAD_VAL
);
726 axf
->Init((*swd
)->sw_octx
);
727 axf
->Update((*swd
)->sw_octx
, cri
->cri_key
,
729 axf
->Update((*swd
)->sw_octx
, hmac_opad_buffer
,
730 HMAC_BLOCK_LEN
- (cri
->cri_klen
/ 8));
732 for (k
= 0; k
< cri
->cri_klen
/ 8; k
++)
733 cri
->cri_key
[k
] ^= HMAC_OPAD_VAL
;
734 (*swd
)->sw_axf
= axf
;
737 case CRYPTO_MD5_KPDK
:
738 axf
= &swcr_auth_hash_key_md5
;
741 case CRYPTO_SHA1_KPDK
:
742 axf
= &swcr_auth_hash_key_sha1
;
744 (*swd
)->sw_ictx
= malloc(axf
->auth_hash
->ctxsize
,
745 M_CRYPTO_DATA
, M_NOWAIT
);
746 if ((*swd
)->sw_ictx
== NULL
) {
747 swcr_freesession(NULL
, i
);
751 /* Store the key so we can "append" it to the payload */
752 (*swd
)->sw_octx
= malloc(cri
->cri_klen
/ 8, M_CRYPTO_DATA
,
754 if ((*swd
)->sw_octx
== NULL
) {
755 swcr_freesession(NULL
, i
);
759 (*swd
)->sw_klen
= cri
->cri_klen
/ 8;
760 memcpy((*swd
)->sw_octx
, cri
->cri_key
, cri
->cri_klen
/ 8);
761 axf
->Init((*swd
)->sw_ictx
);
762 axf
->Update((*swd
)->sw_ictx
, cri
->cri_key
,
764 axf
->Final(NULL
, (*swd
)->sw_ictx
);
765 (*swd
)->sw_axf
= axf
;
769 axf
= &swcr_auth_hash_md5
;
773 axf
= &swcr_auth_hash_sha1
;
775 (*swd
)->sw_ictx
= malloc(axf
->auth_hash
->ctxsize
,
776 M_CRYPTO_DATA
, M_NOWAIT
);
777 if ((*swd
)->sw_ictx
== NULL
) {
778 swcr_freesession(NULL
, i
);
782 axf
->Init((*swd
)->sw_ictx
);
783 (*swd
)->sw_axf
= axf
;
786 case CRYPTO_DEFLATE_COMP
:
787 cxf
= &swcr_comp_algo_deflate
;
788 (*swd
)->sw_cxf
= cxf
;
791 case CRYPTO_GZIP_COMP
:
792 cxf
= &swcr_comp_algo_gzip
;
793 (*swd
)->sw_cxf
= cxf
;
796 swcr_freesession(NULL
, i
);
800 (*swd
)->sw_alg
= cri
->cri_alg
;
802 swd
= &((*swd
)->sw_next
);
811 swcr_freesession(void *arg
, u_int64_t tid
)
813 struct swcr_data
*swd
;
814 const struct swcr_enc_xform
*txf
;
815 const struct swcr_auth_hash
*axf
;
816 const struct swcr_comp_algo
*cxf
;
817 u_int32_t sid
= ((u_int32_t
) tid
) & 0xffffffff;
819 if (sid
> swcr_sesnum
|| swcr_sessions
== NULL
||
820 swcr_sessions
[sid
] == NULL
)
823 /* Silently accept and return */
827 while ((swd
= swcr_sessions
[sid
]) != NULL
) {
828 swcr_sessions
[sid
] = swd
->sw_next
;
830 switch (swd
->sw_alg
) {
832 case CRYPTO_3DES_CBC
:
834 case CRYPTO_CAST_CBC
:
835 case CRYPTO_SKIPJACK_CBC
:
836 case CRYPTO_RIJNDAEL128_CBC
:
837 case CRYPTO_NULL_CBC
:
840 if (swd
->sw_kschedule
)
841 txf
->zerokey(&(swd
->sw_kschedule
));
844 case CRYPTO_MD5_HMAC
:
845 case CRYPTO_MD5_HMAC_96
:
846 case CRYPTO_SHA1_HMAC
:
847 case CRYPTO_SHA1_HMAC_96
:
848 case CRYPTO_SHA2_HMAC
:
849 case CRYPTO_RIPEMD160_HMAC
:
850 case CRYPTO_RIPEMD160_HMAC_96
:
851 case CRYPTO_NULL_HMAC
:
855 memset(swd
->sw_ictx
, 0, axf
->auth_hash
->ctxsize
);
856 free(swd
->sw_ictx
, M_CRYPTO_DATA
);
859 memset(swd
->sw_octx
, 0, axf
->auth_hash
->ctxsize
);
860 free(swd
->sw_octx
, M_CRYPTO_DATA
);
864 case CRYPTO_MD5_KPDK
:
865 case CRYPTO_SHA1_KPDK
:
869 memset(swd
->sw_ictx
, 0, axf
->auth_hash
->ctxsize
);
870 free(swd
->sw_ictx
, M_CRYPTO_DATA
);
873 memset(swd
->sw_octx
, 0, swd
->sw_klen
);
874 free(swd
->sw_octx
, M_CRYPTO_DATA
);
883 free(swd
->sw_ictx
, M_CRYPTO_DATA
);
886 case CRYPTO_DEFLATE_COMP
:
887 case CRYPTO_GZIP_COMP
:
892 free(swd
, M_CRYPTO_DATA
);
898 * Process a software request.
901 swcr_process(void *arg
, struct cryptop
*crp
, int hint
)
903 struct cryptodesc
*crd
;
904 struct swcr_data
*sw
;
912 if (crp
->crp_desc
== NULL
|| crp
->crp_buf
== NULL
) {
913 crp
->crp_etype
= EINVAL
;
917 lid
= crp
->crp_sid
& 0xffffffff;
918 if (lid
>= swcr_sesnum
|| lid
== 0 || swcr_sessions
[lid
] == NULL
) {
919 crp
->crp_etype
= ENOENT
;
923 if (crp
->crp_flags
& CRYPTO_F_IMBUF
) {
924 type
= CRYPTO_BUF_MBUF
;
925 } else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
926 type
= CRYPTO_BUF_IOV
;
928 type
= CRYPTO_BUF_CONTIG
;
931 /* Go through crypto descriptors, processing as we go */
932 for (crd
= crp
->crp_desc
; crd
; crd
= crd
->crd_next
) {
934 * Find the crypto context.
936 * XXX Note that the logic here prevents us from having
937 * XXX the same algorithm multiple times in a session
938 * XXX (or rather, we can but it won't give us the right
939 * XXX results). To do that, we'd need some way of differentiating
940 * XXX between the various instances of an algorithm (so we can
941 * XXX locate the correct crypto context).
943 for (sw
= swcr_sessions
[lid
];
944 sw
&& sw
->sw_alg
!= crd
->crd_alg
;
948 /* No such context ? */
950 crp
->crp_etype
= EINVAL
;
954 switch (sw
->sw_alg
) {
956 case CRYPTO_3DES_CBC
:
958 case CRYPTO_CAST_CBC
:
959 case CRYPTO_SKIPJACK_CBC
:
960 case CRYPTO_RIJNDAEL128_CBC
:
961 if ((crp
->crp_etype
= swcr_encdec(crd
, sw
,
962 crp
->crp_buf
, type
)) != 0)
965 case CRYPTO_NULL_CBC
:
968 case CRYPTO_MD5_HMAC
:
969 case CRYPTO_MD5_HMAC_96
:
970 case CRYPTO_SHA1_HMAC
:
971 case CRYPTO_SHA1_HMAC_96
:
972 case CRYPTO_SHA2_HMAC
:
973 case CRYPTO_RIPEMD160_HMAC
:
974 case CRYPTO_RIPEMD160_HMAC_96
:
975 case CRYPTO_NULL_HMAC
:
976 case CRYPTO_MD5_KPDK
:
977 case CRYPTO_SHA1_KPDK
:
980 if ((crp
->crp_etype
= swcr_authcompute(crp
, crd
, sw
,
981 crp
->crp_buf
, type
)) != 0)
985 case CRYPTO_DEFLATE_COMP
:
986 case CRYPTO_GZIP_COMP
:
987 DPRINTF(("swcr_process: compdec for %d\n", sw
->sw_alg
));
988 if ((crp
->crp_etype
= swcr_compdec(crd
, sw
,
989 crp
->crp_buf
, type
)) != 0)
992 crp
->crp_olen
= (int)sw
->sw_size
;
996 /* Unknown/unsupported algorithm */
997 crp
->crp_etype
= EINVAL
;
1003 DPRINTF(("request %08x done\n", (uint32_t)crp
));
1011 swcr_id
= crypto_get_driverid(CRYPTOCAP_F_SOFTWARE
);
1013 /* This should never happen */
1014 panic("Software crypto device cannot initialize!");
1017 crypto_register(swcr_id
, CRYPTO_DES_CBC
,
1018 0, 0, swcr_newsession
, swcr_freesession
, swcr_process
, NULL
);
1019 #define REGISTER(alg) \
1020 crypto_register(swcr_id, alg, 0, 0, NULL, NULL, NULL, NULL)
1022 REGISTER(CRYPTO_3DES_CBC
);
1023 REGISTER(CRYPTO_BLF_CBC
);
1024 REGISTER(CRYPTO_CAST_CBC
);
1025 REGISTER(CRYPTO_SKIPJACK_CBC
);
1026 REGISTER(CRYPTO_NULL_CBC
);
1027 REGISTER(CRYPTO_MD5_HMAC
);
1028 REGISTER(CRYPTO_MD5_HMAC_96
);
1029 REGISTER(CRYPTO_SHA1_HMAC
);
1030 REGISTER(CRYPTO_SHA1_HMAC_96
);
1031 REGISTER(CRYPTO_SHA2_HMAC
);
1032 REGISTER(CRYPTO_RIPEMD160_HMAC
);
1033 REGISTER(CRYPTO_RIPEMD160_HMAC_96
);
1034 REGISTER(CRYPTO_NULL_HMAC
);
1035 REGISTER(CRYPTO_MD5_KPDK
);
1036 REGISTER(CRYPTO_SHA1_KPDK
);
1037 REGISTER(CRYPTO_MD5
);
1038 REGISTER(CRYPTO_SHA1
);
1039 REGISTER(CRYPTO_RIJNDAEL128_CBC
);
1040 REGISTER(CRYPTO_DEFLATE_COMP
);
1041 REGISTER(CRYPTO_GZIP_COMP
);
1047 * Pseudo-device init routine for software crypto.
1049 void swcryptoattach(int);
1052 swcryptoattach(int num
)