DES using MBEDTLS

I have edited simpleserial-base.c file to call functions of des.h, following is my code:

#include “hal.h”
#include <stdint.h>
#include <stdlib.h>
#include “mbedtls/des.h”
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i)
{
(n) = ( (uint32_t) (b)[(i) ] << 24 )
| ( (uint32_t) (b)[(i) + 1] << 16 )
| ( (uint32_t) (b)[(i) + 2] << 8 )
| ( (uint32_t) (b)[(i) + 3] );
}
#endif
#define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
static const uint32_t LHs[16] =
{
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
};
static const uint32_t RHs[16] =
{
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100,
0x00000001, 0x01000001, 0x00010001, 0x01010001,
0x00000101, 0x01000101, 0x00010101, 0x01010101,
};
void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
int i;
uint32_t X, Y, T;
GET_UINT32_BE( X, key, 0 );
GET_UINT32_BE( Y, key, 4 );
T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
| (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
| (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
| (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
| (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
| (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
X &= 0x0FFFFFFF;
Y &= 0x0FFFFFFF;
for( i = 0; i < 16; i++ )
{
if( i < 2 || i == 8 || i == 15 )
{
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
}
else
{
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
}
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
*SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
}
}
int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
mbedtls_des_setkey( ctx->sk, key );
return( 0 );
}

int mbedtls_des_setkey_dec( mbedtls_des_context ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
int i;
mbedtls_des_setkey( ctx->sk, key );
for( i = 0; i < 16; i += 2 )
{
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
}
return( 0 );
}
uint8_t reset(uint8_t
x, uint8_t len)
{
// Reset key here if needed
return 0x00;
}
mbedtls_des_context ctxt[16];
static const unsigned char KEY[8] = {0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E}; //{‘0’,‘A’, ‘2’,‘3’, ‘4’, ‘5’, ‘6’, ‘7’}; //{0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E};

int main(void)
{
platform_init();
init_uart();
trigger_setup();
simpleserial_init();
while(1){
trigger_high();
simpleserial_addcmd(‘k’, 8, mbedtls_des_setkey_enc);
trigger_low();
simpleserial_addcmd(‘p’, 8, mbedtls_des_setkey_dec);
simpleserial_addcmd(‘x’, 0, reset);
// putch(‘r’);
return 1;
}
}

I am using following code for capturing traces:

SCOPETYPE = ‘OPENADC’
PLATFORM = ‘CWLITEARM’
CRYPTO_TARGET= ‘MBEDTLS’ #‘TINYAES128C’ ‘AVRCRYPTOLIB’
SS_VER=‘SS_VER_1_1’
%run “…/…/Setup_Scripts/Setup_Generic.ipynb”
%%sh -s “$PLATFORM” “$CRYPTO_TARGET” “$SS_VER”
cd …/…/…/hardware/victims/firmware/simpleserial-base
make PLATFORM=$1 CRYPTO_TARGET=$2 SS_VER=$3 -j
cw.program_target(scope, prog, “…/…/…/hardware/victims/firmware/simpleserial-base/simpleserial-base-{}.hex”.format(PLATFORM))

from tqdm import tnrange
import numpy as np
import time
ktp = cw.ktp.Basic()
ktp.fixed_key = False
trace_array =
textin_array =
key, text = ktp.new_pair()
print (“setting key:” + str(key))
N = 2500
for i in tnrange(N, desc=‘Capturing traces’):
scope.arm()
target.simpleserial_write(‘k’, key)
target.simpleserial_write(‘p’, text)
ret = scope.capture()
if ret:
print(“Target timed out!”)
continue
response = target.simpleserial_read(‘r’, 8)
trace_array.append(scope.get_last_trace())
textin_array.append(text)
key, text = ktp.next()

and getting following error (2500 times):

(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command:

kindly tell me if I am sending wrong command or is there any issue in .c file?

There may be other issues, but at least one is with your C callback functions (mbedtls_des_setkey_enc, …). Callback functions need to have specific parameters and return the correct type. The functions that you pass to simpleserial_addcmd() need to take (uint8_t *, uint8_t) and return a uint8_t.

Alex

@Alex_Dewar how can I call mbedtls_des_setkey_enc function?

You’ll need to make a function like:

uint8_t my_enc_func(uint8_t *buf, uint8_t len)
{
    trigger_high();
    // whatever operations you want to study
    trigger_low();
    return 0;
}

// inside main() ...

simpleserial_addcmd('p', N, my_enc_func); // N is the length of whatever data you need sent over the serial lines

while (1) {simpleserial_get();}

It also looks like you weren’t calling simpleserial_get() in your code above. This is important, as this is what actually reads the serial messages and calls functions added by simpleserial_addcmd().

Alex

1 Like

thanks @Alex_Dewar for your guidance

@Alex_Dewar I have changed my .c file as per your guidance but am still getting “Trigger timed out issue”.

C code:

#include “hal.h”
#include <stdint.h>
#include <stdlib.h>
#include “mbedtls/des.h”
#ifndef GET_UINT32_BE
#define GET_UINT32_BE(n,b,i)
{
(n) = ( (uint32_t) (b)[(i) ] << 24 )
| ( (uint32_t) (b)[(i) + 1] << 16 )
| ( (uint32_t) (b)[(i) + 2] << 8 )
| ( (uint32_t) (b)[(i) + 3] );
}
#endif
#define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
static const uint32_t LHs[16] =
{
0x00000000, 0x00000001, 0x00000100, 0x00000101,
0x00010000, 0x00010001, 0x00010100, 0x00010101,
0x01000000, 0x01000001, 0x01000100, 0x01000101,
0x01010000, 0x01010001, 0x01010100, 0x01010101
};
static const uint32_t RHs[16] =
{
0x00000000, 0x01000000, 0x00010000, 0x01010000,
0x00000100, 0x01000100, 0x00010100, 0x01010100,
0x00000001, 0x01000001, 0x00010001, 0x01010001,
0x00000101, 0x01000101, 0x00010101, 0x01010101,
};
void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
int i;
uint32_t X, Y, T;
GET_UINT32_BE( X, key, 0 );
GET_UINT32_BE( Y, key, 4 );
T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
| (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
| (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
| (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
| (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
| (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
X &= 0x0FFFFFFF;
Y &= 0x0FFFFFFF;
for( i = 0; i < 16; i++ )
{
if( i < 2 || i == 8 || i == 15 )
{
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
}
else
{
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
}
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
**SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
}
}
mbedtls_des_context ctxt[16];
static const unsigned char KEY[8] = {0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E};
int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
mbedtls_des_setkey( ctx->sk, key );
return 0;
}
int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
{
int i;
mbedtls_des_setkey( ctx->sk, key );
for( i = 0; i < 16; i += 2 )
{
SWAP( ctx->sk[i ], ctx->sk[30 - i] );
SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
}
return 0;
}
uint8_t my_enc_func(uint8_t *buf, uint8_t len)
{
trigger_high();
int A = mbedtls_des_setkey_enc(&ctxt, KEY);
trigger_low();
return 0;
}
uint8_t my_des_func(uint8_t buf, uint8_t len)
{
int B = mbedtls_des_setkey_dec(&ctxt, KEY);
return 0;
}
uint8_t reset(uint8_t
x, uint8_t len)
{
// Reset key here if needed
return 0x00;
}
int main(void)
{
platform_init();
init_uart();
trigger_setup();
simpleserial_init();
simpleserial_addcmd(‘k’, 8, my_enc_func);
simpleserial_addcmd(‘p’, 16, my_des_func);
simpleserial_addcmd(‘x’, 0, reset);
while(1){
simpleserial_get();
}
return 1;
}

python code for capturing traces:

from tqdm import tnrange
import numpy as np
import time
ktp = cw.ktp.Basic()
ktp.fixed_key = False
ktp.fixed_text = False
trace_array =
textin_array =
ktp.key_len = 8
ktp.text_len = 8
key, text = ktp.new_pair()
print (“setting key:” + str(key))
target.set_key(key)
#target.simpleserial_write(‘k’, key) # (I have tried using this method as well, but it is still not working)
N = 5
for i in tnrange(N, desc=‘Capturing traces’):
scope.arm()
target.simpleserial_write(‘p’, text)
ret = scope.capture()
if ret:
print(“Target timed out!”)
continue
response = target.simpleserial_read(‘r’, 16)
textin_array.append(text)
key, text = ktp.next() #ktp.new_pair()

error:

(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0b
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 08
(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command: z
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0b
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 08
(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command: z
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0b
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 08
(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command: z
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0b
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 08
(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command: z
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0b
(ChipWhisperer Scope WARNING|File _OpenADCInterface.py:642) Timeout in OpenADC capture(), no trigger seen! Trigger forced, data is invalid. Status: 0a
(ChipWhisperer Target WARNING|File SimpleSerial.py:410) Unexpected start to command: z