
|
Rijndael Cipher on a Basic Stamp 
Cryptography is a good thing. It protects your data from prying eyes. Now you can use the Advanced Encryption Standard (AES, aka Rijndael) to protect the data your Stamp sends out into the world.
I've written a code fragment that uses one program slot on a BS2e, BS2sx, BS2p, or BS2pe. (Maybe the BS2px, I'm not sure yet.)
Using the code is relatively easy, just download the program into a free slot on your stamp, put the block to be encrypted in the first 16 bytes of scratchpad RAM, put the key into the next 16 bytes, put the number of the slot you'd like to run when the encryption is done in location 32, and run the program.
This code is written in PBasic 2.0, not the newer 2.5. I'm currently using a self-written development environment with my stamps, and I haven't rewritten it to use Parallax's new PBasic 2.5 tokenizer. So, Please forgive me my GOTOs. :P
You can download the source file here.
I will be uploading an example program soon to demonstrate how to make use of this code.
For the impatient, here's the source for easy viewing:
'{$Stamp BS2e}
'
'******************************************************
'* PBasic Implementation of the Rijndael (AES) Cipher *
'******************************************************
'
'
'Copyright 2009, Rev. Zak T Zennii. <== Just in case....
'
'*************
'* Important *
'*************
'
' This program will overwrite ALMOST ALL of main RAM. (Sorry)
' Please provide the data to be encrypted in Scratchpad RAM locations 0...15
' Please provide the encryption key in Scratchpad RAM locations 16...31
' Please store the program slot number to be run after encryption completes
' in Scratchpad RAM location 32
' Locations 33 and above will remain unaltered by this program.
'
' Thank you for reading :)
'Stash the sBox in EEPROM
Data $63, $7C, $77, $7B, $F2, $6B, $6F, $C5, $30, $01, $67, $2B, $FE, $D7, $AB, $76
Data $CA, $82, $C9, $7D, $FA, $59, $47, $F0, $AD, $D4, $A2, $AF, $9C, $A4, $72, $C0
Data $B7, $FD, $93, $26, $36, $3F, $F7, $CC, $34, $A5, $E5, $F1, $71, $D8, $31, $15
Data $04, $C7, $23, $C3, $18, $96, $05, $9A, $07, $12, $80, $E2, $EB, $27, $B2, $75
Data $09, $83, $2C, $1A, $1B, $6E, $5A, $A0, $52, $3B, $D6, $B3, $29, $E3, $2F, $84
Data $53, $D1, $00, $ED, $20, $FC, $B1, $5B, $6A, $CB, $BE, $39, $4A, $4C, $58, $CF
Data $D0, $EF, $AA, $FB, $43, $4D, $33, $85, $45, $F9, $02, $7F, $50, $3C, $9F, $A8
Data $51, $A3, $40, $8F, $92, $9D, $38, $F5, $BC, $B6, $DA, $21, $10, $FF, $F3, $D2
Data $CD, $0C, $13, $EC, $5F, $97, $44, $17, $C4, $A7, $7E, $3D, $64, $5D, $19, $73
Data $60, $81, $4F, $DC, $22, $2A, $90, $88, $46, $EE, $B8, $14, $DE, $5E, $0B, $DB
Data $E0, $32, $3A, $0A, $49, $06, $24, $5C, $C2, $D3, $AC, $62, $91, $95, $E4, $79
Data $E7, $C8, $37, $6D, $8D, $D5, $4E, $A9, $6C, $56, $F4, $EA, $65, $7A, $AE, $08
Data $BA, $78, $25, $2E, $1C, $A6, $B4, $C6, $E8, $DD, $74, $1F, $4B, $BD, $8B, $8A
Data $70, $3E, $B5, $66, $48, $03, $F6, $0E, $61, $35, $57, $B9, $86, $C1, $1D, $9E
Data $E1, $F8, $98, $11, $69, $D9, $8E, $94, $9B, $1E, $87, $E9, $CE, $55, $28, $DF
Data $8C, $A1, $89, $0D, $BF, $E6, $42, $68, $41, $99, $2D, $0F, $B0, $54, $BB, $16
'Stash the xTime Table in EEPROM
Data $00, $02, $04, $06, $08, $0A, $0C, $0E, $10, $12, $14, $16, $18, $1A, $1C, $1E
Data $20, $22, $24, $26, $28, $2A, $2C, $2E, $30, $32, $34, $36, $38, $3A, $3C, $3E
Data $40, $42, $44, $46, $48, $4A, $4C, $4E, $50, $52, $54, $56, $58, $5A, $5C, $5E
Data $60, $62, $64, $66, $68, $6A, $6C, $6E, $70, $72, $74, $76, $78, $7A, $7C, $7E
Data $80, $82, $84, $86, $88, $8A, $8C, $8E, $90, $92, $94, $96, $98, $9A, $9C, $9E
Data $A0, $A2, $A4, $A6, $A8, $AA, $AC, $AE, $B0, $B2, $B4, $B6, $B8, $BA, $BC, $BE
Data $C0, $C2, $C4, $C6, $C8, $CA, $CC, $CE, $D0, $D2, $D4, $D6, $D8, $DA, $DC, $DE
Data $E0, $E2, $E4, $E6, $E8, $EA, $EC, $EE, $F0, $F2, $F4, $F6, $F8, $FA, $FC, $FE
Data $1B, $19, $1F, $1D, $13, $11, $17, $15, $0B, $09, $0F, $0D, $03, $01, $07, $05
Data $3B, $39, $3F, $3D, $33, $31, $37, $35, $2B, $29, $2F, $2D, $23, $21, $27, $25
Data $5B, $59, $5F, $5D, $53, $51, $57, $55, $4B, $49, $4F, $4D, $43, $41, $47, $45
Data $7B, $79, $7F, $7D, $73, $71, $77, $75, $6B, $69, $6F, $6D, $63, $61, $67, $65
Data $9B, $99, $9F, $9D, $93, $91, $97, $95, $8B, $89, $8F, $8D, $83, $81, $87, $85
Data $BB, $B9, $BF, $BD, $B3, $B1, $B7, $B5, $AB, $A9, $AF, $AD, $A3, $A1, $A7, $A5
Data $DB, $D9, $DF, $DD, $D3, $D1, $D7, $D5, $CB, $C9, $CF, $CD, $C3, $C1, $C7, $C5
Data $FB, $F9, $FF, $FD, $F3, $F1, $F7, $F5, $EB, $E9, $EF, $ED, $E3, $E1, $E7, $E5
n0 var byte
n1 var byte
n2 var byte
n3 var byte
rkey var byte(16)
x var byte
y var byte
j var byte
k var byte
round var byte
'first, read in the key:
for j = 0 to 15
get j+16, rkey(j)
next
for round = 1 to 10
'AddRoundKey and SubBytes:
for j = 0 to 15
get j, x
x = x ^ rKey(j)
read x, x
put j, x
next
'ShiftRows:
get 4, x
get 5, k
put 4, k
get 6, k
put 5, k
get 7, k
put 6, k
put 7, x
get 8, x
get 10, k
put 10, x
put 8, k
get 9, x
get 11, k
put 11, x
put 9, k
get 12, x
get 15, k
put 12, k
get 14, k
put 15, k
get 13, k
put 14, k
put 13, x
if round = 10 then updateKey
'MixColumns:
for j = 0 to 3
get j, n0
get j+4, n1
get j+8, n2
get j+12, n3
x = n0
y = n0 ^ n1 ^ n2 ^ n3
k = n0 ^ n1
read 256 + k, k
n0 = n0 ^ k ^ y
k = n1 ^ n2
read 256 + k, k
n1 = n1 ^ k ^ y
k = n2 ^ n3
read 256 + k, k
n2 = n2 ^ k ^ y
k = n3 ^ x
read 256 + k, k
n3 = n3 ^ k ^ y
put j+0, n0
put j+4, n1
put j+8, n2
put j+12, n3
next
updateKey:
'Update the RoundKey:
read rkey(7), n0
read rkey(11), n1
read rkey(15), n2
read rkey(3), n3
lookup round, [$00, $01, $02, $04, $08, $10, $20, $40, $80, $1B, $36], x
rkey(0) = rkey(0) ^ n0 ^ x
rkey(4) = rkey(4) ^ n1
rkey(8) = rkey(8) ^ n2
rkey(12) = rkey(12) ^ n3
rkey(1) = rkey(1) ^ rKey(0)
rkey(5) = rkey(5) ^ rKey(4)
rkey(9) = rkey(9) ^ rKey(8)
rkey(13) = rkey(13) ^ rKey(12)
rkey(2) = rkey(2) ^ rKey(1)
rkey(6) = rkey(6) ^ rKey(5)
rkey(10) = rkey(10) ^ rKey(9)
rkey(14) = rkey(14) ^ rKey(13)
rkey(3) = rkey(3) ^ rKey(2)
rkey(7) = rkey(7) ^ rKey(6)
rkey(11) = rkey(11) ^ rKey(10)
rkey(15) = rkey(15) ^ rKey(14)
next 'round
'One more AddRoundKey
for j = 0 to 15
get j, x
x = x ^ rKey(j)
put j, x
next
get 32, x
run x
end
|