summaryrefslogtreecommitdiffstats
path: root/sha1/msauth.asm
blob: 6f16b0359c6ce84a9bc6821200b19fbd3b3b618c (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
            INCLUDE "WRISTAPP.I"

	org $100
		jmp	test


idx		EQU	$60
rot_cnt		EQU	$61
k		EQU	$62
src1		EQU	$63
src2		EQU	$64
dest		EQU	$65


; 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:
		jsr sha1
		stop

sha1:	
		ldx #0

sha1_copy_h_to_ae_loop:
		lda 	H,x
		sta	VA,x
		incx
		cpx 	#20
		bne	sha1_copy_h_to_ae_loop

		lda	#0
		sta	idx

sha1_loop:	
			; a contains idx
			cmp	#20
			bhs	sha1_k2
				lda	#(K1-base)
				sta	k

				; T1 = (B & C) | ((~B) & D)
				lda	#(T2-base)
				sta	dest
				lda	#(VB-base)
				sta	src1
				lda	#(FFFFFFFF-base)
				jsr	xor4	;T2=~B

				lda	#(T2-base)
				sta	src1
				lda	#(VD-base)
				jsr	and4	;T2=T2&D

				bra	sha1_t2_or_bandc ; T1=T2 | (B&C)
sha1_k2:
			cmp	#40
			bhs	sha1_k3
				lda	#(K2-base)
				sta	k
				bra	sha1_k24 ; T1 = B ^ C ^ D

sha1_k3:
			cmp	#60
			bhs	sha1_k4
				; T1 = (B & C) | (B & D) | (C & D)
				lda	#(K3-base)
				sta	k

				lda	#(T1-base)
				sta	dest
				lda	#(VD-base)
				sta	src1
				lda	#(VB-base)
				jsr	and4	; T1=B&D

				lda	#(T2-base)
				sta	dest
				lda	#(VC-base)
				jsr	and4	; T2=C&D

				lda	#(T2-base)
				sta	src1
				lda	#(T1-base)
				jsr	or4	; T2=T1|T2

sha1_t2_or_bandc:

				lda	#(T1-base)
				sta	dest
				lda	#(VB-base)
				sta	src1
				lda	#(VC-base)
				jsr	and4	; T1 = B&C

				lda	#(T1-base)
				sta	src1
				lda	#(T2-base)
				jsr	or4	; T1= T1|T2

				bra	sha1_shuffle

sha1_k4:
				lda	#(K4-base)
				sta	k
sha1_k24:
				; T1 = B ^ C ^ D
				lda	#(T1-base)
				sta	dest
				lda	#(VB-base)
				sta	src1
				lda	#(VC-base)
				jsr	xor4
				lda	#(T1-base)
				sta	src1
				lda	#(VD-base)
				jsr	xor4
sha1_shuffle:
			; T1 is src1 and dest here

			lda	#(VE-base)
			jsr	add4	; T1=T1+E

			lda	k
			jsr	add4	; T1=T1+k

			; get data here
			lda	#(ZERO-base)
			jsr	add4	; T1=T1+data

			lda	#(VD-base)
			jsr 	copy_down	; E=D
			lda	#(VC-base)
			jsr 	copy_down	; D=C
			lda	#(VB-base)	
			jsr 	copy_down	; C=B
			lda	#30
			jsr	rot_left	; C=ROTLEFT(C,30)
			lda	#(VA-base)	
			jsr 	copy_down	; B=A

			lda	#(VA-base)
			sta	dest
			sta	src1
			lda	#5
			jsr	rot_left	; A=ROTLEFT(A,5)

			lda	#(T1-base)
			jsr	add4		; A=A+T1

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

		lda	idx
		add	#1
		cmp 	#80
		bhs	sha1_done
		jmp 	sha1_loop
sha1_done:
		rts


copy_down:
		sta	src1
		add	#4
		sta	dest
		lda	#(ZERO-base)

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

and4:
		sta	src2
		lda	#$d4	; and $ffff,a
		sta 	op4_src2
		bra 	op4_binary

xor4:
		sta	src2
		lda	#$d8	; eor $ffff,a
		sta 	op4_src2
		bra	op4_binary
or4:
		sta	src2
		lda	#$da	; or $ffff,a
		sta 	op4_src2
op4_binary:		
		ldx	#0
		lda	#base & $ff
		add	src2
		sta	op4_src2+2
	        txa	
		adc 	#base>>8
		sta	op4_src2+1

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

		lda	#base & $ff
		add	dest
		sta	op4_dst+2
		txa 	
		adc 	#base>>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 	rot_cnt
rot_loop:
		lda	base,x
		rola
		lda	base+1,x
		rola
		sta	base+1,x
		lda	base+2,x
		rola
		sta	base+2,x
		lda	base+3,x
		rola
		sta	base+3,x
		lda	base,x
		rola
		sta	base,x
		dec	rot_cnt
		bne 	rot_loop
		rts

		
data:

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
base:
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

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