xref: /freebsd/sys/opencrypto/xform_sha2.c (revision 255811d758fb82798c83f4690879cca15d7f2329)
12155bb23SAllan Jude /*	$OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $	*/
22155bb23SAllan Jude /*-
32155bb23SAllan Jude  * The authors of this code are John Ioannidis (ji@tla.org),
42155bb23SAllan Jude  * Angelos D. Keromytis (kermit@csd.uch.gr),
52155bb23SAllan Jude  * Niels Provos (provos@physnet.uni-hamburg.de) and
62155bb23SAllan Jude  * Damien Miller (djm@mindrot.org).
72155bb23SAllan Jude  *
82155bb23SAllan Jude  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
92155bb23SAllan Jude  * in November 1995.
102155bb23SAllan Jude  *
112155bb23SAllan Jude  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
122155bb23SAllan Jude  * by Angelos D. Keromytis.
132155bb23SAllan Jude  *
142155bb23SAllan Jude  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
152155bb23SAllan Jude  * and Niels Provos.
162155bb23SAllan Jude  *
172155bb23SAllan Jude  * Additional features in 1999 by Angelos D. Keromytis.
182155bb23SAllan Jude  *
192155bb23SAllan Jude  * AES XTS implementation in 2008 by Damien Miller
202155bb23SAllan Jude  *
212155bb23SAllan Jude  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
222155bb23SAllan Jude  * Angelos D. Keromytis and Niels Provos.
232155bb23SAllan Jude  *
242155bb23SAllan Jude  * Copyright (C) 2001, Angelos D. Keromytis.
252155bb23SAllan Jude  *
262155bb23SAllan Jude  * Copyright (C) 2008, Damien Miller
272155bb23SAllan Jude  * Copyright (c) 2014 The FreeBSD Foundation
282155bb23SAllan Jude  * All rights reserved.
292155bb23SAllan Jude  *
302155bb23SAllan Jude  * Portions of this software were developed by John-Mark Gurney
312155bb23SAllan Jude  * under sponsorship of the FreeBSD Foundation and
322155bb23SAllan Jude  * Rubicon Communications, LLC (Netgate).
332155bb23SAllan Jude  *
342155bb23SAllan Jude  * Permission to use, copy, and modify this software with or without fee
352155bb23SAllan Jude  * is hereby granted, provided that this entire notice is included in
362155bb23SAllan Jude  * all copies of any software which is or includes a copy or
372155bb23SAllan Jude  * modification of this software.
382155bb23SAllan Jude  * You may use this code under the GNU public license if you so wish. Please
392155bb23SAllan Jude  * contribute changes back to the authors under this freer than GPL license
402155bb23SAllan Jude  * so that we may further the use of strong encryption without limitations to
412155bb23SAllan Jude  * all.
422155bb23SAllan Jude  *
432155bb23SAllan Jude  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
442155bb23SAllan Jude  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
452155bb23SAllan Jude  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
462155bb23SAllan Jude  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
472155bb23SAllan Jude  * PURPOSE.
482155bb23SAllan Jude  */
492155bb23SAllan Jude 
502155bb23SAllan Jude #include <sys/cdefs.h>
512155bb23SAllan Jude __FBSDID("$FreeBSD$");
522155bb23SAllan Jude 
532155bb23SAllan Jude #include <crypto/sha2/sha256.h>
542155bb23SAllan Jude #include <crypto/sha2/sha384.h>
552155bb23SAllan Jude #include <crypto/sha2/sha512.h>
562155bb23SAllan Jude #include <opencrypto/xform_auth.h>
572155bb23SAllan Jude 
582155bb23SAllan Jude static	int SHA256Update_int(void *, const u_int8_t *, u_int16_t);
592155bb23SAllan Jude static	int SHA384Update_int(void *, const u_int8_t *, u_int16_t);
602155bb23SAllan Jude static	int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
612155bb23SAllan Jude 
622155bb23SAllan Jude /* Authentication instances */
632155bb23SAllan Jude struct auth_hash auth_hash_hmac_sha2_256 = {
64*255811d7SConrad Meyer 	.type = CRYPTO_SHA2_256_HMAC,
65*255811d7SConrad Meyer 	.name = "HMAC-SHA2-256",
66*255811d7SConrad Meyer 	.keysize = SHA2_256_HMAC_BLOCK_LEN,
67*255811d7SConrad Meyer 	.hashsize = SHA2_256_HASH_LEN,
68*255811d7SConrad Meyer 	.ctxsize = sizeof(SHA256_CTX),
69*255811d7SConrad Meyer 	.blocksize = SHA2_256_HMAC_BLOCK_LEN,
70*255811d7SConrad Meyer 	.Init = (void (*)(void *)) SHA256_Init,
71*255811d7SConrad Meyer 	.Update = SHA256Update_int,
72*255811d7SConrad Meyer 	.Final = (void (*)(u_int8_t *, void *)) SHA256_Final,
732155bb23SAllan Jude };
742155bb23SAllan Jude 
752155bb23SAllan Jude struct auth_hash auth_hash_hmac_sha2_384 = {
76*255811d7SConrad Meyer 	.type = CRYPTO_SHA2_384_HMAC,
77*255811d7SConrad Meyer 	.name = "HMAC-SHA2-384",
78*255811d7SConrad Meyer 	.keysize = SHA2_384_HMAC_BLOCK_LEN,
79*255811d7SConrad Meyer 	.hashsize = SHA2_384_HASH_LEN,
80*255811d7SConrad Meyer 	.ctxsize = sizeof(SHA384_CTX),
81*255811d7SConrad Meyer 	.blocksize = SHA2_384_HMAC_BLOCK_LEN,
82*255811d7SConrad Meyer 	.Init = (void (*)(void *)) SHA384_Init,
83*255811d7SConrad Meyer 	.Update = SHA384Update_int,
84*255811d7SConrad Meyer 	.Final = (void (*)(u_int8_t *, void *)) SHA384_Final,
852155bb23SAllan Jude };
862155bb23SAllan Jude 
872155bb23SAllan Jude struct auth_hash auth_hash_hmac_sha2_512 = {
88*255811d7SConrad Meyer 	.type = CRYPTO_SHA2_512_HMAC,
89*255811d7SConrad Meyer 	.name = "HMAC-SHA2-512",
90*255811d7SConrad Meyer 	.keysize = SHA2_512_HMAC_BLOCK_LEN,
91*255811d7SConrad Meyer 	.hashsize = SHA2_512_HASH_LEN,
92*255811d7SConrad Meyer 	.ctxsize = sizeof(SHA512_CTX),
93*255811d7SConrad Meyer 	.blocksize = SHA2_512_HMAC_BLOCK_LEN,
94*255811d7SConrad Meyer 	.Init = (void (*)(void *)) SHA512_Init,
95*255811d7SConrad Meyer 	.Update = SHA512Update_int,
96*255811d7SConrad Meyer 	.Final = (void (*)(u_int8_t *, void *)) SHA512_Final,
972155bb23SAllan Jude };
982155bb23SAllan Jude 
992155bb23SAllan Jude /*
1002155bb23SAllan Jude  * And now for auth.
1012155bb23SAllan Jude  */
1022155bb23SAllan Jude static int
1032155bb23SAllan Jude SHA256Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
1042155bb23SAllan Jude {
1052155bb23SAllan Jude 	SHA256_Update(ctx, buf, len);
1062155bb23SAllan Jude 	return 0;
1072155bb23SAllan Jude }
1082155bb23SAllan Jude 
1092155bb23SAllan Jude static int
1102155bb23SAllan Jude SHA384Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
1112155bb23SAllan Jude {
1122155bb23SAllan Jude 	SHA384_Update(ctx, buf, len);
1132155bb23SAllan Jude 	return 0;
1142155bb23SAllan Jude }
1152155bb23SAllan Jude 
1162155bb23SAllan Jude static int
1172155bb23SAllan Jude SHA512Update_int(void *ctx, const u_int8_t *buf, u_int16_t len)
1182155bb23SAllan Jude {
1192155bb23SAllan Jude 	SHA512_Update(ctx, buf, len);
1202155bb23SAllan Jude 	return 0;
1212155bb23SAllan Jude }
122