test_compute.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. void vli_print(uint8_t *vli, unsigned int size) {
  6. while (size) {
  7. printf("%02X ", (unsigned)vli[size - 1]);
  8. --size;
  9. }
  10. }
  11. int main() {
  12. int i;
  13. int success;
  14. uint8_t private[uECC_BYTES];
  15. uint8_t public[uECC_BYTES * 2];
  16. uint8_t public_computed[uECC_BYTES * 2];
  17. printf("Testing 256 random private key pairs\n");
  18. for (i = 0; i < 256; ++i) {
  19. printf(".");
  20. #if !LPC11XX
  21. fflush(stdout);
  22. #endif
  23. success = uECC_make_key(public, private);
  24. if (!success) {
  25. printf("uECC_make_key() failed\n");
  26. return 1;
  27. }
  28. success = uECC_compute_public_key(private, public_computed);
  29. if (!success) {
  30. printf("uECC_compute_public_key() failed\n");
  31. }
  32. if (memcmp(public, public_computed, sizeof(public)) != 0) {
  33. printf("Computed and provided public keys are not identical!\n");
  34. printf("Computed public key = ");
  35. vli_print(public_computed, uECC_BYTES);
  36. printf("\n");
  37. printf("Provided public key = ");
  38. vli_print(public, uECC_BYTES);
  39. printf("\n");
  40. printf("Private key = ");
  41. vli_print(private, uECC_BYTES);
  42. printf("\n");
  43. }
  44. }
  45. printf("\n");
  46. printf("Testing private key = 0\n");
  47. memset(private, 0, uECC_BYTES);
  48. success = uECC_compute_public_key(private, public_computed);
  49. if (success) {
  50. printf("uECC_compute_public_key() should have failed\n");
  51. }
  52. return 0;
  53. }