implement crypto.sha256 + some crypto cleanup

This commit is contained in:
joe-conigliaro 2019-07-18 18:50:05 +10:00 committed by Alexander Medvednikov
parent c0911ea74b
commit 43070412f7
8 changed files with 424 additions and 38 deletions

View file

@ -5,7 +5,7 @@
// Package sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256
// hash algorithms as defined in FIPS 180-4.
// Adaped from https://github.com/golang/go/tree/master/src/crypto/sha256
// Adaped from https://github.com/golang/go/tree/master/src/crypto/sha512
module sha512
@ -65,8 +65,6 @@ const (
// digest represents the partial evaluation of a checksum.
struct Digest {
// h [8]uint64
// x [chunk]byte
mut:
h []u64
x []byte
@ -123,8 +121,8 @@ mut:
// Note: when u64 const is working remove this and uncomment above
fn (d mut Digest) reset() {
d.h = [u64(0); 8]
d.x = [byte(0); Chunk]
d.h = [u64(0); 8]
d.x = [byte(0); Chunk]
switch d.function {
case crypto.Hash.SHA384:
d.h[0] = u64(0xcbbb9d5dc1059ed8)
@ -154,14 +152,14 @@ fn (d mut Digest) reset() {
d.h[6] = u64(0x2b0199fc2c85b8aa)
d.h[7] = u64(0x0eb72ddc81c52ca2)
default:
d.h[0] = u64(0x6a09e667f3bcc908)
d.h[1] = u64(0xbb67ae8584caa73b)
d.h[2] = u64(0x3c6ef372fe94f82b)
d.h[3] = u64(0xa54ff53a5f1d36f1)
d.h[4] = u64(0x510e527fade682d1)
d.h[5] = u64(0x9b05688c2b3e6c1f)
d.h[6] = u64(0x1f83d9abfb41bd6b)
d.h[7] = u64(0x5be0cd19137e2179)
d.h[0] = u64(0x6a09e667f3bcc908)
d.h[1] = u64(0xbb67ae8584caa73b)
d.h[2] = u64(0x3c6ef372fe94f82b)
d.h[3] = u64(0xa54ff53a5f1d36f1)
d.h[4] = u64(0x510e527fade682d1)
d.h[5] = u64(0x9b05688c2b3e6c1f)
d.h[6] = u64(0x1f83d9abfb41bd6b)
d.h[7] = u64(0x5be0cd19137e2179)
}
d.nx = 0
d.len = u64(0)
@ -173,22 +171,22 @@ fn _new(hash crypto.Hash) *Digest {
return d
}
// new returns a new hash.Hash computing the SHA-512 checksum.
// new returns a new Digest (implementing hash.Hash) computing the SHA-512 checksum.
pub fn new() *Digest {
return _new(crypto.Hash.SHA512)
}
// new512_224 returns a new hash.Hash computing the SHA-512/224 checksum.
// new512_224 returns a new Digest (implementing hash.Hash) computing the SHA-512/224 checksum.
fn new512_224() *Digest {
return _new(crypto.Hash.SHA512_224)
}
// new512_256 returns a new hash.Hash computing the SHA-512/256 checksum.
// new512_256 returns a new Digest (implementing hash.Hash) computing the SHA-512/256 checksum.
fn new512_256() *Digest {
return _new(crypto.Hash.SHA512_256)
}
// new384 returns a new hash.Hash computing the SHA-384 checksum.
// new384 returns a new Digest (implementing hash.Hash) computing the SHA-384 checksum.
fn new384() *Digest {
return _new(crypto.Hash.SHA384)
}
@ -237,22 +235,22 @@ fn (d mut Digest) sum(b_in mut []byte) []byte {
switch d0.function {
case crypto.Hash.SHA384:
for b in hash.left(Size384) {
b_in << b
}
b_in << b
}
case crypto.Hash.SHA512_224:
for b in hash.left(Size224) {
b_in << b
}
b_in << b
}
case crypto.Hash.SHA512_256:
for b in hash.left(Size256) {
b_in << b
}
b_in << b
}
default:
for b in hash {
b_in << b
}
b_in << b
}
}
return *b_in
return *b_in
}
fn (d mut Digest) checksum() []byte {
@ -261,8 +259,8 @@ fn (d mut Digest) checksum() []byte {
mut tmp := [byte(0); 128]
tmp[0] = 0x80
if int(len)%128 < 112 {
d.write(tmp.left(112-int(len)%128))
if int(len)%128 < 112 {
d.write(tmp.left(112-int(len)%128))
} else {
d.write(tmp.left(128+112-int(len)%128))
}
@ -271,7 +269,7 @@ fn (d mut Digest) checksum() []byte {
len <<= u64(3)
binary.big_endian_put_u64(tmp, u64(0)) // upper 64 bits are always zero, because len variable has type u64
binary.big_endian_put_u64(tmp.right(8), len)
binary.big_endian_put_u64(tmp.right(8), len)
d.write(tmp.left(16))
if d.nx != 0 {
@ -280,7 +278,7 @@ fn (d mut Digest) checksum() []byte {
mut digest := [byte(0); Size]
binary.big_endian_put_u64(digest, d.h[0])
binary.big_endian_put_u64(digest, d.h[0])
binary.big_endian_put_u64(digest.right(8), d.h[1])
binary.big_endian_put_u64(digest.right(16), d.h[2])
binary.big_endian_put_u64(digest.right(24), d.h[3])
@ -306,7 +304,7 @@ pub fn sum384(data []byte) []byte {
mut d := _new(crypto.Hash.SHA384)
d.write(data)
sum := d.checksum()
sum384 := sum.left(Size384)
sum384 := sum.left(Size384)
return sum384
}
@ -315,7 +313,7 @@ pub fn sum512_224(data []byte) []byte {
mut d := _new(crypto.Hash.SHA512_224)
d.write(data)
sum := d.checksum()
sum224 := sum.left(Size224)
sum224 := sum.left(Size224)
return sum224
}
@ -324,14 +322,14 @@ pub fn sum512_256(data []byte) []byte {
mut d := _new(crypto.Hash.SHA512_256)
d.write(data)
sum := d.checksum()
sum256 := sum.left(Size256)
sum256 := sum.left(Size256)
return sum256
}
fn block(dig &Digest, p []byte) {
// For now just use block_generic until we have specific
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(dig, p)
block_generic(dig, p)
}
pub fn (d &Digest) size() int {