li t0,0x20 csrs mstatush,t0 # Big-endian here we go! li a7,4 la a0,msgentermessage ecall # Prompt the user to type a message li a7,8 la a0,message li a1,17 ecall # Read the message li a7,4 la a0,message ecall # Print it back out li a7,4 la a0,msgwait ecall # Tell the user this might take a while... # Count the message length and replace the null terminator with 0x80 la t0,message li t1,0 1: lb t2,0(t0) addi t0,t0,1 addi t1,t1,1 bnez t2,1b addi t0,t0,-1 addi t1,t1,-1 li t2,0x80 sb t2,0(t0) la t0,message+60 slli t1,t1,3 sw t1,0(t0) li s0,0 # Round counter # Copy message into w (full message since we're only doing the one chunk) li t2,16 la t0,message la t1,w 1: ld t3,0(t0) # Copy 2 words of message... sd t3,0(t1) # ...to w addi t0,t0,8 addi t1,t1,8 addi s0,s0,2 blt s0,t2,1b # Wikipedia calls this "Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array" 1: la t0,w sh2add t0,s0,t0 addi t0,t0,-60 # i-15 la t1,w sh2add t1,s0,t1 addi t1,t1,-8 # i-2 la t2,w sh2add t2,s0,t2 addi t2,t2,-64 # i-16 la t3,w sh2add t3,s0,t3 addi t3,t3,-28 # i-7 lw t0,0(t0) lw t1,0(t1) lw t2,0(t2) lw t3,0(t3) sha256sig0 t0,t0 sha256sig1 t1,t1 add t0,t0,t1 add t0,t0,t2 add t0,t0,t3 la t1,w sh2add t1,s0,t1 sw t0,0(t1) addi s0,s0,1 li t0,64 blt s0,t0,1b # Load initial working values la t0,h lw a0,0(t0) lw a1,4(t0) lw a2,8(t0) lw a3,12(t0) lw a4,16(t0) lw a5,20(t0) lw a6,24(t0) lw a7,28(t0) li s0,0 # Wikipedia calls this "Compression function main loop" # S1 1: sha256sum1 t0,a4 # ch and t1,a4,a5 # e AND f andn t2,a6,a4 # g AND (NOT e) - also hey, turns out this instruction is actually useful for something xor t1,t1,t2 # (e AND f) XOR (g AND (NOT e)) # temp1 add t0,t0,t1 # S1 + ch add t0,t0,a7 # + h la t1,k sh2add t1,s0,t1 lw t1,0(t1) add t0,t0,t1 # + k[i] la t1,w sh2add t1,s0,t1 lw t1,0(t1) add t0,t0,t1 # + w[i] # S0 sha256sum0 t1,a0 # maj and t2,a0,a1 # a AND b and t3,a0,a2 # a AND c and t4,a1,a2 # b AND c xor t2,t2,t3 # (a AND b) XOR (a AND c) xor t2,t2,t4 # (a AND b) XOR (a AND c) XOR (b AND c) # temp2 add t1,t1,t2 # S0 + maj mv a7,a6 # h = g mv a6,a5 # g = f mv a5,a4 # f = e add a4,a3,t0 # e = d + temp1 mv a3,a2 # d = c mv a2,a1 # c = b mv a1,a0 # b = a add a0,t0,t1 # a = temp1 + temp2 addi s0,s0,1 li t0,64 blt s0,t0,1b la t0,h lw t1,0(t0) add t1,t1,a0 sw t1,0(t0) lw t1,4(t0) add t1,t1,a1 sw t1,4(t0) lw t1,8(t0) add t1,t1,a2 sw t1,8(t0) lw t1,12(t0) add t1,t1,a3 sw t1,12(t0) lw t1,16(t0) add t1,t1,a4 sw t1,16(t0) lw t1,20(t0) add t1,t1,a5 sw t1,20(t0) lw t1,24(t0) add t1,t1,a6 sw t1,24(t0) lw t1,28(t0) add t1,t1,a7 sw t1,28(t0) la a0,msghash li a7,4 ecall # Print header line la s0,h la s1,h+32 1: lw a0,0(s0) call hexdump addi s0,s0,4 blt s0,s1,1b li a7,10 ecall hexdump: mv t3,a0 li t0,8 li t1,0xf0000000 li t4,0x3a 1: and t2,t3,t1 srli t2,t2,28 addi t2,t2,0x30 blt t2,t4,2f addi t2,t2,0x27 2: mv a0,t2 li a7,11 ecall slli t3,t3,4 addi t0,t0,-1 bnez t0,1b ret h: .word 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 k: .word 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 .word 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 .word 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da .word 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 .word 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 .word 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070 .word 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 .word 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 w: .skip 64*4 message: .word 0x68800000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000 .word 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000008 msgentermessage: .asciz "\nEnter message:\n> " msgwait: .asciz "\n\nPlease wait..." msghash: .asciz "\n\nSHA-256 hash:\n"