des_crypt.c revision 23c57df72989c916b3e98084eb88d48777999691
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* des_crypt.c, DES encryption library routines
*/
#include <sys/sysmacros.h>
#include <des/des_impl.h>
/* EXPORT DELETE START */
#include <rpc/des_crypt.h>
#ifdef sun_hardware
#ifdef _KERNEL
static int g_desfd = -1;
#else
#endif /* _KERNEL */
#endif /* sun */
/* EXPORT DELETE END */
extern struct mod_ops mod_cryptoops;
/*
* Module linkage information for the kernel.
*/
"des encryption",
};
static struct modlcrypto modlcrypto = {
"DES Kernel SW Provider"
};
static struct modlinkage modlinkage = {
&modlmisc,
};
/*
* CSPI information (entry points, provider info, etc.)
*/
typedef enum des_mech_type {
DES_ECB_MECH_INFO_TYPE, /* SUN_CKM_DES_ECB */
DES_CBC_MECH_INFO_TYPE, /* SUN_CKM_DES_CBC */
DES_CFB_MECH_INFO_TYPE, /* SUN_CKM_DES_CFB */
DES3_ECB_MECH_INFO_TYPE, /* SUN_CKM_DES3_ECB */
DES3_CBC_MECH_INFO_TYPE, /* SUN_CKM_DES3_CBC */
DES3_CFB_MECH_INFO_TYPE /* SUN_CKM_DES3_CFB */
/* EXPORT DELETE START */
#define DES_MIN_KEY_LEN DES_MINBYTES
#define DES_MAX_KEY_LEN DES_MAXBYTES
#define DES3_MIN_KEY_LEN DES3_MINBYTES
#define DES3_MAX_KEY_LEN DES3_MAXBYTES
/* EXPORT DELETE END */
#ifndef DES_MIN_KEY_LEN
#define DES_MIN_KEY_LEN 0
#endif
#ifndef DES_MAX_KEY_LEN
#define DES_MAX_KEY_LEN 0
#endif
#ifndef DES3_MIN_KEY_LEN
#define DES3_MIN_KEY_LEN 0
#endif
#ifndef DES3_MAX_KEY_LEN
#define DES3_MAX_KEY_LEN 0
#endif
/*
* Mechanism info structure passed to KCF during registration.
*/
static crypto_mech_info_t des_mech_info_tab[] = {
/* DES_ECB */
/* DES_CBC */
/* DES3_ECB */
/* DES3_CBC */
};
/* operations are in-place if the output buffer is NULL */
static crypto_control_ops_t des_control_ops = {
};
static int
static crypto_cipher_ops_t des_cipher_ops = {
};
static int des_create_ctx_template(crypto_provider_handle_t,
static int des_free_context(crypto_ctx_t *);
static crypto_ctx_ops_t des_ctx_ops = {
};
crypto_key_t *);
static crypto_key_ops_t des_key_ops = {
NULL,
NULL,
NULL,
NULL,
NULL,
};
static crypto_ops_t des_crypto_ops = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static crypto_provider_info_t des_prov_info = {
"DES Software Provider",
{&modlinkage},
NULL,
sizeof (des_mech_info_tab)/sizeof (crypto_mech_info_t),
};
int
_init(void)
{
int ret;
return (ret);
/*
* Register with KCF. If the registration fails, log an
* error but do not uninstall the module, since the functionality
*/
&des_prov_handle)) != CRYPTO_SUCCESS) {
"failed (0x%x)", ret);
}
return (0);
}
int
{
}
/*
* Copy 8 bytes
*/
char *a = (char *)dst; \
char *b = (char *)src; \
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
}
/*
* Copy multiple of 8 bytes
*/
char *a = (char *)dst; \
char *b = (char *)src; \
int i; \
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
} \
}
/*
* CBC mode encryption
*/
/* ARGSUSED */
int
{
int err = 0;
/* EXPORT DELETE START */
/* EXPORT DELETE END */
return (err);
}
/*
* ECB mode encryption
*/
/* ARGSUSED */
int
{
int err = 0;
/* EXPORT DELETE START */
/* EXPORT DELETE END */
return (err);
}
/* EXPORT DELETE START */
/*
* Common code to cbc_crypt() & ecb_crypt()
*/
static int
{
int desdev;
return (DESERR_BADPARAM);
#ifdef sun_hardware
int res;
if (g_desfd < 0 &&
goto software; /* no hardware device */
/*
* hardware
*/
if (len <= DES_QUICKLEN) {
} else {
}
}
#endif
/*
* software
*/
return (DESERR_HWERROR);
}
/*
* Initialize key schedules for DES and DES3
*/
static int
{
/*
* Only keys by value are supported by this module.
*/
case CRYPTO_KEY_RAW:
return (CRYPTO_KEY_SIZE_RANGE);
return (CRYPTO_KEY_SIZE_RANGE);
break;
default:
return (CRYPTO_KEY_TYPE_INCONSISTENT);
}
/*
* Fix parity bits.
* Initialize key schedule even if key is weak.
*/
return (CRYPTO_ARGUMENTS_BAD);
return (CRYPTO_SUCCESS);
}
/* EXPORT DELETE END */
/*
* KCF software provider control entry points.
*/
/* ARGSUSED */
static void
{
}
/*
* KCF software provider encrypt entry points.
*/
static int
{
/* EXPORT DELETE START */
int rv;
int kmflag;
/*
* Only keys by value are supported by this module.
*/
return (CRYPTO_KEY_TYPE_INCONSISTENT);
}
/* Check mechanism type and parameter length */
case DES_ECB_MECH_INFO_TYPE:
/* FALLTHRU */
case DES_CBC_MECH_INFO_TYPE:
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
case DES3_ECB_MECH_INFO_TYPE:
/* FALLTHRU */
case DES3_CBC_MECH_INFO_TYPE:
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
default:
return (CRYPTO_MECHANISM_INVALID);
}
return (rv);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
static void
{
/* LINTED: pointer alignment */
} else {
#ifdef _BIG_ENDIAN
#else
#endif /* _BIG_ENDIAN */
}
}
/* ARGSUSED */
static int
{
int ret;
/* EXPORT DELETE START */
/*
* Plaintext must be a multiple of the block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_DATA_LEN_RANGE);
/*
* We need to just return the length needed to store the output.
* We should not destroy the context for the following case.
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
(void) des_free_context(ctx);
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* ARGSUSED */
static int
{
int ret;
/* EXPORT DELETE START */
/*
* Ciphertext must be a multiple of the block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
/*
* We need to just return the length needed to store the output.
* We should not destroy the context for the following case.
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
(void) des_free_context(ctx);
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_SUCCESS;
/* EXPORT DELETE START */
/* compute number of bytes that will hold the ciphertext */
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do the DES update on the specified input data.
*/
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext)
} else {
}
/* EXPORT DELETE END */
return (ret);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_SUCCESS;
/* EXPORT DELETE START */
/* compute number of bytes that will hold the plaintext */
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do the DES update on the specified input data.
*/
switch (ciphertext->cd_format) {
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
if (ret == CRYPTO_SUCCESS) {
if (ciphertext != plaintext)
} else {
}
/* EXPORT DELETE END */
return (ret);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
/*
* There must be no unprocessed plaintext.
* This happens if the length of the last data is
* not a multiple of the DES block length.
*/
if (des_ctx->dc_remainder_len > 0)
return (CRYPTO_DATA_LEN_RANGE);
(void) des_free_context(ctx);
ciphertext->cd_length = 0;
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
/*
* There must be no unprocessed ciphertext.
* This happens if the length of the last ciphertext is
* not a multiple of the DES block length.
*/
if (des_ctx->dc_remainder_len > 0)
return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
(void) des_free_context(ctx);
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
int ret;
/* EXPORT DELETE START */
/*
* Plaintext must be a multiple of the block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_DATA_LEN_RANGE);
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/* Check mechanism type and parameter length */
case DES_ECB_MECH_INFO_TYPE:
case DES_CBC_MECH_INFO_TYPE:
if (mechanism->cm_param_len > 0 &&
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
case DES3_ECB_MECH_INFO_TYPE:
case DES3_CBC_MECH_INFO_TYPE:
if (mechanism->cm_param_len > 0 &&
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
default:
return (CRYPTO_MECHANISM_INVALID);
}
return (ret);
}
/*
* Do the update on the specified input data.
*/
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
}
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext)
} else {
}
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* ARGSUSED */
static int
{
int ret;
/* EXPORT DELETE START */
/*
* Ciphertext must be a multiple of the block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_DATA_LEN_RANGE);
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/* Check mechanism type and parameter length */
case DES_ECB_MECH_INFO_TYPE:
case DES_CBC_MECH_INFO_TYPE:
if (mechanism->cm_param_len > 0 &&
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
case DES3_ECB_MECH_INFO_TYPE:
case DES3_CBC_MECH_INFO_TYPE:
if (mechanism->cm_param_len > 0 &&
return (CRYPTO_MECHANISM_PARAM_INVALID);
return (CRYPTO_KEY_SIZE_RANGE);
break;
default:
return (CRYPTO_MECHANISM_INVALID);
}
return (ret);
}
/*
* Do the update on the specified input data.
*/
switch (ciphertext->cd_format) {
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
}
if (ret == CRYPTO_SUCCESS) {
if (ciphertext != plaintext)
} else {
}
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/*
* KCF software provider context template entry points.
*/
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
void *keysched;
int rv;
case DES_ECB_MECH_INFO_TYPE:
break;
case DES_CBC_MECH_INFO_TYPE:
break;
case DES3_ECB_MECH_INFO_TYPE:
break;
case DES3_CBC_MECH_INFO_TYPE:
break;
default:
return (CRYPTO_MECHANISM_INVALID);
}
return (CRYPTO_HOST_MEMORY);
}
/*
* Initialize key schedule. Key length information is stored
* in the key.
*/
return (rv);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
}
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/*
* Pass it to des_keycheck() which will
* fix it (parity bits), and check if the fixed key is weak.
*/
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
int expectedkeylen;
return (CRYPTO_ARGUMENTS_BAD);
case DES_ECB_MECH_INFO_TYPE:
case DES_CBC_MECH_INFO_TYPE:
break;
case DES3_ECB_MECH_INFO_TYPE:
case DES3_CBC_MECH_INFO_TYPE:
break;
default:
return (CRYPTO_MECHANISM_INVALID);
}
return (CRYPTO_KEY_TYPE_INCONSISTENT);
return (CRYPTO_KEY_SIZE_RANGE);
return (CRYPTO_WEAK_KEY);
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
int kmflag)
{
int rv = CRYPTO_SUCCESS;
/* EXPORT DELETE START */
void *keysched;
return (CRYPTO_HOST_MEMORY);
/*
* Initialize key schedule.
* Key length is stored in the key.
*/
strength)) != CRYPTO_SUCCESS)
} else {
}
}
case DES_CBC_MECH_INFO_TYPE:
case DES3_CBC_MECH_INFO_TYPE:
break;
case DES_ECB_MECH_INFO_TYPE:
case DES3_ECB_MECH_INFO_TYPE:
}
if (rv != CRYPTO_SUCCESS) {
}
}
/* EXPORT DELETE END */
return (rv);
}