uECC_verify_antifault.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license.
  2. Modifications Copyright 2020, Espressif Systems (Shanghai) PTE LTD. Licensed under the BSD
  3. 2-clause license.
  4. */
  5. /* uECC_verify() calls a number of static functions form here and
  6. uses other definitions, so we just build that whole source file here and then append
  7. our modified version uECC_verify_antifault(). */
  8. #include "micro-ecc/uECC.c"
  9. /* Version of uECC_verify() which also copies message_hash into verified_hash,
  10. but only if the signature is valid. Does this in an FI resistant way.
  11. */
  12. int uECC_verify_antifault(const uint8_t *public_key,
  13. const uint8_t *message_hash,
  14. unsigned hash_size,
  15. const uint8_t *signature,
  16. uECC_Curve curve,
  17. uint8_t *verified_hash) {
  18. uECC_word_t u1[uECC_MAX_WORDS], u2[uECC_MAX_WORDS];
  19. uECC_word_t z[uECC_MAX_WORDS];
  20. uECC_word_t sum[uECC_MAX_WORDS * 2];
  21. uECC_word_t rx[uECC_MAX_WORDS];
  22. uECC_word_t ry[uECC_MAX_WORDS];
  23. uECC_word_t tx[uECC_MAX_WORDS];
  24. uECC_word_t ty[uECC_MAX_WORDS];
  25. uECC_word_t tz[uECC_MAX_WORDS];
  26. const uECC_word_t *points[4];
  27. const uECC_word_t *point;
  28. bitcount_t num_bits;
  29. bitcount_t i;
  30. #if uECC_VLI_NATIVE_LITTLE_ENDIAN
  31. uECC_word_t *_public = (uECC_word_t *)public_key;
  32. #else
  33. uECC_word_t _public[uECC_MAX_WORDS * 2];
  34. #endif
  35. uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS];
  36. wordcount_t num_words = curve->num_words;
  37. wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
  38. rx[num_n_words - 1] = 0;
  39. r[num_n_words - 1] = 0;
  40. s[num_n_words - 1] = 0;
  41. #if uECC_VLI_NATIVE_LITTLE_ENDIAN
  42. bcopy((uint8_t *) r, signature, curve->num_bytes);
  43. bcopy((uint8_t *) s, signature + curve->num_bytes, curve->num_bytes);
  44. #else
  45. uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
  46. uECC_vli_bytesToNative(
  47. _public + num_words, public_key + curve->num_bytes, curve->num_bytes);
  48. uECC_vli_bytesToNative(r, signature, curve->num_bytes);
  49. uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
  50. #endif
  51. /* r, s must not be 0. */
  52. if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
  53. return 0;
  54. }
  55. /* r, s must be < n. */
  56. if (uECC_vli_cmp(curve->n, r, num_n_words) != 1 ||
  57. uECC_vli_cmp(curve->n, s, num_n_words) != 1) {
  58. return 0;
  59. }
  60. /* Calculate u1 and u2. */
  61. uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
  62. u1[num_n_words - 1] = 0;
  63. bits2int(u1, message_hash, hash_size, curve);
  64. uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
  65. uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
  66. /* Calculate sum = G + Q. */
  67. uECC_vli_set(sum, _public, num_words);
  68. uECC_vli_set(sum + num_words, _public + num_words, num_words);
  69. uECC_vli_set(tx, curve->G, num_words);
  70. uECC_vli_set(ty, curve->G + num_words, num_words);
  71. uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
  72. XYcZ_add(tx, ty, sum, sum + num_words, curve);
  73. uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
  74. apply_z(sum, sum + num_words, z, curve);
  75. /* Use Shamir's trick to calculate u1*G + u2*Q */
  76. points[0] = 0;
  77. points[1] = curve->G;
  78. points[2] = _public;
  79. points[3] = sum;
  80. num_bits = smax(uECC_vli_numBits(u1, num_n_words),
  81. uECC_vli_numBits(u2, num_n_words));
  82. point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
  83. ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
  84. uECC_vli_set(rx, point, num_words);
  85. uECC_vli_set(ry, point + num_words, num_words);
  86. uECC_vli_clear(z, num_words);
  87. z[0] = 1;
  88. for (i = num_bits - 2; i >= 0; --i) {
  89. uECC_word_t index;
  90. curve->double_jacobian(rx, ry, z, curve);
  91. index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
  92. point = points[index];
  93. if (point) {
  94. uECC_vli_set(tx, point, num_words);
  95. uECC_vli_set(ty, point + num_words, num_words);
  96. apply_z(tx, ty, z, curve);
  97. uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
  98. XYcZ_add(tx, ty, rx, ry, curve);
  99. uECC_vli_modMult_fast(z, z, tz, curve);
  100. }
  101. }
  102. uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
  103. apply_z(rx, ry, z, curve);
  104. /* v = x1 (mod n) */
  105. if (uECC_vli_cmp(curve->n, rx, num_n_words) != 1) {
  106. uECC_vli_sub(rx, rx, curve->n, num_n_words);
  107. }
  108. /* Anti-FI addition. Copy message_hash into verified_hash, but do it in a
  109. way that it will only happen if v == r (ie, rx == r)
  110. */
  111. const uECC_word_t *mhash_words = (const uECC_word_t *)message_hash;
  112. uECC_word_t *vhash_words = (uECC_word_t *)verified_hash;
  113. unsigned hash_words = hash_size / sizeof(uECC_word_t);
  114. for (unsigned int w = 0; w < hash_words; w++) {
  115. /* note: using curve->num_words here to encourage compiler to re-read this variable */
  116. vhash_words[w] = mhash_words[w] ^ rx[w % curve->num_words] ^ r[w % curve->num_words];
  117. }
  118. /* Curve may be longer than hash, in which case keep reading the rest of the bytes */
  119. for (int w = hash_words; w < curve->num_words; w++) {
  120. vhash_words[w % hash_words] |= rx[w] ^ r[w];
  121. }
  122. /* Accept only if v == r. */
  123. return (int)(uECC_vli_equal(rx, r, num_words));
  124. }