118 lines
3.6 KiB
NASM
Raw Normal View History

2022-07-14 09:29:29 -07:00
2022-07-15 14:30:15 -07:00
// Precodition: input is in memory, starting at [TODO: fix] 0, of the form
2022-07-18 16:12:28 -07:00
// num_bytes, x[0], x[1], ..., x[(num_bytes+31)/32-1]
// Postcodition: output is in memory, starting at [TODO: fix] 0, of the form
2022-07-15 14:30:15 -07:00
// num_blocks, block0[0], block0[1], block1[0], ..., blocklast[1]
global sha2_pad:
2022-07-14 14:58:28 -07:00
// TODO: use kernel memory, and start address not at 0
push 0
mload
2022-07-18 16:12:28 -07:00
// stack: num_bytes
// STEP 1: append 1
// add 1 << (8*(32-k)-1) to x[num_bytes//32], where k := num_bytes%32
2022-07-15 14:30:15 -07:00
dup1
2022-07-18 16:12:28 -07:00
// stack: num_bytes, num_bytes
2022-07-15 14:30:15 -07:00
dup1
2022-07-18 16:12:28 -07:00
// stack: num_bytes, num_bytes, num_bytes
push 32
// stack: 32, num_bytes, num_bytes, num_bytes
swap1
// stack: num_bytes, 32, num_bytes, num_bytes
mod
// stack: k := num_bytes % 32, num_bytes, num_bytes
push 32
sub
// stack: 32 - k, num_bytes, num_bytes
push 8
2022-07-15 14:30:15 -07:00
mul
2022-07-18 16:12:28 -07:00
// stack: 8 * (32 - k), num_bytes, num_bytes
%decrement
// stack: 8 * (32 - k) - 1, num_bytes, num_bytes
push 1
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
shl
// stack: 1 << (8 * (32 - k) - 1), num_bytes, num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: num_bytes, 1 << (8 * (32 - k) - 1), num_bytes
push 32
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
div
// stack: num_bytes // 32, 1 << (8 * (32 - k) - 1), num_bytes
2022-07-15 14:30:15 -07:00
dup1
2022-07-18 16:12:28 -07:00
// stack: num_bytes // 32, num_bytes // 32, 1 << (8 * (32 - k) - 1), num_bytes
2022-07-15 14:30:15 -07:00
mload
2022-07-18 16:12:28 -07:00
// stack: x[num_bytes // 32], num_bytes // 32, 1 << (8 * (32 - k) - 1), num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: num_bytes // 32, x[num_bytes // 32], 1 << (8 * (32 - k) - 1), num_bytes
2022-07-15 14:30:15 -07:00
swap2
2022-07-18 16:12:28 -07:00
// stack: x[num_bytes // 32], 1 << (8 * (32 - k) - 1), num_bytes // 32, num_bytes
add
// stack: x[num_bytes // 32] + 1 << (8 * (32 - k) - 1), num_bytes // 32, num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: num_bytes // 32, x[num_bytes // 32] + 1 << (8 * (32 - k) - 1), num_bytes
2022-07-15 14:30:15 -07:00
mstore
2022-07-18 16:12:28 -07:00
// stack: num_bytes
// STEP 2: insert length
// (add length := num_bytes*8+1 to x[(num_bytes//64)*2-1])
dup1
dup1
// stack: num_bytes, num_bytes, num_bytes
push 8
2022-07-15 14:30:15 -07:00
mul
%increment
2022-07-18 16:12:28 -07:00
// stack: length := num_bytes*8+1, num_bytes, num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: num_bytes, length := num_bytes*8+1, num_bytes
push 64
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
div
// stack: num_bytes // 64, length := num_bytes*8+1, num_bytes
2022-07-15 14:30:15 -07:00
push 2
mul
2022-07-18 16:12:28 -07:00
%decrement
// stack: (num_bytes // 64) * 2 - 1, length := num_bytes*8+1, num_bytes
dup1
// stack: (num_bytes // 64) * 2 - 1, (num_bytes // 64) * 2 - 1, length, num_bytes
2022-07-14 14:58:28 -07:00
mload
2022-07-18 16:12:28 -07:00
// stack: x[(num_bytes // 64) * 2 - 1], (num_bytes // 64) * 2 - 1, length, num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: (num_bytes // 64) * 2 - 1, x[(num_bytes // 64) * 2 - 1], length, num_bytes
2022-07-15 14:30:15 -07:00
swap2
2022-07-18 16:12:28 -07:00
// stack: length, x[(num_bytes // 64) * 2 - 1], (num_bytes // 64) * 2 - 1, num_bytes
2022-07-15 14:30:15 -07:00
add
2022-07-18 16:12:28 -07:00
// stack: x[(num_bytes // 64) * 2 - 1] + length, (num_bytes // 64) * 2 - 1, num_bytes
2022-07-15 14:30:15 -07:00
swap1
2022-07-18 16:12:28 -07:00
// stack: (num_bytes // 64) * 2 - 1, x[(num_bytes // 64) * 2 - 1] + length, num_bytes
2022-07-15 14:30:15 -07:00
mstore
2022-07-18 16:12:28 -07:00
// stack: num_bytes
// STEP 3: insert num_blocks at start
push 64
swap
div
%increment
// stack: num_blocks := num_bytes // 64 + 1
2022-07-15 14:30:15 -07:00
push 0
2022-07-14 14:58:28 -07:00
mstore
2022-07-18 16:12:28 -07:00
// Precodition: stack contains address of one message block, followed by output address
// Postcondition: 64 addresses starting at given output address contain 32-bit chunks of message schedule
global sha2_gen_message_schedule_from_block:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-18 16:12:28 -07:00
// stack: block_addr, output_addr
2022-07-14 14:58:28 -07:00
mload
2022-07-18 16:12:28 -07:00
// stack: block, output_addr
push 16
// stack: counter=16, block, output_addr
global sha2_message_schedule_next_word:
2022-07-14 09:29:29 -07:00
JUMPDEST
2022-07-18 16:12:28 -07:00
// stack: address
global sha2_gen_message_schedules:
JUMPDEST