xref: /titanic_52/usr/src/lib/pkcs11/pkcs11_softtoken/common/softMAC.h (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SOFTMAC_H
28 #define	_SOFTMAC_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/md5.h>
37 #include <sys/sha1.h>
38 #include <security/pkcs11t.h>
39 #include "softSession.h"
40 #include "softObject.h"
41 
42 #define	MD5_HASH_SIZE		16	/* MD5 digest length in bytes */
43 #define	SHA1_HASH_SIZE		20	/* SHA_1 digest length in bytes */
44 #define	MD5_HMAC_BLOCK_SIZE	64    	/* MD5 block size */
45 #define	MD5_HMAC_INTS_PER_BLOCK (MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t))
46 #define	SHA1_HMAC_BLOCK_SIZE	64	/* SHA1-HMAC block size */
47 #define	SHA1_HMAC_INTS_PER_BLOCK	(SHA1_HMAC_BLOCK_SIZE/sizeof (uint32_t))
48 
49 
50 #define	MD5_SSL_PAD_SIZE	48	/* MD5 SSL pad length in bytes */
51 /* 48 (MD5 SSL pad length in bytes) + 16 (key length in bytes) = 64 */
52 #define	MD5_SSL_PAD_AND_KEY_SIZE	64
53 
54 #define	SHA1_SSL_PAD_SIZE	40 /* SHA1 SSL pad length in bytes */
55 /* 40 (SHA1 SSL pad length in bytes) + 20 (key length in bytes) = 104 */
56 #define	SHA1_SSL_PAD_AND_KEY_SIZE	60
57 
58 /*
59  * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
60  */
61 typedef struct md5_hc_ctx {
62 	MD5_CTX		hc_icontext;    /* inner MD5 context */
63 	MD5_CTX		hc_ocontext;    /* outer MD5 context */
64 } md5_hc_ctx_t;
65 
66 /*
67  * Context for SHA1-HMAC and SHA1-HMAC-GENERAL mechanisms.
68  */
69 typedef struct sha1_hc_ctx {
70 	SHA1_CTX	hc_icontext;    /* inner SHA1 context */
71 	SHA1_CTX	hc_ocontext;    /* outer SHA1 context */
72 } sha1_hc_ctx_t;
73 
74 /*
75  * Generic Context struct for HMAC.
76  */
77 typedef struct soft_hmac_ctx {
78 	size_t	hmac_len;    	/* digest len in bytes */
79 	union {
80 		md5_hc_ctx_t	md5_ctx;
81 		sha1_hc_ctx_t	sha1_ctx;
82 	} hc_ctx_u;
83 } soft_hmac_ctx_t;
84 
85 
86 /* Generic MAC envelop macros. Substitute HASH with MD5 or SHA1 */
87 
88 #define	SOFT_MAC_INIT_CTX(HASH, mac_ctx, ipad, opad, len)		\
89 	/* Perform HASH on ipad */					\
90 	HASH##Init(&((mac_ctx)->hc_icontext));				\
91 	HASH##Update(&((mac_ctx)->hc_icontext), ipad, len);		\
92 	/* Perform HASH on opad */					\
93 	HASH##Init(&((mac_ctx)->hc_ocontext));				\
94 	HASH##Update(&((mac_ctx)->hc_ocontext), opad, len);
95 
96 #define	SOFT_MAC_UPDATE(HASH, mac_ctx, pPart, PartLen)			\
97 	HASH##Update(&((mac_ctx)->hc_icontext), pPart, PartLen);
98 
99 
100 #define	SOFT_MAC_FINAL(HASH, mac_ctx, mac)				\
101 	HASH##Final((mac), &((mac_ctx)->hc_icontext));			\
102 	HASH##Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_HASH_SIZE);\
103 	HASH##Final((mac), &((mac_ctx)->hc_ocontext));
104 
105 /*
106  * Function Prototypes.
107  */
108 CK_RV soft_hmac_sign_verify_init_common(soft_session_t *, CK_MECHANISM_PTR,
109 	soft_object_t *, boolean_t);
110 
111 CK_RV mac_init_ctx(soft_session_t *session_p, soft_object_t *,
112 	soft_hmac_ctx_t *, CK_MECHANISM_TYPE);
113 
114 CK_RV soft_hmac_sign_verify_common(soft_session_t *, CK_BYTE_PTR,
115 	CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, boolean_t);
116 
117 CK_RV soft_hmac_sign_verify_update(soft_session_t *, CK_BYTE_PTR,
118 	CK_ULONG, boolean_t);
119 
120 void md5_hmac_ctx_init(md5_hc_ctx_t *, uint32_t *, uint32_t *);
121 
122 void sha1_hmac_ctx_init(sha1_hc_ctx_t *, uint32_t *, uint32_t *);
123 
124 #ifdef	__cplusplus
125 }
126 #endif
127 
128 #endif /* _SOFTMAC_H */
129