Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

XTEA encryption

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
amir.eshraghi@aii1.com

Bascom Member



Joined: 06 Sep 2019
Posts: 5

PostPosted: Fri Jan 31, 2020 1:46 am    Post subject: XTEA encryption Reply with quote

Hi

I would like to know why Bascom XTEA encryption results are different than when we try writing in C language (Such as Arduino, or Mplab- Since I am trying to communicate with another device.)

In Bascom we define key[1] to key [16] , but base on Wiki we have to define key[0] - key[3]. Then I convert 16bit to 4 bit by equaling key[0] = key[1] key[2] key[3] key[4], same thing with a massage. Is that correct?

I am using the same code as https://en.wikipedia.org/wiki/XTEA, all I just add my massage and key and run function.

Bascom Key
Key(1) = &H63
Key(2) = &H3F
Key(3) = &H89
Key(4) = &HB0
Key(5) = &HD5
Key(6) = &HD3
Key(7) = &HAE
Key( Cool = &HC8
Key(9) = &H96
Key(10) = &H37
Key(11) = &HBE
Key(12) = &HA5
Key(13) = &H38
Key(14) = &HC4
Key(15) = &H4B
Key(16) = &H7E


C code Key and massage:
k[4] = {0xB03F8963, 0xC8AED3D5, 0xA5BE3796, 0x7E4BC438};
v[2] = {0xAF8823DC, 0x8F383F3A};

The result I am getting with C code is:
Encrypt: FA4B88ADAAA10BD2


but with Bascom, I am getting
Encrypt: 15B91B456C6367DB

Below is code from Wiki which I use to get the encrypted value in C.
Code:
#include <stdint.h>

/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */

void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}


Thank you

(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Jan 31, 2020 2:25 pm    Post subject: Re: XTEA encryption Reply with quote

amir.eshraghi@aii1.com wrote:

Key(1) = &H63
Key(2) = &H3F
Key(3) = &H89
Key(4) = &HB0


After these assignments the bytes in SRam follow this order:

h63 h3F h89 hB0

k[4] = {0xB03F8963, ... , ... , ...};

In this case the byte order in SRam is:

h63 h89 h3F hB0

Do you notice the difference?
Back to top
View user's profile
amir.eshraghi@aii1.com

Bascom Member



Joined: 06 Sep 2019
Posts: 5

PostPosted: Fri Jan 31, 2020 5:18 pm    Post subject: XTEA encryption Reply with quote

Do you mean the typo, I put the 89 and 3F backward which I fixed and still not getting the same encryption.

below is the Arduino code I wrote to double-check the encryption and not getting same result.

Encrypt: 3648050414A63EC0
Decrypt: 8F383F3AAF8823DC


Code:

uint32_t key[4] = {0xB0893F63, 0xC8AED3D5, 0xA5BE3796, 0x7E4BC438};
//                   B0893F63    C8AED3D5    A5BE3796    7E4BC438

uint32_t v[2] = {0xAF8823DC, 0x8F383F3A};
                      //    AF8823DC    8F383F3A
void setup()
{
  Serial.begin(9600);

   Serial.print("key: ");
  Serial.print(key[3], HEX);
  Serial.print(key[2], HEX);
  Serial.print(key[1], HEX);
  Serial.println(key[0], HEX);

  Serial.print("Data: ");
  Serial.print(v[1], HEX);
  Serial.println(v[0], HEX);

  encipher(32,v,key);
  Serial.print("Encrypt: ");
  Serial.print(v[1], HEX);
  Serial.println(v[0], HEX);

  decipher(32,v,key);
   Serial.print("Decrypt: ");
  Serial.print(v[1], HEX);
  Serial.println(v[0], HEX);


}
void loop()
{
}
void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}


Thanks
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Jan 31, 2020 7:33 pm    Post subject: Re: XTEA encryption Reply with quote

amir.eshraghi@aii1.com wrote:
Do you mean the typo, I put the 89 and 3F

For me it was actual data, otherwise you wouldn't put it here, would you?

I see no Bascom code to test and I'm not interested to set up arduino/c-code projects.
How would anyone know, whether you have more 'typos' in your code?

If you ask for help, then do your part and show a Bascom sample which shows the effect and is simple, cleaned up and ready to run.
Back to top
View user's profile
amir.eshraghi@aii1.com

Bascom Member



Joined: 06 Sep 2019
Posts: 5

PostPosted: Fri Jan 31, 2020 7:51 pm    Post subject: XTEA encryption Reply with quote

The reason I include the other code was to show how I am compering

Below is my Bascom code


Code:


Key(1) = &H63
Key(2) = &H3F
Key(3) = &H89
Key(4) = &HB0
Key(5) = &HD5
Key(6) = &HD3
Key(7) = &HAE
Key(8) = &HC8
Key(9) = &H96
Key(10) = &H37
Key(11) = &HBE
Key(12) = &HA5
Key(13) = &H38
Key(14) = &HC4
Key(15) = &H4B
Key(16) = &H7E

Esn_array(1) = &HDC
Esn_array(2) = &H23
Esn_array(3) = &H88
Esn_array(4) = &HAF
Esn_array(5) = &H3A
Esn_array(6) = &H3F
Esn_array(7) = &H38
Esn_array(8) = &H8F

Xteaencode Esn_array(1) , Key(1) , 8



 
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Fri Jan 31, 2020 7:59 pm    Post subject: Reply with quote

This can't be your code, as it does not contain variable, processor, clock or stack declaration.
You can play stupid, but then you'll be on your own.
Back to top
View user's profile
amir.eshraghi@aii1.com

Bascom Member



Joined: 06 Sep 2019
Posts: 5

PostPosted: Fri Jan 31, 2020 8:05 pm    Post subject: XTEA encryption Reply with quote

Code is very large with many include files for custom pcb and touchscreen and is proprietary. Here is a snippet.

Code:

'******************************************************************************
'************** Compiler Directives & Fuses *****************
'************************************************************

Dim Reset_flags As Byte
Reset_flags = Peek(0)                                       'determine how reset occurred
Mcusr = 0                                                   'clear reset flags

$regfile = "M2560def.dat"                                   'ATMEGA2560 (TQFP)
$crystal = 7372800                                          'oscillator speed (MHz)
Config Clockdiv = 1                                         'clock divider
Config Clock = User                                         'use compiler time functions w/ user set time & date

$hwstack = 600                                              'hardware stack size (500)
$swstack = 600                                              'software stack size (500)
$framesize = 1400                                           'frame size (1300)
'$lib "stackcheck.lib"                                       'stack usage checking
 



Below is the result of encryption

Esn_array(1) = &HDB
Esn_array(2) = &H67
Esn_array(3) = &H63
Esn_array(4) = &H6C
Esn_array(5) = &H45
Esn_array(6) = &H1B
Esn_array(7) = &HB9
Esn_array(Cool = &H15
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Sat Feb 01, 2020 11:07 am    Post subject: Reply with quote

The sole purpose of the XTEA routines in BASCOM is that you can encrypt and decrypt data. you can also automatic encrypt for a boot loader.
The goal was not to make it work for all different TEA variations that exist.

The asm code was based on some delphi code. and this code had memory order reversed. so while still XTEA this makes it harder to share with other platform code.
For this reason another xtea library is included with bascom. if you read the help you can find this info :


When you use other tools to encode your data, you will find differences because of memory order. You can use the xtea2.lib for using the same memory order.
Include it in your code like : $LIB "xtea2.lib"


It is not the default since once projects are compiled with the other versions (boot loader and code) it would break compatibility.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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
You cannot attach files in this forum
You cannot download files in this forum