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
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Use is subject to license terms.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The sha2 module is created with two modlinkages:
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - a modlmisc that allows consumers to directly call the entry points
734b6a94890be549309b21156f8ed6d4561cac51darrenm * SHA2Init, SHA2Update, and SHA2Final.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - a modlcrypto that allows the module to register with the Kernel
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Cryptographic Framework (KCF) as a software provider for the SHA2
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanisms.
734b6a94890be549309b21156f8ed6d4561cac51darrenm "SHA2 Message-Digest Algorithm"
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers "SHA2 Kernel SW Provider"
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Macros to access the SHA2 or SHA2-HMAC contexts from a context passed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * by KCF to one of the entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_SHA2_CTX(ctx) ((sha2_ctx_t *)(ctx)->cc_provider_private)
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_SHA2_HMAC_CTX(ctx) ((sha2_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))) \
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson (len) = (uint32_t)*((ulong_t *)(void *)(m)->cm_param); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t)); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_SHA2_DIGEST_KEY(mech, ctx, key, len, digest) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Mechanism info structure passed to KCF during registration.
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA256_HMAC_GENERAL, SHA256_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA384_HMAC_GENERAL, SHA384_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA512_HMAC_GENERAL, SHA512_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic void sha2_provider_status(crypto_provider_handle_t, uint_t *);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_update(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_final(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_mac_update(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_mac_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_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 sha2_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 sha2_create_ctx_template(crypto_provider_handle_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm "SHA2 Software Provider",
734b6a94890be549309b21156f8ed6d4561cac51darrenm sizeof (sha2_mech_info_tab)/sizeof (crypto_mech_info_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_kcf_provider_handle_t sha2_prov_handle = NULL;
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino * Register with KCF. If the registration fails, do not uninstall the
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino * module, since the functionality provided by misc/sha2 should still
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino * be available.
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino (void) crypto_register_provider(&sha2_prov_info, &sha2_prov_handle);
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (0);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider control entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_provider_status(crypto_provider_handle_t provider, uint_t *status)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider digest entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize SHA2 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (sha2_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_CTX(ctx)->sc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(mechanism->cm_type, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper SHA2 digest update function for uio data.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update_uio(SHA2_CTX *sha2_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 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 SHA2 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 SHA2 digest length, the caller
734b6a94890be549309b21156f8ed6d4561cac51darrenm * must pass a scratch buffer, digest_scratch, which must
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be at least the algorithm's digest length bytes.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_final_uio(SHA2_CTX *sha2_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 SHA2 digest will fit in the current
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
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 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 SHA2 digest update for mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update_mblk(SHA2_CTX *sha2_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 SHA2 digest final for mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest_len is the length of the desired digest. If digest_len
734b6a94890be549309b21156f8ed6d4561cac51darrenm * is smaller than the default SHA2 digest length, the caller
734b6a94890be549309b21156f8ed6d4561cac51darrenm * must pass a scratch buffer, digest_scratch, which must
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be at least the algorithm's digest length bytes.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_final_mblk(SHA2_CTX *sha2_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 SHA2 digest will fit in the current mblk.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA2Final() in-place.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_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 SHA2 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA2 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_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 a SHA2 final.
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_atomic(crypto_provider_handle_t provider,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA inits.
734b6a94890be549309b21156f8ed6d4561cac51darrenm cd_raw.iov_base + data->cd_offset, data->cd_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA updates on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider mac entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * SHA2 HMAC is: SHA2(key XOR opad, SHA2(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: SHA2(key XOR ipad)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * - for outer context: SHA2(key XOR opad)
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Each subsequent SHA2 HMAC update will result in an
734b6a94890be549309b21156f8ed6d4561cac51darrenm * update of the inner context with the specified data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The SHA2 HMAC final will do a SHA2 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, a SHA2 final on the outer context will
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be performed to obtain the SHA2 HMAC digest to return
734b6a94890be549309b21156f8ed6d4561cac51darrenm * to the user.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Initialize a SHA2-HMAC context.
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_init_ctx(sha2_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint64_t ipad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint64_t opad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Determine the block size */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx->hc_mech_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm blocks_per_int64 = SHA256_HMAC_BLOCK_SIZE / sizeof (uint64_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm blocks_per_int64 = SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* XOR key with ipad (0x36) and opad (0x5c) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (i = 0; i < blocks_per_int64; i ++) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform SHA2 on ipad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&ctx->hc_icontext, (uint8_t *)ipad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform SHA2 on opad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&ctx->hc_ocontext, (uint8_t *)opad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_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);
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (sha2_hmac_ctx_t),
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna PROV_SHA2_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, compute context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_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 if (PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len > sha_digest_len)
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_update(crypto_ctx_t *ctx, crypto_data_t *data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 update of the inner context using the specified
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson /* Set the digest lengths to values appropriate to the mechanism */
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len = PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len = PROV_SHA2_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 a SHA2 final on the inner context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 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 +
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna bzero(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_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 uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, initialize 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 /* do a SHA2 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the inner context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * HMAC-SHA384 needs special handling as the outer hash needs only 48
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * bytes of the inner hash value.
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type == SHA384_HMAC_MECH_INFO_TYPE ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm mechanism->cm_type == SHA384_HMAC_GEN_MECH_INFO_TYPE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 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 = sha2_digest_final_uio(&sha2_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&sha2_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_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 uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
52aa4e90b7b198e065746fab6505afed43f0fe88Mark Powers sha2_hmac_ctx.hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, initialize 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 /* do a SHA2 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do a SHA2 final on the inner context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * HMAC-SHA384 needs special handling as the outer hash needs only 48
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * bytes of the inner hash value.
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd if (mechanism->cm_type == SHA384_HMAC_MECH_INFO_TYPE ||
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd mechanism->cm_type == SHA384_HMAC_GEN_MECH_INFO_TYPE)
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the outer context, storing the computed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users 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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_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);
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize SHA2 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_tmpl = kmem_alloc(sizeof (sha2_hmac_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
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 *ctx_template = (crypto_spi_ctx_template_t)sha2_hmac_ctx_tmpl;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We have to free either SHA2 or SHA2-HMAC contexts, which
734b6a94890be549309b21156f8ed6d4561cac51darrenm * have different lengths.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Note: Below is dependent on the mechanism ordering.