|
| 1 | +/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */ |
| 2 | + |
| 3 | +#include "uECC.h" |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#ifndef uECC_TEST_NUMBER_OF_ITERATIONS |
| 9 | +#define uECC_TEST_NUMBER_OF_ITERATIONS 256 |
| 10 | +#endif |
| 11 | + |
| 12 | +#if LPC11XX |
| 13 | + |
| 14 | +#include "/Projects/lpc11xx/peripherals/uart.h" |
| 15 | +#include "/Projects/lpc11xx/peripherals/time.h" |
| 16 | + |
| 17 | +static uint64_t g_rand = 88172645463325252ull; |
| 18 | +int fake_rng(uint8_t *dest, unsigned size) { |
| 19 | + while(size) { |
| 20 | + g_rand ^= (g_rand << 13); |
| 21 | + g_rand ^= (g_rand >> 7); |
| 22 | + g_rand ^= (g_rand << 17); |
| 23 | + |
| 24 | + unsigned amount = (size > 8 ? 8 : size); |
| 25 | + memcpy(dest, &g_rand, amount); |
| 26 | + dest += amount; |
| 27 | + size -= amount; |
| 28 | + } |
| 29 | + return 1; |
| 30 | +} |
| 31 | + |
| 32 | +#endif |
| 33 | + |
| 34 | +void vli_print(char *str, uint8_t *vli, unsigned int size) { |
| 35 | + printf("%s ", str); |
| 36 | + while (size) { |
| 37 | + printf("%02X ", (unsigned)vli[size - 1]); |
| 38 | + --size; |
| 39 | + } |
| 40 | + printf("\n"); |
| 41 | +} |
| 42 | + |
| 43 | +int main() { |
| 44 | +#if LPC11XX |
| 45 | + uartInit(BAUD_115200); |
| 46 | + initTime(); |
| 47 | + |
| 48 | + uECC_set_rng(&fake_rng); |
| 49 | +#endif |
| 50 | + |
| 51 | + uint8_t public[uECC_BYTES * 2]; |
| 52 | + uint8_t private[uECC_BYTES]; |
| 53 | + uint8_t compressed_point[uECC_BYTES + 1]; |
| 54 | + uint8_t decompressed_point[uECC_BYTES * 2]; |
| 55 | + |
| 56 | + int i; |
| 57 | + printf("Testing compression and decompression of %d random EC points\n", uECC_TEST_NUMBER_OF_ITERATIONS); |
| 58 | + |
| 59 | + for (i = 0; i < uECC_TEST_NUMBER_OF_ITERATIONS; ++i) { |
| 60 | + printf("."); |
| 61 | + #if !LPC11XX |
| 62 | + fflush(stdout); |
| 63 | + #endif |
| 64 | + |
| 65 | + /* Generate arbitrary EC point (public) on Curve */ |
| 66 | + if (!uECC_make_key(public, private)) { |
| 67 | + printf("uECC_make_key() failed\n"); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + /* compress and decompress point */ |
| 72 | + uECC_compress(public, compressed_point); |
| 73 | + uECC_decompress(compressed_point, decompressed_point); |
| 74 | + |
| 75 | + if (memcmp(public, decompressed_point, 2 * uECC_BYTES) != 0) { |
| 76 | + printf("Original and decompressed points are not identical!\n"); |
| 77 | + vli_print("Original point = ", public, 2 * uECC_BYTES); |
| 78 | + vli_print("Compressed point = ", compressed_point, uECC_BYTES + 1); |
| 79 | + vli_print("Decompressed point = ", decompressed_point, 2 * uECC_BYTES); |
| 80 | + } |
| 81 | + } |
| 82 | + printf("\n"); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
0 commit comments