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.
main.cpp@0:f83fc7ecf97b, 2017-09-07 (annotated)
- Committer:
- allankliu
- Date:
- Thu Sep 07 12:10:39 2017 +0000
- Revision:
- 0:f83fc7ecf97b
- Child:
- 2:a2a77f01dd26
Init version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
allankliu | 0:f83fc7ecf97b | 1 | /* |
allankliu | 0:f83fc7ecf97b | 2 | * Micro-ECC ported to mbed platform |
allankliu | 0:f83fc7ecf97b | 3 | * Original Author: Ken MacKay |
allankliu | 0:f83fc7ecf97b | 4 | * Original Project: https://github.com/kmackay/micro-ecc |
allankliu | 0:f83fc7ecf97b | 5 | * Ported by: Allan K Liu |
allankliu | 0:f83fc7ecf97b | 6 | * |
allankliu | 0:f83fc7ecf97b | 7 | * Micro-ECC is ported to mbed to evalute its performance |
allankliu | 0:f83fc7ecf97b | 8 | * Micro-ECC is optimized for ARM/ARM-thumb/ARM-thumb2/AVR platform |
allankliu | 0:f83fc7ecf97b | 9 | * Micro-ECC mbed version disabled thumb/thumb2 optimization because of its GCC syntax. |
allankliu | 0:f83fc7ecf97b | 10 | * PS: I am not good at assembly for those projects. |
allankliu | 0:f83fc7ecf97b | 11 | */ |
allankliu | 0:f83fc7ecf97b | 12 | |
allankliu | 0:f83fc7ecf97b | 13 | #include "mbed.h" |
allankliu | 0:f83fc7ecf97b | 14 | #include "uECC.h" |
allankliu | 0:f83fc7ecf97b | 15 | |
allankliu | 0:f83fc7ecf97b | 16 | Serial pc(USBTX, USBRX); |
allankliu | 0:f83fc7ecf97b | 17 | AnalogIn rnd(A1); |
allankliu | 0:f83fc7ecf97b | 18 | Timer t; |
allankliu | 0:f83fc7ecf97b | 19 | |
allankliu | 0:f83fc7ecf97b | 20 | void dumprand() |
allankliu | 0:f83fc7ecf97b | 21 | { |
allankliu | 0:f83fc7ecf97b | 22 | uint8_t buf[16]; |
allankliu | 0:f83fc7ecf97b | 23 | |
allankliu | 0:f83fc7ecf97b | 24 | pc.printf("plain_random:"); |
allankliu | 0:f83fc7ecf97b | 25 | for(int i=0; i<16; i++){ |
allankliu | 0:f83fc7ecf97b | 26 | buf[i] = rand(); |
allankliu | 0:f83fc7ecf97b | 27 | pc.printf("%02X",buf[i]); |
allankliu | 0:f83fc7ecf97b | 28 | } |
allankliu | 0:f83fc7ecf97b | 29 | pc.printf("\r\n"); |
allankliu | 0:f83fc7ecf97b | 30 | |
allankliu | 0:f83fc7ecf97b | 31 | } |
allankliu | 0:f83fc7ecf97b | 32 | |
allankliu | 0:f83fc7ecf97b | 33 | static int RNG(uint8_t *dest, unsigned size) { |
allankliu | 0:f83fc7ecf97b | 34 | // Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of |
allankliu | 0:f83fc7ecf97b | 35 | // random noise). This can take a long time to generate random data if the result of analogRead(0) |
allankliu | 0:f83fc7ecf97b | 36 | // doesn't change very frequently. |
allankliu | 0:f83fc7ecf97b | 37 | pc.printf("Random:\r\n"); |
allankliu | 0:f83fc7ecf97b | 38 | while (size) { |
allankliu | 0:f83fc7ecf97b | 39 | uint8_t val = 0; |
allankliu | 0:f83fc7ecf97b | 40 | for (unsigned i = 0; i < 8; ++i) { |
allankliu | 0:f83fc7ecf97b | 41 | //int init = rnd.read(); |
allankliu | 0:f83fc7ecf97b | 42 | int init = rand(); |
allankliu | 0:f83fc7ecf97b | 43 | pc.printf("%04X",init); |
allankliu | 0:f83fc7ecf97b | 44 | int count = 0; |
allankliu | 0:f83fc7ecf97b | 45 | //while (rnd.read() == init) { |
allankliu | 0:f83fc7ecf97b | 46 | while (rand() == init) { |
allankliu | 0:f83fc7ecf97b | 47 | ++count; |
allankliu | 0:f83fc7ecf97b | 48 | } |
allankliu | 0:f83fc7ecf97b | 49 | |
allankliu | 0:f83fc7ecf97b | 50 | if (count == 0) { |
allankliu | 0:f83fc7ecf97b | 51 | val = (val << 1) | (init & 0x01); |
allankliu | 0:f83fc7ecf97b | 52 | } else { |
allankliu | 0:f83fc7ecf97b | 53 | val = (val << 1) | (count & 0x01); |
allankliu | 0:f83fc7ecf97b | 54 | } |
allankliu | 0:f83fc7ecf97b | 55 | } |
allankliu | 0:f83fc7ecf97b | 56 | *dest = val; |
allankliu | 0:f83fc7ecf97b | 57 | ++dest; |
allankliu | 0:f83fc7ecf97b | 58 | --size; |
allankliu | 0:f83fc7ecf97b | 59 | pc.printf("\r\n"); |
allankliu | 0:f83fc7ecf97b | 60 | } |
allankliu | 0:f83fc7ecf97b | 61 | |
allankliu | 0:f83fc7ecf97b | 62 | // NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar. |
allankliu | 0:f83fc7ecf97b | 63 | return 1; |
allankliu | 0:f83fc7ecf97b | 64 | } |
allankliu | 0:f83fc7ecf97b | 65 | |
allankliu | 0:f83fc7ecf97b | 66 | void dumphex(const char* name, uint8_t* buf, uint8_t size){ |
allankliu | 0:f83fc7ecf97b | 67 | pc.printf(name); |
allankliu | 0:f83fc7ecf97b | 68 | for(int i=0; i<size; i++){ |
allankliu | 0:f83fc7ecf97b | 69 | pc.printf("%02X",buf[i]); |
allankliu | 0:f83fc7ecf97b | 70 | } |
allankliu | 0:f83fc7ecf97b | 71 | pc.printf("\r\n"); |
allankliu | 0:f83fc7ecf97b | 72 | } |
allankliu | 0:f83fc7ecf97b | 73 | |
allankliu | 0:f83fc7ecf97b | 74 | void loop(){ |
allankliu | 0:f83fc7ecf97b | 75 | const struct uECC_Curve_t * curve = uECC_secp160r1(); |
allankliu | 0:f83fc7ecf97b | 76 | int r; |
allankliu | 0:f83fc7ecf97b | 77 | long d; |
allankliu | 0:f83fc7ecf97b | 78 | |
allankliu | 0:f83fc7ecf97b | 79 | uint8_t private1[21]; |
allankliu | 0:f83fc7ecf97b | 80 | uint8_t private2[21]; |
allankliu | 0:f83fc7ecf97b | 81 | |
allankliu | 0:f83fc7ecf97b | 82 | uint8_t public1[40]; |
allankliu | 0:f83fc7ecf97b | 83 | uint8_t public2[40]; |
allankliu | 0:f83fc7ecf97b | 84 | |
allankliu | 0:f83fc7ecf97b | 85 | uint8_t secret1[20]; |
allankliu | 0:f83fc7ecf97b | 86 | uint8_t secret2[20]; |
allankliu | 0:f83fc7ecf97b | 87 | |
allankliu | 0:f83fc7ecf97b | 88 | pc.printf("Start ECC computation\r\n"); |
allankliu | 0:f83fc7ecf97b | 89 | pc.printf("make key 1\r\n"); |
allankliu | 0:f83fc7ecf97b | 90 | t.start(); |
allankliu | 0:f83fc7ecf97b | 91 | uECC_make_key(public1, private1, curve); |
allankliu | 0:f83fc7ecf97b | 92 | dumphex("public1: ", public1, sizeof(public1)); |
allankliu | 0:f83fc7ecf97b | 93 | dumphex("private1: ", private1, sizeof(private1)); |
allankliu | 0:f83fc7ecf97b | 94 | t.stop(); d = t.read_ms(); |
allankliu | 0:f83fc7ecf97b | 95 | t.reset(); t.start(); |
allankliu | 0:f83fc7ecf97b | 96 | pc.printf("time: %dms\r\n",d); |
allankliu | 0:f83fc7ecf97b | 97 | |
allankliu | 0:f83fc7ecf97b | 98 | pc.printf("make key 2\r\n"); |
allankliu | 0:f83fc7ecf97b | 99 | t.start(); |
allankliu | 0:f83fc7ecf97b | 100 | uECC_make_key(public2, private2, curve); |
allankliu | 0:f83fc7ecf97b | 101 | dumphex("public2: ", public2, sizeof(public2)); |
allankliu | 0:f83fc7ecf97b | 102 | dumphex("private2: ", private2, sizeof(private2)); |
allankliu | 0:f83fc7ecf97b | 103 | t.stop(); d = t.read_ms(); |
allankliu | 0:f83fc7ecf97b | 104 | t.reset(); t.start(); |
allankliu | 0:f83fc7ecf97b | 105 | pc.printf("time: %dms\r\n",d); |
allankliu | 0:f83fc7ecf97b | 106 | |
allankliu | 0:f83fc7ecf97b | 107 | pc.printf("make share secret 1\r\n"); |
allankliu | 0:f83fc7ecf97b | 108 | t.start(); |
allankliu | 0:f83fc7ecf97b | 109 | r = uECC_shared_secret(public2, private1, secret1, curve); |
allankliu | 0:f83fc7ecf97b | 110 | pc.printf("r: %04X\r\n",r); |
allankliu | 0:f83fc7ecf97b | 111 | t.stop(); d = t.read_ms(); |
allankliu | 0:f83fc7ecf97b | 112 | t.reset(); t.start(); |
allankliu | 0:f83fc7ecf97b | 113 | pc.printf("time: %dms\r\n",d); |
allankliu | 0:f83fc7ecf97b | 114 | |
allankliu | 0:f83fc7ecf97b | 115 | pc.printf("make share secret 2\r\n"); |
allankliu | 0:f83fc7ecf97b | 116 | t.start(); |
allankliu | 0:f83fc7ecf97b | 117 | r = uECC_shared_secret(public1, private2, secret2, curve); |
allankliu | 0:f83fc7ecf97b | 118 | pc.printf("r: %04X\r\n",r); |
allankliu | 0:f83fc7ecf97b | 119 | t.stop(); d = t.read_ms(); |
allankliu | 0:f83fc7ecf97b | 120 | t.reset(); t.start(); |
allankliu | 0:f83fc7ecf97b | 121 | pc.printf("time: %dms\r\n",d); |
allankliu | 0:f83fc7ecf97b | 122 | |
allankliu | 0:f83fc7ecf97b | 123 | pc.printf("\r\n\r\n"); |
allankliu | 0:f83fc7ecf97b | 124 | wait(1); |
allankliu | 0:f83fc7ecf97b | 125 | } |
allankliu | 0:f83fc7ecf97b | 126 | |
allankliu | 0:f83fc7ecf97b | 127 | int main() { |
allankliu | 0:f83fc7ecf97b | 128 | pc.baud(115200); |
allankliu | 0:f83fc7ecf97b | 129 | dumprand(); |
allankliu | 0:f83fc7ecf97b | 130 | wait(1); |
allankliu | 0:f83fc7ecf97b | 131 | pc.printf("\r\n\r\nmicroECC test\r\n"); |
allankliu | 0:f83fc7ecf97b | 132 | uECC_set_rng(&RNG); |
allankliu | 0:f83fc7ecf97b | 133 | pc.printf("\r\n"); |
allankliu | 0:f83fc7ecf97b | 134 | |
allankliu | 0:f83fc7ecf97b | 135 | while(1) { |
allankliu | 0:f83fc7ecf97b | 136 | loop(); |
allankliu | 0:f83fc7ecf97b | 137 | } |
allankliu | 0:f83fc7ecf97b | 138 | } |