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 */
25168404Spjd#include <sys/zfs_context.h>
26168404Spjd#include <sys/zio.h>
27219089Spjd#ifdef _KERNEL
28219089Spjd#include <crypto/sha2/sha2.h>
29219089Spjd#else
30219089Spjd#include <sha256.h>
31219089Spjd#endif
32168404Spjd
33168404Spjdvoid
34168404Spjdzio_checksum_SHA256(const void *buf, uint64_t size, zio_cksum_t *zcp)
35168404Spjd{
36219089Spjd	SHA256_CTX ctx;
37219089Spjd	zio_cksum_t tmp;
38168404Spjd
39219089Spjd	SHA256_Init(&ctx);
40219089Spjd	SHA256_Update(&ctx, buf, size);
41219089Spjd	SHA256_Final((unsigned char *)&tmp, &ctx);
42168404Spjd
43219089Spjd	/*
44219089Spjd	 * A prior implementation of this function had a
45219089Spjd	 * private SHA256 implementation always wrote things out in
46219089Spjd	 * Big Endian and there wasn't a byteswap variant of it.
47219089Spjd	 * To preseve on disk compatibility we need to force that
48219089Spjd	 * behaviour.
49219089Spjd	 */
50219089Spjd	zcp->zc_word[0] = BE_64(tmp.zc_word[0]);
51219089Spjd	zcp->zc_word[1] = BE_64(tmp.zc_word[1]);
52219089Spjd	zcp->zc_word[2] = BE_64(tmp.zc_word[2]);
53219089Spjd	zcp->zc_word[3] = BE_64(tmp.zc_word[3]);
54168404Spjd}
55