2017-06-28 04:28:27 +00:00
|
|
|
* Copyright (c) 2017 Stephen Heumann
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2017-06-27 06:36:56 +00:00
|
|
|
* This makes a function wrapper that is callable from C,
|
|
|
|
* taking a pointer to the state structure as its argument.
|
|
|
|
macro
|
|
|
|
CFunction &fn
|
|
|
|
phb
|
|
|
|
plx
|
|
|
|
ply
|
|
|
|
tdc
|
|
|
|
pld
|
|
|
|
plb
|
|
|
|
plb
|
|
|
|
phy
|
|
|
|
phx
|
|
|
|
plb
|
|
|
|
pha
|
|
|
|
jsl &fn
|
|
|
|
pld
|
|
|
|
rtl
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
*The 'core' function applied to some words when computing the AES key schedule
|
2017-06-27 06:36:56 +00:00
|
|
|
macro
|
|
|
|
ExpandKeyCore &xorback,&rconoffset
|
|
|
|
|
|
|
|
ShortRegs
|
|
|
|
|
|
|
|
ldy rk-3,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor Rcon-&xorback+&rconoffset,x
|
|
|
|
eor rk-&xorback,x
|
|
|
|
sta rk,x
|
|
|
|
|
|
|
|
ldy rk-2,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+1-&xorback,x
|
|
|
|
sta rk+1,x
|
|
|
|
|
|
|
|
ldy rk-1,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+2-&xorback,x
|
|
|
|
sta rk+2,x
|
|
|
|
|
|
|
|
ldy rk-4,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+3-&xorback,x
|
|
|
|
sta rk+3,x
|
|
|
|
|
|
|
|
LongRegs
|
|
|
|
|
|
|
|
mend
|
|
|
|
|
|
|
|
* Secondary substitution step used when expanding AES-256 keys
|
|
|
|
macro
|
|
|
|
ExpandKeySubst &xorback,&rconoffset
|
|
|
|
|
|
|
|
ShortRegs
|
|
|
|
|
|
|
|
ldy rk-4,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk-&xorback,x
|
|
|
|
sta rk,x
|
|
|
|
|
|
|
|
ldy rk+1-4,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+1-&xorback,x
|
|
|
|
sta rk+1,x
|
|
|
|
|
|
|
|
ldy rk+2-4,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+2-&xorback,x
|
|
|
|
sta rk+2,x
|
|
|
|
|
|
|
|
ldy rk+3-4,x
|
|
|
|
lda Sbox,y
|
|
|
|
eor rk+3-&xorback,x
|
|
|
|
sta rk+3,x
|
|
|
|
|
|
|
|
LongRegs
|
|
|
|
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Generate consecutive words of key schedule that don't use above functions
|
2017-06-27 06:36:56 +00:00
|
|
|
macro
|
|
|
|
ExpandKeyIter &xorback,&nwords
|
|
|
|
lcla &i
|
|
|
|
lda rk,x
|
|
|
|
.loop1
|
|
|
|
eor rk+&i+4-&xorback,x
|
|
|
|
sta rk+&i+4,x
|
|
|
|
&i seta &i+4
|
|
|
|
aif &i/4<&nwords,.loop1
|
|
|
|
|
|
|
|
&i seta 2
|
|
|
|
lda rk+2,x
|
|
|
|
.loop2
|
|
|
|
eor rk+&i+4-&xorback,x
|
|
|
|
sta rk+&i+4,x
|
|
|
|
&i seta &i+4
|
|
|
|
aif &i/4<&nwords,.loop2
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do an initial AddRoundKey step on the starting state (for encryption)
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
|
|
|
AddInitialRoundKey
|
|
|
|
lcla &i
|
|
|
|
.top
|
|
|
|
lda state1+&i
|
|
|
|
eor rk+&i
|
|
|
|
sta state1+&i
|
|
|
|
&i seta &i+2
|
|
|
|
aif &i<16,.top
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do a full normal round, including (in effect) SubBytes through AddRoundKey
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
2017-06-27 06:36:56 +00:00
|
|
|
&lbl NormalRound &round
|
|
|
|
&lbl anop
|
2017-06-27 01:41:44 +00:00
|
|
|
aif &round/2*2=&round,.evenround
|
|
|
|
MixColumn 0,0,5,10,15,state1,state2
|
|
|
|
MixColumn 4,4,9,14,3,state1,state2
|
|
|
|
MixColumn 8,8,13,2,7,state1,state2
|
|
|
|
MixColumn 12,12,1,6,11,state1,state2
|
|
|
|
ago .done
|
|
|
|
.evenround
|
|
|
|
MixColumn 0,0,5,10,15,state2,state1
|
|
|
|
MixColumn 4,4,9,14,3,state2,state1
|
|
|
|
MixColumn 8,8,13,2,7,state2,state1
|
|
|
|
MixColumn 12,12,1,6,11,state2,state1
|
|
|
|
.done
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do the operations on one column for a normal round.
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
|
|
|
MixColumn &i,&A,&B,&C,&D,&state,&out
|
2017-06-27 02:11:19 +00:00
|
|
|
|
2017-06-27 02:49:16 +00:00
|
|
|
aif (&i=0).AND.(&round<>1),.skip
|
2017-06-27 02:11:19 +00:00
|
|
|
ldy &state+&D
|
2017-06-27 02:49:16 +00:00
|
|
|
.skip
|
2017-06-27 02:11:19 +00:00
|
|
|
lda Sbox,Y
|
|
|
|
pha
|
2017-06-27 01:53:17 +00:00
|
|
|
ldx &state+&A
|
2017-06-27 02:11:19 +00:00
|
|
|
eor Xtime2Sbox,X
|
2017-06-27 01:41:44 +00:00
|
|
|
ldy &state+&B
|
|
|
|
eor Xtime3Sbox,Y
|
2017-06-27 01:53:17 +00:00
|
|
|
ldy &state+&C
|
|
|
|
eor Sbox,Y
|
2017-06-27 01:41:44 +00:00
|
|
|
eor rk+&round*16+&i
|
|
|
|
sta &out+&i
|
2017-06-27 01:53:17 +00:00
|
|
|
|
2017-06-27 02:11:19 +00:00
|
|
|
pla
|
|
|
|
eor Xtime3Sbox,Y
|
2017-06-27 01:53:17 +00:00
|
|
|
eor Sbox,X
|
2017-06-27 01:41:44 +00:00
|
|
|
ldy &state+&B
|
|
|
|
eor Xtime2Sbox,Y
|
|
|
|
eor rk+&round*16+&i+1
|
|
|
|
sta &out+&i+1
|
2017-06-27 01:53:17 +00:00
|
|
|
|
2017-06-27 02:11:19 +00:00
|
|
|
lda Sbox,Y
|
|
|
|
pha
|
|
|
|
ldy &state+&D
|
|
|
|
eor Xtime3Sbox,Y
|
2017-06-27 01:53:17 +00:00
|
|
|
eor Sbox,X
|
2017-06-27 01:41:44 +00:00
|
|
|
ldy &state+&C
|
|
|
|
eor Xtime2Sbox,Y
|
|
|
|
eor rk+&round*16+&i+2
|
|
|
|
sta &out+&i+2
|
2017-06-27 01:53:17 +00:00
|
|
|
|
2017-06-27 02:11:19 +00:00
|
|
|
pla
|
2017-06-27 01:41:44 +00:00
|
|
|
eor Sbox,Y
|
2017-06-27 02:11:19 +00:00
|
|
|
eor Xtime3Sbox,X
|
2017-06-27 01:41:44 +00:00
|
|
|
ldy &state+&D
|
|
|
|
eor Xtime2Sbox,Y
|
2017-06-27 06:36:56 +00:00
|
|
|
aif (rk+&round*16+&i+3)>255,.bigindex
|
2017-06-27 01:41:44 +00:00
|
|
|
eor rk+&round*16+&i+3
|
2017-06-27 06:36:56 +00:00
|
|
|
ago .cont
|
|
|
|
.bigindex
|
|
|
|
ldx #&round*16+&i+3
|
|
|
|
eor rk,X
|
|
|
|
.cont
|
2017-06-27 01:41:44 +00:00
|
|
|
sta &out+&i+3
|
2017-06-27 02:49:16 +00:00
|
|
|
aif &i<>12,.skip2
|
|
|
|
tay
|
|
|
|
.skip2
|
2017-06-27 01:41:44 +00:00
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do final round, including (in effect) SubBytes, ShiftRows, and AddRoundKey.
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
|
|
|
FinalRound &round
|
2017-06-27 02:49:16 +00:00
|
|
|
|
|
|
|
FinalRoundStep 3,15,1
|
|
|
|
FinalRoundStep 15,11
|
|
|
|
FinalRoundStep 11,7
|
|
|
|
FinalRoundStep 7,3
|
|
|
|
|
2017-06-27 01:41:44 +00:00
|
|
|
FinalRoundStep 0,0
|
|
|
|
FinalRoundStep 4,4
|
|
|
|
FinalRoundStep 8,8
|
|
|
|
FinalRoundStep 12,12
|
2017-06-27 02:49:16 +00:00
|
|
|
|
2017-06-27 01:41:44 +00:00
|
|
|
FinalRoundStep 13,1
|
|
|
|
FinalRoundStep 1,5
|
|
|
|
FinalRoundStep 5,9
|
|
|
|
FinalRoundStep 9,13
|
2017-06-27 02:49:16 +00:00
|
|
|
|
2017-06-27 01:41:44 +00:00
|
|
|
FinalRoundStep 10,2
|
|
|
|
FinalRoundStep 2,10
|
|
|
|
FinalRoundStep 14,6
|
|
|
|
FinalRoundStep 6,14
|
|
|
|
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do the final round operations for one byte.
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
2017-06-27 02:49:16 +00:00
|
|
|
FinalRoundStep &to,&from,&skipldy
|
2017-06-27 01:41:44 +00:00
|
|
|
|
2017-06-27 02:49:16 +00:00
|
|
|
aif C:&skipldy,.skip
|
2017-06-27 01:41:44 +00:00
|
|
|
ldy state2+&from
|
2017-06-27 02:49:16 +00:00
|
|
|
.skip
|
2017-06-27 01:41:44 +00:00
|
|
|
lda Sbox,Y
|
2017-06-27 06:36:56 +00:00
|
|
|
aif (rk+&round*16+&to)>255,.bigindex
|
2017-06-27 01:41:44 +00:00
|
|
|
eor rk+&round*16+&to
|
2017-06-27 06:36:56 +00:00
|
|
|
ago .cont
|
|
|
|
.bigindex
|
|
|
|
ldx #&round*16+&to
|
|
|
|
eor rk,X
|
|
|
|
.cont
|
2017-06-27 01:41:44 +00:00
|
|
|
sta state1+&to
|
|
|
|
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Perform an inverse normal round (for decryption)
|
2017-06-27 23:17:00 +00:00
|
|
|
macro
|
|
|
|
InvNormalRound &round,&state
|
|
|
|
lcla &i
|
|
|
|
lclc &state
|
|
|
|
lclc &out
|
|
|
|
|
|
|
|
aif &round/2*2=&round,.evenround
|
|
|
|
&state setc state2
|
|
|
|
&out setc state1
|
|
|
|
ago .cont
|
|
|
|
.evenround
|
|
|
|
&state setc state1
|
|
|
|
&out setc state2
|
|
|
|
.cont
|
|
|
|
|
2017-06-28 03:35:57 +00:00
|
|
|
InvMixColumn 12,1,6,11,12,1
|
2017-06-27 23:17:00 +00:00
|
|
|
InvMixColumn 0,5,10,15,0
|
|
|
|
InvMixColumn 8,13,2,7,8
|
2017-06-28 03:35:57 +00:00
|
|
|
InvMixColumn 4,9,14,3,4,dotax=1
|
2017-06-27 23:17:00 +00:00
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Perform the operations for one column in an inverse normal round
|
2017-06-27 23:17:00 +00:00
|
|
|
macro
|
2017-06-28 03:35:57 +00:00
|
|
|
InvMixColumn &A,&B,&C,&D,&i,&skipldx,&dotax
|
2017-06-28 00:24:06 +00:00
|
|
|
|
2017-06-28 03:35:57 +00:00
|
|
|
aif C:&skipldx,.skip
|
2017-06-28 00:24:06 +00:00
|
|
|
ldx &state+&i+2
|
2017-06-28 03:35:57 +00:00
|
|
|
.skip
|
2017-06-28 00:24:06 +00:00
|
|
|
lda Xtime9,X
|
|
|
|
ldy &state+&i+0
|
|
|
|
eor XtimeB,Y
|
|
|
|
ldy &state+&i+1
|
|
|
|
eor XtimeD,Y
|
|
|
|
ldy &state+&i+3
|
|
|
|
eor XtimeE,Y
|
|
|
|
tay
|
|
|
|
lda InvSbox,Y
|
2017-06-28 01:44:14 +00:00
|
|
|
eor rk+(&round-1)*16+&D
|
2017-06-28 00:24:06 +00:00
|
|
|
sta &out+&D
|
2017-06-27 23:17:00 +00:00
|
|
|
|
|
|
|
ldy &state+&i+0
|
|
|
|
lda XtimeE,Y
|
|
|
|
ldy &state+&i+1
|
|
|
|
eor XtimeB,Y
|
2017-06-28 00:24:06 +00:00
|
|
|
eor XtimeD,X
|
2017-06-27 23:17:00 +00:00
|
|
|
ldy &state+&i+3
|
|
|
|
eor Xtime9,Y
|
|
|
|
tay
|
|
|
|
lda InvSbox,Y
|
2017-06-28 01:44:14 +00:00
|
|
|
eor rk+(&round-1)*16+&A
|
2017-06-27 23:17:00 +00:00
|
|
|
sta &out+&A
|
|
|
|
|
|
|
|
ldy &state+&i+0
|
|
|
|
lda Xtime9,Y
|
|
|
|
ldy &state+&i+1
|
|
|
|
eor XtimeE,Y
|
2017-06-28 00:24:06 +00:00
|
|
|
eor XtimeB,X
|
2017-06-27 23:17:00 +00:00
|
|
|
ldy &state+&i+3
|
|
|
|
eor XtimeD,Y
|
|
|
|
tay
|
|
|
|
lda InvSbox,Y
|
2017-06-28 01:44:14 +00:00
|
|
|
eor rk+(&round-1)*16+&B
|
2017-06-27 23:17:00 +00:00
|
|
|
sta &out+&B
|
|
|
|
|
|
|
|
ldy &state+&i+0
|
|
|
|
lda XtimeD,Y
|
|
|
|
ldy &state+&i+1
|
|
|
|
eor Xtime9,Y
|
2017-06-28 00:24:06 +00:00
|
|
|
eor XtimeE,X
|
2017-06-27 23:17:00 +00:00
|
|
|
ldy &state+&i+3
|
|
|
|
eor XtimeB,Y
|
|
|
|
tay
|
|
|
|
lda InvSbox,Y
|
2017-06-28 01:44:14 +00:00
|
|
|
eor rk+(&round-1)*16+&C
|
2017-06-28 03:35:57 +00:00
|
|
|
aif (C:&dotax).AND.(&round<>1),.dotax
|
2017-06-27 23:17:00 +00:00
|
|
|
sta &out+&C
|
2017-06-28 03:35:57 +00:00
|
|
|
ago .done
|
|
|
|
.dotax
|
|
|
|
tax
|
|
|
|
.done
|
2017-06-27 23:17:00 +00:00
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do the inverse final round steps for one byte.
|
2017-06-27 23:17:00 +00:00
|
|
|
macro
|
2017-06-28 03:35:57 +00:00
|
|
|
InvFinalRoundStep &to,&from,&dotax
|
2017-06-27 23:17:00 +00:00
|
|
|
|
|
|
|
lda state1+&from
|
2017-06-27 23:35:04 +00:00
|
|
|
aif (rk+&round*16+&from)>255,.bigindex
|
2017-06-27 23:17:00 +00:00
|
|
|
eor rk+&round*16+&from
|
2017-06-27 23:35:04 +00:00
|
|
|
ago .cont
|
|
|
|
.bigindex
|
|
|
|
ldx #&round*16+&from
|
|
|
|
eor rk,X
|
|
|
|
.cont
|
2017-06-27 23:17:00 +00:00
|
|
|
tay
|
|
|
|
lda InvSbox,Y
|
2017-06-28 01:44:14 +00:00
|
|
|
|
|
|
|
aif (rk+(&round-1)*16+&to)>255,.bigindex2
|
|
|
|
eor rk+(&round-1)*16+&to
|
|
|
|
ago .cont2
|
|
|
|
.bigindex2
|
|
|
|
ldx #(&round-1)*16+&to
|
|
|
|
eor rk,X
|
|
|
|
.cont2
|
2017-06-28 03:35:57 +00:00
|
|
|
aif C:&dotax,.dotax
|
2017-06-27 23:17:00 +00:00
|
|
|
sta state2+&to
|
2017-06-28 03:35:57 +00:00
|
|
|
ago .done
|
|
|
|
.dotax
|
|
|
|
tax
|
|
|
|
.done
|
2017-06-27 23:17:00 +00:00
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Do the inverse of the final round (which comes first for decryption).
|
2017-06-27 23:17:00 +00:00
|
|
|
macro
|
|
|
|
InvFinalRound &round
|
|
|
|
|
|
|
|
InvFinalRoundStep 0,0
|
|
|
|
InvFinalRoundStep 4,4
|
|
|
|
InvFinalRoundStep 8,8
|
|
|
|
InvFinalRoundStep 12,12
|
|
|
|
|
|
|
|
InvFinalRoundStep 1,13
|
|
|
|
InvFinalRoundStep 13,9
|
|
|
|
InvFinalRoundStep 9,5
|
|
|
|
InvFinalRoundStep 5,1
|
|
|
|
|
|
|
|
InvFinalRoundStep 15,3
|
|
|
|
InvFinalRoundStep 3,7
|
|
|
|
InvFinalRoundStep 7,11
|
|
|
|
InvFinalRoundStep 11,15
|
2017-06-28 03:35:57 +00:00
|
|
|
|
|
|
|
InvFinalRoundStep 2,10
|
|
|
|
InvFinalRoundStep 10,2
|
|
|
|
InvFinalRoundStep 6,14
|
|
|
|
InvFinalRoundStep 14,6,1
|
2017-06-27 23:17:00 +00:00
|
|
|
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Set registers to 8 bits
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
|
|
|
ShortRegs
|
|
|
|
sep #$30
|
|
|
|
longa off
|
|
|
|
longi off
|
|
|
|
mend
|
|
|
|
|
2017-06-28 04:28:27 +00:00
|
|
|
* Set registers to 16 bits
|
2017-06-27 01:41:44 +00:00
|
|
|
macro
|
|
|
|
LongRegs
|
|
|
|
rep #$30
|
|
|
|
longa on
|
|
|
|
longi on
|
|
|
|
mend
|