xref: /illumos-gate/usr/src/uts/common/fs/zfs/sha256.c (revision 770499e1)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
517f17c2dSbonwick  * Common Development and Distribution License (the "License").
617f17c2dSbonwick  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22b24ab676SJeff Bonwick  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
2545818ee1SMatthew Ahrens /*
2645818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27*770499e1SDan Kimmel  * Copyright (c) 2016 by Delphix. All rights reserved.
2845818ee1SMatthew Ahrens  */
29fa9e4066Sahrens #include <sys/zfs_context.h>
30fa9e4066Sahrens #include <sys/zio.h>
3197322426SDarren J Moffat #include <sys/sha2.h>
32*770499e1SDan Kimmel #include <sys/abd.h>
33*770499e1SDan Kimmel 
34*770499e1SDan Kimmel static int
sha_incremental(void * buf,size_t size,void * arg)35*770499e1SDan Kimmel sha_incremental(void *buf, size_t size, void *arg)
36*770499e1SDan Kimmel {
37*770499e1SDan Kimmel 	SHA2_CTX *ctx = arg;
38*770499e1SDan Kimmel 	SHA2Update(ctx, buf, size);
39*770499e1SDan Kimmel 	return (0);
40*770499e1SDan Kimmel }
41fa9e4066Sahrens 
4245818ee1SMatthew Ahrens /*ARGSUSED*/
43fa9e4066Sahrens void
abd_checksum_SHA256(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)44*770499e1SDan Kimmel abd_checksum_SHA256(abd_t *abd, uint64_t size,
4545818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
46fa9e4066Sahrens {
4797322426SDarren J Moffat 	SHA2_CTX ctx;
4897322426SDarren J Moffat 	zio_cksum_t tmp;
4997322426SDarren J Moffat 
5097322426SDarren J Moffat 	SHA2Init(SHA256, &ctx);
51*770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size, sha_incremental, &ctx);
5297322426SDarren J Moffat 	SHA2Final(&tmp, &ctx);
5397322426SDarren J Moffat 
5497322426SDarren J Moffat 	/*
5597322426SDarren J Moffat 	 * A prior implementation of this function had a
5697322426SDarren J Moffat 	 * private SHA256 implementation always wrote things out in
5797322426SDarren J Moffat 	 * Big Endian and there wasn't a byteswap variant of it.
58*770499e1SDan Kimmel 	 * To preserve on disk compatibility we need to force that
59*770499e1SDan Kimmel 	 * behavior.
6097322426SDarren J Moffat 	 */
6197322426SDarren J Moffat 	zcp->zc_word[0] = BE_64(tmp.zc_word[0]);
6297322426SDarren J Moffat 	zcp->zc_word[1] = BE_64(tmp.zc_word[1]);
6397322426SDarren J Moffat 	zcp->zc_word[2] = BE_64(tmp.zc_word[2]);
6497322426SDarren J Moffat 	zcp->zc_word[3] = BE_64(tmp.zc_word[3]);
65fa9e4066Sahrens }
6645818ee1SMatthew Ahrens 
6745818ee1SMatthew Ahrens /*ARGSUSED*/
6845818ee1SMatthew Ahrens void
abd_checksum_SHA512_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)69*770499e1SDan Kimmel abd_checksum_SHA512_native(abd_t *abd, uint64_t size,
7045818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
7145818ee1SMatthew Ahrens {
7245818ee1SMatthew Ahrens 	SHA2_CTX	ctx;
7345818ee1SMatthew Ahrens 
7445818ee1SMatthew Ahrens 	SHA2Init(SHA512_256, &ctx);
75*770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size, sha_incremental, &ctx);
7645818ee1SMatthew Ahrens 	SHA2Final(zcp, &ctx);
7745818ee1SMatthew Ahrens }
7845818ee1SMatthew Ahrens 
7945818ee1SMatthew Ahrens /*ARGSUSED*/
8045818ee1SMatthew Ahrens void
abd_checksum_SHA512_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)81*770499e1SDan Kimmel abd_checksum_SHA512_byteswap(abd_t *abd, uint64_t size,
8245818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
8345818ee1SMatthew Ahrens {
8445818ee1SMatthew Ahrens 	zio_cksum_t	tmp;
8545818ee1SMatthew Ahrens 
86*770499e1SDan Kimmel 	abd_checksum_SHA512_native(abd, size, ctx_template, &tmp);
8745818ee1SMatthew Ahrens 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
8845818ee1SMatthew Ahrens 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
8945818ee1SMatthew Ahrens 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
9045818ee1SMatthew Ahrens 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
9145818ee1SMatthew Ahrens }
92