crypto: add a crypto.ripemd160 module (#24119)

This commit is contained in:
Roman 2025-04-05 23:05:38 +07:00 committed by GitHub
parent 5211debe75
commit 52b5c3204e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 354 additions and 0 deletions

View file

@ -0,0 +1,150 @@
// Based on: https://github.com/golang/crypto/blob/master/ripemd160/ripemd160.go
module ripemd160
// RIPEMD-160 is designed by Hans Dobbertin, Antoon Bosselaers, and Bart
// Preneel with specifications available at:
// http://homes.esat.kuleuven.be/~cosicart/pdf/AB-9601/AB-9601.pdf.
// The size of the checksum in bytes.
const size = 20
// The block size of the hash algorithm in bytes.
const block_size = 64
const _s0 = u32(0x67452301)
const _s1 = u32(0xefcdab89)
const _s2 = u32(0x98badcfe)
const _s3 = u32(0x10325476)
const _s4 = u32(0xc3d2e1f0)
// Digest represents the partial evaluation of a checksum.
struct Digest {
mut:
s []u32
x []u8
nx int
tc u64
}
// free the resources taken by the Digest `d`
@[unsafe]
pub fn (mut d Digest) free() {
$if prealloc {
return
}
unsafe { d.x.free() }
}
fn (mut d Digest) init() {
d.s = []u32{len: 5}
d.x = []u8{len: block_size}
d.reset()
}
// reset the state of the Digest `d`
pub fn (mut d Digest) reset() {
d.s[0] = u32(_s0)
d.s[1] = u32(_s1)
d.s[2] = u32(_s2)
d.s[3] = u32(_s3)
d.s[4] = u32(_s4)
d.nx = 0
d.tc = 0
}
fn (d &Digest) clone() &Digest {
return &Digest{
...d
s: d.s.clone()
x: d.x.clone()
}
}
// new returns a new Digest (implementing hash.Hash) computing the MD5 checksum.
pub fn new() &Digest {
mut d := &Digest{}
d.init()
return d
}
// size returns the size of the checksum in bytes.
pub fn (d &Digest) size() int {
return size
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
}
// hexhash returns a hexadecimal RIPEMD-160 hash sum `string` of `s`.
pub fn hexhash(s string) string {
mut d := new()
d.init()
d.write(s.bytes()) or { panic(err) }
return d.sum([]).hex()
}
// write writes the contents of `p_` to the internal hash representation.
pub fn (mut d Digest) write(p_ []u8) !int {
unsafe {
mut p := p_
mut nn := p.len
d.tc += u64(nn)
if d.nx > 0 {
mut n := p.len
if n > block_size - d.nx {
n = block_size - d.nx
}
for i := 0; i < n; i++ {
d.x[d.nx + i] = p[i]
}
d.nx += n
if d.nx == block_size {
block(mut d, d.x[0..])
d.nx = 0
}
p = p[n..]
}
n := block(mut d, p)
p = p[n..]
if p.len > 0 {
d.nx = copy(mut d.x[..], p)
}
return nn
}
}
// sum returns the RIPEMD-160 sum of the bytes in `inp`.
pub fn (d0 &Digest) sum(inp []u8) []u8 {
mut d := d0.clone()
mut tc := d.tc
mut tmp := []u8{len: 64}
tmp[0] = 0x80
if tc % 64 < 56 {
d.write(tmp[0..56 - tc % 64]) or { panic(err) }
} else {
d.write(tmp[0..64 + 56 - tc % 64]) or { panic(err) }
}
// Length in bits.
tc <<= 3
for i := u16(0); i < 8; i++ {
tmp[i] = u8(tc >> (8 * i))
}
d.write(tmp[0..8]) or { panic(err) }
if d.nx != 0 {
panic('v_crypto/ripemd160: d.nx != 0: ${d.nx}')
}
mut digest := []u8{len: size}
for i, s in d.s {
digest[i * 4] = u8(s)
digest[i * 4 + 1] = u8(s >> 8)
digest[i * 4 + 2] = u8(s >> 16)
digest[i * 4 + 3] = u8(s >> 24)
}
return digest
}

View file

@ -0,0 +1,49 @@
import crypto.ripemd160
struct MdTest {
o string
i string
}
const vectors = [
MdTest{'9c1185a5c5e9fc54612808977ee8f548b2258d31', ''},
MdTest{'0bdc9d2d256b3ee9daae347be6f4dc835a467ffe', 'a'},
MdTest{'8eb208f7e05d987a9b044a8e98c6b087f15a0bfc', 'abc'},
MdTest{'5d0689ef49d2fae572b881b123a85ffa21595f36', 'message digest'},
MdTest{'f71c27109c692c1b56bbdceb5b9d2865b3708dbc', 'abcdefghijklmnopqrstuvwxyz'},
MdTest{'12a053384a9c0c88e405a06c27dcf49ada62eb2b', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'},
MdTest{'b0e20b6e3116640286ed3a87a5713079b21f5189', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'},
MdTest{'9b752e45573d4b39f4dbd3323cab82bf63326bfb', '12345678901234567890123456789012345678901234567890123456789012345678901234567890'},
]
fn test_vectors() {
for tv in vectors {
mut md := ripemd160.new()
for j := 0; j < 3; j++ {
if j < 2 {
md.write(tv.i.bytes()) or { panic(err) }
} else {
half := tv.i.len / 2
md.write(tv.i.bytes()[0..tv.i.len / 2]) or { panic(err) }
md.sum([])
md.write(tv.i.bytes()[tv.i.len / 2..]) or { panic(err) }
}
assert md.sum([]).hex() == tv.o
md.reset()
}
}
}
fn million_a() string {
mut md := ripemd160.new()
for _ in 0 .. 100000 {
md.write('aaaaaaaaaa'.bytes()) or { panic(err) }
}
return md.sum([]).hex()
}
fn test_million_a() {
out := '52783243c1697bdbe16d37f97f68f08325dc1528'
s := million_a()
assert s == out
}

View file

@ -0,0 +1,155 @@
// Based on: https://github.com/golang/crypto/blob/master/ripemd160/ripemd160block.go
// RIPEMD-160 block step.
module ripemd160
import math.bits
// vfmt off
const n__ = [
u32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13,
]
const r__ = [
u32(11), 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6,
]
const n_ = [
u32(5), 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11,
]
const r_ = [
u32(8), 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11,
]
// vfmt on
@[direct_array_access]
fn block(mut md Digest, p0 []u8) int {
mut p := p0.clone()
mut n := 0
mut x := []u32{len: 16}
mut alpha := u32(0)
mut beta := u32(0)
for p.len >= block_size {
mut a, mut b, mut c, mut d, mut e := md.s[0], md.s[1], md.s[2], md.s[3], md.s[4]
mut aa, mut bb, mut cc, mut dd, mut ee := a, b, c, d, e
mut j := 0
for i := 0; i < 16; i++ {
x[i] = u32(p[j]) | u32(p[j + 1]) << 8 | u32(p[j + 2]) << 16 | u32(p[j + 3]) << 24
j += 4
}
mut i := 0
for i < 16 {
alpha = a + (b ^ c ^ d) + x[n__[i]]
mut s := int(r__[i])
alpha = bits.rotate_left_32(alpha, s) + e
beta = bits.rotate_left_32(c, 10)
a, b, c, d, e = e, alpha, b, beta, d
alpha = aa + (bb ^ (cc | ~dd)) + x[n_[i]] + 0x50a28be6
s = int(r_[i])
alpha = bits.rotate_left_32(alpha, s) + ee
beta = bits.rotate_left_32(cc, 10)
aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
i++
}
for i < 32 {
alpha = a + (b & c | ~b & d) + x[n__[i]] + 0x5a827999
mut s := int(r__[i])
alpha = bits.rotate_left_32(alpha, s) + e
beta = bits.rotate_left_32(c, 10)
a, b, c, d, e = e, alpha, b, beta, d
// parallel line
alpha = aa + (bb & dd | cc & ~dd) + x[n_[i]] + 0x5c4dd124
s = int(r_[i])
alpha = bits.rotate_left_32(alpha, s) + ee
beta = bits.rotate_left_32(cc, 10)
aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
i++
}
for i < 48 {
alpha = a + (b | ~c ^ d) + x[n__[i]] + 0x6ed9eba1
mut s := int(r__[i])
alpha = bits.rotate_left_32(alpha, s) + e
beta = bits.rotate_left_32(c, 10)
a, b, c, d, e = e, alpha, b, beta, d
// parallel line
alpha = aa + (bb | ~cc ^ dd) + x[n_[i]] + 0x6d703ef3
s = int(r_[i])
alpha = bits.rotate_left_32(alpha, s) + ee
beta = bits.rotate_left_32(cc, 10)
aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
i++
}
for i < 64 {
alpha = a + (b & d | c & ~d) + x[n__[i]] + 0x8f1bbcdc
mut s := int(r__[i])
alpha = bits.rotate_left_32(alpha, s) + e
beta = bits.rotate_left_32(c, 10)
a, b, c, d, e = e, alpha, b, beta, d
// parallel line
alpha = aa + (bb & cc | ~bb & dd) + x[n_[i]] + 0x7a6d76e9
s = int(r_[i])
alpha = bits.rotate_left_32(alpha, s) + ee
beta = bits.rotate_left_32(cc, 10)
aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
i++
}
for i < 80 {
alpha = a + (b ^ (c | ~d)) + x[n__[i]] + 0xa953fd4e
mut s := int(r__[i])
alpha = bits.rotate_left_32(alpha, s) + e
beta = bits.rotate_left_32(c, 10)
a, b, c, d, e = e, alpha, b, beta, d
// parallel line
alpha = aa + (bb ^ cc ^ dd) + x[n_[i]]
s = int(r_[i])
alpha = bits.rotate_left_32(alpha, s) + ee
beta = bits.rotate_left_32(cc, 10)
aa, bb, cc, dd, ee = ee, alpha, bb, beta, dd
i++
}
dd += c + md.s[1]
md.s[1] = md.s[2] + d + ee
md.s[2] = md.s[3] + e + aa
md.s[3] = md.s[4] + a + bb
md.s[4] = md.s[0] + b + cc
md.s[0] = dd
p = p[block_size..].clone()
n += block_size
}
return n
}