Equivalent PHP code based on Arduino code for AES256

Hello,

I am trying to convert some Arduino code over to some PHP code. So far everything has worked great except on piece, the AES 256 ecb decryption. Here’s the Arduino code I am trying to convert.

#define PASSWORD_HEX_SIZE 32
#define PASSWORD_SIZE 16
#define AES256_CRYPTO_KEY_SIZE 32
#define CHALLENGE_TOKEN_SIZE 16

    aes256_context ctx;
    aes256_init(&ctx, cryptoKey);
    aes256_decrypt_ecb(&ctx, password);
    aes256_done(&ctx);

    char passwordAsChar[PASSWORD_SIZE + 1];
    memset(&passwordAsChar, 0, sizeof(passwordAsChar));

    for (int i = 0; i < sizeof(password); ++i)
      passwordAsChar[i] = password[i];

    if (strcmp(passwordAsChar, PASSWORD) == 0)
    {....etc

Here is my example PHP trying to get it to work. The decrypted result should be “testpassword”, but I get garbage. Any ideas? The other end is an iPhone doing the encryption with the Cipher provided which is “Cyber142142” in the example below.

<?php

$submitPassword = "f9d23e949927e41c448df220a3c0cc3e";
$key = "Cyber142142";

echo "Password: " . $submitPassword . "\n";
echo "Cipher:   " . $key . "\n";

$encrypted_text = pack('H*',$submitPassword);
echo "ASCII: " . $encrypted_text . "\n";
//echo "ASCII(unpack): " . print_r(unpack('C*',$encrypted_text)) . "\n";

$td = mcrypt_module_open('rijndael-256', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_key_size($td));
mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $encrypted_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

echo "Decrypted Text: " . trim($decrypted) . "\n";
//echo "Decrypted Text(unpack): " . print_r(unpack('C*',trim($decrypted))) . "\n";

?>

Have you had any luck with this? I know a little php but I'm not sure the solution.

I ended up just porting the aes256 code to just a std c program that takes command line arguments. I then just invoke it via php and capture the output for comparison. Not pretty but it works and it was quick XD.

Would you mind sharing the ported C code? Thanks!

edwaa: Would you mind sharing the ported C code? Thanks!

The whole project that uses this AES is here. See the AES dir:

https://github.com/smanders7/garagedooropener

My first thought might be that there's a byte ordering issue? This can be with the key scheduling, not just the ciphertext...

If you have a problem like this with a cipher a way back to sanity is to find correct test-vectors for the algorithm and put those through the system - you can at least find out which end of the communication is doing it wrong.

Is there a reason to use ECB instead of CBC with an IV? Are you attempting privacy or authentication?