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