1 |
81 |
rehayes |
; 345678901234567890123456789012345678901234567890123456789012345678901234567890
|
2 |
|
|
; Copyright (c) 2010, Robert Hayes
|
3 |
|
|
; SKIPJACK ENCRYPT/DECRYPT for xgate RISC processor core
|
4 |
|
|
; Bob Hayes - August 7, 2010
|
5 |
|
|
; Version 0.1 Basic SKIPJACK Encrypt and Decrypt modules for the Xgate
|
6 |
|
|
; processor. These routines do the basic codebook encrypt and decrypt
|
7 |
|
|
; functions, other modes of use such as output feedback,cipher feedback and
|
8 |
|
|
; cipher block chaining can be added at the host code level or the routines
|
9 |
|
|
; could be expanded to incorporate the required functionality.
|
10 |
|
|
|
11 |
|
|
; This implementation is believed to be compliant with the SKIPJACK algorithm
|
12 |
|
|
; as described in "SKIPJACK and KEA Algorithm Specifications" Version 2.0
|
13 |
|
|
; dated 29 May 1998, which is available from the National Institute for
|
14 |
|
|
; Standards and Technology:
|
15 |
|
|
; http://csrc.nist.gov/groups/STM/cavp/documents/skipjack/skipjack.pdf
|
16 |
|
|
|
17 |
|
|
;
|
18 |
|
|
; This source file is free software: you can redistribute it and/or modify
|
19 |
|
|
; it under the terms of the GNU Lesser General Public License as published
|
20 |
|
|
; by the Free Software Foundation, either version 3 of the License, or
|
21 |
|
|
; (at your option) any later version.
|
22 |
|
|
;
|
23 |
|
|
; Supplemental terms.
|
24 |
|
|
; * Redistributions of source code must retain the above copyright
|
25 |
|
|
; notice, this list of conditions and the following disclaimer.
|
26 |
|
|
; * Neither the name of the <organization> nor the
|
27 |
|
|
; names of its contributors may be used to endorse or promote products
|
28 |
|
|
; derived from this software without specific prior written permission.
|
29 |
|
|
;
|
30 |
|
|
; THIS SOFTWARE IS PROVIDED BY Robert Hayes ''AS IS'' AND ANY
|
31 |
|
|
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
32 |
|
|
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
33 |
|
|
; DISCLAIMED. IN NO EVENT SHALL Robert Hayes BE LIABLE FOR ANY
|
34 |
|
|
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
35 |
|
|
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
36 |
|
|
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
37 |
|
|
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
38 |
|
|
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
39 |
|
|
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
40 |
|
|
;
|
41 |
|
|
; You should have received a copy of the GNU General Public License
|
42 |
|
|
; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
CPU XGATE
|
46 |
|
|
|
47 |
|
|
ORG $fe00
|
48 |
|
|
DS.W 2 ; reserve two words at channel 0, always unused for thread
|
49 |
|
|
; channel 1
|
50 |
|
|
DC.W SK_ENCRYPT ; point to start address
|
51 |
|
|
DC.W ROUND_E ; point to initial variables (Loaded into R1)
|
52 |
|
|
; channel 2
|
53 |
|
|
DC.W SK_DECRYPT ; point to start address
|
54 |
|
|
DC.W ROUND_D ; point to initial variables (Loaded into R1)
|
55 |
|
|
; channel 3
|
56 |
|
|
DC.W _ERROR ; point to start address
|
57 |
|
|
DC.W V_PTR ; point to initial variables
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
ORG $2000 ; Setup a data storage area
|
61 |
|
|
|
62 |
|
|
V_PTR EQU 123
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
;-------------------------------------------------------------------------------
|
66 |
|
|
; Constants used for offset caculations from R1 of SKIPJACK RAM variables.
|
67 |
|
|
; These offsets are used by both encrypt and decrypt routines.
|
68 |
|
|
;-------------------------------------------------------------------------------
|
69 |
|
|
SK_ROUND EQU 0
|
70 |
|
|
SK_W1 EQU 2
|
71 |
|
|
SK_W2 EQU 4
|
72 |
|
|
SK_W3 EQU 6
|
73 |
|
|
SK_W4 EQU 8
|
74 |
|
|
SK_KEY1 EQU 10
|
75 |
|
|
SK_KEY2 EQU 12
|
76 |
|
|
SK_KEY3 EQU 14
|
77 |
|
|
SK_KEY4 EQU 16
|
78 |
|
|
SK_KEY5 EQU 18
|
79 |
|
|
SK_KEY_P EQU 20
|
80 |
|
|
SK_F_P EQU 22
|
81 |
|
|
|
82 |
|
|
;-------------------------------------------------------------------------------
|
83 |
|
|
; RAM Variables for Skipjack Encryption
|
84 |
|
|
;-------------------------------------------------------------------------------
|
85 |
|
|
ROUND_E DC.W $55aa ; R1+0
|
86 |
|
|
PT DC.B $33,$22,$11,$00,$dd,$cc,$bb,$aa ; R1+2
|
87 |
|
|
KEYE_N DS.W 5 ; R1+10
|
88 |
|
|
KEYE_PTR DS.W 1 ; R1+20
|
89 |
|
|
F_PTR_E DC.W F_TABLE ; R1+22
|
90 |
|
|
|
91 |
|
|
KEY DC.B $99,$00,$77,$88,$55,$66,$33,$44,$11,$22
|
92 |
|
|
; R1 can only be used to explictly address the first 32 bytes
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
;-------------------------------------------------------------------------------
|
96 |
|
|
; Variables for Skipjack Decryption
|
97 |
|
|
;-------------------------------------------------------------------------------
|
98 |
|
|
ALIGN 1
|
99 |
|
|
ROUND_D DC.W $55aa ; R1+0
|
100 |
|
|
CT DC.B $25,$87,$ca,$e2,$7a,$12,$d3,$00 ; R1+2
|
101 |
|
|
KEYD_N DS.W 5 ; R1+10
|
102 |
|
|
KEYD_PTR DS.W 1 ; R1+20
|
103 |
|
|
F_PTR_D DC.W F_TABLE ; R1+22
|
104 |
|
|
|
105 |
|
|
KEYD DC.B $99,$00,$77,$88,$55,$66,$33,$44,$11,$22
|
106 |
|
|
; R1 can only be used to explictly address the first 32 bytes
|
107 |
|
|
|
108 |
|
|
ALIGN 1
|
109 |
|
|
|
110 |
|
|
;-------------------------------------------------------------------------------
|
111 |
|
|
; Place where undefined interrupts go
|
112 |
|
|
;-------------------------------------------------------------------------------
|
113 |
|
|
_ERROR
|
114 |
|
|
LDL R2,#$04 ; Sent Message to Testbench Error Register
|
115 |
|
|
LDH R2,#$80
|
116 |
|
|
LDL R3,#$ff
|
117 |
|
|
STB R3,(R2,#0)
|
118 |
|
|
|
119 |
|
|
SIF
|
120 |
|
|
RTS
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
;-------------------------------------------------------------------------------
|
124 |
|
|
; Skipjack Encryption
|
125 |
|
|
;-------------------------------------------------------------------------------
|
126 |
|
|
SK_ENCRYPT
|
127 |
|
|
;--- Only used for testbench, Delete for production release --------------------
|
128 |
|
|
LDL R2,#$00 ; Sent Message to Testbench Check Point Register
|
129 |
|
|
LDH R2,#$80
|
130 |
|
|
LDL R3,#$01
|
131 |
|
|
STB R3,(R2,#0)
|
132 |
|
|
STB R3,(R2,#2) ; Send Message to clear Testbench interrupt register
|
133 |
|
|
;-------------------------------------------------------------------------------
|
134 |
|
|
|
135 |
|
|
; Copy and invert key so we can use the native XNOR functions
|
136 |
|
|
; Because this is only required to be done once the Host may implement
|
137 |
|
|
; the key inversion and then this code could be deleted.
|
138 |
|
|
LDL R3,#KEY
|
139 |
|
|
LDH R3,#KEY>>8 ; R3 is address of KEY
|
140 |
|
|
|
141 |
|
|
LDW R6,(R3,#0)
|
142 |
|
|
COM R6
|
143 |
|
|
STW R6,(R1,#SK_KEY1)
|
144 |
|
|
LDW R6,(R3,#2)
|
145 |
|
|
COM R6
|
146 |
|
|
STW R6,(R1,#SK_KEY2)
|
147 |
|
|
LDW R6,(R3,#4)
|
148 |
|
|
COM R6
|
149 |
|
|
STW R6,(R1,#SK_KEY3)
|
150 |
|
|
LDW R6,(R3,#6)
|
151 |
|
|
COM R6
|
152 |
|
|
STW R6,(R1,#SK_KEY4)
|
153 |
|
|
LDW R6,(R3,#8)
|
154 |
|
|
COM R6
|
155 |
|
|
STW R6,(R1,#SK_KEY5)
|
156 |
|
|
|
157 |
|
|
; The following code to initialize the F Table pointer could be done by the host
|
158 |
|
|
; to save a few cycles of execution since it is only needed to be done once.
|
159 |
|
|
LDL R5,#F_TABLE
|
160 |
|
|
LDH R5,#F_TABLE>>8 ; R5 is address of F Table Pointer
|
161 |
|
|
STW R5,(R1,#SK_F_P) ; Save F Table Pointer
|
162 |
|
|
|
163 |
|
|
; Start of the SKIPJACK encrypt code
|
164 |
|
|
AND R3,R3,R0 ; Clear R3
|
165 |
|
|
STW R3,(R1,#SK_ROUND) ; Save initial Round Counter
|
166 |
|
|
LDL R3,#SK_KEY1 ; Set the initial Key counter value
|
167 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key counter initial value
|
168 |
|
|
|
169 |
|
|
SKE_LOOP:
|
170 |
|
|
LDW R3,(R1,#SK_KEY_P) ; Get Key Counter value
|
171 |
|
|
LDW R7,(R1,R3+) ; Get word of Key
|
172 |
|
|
CMPL R3,#(SK_KEY5+2) ; Check for rollover
|
173 |
|
|
BNE SKE_K1_OK ; Branch if rollover
|
174 |
|
|
LDL R3,#SK_KEY1 ; Set the initial Key counter value
|
175 |
|
|
|
176 |
|
|
SKE_K1_OK:
|
177 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key Pointer
|
178 |
|
|
LDW R5,(R1,#SK_F_P) ; Get F Table Base Address
|
179 |
|
|
LDL R4,#$70 ; Set Bitfield extract field
|
180 |
|
|
LDW R6,(R1,#SK_W1) ; Get W1
|
181 |
|
|
TFR R2,PC ; Subroutine Call
|
182 |
|
|
BRA CALC_G
|
183 |
|
|
|
184 |
|
|
LDW R3,(R1,#SK_KEY_P) ; Get Key Counter
|
185 |
|
|
LDW R7,(R1,R3+) ; Get word of Key
|
186 |
|
|
CMPL R3,#(SK_KEY5+2) ; Check for rollover
|
187 |
|
|
BNE SKE_K2_OK ; Branch if not rollover
|
188 |
|
|
LDL R3,#SK_KEY1 ; Set the initial Key counter value
|
189 |
|
|
|
190 |
|
|
SKE_K2_OK:
|
191 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key Pointer
|
192 |
|
|
TFR R2,PC ; Subroutine Call
|
193 |
|
|
BRA CALC_G
|
194 |
|
|
|
195 |
|
|
LDW R3,(R1,#SK_ROUND) ; Load Round Counter
|
196 |
|
|
BITL R3,#8 ;
|
197 |
|
|
BNE SKE_RUL_B ; Do rule B when bit 3 of round counter is set
|
198 |
|
|
|
199 |
|
|
SKE_RUL_A:
|
200 |
|
|
ADDL R3,#1 ; Update the Round Counter
|
201 |
|
|
LDW R5,(R1,#SK_W4) ; Get W4
|
202 |
|
|
XNOR R5,R6,R5
|
203 |
|
|
XNOR R5,R5,R3 ; XOR the Round counter
|
204 |
|
|
LDW R7,(R1,#SK_W2) ; Load W2=NEXT_W3
|
205 |
|
|
BRA SKE_SHIFT ; R5=NEXT_W1, R6=NEXT_W2, R7=NEXT_W3
|
206 |
|
|
|
207 |
|
|
SKE_RUL_B:
|
208 |
|
|
ADDL R3,#1 ; Update Round Counter
|
209 |
|
|
LDW R7,(R1,#SK_W1) ; Get W1
|
210 |
|
|
XNOR R7,R7,R3 ; XNOR W1 and Round Counter
|
211 |
|
|
LDW R5,(R1,#SK_W2) ; Load W2
|
212 |
|
|
XNOR R7,R5,R7 ; XNOR previous result with W2
|
213 |
|
|
LDW R5,(R1,#SK_W4) ; Load W4=NEXT_W1
|
214 |
|
|
|
215 |
|
|
SKE_SHIFT:
|
216 |
|
|
STW R3,(R1,#SK_ROUND) ; Save the Round Counter
|
217 |
|
|
STW R5,(R1,#SK_W1) ; Store New W1
|
218 |
|
|
STW R6,(R1,#SK_W2) ; Store New W2
|
219 |
|
|
LDW R6,(R1,#SK_W3) ; Load W3
|
220 |
|
|
STW R6,(R1,#SK_W4) ; Store New W4
|
221 |
|
|
STW R7,(R1,#SK_W3) ; Store New W3
|
222 |
|
|
|
223 |
|
|
CMPL R3,#31 ; Check for last round
|
224 |
|
|
BRKP_3 BLS SKE_LOOP
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
;--- Only used for testbench, Delete for production release --------------------
|
228 |
|
|
LDL R2,#$00 ; Sent Message to Testbench Check Point Register
|
229 |
|
|
LDH R2,#$80
|
230 |
|
|
LDL R3,#$02
|
231 |
|
|
STB R3,(R2,#0)
|
232 |
|
|
;-------------------------------------------------------------------------------
|
233 |
|
|
|
234 |
|
|
SIF
|
235 |
|
|
RTS
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
CALC_G
|
239 |
|
|
BFEXT R3,R6,R4 ; Copy low byte of W1 to R3
|
240 |
|
|
BFINSX R3,R7,R4 ; XNOR the low byte of W1 with the low byte of KEY_N
|
241 |
|
|
ADD R3,R3,R5 ; Caculate full F Table byte address
|
242 |
|
|
LDB R3,(R3,#0) ; Get F Table output
|
243 |
|
|
ROL R6,#8 ; Move the high byte of W1 to the low byte of R6
|
244 |
|
|
BFINSX R6,R3,R4 ; XNOR the high byte of W1 with the F table output
|
245 |
|
|
BFINS R3,R6,R4 ; Copy low byte
|
246 |
|
|
ROL R6,#8 ; Put low byte of W1 back to the low byte of R6
|
247 |
|
|
|
248 |
|
|
ROL R7,#8 ; Move the high byte of KEY_N to the low byte of R7
|
249 |
|
|
BFINSX R3,R7,R4 ; XNOR temp with the high byte of KEY_N
|
250 |
|
|
|
251 |
|
|
ANDH R3,#0 ; Clear R3 high byte
|
252 |
|
|
ADD R3,R3,R5 ; Caculate full F Table byte address
|
253 |
|
|
LDB R3,(R3,#0) ; Get F Table output
|
254 |
|
|
BFINSX R6,R3,R4 ; XNOR the low byte of W1 with the F table output
|
255 |
|
|
|
256 |
|
|
JAL R2 ; Jump to return address
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
;-------------------------------------------------------------------------------
|
260 |
|
|
; Skipjack Decryption
|
261 |
|
|
;-------------------------------------------------------------------------------
|
262 |
|
|
SK_DECRYPT
|
263 |
|
|
;--- Only used for testbench, Delete for production release --------------------
|
264 |
|
|
LDL R2,#$00 ; Sent Message to Testbench Check Point Register
|
265 |
|
|
LDH R2,#$80
|
266 |
|
|
LDL R3,#$02
|
267 |
|
|
STB R3,(R2,#0)
|
268 |
|
|
STB R3,(R2,#2) ; Send Message to clear Testbench interrupt register
|
269 |
|
|
;-------------------------------------------------------------------------------
|
270 |
|
|
|
271 |
|
|
; Copy and invert key so we can use the native XNOR functions
|
272 |
|
|
; Because this is only required to be done once the Host may implement
|
273 |
|
|
; the key inversion and then this code could be deleted.
|
274 |
|
|
LDL R3,#KEYD
|
275 |
|
|
LDH R3,#KEYD>>8 ; R3 is address of KEY
|
276 |
|
|
|
277 |
|
|
LDW R6,(R3,#0)
|
278 |
|
|
COM R6
|
279 |
|
|
STW R6,(R1,#SK_KEY1)
|
280 |
|
|
LDW R6,(R3,#2)
|
281 |
|
|
COM R6
|
282 |
|
|
STW R6,(R1,#SK_KEY2)
|
283 |
|
|
LDW R6,(R3,#4)
|
284 |
|
|
COM R6
|
285 |
|
|
STW R6,(R1,#SK_KEY3)
|
286 |
|
|
LDW R6,(R3,#6)
|
287 |
|
|
COM R6
|
288 |
|
|
STW R6,(R1,#SK_KEY4)
|
289 |
|
|
LDW R6,(R3,#8)
|
290 |
|
|
COM R6
|
291 |
|
|
STW R6,(R1,#SK_KEY5)
|
292 |
|
|
|
293 |
|
|
; The following code to initialize the F Table pointer could be done by the host
|
294 |
|
|
; to save a few cycles of execution since it is only needed to be done once.
|
295 |
|
|
LDL R5,#F_TABLE
|
296 |
|
|
LDH R5,#F_TABLE>>8 ; R5 is address of F Table Pointer
|
297 |
|
|
STW R5,(R1,#SK_F_P) ; Save F Table Pointer
|
298 |
|
|
|
299 |
|
|
; Start of the SKIPJACK decrypt code
|
300 |
|
|
LDL R3,#32 ;
|
301 |
|
|
STW R3,(R1,#SK_ROUND) ; Save initial Round Counter
|
302 |
|
|
LDL R3,#SK_KEY5 ; Set the initial Key counter value
|
303 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key counter initial value
|
304 |
|
|
|
305 |
|
|
SKD_LOOP:
|
306 |
|
|
LDW R3,(R1,#SK_KEY_P) ; Get Key Counter value
|
307 |
|
|
LDW R7,(R1,-R3) ; Get word of Key
|
308 |
|
|
CMPL R3,#(SK_KEY1) ; Check for rollover
|
309 |
|
|
BNE SKD_K1_OK ; Branch if rollover
|
310 |
|
|
LDL R3,#(SK_KEY5+2) ; Set the initial Key counter value
|
311 |
|
|
|
312 |
|
|
SKD_K1_OK:
|
313 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key Pointer
|
314 |
|
|
LDW R5,(R1,#SK_F_P) ; Get F Table Base Address
|
315 |
|
|
LDL R4,#$70 ; Set Bitfield extract field
|
316 |
|
|
LDW R6,(R1,#SK_W2) ; Get W2
|
317 |
|
|
TFR R2,PC ; Subroutine Call
|
318 |
|
|
BRA CALC_GN
|
319 |
|
|
|
320 |
|
|
LDW R3,(R1,#SK_KEY_P) ; Get Key Counter
|
321 |
|
|
LDW R7,(R1,-R3) ; Get word of Key
|
322 |
|
|
CMPL R3,#(SK_KEY1) ; Check for rollover
|
323 |
|
|
BNE SKD_K2_OK ; Branch if not rollover
|
324 |
|
|
LDL R3,#(SK_KEY5+2) ; Set the initial Key counter value
|
325 |
|
|
|
326 |
|
|
SKD_K2_OK:
|
327 |
|
|
STW R3,(R1,#SK_KEY_P) ; Save Key Pointer
|
328 |
|
|
TFR R2,PC ; Subroutine Call
|
329 |
|
|
BRA CALC_GN ; Return with the new W1 in R6
|
330 |
|
|
|
331 |
|
|
LDW R3,(R1,#SK_ROUND) ; Load Round Counter
|
332 |
|
|
MOV R4,R3 ; Copy R3 to R4
|
333 |
|
|
SUBL R4,#1 ; New Round Counter used to pick Rule A or B
|
334 |
|
|
BITL R4,#8 ;
|
335 |
|
|
BNE SKD_RUL_B ; Do rule B when bit 3 of round counter is set
|
336 |
|
|
|
337 |
|
|
SKD_RUL_A:
|
338 |
|
|
LDW R5,(R1,#SK_W2) ; Get W2
|
339 |
|
|
LDW R2,(R1,#SK_W1) ; Get W1
|
340 |
|
|
XNOR R5,R2,R5
|
341 |
|
|
XNOR R5,R5,R3 ; XOR the Round counter
|
342 |
|
|
LDW R7,(R1,#SK_W3) ; Load R7=NEXT_W2
|
343 |
|
|
BRA SKD_SHIFT ; R6=NEXT_W1, R7=NEXT_W2, R5=NEXT_W4
|
344 |
|
|
|
345 |
|
|
SKD_RUL_B:
|
346 |
|
|
LDW R5,(R1,#SK_W3) ; Get W3
|
347 |
|
|
XNOR R7,R6,R3 ; XNOR G and Round Counter
|
348 |
|
|
XNOR R7,R7,R5 ; XNOR W3 R7=NEXT_W2
|
349 |
|
|
LDW R5,(R1,#SK_W1) ; Load R5=NEXT_W4
|
350 |
|
|
|
351 |
|
|
SKD_SHIFT:
|
352 |
|
|
STW R4,(R1,#SK_ROUND) ; Save the Round Counter
|
353 |
|
|
STW R6,(R1,#SK_W1) ; Store New W1
|
354 |
|
|
LDW R6,(R1,#SK_W4) ; Load W4
|
355 |
|
|
STW R6,(R1,#SK_W3) ; Store New W3
|
356 |
|
|
STW R7,(R1,#SK_W2) ; Store New W2
|
357 |
|
|
STW R5,(R1,#SK_W4) ; Store New W4
|
358 |
|
|
|
359 |
|
|
CMPL R4,#1
|
360 |
|
|
BRKP_1 BHS SKD_LOOP
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
;--- Only used for testbench, Delete for production release --------------------
|
364 |
|
|
LDL R2,#$00 ; Sent Message to Testbench Check Point Register
|
365 |
|
|
LDH R2,#$80
|
366 |
|
|
LDL R3,#$02
|
367 |
|
|
STB R3,(R2,#0)
|
368 |
|
|
;-------------------------------------------------------------------------------
|
369 |
|
|
SIF
|
370 |
|
|
RTS
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
CALC_GN
|
374 |
|
|
BRKP_2 ROL R7,#8 ; Flip bytes of KEY_N
|
375 |
|
|
ROL R6,#8 ; Flip bytes of W2
|
376 |
|
|
BFEXT R3,R6,R4 ; Copy high byte of W2 to R3
|
377 |
|
|
BFINSX R3,R7,R4 ; XNOR the high byte of W2 with the high byte of KEY_N
|
378 |
|
|
ADD R3,R3,R5 ; Caculate full F Table byte address
|
379 |
|
|
LDB R3,(R3,#0) ; Get F Table output
|
380 |
|
|
ROL R6,#8 ; Swap the bytes of W2 back to starting place
|
381 |
|
|
BFINSX R6,R3,R4 ; XNOR the low byte of W2 with the F table output
|
382 |
|
|
|
383 |
|
|
ROL R7,#8 ; Flip bytes of KEY_N back to starting place
|
384 |
|
|
BFEXT R3,R6,R4 ; Copy low byte of W2 to R3
|
385 |
|
|
BFINSX R3,R7,R4 ; XNOR W2 final with the low byte of KEY_N
|
386 |
|
|
|
387 |
|
|
ADD R3,R3,R5 ; Caculate full F Table byte address
|
388 |
|
|
LDB R3,(R3,#0) ; Get F Table output
|
389 |
|
|
ROL R6,#8 ; Put high byte of W2 to the low byte of R6
|
390 |
|
|
BFINSX R6,R3,R4 ; XNOR the high byte of W2 with the F table output
|
391 |
|
|
ROL R6,#8 ; Put high byte of W2 back to the high byte of R6
|
392 |
|
|
|
393 |
|
|
JAL R2 ; Jump to return address
|
394 |
|
|
|
395 |
|
|
;-------------------------------------------------------------------------------
|
396 |
|
|
;-------------------------------------------------------------------------------
|
397 |
|
|
;-------------------------------------------------------------------------------
|
398 |
|
|
|
399 |
|
|
; Note locations in the $8000 range are used by the testbench
|
400 |
|
|
|
401 |
|
|
ORG $8040
|
402 |
|
|
|
403 |
|
|
; These memory locations are read by the testbench debugger to trigger
|
404 |
|
|
; register captures when the saved address if used
|
405 |
|
|
BREAK_CAPT_0 DC.W SKD_K1_OK
|
406 |
|
|
BREAK_CAPT_1 DC.W BRKP_1
|
407 |
|
|
BREAK_CAPT_2 DC.W BRKP_2
|
408 |
|
|
BREAK_CAPT_3 DC.W $0000
|
409 |
|
|
BREAK_CAPT_4 DC.W SKE_LOOP
|
410 |
|
|
BREAK_CAPT_5 DC.W BRKP_3
|
411 |
|
|
BREAK_CAPT_6 DC.W SKD_LOOP
|
412 |
|
|
BREAK_CAPT_7 DC.W BRKP_1
|
413 |
|
|
|
414 |
|
|
ORG $9000
|
415 |
|
|
|
416 |
|
|
; This is the inverted F Table that is used by the G Function
|
417 |
|
|
F_TABLE:
|
418 |
|
|
DC.B $5C,$28,$F6,$7C,$07,$B7,$09,$0B
|
419 |
|
|
DC.B $4C,$DE,$EA,$87,$66,$4E,$50,$06
|
420 |
|
|
DC.B $18,$D2,$B2,$75,$31,$B3,$35,$D1
|
421 |
|
|
DC.B $AD,$6A,$26,$E1,$B1,$C7,$BB,$D7
|
422 |
|
|
DC.B $F5,$20,$FD,$5F,$E8,$0E,$9F,$97
|
423 |
|
|
DC.B $ED,$48,$85,$3C,$16,$05,$C2,$AC
|
424 |
|
|
DC.B $69,$7B,$94,$45,$0D,$9C,$65,$E6
|
425 |
|
|
DC.B $83,$51,$1A,$0A,$08,$E9,$95,$5D
|
426 |
|
|
DC.B $C6,$49,$84,$F0,$3E,$6C,$7E,$E4
|
427 |
|
|
DC.B $11,$4B,$E5,$15,$2F,$6E,$D0,$47
|
428 |
|
|
DC.B $AA,$46,$25,$7A,$C0,$BE,$40,$1F
|
429 |
|
|
DC.B $A5,$A7,$7F,$A0,$99,$F4,$27,$6F
|
430 |
|
|
DC.B $CA,$2A,$3F,$58,$CC,$F9,$9A,$96
|
431 |
|
|
DC.B $BA,$FF,$6B,$A9,$92,$67,$64,$89
|
432 |
|
|
DC.B $68,$03,$4D,$3D,$4F,$01,$24,$DF
|
433 |
|
|
DC.B $1E,$14,$29,$1B,$22,$B8,$B5,$E2
|
434 |
|
|
DC.B $BD,$12,$61,$91,$B6,$C3,$32,$BC
|
435 |
|
|
DC.B $D8,$2D,$F8,$2B,$21,$38,$98,$E7
|
436 |
|
|
DC.B $76,$34,$CF,$E0,$72,$39,$70,$55
|
437 |
|
|
DC.B $37,$8B,$23,$36,$A2,$A3,$CE,$5B
|
438 |
|
|
DC.B $8F,$77,$9E,$D3,$60,$F2,$D4,$78
|
439 |
|
|
DC.B $AF,$7D,$AB,$9B,$D9,$82,$FC,$BF
|
440 |
|
|
DC.B $CB,$B4,$E3,$8C,$2E,$3B,$02,$C4
|
441 |
|
|
DC.B $33,$04,$80,$54,$19,$C1,$A4,$5A
|
442 |
|
|
DC.B $52,$FB,$DC,$63,$EB,$AE,$DD,$0F
|
443 |
|
|
DC.B $D6,$86,$8E,$81,$00,$73,$F1,$1D
|
444 |
|
|
DC.B $F3,$10,$43,$8D,$8A,$90,$C8,$5E
|
445 |
|
|
DC.B $13,$2C,$71,$9D,$74,$79,$EF,$17
|
446 |
|
|
DC.B $F7,$88,$EE,$41,$6D,$B0,$DB,$3A
|
447 |
|
|
DC.B $CD,$C9,$62,$30,$0C,$59,$44,$53
|
448 |
|
|
DC.B $A1,$93,$56,$EC,$A8,$DA,$4A,$1C
|
449 |
|
|
DC.B $42,$57,$C5,$FE,$FA,$A6,$D5,$B9
|
450 |
|
|
|
451 |
|
|
;-------------------------------------------------------------------------------
|
452 |
|
|
; This is the un-inverted F Table and it is included for reference only
|
453 |
|
|
; Not required
|
454 |
|
|
DC.B $a3,$d7,$09,$83,$f8,$48,$f6,$f4
|
455 |
|
|
DC.B $b3,$21,$15,$78,$99,$b1,$af,$f9
|
456 |
|
|
DC.B $e7,$2d,$4d,$8a,$ce,$4c,$ca,$2e
|
457 |
|
|
DC.B $52,$95,$d9,$1e,$4e,$38,$44,$28
|
458 |
|
|
DC.B $0a,$df,$02,$a0,$17,$f1,$60,$68
|
459 |
|
|
DC.B $12,$b7,$7a,$c3,$e9,$fa,$3d,$53
|
460 |
|
|
DC.B $96,$84,$6b,$ba,$f2,$63,$9a,$19
|
461 |
|
|
DC.B $7c,$ae,$e5,$f5,$f7,$16,$6a,$a2
|
462 |
|
|
DC.B $39,$b6,$7b,$0f,$c1,$93,$81,$1b
|
463 |
|
|
DC.B $ee,$b4,$1a,$ea,$d0,$91,$2f,$b8
|
464 |
|
|
DC.B $55,$b9,$da,$85,$3f,$41,$bf,$e0
|
465 |
|
|
DC.B $5a,$58,$80,$5f,$66,$0b,$d8,$90
|
466 |
|
|
DC.B $35,$d5,$c0,$a7,$33,$06,$65,$69
|
467 |
|
|
DC.B $45,$00,$94,$56,$6d,$98,$9b,$76
|
468 |
|
|
DC.B $97,$fc,$b2,$c2,$b0,$fe,$db,$20
|
469 |
|
|
DC.B $e1,$eb,$d6,$e4,$dd,$47,$4a,$1d
|
470 |
|
|
DC.B $42,$ed,$9e,$6e,$49,$3c,$cd,$43
|
471 |
|
|
DC.B $27,$d2,$07,$d4,$de,$c7,$67,$18
|
472 |
|
|
DC.B $89,$cb,$30,$1f,$8d,$c6,$8f,$aa
|
473 |
|
|
DC.B $c8,$74,$dc,$c9,$5d,$5c,$31,$a4
|
474 |
|
|
DC.B $70,$88,$61,$2c,$9f,$0d,$2b,$87
|
475 |
|
|
DC.B $50,$82,$54,$64,$26,$7d,$03,$40
|
476 |
|
|
DC.B $34,$4b,$1c,$73,$d1,$c4,$fd,$3b
|
477 |
|
|
DC.B $cc,$fb,$7f,$ab,$e6,$3e,$5b,$a5
|
478 |
|
|
DC.B $ad,$04,$23,$9c,$14,$51,$22,$f0
|
479 |
|
|
DC.B $29,$79,$71,$7e,$ff,$8c,$0e,$e2
|
480 |
|
|
DC.B $0c,$ef,$bc,$72,$75,$6f,$37,$a1
|
481 |
|
|
DC.B $ec,$d3,$8e,$62,$8b,$86,$10,$e8
|
482 |
|
|
DC.B $08,$77,$11,$be,$92,$4f,$24,$c5
|
483 |
|
|
DC.B $32,$36,$9d,$cf,$f3,$a6,$bb,$ac
|
484 |
|
|
DC.B $5e,$6c,$a9,$13,$57,$25,$b5,$e3
|
485 |
|
|
DC.B $bd,$a8,$3a,$01,$05,$59,$2a,$46
|
486 |
|
|
;-------------------------------------------------------------------------------
|
487 |
|
|
|
488 |
|
|
|
489 |
|
|
_BENCH DS.W 8
|
490 |
|
|
|
491 |
|
|
|