xref: /onnv-gate/usr/src/uts/common/sys/sha2.h (revision 6281:abaa1bb4fbe5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51694Sdarrenm  * Common Development and Distribution License (the "License").
61694Sdarrenm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6281Sda73024  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _SYS_SHA2_H
270Sstevel@tonic-gate #define	_SYS_SHA2_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>		/* for uint_* */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #ifdef	__cplusplus
340Sstevel@tonic-gate extern "C" {
350Sstevel@tonic-gate #endif
360Sstevel@tonic-gate 
371694Sdarrenm #define	SHA2_HMAC_MIN_KEY_LEN	8	/* SHA2-HMAC min key length in bits */
381694Sdarrenm #define	SHA2_HMAC_MAX_KEY_LEN	INT_MAX /* SHA2-HMAC max key length in bits */
391694Sdarrenm 
401694Sdarrenm #define	SHA256_DIGEST_LENGTH	32	/* SHA256 digest length in bytes */
411694Sdarrenm #define	SHA384_DIGEST_LENGTH	48	/* SHA384 digest length in bytes */
421694Sdarrenm #define	SHA512_DIGEST_LENGTH	64	/* SHA512 digest length in bytes */
431694Sdarrenm 
441694Sdarrenm #define	SHA256_HMAC_BLOCK_SIZE	64	/* SHA256-HMAC block size */
451694Sdarrenm #define	SHA512_HMAC_BLOCK_SIZE	128	/* SHA512-HMAC block size */
461694Sdarrenm 
470Sstevel@tonic-gate #define	SHA256			0
480Sstevel@tonic-gate #define	SHA256_HMAC		1
490Sstevel@tonic-gate #define	SHA256_HMAC_GEN		2
500Sstevel@tonic-gate #define	SHA384			3
510Sstevel@tonic-gate #define	SHA384_HMAC		4
520Sstevel@tonic-gate #define	SHA384_HMAC_GEN		5
530Sstevel@tonic-gate #define	SHA512			6
540Sstevel@tonic-gate #define	SHA512_HMAC		7
550Sstevel@tonic-gate #define	SHA512_HMAC_GEN		8
560Sstevel@tonic-gate 
571694Sdarrenm /*
581694Sdarrenm  * SHA2 context.
591694Sdarrenm  * The contents of this structure are a private interface between the
601694Sdarrenm  * Init/Update/Final calls of the functions defined below.
611694Sdarrenm  * Callers must never attempt to read or write any of the fields
62*6281Sda73024  * in this structure directly.
631694Sdarrenm  */
640Sstevel@tonic-gate typedef struct 	{
650Sstevel@tonic-gate 	uint32_t algotype;		/* Algorithm Type */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/* state (ABCDEFGH) */
680Sstevel@tonic-gate 	union {
690Sstevel@tonic-gate 		uint32_t s32[8];	/* for SHA256 */
700Sstevel@tonic-gate 		uint64_t s64[8];	/* for SHA384/512 */
710Sstevel@tonic-gate 	} state;
720Sstevel@tonic-gate 	/* number of bits */
730Sstevel@tonic-gate 	union {
740Sstevel@tonic-gate 		uint32_t c32[2];	/* for SHA256 , modulo 2^64 */
750Sstevel@tonic-gate 		uint64_t c64[2];	/* for SHA384/512, modulo 2^128 */
760Sstevel@tonic-gate 	} count;
770Sstevel@tonic-gate 	union {
780Sstevel@tonic-gate 		uint8_t		buf8[128];	/* undigested input */
790Sstevel@tonic-gate 		uint32_t	buf32[32];	/* realigned input */
800Sstevel@tonic-gate 		uint64_t	buf64[16];	/* realigned input */
810Sstevel@tonic-gate 	} buf_un;
820Sstevel@tonic-gate } SHA2_CTX;
830Sstevel@tonic-gate 
841694Sdarrenm typedef SHA2_CTX SHA256_CTX;
851694Sdarrenm typedef SHA2_CTX SHA384_CTX;
861694Sdarrenm typedef SHA2_CTX SHA512_CTX;
871694Sdarrenm 
880Sstevel@tonic-gate extern void SHA2Init(uint64_t mech, SHA2_CTX *);
890Sstevel@tonic-gate 
901694Sdarrenm extern void SHA2Update(SHA2_CTX *, const void *, size_t);
911694Sdarrenm 
921694Sdarrenm extern void SHA2Final(void *, SHA2_CTX *);
931694Sdarrenm 
941694Sdarrenm extern void SHA256Init(SHA256_CTX *);
951694Sdarrenm 
961694Sdarrenm extern void SHA256Update(SHA256_CTX *, const void *, size_t);
971694Sdarrenm 
981694Sdarrenm extern void SHA256Final(void *, SHA256_CTX *);
991694Sdarrenm 
1001694Sdarrenm extern void SHA384Init(SHA384_CTX *);
1011694Sdarrenm 
1021694Sdarrenm extern void SHA384Update(SHA384_CTX *, const void *, size_t);
1031694Sdarrenm 
1041694Sdarrenm extern void SHA384Final(void *, SHA384_CTX *);
1051694Sdarrenm 
1061694Sdarrenm extern void SHA512Init(SHA512_CTX *);
1071694Sdarrenm 
1081694Sdarrenm extern void SHA512Update(SHA512_CTX *, const void *, size_t);
1091694Sdarrenm 
1101694Sdarrenm extern void SHA512Final(void *, SHA512_CTX *);
1110Sstevel@tonic-gate 
1121694Sdarrenm #ifdef _SHA2_IMPL
1131694Sdarrenm /*
1141694Sdarrenm  * The following types/functions are all private to the implementation
1151694Sdarrenm  * of the SHA2 functions and must not be used by consumers of the interface
1161694Sdarrenm  */
1171694Sdarrenm 
1181694Sdarrenm /*
1191694Sdarrenm  * List of support mechanisms in this module.
1201694Sdarrenm  *
1211694Sdarrenm  * It is important to note that in the module, division or modulus calculations
1221694Sdarrenm  * are used on the enumerated type to determine which mechanism is being used;
1231694Sdarrenm  * therefore, changing the order or additional mechanisms should be done
1241694Sdarrenm  * carefully
1251694Sdarrenm  */
1261694Sdarrenm typedef enum sha2_mech_type {
1271694Sdarrenm 	SHA256_MECH_INFO_TYPE,		/* SUN_CKM_SHA256 */
1281694Sdarrenm 	SHA256_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC */
1291694Sdarrenm 	SHA256_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC_GENERAL */
1301694Sdarrenm 	SHA384_MECH_INFO_TYPE,		/* SUN_CKM_SHA384 */
1311694Sdarrenm 	SHA384_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC */
1321694Sdarrenm 	SHA384_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC_GENERAL */
1331694Sdarrenm 	SHA512_MECH_INFO_TYPE,		/* SUN_CKM_SHA512 */
1341694Sdarrenm 	SHA512_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC */
1351694Sdarrenm 	SHA512_HMAC_GEN_MECH_INFO_TYPE	/* SUN_CKM_SHA512_HMAC_GENERAL */
1361694Sdarrenm } sha2_mech_type_t;
1371694Sdarrenm 
1381694Sdarrenm #endif /* _SHA2_IMPL */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #ifdef	__cplusplus
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate #endif
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #endif /* _SYS_SHA2_H */
145