summaryrefslogtreecommitdiffstats
path: root/sha1/msauth.asm
blob: fc5e29122fe0c10aac1e775199733a642e90f4a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
            INCLUDE "WRISTAPP.I"


idx		EQU	$60
lidx		EQU	$61
src1		EQU	$63
src2		EQU	$64
dest		EQU	$65
i		EQU	$66
j		EQU	$67
opidx		EQU	$62


; BYTES 60-67 are persisent RAM for our use
; BYTES 110-435 are copied in from the EEPROM when we run

	org $110
START	EQU   *

;0x110 The main entry point - WRIST_MAIN
	jmp	main

;0x113 Called when we are suspended for any reason - WRIST_SUSPEND
	rts
	nop
      	nop

;0x116 Called to handle any timers or time events - WRIST_DOTIC
	rts
	nop
	nop

;0x119 Called when the COMM app starts and we have timers pending - WRIST_INCOMM
	rts
	nop
	nop

;0x11c Called when the COMM app loads new data - WRIST_NEWDATA
	rts
	nop
        nop

;0x11f The state table get routine - WRIST_GETSTATE
	lda	state_table0,X
        rts

;0x123 The state handler for state 0
	jmp	handle_st0_event
;0x126 Offset of state zero into the state table
        db  	state_table0 - state_table0


hello_string:   timex6  "HELLO "

; state table
state_table0:
        	db  	0
		db	EVT_ENTER,	TIM_ONCE,	0 	; Initial state
 	        db  	EVT_RESUME,	TIM_ONCE,	0	; Resume from a nested app
        	db  	EVT_DNNEXT,	TIM_ONCE,	0	; Next button
        	db  	EVT_MODE,	TIM_ONCE,	$FF	; Mode button - quit
        	db  	EVT_END

;
; (7) This is the main initialization routine which is called when we first get the app into memory
;
main:
		lda	#$c0		        ; We want button beeps and to indicate that we have been loaded
		sta	$96
	    	rts


handle_st0_event:
 	  	bset	1,$8f			; Indicate that we can be suspended
;         	lda	BTNSTATE		; Get the event
		
		jsr	CLEARALL		; Clear the screen

		lda	#hello_string-start
		jsr	PUT6TOP                 

		lda	#SYS8_MODE              ; Get the system offset for the 'MODE' string
		jsr	PUTMSGBOT               ; and put it on the bottom line

		rts



test:


		lda	#0
		sta	lidx

		; data starts off as all zeros
		; fixme correct time here
		lda	#$a6
		sta	DATA1+4
		lda	#$a7
		sta	DATA1+5

		; don't need to init H from IH already there
		jsr	sha1_block
		inc	lidx
		jsr	sha1_block

		; Copy H->DATA, IH->H
		ldx	#20
test_l1:
		lda	(H-1),x
		sta	(DATA1-1),x
		lda	(IH-1),x
		sta	(H-1),x
		decx
		bne test_l1

		lda 	#$80
		sta	DATA1+23

		lda	#$a0
		sta	DATA2

		; set KEYOR = 5c5c5c5c
		lda	#$5c
		ldx 	#4
test_l2:
		sta	(KEYOR-1),x
		decx
		bne	test_l2

		dec	lidx
		jsr	sha1_block
		inc	lidx
		jsr	sha1_block

		lda 	H+16
		and	#$F
		tax
		add	#(H-DATA_START)
		sta	src1
		lda	H+3,x
		and	#$7F
		sta	H+3,x
		lda	#(T1-DATA_START)
		sta	dest
		lda	#(ZERO-DATA_START)
		jsr 	add4
		
		

end:		bra end



get_w32_015_to_t2:
		lda	#(T2-DATA_START)
		sta	dest
		lda	#(ZERO-DATA_START)
		sta	src1

		lda	lidx
		cmp 	#0
		bne 	get_w32_015_to_t2_l1

			lda	j
			cmp 	#3
			bhs	get_w32_015_to_t2_z1
				lsla
				lsla
				add	#(KEY-DATA_START)
				sta	src1
get_w32_015_to_t2_z1:
			lda	#(ZERO-DATA_START)
			jsr	add4
			lda	#(T2-DATA_START)
			sta	src1
			lda	#(KEYOR-DATA_START)
			jsr 	xor4
		rts
get_w32_015_to_t2_l1:
		lda	j
		cmp	#6
		bhs	get_w32_015_to_t2_l2
			lsla
			lsla
			add	#(DATA1-DATA_START)
			bra	get_w32_015_to_t2_e
