xref: /titanic_50/usr/src/uts/common/fs/zfs/sha256.c (revision 45818ee124adeaaf947698996b4f4c722afc6d1f)
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  */
25*45818ee1SMatthew Ahrens /*
26*45818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
27*45818ee1SMatthew Ahrens  */
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/zio.h>
3097322426SDarren J Moffat #include <sys/sha2.h>
31fa9e4066Sahrens 
32*45818ee1SMatthew Ahrens /*ARGSUSED*/
33fa9e4066Sahrens void
zio_checksum_SHA256(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)34*45818ee1SMatthew Ahrens zio_checksum_SHA256(const void *buf, uint64_t size,
35*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
36fa9e4066Sahrens {
3797322426SDarren J Moffat 	SHA2_CTX ctx;
3897322426SDarren J Moffat 	zio_cksum_t tmp;
39fa9e4066Sahrens 
4097322426SDarren J Moffat 	SHA2Init(SHA256, &ctx);
4197322426SDarren J Moffat 	SHA2Update(&ctx, buf, size);
4297322426SDarren J Moffat 	SHA2Final(&tmp, &ctx);
43fa9e4066Sahrens 
4497322426SDarren J Moffat 	/*
4597322426SDarren J Moffat 	 * A prior implementation of this function had a
4697322426SDarren J Moffat 	 * private SHA256 implementation always wrote things out in
4797322426SDarren J Moffat 	 * Big Endian and there wasn't a byteswap variant of it.
4897322426SDarren J Moffat 	 * To preseve on disk compatibility we need to force that
4997322426SDarren J Moffat 	 * behaviour.
5097322426SDarren J Moffat 	 */
5197322426SDarren J Moffat 	zcp->zc_word[0] = BE_64(tmp.zc_word[0]);
5297322426SDarren J Moffat 	zcp->zc_word[1] = BE_64(tmp.zc_word[1]);
5397322426SDarren J Moffat 	zcp->zc_word[2] = BE_64(tmp.zc_word[2]);
5497322426SDarren J Moffat 	zcp->zc_word[3] = BE_64(tmp.zc_word[3]);
55fa9e4066Sahrens }
56*45818ee1SMatthew Ahrens 
57*45818ee1SMatthew Ahrens /*ARGSUSED*/
58*45818ee1SMatthew Ahrens void
zio_checksum_SHA512_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)59*45818ee1SMatthew Ahrens zio_checksum_SHA512_native(const void *buf, uint64_t size,
60*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
61*45818ee1SMatthew Ahrens {
62*45818ee1SMatthew Ahrens 	SHA2_CTX	ctx;
63*45818ee1SMatthew Ahrens 
64*45818ee1SMatthew Ahrens 	SHA2Init(SHA512_256, &ctx);
65*45818ee1SMatthew Ahrens 	SHA2Update(&ctx, buf, size);
66*45818ee1SMatthew Ahrens 	SHA2Final(zcp, &ctx);
67*45818ee1SMatthew Ahrens }
68*45818ee1SMatthew Ahrens 
69*45818ee1SMatthew Ahrens /*ARGSUSED*/
70*45818ee1SMatthew Ahrens void
zio_checksum_SHA512_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)71*45818ee1SMatthew Ahrens zio_checksum_SHA512_byteswap(const void *buf, uint64_t size,
72*45818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
73*45818ee1SMatthew Ahrens {
74*45818ee1SMatthew Ahrens 	zio_cksum_t	tmp;
75*45818ee1SMatthew Ahrens 
76*45818ee1SMatthew Ahrens 	zio_checksum_SHA512_native(buf, size, ctx_template, &tmp);
77*45818ee1SMatthew Ahrens 	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
78*45818ee1SMatthew Ahrens 	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
79*45818ee1SMatthew Ahrens 	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
80*45818ee1SMatthew Ahrens 	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
81*45818ee1SMatthew Ahrens }
82