|
|
View previous topic :: View next topic |
Author |
Message |
JBM
Joined: 12 May 2004 Posts: 54 Location: edinburgh, Scotland
|
XTEA encryption library with smartcard example |
Posted: Tue Dec 13, 2005 12:16 pm |
|
|
I've only seen a tiny number of posts in this forum even mentioning encryption on PICs. So here's one.
I've recenty written an implementation of the XTEA algorithm for PICs, which seems to do the whole 'encrypt-decrypt' business quite well.
Below is a link to an example project I've finished which uses my library. It's an entrirely PIC-based smartcard door locking / person verification system. The zip file is avaliable here. (Right-click, Save Link As...)
I'd be interested to see where this code gets used. Just add a quick post to say where you're using it. Thanks.
Code: | /*********************************************************************
* Public Domiain Work
* The XTEA algorithm is public domain, as is this
* implementation for PICs using the CCS compilers.
*
********************************************************************/
int32 DELTA = 0x9e3779b9;
int32 key[4];
/*********************************************************************
* Function: void Encode(int32* data, int8 dataLength)
* Encodes data pointed to by *data. dataLength is
* measured in int32s, and must be an even number. This
* means the minimum number of bytes to be encoded is 8.
* eg encoding 8 bytes:
* int8 some_bytes[8];
* Encode(some_bytes,2);
*
* uses the key[] variable - ensure it is appropriately loaded
*
********************************************************************/
void Encode(int32* data, int8 dataLength)
{
int8 i=0;
int32 x1;
int32 x2;
int32 sum;
int8 iterationCount;
while(i<dataLength)
{
sum = 0;
x1=data[i];
x2=data[i+1];
iterationCount = NUM_ITERATIONS;
while(iterationCount > 0)
{
x1 += (((x2<<4) ^ (x2>>5)) + x2) ^ (sum + key[(sum&0x03)]);
sum+=DELTA;
x2 += (((x1<<4) ^ (x1>>5)) + x1) ^ (sum + key[((sum>>11)&0x03)]);
iterationCount--;
}
data[i]=x1;
data[i+1]=x2;
i+=2;
}
}
/*********************************************************************
* Function: void Decode(int32* data, int8 dataLength)
* Decodes data pointed to by *data. dataLength is
* measured in int32s, and must be an even number. This
* means the minimum number of bytes to be decoded is 8.
* eg decoding 8 bytes:
* int8 some_bytes[8];
* Decode(some_bytes,2);
*
* uses the key[] variable - ensure it is appropriately loaded
*
********************************************************************/
void Decode(int32* data, int8 dataLength)
{
int8 i=0;
int32 x1;
int32 x2;
int32 sum;
int8 iterations;
iterations = NUM_ITERATIONS;
while(i<dataLength)
{
sum = DELTA*iterations;
x1=data[i];
x2=data[i+1];
while(sum != 0)
{
x2 -= (((x1<<4) ^ (x1>>5)) + x1) ^ (sum + key[((sum>>11)&0x03)] );
sum-=DELTA;
x1 -= (((x2<<4) ^ (x2>>5)) + x2) ^ (sum + key[(sum&0x03)]);
}
data[i]=x1;
data[i+1]=x2;
i+=2;
}
} |
Last edited by JBM on Wed Feb 15, 2006 3:44 pm; edited 3 times in total |
|
|
iso9001
Joined: 02 Dec 2003 Posts: 262
|
|
Posted: Wed Feb 15, 2006 3:29 pm |
|
|
Very cool.
Thanks I've been looking into a secure bootloader and this will come in handy for sure.
One question tho. You say the min number of bytes to be encoded is 8 but then send a dataLength of 2..... Soooooo which is it?
Also, you have a typeo in the decode section, says "encoded" bla bla bla, |
|
|
JBM
Joined: 12 May 2004 Posts: 54 Location: edinburgh, Scotland
|
|
Posted: Wed Feb 15, 2006 3:38 pm |
|
|
Both are true - the minimum nuber of bytes to be encoded is 8, but datalength is measuered in int32's.
8 bytes = 8 * 8 bits = 64 bits
int32 * 2 = 64 bits
This is because XTEA was originally made for 32-bit machines. This means you need to make sure that any time you call either funcion, DataLength must be an even number.
Good point about the encoded though!
-JBM
P.S. Please use this code (this bit is public domain), and when you've got the secure bloader going (looks a very interesting indeed), stick it in the code library. I'm hoping that one day this board can move to a wiki, and further encourage sharing of code. |
|
|
JBM
Joined: 12 May 2004 Posts: 54 Location: edinburgh, Scotland
|
|
Posted: Wed Feb 15, 2006 4:09 pm |
|
|
I don't quite see what you mean in your last post.
I'll try and clarify:
Code: | char data[8]={1,2,3,4,5,6,7,8};
encode(data,2);
//contents of data are scrambled
decode(data,2);
//contents of data are back to exaclty what they were before |
There is no need for any other operation on the data at all. Have a look at the example I've coded for how it's implemented.
-JBM
<looks like you removed your previous post as I was writing the reply>
Last edited by JBM on Wed Feb 15, 2006 4:11 pm; edited 1 time in total |
|
|
iso9001
Joined: 02 Dec 2003 Posts: 262
|
|
Posted: Wed Feb 15, 2006 4:09 pm |
|
|
Sorry about that I answered it myself then realized how dumb I was being
One question however, Isn't there a rather large instruction penalty for using arraysing in pics ? Somthing like 8 or 9 operations ? That was my understanding, but looking at it I dont really see a way to do it w/o arrays. So nevermind I guess
I wouldn't be too excited to see my bootloader.... not going to be pretty. It'll work, but defienetly won't be pretty.
There is a guy who made a xtea bootloader on microchips forum, it uses xmodem and whatnot. Mine won't use all that, real simple. |
|
|
eXtenZy
Joined: 24 May 2009 Posts: 1
|
|
Posted: Sun May 24, 2009 5:42 pm |
|
|
Ok, newbie question :
If I am trying to encode the following string "1234567890-qwertyuiopasdfghjklzxcvbnm", 37 characters long.
I copy the string into a int32* variable, letting aside all the warnings and whatnot, I manage to encode the string like this:
Code: | Encode(sir, sizeof(char)*strlen(sir)); //equivallent to Encode(sir, 74); |
Again, all works ok, but it takes 125ms to encode the string in simulation (32 iterations), with the PIC running at 20Mhz (PIC18f67J50), which seems a lot . Am I doing something wrong (apart from the data conversions that would make a C guru pull his hair out )?
Thanks. |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Mon Dec 14, 2009 7:09 am |
|
|
I have tried this encryption on a PIC16F687 using the following:-
in encode and decode functions i have added
in my main
Code: |
char testdata[8]={1,2,3,4,5,6,7,8};
char encrypted;
char decrypted;
.
.
.
printf("Testdata :- %s\n\r", testdata);
encrypted = encode(testdata, 2);
printf("Encrypted :- %s\n\r", encrypted);
decrypted = decode(encrypted, 2);
printf("Decrypted :- %s\n\r", decrypted);
|
I run this using simulator but the output from the decrypted is not the test data. Am I doing something wrong?
Thanks
SH |
|
|
JBM
Joined: 12 May 2004 Posts: 54 Location: edinburgh, Scotland
|
|
Posted: Mon Dec 14, 2009 9:51 am |
|
|
sjharris: Your code isn't wrong, you're just misunderstanding what the functions do: The encrypt and decrypt functions modify the contents of 'testdata' - try running this fragment and you'll see what I mean.
Code: | printf("Testdata :- %s\n\r", testdata);
encode(testdata, 2);
printf("Encrypted :- %s\n\r", testdata);
|
The data in testdata is encrypted and overwrites the original contents of testdata.
Also, you would need to make 'encrypted' and 'decrypted' pointers, rather than characters before they would be of any use to you.
-JBM |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|