You are reading a single comment by @d0773d and its replies. Click here to read the full conversation.
  • casting an int doesn't change anything. I'm still receiving the same output

    This is my complete code of jswrap_crypto.c:

    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <stdio.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <string.h>
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) <stdint.h>
    
    // Enable both ECB and CBC mode. Note this can be done before including aes.h or at compile-time.
    // E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DECB=1
    [#define](http://forum.espruino.com/sear­ch/?q=%23define) CBC 1
    [#define](http://forum.espruino.com/sear­ch/?q=%23define) ECB 0 //ECB is not a very secure cipher to use.
    
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "aes.h"
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "jswrap_crypto.h"
    [#include](http://forum.espruino.com/sea­rch/?q=%23include) "jsinteractive.h"
    
    static void phex(uint8_t* str);
    static void test_encrypt_cbc(void);
    static void test_decrypt_cbc(void);
    
    
    // Let's define the JavaScript class that will contain our `aescbc()` method. We'll call it `Hello`
    /*JSON{
      "type" : "class",
      "class" : "Crypto"
    }*/
    
    // Now, we define the `jswrap_crypto_aescbc` to be a `staticmethod` on the `Hello` class
    /*JSON{
      "type" : "staticmethod",
      "class" : "Crypto",
      "name" : "aescbc",
      "generate" : "jswrap_crypto_aescbc"
    }*/
    void jswrap_crypto_aescbc(void)
    {
        test_encrypt_cbc();
        //test_decrypt_cbc();
    }
    
    
    
    // prints string as hex
    static void phex(uint8_t* str)
    {
        unsigned char i;
        for(i = 0; i < 16; ++i)
            jsiConsolePrint(str[i]);
        jsiConsolePrint("\n");
    }
    
    
    
    static void test_decrypt_cbc(void)
    {
      // Example "simulating" a smaller buffer...
    
      uint8_t key[]    = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
      uint8_t iv[]     = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
    
      uint8_t in[128] =  { 0x96, 0x25, 0xC9, 0x62, 0x8B, 0x82, 0x0D, 0xCB, 0x08, 0x6C, 0x51, 0x26, 0xF6, 0x52, 0x80, 0x79,
                           0xE6, 0xBF, 0x39, 0xFA, 0x47, 0x95, 0x14, 0x4B, 0x98, 0xAF, 0xBF, 0xA7, 0x8C, 0x5A, 0x44, 0x20,
                           0xD4, 0x14, 0x86, 0x9F, 0xFF, 0x7E, 0xC2, 0x02, 0xF5, 0xA4, 0x34, 0xEE, 0xF3, 0xF5, 0xD1, 0x1D,
                           0xE0, 0xE5, 0x78, 0xCD, 0xFA, 0x30, 0xBE, 0x59, 0x34, 0x9E, 0xDA, 0xE0, 0x56, 0xC1, 0x06, 0xA8,
                           0x9F, 0xEA, 0x9B, 0xBB, 0x25, 0x54, 0x37, 0xCF, 0x33, 0x61, 0x5F, 0xC5, 0x77, 0x7B, 0xC7, 0xC1,
                           0x2D, 0x4A, 0x67, 0xCA, 0xFA, 0x06, 0x60, 0x02, 0x23, 0x83, 0x44, 0x09, 0x75, 0xFC, 0xB2, 0xB0,
                           0xC2, 0xF6, 0x0B, 0x1A, 0x45, 0x5C, 0x50, 0xEB, 0x64, 0x97, 0xA9, 0x81, 0xE9, 0xF9, 0x8C, 0xB6,
                           0x9E, 0x57, 0xD9, 0x48, 0xBC, 0x30, 0x5E, 0x17, 0xD3, 0x5A, 0xE4, 0x9D, 0x1E, 0xFE, 0x5D, 0x04 };
    
      uint8_t out[128] = { 0x96, 0x25, 0xC9, 0x62, 0x8B, 0x82, 0x0D, 0xCB, 0x08, 0x6C, 0x51, 0x26, 0xF6, 0x52, 0x80, 0x79,
                           0xE6, 0xBF, 0x39, 0xFA, 0x47, 0x95, 0x14, 0x4B, 0x98, 0xAF, 0xBF, 0xA7, 0x8C, 0x5A, 0x44, 0x20,
                           0xD4, 0x14, 0x86, 0x9F, 0xFF, 0x7E, 0xC2, 0x02, 0xF5, 0xA4, 0x34, 0xEE, 0xF3, 0xF5, 0xD1, 0x1D,
                           0xE0, 0xE5, 0x78, 0xCD, 0xFA, 0x30, 0xBE, 0x59, 0x34, 0x9E, 0xDA, 0xE0, 0x56, 0xC1, 0x06, 0xA8,
                           0x9F, 0xEA, 0x9B, 0xBB, 0x25, 0x54, 0x37, 0xCF, 0x33, 0x61, 0x5F, 0xC5, 0x77, 0x7B, 0xC7, 0xC1,
                           0x2D, 0x4A, 0x67, 0xCA, 0xFA, 0x06, 0x60, 0x02, 0x23, 0x83, 0x44, 0x09, 0x75, 0xFC, 0xB2, 0xB0,
                           0xC2, 0xF6, 0x0B, 0x1A, 0x45, 0x5C, 0x50, 0xEB, 0x64, 0x97, 0xA9, 0x81, 0xE9, 0xF9, 0x8C, 0xB6,
                           0x9E, 0x57, 0xD9, 0x48, 0xBC, 0x30, 0x5E, 0x17, 0xD3, 0x5A, 0xE4, 0x9D, 0x1E, 0xFE, 0x5D, 0x04 };
      uint8_t buffer[128];
    
      AES128_CBC_decrypt_buffer(buffer,  in,  128, key, iv);
      //AES128_CBC_decrypt_buffer(buffer+16, in+16, 16, 0, 0);
      //AES128_CBC_decrypt_buffer(buffer+32, in+32, 16, 0, 0);
      //AES128_CBC_decrypt_buffer(buffer+48, in+48, 16, 0, 0);
      //AES128_CBC_decrypt_buffer(buffer+64, in+64, 16, 0, 0);
    
      jsiConsolePrint("CBC decrypt: ");
    
      if(0 == strncmp((char*) out, (char*) buffer, 128))
      {
        jsiConsolePrint("SUCCESS!\n");
      }
      else
      {
        jsiConsolePrint("FAILURE!\n");
      }
    }
    
    static void test_encrypt_cbc(void)
    {
      uint8_t key[]    = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
      uint8_t iv[]     = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
    
      uint8_t in[128]  = { 0x7B, 0x22, 0x50, 0x48, 0x22, 0x3A, 0x5B, 0x22, 0x52, 0x65, 0x73, 0x75, 0x6C, 0x74, 0x22, 0x2C,
                           0x35, 0x2E, 0x35, 0x5D, 0x2C, 0x22, 0x45, 0x43, 0x22, 0x3A, 0x5B, 0x22, 0x52, 0x65, 0x73, 0x75,
                           0x6C, 0x74, 0x22, 0x2C, 0x37, 0x30, 0x30, 0x5D, 0x2C, 0x22, 0x52, 0x65, 0x73, 0x54, 0x65, 0x6D,
                           0x70, 0x22, 0x3A, 0x5B, 0x22, 0x52, 0x65, 0x73, 0x75, 0x6C, 0x74, 0x22, 0x2C, 0x31, 0x33, 0x37,
                           0x2E, 0x35, 0x5D, 0x7D, 0x30 };
    
      uint8_t buffer[128];
    
      AES128_CBC_encrypt_buffer(buffer, in, 128, key, iv);
    
      int i;
      
      //Just a test which outputs the correct ascii
      char buf[4];
      buf[0] = 0X7B;
      buf[1] = 0x22;
      buf[2] = 0x50;
      buf[3] = 0;
      jsiConsolePrint(in);
    
      jsiConsolePrint("\n int: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%d,", (int)in[i]);
      jsiConsolePrintf("\n");
    
      jsiConsolePrint("\n int as hex: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%x,", (int)in[i]);
      jsiConsolePrintf("\n");
    
      /*jsiConsolePrint("\n int as string: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%s,", in[i]);
      jsiConsolePrintf("\n");*/
    
      jsiConsolePrint("\n int as char: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%c,", (int)in[i]);
      jsiConsolePrintf("\n");
    
      int n = sizeof(in);
      jsiConsolePrint(n);
      
      jsiConsolePrint("CBC encrypt: ");
    
      /*if(0 == strncmp((char*) out, (char*) buffer, 128))
      {
        jsiConsolePrint("SUCCESS!\n");
      }
      else
      {
        jsiConsolePrint("FAILURE!\n");
      }*/
    }
    
About

Avatar for d0773d @d0773d started