usr/src/uts/common/fs/zfs/sha256.c
author eschrock
Fri, 03 Mar 2006 20:08:16 -0800
changeset 1544 938876158511
parent 789 b348f31ed315
child 5688 c0b02c8fd2c0
permissions -rw-r--r--
PSARC 2006/077 zpool clear PSARC 2006/139 FMA for ZFS 6284889 arc should replace the znode cache 6333006 DMU & DSL should not panic upon I/O error 6333092 concurrent reads to a file not scaling with number of readers 6338081 ZFS/FMA phase 1 6338386 need persistent error log 6341326 i/o error causes arc buf hash table corruption 6341639 zfs backup/restore should compute/verify checksum of backup stream 6348002 out of space due to changing properties 6354724 inaccurate error message from zfs restore 6354872 dmu_sync() blows predictive accounting 6355416 zpool scrubbing consumes all memory, system hung 6363995 df should only load libzfs when it encounters a ZFS filesystem 6366320 zfs backup/restore doesn't like signals 6368892 mount -m support needed for legacy mounts 6368902 boot archive fstat support needed for ZFS Mountroot 6369424 BFU complains when bfu'ing a ZFS root filesystem 6374062 mountroot support needed for ZFS 6376356 dirtying dbuf obj=43 lvl=0 blkid=0 but not tx_held 6378391 unused members of dmu_objset_stats_t 6378392 clean up zfs_cmd_t structure 6378685 buf_init should allocate its hash table more carefully 6378976 ziltest should be a first class citizen 6381086 zdb segfaults if there is a spa deferred-free bplist 6381203 deadlock due to i/o while assigning (tc_lock held) 6381209 freed space is not immediately available 6381344 'zpool clear' 6381345 FAULTED devices should really be UNAVAIL 6381346 import should mark devices as persistently unavailable 6383272 recursive mutex_enter() during log replay with zfs root 6386326 origin property is not displayed 6386354 libzfs does too much in its _init section, calls exit(1) 6386624 zpool should not complain about non-existent devices from libdiskmgt 6386910 spa needs to be i/o error hardened 6387735 need a mechanism to inject faults into ZFS 6387736 internal ZFS utilities should be placed in an ON-private package 6389928 libzfs should ship a lint library 6390609 malformed vdev config panics on zpool_create() 6390677 version number checking makes upgrades challenging 6390713 ztest hangs in zil_suspend() 6391873 metadata compression should be turned back on 6392113 ztest sometimes reports leaked blocks because ZIL isn't resilvered 6393004 minor memory leak in unique_insert()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     5
 * Common Development and Distribution License, Version 1.0 only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     6
 * (the "License").  You may not use this file except in compliance
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 * with the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/zio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/zio_checksum.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
 * SHA-256 checksum, as specified in FIPS 180-2, available at:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
 * http://csrc.nist.gov/cryptval
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
 * This is a very compact implementation of SHA-256.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
 * It is designed to be simple and portable, not to be fast.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
 * The literal definitions according to FIPS180-2 would be:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
 * 	Ch(x, y, z)     (((x) & (y)) ^ ((~(x)) & (z)))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
 * 	Maj(x, y, z)    (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 * We use logical equivalents which require one less op.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
#define	Ch(x, y, z)	((z) ^ ((x) & ((y) ^ (z))))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
#define	Maj(x, y, z)	(((x) & (y)) ^ ((z) & ((x) ^ (y))))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#define	Rot32(x, s)	(((x) >> s) | ((x) << (32 - s)))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
#define	SIGMA0(x)	(Rot32(x, 2) ^ Rot32(x, 13) ^ Rot32(x, 22))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
#define	SIGMA1(x)	(Rot32(x, 6) ^ Rot32(x, 11) ^ Rot32(x, 25))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
#define	sigma0(x)	(Rot32(x, 7) ^ Rot32(x, 18) ^ ((x) >> 3))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
#define	sigma1(x)	(Rot32(x, 17) ^ Rot32(x, 19) ^ ((x) >> 10))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
static const uint32_t SHA256_K[64] = {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
SHA256Transform(uint32_t *H, const uint8_t *cp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
	uint32_t a, b, c, d, e, f, g, h, t, T1, T2, W[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
	for (t = 0; t < 16; t++, cp += 4)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
		W[t] = (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
	for (t = 16; t < 64; t++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
		W[t] = sigma1(W[t - 2]) + W[t - 7] +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
		    sigma0(W[t - 15]) + W[t - 16];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
	a = H[0]; b = H[1]; c = H[2]; d = H[3];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
	e = H[4]; f = H[5]; g = H[6]; h = H[7];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
	for (t = 0; t < 64; t++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
		T1 = h + SIGMA1(e) + Ch(e, f, g) + SHA256_K[t] + W[t];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
		T2 = SIGMA0(a) + Maj(a, b, c);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
		h = g; g = f; f = e; e = d + T1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
		d = c; c = b; b = a; a = T1 + T2;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
	H[0] += a; H[1] += b; H[2] += c; H[3] += d;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
	H[4] += e; H[5] += f; H[6] += g; H[7] += h;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
zio_checksum_SHA256(const void *buf, uint64_t size, zio_cksum_t *zcp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
	uint32_t H[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	    0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
	uint8_t pad[128];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
	int padsize = size & 63;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
	for (i = 0; i < size - padsize; i += 64)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
		SHA256Transform(H, (uint8_t *)buf + i);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
	for (i = 0; i < padsize; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
		pad[i] = ((uint8_t *)buf)[i];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
	for (pad[padsize++] = 0x80; (padsize & 63) != 56; padsize++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
		pad[padsize] = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
	for (i = 0; i < 8; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
		pad[padsize++] = (size << 3) >> (56 - 8 * i);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
	for (i = 0; i < padsize; i += 64)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
		SHA256Transform(H, pad + i);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
	ZIO_SET_CHECKSUM(zcp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
	    (uint64_t)H[0] << 32 | H[1],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
	    (uint64_t)H[2] << 32 | H[3],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
	    (uint64_t)H[4] << 32 | H[5],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
	    (uint64_t)H[6] << 32 | H[7]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
}