get_w32_015_to_t2_l2:
		cmp	#15
		bne	get_w32_015_to_t2_l3
			lda	#(DATA2-DATA_START)
			bra	get_w32_015_to_t2_e
get_w32_015_to_t2_l3:	
			lda	#(ZERO-DATA_START)
get_w32_015_to_t2_e:
		jsr	add4
		rts

get_w32_to_t2:
	lda	idx
	cmp	#16
	bhs	get_w32_to_t2_l0
		sta	j			; <16 use our function
		jsr	get_w32_015_to_t2
		rts
get_w32_to_t2_l0:
	sub 	#15				; store number of iterations needed in i
	sta	i

	; fill W
	lda	#0
	sta	j
get_w32_to_t2_l1:
		jsr	get_w32_015_to_t2	
		lda	j
		lsla
		lsla
		add	#(WBUF-DATA_START)
		sta 	dest
		lda	#(T2-DATA_START)
		sta	src1
		lda	#(ZERO-DATA_START)
		jsr 	add4

	inc	j
	lda	j	
	cmp 	#16
	bne	get_w32_to_t2_l1	


get_w32_to_t2_l2:
		lda	#(T2-DATA_START)
		sta	dest
		lda	#(WBUF13 - DATA_START)
		sta	src1
		lda	#(WBUF8 - DATA_START)
		jsr	xor4		; T2 =W13 ^ W8
		lda	#(T2-DATA_START)
		sta	src1
		lda	#(WBUF2 - DATA_START)
		jsr	xor4		; T2 = T2 ^ W2
		lda	#(WBUF0 - DATA_START)
		jsr	xor4		; T2= T2 ^ W0
		lda	#1
		jsr	rot_left	; T2= ROTLEFT(T2,1)


		; for (x=0;x<14;++x) wbuf[x]=wbuf[x+1]
		ldx	#((WBUF0 + $FF - 3 ) - WBUF14)
get_w32_to_t2_l3:
			lda 	(WBUF15 + 3 - $FF),x
			sta 	(WBUF14 + 3 - $FF),x
		incx
		bne	get_w32_to_t2_l3
		lda	#(WBUF15-DATA_START)
		sta	dest
		lda	#(ZERO-DATA_START)
		jsr	add4		; WBUF15=T2
		
	dec i
	bne get_w32_to_t2_l2
	rts


sha1_block:	
		ldx #20

sha1_block_copy_h_to_ae_loop:
		lda 	(H-1),x
		sta	(VA-1),x
		decx
		bne	sha1_block_copy_h_to_ae_loop

		lda	#0

sha1_block_loop:	
			; a contains idx
			sta	idx
			cmp	#20
			bhs	sha1_block_k2
				lda	#(K1-DATA_START)
				sta	k

				ldx	#(T2eNotBandD-OPLIST_START)
				jsr	op4_list

				bra	sha1_block_t2_or_bandc ; T1=T2 | (B&C)
sha1_block_k2:
			cmp	#40
			bhs	sha1_block_k3
				lda	#(K2-DATA_START)
				sta	k
				bra	sha1_block_k24 ; T1 = B ^ C ^ D

sha1_block_k3:
			cmp	#60
			bhs	sha1_block_k4
				; T1 = (B & C) | (B & D) | (C & D)
				lda	#(K3-DATA_START)
				sta	k

				ldx	#(T2eCandDorBandD-OPLIST_START)
				jsr	op4_list
 
sha1_block_t2_or_bandc:

				ldx	#(T1eT2orBandC-OPLIST_START)
				jsr	op4_list
				bra	sha1_block_shuffle

sha1_block_k4:
				lda	#(K4-DATA_START)
				sta	k
sha1_block_k24:

				ldx 	#(T1eBxorCxorD-OPLIST_START)
				jsr	op4_list

sha1_block_shuffle:

			jsr	get_w32_to_t2 	; W[i]->T2

			ldx	#(shuffleop-OPLIST_START)
			jsr	op4_list

		lda	idx
		add	#1
		cmp 	#80
		bhs	sha1_block_done
		jmp 	sha1_block_loop
sha1_block_done:


		ldx	#0
sha1_block_add0:
		clc
sha1_block_add1:
		lda	VA,x
		adc	H,x
		sta	H,x
		incx
		txa	
		and	#3
		bne	sha1_block_add1
		cpx	#20
		bne	sha1_block_add0

rts1:
		rts

