xref: /titanic_51/usr/src/lib/pkcs11/pkcs11_softtoken/common/softMAC.h (revision f66d273d14eede3a1bb803a39414588d8f143a98)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*f66d273dSizick  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _SOFTMAC_H
287c478bd9Sstevel@tonic-gate #define	_SOFTMAC_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef __cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/md5.h>
377c478bd9Sstevel@tonic-gate #include <sys/sha1.h>
38*f66d273dSizick #include <sys/sha2.h>
397c478bd9Sstevel@tonic-gate #include <security/pkcs11t.h>
407c478bd9Sstevel@tonic-gate #include "softSession.h"
417c478bd9Sstevel@tonic-gate #include "softObject.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	MD5_HASH_SIZE		16	/* MD5 digest length in bytes */
447c478bd9Sstevel@tonic-gate #define	SHA1_HASH_SIZE		20	/* SHA_1 digest length in bytes */
457c478bd9Sstevel@tonic-gate #define	MD5_HMAC_BLOCK_SIZE	64    	/* MD5 block size */
467c478bd9Sstevel@tonic-gate #define	MD5_HMAC_INTS_PER_BLOCK (MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t))
477c478bd9Sstevel@tonic-gate #define	SHA1_HMAC_BLOCK_SIZE	64	/* SHA1-HMAC block size */
487c478bd9Sstevel@tonic-gate #define	SHA1_HMAC_INTS_PER_BLOCK	(SHA1_HMAC_BLOCK_SIZE/sizeof (uint32_t))
49*f66d273dSizick #define	SHA256_HMAC_INTS_PER_BLOCK	\
50*f66d273dSizick 	(SHA256_HMAC_BLOCK_SIZE/sizeof (uint64_t))
51*f66d273dSizick #define	SHA512_HMAC_INTS_PER_BLOCK	\
52*f66d273dSizick 	(SHA512_HMAC_BLOCK_SIZE/sizeof (uint64_t))
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	MD5_SSL_PAD_SIZE	48	/* MD5 SSL pad length in bytes */
567c478bd9Sstevel@tonic-gate /* 48 (MD5 SSL pad length in bytes) + 16 (key length in bytes) = 64 */
577c478bd9Sstevel@tonic-gate #define	MD5_SSL_PAD_AND_KEY_SIZE	64
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	SHA1_SSL_PAD_SIZE	40 /* SHA1 SSL pad length in bytes */
607c478bd9Sstevel@tonic-gate /* 40 (SHA1 SSL pad length in bytes) + 20 (key length in bytes) = 104 */
617c478bd9Sstevel@tonic-gate #define	SHA1_SSL_PAD_AND_KEY_SIZE	60
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate typedef struct md5_hc_ctx {
677c478bd9Sstevel@tonic-gate 	MD5_CTX		hc_icontext;    /* inner MD5 context */
687c478bd9Sstevel@tonic-gate 	MD5_CTX		hc_ocontext;    /* outer MD5 context */
697c478bd9Sstevel@tonic-gate } md5_hc_ctx_t;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Context for SHA1-HMAC and SHA1-HMAC-GENERAL mechanisms.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate typedef struct sha1_hc_ctx {
757c478bd9Sstevel@tonic-gate 	SHA1_CTX	hc_icontext;    /* inner SHA1 context */
767c478bd9Sstevel@tonic-gate 	SHA1_CTX	hc_ocontext;    /* outer SHA1 context */
777c478bd9Sstevel@tonic-gate } sha1_hc_ctx_t;
787c478bd9Sstevel@tonic-gate 
79*f66d273dSizick typedef struct sha2_hc_ctx {
80*f66d273dSizick 	SHA2_CTX	hc_icontext;    /* inner SHA2 context */
81*f66d273dSizick 	SHA2_CTX	hc_ocontext;    /* outer SHA2 context */
82*f66d273dSizick } sha2_hc_ctx_t;
83*f66d273dSizick 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Generic Context struct for HMAC.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate typedef struct soft_hmac_ctx {
887c478bd9Sstevel@tonic-gate 	size_t	hmac_len;    	/* digest len in bytes */
897c478bd9Sstevel@tonic-gate 	union {
907c478bd9Sstevel@tonic-gate 		md5_hc_ctx_t	md5_ctx;
917c478bd9Sstevel@tonic-gate 		sha1_hc_ctx_t	sha1_ctx;
92*f66d273dSizick 		sha2_hc_ctx_t	sha2_ctx;
937c478bd9Sstevel@tonic-gate 	} hc_ctx_u;
947c478bd9Sstevel@tonic-gate } soft_hmac_ctx_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 
97*f66d273dSizick /* Generic MAC envelop macros. Substitute HASH with MD5, SHA1, & SHA2 mechs */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	SOFT_MAC_INIT_CTX(HASH, mac_ctx, ipad, opad, len)		\
1007c478bd9Sstevel@tonic-gate 	/* Perform HASH on ipad */					\
1017c478bd9Sstevel@tonic-gate 	HASH##Init(&((mac_ctx)->hc_icontext));				\
1027c478bd9Sstevel@tonic-gate 	HASH##Update(&((mac_ctx)->hc_icontext), ipad, len);		\
1037c478bd9Sstevel@tonic-gate 	/* Perform HASH on opad */					\
1047c478bd9Sstevel@tonic-gate 	HASH##Init(&((mac_ctx)->hc_ocontext));				\
1057c478bd9Sstevel@tonic-gate 	HASH##Update(&((mac_ctx)->hc_ocontext), opad, len);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	SOFT_MAC_UPDATE(HASH, mac_ctx, pPart, PartLen)			\
1087c478bd9Sstevel@tonic-gate 	HASH##Update(&((mac_ctx)->hc_icontext), pPart, PartLen);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	SOFT_MAC_FINAL(HASH, mac_ctx, mac)				\
1117c478bd9Sstevel@tonic-gate 	HASH##Final((mac), &((mac_ctx)->hc_icontext));			\
1127c478bd9Sstevel@tonic-gate 	HASH##Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_HASH_SIZE);\
1137c478bd9Sstevel@tonic-gate 	HASH##Final((mac), &((mac_ctx)->hc_ocontext));
1147c478bd9Sstevel@tonic-gate 
115*f66d273dSizick #define	SOFT_MAC_FINAL_2(HASH, mac_ctx, mac)				\
116*f66d273dSizick 	SHA2Final((mac), &((mac_ctx)->hc_icontext));			\
117*f66d273dSizick 	SHA2Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_DIGEST_LENGTH); \
118*f66d273dSizick 	SHA2Final((mac), &((mac_ctx)->hc_ocontext));
119*f66d273dSizick 
120*f66d273dSizick #define	CKM_TO_SHA2(ckm_value)	\
121*f66d273dSizick 	(ckm_value % 0x10) + (((ckm_value - 0x250) / 0x10) * 3)
122*f66d273dSizick 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Function Prototypes.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_init_common(soft_session_t *, CK_MECHANISM_PTR,
1277c478bd9Sstevel@tonic-gate 	soft_object_t *, boolean_t);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate CK_RV mac_init_ctx(soft_session_t *session_p, soft_object_t *,
1307c478bd9Sstevel@tonic-gate 	soft_hmac_ctx_t *, CK_MECHANISM_TYPE);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_common(soft_session_t *, CK_BYTE_PTR,
1337c478bd9Sstevel@tonic-gate 	CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, boolean_t);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_update(soft_session_t *, CK_BYTE_PTR,
1367c478bd9Sstevel@tonic-gate 	CK_ULONG, boolean_t);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate void md5_hmac_ctx_init(md5_hc_ctx_t *, uint32_t *, uint32_t *);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate void sha1_hmac_ctx_init(sha1_hc_ctx_t *, uint32_t *, uint32_t *);
1417c478bd9Sstevel@tonic-gate 
142*f66d273dSizick void sha2_hmac_ctx_init(uint_t mech, sha2_hc_ctx_t *, uint64_t *, uint64_t *,
143*f66d273dSizick     uint_t, uint_t);
144*f66d273dSizick 
1457c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate #endif
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #endif /* _SOFTMAC_H */
150