1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5185029Spjd * Common Development and Distribution License (the "License").
6185029Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25290757Smav/*
26290757Smav * Copyright 2013 Saso Kiselkov. All rights reserved.
27290757Smav */
28168404Spjd#include <sys/zfs_context.h>
29168404Spjd#include <sys/zio.h>
30219089Spjd#ifdef _KERNEL
31219089Spjd#include <crypto/sha2/sha2.h>
32219089Spjd#else
33219089Spjd#include <sha256.h>
34219089Spjd#endif
35168404Spjd
36290757Smav/*ARGSUSED*/
37168404Spjdvoid
38290757Smavzio_checksum_SHA256(const void *buf, uint64_t size,
39290757Smav    const void *ctx_template, zio_cksum_t *zcp)
40168404Spjd{
41219089Spjd	SHA256_CTX ctx;
42219089Spjd	zio_cksum_t tmp;
43168404Spjd
44219089Spjd	SHA256_Init(&ctx);
45219089Spjd	SHA256_Update(&ctx, buf, size);
46219089Spjd	SHA256_Final((unsigned char *)&tmp, &ctx);
47168404Spjd
48219089Spjd	/*
49219089Spjd	 * A prior implementation of this function had a
50219089Spjd	 * private SHA256 implementation always wrote things out in
51219089Spjd	 * Big Endian and there wasn't a byteswap variant of it.
52219089Spjd	 * To preseve on disk compatibility we need to force that
53219089Spjd	 * behaviour.
54219089Spjd	 */
55219089Spjd	zcp->zc_word[0] = BE_64(tmp.zc_word[0]);
56219089Spjd	zcp->zc_word[1] = BE_64(tmp.zc_word[1]);
57219089Spjd	zcp->zc_word[2] = BE_64(tmp.zc_word[2]);
58219089Spjd	zcp->zc_word[3] = BE_64(tmp.zc_word[3]);
59168404Spjd}
60290757Smav
61290757Smav#ifdef illumos
62290757Smav/*ARGSUSED*/
63290757Smavvoid
64290757Smavzio_checksum_SHA512_native(const void *buf, uint64_t size,
65290757Smav    const void *ctx_template, zio_cksum_t *zcp)
66290757Smav{
67290757Smav	SHA2_CTX	ctx;
68290757Smav
69290757Smav	SHA2Init(SHA512_256, &ctx);
70290757Smav	SHA2Update(&ctx, buf, size);
71290757Smav	SHA2Final(zcp, &ctx);
72290757Smav}
73290757Smav
74290757Smav/*ARGSUSED*/
75290757Smavvoid
76290757Smavzio_checksum_SHA512_byteswap(const void *buf, uint64_t size,
77290757Smav    const void *ctx_template, zio_cksum_t *zcp)
78290757Smav{
79290757Smav	zio_cksum_t	tmp;
80290757Smav
81290757Smav	zio_checksum_SHA512_native(buf, size, ctx_template, &tmp);
82290757Smav	zcp->zc_word[0] = BSWAP_64(tmp.zc_word[0]);
83290757Smav	zcp->zc_word[1] = BSWAP_64(tmp.zc_word[1]);
84290757Smav	zcp->zc_word[2] = BSWAP_64(tmp.zc_word[2]);
85290757Smav	zcp->zc_word[3] = BSWAP_64(tmp.zc_word[3]);
86290757Smav}
87290757Smav#endif
88