a2audit/shasum/shasum.go
Zellyn Hunter 54710c10a0 shasum: Longer tests, slightly shorter code
Added tests for 0x1ff- and 0x200-length inputs. Also changed code that
sets k constants to use constant values, rather than copying, to save
a few bytes.
2016-10-10 20:54:50 -04:00

212 lines
5.8 KiB
Go

package main
import (
"crypto/sha1"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
)
// 0xfe00 - 0xffff
var rom = []byte{
0xC6, 0x34, 0xF0, 0x9F, 0xCA, 0xD0, 0x16, 0xC9,
0xBA, 0xD0, 0xBB, 0x85, 0x31, 0xA5, 0x3E, 0x91,
0x40, 0xE6, 0x40, 0xD0, 0x02, 0xE6, 0x41, 0x60,
0xA4, 0x34, 0xB9, 0xFF, 0x01, 0x85, 0x31, 0x60,
0xA2, 0x01, 0xB5, 0x3E, 0x95, 0x42, 0x95, 0x44,
0xCA, 0x10, 0xF7, 0x60, 0xB1, 0x3C, 0x91, 0x42,
0x20, 0xB4, 0xFC, 0x90, 0xF7, 0x60, 0xB1, 0x3C,
0xD1, 0x42, 0xF0, 0x1C, 0x20, 0x92, 0xFD, 0xB1,
0x3C, 0x20, 0xDA, 0xFD, 0xA9, 0xA0, 0x20, 0xED,
0xFD, 0xA9, 0xA8, 0x20, 0xED, 0xFD, 0xB1, 0x42,
0x20, 0xDA, 0xFD, 0xA9, 0xA9, 0x20, 0xED, 0xFD,
0x20, 0xB4, 0xFC, 0x90, 0xD9, 0x60, 0x20, 0x75,
0xFE, 0xA9, 0x14, 0x48, 0x20, 0xD0, 0xF8, 0x20,
0x53, 0xF9, 0x85, 0x3A, 0x84, 0x3B, 0x68, 0x38,
0xE9, 0x01, 0xD0, 0xEF, 0x60, 0x8A, 0xF0, 0x07,
0xB5, 0x3C, 0x95, 0x3A, 0xCA, 0x10, 0xF9, 0x60,
0xA0, 0x3F, 0xD0, 0x02, 0xA0, 0xFF, 0x84, 0x32,
0x60, 0xA9, 0x00, 0x85, 0x3E, 0xA2, 0x38, 0xA0,
0x1B, 0xD0, 0x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s(tobytes(expand(pad(rom[:0x37]))))
fmt.Printf("%X\n", sha1.Sum(rom[:0]))
// fmt.Printf("%X\n", shasum(rom[:0]))
// fmt.Printf("%X\n", sha1.Sum(rom[:0x37]))
fmt.Printf("%X\n", shasum(rom[:0x37]))
fmt.Printf("%X\n", sha1.Sum(rom[:0x100]))
// fmt.Printf("%X\n", shasum(rom[:0x100]))
// fmt.Printf("%X\n", sha1.Sum(rom[:0x1ff]))
fmt.Printf("%X\n", shasum(rom[:0x1ff]))
// fmt.Printf("%X\n", sha1.Sum(rom[:0x200]))
fmt.Printf("%X\n", shasum(rom[:0x200]))
}
func print40s(in []byte) {
for i := 0; i < len(in); i += 20 {
end := i + 20
if end > len(in) {
end = len(in)
}
fmt.Println(strings.ToUpper(hex.EncodeToString(in[i:end])))
}
fmt.Printf("length: %d 0x%x\n", len(in), len(in))
}
func pad(in []byte) []byte {
out := make([]byte, len(in))
copy(out, in)
out = append(out, 0x80)
for len(out)%64 != 56 {
out = append(out, 0)
}
l := uint64(len(in)) << 3
for i := uint(0); i < 8; i++ {
out = append(out, byte(l>>(56-8*i)))
}
return out
}
func tobytes(in []uint32) []byte {
out := make([]byte, len(in)*4)
for i, w := range in {
binary.BigEndian.PutUint32(out[i*4:], w)
}
return out
}
func expand(in []byte) []uint32 {
if len(in) != 64 {
panic(fmt.Sprintf("expand() expects 64-byte slices as input; got %d", len(in)))
}
var w [80]uint32
for i := 0; i < 16; i++ {
w[i] = binary.BigEndian.Uint32(in[i*4:])
}
for i := 16; i < 80; i++ {
w[i] = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]
w[i] = w[i]<<1 + w[i]>>31
}
return w[:]
}
func shasum(in []byte) [20]byte {
h0 := uint32(0x67452301)
h1 := uint32(0xEFCDAB89)
h2 := uint32(0x98BADCFE)
h3 := uint32(0x10325476)
h4 := uint32(0xC3D2E1F0)
in2 := pad(in)
_ = in2
_ = h0
_ = h1
_ = h2
_ = h3
_ = h4
for j := 0; j < len(in2); j += 64 {
w := expand(in2[j : j+64])
if len(w) != 80 {
panic(fmt.Sprintf("expand should return 80 words; got %d", len(w)))
}
a := h0
b := h1
c := h2
d := h3
e := h4
var f, k uint32
for i, wi := range w {
switch {
case i <= 19:
f = d ^ (b & (c ^ d))
k = 0x5A827999
case i <= 39:
f = b ^ c ^ d
k = 0x6ED9EBA1
case i <= 59:
f = (b & c) | (d & (b | c))
k = 0x8F1BBCDC
default:
f = b ^ c ^ d
k = 0xCA62C1D6
}
temp := ((a << 5) | (a >> 27)) + f + e + k + wi
e = d
d = c
c = (b << 30) | (b >> 2)
b = a
a = temp
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
}
var result [20]byte
binary.BigEndian.PutUint32(result[0:], h0)
binary.BigEndian.PutUint32(result[4:], h1)
binary.BigEndian.PutUint32(result[8:], h2)
binary.BigEndian.PutUint32(result[12:], h3)
binary.BigEndian.PutUint32(result[16:], h4)
return result
}