2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#ifndef _SOFTMAC_H
2N/A#define _SOFTMAC_H
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/A#include <sys/md5.h>
2N/A#include <sys/sha1.h>
2N/A#include <sys/sha2.h>
2N/A#include <security/pkcs11t.h>
2N/A#include "softSession.h"
2N/A#include "softObject.h"
2N/A
2N/A#define MD5_HMAC_INTS_PER_BLOCK \
2N/A (MD5_HMAC_BLOCK_SIZE / sizeof (uint32_t))
2N/A#define SHA1_HMAC_INTS_PER_BLOCK \
2N/A (SHA1_HMAC_BLOCK_SIZE / sizeof (uint32_t))
2N/A#define SHA256_HMAC_INTS_PER_BLOCK \
2N/A (SHA256_HMAC_BLOCK_SIZE / sizeof (uint64_t))
2N/A#define SHA512_HMAC_INTS_PER_BLOCK \
2N/A (SHA512_HMAC_BLOCK_SIZE / sizeof (uint64_t))
2N/A
2N/A
2N/A#define MD5_SSL_PAD_SIZE 48 /* MD5 SSL pad length in bytes */
2N/A/* 48 (MD5 SSL pad length in bytes) + 16 (key length in bytes) = 64 */
2N/A#define MD5_SSL_PAD_AND_KEY_SIZE 64
2N/A
2N/A#define SHA1_SSL_PAD_SIZE 40 /* SHA1 SSL pad length in bytes */
2N/A/* 40 (SHA1 SSL pad length in bytes) + 20 (key length in bytes) = 104 */
2N/A#define SHA1_SSL_PAD_AND_KEY_SIZE 60
2N/A
2N/A/*
2N/A * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
2N/A */
2N/Atypedef struct md5_hc_ctx {
2N/A MD5_CTX hc_icontext; /* inner MD5 context */
2N/A MD5_CTX hc_ocontext; /* outer MD5 context */
2N/A} md5_hc_ctx_t;
2N/A
2N/A/*
2N/A * Context for SHA1-HMAC and SHA1-HMAC-GENERAL mechanisms.
2N/A */
2N/Atypedef struct sha1_hc_ctx {
2N/A SHA1_CTX hc_icontext; /* inner SHA1 context */
2N/A SHA1_CTX hc_ocontext; /* outer SHA1 context */
2N/A} sha1_hc_ctx_t;
2N/A
2N/Atypedef struct sha2_hc_ctx {
2N/A SHA2_CTX hc_icontext; /* inner SHA2 context */
2N/A SHA2_CTX hc_ocontext; /* outer SHA2 context */
2N/A} sha2_hc_ctx_t;
2N/A
2N/A/*
2N/A * Generic Context struct for HMAC.
2N/A */
2N/Atypedef struct soft_hmac_ctx {
2N/A size_t hmac_len; /* digest len in bytes */
2N/A union {
2N/A md5_hc_ctx_t md5_ctx;
2N/A sha1_hc_ctx_t sha1_ctx;
2N/A sha2_hc_ctx_t sha2_ctx;
2N/A } hc_ctx_u;
2N/A} soft_hmac_ctx_t;
2N/A
2N/A
2N/A/* Generic MAC envelop macros. Substitute HASH with MD5, SHA1, & SHA2 mechs */
2N/A
2N/A#define SOFT_MAC_INIT_CTX(HASH, mac_ctx, ipad, opad, len) \
2N/A /* Perform HASH on ipad */ \
2N/A HASH##Init(&((mac_ctx)->hc_icontext)); \
2N/A HASH##Update(&((mac_ctx)->hc_icontext), (ipad), (len)); \
2N/A /* Perform HASH on opad */ \
2N/A HASH##Init(&((mac_ctx)->hc_ocontext)); \
2N/A HASH##Update(&((mac_ctx)->hc_ocontext), (opad), (len));
2N/A
2N/A#define SOFT_MAC_UPDATE(HASH, mac_ctx, pPart, PartLen) \
2N/A HASH##Update(&((mac_ctx)->hc_icontext), (pPart), (PartLen));
2N/A
2N/A#define SOFT_MAC_FINAL(HASH, mac_ctx, mac) \
2N/A HASH##Final((mac), &((mac_ctx)->hc_icontext)); \
2N/A HASH##Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_DIGEST_LENGTH);\
2N/A HASH##Final((mac), &((mac_ctx)->hc_ocontext));
2N/A
2N/A#define CKM_TO_SHA2(ckm_value) \
2N/A ((((ckm_value) % 0x10) < 5) \
2N/A ? ((ckm_value) % 0x10) + ((((ckm_value) - CKM_SHA256) / 0x10) * 3) \
2N/A : ((ckm_value) - CKM_SHA224 + SHA224))
2N/A
2N/A/*
2N/A * Function Prototypes.
2N/A */
2N/ACK_RV soft_hmac_sign_verify_init_common(soft_session_t *session_p,
2N/A CK_MECHANISM_PTR pMechanism, soft_object_t *key_p, boolean_t sign_op);
2N/A
2N/ACK_RV mac_init_ctx(soft_session_t *session_p, soft_object_t *key_p,
2N/A soft_hmac_ctx_t *ctx, CK_MECHANISM_TYPE mech);
2N/A
2N/ACK_RV soft_hmac_sign_verify_common(soft_session_t *session_p,
2N/A CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSigned,
2N/A CK_ULONG_PTR pulSignedLen, boolean_t sign_op);
2N/A
2N/ACK_RV soft_hmac_sign_verify_update(soft_session_t *session_p,
2N/A CK_BYTE_PTR pPart, CK_ULONG ulPartLen, boolean_t sign_op);
2N/A
2N/Avoid md5_hmac_ctx_init(md5_hc_ctx_t *md5_hmac_ctx, uint32_t *ipad,
2N/A uint32_t *opad);
2N/A
2N/Avoid sha1_hmac_ctx_init(sha1_hc_ctx_t *sha1_hmac_ctx, uint32_t *ipad,
2N/A uint32_t *opad);
2N/A
2N/Avoid sha2_hmac_ctx_init(uint_t mech, sha2_hc_ctx_t *ctx, uint64_t *ipad,
2N/A uint64_t *opad, uint_t blocks_per_int64, uint_t block_size);
2N/A
2N/A#ifdef __cplusplus
2N/A}
2N/A#endif
2N/A
2N/A#endif /* _SOFTMAC_H */