xref: /btstack/3rd-party/micro-ecc/test/ecc_test/ecc_test.ino (revision af03003c8ac55cf0eea9563b597879b24aee256f)
1*af03003cSMatthias Ringwald#include <uECC.h>
2*af03003cSMatthias Ringwald
3*af03003cSMatthias Ringwaldextern "C" {
4*af03003cSMatthias Ringwald
5*af03003cSMatthias Ringwaldstatic int RNG(uint8_t *dest, unsigned size) {
6*af03003cSMatthias Ringwald  // Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of
7*af03003cSMatthias Ringwald  // random noise). This can take a long time to generate random data if the result of analogRead(0)
8*af03003cSMatthias Ringwald  // doesn't change very frequently.
9*af03003cSMatthias Ringwald  while (size) {
10*af03003cSMatthias Ringwald    uint8_t val = 0;
11*af03003cSMatthias Ringwald    for (unsigned i = 0; i < 8; ++i) {
12*af03003cSMatthias Ringwald      int init = analogRead(0);
13*af03003cSMatthias Ringwald      int count = 0;
14*af03003cSMatthias Ringwald      while (analogRead(0) == init) {
15*af03003cSMatthias Ringwald        ++count;
16*af03003cSMatthias Ringwald      }
17*af03003cSMatthias Ringwald
18*af03003cSMatthias Ringwald      if (count == 0) {
19*af03003cSMatthias Ringwald         val = (val << 1) | (init & 0x01);
20*af03003cSMatthias Ringwald      } else {
21*af03003cSMatthias Ringwald         val = (val << 1) | (count & 0x01);
22*af03003cSMatthias Ringwald      }
23*af03003cSMatthias Ringwald    }
24*af03003cSMatthias Ringwald    *dest = val;
25*af03003cSMatthias Ringwald    ++dest;
26*af03003cSMatthias Ringwald    --size;
27*af03003cSMatthias Ringwald  }
28*af03003cSMatthias Ringwald  // NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar.
29*af03003cSMatthias Ringwald  return 1;
30*af03003cSMatthias Ringwald}
31*af03003cSMatthias Ringwald
32*af03003cSMatthias Ringwald}  // extern "C"
33*af03003cSMatthias Ringwald
34*af03003cSMatthias Ringwaldvoid setup() {
35*af03003cSMatthias Ringwald  Serial.begin(115200);
36*af03003cSMatthias Ringwald  Serial.print("Testing ecc\n");
37*af03003cSMatthias Ringwald  uECC_set_rng(&RNG);
38*af03003cSMatthias Ringwald}
39*af03003cSMatthias Ringwald
40*af03003cSMatthias Ringwaldvoid loop() {
41*af03003cSMatthias Ringwald  uint8_t private1[uECC_BYTES];
42*af03003cSMatthias Ringwald  uint8_t private2[uECC_BYTES];
43*af03003cSMatthias Ringwald
44*af03003cSMatthias Ringwald  uint8_t public1[uECC_BYTES * 2];
45*af03003cSMatthias Ringwald  uint8_t public2[uECC_BYTES * 2];
46*af03003cSMatthias Ringwald
47*af03003cSMatthias Ringwald  uint8_t secret1[uECC_BYTES];
48*af03003cSMatthias Ringwald  uint8_t secret2[uECC_BYTES];
49*af03003cSMatthias Ringwald
50*af03003cSMatthias Ringwald  unsigned long a = millis();
51*af03003cSMatthias Ringwald  uECC_make_key(public1, private1);
52*af03003cSMatthias Ringwald  unsigned long b = millis();
53*af03003cSMatthias Ringwald
54*af03003cSMatthias Ringwald  Serial.print("Made key 1 in "); Serial.println(b-a);
55*af03003cSMatthias Ringwald  a = millis();
56*af03003cSMatthias Ringwald  uECC_make_key(public2, private2);
57*af03003cSMatthias Ringwald  b = millis();
58*af03003cSMatthias Ringwald  Serial.print("Made key 2 in "); Serial.println(b-a);
59*af03003cSMatthias Ringwald
60*af03003cSMatthias Ringwald  a = millis();
61*af03003cSMatthias Ringwald  int r = uECC_shared_secret(public2, private1, secret1);
62*af03003cSMatthias Ringwald  b = millis();
63*af03003cSMatthias Ringwald  Serial.print("Shared secret 1 in "); Serial.println(b-a);
64*af03003cSMatthias Ringwald  if (!r) {
65*af03003cSMatthias Ringwald    Serial.print("shared_secret() failed (1)\n");
66*af03003cSMatthias Ringwald    return;
67*af03003cSMatthias Ringwald  }
68*af03003cSMatthias Ringwald
69*af03003cSMatthias Ringwald  a = millis();
70*af03003cSMatthias Ringwald  r = uECC_shared_secret(public1, private2, secret2);
71*af03003cSMatthias Ringwald  b = millis();
72*af03003cSMatthias Ringwald  Serial.print("Shared secret 2 in "); Serial.println(b-a);
73*af03003cSMatthias Ringwald  if (!r) {
74*af03003cSMatthias Ringwald    Serial.print("shared_secret() failed (2)\n");
75*af03003cSMatthias Ringwald    return;
76*af03003cSMatthias Ringwald  }
77*af03003cSMatthias Ringwald
78*af03003cSMatthias Ringwald  if (memcmp(secret1, secret2, sizeof(secret1)) != 0) {
79*af03003cSMatthias Ringwald    Serial.print("Shared secrets are not identical!\n");
80*af03003cSMatthias Ringwald  } else {
81*af03003cSMatthias Ringwald    Serial.print("Shared secrets are identical\n");
82*af03003cSMatthias Ringwald  }
83*af03003cSMatthias Ringwald}
84*af03003cSMatthias Ringwald
85