|
| 1 | +// Copyright 2009 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package sha256 implements the SHA-224 and SHA-256 hash algorithms as defined |
| 6 | +// in FIPS 180-4. |
| 7 | +package sha256 |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "internal/byteorder" |
| 12 | +) |
| 13 | + |
| 14 | +// The size of a SHA-256 checksum in bytes. |
| 15 | +const size = 32 |
| 16 | + |
| 17 | +// The size of a SHA-224 checksum in bytes. |
| 18 | +const size224 = 28 |
| 19 | + |
| 20 | +// The block size of SHA-256 and SHA-224 in bytes. |
| 21 | +const blockSize = 64 |
| 22 | + |
| 23 | +const ( |
| 24 | + chunk = 64 |
| 25 | + init0 = 0x6A09E667 |
| 26 | + init1 = 0xBB67AE85 |
| 27 | + init2 = 0x3C6EF372 |
| 28 | + init3 = 0xA54FF53A |
| 29 | + init4 = 0x510E527F |
| 30 | + init5 = 0x9B05688C |
| 31 | + init6 = 0x1F83D9AB |
| 32 | + init7 = 0x5BE0CD19 |
| 33 | + init0_224 = 0xC1059ED8 |
| 34 | + init1_224 = 0x367CD507 |
| 35 | + init2_224 = 0x3070DD17 |
| 36 | + init3_224 = 0xF70E5939 |
| 37 | + init4_224 = 0xFFC00B31 |
| 38 | + init5_224 = 0x68581511 |
| 39 | + init6_224 = 0x64F98FA7 |
| 40 | + init7_224 = 0xBEFA4FA4 |
| 41 | +) |
| 42 | + |
| 43 | +// Digest is a SHA-224 or SHA-256 [hash.Hash] implementation. |
| 44 | +type Digest struct { |
| 45 | + h [8]uint32 |
| 46 | + x [chunk]byte |
| 47 | + nx int |
| 48 | + len uint64 |
| 49 | + is224 bool // mark if this digest is SHA-224 |
| 50 | +} |
| 51 | + |
| 52 | +const ( |
| 53 | + magic224 = "sha\x02" |
| 54 | + magic256 = "sha\x03" |
| 55 | + marshaledSize = len(magic256) + 8*4 + chunk + 8 |
| 56 | +) |
| 57 | + |
| 58 | +func (d *Digest) MarshalBinary() ([]byte, error) { |
| 59 | + return d.AppendBinary(make([]byte, 0, marshaledSize)) |
| 60 | +} |
| 61 | + |
| 62 | +func (d *Digest) AppendBinary(b []byte) ([]byte, error) { |
| 63 | + if d.is224 { |
| 64 | + b = append(b, magic224...) |
| 65 | + } else { |
| 66 | + b = append(b, magic256...) |
| 67 | + } |
| 68 | + b = byteorder.BeAppendUint32(b, d.h[0]) |
| 69 | + b = byteorder.BeAppendUint32(b, d.h[1]) |
| 70 | + b = byteorder.BeAppendUint32(b, d.h[2]) |
| 71 | + b = byteorder.BeAppendUint32(b, d.h[3]) |
| 72 | + b = byteorder.BeAppendUint32(b, d.h[4]) |
| 73 | + b = byteorder.BeAppendUint32(b, d.h[5]) |
| 74 | + b = byteorder.BeAppendUint32(b, d.h[6]) |
| 75 | + b = byteorder.BeAppendUint32(b, d.h[7]) |
| 76 | + b = append(b, d.x[:d.nx]...) |
| 77 | + b = append(b, make([]byte, len(d.x)-d.nx)...) |
| 78 | + b = byteorder.BeAppendUint64(b, d.len) |
| 79 | + return b, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (d *Digest) UnmarshalBinary(b []byte) error { |
| 83 | + if len(b) < len(magic224) || (d.is224 && string(b[:len(magic224)]) != magic224) || (!d.is224 && string(b[:len(magic256)]) != magic256) { |
| 84 | + return errors.New("crypto/sha256: invalid hash state identifier") |
| 85 | + } |
| 86 | + if len(b) != marshaledSize { |
| 87 | + return errors.New("crypto/sha256: invalid hash state size") |
| 88 | + } |
| 89 | + b = b[len(magic224):] |
| 90 | + b, d.h[0] = consumeUint32(b) |
| 91 | + b, d.h[1] = consumeUint32(b) |
| 92 | + b, d.h[2] = consumeUint32(b) |
| 93 | + b, d.h[3] = consumeUint32(b) |
| 94 | + b, d.h[4] = consumeUint32(b) |
| 95 | + b, d.h[5] = consumeUint32(b) |
| 96 | + b, d.h[6] = consumeUint32(b) |
| 97 | + b, d.h[7] = consumeUint32(b) |
| 98 | + b = b[copy(d.x[:], b):] |
| 99 | + b, d.len = consumeUint64(b) |
| 100 | + d.nx = int(d.len % chunk) |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func consumeUint64(b []byte) ([]byte, uint64) { |
| 105 | + return b[8:], byteorder.BeUint64(b) |
| 106 | +} |
| 107 | + |
| 108 | +func consumeUint32(b []byte) ([]byte, uint32) { |
| 109 | + return b[4:], byteorder.BeUint32(b) |
| 110 | +} |
| 111 | + |
| 112 | +func (d *Digest) Reset() { |
| 113 | + if !d.is224 { |
| 114 | + d.h[0] = init0 |
| 115 | + d.h[1] = init1 |
| 116 | + d.h[2] = init2 |
| 117 | + d.h[3] = init3 |
| 118 | + d.h[4] = init4 |
| 119 | + d.h[5] = init5 |
| 120 | + d.h[6] = init6 |
| 121 | + d.h[7] = init7 |
| 122 | + } else { |
| 123 | + d.h[0] = init0_224 |
| 124 | + d.h[1] = init1_224 |
| 125 | + d.h[2] = init2_224 |
| 126 | + d.h[3] = init3_224 |
| 127 | + d.h[4] = init4_224 |
| 128 | + d.h[5] = init5_224 |
| 129 | + d.h[6] = init6_224 |
| 130 | + d.h[7] = init7_224 |
| 131 | + } |
| 132 | + d.nx = 0 |
| 133 | + d.len = 0 |
| 134 | +} |
| 135 | + |
| 136 | +// New returns a new Digest computing the SHA-256 hash. |
| 137 | +func New() *Digest { |
| 138 | + d := new(Digest) |
| 139 | + d.Reset() |
| 140 | + return d |
| 141 | +} |
| 142 | + |
| 143 | +// New224 returns a new Digest computing the SHA-224 hash. |
| 144 | +func New224() *Digest { |
| 145 | + d := new(Digest) |
| 146 | + d.is224 = true |
| 147 | + d.Reset() |
| 148 | + return d |
| 149 | +} |
| 150 | + |
| 151 | +func (d *Digest) Size() int { |
| 152 | + if !d.is224 { |
| 153 | + return size |
| 154 | + } |
| 155 | + return size224 |
| 156 | +} |
| 157 | + |
| 158 | +func (d *Digest) BlockSize() int { return blockSize } |
| 159 | + |
| 160 | +func (d *Digest) Write(p []byte) (nn int, err error) { |
| 161 | + nn = len(p) |
| 162 | + d.len += uint64(nn) |
| 163 | + if d.nx > 0 { |
| 164 | + n := copy(d.x[d.nx:], p) |
| 165 | + d.nx += n |
| 166 | + if d.nx == chunk { |
| 167 | + block(d, d.x[:]) |
| 168 | + d.nx = 0 |
| 169 | + } |
| 170 | + p = p[n:] |
| 171 | + } |
| 172 | + if len(p) >= chunk { |
| 173 | + n := len(p) &^ (chunk - 1) |
| 174 | + block(d, p[:n]) |
| 175 | + p = p[n:] |
| 176 | + } |
| 177 | + if len(p) > 0 { |
| 178 | + d.nx = copy(d.x[:], p) |
| 179 | + } |
| 180 | + return |
| 181 | +} |
| 182 | + |
| 183 | +func (d *Digest) Sum(in []byte) []byte { |
| 184 | + // Make a copy of d so that caller can keep writing and summing. |
| 185 | + d0 := *d |
| 186 | + hash := d0.checkSum() |
| 187 | + if d0.is224 { |
| 188 | + return append(in, hash[:size224]...) |
| 189 | + } |
| 190 | + return append(in, hash[:]...) |
| 191 | +} |
| 192 | + |
| 193 | +func (d *Digest) checkSum() [size]byte { |
| 194 | + len := d.len |
| 195 | + // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. |
| 196 | + var tmp [64 + 8]byte // padding + length buffer |
| 197 | + tmp[0] = 0x80 |
| 198 | + var t uint64 |
| 199 | + if len%64 < 56 { |
| 200 | + t = 56 - len%64 |
| 201 | + } else { |
| 202 | + t = 64 + 56 - len%64 |
| 203 | + } |
| 204 | + |
| 205 | + // Length in bits. |
| 206 | + len <<= 3 |
| 207 | + padlen := tmp[:t+8] |
| 208 | + byteorder.BePutUint64(padlen[t+0:], len) |
| 209 | + d.Write(padlen) |
| 210 | + |
| 211 | + if d.nx != 0 { |
| 212 | + panic("d.nx != 0") |
| 213 | + } |
| 214 | + |
| 215 | + var digest [size]byte |
| 216 | + |
| 217 | + byteorder.BePutUint32(digest[0:], d.h[0]) |
| 218 | + byteorder.BePutUint32(digest[4:], d.h[1]) |
| 219 | + byteorder.BePutUint32(digest[8:], d.h[2]) |
| 220 | + byteorder.BePutUint32(digest[12:], d.h[3]) |
| 221 | + byteorder.BePutUint32(digest[16:], d.h[4]) |
| 222 | + byteorder.BePutUint32(digest[20:], d.h[5]) |
| 223 | + byteorder.BePutUint32(digest[24:], d.h[6]) |
| 224 | + if !d.is224 { |
| 225 | + byteorder.BePutUint32(digest[28:], d.h[7]) |
| 226 | + } |
| 227 | + |
| 228 | + return digest |
| 229 | +} |
0 commit comments