1c1cb2cd8Shaad /*
2c1cb2cd8Shaad * CDDL HEADER START
3c1cb2cd8Shaad *
4c1cb2cd8Shaad * The contents of this file are subject to the terms of the
5c1cb2cd8Shaad * Common Development and Distribution License (the "License").
6c1cb2cd8Shaad * You may not use this file except in compliance with the License.
7c1cb2cd8Shaad *
8c1cb2cd8Shaad * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c1cb2cd8Shaad * or http://www.opensolaris.org/os/licensing.
10c1cb2cd8Shaad * See the License for the specific language governing permissions
11c1cb2cd8Shaad * and limitations under the License.
12c1cb2cd8Shaad *
13c1cb2cd8Shaad * When distributing Covered Code, include this CDDL HEADER in each
14c1cb2cd8Shaad * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c1cb2cd8Shaad * If applicable, add the following below this CDDL HEADER, with the
16c1cb2cd8Shaad * fields enclosed by brackets "[]" replaced with your own identifying
17c1cb2cd8Shaad * information: Portions Copyright [yyyy] [name of copyright owner]
18c1cb2cd8Shaad *
19c1cb2cd8Shaad * CDDL HEADER END
20c1cb2cd8Shaad */
21c1cb2cd8Shaad /*
22f59c7639Shaad * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23c1cb2cd8Shaad * Use is subject to license terms.
24c1cb2cd8Shaad */
253227e6cfSchs /*
263227e6cfSchs * Copyright 2013 Saso Kiselkov. All rights reserved.
273227e6cfSchs */
28c1cb2cd8Shaad #include <sys/zfs_context.h>
29c1cb2cd8Shaad #include <sys/zio.h>
30*ba2539a9Schs #ifdef __FreeBSD__
313227e6cfSchs #ifdef _KERNEL
323227e6cfSchs #include <crypto/sha2/sha256.h>
333227e6cfSchs #include <crypto/sha2/sha512t.h>
343227e6cfSchs #else
353227e6cfSchs #include <sha256.h>
363227e6cfSchs #include <sha512t.h>
373227e6cfSchs #endif
38*ba2539a9Schs #else
39*ba2539a9Schs #include <sys/sha2.h>
40*ba2539a9Schs #endif
41c1cb2cd8Shaad
423227e6cfSchs /*ARGSUSED*/
43c1cb2cd8Shaad void
zio_checksum_SHA256(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)443227e6cfSchs zio_checksum_SHA256(const void *buf, uint64_t size,
453227e6cfSchs const void *ctx_template, zio_cksum_t *zcp)
46c1cb2cd8Shaad {
473227e6cfSchs SHA256_CTX ctx;
48f59c7639Shaad zio_cksum_t tmp;
49c1cb2cd8Shaad
503227e6cfSchs SHA256_Init(&ctx);
513227e6cfSchs SHA256_Update(&ctx, buf, size);
523227e6cfSchs SHA256_Final((unsigned char *)&tmp, &ctx);
53c1cb2cd8Shaad
54f59c7639Shaad /*
55f59c7639Shaad * A prior implementation of this function had a
56f59c7639Shaad * private SHA256 implementation always wrote things out in
57f59c7639Shaad * Big Endian and there wasn't a byteswap variant of it.
58f59c7639Shaad * To preseve on disk compatibility we need to force that
59f59c7639Shaad * behaviour.
60f59c7639Shaad */
61f59c7639Shaad zcp->zc_word[0] = BE_64(tmp.zc_word[0]);
62f59c7639Shaad zcp->zc_word[1] = BE_64(tmp.zc_word[1]);
63f59c7639Shaad zcp->zc_word[2] = BE_64(tmp.zc_word[2]);
64f59c7639Shaad zcp->zc_word[3] = BE_64(tmp.zc_word[3]);
65c1cb2cd8Shaad }
663227e6cfSchs
67*ba2539a9Schs #ifndef __NetBSD__
683227e6cfSchs /*ARGSUSED*/
693227e6cfSchs void
zio_checksum_SHA512_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)703227e6cfSchs zio_checksum_SHA512_native(const void *buf, uint64_t size,
713227e6cfSchs const void *ctx_template, zio_cksum_t *zcp)
723227e6cfSchs {
733227e6cfSchs SHA512_CTX ctx;
743227e6cfSchs
753227e6cfSchs SHA512_256_Init(&ctx);
763227e6cfSchs SHA512_256_Update(&ctx, buf, size);
773227e6cfSchs SHA512_256_Final((unsigned char *)zcp, &ctx);
783227e6cfSchs }
793227e6cfSchs
803227e6cfSchs /*ARGSUSED*/
813227e6cfSchs void
zio_checksum_SHA512_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)823227e6cfSchs zio_checksum_SHA512_byteswap(const void *buf, uint64_t size,
833227e6cfSchs const void *ctx_template, zio_cksum_t *zcp)
843227e6cfSchs {
853227e6cfSchs zio_cksum_t tmp;
863227e6cfSchs
873227e6cfSchs zio_checksum_SHA512_native(buf, size, ctx_template, &tmp);
883227e6cfSchs zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
893227e6cfSchs zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
903227e6cfSchs zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
913227e6cfSchs zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
923227e6cfSchs }
93*ba2539a9Schs #endif
94