md5_mod.c revision d2b323064c1e0b7131b3f784139d57c317e9b625
734b6a94890be549309b21156f8ed6d4561cac51darrenm * CDDL HEADER START
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The contents of this file are subject to the terms of the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Common Development and Distribution License (the "License").
734b6a94890be549309b21156f8ed6d4561cac51darrenm * You may not use this file except in compliance with the License.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
734b6a94890be549309b21156f8ed6d4561cac51darrenm * See the License for the specific language governing permissions
734b6a94890be549309b21156f8ed6d4561cac51darrenm * and limitations under the License.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * When distributing Covered Code, include this CDDL HEADER in each
734b6a94890be549309b21156f8ed6d4561cac51darrenm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * If applicable, add the following below this CDDL HEADER, with the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * fields enclosed by brackets "[]" replaced with your own identifying
734b6a94890be549309b21156f8ed6d4561cac51darrenm * information: Portions Copyright [yyyy] [name of copyright owner]
734b6a94890be549309b21156f8ed6d4561cac51darrenm * CDDL HEADER END
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Use is subject to license terms.
734b6a94890be549309b21156f8ed6d4561cac51darrenm#pragma ident "%Z%%M% %I% %E% SMI"
734b6a94890be549309b21156f8ed6d4561cac51darrenm * In kernel module, the md5 module is created with two modlinkages:
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - a modlmisc that allows consumers to directly call the entry points
734b6a94890be549309b21156f8ed6d4561cac51darrenm * MD5Init, MD5Update, and MD5Final.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - a modlcrypto that allows the module to register with the Kernel
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Cryptographic Framework (KCF) as a software provider for the MD5
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanisms.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Module linkage information for the kernel.
734b6a94890be549309b21156f8ed6d4561cac51darrenm "MD5 Message-Digest Algorithm"
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers "MD5 Kernel SW Provider"
734b6a94890be549309b21156f8ed6d4561cac51darrenm * CSPI information (entry points, provider info, etc.)
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef enum md5_mech_type {
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5_HMAC_GEN_MECH_INFO_TYPE /* SUN_CKM_MD5_HMAC_GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define MD5_DIGEST_LENGTH 16 /* MD5 digest length in bytes */
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define MD5_HMAC_MIN_KEY_LEN 8 /* MD5-HMAC min key length in bits */
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define MD5_HMAC_MAX_KEY_LEN INT_MAX /* MD5-HMAC max key length in bits */
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define MD5_HMAC_INTS_PER_BLOCK (MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t))
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Context for MD5 mechanism.
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef struct md5_ctx {
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef struct md5_hmac_ctx {
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Macros to access the MD5 or MD5-HMAC contexts from a context passed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * by KCF to one of the entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_MD5_CTX(ctx) ((md5_ctx_t *)(ctx)->cc_provider_private)
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_MD5_HMAC_CTX(ctx) ((md5_hmac_ctx_t *)(ctx)->cc_provider_private)
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* to extract the digest length passed as mechanism parameter */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (IS_P2ALIGNED((m)->cm_param, sizeof (ulong_t))) \
734b6a94890be549309b21156f8ed6d4561cac51darrenm (len) = (uint32_t)*((ulong_t *)mechanism->cm_param); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t)); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_MD5_DIGEST_KEY(ctx, key, len, digest) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Mechanism info structure passed to KCF during registration.
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* MD5-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* MD5-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_MD5_HMAC_GENERAL, MD5_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic void md5_provider_status(crypto_provider_handle_t, uint_t *);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_digest_update(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_digest_final(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_mac_update(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_mac_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_mac_verify_atomic(crypto_provider_handle_t, crypto_session_id_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int md5_create_ctx_template(crypto_provider_handle_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm "MD5 Software Provider",
734b6a94890be549309b21156f8ed6d4561cac51darrenm sizeof (md5_mech_info_tab)/sizeof (crypto_mech_info_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_kcf_provider_handle_t md5_prov_handle = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Register with KCF. If the registration fails, log an
734b6a94890be549309b21156f8ed6d4561cac51darrenm * error but do not uninstall the module, since the functionality
734b6a94890be549309b21156f8ed6d4561cac51darrenm * provided by misc/md5 should still be available.
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (0);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Unregister from KCF if previous registration succeeded.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((ret = crypto_unregister_provider(md5_prov_handle)) !=
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider control entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_provider_status(crypto_provider_handle_t provider, uint_t *status)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider digest entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize MD5 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (md5_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_MD5_CTX(ctx)->mc_mech_type = MD5_MECH_INFO_TYPE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper MD5 digest update function for uio data.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_update_uio(MD5_CTX *md5_ctx, crypto_data_t *data)
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first iovec containing data to be
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= data->cd_uio->uio_iov[vec_idx++].iov_len)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Now do the digesting on the iovecs.
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (vec_idx < data->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(data->cd_uio->uio_iov[vec_idx].iov_len -
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(md5_ctx, data->cd_uio->uio_iov[vec_idx].iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The end of the specified iovec's was reached but
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the length requested could not be processed, i.e.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested to digest more data than it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper MD5 digest final function for uio data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest_len is the length of the desired digest. If digest_len
734b6a94890be549309b21156f8ed6d4561cac51darrenm * is smaller than the default MD5 digest length, the caller
734b6a94890be549309b21156f8ed6d4561cac51darrenm * must pass a scratch buffer, digest_scratch, which must
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be at least MD5_DIGEST_LENGTH bytes.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_final_uio(MD5_CTX *md5_ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first iovec containing ptr to the digest to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be returned.
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is
734b6a94890be549309b21156f8ed6d4561cac51darrenm * larger than the total size of the buffers
734b6a94890be549309b21156f8ed6d4561cac51darrenm * it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed MD5 digest will fit in the current
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed digest will be crossing one or more iovec's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * This is bad performance-wise but we need to support it.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate a small scratch buffer on the stack and
734b6a94890be549309b21156f8ed6d4561cac51darrenm * copy it piece meal to the specified digest iovec's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The end of the specified iovec's was reached but
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the length requested could not be processed, i.e.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested to digest more data than it
734b6a94890be549309b21156f8ed6d4561cac51darrenm * provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper MD5 digest update for mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_update_mblk(MD5_CTX *md5_ctx, crypto_data_t *data)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first mblk_t containing data to be digested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Now do the digesting on the mblk chain.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The end of the mblk was reached but the length requested
734b6a94890be549309b21156f8ed6d4561cac51darrenm * could not be processed, i.e. The caller requested
734b6a94890be549309b21156f8ed6d4561cac51darrenm * to digest more data than it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper MD5 digest final for mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest_len is the length of the desired digest. If digest_len
734b6a94890be549309b21156f8ed6d4561cac51darrenm * is smaller than the default MD5 digest length, the caller
734b6a94890be549309b21156f8ed6d4561cac51darrenm * must pass a scratch buffer, digest_scratch, which must
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be at least MD5_DIGEST_LENGTH bytes.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_final_mblk(MD5_CTX *md5_ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first mblk_t that will be used to store the digest.
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed MD5 digest will fit in the current mblk.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the MD5Final() in-place.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed digest will be crossing one or more mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * This is bad performance-wise but we need to support it.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate a small scratch buffer on the stack and
734b6a94890be549309b21156f8ed6d4561cac51darrenm * copy it piece meal to the specified digest iovec's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The end of the specified mblk was reached but
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the length requested could not be processed, i.e.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested to digest more data than it
734b6a94890be549309b21156f8ed6d4561cac51darrenm * provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We need to just return the length needed to store the output.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We should not destroy the context for the following cases.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the MD5 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the MD5 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We need to just return the length needed to store the output.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We should not destroy the context for the following cases.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the MD5 init.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the MD5 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&md5_ctx, data->cd_raw.iov_base + data->cd_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider mac entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * MD5 HMAC is: MD5(key XOR opad, MD5(key XOR ipad, text))
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The initialization routine initializes what we denote
734b6a94890be549309b21156f8ed6d4561cac51darrenm * as the inner and outer contexts by doing
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - for inner context: MD5(key XOR ipad)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - for outer context: MD5(key XOR opad)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Each subsequent MD5 HMAC update will result in an
734b6a94890be549309b21156f8ed6d4561cac51darrenm * update of the inner context with the specified data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The MD5 HMAC final will do a MD5 final operation on the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * inner context, and the resulting digest will be used
734b6a94890be549309b21156f8ed6d4561cac51darrenm * as the data for an update on the outer context. Last
734b6a94890be549309b21156f8ed6d4561cac51darrenm * but not least, an MD5 final on the outer context will
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be performed to obtain the MD5 HMAC digest to return
734b6a94890be549309b21156f8ed6d4561cac51darrenm * to the user.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Initialize a MD5-HMAC context.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_mac_init_ctx(md5_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* XOR key with ipad (0x36) and opad (0x5c) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (i = 0; i < MD5_HMAC_INTS_PER_BLOCK; i++) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform MD5 on ipad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&ctx->hc_icontext, ipad, MD5_HMAC_BLOCK_SIZE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform MD5 on opad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&ctx->hc_ocontext, opad, MD5_HMAC_BLOCK_SIZE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Initializes a multi-part MAC operation.
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (md5_hmac_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, compute context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm md5_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Hash the passed-in key to get a smaller key.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The inner context is used since it hasn't been
734b6a94890be549309b21156f8ed6d4561cac51darrenm * initialized yet.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Get the mechanism parameters, if applicable.
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_MD5_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_mac_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 update of the inner context using the specified
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (PROV_MD5_HMAC_CTX(ctx)->hc_mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers digest_len = PROV_MD5_HMAC_CTX(ctx)->hc_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We need to just return the length needed to store the output.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We should not destroy the context for the following cases.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final on the inner context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Final(digest, &PROV_MD5_HMAC_CTX(ctx)->hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final on the outer context, storing the computing
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_update_uio(&(ctx).hc_icontext, data); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, compute context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Hash the passed-in key to get a smaller key.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The inner context is used since it hasn't been
734b6a94890be549309b21156f8ed6d4561cac51darrenm * initialized yet.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Get the mechanism parameters, if applicable.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do an MD5 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do an MD5 final on the inner context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final on the outer context, storing the computed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_uio(&md5_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = md5_digest_final_mblk(&md5_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Extra paranoia: zeroizing the local context on the stack */
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_mac_verify_atomic(crypto_provider_handle_t provider,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, compute context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Hash the passed-in key to get a smaller key.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The inner context is used since it hasn't been
734b6a94890be549309b21156f8ed6d4561cac51darrenm * initialized yet.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Get the mechanism parameters, if applicable.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do an MD5 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do an MD5 final on the inner context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an MD5 final on the outer context, storing the computed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the local digest buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Compare the computed digest against the expected digest passed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * as argument.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* jump to the first iovec containing the expected digest */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is
734b6a94890be549309b21156f8ed6d4561cac51darrenm * larger than the total size of the buffers
734b6a94890be549309b21156f8ed6d4561cac51darrenm * it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do the comparison of computed digest vs specified one */
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (vec_idx < mac->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(mac->cd_uio->uio_iov[vec_idx].iov_len -
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* jump to the first mblk_t containing the expected digest */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider context management entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmmd5_create_ctx_template(crypto_provider_handle_t provider,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE) &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm (mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE))
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize MD5 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm md5_hmac_ctx_tmpl = kmem_alloc(sizeof (md5_hmac_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Hash the passed-in key to get a smaller key.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The inner context is used since it hasn't been
734b6a94890be549309b21156f8ed6d4561cac51darrenm * initialized yet.
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_MD5_DIGEST_KEY(&md5_hmac_ctx_tmpl->hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm md5_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm *ctx_template = (crypto_spi_ctx_template_t)md5_hmac_ctx_tmpl;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We have to free either MD5 or MD5-HMAC contexts, which
734b6a94890be549309b21156f8ed6d4561cac51darrenm * have different lengths.