xref: /illumos-gate/usr/src/uts/common/sys/sha2.h (revision 45818ee1)
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
5734b6a94Sdarrenm  * Common Development and Distribution License (the "License").
6734b6a94Sdarrenm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225b675b31SVladimir Kotal  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*45818ee1SMatthew Ahrens /* Copyright 2013 Saso Kiselkov.  All rights reserved. */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _SYS_SHA2_H
287c478bd9Sstevel@tonic-gate #define	_SYS_SHA2_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>		/* for uint_* */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
365b675b31SVladimir Kotal #define	SHA2_HMAC_MIN_KEY_LEN	1	/* SHA2-HMAC min key length in bytes */
375b675b31SVladimir Kotal #define	SHA2_HMAC_MAX_KEY_LEN	INT_MAX	/* SHA2-HMAC max key length in bytes */
38f66d273dSizick 
39f66d273dSizick #define	SHA256_DIGEST_LENGTH	32	/* SHA256 digest length in bytes */
40f66d273dSizick #define	SHA384_DIGEST_LENGTH	48	/* SHA384 digest length in bytes */
41f66d273dSizick #define	SHA512_DIGEST_LENGTH	64	/* SHA512 digest length in bytes */
42f66d273dSizick 
43*45818ee1SMatthew Ahrens /* Truncated versions of SHA-512 according to FIPS-180-4, section 5.3.6 */
44*45818ee1SMatthew Ahrens #define	SHA512_224_DIGEST_LENGTH	28	/* SHA512/224 digest length */
45*45818ee1SMatthew Ahrens #define	SHA512_256_DIGEST_LENGTH	32	/* SHA512/256 digest length */
46*45818ee1SMatthew Ahrens 
47f66d273dSizick #define	SHA256_HMAC_BLOCK_SIZE	64	/* SHA256-HMAC block size */
48f66d273dSizick #define	SHA512_HMAC_BLOCK_SIZE	128	/* SHA512-HMAC block size */
49f66d273dSizick 
50734b6a94Sdarrenm #define	SHA256			0
51734b6a94Sdarrenm #define	SHA256_HMAC		1
52734b6a94Sdarrenm #define	SHA256_HMAC_GEN		2
53734b6a94Sdarrenm #define	SHA384			3
54734b6a94Sdarrenm #define	SHA384_HMAC		4
55734b6a94Sdarrenm #define	SHA384_HMAC_GEN		5
56734b6a94Sdarrenm #define	SHA512			6
57734b6a94Sdarrenm #define	SHA512_HMAC		7
58734b6a94Sdarrenm #define	SHA512_HMAC_GEN		8
59*45818ee1SMatthew Ahrens #define	SHA512_224		9
60*45818ee1SMatthew Ahrens #define	SHA512_256		10
61f66d273dSizick 
62734b6a94Sdarrenm /*
63734b6a94Sdarrenm  * SHA2 context.
64734b6a94Sdarrenm  * The contents of this structure are a private interface between the
65734b6a94Sdarrenm  * Init/Update/Final calls of the functions defined below.
66734b6a94Sdarrenm  * Callers must never attempt to read or write any of the fields
6755553f71Sda73024  * in this structure directly.
68734b6a94Sdarrenm  */
697c478bd9Sstevel@tonic-gate typedef struct 	{
707c478bd9Sstevel@tonic-gate 	uint32_t algotype;		/* Algorithm Type */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* state (ABCDEFGH) */
737c478bd9Sstevel@tonic-gate 	union {
747c478bd9Sstevel@tonic-gate 		uint32_t s32[8];	/* for SHA256 */
757c478bd9Sstevel@tonic-gate 		uint64_t s64[8];	/* for SHA384/512 */
767c478bd9Sstevel@tonic-gate 	} state;
777c478bd9Sstevel@tonic-gate 	/* number of bits */
787c478bd9Sstevel@tonic-gate 	union {
797c478bd9Sstevel@tonic-gate 		uint32_t c32[2];	/* for SHA256 , modulo 2^64 */
807c478bd9Sstevel@tonic-gate 		uint64_t c64[2];	/* for SHA384/512, modulo 2^128 */
817c478bd9Sstevel@tonic-gate 	} count;
827c478bd9Sstevel@tonic-gate 	union {
837c478bd9Sstevel@tonic-gate 		uint8_t		buf8[128];	/* undigested input */
847c478bd9Sstevel@tonic-gate 		uint32_t	buf32[32];	/* realigned input */
857c478bd9Sstevel@tonic-gate 		uint64_t	buf64[16];	/* realigned input */
867c478bd9Sstevel@tonic-gate 	} buf_un;
877c478bd9Sstevel@tonic-gate } SHA2_CTX;
887c478bd9Sstevel@tonic-gate 
89734b6a94Sdarrenm typedef SHA2_CTX SHA256_CTX;
90734b6a94Sdarrenm typedef SHA2_CTX SHA384_CTX;
91734b6a94Sdarrenm typedef SHA2_CTX SHA512_CTX;
92734b6a94Sdarrenm 
937c478bd9Sstevel@tonic-gate extern void SHA2Init(uint64_t mech, SHA2_CTX *);
947c478bd9Sstevel@tonic-gate 
95734b6a94Sdarrenm extern void SHA2Update(SHA2_CTX *, const void *, size_t);
967c478bd9Sstevel@tonic-gate 
97734b6a94Sdarrenm extern void SHA2Final(void *, SHA2_CTX *);
98734b6a94Sdarrenm 
99734b6a94Sdarrenm extern void SHA256Init(SHA256_CTX *);
100734b6a94Sdarrenm 
101734b6a94Sdarrenm extern void SHA256Update(SHA256_CTX *, const void *, size_t);
102734b6a94Sdarrenm 
103734b6a94Sdarrenm extern void SHA256Final(void *, SHA256_CTX *);
104734b6a94Sdarrenm 
105734b6a94Sdarrenm extern void SHA384Init(SHA384_CTX *);
106734b6a94Sdarrenm 
107734b6a94Sdarrenm extern void SHA384Update(SHA384_CTX *, const void *, size_t);
108734b6a94Sdarrenm 
109734b6a94Sdarrenm extern void SHA384Final(void *, SHA384_CTX *);
110734b6a94Sdarrenm 
111734b6a94Sdarrenm extern void SHA512Init(SHA512_CTX *);
112734b6a94Sdarrenm 
113734b6a94Sdarrenm extern void SHA512Update(SHA512_CTX *, const void *, size_t);
114734b6a94Sdarrenm 
115734b6a94Sdarrenm extern void SHA512Final(void *, SHA512_CTX *);
116734b6a94Sdarrenm 
117734b6a94Sdarrenm #ifdef _SHA2_IMPL
118734b6a94Sdarrenm /*
119734b6a94Sdarrenm  * The following types/functions are all private to the implementation
120734b6a94Sdarrenm  * of the SHA2 functions and must not be used by consumers of the interface
121734b6a94Sdarrenm  */
122734b6a94Sdarrenm 
123734b6a94Sdarrenm /*
124734b6a94Sdarrenm  * List of support mechanisms in this module.
125734b6a94Sdarrenm  *
126734b6a94Sdarrenm  * It is important to note that in the module, division or modulus calculations
127734b6a94Sdarrenm  * are used on the enumerated type to determine which mechanism is being used;
128734b6a94Sdarrenm  * therefore, changing the order or additional mechanisms should be done
129734b6a94Sdarrenm  * carefully
130734b6a94Sdarrenm  */
131734b6a94Sdarrenm typedef enum sha2_mech_type {
132734b6a94Sdarrenm 	SHA256_MECH_INFO_TYPE,		/* SUN_CKM_SHA256 */
133734b6a94Sdarrenm 	SHA256_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC */
134734b6a94Sdarrenm 	SHA256_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA256_HMAC_GENERAL */
135734b6a94Sdarrenm 	SHA384_MECH_INFO_TYPE,		/* SUN_CKM_SHA384 */
136734b6a94Sdarrenm 	SHA384_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC */
137734b6a94Sdarrenm 	SHA384_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA384_HMAC_GENERAL */
138734b6a94Sdarrenm 	SHA512_MECH_INFO_TYPE,		/* SUN_CKM_SHA512 */
139734b6a94Sdarrenm 	SHA512_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC */
140*45818ee1SMatthew Ahrens 	SHA512_HMAC_GEN_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_HMAC_GENERAL */
141*45818ee1SMatthew Ahrens 	SHA512_224_MECH_INFO_TYPE,	/* SUN_CKM_SHA512_224 */
142*45818ee1SMatthew Ahrens 	SHA512_256_MECH_INFO_TYPE	/* SUN_CKM_SHA512_256 */
143734b6a94Sdarrenm } sha2_mech_type_t;
144734b6a94Sdarrenm 
145734b6a94Sdarrenm #endif /* _SHA2_IMPL */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #endif /* _SYS_SHA2_H */
152