• I have built a new firmware for the Puck.js in order to add a C function that does some Elliptic Curve math that would be too slow in JS. I have gotten this firmware to build and load onto the puck successfully, but when I try to run the function I wrote it crashes for some reason (puck becomes unresponsive, disconnects from Web IDE). The function is included below for reference. I used LED lighting to narrow down where exactly it stops working and it appears to be on the line with "mbedtls_ecp_group_load".

    When I build Espruino with a LINUX target, this code works completely fine, so there doesn't appear to be anything wrong with the code itself. I'm having trouble debugging what could be wrong here. Is there any way to attach a debugger to the Puck or something else I could do here? It seems very difficult to debug C code on the Puck because if anything goes wrong it just disconnects from the Web IDE.

    Thank you.

    /*JSON{
      "type" : "staticmethod",
      "class" : "crypto",
      "name" : "PointGen",
      "generate" : "jswrap_crypto_PointGen",
      "params" : [
        ["pkey","JsVar","Private key to multiply P-224 generator with, should be a string up to 100 chars"]
      ],
      "return" : ["JsVar","Returns an X coordinate string"],
      "ifdef" : "USE_TLS"
    }
    
    Key generator
    */
    JsVar *jswrap_crypto_PointGen(JsVar * pkey){
    
      jshPinOutput(LED1_PININDEX, 1);
    
      //jsiConsolePrintf("Allocating buffers...\n");
      char* pkeystr = malloc(20);
      char* outputbuf = malloc(80);
    
    
    
      //jsiConsolePrintf("Retrieving string from jvar...\n");
      jsvGetString(pkey, pkeystr, 19);
    
      //jsiConsolePrintf("%s\n", pkeystr);
    
      mbedtls_mpi * mpikey = malloc(sizeof(mbedtls_mpi));
      mbedtls_mpi_init(mpikey);
    
     
    
      //jsiConsolePrintf("Converting to MPI...\n");
      int ret = mbedtls_mpi_read_string(mpikey, 10, pkeystr);
      if(ret != 0)
      {
        return jsvVarPrintf("MPI READ - %d\n", ret);
      }
    
      mbedtls_ecp_group * grp = malloc(sizeof(mbedtls_ecp_group));
    
      jshPinOutput(LED2_PININDEX, 1);
    
      //jsiConsolePrintf("Loading group parameters...\n");
      ret = mbedtls_ecp_group_load(grp, MBEDTLS_ECP_DP_SECP224R1);
      if(ret != 0)
      {
        jshPinOutput(LED2_PININDEX, 0);
        jshPinOutput(LED3_PININDEX, 1);
        return jsvVarPrintf("GROUP LOAD FAILED - %d\n", ret);
      }
    
      jshPinOutput(LED3_PININDEX, 1);
    
      mbedtls_ecp_point * pt = malloc(sizeof(mbedtls_ecp_point));
      mbedtls_ecp_point_init(pt);
    
      //jsiConsolePrintf("Multiplying points...\n");
      ret = mbedtls_ecp_mul(grp, pt, mpikey, &grp->G, NULL, NULL);
      if(ret != 0)
      {
        return jsvVarPrintf("ECP MUL - %d\n", ret);
      }
    
      //jsiConsolePrintf("Writing to string...\n");
      size_t outputlen;
      ret = mbedtls_mpi_write_string(&pt->X, 10, outputbuf, 120, &outputlen);
      if(ret != 0)
      {
        return jsvVarPrintf("MPI WRITE - %d\n", ret);
      }
    
      JsVar * returnstring = jsvNewFromString(outputbuf);
    
      free(pkeystr);
      free(outputbuf);
    
      return returnstring;
    }
    
About

Avatar for Cryptizard @Cryptizard started