PDA

View Full Version : Keeloq Encoder example code



Ioannis
- 10th November 2015, 20:08
Hi.

I am trying to make a Keeloq encoder in software but my approach seems wrong I guess.

I have an assembly code that is used to decode the encrypted data and though that if I use this routine on the encrypted data I may get the initial data back.

Obviously this is not working. I did not posted the code because it is licensed from Microchip.

So, if anyone knows how to encode using keeloq but without special chips like the 636, please reply.

Ioannis

richard
- 10th November 2015, 23:08
of course its been done on an arduino
https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=10&cad=rja&uact=8&ved=0CGoQFjAJahUKEwjP8963-IbJAhUk4qYKHeClAUA&url=https%3A%2F%2Fgithub.com%2Ffranksmicro%2FArdui no%2Ftree%2Fmaster%2Flibraries%2FKeeloq&usg=AFQjCNEx0y4UYaHCMAc6lpFcLGTKSBL0Fg&sig2=ZUiSH-Cj4vCzBoCCF07SvA

this link may help

Ioannis
- 11th November 2015, 07:50
Thank Richard.

A bit cryptic that C but will try to understand what is going on!

Ioannis

richard
- 11th November 2015, 08:29
I think this is pretty close for decrypt , but don't ask me how it works or why it loops 528 times



x var long
r var long
keyBitVal var long
bitVal var long
key var byte[8] ;key is long + long = 64 bits you need to input this somehow
index var word
KeeLoq_NLF const $3A5C742E


decrypt: ; input to decrypt is in var x -> output is in var x
while r<528
keyBitNo = (15-r) & 63
keyBitVal = key.0[keyBitNo]
index = 1 * x0.[1] + 2 * x.0[9] + 4 * x.0[20] + 8 * x.0[26] + 16 * x.0[31]
bitVal = x0.[0] ^ x.0[16] ^ KeeLoq_NLF.0[index] ^ keyBitVal
x = (x>>1) ^ bitVal<<31;
r=r+1
wend
return

richard
- 11th November 2015, 08:36
x = (x>>1) ^ bitVal<<31;

a clever person could knock up some code to do 32 bit shift left , right and a 32 bit xor then the whole thing could be done without pbpl at all

Ioannis
- 11th November 2015, 08:38
Thanks Richard for the effort on this.

I'll try to do the same on the encryption part which is what I am interested in.

About PBPL, sure I will try to do this in PBP since the code is inteded to run on a low end PIC.

Thanks again,
Ioannis

richard
- 11th November 2015, 08:50
I feel there is something missing here , the code is meant to be rolling encryption . so something needs to be saved for the next call doesn't it ?

Ioannis
- 11th November 2015, 08:58
The general idea from the limited data I could find is this.

You have your secret keys (8 bytes) that both the encrypter and decrypter know.

Then you have also 4 bytes of data (32 bits) that will be encoded byt the encoder (either hardware or software).

The result will be placed (after the crazy 512 shifts and rotatons) at the same registers of the 4 data bytes. They call it CSR3:CSR0 registers in the data sheet and ANs.

Regarding the decryption I am very close, almost working. But the Encoder is a bit harder to accomplish, mainly because resources are limited on a 12Fxxx chips.

Thanks,
Ioannis