0

I'm transcribing a C code for Matlab from a multiplication by 2 in the Gallois field, the problem is that my matlab code is not displaying the same value as the C code. Apparently everything is ok, I commented the code in matlab to identify the adaptations of the C code, below the codes.

C:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  unsigned char value = 0xaa;
  signed char temp;
  // cast to signed value
  temp = (signed char) value;
  printf("\n%d",temp);
  // if MSB is 1, then this will signed extend and fill the temp variable with 1's
  temp = temp >> 7;
  printf("\n%d",temp);
  // AND with the reduction variable
  temp = temp & 0x1b;
  printf("\n%d",temp);
  // finally shift and reduce the value
  printf("\n%d",((value << 1)^temp));
}

Output:

-86
-1
27
335

MatLab:

hex = uint8(hex2dec('1B'));                     % define 0x1b
temp = uint8(hex2dec('AA'));                    % temp = (signed char) value;
disp(temp);
value = uint8(hex2dec('AA'));                   % unsigned char value = 0xaa
temp = bitsra(temp,7);                          % temp = temp >> 7;
disp(temp);
temp = bitand(temp,hex);                        % temp = temp and 0x1b  
disp(temp);
galois_value =  bitxor(bitsll(value,1),temp);   % ((value << 1)^temp)
disp(galois_value);                             % printf ("\n%u",...)                    

Output:

 170

    1

    1

   85

The C code works correctly, I'm printing %d in the C code to show the integer value of the varible because the cast in the beggining of the code.

Someone knows what is happening

Mutante
  • 278
  • 5
  • 18
  • 1
    Why do you think that `temp = uint8(hex2dec('AA'));` is equivalent to `temp = (signed char) value;`? – Mad Physicist Jun 05 '17 at 16:05
  • You need to look at where the computations begin to diverge on their own instead of making us do it for you. – Mad Physicist Jun 05 '17 at 16:06
  • Add a `printf` after every line in C and `disp` in MATLAB. – Mad Physicist Jun 05 '17 at 16:07
  • Because, I can't convert hex to int, only to double. – Mutante Jun 05 '17 at 16:07
  • Even if I use double, the final value is different. – Mutante Jun 05 '17 at 16:08
  • 1
    I mean why do you think `uint8` is the same as `signed char`? – Mad Physicist Jun 05 '17 at 16:09
  • 1
    I'm using `uint8` because the `bitand` and `bitxor` only accept values from the same type, if I cast to `int8` instead of `uint8` the code doesn't works. – Mutante Jun 05 '17 at 16:11
  • When you provide a set of printouts for each line showing where the results begin to divege, an an analysis of why you think it is happening (in your question, not in the comments), I will answer it. – Mad Physicist Jun 05 '17 at 16:13
  • When I print the value of the variable `temp` in the C code, the value shown doesn't exist, I'm going to update the question to show you. – Mutante Jun 05 '17 at 16:15
  • Thanks. I'm not saying to print `temp` every time. I'm saying to print the result of every line. In MATLAB too. – Mad Physicist Jun 05 '17 at 16:15
  • Perhaps you should first get the C code working with data types acceptable to Matlab. – Weather Vane Jun 05 '17 at 16:16
  • If you have `uint8 value;`, you can copy the highest bit into all bits using `uint8_t temp = (value & 128) ? 255 : 0;` or even `uint8_t temp = (((unsigned int)(value & 128) << 9) - ((unsigned int)(value & 128) << 2)) >> 8;` Do not make assumptions about two's complement format. – Nominal Animal Jun 05 '17 at 16:16
  • Or, use `uint8_t temp = (value & 128) ? 0x1B : 0x00;` or perhaps even `uint8_t temp = ((unsigned int)(value & 128) * 54) >> 8;` – Nominal Animal Jun 05 '17 at 16:18
  • @MadPhysicist I've edited the post to show the value of the variables after every operation. – Mutante Jun 05 '17 at 16:19
  • @NominalAnimal the C code is from a texas library to a MSP430 MCU, I can't change the C code, I need to adapt the matlab code to work like the C code from texas. – Mutante Jun 05 '17 at 16:20
  • 2
    You need to add the output to the question, as well as an explanation of why the values come out differently when they do. – Mad Physicist Jun 05 '17 at 16:20
  • @MadPhysicist I've edited again with the output values. – Mutante Jun 05 '17 at 16:27
  • In Matlab, you can calculate `temp` using `temp = uint8((bitand(value, 128) * 54) / 256);`. That way, `temp == 0x1B` if `value` has bit 7 set, otherwise `temp == 0`. – Nominal Animal Jun 05 '17 at 16:32
  • @NominalAnimal the output value gets `84` with this `temp` function, same as if I use all the variables as `int8`. – Mutante Jun 05 '17 at 16:35

1 Answers1

2

Try this:

hex = uint8(hex2dec('1B')); 

temp = typecast(uint8(hex2dec('AA')), 'int8');
disp(temp);

temp = bitshift(temp,-7); 
disp(temp);

temp = bitand(typecast(temp,'uint8'),hex); 
disp(temp);

galois_value =  bitxor(bitshift(uint16(hex2dec('AA')),1),uint16(temp));   
disp(galois_value);  
wyc
  • 351
  • 2
  • 11
  • Like I said before, I didn't create the C code, It's from Texas Company, It's from a library of AES criptography used in MSP430 series MCU, should I assume that when I implement the rest of the code the encryption will work? Because the output value of the original texas function is different, what was to be 335 is now being 85. – Mutante Jun 05 '17 at 20:26
  • @Mutante, sorry. I missed that you are converting from C to Matlab. I've updated my post. Take a look again please. – wyc Jun 05 '17 at 20:59
  • The `typecast` function was very useful, I didn't know of its existence, thanks @wyc! – Mutante Jun 06 '17 at 15:43