734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * CDDL HEADER START
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
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 *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
734b6a94890be549309b21156f8ed6d4561cac51darrenm * or http://www.opensolaris.org/os/licensing.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * See the License for the specific language governing permissions
734b6a94890be549309b21156f8ed6d4561cac51darrenm * and limitations under the License.
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
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 *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * CDDL HEADER END
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Use is subject to license terms.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/modctl.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/cmn_err.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/crypto/common.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/crypto/spi.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/strsun.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/systm.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/sysmacros.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define _SHA2_IMPL
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <sys/sha2.h>
b5a2d8455dfa3190fc977c4bec53e91c99012767Hai-May Chao#include <sha2/sha2_impl.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic struct modlmisc modlmisc = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm &mod_miscops,
734b6a94890be549309b21156f8ed6d4561cac51darrenm "SHA2 Message-Digest Algorithm"
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic struct modlcrypto modlcrypto = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm &mod_cryptoops,
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers "SHA2 Kernel SW Provider"
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic struct modlinkage modlinkage = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm MODREV_1, &modlmisc, &modlcrypto, NULL
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Macros to access the SHA2 or SHA2-HMAC contexts from a context passed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * by KCF to one of the entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
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
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* to extract the digest length passed as mechanism parameter */
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_SHA2_GET_DIGEST_LEN(m, len) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (IS_P2ALIGNED((m)->cm_param, sizeof (ulong_t))) \
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson (len) = (uint32_t)*((ulong_t *)(void *)(m)->cm_param); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm else { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm ulong_t tmp_ulong; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t)); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm (len) = (uint32_t)tmp_ulong; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm } \
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define PROV_SHA2_DIGEST_KEY(mech, ctx, key, len, digest) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(mech, ctx); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(ctx, key, len); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, ctx); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Mechanism info structure passed to KCF during registration.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_mech_info_t sha2_mech_info_tab[] = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA256, SHA256_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA256_HMAC, SHA256_HMAC_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA256-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA256_HMAC_GENERAL, SHA256_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA384, SHA384_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA384_HMAC, SHA384_HMAC_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA384-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA384_HMAC_GENERAL, SHA384_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA512, SHA512_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm 0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512-HMAC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA512_HMAC, SHA512_HMAC_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES},
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* SHA512-HMAC GENERAL */
734b6a94890be549309b21156f8ed6d4561cac51darrenm {SUN_CKM_SHA512_HMAC_GENERAL, SHA512_HMAC_GEN_MECH_INFO_TYPE,
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_HMAC_MIN_KEY_LEN, SHA2_HMAC_MAX_KEY_LEN,
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal CRYPTO_KEYSIZE_UNIT_IN_BYTES}
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic void sha2_provider_status(crypto_provider_handle_t, uint_t *);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_control_ops_t sha2_control_ops = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_provider_status
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_update(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_final(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_digest_ops_t sha2_digest_ops = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_digest_init,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_digest_update,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_digest_final,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_digest_atomic
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_mac_update(crypto_ctx_t *, crypto_data_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_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 *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t, crypto_req_handle_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 *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_mac_ops_t sha2_mac_ops = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_update,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_final,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_atomic,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_verify_atomic
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_create_ctx_template(crypto_provider_handle_t,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t *, crypto_req_handle_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int sha2_free_context(crypto_ctx_t *);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_ctx_ops_t sha2_ctx_ops = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_create_ctx_template,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_free_context
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_ops_t sha2_crypto_ops = {
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_control_ops,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_digest_ops,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_mac_ops,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino &sha2_ctx_ops,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino NULL,
735564919188238196dbd0d320770dda59b38369Anthony Scarpino NULL,
6ea3c0609e50782557505b88bb391b786bca32c9Garrett D'Amore NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_provider_info_t sha2_prov_info = {
735564919188238196dbd0d320770dda59b38369Anthony Scarpino CRYPTO_SPI_VERSION_4,
734b6a94890be549309b21156f8ed6d4561cac51darrenm "SHA2 Software Provider",
734b6a94890be549309b21156f8ed6d4561cac51darrenm CRYPTO_SW_PROVIDER,
734b6a94890be549309b21156f8ed6d4561cac51darrenm {&modlinkage},
734b6a94890be549309b21156f8ed6d4561cac51darrenm NULL,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_crypto_ops,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sizeof (sha2_mech_info_tab)/sizeof (crypto_mech_info_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mech_info_tab
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic crypto_kcf_provider_handle_t sha2_prov_handle = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmint
734b6a94890be549309b21156f8ed6d4561cac51darrenm_init()
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((ret = mod_install(&modlinkage)) != 0)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino (void) crypto_register_provider(&sha2_prov_info, &sha2_prov_handle);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (0);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmint
734b6a94890be549309b21156f8ed6d4561cac51darrenm_info(struct modinfo *modinfop)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (mod_info(&modlinkage, modinfop));
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider control entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic void
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_provider_status(crypto_provider_handle_t provider, uint_t *status)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm *status = CRYPTO_PROVIDER_READY;
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider digest entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize SHA2 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (sha2_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_kmflag(req));
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx->cc_provider_private == NULL)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_HOST_MEMORY);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_CTX(ctx)->sc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(mechanism->cm_type, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper SHA2 digest update function for uio data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update_uio(SHA2_CTX *sha2_ctx, crypto_data_t *data)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = data->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = data->cd_length;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t vec_idx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (data->cd_uio->uio_segflg != UIO_SYSSPACE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first iovec containing data to be
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset >= data->cd_uio->uio_iov[vec_idx].iov_len;
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= data->cd_uio->uio_iov[vec_idx++].iov_len)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == data->cd_uio->uio_iovcnt) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Now do the digesting on the iovecs.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
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 offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(sha2_ctx, (uint8_t *)data->cd_uio->
734b6a94890be549309b21156f8ed6d4561cac51darrenm uio_iov[vec_idx].iov_base + offset, cur_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm vec_idx++;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
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.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_final_uio(SHA2_CTX *sha2_ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ulong_t digest_len, uchar_t *digest_scratch)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = digest->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t vec_idx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (digest->cd_uio->uio_segflg != UIO_SYSSPACE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first iovec containing ptr to the digest to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * be returned.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm vec_idx < digest->cd_uio->uio_iovcnt;
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == digest->cd_uio->uio_iovcnt) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is
734b6a94890be549309b21156f8ed6d4561cac51darrenm * larger than the total size of the buffers
734b6a94890be549309b21156f8ed6d4561cac51darrenm * it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (offset + digest_len <=
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_uio->uio_iov[vec_idx].iov_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed SHA2 digest will fit in the current
734b6a94890be549309b21156f8ed6d4561cac51darrenm * iovec.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm (digest_len != SHA256_DIGEST_LENGTH)) ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers (digest_len != SHA512_DIGEST_LENGTH))) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest_scratch, sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest_scratch, (uchar_t *)digest->
734b6a94890be549309b21156f8ed6d4561cac51darrenm cd_uio->uio_iov[vec_idx].iov_base + offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((uchar_t *)digest->
734b6a94890be549309b21156f8ed6d4561cac51darrenm cd_uio->uio_iov[vec_idx].iov_base + offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digest_tmp[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t scratch_offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest_tmp, sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len =
734b6a94890be549309b21156f8ed6d4561cac51darrenm MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest_tmp + scratch_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_uio->uio_iov[vec_idx].iov_base + offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm vec_idx++;
734b6a94890be549309b21156f8ed6d4561cac51darrenm scratch_offset += cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Helper SHA2 digest update for mblk's.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update_mblk(SHA2_CTX *sha2_ctx, crypto_data_t *data)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = data->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = data->cd_length;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mblk_t *mp;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first mblk_t containing data to be digested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= MBLKL(mp), mp = mp->b_cont)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mp == NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Now do the digesting on the mblk chain.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (mp != NULL && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(MBLKL(mp) - offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(sha2_ctx, mp->b_rptr + offset, cur_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mp = mp->b_cont;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mp == NULL && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
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.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_final_mblk(SHA2_CTX *sha2_ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm ulong_t digest_len, uchar_t *digest_scratch)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = digest->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mblk_t *mp;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Jump to the first mblk_t that will be used to store the digest.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= MBLKL(mp), mp = mp->b_cont)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mp == NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (offset + digest_len <= MBLKL(mp)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The computed SHA2 digest will fit in the current mblk.
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA2Final() in-place.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (((sha2_ctx->algotype <= SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm (digest_len != SHA256_DIGEST_LENGTH)) ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm ((sha2_ctx->algotype > SHA256_HMAC_GEN_MECH_INFO_TYPE) &&
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers (digest_len != SHA512_DIGEST_LENGTH))) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest_scratch, sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(mp->b_rptr + offset, sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digest_tmp[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t scratch_offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest_tmp, sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (mp != NULL && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(MBLKL(mp) - offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest_tmp + scratch_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm mp->b_rptr + offset, cur_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mp = mp->b_cont;
734b6a94890be549309b21156f8ed6d4561cac51darrenm scratch_offset += cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mp == NULL && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_DATA_LEN_RANGE);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ASSERT(ctx->cc_provider_private != NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (PROV_SHA2_CTX(ctx)->sc_mech_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA384_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((digest->cd_length == 0) ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm (digest->cd_length < sha_digest_len)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_BUFFER_TOO_SMALL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA2 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (data->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data->cd_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret != CRYPTO_SUCCESS) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (digest->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest, sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest, sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret == CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ASSERT(ctx->cc_provider_private != NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA2 update on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (data->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data->cd_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ASSERT(ctx->cc_provider_private != NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (PROV_SHA2_CTX(ctx)->sc_mech_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA384_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((digest->cd_length == 0) ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm (digest->cd_length < sha_digest_len)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_BUFFER_TOO_SMALL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (digest->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_offset, &PROV_SHA2_CTX(ctx)->sc_sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest, sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&PROV_SHA2_CTX(ctx)->sc_sha2_ctx,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest, sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* all done, free context and return */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret == CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_digest_atomic(crypto_provider_handle_t provider,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_data_t *data, crypto_data_t *digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_CTX sha2_ctx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint32_t sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA inits.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(mechanism->cm_type, &sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (data->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&sha2_ctx, (uint8_t *)data->
734b6a94890be549309b21156f8ed6d4561cac51darrenm cd_raw.iov_base + data->cd_offset, data->cd_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&sha2_ctx, data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&sha2_ctx, data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do the SHA updates on the specified input data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret != CRYPTO_SUCCESS) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final, must be done separately since the digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * type can be different than the input data type.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (digest->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)digest->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_offset, &sha2_ctx);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&sha2_ctx, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&sha2_ctx, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len, NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret == CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider mac entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * SHA2 HMAC is: SHA2(key XOR opad, SHA2(key XOR ipad, text))
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Init:
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 *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Update:
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Each subsequent SHA2 HMAC update will result in an
734b6a94890be549309b21156f8ed6d4561cac51darrenm * update of the inner context with the specified data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Final:
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Initialize a SHA2-HMAC context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic void
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_init_ctx(sha2_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint64_t ipad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint64_t opad[SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t)];
734b6a94890be549309b21156f8ed6d4561cac51darrenm int i, block_size, blocks_per_int64;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Determine the block size */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx->hc_mech_type <= SHA256_HMAC_GEN_MECH_INFO_TYPE) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm block_size = SHA256_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm blocks_per_int64 = SHA256_HMAC_BLOCK_SIZE / sizeof (uint64_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm block_size = SHA512_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm blocks_per_int64 = SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm (void) bzero(ipad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenm (void) bzero(opad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenm (void) bcopy(keyval, ipad, length_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm (void) bcopy(keyval, opad, length_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* XOR key with ipad (0x36) and opad (0x5c) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (i = 0; i < blocks_per_int64; i ++) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ipad[i] ^= 0x3636363636363636;
734b6a94890be549309b21156f8ed6d4561cac51darrenm opad[i] ^= 0x5c5c5c5c5c5c5c5c;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform SHA2 on ipad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(ctx->hc_mech_type, &ctx->hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&ctx->hc_icontext, (uint8_t *)ipad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* perform SHA2 on opad */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Init(ctx->hc_mech_type, &ctx->hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&ctx->hc_ocontext, (uint8_t *)opad, block_size);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t sha_digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mechanism->cm_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (key->ck_format != CRYPTO_KEY_RAW)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = kmem_alloc(sizeof (sha2_hmac_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_kmflag(req));
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx->cc_provider_private == NULL)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_HOST_MEMORY);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna PROV_SHA2_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx_template != NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, PROV_SHA2_HMAC_CTX(ctx),
734b6a94890be549309b21156f8ed6d4561cac51darrenm sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, compute context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (keylen_in_bytes > sha_hmac_block_size) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digested_key[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &hmac_ctx->hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm key->ck_data, keylen_in_bytes, digested_key);
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(PROV_SHA2_HMAC_CTX(ctx),
734b6a94890be549309b21156f8ed6d4561cac51darrenm digested_key, sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(PROV_SHA2_HMAC_CTX(ctx),
734b6a94890be549309b21156f8ed6d4561cac51darrenm key->ck_data, keylen_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Get the mechanism parameters, if applicable.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type % 3 == 2) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_param == NULL ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm mechanism->cm_param_len != sizeof (ulong_t))
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_GET_DIGEST_LEN(mechanism,
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len > sha_digest_len)
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret != CRYPTO_SUCCESS) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_update(crypto_ctx_t *ctx, crypto_data_t *data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ASSERT(ctx->cc_provider_private != NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 update of the inner context using the specified
734b6a94890be549309b21156f8ed6d4561cac51darrenm * data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (data->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&PROV_SHA2_HMAC_CTX(ctx)->hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm (uint8_t *)data->cd_raw.iov_base + data->cd_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm data->cd_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext, data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext, data);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digest[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint32_t digest_len, sha_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm ASSERT(ctx->cc_provider_private != NULL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson /* Set the digest lengths to values appropriate to the mechanism */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (PROV_SHA2_HMAC_CTX(ctx)->hc_mech_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_MECH_INFO_TYPE:
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna sha_digest_len = digest_len = SHA384_DIGEST_LENGTH;
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len = PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len = PROV_SHA2_HMAC_CTX(ctx)->hc_digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_BUFFER_TOO_SMALL);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the inner context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &PROV_SHA2_HMAC_CTX(ctx)->hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the outer context, storing the computing
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mac->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (digest_len != sha_digest_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_offset, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(
734b6a94890be549309b21156f8ed6d4561cac51darrenm &PROV_SHA2_HMAC_CTX(ctx)->hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret == CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
ba5f469c0173c4d47f377c20b530f5be165d49dckrishna bzero(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define SHA2_MAC_UPDATE(data, ctx, ret) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (data->cd_format) { \
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW: \
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&(ctx).hc_icontext, \
734b6a94890be549309b21156f8ed6d4561cac51darrenm (uint8_t *)data->cd_raw.iov_base + \
734b6a94890be549309b21156f8ed6d4561cac51darrenm data->cd_offset, data->cd_length); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm break; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO: \
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_uio(&(ctx).hc_icontext, data); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm break; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK: \
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_update_mblk(&(ctx).hc_icontext, \
734b6a94890be549309b21156f8ed6d4561cac51darrenm data); \
734b6a94890be549309b21156f8ed6d4561cac51darrenm break; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm default: \
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD; \
734b6a94890be549309b21156f8ed6d4561cac51darrenm } \
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_mac_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{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digest[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_t sha2_hmac_ctx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mechanism->cm_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (key->ck_format != CRYPTO_KEY_RAW)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx_template != NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx.hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, initialize context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (keylen_in_bytes > sha_hmac_block_size) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_hmac_ctx.hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm key->ck_data, keylen_in_bytes, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(&sha2_hmac_ctx, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(&sha2_hmac_ctx, key->ck_data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm keylen_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* get the mechanism parameters, if applicable */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if ((mechanism->cm_type % 3) == 2) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_param == NULL ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm mechanism->cm_param_len != sizeof (ulong_t)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_GET_DIGEST_LEN(mechanism, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (digest_len > sha_digest_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do a SHA2 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_MAC_UPDATE(data, sha2_hmac_ctx, ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret != CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the inner context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &sha2_hmac_ctx.hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * HMAC-SHA384 needs special handling as the outer hash needs only 48
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * bytes of the inner hash value.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
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,
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA384_DIGEST_LENGTH);
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the outer context, storing the computed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mac->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (digest_len != sha_digest_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller requested a short digest. Digest
734b6a94890be549309b21156f8ed6d4561cac51darrenm * into a scratch buffer and return to
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the user only what was requested.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &sha2_hmac_ctx.hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_offset, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final((unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_offset, &sha2_hmac_ctx.hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_uio(&sha2_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = sha2_digest_final_mblk(&sha2_hmac_ctx.hc_ocontext, mac,
734b6a94890be549309b21156f8ed6d4561cac51darrenm digest_len, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret == CRYPTO_SUCCESS) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenmbail:
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(&sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
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{
734b6a94890be549309b21156f8ed6d4561cac51darrenm int ret = CRYPTO_SUCCESS;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digest[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_t sha2_hmac_ctx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint32_t sha_digest_len, digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mechanism->cm_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (key->ck_format != CRYPTO_KEY_RAW)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx_template != NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* reuse context template */
734b6a94890be549309b21156f8ed6d4561cac51darrenm bcopy(ctx_template, &sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
52aa4e90b7b198e065746fab6505afed43f0fe88Mark Powers sha2_hmac_ctx.hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* no context template, initialize context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (keylen_in_bytes > sha_hmac_block_size) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_hmac_ctx.hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm key->ck_data, keylen_in_bytes, digest);
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(&sha2_hmac_ctx, digest,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(&sha2_hmac_ctx, key->ck_data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm keylen_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* get the mechanism parameters, if applicable */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_type % 3 == 2) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mechanism->cm_param == NULL ||
734b6a94890be549309b21156f8ed6d4561cac51darrenm mechanism->cm_param_len != sizeof (ulong_t)) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_GET_DIGEST_LEN(mechanism, digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (digest_len > sha_digest_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_MECHANISM_PARAM_INVALID;
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mac->cd_length != digest_len) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_INVALID_MAC;
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do a SHA2 update of the inner context using the specified data */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2_MAC_UPDATE(data, sha2_hmac_ctx, ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ret != CRYPTO_SUCCESS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* the update failed, free context and bail */
734b6a94890be549309b21156f8ed6d4561cac51darrenm goto bail;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* do a SHA2 final on the inner context */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &sha2_hmac_ctx.hc_icontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do an SHA2 update on the outer context, feeding the inner
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest as data.
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd *
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * HMAC-SHA384 needs special handling as the outer hash needs only 48
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd * bytes of the inner hash value.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
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,
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd SHA384_DIGEST_LENGTH);
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd else
0358d3a672326bc66debe0a1c3a2372cd9f0f662danmcd SHA2Update(&sha2_hmac_ctx.hc_ocontext, digest, sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Do a SHA2 final on the outer context, storing the computed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * digest in the users buffer.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm SHA2Final(digest, &sha2_hmac_ctx.hc_ocontext);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Compare the computed digest against the expected digest passed
734b6a94890be549309b21156f8ed6d4561cac51darrenm * as argument.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mac->cd_format) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_RAW:
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_offset, digest_len) != 0)
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_INVALID_MAC;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_UIO: {
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = mac->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t vec_idx;
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t scratch_offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* we support only kernel buffer */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mac->cd_uio->uio_segflg != UIO_SYSSPACE)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* jump to the first iovec containing the expected digest */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (vec_idx = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset >= mac->cd_uio->uio_iov[vec_idx].iov_len &&
734b6a94890be549309b21156f8ed6d4561cac51darrenm vec_idx < mac->cd_uio->uio_iovcnt;
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= mac->cd_uio->uio_iov[vec_idx++].iov_len)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (vec_idx == mac->cd_uio->uio_iovcnt) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is
734b6a94890be549309b21156f8ed6d4561cac51darrenm * larger than the total size of the buffers
734b6a94890be549309b21156f8ed6d4561cac51darrenm * it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_DATA_LEN_RANGE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
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 offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (bcmp(digest + scratch_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_uio->uio_iov[vec_idx].iov_base + offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len) != 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_INVALID_MAC;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm vec_idx++;
734b6a94890be549309b21156f8ed6d4561cac51darrenm scratch_offset += cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm case CRYPTO_DATA_MBLK: {
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t offset = mac->cd_offset;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mblk_t *mp;
734b6a94890be549309b21156f8ed6d4561cac51darrenm off_t scratch_offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t length = digest_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm size_t cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* jump to the first mblk_t containing the expected digest */
734b6a94890be549309b21156f8ed6d4561cac51darrenm for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers offset -= MBLKL(mp), mp = mp->b_cont)
d2b323064c1e0b7131b3f784139d57c317e9b625mcpowers ;
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (mp == NULL) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * The caller specified an offset that is larger than
734b6a94890be549309b21156f8ed6d4561cac51darrenm * the total size of the buffers it provided.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_DATA_LEN_RANGE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm while (mp != NULL && length > 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm cur_len = MIN(MBLKL(mp) - offset, length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (bcmp(digest + scratch_offset,
734b6a94890be549309b21156f8ed6d4561cac51darrenm mp->b_rptr + offset, cur_len) != 0) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_INVALID_MAC;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm length -= cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm mp = mp->b_cont;
734b6a94890be549309b21156f8ed6d4561cac51darrenm scratch_offset += cur_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm offset = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm ret = CRYPTO_ARGUMENTS_BAD;
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenmbail:
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(&sha2_hmac_ctx, sizeof (sha2_hmac_ctx_t));
734b6a94890be549309b21156f8ed6d4561cac51darrenm mac->cd_length = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (ret);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * KCF software provider context management entry points.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* ARGSUSED */
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_create_ctx_template(crypto_provider_handle_t provider,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_mechanism_t *mechanism, crypto_key_t *key,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_req_handle_t req)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_t *sha2_hmac_ctx_tmpl;
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint32_t sha_digest_len, sha_hmac_block_size;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson * Set the digest length and block size to values appropriate to the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * mechanism
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm switch (mechanism->cm_type) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA256_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA256_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA256_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA384_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm case SHA512_HMAC_GEN_MECH_INFO_TYPE:
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len = SHA512_DIGEST_LENGTH;
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_hmac_block_size = SHA512_HMAC_BLOCK_SIZE;
734b6a94890be549309b21156f8ed6d4561cac51darrenm break;
734b6a94890be549309b21156f8ed6d4561cac51darrenm default:
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_MECHANISM_INVALID);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* Add support for key by attributes (RFE 4706552) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (key->ck_format != CRYPTO_KEY_RAW)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_ARGUMENTS_BAD);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Allocate and initialize SHA2 context.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_tmpl = kmem_alloc(sizeof (sha2_hmac_ctx_t),
734b6a94890be549309b21156f8ed6d4561cac51darrenm crypto_kmflag(req));
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (sha2_hmac_ctx_tmpl == NULL)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_HOST_MEMORY);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (keylen_in_bytes > sha_hmac_block_size) {
734b6a94890be549309b21156f8ed6d4561cac51darrenm uchar_t digested_key[SHA512_DIGEST_LENGTH];
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
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 */
734b6a94890be549309b21156f8ed6d4561cac51darrenm PROV_SHA2_DIGEST_KEY(mechanism->cm_type / 3,
734b6a94890be549309b21156f8ed6d4561cac51darrenm &sha2_hmac_ctx_tmpl->hc_icontext,
734b6a94890be549309b21156f8ed6d4561cac51darrenm key->ck_data, keylen_in_bytes, digested_key);
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(sha2_hmac_ctx_tmpl, digested_key,
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha_digest_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm } else {
734b6a94890be549309b21156f8ed6d4561cac51darrenm sha2_mac_init_ctx(sha2_hmac_ctx_tmpl, key->ck_data,
734b6a94890be549309b21156f8ed6d4561cac51darrenm keylen_in_bytes);
734b6a94890be549309b21156f8ed6d4561cac51darrenm }
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm *ctx_template = (crypto_spi_ctx_template_t)sha2_hmac_ctx_tmpl;
734b6a94890be549309b21156f8ed6d4561cac51darrenm *ctx_template_size = sizeof (sha2_hmac_ctx_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmstatic int
734b6a94890be549309b21156f8ed6d4561cac51darrenmsha2_free_context(crypto_ctx_t *ctx)
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm uint_t ctx_len;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (ctx->cc_provider_private == NULL)
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * We have to free either SHA2 or SHA2-HMAC contexts, which
734b6a94890be549309b21156f8ed6d4561cac51darrenm * have different lengths.
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Note: Below is dependent on the mechanism ordering.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm if (PROV_SHA2_CTX(ctx)->sc_mech_type % 3 == 0)
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx_len = sizeof (sha2_ctx_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm else
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx_len = sizeof (sha2_hmac_ctx_t);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm bzero(ctx->cc_provider_private, ctx_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm kmem_free(ctx->cc_provider_private, ctx_len);
734b6a94890be549309b21156f8ed6d4561cac51darrenm ctx->cc_provider_private = NULL;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm return (CRYPTO_SUCCESS);
734b6a94890be549309b21156f8ed6d4561cac51darrenm}