10Sstevel@tonic-gate /*
2*11688SPeter.Shoults@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * STREAMS Crypto Module
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * This module is used to facilitate Kerberos encryption
80Sstevel@tonic-gate * operations for the telnet daemon and rlogin daemon.
90Sstevel@tonic-gate * Because the Solaris telnet and rlogin daemons run mostly
100Sstevel@tonic-gate * in-kernel via 'telmod' and 'rlmod', this module must be
110Sstevel@tonic-gate * pushed on the STREAM *below* telmod or rlmod.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Parts of the 3DES key derivation code are covered by the
140Sstevel@tonic-gate * following copyright.
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright (C) 1998 by the FundsXpress, INC.
170Sstevel@tonic-gate *
180Sstevel@tonic-gate * All rights reserved.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * Export of this software from the United States of America may require
210Sstevel@tonic-gate * a specific license from the United States Government. It is the
220Sstevel@tonic-gate * responsibility of any person or organization contemplating export to
230Sstevel@tonic-gate * obtain such a license before exporting.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
260Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
270Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
280Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
290Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
300Sstevel@tonic-gate * the name of FundsXpress. not be used in advertising or publicity pertaining
310Sstevel@tonic-gate * to distribution of the software without specific, written prior
320Sstevel@tonic-gate * permission. FundsXpress makes no representations about the suitability of
330Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
340Sstevel@tonic-gate * or implied warranty.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
370Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
380Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/errno.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/time.h>
460Sstevel@tonic-gate #include <sys/stropts.h>
470Sstevel@tonic-gate #include <sys/stream.h>
480Sstevel@tonic-gate #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/strlog.h>
500Sstevel@tonic-gate #include <sys/cmn_err.h>
510Sstevel@tonic-gate #include <sys/conf.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/kmem.h>
540Sstevel@tonic-gate #include <sys/strsun.h>
550Sstevel@tonic-gate #include <sys/random.h>
560Sstevel@tonic-gate #include <sys/types.h>
570Sstevel@tonic-gate #include <sys/byteorder.h>
580Sstevel@tonic-gate #include <sys/cryptmod.h>
590Sstevel@tonic-gate #include <sys/crc32.h>
600Sstevel@tonic-gate #include <sys/policy.h>
610Sstevel@tonic-gate
620Sstevel@tonic-gate #include <sys/crypto/api.h>
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * Function prototypes.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate static int cryptmodopen(queue_t *, dev_t *, int, int, cred_t *);
680Sstevel@tonic-gate static void cryptmodrput(queue_t *, mblk_t *);
690Sstevel@tonic-gate static void cryptmodwput(queue_t *, mblk_t *);
700Sstevel@tonic-gate static int cryptmodclose(queue_t *);
710Sstevel@tonic-gate static int cryptmodwsrv(queue_t *);
720Sstevel@tonic-gate static int cryptmodrsrv(queue_t *);
730Sstevel@tonic-gate
740Sstevel@tonic-gate static mblk_t *do_encrypt(queue_t *q, mblk_t *mp);
750Sstevel@tonic-gate static mblk_t *do_decrypt(queue_t *q, mblk_t *mp);
760Sstevel@tonic-gate
770Sstevel@tonic-gate #define CRYPTMOD_ID 5150
780Sstevel@tonic-gate
790Sstevel@tonic-gate #define CFB_BLKSZ 8
800Sstevel@tonic-gate
810Sstevel@tonic-gate #define K5CLENGTH 5
820Sstevel@tonic-gate
830Sstevel@tonic-gate static struct module_info cryptmod_minfo = {
840Sstevel@tonic-gate CRYPTMOD_ID, /* mi_idnum */
850Sstevel@tonic-gate "cryptmod", /* mi_idname */
860Sstevel@tonic-gate 0, /* mi_minpsz */
870Sstevel@tonic-gate INFPSZ, /* mi_maxpsz */
880Sstevel@tonic-gate 65536, /* mi_hiwat */
890Sstevel@tonic-gate 1024 /* mi_lowat */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate
920Sstevel@tonic-gate static struct qinit cryptmod_rinit = {
930Sstevel@tonic-gate (int (*)())cryptmodrput, /* qi_putp */
940Sstevel@tonic-gate cryptmodrsrv, /* qi_svc */
950Sstevel@tonic-gate cryptmodopen, /* qi_qopen */
960Sstevel@tonic-gate cryptmodclose, /* qi_qclose */
970Sstevel@tonic-gate NULL, /* qi_qadmin */
980Sstevel@tonic-gate &cryptmod_minfo, /* qi_minfo */
990Sstevel@tonic-gate NULL /* qi_mstat */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate static struct qinit cryptmod_winit = {
1030Sstevel@tonic-gate (int (*)())cryptmodwput, /* qi_putp */
1040Sstevel@tonic-gate cryptmodwsrv, /* qi_srvp */
1050Sstevel@tonic-gate NULL, /* qi_qopen */
1060Sstevel@tonic-gate NULL, /* qi_qclose */
1070Sstevel@tonic-gate NULL, /* qi_qadmin */
1080Sstevel@tonic-gate &cryptmod_minfo, /* qi_minfo */
1090Sstevel@tonic-gate NULL /* qi_mstat */
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate static struct streamtab cryptmod_info = {
1130Sstevel@tonic-gate &cryptmod_rinit, /* st_rdinit */
1140Sstevel@tonic-gate &cryptmod_winit, /* st_wrinit */
1150Sstevel@tonic-gate NULL, /* st_muxrinit */
1160Sstevel@tonic-gate NULL /* st_muxwinit */
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate typedef struct {
1200Sstevel@tonic-gate uint_t hash_len;
1210Sstevel@tonic-gate uint_t confound_len;
1220Sstevel@tonic-gate int (*hashfunc)();
1230Sstevel@tonic-gate } hash_info_t;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #define MAX_CKSUM_LEN 20
1260Sstevel@tonic-gate #define CONFOUNDER_LEN 8
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate #define SHA1_HASHSIZE 20
1290Sstevel@tonic-gate #define MD5_HASHSIZE 16
1300Sstevel@tonic-gate #define CRC32_HASHSIZE 4
1313518Spk193450 #define MSGBUF_SIZE 4096
1323518Spk193450 #define CONFOUNDER_BYTES 128
1333518Spk193450
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static int crc32_calc(uchar_t *, uchar_t *, uint_t);
1360Sstevel@tonic-gate static int md5_calc(uchar_t *, uchar_t *, uint_t);
1370Sstevel@tonic-gate static int sha1_calc(uchar_t *, uchar_t *, uint_t);
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate static hash_info_t null_hash = {0, 0, NULL};
1400Sstevel@tonic-gate static hash_info_t crc32_hash = {CRC32_HASHSIZE, CONFOUNDER_LEN, crc32_calc};
1410Sstevel@tonic-gate static hash_info_t md5_hash = {MD5_HASHSIZE, CONFOUNDER_LEN, md5_calc};
1420Sstevel@tonic-gate static hash_info_t sha1_hash = {SHA1_HASHSIZE, CONFOUNDER_LEN, sha1_calc};
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate static crypto_mech_type_t sha1_hmac_mech = CRYPTO_MECH_INVALID;
1450Sstevel@tonic-gate static crypto_mech_type_t md5_hmac_mech = CRYPTO_MECH_INVALID;
1460Sstevel@tonic-gate static crypto_mech_type_t sha1_hash_mech = CRYPTO_MECH_INVALID;
1470Sstevel@tonic-gate static crypto_mech_type_t md5_hash_mech = CRYPTO_MECH_INVALID;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate static int kef_crypt(struct cipher_data_t *, void *,
1500Sstevel@tonic-gate crypto_data_format_t, size_t, int);
1510Sstevel@tonic-gate static mblk_t *
1520Sstevel@tonic-gate arcfour_hmac_md5_encrypt(queue_t *, struct tmodinfo *,
1530Sstevel@tonic-gate mblk_t *, hash_info_t *);
1540Sstevel@tonic-gate static mblk_t *
1550Sstevel@tonic-gate arcfour_hmac_md5_decrypt(queue_t *, struct tmodinfo *,
1560Sstevel@tonic-gate mblk_t *, hash_info_t *);
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static int
1590Sstevel@tonic-gate do_hmac(crypto_mech_type_t, crypto_key_t *, char *, int, char *, int);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate * This is the loadable module wrapper.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate #include <sys/modctl.h>
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate static struct fmodsw fsw = {
1670Sstevel@tonic-gate "cryptmod",
1680Sstevel@tonic-gate &cryptmod_info,
1690Sstevel@tonic-gate D_MP | D_MTQPAIR
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * Module linkage information for the kernel.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
1760Sstevel@tonic-gate &mod_strmodops,
1777227Sps57422 "STREAMS encryption module",
1780Sstevel@tonic-gate &fsw
1790Sstevel@tonic-gate };
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate static struct modlinkage modlinkage = {
1820Sstevel@tonic-gate MODREV_1,
1830Sstevel@tonic-gate &modlstrmod,
1840Sstevel@tonic-gate NULL
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate int
_init(void)1880Sstevel@tonic-gate _init(void)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate return (mod_install(&modlinkage));
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate int
_fini(void)1940Sstevel@tonic-gate _fini(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate return (mod_remove(&modlinkage));
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2000Sstevel@tonic-gate _info(struct modinfo *modinfop)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate static void
cleanup(struct cipher_data_t * cd)2060Sstevel@tonic-gate cleanup(struct cipher_data_t *cd)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate if (cd->key != NULL) {
2090Sstevel@tonic-gate bzero(cd->key, cd->keylen);
2100Sstevel@tonic-gate kmem_free(cd->key, cd->keylen);
2110Sstevel@tonic-gate cd->key = NULL;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (cd->ckey != NULL) {
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * ckey is a crypto_key_t structure which references
2170Sstevel@tonic-gate * "cd->key" for its raw key data. Since that was already
2180Sstevel@tonic-gate * cleared out, we don't need another "bzero" here.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate kmem_free(cd->ckey, sizeof (crypto_key_t));
2210Sstevel@tonic-gate cd->ckey = NULL;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate if (cd->block != NULL) {
2250Sstevel@tonic-gate kmem_free(cd->block, cd->blocklen);
2260Sstevel@tonic-gate cd->block = NULL;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if (cd->saveblock != NULL) {
2300Sstevel@tonic-gate kmem_free(cd->saveblock, cd->blocklen);
2310Sstevel@tonic-gate cd->saveblock = NULL;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if (cd->ivec != NULL) {
2350Sstevel@tonic-gate kmem_free(cd->ivec, cd->ivlen);
2360Sstevel@tonic-gate cd->ivec = NULL;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if (cd->d_encr_key.ck_data != NULL) {
2400Sstevel@tonic-gate bzero(cd->d_encr_key.ck_data, cd->keylen);
2410Sstevel@tonic-gate kmem_free(cd->d_encr_key.ck_data, cd->keylen);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate if (cd->d_hmac_key.ck_data != NULL) {
2450Sstevel@tonic-gate bzero(cd->d_hmac_key.ck_data, cd->keylen);
2460Sstevel@tonic-gate kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (cd->enc_tmpl != NULL)
2500Sstevel@tonic-gate (void) crypto_destroy_ctx_template(cd->enc_tmpl);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (cd->hmac_tmpl != NULL)
2530Sstevel@tonic-gate (void) crypto_destroy_ctx_template(cd->hmac_tmpl);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (cd->ctx != NULL) {
2560Sstevel@tonic-gate crypto_cancel_ctx(cd->ctx);
2570Sstevel@tonic-gate cd->ctx = NULL;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* ARGSUSED */
2620Sstevel@tonic-gate static int
cryptmodopen(queue_t * rq,dev_t * dev,int oflag,int sflag,cred_t * crp)2630Sstevel@tonic-gate cryptmodopen(queue_t *rq, dev_t *dev, int oflag, int sflag, cred_t *crp)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate struct tmodinfo *tmi;
2660Sstevel@tonic-gate ASSERT(rq);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (sflag != MODOPEN)
2690Sstevel@tonic-gate return (EINVAL);
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
2720Sstevel@tonic-gate "cryptmodopen: opening module(PID %d)",
2730Sstevel@tonic-gate ddi_get_pid()));
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate if (rq->q_ptr != NULL) {
2760Sstevel@tonic-gate cmn_err(CE_WARN, "cryptmodopen: already opened");
2770Sstevel@tonic-gate return (0);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate * Allocate and initialize per-Stream structure.
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate tmi = (struct tmodinfo *)kmem_zalloc(sizeof (struct tmodinfo),
2840Sstevel@tonic-gate KM_SLEEP);
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate tmi->enc_data.method = CRYPT_METHOD_NONE;
2870Sstevel@tonic-gate tmi->dec_data.method = CRYPT_METHOD_NONE;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate tmi->ready = (CRYPT_READ_READY | CRYPT_WRITE_READY);
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate rq->q_ptr = WR(rq)->q_ptr = tmi;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate sha1_hmac_mech = crypto_mech2id(SUN_CKM_SHA1_HMAC);
2940Sstevel@tonic-gate md5_hmac_mech = crypto_mech2id(SUN_CKM_MD5_HMAC);
2950Sstevel@tonic-gate sha1_hash_mech = crypto_mech2id(SUN_CKM_SHA1);
2960Sstevel@tonic-gate md5_hash_mech = crypto_mech2id(SUN_CKM_MD5);
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate qprocson(rq);
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate return (0);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate static int
cryptmodclose(queue_t * rq)3040Sstevel@tonic-gate cryptmodclose(queue_t *rq)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)rq->q_ptr;
3070Sstevel@tonic-gate ASSERT(tmi);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate qprocsoff(rq);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate cleanup(&tmi->enc_data);
3120Sstevel@tonic-gate cleanup(&tmi->dec_data);
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate kmem_free(tmi, sizeof (struct tmodinfo));
3150Sstevel@tonic-gate rq->q_ptr = WR(rq)->q_ptr = NULL;
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * plaintext_offset
3220Sstevel@tonic-gate *
3230Sstevel@tonic-gate * Calculate exactly how much space is needed in front
3240Sstevel@tonic-gate * of the "plaintext" in an mbuf so it can be positioned
3250Sstevel@tonic-gate * 1 time instead of potentially moving the data multiple
3260Sstevel@tonic-gate * times.
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate static int
plaintext_offset(struct cipher_data_t * cd)3290Sstevel@tonic-gate plaintext_offset(struct cipher_data_t *cd)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate int headspace = 0;
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /* 4 byte length prepended to all RCMD msgs */
3340Sstevel@tonic-gate if (ANY_RCMD_MODE(cd->option_mask))
3350Sstevel@tonic-gate headspace += RCMD_LEN_SZ;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /* RCMD V2 mode adds an additional 4 byte plaintext length */
3380Sstevel@tonic-gate if (cd->option_mask & CRYPTOPT_RCMD_MODE_V2)
3390Sstevel@tonic-gate headspace += RCMD_LEN_SZ;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /* Need extra space for hash and counfounder */
3420Sstevel@tonic-gate switch (cd->method) {
3430Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
3440Sstevel@tonic-gate headspace += null_hash.hash_len + null_hash.confound_len;
3450Sstevel@tonic-gate break;
3460Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
3470Sstevel@tonic-gate headspace += crc32_hash.hash_len + crc32_hash.confound_len;
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
3500Sstevel@tonic-gate headspace += md5_hash.hash_len + md5_hash.confound_len;
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
3530Sstevel@tonic-gate headspace += sha1_hash.confound_len;
3540Sstevel@tonic-gate break;
3550Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
3560Sstevel@tonic-gate headspace += md5_hash.hash_len + md5_hash.confound_len;
3570Sstevel@tonic-gate break;
3580Sstevel@tonic-gate case CRYPT_METHOD_AES128:
3590Sstevel@tonic-gate case CRYPT_METHOD_AES256:
3600Sstevel@tonic-gate headspace += DEFAULT_AES_BLOCKLEN;
3610Sstevel@tonic-gate break;
3620Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
3630Sstevel@tonic-gate case CRYPT_METHOD_NONE:
3640Sstevel@tonic-gate break;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate return (headspace);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate * encrypt_size
3710Sstevel@tonic-gate *
3720Sstevel@tonic-gate * Calculate the resulting size when encrypting 'plainlen' bytes
3730Sstevel@tonic-gate * of data.
3740Sstevel@tonic-gate */
3750Sstevel@tonic-gate static size_t
encrypt_size(struct cipher_data_t * cd,size_t plainlen)3760Sstevel@tonic-gate encrypt_size(struct cipher_data_t *cd, size_t plainlen)
3770Sstevel@tonic-gate {
3780Sstevel@tonic-gate size_t cipherlen;
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate switch (cd->method) {
3810Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
3820Sstevel@tonic-gate cipherlen = (size_t)P2ROUNDUP(null_hash.hash_len +
3830Sstevel@tonic-gate plainlen, 8);
3840Sstevel@tonic-gate break;
3850Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
3860Sstevel@tonic-gate cipherlen = (size_t)P2ROUNDUP(md5_hash.hash_len +
3870Sstevel@tonic-gate md5_hash.confound_len +
3880Sstevel@tonic-gate plainlen, 8);
3890Sstevel@tonic-gate break;
3900Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
3910Sstevel@tonic-gate cipherlen = (size_t)P2ROUNDUP(crc32_hash.hash_len +
3920Sstevel@tonic-gate crc32_hash.confound_len +
3930Sstevel@tonic-gate plainlen, 8);
3940Sstevel@tonic-gate break;
3950Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
3960Sstevel@tonic-gate cipherlen = (size_t)P2ROUNDUP(sha1_hash.confound_len +
3970Sstevel@tonic-gate plainlen, 8) +
3980Sstevel@tonic-gate sha1_hash.hash_len;
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
4010Sstevel@tonic-gate cipherlen = (size_t)P2ROUNDUP(md5_hash.confound_len +
4020Sstevel@tonic-gate plainlen, 1) + md5_hash.hash_len;
4030Sstevel@tonic-gate break;
4040Sstevel@tonic-gate case CRYPT_METHOD_AES128:
4050Sstevel@tonic-gate case CRYPT_METHOD_AES256:
4060Sstevel@tonic-gate /* No roundup for AES-CBC-CTS */
4070Sstevel@tonic-gate cipherlen = DEFAULT_AES_BLOCKLEN + plainlen +
4080Sstevel@tonic-gate AES_TRUNCATED_HMAC_LEN;
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
4110Sstevel@tonic-gate case CRYPT_METHOD_NONE:
4120Sstevel@tonic-gate cipherlen = plainlen;
4130Sstevel@tonic-gate break;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate return (cipherlen);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * des_cfb_encrypt
4210Sstevel@tonic-gate *
4220Sstevel@tonic-gate * Encrypt the mblk data using DES with cipher feedback.
4230Sstevel@tonic-gate *
4240Sstevel@tonic-gate * Given that V[i] is the initial 64 bit vector, V[n] is the nth 64 bit
4250Sstevel@tonic-gate * vector, D[n] is the nth chunk of 64 bits of data to encrypt
4260Sstevel@tonic-gate * (decrypt), and O[n] is the nth chunk of 64 bits of encrypted
4270Sstevel@tonic-gate * (decrypted) data, then:
4280Sstevel@tonic-gate *
4290Sstevel@tonic-gate * V[0] = DES(V[i], key)
4300Sstevel@tonic-gate * O[n] = D[n] <exclusive or > V[n]
4310Sstevel@tonic-gate * V[n+1] = DES(O[n], key)
4320Sstevel@tonic-gate *
4330Sstevel@tonic-gate * The size of the message being encrypted does not change in this
4340Sstevel@tonic-gate * algorithm, num_bytes in == num_bytes out.
4350Sstevel@tonic-gate */
4360Sstevel@tonic-gate static mblk_t *
des_cfb_encrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp)4370Sstevel@tonic-gate des_cfb_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate int savedbytes;
4400Sstevel@tonic-gate char *iptr, *optr, *lastoutput;
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate lastoutput = optr = (char *)mp->b_rptr;
4430Sstevel@tonic-gate iptr = (char *)mp->b_rptr;
4440Sstevel@tonic-gate savedbytes = tmi->enc_data.bytes % CFB_BLKSZ;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate while (iptr < (char *)mp->b_wptr) {
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate * Do DES-ECB.
4490Sstevel@tonic-gate * The first time this runs, the 'tmi->enc_data.block' will
4500Sstevel@tonic-gate * contain the initialization vector that should have been
4510Sstevel@tonic-gate * passed in with the SETUP ioctl.
4520Sstevel@tonic-gate *
4530Sstevel@tonic-gate * V[n] = DES(V[n-1], key)
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
4560Sstevel@tonic-gate int retval = 0;
4570Sstevel@tonic-gate retval = kef_crypt(&tmi->enc_data,
4580Sstevel@tonic-gate tmi->enc_data.block,
4590Sstevel@tonic-gate CRYPTO_DATA_RAW,
4600Sstevel@tonic-gate tmi->enc_data.blocklen,
4610Sstevel@tonic-gate CRYPT_ENCRYPT);
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (retval != CRYPTO_SUCCESS) {
4640Sstevel@tonic-gate #ifdef DEBUG
4650Sstevel@tonic-gate cmn_err(CE_WARN, "des_cfb_encrypt: kef_crypt "
4660Sstevel@tonic-gate "failed - error 0x%0x", retval);
4670Sstevel@tonic-gate #endif
4680Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
4690Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
4700Sstevel@tonic-gate *mp->b_rptr = EIO;
4710Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
4720Sstevel@tonic-gate freemsg(mp->b_cont);
4730Sstevel@tonic-gate mp->b_cont = NULL;
4740Sstevel@tonic-gate qreply(WR(q), mp);
4750Sstevel@tonic-gate return (NULL);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate /* O[n] = I[n] ^ V[n] */
4800Sstevel@tonic-gate *(optr++) = *(iptr++) ^
4810Sstevel@tonic-gate tmi->enc_data.block[tmi->enc_data.bytes % CFB_BLKSZ];
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate tmi->enc_data.bytes++;
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * Feedback the encrypted output as the input to next DES call.
4860Sstevel@tonic-gate */
4870Sstevel@tonic-gate if (!(tmi->enc_data.bytes % CFB_BLKSZ)) {
4880Sstevel@tonic-gate char *dbptr = tmi->enc_data.block;
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate * Get the last bits of input from the previous
4910Sstevel@tonic-gate * msg block that we haven't yet used as feedback input.
4920Sstevel@tonic-gate */
4930Sstevel@tonic-gate if (savedbytes > 0) {
4940Sstevel@tonic-gate bcopy(tmi->enc_data.saveblock,
4950Sstevel@tonic-gate dbptr, (size_t)savedbytes);
4960Sstevel@tonic-gate dbptr += savedbytes;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate * Now copy the correct bytes from the current input
5010Sstevel@tonic-gate * stream and update the 'lastoutput' ptr
5020Sstevel@tonic-gate */
5030Sstevel@tonic-gate bcopy(lastoutput, dbptr,
5040Sstevel@tonic-gate (size_t)(CFB_BLKSZ - savedbytes));
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate lastoutput += (CFB_BLKSZ - savedbytes);
5070Sstevel@tonic-gate savedbytes = 0;
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate * If there are bytes of input here that we need in the next
5120Sstevel@tonic-gate * block to build an ivec, save them off here.
5130Sstevel@tonic-gate */
5140Sstevel@tonic-gate if (lastoutput < optr) {
5150Sstevel@tonic-gate bcopy(lastoutput,
5160Sstevel@tonic-gate tmi->enc_data.saveblock + savedbytes,
5170Sstevel@tonic-gate (uint_t)(optr - lastoutput));
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate return (mp);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate * des_cfb_decrypt
5240Sstevel@tonic-gate *
5250Sstevel@tonic-gate * Decrypt the data in the mblk using DES in Cipher Feedback mode
5260Sstevel@tonic-gate *
5270Sstevel@tonic-gate * # bytes in == # bytes out, no padding, confounding, or hashing
5280Sstevel@tonic-gate * is added.
5290Sstevel@tonic-gate *
5300Sstevel@tonic-gate */
5310Sstevel@tonic-gate static mblk_t *
des_cfb_decrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp)5320Sstevel@tonic-gate des_cfb_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate uint_t len;
5350Sstevel@tonic-gate uint_t savedbytes;
5360Sstevel@tonic-gate char *iptr;
5370Sstevel@tonic-gate char *lastinput;
5380Sstevel@tonic-gate uint_t cp;
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate len = MBLKL(mp);
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate /* decrypted output goes into the new data buffer */
5430Sstevel@tonic-gate lastinput = iptr = (char *)mp->b_rptr;
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate savedbytes = tmi->dec_data.bytes % tmi->dec_data.blocklen;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * Save the input CFB_BLKSZ bytes at a time.
5490Sstevel@tonic-gate * We are trying to decrypt in-place, but need to keep
5500Sstevel@tonic-gate * a small sliding window of encrypted text to be
5510Sstevel@tonic-gate * used to construct the feedback buffer.
5520Sstevel@tonic-gate */
5530Sstevel@tonic-gate cp = ((tmi->dec_data.blocklen - savedbytes) > len ? len :
5540Sstevel@tonic-gate tmi->dec_data.blocklen - savedbytes);
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate bcopy(lastinput, tmi->dec_data.saveblock + savedbytes, cp);
5570Sstevel@tonic-gate savedbytes += cp;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate lastinput += cp;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate while (iptr < (char *)mp->b_wptr) {
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate * Do DES-ECB.
5640Sstevel@tonic-gate * The first time this runs, the 'tmi->dec_data.block' will
5650Sstevel@tonic-gate * contain the initialization vector that should have been
5660Sstevel@tonic-gate * passed in with the SETUP ioctl.
5670Sstevel@tonic-gate */
5680Sstevel@tonic-gate if (!(tmi->dec_data.bytes % CFB_BLKSZ)) {
5690Sstevel@tonic-gate int retval;
5700Sstevel@tonic-gate retval = kef_crypt(&tmi->dec_data,
5710Sstevel@tonic-gate tmi->dec_data.block,
5720Sstevel@tonic-gate CRYPTO_DATA_RAW,
5730Sstevel@tonic-gate tmi->dec_data.blocklen,
5740Sstevel@tonic-gate CRYPT_ENCRYPT);
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate if (retval != CRYPTO_SUCCESS) {
5770Sstevel@tonic-gate #ifdef DEBUG
5780Sstevel@tonic-gate cmn_err(CE_WARN, "des_cfb_decrypt: kef_crypt "
5790Sstevel@tonic-gate "failed - status 0x%0x", retval);
5800Sstevel@tonic-gate #endif
5810Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
5820Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
5830Sstevel@tonic-gate *mp->b_rptr = EIO;
5840Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
5850Sstevel@tonic-gate freemsg(mp->b_cont);
5860Sstevel@tonic-gate mp->b_cont = NULL;
5870Sstevel@tonic-gate qreply(WR(q), mp);
5880Sstevel@tonic-gate return (NULL);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate * To decrypt, XOR the input with the output from the DES call
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate *(iptr++) ^= tmi->dec_data.block[tmi->dec_data.bytes %
5960Sstevel@tonic-gate CFB_BLKSZ];
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate tmi->dec_data.bytes++;
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate * Feedback the encrypted input for next DES call.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate if (!(tmi->dec_data.bytes % tmi->dec_data.blocklen)) {
6040Sstevel@tonic-gate char *dbptr = tmi->dec_data.block;
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate * Get the last bits of input from the previous block
6070Sstevel@tonic-gate * that we haven't yet processed.
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate if (savedbytes > 0) {
6100Sstevel@tonic-gate bcopy(tmi->dec_data.saveblock,
6110Sstevel@tonic-gate dbptr, savedbytes);
6120Sstevel@tonic-gate dbptr += savedbytes;
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate savedbytes = 0;
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate * This block makes sure that our local
6190Sstevel@tonic-gate * buffer of input data is full and can
6200Sstevel@tonic-gate * be accessed from the beginning.
6210Sstevel@tonic-gate */
6220Sstevel@tonic-gate if (lastinput < (char *)mp->b_wptr) {
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate /* How many bytes are left in the mblk? */
6250Sstevel@tonic-gate cp = (((char *)mp->b_wptr - lastinput) >
6260Sstevel@tonic-gate tmi->dec_data.blocklen ?
6270Sstevel@tonic-gate tmi->dec_data.blocklen :
6280Sstevel@tonic-gate (char *)mp->b_wptr - lastinput);
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /* copy what we need */
6310Sstevel@tonic-gate bcopy(lastinput, tmi->dec_data.saveblock,
6320Sstevel@tonic-gate cp);
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate lastinput += cp;
6350Sstevel@tonic-gate savedbytes = cp;
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate return (mp);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate * crc32_calc
6450Sstevel@tonic-gate *
6460Sstevel@tonic-gate * Compute a CRC32 checksum on the input
6470Sstevel@tonic-gate */
6480Sstevel@tonic-gate static int
crc32_calc(uchar_t * buf,uchar_t * input,uint_t len)6490Sstevel@tonic-gate crc32_calc(uchar_t *buf, uchar_t *input, uint_t len)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate uint32_t crc;
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate CRC32(crc, input, len, 0, crc32_table);
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate buf[0] = (uchar_t)(crc & 0xff);
6560Sstevel@tonic-gate buf[1] = (uchar_t)((crc >> 8) & 0xff);
6570Sstevel@tonic-gate buf[2] = (uchar_t)((crc >> 16) & 0xff);
6580Sstevel@tonic-gate buf[3] = (uchar_t)((crc >> 24) & 0xff);
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate return (CRYPTO_SUCCESS);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate static int
kef_digest(crypto_mech_type_t digest_type,uchar_t * input,uint_t inlen,uchar_t * output,uint_t hashlen)6640Sstevel@tonic-gate kef_digest(crypto_mech_type_t digest_type,
6650Sstevel@tonic-gate uchar_t *input, uint_t inlen,
6660Sstevel@tonic-gate uchar_t *output, uint_t hashlen)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate iovec_t v1, v2;
6690Sstevel@tonic-gate crypto_data_t d1, d2;
6700Sstevel@tonic-gate crypto_mechanism_t mech;
6710Sstevel@tonic-gate int rv;
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate mech.cm_type = digest_type;
6740Sstevel@tonic-gate mech.cm_param = 0;
6750Sstevel@tonic-gate mech.cm_param_len = 0;
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate v1.iov_base = (void *)input;
6780Sstevel@tonic-gate v1.iov_len = inlen;
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate d1.cd_format = CRYPTO_DATA_RAW;
6810Sstevel@tonic-gate d1.cd_offset = 0;
6820Sstevel@tonic-gate d1.cd_length = v1.iov_len;
6830Sstevel@tonic-gate d1.cd_raw = v1;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate v2.iov_base = (void *)output;
6860Sstevel@tonic-gate v2.iov_len = hashlen;
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate d2.cd_format = CRYPTO_DATA_RAW;
6890Sstevel@tonic-gate d2.cd_offset = 0;
6900Sstevel@tonic-gate d2.cd_length = v2.iov_len;
6910Sstevel@tonic-gate d2.cd_raw = v2;
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate rv = crypto_digest(&mech, &d1, &d2, NULL);
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate return (rv);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate * sha1_calc
7000Sstevel@tonic-gate *
7010Sstevel@tonic-gate * Get a SHA1 hash on the input data.
7020Sstevel@tonic-gate */
7030Sstevel@tonic-gate static int
sha1_calc(uchar_t * output,uchar_t * input,uint_t inlen)7040Sstevel@tonic-gate sha1_calc(uchar_t *output, uchar_t *input, uint_t inlen)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate int rv;
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate rv = kef_digest(sha1_hash_mech, input, inlen, output, SHA1_HASHSIZE);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate return (rv);
7110Sstevel@tonic-gate }
7120Sstevel@tonic-gate
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate * Get an MD5 hash on the input data.
7150Sstevel@tonic-gate * md5_calc
7160Sstevel@tonic-gate *
7170Sstevel@tonic-gate */
7180Sstevel@tonic-gate static int
md5_calc(uchar_t * output,uchar_t * input,uint_t inlen)7190Sstevel@tonic-gate md5_calc(uchar_t *output, uchar_t *input, uint_t inlen)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate int rv;
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate rv = kef_digest(md5_hash_mech, input, inlen, output, MD5_HASHSIZE);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate return (rv);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate * nfold
7300Sstevel@tonic-gate * duplicate the functionality of the krb5_nfold function from
7310Sstevel@tonic-gate * the userland kerberos mech.
7320Sstevel@tonic-gate * This is needed to derive keys for use with 3DES/SHA1-HMAC
7330Sstevel@tonic-gate * ciphers.
7340Sstevel@tonic-gate */
7350Sstevel@tonic-gate static void
nfold(int inbits,uchar_t * in,int outbits,uchar_t * out)7360Sstevel@tonic-gate nfold(int inbits, uchar_t *in, int outbits, uchar_t *out)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate int a, b, c, lcm;
7390Sstevel@tonic-gate int byte, i, msbit;
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate inbits >>= 3;
7420Sstevel@tonic-gate outbits >>= 3;
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate /* first compute lcm(n,k) */
7450Sstevel@tonic-gate a = outbits;
7460Sstevel@tonic-gate b = inbits;
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate while (b != 0) {
7490Sstevel@tonic-gate c = b;
7500Sstevel@tonic-gate b = a%b;
7510Sstevel@tonic-gate a = c;
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate lcm = outbits*inbits/a;
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate /* now do the real work */
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate bzero(out, outbits);
7590Sstevel@tonic-gate byte = 0;
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate * Compute the msbit in k which gets added into this byte
7630Sstevel@tonic-gate * first, start with the msbit in the first, unrotated byte
7640Sstevel@tonic-gate * then, for each byte, shift to the right for each repetition
7650Sstevel@tonic-gate * last, pick out the correct byte within that shifted repetition
7660Sstevel@tonic-gate */
7670Sstevel@tonic-gate for (i = lcm-1; i >= 0; i--) {
7680Sstevel@tonic-gate msbit = (((inbits<<3)-1)
7690Sstevel@tonic-gate +(((inbits<<3)+13)*(i/inbits))
7700Sstevel@tonic-gate +((inbits-(i%inbits))<<3)) %(inbits<<3);
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate /* pull out the byte value itself */
7730Sstevel@tonic-gate byte += (((in[((inbits-1)-(msbit>>3))%inbits]<<8)|
7740Sstevel@tonic-gate (in[((inbits)-(msbit>>3))%inbits]))
7750Sstevel@tonic-gate >>((msbit&7)+1))&0xff;
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate /* do the addition */
7780Sstevel@tonic-gate byte += out[i%outbits];
7790Sstevel@tonic-gate out[i%outbits] = byte&0xff;
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate byte >>= 8;
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate /* if there's a carry bit left over, add it back in */
7850Sstevel@tonic-gate if (byte) {
7860Sstevel@tonic-gate for (i = outbits-1; i >= 0; i--) {
7870Sstevel@tonic-gate /* do the addition */
7880Sstevel@tonic-gate byte += out[i];
7890Sstevel@tonic-gate out[i] = byte&0xff;
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /* keep around the carry bit, if any */
7920Sstevel@tonic-gate byte >>= 8;
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate #define smask(step) ((1<<step)-1)
7980Sstevel@tonic-gate #define pstep(x, step) (((x)&smask(step))^(((x)>>step)&smask(step)))
7990Sstevel@tonic-gate #define parity_char(x) pstep(pstep(pstep((x), 4), 2), 1)
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate /*
8020Sstevel@tonic-gate * Duplicate the functionality of the "dk_derive_key" function
8030Sstevel@tonic-gate * in the Kerberos mechanism.
8040Sstevel@tonic-gate */
8050Sstevel@tonic-gate static int
derive_key(struct cipher_data_t * cdata,uchar_t * constdata,int constlen,char * dkey,int keybytes,int blocklen)8060Sstevel@tonic-gate derive_key(struct cipher_data_t *cdata, uchar_t *constdata,
8070Sstevel@tonic-gate int constlen, char *dkey, int keybytes,
8080Sstevel@tonic-gate int blocklen)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate int rv = 0;
8110Sstevel@tonic-gate int n = 0, i;
8120Sstevel@tonic-gate char *inblock;
8130Sstevel@tonic-gate char *rawkey;
8140Sstevel@tonic-gate char *zeroblock;
8150Sstevel@tonic-gate char *saveblock;
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate inblock = kmem_zalloc(blocklen, KM_SLEEP);
8180Sstevel@tonic-gate rawkey = kmem_zalloc(keybytes, KM_SLEEP);
8190Sstevel@tonic-gate zeroblock = kmem_zalloc(blocklen, KM_SLEEP);
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate if (constlen == blocklen)
8220Sstevel@tonic-gate bcopy(constdata, inblock, blocklen);
8230Sstevel@tonic-gate else
8240Sstevel@tonic-gate nfold(constlen * 8, constdata,
8250Sstevel@tonic-gate blocklen * 8, (uchar_t *)inblock);
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate * zeroblock is an IV of all 0's.
8290Sstevel@tonic-gate *
8300Sstevel@tonic-gate * The "block" section of the cdata record is used as the
8310Sstevel@tonic-gate * IV for crypto operations in the kef_crypt function.
8320Sstevel@tonic-gate *
8330Sstevel@tonic-gate * We use 'block' as a generic IV data buffer because it
8340Sstevel@tonic-gate * is attached to the stream state data and thus can
8350Sstevel@tonic-gate * be used to hold information that must carry over
8360Sstevel@tonic-gate * from processing of one mblk to another.
8370Sstevel@tonic-gate *
8380Sstevel@tonic-gate * Here, we save the current IV and replace it with
8390Sstevel@tonic-gate * and empty IV (all 0's) for use when deriving the
8400Sstevel@tonic-gate * keys. Once the key derivation is done, we swap the
8410Sstevel@tonic-gate * old IV back into place.
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate saveblock = cdata->block;
8440Sstevel@tonic-gate cdata->block = zeroblock;
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate while (n < keybytes) {
8470Sstevel@tonic-gate rv = kef_crypt(cdata, inblock, CRYPTO_DATA_RAW,
8480Sstevel@tonic-gate blocklen, CRYPT_ENCRYPT);
8490Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
8500Sstevel@tonic-gate /* put the original IV block back in place */
8510Sstevel@tonic-gate cdata->block = saveblock;
8520Sstevel@tonic-gate cmn_err(CE_WARN, "failed to derive a key: %0x", rv);
8530Sstevel@tonic-gate goto cleanup;
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate if (keybytes - n < blocklen) {
8570Sstevel@tonic-gate bcopy(inblock, rawkey+n, (keybytes-n));
8580Sstevel@tonic-gate break;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate bcopy(inblock, rawkey+n, blocklen);
8610Sstevel@tonic-gate n += blocklen;
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate /* put the original IV block back in place */
8640Sstevel@tonic-gate cdata->block = saveblock;
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate /* finally, make the key */
8670Sstevel@tonic-gate if (cdata->method == CRYPT_METHOD_DES3_CBC_SHA1) {
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate * 3DES key derivation requires that we make sure the
8700Sstevel@tonic-gate * key has the proper parity.
8710Sstevel@tonic-gate */
8720Sstevel@tonic-gate for (i = 0; i < 3; i++) {
8730Sstevel@tonic-gate bcopy(rawkey+(i*7), dkey+(i*8), 7);
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate /* 'dkey' is our derived key output buffer */
8760Sstevel@tonic-gate dkey[i*8+7] = (((dkey[i*8]&1)<<1) |
8770Sstevel@tonic-gate ((dkey[i*8+1]&1)<<2) |
8780Sstevel@tonic-gate ((dkey[i*8+2]&1)<<3) |
8790Sstevel@tonic-gate ((dkey[i*8+3]&1)<<4) |
8800Sstevel@tonic-gate ((dkey[i*8+4]&1)<<5) |
8810Sstevel@tonic-gate ((dkey[i*8+5]&1)<<6) |
8820Sstevel@tonic-gate ((dkey[i*8+6]&1)<<7));
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate for (n = 0; n < 8; n++) {
8850Sstevel@tonic-gate dkey[i*8 + n] &= 0xfe;
8860Sstevel@tonic-gate dkey[i*8 + n] |= 1^parity_char(dkey[i*8 + n]);
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate } else if (IS_AES_METHOD(cdata->method)) {
8900Sstevel@tonic-gate bcopy(rawkey, dkey, keybytes);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate cleanup:
8930Sstevel@tonic-gate kmem_free(inblock, blocklen);
8940Sstevel@tonic-gate kmem_free(zeroblock, blocklen);
8950Sstevel@tonic-gate kmem_free(rawkey, keybytes);
8960Sstevel@tonic-gate return (rv);
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate /*
9000Sstevel@tonic-gate * create_derived_keys
9010Sstevel@tonic-gate *
9020Sstevel@tonic-gate * Algorithm for deriving a new key and an HMAC key
9030Sstevel@tonic-gate * before computing the 3DES-SHA1-HMAC operation on the plaintext
9040Sstevel@tonic-gate * This algorithm matches the work done by Kerberos mechanism
9050Sstevel@tonic-gate * in userland.
9060Sstevel@tonic-gate */
9070Sstevel@tonic-gate static int
create_derived_keys(struct cipher_data_t * cdata,uint32_t usage,crypto_key_t * enckey,crypto_key_t * hmackey)9080Sstevel@tonic-gate create_derived_keys(struct cipher_data_t *cdata, uint32_t usage,
9090Sstevel@tonic-gate crypto_key_t *enckey, crypto_key_t *hmackey)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate uchar_t constdata[K5CLENGTH];
9120Sstevel@tonic-gate int keybytes;
9130Sstevel@tonic-gate int rv;
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate constdata[0] = (usage>>24)&0xff;
9160Sstevel@tonic-gate constdata[1] = (usage>>16)&0xff;
9170Sstevel@tonic-gate constdata[2] = (usage>>8)&0xff;
9180Sstevel@tonic-gate constdata[3] = usage & 0xff;
9190Sstevel@tonic-gate /* Use "0xAA" for deriving encryption key */
9200Sstevel@tonic-gate constdata[4] = 0xAA; /* from MIT Kerberos code */
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate enckey->ck_length = cdata->keylen * 8;
9230Sstevel@tonic-gate enckey->ck_format = CRYPTO_KEY_RAW;
9240Sstevel@tonic-gate enckey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate switch (cdata->method) {
9270Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
9280Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
9290Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
9300Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
9310Sstevel@tonic-gate keybytes = 8;
9320Sstevel@tonic-gate break;
9330Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
9340Sstevel@tonic-gate keybytes = CRYPT_DES3_KEYBYTES;
9350Sstevel@tonic-gate break;
9360Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
9370Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
9380Sstevel@tonic-gate keybytes = CRYPT_ARCFOUR_KEYBYTES;
9390Sstevel@tonic-gate break;
9400Sstevel@tonic-gate case CRYPT_METHOD_AES128:
9410Sstevel@tonic-gate keybytes = CRYPT_AES128_KEYBYTES;
9420Sstevel@tonic-gate break;
9430Sstevel@tonic-gate case CRYPT_METHOD_AES256:
9440Sstevel@tonic-gate keybytes = CRYPT_AES256_KEYBYTES;
9450Sstevel@tonic-gate break;
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate /* derive main crypto key */
9490Sstevel@tonic-gate rv = derive_key(cdata, constdata, sizeof (constdata),
9500Sstevel@tonic-gate enckey->ck_data, keybytes, cdata->blocklen);
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) {
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate /* Use "0x55" for deriving mac key */
9550Sstevel@tonic-gate constdata[4] = 0x55;
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate hmackey->ck_length = cdata->keylen * 8;
9580Sstevel@tonic-gate hmackey->ck_format = CRYPTO_KEY_RAW;
9590Sstevel@tonic-gate hmackey->ck_data = kmem_zalloc(cdata->keylen, KM_SLEEP);
9600Sstevel@tonic-gate
9610Sstevel@tonic-gate rv = derive_key(cdata, constdata, sizeof (constdata),
9620Sstevel@tonic-gate hmackey->ck_data, keybytes,
9630Sstevel@tonic-gate cdata->blocklen);
9640Sstevel@tonic-gate } else {
9650Sstevel@tonic-gate cmn_err(CE_WARN, "failed to derive crypto key: %02x", rv);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate return (rv);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate * Compute 3-DES crypto and HMAC.
9730Sstevel@tonic-gate */
9740Sstevel@tonic-gate static int
kef_decr_hmac(struct cipher_data_t * cdata,mblk_t * mp,int length,char * hmac,int hmaclen)9750Sstevel@tonic-gate kef_decr_hmac(struct cipher_data_t *cdata,
9760Sstevel@tonic-gate mblk_t *mp, int length,
9770Sstevel@tonic-gate char *hmac, int hmaclen)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate int rv = CRYPTO_FAILED;
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate crypto_mechanism_t encr_mech;
9820Sstevel@tonic-gate crypto_mechanism_t mac_mech;
9830Sstevel@tonic-gate crypto_data_t dd;
9840Sstevel@tonic-gate crypto_data_t mac;
9850Sstevel@tonic-gate iovec_t v1;
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate ASSERT(cdata != NULL);
9880Sstevel@tonic-gate ASSERT(mp != NULL);
9890Sstevel@tonic-gate ASSERT(hmac != NULL);
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate bzero(&dd, sizeof (dd));
9920Sstevel@tonic-gate dd.cd_format = CRYPTO_DATA_MBLK;
9930Sstevel@tonic-gate dd.cd_offset = 0;
9940Sstevel@tonic-gate dd.cd_length = length;
9950Sstevel@tonic-gate dd.cd_mp = mp;
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate v1.iov_base = hmac;
9980Sstevel@tonic-gate v1.iov_len = hmaclen;
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate mac.cd_format = CRYPTO_DATA_RAW;
10010Sstevel@tonic-gate mac.cd_offset = 0;
10020Sstevel@tonic-gate mac.cd_length = hmaclen;
10030Sstevel@tonic-gate mac.cd_raw = v1;
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate * cdata->block holds the IVEC
10070Sstevel@tonic-gate */
10080Sstevel@tonic-gate encr_mech.cm_type = cdata->mech_type;
10090Sstevel@tonic-gate encr_mech.cm_param = cdata->block;
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate if (cdata->block != NULL)
10120Sstevel@tonic-gate encr_mech.cm_param_len = cdata->blocklen;
10130Sstevel@tonic-gate else
10140Sstevel@tonic-gate encr_mech.cm_param_len = 0;
10150Sstevel@tonic-gate
10160Sstevel@tonic-gate rv = crypto_decrypt(&encr_mech, &dd, &cdata->d_encr_key,
10170Sstevel@tonic-gate cdata->enc_tmpl, NULL, NULL);
10180Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
10190Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_decrypt failed: %0x", rv);
10200Sstevel@tonic-gate return (rv);
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate mac_mech.cm_type = sha1_hmac_mech;
10240Sstevel@tonic-gate mac_mech.cm_param = NULL;
10250Sstevel@tonic-gate mac_mech.cm_param_len = 0;
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate * Compute MAC of the plaintext decrypted above.
10290Sstevel@tonic-gate */
10300Sstevel@tonic-gate rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
10310Sstevel@tonic-gate cdata->hmac_tmpl, &mac, NULL);
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
10340Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate
10370Sstevel@tonic-gate return (rv);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate * Compute 3-DES crypto and HMAC.
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate static int
kef_encr_hmac(struct cipher_data_t * cdata,mblk_t * mp,int length,char * hmac,int hmaclen)10440Sstevel@tonic-gate kef_encr_hmac(struct cipher_data_t *cdata,
10450Sstevel@tonic-gate mblk_t *mp, int length,
10460Sstevel@tonic-gate char *hmac, int hmaclen)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate int rv = CRYPTO_FAILED;
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate crypto_mechanism_t encr_mech;
10510Sstevel@tonic-gate crypto_mechanism_t mac_mech;
10520Sstevel@tonic-gate crypto_data_t dd;
10530Sstevel@tonic-gate crypto_data_t mac;
10540Sstevel@tonic-gate iovec_t v1;
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate ASSERT(cdata != NULL);
10570Sstevel@tonic-gate ASSERT(mp != NULL);
10580Sstevel@tonic-gate ASSERT(hmac != NULL);
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate bzero(&dd, sizeof (dd));
10610Sstevel@tonic-gate dd.cd_format = CRYPTO_DATA_MBLK;
10620Sstevel@tonic-gate dd.cd_offset = 0;
10630Sstevel@tonic-gate dd.cd_length = length;
10640Sstevel@tonic-gate dd.cd_mp = mp;
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate v1.iov_base = hmac;
10670Sstevel@tonic-gate v1.iov_len = hmaclen;
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate mac.cd_format = CRYPTO_DATA_RAW;
10700Sstevel@tonic-gate mac.cd_offset = 0;
10710Sstevel@tonic-gate mac.cd_length = hmaclen;
10720Sstevel@tonic-gate mac.cd_raw = v1;
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate * cdata->block holds the IVEC
10760Sstevel@tonic-gate */
10770Sstevel@tonic-gate encr_mech.cm_type = cdata->mech_type;
10780Sstevel@tonic-gate encr_mech.cm_param = cdata->block;
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate if (cdata->block != NULL)
10810Sstevel@tonic-gate encr_mech.cm_param_len = cdata->blocklen;
10820Sstevel@tonic-gate else
10830Sstevel@tonic-gate encr_mech.cm_param_len = 0;
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate mac_mech.cm_type = sha1_hmac_mech;
10860Sstevel@tonic-gate mac_mech.cm_param = NULL;
10870Sstevel@tonic-gate mac_mech.cm_param_len = 0;
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate rv = crypto_mac(&mac_mech, &dd, &cdata->d_hmac_key,
10900Sstevel@tonic-gate cdata->hmac_tmpl, &mac, NULL);
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
10930Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
10940Sstevel@tonic-gate return (rv);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate rv = crypto_encrypt(&encr_mech, &dd, &cdata->d_encr_key,
10980Sstevel@tonic-gate cdata->enc_tmpl, NULL, NULL);
10990Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
11000Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_encrypt failed: %0x", rv);
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate return (rv);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate * kef_crypt
11080Sstevel@tonic-gate *
11090Sstevel@tonic-gate * Use the Kernel encryption framework to provide the
11100Sstevel@tonic-gate * crypto operations for the indicated data.
11110Sstevel@tonic-gate */
11120Sstevel@tonic-gate static int
kef_crypt(struct cipher_data_t * cdata,void * indata,crypto_data_format_t fmt,size_t length,int mode)11130Sstevel@tonic-gate kef_crypt(struct cipher_data_t *cdata,
11140Sstevel@tonic-gate void *indata, crypto_data_format_t fmt,
11150Sstevel@tonic-gate size_t length, int mode)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate int rv = CRYPTO_FAILED;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate crypto_mechanism_t mech;
11200Sstevel@tonic-gate crypto_key_t crkey;
11210Sstevel@tonic-gate iovec_t v1;
11220Sstevel@tonic-gate crypto_data_t d1;
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate ASSERT(cdata != NULL);
11250Sstevel@tonic-gate ASSERT(indata != NULL);
11260Sstevel@tonic-gate ASSERT(fmt == CRYPTO_DATA_RAW || fmt == CRYPTO_DATA_MBLK);
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate bzero(&crkey, sizeof (crkey));
11290Sstevel@tonic-gate bzero(&d1, sizeof (d1));
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate crkey.ck_format = CRYPTO_KEY_RAW;
11320Sstevel@tonic-gate crkey.ck_data = cdata->key;
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate /* keys are measured in bits, not bytes, so multiply by 8 */
11350Sstevel@tonic-gate crkey.ck_length = cdata->keylen * 8;
11360Sstevel@tonic-gate
11370Sstevel@tonic-gate if (fmt == CRYPTO_DATA_RAW) {
11380Sstevel@tonic-gate v1.iov_base = (char *)indata;
11390Sstevel@tonic-gate v1.iov_len = length;
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate d1.cd_format = fmt;
11430Sstevel@tonic-gate d1.cd_offset = 0;
11440Sstevel@tonic-gate d1.cd_length = length;
11450Sstevel@tonic-gate if (fmt == CRYPTO_DATA_RAW)
11460Sstevel@tonic-gate d1.cd_raw = v1;
11470Sstevel@tonic-gate else if (fmt == CRYPTO_DATA_MBLK)
11480Sstevel@tonic-gate d1.cd_mp = (mblk_t *)indata;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate mech.cm_type = cdata->mech_type;
11510Sstevel@tonic-gate mech.cm_param = cdata->block;
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate * cdata->block holds the IVEC
11540Sstevel@tonic-gate */
11550Sstevel@tonic-gate if (cdata->block != NULL)
11560Sstevel@tonic-gate mech.cm_param_len = cdata->blocklen;
11570Sstevel@tonic-gate else
11580Sstevel@tonic-gate mech.cm_param_len = 0;
11590Sstevel@tonic-gate
11600Sstevel@tonic-gate /*
11610Sstevel@tonic-gate * encrypt and decrypt in-place
11620Sstevel@tonic-gate */
11630Sstevel@tonic-gate if (mode == CRYPT_ENCRYPT)
11640Sstevel@tonic-gate rv = crypto_encrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
11650Sstevel@tonic-gate else
11660Sstevel@tonic-gate rv = crypto_decrypt(&mech, &d1, &crkey, NULL, NULL, NULL);
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
11690Sstevel@tonic-gate cmn_err(CE_WARN, "%s returned error %08x",
11700Sstevel@tonic-gate (mode == CRYPT_ENCRYPT ? "crypto_encrypt" :
11710Sstevel@tonic-gate "crypto_decrypt"), rv);
11720Sstevel@tonic-gate return (CRYPTO_FAILED);
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate return (rv);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate static int
do_hmac(crypto_mech_type_t mech,crypto_key_t * key,char * data,int datalen,char * hmac,int hmaclen)11790Sstevel@tonic-gate do_hmac(crypto_mech_type_t mech,
11800Sstevel@tonic-gate crypto_key_t *key,
11810Sstevel@tonic-gate char *data, int datalen,
11820Sstevel@tonic-gate char *hmac, int hmaclen)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate int rv = 0;
11850Sstevel@tonic-gate crypto_mechanism_t mac_mech;
11860Sstevel@tonic-gate crypto_data_t dd;
11870Sstevel@tonic-gate crypto_data_t mac;
11880Sstevel@tonic-gate iovec_t vdata, vmac;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate mac_mech.cm_type = mech;
11910Sstevel@tonic-gate mac_mech.cm_param = NULL;
11920Sstevel@tonic-gate mac_mech.cm_param_len = 0;
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate vdata.iov_base = data;
11950Sstevel@tonic-gate vdata.iov_len = datalen;
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate bzero(&dd, sizeof (dd));
11980Sstevel@tonic-gate dd.cd_format = CRYPTO_DATA_RAW;
11990Sstevel@tonic-gate dd.cd_offset = 0;
12000Sstevel@tonic-gate dd.cd_length = datalen;
12010Sstevel@tonic-gate dd.cd_raw = vdata;
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate vmac.iov_base = hmac;
12040Sstevel@tonic-gate vmac.iov_len = hmaclen;
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate mac.cd_format = CRYPTO_DATA_RAW;
12070Sstevel@tonic-gate mac.cd_offset = 0;
12080Sstevel@tonic-gate mac.cd_length = hmaclen;
12090Sstevel@tonic-gate mac.cd_raw = vmac;
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate /*
12120Sstevel@tonic-gate * Compute MAC of the plaintext decrypted above.
12130Sstevel@tonic-gate */
12140Sstevel@tonic-gate rv = crypto_mac(&mac_mech, &dd, key, NULL, &mac, NULL);
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
12170Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_mac failed: %0x", rv);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate return (rv);
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate
12230Sstevel@tonic-gate #define XOR_BLOCK(src, dst) \
12240Sstevel@tonic-gate (dst)[0] ^= (src)[0]; \
12250Sstevel@tonic-gate (dst)[1] ^= (src)[1]; \
12260Sstevel@tonic-gate (dst)[2] ^= (src)[2]; \
12270Sstevel@tonic-gate (dst)[3] ^= (src)[3]; \
12280Sstevel@tonic-gate (dst)[4] ^= (src)[4]; \
12290Sstevel@tonic-gate (dst)[5] ^= (src)[5]; \
12300Sstevel@tonic-gate (dst)[6] ^= (src)[6]; \
12310Sstevel@tonic-gate (dst)[7] ^= (src)[7]; \
12320Sstevel@tonic-gate (dst)[8] ^= (src)[8]; \
12330Sstevel@tonic-gate (dst)[9] ^= (src)[9]; \
12340Sstevel@tonic-gate (dst)[10] ^= (src)[10]; \
12350Sstevel@tonic-gate (dst)[11] ^= (src)[11]; \
12360Sstevel@tonic-gate (dst)[12] ^= (src)[12]; \
12370Sstevel@tonic-gate (dst)[13] ^= (src)[13]; \
12380Sstevel@tonic-gate (dst)[14] ^= (src)[14]; \
12390Sstevel@tonic-gate (dst)[15] ^= (src)[15]
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate #define xorblock(x, y) XOR_BLOCK(y, x)
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate static int
aes_cbc_cts_encrypt(struct tmodinfo * tmi,uchar_t * plain,size_t length)12440Sstevel@tonic-gate aes_cbc_cts_encrypt(struct tmodinfo *tmi, uchar_t *plain, size_t length)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate int result = CRYPTO_SUCCESS;
12470Sstevel@tonic-gate unsigned char tmp[DEFAULT_AES_BLOCKLEN];
12480Sstevel@tonic-gate unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
12490Sstevel@tonic-gate unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
12500Sstevel@tonic-gate int nblocks = 0, blockno;
12510Sstevel@tonic-gate crypto_data_t ct, pt;
12520Sstevel@tonic-gate crypto_mechanism_t mech;
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate mech.cm_type = tmi->enc_data.mech_type;
12550Sstevel@tonic-gate if (tmi->enc_data.ivlen > 0 && tmi->enc_data.ivec != NULL) {
12560Sstevel@tonic-gate bcopy(tmi->enc_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
12570Sstevel@tonic-gate } else {
12580Sstevel@tonic-gate bzero(tmp, sizeof (tmp));
12590Sstevel@tonic-gate }
12608143SPeter.Shoults@Sun.COM mech.cm_param = NULL;
12618143SPeter.Shoults@Sun.COM mech.cm_param_len = 0;
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate bzero(&ct, sizeof (crypto_data_t));
12660Sstevel@tonic-gate bzero(&pt, sizeof (crypto_data_t));
12670Sstevel@tonic-gate
12680Sstevel@tonic-gate if (nblocks == 1) {
12690Sstevel@tonic-gate pt.cd_format = CRYPTO_DATA_RAW;
12700Sstevel@tonic-gate pt.cd_length = length;
12710Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)plain;
12720Sstevel@tonic-gate pt.cd_raw.iov_len = length;
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate result = crypto_encrypt(&mech, &pt,
12750Sstevel@tonic-gate &tmi->enc_data.d_encr_key, NULL, NULL, NULL);
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
12780Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
12790Sstevel@tonic-gate "crypto_encrypt failed: %0x", result);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate } else {
12820Sstevel@tonic-gate size_t nleft;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate ct.cd_format = CRYPTO_DATA_RAW;
12850Sstevel@tonic-gate ct.cd_offset = 0;
12860Sstevel@tonic-gate ct.cd_length = DEFAULT_AES_BLOCKLEN;
12870Sstevel@tonic-gate
12880Sstevel@tonic-gate pt.cd_format = CRYPTO_DATA_RAW;
12890Sstevel@tonic-gate pt.cd_offset = 0;
12900Sstevel@tonic-gate pt.cd_length = DEFAULT_AES_BLOCKLEN;
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate result = crypto_encrypt_init(&mech,
12930Sstevel@tonic-gate &tmi->enc_data.d_encr_key,
12940Sstevel@tonic-gate tmi->enc_data.enc_tmpl,
12950Sstevel@tonic-gate &tmi->enc_data.ctx, NULL);
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
12980Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
12990Sstevel@tonic-gate "crypto_encrypt_init failed: %0x", result);
13000Sstevel@tonic-gate goto cleanup;
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate
13030Sstevel@tonic-gate for (blockno = 0; blockno < nblocks - 2; blockno++) {
13040Sstevel@tonic-gate xorblock(tmp, plain + blockno * DEFAULT_AES_BLOCKLEN);
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp;
13070Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)plain +
13100Sstevel@tonic-gate blockno * DEFAULT_AES_BLOCKLEN;
13110Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate result = crypto_encrypt_update(tmi->enc_data.ctx,
13140Sstevel@tonic-gate &pt, &ct, NULL);
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
13170Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13180Sstevel@tonic-gate "crypto_encrypt_update failed: %0x",
13190Sstevel@tonic-gate result);
13200Sstevel@tonic-gate goto cleanup;
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate /* copy result over original bytes */
13230Sstevel@tonic-gate /* make another copy for the next XOR step */
13240Sstevel@tonic-gate bcopy(plain + blockno * DEFAULT_AES_BLOCKLEN,
13250Sstevel@tonic-gate tmp, DEFAULT_AES_BLOCKLEN);
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate /* XOR cipher text from n-3 with plain text from n-2 */
13280Sstevel@tonic-gate xorblock(tmp, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp;
13310Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13320Sstevel@tonic-gate
13330Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)tmp2;
13340Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate /* encrypt XOR-ed block N-2 */
13370Sstevel@tonic-gate result = crypto_encrypt_update(tmi->enc_data.ctx,
13380Sstevel@tonic-gate &pt, &ct, NULL);
13390Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
13400Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13410Sstevel@tonic-gate "crypto_encrypt_update(2) failed: %0x",
13420Sstevel@tonic-gate result);
13430Sstevel@tonic-gate goto cleanup;
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate nleft = length - (nblocks - 1) * DEFAULT_AES_BLOCKLEN;
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate bzero(tmp3, sizeof (tmp3));
13480Sstevel@tonic-gate /* Save final plaintext bytes from n-1 */
13490Sstevel@tonic-gate bcopy(plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
13500Sstevel@tonic-gate nleft);
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate /* Overwrite n-1 with cipher text from n-2 */
13530Sstevel@tonic-gate bcopy(tmp2, plain + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
13540Sstevel@tonic-gate nleft);
13550Sstevel@tonic-gate
13560Sstevel@tonic-gate bcopy(tmp2, tmp, DEFAULT_AES_BLOCKLEN);
13570Sstevel@tonic-gate /* XOR cipher text from n-1 with plain text from n-1 */
13580Sstevel@tonic-gate xorblock(tmp, tmp3);
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp;
13610Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)tmp2;
13640Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate /* encrypt block N-2 */
13670Sstevel@tonic-gate result = crypto_encrypt_update(tmi->enc_data.ctx,
13680Sstevel@tonic-gate &pt, &ct, NULL);
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
13710Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13720Sstevel@tonic-gate "crypto_encrypt_update(3) failed: %0x",
13730Sstevel@tonic-gate result);
13740Sstevel@tonic-gate goto cleanup;
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate bcopy(tmp2, plain + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
13780Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN);
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)tmp2;
13820Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
13830Sstevel@tonic-gate
13840Sstevel@tonic-gate /*
13850Sstevel@tonic-gate * Ignore the output on the final step.
13860Sstevel@tonic-gate */
13870Sstevel@tonic-gate result = crypto_encrypt_final(tmi->enc_data.ctx, &ct, NULL);
13880Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
13890Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_encrypt: "
13900Sstevel@tonic-gate "crypto_encrypt_final(3) failed: %0x",
13910Sstevel@tonic-gate result);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate tmi->enc_data.ctx = NULL;
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate cleanup:
13960Sstevel@tonic-gate bzero(tmp, sizeof (tmp));
13970Sstevel@tonic-gate bzero(tmp2, sizeof (tmp));
13980Sstevel@tonic-gate bzero(tmp3, sizeof (tmp));
13990Sstevel@tonic-gate bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
14000Sstevel@tonic-gate return (result);
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate
14030Sstevel@tonic-gate static int
aes_cbc_cts_decrypt(struct tmodinfo * tmi,uchar_t * buff,size_t length)14040Sstevel@tonic-gate aes_cbc_cts_decrypt(struct tmodinfo *tmi, uchar_t *buff, size_t length)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate int result = CRYPTO_SUCCESS;
14070Sstevel@tonic-gate unsigned char tmp[DEFAULT_AES_BLOCKLEN];
14080Sstevel@tonic-gate unsigned char tmp2[DEFAULT_AES_BLOCKLEN];
14090Sstevel@tonic-gate unsigned char tmp3[DEFAULT_AES_BLOCKLEN];
14100Sstevel@tonic-gate int nblocks = 0, blockno;
14110Sstevel@tonic-gate crypto_data_t ct, pt;
14120Sstevel@tonic-gate crypto_mechanism_t mech;
14130Sstevel@tonic-gate
14140Sstevel@tonic-gate mech.cm_type = tmi->enc_data.mech_type;
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
14170Sstevel@tonic-gate tmi->dec_data.ivlen > 0 && tmi->dec_data.ivec != NULL) {
14180Sstevel@tonic-gate bcopy(tmi->dec_data.ivec, tmp, DEFAULT_AES_BLOCKLEN);
14190Sstevel@tonic-gate } else {
14200Sstevel@tonic-gate bzero(tmp, sizeof (tmp));
14210Sstevel@tonic-gate }
14228143SPeter.Shoults@Sun.COM mech.cm_param_len = 0;
14238143SPeter.Shoults@Sun.COM mech.cm_param = NULL;
14248143SPeter.Shoults@Sun.COM
14250Sstevel@tonic-gate nblocks = (length + DEFAULT_AES_BLOCKLEN - 1) / DEFAULT_AES_BLOCKLEN;
14260Sstevel@tonic-gate
14270Sstevel@tonic-gate bzero(&pt, sizeof (pt));
14280Sstevel@tonic-gate bzero(&ct, sizeof (ct));
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate if (nblocks == 1) {
14310Sstevel@tonic-gate ct.cd_format = CRYPTO_DATA_RAW;
14320Sstevel@tonic-gate ct.cd_length = length;
14330Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)buff;
14340Sstevel@tonic-gate ct.cd_raw.iov_len = length;
14350Sstevel@tonic-gate
14360Sstevel@tonic-gate result = crypto_decrypt(&mech, &ct,
14370Sstevel@tonic-gate &tmi->dec_data.d_encr_key, NULL, NULL, NULL);
14380Sstevel@tonic-gate
14390Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
14400Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14410Sstevel@tonic-gate "crypto_decrypt failed: %0x", result);
14420Sstevel@tonic-gate goto cleanup;
14430Sstevel@tonic-gate }
14440Sstevel@tonic-gate } else {
14450Sstevel@tonic-gate ct.cd_format = CRYPTO_DATA_RAW;
14460Sstevel@tonic-gate ct.cd_offset = 0;
14470Sstevel@tonic-gate ct.cd_length = DEFAULT_AES_BLOCKLEN;
14480Sstevel@tonic-gate
14490Sstevel@tonic-gate pt.cd_format = CRYPTO_DATA_RAW;
14500Sstevel@tonic-gate pt.cd_offset = 0;
14510Sstevel@tonic-gate pt.cd_length = DEFAULT_AES_BLOCKLEN;
14520Sstevel@tonic-gate
14538143SPeter.Shoults@Sun.COM result = crypto_decrypt_init(&mech,
14540Sstevel@tonic-gate &tmi->dec_data.d_encr_key,
14550Sstevel@tonic-gate tmi->dec_data.enc_tmpl,
14560Sstevel@tonic-gate &tmi->dec_data.ctx, NULL);
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
14590Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14600Sstevel@tonic-gate "crypto_decrypt_init failed: %0x", result);
14610Sstevel@tonic-gate goto cleanup;
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate for (blockno = 0; blockno < nblocks - 2; blockno++) {
14640Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)buff +
14650Sstevel@tonic-gate (blockno * DEFAULT_AES_BLOCKLEN);
14660Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp2;
14690Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
14700Sstevel@tonic-gate
14710Sstevel@tonic-gate /*
14720Sstevel@tonic-gate * Save the input to the decrypt so it can
14730Sstevel@tonic-gate * be used later for an XOR operation
14740Sstevel@tonic-gate */
14750Sstevel@tonic-gate bcopy(buff + (blockno * DEFAULT_AES_BLOCKLEN),
14760Sstevel@tonic-gate tmi->dec_data.block, DEFAULT_AES_BLOCKLEN);
14770Sstevel@tonic-gate
14781398Sethindra result = crypto_decrypt_update(tmi->dec_data.ctx,
14790Sstevel@tonic-gate &ct, &pt, NULL);
14800Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
14810Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
14820Sstevel@tonic-gate "crypto_decrypt_update(1) error - "
14830Sstevel@tonic-gate "result = 0x%08x", result);
14840Sstevel@tonic-gate goto cleanup;
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate xorblock(tmp2, tmp);
14870Sstevel@tonic-gate bcopy(tmp2, buff + blockno * DEFAULT_AES_BLOCKLEN,
14880Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN);
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate * The original cipher text is used as the xor
14910Sstevel@tonic-gate * for the next block, save it here.
14920Sstevel@tonic-gate */
14930Sstevel@tonic-gate bcopy(tmi->dec_data.block, tmp, DEFAULT_AES_BLOCKLEN);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)buff +
14960Sstevel@tonic-gate ((nblocks - 2) * DEFAULT_AES_BLOCKLEN);
14970Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
14980Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp2;
14990Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15000Sstevel@tonic-gate
15010Sstevel@tonic-gate result = crypto_decrypt_update(tmi->dec_data.ctx,
15020Sstevel@tonic-gate &ct, &pt, NULL);
15030Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
15040Sstevel@tonic-gate cmn_err(CE_WARN,
15050Sstevel@tonic-gate "aes_cbc_cts_decrypt: "
15060Sstevel@tonic-gate "crypto_decrypt_update(2) error -"
15070Sstevel@tonic-gate " result = 0x%08x", result);
15080Sstevel@tonic-gate goto cleanup;
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate bzero(tmp3, sizeof (tmp3));
15110Sstevel@tonic-gate bcopy(buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN, tmp3,
15120Sstevel@tonic-gate length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15130Sstevel@tonic-gate
15140Sstevel@tonic-gate xorblock(tmp2, tmp3);
15150Sstevel@tonic-gate bcopy(tmp2, buff + (nblocks - 1) * DEFAULT_AES_BLOCKLEN,
15160Sstevel@tonic-gate length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate /* 2nd to last block ... */
15190Sstevel@tonic-gate bcopy(tmp3, tmp2,
15200Sstevel@tonic-gate length - ((nblocks - 1) * DEFAULT_AES_BLOCKLEN));
15210Sstevel@tonic-gate
15220Sstevel@tonic-gate ct.cd_raw.iov_base = (char *)tmp2;
15230Sstevel@tonic-gate ct.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15240Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp3;
15250Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate result = crypto_decrypt_update(tmi->dec_data.ctx,
15280Sstevel@tonic-gate &ct, &pt, NULL);
15290Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
15300Sstevel@tonic-gate cmn_err(CE_WARN,
15310Sstevel@tonic-gate "aes_cbc_cts_decrypt: "
15320Sstevel@tonic-gate "crypto_decrypt_update(3) error - "
15330Sstevel@tonic-gate "result = 0x%08x", result);
15340Sstevel@tonic-gate goto cleanup;
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate xorblock(tmp3, tmp);
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate
15390Sstevel@tonic-gate /* Finally, update the 2nd to last block and we are done. */
15400Sstevel@tonic-gate bcopy(tmp3, buff + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
15410Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN);
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate /* Do Final step, but ignore output */
15440Sstevel@tonic-gate pt.cd_raw.iov_base = (char *)tmp2;
15450Sstevel@tonic-gate pt.cd_raw.iov_len = DEFAULT_AES_BLOCKLEN;
15460Sstevel@tonic-gate result = crypto_decrypt_final(tmi->dec_data.ctx, &pt, NULL);
15470Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
15480Sstevel@tonic-gate cmn_err(CE_WARN, "aes_cbc_cts_decrypt: "
15490Sstevel@tonic-gate "crypto_decrypt_final error - "
15500Sstevel@tonic-gate "result = 0x%0x", result);
15510Sstevel@tonic-gate }
15520Sstevel@tonic-gate tmi->dec_data.ctx = NULL;
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate cleanup:
15560Sstevel@tonic-gate bzero(tmp, sizeof (tmp));
15570Sstevel@tonic-gate bzero(tmp2, sizeof (tmp));
15580Sstevel@tonic-gate bzero(tmp3, sizeof (tmp));
15590Sstevel@tonic-gate bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
15600Sstevel@tonic-gate return (result);
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate
15630Sstevel@tonic-gate /*
15640Sstevel@tonic-gate * AES decrypt
15650Sstevel@tonic-gate *
15660Sstevel@tonic-gate * format of ciphertext when using AES
15670Sstevel@tonic-gate * +-------------+------------+------------+
15680Sstevel@tonic-gate * | confounder | msg-data | hmac |
15690Sstevel@tonic-gate * +-------------+------------+------------+
15700Sstevel@tonic-gate */
15710Sstevel@tonic-gate static mblk_t *
aes_decrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)15720Sstevel@tonic-gate aes_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
15730Sstevel@tonic-gate hash_info_t *hash)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate int result;
15760Sstevel@tonic-gate size_t enclen;
15770Sstevel@tonic-gate size_t inlen;
15780Sstevel@tonic-gate uchar_t hmacbuff[64];
15790Sstevel@tonic-gate uchar_t tmpiv[DEFAULT_AES_BLOCKLEN];
15800Sstevel@tonic-gate
15810Sstevel@tonic-gate inlen = (size_t)MBLKL(mp);
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate enclen = inlen - AES_TRUNCATED_HMAC_LEN;
15840Sstevel@tonic-gate if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
15850Sstevel@tonic-gate tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0) {
15860Sstevel@tonic-gate int nblocks = (enclen + DEFAULT_AES_BLOCKLEN - 1) /
15870Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN;
15880Sstevel@tonic-gate bcopy(mp->b_rptr + DEFAULT_AES_BLOCKLEN * (nblocks - 2),
15890Sstevel@tonic-gate tmpiv, DEFAULT_AES_BLOCKLEN);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate
15920Sstevel@tonic-gate /* AES Decrypt */
15930Sstevel@tonic-gate result = aes_cbc_cts_decrypt(tmi, mp->b_rptr, enclen);
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
15960Sstevel@tonic-gate cmn_err(CE_WARN,
15970Sstevel@tonic-gate "aes_decrypt: aes_cbc_cts_decrypt "
15980Sstevel@tonic-gate "failed - error %0x", result);
15990Sstevel@tonic-gate goto cleanup;
16000Sstevel@tonic-gate }
16010Sstevel@tonic-gate
16020Sstevel@tonic-gate /* Verify the HMAC */
16030Sstevel@tonic-gate result = do_hmac(sha1_hmac_mech,
16040Sstevel@tonic-gate &tmi->dec_data.d_hmac_key,
16050Sstevel@tonic-gate (char *)mp->b_rptr, enclen,
16060Sstevel@tonic-gate (char *)hmacbuff, hash->hash_len);
16070Sstevel@tonic-gate
16080Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
16090Sstevel@tonic-gate cmn_err(CE_WARN,
16100Sstevel@tonic-gate "aes_decrypt: do_hmac failed - error %0x", result);
16110Sstevel@tonic-gate goto cleanup;
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate
16140Sstevel@tonic-gate if (bcmp(hmacbuff, mp->b_rptr + enclen,
16150Sstevel@tonic-gate AES_TRUNCATED_HMAC_LEN) != 0) {
16160Sstevel@tonic-gate result = -1;
16170Sstevel@tonic-gate cmn_err(CE_WARN, "aes_decrypt: checksum verification failed");
16180Sstevel@tonic-gate goto cleanup;
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate
16210Sstevel@tonic-gate /* truncate the mblk at the end of the decrypted text */
16220Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + enclen;
16230Sstevel@tonic-gate
16240Sstevel@tonic-gate /* Adjust the beginning of the buffer to skip the confounder */
16250Sstevel@tonic-gate mp->b_rptr += DEFAULT_AES_BLOCKLEN;
16260Sstevel@tonic-gate
16270Sstevel@tonic-gate if (tmi->dec_data.ivec_usage != IVEC_NEVER &&
16280Sstevel@tonic-gate tmi->dec_data.ivec != NULL && tmi->dec_data.ivlen > 0)
16290Sstevel@tonic-gate bcopy(tmpiv, tmi->dec_data.ivec, DEFAULT_AES_BLOCKLEN);
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate cleanup:
16320Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
16330Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
16340Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
16350Sstevel@tonic-gate *mp->b_rptr = EIO;
16360Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
16370Sstevel@tonic-gate freemsg(mp->b_cont);
16380Sstevel@tonic-gate mp->b_cont = NULL;
16390Sstevel@tonic-gate qreply(WR(q), mp);
16400Sstevel@tonic-gate return (NULL);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate return (mp);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate * AES encrypt
16470Sstevel@tonic-gate *
16480Sstevel@tonic-gate * format of ciphertext when using AES
16490Sstevel@tonic-gate * +-------------+------------+------------+
16500Sstevel@tonic-gate * | confounder | msg-data | hmac |
16510Sstevel@tonic-gate * +-------------+------------+------------+
16520Sstevel@tonic-gate */
16530Sstevel@tonic-gate static mblk_t *
aes_encrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)16540Sstevel@tonic-gate aes_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
16550Sstevel@tonic-gate hash_info_t *hash)
16560Sstevel@tonic-gate {
16570Sstevel@tonic-gate int result;
16580Sstevel@tonic-gate size_t cipherlen;
16590Sstevel@tonic-gate size_t inlen;
16600Sstevel@tonic-gate uchar_t hmacbuff[64];
16610Sstevel@tonic-gate
16620Sstevel@tonic-gate inlen = (size_t)MBLKL(mp);
16630Sstevel@tonic-gate
16640Sstevel@tonic-gate cipherlen = encrypt_size(&tmi->enc_data, inlen);
16650Sstevel@tonic-gate
16660Sstevel@tonic-gate ASSERT(MBLKSIZE(mp) >= cipherlen);
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate * Shift the rptr back enough to insert the confounder.
16700Sstevel@tonic-gate */
16710Sstevel@tonic-gate mp->b_rptr -= DEFAULT_AES_BLOCKLEN;
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate /* Get random data for confounder */
16740Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
16750Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN);
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate /*
16780Sstevel@tonic-gate * Because we encrypt in-place, we need to calculate
16790Sstevel@tonic-gate * the HMAC of the plaintext now, then stick it on
16800Sstevel@tonic-gate * the end of the ciphertext down below.
16810Sstevel@tonic-gate */
16820Sstevel@tonic-gate result = do_hmac(sha1_hmac_mech,
16830Sstevel@tonic-gate &tmi->enc_data.d_hmac_key,
16840Sstevel@tonic-gate (char *)mp->b_rptr, DEFAULT_AES_BLOCKLEN + inlen,
16850Sstevel@tonic-gate (char *)hmacbuff, hash->hash_len);
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
16880Sstevel@tonic-gate cmn_err(CE_WARN, "aes_encrypt: do_hmac failed - error %0x",
16890Sstevel@tonic-gate result);
16900Sstevel@tonic-gate goto cleanup;
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate /* Encrypt using AES-CBC-CTS */
16930Sstevel@tonic-gate result = aes_cbc_cts_encrypt(tmi, mp->b_rptr,
16940Sstevel@tonic-gate inlen + DEFAULT_AES_BLOCKLEN);
16950Sstevel@tonic-gate
16960Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
16970Sstevel@tonic-gate cmn_err(CE_WARN, "aes_encrypt: aes_cbc_cts_encrypt "
16980Sstevel@tonic-gate "failed - error %0x", result);
16990Sstevel@tonic-gate goto cleanup;
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate /* copy the truncated HMAC to the end of the mblk */
17030Sstevel@tonic-gate bcopy(hmacbuff, mp->b_rptr + DEFAULT_AES_BLOCKLEN + inlen,
17040Sstevel@tonic-gate AES_TRUNCATED_HMAC_LEN);
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + cipherlen;
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate /*
17090Sstevel@tonic-gate * The final block of cipher text (not the HMAC) is used
17100Sstevel@tonic-gate * as the next IV.
17110Sstevel@tonic-gate */
17120Sstevel@tonic-gate if (tmi->enc_data.ivec_usage != IVEC_NEVER &&
17130Sstevel@tonic-gate tmi->enc_data.ivec != NULL) {
17140Sstevel@tonic-gate int nblocks = (inlen + 2 * DEFAULT_AES_BLOCKLEN - 1) /
17150Sstevel@tonic-gate DEFAULT_AES_BLOCKLEN;
17160Sstevel@tonic-gate
17170Sstevel@tonic-gate bcopy(mp->b_rptr + (nblocks - 2) * DEFAULT_AES_BLOCKLEN,
17180Sstevel@tonic-gate tmi->enc_data.ivec, DEFAULT_AES_BLOCKLEN);
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate cleanup:
17220Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
17230Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
17240Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
17250Sstevel@tonic-gate *mp->b_rptr = EIO;
17260Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
17270Sstevel@tonic-gate freemsg(mp->b_cont);
17280Sstevel@tonic-gate mp->b_cont = NULL;
17290Sstevel@tonic-gate qreply(WR(q), mp);
17300Sstevel@tonic-gate return (NULL);
17310Sstevel@tonic-gate }
17320Sstevel@tonic-gate return (mp);
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate /*
17360Sstevel@tonic-gate * ARCFOUR-HMAC-MD5 decrypt
17370Sstevel@tonic-gate *
17380Sstevel@tonic-gate * format of ciphertext when using ARCFOUR-HMAC-MD5
17390Sstevel@tonic-gate * +-----------+------------+------------+
17400Sstevel@tonic-gate * | hmac | confounder | msg-data |
17410Sstevel@tonic-gate * +-----------+------------+------------+
17420Sstevel@tonic-gate *
17430Sstevel@tonic-gate */
17440Sstevel@tonic-gate static mblk_t *
arcfour_hmac_md5_decrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)17450Sstevel@tonic-gate arcfour_hmac_md5_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
17460Sstevel@tonic-gate hash_info_t *hash)
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate int result;
17490Sstevel@tonic-gate size_t cipherlen;
17500Sstevel@tonic-gate size_t inlen;
17510Sstevel@tonic-gate size_t saltlen;
17520Sstevel@tonic-gate crypto_key_t k1, k2;
17530Sstevel@tonic-gate crypto_data_t indata;
17540Sstevel@tonic-gate iovec_t v1;
17550Sstevel@tonic-gate uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
17560Sstevel@tonic-gate 0xab, 0xab, 0xab, 0xab };
17570Sstevel@tonic-gate uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
17580Sstevel@tonic-gate uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
17590Sstevel@tonic-gate uchar_t cksum[MD5_HASHSIZE];
17600Sstevel@tonic-gate uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
17610Sstevel@tonic-gate crypto_mechanism_t mech;
17620Sstevel@tonic-gate int usage;
17630Sstevel@tonic-gate
1764*11688SPeter.Shoults@Sun.COM bzero(&indata, sizeof (indata));
1765*11688SPeter.Shoults@Sun.COM
17660Sstevel@tonic-gate /* The usage constant is 1026 for all "old" rcmd mode operations */
17670Sstevel@tonic-gate if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
17680Sstevel@tonic-gate usage = RCMDV1_USAGE;
17690Sstevel@tonic-gate else
17700Sstevel@tonic-gate usage = ARCFOUR_DECRYPT_USAGE;
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate /*
17730Sstevel@tonic-gate * The size at this point should be the size of
17740Sstevel@tonic-gate * all the plaintext plus the optional plaintext length
17750Sstevel@tonic-gate * needed for RCMD V2 mode. There should also be room
17760Sstevel@tonic-gate * at the head of the mblk for the confounder and hash info.
17770Sstevel@tonic-gate */
17780Sstevel@tonic-gate inlen = (size_t)MBLKL(mp);
17790Sstevel@tonic-gate
17800Sstevel@tonic-gate /*
17810Sstevel@tonic-gate * The cipherlen does not include the HMAC at the
17820Sstevel@tonic-gate * head of the buffer.
17830Sstevel@tonic-gate */
17840Sstevel@tonic-gate cipherlen = inlen - hash->hash_len;
17850Sstevel@tonic-gate
17860Sstevel@tonic-gate ASSERT(MBLKSIZE(mp) >= cipherlen);
17870Sstevel@tonic-gate if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
17880Sstevel@tonic-gate bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
17890Sstevel@tonic-gate saltdata[9] = 0;
17900Sstevel@tonic-gate saltdata[10] = usage & 0xff;
17910Sstevel@tonic-gate saltdata[11] = (usage >> 8) & 0xff;
17920Sstevel@tonic-gate saltdata[12] = (usage >> 16) & 0xff;
17930Sstevel@tonic-gate saltdata[13] = (usage >> 24) & 0xff;
17940Sstevel@tonic-gate saltlen = 14;
17950Sstevel@tonic-gate } else {
17960Sstevel@tonic-gate saltdata[0] = usage & 0xff;
17970Sstevel@tonic-gate saltdata[1] = (usage >> 8) & 0xff;
17980Sstevel@tonic-gate saltdata[2] = (usage >> 16) & 0xff;
17990Sstevel@tonic-gate saltdata[3] = (usage >> 24) & 0xff;
18000Sstevel@tonic-gate saltlen = 4;
18010Sstevel@tonic-gate }
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate * Use the salt value to create a key to be used
18040Sstevel@tonic-gate * for subsequent HMAC operations.
18050Sstevel@tonic-gate */
18060Sstevel@tonic-gate result = do_hmac(md5_hmac_mech,
18070Sstevel@tonic-gate tmi->dec_data.ckey,
18080Sstevel@tonic-gate (char *)saltdata, saltlen,
18090Sstevel@tonic-gate (char *)k1data, sizeof (k1data));
18100Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
18110Sstevel@tonic-gate cmn_err(CE_WARN,
18120Sstevel@tonic-gate "arcfour_hmac_md5_decrypt: do_hmac(k1)"
18130Sstevel@tonic-gate "failed - error %0x", result);
18140Sstevel@tonic-gate goto cleanup;
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate bcopy(k1data, k2data, sizeof (k1data));
18170Sstevel@tonic-gate
18180Sstevel@tonic-gate /*
18190Sstevel@tonic-gate * For the neutered MS RC4 encryption type,
18200Sstevel@tonic-gate * set the trailing 9 bytes to 0xab per the
18210Sstevel@tonic-gate * RC4-HMAC spec.
18220Sstevel@tonic-gate */
18230Sstevel@tonic-gate if (tmi->dec_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
18240Sstevel@tonic-gate bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate
18270Sstevel@tonic-gate mech.cm_type = tmi->dec_data.mech_type;
18280Sstevel@tonic-gate mech.cm_param = NULL;
18290Sstevel@tonic-gate mech.cm_param_len = 0;
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate /*
18320Sstevel@tonic-gate * If we have not yet initialized the decryption key,
18330Sstevel@tonic-gate * context, and template, do it now.
18340Sstevel@tonic-gate */
18350Sstevel@tonic-gate if (tmi->dec_data.ctx == NULL ||
18360Sstevel@tonic-gate (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
18370Sstevel@tonic-gate k1.ck_format = CRYPTO_KEY_RAW;
18380Sstevel@tonic-gate k1.ck_length = CRYPT_ARCFOUR_KEYBYTES * 8;
18390Sstevel@tonic-gate k1.ck_data = k1data;
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate tmi->dec_data.d_encr_key.ck_format = CRYPTO_KEY_RAW;
18420Sstevel@tonic-gate tmi->dec_data.d_encr_key.ck_length = k1.ck_length;
18430Sstevel@tonic-gate if (tmi->dec_data.d_encr_key.ck_data == NULL)
18440Sstevel@tonic-gate tmi->dec_data.d_encr_key.ck_data = kmem_zalloc(
18450Sstevel@tonic-gate CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
18460Sstevel@tonic-gate
18470Sstevel@tonic-gate /*
18480Sstevel@tonic-gate * HMAC operation creates the encryption
18490Sstevel@tonic-gate * key to be used for the decrypt operations.
18500Sstevel@tonic-gate */
18510Sstevel@tonic-gate result = do_hmac(md5_hmac_mech, &k1,
18520Sstevel@tonic-gate (char *)mp->b_rptr, hash->hash_len,
18530Sstevel@tonic-gate (char *)tmi->dec_data.d_encr_key.ck_data,
18540Sstevel@tonic-gate CRYPT_ARCFOUR_KEYBYTES);
18550Sstevel@tonic-gate
18560Sstevel@tonic-gate
18570Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
18580Sstevel@tonic-gate cmn_err(CE_WARN,
18590Sstevel@tonic-gate "arcfour_hmac_md5_decrypt: do_hmac(k3)"
18600Sstevel@tonic-gate "failed - error %0x", result);
18610Sstevel@tonic-gate goto cleanup;
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate }
18640Sstevel@tonic-gate
18650Sstevel@tonic-gate tmi->dec_data.enc_tmpl = NULL;
18660Sstevel@tonic-gate
18670Sstevel@tonic-gate if (tmi->dec_data.ctx == NULL &&
18680Sstevel@tonic-gate (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
18690Sstevel@tonic-gate /*
18700Sstevel@tonic-gate * Only create a template if we are doing
18710Sstevel@tonic-gate * chaining from block to block.
18720Sstevel@tonic-gate */
18730Sstevel@tonic-gate result = crypto_create_ctx_template(&mech,
18740Sstevel@tonic-gate &tmi->dec_data.d_encr_key,
18750Sstevel@tonic-gate &tmi->dec_data.enc_tmpl,
18760Sstevel@tonic-gate KM_SLEEP);
18770Sstevel@tonic-gate if (result == CRYPTO_NOT_SUPPORTED) {
18780Sstevel@tonic-gate tmi->dec_data.enc_tmpl = NULL;
18790Sstevel@tonic-gate } else if (result != CRYPTO_SUCCESS) {
18800Sstevel@tonic-gate cmn_err(CE_WARN,
18810Sstevel@tonic-gate "arcfour_hmac_md5_decrypt: "
18820Sstevel@tonic-gate "failed to create dec template "
18830Sstevel@tonic-gate "for RC4 encrypt: %0x", result);
18840Sstevel@tonic-gate goto cleanup;
18850Sstevel@tonic-gate }
18860Sstevel@tonic-gate
18870Sstevel@tonic-gate result = crypto_decrypt_init(&mech,
18880Sstevel@tonic-gate &tmi->dec_data.d_encr_key,
18890Sstevel@tonic-gate tmi->dec_data.enc_tmpl,
18900Sstevel@tonic-gate &tmi->dec_data.ctx, NULL);
18910Sstevel@tonic-gate
18920Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
18930Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_decrypt_init failed:"
18940Sstevel@tonic-gate " %0x", result);
18950Sstevel@tonic-gate goto cleanup;
18960Sstevel@tonic-gate }
18970Sstevel@tonic-gate }
18980Sstevel@tonic-gate
18990Sstevel@tonic-gate /* adjust the rptr so we don't decrypt the original hmac field */
19000Sstevel@tonic-gate
19010Sstevel@tonic-gate v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
19020Sstevel@tonic-gate v1.iov_len = cipherlen;
19030Sstevel@tonic-gate
19040Sstevel@tonic-gate indata.cd_format = CRYPTO_DATA_RAW;
19050Sstevel@tonic-gate indata.cd_offset = 0;
19060Sstevel@tonic-gate indata.cd_length = cipherlen;
19070Sstevel@tonic-gate indata.cd_raw = v1;
19080Sstevel@tonic-gate
19090Sstevel@tonic-gate if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
19100Sstevel@tonic-gate result = crypto_decrypt_update(tmi->dec_data.ctx,
19110Sstevel@tonic-gate &indata, NULL, NULL);
19120Sstevel@tonic-gate else
19130Sstevel@tonic-gate result = crypto_decrypt(&mech, &indata,
19140Sstevel@tonic-gate &tmi->dec_data.d_encr_key, NULL, NULL, NULL);
19150Sstevel@tonic-gate
19160Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
19170Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_decrypt_update failed:"
19180Sstevel@tonic-gate " %0x", result);
19190Sstevel@tonic-gate goto cleanup;
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate
19220Sstevel@tonic-gate k2.ck_format = CRYPTO_KEY_RAW;
19230Sstevel@tonic-gate k2.ck_length = sizeof (k2data) * 8;
19240Sstevel@tonic-gate k2.ck_data = k2data;
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate result = do_hmac(md5_hmac_mech,
19270Sstevel@tonic-gate &k2,
19280Sstevel@tonic-gate (char *)mp->b_rptr + hash->hash_len, cipherlen,
19290Sstevel@tonic-gate (char *)cksum, hash->hash_len);
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
19320Sstevel@tonic-gate cmn_err(CE_WARN,
19330Sstevel@tonic-gate "arcfour_hmac_md5_decrypt: do_hmac(k2)"
19340Sstevel@tonic-gate "failed - error %0x", result);
19350Sstevel@tonic-gate goto cleanup;
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate
19380Sstevel@tonic-gate if (bcmp(cksum, mp->b_rptr, hash->hash_len) != 0) {
19390Sstevel@tonic-gate cmn_err(CE_WARN, "arcfour_decrypt HMAC comparison failed");
19400Sstevel@tonic-gate result = -1;
19410Sstevel@tonic-gate goto cleanup;
19420Sstevel@tonic-gate }
19430Sstevel@tonic-gate
19440Sstevel@tonic-gate /*
19450Sstevel@tonic-gate * adjust the start of the mblk to skip over the
19460Sstevel@tonic-gate * hash and confounder.
19470Sstevel@tonic-gate */
19480Sstevel@tonic-gate mp->b_rptr += hash->hash_len + hash->confound_len;
19490Sstevel@tonic-gate
19500Sstevel@tonic-gate cleanup:
19510Sstevel@tonic-gate bzero(k1data, sizeof (k1data));
19520Sstevel@tonic-gate bzero(k2data, sizeof (k2data));
19530Sstevel@tonic-gate bzero(cksum, sizeof (cksum));
19540Sstevel@tonic-gate bzero(saltdata, sizeof (saltdata));
19550Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
19560Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
19570Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
19580Sstevel@tonic-gate *mp->b_rptr = EIO;
19590Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
19600Sstevel@tonic-gate freemsg(mp->b_cont);
19610Sstevel@tonic-gate mp->b_cont = NULL;
19620Sstevel@tonic-gate qreply(WR(q), mp);
19630Sstevel@tonic-gate return (NULL);
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate return (mp);
19660Sstevel@tonic-gate }
19670Sstevel@tonic-gate
19680Sstevel@tonic-gate /*
19690Sstevel@tonic-gate * ARCFOUR-HMAC-MD5 encrypt
19700Sstevel@tonic-gate *
19710Sstevel@tonic-gate * format of ciphertext when using ARCFOUR-HMAC-MD5
19720Sstevel@tonic-gate * +-----------+------------+------------+
19730Sstevel@tonic-gate * | hmac | confounder | msg-data |
19740Sstevel@tonic-gate * +-----------+------------+------------+
19750Sstevel@tonic-gate *
19760Sstevel@tonic-gate */
19770Sstevel@tonic-gate static mblk_t *
arcfour_hmac_md5_encrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)19780Sstevel@tonic-gate arcfour_hmac_md5_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp,
19790Sstevel@tonic-gate hash_info_t *hash)
19800Sstevel@tonic-gate {
19810Sstevel@tonic-gate int result;
19820Sstevel@tonic-gate size_t cipherlen;
19830Sstevel@tonic-gate size_t inlen;
19840Sstevel@tonic-gate size_t saltlen;
19850Sstevel@tonic-gate crypto_key_t k1, k2;
19860Sstevel@tonic-gate crypto_data_t indata;
19870Sstevel@tonic-gate iovec_t v1;
19880Sstevel@tonic-gate uchar_t ms_exp[9] = {0xab, 0xab, 0xab, 0xab, 0xab,
19890Sstevel@tonic-gate 0xab, 0xab, 0xab, 0xab };
19900Sstevel@tonic-gate uchar_t k1data[CRYPT_ARCFOUR_KEYBYTES];
19910Sstevel@tonic-gate uchar_t k2data[CRYPT_ARCFOUR_KEYBYTES];
19920Sstevel@tonic-gate uchar_t saltdata[CRYPT_ARCFOUR_KEYBYTES];
19930Sstevel@tonic-gate crypto_mechanism_t mech;
19940Sstevel@tonic-gate int usage;
19950Sstevel@tonic-gate
19967227Sps57422 bzero(&indata, sizeof (indata));
19977227Sps57422
19980Sstevel@tonic-gate /* The usage constant is 1026 for all "old" rcmd mode operations */
19990Sstevel@tonic-gate if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)
20000Sstevel@tonic-gate usage = RCMDV1_USAGE;
20010Sstevel@tonic-gate else
20020Sstevel@tonic-gate usage = ARCFOUR_ENCRYPT_USAGE;
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate mech.cm_type = tmi->enc_data.mech_type;
20050Sstevel@tonic-gate mech.cm_param = NULL;
20060Sstevel@tonic-gate mech.cm_param_len = 0;
20070Sstevel@tonic-gate
20080Sstevel@tonic-gate /*
20090Sstevel@tonic-gate * The size at this point should be the size of
20100Sstevel@tonic-gate * all the plaintext plus the optional plaintext length
20110Sstevel@tonic-gate * needed for RCMD V2 mode. There should also be room
20120Sstevel@tonic-gate * at the head of the mblk for the confounder and hash info.
20130Sstevel@tonic-gate */
20140Sstevel@tonic-gate inlen = (size_t)MBLKL(mp);
20150Sstevel@tonic-gate
20160Sstevel@tonic-gate cipherlen = encrypt_size(&tmi->enc_data, inlen);
20170Sstevel@tonic-gate
20180Sstevel@tonic-gate ASSERT(MBLKSIZE(mp) >= cipherlen);
20190Sstevel@tonic-gate
20200Sstevel@tonic-gate /*
20210Sstevel@tonic-gate * Shift the rptr back enough to insert
20220Sstevel@tonic-gate * the confounder and hash.
20230Sstevel@tonic-gate */
20240Sstevel@tonic-gate mp->b_rptr -= (hash->confound_len + hash->hash_len);
20250Sstevel@tonic-gate
20260Sstevel@tonic-gate /* zero out the hash area */
20270Sstevel@tonic-gate bzero(mp->b_rptr, (size_t)hash->hash_len);
20280Sstevel@tonic-gate
20290Sstevel@tonic-gate if (cipherlen > inlen) {
20300Sstevel@tonic-gate bzero(mp->b_wptr, MBLKTAIL(mp));
20310Sstevel@tonic-gate }
20320Sstevel@tonic-gate
20330Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20340Sstevel@tonic-gate bcopy(ARCFOUR_EXP_SALT, saltdata, strlen(ARCFOUR_EXP_SALT));
20350Sstevel@tonic-gate saltdata[9] = 0;
20360Sstevel@tonic-gate saltdata[10] = usage & 0xff;
20370Sstevel@tonic-gate saltdata[11] = (usage >> 8) & 0xff;
20380Sstevel@tonic-gate saltdata[12] = (usage >> 16) & 0xff;
20390Sstevel@tonic-gate saltdata[13] = (usage >> 24) & 0xff;
20400Sstevel@tonic-gate saltlen = 14;
20410Sstevel@tonic-gate } else {
20420Sstevel@tonic-gate saltdata[0] = usage & 0xff;
20430Sstevel@tonic-gate saltdata[1] = (usage >> 8) & 0xff;
20440Sstevel@tonic-gate saltdata[2] = (usage >> 16) & 0xff;
20450Sstevel@tonic-gate saltdata[3] = (usage >> 24) & 0xff;
20460Sstevel@tonic-gate saltlen = 4;
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate /*
20490Sstevel@tonic-gate * Use the salt value to create a key to be used
20500Sstevel@tonic-gate * for subsequent HMAC operations.
20510Sstevel@tonic-gate */
20520Sstevel@tonic-gate result = do_hmac(md5_hmac_mech,
20530Sstevel@tonic-gate tmi->enc_data.ckey,
20540Sstevel@tonic-gate (char *)saltdata, saltlen,
20550Sstevel@tonic-gate (char *)k1data, sizeof (k1data));
20560Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
20570Sstevel@tonic-gate cmn_err(CE_WARN,
20580Sstevel@tonic-gate "arcfour_hmac_md5_encrypt: do_hmac(k1)"
20590Sstevel@tonic-gate "failed - error %0x", result);
20600Sstevel@tonic-gate goto cleanup;
20610Sstevel@tonic-gate }
20620Sstevel@tonic-gate
20630Sstevel@tonic-gate bcopy(k1data, k2data, sizeof (k2data));
20640Sstevel@tonic-gate
20650Sstevel@tonic-gate /*
20660Sstevel@tonic-gate * For the neutered MS RC4 encryption type,
20670Sstevel@tonic-gate * set the trailing 9 bytes to 0xab per the
20680Sstevel@tonic-gate * RC4-HMAC spec.
20690Sstevel@tonic-gate */
20700Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP) {
20710Sstevel@tonic-gate bcopy((void *)&k1data[7], ms_exp, sizeof (ms_exp));
20720Sstevel@tonic-gate }
20730Sstevel@tonic-gate
20740Sstevel@tonic-gate /*
20750Sstevel@tonic-gate * Get the confounder bytes.
20760Sstevel@tonic-gate */
20770Sstevel@tonic-gate (void) random_get_pseudo_bytes(
20780Sstevel@tonic-gate (uint8_t *)(mp->b_rptr + hash->hash_len),
20790Sstevel@tonic-gate (size_t)hash->confound_len);
20800Sstevel@tonic-gate
20810Sstevel@tonic-gate k2.ck_data = k2data;
20820Sstevel@tonic-gate k2.ck_format = CRYPTO_KEY_RAW;
20830Sstevel@tonic-gate k2.ck_length = sizeof (k2data) * 8;
20840Sstevel@tonic-gate
20850Sstevel@tonic-gate /*
20860Sstevel@tonic-gate * This writes the HMAC to the hash area in the
20870Sstevel@tonic-gate * mblk. The key used is the one just created by
20880Sstevel@tonic-gate * the previous HMAC operation.
20890Sstevel@tonic-gate * The data being processed is the confounder bytes
20900Sstevel@tonic-gate * PLUS the input plaintext.
20910Sstevel@tonic-gate */
20920Sstevel@tonic-gate result = do_hmac(md5_hmac_mech, &k2,
20930Sstevel@tonic-gate (char *)mp->b_rptr + hash->hash_len,
20940Sstevel@tonic-gate hash->confound_len + inlen,
20950Sstevel@tonic-gate (char *)mp->b_rptr, hash->hash_len);
20960Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
20970Sstevel@tonic-gate cmn_err(CE_WARN,
20980Sstevel@tonic-gate "arcfour_hmac_md5_encrypt: do_hmac(k2)"
20990Sstevel@tonic-gate "failed - error %0x", result);
21000Sstevel@tonic-gate goto cleanup;
21010Sstevel@tonic-gate }
21020Sstevel@tonic-gate /*
21030Sstevel@tonic-gate * Because of the odd way that MIT uses RC4 keys
21040Sstevel@tonic-gate * on the rlogin stream, we only need to create
21050Sstevel@tonic-gate * this key once.
21060Sstevel@tonic-gate * However, if using "old" rcmd mode, we need to do
21070Sstevel@tonic-gate * it every time.
21080Sstevel@tonic-gate */
21090Sstevel@tonic-gate if (tmi->enc_data.ctx == NULL ||
21100Sstevel@tonic-gate (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V1)) {
21110Sstevel@tonic-gate crypto_key_t *key = &tmi->enc_data.d_encr_key;
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate k1.ck_data = k1data;
21140Sstevel@tonic-gate k1.ck_format = CRYPTO_KEY_RAW;
21150Sstevel@tonic-gate k1.ck_length = sizeof (k1data) * 8;
21160Sstevel@tonic-gate
21170Sstevel@tonic-gate key->ck_format = CRYPTO_KEY_RAW;
21180Sstevel@tonic-gate key->ck_length = k1.ck_length;
21190Sstevel@tonic-gate if (key->ck_data == NULL)
21200Sstevel@tonic-gate key->ck_data = kmem_zalloc(
21210Sstevel@tonic-gate CRYPT_ARCFOUR_KEYBYTES, KM_SLEEP);
21220Sstevel@tonic-gate
21230Sstevel@tonic-gate /*
21240Sstevel@tonic-gate * The final HMAC operation creates the encryption
21250Sstevel@tonic-gate * key to be used for the encrypt operation.
21260Sstevel@tonic-gate */
21270Sstevel@tonic-gate result = do_hmac(md5_hmac_mech, &k1,
21280Sstevel@tonic-gate (char *)mp->b_rptr, hash->hash_len,
21290Sstevel@tonic-gate (char *)key->ck_data, CRYPT_ARCFOUR_KEYBYTES);
21300Sstevel@tonic-gate
21310Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
21320Sstevel@tonic-gate cmn_err(CE_WARN,
21330Sstevel@tonic-gate "arcfour_hmac_md5_encrypt: do_hmac(k3)"
21340Sstevel@tonic-gate "failed - error %0x", result);
21350Sstevel@tonic-gate goto cleanup;
21360Sstevel@tonic-gate }
21370Sstevel@tonic-gate }
21380Sstevel@tonic-gate
21390Sstevel@tonic-gate /*
21400Sstevel@tonic-gate * If the context has not been initialized, do it now.
21410Sstevel@tonic-gate */
21420Sstevel@tonic-gate if (tmi->enc_data.ctx == NULL &&
21430Sstevel@tonic-gate (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)) {
21440Sstevel@tonic-gate /*
21450Sstevel@tonic-gate * Only create a template if we are doing
21460Sstevel@tonic-gate * chaining from block to block.
21470Sstevel@tonic-gate */
21480Sstevel@tonic-gate result = crypto_create_ctx_template(&mech,
21490Sstevel@tonic-gate &tmi->enc_data.d_encr_key,
21500Sstevel@tonic-gate &tmi->enc_data.enc_tmpl,
21510Sstevel@tonic-gate KM_SLEEP);
21520Sstevel@tonic-gate if (result == CRYPTO_NOT_SUPPORTED) {
21530Sstevel@tonic-gate tmi->enc_data.enc_tmpl = NULL;
21540Sstevel@tonic-gate } else if (result != CRYPTO_SUCCESS) {
21550Sstevel@tonic-gate cmn_err(CE_WARN, "failed to create enc template "
21560Sstevel@tonic-gate "for RC4 encrypt: %0x", result);
21570Sstevel@tonic-gate goto cleanup;
21580Sstevel@tonic-gate }
21590Sstevel@tonic-gate
21600Sstevel@tonic-gate result = crypto_encrypt_init(&mech,
21610Sstevel@tonic-gate &tmi->enc_data.d_encr_key,
21620Sstevel@tonic-gate tmi->enc_data.enc_tmpl,
21630Sstevel@tonic-gate &tmi->enc_data.ctx, NULL);
21640Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
21650Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_encrypt_init failed:"
21660Sstevel@tonic-gate " %0x", result);
21670Sstevel@tonic-gate goto cleanup;
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate }
21700Sstevel@tonic-gate v1.iov_base = (char *)mp->b_rptr + hash->hash_len;
21710Sstevel@tonic-gate v1.iov_len = hash->confound_len + inlen;
21720Sstevel@tonic-gate
21730Sstevel@tonic-gate indata.cd_format = CRYPTO_DATA_RAW;
21740Sstevel@tonic-gate indata.cd_offset = 0;
21750Sstevel@tonic-gate indata.cd_length = hash->confound_len + inlen;
21760Sstevel@tonic-gate indata.cd_raw = v1;
21770Sstevel@tonic-gate
21780Sstevel@tonic-gate if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
21790Sstevel@tonic-gate result = crypto_encrypt_update(tmi->enc_data.ctx,
21800Sstevel@tonic-gate &indata, NULL, NULL);
21810Sstevel@tonic-gate else
21820Sstevel@tonic-gate result = crypto_encrypt(&mech, &indata,
21830Sstevel@tonic-gate &tmi->enc_data.d_encr_key, NULL,
21840Sstevel@tonic-gate NULL, NULL);
21850Sstevel@tonic-gate
21860Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
21870Sstevel@tonic-gate cmn_err(CE_WARN, "crypto_encrypt_update failed: 0x%0x",
21880Sstevel@tonic-gate result);
21890Sstevel@tonic-gate }
21900Sstevel@tonic-gate
21910Sstevel@tonic-gate cleanup:
21920Sstevel@tonic-gate bzero(k1data, sizeof (k1data));
21930Sstevel@tonic-gate bzero(k2data, sizeof (k2data));
21940Sstevel@tonic-gate bzero(saltdata, sizeof (saltdata));
21950Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
21960Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
21970Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
21980Sstevel@tonic-gate *mp->b_rptr = EIO;
21990Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
22000Sstevel@tonic-gate freemsg(mp->b_cont);
22010Sstevel@tonic-gate mp->b_cont = NULL;
22020Sstevel@tonic-gate qreply(WR(q), mp);
22030Sstevel@tonic-gate return (NULL);
22040Sstevel@tonic-gate }
22050Sstevel@tonic-gate return (mp);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate
22080Sstevel@tonic-gate /*
22090Sstevel@tonic-gate * DES-CBC-[HASH] encrypt
22100Sstevel@tonic-gate *
22110Sstevel@tonic-gate * Needed to support userland apps that must support Kerberos V5
22120Sstevel@tonic-gate * encryption DES-CBC encryption modes.
22130Sstevel@tonic-gate *
22140Sstevel@tonic-gate * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
22150Sstevel@tonic-gate *
22160Sstevel@tonic-gate * format of ciphertext for DES-CBC functions, per RFC1510 is:
22170Sstevel@tonic-gate * +-----------+----------+-------------+-----+
22180Sstevel@tonic-gate * |confounder | cksum | msg-data | pad |
22190Sstevel@tonic-gate * +-----------+----------+-------------+-----+
22200Sstevel@tonic-gate *
22210Sstevel@tonic-gate * format of ciphertext when using DES3-SHA1-HMAC
22220Sstevel@tonic-gate * +-----------+----------+-------------+-----+
22230Sstevel@tonic-gate * |confounder | msg-data | hmac | pad |
22240Sstevel@tonic-gate * +-----------+----------+-------------+-----+
22250Sstevel@tonic-gate *
22260Sstevel@tonic-gate * The confounder is 8 bytes of random data.
22270Sstevel@tonic-gate * The cksum depends on the hash being used.
22280Sstevel@tonic-gate * 4 bytes for CRC32
22290Sstevel@tonic-gate * 16 bytes for MD5
22300Sstevel@tonic-gate * 20 bytes for SHA1
22310Sstevel@tonic-gate * 0 bytes for RAW
22320Sstevel@tonic-gate *
22330Sstevel@tonic-gate */
22340Sstevel@tonic-gate static mblk_t *
des_cbc_encrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)22350Sstevel@tonic-gate des_cbc_encrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
22360Sstevel@tonic-gate {
22370Sstevel@tonic-gate int result;
22380Sstevel@tonic-gate size_t cipherlen;
22390Sstevel@tonic-gate size_t inlen;
22400Sstevel@tonic-gate size_t plainlen;
22410Sstevel@tonic-gate
22420Sstevel@tonic-gate /*
22430Sstevel@tonic-gate * The size at this point should be the size of
22440Sstevel@tonic-gate * all the plaintext plus the optional plaintext length
22450Sstevel@tonic-gate * needed for RCMD V2 mode. There should also be room
22460Sstevel@tonic-gate * at the head of the mblk for the confounder and hash info.
22470Sstevel@tonic-gate */
22480Sstevel@tonic-gate inlen = (size_t)MBLKL(mp);
22490Sstevel@tonic-gate
22500Sstevel@tonic-gate /*
22510Sstevel@tonic-gate * The output size will be a multiple of 8 because this algorithm
22520Sstevel@tonic-gate * only works on 8 byte chunks.
22530Sstevel@tonic-gate */
22540Sstevel@tonic-gate cipherlen = encrypt_size(&tmi->enc_data, inlen);
22550Sstevel@tonic-gate
22560Sstevel@tonic-gate ASSERT(MBLKSIZE(mp) >= cipherlen);
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate if (cipherlen > inlen) {
22590Sstevel@tonic-gate bzero(mp->b_wptr, MBLKTAIL(mp));
22600Sstevel@tonic-gate }
22610Sstevel@tonic-gate
22620Sstevel@tonic-gate /*
22630Sstevel@tonic-gate * Shift the rptr back enough to insert
22640Sstevel@tonic-gate * the confounder and hash.
22650Sstevel@tonic-gate */
22660Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
22670Sstevel@tonic-gate mp->b_rptr -= hash->confound_len;
22680Sstevel@tonic-gate } else {
22690Sstevel@tonic-gate mp->b_rptr -= (hash->confound_len + hash->hash_len);
22700Sstevel@tonic-gate
22710Sstevel@tonic-gate /* zero out the hash area */
22720Sstevel@tonic-gate bzero(mp->b_rptr + hash->confound_len, (size_t)hash->hash_len);
22730Sstevel@tonic-gate }
22740Sstevel@tonic-gate
22750Sstevel@tonic-gate /* get random confounder from our friend, the 'random' module */
22760Sstevel@tonic-gate if (hash->confound_len > 0) {
22770Sstevel@tonic-gate (void) random_get_pseudo_bytes((uint8_t *)mp->b_rptr,
22780Sstevel@tonic-gate (size_t)hash->confound_len);
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate
22810Sstevel@tonic-gate /*
22820Sstevel@tonic-gate * For 3DES we calculate an HMAC later.
22830Sstevel@tonic-gate */
22840Sstevel@tonic-gate if (tmi->enc_data.method != CRYPT_METHOD_DES3_CBC_SHA1) {
22850Sstevel@tonic-gate /* calculate chksum of confounder + input */
22860Sstevel@tonic-gate if (hash->hash_len > 0 && hash->hashfunc != NULL) {
22870Sstevel@tonic-gate uchar_t cksum[MAX_CKSUM_LEN];
22880Sstevel@tonic-gate
22890Sstevel@tonic-gate result = hash->hashfunc(cksum, mp->b_rptr,
22900Sstevel@tonic-gate cipherlen);
22910Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
22920Sstevel@tonic-gate goto failure;
22930Sstevel@tonic-gate }
22940Sstevel@tonic-gate
22950Sstevel@tonic-gate /* put hash in place right after the confounder */
22960Sstevel@tonic-gate bcopy(cksum, (mp->b_rptr + hash->confound_len),
22970Sstevel@tonic-gate (size_t)hash->hash_len);
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate /*
23010Sstevel@tonic-gate * In order to support the "old" Kerberos RCMD protocol,
23020Sstevel@tonic-gate * we must use the IVEC 3 different ways:
23030Sstevel@tonic-gate * IVEC_REUSE = keep using the same IV each time, this is
23040Sstevel@tonic-gate * ugly and insecure, but necessary for
23050Sstevel@tonic-gate * backwards compatibility with existing MIT code.
23060Sstevel@tonic-gate * IVEC_ONETIME = Use the ivec as initialized when the crypto
23070Sstevel@tonic-gate * was setup (see setup_crypto routine).
23080Sstevel@tonic-gate * IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
23090Sstevel@tonic-gate */
23100Sstevel@tonic-gate if (tmi->enc_data.ivec_usage == IVEC_NEVER) {
23110Sstevel@tonic-gate bzero(tmi->enc_data.block, tmi->enc_data.blocklen);
23120Sstevel@tonic-gate } else if (tmi->enc_data.ivec_usage == IVEC_REUSE) {
23130Sstevel@tonic-gate bcopy(tmi->enc_data.ivec, tmi->enc_data.block,
23140Sstevel@tonic-gate tmi->enc_data.blocklen);
23150Sstevel@tonic-gate }
23160Sstevel@tonic-gate
23170Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23180Sstevel@tonic-gate /*
23190Sstevel@tonic-gate * The input length already included the hash size,
23200Sstevel@tonic-gate * don't include this in the plaintext length
23210Sstevel@tonic-gate * calculations.
23220Sstevel@tonic-gate */
23230Sstevel@tonic-gate plainlen = cipherlen - hash->hash_len;
23240Sstevel@tonic-gate
23250Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + plainlen;
23260Sstevel@tonic-gate
23270Sstevel@tonic-gate result = kef_encr_hmac(&tmi->enc_data,
23280Sstevel@tonic-gate (void *)mp, (size_t)plainlen,
23290Sstevel@tonic-gate (char *)(mp->b_rptr + plainlen),
23300Sstevel@tonic-gate hash->hash_len);
23310Sstevel@tonic-gate } else {
23320Sstevel@tonic-gate ASSERT(mp->b_rptr + cipherlen <= DB_LIM(mp));
23330Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + cipherlen;
23340Sstevel@tonic-gate result = kef_crypt(&tmi->enc_data, (void *)mp,
23350Sstevel@tonic-gate CRYPTO_DATA_MBLK, (size_t)cipherlen,
23360Sstevel@tonic-gate CRYPT_ENCRYPT);
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate failure:
23390Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
23400Sstevel@tonic-gate #ifdef DEBUG
23410Sstevel@tonic-gate cmn_err(CE_WARN,
23420Sstevel@tonic-gate "des_cbc_encrypt: kef_crypt encrypt "
23430Sstevel@tonic-gate "failed (len: %ld) - error %0x",
23440Sstevel@tonic-gate cipherlen, result);
23450Sstevel@tonic-gate #endif
23460Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
23470Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
23480Sstevel@tonic-gate *mp->b_rptr = EIO;
23490Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
23500Sstevel@tonic-gate freemsg(mp->b_cont);
23510Sstevel@tonic-gate mp->b_cont = NULL;
23520Sstevel@tonic-gate qreply(WR(q), mp);
23530Sstevel@tonic-gate return (NULL);
23540Sstevel@tonic-gate } else if (tmi->enc_data.ivec_usage == IVEC_ONETIME) {
23550Sstevel@tonic-gate /*
23560Sstevel@tonic-gate * Because we are using KEF, we must manually
23570Sstevel@tonic-gate * update our IV.
23580Sstevel@tonic-gate */
23590Sstevel@tonic-gate bcopy(mp->b_wptr - tmi->enc_data.ivlen,
23600Sstevel@tonic-gate tmi->enc_data.block, tmi->enc_data.ivlen);
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
23630Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + cipherlen;
23640Sstevel@tonic-gate }
23650Sstevel@tonic-gate
23660Sstevel@tonic-gate return (mp);
23670Sstevel@tonic-gate }
23680Sstevel@tonic-gate
23690Sstevel@tonic-gate /*
23700Sstevel@tonic-gate * des_cbc_decrypt
23710Sstevel@tonic-gate *
23720Sstevel@tonic-gate *
23730Sstevel@tonic-gate * Needed to support userland apps that must support Kerberos V5
23740Sstevel@tonic-gate * encryption DES-CBC decryption modes.
23750Sstevel@tonic-gate *
23760Sstevel@tonic-gate * The HASH values supported are RAW(NULL), MD5, CRC32, and SHA1
23770Sstevel@tonic-gate *
23780Sstevel@tonic-gate * format of ciphertext for DES-CBC functions, per RFC1510 is:
23790Sstevel@tonic-gate * +-----------+----------+-------------+-----+
23800Sstevel@tonic-gate * |confounder | cksum | msg-data | pad |
23810Sstevel@tonic-gate * +-----------+----------+-------------+-----+
23820Sstevel@tonic-gate *
23830Sstevel@tonic-gate * format of ciphertext when using DES3-SHA1-HMAC
23840Sstevel@tonic-gate * +-----------+----------+-------------+-----+
23850Sstevel@tonic-gate * |confounder | msg-data | hmac | pad |
23860Sstevel@tonic-gate * +-----------+----------+-------------+-----+
23870Sstevel@tonic-gate *
23880Sstevel@tonic-gate * The confounder is 8 bytes of random data.
23890Sstevel@tonic-gate * The cksum depends on the hash being used.
23900Sstevel@tonic-gate * 4 bytes for CRC32
23910Sstevel@tonic-gate * 16 bytes for MD5
23920Sstevel@tonic-gate * 20 bytes for SHA1
23930Sstevel@tonic-gate * 0 bytes for RAW
23940Sstevel@tonic-gate *
23950Sstevel@tonic-gate */
23960Sstevel@tonic-gate static mblk_t *
des_cbc_decrypt(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,hash_info_t * hash)23970Sstevel@tonic-gate des_cbc_decrypt(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, hash_info_t *hash)
23980Sstevel@tonic-gate {
23990Sstevel@tonic-gate uint_t inlen, datalen;
24000Sstevel@tonic-gate int result = 0;
24010Sstevel@tonic-gate uchar_t *optr = NULL;
24020Sstevel@tonic-gate uchar_t cksum[MAX_CKSUM_LEN], newcksum[MAX_CKSUM_LEN];
24030Sstevel@tonic-gate uchar_t nextiv[DEFAULT_DES_BLOCKLEN];
24040Sstevel@tonic-gate
24050Sstevel@tonic-gate /* Compute adjusted size */
24060Sstevel@tonic-gate inlen = MBLKL(mp);
24070Sstevel@tonic-gate
24080Sstevel@tonic-gate optr = mp->b_rptr;
24090Sstevel@tonic-gate
24100Sstevel@tonic-gate /*
24110Sstevel@tonic-gate * In order to support the "old" Kerberos RCMD protocol,
24120Sstevel@tonic-gate * we must use the IVEC 3 different ways:
24130Sstevel@tonic-gate * IVEC_REUSE = keep using the same IV each time, this is
24140Sstevel@tonic-gate * ugly and insecure, but necessary for
24150Sstevel@tonic-gate * backwards compatibility with existing MIT code.
24160Sstevel@tonic-gate * IVEC_ONETIME = Use the ivec as initialized when the crypto
24170Sstevel@tonic-gate * was setup (see setup_crypto routine).
24180Sstevel@tonic-gate * IVEC_NEVER = never use an IVEC, use a bunch of 0's as the IV (yuk).
24190Sstevel@tonic-gate */
24200Sstevel@tonic-gate if (tmi->dec_data.ivec_usage == IVEC_NEVER)
24210Sstevel@tonic-gate bzero(tmi->dec_data.block, tmi->dec_data.blocklen);
24220Sstevel@tonic-gate else if (tmi->dec_data.ivec_usage == IVEC_REUSE)
24230Sstevel@tonic-gate bcopy(tmi->dec_data.ivec, tmi->dec_data.block,
24240Sstevel@tonic-gate tmi->dec_data.blocklen);
24250Sstevel@tonic-gate
24260Sstevel@tonic-gate if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24270Sstevel@tonic-gate /*
24280Sstevel@tonic-gate * Do not decrypt the HMAC at the end
24290Sstevel@tonic-gate */
24300Sstevel@tonic-gate int decrypt_len = inlen - hash->hash_len;
24310Sstevel@tonic-gate
24320Sstevel@tonic-gate /*
24330Sstevel@tonic-gate * Move the wptr so the mblk appears to end
24340Sstevel@tonic-gate * BEFORE the HMAC section.
24350Sstevel@tonic-gate */
24360Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + decrypt_len;
24370Sstevel@tonic-gate
24380Sstevel@tonic-gate /*
24390Sstevel@tonic-gate * Because we are using KEF, we must manually update our
24400Sstevel@tonic-gate * IV.
24410Sstevel@tonic-gate */
24420Sstevel@tonic-gate if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24430Sstevel@tonic-gate bcopy(mp->b_rptr + decrypt_len - tmi->dec_data.ivlen,
24440Sstevel@tonic-gate nextiv, tmi->dec_data.ivlen);
24450Sstevel@tonic-gate }
24460Sstevel@tonic-gate
24470Sstevel@tonic-gate result = kef_decr_hmac(&tmi->dec_data, mp, decrypt_len,
24480Sstevel@tonic-gate (char *)newcksum, hash->hash_len);
24490Sstevel@tonic-gate } else {
24500Sstevel@tonic-gate /*
24510Sstevel@tonic-gate * Because we are using KEF, we must manually update our
24520Sstevel@tonic-gate * IV.
24530Sstevel@tonic-gate */
24540Sstevel@tonic-gate if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24550Sstevel@tonic-gate bcopy(mp->b_wptr - tmi->enc_data.ivlen, nextiv,
24560Sstevel@tonic-gate tmi->dec_data.ivlen);
24570Sstevel@tonic-gate }
24580Sstevel@tonic-gate result = kef_crypt(&tmi->dec_data, (void *)mp,
24590Sstevel@tonic-gate CRYPTO_DATA_MBLK, (size_t)inlen, CRYPT_DECRYPT);
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate if (result != CRYPTO_SUCCESS) {
24620Sstevel@tonic-gate #ifdef DEBUG
24630Sstevel@tonic-gate cmn_err(CE_WARN,
24640Sstevel@tonic-gate "des_cbc_decrypt: kef_crypt decrypt "
24650Sstevel@tonic-gate "failed - error %0x", result);
24660Sstevel@tonic-gate #endif
24670Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
24680Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
24690Sstevel@tonic-gate *mp->b_rptr = EIO;
24700Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
24710Sstevel@tonic-gate freemsg(mp->b_cont);
24720Sstevel@tonic-gate mp->b_cont = NULL;
24730Sstevel@tonic-gate qreply(WR(q), mp);
24740Sstevel@tonic-gate return (NULL);
24750Sstevel@tonic-gate }
24760Sstevel@tonic-gate
24770Sstevel@tonic-gate /*
24780Sstevel@tonic-gate * Manually update the IV, KEF does not track this for us.
24790Sstevel@tonic-gate */
24800Sstevel@tonic-gate if (tmi->dec_data.ivec_usage == IVEC_ONETIME) {
24810Sstevel@tonic-gate bcopy(nextiv, tmi->dec_data.block, tmi->dec_data.ivlen);
24820Sstevel@tonic-gate }
24830Sstevel@tonic-gate
24840Sstevel@tonic-gate /* Verify the checksum(if necessary) */
24850Sstevel@tonic-gate if (hash->hash_len > 0) {
24860Sstevel@tonic-gate if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1) {
24870Sstevel@tonic-gate bcopy(mp->b_rptr + inlen - hash->hash_len, cksum,
24880Sstevel@tonic-gate hash->hash_len);
24890Sstevel@tonic-gate } else {
24900Sstevel@tonic-gate bcopy(optr + hash->confound_len, cksum, hash->hash_len);
24910Sstevel@tonic-gate
24920Sstevel@tonic-gate /* zero the cksum in the buffer */
24930Sstevel@tonic-gate ASSERT(optr + hash->confound_len + hash->hash_len <=
24940Sstevel@tonic-gate DB_LIM(mp));
24950Sstevel@tonic-gate bzero(optr + hash->confound_len, hash->hash_len);
24960Sstevel@tonic-gate
24970Sstevel@tonic-gate /* calculate MD5 chksum of confounder + input */
24980Sstevel@tonic-gate if (hash->hashfunc) {
24990Sstevel@tonic-gate (void) hash->hashfunc(newcksum, optr, inlen);
25000Sstevel@tonic-gate }
25010Sstevel@tonic-gate }
25020Sstevel@tonic-gate
25030Sstevel@tonic-gate if (bcmp(cksum, newcksum, hash->hash_len)) {
25040Sstevel@tonic-gate #ifdef DEBUG
25050Sstevel@tonic-gate cmn_err(CE_WARN, "des_cbc_decrypt: checksum "
25060Sstevel@tonic-gate "verification failed");
25070Sstevel@tonic-gate #endif
25080Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
25090Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
25100Sstevel@tonic-gate *mp->b_rptr = EIO;
25110Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
25120Sstevel@tonic-gate freemsg(mp->b_cont);
25130Sstevel@tonic-gate mp->b_cont = NULL;
25140Sstevel@tonic-gate qreply(WR(q), mp);
25150Sstevel@tonic-gate return (NULL);
25160Sstevel@tonic-gate }
25170Sstevel@tonic-gate }
25180Sstevel@tonic-gate
25190Sstevel@tonic-gate datalen = inlen - hash->confound_len - hash->hash_len;
25200Sstevel@tonic-gate
25210Sstevel@tonic-gate /* Move just the decrypted input into place if necessary */
25220Sstevel@tonic-gate if (hash->confound_len > 0 || hash->hash_len > 0) {
25230Sstevel@tonic-gate if (tmi->dec_data.method == CRYPT_METHOD_DES3_CBC_SHA1)
25240Sstevel@tonic-gate mp->b_rptr += hash->confound_len;
25250Sstevel@tonic-gate else
25260Sstevel@tonic-gate mp->b_rptr += hash->confound_len + hash->hash_len;
25270Sstevel@tonic-gate }
25280Sstevel@tonic-gate
25290Sstevel@tonic-gate ASSERT(mp->b_rptr + datalen <= DB_LIM(mp));
25300Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + datalen;
25310Sstevel@tonic-gate
25320Sstevel@tonic-gate return (mp);
25330Sstevel@tonic-gate }
25340Sstevel@tonic-gate
25350Sstevel@tonic-gate static mblk_t *
do_decrypt(queue_t * q,mblk_t * mp)25360Sstevel@tonic-gate do_decrypt(queue_t *q, mblk_t *mp)
25370Sstevel@tonic-gate {
25380Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25390Sstevel@tonic-gate mblk_t *outmp;
25400Sstevel@tonic-gate
25410Sstevel@tonic-gate switch (tmi->dec_data.method) {
25420Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
25430Sstevel@tonic-gate outmp = des_cfb_decrypt(q, tmi, mp);
25440Sstevel@tonic-gate break;
25450Sstevel@tonic-gate case CRYPT_METHOD_NONE:
25460Sstevel@tonic-gate outmp = mp;
25470Sstevel@tonic-gate break;
25480Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
25490Sstevel@tonic-gate outmp = des_cbc_decrypt(q, tmi, mp, &null_hash);
25500Sstevel@tonic-gate break;
25510Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
25520Sstevel@tonic-gate outmp = des_cbc_decrypt(q, tmi, mp, &md5_hash);
25530Sstevel@tonic-gate break;
25540Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
25550Sstevel@tonic-gate outmp = des_cbc_decrypt(q, tmi, mp, &crc32_hash);
25560Sstevel@tonic-gate break;
25570Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
25580Sstevel@tonic-gate outmp = des_cbc_decrypt(q, tmi, mp, &sha1_hash);
25590Sstevel@tonic-gate break;
25600Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
25610Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
25620Sstevel@tonic-gate outmp = arcfour_hmac_md5_decrypt(q, tmi, mp, &md5_hash);
25630Sstevel@tonic-gate break;
25640Sstevel@tonic-gate case CRYPT_METHOD_AES128:
25650Sstevel@tonic-gate case CRYPT_METHOD_AES256:
25660Sstevel@tonic-gate outmp = aes_decrypt(q, tmi, mp, &sha1_hash);
25670Sstevel@tonic-gate break;
25680Sstevel@tonic-gate }
25690Sstevel@tonic-gate return (outmp);
25700Sstevel@tonic-gate }
25710Sstevel@tonic-gate
25720Sstevel@tonic-gate /*
25730Sstevel@tonic-gate * do_encrypt
25740Sstevel@tonic-gate *
25750Sstevel@tonic-gate * Generic encryption routine for a single message block.
25760Sstevel@tonic-gate * The input mblk may be replaced by some encrypt routines
25770Sstevel@tonic-gate * because they add extra data in some cases that may exceed
25780Sstevel@tonic-gate * the input mblk_t size limit.
25790Sstevel@tonic-gate */
25800Sstevel@tonic-gate static mblk_t *
do_encrypt(queue_t * q,mblk_t * mp)25810Sstevel@tonic-gate do_encrypt(queue_t *q, mblk_t *mp)
25820Sstevel@tonic-gate {
25830Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
25840Sstevel@tonic-gate mblk_t *outmp;
25850Sstevel@tonic-gate
25860Sstevel@tonic-gate switch (tmi->enc_data.method) {
25870Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
25880Sstevel@tonic-gate outmp = des_cfb_encrypt(q, tmi, mp);
25890Sstevel@tonic-gate break;
25900Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
25910Sstevel@tonic-gate outmp = des_cbc_encrypt(q, tmi, mp, &null_hash);
25920Sstevel@tonic-gate break;
25930Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
25940Sstevel@tonic-gate outmp = des_cbc_encrypt(q, tmi, mp, &md5_hash);
25950Sstevel@tonic-gate break;
25960Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
25970Sstevel@tonic-gate outmp = des_cbc_encrypt(q, tmi, mp, &crc32_hash);
25980Sstevel@tonic-gate break;
25990Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
26000Sstevel@tonic-gate outmp = des_cbc_encrypt(q, tmi, mp, &sha1_hash);
26010Sstevel@tonic-gate break;
26020Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
26030Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
26040Sstevel@tonic-gate outmp = arcfour_hmac_md5_encrypt(q, tmi, mp, &md5_hash);
26050Sstevel@tonic-gate break;
26060Sstevel@tonic-gate case CRYPT_METHOD_AES128:
26070Sstevel@tonic-gate case CRYPT_METHOD_AES256:
26080Sstevel@tonic-gate outmp = aes_encrypt(q, tmi, mp, &sha1_hash);
26090Sstevel@tonic-gate break;
26100Sstevel@tonic-gate case CRYPT_METHOD_NONE:
26110Sstevel@tonic-gate outmp = mp;
26120Sstevel@tonic-gate break;
26130Sstevel@tonic-gate }
26140Sstevel@tonic-gate return (outmp);
26150Sstevel@tonic-gate }
26160Sstevel@tonic-gate
26170Sstevel@tonic-gate /*
26180Sstevel@tonic-gate * setup_crypto
26190Sstevel@tonic-gate *
26200Sstevel@tonic-gate * This takes the data from the CRYPTIOCSETUP ioctl
26210Sstevel@tonic-gate * and sets up a cipher_data_t structure for either
26220Sstevel@tonic-gate * encryption or decryption. This is where the
26230Sstevel@tonic-gate * key and initialization vector data get stored
26240Sstevel@tonic-gate * prior to beginning any crypto functions.
26250Sstevel@tonic-gate *
26260Sstevel@tonic-gate * Special note:
26270Sstevel@tonic-gate * Some applications(e.g. telnetd) have ability to switch
26280Sstevel@tonic-gate * crypto on/off periodically. Thus, the application may call
26290Sstevel@tonic-gate * the CRYPTIOCSETUP ioctl many times for the same stream.
26300Sstevel@tonic-gate * If the CRYPTIOCSETUP is called with 0 length key or ivec fields
26310Sstevel@tonic-gate * assume that the key, block, and saveblock fields that are already
26320Sstevel@tonic-gate * set from a previous CRIOCSETUP call are still valid. This helps avoid
26330Sstevel@tonic-gate * a rekeying error that could occur if we overwrite these fields
26340Sstevel@tonic-gate * with each CRYPTIOCSETUP call.
26350Sstevel@tonic-gate * In short, sometimes, CRYPTIOCSETUP is used to simply toggle on/off
26360Sstevel@tonic-gate * without resetting the original crypto parameters.
26370Sstevel@tonic-gate *
26380Sstevel@tonic-gate */
26390Sstevel@tonic-gate static int
setup_crypto(struct cr_info_t * ci,struct cipher_data_t * cd,int encrypt)26400Sstevel@tonic-gate setup_crypto(struct cr_info_t *ci, struct cipher_data_t *cd, int encrypt)
26410Sstevel@tonic-gate {
26420Sstevel@tonic-gate uint_t newblocklen;
26430Sstevel@tonic-gate uint32_t enc_usage = 0, dec_usage = 0;
26440Sstevel@tonic-gate int rv;
26450Sstevel@tonic-gate
26460Sstevel@tonic-gate /*
26470Sstevel@tonic-gate * Initial sanity checks
26480Sstevel@tonic-gate */
26490Sstevel@tonic-gate if (!CR_METHOD_OK(ci->crypto_method)) {
26500Sstevel@tonic-gate cmn_err(CE_WARN, "Illegal crypto method (%d)",
26510Sstevel@tonic-gate ci->crypto_method);
26520Sstevel@tonic-gate return (EINVAL);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate if (!CR_OPTIONS_OK(ci->option_mask)) {
26550Sstevel@tonic-gate cmn_err(CE_WARN, "Illegal crypto options (%d)",
26560Sstevel@tonic-gate ci->option_mask);
26570Sstevel@tonic-gate return (EINVAL);
26580Sstevel@tonic-gate }
26590Sstevel@tonic-gate if (!CR_IVUSAGE_OK(ci->ivec_usage)) {
26600Sstevel@tonic-gate cmn_err(CE_WARN, "Illegal ivec usage value (%d)",
26610Sstevel@tonic-gate ci->ivec_usage);
26620Sstevel@tonic-gate return (EINVAL);
26630Sstevel@tonic-gate }
26640Sstevel@tonic-gate
26650Sstevel@tonic-gate cd->method = ci->crypto_method;
26660Sstevel@tonic-gate cd->bytes = 0;
26670Sstevel@tonic-gate
26680Sstevel@tonic-gate if (ci->keylen > 0) {
26690Sstevel@tonic-gate if (cd->key != NULL) {
26700Sstevel@tonic-gate kmem_free(cd->key, cd->keylen);
26710Sstevel@tonic-gate cd->key = NULL;
26720Sstevel@tonic-gate cd->keylen = 0;
26730Sstevel@tonic-gate }
26740Sstevel@tonic-gate /*
26750Sstevel@tonic-gate * cd->key holds the copy of the raw key bytes passed in
26760Sstevel@tonic-gate * from the userland app.
26770Sstevel@tonic-gate */
26780Sstevel@tonic-gate cd->key = (char *)kmem_alloc((size_t)ci->keylen, KM_SLEEP);
26790Sstevel@tonic-gate
26800Sstevel@tonic-gate cd->keylen = ci->keylen;
26810Sstevel@tonic-gate bcopy(ci->key, cd->key, (size_t)ci->keylen);
26820Sstevel@tonic-gate }
26830Sstevel@tonic-gate
26840Sstevel@tonic-gate /*
26850Sstevel@tonic-gate * Configure the block size based on the type of cipher.
26860Sstevel@tonic-gate */
26870Sstevel@tonic-gate switch (cd->method) {
26880Sstevel@tonic-gate case CRYPT_METHOD_NONE:
26890Sstevel@tonic-gate newblocklen = 0;
26900Sstevel@tonic-gate break;
26910Sstevel@tonic-gate case CRYPT_METHOD_DES_CFB:
26920Sstevel@tonic-gate newblocklen = DEFAULT_DES_BLOCKLEN;
26930Sstevel@tonic-gate cd->mech_type = crypto_mech2id(SUN_CKM_DES_ECB);
26940Sstevel@tonic-gate break;
26950Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_NULL:
26960Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_MD5:
26970Sstevel@tonic-gate case CRYPT_METHOD_DES_CBC_CRC:
26980Sstevel@tonic-gate newblocklen = DEFAULT_DES_BLOCKLEN;
26990Sstevel@tonic-gate cd->mech_type = crypto_mech2id(SUN_CKM_DES_CBC);
27000Sstevel@tonic-gate break;
27010Sstevel@tonic-gate case CRYPT_METHOD_DES3_CBC_SHA1:
27020Sstevel@tonic-gate newblocklen = DEFAULT_DES_BLOCKLEN;
27030Sstevel@tonic-gate cd->mech_type = crypto_mech2id(SUN_CKM_DES3_CBC);
27040Sstevel@tonic-gate /* 3DES always uses the old usage constant */
27050Sstevel@tonic-gate enc_usage = RCMDV1_USAGE;
27060Sstevel@tonic-gate dec_usage = RCMDV1_USAGE;
27070Sstevel@tonic-gate break;
27080Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5:
27090Sstevel@tonic-gate case CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP:
27100Sstevel@tonic-gate newblocklen = 0;
27110Sstevel@tonic-gate cd->mech_type = crypto_mech2id(SUN_CKM_RC4);
27120Sstevel@tonic-gate break;
27130Sstevel@tonic-gate case CRYPT_METHOD_AES128:
27140Sstevel@tonic-gate case CRYPT_METHOD_AES256:
27150Sstevel@tonic-gate newblocklen = DEFAULT_AES_BLOCKLEN;
27160Sstevel@tonic-gate cd->mech_type = crypto_mech2id(SUN_CKM_AES_ECB);
27170Sstevel@tonic-gate enc_usage = AES_ENCRYPT_USAGE;
27180Sstevel@tonic-gate dec_usage = AES_DECRYPT_USAGE;
27190Sstevel@tonic-gate break;
27200Sstevel@tonic-gate }
27210Sstevel@tonic-gate if (cd->mech_type == CRYPTO_MECH_INVALID) {
27220Sstevel@tonic-gate return (CRYPTO_FAILED);
27230Sstevel@tonic-gate }
27240Sstevel@tonic-gate
27250Sstevel@tonic-gate /*
27260Sstevel@tonic-gate * If RC4, initialize the master crypto key used by
27270Sstevel@tonic-gate * the RC4 algorithm to derive the final encrypt and decrypt keys.
27280Sstevel@tonic-gate */
27290Sstevel@tonic-gate if (cd->keylen > 0 && IS_RC4_METHOD(cd->method)) {
27300Sstevel@tonic-gate /*
27310Sstevel@tonic-gate * cd->ckey is a kernel crypto key structure used as the
27320Sstevel@tonic-gate * master key in the RC4-HMAC crypto operations.
27330Sstevel@tonic-gate */
27340Sstevel@tonic-gate if (cd->ckey == NULL) {
27350Sstevel@tonic-gate cd->ckey = (crypto_key_t *)kmem_zalloc(
27360Sstevel@tonic-gate sizeof (crypto_key_t), KM_SLEEP);
27370Sstevel@tonic-gate }
27380Sstevel@tonic-gate
27390Sstevel@tonic-gate cd->ckey->ck_format = CRYPTO_KEY_RAW;
27400Sstevel@tonic-gate cd->ckey->ck_data = cd->key;
27410Sstevel@tonic-gate
27420Sstevel@tonic-gate /* key length for EF is measured in bits */
27430Sstevel@tonic-gate cd->ckey->ck_length = cd->keylen * 8;
27440Sstevel@tonic-gate }
27450Sstevel@tonic-gate
27460Sstevel@tonic-gate /*
27470Sstevel@tonic-gate * cd->block and cd->saveblock are used as temporary storage for
27480Sstevel@tonic-gate * data that must be carried over between encrypt/decrypt operations
27490Sstevel@tonic-gate * in some of the "feedback" modes.
27500Sstevel@tonic-gate */
27510Sstevel@tonic-gate if (newblocklen != cd->blocklen) {
27520Sstevel@tonic-gate if (cd->block != NULL) {
27530Sstevel@tonic-gate kmem_free(cd->block, cd->blocklen);
27540Sstevel@tonic-gate cd->block = NULL;
27550Sstevel@tonic-gate }
27560Sstevel@tonic-gate
27570Sstevel@tonic-gate if (cd->saveblock != NULL) {
27580Sstevel@tonic-gate kmem_free(cd->saveblock, cd->blocklen);
27590Sstevel@tonic-gate cd->saveblock = NULL;
27600Sstevel@tonic-gate }
27610Sstevel@tonic-gate
27620Sstevel@tonic-gate cd->blocklen = newblocklen;
27630Sstevel@tonic-gate if (cd->blocklen) {
27640Sstevel@tonic-gate cd->block = (char *)kmem_zalloc((size_t)cd->blocklen,
27650Sstevel@tonic-gate KM_SLEEP);
27660Sstevel@tonic-gate }
27670Sstevel@tonic-gate
27680Sstevel@tonic-gate if (cd->method == CRYPT_METHOD_DES_CFB)
27690Sstevel@tonic-gate cd->saveblock = (char *)kmem_zalloc(cd->blocklen,
27700Sstevel@tonic-gate KM_SLEEP);
27710Sstevel@tonic-gate else
27720Sstevel@tonic-gate cd->saveblock = NULL;
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate
27750Sstevel@tonic-gate if (ci->iveclen != cd->ivlen) {
27760Sstevel@tonic-gate if (cd->ivec != NULL) {
27770Sstevel@tonic-gate kmem_free(cd->ivec, cd->ivlen);
27780Sstevel@tonic-gate cd->ivec = NULL;
27790Sstevel@tonic-gate }
27800Sstevel@tonic-gate if (ci->ivec_usage != IVEC_NEVER && ci->iveclen > 0) {
27810Sstevel@tonic-gate cd->ivec = (char *)kmem_zalloc((size_t)ci->iveclen,
27820Sstevel@tonic-gate KM_SLEEP);
27830Sstevel@tonic-gate cd->ivlen = ci->iveclen;
27840Sstevel@tonic-gate } else {
27850Sstevel@tonic-gate cd->ivlen = 0;
27860Sstevel@tonic-gate cd->ivec = NULL;
27870Sstevel@tonic-gate }
27880Sstevel@tonic-gate }
27890Sstevel@tonic-gate cd->option_mask = ci->option_mask;
27900Sstevel@tonic-gate
27910Sstevel@tonic-gate /*
27920Sstevel@tonic-gate * Old protocol requires a static 'usage' value for
27930Sstevel@tonic-gate * deriving keys. Yuk.
27940Sstevel@tonic-gate */
27950Sstevel@tonic-gate if (cd->option_mask & CRYPTOPT_RCMD_MODE_V1) {
27960Sstevel@tonic-gate enc_usage = dec_usage = RCMDV1_USAGE;
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate
27990Sstevel@tonic-gate if (cd->ivlen > cd->blocklen) {
28000Sstevel@tonic-gate cmn_err(CE_WARN, "setup_crypto: IV longer than block size");
28010Sstevel@tonic-gate return (EINVAL);
28020Sstevel@tonic-gate }
28030Sstevel@tonic-gate
28040Sstevel@tonic-gate /*
28050Sstevel@tonic-gate * If we are using an IVEC "correctly" (i.e. set it once)
28060Sstevel@tonic-gate * copy it here.
28070Sstevel@tonic-gate */
28080Sstevel@tonic-gate if (ci->ivec_usage == IVEC_ONETIME && cd->block != NULL)
28090Sstevel@tonic-gate bcopy(ci->ivec, cd->block, (size_t)cd->ivlen);
28100Sstevel@tonic-gate
28110Sstevel@tonic-gate cd->ivec_usage = ci->ivec_usage;
28120Sstevel@tonic-gate if (cd->ivec != NULL) {
28130Sstevel@tonic-gate /* Save the original IVEC in case we need it later */
28140Sstevel@tonic-gate bcopy(ci->ivec, cd->ivec, (size_t)cd->ivlen);
28150Sstevel@tonic-gate }
28160Sstevel@tonic-gate /*
28170Sstevel@tonic-gate * Special handling for 3DES-SHA1-HMAC and AES crypto:
28180Sstevel@tonic-gate * generate derived keys and context templates
28190Sstevel@tonic-gate * for better performance.
28200Sstevel@tonic-gate */
28210Sstevel@tonic-gate if (cd->method == CRYPT_METHOD_DES3_CBC_SHA1 ||
28220Sstevel@tonic-gate IS_AES_METHOD(cd->method)) {
28230Sstevel@tonic-gate crypto_mechanism_t enc_mech;
28240Sstevel@tonic-gate crypto_mechanism_t hmac_mech;
28250Sstevel@tonic-gate
28260Sstevel@tonic-gate if (cd->d_encr_key.ck_data != NULL) {
28270Sstevel@tonic-gate bzero(cd->d_encr_key.ck_data, cd->keylen);
28280Sstevel@tonic-gate kmem_free(cd->d_encr_key.ck_data, cd->keylen);
28290Sstevel@tonic-gate }
28300Sstevel@tonic-gate
28310Sstevel@tonic-gate if (cd->d_hmac_key.ck_data != NULL) {
28320Sstevel@tonic-gate bzero(cd->d_hmac_key.ck_data, cd->keylen);
28330Sstevel@tonic-gate kmem_free(cd->d_hmac_key.ck_data, cd->keylen);
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate
28360Sstevel@tonic-gate if (cd->enc_tmpl != NULL)
28370Sstevel@tonic-gate (void) crypto_destroy_ctx_template(cd->enc_tmpl);
28380Sstevel@tonic-gate
28390Sstevel@tonic-gate if (cd->hmac_tmpl != NULL)
28400Sstevel@tonic-gate (void) crypto_destroy_ctx_template(cd->hmac_tmpl);
28410Sstevel@tonic-gate
28420Sstevel@tonic-gate enc_mech.cm_type = cd->mech_type;
28430Sstevel@tonic-gate enc_mech.cm_param = cd->ivec;
28440Sstevel@tonic-gate enc_mech.cm_param_len = cd->ivlen;
28450Sstevel@tonic-gate
28460Sstevel@tonic-gate hmac_mech.cm_type = sha1_hmac_mech;
28470Sstevel@tonic-gate hmac_mech.cm_param = NULL;
28480Sstevel@tonic-gate hmac_mech.cm_param_len = 0;
28490Sstevel@tonic-gate
28500Sstevel@tonic-gate /*
28510Sstevel@tonic-gate * Create the derived keys.
28520Sstevel@tonic-gate */
28530Sstevel@tonic-gate rv = create_derived_keys(cd,
28540Sstevel@tonic-gate (encrypt ? enc_usage : dec_usage),
28550Sstevel@tonic-gate &cd->d_encr_key, &cd->d_hmac_key);
28560Sstevel@tonic-gate
28570Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) {
28580Sstevel@tonic-gate cmn_err(CE_WARN, "failed to create derived "
28590Sstevel@tonic-gate "keys: %0x", rv);
28600Sstevel@tonic-gate return (CRYPTO_FAILED);
28610Sstevel@tonic-gate }
28620Sstevel@tonic-gate
28630Sstevel@tonic-gate rv = crypto_create_ctx_template(&enc_mech,
28640Sstevel@tonic-gate &cd->d_encr_key,
28650Sstevel@tonic-gate &cd->enc_tmpl, KM_SLEEP);
28660Sstevel@tonic-gate if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28670Sstevel@tonic-gate cd->enc_tmpl = NULL;
28680Sstevel@tonic-gate } else if (rv != CRYPTO_SUCCESS) {
28690Sstevel@tonic-gate cmn_err(CE_WARN, "failed to create enc template "
28700Sstevel@tonic-gate "for d_encr_key: %0x", rv);
28710Sstevel@tonic-gate return (CRYPTO_FAILED);
28720Sstevel@tonic-gate }
28730Sstevel@tonic-gate
28740Sstevel@tonic-gate rv = crypto_create_ctx_template(&hmac_mech,
28750Sstevel@tonic-gate &cd->d_hmac_key,
28760Sstevel@tonic-gate &cd->hmac_tmpl, KM_SLEEP);
28770Sstevel@tonic-gate if (rv == CRYPTO_MECH_NOT_SUPPORTED) {
28780Sstevel@tonic-gate cd->hmac_tmpl = NULL;
28790Sstevel@tonic-gate } else if (rv != CRYPTO_SUCCESS) {
28800Sstevel@tonic-gate cmn_err(CE_WARN, "failed to create hmac template:"
28810Sstevel@tonic-gate " %0x", rv);
28820Sstevel@tonic-gate return (CRYPTO_FAILED);
28830Sstevel@tonic-gate }
28840Sstevel@tonic-gate } else if (IS_RC4_METHOD(cd->method)) {
28850Sstevel@tonic-gate bzero(&cd->d_encr_key, sizeof (crypto_key_t));
28860Sstevel@tonic-gate bzero(&cd->d_hmac_key, sizeof (crypto_key_t));
28870Sstevel@tonic-gate cd->ctx = NULL;
28880Sstevel@tonic-gate cd->enc_tmpl = NULL;
28890Sstevel@tonic-gate cd->hmac_tmpl = NULL;
28900Sstevel@tonic-gate }
28910Sstevel@tonic-gate
28920Sstevel@tonic-gate /* Final sanity checks, make sure no fields are NULL */
28930Sstevel@tonic-gate if (cd->method != CRYPT_METHOD_NONE) {
28940Sstevel@tonic-gate if (cd->block == NULL && cd->blocklen > 0) {
28950Sstevel@tonic-gate #ifdef DEBUG
28960Sstevel@tonic-gate cmn_err(CE_WARN,
28970Sstevel@tonic-gate "setup_crypto: IV block not allocated");
28980Sstevel@tonic-gate #endif
28990Sstevel@tonic-gate return (ENOMEM);
29000Sstevel@tonic-gate }
29010Sstevel@tonic-gate if (cd->key == NULL && cd->keylen > 0) {
29020Sstevel@tonic-gate #ifdef DEBUG
29030Sstevel@tonic-gate cmn_err(CE_WARN,
29040Sstevel@tonic-gate "setup_crypto: key block not allocated");
29050Sstevel@tonic-gate #endif
29060Sstevel@tonic-gate return (ENOMEM);
29070Sstevel@tonic-gate }
29080Sstevel@tonic-gate if (cd->method == CRYPT_METHOD_DES_CFB &&
29090Sstevel@tonic-gate cd->saveblock == NULL && cd->blocklen > 0) {
29100Sstevel@tonic-gate #ifdef DEBUG
29110Sstevel@tonic-gate cmn_err(CE_WARN,
29120Sstevel@tonic-gate "setup_crypto: save block not allocated");
29130Sstevel@tonic-gate #endif
29140Sstevel@tonic-gate return (ENOMEM);
29150Sstevel@tonic-gate }
29160Sstevel@tonic-gate if (cd->ivec == NULL && cd->ivlen > 0) {
29170Sstevel@tonic-gate #ifdef DEBUG
29180Sstevel@tonic-gate cmn_err(CE_WARN,
29190Sstevel@tonic-gate "setup_crypto: IV not allocated");
29200Sstevel@tonic-gate #endif
29210Sstevel@tonic-gate return (ENOMEM);
29220Sstevel@tonic-gate }
29230Sstevel@tonic-gate }
29240Sstevel@tonic-gate return (0);
29250Sstevel@tonic-gate }
29260Sstevel@tonic-gate
29270Sstevel@tonic-gate /*
29280Sstevel@tonic-gate * RCMDS require a 4 byte, clear text
29290Sstevel@tonic-gate * length field before each message.
29300Sstevel@tonic-gate * Add it now.
29310Sstevel@tonic-gate */
29320Sstevel@tonic-gate static mblk_t *
mklenmp(mblk_t * bp,uint32_t len)29330Sstevel@tonic-gate mklenmp(mblk_t *bp, uint32_t len)
29340Sstevel@tonic-gate {
29350Sstevel@tonic-gate mblk_t *lenmp;
29360Sstevel@tonic-gate uchar_t *ucp;
29370Sstevel@tonic-gate
29380Sstevel@tonic-gate if (bp->b_rptr - 4 < DB_BASE(bp) || DB_REF(bp) > 1) {
29390Sstevel@tonic-gate lenmp = allocb(4, BPRI_MED);
29400Sstevel@tonic-gate if (lenmp != NULL) {
29410Sstevel@tonic-gate lenmp->b_rptr = lenmp->b_wptr = DB_LIM(lenmp);
29420Sstevel@tonic-gate linkb(lenmp, bp);
29430Sstevel@tonic-gate bp = lenmp;
29440Sstevel@tonic-gate }
29450Sstevel@tonic-gate }
29460Sstevel@tonic-gate ucp = bp->b_rptr;
29470Sstevel@tonic-gate *--ucp = len;
29480Sstevel@tonic-gate *--ucp = len >> 8;
29490Sstevel@tonic-gate *--ucp = len >> 16;
29500Sstevel@tonic-gate *--ucp = len >> 24;
29510Sstevel@tonic-gate
29520Sstevel@tonic-gate bp->b_rptr = ucp;
29530Sstevel@tonic-gate
29540Sstevel@tonic-gate return (bp);
29550Sstevel@tonic-gate }
29560Sstevel@tonic-gate
29573518Spk193450 static mblk_t *
encrypt_block(queue_t * q,struct tmodinfo * tmi,mblk_t * mp,size_t plainlen)29583518Spk193450 encrypt_block(queue_t *q, struct tmodinfo *tmi, mblk_t *mp, size_t plainlen)
29593518Spk193450 {
29603518Spk193450 mblk_t *newmp;
29613518Spk193450 size_t headspace;
29623518Spk193450
29633518Spk193450 mblk_t *cbp;
29643518Spk193450 size_t cipherlen;
29653518Spk193450 size_t extra = 0;
29663518Spk193450 uint32_t ptlen = (uint32_t)plainlen;
29673518Spk193450 /*
29683518Spk193450 * If we are using the "NEW" RCMD mode,
29693518Spk193450 * add 4 bytes to the plaintext for the
29703518Spk193450 * plaintext length that gets prepended
29713518Spk193450 * before encrypting.
29723518Spk193450 */
29733518Spk193450 if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
29743518Spk193450 ptlen += 4;
29753518Spk193450
29763518Spk193450 cipherlen = encrypt_size(&tmi->enc_data, (size_t)ptlen);
29773518Spk193450
29783518Spk193450 /*
29793518Spk193450 * if we must allocb, then make sure its enough
29803518Spk193450 * to hold the length field so we dont have to allocb
29813518Spk193450 * again down below in 'mklenmp'
29823518Spk193450 */
29833518Spk193450 if (ANY_RCMD_MODE(tmi->enc_data.option_mask)) {
29843518Spk193450 extra = sizeof (uint32_t);
29853518Spk193450 }
29863518Spk193450
29873518Spk193450 /*
29883518Spk193450 * Calculate how much space is needed in front of
29893518Spk193450 * the data.
29903518Spk193450 */
29913518Spk193450 headspace = plaintext_offset(&tmi->enc_data);
29923518Spk193450
29933518Spk193450 /*
29943518Spk193450 * If the current block is too small, reallocate
29953518Spk193450 * one large enough to hold the hdr, tail, and
29963518Spk193450 * ciphertext.
29973518Spk193450 */
29983518Spk193450 if ((cipherlen + extra >= MBLKSIZE(mp)) || DB_REF(mp) > 1) {
29993518Spk193450 int sz = P2ROUNDUP(cipherlen+extra, 8);
30003518Spk193450
30013518Spk193450 cbp = allocb_tmpl(sz, mp);
30023518Spk193450 if (cbp == NULL) {
30033518Spk193450 cmn_err(CE_WARN,
30043518Spk193450 "allocb (%d bytes) failed", sz);
30053518Spk193450 return (NULL);
30063518Spk193450 }
30073518Spk193450
30083518Spk193450 cbp->b_cont = mp->b_cont;
30093518Spk193450
30103518Spk193450 /*
30113518Spk193450 * headspace includes the length fields needed
30123518Spk193450 * for the RCMD modes (v1 == 4 bytes, V2 = 8)
30133518Spk193450 */
30148143SPeter.Shoults@Sun.COM ASSERT(cbp->b_rptr + P2ROUNDUP(plainlen+headspace, 8)
30158143SPeter.Shoults@Sun.COM <= DB_LIM(cbp));
30168143SPeter.Shoults@Sun.COM
30173518Spk193450 cbp->b_rptr = DB_BASE(cbp) + headspace;
30183518Spk193450 bcopy(mp->b_rptr, cbp->b_rptr, plainlen);
30193518Spk193450 cbp->b_wptr = cbp->b_rptr + plainlen;
30203518Spk193450
30213518Spk193450 freeb(mp);
30223518Spk193450 } else {
30233518Spk193450 size_t extra = 0;
30243518Spk193450 cbp = mp;
30253518Spk193450
30263518Spk193450 /*
30273518Spk193450 * Some ciphers add HMAC after the final block
30283518Spk193450 * of the ciphertext, not at the beginning like the
30293518Spk193450 * 1-DES ciphers.
30303518Spk193450 */
30313518Spk193450 if (tmi->enc_data.method ==
30323518Spk193450 CRYPT_METHOD_DES3_CBC_SHA1 ||
30333518Spk193450 IS_AES_METHOD(tmi->enc_data.method)) {
30343518Spk193450 extra = sha1_hash.hash_len;
30353518Spk193450 }
30363518Spk193450
30373518Spk193450 /*
30383518Spk193450 * Make sure the rptr is positioned correctly so that
30393518Spk193450 * routines later do not have to shift this data around
30403518Spk193450 */
30417227Sps57422 if ((cbp->b_rptr + P2ROUNDUP(cipherlen + extra, 8) >
30423518Spk193450 DB_LIM(cbp)) ||
30433518Spk193450 (cbp->b_rptr - headspace < DB_BASE(cbp))) {
30443518Spk193450 ovbcopy(cbp->b_rptr, DB_BASE(cbp) + headspace,
30453518Spk193450 plainlen);
30463518Spk193450 cbp->b_rptr = DB_BASE(cbp) + headspace;
30473518Spk193450 cbp->b_wptr = cbp->b_rptr + plainlen;
30483518Spk193450 }
30493518Spk193450 }
30503518Spk193450
30513518Spk193450 ASSERT(cbp->b_rptr - headspace >= DB_BASE(cbp));
30523518Spk193450 ASSERT(cbp->b_wptr <= DB_LIM(cbp));
30533518Spk193450
30543518Spk193450 /*
30553518Spk193450 * If using RCMD_MODE_V2 (new rcmd mode), prepend
30563518Spk193450 * the plaintext length before the actual plaintext.
30573518Spk193450 */
30583518Spk193450 if (tmi->enc_data.option_mask & CRYPTOPT_RCMD_MODE_V2) {
30593518Spk193450 cbp->b_rptr -= RCMD_LEN_SZ;
30603518Spk193450
30613518Spk193450 /* put plaintext length at head of buffer */
30623518Spk193450 *(cbp->b_rptr + 3) = (uchar_t)(plainlen & 0xff);
30633518Spk193450 *(cbp->b_rptr + 2) = (uchar_t)((plainlen >> 8) & 0xff);
30643518Spk193450 *(cbp->b_rptr + 1) = (uchar_t)((plainlen >> 16) & 0xff);
30653518Spk193450 *(cbp->b_rptr) = (uchar_t)((plainlen >> 24) & 0xff);
30663518Spk193450 }
30673518Spk193450
30683518Spk193450 newmp = do_encrypt(q, cbp);
30693518Spk193450
30703518Spk193450 if (newmp != NULL &&
30713518Spk193450 (tmi->enc_data.option_mask &
30723518Spk193450 (CRYPTOPT_RCMD_MODE_V1 | CRYPTOPT_RCMD_MODE_V2))) {
30733518Spk193450 mblk_t *lp;
30743518Spk193450 /*
30753518Spk193450 * Add length field, required when this is
30763518Spk193450 * used to encrypt "r*" commands(rlogin, rsh)
30773518Spk193450 * with Kerberos.
30783518Spk193450 */
30793518Spk193450 lp = mklenmp(newmp, plainlen);
30803518Spk193450
30813518Spk193450 if (lp == NULL) {
30823518Spk193450 freeb(newmp);
30833518Spk193450 return (NULL);
30843518Spk193450 } else {
30853518Spk193450 newmp = lp;
30863518Spk193450 }
30873518Spk193450 }
30883518Spk193450 return (newmp);
30893518Spk193450 }
30903518Spk193450
30910Sstevel@tonic-gate /*
30920Sstevel@tonic-gate * encrypt_msgb
30930Sstevel@tonic-gate *
30940Sstevel@tonic-gate * encrypt a single message. This routine adds the
30950Sstevel@tonic-gate * RCMD overhead bytes when necessary.
30960Sstevel@tonic-gate */
30970Sstevel@tonic-gate static mblk_t *
encrypt_msgb(queue_t * q,struct tmodinfo * tmi,mblk_t * mp)30980Sstevel@tonic-gate encrypt_msgb(queue_t *q, struct tmodinfo *tmi, mblk_t *mp)
30990Sstevel@tonic-gate {
31003518Spk193450 size_t plainlen, outlen;
31013518Spk193450 mblk_t *newmp = NULL;
31023518Spk193450
31033518Spk193450 /* If not encrypting, do nothing */
31040Sstevel@tonic-gate if (tmi->enc_data.method == CRYPT_METHOD_NONE) {
31050Sstevel@tonic-gate return (mp);
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate
31083518Spk193450 plainlen = MBLKL(mp);
31093518Spk193450 if (plainlen == 0)
31103518Spk193450 return (NULL);
31113518Spk193450
31120Sstevel@tonic-gate /*
31133518Spk193450 * If the block is too big, we encrypt in 4K chunks so that
31143518Spk193450 * older rlogin clients do not choke on the larger buffers.
31150Sstevel@tonic-gate */
31163518Spk193450 while ((plainlen = MBLKL(mp)) > MSGBUF_SIZE) {
31173518Spk193450 mblk_t *mp1 = NULL;
31183518Spk193450 outlen = MSGBUF_SIZE;
31190Sstevel@tonic-gate /*
31203518Spk193450 * Allocate a new buffer that is only 4K bytes, the
31213518Spk193450 * extra bytes are for crypto overhead.
31220Sstevel@tonic-gate */
31233518Spk193450 mp1 = allocb(outlen + CONFOUNDER_BYTES, BPRI_MED);
31243518Spk193450 if (mp1 == NULL) {
31253518Spk193450 cmn_err(CE_WARN,
31263518Spk193450 "allocb (%d bytes) failed",
31273518Spk193450 (int)(outlen + CONFOUNDER_BYTES));
31283518Spk193450 return (NULL);
31290Sstevel@tonic-gate }
31303518Spk193450 /* Copy the next 4K bytes from the old block. */
31313518Spk193450 bcopy(mp->b_rptr, mp1->b_rptr, outlen);
31323518Spk193450 mp1->b_wptr = mp1->b_rptr + outlen;
31333518Spk193450 /* Advance the old block. */
31343518Spk193450 mp->b_rptr += outlen;
31353518Spk193450
31363518Spk193450 /* encrypt the new block */
31373518Spk193450 newmp = encrypt_block(q, tmi, mp1, outlen);
31383518Spk193450 if (newmp == NULL)
31393518Spk193450 return (NULL);
31403518Spk193450
31413518Spk193450 putnext(q, newmp);
31420Sstevel@tonic-gate }
31433518Spk193450 newmp = NULL;
31443518Spk193450 /* If there is data left (< MSGBUF_SIZE), encrypt it. */
31453532Spk193450 if ((plainlen = MBLKL(mp)) > 0)
31463518Spk193450 newmp = encrypt_block(q, tmi, mp, plainlen);
31470Sstevel@tonic-gate
31480Sstevel@tonic-gate return (newmp);
31490Sstevel@tonic-gate }
31500Sstevel@tonic-gate
31510Sstevel@tonic-gate /*
31520Sstevel@tonic-gate * cryptmodwsrv
31530Sstevel@tonic-gate *
31540Sstevel@tonic-gate * Service routine for the write queue.
31550Sstevel@tonic-gate *
31560Sstevel@tonic-gate * Because data may be placed in the queue to hold between
31570Sstevel@tonic-gate * the CRYPTIOCSTOP and CRYPTIOCSTART ioctls, the service routine is needed.
31580Sstevel@tonic-gate */
31590Sstevel@tonic-gate static int
cryptmodwsrv(queue_t * q)31600Sstevel@tonic-gate cryptmodwsrv(queue_t *q)
31610Sstevel@tonic-gate {
31620Sstevel@tonic-gate mblk_t *mp;
31630Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
31640Sstevel@tonic-gate
31650Sstevel@tonic-gate while ((mp = getq(q)) != NULL) {
31660Sstevel@tonic-gate switch (mp->b_datap->db_type) {
31670Sstevel@tonic-gate default:
31680Sstevel@tonic-gate /*
31690Sstevel@tonic-gate * wput does not queue anything > QPCTL
31700Sstevel@tonic-gate */
31710Sstevel@tonic-gate if (!canputnext(q) ||
31720Sstevel@tonic-gate !(tmi->ready & CRYPT_WRITE_READY)) {
31730Sstevel@tonic-gate if (!putbq(q, mp)) {
31740Sstevel@tonic-gate freemsg(mp);
31750Sstevel@tonic-gate }
31760Sstevel@tonic-gate return (0);
31770Sstevel@tonic-gate }
31780Sstevel@tonic-gate putnext(q, mp);
31790Sstevel@tonic-gate break;
31800Sstevel@tonic-gate case M_DATA:
31810Sstevel@tonic-gate if (canputnext(q) && (tmi->ready & CRYPT_WRITE_READY)) {
31820Sstevel@tonic-gate mblk_t *bp;
31830Sstevel@tonic-gate mblk_t *newmsg = NULL;
31840Sstevel@tonic-gate
31850Sstevel@tonic-gate /*
31860Sstevel@tonic-gate * If multiple msgs, concat into 1
31870Sstevel@tonic-gate * to minimize crypto operations later.
31880Sstevel@tonic-gate */
31890Sstevel@tonic-gate if (mp->b_cont != NULL) {
31900Sstevel@tonic-gate bp = msgpullup(mp, -1);
31910Sstevel@tonic-gate if (bp != NULL) {
31920Sstevel@tonic-gate freemsg(mp);
31930Sstevel@tonic-gate mp = bp;
31940Sstevel@tonic-gate }
31950Sstevel@tonic-gate }
31960Sstevel@tonic-gate newmsg = encrypt_msgb(q, tmi, mp);
31970Sstevel@tonic-gate if (newmsg != NULL)
31980Sstevel@tonic-gate putnext(q, newmsg);
31990Sstevel@tonic-gate } else {
32000Sstevel@tonic-gate if (!putbq(q, mp)) {
32010Sstevel@tonic-gate freemsg(mp);
32020Sstevel@tonic-gate }
32030Sstevel@tonic-gate return (0);
32040Sstevel@tonic-gate }
32050Sstevel@tonic-gate break;
32060Sstevel@tonic-gate }
32070Sstevel@tonic-gate }
32080Sstevel@tonic-gate return (0);
32090Sstevel@tonic-gate }
32100Sstevel@tonic-gate
32110Sstevel@tonic-gate static void
start_stream(queue_t * wq,mblk_t * mp,uchar_t dir)32120Sstevel@tonic-gate start_stream(queue_t *wq, mblk_t *mp, uchar_t dir)
32130Sstevel@tonic-gate {
32140Sstevel@tonic-gate mblk_t *newmp = NULL;
32150Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate if (dir == CRYPT_ENCRYPT) {
32180Sstevel@tonic-gate tmi->ready |= CRYPT_WRITE_READY;
32190Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5, SL_TRACE|SL_NOTE,
32200Sstevel@tonic-gate "start_stream: restart ENCRYPT/WRITE q"));
32210Sstevel@tonic-gate
32220Sstevel@tonic-gate enableok(wq);
32230Sstevel@tonic-gate qenable(wq);
32240Sstevel@tonic-gate } else if (dir == CRYPT_DECRYPT) {
32250Sstevel@tonic-gate /*
32260Sstevel@tonic-gate * put any extra data in the RD
32270Sstevel@tonic-gate * queue to be processed and
32280Sstevel@tonic-gate * sent back up.
32290Sstevel@tonic-gate */
32300Sstevel@tonic-gate newmp = mp->b_cont;
32310Sstevel@tonic-gate mp->b_cont = NULL;
32320Sstevel@tonic-gate
32330Sstevel@tonic-gate tmi->ready |= CRYPT_READ_READY;
32340Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
32350Sstevel@tonic-gate SL_TRACE|SL_NOTE,
32360Sstevel@tonic-gate "start_stream: restart "
32370Sstevel@tonic-gate "DECRYPT/READ q"));
32380Sstevel@tonic-gate
32390Sstevel@tonic-gate if (newmp != NULL)
32400Sstevel@tonic-gate if (!putbq(RD(wq), newmp))
32410Sstevel@tonic-gate freemsg(newmp);
32420Sstevel@tonic-gate
32430Sstevel@tonic-gate enableok(RD(wq));
32440Sstevel@tonic-gate qenable(RD(wq));
32450Sstevel@tonic-gate }
32460Sstevel@tonic-gate
32470Sstevel@tonic-gate miocack(wq, mp, 0, 0);
32480Sstevel@tonic-gate }
32490Sstevel@tonic-gate
32500Sstevel@tonic-gate /*
32510Sstevel@tonic-gate * Write-side put procedure. Its main task is to detect ioctls and
32520Sstevel@tonic-gate * FLUSH operations. Other message types are passed on through.
32530Sstevel@tonic-gate */
32540Sstevel@tonic-gate static void
cryptmodwput(queue_t * wq,mblk_t * mp)32550Sstevel@tonic-gate cryptmodwput(queue_t *wq, mblk_t *mp)
32560Sstevel@tonic-gate {
32570Sstevel@tonic-gate struct iocblk *iocp;
32580Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)wq->q_ptr;
32590Sstevel@tonic-gate int ret, err;
32600Sstevel@tonic-gate
32610Sstevel@tonic-gate switch (mp->b_datap->db_type) {
32620Sstevel@tonic-gate case M_DATA:
32630Sstevel@tonic-gate if (wq->q_first == NULL && canputnext(wq) &&
32640Sstevel@tonic-gate (tmi->ready & CRYPT_WRITE_READY) &&
32650Sstevel@tonic-gate tmi->enc_data.method == CRYPT_METHOD_NONE) {
32660Sstevel@tonic-gate putnext(wq, mp);
32670Sstevel@tonic-gate return;
32680Sstevel@tonic-gate }
32690Sstevel@tonic-gate /* else, put it in the service queue */
32700Sstevel@tonic-gate if (!putq(wq, mp)) {
32710Sstevel@tonic-gate freemsg(mp);
32720Sstevel@tonic-gate }
32730Sstevel@tonic-gate break;
32740Sstevel@tonic-gate case M_FLUSH:
32750Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) {
32760Sstevel@tonic-gate flushq(wq, FLUSHDATA);
32770Sstevel@tonic-gate }
32780Sstevel@tonic-gate putnext(wq, mp);
32790Sstevel@tonic-gate break;
32800Sstevel@tonic-gate case M_IOCTL:
32810Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr;
32820Sstevel@tonic-gate switch (iocp->ioc_cmd) {
32830Sstevel@tonic-gate case CRYPTIOCSETUP:
32840Sstevel@tonic-gate ret = 0;
32850Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
32860Sstevel@tonic-gate SL_TRACE | SL_NOTE,
32870Sstevel@tonic-gate "wput: got CRYPTIOCSETUP "
32880Sstevel@tonic-gate "ioctl(%d)", iocp->ioc_cmd));
32890Sstevel@tonic-gate
32900Sstevel@tonic-gate if ((err = miocpullup(mp,
32910Sstevel@tonic-gate sizeof (struct cr_info_t))) != 0) {
32920Sstevel@tonic-gate cmn_err(CE_WARN,
32930Sstevel@tonic-gate "wput: miocpullup failed for cr_info_t");
32940Sstevel@tonic-gate miocnak(wq, mp, 0, err);
32950Sstevel@tonic-gate } else {
32960Sstevel@tonic-gate struct cr_info_t *ci;
32970Sstevel@tonic-gate ci = (struct cr_info_t *)mp->b_cont->b_rptr;
32980Sstevel@tonic-gate
32990Sstevel@tonic-gate if (ci->direction_mask & CRYPT_ENCRYPT) {
33000Sstevel@tonic-gate ret = setup_crypto(ci, &tmi->enc_data, 1);
33010Sstevel@tonic-gate }
33020Sstevel@tonic-gate
33030Sstevel@tonic-gate if (ret == 0 &&
33040Sstevel@tonic-gate (ci->direction_mask & CRYPT_DECRYPT)) {
33050Sstevel@tonic-gate ret = setup_crypto(ci, &tmi->dec_data, 0);
33060Sstevel@tonic-gate }
33070Sstevel@tonic-gate if (ret == 0 &&
33080Sstevel@tonic-gate (ci->direction_mask & CRYPT_DECRYPT) &&
33090Sstevel@tonic-gate ANY_RCMD_MODE(tmi->dec_data.option_mask)) {
33100Sstevel@tonic-gate bzero(&tmi->rcmd_state,
33110Sstevel@tonic-gate sizeof (tmi->rcmd_state));
33120Sstevel@tonic-gate }
33130Sstevel@tonic-gate if (ret == 0) {
33140Sstevel@tonic-gate miocack(wq, mp, 0, 0);
33150Sstevel@tonic-gate } else {
33160Sstevel@tonic-gate cmn_err(CE_WARN,
33170Sstevel@tonic-gate "wput: setup_crypto failed");
33180Sstevel@tonic-gate miocnak(wq, mp, 0, ret);
33190Sstevel@tonic-gate }
33200Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
33210Sstevel@tonic-gate SL_TRACE|SL_NOTE,
33220Sstevel@tonic-gate "wput: done with SETUP "
33230Sstevel@tonic-gate "ioctl"));
33240Sstevel@tonic-gate }
33250Sstevel@tonic-gate break;
33260Sstevel@tonic-gate case CRYPTIOCSTOP:
33270Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
33280Sstevel@tonic-gate SL_TRACE|SL_NOTE,
33290Sstevel@tonic-gate "wput: got CRYPTIOCSTOP "
33300Sstevel@tonic-gate "ioctl(%d)", iocp->ioc_cmd));
33310Sstevel@tonic-gate
33320Sstevel@tonic-gate if ((err = miocpullup(mp, sizeof (uint32_t))) != 0) {
33330Sstevel@tonic-gate cmn_err(CE_WARN,
33340Sstevel@tonic-gate "wput: CRYPTIOCSTOP ioctl wrong "
33350Sstevel@tonic-gate "size (%d should be %d)",
33360Sstevel@tonic-gate (int)iocp->ioc_count,
33370Sstevel@tonic-gate (int)sizeof (uint32_t));
33380Sstevel@tonic-gate miocnak(wq, mp, 0, err);
33390Sstevel@tonic-gate } else {
33400Sstevel@tonic-gate uint32_t *stopdir;
33410Sstevel@tonic-gate
33420Sstevel@tonic-gate stopdir = (uint32_t *)mp->b_cont->b_rptr;
33430Sstevel@tonic-gate if (!CR_DIRECTION_OK(*stopdir)) {
33440Sstevel@tonic-gate miocnak(wq, mp, 0, EINVAL);
33450Sstevel@tonic-gate return;
33460Sstevel@tonic-gate }
33470Sstevel@tonic-gate
33480Sstevel@tonic-gate /* disable the queues until further notice */
33490Sstevel@tonic-gate if (*stopdir & CRYPT_ENCRYPT) {
33500Sstevel@tonic-gate noenable(wq);
33510Sstevel@tonic-gate tmi->ready &= ~CRYPT_WRITE_READY;
33520Sstevel@tonic-gate }
33530Sstevel@tonic-gate if (*stopdir & CRYPT_DECRYPT) {
33540Sstevel@tonic-gate noenable(RD(wq));
33550Sstevel@tonic-gate tmi->ready &= ~CRYPT_READ_READY;
33560Sstevel@tonic-gate }
33570Sstevel@tonic-gate
33580Sstevel@tonic-gate miocack(wq, mp, 0, 0);
33590Sstevel@tonic-gate }
33600Sstevel@tonic-gate break;
33610Sstevel@tonic-gate case CRYPTIOCSTARTDEC:
33620Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
33630Sstevel@tonic-gate SL_TRACE|SL_NOTE,
33640Sstevel@tonic-gate "wput: got CRYPTIOCSTARTDEC "
33650Sstevel@tonic-gate "ioctl(%d)", iocp->ioc_cmd));
33660Sstevel@tonic-gate
33670Sstevel@tonic-gate start_stream(wq, mp, CRYPT_DECRYPT);
33680Sstevel@tonic-gate break;
33690Sstevel@tonic-gate case CRYPTIOCSTARTENC:
33700Sstevel@tonic-gate (void) (STRLOG(CRYPTMOD_ID, 0, 5,
33710Sstevel@tonic-gate SL_TRACE|SL_NOTE,
33720Sstevel@tonic-gate "wput: got CRYPTIOCSTARTENC "
33730Sstevel@tonic-gate "ioctl(%d)", iocp->ioc_cmd));
33740Sstevel@tonic-gate
33750Sstevel@tonic-gate start_stream(wq, mp, CRYPT_ENCRYPT);
33760Sstevel@tonic-gate break;
33770Sstevel@tonic-gate default:
33780Sstevel@tonic-gate putnext(wq, mp);
33790Sstevel@tonic-gate break;
33800Sstevel@tonic-gate }
33810Sstevel@tonic-gate break;
33820Sstevel@tonic-gate default:
33830Sstevel@tonic-gate if (queclass(mp) < QPCTL) {
33840Sstevel@tonic-gate if (wq->q_first != NULL || !canputnext(wq)) {
33850Sstevel@tonic-gate if (!putq(wq, mp))
33860Sstevel@tonic-gate freemsg(mp);
33870Sstevel@tonic-gate return;
33880Sstevel@tonic-gate }
33890Sstevel@tonic-gate }
33900Sstevel@tonic-gate putnext(wq, mp);
33910Sstevel@tonic-gate break;
33920Sstevel@tonic-gate }
33930Sstevel@tonic-gate }
33940Sstevel@tonic-gate
33950Sstevel@tonic-gate /*
33960Sstevel@tonic-gate * decrypt_rcmd_mblks
33970Sstevel@tonic-gate *
33980Sstevel@tonic-gate * Because kerberized r* commands(rsh, rlogin, etc)
33990Sstevel@tonic-gate * use a 4 byte length field to indicate the # of
34000Sstevel@tonic-gate * PLAINTEXT bytes that are encrypted in the field
34010Sstevel@tonic-gate * that follows, we must parse out each message and
34020Sstevel@tonic-gate * break out the length fields prior to sending them
34030Sstevel@tonic-gate * upstream to our Solaris r* clients/servers which do
34040Sstevel@tonic-gate * NOT understand this format.
34050Sstevel@tonic-gate *
34060Sstevel@tonic-gate * Kerberized/encrypted message format:
34070Sstevel@tonic-gate * -------------------------------
34080Sstevel@tonic-gate * | XXXX | N bytes of ciphertext|
34090Sstevel@tonic-gate * -------------------------------
34100Sstevel@tonic-gate *
34110Sstevel@tonic-gate * Where: XXXX = number of plaintext bytes that were encrypted in
34120Sstevel@tonic-gate * to make the ciphertext field. This is done
34130Sstevel@tonic-gate * because we are using a cipher that pads out to
34140Sstevel@tonic-gate * an 8 byte boundary. We only want the application
34150Sstevel@tonic-gate * layer to see the correct number of plain text bytes,
34160Sstevel@tonic-gate * not plaintext + pad. So, after we decrypt, we
34170Sstevel@tonic-gate * must trim the output block down to the intended
34180Sstevel@tonic-gate * plaintext length and eliminate the pad bytes.
34190Sstevel@tonic-gate *
34200Sstevel@tonic-gate * This routine takes the entire input message, breaks it into
34210Sstevel@tonic-gate * a new message that does not contain these length fields and
34220Sstevel@tonic-gate * returns a message consisting of mblks filled with just ciphertext.
34230Sstevel@tonic-gate *
34240Sstevel@tonic-gate */
34250Sstevel@tonic-gate static mblk_t *
decrypt_rcmd_mblks(queue_t * q,mblk_t * mp)34260Sstevel@tonic-gate decrypt_rcmd_mblks(queue_t *q, mblk_t *mp)
34270Sstevel@tonic-gate {
34280Sstevel@tonic-gate mblk_t *newmp = NULL;
34290Sstevel@tonic-gate size_t msglen;
34300Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
34310Sstevel@tonic-gate
34320Sstevel@tonic-gate msglen = msgsize(mp);
34330Sstevel@tonic-gate
34340Sstevel@tonic-gate /*
34350Sstevel@tonic-gate * If we need the length field, get it here.
34360Sstevel@tonic-gate * Test the "plaintext length" indicator.
34370Sstevel@tonic-gate */
34380Sstevel@tonic-gate if (tmi->rcmd_state.pt_len == 0) {
34390Sstevel@tonic-gate uint32_t elen;
34400Sstevel@tonic-gate int tocopy;
34410Sstevel@tonic-gate mblk_t *nextp;
34420Sstevel@tonic-gate
34430Sstevel@tonic-gate /*
34440Sstevel@tonic-gate * Make sure we have recieved all 4 bytes of the
34450Sstevel@tonic-gate * length field.
34460Sstevel@tonic-gate */
34470Sstevel@tonic-gate while (mp != NULL) {
34480Sstevel@tonic-gate ASSERT(tmi->rcmd_state.cd_len < sizeof (uint32_t));
34490Sstevel@tonic-gate
34500Sstevel@tonic-gate tocopy = sizeof (uint32_t) -
34510Sstevel@tonic-gate tmi->rcmd_state.cd_len;
34520Sstevel@tonic-gate if (tocopy > msglen)
34530Sstevel@tonic-gate tocopy = msglen;
34540Sstevel@tonic-gate
34550Sstevel@tonic-gate ASSERT(mp->b_rptr + tocopy <= DB_LIM(mp));
34560Sstevel@tonic-gate bcopy(mp->b_rptr,
34570Sstevel@tonic-gate (char *)(&tmi->rcmd_state.next_len +
34580Sstevel@tonic-gate tmi->rcmd_state.cd_len), tocopy);
34590Sstevel@tonic-gate
34600Sstevel@tonic-gate tmi->rcmd_state.cd_len += tocopy;
34610Sstevel@tonic-gate
34620Sstevel@tonic-gate if (tmi->rcmd_state.cd_len >= sizeof (uint32_t)) {
34630Sstevel@tonic-gate tmi->rcmd_state.next_len =
34640Sstevel@tonic-gate ntohl(tmi->rcmd_state.next_len);
34650Sstevel@tonic-gate break;
34660Sstevel@tonic-gate }
34670Sstevel@tonic-gate
34680Sstevel@tonic-gate nextp = mp->b_cont;
34690Sstevel@tonic-gate mp->b_cont = NULL;
34700Sstevel@tonic-gate freeb(mp);
34710Sstevel@tonic-gate mp = nextp;
34720Sstevel@tonic-gate }
34730Sstevel@tonic-gate
34740Sstevel@tonic-gate if (mp == NULL) {
34750Sstevel@tonic-gate return (NULL);
34760Sstevel@tonic-gate }
34770Sstevel@tonic-gate /*
34780Sstevel@tonic-gate * recalculate the msglen now that we've read the
34790Sstevel@tonic-gate * length and adjusted the bufptr (b_rptr).
34800Sstevel@tonic-gate */
34810Sstevel@tonic-gate msglen -= tocopy;
34820Sstevel@tonic-gate mp->b_rptr += tocopy;
34830Sstevel@tonic-gate
34840Sstevel@tonic-gate tmi->rcmd_state.pt_len = tmi->rcmd_state.next_len;
34850Sstevel@tonic-gate
34860Sstevel@tonic-gate if (tmi->rcmd_state.pt_len <= 0) {
34870Sstevel@tonic-gate /*
34880Sstevel@tonic-gate * Return an IO error to break the connection. there
34890Sstevel@tonic-gate * is no way to recover from this. Usually it means
34900Sstevel@tonic-gate * the app has incorrectly requested decryption on
34910Sstevel@tonic-gate * a non-encrypted stream, thus the "pt_len" field
34920Sstevel@tonic-gate * is negative.
34930Sstevel@tonic-gate */
34940Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
34950Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
34960Sstevel@tonic-gate *mp->b_rptr = EIO;
34970Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
34980Sstevel@tonic-gate
34990Sstevel@tonic-gate freemsg(mp->b_cont);
35000Sstevel@tonic-gate mp->b_cont = NULL;
35010Sstevel@tonic-gate qreply(WR(q), mp);
35020Sstevel@tonic-gate tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35030Sstevel@tonic-gate return (NULL);
35040Sstevel@tonic-gate }
35050Sstevel@tonic-gate
35060Sstevel@tonic-gate /*
35070Sstevel@tonic-gate * If this is V2 mode, then the encrypted data is actually
35080Sstevel@tonic-gate * 4 bytes bigger than the indicated len because the plaintext
35090Sstevel@tonic-gate * length is encrypted for an additional security check, but
35100Sstevel@tonic-gate * its not counted as part of the overall length we just read.
35110Sstevel@tonic-gate * Strange and confusing, but true.
35120Sstevel@tonic-gate */
35130Sstevel@tonic-gate
35140Sstevel@tonic-gate if (tmi->dec_data.option_mask & CRYPTOPT_RCMD_MODE_V2)
35150Sstevel@tonic-gate elen = tmi->rcmd_state.pt_len + 4;
35160Sstevel@tonic-gate else
35170Sstevel@tonic-gate elen = tmi->rcmd_state.pt_len;
35180Sstevel@tonic-gate
35190Sstevel@tonic-gate tmi->rcmd_state.cd_len = encrypt_size(&tmi->dec_data, elen);
35200Sstevel@tonic-gate
35210Sstevel@tonic-gate /*
35220Sstevel@tonic-gate * Allocate an mblk to hold the cipher text until it is
35230Sstevel@tonic-gate * all ready to be processed.
35240Sstevel@tonic-gate */
35250Sstevel@tonic-gate tmi->rcmd_state.c_msg = allocb(tmi->rcmd_state.cd_len,
35260Sstevel@tonic-gate BPRI_HI);
35270Sstevel@tonic-gate if (tmi->rcmd_state.c_msg == NULL) {
35280Sstevel@tonic-gate #ifdef DEBUG
35290Sstevel@tonic-gate cmn_err(CE_WARN, "decrypt_rcmd_msgb: allocb failed "
35300Sstevel@tonic-gate "for %d bytes",
35310Sstevel@tonic-gate (int)tmi->rcmd_state.cd_len);
35320Sstevel@tonic-gate #endif
35330Sstevel@tonic-gate /*
35340Sstevel@tonic-gate * Return an IO error to break the connection.
35350Sstevel@tonic-gate */
35360Sstevel@tonic-gate mp->b_datap->db_type = M_ERROR;
35370Sstevel@tonic-gate mp->b_rptr = mp->b_datap->db_base;
35380Sstevel@tonic-gate *mp->b_rptr = EIO;
35390Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (char);
35400Sstevel@tonic-gate freemsg(mp->b_cont);
35410Sstevel@tonic-gate mp->b_cont = NULL;
35420Sstevel@tonic-gate tmi->rcmd_state.cd_len = tmi->rcmd_state.pt_len = 0;
35430Sstevel@tonic-gate qreply(WR(q), mp);
35440Sstevel@tonic-gate return (NULL);
35450Sstevel@tonic-gate }
35460Sstevel@tonic-gate }
35470Sstevel@tonic-gate
35480Sstevel@tonic-gate /*
35490Sstevel@tonic-gate * If this entire message was just the length field,
35500Sstevel@tonic-gate * free and return. The actual data will probably be next.
35510Sstevel@tonic-gate */
35520Sstevel@tonic-gate if (msglen == 0) {
35530Sstevel@tonic-gate freemsg(mp);
35540Sstevel@tonic-gate return (NULL);
35550Sstevel@tonic-gate }
35560Sstevel@tonic-gate
35570Sstevel@tonic-gate /*
35580Sstevel@tonic-gate * Copy as much of the cipher text as possible into
35590Sstevel@tonic-gate * the new msgb (c_msg).
35600Sstevel@tonic-gate *
35610Sstevel@tonic-gate * Logic: if we got some bytes (msglen) and we still
35620Sstevel@tonic-gate * "need" some bytes (len-rcvd), get them here.
35630Sstevel@tonic-gate */
35640Sstevel@tonic-gate ASSERT(tmi->rcmd_state.c_msg != NULL);
35650Sstevel@tonic-gate if (msglen > 0 &&
35660Sstevel@tonic-gate (tmi->rcmd_state.cd_len > MBLKL(tmi->rcmd_state.c_msg))) {
35670Sstevel@tonic-gate mblk_t *bp, *nextp;
35680Sstevel@tonic-gate size_t n;
35690Sstevel@tonic-gate
35700Sstevel@tonic-gate /*
35710Sstevel@tonic-gate * Walk the mblks and copy just as many bytes as we need
35720Sstevel@tonic-gate * for this particular block of cipher text.
35730Sstevel@tonic-gate */
35740Sstevel@tonic-gate bp = mp;
35750Sstevel@tonic-gate while (bp != NULL) {
35760Sstevel@tonic-gate size_t needed;
35770Sstevel@tonic-gate size_t tocopy;
35780Sstevel@tonic-gate n = MBLKL(bp);
35790Sstevel@tonic-gate
35800Sstevel@tonic-gate needed = tmi->rcmd_state.cd_len -
35810Sstevel@tonic-gate MBLKL(tmi->rcmd_state.c_msg);
35820Sstevel@tonic-gate
35830Sstevel@tonic-gate tocopy = (needed >= n ? n : needed);
35840Sstevel@tonic-gate
35850Sstevel@tonic-gate ASSERT(bp->b_rptr + tocopy <= DB_LIM(bp));
35860Sstevel@tonic-gate ASSERT(tmi->rcmd_state.c_msg->b_wptr + tocopy <=
35870Sstevel@tonic-gate DB_LIM(tmi->rcmd_state.c_msg));
35880Sstevel@tonic-gate
35890Sstevel@tonic-gate /* Copy to end of new mblk */
35900Sstevel@tonic-gate bcopy(bp->b_rptr, tmi->rcmd_state.c_msg->b_wptr,
35910Sstevel@tonic-gate tocopy);
35920Sstevel@tonic-gate
35930Sstevel@tonic-gate tmi->rcmd_state.c_msg->b_wptr += tocopy;
35940Sstevel@tonic-gate
35950Sstevel@tonic-gate bp->b_rptr += tocopy;
35960Sstevel@tonic-gate
35970Sstevel@tonic-gate nextp = bp->b_cont;
35980Sstevel@tonic-gate
35990Sstevel@tonic-gate /*
36000Sstevel@tonic-gate * If we used this whole block, free it and
36010Sstevel@tonic-gate * move on.
36020Sstevel@tonic-gate */
36030Sstevel@tonic-gate if (!MBLKL(bp)) {
36040Sstevel@tonic-gate freeb(bp);
36050Sstevel@tonic-gate bp = NULL;
36060Sstevel@tonic-gate }
36070Sstevel@tonic-gate
36080Sstevel@tonic-gate /* If we got what we needed, stop the loop */
36090Sstevel@tonic-gate if (MBLKL(tmi->rcmd_state.c_msg) ==
36100Sstevel@tonic-gate tmi->rcmd_state.cd_len) {
36110Sstevel@tonic-gate /*
36120Sstevel@tonic-gate * If there is more data in the message,
36130Sstevel@tonic-gate * its for another block of cipher text,
36140Sstevel@tonic-gate * put it back in the queue for next time.
36150Sstevel@tonic-gate */
36160Sstevel@tonic-gate if (bp) {
36170Sstevel@tonic-gate if (!putbq(q, bp))
36180Sstevel@tonic-gate freemsg(bp);
36190Sstevel@tonic-gate } else if (nextp != NULL) {
36200Sstevel@tonic-gate /*
36210Sstevel@tonic-gate * If there is more, put it back in the
36220Sstevel@tonic-gate * queue for another pass thru.
36230Sstevel@tonic-gate */
36240Sstevel@tonic-gate if (!putbq(q, nextp))
36250Sstevel@tonic-gate freemsg(nextp);
36260Sstevel@tonic-gate }
36270Sstevel@tonic-gate break;
36280Sstevel@tonic-gate }
36290Sstevel@tonic-gate bp = nextp;
36300Sstevel@tonic-gate }
36310Sstevel@tonic-gate }
36320Sstevel@tonic-gate /*
36330Sstevel@tonic-gate * Finally, if we received all the cipher text data for
36340Sstevel@tonic-gate * this message, decrypt it into a new msg and send it up
36350Sstevel@tonic-gate * to the app.
36360Sstevel@tonic-gate */
36370Sstevel@tonic-gate if (tmi->rcmd_state.pt_len > 0 &&
36380Sstevel@tonic-gate MBLKL(tmi->rcmd_state.c_msg) == tmi->rcmd_state.cd_len) {
36390Sstevel@tonic-gate mblk_t *bp;
36400Sstevel@tonic-gate mblk_t *newbp;
36410Sstevel@tonic-gate
36420Sstevel@tonic-gate /*
36430Sstevel@tonic-gate * Now we can use our msg that we created when the
36440Sstevel@tonic-gate * initial message boundary was detected.
36450Sstevel@tonic-gate */
36460Sstevel@tonic-gate bp = tmi->rcmd_state.c_msg;
36470Sstevel@tonic-gate tmi->rcmd_state.c_msg = NULL;
36480Sstevel@tonic-gate
36490Sstevel@tonic-gate newbp = do_decrypt(q, bp);
36500Sstevel@tonic-gate if (newbp != NULL) {
36510Sstevel@tonic-gate bp = newbp;
36520Sstevel@tonic-gate /*
36530Sstevel@tonic-gate * If using RCMD_MODE_V2 ("new" mode),
36540Sstevel@tonic-gate * look at the 4 byte plaintext length that
36550Sstevel@tonic-gate * was just decrypted and compare with the
36560Sstevel@tonic-gate * original pt_len value that was received.
36570Sstevel@tonic-gate */
36580Sstevel@tonic-gate if (tmi->dec_data.option_mask &
36590Sstevel@tonic-gate CRYPTOPT_RCMD_MODE_V2) {
36600Sstevel@tonic-gate uint32_t pt_len2;
36610Sstevel@tonic-gate
36620Sstevel@tonic-gate pt_len2 = *(uint32_t *)bp->b_rptr;
36630Sstevel@tonic-gate pt_len2 = ntohl(pt_len2);
36640Sstevel@tonic-gate /*
36650Sstevel@tonic-gate * Make sure the 2 pt len fields agree.
36660Sstevel@tonic-gate */
36670Sstevel@tonic-gate if (pt_len2 != tmi->rcmd_state.pt_len) {
36680Sstevel@tonic-gate cmn_err(CE_WARN,
36690Sstevel@tonic-gate "Inconsistent length fields"
36700Sstevel@tonic-gate " received %d != %d",
36710Sstevel@tonic-gate (int)tmi->rcmd_state.pt_len,
36720Sstevel@tonic-gate (int)pt_len2);
36730Sstevel@tonic-gate bp->b_datap->db_type = M_ERROR;
36740Sstevel@tonic-gate bp->b_rptr = bp->b_datap->db_base;
36750Sstevel@tonic-gate *bp->b_rptr = EIO;
36760Sstevel@tonic-gate bp->b_wptr = bp->b_rptr + sizeof (char);
36770Sstevel@tonic-gate freemsg(bp->b_cont);
36780Sstevel@tonic-gate bp->b_cont = NULL;
36790Sstevel@tonic-gate tmi->rcmd_state.cd_len = 0;
36800Sstevel@tonic-gate qreply(WR(q), bp);
36810Sstevel@tonic-gate return (NULL);
36820Sstevel@tonic-gate }
36830Sstevel@tonic-gate bp->b_rptr += sizeof (uint32_t);
36840Sstevel@tonic-gate }
36850Sstevel@tonic-gate
36860Sstevel@tonic-gate /*
36870Sstevel@tonic-gate * Trim the decrypted block the length originally
36880Sstevel@tonic-gate * indicated by the sender. This is to remove any
36890Sstevel@tonic-gate * padding bytes that the sender added to satisfy
36900Sstevel@tonic-gate * requirements of the crypto algorithm.
36910Sstevel@tonic-gate */
36920Sstevel@tonic-gate bp->b_wptr = bp->b_rptr + tmi->rcmd_state.pt_len;
36930Sstevel@tonic-gate
36940Sstevel@tonic-gate newmp = bp;
36950Sstevel@tonic-gate
36960Sstevel@tonic-gate /*
36970Sstevel@tonic-gate * Reset our state to indicate we are ready
36980Sstevel@tonic-gate * for a new message.
36990Sstevel@tonic-gate */
37000Sstevel@tonic-gate tmi->rcmd_state.pt_len = 0;
37010Sstevel@tonic-gate tmi->rcmd_state.cd_len = 0;
37020Sstevel@tonic-gate } else {
37030Sstevel@tonic-gate #ifdef DEBUG
37040Sstevel@tonic-gate cmn_err(CE_WARN,
37050Sstevel@tonic-gate "decrypt_rcmd: do_decrypt on %d bytes failed",
37060Sstevel@tonic-gate (int)tmi->rcmd_state.cd_len);
37070Sstevel@tonic-gate #endif
37080Sstevel@tonic-gate /*
37090Sstevel@tonic-gate * do_decrypt already handled failures, just
37100Sstevel@tonic-gate * return NULL.
37110Sstevel@tonic-gate */
37120Sstevel@tonic-gate tmi->rcmd_state.pt_len = 0;
37130Sstevel@tonic-gate tmi->rcmd_state.cd_len = 0;
37140Sstevel@tonic-gate return (NULL);
37150Sstevel@tonic-gate }
37160Sstevel@tonic-gate }
37170Sstevel@tonic-gate
37180Sstevel@tonic-gate /*
37190Sstevel@tonic-gate * return the new message with the 'length' fields removed
37200Sstevel@tonic-gate */
37210Sstevel@tonic-gate return (newmp);
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate
37240Sstevel@tonic-gate /*
37250Sstevel@tonic-gate * cryptmodrsrv
37260Sstevel@tonic-gate *
37270Sstevel@tonic-gate * Read queue service routine
37280Sstevel@tonic-gate * Necessary because if the ready flag is not set
37290Sstevel@tonic-gate * (via CRYPTIOCSTOP/CRYPTIOCSTART ioctls) then the data
37300Sstevel@tonic-gate * must remain on queue and not be passed along.
37310Sstevel@tonic-gate */
37320Sstevel@tonic-gate static int
cryptmodrsrv(queue_t * q)37330Sstevel@tonic-gate cryptmodrsrv(queue_t *q)
37340Sstevel@tonic-gate {
37350Sstevel@tonic-gate mblk_t *mp, *bp;
37360Sstevel@tonic-gate struct tmodinfo *tmi = (struct tmodinfo *)q->q_ptr;
37370Sstevel@tonic-gate
37380Sstevel@tonic-gate while ((mp = getq(q)) != NULL) {
37390Sstevel@tonic-gate switch (mp->b_datap->db_type) {
37400Sstevel@tonic-gate case M_DATA:
37410Sstevel@tonic-gate if (canputnext(q) && tmi->ready & CRYPT_READ_READY) {
37420Sstevel@tonic-gate /*
37430Sstevel@tonic-gate * Process "rcmd" messages differently because
37440Sstevel@tonic-gate * they contain a 4 byte plaintext length
37450Sstevel@tonic-gate * id that needs to be removed.
37460Sstevel@tonic-gate */
37470Sstevel@tonic-gate if (tmi->dec_data.method != CRYPT_METHOD_NONE &&
37480Sstevel@tonic-gate (tmi->dec_data.option_mask &
37490Sstevel@tonic-gate (CRYPTOPT_RCMD_MODE_V1 |
37500Sstevel@tonic-gate CRYPTOPT_RCMD_MODE_V2))) {
37510Sstevel@tonic-gate mp = decrypt_rcmd_mblks(q, mp);
37520Sstevel@tonic-gate if (mp)
37530Sstevel@tonic-gate putnext(q, mp);
37540Sstevel@tonic-gate continue;
37550Sstevel@tonic-gate }
37560Sstevel@tonic-gate if ((bp = msgpullup(mp, -1)) != NULL) {
37570Sstevel@tonic-gate freemsg(mp);
37580Sstevel@tonic-gate if (MBLKL(bp) > 0) {
37590Sstevel@tonic-gate mp = do_decrypt(q, bp);
37600Sstevel@tonic-gate if (mp != NULL)
37610Sstevel@tonic-gate putnext(q, mp);
37620Sstevel@tonic-gate }
37630Sstevel@tonic-gate }
37640Sstevel@tonic-gate } else {
37650Sstevel@tonic-gate if (!putbq(q, mp)) {
37660Sstevel@tonic-gate freemsg(mp);
37670Sstevel@tonic-gate }
37680Sstevel@tonic-gate return (0);
37690Sstevel@tonic-gate }
37700Sstevel@tonic-gate break;
37710Sstevel@tonic-gate default:
37720Sstevel@tonic-gate /*
37730Sstevel@tonic-gate * rput does not queue anything > QPCTL, so we don't
37740Sstevel@tonic-gate * need to check for it here.
37750Sstevel@tonic-gate */
37760Sstevel@tonic-gate if (!canputnext(q)) {
37770Sstevel@tonic-gate if (!putbq(q, mp))
37780Sstevel@tonic-gate freemsg(mp);
37790Sstevel@tonic-gate return (0);
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate putnext(q, mp);
37820Sstevel@tonic-gate break;
37830Sstevel@tonic-gate }
37840Sstevel@tonic-gate }
37850Sstevel@tonic-gate return (0);
37860Sstevel@tonic-gate }
37870Sstevel@tonic-gate
37880Sstevel@tonic-gate
37890Sstevel@tonic-gate /*
37900Sstevel@tonic-gate * Read-side put procedure.
37910Sstevel@tonic-gate */
37920Sstevel@tonic-gate static void
cryptmodrput(queue_t * rq,mblk_t * mp)37930Sstevel@tonic-gate cryptmodrput(queue_t *rq, mblk_t *mp)
37940Sstevel@tonic-gate {
37950Sstevel@tonic-gate switch (mp->b_datap->db_type) {
37960Sstevel@tonic-gate case M_DATA:
37970Sstevel@tonic-gate if (!putq(rq, mp)) {
37980Sstevel@tonic-gate freemsg(mp);
37990Sstevel@tonic-gate }
38000Sstevel@tonic-gate break;
38010Sstevel@tonic-gate case M_FLUSH:
38020Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) {
38030Sstevel@tonic-gate flushq(rq, FLUSHALL);
38040Sstevel@tonic-gate }
38050Sstevel@tonic-gate putnext(rq, mp);
38060Sstevel@tonic-gate break;
38070Sstevel@tonic-gate default:
38080Sstevel@tonic-gate if (queclass(mp) < QPCTL) {
38090Sstevel@tonic-gate if (rq->q_first != NULL || !canputnext(rq)) {
38100Sstevel@tonic-gate if (!putq(rq, mp))
38110Sstevel@tonic-gate freemsg(mp);
38120Sstevel@tonic-gate return;
38130Sstevel@tonic-gate }
38140Sstevel@tonic-gate }
38150Sstevel@tonic-gate putnext(rq, mp);
38160Sstevel@tonic-gate break;
38170Sstevel@tonic-gate }
38180Sstevel@tonic-gate }
3819