5

i am trying to handle a problem using encryption and decryption algorithms, i used below program to test my requirements and i realised an odd problem.

i am using polarssl for my encryption and decryption needs.

This program works well when i compile it with icc compiler on intel processor using this command :(aes.c from polarssl)

icc Deneme.c aes.c -o deneme

Output:

Encryption result, encrypted value:
11 22 7 86 a2 d3 ed 95 b9 14 c0 57 f7 af 5f dc 93 66 77 68 44 12 9f 1b 72 6a ea 51 b8 f7 1d a4
Decryption result, plain array:
a2 d8 69 9c 77 73 c7 5e 1d 3b 83 26 6e 2f 35 30 9d f0 f2 e5 0 0 0 0 0 0 0 0 0 0 0 0

When i tried to compile with icc compiler but this time run on microprocessor; encryption result are not true.

command:

icc -mmic Deneme.c aes.c -o deneme

Output:

Encryption result, encrypted value:
4a 4b 4c 32 e0 ef fb 22 4a 67 7e 2c 3a fe 36 65 4b 5b 18 9 63 28 a1 c4 8e b7 e4 c6 33 a7 36 7c
Decryption result, plain array:
a2 d8 69 9c 77 73 c7 5e 1d 3b 83 26 6e 2f 35 30 9d f0 f2 e5 0 0 0 0 0 0 0 0 0 0 0 0

What makes my encrypted values different??

here is full program

#include <stdio.h>
#include <polarssl/aes.h>

const int ENCRIPTION = 1;
const int DECRIPTION = 0;

void printByteArray(unsigned char * array, int length) {
    int i = 0;
    for (i = 0; i < length; i++) {
        printf("%x ", array[i]);
    }
    printf("\n");
}

void encrypt() {
    unsigned char o[32];


    unsigned char key[16] = { 0x03, 0xC9, 0xD8, 0xE6, 0x01, 0xA5, 0x05,
            0x9F, 0x11, 0xBF, 0x44, 0x9D, 0xF9, 0x55, 0x18, 0xED

    };

    unsigned char iv[16] = { 0x03, 0xB5, 0x62, 0x57, 0xC8, 0x69, 0x22, 0x89,
            0xF4, 0x96, 0x2B, 0x05, 0x44, 0x2B, 0xD0, 0xA7

    };

    unsigned char plain[32]=
    {
      0xA2, 0xD8, 0x69, 0x9C, 0x77, 0x73, 0xC7, 0x5E, 0x1D, 0x3B, 0x83, 0x26, 0x6E, 0x2F, 0x35, 0x30,
      0x9D, 0xF0, 0xF2, 0xE5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    aes_context aesCtx;

    aes_setkey_enc(&aesCtx, key, 128);
    aes_crypt_cbc(&aesCtx, ENCRIPTION, 32, iv, plain, o);

    printf("Encryption result, encrypted value:\n");
    printByteArray(o, 32);
}

decrypt() {

    unsigned char o[32];

    unsigned char key[16] = { 0x03, 0xC9, 0xD8, 0xE6, 0x01, 0xA5, 0x05,
            0x9F, 0x11, 0xBF, 0x44, 0x9D, 0xF9, 0x55, 0x18, 0xED

    };

    unsigned char iv[16] = { 0x03, 0xB5, 0x62, 0x57, 0xC8, 0x69, 0x22, 0x89,
            0xF4, 0x96, 0x2B, 0x05, 0x44, 0x2B, 0xD0, 0xA7

    };

    unsigned char encryptedValue[32] = { 0x11, 0x22, 0x07, 0x86,
            0xA2, 0xD3, 0xED, 0x95, 0xB9, 0x14, 0xC0, 0x57, 0xF7, 0xAF, 0x5F,
            0xDC, 0x93, 0x66, 0x77, 0x68, 0x44, 0x12, 0x9F, 0x1B, 0x72, 0x6A,
            0xEA, 0x51, 0xB8, 0xF7, 0x1D, 0xA4 };

    aes_context aesCtx;

    aes_setkey_dec(&aesCtx, key, 128);
    aes_crypt_cbc(&aesCtx, DECRIPTION, 32, iv, encryptedValue,
            o);

    printf("Decryption result, plain array:\n");
    printByteArray(o, 32);

}

int main() {

    encrypt();
    decrypt();
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
nerd
  • 115
  • 1
  • 7
  • So the `-mmic` encryption results in a different ciphertext, but for decryption, do you use this different ciphertext, or do you use the original one? I presume the first option, the different one. – Maarten Bodewes Aug 06 '14 at 14:17
  • i used original inputs; but regardless of they are original or not ,they need to be reversable – nerd Aug 06 '14 at 14:31
  • So, if I understand correctly, you cannot decrypt what is encrypted with `-mmic`? That's certainly weird. Sounds like a bug to me. If you specify the IV, then encryption should be deterministic. So the ciphertext should be identical for that reason alone. – Maarten Bodewes Aug 06 '14 at 17:09
  • no, actually. the first output is true output, input is 'plain' (which starts with a2 d8 ...) and i tried to encrypt it and run on mic it gives wrong encrypted value(4a 4b ...) but it gives true answer in host processor. (11 22 ...) – nerd Aug 07 '14 at 06:49
  • Your code demonstrates that decryption works using the same input. Can you please change your code to try to feed the output of `encrypt()` into `decrypt()` to see if the changed output with `-mmic` actually makes a difference. – Aaron Digulla Aug 07 '14 at 07:50
  • @AaronDigulla AES is deterministic. There's one correct output for any given input; if it's diferent, it's wrong. –  Nov 07 '14 at 23:47
  • @duskwuff: I know but that doesn't help to find the bug. So I'm just covering all the bases. The next clue we find might be the one which solves the riddle. – Aaron Digulla Nov 10 '14 at 10:15
  • 3
    I've suggested that this difference of behaviour between processors maybe is due to a difference of endianness between the processors. – tinyfiledialogs Nov 11 '14 at 23:39

1 Answers1

0

icc by default uses optimization for performance on your code and this limits correctness. However, you can use -O0 option to compile without any optimization on both and compare the results.

-mmic generates instruction set for MIC ie Xeon phi architecture.