From 6776110f7856035124323f14e01b9b286e35200f Mon Sep 17 00:00:00 2001 From: bstnbuck <59334447+bstnbuck@users.noreply.github.com> Date: Mon, 22 Apr 2024 12:41:02 +0200 Subject: [PATCH] add RIPEMD-160 --- .github/workflows/linux.yml | 2 +- .github/workflows/win.yml | 2 +- README.md | 4 +- ripemd160/ripemd160.v | 162 +++++++++++++++++++++++++++++++++++ ripemd160/ripemd160_test.v | 49 +++++++++++ ripemd160/ripemd160block.v | 163 ++++++++++++++++++++++++++++++++++++ 6 files changed, 378 insertions(+), 4 deletions(-) create mode 100644 ripemd160/ripemd160.v create mode 100644 ripemd160/ripemd160_test.v create mode 100644 ripemd160/ripemd160block.v diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 0966f6b..d6d077c 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -18,4 +18,4 @@ jobs: cd v make - name: test algorithms - run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/ \ No newline at end of file + run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/ ripemd160/ \ No newline at end of file diff --git a/.github/workflows/win.yml b/.github/workflows/win.yml index c23ad3b..cd83821 100644 --- a/.github/workflows/win.yml +++ b/.github/workflows/win.yml @@ -18,4 +18,4 @@ jobs: cd v .\make.bat -tcc - name: test algorithms - run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/ \ No newline at end of file + run: ./v/v test md4/ tea/ xtea/ _cipher/ salsa20/ ripemd160/ \ No newline at end of file diff --git a/README.md b/README.md index 84db8fd..398f6b7 100644 --- a/README.md +++ b/README.md @@ -79,14 +79,14 @@ The V wrapper libsodium [[Git](https://github.com/vlang/libsodium)] has some of | HC-(128,256) | symmetric stream cipher | moderate | :x: | | Kyber(512,1024) | key encapsulation mechanism, post-quanten crypto | low | :x: | | MD4 | legacy hash-algorithm | low | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/md4)] | -| **RIPEMD160** | legacy hash-algorithm | moderate | :x: | +| **RIPEMD160** | legacy hash-algorithm | moderate | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/ripemd160)] | | **(X)Salsa20** | symmetric stream cipher | high | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/salsa20)] | | **scrypt** | hash-algorithm / key derivation function | high | :x: | | TEA, XTEA | legacy block cipher | low | experimental :yellow_circle: [[Git](https://github.com/bstnbuck/V-crypto/tree/main/tea)] [[Git](https://github.com/bstnbuck/V-crypto/tree/main/xtea)]| | **Twofisch** | symmetric block cipher | moderate | :x: | | **yescrypt** | hash-algorithm / key derivation function | high | :x: | -> Last Update: 20-04-2024 +> Last Update: 22-04-2024 --- ## v_crypto ### Installation diff --git a/ripemd160/ripemd160.v b/ripemd160/ripemd160.v new file mode 100644 index 0000000..5dba444 --- /dev/null +++ b/ripemd160/ripemd160.v @@ -0,0 +1,162 @@ +// Based on: https://github.com/golang/crypto/blob/master/ripemd160/ripemd160.go + +// Package ripemd160 implements the RIPEMD-160 hash algorithm. +// +// Deprecated: RIPEMD-160 is a legacy hash and should not be used for new +// applications. Instead, use a modern hash like SHA-256 (from crypto/sha256). +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) + +// vfmt off + +// Digest represents the partial evaluation of a checksum. +struct Digest { +mut: + s []u32 // running context + x []u8 // temporary buffer + nx int // index into x + tc u64 // total count of bytes processed +} + +// vfmt on + +// 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: ripemd160.block_size} + d.reset() +} + +// reset the state of the Digest `d` +pub fn (mut d Digest) reset() { + d.s[0] = u32(ripemd160._s0) + d.s[1] = u32(ripemd160._s1) + d.s[2] = u32(ripemd160._s2) + d.s[3] = u32(ripemd160._s3) + d.s[4] = u32(ripemd160._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 ripemd160.size +} + +// block_size returns the block size of the checksum in bytes. +pub fn (d &Digest) block_size() int { + return ripemd160.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 > ripemd160.block_size - d.nx { + n = ripemd160.block_size - d.nx + } + for i := 0; i < n; i++ { + d.x[d.nx + i] = p[i] + } + d.nx += n + if d.nx == ripemd160.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 { + // Make a copy of d0 so that caller can keep writing and summing. + mut d := d0.clone() + + // Padding. Add a 1 bit and 0 bits until 56 bytes mod 64. + 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: ripemd160.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 +} diff --git a/ripemd160/ripemd160_test.v b/ripemd160/ripemd160_test.v new file mode 100644 index 0000000..78b84cd --- /dev/null +++ b/ripemd160/ripemd160_test.v @@ -0,0 +1,49 @@ +module ripemd160 + +fn test_blocksize() { + mut d := new() + d.init() + assert d.block_size() == 64 +} + +fn test_size() { + mut d := new() + d.init() + assert d.size() == 20 +} + +fn test_ripemd160_hexhash() { + assert hexhash('') == '9c1185a5c5e9fc54612808977ee8f548b2258d31' + assert hexhash('message digest') == '5d0689ef49d2fae572b881b123a85ffa21595f36' +} + +fn test_ripemd160_full() { + mut d := new() + d.init() + d.write(''.bytes()) or { assert false } + assert d.sum([]).hex() == '9c1185a5c5e9fc54612808977ee8f548b2258d31' + + d.reset() + + d.write('a'.bytes()) or { assert false } + assert d.sum([]).hex() == '0bdc9d2d256b3ee9daae347be6f4dc835a467ffe' + + d.reset() + + d.write('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.bytes()) or { + assert false + } + assert d.sum([]).hex() == 'b0e20b6e3116640286ed3a87a5713079b21f5189' + + d.reset() + + d.write('12345678901234567890123456789012345678901234567890123456789012345678901234567890'.bytes()) or { + assert false + } + assert d.sum([]).hex() == '9b752e45573d4b39f4dbd3323cab82bf63326bfb' + + d.reset() + + d.write('Hello from v_crypto RIPEMD-160 implemented in pure V!'.bytes()) or { assert false } + assert d.sum([]).hex() == '0d33ed8a6a5dc2bdcab081cecadd2fad0cb35ed8' +} diff --git a/ripemd160/ripemd160block.v b/ripemd160/ripemd160block.v new file mode 100644 index 0000000..8ab960a --- /dev/null +++ b/ripemd160/ripemd160block.v @@ -0,0 +1,163 @@ +// Based on: https://github.com/golang/crypto/blob/master/ripemd160/ripemd160block.go + +// RIPEMD-160 block step. +module ripemd160 + +import math.bits + +@[direct_array_access] +fn block(mut md Digest, p0 []u8) int { + // vfmt off + // work buffer indices and roll amounts for one line + 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, + ] + + 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, + ] + + // same for the other parallel one + 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, + ] + + 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 + 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 + } + + // round 1 + 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++ + } + + // round 2 + 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++ + } + + // round 3 + 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++ + } + + // round 4 + 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++ + } + + // round 5 + 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++ + } + + // combine results + 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 +}