test_ecdsa.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  2. #include "uECC.h"
  3. #include <stdio.h>
  4. #include <string.h>
  5. #if LPC11XX
  6. #include "/Projects/lpc11xx/peripherals/uart.h"
  7. #include "/Projects/lpc11xx/peripherals/time.h"
  8. static uint64_t g_rand = 88172645463325252ull;
  9. int fake_rng(uint8_t *dest, unsigned size) {
  10. while (size) {
  11. g_rand ^= (g_rand << 13);
  12. g_rand ^= (g_rand >> 7);
  13. g_rand ^= (g_rand << 17);
  14. unsigned amount = (size > 8 ? 8 : size);
  15. memcpy(dest, &g_rand, amount);
  16. dest += amount;
  17. size -= amount;
  18. }
  19. return 1;
  20. }
  21. #endif
  22. int main() {
  23. #if LPC11XX
  24. uartInit(BAUD_115200);
  25. initTime();
  26. uECC_set_rng(&fake_rng);
  27. #endif
  28. uint8_t public[uECC_BYTES * 2];
  29. uint8_t private[uECC_BYTES];
  30. uint8_t hash[uECC_BYTES];
  31. uint8_t sig[uECC_BYTES * 2];
  32. int i;
  33. printf("Testing 256 signatures\n");
  34. for (i = 0; i < 256; ++i) {
  35. printf(".");
  36. #if !LPC11XX
  37. fflush(stdout);
  38. #endif
  39. if (!uECC_make_key(public, private)) {
  40. printf("uECC_make_key() failed\n");
  41. continue;
  42. }
  43. memcpy(hash, public, uECC_BYTES);
  44. if (!uECC_sign(private, hash, sig)) {
  45. printf("uECC_sign() failed\n");
  46. continue;
  47. }
  48. if (!uECC_verify(public, hash, sig)) {
  49. printf("uECC_verify() failed\n");
  50. }
  51. }
  52. printf("\n");
  53. return 0;
  54. }