;SHA-1 hashing code - shamelessly taken from Goplat include "settings.inc" include "ti83plus.inc" include "equates.inc" SEGMENT Main GLOBALS ON ;Keep these contiguous sha1_temp EQU appData ;appData ;4 sha1_a EQU sha1_temp+4 ;4 sha1_b EQU sha1_a+4 ;4 sha1_c EQU sha1_b+4 ;4 sha1_d EQU sha1_c+4 ;4 sha1_e EQU sha1_d+4 ;4 sha1_f EQU sha1_e+4 ;4 sha1_k EQU sha1_f+4 ;4 sha1_f_op_ptr EQU saveSScreen ;2 sha1_block EQU sha1_f_op_ptr+2 ;64 sha1_hash EQU sha1_block+364 ;20 (364?) sha1_length EQU sha1_hash+20 ;8 sha1_block_ptr EQU sha1_length+8 ;2 sha1_default_hash: DB 067h,045h,023h,001h DB 0EFh,0CDh,0ABh,089h DB 098h,0BAh,0DCh,0FEh DB 010h,032h,054h,076h DB 0C3h,0D2h,0E1h,0F0h jigMasterKey: DB 46h,0DCh,0EAh,0D3h,17h,0FEh,45h,0D8h,09h,23h,0EBh,97h,0E4h,95h,64h,10h,0D4h,0CDh,0B2h,0C2h ;jigKey: ; DB 04h,4Eh,61h,1Bh,0A6h,0A6h,0E3h,9Ah,98h,0CFh,35h,81h,2Ch,80h,68h,0C7h,0FCh,5Fh,7Ah,0E8h HMACInit: ;HL => key to use push hl call SHA1Init pop hl ld de,jigKeyTemp ld b,20 $$: ld a,(hl) xor 36h ld (de),a inc hl inc de djnz $B ld b,64-20 ld a,36h $$: ld (de),a inc de djnz $B ld b,64 ld hl,jigKeyTemp $$: ld a,(hl) push hl push bc call HMACAddByte pop bc pop hl inc hl djnz $B ret HMACAddByte: jr SHA1AddByte HMACDone: call SHA1Final ld hl,sha1_hash ld de,jigResponseTemp ld bc,20 ldir ld b,64 ld hl,jigKeyTemp $$: ld a,(hl) xor 6Ah ld (hl),a inc hl djnz $B call SHA1Init ld b,64 ld hl,jigKeyTemp $$: ld a,(hl) push hl push bc call SHA1AddByte pop bc pop hl inc hl djnz $B ld b,20 ld hl,jigResponseTemp $$: ld a,(hl) push hl push bc call SHA1AddByte pop bc pop hl inc hl djnz $B jr SHA1Final SHA1Init: ld hl,sha1_default_hash ld de,sha1_hash ld bc,20 ldir ld hl,sha1_length ld bc,8 B_CALL MemClear ld hl,sha1_block ld (sha1_block_ptr),hl ret SHA1Final: ;Returns 20-byte hash in sha1_hash sha1_pad: ; append the bit '1' to the message ; append 0 <= k < 512 bits '0', so that the resulting message length (in bits) ; is congruent to 448 = -64 (mod 512) ld a, 80h sha1_pad_zero: call sha1_add_byte_no_length ld a, (sha1_block_ptr) cp (sha1_block+56) & 255 ld a, 00h jr nz, sha1_pad_zero ; append length of message (before padding), in bits, as 64-bit big-endian integer ld hl, sha1_length ld de, (sha1_block_ptr) ld bc, 8 ldir jr sha1_process_block SHA1AddByte: ;A = byte to add/hash sha1_add_byte: push af ld hl, sha1_length+7 ld a, (hl) add a, 8 ld (hl), a jr nc, _length_ok _length_inc: dec hl inc (hl) jr z, _length_inc _length_ok: pop af sha1_add_byte_no_length: ld de, (sha1_block_ptr) ld (de), a inc de ld (sha1_block_ptr), de ld a, e cp (sha1_block+64) & 255 ret nz sha1_process_block: ; Extend the sixteen 32-bit words into eighty 32-bit words: ; for i from 16 to 79 ; w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1 ld ix, sha1_block+63 ld c, 64 _extend: ld b, 4 _extend_inner: inc ix ld a, (ix-12) xor (ix-32) xor (ix-56) xor (ix-64) ld (ix), a djnz _extend_inner push ix pop hl ld a, (ix-3) rlca rl (hl) dec hl rl (hl) dec hl rl (hl) dec hl rl (hl) dec hl dec c jr nz, _extend ; Initialize hash value for this chunk: ; a = h0 ; b = h1 ; c = h2 ; d = h3 ; e = h4 ld hl, sha1_hash ld de, sha1_a ld bc, 20 ldir ; Main loop ld hl, sha1_block-1 ld (sha1_block_ptr), hl ld hl, operation_mux call sha1_20rounds DB 5Ah,82h,79h,99h ld hl, operation_xor call sha1_20rounds db 6Eh,0D9h,0EBh,0A1h ld hl, operation_maj call sha1_20rounds db 8Fh,1Bh,0BCh,0DCh ld hl, operation_xor call sha1_20rounds db 0CAh,62h,0C1h,0D6h ; Add this chunk's hash to result so far ; h0 += a ; h1 += b ; h2 += c ; h3 += d ; h4 += e ld de, sha1_hash+19 ld hl, sha1_a+19 ld c, 5 _add_result: call add_32bits dec c jr nz, _add_result ld hl, sha1_block ld (sha1_block_ptr), hl ret sha1_20rounds: ld (sha1_f_op_ptr), hl pop hl ld de, sha1_k ld bc, 4 ldir push hl ld b, 20 _rounds: push bc ; f = call do_f_operation ; temp = (a leftrotate 5) + f + e + k + w[i] ld hl, sha1_a ld de, sha1_temp ld bc, 4 ldir ld a, (sha1_temp) rrca rrca rrca rrca ld hl, sha1_temp+3 rld rl (hl) dec hl rld rl (hl) dec hl rld rl (hl) dec hl rld rl (hl) ld hl, sha1_k+3 call add_to_temp ; k call add_to_temp ; f call add_to_temp ; e ld hl, (sha1_block_ptr) inc hl inc hl inc hl inc hl ld (sha1_block_ptr), hl call add_to_temp ; e = d ; d = c ; c = b leftrotate 30 ; b = a ; a = temp ld hl, sha1_d+3 ld de, sha1_e+3 ld bc, 20 lddr ld a, (sha1_c+3) ld b, 2 _ror2: ld hl, sha1_c rrca rr (hl) inc hl rr (hl) inc hl rr (hl) inc hl rr (hl) djnz _ror2 pop bc djnz _rounds ret do_f_operation: ld ix, sha1_a ld hl, (sha1_f_op_ptr) ld b, 4 jp (hl) operation_mux: ; f = (b & c) | (~b & d) = ((c ^ d) & B) ^ d ld a, (ix+8) xor (ix+12) and (ix+4) xor (ix+12) ld (ix+20), a inc ix djnz operation_mux ret operation_xor: ; f = b ^ c ^ d ld a, (ix+4) xor (ix+8) xor (ix+12) ld (ix+20), a inc ix djnz operation_xor ret operation_maj: ; f = (b & c) | (b & d) | (c & d) ld c, (ix+4) ld d, (ix+8) ld e, (ix+12) ld a, c and d ld h, a ld a, c and e ld l, a ld a, d and e or h or l ld (ix+20), a inc ix djnz operation_maj ret add_to_temp: ld de, sha1_temp+3 add_32bits: ld b, 4 or a _add_loop: ld a, (de) adc a, (hl) ld (de), a dec de dec hl djnz _add_loop ret