jsiConsolePrint(); usage example?

Posted on
  • Let's say, for example, I have the following code:

    uint8_t in[]  = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
                        0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
                        0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
                        0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
    

    And I want to output that array using:
    '''jsiConsolePrint();'''

    Does jsIConsolePrint() only allow a string input? Do I need to for each through the array then store each segment of the array to a string?

    The reason why I ask is. I tried to use a for each to loop through the array and output each segment of the array to the WebIDE and I expected to see the ascii representation of the hex value. However, I received what looked like all %.

    The ascii representation should be:
    kÁ¾â.@é=~s®-W¬·o¬E¯Q0ÈF£\äåûÁ
    Rïö$EßO+A{æl7

    I even tried placing the hex representation of the alphabet into the array; however, I still received all % as the output.

    Eventually, my real world code will receive an encrypted JSON string and I would like to output the unencrypted JSON string to the WebIDE console. I have AES128-CBC encryption and decryption working (so I think, I don't want to jinx myself). I'm just having trouble outputting the text to the console.

  • To my knowledge it will only allow a string. Consider using jsiConsolePrintf which is pretty much like the usual printf. Does that help?

  • Im AFK at the moment so I will get back to you later today with the results.

  • @Stevie i'm still getting the same issue, but now I am receiving
    MMM MMMMMMMMMMMMMMMUMMMM

    Full code:

    [#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 test_encrypt_cbc(void);
    
    
    
    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, 0x61, 0x72, 0x72, 0x61, 0x79, 0x22, 0x3a, 0x5b, 0x31, 0x2c, 0x32, 0x2c, 0x33, 0x5d,
                           0x2c, 0x22, 0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65, 0x2c,
                           0x22, 0x6e, 0x75, 0x6c, 0x6c, 0x22, 0x3a, 0x6e, 0x75, 0x6c, 0x6c, 0x2c, 0x22, 0x6e, 0x75, 0x6d,
                           0x62, 0x65, 0x72, 0x22, 0x3a, 0x31, 0x32, 0x33, 0x2c, 0x22, 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74,
                           0x22, 0x3a, 0x7b, 0x22, 0x61, 0x22, 0x3a, 0x22, 0x62, 0x22, 0x2c, 0x22, 0x63, 0x22, 0x3a, 0x22,
                           0x64, 0x22, 0x2c, 0x22, 0x65, 0x22, 0x3a, 0x22, 0x66, 0x22, 0x7d, 0x2c, 0x22, 0x73, 0x74, 0x72,
                           0x69, 0x6e, 0x67, 0x22, 0x3a, 0x22, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c,
                           0x64, 0x22, 0x7d };
      uint8_t buffer[128];
    
      AES128_CBC_encrypt_buffer(buffer, in, 128, key, iv);
    
      int i;
    
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf(in[i]);
      jsiConsolePrintf("\n");
    
      jsiConsolePrint("CBC encrypt: ");
    }
    

    1 Attachment

    • 2015-05-27 23_59_29-Espruino Web IDE.png
  • jsiConsolePrintf is like printf (I'd google it)... It needs a format string at the start which specifies what the next arguments actually are.

    In Espruino I added some special format characters for JS variables, see this

    So you want to do something like:

    for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%d, ", in[i]);
      jsiConsolePrintf("\n");
    

    That'll output a comma-separated list of numbers. If you want to output just the actual character you can use %c - jsiConsolePrintf("%c", in[i]);.

    jsiConsolePrint itself expects a C string - that's a pointer to a list of characters that is terminated with a 0 character, so you won't be able to just pass in the character itself.

    If you did want to use it, you'd have to do:

    char buf[2];
    buf[0] = myCharacter;
    buf[1] = 0;
    jsiConsolePrint(buf);
    

    (Of course you could also use jsiConsolePrintChar, which is probably what you want)

    ... anyway, it's just one of the many reasons that programming embedded devices in C is painful :)

  • 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 };
    
      jsiConsolePrint("\n int: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%d,", in[i]);
      jsiConsolePrintf("\n");
    
      jsiConsolePrint("\n as hex: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%x,", in[i]);
      jsiConsolePrintf("\n");
    
      jsiConsolePrint("\n as string: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%s,", in[i]);
      jsiConsolePrintf("\n");
    
      jsiConsolePrint("\n as char: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%c,", in[i]);
      jsiConsolePrintf("\n");
    

    output:

    int:
    123,35,82,75,38,63,93,37,90,108,121,126,­96,121,44,35,78,195,192,206,161,6,193,15­9,231,158,226,59,181,37,61,141,111,97,24­7,132,126,142,86,56,172,92,17,73,79,66,4­1,211,97,176,235,108,38,156,243,61,207,2­06,186,222,1,250,166,113,184,220,198,15,­154,97,79,210,150,107,198,10,235,131,134­,21,231,112,183,50,100,147,164,100,233,2­11,42,58,163,71,225,236,176,146,103,250,­186,141,42,68,41,122,14,13,121,204,22,15­1,117,104,183,133,135,191,184,132,72,67,­74,170,236,83,242,47,
    as hex:
    7b,23,52,4b,26,3f,5d,25,5a,6c,79,7e,60,7­9,2c,23,4e,c3,c0,ce,a1,6,c1,9f,e7,9e,e2,­3b,b5,25,3d,8d,6f,61,f7,84,7e,8e,56,38,a­c,5c,11,49,4f,42,29,d3,61,b0,eb,6c,26,9c­,f3,3d,cf,ce,ba,de,1,fa,a6,71,b8,dc,c6,f­,9a,61,4f,d2,96,6b,c6,a,eb,83,86,15,e7,7­0,b7,32,64,93,a4,64,e9,d3,2a,3a,a3,47,e1­,ec,b0,92,67,fa,ba,8d,2a,44,29,7a,e,d,79­,cc,16,97,75,68,b7,85,87,bf,b8,84,48,43,­4a,aa,ec,53,f2,2f,
    as string:
    ,,,,,,%,,,M%,%,,M%,%,M%,,,,M%,,%,,%,,,,,­,%,,,,,%,,M%,,,,M%,M%,M%,%,%,,,,,%,M%,,M­%,,M%,,,,,,,
    %,,,%,M%,,,,,%,,,,,,,,,,%,,M%,,,M%,,M%,M­%,,,,,,,%,,M%,,,,,,,M%,,,,%,%,M%,,,%,M%,­,%,,,M%,M%,M%,,,,,,,,
    as char:
    {,#,R,K,&,?,],%,Z,l,y,~,`,y,,,#,N,Ã,À,Î,­¡,,Á,,ç,,â,;,µ,%,=,,o,a,÷,,~,,V,8,¬,\,,I­,O,B,),Ó,a,°,ë,l,&,,ó,=,Ï,Î,º,Þ,,ú,¦,q,¸­,Ü,Æ,,,a,O,Ò,,k,Æ,
    ,y,Ì,,,u,h,·,…,,¿,¸,,H,C,J,ª,ì,S,ò,/,°,,­g,ú,º,,*,D,),z,,

    The array of hex should be the equivalence of ascii: {"PH":["Result",5.5],"EC":["Result",700]­,"ResTemp":["Result",137.5]}

    However I am still not receiving the correct ascii.

    • decimal 123,35,72,75
    • ascii text of decimal {#HK
    • What you got: {,#,R,K,

    So it appears to be printing numbers reliably - they're just not the right ones. Is that code really exactly what you have? You're not doing something to the array inbetween?

    What happens if you change jsiConsolePrintf("%d,", in[i]); to jsiConsolePrintf("%d,", (int)in[i]); (and so on for the others).

    As I said above, trying to print as a string - with jsiConsolePrint (or %s) is just really bad news. You're basically printing the contents of memory starting from the address with the same number as your character - hence why you're getting garbage like ,,,,,,%,,,M. That kind of thing can usually totally crash the chip as well as printing garbage :)

  • 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");
      }*/
    }
    
  • Are you sure that above is the correct source code? The one you actually tested? It seems to me that the int and hex output are the same values. So let's concentrate on the hex one, only. In that case it seems as if what you are outputting is

    jsConsolePrintf("%d", in[i] + i);
    

    Does that ring a bell? Are you somewhere modifying the in array? Otherwise it would be an extremely odd bug of jsiConsolePrintf. One which adds an increasing number for each call. And which resets itself which you switch from %d to %x... Unlikely...

  • @Stevie I edited my source code so many times while trying to figure all this out. I attached the complete source code of the library

    I also thought the same about the +1. I don't think I am editing the array. All I am doing is iterating through the array to output whats in the array(usually hex to the ascii equivalent) and the first character is { which is correct.


    1 Attachment

  • Ahh, ok, so you do call AES128_CBC_encrypt_buffer(buffer, in, 128, key, iv); on the data?

    Maybe just simplify to this:

    static void test_encrypt_cbc(void)
    {
      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 };
      int i;
      
      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 char: ");
      for(i = 0; i < 128; ++i)
        jsiConsolePrintf("%c,", (int)in[i]);
      jsiConsolePrintf("\n");
      int n = sizeof(in);
      jsiConsolePrint(n);
    }
    

    and make sure the correct data is output? My guess is AES128_CBC_encrypt_buffer might be fiddling with the data inside in

  • @Gordon yup, AES128_CBC_Encrypt_Buffer is fiddling with the data inside of in, errr. And I wonder why that is.....

    {"PH":["Result",5.5],"EC":["Result",700]­,"ResTemp":["Result",137.5]}
    int:
    123,34,80,72,34,58,91,34,82,101,115,117,­108,116,34,44,53,46,53,93,44,34,69,67,34­,58,91,34,82,101,115,117,108,116,34,44,5­5,48,48,93,44,34,82,101,115,84,101,109,1­12,34,58,91,34,82,101,115,117,108,116,34­,44,49,51,55,46,53,93,125,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,
    int as hex:
    7b,22,50,48,22,3a,5b,22,52,65,73,75,6c,7­4,22,2c,35,2e,35,5d,2c,22,45,43,22,3a,5b­,22,52,65,73,75,6c,74,22,2c,37,30,30,5d,­2c,22,52,65,73,54,65,6d,70,22,3a,5b,22,5­2,65,73,75,6c,74,22,2c,31,33,37,2e,35,5d­,7d,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,
    int as char:
    {,",P,H,",:,[,",R,e,s,u,l,t,",,,5,.,5,],­,,",E,C,",:,[,",R,e,s,u,l,t,",,,7,0,0,],­,,",R,e,s,T,e,m,p,",:,[,",R,e,s,u,l,t,",­,,1,3,7,.,5,],},,,,,,,,,,,,,,,,,,,,,,,,,­,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

    And I don't have the slightest idea why AES_128_CBC_encrypt_Buffer is doing that.....

  • And I don't have the slightest idea why AES_128_CBC_encrypt_Buffer is doing that.....

    Because in line 512 XorWithIv modifies it? Maybe that should be

        BlockCopy(output, input);
        XorWithIv(output);
    
  • I don't know what it says in the docs, but it could be that in order to save memory they're fiddling with the input data intentionally? I guess it's not a problem, it just screws up your debugging :)

  • https://github.com/kokke/tiny-AES128-C is the library I am using. The test.c compiles correctly. I am just trying to implement my own "stuff" to encrypt and decrypt as a test instead of using the test data.

    I uploaded the test.c file

    Eventually, I would like to create a AESCBC library into Espruino, but since i'm new to programming in general I seem to be hitting a brick wall :/ Maybe one day I will get this to work. Or maybe someone who is ambitious enough can get this to work ;-)


    1 Attachment

  • Well, in any case, the input buffer is in fact modified so jsiConsolePrintf works fine.

    And looking at the source code of the AES code, I believe modifying the input buffer is not necessary, and as such probably unintended. I would bet on that. But technically it is not a bug unless there is a promise that it does not modify the input buffer.

  • @Gordon @Stevie I finally got this to work :-) I needed to work with the buffer[] array. Now, all I need to figure out is create a random IV and MAC the encrypted data.

    How do I work with function arguments? I would like to pass in the Key, IV, hashkey and what every data needs to be encrypted. I want to execute Crypto.aescbc(key, iv, hashKey, data);

  • ...

  • since i'm new to programming in general I seem to be hitting a brick wall

    @d0773d, I'd like to commend you for the perseverance you demonstrate. I guess I have to for a class to you despite having a past of programming...

  • Best bet is to have a look at src/jwrap_*.c - there are loads of functions in there that work the same way. Basically in the comment at the start you describe what the arguments are. I guess you'll either want int32 (a 32 bit integer) or JsVar (a JavaScript variable, that might be something like an array).

    Actually unpacking something like an array into a C array is a bit more painful though - you'd probably want to see something like this as an example

    /*JSON{
      "type" : "staticmethod",
      "class" : "Crypto",
      "name" : "aescbc",
      "generate" : "jswrap_crypto_aescbc",
      "params" : [
        ["key","int32","A description for users"],
        ["iv","int32","A description for users"],
        ["hashKey","int32","A description for users"],
        ["data","JsVar","A description for users"]
       ]
    }*/
    void jswrap_crypto_aescbc(int key, int iv, int hashkey, JsVar *data)
    {
      if (!jsvIsIterable(data)) {
        jsExceptionHere(JSET_ERROR, "Expecting an array/string for 4th argument, got %t", data);
        return;
      }
    
      char data[128];
      JsvIterator it;
      jsvIteratorNew(&it, data);
      unsigned int i = 0;
      while (jsvIteratorHasElement(&it) && i<sizeof(data)) {
        data[i++] = (char)jsvIteratorGetIntegerValue(&it);
        jsvIteratorNext(&it);
      }
      jsvIteratorFree(&it);    
    
      // do stuff
    
      // write result back
      jsvIteratorNew(&it, data);
      i=0;
      while (jsvIteratorHasElement(&it) && i<sizeof(data)) {
        jsvUnLock(jsvIteratorSetValue(&it, jsvNewFromInteger(data[i++])));
        jsvIteratorNext(&it);
      }
      jsvIteratorFree(&it);
    }
    

    I haven't actually tested that, but it should work...

  • @allObjects Thanks. I couldn't sleep last night until I figured all this out. Plus I am going camping for a week and a half. Which will give me a decent break from my project; allowing me to approach my project again with a clearer mind.

    @Gordon I'm all coded out :/ I have yet to try your example, but when I do, I'm sure I will respond back with more questions ;-)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

jsiConsolePrint(); usage example?

Posted by Avatar for d0773d @d0773d

Actions