94 lines
3.5 KiB
NASM
Raw Normal View History

2022-09-18 11:05:00 -07:00
/// Variables beginning with _ are in memory
2022-09-18 10:20:25 -07:00
///
/// def ripemd160(_input):
/// state, count, _buffer = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0], 0, [0]*64
2022-09-19 12:09:57 -07:00
/// state, count, _buffer = ripemd_update(state, count, _buffer, len(input) , bytes = _input )
/// state, count, _buffer = ripemd_update(state, count, _buffer, padlength(len(input)), bytes = [0x80]+[0]*63)
/// state, count, _buffer = ripemd_update(state, count, _buffer, 8, bytes = size(len(_input)))
2022-09-18 10:20:25 -07:00
/// return process(state)
2022-09-19 12:09:57 -07:00
///
2022-09-18 11:05:00 -07:00
/// ripemd is called on a stack with ADDR and length
/// ripemd_update will receive and return the stack in the form:
/// stack: *state, count, length, offset
2022-09-19 12:09:57 -07:00
/// where offset is the virtual address of the bytes argument
2022-09-18 11:05:00 -07:00
2022-09-18 10:20:25 -07:00
global ripemd:
2022-09-18 11:05:00 -07:00
// stack: ADDR, length
2022-09-19 12:09:57 -07:00
$stack (a, b, c, length) -> (64, length, 0x80, 63, a, b, c, length, length)
// stack: 64, length, 0x80, 63, a, b, c, length, length
%jump(ripemd_storage) // stores the following into memory
// init _buffer at offset 0 [consumes 64]
// store _size at offset 64 [consumes length]
// store _padding at offset 72 [consumes 0x80, 63]
// store _input at offset 136 [consumes ADDR, length]
ripemd_init:
2022-09-18 10:20:25 -07:00
// stack: length
2022-09-18 11:05:00 -07:00
%stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process)
// stack: count = 0, length, offset = 136, ripemd_1, ripemd_2, process
2022-09-18 10:20:25 -07:00
%stack (c, l, o, l1, l2, l3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, c, l, o, l1, l2, l3)
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, offset, *labels
%jump(ripemd_update)
ripemd_1:
2022-09-18 11:05:00 -07:00
// stack: *state, count, length , offset, *labels
DUP7
// stack: length, *state, count, length , offset, *labels
2022-09-18 10:20:25 -07:00
%padlength
2022-09-18 11:05:00 -07:00
// stack: padlength, *state, count, length , offset, *labels
2022-09-18 10:20:25 -07:00
SWAP7
POP
2022-09-18 11:05:00 -07:00
// stack: *state, count, length = padlength, offset, *labels
%stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, length, 72)
2022-09-18 10:20:25 -07:00
%jump(ripemd_update)
ripemd_2:
// stack: *state, count, length, offset, *labels
2022-09-18 11:05:00 -07:00
%stack (a, b, c, d, e, count, length, offset) -> (a, b, c, d, e, count, 8, 64)
2022-09-18 10:20:25 -07:00
// stack: *state, count, length, offset, *labels
%jump(ripemd_update)
2022-09-16 10:12:21 -07:00
process:
2022-09-18 10:20:25 -07:00
// stack: a , b, c, d, e, count, length, offset
2022-09-16 10:12:21 -07:00
%flip_bytes_u32
2022-09-18 10:20:25 -07:00
// stack: a', b, c, d, e, *vars
2022-09-16 10:12:21 -07:00
SWAP1
%flip_bytes_32
%shl_const(32)
OR
2022-09-18 10:20:25 -07:00
// stack: b' a', c, d, e, *vars
2022-09-16 10:12:21 -07:00
SWAP1
%flip_bytes_32
%shl_const(64)
OR
2022-09-18 10:20:25 -07:00
// stack: c' b' a', d, e, *vars
2022-09-16 10:12:21 -07:00
SWAP1
%flip_bytes_32
%shl_const(96)
OR
2022-09-18 10:20:25 -07:00
// stack: d' c' b' a', e, *vars
2022-09-16 10:12:21 -07:00
SWAP1
%flip_bytes_32
%shl_const(96)
OR
2022-09-18 10:20:25 -07:00
// stack: e' d' c' b' a', *vars
%stack (result, x, y, z) -> result
// stack: result
2022-09-16 19:18:26 -07:00
2022-09-18 11:05:00 -07:00
/// def padlength(length):
/// x = 56 - length % 64
2022-09-16 19:18:26 -07:00
/// return x + 64*(x < 9)
%macro padlength
2022-09-18 11:05:00 -07:00
// stack: count
2022-09-16 19:18:26 -07:00
%mod_const(64)
2022-09-18 11:05:00 -07:00
// stack: count % 64
2022-09-16 19:18:26 -07:00
PUSH 56
SUB
2022-09-18 11:05:00 -07:00
// stack: x = 56 - count % 64
2022-09-16 19:18:26 -07:00
DUP1
%lt_const(9)
// stack: x < 9 , x
%mul_const(64)
// stack: 64*(x < 9) , x
ADD
// stack: 64*(x < 9) + x
2022-09-18 10:20:25 -07:00
%endmacro