107 lines
4.3 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:
2022-09-19 18:11:07 -07:00
/// stack: STATE, count, length, virt
/// where virt is the virtual address of the bytes argument
2022-09-18 11:05:00 -07:00
2022-09-19 19:04:22 -07:00
global ripemd_alt:
// stack: length, INPUT
%stack (length) -> (64, length, 0x80, 63, length, length)
2022-09-21 13:42:13 -07:00
// stack: 64, length, 0x80, 63, length, length, INPUT
2022-09-19 19:04:22 -07:00
%jump(ripemd_storage) // stores the following into memory
// init _buffer at virt 0 [consumes 64]
// store _size at virt 64 [consumes length]
// store _padding at virt 72 [consumes 0x80, 63]
// store _input at virt 136 [consumes length]
2022-09-18 10:20:25 -07:00
global ripemd:
2022-09-21 13:42:13 -07:00
// stack: ADDR, length
%stack (ADDR: 3, length) -> (64, length, 0x80, 63, ADDR, length, length)
// stack: 64, length, 0x80, 63, ADDR, length, length
2022-09-19 12:09:57 -07:00
%jump(ripemd_storage) // stores the following into memory
2022-09-19 18:11:07 -07:00
// init _buffer at virt 0 [consumes 64]
// store _size at virt 64 [consumes length]
// store _padding at virt 72 [consumes 0x80, 63]
// store _input at virt 136 [consumes ADDR, length]
2022-09-21 13:42:13 -07:00
2022-09-20 10:29:43 -07:00
global ripemd_init:
2022-09-18 10:20:25 -07:00
// stack: length
2022-09-19 18:11:07 -07:00
%stack (length) -> ( 0, length, 136, ripemd_1, ripemd_2, process)
// stack: count = 0, length, virt = 136, ripemd_1, ripemd_2, process
2022-09-21 13:42:13 -07:00
%stack (ARGS: 3, LABELS: 3) -> (0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, ARGS, LABELS)
// stack: 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0, count, length, virt, LABELS
2022-09-18 10:20:25 -07:00
%jump(ripemd_update)
ripemd_1:
2022-09-21 13:42:13 -07:00
// stack: STATE, count, length , virt , LABELS
2022-09-18 11:05:00 -07:00
DUP7
2022-09-21 13:42:13 -07:00
// stack: length, STATE, count, length , virt , LABELS
2022-09-18 10:20:25 -07:00
%padlength
2022-09-21 13:42:13 -07:00
// stack: padlength, STATE, count, length , virt , LABELS
2022-09-18 10:20:25 -07:00
SWAP7
POP
2022-09-21 13:42:13 -07:00
// stack: STATE, count, length = padlength, virt , LABELS
%stack (STATE: 5, count, length, virt) -> (STATE, count, length, 72)
// STATE, count, length , virt = 72, LABELS
2022-09-18 10:20:25 -07:00
%jump(ripemd_update)
ripemd_2:
2022-09-21 13:42:13 -07:00
// stack: STATE, count, length , virt , LABELS
%stack (STATE: 5, count, length, virt) -> (STATE, count, 8, 64)
// stack: STATE, count, length = 8, virt = 64, LABELS
2022-09-18 10:20:25 -07:00
%jump(ripemd_update)
2022-09-16 10:12:21 -07:00
process:
2022-09-19 18:11:07 -07:00
// stack: a , b, c, d, e, count, length, virt
2022-09-21 20:03:11 -07:00
%reverse_bytes_u32
2022-09-21 16:40:27 -07:00
// stack: a', b, c, d, e, VARS
2022-09-16 10:12:21 -07:00
SWAP1
2022-09-21 20:03:11 -07:00
%reverse_bytes_u32
2022-09-16 10:12:21 -07:00
%shl_const(32)
OR
2022-09-21 16:40:27 -07:00
// stack: b' a', c, d, e, VARS
2022-09-16 10:12:21 -07:00
SWAP1
2022-09-21 20:03:11 -07:00
%reverse_bytes_u32
2022-09-16 10:12:21 -07:00
%shl_const(64)
OR
2022-09-21 16:40:27 -07:00
// stack: c' b' a', d, e, VARS
2022-09-16 10:12:21 -07:00
SWAP1
2022-09-21 20:03:11 -07:00
%reverse_bytes_u32
2022-09-16 10:12:21 -07:00
%shl_const(96)
OR
2022-09-21 16:40:27 -07:00
// stack: d' c' b' a', e, VARS
2022-09-16 10:12:21 -07:00
SWAP1
2022-09-21 20:03:11 -07:00
%reverse_bytes_u32
2022-09-16 10:12:21 -07:00
%shl_const(96)
OR
2022-09-21 16:40:27 -07:00
// stack: e' d' c' b' a', VARS
2022-09-22 09:27:11 -07:00
%stack (result, VARS: 3) -> (0xdeadbeef, result)
2022-09-21 16:40:27 -07:00
// stack: 0xdeadbeef, result
2022-09-22 09:27:11 -07:00
JUMP
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