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