op4_list:
		lda	OPLIST_START,x
		sta	op4_src2
		incx	
		lda	OPLIST_START,x
		sta	src1
		incx	
		lda	OPLIST_START,x
		sta	src2
		incx
		lda	OPLIST_START,x
		sta	dest
		incx	
		stx	opidx
		lda	op4_src2
		tsta
		beq	rts1
		bmi     op4_list_l1

			; rotates dest left src1 times
			ldx dest
op_list_l0:
				lda	DATA_START,x
				rola
				lda	DATA_START+1,x
				rola
				sta	DATA_START+1,x
				lda	DATA_START+2,x
				rola
				sta	DATA_START+2,x
				lda	DATA_START+3,x
				rola
				sta	DATA_START+3,x
				lda	DATA_START,x
				rola
				sta	DATA_START,x
			dec	src1
			bne 	op_list_l0
		bra	op4_list_l2
op4_list_l1:
			jsr	op4_binary
op4_list_l2:
		ldx	opidx
		bra	op4_list
		

add4:
		sta	src2
		lda	#$d9	; adc $ffff,a
		sta 	op4_src2
		bra 	op4_binary

xor4:
		sta	src2
		lda	#$d8	; eor $ffff,a
		sta 	op4_src2
		bra	op4_binary

op4_binary:		
		ldx	#0
		lda	#DATA_START & $ff
		add	src2
		sta	op4_src2+2
	        txa	
		adc 	#DATA_START>>8
		sta	op4_src2+1

		lda	#DATA_START & $ff
		add	src1
		sta	op4_src1+2
		txa 	
		adc 	#DATA_START>>8
		sta	op4_src1+1

		lda	#DATA_START & $ff
		add	dest
		sta	op4_dst+2
		txa 	
		adc 	#DATA_START>>8
		sta	op4_dst+1
		 		
		clc
op4_1:
op4_src1:
			lda	$fffe,x
op4_src2:
			adc	$fffe,x
op4_dst:
			sta	$fffe,x
			incx
			txa
			and	#3
			bne	op4_1
		rts

rot_left:	
		ldx dest
		sta 	j
rot_loop:
		lda	DATA_START,x
		rola
		lda	DATA_START+1,x
		rola
		sta	DATA_START+1,x
		lda	DATA_START+2,x
		rola
		sta	DATA_START+2,x
		lda	DATA_START+3,x
		rola
		sta	DATA_START+3,x
		lda	DATA_START,x
		rola
		sta	DATA_START,x
		dec	j
		bne 	rot_loop
		rts


OPLIST_START:

T2eNotBandD:
				DB	$d8		;eor
				DB	(VB-DATA_START)	;src1
				DB	(FFFFFFFF-DATA_START)	;src2
				DB	(T2-DATA_START)	;dest
				DB	$d4		;and
				DB	(VD-DATA_START)	;src1
				DB	(T2-DATA_START)	;src2
				DB	(T2-DATA_START)	;dest
				DB	0		;stop
				

T2eCandDorBandD:		; T1 = C&D, T2=B&D, T2=T1|T2
				DB	$d4		;and
				DB	(VC-DATA_START) ;src1
				DB	(VD-DATA_START)	;src2
				DB	(T1-DATA_START)	;dest
				DB	$d4		;and
				DB	(VB-DATA_START) ;src1
				DB	(VD-DATA_START)	;src2
				DB	(T2-DATA_START)	;dest
				DB	$da		;or
				DB	(T1-DATA_START)	;src1
				DB	(T2-DATA_START)	;src2
				DB	(T2-DATA_START)	;dest
				DB	0		;stop

T1eT2orBandC:			; T1=B&C; T1=T1|T2
				DB	$d4		;and
				DB	(VB-DATA_START) ;src1
				DB	(VC-DATA_START)	;src2
				DB	(T1-DATA_START)	;dest
				DB	$da		;or
				DB	(T1-DATA_START)	;src1
				DB	(T2-DATA_START)	;src2
				DB	(T1-DATA_START)	;dest
				DB	0		;stop
			
				

T1eBxorCxorD:
			DB	$d8		;xor
			DB	(VB-DATA_START)	;src1
			DB	(VC-DATA_START)	;src2
			DB	(T1-DATA_START)	;dst
			DB	$d8		;xor
			DB	(T1-DATA_START)	;src1
			DB	(VD-DATA_START) ;src2
			DB	(T1-DATA_START)	;dst
			DB	0		;stop
	
shuffleop:


			DB	$d9		;add
			DB	(T1-DATA_START)	;src1
			DB	(VE-DATA_START) ;src2
			DB	(T1-DATA_START)	;dest

			DB	$d9		;add
			DB	(T1-DATA_START)	;src1
