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 #ifdef __cplusplus 317c478bd9Sstevel@tonic-gate extern "C" { 327c478bd9Sstevel@tonic-gate #endif 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <sys/md5.h> 357c478bd9Sstevel@tonic-gate #include <sys/sha1.h> 36*f66d273dSizick #include <sys/sha2.h> 377c478bd9Sstevel@tonic-gate #include <security/pkcs11t.h> 387c478bd9Sstevel@tonic-gate #include "softSession.h" 397c478bd9Sstevel@tonic-gate #include "softObject.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #define MD5_HASH_SIZE 16 /* MD5 digest length in bytes */ 427c478bd9Sstevel@tonic-gate #define SHA1_HASH_SIZE 20 /* SHA_1 digest length in bytes */ 437c478bd9Sstevel@tonic-gate #define MD5_HMAC_BLOCK_SIZE 64 /* MD5 block size */ 447c478bd9Sstevel@tonic-gate #define MD5_HMAC_INTS_PER_BLOCK (MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t)) 457c478bd9Sstevel@tonic-gate #define SHA1_HMAC_BLOCK_SIZE 64 /* SHA1-HMAC block size */ 467c478bd9Sstevel@tonic-gate #define SHA1_HMAC_INTS_PER_BLOCK (SHA1_HMAC_BLOCK_SIZE/sizeof (uint32_t)) 47*f66d273dSizick #define SHA256_HMAC_INTS_PER_BLOCK \ 48*f66d273dSizick (SHA256_HMAC_BLOCK_SIZE/sizeof (uint64_t)) 49*f66d273dSizick #define SHA512_HMAC_INTS_PER_BLOCK \ 50*f66d273dSizick (SHA512_HMAC_BLOCK_SIZE/sizeof (uint64_t)) 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #define MD5_SSL_PAD_SIZE 48 /* MD5 SSL pad length in bytes */ 547c478bd9Sstevel@tonic-gate /* 48 (MD5 SSL pad length in bytes) + 16 (key length in bytes) = 64 */ 557c478bd9Sstevel@tonic-gate #define MD5_SSL_PAD_AND_KEY_SIZE 64 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define SHA1_SSL_PAD_SIZE 40 /* SHA1 SSL pad length in bytes */ 587c478bd9Sstevel@tonic-gate /* 40 (SHA1 SSL pad length in bytes) + 20 (key length in bytes) = 104 */ 597c478bd9Sstevel@tonic-gate #define SHA1_SSL_PAD_AND_KEY_SIZE 60 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms. 637c478bd9Sstevel@tonic-gate */ 647c478bd9Sstevel@tonic-gate typedef struct md5_hc_ctx { 657c478bd9Sstevel@tonic-gate MD5_CTX hc_icontext; /* inner MD5 context */ 667c478bd9Sstevel@tonic-gate MD5_CTX hc_ocontext; /* outer MD5 context */ 677c478bd9Sstevel@tonic-gate } md5_hc_ctx_t; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Context for SHA1-HMAC and SHA1-HMAC-GENERAL mechanisms. 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate typedef struct sha1_hc_ctx { 737c478bd9Sstevel@tonic-gate SHA1_CTX hc_icontext; /* inner SHA1 context */ 747c478bd9Sstevel@tonic-gate SHA1_CTX hc_ocontext; /* outer SHA1 context */ 757c478bd9Sstevel@tonic-gate } sha1_hc_ctx_t; 767c478bd9Sstevel@tonic-gate 77*f66d273dSizick typedef struct sha2_hc_ctx { 78*f66d273dSizick SHA2_CTX hc_icontext; /* inner SHA2 context */ 79*f66d273dSizick SHA2_CTX hc_ocontext; /* outer SHA2 context */ 80*f66d273dSizick } sha2_hc_ctx_t; 81*f66d273dSizick 827c478bd9Sstevel@tonic-gate /* 837c478bd9Sstevel@tonic-gate * Generic Context struct for HMAC. 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate typedef struct soft_hmac_ctx { 867c478bd9Sstevel@tonic-gate size_t hmac_len; /* digest len in bytes */ 877c478bd9Sstevel@tonic-gate union { 887c478bd9Sstevel@tonic-gate md5_hc_ctx_t md5_ctx; 897c478bd9Sstevel@tonic-gate sha1_hc_ctx_t sha1_ctx; 90*f66d273dSizick sha2_hc_ctx_t sha2_ctx; 917c478bd9Sstevel@tonic-gate } hc_ctx_u; 927c478bd9Sstevel@tonic-gate } soft_hmac_ctx_t; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate 95*f66d273dSizick /* Generic MAC envelop macros. Substitute HASH with MD5, SHA1, & SHA2 mechs */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate #define SOFT_MAC_INIT_CTX(HASH, mac_ctx, ipad, opad, len) \ 987c478bd9Sstevel@tonic-gate /* Perform HASH on ipad */ \ 997c478bd9Sstevel@tonic-gate HASH##Init(&((mac_ctx)->hc_icontext)); \ 1007c478bd9Sstevel@tonic-gate HASH##Update(&((mac_ctx)->hc_icontext), ipad, len); \ 1017c478bd9Sstevel@tonic-gate /* Perform HASH on opad */ \ 1027c478bd9Sstevel@tonic-gate HASH##Init(&((mac_ctx)->hc_ocontext)); \ 1037c478bd9Sstevel@tonic-gate HASH##Update(&((mac_ctx)->hc_ocontext), opad, len); 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate #define SOFT_MAC_UPDATE(HASH, mac_ctx, pPart, PartLen) \ 1067c478bd9Sstevel@tonic-gate HASH##Update(&((mac_ctx)->hc_icontext), pPart, PartLen); 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #define SOFT_MAC_FINAL(HASH, mac_ctx, mac) \ 1097c478bd9Sstevel@tonic-gate HASH##Final((mac), &((mac_ctx)->hc_icontext)); \ 1107c478bd9Sstevel@tonic-gate HASH##Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_HASH_SIZE);\ 1117c478bd9Sstevel@tonic-gate HASH##Final((mac), &((mac_ctx)->hc_ocontext)); 1127c478bd9Sstevel@tonic-gate 113*f66d273dSizick #define SOFT_MAC_FINAL_2(HASH, mac_ctx, mac) \ 114*f66d273dSizick SHA2Final((mac), &((mac_ctx)->hc_icontext)); \ 115*f66d273dSizick SHA2Update(&((mac_ctx)->hc_ocontext), (mac), HASH##_DIGEST_LENGTH); \ 116*f66d273dSizick SHA2Final((mac), &((mac_ctx)->hc_ocontext)); 117*f66d273dSizick 118*f66d273dSizick #define CKM_TO_SHA2(ckm_value) \ 119*f66d273dSizick (ckm_value % 0x10) + (((ckm_value - 0x250) / 0x10) * 3) 120*f66d273dSizick 1217c478bd9Sstevel@tonic-gate /* 1227c478bd9Sstevel@tonic-gate * Function Prototypes. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_init_common(soft_session_t *, CK_MECHANISM_PTR, 1257c478bd9Sstevel@tonic-gate soft_object_t *, boolean_t); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate CK_RV mac_init_ctx(soft_session_t *session_p, soft_object_t *, 1287c478bd9Sstevel@tonic-gate soft_hmac_ctx_t *, CK_MECHANISM_TYPE); 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_common(soft_session_t *, CK_BYTE_PTR, 1317c478bd9Sstevel@tonic-gate CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, boolean_t); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate CK_RV soft_hmac_sign_verify_update(soft_session_t *, CK_BYTE_PTR, 1347c478bd9Sstevel@tonic-gate CK_ULONG, boolean_t); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate void md5_hmac_ctx_init(md5_hc_ctx_t *, uint32_t *, uint32_t *); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate void sha1_hmac_ctx_init(sha1_hc_ctx_t *, uint32_t *, uint32_t *); 1397c478bd9Sstevel@tonic-gate 140*f66d273dSizick void sha2_hmac_ctx_init(uint_t mech, sha2_hc_ctx_t *, uint64_t *, uint64_t *, 141*f66d273dSizick uint_t, uint_t); 142*f66d273dSizick 1437c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate #endif 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate #endif /* _SOFTMAC_H */ 148