Micro-ECC is an open source implementation for ECC running in an embedded microcontroller. This is a port for mbed. Please do more test and update assembly optimization for Cortex-M, aka, ARM-thumb.

Dependencies:   mbed uECC

Files at this revision

API Documentation at this revision

Comitter:
allankliu
Date:
Thu Sep 07 12:10:39 2017 +0000
Child:
1:d14633bd13d0
Commit message:
Init version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
uECC.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 07 12:10:39 2017 +0000
@@ -0,0 +1,138 @@
+/*
+ *  Micro-ECC ported to mbed platform
+ *  Original Author:    Ken MacKay
+ *  Original Project:   https://github.com/kmackay/micro-ecc
+ *  Ported by:  Allan K Liu
+ *
+ *  Micro-ECC is ported to mbed to evalute its performance 
+ *  Micro-ECC is optimized for ARM/ARM-thumb/ARM-thumb2/AVR platform
+ *  Micro-ECC mbed version disabled thumb/thumb2 optimization because of its GCC syntax. 
+ *      PS: I am not good at assembly for those projects.
+ */
+
+#include "mbed.h"
+#include "uECC.h"
+
+Serial pc(USBTX, USBRX);
+AnalogIn rnd(A1);
+Timer t;
+
+void dumprand()
+{
+    uint8_t buf[16];
+    
+    pc.printf("plain_random:");
+    for(int i=0; i<16; i++){
+        buf[i] = rand();
+        pc.printf("%02X",buf[i]);
+    }
+    pc.printf("\r\n");
+    
+}
+    
+static int RNG(uint8_t *dest, unsigned size) {
+  // Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of 
+  // random noise). This can take a long time to generate random data if the result of analogRead(0) 
+  // doesn't change very frequently.
+  pc.printf("Random:\r\n");
+  while (size) {
+    uint8_t val = 0;    
+    for (unsigned i = 0; i < 8; ++i) {
+      //int init = rnd.read();
+      int init = rand();
+      pc.printf("%04X",init);
+      int count = 0;
+      //while (rnd.read() == init) {
+      while (rand() == init) {  
+        ++count;
+      }
+      
+      if (count == 0) {
+         val = (val << 1) | (init & 0x01);
+      } else {
+         val = (val << 1) | (count & 0x01);
+      }
+    }
+    *dest = val;
+    ++dest;
+    --size;
+    pc.printf("\r\n");
+  }
+  
+  // NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar.
+  return 1;
+}
+
+void dumphex(const char* name, uint8_t* buf, uint8_t size){
+  pc.printf(name);
+  for(int i=0; i<size; i++){
+    pc.printf("%02X",buf[i]);  
+  }
+  pc.printf("\r\n");  
+}
+    
+void loop(){
+  const struct uECC_Curve_t * curve = uECC_secp160r1();
+  int r;
+  long d;
+  
+  uint8_t private1[21];
+  uint8_t private2[21];
+  
+  uint8_t public1[40];
+  uint8_t public2[40];
+  
+  uint8_t secret1[20];
+  uint8_t secret2[20];
+
+  pc.printf("Start ECC computation\r\n");
+  pc.printf("make key 1\r\n");
+  t.start();
+  uECC_make_key(public1, private1, curve);
+  dumphex("public1: ", public1, sizeof(public1));
+  dumphex("private1: ", private1, sizeof(private1));
+  t.stop(); d = t.read_ms();
+  t.reset(); t.start();  
+  pc.printf("time: %dms\r\n",d);
+  
+  pc.printf("make key 2\r\n");
+  t.start();
+  uECC_make_key(public2, private2, curve);
+  dumphex("public2: ", public2, sizeof(public2));
+  dumphex("private2: ", private2, sizeof(private2));
+  t.stop(); d = t.read_ms();
+  t.reset(); t.start();  
+  pc.printf("time: %dms\r\n",d);
+  
+  pc.printf("make share secret 1\r\n");
+  t.start();
+  r = uECC_shared_secret(public2, private1, secret1, curve);
+  pc.printf("r: %04X\r\n",r);
+  t.stop(); d = t.read_ms();
+  t.reset(); t.start();  
+  pc.printf("time: %dms\r\n",d);
+  
+  pc.printf("make share secret 2\r\n");
+  t.start();
+  r = uECC_shared_secret(public1, private2, secret2, curve);
+  pc.printf("r: %04X\r\n",r);
+  t.stop(); d = t.read_ms();
+  t.reset(); t.start();  
+  pc.printf("time: %dms\r\n",d);
+  
+  pc.printf("\r\n\r\n");
+  wait(1);
+}
+
+int main() {
+    pc.baud(115200);
+    dumprand();
+    wait(1);
+    pc.printf("\r\n\r\nmicroECC test\r\n");
+    uECC_set_rng(&RNG);
+    pc.printf("\r\n");
+    
+    while(1) {
+        loop();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 07 12:10:39 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/a330f0fddbec
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uECC.lib	Thu Sep 07 12:10:39 2017 +0000
@@ -0,0 +1,1 @@
+uECC#b6fdeddc0bc9