2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-07-16 14:20:51 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-07-17 11:00:15 +02:00
|
|
|
// This is the generic version with no architecture optimizations.
|
|
|
|
// In its own file so that an architecture
|
|
|
|
// optimized verision can be substituted
|
2019-07-16 14:20:51 +02:00
|
|
|
|
2019-07-15 17:49:01 +02:00
|
|
|
module sha1
|
|
|
|
|
|
|
|
import math.bits
|
|
|
|
|
|
|
|
const (
|
2019-10-24 13:48:20 +02:00
|
|
|
_k0 = 0x5A827999
|
|
|
|
_k1 = 0x6ED9EBA1
|
|
|
|
_k2 = 0x8F1BBCDC
|
|
|
|
_k3 = 0xCA62C1D6
|
2019-07-15 17:49:01 +02:00
|
|
|
)
|
|
|
|
|
2019-08-07 13:37:07 +02:00
|
|
|
fn block_generic(dig mut Digest, p_ []byte) {
|
|
|
|
mut p := p_
|
2019-09-15 11:26:05 +02:00
|
|
|
mut w := [u32(0)].repeat(16)
|
2019-07-15 17:49:01 +02:00
|
|
|
mut h0 := dig.h[0]
|
|
|
|
mut h1 := dig.h[1]
|
|
|
|
mut h2 := dig.h[2]
|
|
|
|
mut h3 := dig.h[3]
|
|
|
|
mut h4 := dig.h[4]
|
2019-10-24 13:48:20 +02:00
|
|
|
for p.len >= chunk {
|
2019-07-15 17:49:01 +02:00
|
|
|
// Can interlace the computation of w with the
|
|
|
|
// rounds below if needed for speed.
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in 0..16 {
|
2019-07-15 17:49:01 +02:00
|
|
|
j := i * 4
|
2019-09-26 13:57:31 +02:00
|
|
|
w[i] = u32(p[j]<<24) | u32(p[j+1]<<16) | u32(p[j+2]<<8) | u32(p[j+3])
|
2019-07-15 17:49:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mut a := h0
|
|
|
|
mut b := h1
|
|
|
|
mut c := h2
|
|
|
|
mut d := h3
|
|
|
|
mut e := h4
|
|
|
|
|
|
|
|
// Each of the four 20-iteration rounds
|
|
|
|
// differs only in the computation of f and
|
2019-10-24 13:48:20 +02:00
|
|
|
// the choice of K (_k0, _k1, etc).
|
2019-07-15 17:49:01 +02:00
|
|
|
mut i := 0
|
|
|
|
for i < 16 {
|
2019-09-26 13:57:31 +02:00
|
|
|
f := b&c | (~b)&d
|
2019-10-24 13:48:20 +02:00
|
|
|
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k0)
|
2019-07-15 17:49:01 +02:00
|
|
|
e = d
|
|
|
|
d = c
|
|
|
|
c = bits.rotate_left_32(b, 30)
|
|
|
|
b = a
|
|
|
|
a = t
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for i < 20 {
|
|
|
|
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
2020-01-23 22:49:13 +01:00
|
|
|
w[i&0xf] = tmp<<1 | (tmp>>(32-1))
|
2019-07-15 17:49:01 +02:00
|
|
|
f := b&c | (~b)&d
|
2019-10-24 13:48:20 +02:00
|
|
|
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k0)
|
2019-07-15 17:49:01 +02:00
|
|
|
e = d
|
|
|
|
d = c
|
|
|
|
c = bits.rotate_left_32(b, 30)
|
|
|
|
b = a
|
|
|
|
a = t
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for i < 40 {
|
|
|
|
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
2020-01-23 22:49:13 +01:00
|
|
|
w[i&0xf] = tmp<<1 | (tmp>>(32-1))
|
2019-07-15 17:49:01 +02:00
|
|
|
f := b ^ c ^ d
|
2019-10-24 13:48:20 +02:00
|
|
|
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k1)
|
2019-07-15 17:49:01 +02:00
|
|
|
e = d
|
|
|
|
d = c
|
|
|
|
c = bits.rotate_left_32(b, 30)
|
|
|
|
b = a
|
|
|
|
a = t
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for i < 60 {
|
|
|
|
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
2020-01-23 22:49:13 +01:00
|
|
|
w[i&0xf] = tmp<<1 | (tmp>>(32-1))
|
2019-07-15 17:49:01 +02:00
|
|
|
f := ((b | c) & d) | (b & c)
|
2019-10-24 13:48:20 +02:00
|
|
|
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k2)
|
2019-07-15 17:49:01 +02:00
|
|
|
e = d
|
|
|
|
d = c
|
|
|
|
c = bits.rotate_left_32(b, 30)
|
|
|
|
b = a
|
|
|
|
a = t
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for i < 80 {
|
|
|
|
tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[(i)&0xf]
|
2020-01-23 22:49:13 +01:00
|
|
|
w[i&0xf] = tmp<<1 | (tmp>>(32-1))
|
2019-07-15 17:49:01 +02:00
|
|
|
f := b ^ c ^ d
|
2019-10-24 13:48:20 +02:00
|
|
|
t := bits.rotate_left_32(a, 5) + f + e + w[i&0xf] + u32(_k3)
|
2019-07-15 17:49:01 +02:00
|
|
|
e = d
|
|
|
|
d = c
|
|
|
|
c = bits.rotate_left_32(b, 30)
|
|
|
|
b = a
|
|
|
|
a = t
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
h0 += a
|
|
|
|
h1 += b
|
|
|
|
h2 += c
|
|
|
|
h3 += d
|
|
|
|
h4 += e
|
|
|
|
|
2019-10-24 13:48:20 +02:00
|
|
|
if chunk >= p.len {
|
2019-11-14 08:00:22 +01:00
|
|
|
p = []
|
2019-07-15 17:49:01 +02:00
|
|
|
} else {
|
2019-10-27 08:03:15 +01:00
|
|
|
p = p[chunk..]
|
2019-07-15 17:49:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dig.h[0] = h0
|
|
|
|
dig.h[1] = h1
|
|
|
|
dig.h[2] = h2
|
|
|
|
dig.h[3] = h3
|
|
|
|
dig.h[4] = h4
|
|
|
|
}
|