2 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * STREAMS Crypto Module
7 * This module is used to facilitate Kerberos encryption
8 * operations for the telnet daemon and rlogin daemon.
9 * Because the Solaris telnet and rlogin daemons run mostly
10 * in-kernel via 'telmod' and 'rlmod', this module must be
11 * pushed on the STREAM *below* telmod or rlmod.
13 * Parts of the 3DES key derivation code are covered by the
14 * following copyright.
16 * Copyright (C) 1998 by the FundsXpress, INC.
18 * All rights reserved.
20 * Export of this software from the United States of America may require
21 * a specific license from the United States Government. It is the
22 * responsibility of any person or organization contemplating export to
23 * obtain such a license before exporting.
25 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
26 * distribute this software and its documentation for any purpose and
27 * without fee is hereby granted, provided that the above copyright
28 * notice appear in all copies and that both that copyright notice and
29 * this permission notice appear in supporting documentation, and that
30 * the name of FundsXpress. not be used in advertising or publicity pertaining
31 * to distribution of the software without specific, written prior
32 * permission. FundsXpress makes no representations about the suitability of
33 * this software for any purpose. It is provided "as is" without express
34 * or implied warranty.
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
37 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
38 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
41 #include <sys/types.h>
42 #include <sys/sysmacros.h>
43 #include <sys/errno.h>
44 #include <sys/debug.h>
46 #include <sys/stropts.h>
47 #include <sys/stream.h>
48 #include <sys/strsubr.h>
49 #include <sys/strlog.h>
50 #include <sys/cmn_err.h>
52 #include <sys/sunddi.h>
54 #include <sys/strsun.h>
55 #include <sys/random.h>
56 #include <sys/types.h>
57 #include <sys/byteorder.h>
58 #include <sys/cryptmod.h>
59 #include <sys/crc32.h>
60 #include <sys/policy.h>
62 #include <sys/crypto/api.h>
65 * Function prototypes.
67 static int cryptmodopen(queue_t
*, dev_t
*, int, int, cred_t
*);
68 static void cryptmodrput(queue_t
*, mblk_t
*);
69 static void cryptmodwput(queue_t
*, mblk_t
*);
70 static int cryptmodclose(queue_t
*);
71 static int cryptmodwsrv(queue_t
*);
72 static int cryptmodrsrv(queue_t
*);
74 static mblk_t
*do_encrypt(queue_t
*q
, mblk_t
*mp
);
75 static mblk_t
*do_decrypt(queue_t
*q
, mblk_t
*mp
);
77 #define CRYPTMOD_ID 5150
83 static struct module_info cryptmod_minfo
= {
84 CRYPTMOD_ID
, /* mi_idnum */
85 "cryptmod", /* mi_idname */
87 INFPSZ
, /* mi_maxpsz */
92 static struct qinit cryptmod_rinit
= {
93 (int (*)())cryptmodrput
, /* qi_putp */
94 cryptmodrsrv
, /* qi_svc */
95 cryptmodopen
, /* qi_qopen */
96 cryptmodclose
, /* qi_qclose */
98 &cryptmod_minfo
, /* qi_minfo */
102 static struct qinit cryptmod_winit
= {
103 (int (*)())cryptmodwput
, /* qi_putp */
104 cryptmodwsrv
, /* qi_srvp */
106 NULL
, /* qi_qclose */
107 NULL
, /* qi_qadmin */
108 &cryptmod_minfo
, /* qi_minfo */
112 static struct streamtab cryptmod_info
= {
113 &cryptmod_rinit
, /* st_rdinit */
114 &cryptmod_winit
, /* st_wrinit */
115 NULL
, /* st_muxrinit */
116 NULL
/* st_muxwinit */
125 #define MAX_CKSUM_LEN 20
126 #define CONFOUNDER_LEN 8
128 #define SHA1_HASHSIZE 20
129 #define MD5_HASHSIZE 16
130 #define CRC32_HASHSIZE 4
131 #define MSGBUF_SIZE 4096
132 #define CONFOUNDER_BYTES 128
135 static int crc32_calc(uchar_t
*, uchar_t
*, uint_t
);
136 static int md5_calc(uchar_t
*, uchar_t
*, uint_t
);
137 static int sha1_calc(uchar_t
*, uchar_t
*, uint_t
);
139 static hash_info_t null_hash
= {0, 0, NULL
};
140 static hash_info_t crc32_hash
= {CRC32_HASHSIZE
, CONFOUNDER_LEN
, crc32_calc
};
141 static hash_info_t md5_hash
= {MD5_HASHSIZE
, CONFOUNDER_LEN
, md5_calc
};
142 static hash_info_t sha1_hash
= {SHA1_HASHSIZE
, CONFOUNDER_LEN
, sha1_calc
};
144 static crypto_mech_type_t sha1_hmac_mech
= CRYPTO_MECH_INVALID
;
145 static crypto_mech_type_t md5_hmac_mech
= CRYPTO_MECH_INVALID
;
146 static crypto_mech_type_t sha1_hash_mech
= CRYPTO_MECH_INVALID
;
147 static crypto_mech_type_t md5_hash_mech
= CRYPTO_MECH_INVALID
;
149 static int kef_crypt(struct cipher_data_t
*, void *,
150 crypto_data_format_t
, size_t, int);
152 arcfour_hmac_md5_encrypt(queue_t
*, struct tmodinfo
*,
153 mblk_t
*, hash_info_t
*);
155 arcfour_hmac_md5_decrypt(queue_t
*, struct tmodinfo
*,
156 mblk_t
*, hash_info_t
*);
159 do_hmac(crypto_mech_type_t
, crypto_key_t
*, char *, int, char *, int);
162 * This is the loadable module wrapper.
164 #include <sys/modctl.h>
166 static struct fmodsw fsw
= {
173 * Module linkage information for the kernel.
175 static struct modlstrmod modlstrmod
= {
177 "STREAMS encryption module",
181 static struct modlinkage modlinkage
= {
190 return (mod_install(&modlinkage
));
196 return (mod_remove(&modlinkage
));
200 _info(struct modinfo
*modinfop
)
202 return (mod_info(&modlinkage
, modinfop
));
206 cleanup(struct cipher_data_t
*cd
)
208 if (cd
->key
!= NULL
) {
209 bzero(cd
->key
, cd
->keylen
);
210 kmem_free(cd
->key
, cd
->keylen
);
214 if (cd
->ckey
!= NULL
) {
216 * ckey is a crypto_key_t structure which references
217 * "cd->key" for its raw key data. Since that was already
218 * cleared out, we don't need another "bzero" here.
220 kmem_free(cd
->ckey
, sizeof (crypto_key_t
));
224 if (cd
->block
!= NULL
) {
225 kmem_free(cd
->block
, cd
->blocklen
);
229 if (cd
->saveblock
!= NULL
) {
230 kmem_free(cd
->saveblock
, cd
->blocklen
);
231 cd
->saveblock
= NULL
;
234 if (cd
->ivec
!= NULL
) {
235 kmem_free(cd
->ivec
, cd
->ivlen
);
239 if (cd
->d_encr_key
.ck_data
!= NULL
) {
240 bzero(cd
->d_encr_key
.ck_data
, cd
->keylen
);
241 kmem_free(cd
->d_encr_key
.ck_data
, cd
->keylen
);
244 if (cd
->d_hmac_key
.ck_data
!= NULL
) {
245 bzero(cd
->d_hmac_key
.ck_data
, cd
->keylen
);
246 kmem_free(cd
->d_hmac_key
.ck_data
, cd
->keylen
);
249 if (cd
->enc_tmpl
!= NULL
)
250 (void) crypto_destroy_ctx_template(cd
->enc_tmpl
);
252 if (cd
->hmac_tmpl
!= NULL
)
253 (void) crypto_destroy_ctx_template(cd
->hmac_tmpl
);
255 if (cd
->ctx
!= NULL
) {
256 crypto_cancel_ctx(cd
->ctx
);
263 cryptmodopen(queue_t
*rq
, dev_t
*dev
, int oflag
, int sflag
, cred_t
*crp
)
265 struct tmodinfo
*tmi
;
268 if (sflag
!= MODOPEN
)
271 (void) (STRLOG(CRYPTMOD_ID
, 0, 5, SL_TRACE
|SL_NOTE
,
272 "cryptmodopen: opening module(PID %d)",
275 if (rq
->q_ptr
!= NULL
) {
276 cmn_err(CE_WARN
, "cryptmodopen: already opened");
281 * Allocate and initialize per-Stream structure.
283 tmi
= (struct tmodinfo
*)kmem_zalloc(sizeof (struct tmodinfo
),
286 tmi
->enc_data
.method
= CRYPT_METHOD_NONE
;
287 tmi
->dec_data
.method
= CRYPT_METHOD_NONE
;
289 tmi
->ready
= (CRYPT_READ_READY
| CRYPT_WRITE_READY
);
291 rq
->q_ptr
= WR(rq
)->q_ptr
= tmi
;
293 sha1_hmac_mech
= crypto_mech2id(SUN_CKM_SHA1_HMAC
);
294 md5_hmac_mech
= crypto_mech2id(SUN_CKM_MD5_HMAC
);
295 sha1_hash_mech
= crypto_mech2id(SUN_CKM_SHA1
);
296 md5_hash_mech
= crypto_mech2id(SUN_CKM_MD5
);
304 cryptmodclose(queue_t
*rq
)
306 struct tmodinfo
*tmi
= (struct tmodinfo
*)rq
->q_ptr
;
311 cleanup(&tmi
->enc_data
);
312 cleanup(&tmi
->dec_data
);
314 kmem_free(tmi
, sizeof (struct tmodinfo
));
315 rq
->q_ptr
= WR(rq
)->q_ptr
= NULL
;
323 * Calculate exactly how much space is needed in front
324 * of the "plaintext" in an mbuf so it can be positioned
325 * 1 time instead of potentially moving the data multiple
329 plaintext_offset(struct cipher_data_t
*cd
)
333 /* 4 byte length prepended to all RCMD msgs */
334 if (ANY_RCMD_MODE(cd
->option_mask
))
335 headspace
+= RCMD_LEN_SZ
;
337 /* RCMD V2 mode adds an additional 4 byte plaintext length */
338 if (cd
->option_mask
& CRYPTOPT_RCMD_MODE_V2
)
339 headspace
+= RCMD_LEN_SZ
;
341 /* Need extra space for hash and counfounder */
342 switch (cd
->method
) {
343 case CRYPT_METHOD_DES_CBC_NULL
:
344 headspace
+= null_hash
.hash_len
+ null_hash
.confound_len
;
346 case CRYPT_METHOD_DES_CBC_CRC
:
347 headspace
+= crc32_hash
.hash_len
+ crc32_hash
.confound_len
;
349 case CRYPT_METHOD_DES_CBC_MD5
:
350 headspace
+= md5_hash
.hash_len
+ md5_hash
.confound_len
;
352 case CRYPT_METHOD_DES3_CBC_SHA1
:
353 headspace
+= sha1_hash
.confound_len
;
355 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
356 headspace
+= md5_hash
.hash_len
+ md5_hash
.confound_len
;
358 case CRYPT_METHOD_AES128
:
359 case CRYPT_METHOD_AES256
:
360 headspace
+= DEFAULT_AES_BLOCKLEN
;
362 case CRYPT_METHOD_DES_CFB
:
363 case CRYPT_METHOD_NONE
:
372 * Calculate the resulting size when encrypting 'plainlen' bytes
376 encrypt_size(struct cipher_data_t
*cd
, size_t plainlen
)
380 switch (cd
->method
) {
381 case CRYPT_METHOD_DES_CBC_NULL
:
382 cipherlen
= (size_t)P2ROUNDUP(null_hash
.hash_len
+
385 case CRYPT_METHOD_DES_CBC_MD5
:
386 cipherlen
= (size_t)P2ROUNDUP(md5_hash
.hash_len
+
387 md5_hash
.confound_len
+
390 case CRYPT_METHOD_DES_CBC_CRC
:
391 cipherlen
= (size_t)P2ROUNDUP(crc32_hash
.hash_len
+
392 crc32_hash
.confound_len
+
395 case CRYPT_METHOD_DES3_CBC_SHA1
:
396 cipherlen
= (size_t)P2ROUNDUP(sha1_hash
.confound_len
+
400 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
401 cipherlen
= (size_t)P2ROUNDUP(md5_hash
.confound_len
+
402 plainlen
, 1) + md5_hash
.hash_len
;
404 case CRYPT_METHOD_AES128
:
405 case CRYPT_METHOD_AES256
:
406 /* No roundup for AES-CBC-CTS */
407 cipherlen
= DEFAULT_AES_BLOCKLEN
+ plainlen
+
408 AES_TRUNCATED_HMAC_LEN
;
410 case CRYPT_METHOD_DES_CFB
:
411 case CRYPT_METHOD_NONE
:
412 cipherlen
= plainlen
;
422 * Encrypt the mblk data using DES with cipher feedback.
424 * Given that V[i] is the initial 64 bit vector, V[n] is the nth 64 bit
425 * vector, D[n] is the nth chunk of 64 bits of data to encrypt
426 * (decrypt), and O[n] is the nth chunk of 64 bits of encrypted
427 * (decrypted) data, then:
429 * V[0] = DES(V[i], key)
430 * O[n] = D[n] <exclusive or > V[n]
431 * V[n+1] = DES(O[n], key)
433 * The size of the message being encrypted does not change in this
434 * algorithm, num_bytes in == num_bytes out.
437 des_cfb_encrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
)
440 char *iptr
, *optr
, *lastoutput
;
442 lastoutput
= optr
= (char *)mp
->b_rptr
;
443 iptr
= (char *)mp
->b_rptr
;
444 savedbytes
= tmi
->enc_data
.bytes
% CFB_BLKSZ
;
446 while (iptr
< (char *)mp
->b_wptr
) {
449 * The first time this runs, the 'tmi->enc_data.block' will
450 * contain the initialization vector that should have been
451 * passed in with the SETUP ioctl.
453 * V[n] = DES(V[n-1], key)
455 if (!(tmi
->enc_data
.bytes
% CFB_BLKSZ
)) {
457 retval
= kef_crypt(&tmi
->enc_data
,
460 tmi
->enc_data
.blocklen
,
463 if (retval
!= CRYPTO_SUCCESS
) {
465 cmn_err(CE_WARN
, "des_cfb_encrypt: kef_crypt "
466 "failed - error 0x%0x", retval
);
468 mp
->b_datap
->db_type
= M_ERROR
;
469 mp
->b_rptr
= mp
->b_datap
->db_base
;
471 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
479 /* O[n] = I[n] ^ V[n] */
480 *(optr
++) = *(iptr
++) ^
481 tmi
->enc_data
.block
[tmi
->enc_data
.bytes
% CFB_BLKSZ
];
483 tmi
->enc_data
.bytes
++;
485 * Feedback the encrypted output as the input to next DES call.
487 if (!(tmi
->enc_data
.bytes
% CFB_BLKSZ
)) {
488 char *dbptr
= tmi
->enc_data
.block
;
490 * Get the last bits of input from the previous
491 * msg block that we haven't yet used as feedback input.
493 if (savedbytes
> 0) {
494 bcopy(tmi
->enc_data
.saveblock
,
495 dbptr
, (size_t)savedbytes
);
500 * Now copy the correct bytes from the current input
501 * stream and update the 'lastoutput' ptr
503 bcopy(lastoutput
, dbptr
,
504 (size_t)(CFB_BLKSZ
- savedbytes
));
506 lastoutput
+= (CFB_BLKSZ
- savedbytes
);
511 * If there are bytes of input here that we need in the next
512 * block to build an ivec, save them off here.
514 if (lastoutput
< optr
) {
516 tmi
->enc_data
.saveblock
+ savedbytes
,
517 (uint_t
)(optr
- lastoutput
));
525 * Decrypt the data in the mblk using DES in Cipher Feedback mode
527 * # bytes in == # bytes out, no padding, confounding, or hashing
532 des_cfb_decrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
)
542 /* decrypted output goes into the new data buffer */
543 lastinput
= iptr
= (char *)mp
->b_rptr
;
545 savedbytes
= tmi
->dec_data
.bytes
% tmi
->dec_data
.blocklen
;
548 * Save the input CFB_BLKSZ bytes at a time.
549 * We are trying to decrypt in-place, but need to keep
550 * a small sliding window of encrypted text to be
551 * used to construct the feedback buffer.
553 cp
= ((tmi
->dec_data
.blocklen
- savedbytes
) > len
? len
:
554 tmi
->dec_data
.blocklen
- savedbytes
);
556 bcopy(lastinput
, tmi
->dec_data
.saveblock
+ savedbytes
, cp
);
561 while (iptr
< (char *)mp
->b_wptr
) {
564 * The first time this runs, the 'tmi->dec_data.block' will
565 * contain the initialization vector that should have been
566 * passed in with the SETUP ioctl.
568 if (!(tmi
->dec_data
.bytes
% CFB_BLKSZ
)) {
570 retval
= kef_crypt(&tmi
->dec_data
,
573 tmi
->dec_data
.blocklen
,
576 if (retval
!= CRYPTO_SUCCESS
) {
578 cmn_err(CE_WARN
, "des_cfb_decrypt: kef_crypt "
579 "failed - status 0x%0x", retval
);
581 mp
->b_datap
->db_type
= M_ERROR
;
582 mp
->b_rptr
= mp
->b_datap
->db_base
;
584 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
593 * To decrypt, XOR the input with the output from the DES call
595 *(iptr
++) ^= tmi
->dec_data
.block
[tmi
->dec_data
.bytes
%
598 tmi
->dec_data
.bytes
++;
601 * Feedback the encrypted input for next DES call.
603 if (!(tmi
->dec_data
.bytes
% tmi
->dec_data
.blocklen
)) {
604 char *dbptr
= tmi
->dec_data
.block
;
606 * Get the last bits of input from the previous block
607 * that we haven't yet processed.
609 if (savedbytes
> 0) {
610 bcopy(tmi
->dec_data
.saveblock
,
618 * This block makes sure that our local
619 * buffer of input data is full and can
620 * be accessed from the beginning.
622 if (lastinput
< (char *)mp
->b_wptr
) {
624 /* How many bytes are left in the mblk? */
625 cp
= (((char *)mp
->b_wptr
- lastinput
) >
626 tmi
->dec_data
.blocklen
?
627 tmi
->dec_data
.blocklen
:
628 (char *)mp
->b_wptr
- lastinput
);
630 /* copy what we need */
631 bcopy(lastinput
, tmi
->dec_data
.saveblock
,
646 * Compute a CRC32 checksum on the input
649 crc32_calc(uchar_t
*buf
, uchar_t
*input
, uint_t len
)
653 CRC32(crc
, input
, len
, 0, crc32_table
);
655 buf
[0] = (uchar_t
)(crc
& 0xff);
656 buf
[1] = (uchar_t
)((crc
>> 8) & 0xff);
657 buf
[2] = (uchar_t
)((crc
>> 16) & 0xff);
658 buf
[3] = (uchar_t
)((crc
>> 24) & 0xff);
660 return (CRYPTO_SUCCESS
);
664 kef_digest(crypto_mech_type_t digest_type
,
665 uchar_t
*input
, uint_t inlen
,
666 uchar_t
*output
, uint_t hashlen
)
669 crypto_data_t d1
, d2
;
670 crypto_mechanism_t mech
;
673 mech
.cm_type
= digest_type
;
675 mech
.cm_param_len
= 0;
677 v1
.iov_base
= (void *)input
;
680 d1
.cd_format
= CRYPTO_DATA_RAW
;
682 d1
.cd_length
= v1
.iov_len
;
685 v2
.iov_base
= (void *)output
;
686 v2
.iov_len
= hashlen
;
688 d2
.cd_format
= CRYPTO_DATA_RAW
;
690 d2
.cd_length
= v2
.iov_len
;
693 rv
= crypto_digest(&mech
, &d1
, &d2
, NULL
);
701 * Get a SHA1 hash on the input data.
704 sha1_calc(uchar_t
*output
, uchar_t
*input
, uint_t inlen
)
708 rv
= kef_digest(sha1_hash_mech
, input
, inlen
, output
, SHA1_HASHSIZE
);
714 * Get an MD5 hash on the input data.
719 md5_calc(uchar_t
*output
, uchar_t
*input
, uint_t inlen
)
723 rv
= kef_digest(md5_hash_mech
, input
, inlen
, output
, MD5_HASHSIZE
);
730 * duplicate the functionality of the krb5_nfold function from
731 * the userland kerberos mech.
732 * This is needed to derive keys for use with 3DES/SHA1-HMAC
736 nfold(int inbits
, uchar_t
*in
, int outbits
, uchar_t
*out
)
744 /* first compute lcm(n,k) */
754 lcm
= outbits
*inbits
/a
;
756 /* now do the real work */
762 * Compute the msbit in k which gets added into this byte
763 * first, start with the msbit in the first, unrotated byte
764 * then, for each byte, shift to the right for each repetition
765 * last, pick out the correct byte within that shifted repetition
767 for (i
= lcm
-1; i
>= 0; i
--) {
768 msbit
= (((inbits
<<3)-1)
769 +(((inbits
<<3)+13)*(i
/inbits
))
770 +((inbits
-(i
%inbits
))<<3)) %(inbits
<<3);
772 /* pull out the byte value itself */
773 byte
+= (((in
[((inbits
-1)-(msbit
>>3))%inbits
]<<8)|
774 (in
[((inbits
)-(msbit
>>3))%inbits
]))
775 >>((msbit
&7)+1))&0xff;
777 /* do the addition */
778 byte
+= out
[i
%outbits
];
779 out
[i
%outbits
] = byte
&0xff;
784 /* if there's a carry bit left over, add it back in */
786 for (i
= outbits
-1; i
>= 0; i
--) {
787 /* do the addition */
791 /* keep around the carry bit, if any */
797 #define smask(step) ((1<<step)-1)
798 #define pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
799 #define parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
802 * Duplicate the functionality of the "dk_derive_key" function
803 * in the Kerberos mechanism.
806 derive_key(struct cipher_data_t
*cdata
, uchar_t
*constdata
,
807 int constlen
, char *dkey
, int keybytes
,
817 inblock
= kmem_zalloc(blocklen
, KM_SLEEP
);
818 rawkey
= kmem_zalloc(keybytes
, KM_SLEEP
);
819 zeroblock
= kmem_zalloc(blocklen
, KM_SLEEP
);
821 if (constlen
== blocklen
)
822 bcopy(constdata
, inblock
, blocklen
);
824 nfold(constlen
* 8, constdata
,
825 blocklen
* 8, (uchar_t
*)inblock
);
828 * zeroblock is an IV of all 0's.
830 * The "block" section of the cdata record is used as the
831 * IV for crypto operations in the kef_crypt function.
833 * We use 'block' as a generic IV data buffer because it
834 * is attached to the stream state data and thus can
835 * be used to hold information that must carry over
836 * from processing of one mblk to another.
838 * Here, we save the current IV and replace it with
839 * and empty IV (all 0's) for use when deriving the
840 * keys. Once the key derivation is done, we swap the
841 * old IV back into place.
843 saveblock
= cdata
->block
;
844 cdata
->block
= zeroblock
;
846 while (n
< keybytes
) {
847 rv
= kef_crypt(cdata
, inblock
, CRYPTO_DATA_RAW
,
848 blocklen
, CRYPT_ENCRYPT
);
849 if (rv
!= CRYPTO_SUCCESS
) {
850 /* put the original IV block back in place */
851 cdata
->block
= saveblock
;
852 cmn_err(CE_WARN
, "failed to derive a key: %0x", rv
);
856 if (keybytes
- n
< blocklen
) {
857 bcopy(inblock
, rawkey
+n
, (keybytes
-n
));
860 bcopy(inblock
, rawkey
+n
, blocklen
);
863 /* put the original IV block back in place */
864 cdata
->block
= saveblock
;
866 /* finally, make the key */
867 if (cdata
->method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
869 * 3DES key derivation requires that we make sure the
870 * key has the proper parity.
872 for (i
= 0; i
< 3; i
++) {
873 bcopy(rawkey
+(i
*7), dkey
+(i
*8), 7);
875 /* 'dkey' is our derived key output buffer */
876 dkey
[i
*8+7] = (((dkey
[i
*8]&1)<<1) |
877 ((dkey
[i
*8+1]&1)<<2) |
878 ((dkey
[i
*8+2]&1)<<3) |
879 ((dkey
[i
*8+3]&1)<<4) |
880 ((dkey
[i
*8+4]&1)<<5) |
881 ((dkey
[i
*8+5]&1)<<6) |
882 ((dkey
[i
*8+6]&1)<<7));
884 for (n
= 0; n
< 8; n
++) {
885 dkey
[i
*8 + n
] &= 0xfe;
886 dkey
[i
*8 + n
] |= 1^parity_char(dkey
[i
*8 + n
]);
889 } else if (IS_AES_METHOD(cdata
->method
)) {
890 bcopy(rawkey
, dkey
, keybytes
);
893 kmem_free(inblock
, blocklen
);
894 kmem_free(zeroblock
, blocklen
);
895 kmem_free(rawkey
, keybytes
);
900 * create_derived_keys
902 * Algorithm for deriving a new key and an HMAC key
903 * before computing the 3DES-SHA1-HMAC operation on the plaintext
904 * This algorithm matches the work done by Kerberos mechanism
908 create_derived_keys(struct cipher_data_t
*cdata
, uint32_t usage
,
909 crypto_key_t
*enckey
, crypto_key_t
*hmackey
)
911 uchar_t constdata
[K5CLENGTH
];
915 constdata
[0] = (usage
>>24)&0xff;
916 constdata
[1] = (usage
>>16)&0xff;
917 constdata
[2] = (usage
>>8)&0xff;
918 constdata
[3] = usage
& 0xff;
919 /* Use "0xAA" for deriving encryption key */
920 constdata
[4] = 0xAA; /* from MIT Kerberos code */
922 enckey
->ck_length
= cdata
->keylen
* 8;
923 enckey
->ck_format
= CRYPTO_KEY_RAW
;
924 enckey
->ck_data
= kmem_zalloc(cdata
->keylen
, KM_SLEEP
);
926 switch (cdata
->method
) {
927 case CRYPT_METHOD_DES_CFB
:
928 case CRYPT_METHOD_DES_CBC_NULL
:
929 case CRYPT_METHOD_DES_CBC_MD5
:
930 case CRYPT_METHOD_DES_CBC_CRC
:
933 case CRYPT_METHOD_DES3_CBC_SHA1
:
934 keybytes
= CRYPT_DES3_KEYBYTES
;
936 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
937 case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
:
938 keybytes
= CRYPT_ARCFOUR_KEYBYTES
;
940 case CRYPT_METHOD_AES128
:
941 keybytes
= CRYPT_AES128_KEYBYTES
;
943 case CRYPT_METHOD_AES256
:
944 keybytes
= CRYPT_AES256_KEYBYTES
;
948 /* derive main crypto key */
949 rv
= derive_key(cdata
, constdata
, sizeof (constdata
),
950 enckey
->ck_data
, keybytes
, cdata
->blocklen
);
952 if (rv
== CRYPTO_SUCCESS
) {
954 /* Use "0x55" for deriving mac key */
957 hmackey
->ck_length
= cdata
->keylen
* 8;
958 hmackey
->ck_format
= CRYPTO_KEY_RAW
;
959 hmackey
->ck_data
= kmem_zalloc(cdata
->keylen
, KM_SLEEP
);
961 rv
= derive_key(cdata
, constdata
, sizeof (constdata
),
962 hmackey
->ck_data
, keybytes
,
965 cmn_err(CE_WARN
, "failed to derive crypto key: %02x", rv
);
972 * Compute 3-DES crypto and HMAC.
975 kef_decr_hmac(struct cipher_data_t
*cdata
,
976 mblk_t
*mp
, int length
,
977 char *hmac
, int hmaclen
)
979 int rv
= CRYPTO_FAILED
;
981 crypto_mechanism_t encr_mech
;
982 crypto_mechanism_t mac_mech
;
987 ASSERT(cdata
!= NULL
);
989 ASSERT(hmac
!= NULL
);
991 bzero(&dd
, sizeof (dd
));
992 dd
.cd_format
= CRYPTO_DATA_MBLK
;
994 dd
.cd_length
= length
;
998 v1
.iov_len
= hmaclen
;
1000 mac
.cd_format
= CRYPTO_DATA_RAW
;
1002 mac
.cd_length
= hmaclen
;
1006 * cdata->block holds the IVEC
1008 encr_mech
.cm_type
= cdata
->mech_type
;
1009 encr_mech
.cm_param
= cdata
->block
;
1011 if (cdata
->block
!= NULL
)
1012 encr_mech
.cm_param_len
= cdata
->blocklen
;
1014 encr_mech
.cm_param_len
= 0;
1016 rv
= crypto_decrypt(&encr_mech
, &dd
, &cdata
->d_encr_key
,
1017 cdata
->enc_tmpl
, NULL
, NULL
);
1018 if (rv
!= CRYPTO_SUCCESS
) {
1019 cmn_err(CE_WARN
, "crypto_decrypt failed: %0x", rv
);
1023 mac_mech
.cm_type
= sha1_hmac_mech
;
1024 mac_mech
.cm_param
= NULL
;
1025 mac_mech
.cm_param_len
= 0;
1028 * Compute MAC of the plaintext decrypted above.
1030 rv
= crypto_mac(&mac_mech
, &dd
, &cdata
->d_hmac_key
,
1031 cdata
->hmac_tmpl
, &mac
, NULL
);
1033 if (rv
!= CRYPTO_SUCCESS
) {
1034 cmn_err(CE_WARN
, "crypto_mac failed: %0x", rv
);
1041 * Compute 3-DES crypto and HMAC.
1044 kef_encr_hmac(struct cipher_data_t
*cdata
,
1045 mblk_t
*mp
, int length
,
1046 char *hmac
, int hmaclen
)
1048 int rv
= CRYPTO_FAILED
;
1050 crypto_mechanism_t encr_mech
;
1051 crypto_mechanism_t mac_mech
;
1056 ASSERT(cdata
!= NULL
);
1058 ASSERT(hmac
!= NULL
);
1060 bzero(&dd
, sizeof (dd
));
1061 dd
.cd_format
= CRYPTO_DATA_MBLK
;
1063 dd
.cd_length
= length
;
1067 v1
.iov_len
= hmaclen
;
1069 mac
.cd_format
= CRYPTO_DATA_RAW
;
1071 mac
.cd_length
= hmaclen
;
1075 * cdata->block holds the IVEC
1077 encr_mech
.cm_type
= cdata
->mech_type
;
1078 encr_mech
.cm_param
= cdata
->block
;
1080 if (cdata
->block
!= NULL
)
1081 encr_mech
.cm_param_len
= cdata
->blocklen
;
1083 encr_mech
.cm_param_len
= 0;
1085 mac_mech
.cm_type
= sha1_hmac_mech
;
1086 mac_mech
.cm_param
= NULL
;
1087 mac_mech
.cm_param_len
= 0;
1089 rv
= crypto_mac(&mac_mech
, &dd
, &cdata
->d_hmac_key
,
1090 cdata
->hmac_tmpl
, &mac
, NULL
);
1092 if (rv
!= CRYPTO_SUCCESS
) {
1093 cmn_err(CE_WARN
, "crypto_mac failed: %0x", rv
);
1097 rv
= crypto_encrypt(&encr_mech
, &dd
, &cdata
->d_encr_key
,
1098 cdata
->enc_tmpl
, NULL
, NULL
);
1099 if (rv
!= CRYPTO_SUCCESS
) {
1100 cmn_err(CE_WARN
, "crypto_encrypt failed: %0x", rv
);
1109 * Use the Kernel encryption framework to provide the
1110 * crypto operations for the indicated data.
1113 kef_crypt(struct cipher_data_t
*cdata
,
1114 void *indata
, crypto_data_format_t fmt
,
1115 size_t length
, int mode
)
1117 int rv
= CRYPTO_FAILED
;
1119 crypto_mechanism_t mech
;
1124 ASSERT(cdata
!= NULL
);
1125 ASSERT(indata
!= NULL
);
1126 ASSERT(fmt
== CRYPTO_DATA_RAW
|| fmt
== CRYPTO_DATA_MBLK
);
1128 bzero(&crkey
, sizeof (crkey
));
1129 bzero(&d1
, sizeof (d1
));
1131 crkey
.ck_format
= CRYPTO_KEY_RAW
;
1132 crkey
.ck_data
= cdata
->key
;
1134 /* keys are measured in bits, not bytes, so multiply by 8 */
1135 crkey
.ck_length
= cdata
->keylen
* 8;
1137 if (fmt
== CRYPTO_DATA_RAW
) {
1138 v1
.iov_base
= (char *)indata
;
1139 v1
.iov_len
= length
;
1144 d1
.cd_length
= length
;
1145 if (fmt
== CRYPTO_DATA_RAW
)
1147 else if (fmt
== CRYPTO_DATA_MBLK
)
1148 d1
.cd_mp
= (mblk_t
*)indata
;
1150 mech
.cm_type
= cdata
->mech_type
;
1151 mech
.cm_param
= cdata
->block
;
1153 * cdata->block holds the IVEC
1155 if (cdata
->block
!= NULL
)
1156 mech
.cm_param_len
= cdata
->blocklen
;
1158 mech
.cm_param_len
= 0;
1161 * encrypt and decrypt in-place
1163 if (mode
== CRYPT_ENCRYPT
)
1164 rv
= crypto_encrypt(&mech
, &d1
, &crkey
, NULL
, NULL
, NULL
);
1166 rv
= crypto_decrypt(&mech
, &d1
, &crkey
, NULL
, NULL
, NULL
);
1168 if (rv
!= CRYPTO_SUCCESS
) {
1169 cmn_err(CE_WARN
, "%s returned error %08x",
1170 (mode
== CRYPT_ENCRYPT
? "crypto_encrypt" :
1171 "crypto_decrypt"), rv
);
1172 return (CRYPTO_FAILED
);
1179 do_hmac(crypto_mech_type_t mech
,
1181 char *data
, int datalen
,
1182 char *hmac
, int hmaclen
)
1185 crypto_mechanism_t mac_mech
;
1188 iovec_t vdata
, vmac
;
1190 mac_mech
.cm_type
= mech
;
1191 mac_mech
.cm_param
= NULL
;
1192 mac_mech
.cm_param_len
= 0;
1194 vdata
.iov_base
= data
;
1195 vdata
.iov_len
= datalen
;
1197 bzero(&dd
, sizeof (dd
));
1198 dd
.cd_format
= CRYPTO_DATA_RAW
;
1200 dd
.cd_length
= datalen
;
1203 vmac
.iov_base
= hmac
;
1204 vmac
.iov_len
= hmaclen
;
1206 mac
.cd_format
= CRYPTO_DATA_RAW
;
1208 mac
.cd_length
= hmaclen
;
1212 * Compute MAC of the plaintext decrypted above.
1214 rv
= crypto_mac(&mac_mech
, &dd
, key
, NULL
, &mac
, NULL
);
1216 if (rv
!= CRYPTO_SUCCESS
) {
1217 cmn_err(CE_WARN
, "crypto_mac failed: %0x", rv
);
1223 #define XOR_BLOCK(src, dst) \
1224 (dst)[0] ^= (src)[0]; \
1225 (dst)[1] ^= (src)[1]; \
1226 (dst)[2] ^= (src)[2]; \
1227 (dst)[3] ^= (src)[3]; \
1228 (dst)[4] ^= (src)[4]; \
1229 (dst)[5] ^= (src)[5]; \
1230 (dst)[6] ^= (src)[6]; \
1231 (dst)[7] ^= (src)[7]; \
1232 (dst)[8] ^= (src)[8]; \
1233 (dst)[9] ^= (src)[9]; \
1234 (dst)[10] ^= (src)[10]; \
1235 (dst)[11] ^= (src)[11]; \
1236 (dst)[12] ^= (src)[12]; \
1237 (dst)[13] ^= (src)[13]; \
1238 (dst)[14] ^= (src)[14]; \
1239 (dst)[15] ^= (src)[15]
1241 #define xorblock(x, y) XOR_BLOCK(y, x)
1244 aes_cbc_cts_encrypt(struct tmodinfo
*tmi
, uchar_t
*plain
, size_t length
)
1246 int result
= CRYPTO_SUCCESS
;
1247 unsigned char tmp
[DEFAULT_AES_BLOCKLEN
];
1248 unsigned char tmp2
[DEFAULT_AES_BLOCKLEN
];
1249 unsigned char tmp3
[DEFAULT_AES_BLOCKLEN
];
1250 int nblocks
= 0, blockno
;
1251 crypto_data_t ct
, pt
;
1252 crypto_mechanism_t mech
;
1254 mech
.cm_type
= tmi
->enc_data
.mech_type
;
1255 if (tmi
->enc_data
.ivlen
> 0 && tmi
->enc_data
.ivec
!= NULL
) {
1256 bcopy(tmi
->enc_data
.ivec
, tmp
, DEFAULT_AES_BLOCKLEN
);
1258 bzero(tmp
, sizeof (tmp
));
1260 mech
.cm_param
= NULL
;
1261 mech
.cm_param_len
= 0;
1263 nblocks
= (length
+ DEFAULT_AES_BLOCKLEN
- 1) / DEFAULT_AES_BLOCKLEN
;
1265 bzero(&ct
, sizeof (crypto_data_t
));
1266 bzero(&pt
, sizeof (crypto_data_t
));
1269 pt
.cd_format
= CRYPTO_DATA_RAW
;
1270 pt
.cd_length
= length
;
1271 pt
.cd_raw
.iov_base
= (char *)plain
;
1272 pt
.cd_raw
.iov_len
= length
;
1274 result
= crypto_encrypt(&mech
, &pt
,
1275 &tmi
->enc_data
.d_encr_key
, NULL
, NULL
, NULL
);
1277 if (result
!= CRYPTO_SUCCESS
) {
1278 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1279 "crypto_encrypt failed: %0x", result
);
1284 ct
.cd_format
= CRYPTO_DATA_RAW
;
1286 ct
.cd_length
= DEFAULT_AES_BLOCKLEN
;
1288 pt
.cd_format
= CRYPTO_DATA_RAW
;
1290 pt
.cd_length
= DEFAULT_AES_BLOCKLEN
;
1292 result
= crypto_encrypt_init(&mech
,
1293 &tmi
->enc_data
.d_encr_key
,
1294 tmi
->enc_data
.enc_tmpl
,
1295 &tmi
->enc_data
.ctx
, NULL
);
1297 if (result
!= CRYPTO_SUCCESS
) {
1298 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1299 "crypto_encrypt_init failed: %0x", result
);
1303 for (blockno
= 0; blockno
< nblocks
- 2; blockno
++) {
1304 xorblock(tmp
, plain
+ blockno
* DEFAULT_AES_BLOCKLEN
);
1306 pt
.cd_raw
.iov_base
= (char *)tmp
;
1307 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1309 ct
.cd_raw
.iov_base
= (char *)plain
+
1310 blockno
* DEFAULT_AES_BLOCKLEN
;
1311 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1313 result
= crypto_encrypt_update(tmi
->enc_data
.ctx
,
1316 if (result
!= CRYPTO_SUCCESS
) {
1317 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1318 "crypto_encrypt_update failed: %0x",
1322 /* copy result over original bytes */
1323 /* make another copy for the next XOR step */
1324 bcopy(plain
+ blockno
* DEFAULT_AES_BLOCKLEN
,
1325 tmp
, DEFAULT_AES_BLOCKLEN
);
1327 /* XOR cipher text from n-3 with plain text from n-2 */
1328 xorblock(tmp
, plain
+ (nblocks
- 2) * DEFAULT_AES_BLOCKLEN
);
1330 pt
.cd_raw
.iov_base
= (char *)tmp
;
1331 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1333 ct
.cd_raw
.iov_base
= (char *)tmp2
;
1334 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1336 /* encrypt XOR-ed block N-2 */
1337 result
= crypto_encrypt_update(tmi
->enc_data
.ctx
,
1339 if (result
!= CRYPTO_SUCCESS
) {
1340 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1341 "crypto_encrypt_update(2) failed: %0x",
1345 nleft
= length
- (nblocks
- 1) * DEFAULT_AES_BLOCKLEN
;
1347 bzero(tmp3
, sizeof (tmp3
));
1348 /* Save final plaintext bytes from n-1 */
1349 bcopy(plain
+ (nblocks
- 1) * DEFAULT_AES_BLOCKLEN
, tmp3
,
1352 /* Overwrite n-1 with cipher text from n-2 */
1353 bcopy(tmp2
, plain
+ (nblocks
- 1) * DEFAULT_AES_BLOCKLEN
,
1356 bcopy(tmp2
, tmp
, DEFAULT_AES_BLOCKLEN
);
1357 /* XOR cipher text from n-1 with plain text from n-1 */
1358 xorblock(tmp
, tmp3
);
1360 pt
.cd_raw
.iov_base
= (char *)tmp
;
1361 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1363 ct
.cd_raw
.iov_base
= (char *)tmp2
;
1364 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1366 /* encrypt block N-2 */
1367 result
= crypto_encrypt_update(tmi
->enc_data
.ctx
,
1370 if (result
!= CRYPTO_SUCCESS
) {
1371 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1372 "crypto_encrypt_update(3) failed: %0x",
1377 bcopy(tmp2
, plain
+ (nblocks
- 2) * DEFAULT_AES_BLOCKLEN
,
1378 DEFAULT_AES_BLOCKLEN
);
1381 ct
.cd_raw
.iov_base
= (char *)tmp2
;
1382 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1385 * Ignore the output on the final step.
1387 result
= crypto_encrypt_final(tmi
->enc_data
.ctx
, &ct
, NULL
);
1388 if (result
!= CRYPTO_SUCCESS
) {
1389 cmn_err(CE_WARN
, "aes_cbc_cts_encrypt: "
1390 "crypto_encrypt_final(3) failed: %0x",
1393 tmi
->enc_data
.ctx
= NULL
;
1396 bzero(tmp
, sizeof (tmp
));
1397 bzero(tmp2
, sizeof (tmp
));
1398 bzero(tmp3
, sizeof (tmp
));
1399 bzero(tmi
->enc_data
.block
, tmi
->enc_data
.blocklen
);
1404 aes_cbc_cts_decrypt(struct tmodinfo
*tmi
, uchar_t
*buff
, size_t length
)
1406 int result
= CRYPTO_SUCCESS
;
1407 unsigned char tmp
[DEFAULT_AES_BLOCKLEN
];
1408 unsigned char tmp2
[DEFAULT_AES_BLOCKLEN
];
1409 unsigned char tmp3
[DEFAULT_AES_BLOCKLEN
];
1410 int nblocks
= 0, blockno
;
1411 crypto_data_t ct
, pt
;
1412 crypto_mechanism_t mech
;
1414 mech
.cm_type
= tmi
->enc_data
.mech_type
;
1416 if (tmi
->dec_data
.ivec_usage
!= IVEC_NEVER
&&
1417 tmi
->dec_data
.ivlen
> 0 && tmi
->dec_data
.ivec
!= NULL
) {
1418 bcopy(tmi
->dec_data
.ivec
, tmp
, DEFAULT_AES_BLOCKLEN
);
1420 bzero(tmp
, sizeof (tmp
));
1422 mech
.cm_param_len
= 0;
1423 mech
.cm_param
= NULL
;
1425 nblocks
= (length
+ DEFAULT_AES_BLOCKLEN
- 1) / DEFAULT_AES_BLOCKLEN
;
1427 bzero(&pt
, sizeof (pt
));
1428 bzero(&ct
, sizeof (ct
));
1431 ct
.cd_format
= CRYPTO_DATA_RAW
;
1432 ct
.cd_length
= length
;
1433 ct
.cd_raw
.iov_base
= (char *)buff
;
1434 ct
.cd_raw
.iov_len
= length
;
1436 result
= crypto_decrypt(&mech
, &ct
,
1437 &tmi
->dec_data
.d_encr_key
, NULL
, NULL
, NULL
);
1439 if (result
!= CRYPTO_SUCCESS
) {
1440 cmn_err(CE_WARN
, "aes_cbc_cts_decrypt: "
1441 "crypto_decrypt failed: %0x", result
);
1445 ct
.cd_format
= CRYPTO_DATA_RAW
;
1447 ct
.cd_length
= DEFAULT_AES_BLOCKLEN
;
1449 pt
.cd_format
= CRYPTO_DATA_RAW
;
1451 pt
.cd_length
= DEFAULT_AES_BLOCKLEN
;
1453 result
= crypto_decrypt_init(&mech
,
1454 &tmi
->dec_data
.d_encr_key
,
1455 tmi
->dec_data
.enc_tmpl
,
1456 &tmi
->dec_data
.ctx
, NULL
);
1458 if (result
!= CRYPTO_SUCCESS
) {
1459 cmn_err(CE_WARN
, "aes_cbc_cts_decrypt: "
1460 "crypto_decrypt_init failed: %0x", result
);
1463 for (blockno
= 0; blockno
< nblocks
- 2; blockno
++) {
1464 ct
.cd_raw
.iov_base
= (char *)buff
+
1465 (blockno
* DEFAULT_AES_BLOCKLEN
);
1466 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1468 pt
.cd_raw
.iov_base
= (char *)tmp2
;
1469 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1472 * Save the input to the decrypt so it can
1473 * be used later for an XOR operation
1475 bcopy(buff
+ (blockno
* DEFAULT_AES_BLOCKLEN
),
1476 tmi
->dec_data
.block
, DEFAULT_AES_BLOCKLEN
);
1478 result
= crypto_decrypt_update(tmi
->dec_data
.ctx
,
1480 if (result
!= CRYPTO_SUCCESS
) {
1481 cmn_err(CE_WARN
, "aes_cbc_cts_decrypt: "
1482 "crypto_decrypt_update(1) error - "
1483 "result = 0x%08x", result
);
1486 xorblock(tmp2
, tmp
);
1487 bcopy(tmp2
, buff
+ blockno
* DEFAULT_AES_BLOCKLEN
,
1488 DEFAULT_AES_BLOCKLEN
);
1490 * The original cipher text is used as the xor
1491 * for the next block, save it here.
1493 bcopy(tmi
->dec_data
.block
, tmp
, DEFAULT_AES_BLOCKLEN
);
1495 ct
.cd_raw
.iov_base
= (char *)buff
+
1496 ((nblocks
- 2) * DEFAULT_AES_BLOCKLEN
);
1497 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1498 pt
.cd_raw
.iov_base
= (char *)tmp2
;
1499 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1501 result
= crypto_decrypt_update(tmi
->dec_data
.ctx
,
1503 if (result
!= CRYPTO_SUCCESS
) {
1505 "aes_cbc_cts_decrypt: "
1506 "crypto_decrypt_update(2) error -"
1507 " result = 0x%08x", result
);
1510 bzero(tmp3
, sizeof (tmp3
));
1511 bcopy(buff
+ (nblocks
- 1) * DEFAULT_AES_BLOCKLEN
, tmp3
,
1512 length
- ((nblocks
- 1) * DEFAULT_AES_BLOCKLEN
));
1514 xorblock(tmp2
, tmp3
);
1515 bcopy(tmp2
, buff
+ (nblocks
- 1) * DEFAULT_AES_BLOCKLEN
,
1516 length
- ((nblocks
- 1) * DEFAULT_AES_BLOCKLEN
));
1518 /* 2nd to last block ... */
1520 length
- ((nblocks
- 1) * DEFAULT_AES_BLOCKLEN
));
1522 ct
.cd_raw
.iov_base
= (char *)tmp2
;
1523 ct
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1524 pt
.cd_raw
.iov_base
= (char *)tmp3
;
1525 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1527 result
= crypto_decrypt_update(tmi
->dec_data
.ctx
,
1529 if (result
!= CRYPTO_SUCCESS
) {
1531 "aes_cbc_cts_decrypt: "
1532 "crypto_decrypt_update(3) error - "
1533 "result = 0x%08x", result
);
1536 xorblock(tmp3
, tmp
);
1539 /* Finally, update the 2nd to last block and we are done. */
1540 bcopy(tmp3
, buff
+ (nblocks
- 2) * DEFAULT_AES_BLOCKLEN
,
1541 DEFAULT_AES_BLOCKLEN
);
1543 /* Do Final step, but ignore output */
1544 pt
.cd_raw
.iov_base
= (char *)tmp2
;
1545 pt
.cd_raw
.iov_len
= DEFAULT_AES_BLOCKLEN
;
1546 result
= crypto_decrypt_final(tmi
->dec_data
.ctx
, &pt
, NULL
);
1547 if (result
!= CRYPTO_SUCCESS
) {
1548 cmn_err(CE_WARN
, "aes_cbc_cts_decrypt: "
1549 "crypto_decrypt_final error - "
1550 "result = 0x%0x", result
);
1552 tmi
->dec_data
.ctx
= NULL
;
1556 bzero(tmp
, sizeof (tmp
));
1557 bzero(tmp2
, sizeof (tmp
));
1558 bzero(tmp3
, sizeof (tmp
));
1559 bzero(tmi
->dec_data
.block
, tmi
->dec_data
.blocklen
);
1566 * format of ciphertext when using AES
1567 * +-------------+------------+------------+
1568 * | confounder | msg-data | hmac |
1569 * +-------------+------------+------------+
1572 aes_decrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
,
1578 uchar_t hmacbuff
[64];
1579 uchar_t tmpiv
[DEFAULT_AES_BLOCKLEN
];
1581 inlen
= (size_t)MBLKL(mp
);
1583 enclen
= inlen
- AES_TRUNCATED_HMAC_LEN
;
1584 if (tmi
->dec_data
.ivec_usage
!= IVEC_NEVER
&&
1585 tmi
->dec_data
.ivec
!= NULL
&& tmi
->dec_data
.ivlen
> 0) {
1586 int nblocks
= (enclen
+ DEFAULT_AES_BLOCKLEN
- 1) /
1587 DEFAULT_AES_BLOCKLEN
;
1588 bcopy(mp
->b_rptr
+ DEFAULT_AES_BLOCKLEN
* (nblocks
- 2),
1589 tmpiv
, DEFAULT_AES_BLOCKLEN
);
1593 result
= aes_cbc_cts_decrypt(tmi
, mp
->b_rptr
, enclen
);
1595 if (result
!= CRYPTO_SUCCESS
) {
1597 "aes_decrypt: aes_cbc_cts_decrypt "
1598 "failed - error %0x", result
);
1602 /* Verify the HMAC */
1603 result
= do_hmac(sha1_hmac_mech
,
1604 &tmi
->dec_data
.d_hmac_key
,
1605 (char *)mp
->b_rptr
, enclen
,
1606 (char *)hmacbuff
, hash
->hash_len
);
1608 if (result
!= CRYPTO_SUCCESS
) {
1610 "aes_decrypt: do_hmac failed - error %0x", result
);
1614 if (bcmp(hmacbuff
, mp
->b_rptr
+ enclen
,
1615 AES_TRUNCATED_HMAC_LEN
) != 0) {
1617 cmn_err(CE_WARN
, "aes_decrypt: checksum verification failed");
1621 /* truncate the mblk at the end of the decrypted text */
1622 mp
->b_wptr
= mp
->b_rptr
+ enclen
;
1624 /* Adjust the beginning of the buffer to skip the confounder */
1625 mp
->b_rptr
+= DEFAULT_AES_BLOCKLEN
;
1627 if (tmi
->dec_data
.ivec_usage
!= IVEC_NEVER
&&
1628 tmi
->dec_data
.ivec
!= NULL
&& tmi
->dec_data
.ivlen
> 0)
1629 bcopy(tmpiv
, tmi
->dec_data
.ivec
, DEFAULT_AES_BLOCKLEN
);
1632 if (result
!= CRYPTO_SUCCESS
) {
1633 mp
->b_datap
->db_type
= M_ERROR
;
1634 mp
->b_rptr
= mp
->b_datap
->db_base
;
1636 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
1637 freemsg(mp
->b_cont
);
1648 * format of ciphertext when using AES
1649 * +-------------+------------+------------+
1650 * | confounder | msg-data | hmac |
1651 * +-------------+------------+------------+
1654 aes_encrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
,
1660 uchar_t hmacbuff
[64];
1662 inlen
= (size_t)MBLKL(mp
);
1664 cipherlen
= encrypt_size(&tmi
->enc_data
, inlen
);
1666 ASSERT(MBLKSIZE(mp
) >= cipherlen
);
1669 * Shift the rptr back enough to insert the confounder.
1671 mp
->b_rptr
-= DEFAULT_AES_BLOCKLEN
;
1673 /* Get random data for confounder */
1674 (void) random_get_pseudo_bytes((uint8_t *)mp
->b_rptr
,
1675 DEFAULT_AES_BLOCKLEN
);
1678 * Because we encrypt in-place, we need to calculate
1679 * the HMAC of the plaintext now, then stick it on
1680 * the end of the ciphertext down below.
1682 result
= do_hmac(sha1_hmac_mech
,
1683 &tmi
->enc_data
.d_hmac_key
,
1684 (char *)mp
->b_rptr
, DEFAULT_AES_BLOCKLEN
+ inlen
,
1685 (char *)hmacbuff
, hash
->hash_len
);
1687 if (result
!= CRYPTO_SUCCESS
) {
1688 cmn_err(CE_WARN
, "aes_encrypt: do_hmac failed - error %0x",
1692 /* Encrypt using AES-CBC-CTS */
1693 result
= aes_cbc_cts_encrypt(tmi
, mp
->b_rptr
,
1694 inlen
+ DEFAULT_AES_BLOCKLEN
);
1696 if (result
!= CRYPTO_SUCCESS
) {
1697 cmn_err(CE_WARN
, "aes_encrypt: aes_cbc_cts_encrypt "
1698 "failed - error %0x", result
);
1702 /* copy the truncated HMAC to the end of the mblk */
1703 bcopy(hmacbuff
, mp
->b_rptr
+ DEFAULT_AES_BLOCKLEN
+ inlen
,
1704 AES_TRUNCATED_HMAC_LEN
);
1706 mp
->b_wptr
= mp
->b_rptr
+ cipherlen
;
1709 * The final block of cipher text (not the HMAC) is used
1712 if (tmi
->enc_data
.ivec_usage
!= IVEC_NEVER
&&
1713 tmi
->enc_data
.ivec
!= NULL
) {
1714 int nblocks
= (inlen
+ 2 * DEFAULT_AES_BLOCKLEN
- 1) /
1715 DEFAULT_AES_BLOCKLEN
;
1717 bcopy(mp
->b_rptr
+ (nblocks
- 2) * DEFAULT_AES_BLOCKLEN
,
1718 tmi
->enc_data
.ivec
, DEFAULT_AES_BLOCKLEN
);
1722 if (result
!= CRYPTO_SUCCESS
) {
1723 mp
->b_datap
->db_type
= M_ERROR
;
1724 mp
->b_rptr
= mp
->b_datap
->db_base
;
1726 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
1727 freemsg(mp
->b_cont
);
1736 * ARCFOUR-HMAC-MD5 decrypt
1738 * format of ciphertext when using ARCFOUR-HMAC-MD5
1739 * +-----------+------------+------------+
1740 * | hmac | confounder | msg-data |
1741 * +-----------+------------+------------+
1745 arcfour_hmac_md5_decrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
,
1752 crypto_key_t k1
, k2
;
1753 crypto_data_t indata
;
1755 uchar_t ms_exp
[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
1756 0xab, 0xab, 0xab, 0xab };
1757 uchar_t k1data
[CRYPT_ARCFOUR_KEYBYTES
];
1758 uchar_t k2data
[CRYPT_ARCFOUR_KEYBYTES
];
1759 uchar_t cksum
[MD5_HASHSIZE
];
1760 uchar_t saltdata
[CRYPT_ARCFOUR_KEYBYTES
];
1761 crypto_mechanism_t mech
;
1764 bzero(&indata
, sizeof (indata
));
1766 /* The usage constant is 1026 for all "old" rcmd mode operations */
1767 if (tmi
->dec_data
.option_mask
& CRYPTOPT_RCMD_MODE_V1
)
1768 usage
= RCMDV1_USAGE
;
1770 usage
= ARCFOUR_DECRYPT_USAGE
;
1773 * The size at this point should be the size of
1774 * all the plaintext plus the optional plaintext length
1775 * needed for RCMD V2 mode. There should also be room
1776 * at the head of the mblk for the confounder and hash info.
1778 inlen
= (size_t)MBLKL(mp
);
1781 * The cipherlen does not include the HMAC at the
1782 * head of the buffer.
1784 cipherlen
= inlen
- hash
->hash_len
;
1786 ASSERT(MBLKSIZE(mp
) >= cipherlen
);
1787 if (tmi
->dec_data
.method
== CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
) {
1788 bcopy(ARCFOUR_EXP_SALT
, saltdata
, strlen(ARCFOUR_EXP_SALT
));
1790 saltdata
[10] = usage
& 0xff;
1791 saltdata
[11] = (usage
>> 8) & 0xff;
1792 saltdata
[12] = (usage
>> 16) & 0xff;
1793 saltdata
[13] = (usage
>> 24) & 0xff;
1796 saltdata
[0] = usage
& 0xff;
1797 saltdata
[1] = (usage
>> 8) & 0xff;
1798 saltdata
[2] = (usage
>> 16) & 0xff;
1799 saltdata
[3] = (usage
>> 24) & 0xff;
1803 * Use the salt value to create a key to be used
1804 * for subsequent HMAC operations.
1806 result
= do_hmac(md5_hmac_mech
,
1808 (char *)saltdata
, saltlen
,
1809 (char *)k1data
, sizeof (k1data
));
1810 if (result
!= CRYPTO_SUCCESS
) {
1812 "arcfour_hmac_md5_decrypt: do_hmac(k1)"
1813 "failed - error %0x", result
);
1816 bcopy(k1data
, k2data
, sizeof (k1data
));
1819 * For the neutered MS RC4 encryption type,
1820 * set the trailing 9 bytes to 0xab per the
1823 if (tmi
->dec_data
.method
== CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
) {
1824 bcopy((void *)&k1data
[7], ms_exp
, sizeof (ms_exp
));
1827 mech
.cm_type
= tmi
->dec_data
.mech_type
;
1828 mech
.cm_param
= NULL
;
1829 mech
.cm_param_len
= 0;
1832 * If we have not yet initialized the decryption key,
1833 * context, and template, do it now.
1835 if (tmi
->dec_data
.ctx
== NULL
||
1836 (tmi
->dec_data
.option_mask
& CRYPTOPT_RCMD_MODE_V1
)) {
1837 k1
.ck_format
= CRYPTO_KEY_RAW
;
1838 k1
.ck_length
= CRYPT_ARCFOUR_KEYBYTES
* 8;
1839 k1
.ck_data
= k1data
;
1841 tmi
->dec_data
.d_encr_key
.ck_format
= CRYPTO_KEY_RAW
;
1842 tmi
->dec_data
.d_encr_key
.ck_length
= k1
.ck_length
;
1843 if (tmi
->dec_data
.d_encr_key
.ck_data
== NULL
)
1844 tmi
->dec_data
.d_encr_key
.ck_data
= kmem_zalloc(
1845 CRYPT_ARCFOUR_KEYBYTES
, KM_SLEEP
);
1848 * HMAC operation creates the encryption
1849 * key to be used for the decrypt operations.
1851 result
= do_hmac(md5_hmac_mech
, &k1
,
1852 (char *)mp
->b_rptr
, hash
->hash_len
,
1853 (char *)tmi
->dec_data
.d_encr_key
.ck_data
,
1854 CRYPT_ARCFOUR_KEYBYTES
);
1857 if (result
!= CRYPTO_SUCCESS
) {
1859 "arcfour_hmac_md5_decrypt: do_hmac(k3)"
1860 "failed - error %0x", result
);
1865 tmi
->dec_data
.enc_tmpl
= NULL
;
1867 if (tmi
->dec_data
.ctx
== NULL
&&
1868 (tmi
->dec_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)) {
1870 * Only create a template if we are doing
1871 * chaining from block to block.
1873 result
= crypto_create_ctx_template(&mech
,
1874 &tmi
->dec_data
.d_encr_key
,
1875 &tmi
->dec_data
.enc_tmpl
,
1877 if (result
== CRYPTO_NOT_SUPPORTED
) {
1878 tmi
->dec_data
.enc_tmpl
= NULL
;
1879 } else if (result
!= CRYPTO_SUCCESS
) {
1881 "arcfour_hmac_md5_decrypt: "
1882 "failed to create dec template "
1883 "for RC4 encrypt: %0x", result
);
1887 result
= crypto_decrypt_init(&mech
,
1888 &tmi
->dec_data
.d_encr_key
,
1889 tmi
->dec_data
.enc_tmpl
,
1890 &tmi
->dec_data
.ctx
, NULL
);
1892 if (result
!= CRYPTO_SUCCESS
) {
1893 cmn_err(CE_WARN
, "crypto_decrypt_init failed:"
1899 /* adjust the rptr so we don't decrypt the original hmac field */
1901 v1
.iov_base
= (char *)mp
->b_rptr
+ hash
->hash_len
;
1902 v1
.iov_len
= cipherlen
;
1904 indata
.cd_format
= CRYPTO_DATA_RAW
;
1905 indata
.cd_offset
= 0;
1906 indata
.cd_length
= cipherlen
;
1909 if (tmi
->dec_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)
1910 result
= crypto_decrypt_update(tmi
->dec_data
.ctx
,
1911 &indata
, NULL
, NULL
);
1913 result
= crypto_decrypt(&mech
, &indata
,
1914 &tmi
->dec_data
.d_encr_key
, NULL
, NULL
, NULL
);
1916 if (result
!= CRYPTO_SUCCESS
) {
1917 cmn_err(CE_WARN
, "crypto_decrypt_update failed:"
1922 k2
.ck_format
= CRYPTO_KEY_RAW
;
1923 k2
.ck_length
= sizeof (k2data
) * 8;
1924 k2
.ck_data
= k2data
;
1926 result
= do_hmac(md5_hmac_mech
,
1928 (char *)mp
->b_rptr
+ hash
->hash_len
, cipherlen
,
1929 (char *)cksum
, hash
->hash_len
);
1931 if (result
!= CRYPTO_SUCCESS
) {
1933 "arcfour_hmac_md5_decrypt: do_hmac(k2)"
1934 "failed - error %0x", result
);
1938 if (bcmp(cksum
, mp
->b_rptr
, hash
->hash_len
) != 0) {
1939 cmn_err(CE_WARN
, "arcfour_decrypt HMAC comparison failed");
1945 * adjust the start of the mblk to skip over the
1946 * hash and confounder.
1948 mp
->b_rptr
+= hash
->hash_len
+ hash
->confound_len
;
1951 bzero(k1data
, sizeof (k1data
));
1952 bzero(k2data
, sizeof (k2data
));
1953 bzero(cksum
, sizeof (cksum
));
1954 bzero(saltdata
, sizeof (saltdata
));
1955 if (result
!= CRYPTO_SUCCESS
) {
1956 mp
->b_datap
->db_type
= M_ERROR
;
1957 mp
->b_rptr
= mp
->b_datap
->db_base
;
1959 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
1960 freemsg(mp
->b_cont
);
1969 * ARCFOUR-HMAC-MD5 encrypt
1971 * format of ciphertext when using ARCFOUR-HMAC-MD5
1972 * +-----------+------------+------------+
1973 * | hmac | confounder | msg-data |
1974 * +-----------+------------+------------+
1978 arcfour_hmac_md5_encrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
,
1985 crypto_key_t k1
, k2
;
1986 crypto_data_t indata
;
1988 uchar_t ms_exp
[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
1989 0xab, 0xab, 0xab, 0xab };
1990 uchar_t k1data
[CRYPT_ARCFOUR_KEYBYTES
];
1991 uchar_t k2data
[CRYPT_ARCFOUR_KEYBYTES
];
1992 uchar_t saltdata
[CRYPT_ARCFOUR_KEYBYTES
];
1993 crypto_mechanism_t mech
;
1996 bzero(&indata
, sizeof (indata
));
1998 /* The usage constant is 1026 for all "old" rcmd mode operations */
1999 if (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V1
)
2000 usage
= RCMDV1_USAGE
;
2002 usage
= ARCFOUR_ENCRYPT_USAGE
;
2004 mech
.cm_type
= tmi
->enc_data
.mech_type
;
2005 mech
.cm_param
= NULL
;
2006 mech
.cm_param_len
= 0;
2009 * The size at this point should be the size of
2010 * all the plaintext plus the optional plaintext length
2011 * needed for RCMD V2 mode. There should also be room
2012 * at the head of the mblk for the confounder and hash info.
2014 inlen
= (size_t)MBLKL(mp
);
2016 cipherlen
= encrypt_size(&tmi
->enc_data
, inlen
);
2018 ASSERT(MBLKSIZE(mp
) >= cipherlen
);
2021 * Shift the rptr back enough to insert
2022 * the confounder and hash.
2024 mp
->b_rptr
-= (hash
->confound_len
+ hash
->hash_len
);
2026 /* zero out the hash area */
2027 bzero(mp
->b_rptr
, (size_t)hash
->hash_len
);
2029 if (cipherlen
> inlen
) {
2030 bzero(mp
->b_wptr
, MBLKTAIL(mp
));
2033 if (tmi
->enc_data
.method
== CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
) {
2034 bcopy(ARCFOUR_EXP_SALT
, saltdata
, strlen(ARCFOUR_EXP_SALT
));
2036 saltdata
[10] = usage
& 0xff;
2037 saltdata
[11] = (usage
>> 8) & 0xff;
2038 saltdata
[12] = (usage
>> 16) & 0xff;
2039 saltdata
[13] = (usage
>> 24) & 0xff;
2042 saltdata
[0] = usage
& 0xff;
2043 saltdata
[1] = (usage
>> 8) & 0xff;
2044 saltdata
[2] = (usage
>> 16) & 0xff;
2045 saltdata
[3] = (usage
>> 24) & 0xff;
2049 * Use the salt value to create a key to be used
2050 * for subsequent HMAC operations.
2052 result
= do_hmac(md5_hmac_mech
,
2054 (char *)saltdata
, saltlen
,
2055 (char *)k1data
, sizeof (k1data
));
2056 if (result
!= CRYPTO_SUCCESS
) {
2058 "arcfour_hmac_md5_encrypt: do_hmac(k1)"
2059 "failed - error %0x", result
);
2063 bcopy(k1data
, k2data
, sizeof (k2data
));
2066 * For the neutered MS RC4 encryption type,
2067 * set the trailing 9 bytes to 0xab per the
2070 if (tmi
->enc_data
.method
== CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
) {
2071 bcopy((void *)&k1data
[7], ms_exp
, sizeof (ms_exp
));
2075 * Get the confounder bytes.
2077 (void) random_get_pseudo_bytes(
2078 (uint8_t *)(mp
->b_rptr
+ hash
->hash_len
),
2079 (size_t)hash
->confound_len
);
2081 k2
.ck_data
= k2data
;
2082 k2
.ck_format
= CRYPTO_KEY_RAW
;
2083 k2
.ck_length
= sizeof (k2data
) * 8;
2086 * This writes the HMAC to the hash area in the
2087 * mblk. The key used is the one just created by
2088 * the previous HMAC operation.
2089 * The data being processed is the confounder bytes
2090 * PLUS the input plaintext.
2092 result
= do_hmac(md5_hmac_mech
, &k2
,
2093 (char *)mp
->b_rptr
+ hash
->hash_len
,
2094 hash
->confound_len
+ inlen
,
2095 (char *)mp
->b_rptr
, hash
->hash_len
);
2096 if (result
!= CRYPTO_SUCCESS
) {
2098 "arcfour_hmac_md5_encrypt: do_hmac(k2)"
2099 "failed - error %0x", result
);
2103 * Because of the odd way that MIT uses RC4 keys
2104 * on the rlogin stream, we only need to create
2106 * However, if using "old" rcmd mode, we need to do
2109 if (tmi
->enc_data
.ctx
== NULL
||
2110 (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V1
)) {
2111 crypto_key_t
*key
= &tmi
->enc_data
.d_encr_key
;
2113 k1
.ck_data
= k1data
;
2114 k1
.ck_format
= CRYPTO_KEY_RAW
;
2115 k1
.ck_length
= sizeof (k1data
) * 8;
2117 key
->ck_format
= CRYPTO_KEY_RAW
;
2118 key
->ck_length
= k1
.ck_length
;
2119 if (key
->ck_data
== NULL
)
2120 key
->ck_data
= kmem_zalloc(
2121 CRYPT_ARCFOUR_KEYBYTES
, KM_SLEEP
);
2124 * The final HMAC operation creates the encryption
2125 * key to be used for the encrypt operation.
2127 result
= do_hmac(md5_hmac_mech
, &k1
,
2128 (char *)mp
->b_rptr
, hash
->hash_len
,
2129 (char *)key
->ck_data
, CRYPT_ARCFOUR_KEYBYTES
);
2131 if (result
!= CRYPTO_SUCCESS
) {
2133 "arcfour_hmac_md5_encrypt: do_hmac(k3)"
2134 "failed - error %0x", result
);
2140 * If the context has not been initialized, do it now.
2142 if (tmi
->enc_data
.ctx
== NULL
&&
2143 (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)) {
2145 * Only create a template if we are doing
2146 * chaining from block to block.
2148 result
= crypto_create_ctx_template(&mech
,
2149 &tmi
->enc_data
.d_encr_key
,
2150 &tmi
->enc_data
.enc_tmpl
,
2152 if (result
== CRYPTO_NOT_SUPPORTED
) {
2153 tmi
->enc_data
.enc_tmpl
= NULL
;
2154 } else if (result
!= CRYPTO_SUCCESS
) {
2155 cmn_err(CE_WARN
, "failed to create enc template "
2156 "for RC4 encrypt: %0x", result
);
2160 result
= crypto_encrypt_init(&mech
,
2161 &tmi
->enc_data
.d_encr_key
,
2162 tmi
->enc_data
.enc_tmpl
,
2163 &tmi
->enc_data
.ctx
, NULL
);
2164 if (result
!= CRYPTO_SUCCESS
) {
2165 cmn_err(CE_WARN
, "crypto_encrypt_init failed:"
2170 v1
.iov_base
= (char *)mp
->b_rptr
+ hash
->hash_len
;
2171 v1
.iov_len
= hash
->confound_len
+ inlen
;
2173 indata
.cd_format
= CRYPTO_DATA_RAW
;
2174 indata
.cd_offset
= 0;
2175 indata
.cd_length
= hash
->confound_len
+ inlen
;
2178 if (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)
2179 result
= crypto_encrypt_update(tmi
->enc_data
.ctx
,
2180 &indata
, NULL
, NULL
);
2182 result
= crypto_encrypt(&mech
, &indata
,
2183 &tmi
->enc_data
.d_encr_key
, NULL
,
2186 if (result
!= CRYPTO_SUCCESS
) {
2187 cmn_err(CE_WARN
, "crypto_encrypt_update failed: 0x%0x",
2192 bzero(k1data
, sizeof (k1data
));
2193 bzero(k2data
, sizeof (k2data
));
2194 bzero(saltdata
, sizeof (saltdata
));
2195 if (result
!= CRYPTO_SUCCESS
) {
2196 mp
->b_datap
->db_type
= M_ERROR
;
2197 mp
->b_rptr
= mp
->b_datap
->db_base
;
2199 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
2200 freemsg(mp
->b_cont
);
2209 * DES-CBC-[HASH] encrypt
2211 * Needed to support userland apps that must support Kerberos V5
2212 * encryption DES-CBC encryption modes.
2214 * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
2216 * format of ciphertext for DES-CBC functions, per RFC1510 is:
2217 * +-----------+----------+-------------+-----+
2218 * |confounder | cksum | msg-data | pad |
2219 * +-----------+----------+-------------+-----+
2221 * format of ciphertext when using DES3-SHA1-HMAC
2222 * +-----------+----------+-------------+-----+
2223 * |confounder | msg-data | hmac | pad |
2224 * +-----------+----------+-------------+-----+
2226 * The confounder is 8 bytes of random data.
2227 * The cksum depends on the hash being used.
2235 des_cbc_encrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
, hash_info_t
*hash
)
2243 * The size at this point should be the size of
2244 * all the plaintext plus the optional plaintext length
2245 * needed for RCMD V2 mode. There should also be room
2246 * at the head of the mblk for the confounder and hash info.
2248 inlen
= (size_t)MBLKL(mp
);
2251 * The output size will be a multiple of 8 because this algorithm
2252 * only works on 8 byte chunks.
2254 cipherlen
= encrypt_size(&tmi
->enc_data
, inlen
);
2256 ASSERT(MBLKSIZE(mp
) >= cipherlen
);
2258 if (cipherlen
> inlen
) {
2259 bzero(mp
->b_wptr
, MBLKTAIL(mp
));
2263 * Shift the rptr back enough to insert
2264 * the confounder and hash.
2266 if (tmi
->enc_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
2267 mp
->b_rptr
-= hash
->confound_len
;
2269 mp
->b_rptr
-= (hash
->confound_len
+ hash
->hash_len
);
2271 /* zero out the hash area */
2272 bzero(mp
->b_rptr
+ hash
->confound_len
, (size_t)hash
->hash_len
);
2275 /* get random confounder from our friend, the 'random' module */
2276 if (hash
->confound_len
> 0) {
2277 (void) random_get_pseudo_bytes((uint8_t *)mp
->b_rptr
,
2278 (size_t)hash
->confound_len
);
2282 * For 3DES we calculate an HMAC later.
2284 if (tmi
->enc_data
.method
!= CRYPT_METHOD_DES3_CBC_SHA1
) {
2285 /* calculate chksum of confounder + input */
2286 if (hash
->hash_len
> 0 && hash
->hashfunc
!= NULL
) {
2287 uchar_t cksum
[MAX_CKSUM_LEN
];
2289 result
= hash
->hashfunc(cksum
, mp
->b_rptr
,
2291 if (result
!= CRYPTO_SUCCESS
) {
2295 /* put hash in place right after the confounder */
2296 bcopy(cksum
, (mp
->b_rptr
+ hash
->confound_len
),
2297 (size_t)hash
->hash_len
);
2301 * In order to support the "old" Kerberos RCMD protocol,
2302 * we must use the IVEC 3 different ways:
2303 * IVEC_REUSE = keep using the same IV each time, this is
2304 * ugly and insecure, but necessary for
2305 * backwards compatibility with existing MIT code.
2306 * IVEC_ONETIME = Use the ivec as initialized when the crypto
2307 * was setup (see setup_crypto routine).
2308 * IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
2310 if (tmi
->enc_data
.ivec_usage
== IVEC_NEVER
) {
2311 bzero(tmi
->enc_data
.block
, tmi
->enc_data
.blocklen
);
2312 } else if (tmi
->enc_data
.ivec_usage
== IVEC_REUSE
) {
2313 bcopy(tmi
->enc_data
.ivec
, tmi
->enc_data
.block
,
2314 tmi
->enc_data
.blocklen
);
2317 if (tmi
->enc_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
2319 * The input length already included the hash size,
2320 * don't include this in the plaintext length
2323 plainlen
= cipherlen
- hash
->hash_len
;
2325 mp
->b_wptr
= mp
->b_rptr
+ plainlen
;
2327 result
= kef_encr_hmac(&tmi
->enc_data
,
2328 (void *)mp
, (size_t)plainlen
,
2329 (char *)(mp
->b_rptr
+ plainlen
),
2332 ASSERT(mp
->b_rptr
+ cipherlen
<= DB_LIM(mp
));
2333 mp
->b_wptr
= mp
->b_rptr
+ cipherlen
;
2334 result
= kef_crypt(&tmi
->enc_data
, (void *)mp
,
2335 CRYPTO_DATA_MBLK
, (size_t)cipherlen
,
2339 if (result
!= CRYPTO_SUCCESS
) {
2342 "des_cbc_encrypt: kef_crypt encrypt "
2343 "failed (len: %ld) - error %0x",
2346 mp
->b_datap
->db_type
= M_ERROR
;
2347 mp
->b_rptr
= mp
->b_datap
->db_base
;
2349 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
2350 freemsg(mp
->b_cont
);
2354 } else if (tmi
->enc_data
.ivec_usage
== IVEC_ONETIME
) {
2356 * Because we are using KEF, we must manually
2359 bcopy(mp
->b_wptr
- tmi
->enc_data
.ivlen
,
2360 tmi
->enc_data
.block
, tmi
->enc_data
.ivlen
);
2362 if (tmi
->enc_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
2363 mp
->b_wptr
= mp
->b_rptr
+ cipherlen
;
2373 * Needed to support userland apps that must support Kerberos V5
2374 * encryption DES-CBC decryption modes.
2376 * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
2378 * format of ciphertext for DES-CBC functions, per RFC1510 is:
2379 * +-----------+----------+-------------+-----+
2380 * |confounder | cksum | msg-data | pad |
2381 * +-----------+----------+-------------+-----+
2383 * format of ciphertext when using DES3-SHA1-HMAC
2384 * +-----------+----------+-------------+-----+
2385 * |confounder | msg-data | hmac | pad |
2386 * +-----------+----------+-------------+-----+
2388 * The confounder is 8 bytes of random data.
2389 * The cksum depends on the hash being used.
2397 des_cbc_decrypt(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
, hash_info_t
*hash
)
2399 uint_t inlen
, datalen
;
2401 uchar_t
*optr
= NULL
;
2402 uchar_t cksum
[MAX_CKSUM_LEN
], newcksum
[MAX_CKSUM_LEN
];
2403 uchar_t nextiv
[DEFAULT_DES_BLOCKLEN
];
2405 /* Compute adjusted size */
2411 * In order to support the "old" Kerberos RCMD protocol,
2412 * we must use the IVEC 3 different ways:
2413 * IVEC_REUSE = keep using the same IV each time, this is
2414 * ugly and insecure, but necessary for
2415 * backwards compatibility with existing MIT code.
2416 * IVEC_ONETIME = Use the ivec as initialized when the crypto
2417 * was setup (see setup_crypto routine).
2418 * IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
2420 if (tmi
->dec_data
.ivec_usage
== IVEC_NEVER
)
2421 bzero(tmi
->dec_data
.block
, tmi
->dec_data
.blocklen
);
2422 else if (tmi
->dec_data
.ivec_usage
== IVEC_REUSE
)
2423 bcopy(tmi
->dec_data
.ivec
, tmi
->dec_data
.block
,
2424 tmi
->dec_data
.blocklen
);
2426 if (tmi
->dec_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
2428 * Do not decrypt the HMAC at the end
2430 int decrypt_len
= inlen
- hash
->hash_len
;
2433 * Move the wptr so the mblk appears to end
2434 * BEFORE the HMAC section.
2436 mp
->b_wptr
= mp
->b_rptr
+ decrypt_len
;
2439 * Because we are using KEF, we must manually update our
2442 if (tmi
->dec_data
.ivec_usage
== IVEC_ONETIME
) {
2443 bcopy(mp
->b_rptr
+ decrypt_len
- tmi
->dec_data
.ivlen
,
2444 nextiv
, tmi
->dec_data
.ivlen
);
2447 result
= kef_decr_hmac(&tmi
->dec_data
, mp
, decrypt_len
,
2448 (char *)newcksum
, hash
->hash_len
);
2451 * Because we are using KEF, we must manually update our
2454 if (tmi
->dec_data
.ivec_usage
== IVEC_ONETIME
) {
2455 bcopy(mp
->b_wptr
- tmi
->enc_data
.ivlen
, nextiv
,
2456 tmi
->dec_data
.ivlen
);
2458 result
= kef_crypt(&tmi
->dec_data
, (void *)mp
,
2459 CRYPTO_DATA_MBLK
, (size_t)inlen
, CRYPT_DECRYPT
);
2461 if (result
!= CRYPTO_SUCCESS
) {
2464 "des_cbc_decrypt: kef_crypt decrypt "
2465 "failed - error %0x", result
);
2467 mp
->b_datap
->db_type
= M_ERROR
;
2468 mp
->b_rptr
= mp
->b_datap
->db_base
;
2470 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
2471 freemsg(mp
->b_cont
);
2478 * Manually update the IV, KEF does not track this for us.
2480 if (tmi
->dec_data
.ivec_usage
== IVEC_ONETIME
) {
2481 bcopy(nextiv
, tmi
->dec_data
.block
, tmi
->dec_data
.ivlen
);
2484 /* Verify the checksum(if necessary) */
2485 if (hash
->hash_len
> 0) {
2486 if (tmi
->dec_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
) {
2487 bcopy(mp
->b_rptr
+ inlen
- hash
->hash_len
, cksum
,
2490 bcopy(optr
+ hash
->confound_len
, cksum
, hash
->hash_len
);
2492 /* zero the cksum in the buffer */
2493 ASSERT(optr
+ hash
->confound_len
+ hash
->hash_len
<=
2495 bzero(optr
+ hash
->confound_len
, hash
->hash_len
);
2497 /* calculate MD5 chksum of confounder + input */
2498 if (hash
->hashfunc
) {
2499 (void) hash
->hashfunc(newcksum
, optr
, inlen
);
2503 if (bcmp(cksum
, newcksum
, hash
->hash_len
)) {
2505 cmn_err(CE_WARN
, "des_cbc_decrypt: checksum "
2506 "verification failed");
2508 mp
->b_datap
->db_type
= M_ERROR
;
2509 mp
->b_rptr
= mp
->b_datap
->db_base
;
2511 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
2512 freemsg(mp
->b_cont
);
2519 datalen
= inlen
- hash
->confound_len
- hash
->hash_len
;
2521 /* Move just the decrypted input into place if necessary */
2522 if (hash
->confound_len
> 0 || hash
->hash_len
> 0) {
2523 if (tmi
->dec_data
.method
== CRYPT_METHOD_DES3_CBC_SHA1
)
2524 mp
->b_rptr
+= hash
->confound_len
;
2526 mp
->b_rptr
+= hash
->confound_len
+ hash
->hash_len
;
2529 ASSERT(mp
->b_rptr
+ datalen
<= DB_LIM(mp
));
2530 mp
->b_wptr
= mp
->b_rptr
+ datalen
;
2536 do_decrypt(queue_t
*q
, mblk_t
*mp
)
2538 struct tmodinfo
*tmi
= (struct tmodinfo
*)q
->q_ptr
;
2541 switch (tmi
->dec_data
.method
) {
2542 case CRYPT_METHOD_DES_CFB
:
2543 outmp
= des_cfb_decrypt(q
, tmi
, mp
);
2545 case CRYPT_METHOD_NONE
:
2548 case CRYPT_METHOD_DES_CBC_NULL
:
2549 outmp
= des_cbc_decrypt(q
, tmi
, mp
, &null_hash
);
2551 case CRYPT_METHOD_DES_CBC_MD5
:
2552 outmp
= des_cbc_decrypt(q
, tmi
, mp
, &md5_hash
);
2554 case CRYPT_METHOD_DES_CBC_CRC
:
2555 outmp
= des_cbc_decrypt(q
, tmi
, mp
, &crc32_hash
);
2557 case CRYPT_METHOD_DES3_CBC_SHA1
:
2558 outmp
= des_cbc_decrypt(q
, tmi
, mp
, &sha1_hash
);
2560 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
2561 case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
:
2562 outmp
= arcfour_hmac_md5_decrypt(q
, tmi
, mp
, &md5_hash
);
2564 case CRYPT_METHOD_AES128
:
2565 case CRYPT_METHOD_AES256
:
2566 outmp
= aes_decrypt(q
, tmi
, mp
, &sha1_hash
);
2575 * Generic encryption routine for a single message block.
2576 * The input mblk may be replaced by some encrypt routines
2577 * because they add extra data in some cases that may exceed
2578 * the input mblk_t size limit.
2581 do_encrypt(queue_t
*q
, mblk_t
*mp
)
2583 struct tmodinfo
*tmi
= (struct tmodinfo
*)q
->q_ptr
;
2586 switch (tmi
->enc_data
.method
) {
2587 case CRYPT_METHOD_DES_CFB
:
2588 outmp
= des_cfb_encrypt(q
, tmi
, mp
);
2590 case CRYPT_METHOD_DES_CBC_NULL
:
2591 outmp
= des_cbc_encrypt(q
, tmi
, mp
, &null_hash
);
2593 case CRYPT_METHOD_DES_CBC_MD5
:
2594 outmp
= des_cbc_encrypt(q
, tmi
, mp
, &md5_hash
);
2596 case CRYPT_METHOD_DES_CBC_CRC
:
2597 outmp
= des_cbc_encrypt(q
, tmi
, mp
, &crc32_hash
);
2599 case CRYPT_METHOD_DES3_CBC_SHA1
:
2600 outmp
= des_cbc_encrypt(q
, tmi
, mp
, &sha1_hash
);
2602 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
2603 case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
:
2604 outmp
= arcfour_hmac_md5_encrypt(q
, tmi
, mp
, &md5_hash
);
2606 case CRYPT_METHOD_AES128
:
2607 case CRYPT_METHOD_AES256
:
2608 outmp
= aes_encrypt(q
, tmi
, mp
, &sha1_hash
);
2610 case CRYPT_METHOD_NONE
:
2620 * This takes the data from the CRYPTIOCSETUP ioctl
2621 * and sets up a cipher_data_t structure for either
2622 * encryption or decryption. This is where the
2623 * key and initialization vector data get stored
2624 * prior to beginning any crypto functions.
2627 * Some applications(e.g. telnetd) have ability to switch
2628 * crypto on/off periodically. Thus, the application may call
2629 * the CRYPTIOCSETUP ioctl many times for the same stream.
2630 * If the CRYPTIOCSETUP is called with 0 length key or ivec fields
2631 * assume that the key, block, and saveblock fields that are already
2632 * set from a previous CRIOCSETUP call are still valid. This helps avoid
2633 * a rekeying error that could occur if we overwrite these fields
2634 * with each CRYPTIOCSETUP call.
2635 * In short, sometimes, CRYPTIOCSETUP is used to simply toggle on/off
2636 * without resetting the original crypto parameters.
2640 setup_crypto(struct cr_info_t
*ci
, struct cipher_data_t
*cd
, int encrypt
)
2643 uint32_t enc_usage
= 0, dec_usage
= 0;
2647 * Initial sanity checks
2649 if (!CR_METHOD_OK(ci
->crypto_method
)) {
2650 cmn_err(CE_WARN
, "Illegal crypto method (%d)",
2654 if (!CR_OPTIONS_OK(ci
->option_mask
)) {
2655 cmn_err(CE_WARN
, "Illegal crypto options (%d)",
2659 if (!CR_IVUSAGE_OK(ci
->ivec_usage
)) {
2660 cmn_err(CE_WARN
, "Illegal ivec usage value (%d)",
2665 cd
->method
= ci
->crypto_method
;
2668 if (ci
->keylen
> 0) {
2669 if (cd
->key
!= NULL
) {
2670 kmem_free(cd
->key
, cd
->keylen
);
2675 * cd->key holds the copy of the raw key bytes passed in
2676 * from the userland app.
2678 cd
->key
= (char *)kmem_alloc((size_t)ci
->keylen
, KM_SLEEP
);
2680 cd
->keylen
= ci
->keylen
;
2681 bcopy(ci
->key
, cd
->key
, (size_t)ci
->keylen
);
2685 * Configure the block size based on the type of cipher.
2687 switch (cd
->method
) {
2688 case CRYPT_METHOD_NONE
:
2691 case CRYPT_METHOD_DES_CFB
:
2692 newblocklen
= DEFAULT_DES_BLOCKLEN
;
2693 cd
->mech_type
= crypto_mech2id(SUN_CKM_DES_ECB
);
2695 case CRYPT_METHOD_DES_CBC_NULL
:
2696 case CRYPT_METHOD_DES_CBC_MD5
:
2697 case CRYPT_METHOD_DES_CBC_CRC
:
2698 newblocklen
= DEFAULT_DES_BLOCKLEN
;
2699 cd
->mech_type
= crypto_mech2id(SUN_CKM_DES_CBC
);
2701 case CRYPT_METHOD_DES3_CBC_SHA1
:
2702 newblocklen
= DEFAULT_DES_BLOCKLEN
;
2703 cd
->mech_type
= crypto_mech2id(SUN_CKM_DES3_CBC
);
2704 /* 3DES always uses the old usage constant */
2705 enc_usage
= RCMDV1_USAGE
;
2706 dec_usage
= RCMDV1_USAGE
;
2708 case CRYPT_METHOD_ARCFOUR_HMAC_MD5
:
2709 case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP
:
2711 cd
->mech_type
= crypto_mech2id(SUN_CKM_RC4
);
2713 case CRYPT_METHOD_AES128
:
2714 case CRYPT_METHOD_AES256
:
2715 newblocklen
= DEFAULT_AES_BLOCKLEN
;
2716 cd
->mech_type
= crypto_mech2id(SUN_CKM_AES_ECB
);
2717 enc_usage
= AES_ENCRYPT_USAGE
;
2718 dec_usage
= AES_DECRYPT_USAGE
;
2721 if (cd
->mech_type
== CRYPTO_MECH_INVALID
) {
2722 return (CRYPTO_FAILED
);
2726 * If RC4, initialize the master crypto key used by
2727 * the RC4 algorithm to derive the final encrypt and decrypt keys.
2729 if (cd
->keylen
> 0 && IS_RC4_METHOD(cd
->method
)) {
2731 * cd->ckey is a kernel crypto key structure used as the
2732 * master key in the RC4-HMAC crypto operations.
2734 if (cd
->ckey
== NULL
) {
2735 cd
->ckey
= (crypto_key_t
*)kmem_zalloc(
2736 sizeof (crypto_key_t
), KM_SLEEP
);
2739 cd
->ckey
->ck_format
= CRYPTO_KEY_RAW
;
2740 cd
->ckey
->ck_data
= cd
->key
;
2742 /* key length for EF is measured in bits */
2743 cd
->ckey
->ck_length
= cd
->keylen
* 8;
2747 * cd->block and cd->saveblock are used as temporary storage for
2748 * data that must be carried over between encrypt/decrypt operations
2749 * in some of the "feedback" modes.
2751 if (newblocklen
!= cd
->blocklen
) {
2752 if (cd
->block
!= NULL
) {
2753 kmem_free(cd
->block
, cd
->blocklen
);
2757 if (cd
->saveblock
!= NULL
) {
2758 kmem_free(cd
->saveblock
, cd
->blocklen
);
2759 cd
->saveblock
= NULL
;
2762 cd
->blocklen
= newblocklen
;
2764 cd
->block
= (char *)kmem_zalloc((size_t)cd
->blocklen
,
2768 if (cd
->method
== CRYPT_METHOD_DES_CFB
)
2769 cd
->saveblock
= (char *)kmem_zalloc(cd
->blocklen
,
2772 cd
->saveblock
= NULL
;
2775 if (ci
->iveclen
!= cd
->ivlen
) {
2776 if (cd
->ivec
!= NULL
) {
2777 kmem_free(cd
->ivec
, cd
->ivlen
);
2780 if (ci
->ivec_usage
!= IVEC_NEVER
&& ci
->iveclen
> 0) {
2781 cd
->ivec
= (char *)kmem_zalloc((size_t)ci
->iveclen
,
2783 cd
->ivlen
= ci
->iveclen
;
2789 cd
->option_mask
= ci
->option_mask
;
2792 * Old protocol requires a static 'usage' value for
2793 * deriving keys. Yuk.
2795 if (cd
->option_mask
& CRYPTOPT_RCMD_MODE_V1
) {
2796 enc_usage
= dec_usage
= RCMDV1_USAGE
;
2799 if (cd
->ivlen
> cd
->blocklen
) {
2800 cmn_err(CE_WARN
, "setup_crypto: IV longer than block size");
2805 * If we are using an IVEC "correctly" (i.e. set it once)
2808 if (ci
->ivec_usage
== IVEC_ONETIME
&& cd
->block
!= NULL
)
2809 bcopy(ci
->ivec
, cd
->block
, (size_t)cd
->ivlen
);
2811 cd
->ivec_usage
= ci
->ivec_usage
;
2812 if (cd
->ivec
!= NULL
) {
2813 /* Save the original IVEC in case we need it later */
2814 bcopy(ci
->ivec
, cd
->ivec
, (size_t)cd
->ivlen
);
2817 * Special handling for 3DES-SHA1-HMAC and AES crypto:
2818 * generate derived keys and context templates
2819 * for better performance.
2821 if (cd
->method
== CRYPT_METHOD_DES3_CBC_SHA1
||
2822 IS_AES_METHOD(cd
->method
)) {
2823 crypto_mechanism_t enc_mech
;
2824 crypto_mechanism_t hmac_mech
;
2826 if (cd
->d_encr_key
.ck_data
!= NULL
) {
2827 bzero(cd
->d_encr_key
.ck_data
, cd
->keylen
);
2828 kmem_free(cd
->d_encr_key
.ck_data
, cd
->keylen
);
2831 if (cd
->d_hmac_key
.ck_data
!= NULL
) {
2832 bzero(cd
->d_hmac_key
.ck_data
, cd
->keylen
);
2833 kmem_free(cd
->d_hmac_key
.ck_data
, cd
->keylen
);
2836 if (cd
->enc_tmpl
!= NULL
)
2837 (void) crypto_destroy_ctx_template(cd
->enc_tmpl
);
2839 if (cd
->hmac_tmpl
!= NULL
)
2840 (void) crypto_destroy_ctx_template(cd
->hmac_tmpl
);
2842 enc_mech
.cm_type
= cd
->mech_type
;
2843 enc_mech
.cm_param
= cd
->ivec
;
2844 enc_mech
.cm_param_len
= cd
->ivlen
;
2846 hmac_mech
.cm_type
= sha1_hmac_mech
;
2847 hmac_mech
.cm_param
= NULL
;
2848 hmac_mech
.cm_param_len
= 0;
2851 * Create the derived keys.
2853 rv
= create_derived_keys(cd
,
2854 (encrypt
? enc_usage
: dec_usage
),
2855 &cd
->d_encr_key
, &cd
->d_hmac_key
);
2857 if (rv
!= CRYPTO_SUCCESS
) {
2858 cmn_err(CE_WARN
, "failed to create derived "
2860 return (CRYPTO_FAILED
);
2863 rv
= crypto_create_ctx_template(&enc_mech
,
2865 &cd
->enc_tmpl
, KM_SLEEP
);
2866 if (rv
== CRYPTO_MECH_NOT_SUPPORTED
) {
2867 cd
->enc_tmpl
= NULL
;
2868 } else if (rv
!= CRYPTO_SUCCESS
) {
2869 cmn_err(CE_WARN
, "failed to create enc template "
2870 "for d_encr_key: %0x", rv
);
2871 return (CRYPTO_FAILED
);
2874 rv
= crypto_create_ctx_template(&hmac_mech
,
2876 &cd
->hmac_tmpl
, KM_SLEEP
);
2877 if (rv
== CRYPTO_MECH_NOT_SUPPORTED
) {
2878 cd
->hmac_tmpl
= NULL
;
2879 } else if (rv
!= CRYPTO_SUCCESS
) {
2880 cmn_err(CE_WARN
, "failed to create hmac template:"
2882 return (CRYPTO_FAILED
);
2884 } else if (IS_RC4_METHOD(cd
->method
)) {
2885 bzero(&cd
->d_encr_key
, sizeof (crypto_key_t
));
2886 bzero(&cd
->d_hmac_key
, sizeof (crypto_key_t
));
2888 cd
->enc_tmpl
= NULL
;
2889 cd
->hmac_tmpl
= NULL
;
2892 /* Final sanity checks, make sure no fields are NULL */
2893 if (cd
->method
!= CRYPT_METHOD_NONE
) {
2894 if (cd
->block
== NULL
&& cd
->blocklen
> 0) {
2897 "setup_crypto: IV block not allocated");
2901 if (cd
->key
== NULL
&& cd
->keylen
> 0) {
2904 "setup_crypto: key block not allocated");
2908 if (cd
->method
== CRYPT_METHOD_DES_CFB
&&
2909 cd
->saveblock
== NULL
&& cd
->blocklen
> 0) {
2912 "setup_crypto: save block not allocated");
2916 if (cd
->ivec
== NULL
&& cd
->ivlen
> 0) {
2919 "setup_crypto: IV not allocated");
2928 * RCMDS require a 4 byte, clear text
2929 * length field before each message.
2933 mklenmp(mblk_t
*bp
, uint32_t len
)
2938 if (bp
->b_rptr
- 4 < DB_BASE(bp
) || DB_REF(bp
) > 1) {
2939 lenmp
= allocb(4, BPRI_MED
);
2940 if (lenmp
!= NULL
) {
2941 lenmp
->b_rptr
= lenmp
->b_wptr
= DB_LIM(lenmp
);
2958 encrypt_block(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
, size_t plainlen
)
2966 uint32_t ptlen
= (uint32_t)plainlen
;
2968 * If we are using the "NEW" RCMD mode,
2969 * add 4 bytes to the plaintext for the
2970 * plaintext length that gets prepended
2971 * before encrypting.
2973 if (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)
2976 cipherlen
= encrypt_size(&tmi
->enc_data
, (size_t)ptlen
);
2979 * if we must allocb, then make sure its enough
2980 * to hold the length field so we dont have to allocb
2981 * again down below in 'mklenmp'
2983 if (ANY_RCMD_MODE(tmi
->enc_data
.option_mask
)) {
2984 extra
= sizeof (uint32_t);
2988 * Calculate how much space is needed in front of
2991 headspace
= plaintext_offset(&tmi
->enc_data
);
2994 * If the current block is too small, reallocate
2995 * one large enough to hold the hdr, tail, and
2998 if ((cipherlen
+ extra
>= MBLKSIZE(mp
)) || DB_REF(mp
) > 1) {
2999 int sz
= P2ROUNDUP(cipherlen
+extra
, 8);
3001 cbp
= allocb_tmpl(sz
, mp
);
3004 "allocb (%d bytes) failed", sz
);
3008 cbp
->b_cont
= mp
->b_cont
;
3011 * headspace includes the length fields needed
3012 * for the RCMD modes (v1 == 4 bytes, V2 = 8)
3014 ASSERT(cbp
->b_rptr
+ P2ROUNDUP(plainlen
+headspace
, 8)
3017 cbp
->b_rptr
= DB_BASE(cbp
) + headspace
;
3018 bcopy(mp
->b_rptr
, cbp
->b_rptr
, plainlen
);
3019 cbp
->b_wptr
= cbp
->b_rptr
+ plainlen
;
3027 * Some ciphers add HMAC after the final block
3028 * of the ciphertext, not at the beginning like the
3031 if (tmi
->enc_data
.method
==
3032 CRYPT_METHOD_DES3_CBC_SHA1
||
3033 IS_AES_METHOD(tmi
->enc_data
.method
)) {
3034 extra
= sha1_hash
.hash_len
;
3038 * Make sure the rptr is positioned correctly so that
3039 * routines later do not have to shift this data around
3041 if ((cbp
->b_rptr
+ P2ROUNDUP(cipherlen
+ extra
, 8) >
3043 (cbp
->b_rptr
- headspace
< DB_BASE(cbp
))) {
3044 ovbcopy(cbp
->b_rptr
, DB_BASE(cbp
) + headspace
,
3046 cbp
->b_rptr
= DB_BASE(cbp
) + headspace
;
3047 cbp
->b_wptr
= cbp
->b_rptr
+ plainlen
;
3051 ASSERT(cbp
->b_rptr
- headspace
>= DB_BASE(cbp
));
3052 ASSERT(cbp
->b_wptr
<= DB_LIM(cbp
));
3055 * If using RCMD_MODE_V2 (new rcmd mode), prepend
3056 * the plaintext length before the actual plaintext.
3058 if (tmi
->enc_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
) {
3059 cbp
->b_rptr
-= RCMD_LEN_SZ
;
3061 /* put plaintext length at head of buffer */
3062 *(cbp
->b_rptr
+ 3) = (uchar_t
)(plainlen
& 0xff);
3063 *(cbp
->b_rptr
+ 2) = (uchar_t
)((plainlen
>> 8) & 0xff);
3064 *(cbp
->b_rptr
+ 1) = (uchar_t
)((plainlen
>> 16) & 0xff);
3065 *(cbp
->b_rptr
) = (uchar_t
)((plainlen
>> 24) & 0xff);
3068 newmp
= do_encrypt(q
, cbp
);
3070 if (newmp
!= NULL
&&
3071 (tmi
->enc_data
.option_mask
&
3072 (CRYPTOPT_RCMD_MODE_V1
| CRYPTOPT_RCMD_MODE_V2
))) {
3075 * Add length field, required when this is
3076 * used to encrypt "r*" commands(rlogin, rsh)
3079 lp
= mklenmp(newmp
, plainlen
);
3094 * encrypt a single message. This routine adds the
3095 * RCMD overhead bytes when necessary.
3098 encrypt_msgb(queue_t
*q
, struct tmodinfo
*tmi
, mblk_t
*mp
)
3100 size_t plainlen
, outlen
;
3101 mblk_t
*newmp
= NULL
;
3103 /* If not encrypting, do nothing */
3104 if (tmi
->enc_data
.method
== CRYPT_METHOD_NONE
) {
3108 plainlen
= MBLKL(mp
);
3113 * If the block is too big, we encrypt in 4K chunks so that
3114 * older rlogin clients do not choke on the larger buffers.
3116 while ((plainlen
= MBLKL(mp
)) > MSGBUF_SIZE
) {
3118 outlen
= MSGBUF_SIZE
;
3120 * Allocate a new buffer that is only 4K bytes, the
3121 * extra bytes are for crypto overhead.
3123 mp1
= allocb(outlen
+ CONFOUNDER_BYTES
, BPRI_MED
);
3126 "allocb (%d bytes) failed",
3127 (int)(outlen
+ CONFOUNDER_BYTES
));
3130 /* Copy the next 4K bytes from the old block. */
3131 bcopy(mp
->b_rptr
, mp1
->b_rptr
, outlen
);
3132 mp1
->b_wptr
= mp1
->b_rptr
+ outlen
;
3133 /* Advance the old block. */
3134 mp
->b_rptr
+= outlen
;
3136 /* encrypt the new block */
3137 newmp
= encrypt_block(q
, tmi
, mp1
, outlen
);
3144 /* If there is data left (< MSGBUF_SIZE), encrypt it. */
3145 if ((plainlen
= MBLKL(mp
)) > 0)
3146 newmp
= encrypt_block(q
, tmi
, mp
, plainlen
);
3154 * Service routine for the write queue.
3156 * Because data may be placed in the queue to hold between
3157 * the CRYPTIOCSTOP and CRYPTIOCSTART ioctls, the service routine is needed.
3160 cryptmodwsrv(queue_t
*q
)
3163 struct tmodinfo
*tmi
= (struct tmodinfo
*)q
->q_ptr
;
3165 while ((mp
= getq(q
)) != NULL
) {
3166 switch (mp
->b_datap
->db_type
) {
3169 * wput does not queue anything > QPCTL
3171 if (!canputnext(q
) ||
3172 !(tmi
->ready
& CRYPT_WRITE_READY
)) {
3173 if (!putbq(q
, mp
)) {
3181 if (canputnext(q
) && (tmi
->ready
& CRYPT_WRITE_READY
)) {
3183 mblk_t
*newmsg
= NULL
;
3186 * If multiple msgs, concat into 1
3187 * to minimize crypto operations later.
3189 if (mp
->b_cont
!= NULL
) {
3190 bp
= msgpullup(mp
, -1);
3196 newmsg
= encrypt_msgb(q
, tmi
, mp
);
3200 if (!putbq(q
, mp
)) {
3212 start_stream(queue_t
*wq
, mblk_t
*mp
, uchar_t dir
)
3214 mblk_t
*newmp
= NULL
;
3215 struct tmodinfo
*tmi
= (struct tmodinfo
*)wq
->q_ptr
;
3217 if (dir
== CRYPT_ENCRYPT
) {
3218 tmi
->ready
|= CRYPT_WRITE_READY
;
3219 (void) (STRLOG(CRYPTMOD_ID
, 0, 5, SL_TRACE
|SL_NOTE
,
3220 "start_stream: restart ENCRYPT/WRITE q"));
3224 } else if (dir
== CRYPT_DECRYPT
) {
3226 * put any extra data in the RD
3227 * queue to be processed and
3233 tmi
->ready
|= CRYPT_READ_READY
;
3234 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3236 "start_stream: restart "
3240 if (!putbq(RD(wq
), newmp
))
3247 miocack(wq
, mp
, 0, 0);
3251 * Write-side put procedure. Its main task is to detect ioctls and
3252 * FLUSH operations. Other message types are passed on through.
3255 cryptmodwput(queue_t
*wq
, mblk_t
*mp
)
3257 struct iocblk
*iocp
;
3258 struct tmodinfo
*tmi
= (struct tmodinfo
*)wq
->q_ptr
;
3261 switch (mp
->b_datap
->db_type
) {
3263 if (wq
->q_first
== NULL
&& canputnext(wq
) &&
3264 (tmi
->ready
& CRYPT_WRITE_READY
) &&
3265 tmi
->enc_data
.method
== CRYPT_METHOD_NONE
) {
3269 /* else, put it in the service queue */
3270 if (!putq(wq
, mp
)) {
3275 if (*mp
->b_rptr
& FLUSHW
) {
3276 flushq(wq
, FLUSHDATA
);
3281 iocp
= (struct iocblk
*)mp
->b_rptr
;
3282 switch (iocp
->ioc_cmd
) {
3285 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3287 "wput: got CRYPTIOCSETUP "
3288 "ioctl(%d)", iocp
->ioc_cmd
));
3290 if ((err
= miocpullup(mp
,
3291 sizeof (struct cr_info_t
))) != 0) {
3293 "wput: miocpullup failed for cr_info_t");
3294 miocnak(wq
, mp
, 0, err
);
3296 struct cr_info_t
*ci
;
3297 ci
= (struct cr_info_t
*)mp
->b_cont
->b_rptr
;
3299 if (ci
->direction_mask
& CRYPT_ENCRYPT
) {
3300 ret
= setup_crypto(ci
, &tmi
->enc_data
, 1);
3304 (ci
->direction_mask
& CRYPT_DECRYPT
)) {
3305 ret
= setup_crypto(ci
, &tmi
->dec_data
, 0);
3308 (ci
->direction_mask
& CRYPT_DECRYPT
) &&
3309 ANY_RCMD_MODE(tmi
->dec_data
.option_mask
)) {
3310 bzero(&tmi
->rcmd_state
,
3311 sizeof (tmi
->rcmd_state
));
3314 miocack(wq
, mp
, 0, 0);
3317 "wput: setup_crypto failed");
3318 miocnak(wq
, mp
, 0, ret
);
3320 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3322 "wput: done with SETUP "
3327 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3329 "wput: got CRYPTIOCSTOP "
3330 "ioctl(%d)", iocp
->ioc_cmd
));
3332 if ((err
= miocpullup(mp
, sizeof (uint32_t))) != 0) {
3334 "wput: CRYPTIOCSTOP ioctl wrong "
3335 "size (%d should be %d)",
3336 (int)iocp
->ioc_count
,
3337 (int)sizeof (uint32_t));
3338 miocnak(wq
, mp
, 0, err
);
3342 stopdir
= (uint32_t *)mp
->b_cont
->b_rptr
;
3343 if (!CR_DIRECTION_OK(*stopdir
)) {
3344 miocnak(wq
, mp
, 0, EINVAL
);
3348 /* disable the queues until further notice */
3349 if (*stopdir
& CRYPT_ENCRYPT
) {
3351 tmi
->ready
&= ~CRYPT_WRITE_READY
;
3353 if (*stopdir
& CRYPT_DECRYPT
) {
3355 tmi
->ready
&= ~CRYPT_READ_READY
;
3358 miocack(wq
, mp
, 0, 0);
3361 case CRYPTIOCSTARTDEC
:
3362 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3364 "wput: got CRYPTIOCSTARTDEC "
3365 "ioctl(%d)", iocp
->ioc_cmd
));
3367 start_stream(wq
, mp
, CRYPT_DECRYPT
);
3369 case CRYPTIOCSTARTENC
:
3370 (void) (STRLOG(CRYPTMOD_ID
, 0, 5,
3372 "wput: got CRYPTIOCSTARTENC "
3373 "ioctl(%d)", iocp
->ioc_cmd
));
3375 start_stream(wq
, mp
, CRYPT_ENCRYPT
);
3383 if (queclass(mp
) < QPCTL
) {
3384 if (wq
->q_first
!= NULL
|| !canputnext(wq
)) {
3396 * decrypt_rcmd_mblks
3398 * Because kerberized r* commands(rsh, rlogin, etc)
3399 * use a 4 byte length field to indicate the # of
3400 * PLAINTEXT bytes that are encrypted in the field
3401 * that follows, we must parse out each message and
3402 * break out the length fields prior to sending them
3403 * upstream to our Solaris r* clients/servers which do
3404 * NOT understand this format.
3406 * Kerberized/encrypted message format:
3407 * -------------------------------
3408 * | XXXX | N bytes of ciphertext|
3409 * -------------------------------
3411 * Where: XXXX = number of plaintext bytes that were encrypted in
3412 * to make the ciphertext field. This is done
3413 * because we are using a cipher that pads out to
3414 * an 8 byte boundary. We only want the application
3415 * layer to see the correct number of plain text bytes,
3416 * not plaintext + pad. So, after we decrypt, we
3417 * must trim the output block down to the intended
3418 * plaintext length and eliminate the pad bytes.
3420 * This routine takes the entire input message, breaks it into
3421 * a new message that does not contain these length fields and
3422 * returns a message consisting of mblks filled with just ciphertext.
3426 decrypt_rcmd_mblks(queue_t
*q
, mblk_t
*mp
)
3428 mblk_t
*newmp
= NULL
;
3430 struct tmodinfo
*tmi
= (struct tmodinfo
*)q
->q_ptr
;
3432 msglen
= msgsize(mp
);
3435 * If we need the length field, get it here.
3436 * Test the "plaintext length" indicator.
3438 if (tmi
->rcmd_state
.pt_len
== 0) {
3444 * Make sure we have recieved all 4 bytes of the
3447 while (mp
!= NULL
) {
3448 ASSERT(tmi
->rcmd_state
.cd_len
< sizeof (uint32_t));
3450 tocopy
= sizeof (uint32_t) -
3451 tmi
->rcmd_state
.cd_len
;
3452 if (tocopy
> msglen
)
3455 ASSERT(mp
->b_rptr
+ tocopy
<= DB_LIM(mp
));
3457 (char *)(&tmi
->rcmd_state
.next_len
+
3458 tmi
->rcmd_state
.cd_len
), tocopy
);
3460 tmi
->rcmd_state
.cd_len
+= tocopy
;
3462 if (tmi
->rcmd_state
.cd_len
>= sizeof (uint32_t)) {
3463 tmi
->rcmd_state
.next_len
=
3464 ntohl(tmi
->rcmd_state
.next_len
);
3478 * recalculate the msglen now that we've read the
3479 * length and adjusted the bufptr (b_rptr).
3482 mp
->b_rptr
+= tocopy
;
3484 tmi
->rcmd_state
.pt_len
= tmi
->rcmd_state
.next_len
;
3486 if (tmi
->rcmd_state
.pt_len
<= 0) {
3488 * Return an IO error to break the connection. there
3489 * is no way to recover from this. Usually it means
3490 * the app has incorrectly requested decryption on
3491 * a non-encrypted stream, thus the "pt_len" field
3494 mp
->b_datap
->db_type
= M_ERROR
;
3495 mp
->b_rptr
= mp
->b_datap
->db_base
;
3497 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
3499 freemsg(mp
->b_cont
);
3502 tmi
->rcmd_state
.cd_len
= tmi
->rcmd_state
.pt_len
= 0;
3507 * If this is V2 mode, then the encrypted data is actually
3508 * 4 bytes bigger than the indicated len because the plaintext
3509 * length is encrypted for an additional security check, but
3510 * its not counted as part of the overall length we just read.
3511 * Strange and confusing, but true.
3514 if (tmi
->dec_data
.option_mask
& CRYPTOPT_RCMD_MODE_V2
)
3515 elen
= tmi
->rcmd_state
.pt_len
+ 4;
3517 elen
= tmi
->rcmd_state
.pt_len
;
3519 tmi
->rcmd_state
.cd_len
= encrypt_size(&tmi
->dec_data
, elen
);
3522 * Allocate an mblk to hold the cipher text until it is
3523 * all ready to be processed.
3525 tmi
->rcmd_state
.c_msg
= allocb(tmi
->rcmd_state
.cd_len
,
3527 if (tmi
->rcmd_state
.c_msg
== NULL
) {
3529 cmn_err(CE_WARN
, "decrypt_rcmd_msgb: allocb failed "
3531 (int)tmi
->rcmd_state
.cd_len
);
3534 * Return an IO error to break the connection.
3536 mp
->b_datap
->db_type
= M_ERROR
;
3537 mp
->b_rptr
= mp
->b_datap
->db_base
;
3539 mp
->b_wptr
= mp
->b_rptr
+ sizeof (char);
3540 freemsg(mp
->b_cont
);
3542 tmi
->rcmd_state
.cd_len
= tmi
->rcmd_state
.pt_len
= 0;
3549 * If this entire message was just the length field,
3550 * free and return. The actual data will probably be next.
3558 * Copy as much of the cipher text as possible into
3559 * the new msgb (c_msg).
3561 * Logic: if we got some bytes (msglen) and we still
3562 * "need" some bytes (len-rcvd), get them here.
3564 ASSERT(tmi
->rcmd_state
.c_msg
!= NULL
);
3566 (tmi
->rcmd_state
.cd_len
> MBLKL(tmi
->rcmd_state
.c_msg
))) {
3571 * Walk the mblks and copy just as many bytes as we need
3572 * for this particular block of cipher text.
3575 while (bp
!= NULL
) {
3580 needed
= tmi
->rcmd_state
.cd_len
-
3581 MBLKL(tmi
->rcmd_state
.c_msg
);
3583 tocopy
= (needed
>= n
? n
: needed
);
3585 ASSERT(bp
->b_rptr
+ tocopy
<= DB_LIM(bp
));
3586 ASSERT(tmi
->rcmd_state
.c_msg
->b_wptr
+ tocopy
<=
3587 DB_LIM(tmi
->rcmd_state
.c_msg
));
3589 /* Copy to end of new mblk */
3590 bcopy(bp
->b_rptr
, tmi
->rcmd_state
.c_msg
->b_wptr
,
3593 tmi
->rcmd_state
.c_msg
->b_wptr
+= tocopy
;
3595 bp
->b_rptr
+= tocopy
;
3600 * If we used this whole block, free it and
3608 /* If we got what we needed, stop the loop */
3609 if (MBLKL(tmi
->rcmd_state
.c_msg
) ==
3610 tmi
->rcmd_state
.cd_len
) {
3612 * If there is more data in the message,
3613 * its for another block of cipher text,
3614 * put it back in the queue for next time.
3619 } else if (nextp
!= NULL
) {
3621 * If there is more, put it back in the
3622 * queue for another pass thru.
3624 if (!putbq(q
, nextp
))
3633 * Finally, if we received all the cipher text data for
3634 * this message, decrypt it into a new msg and send it up
3637 if (tmi
->rcmd_state
.pt_len
> 0 &&
3638 MBLKL(tmi
->rcmd_state
.c_msg
) == tmi
->rcmd_state
.cd_len
) {
3643 * Now we can use our msg that we created when the
3644 * initial message boundary was detected.
3646 bp
= tmi
->rcmd_state
.c_msg
;
3647 tmi
->rcmd_state
.c_msg
= NULL
;
3649 newbp
= do_decrypt(q
, bp
);
3650 if (newbp
!= NULL
) {
3653 * If using RCMD_MODE_V2 ("new" mode),
3654 * look at the 4 byte plaintext length that
3655 * was just decrypted and compare with the
3656 * original pt_len value that was received.
3658 if (tmi
->dec_data
.option_mask
&
3659 CRYPTOPT_RCMD_MODE_V2
) {
3662 pt_len2
= *(uint32_t *)bp
->b_rptr
;
3663 pt_len2
= ntohl(pt_len2
);
3665 * Make sure the 2 pt len fields agree.
3667 if (pt_len2
!= tmi
->rcmd_state
.pt_len
) {
3669 "Inconsistent length fields"
3670 " received %d != %d",
3671 (int)tmi
->rcmd_state
.pt_len
,
3673 bp
->b_datap
->db_type
= M_ERROR
;
3674 bp
->b_rptr
= bp
->b_datap
->db_base
;
3676 bp
->b_wptr
= bp
->b_rptr
+ sizeof (char);
3677 freemsg(bp
->b_cont
);
3679 tmi
->rcmd_state
.cd_len
= 0;
3683 bp
->b_rptr
+= sizeof (uint32_t);
3687 * Trim the decrypted block the length originally
3688 * indicated by the sender. This is to remove any
3689 * padding bytes that the sender added to satisfy
3690 * requirements of the crypto algorithm.
3692 bp
->b_wptr
= bp
->b_rptr
+ tmi
->rcmd_state
.pt_len
;
3697 * Reset our state to indicate we are ready
3698 * for a new message.
3700 tmi
->rcmd_state
.pt_len
= 0;
3701 tmi
->rcmd_state
.cd_len
= 0;
3705 "decrypt_rcmd: do_decrypt on %d bytes failed",
3706 (int)tmi
->rcmd_state
.cd_len
);
3709 * do_decrypt already handled failures, just
3712 tmi
->rcmd_state
.pt_len
= 0;
3713 tmi
->rcmd_state
.cd_len
= 0;
3719 * return the new message with the 'length' fields removed
3727 * Read queue service routine
3728 * Necessary because if the ready flag is not set
3729 * (via CRYPTIOCSTOP/CRYPTIOCSTART ioctls) then the data
3730 * must remain on queue and not be passed along.
3733 cryptmodrsrv(queue_t
*q
)
3736 struct tmodinfo
*tmi
= (struct tmodinfo
*)q
->q_ptr
;
3738 while ((mp
= getq(q
)) != NULL
) {
3739 switch (mp
->b_datap
->db_type
) {
3741 if (canputnext(q
) && tmi
->ready
& CRYPT_READ_READY
) {
3743 * Process "rcmd" messages differently because
3744 * they contain a 4 byte plaintext length
3745 * id that needs to be removed.
3747 if (tmi
->dec_data
.method
!= CRYPT_METHOD_NONE
&&
3748 (tmi
->dec_data
.option_mask
&
3749 (CRYPTOPT_RCMD_MODE_V1
|
3750 CRYPTOPT_RCMD_MODE_V2
))) {
3751 mp
= decrypt_rcmd_mblks(q
, mp
);
3756 if ((bp
= msgpullup(mp
, -1)) != NULL
) {
3758 if (MBLKL(bp
) > 0) {
3759 mp
= do_decrypt(q
, bp
);
3765 if (!putbq(q
, mp
)) {
3773 * rput does not queue anything > QPCTL, so we don't
3774 * need to check for it here.
3776 if (!canputnext(q
)) {
3790 * Read-side put procedure.
3793 cryptmodrput(queue_t
*rq
, mblk_t
*mp
)
3795 switch (mp
->b_datap
->db_type
) {
3797 if (!putq(rq
, mp
)) {
3802 if (*mp
->b_rptr
& FLUSHR
) {
3803 flushq(rq
, FLUSHALL
);
3808 if (queclass(mp
) < QPCTL
) {
3809 if (rq
->q_first
!= NULL
|| !canputnext(rq
)) {