k:
			DB	0		;src2
			DB	(T1-DATA_START)	;dest

			DB	$d9		;add
			DB	(T1-DATA_START)	;src1
			DB	(T2-DATA_START)	;src2
			DB	(T1-DATA_START)	;dest

			DB	$d9		;add
			DB	(VD-DATA_START) ;src1
			DB	(ZERO-DATA_START) ;src2
			DB	(VE-DATA_START) ;dest

			DB	$d9		;add
			DB	(VC-DATA_START) ;src1
			DB	(ZERO-DATA_START) ;src2
			DB	(VD-DATA_START) ;dest

			DB	$d9		;add
			DB	(VB-DATA_START) ;src1
			DB	(ZERO-DATA_START) ;src2
			DB	(VC-DATA_START) ;dest

			DB	$d9		;add
			DB	(VA-DATA_START) ;src1
			DB	(ZERO-DATA_START) ;src2
			DB	(VB-DATA_START) ;dest

			DB	$1		;rot left
			DB	30		;30 times
			DB	0		; N/A
			DB	(VC-DATA_START) ;dest

			DB	$1		;rot left
			DB	5		;5 times
			DB	0		; N/A
			DB	(VA-DATA_START) ;dest

			DB	$d9		;add
			DB	(VA-DATA_START) ;src1
			DB	(T1-DATA_START) ;src2
			DB	(VA-DATA_START) ;dest

			DB 	$0


OPLIST_END:


		
DATA_START:

KEY:
		DB	$89
		DB	$0e
		DB	$38
		DB      $6e


		DB	$bb
		DB	$3c
		DB	$cf
		DB	$e9

		DB	$00
		DB	$00
		DB	$4f
		DB	$e4
		
IH:
		DB      $01
		DB      $23
		DB      $45
		DB      $67
		DB      $89
		DB      $AB
		DB      $CD
		DB      $EF
		DB      $FE
		DB      $DC
		DB      $BA
		DB      $98
		DB      $76
		DB      $54
		DB      $32
		DB      $10
		DB      $F0
		DB      $E1
		DB      $D2
		DB      $C3
		
H:
		DB      $01
		DB      $23
		DB      $45
		DB      $67
		DB      $89
		DB      $AB
		DB      $CD
		DB      $EF
		DB      $FE
		DB      $DC
		DB      $BA
		DB      $98
		DB      $76
		DB      $54
		DB      $32
		DB      $10
		DB      $F0
		DB      $E1
		DB      $D2
		DB      $C3
K1:
        DB      $99
        DB      $79
        DB      $82
        DB      $5A
K2:
        DB      $A1
        DB      $EB
        DB      $D9
        DB      $6E
K3:
		DB      $DC
		DB      $BC
		DB      $1B
		DB      $8F
K4:
		DB      $D6
		DB      $C1
		DB      $62
		DB      $CA
K5:
		DB      $D0
		DB      $83
		DB      $8C
		DB      $46

FFFFFFFF:
		DB 	$FF
		DB 	$FF
		DB 	$FF
		DB 	$FF

KEYOR:
		DW	$3636
		DW	$3636


bss:

VA:		DW	0
		DW	0
VB:		DW	0
		DW	0
VC:		DW	0
		DW	0
VD:		DW	0
		DW	0
VE:		DW	0
		DW	0

T1:		DW	0 
		DW	0

T2:		DW	0 
		DW	0

ZERO:		DW	0 
		DW	0


WBUF:		
WBUF0:
		DW	0
		DW	0
WBUF1:
		DW	0
		DW	0
WBUF2:
		DW	0
		DW	0
WBUF3:
		DW	0
		DW	0
WBUF4:
		DW	0
		DW	0
WBUF5:
		DW	0
		DW	0
WBUF6:
		DW	0
		DW	0
WBUF7:
		DW	0
		DW	0
WBUF8:
		DW	0
		DW	0
WBUF9:
		DW	0
		DW	0
WBUF10:
		DW	0
		DW	0
WBUF11:
		DW	0
		DW	0
WBUF12:
		DW	0
		DW	0
WBUF13:
		DW	0
		DW	0
WBUF14:
		DW	0
		DW	0
WBUF15:
		DW	0
		DW	0


DATA1:
		DW	0
		DW	0
		DW	0
		DW	0
		DB	0
		DB	0
		DB	0
		DB	$80
		DW	0
		DW	0
		DW	0
		DW	0
		DW	0
		DW	0
DATA2:	
		DB	$40
		DB	$02
		DB	0
		DB	0

DATA_END: