test_ecdsa_deterministic.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #define SHA256_BLOCK_LENGTH 64
  23. #define SHA256_DIGEST_LENGTH 32
  24. typedef struct SHA256_CTX {
  25. uint32_t state[8];
  26. uint64_t bitcount;
  27. uint8_t buffer[SHA256_BLOCK_LENGTH];
  28. } SHA256_CTX;
  29. extern void SHA256_Init(SHA256_CTX *ctx);
  30. extern void SHA256_Update(SHA256_CTX *ctx, const uint8_t *message, size_t message_size);
  31. extern void SHA256_Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA256_CTX *ctx);
  32. typedef struct SHA256_HashContext {
  33. uECC_HashContext uECC;
  34. SHA256_CTX ctx;
  35. } SHA256_HashContext;
  36. static void init_SHA256(uECC_HashContext *base) {
  37. SHA256_HashContext *context = (SHA256_HashContext *)base;
  38. SHA256_Init(&context->ctx);
  39. }
  40. static void update_SHA256(uECC_HashContext *base,
  41. const uint8_t *message,
  42. unsigned message_size) {
  43. SHA256_HashContext *context = (SHA256_HashContext *)base;
  44. SHA256_Update(&context->ctx, message, message_size);
  45. }
  46. static void finish_SHA256(uECC_HashContext *base, uint8_t *hash_result) {
  47. SHA256_HashContext *context = (SHA256_HashContext *)base;
  48. SHA256_Final(hash_result, &context->ctx);
  49. }
  50. int main() {
  51. #if LPC11XX
  52. uartInit(BAUD_115200);
  53. initTime();
  54. uECC_set_rng(&fake_rng);
  55. #endif
  56. uint8_t public[uECC_BYTES * 2];
  57. uint8_t private[uECC_BYTES];
  58. uint8_t hash[uECC_BYTES];
  59. uint8_t sig[uECC_BYTES * 2];
  60. uint8_t tmp[2 * SHA256_DIGEST_LENGTH + SHA256_BLOCK_LENGTH];
  61. SHA256_HashContext ctx = {{
  62. &init_SHA256,
  63. &update_SHA256,
  64. &finish_SHA256,
  65. SHA256_BLOCK_LENGTH,
  66. SHA256_DIGEST_LENGTH,
  67. tmp
  68. }};
  69. int i;
  70. printf("Testing 256 signatures\n");
  71. for (i = 0; i < 256; ++i) {
  72. printf(".");
  73. #if !LPC11XX
  74. fflush(stdout);
  75. #endif
  76. if (!uECC_make_key(public, private)) {
  77. printf("uECC_make_key() failed\n");
  78. continue;
  79. }
  80. memcpy(hash, public, uECC_BYTES);
  81. if (!uECC_sign_deterministic(private, hash, &ctx.uECC, sig)) {
  82. printf("uECC_sign() failed\n");
  83. continue;
  84. }
  85. if (!uECC_verify(public, hash, sig)) {
  86. printf("uECC_verify() failed\n");
  87. }
  88. }
  89. printf("\n");
  90. return 0;
  91. }