| 1 |
;ACME 0.97 |
| 2 |
|
| 3 |
!ifdef lib_6502_rc4_a !eof |
| 4 |
lib_6502_rc4_a = 1 |
| 5 |
|
| 6 |
; this is an implementation of the stream cipher algorithm known as RC4. |
| 7 |
|
| 8 |
; you need to define these addresses in your code: |
| 9 |
; single bytes: |
| 10 |
; rc4_length key/chunk length (only used temporarily) |
| 11 |
; rc4_count iteration count (only used temporarily) |
| 12 |
; rc4_ii holds internal state (do not touch!) |
| 13 |
; rc4_jj holds internal state (do not touch!) |
| 14 |
; multi-byte areas: |
| 15 |
; rc4_state 256 bytes of internal state (do not touch!) |
| 16 |
; rc4_key key buffer |
| 17 |
; rc4_in input buffer |
| 18 |
; rc4_out output buffer |
| 19 |
; the sizes of the buffers limit how much data you can pass to the functions. |
| 20 |
; all three buffer addresses may be identical, in that case the output will |
| 21 |
; overwrite the input (and/or the key). |
| 22 |
|
| 23 |
; functions you can then call: |
| 24 |
; rc4_init initialise state |
| 25 |
; rc4_usekey_X use key (in key buffer, length in X) to change state |
| 26 |
; rc4_reset reset ii and jj (call between keying and processing) |
| 27 |
; rc4_process_X de/encrypt data from input to output buffer (length in X) |
| 28 |
|
| 29 |
; here's some example code: |
| 30 |
!if 0 { |
| 31 |
!src <6502/rc4.a> ; include this file |
| 32 |
entrypoint |
| 33 |
jsr rc4_init ; (re-)init rc4 internal state |
| 34 |
[copy user-supplied key (or its hash) to key buf] |
| 35 |
[maybe append some salt to keybuf] |
| 36 |
; do this at least once, but some |
| 37 |
; algorithms need you to do this N times: |
| 38 |
ldx #TOTALKEYLENGTH |
| 39 |
jsr rc4_usekey_X |
| 40 |
; end of (optional) loop |
| 41 |
jsr rc4_reset |
| 42 |
[copy chunk of input data to input buffer] |
| 43 |
ldx #CHUNKSIZE |
| 44 |
jsr rc4_process_X |
| 45 |
[read from output buffer] |
| 46 |
rts |
| 47 |
+rc4_code ; place actual code |
| 48 |
} |
| 49 |
|
| 50 |
!macro rc4_code { |
| 51 |
; create shorter names |
| 52 |
.length = rc4_length |
| 53 |
.count = rc4_count |
| 54 |
.ii = rc4_ii |
| 55 |
.jj = rc4_jj |
| 56 |
.state = rc4_state |
| 57 |
.keybuf = rc4_key |
| 58 |
.inbuf = rc4_in |
| 59 |
.outbuf = rc4_out |
| 60 |
|
| 61 |
; fill state vector with initial values (00..ff) and init both counters |
| 62 |
rc4_init |
| 63 |
ldx #0 |
| 64 |
- txa |
| 65 |
sta .state, x |
| 66 |
inx |
| 67 |
bne - |
| 68 |
;FALLTHROUGH |
| 69 |
; reset state counters to zero (actually, only really needed for jj) |
| 70 |
rc4_reset ldx #0 |
| 71 |
stx .ii |
| 72 |
stx .jj |
| 73 |
rts |
| 74 |
|
| 75 |
; use key (in key buffer, length in X) to change permutation in state array |
| 76 |
; X==0 means 256! |
| 77 |
rc4_usekey_X |
| 78 |
; X holds .ii (but we count from 0 to 255 and so do not use the real .ii at all) |
| 79 |
; Y holds .jj or .count (.jj is then in A) |
| 80 |
stx .length |
| 81 |
ldx #0 |
| 82 |
stx .count |
| 83 |
; do 256 mixing operations |
| 84 |
;.ii = 0 |
| 85 |
;ldx #0 ;do { |
| 86 |
ldy .jj |
| 87 |
--- ;jj += state[ii] + key[count]; |
| 88 |
tya;lda .jj |
| 89 |
clc |
| 90 |
adc .state, x |
| 91 |
ldy .count |
| 92 |
clc |
| 93 |
adc .keybuf, y |
| 94 |
;sta .jj |
| 95 |
;if (++count == length) |
| 96 |
; count = 0; |
| 97 |
iny |
| 98 |
cpy .length |
| 99 |
bne + |
| 100 |
ldy #0 |
| 101 |
+ sty .count |
| 102 |
tay ; now Y holds .jj |
| 103 |
lda .state, x ;tmp = state[ii] |
| 104 |
pha |
| 105 |
lda .state, y ;state[ii] = state[jj] |
| 106 |
sta .state, x |
| 107 |
pla ;state[jj] = tmp |
| 108 |
sta .state, y |
| 109 |
inx ;} while (++ii); |
| 110 |
bne --- |
| 111 |
sty .jj ; writeback |
| 112 |
rts |
| 113 |
|
| 114 |
; de/encrypt first X bytes of input buffer to output buffer |
| 115 |
; X==0 means 256! |
| 116 |
rc4_process_X |
| 117 |
stx .length |
| 118 |
ldx #0 |
| 119 |
--- stx .count |
| 120 |
inc .ii ; jj += state[++ii] |
| 121 |
lda .jj |
| 122 |
ldx .ii |
| 123 |
clc |
| 124 |
adc .state, x |
| 125 |
sta .jj |
| 126 |
tay ; now Y holds .jj |
| 127 |
lda .state, x ; tmp = state[ii]; |
| 128 |
pha |
| 129 |
lda .state, y ; state[ii] = state[jj]; |
| 130 |
sta .state, x |
| 131 |
pla ; state[jj] = tmp; |
| 132 |
sta .state, y |
| 133 |
clc ; nn = state[ii] + state[jj]; |
| 134 |
adc .state, x |
| 135 |
tax ; buf[aktueller_offset] ^= state[nn] |
| 136 |
lda .state, x |
| 137 |
ldx .count |
| 138 |
eor .inbuf, x |
| 139 |
sta .outbuf, x |
| 140 |
; all done? |
| 141 |
inx |
| 142 |
cpx .length |
| 143 |
bne --- |
| 144 |
rts |
| 145 |
} |
| 146 |
|
| 147 |
!macro rc4_test { |
| 148 |
; TODO: add code for automated testing! |
| 149 |
} |