ecc_dh.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* ec_dh.c - TinyCrypt implementation of EC-DH */
  2. /*
  3. * Copyright (c) 2014, Kenneth MacKay
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions are met:
  31. *
  32. * - Redistributions of source code must retain the above copyright notice,
  33. * this list of conditions and the following disclaimer.
  34. *
  35. * - Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in the
  37. * documentation and/or other materials provided with the distribution.
  38. *
  39. * - Neither the name of Intel Corporation nor the names of its contributors
  40. * may be used to endorse or promote products derived from this software
  41. * without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  44. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  47. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  48. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  49. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  50. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  51. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  52. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  53. * POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include <tinycrypt/constants.h>
  56. #include <tinycrypt/ecc.h>
  57. #include <tinycrypt/ecc_dh.h>
  58. #include <tinycrypt/utils.h>
  59. #include <string.h>
  60. #if default_RNG_defined
  61. static uECC_RNG_Function g_rng_function = &default_CSPRNG;
  62. #else
  63. static uECC_RNG_Function g_rng_function = 0;
  64. #endif
  65. int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
  66. unsigned int *d, uECC_Curve curve)
  67. {
  68. uECC_word_t _private[NUM_ECC_WORDS];
  69. uECC_word_t _public[NUM_ECC_WORDS * 2];
  70. /* This function is designed for test purposes-only (such as validating NIST
  71. * test vectors) as it uses a provided value for d instead of generating
  72. * it uniformly at random. */
  73. memcpy (_private, d, NUM_ECC_BYTES);
  74. /* Computing public-key from private: */
  75. if (EccPoint_compute_public_key(_public, _private, curve)) {
  76. /* Converting buffers to correct bit order: */
  77. uECC_vli_nativeToBytes(private_key,
  78. BITS_TO_BYTES(curve->num_n_bits),
  79. _private);
  80. uECC_vli_nativeToBytes(public_key,
  81. curve->num_bytes,
  82. _public);
  83. uECC_vli_nativeToBytes(public_key + curve->num_bytes,
  84. curve->num_bytes,
  85. _public + curve->num_words);
  86. /* erasing temporary buffer used to store secret: */
  87. _set_secure(_private, 0, NUM_ECC_BYTES);
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
  93. {
  94. uECC_word_t _random[NUM_ECC_WORDS * 2];
  95. uECC_word_t _private[NUM_ECC_WORDS];
  96. uECC_word_t _public[NUM_ECC_WORDS * 2];
  97. uECC_word_t tries;
  98. for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
  99. /* Generating _private uniformly at random: */
  100. uECC_RNG_Function rng_function = uECC_get_rng();
  101. if (!rng_function ||
  102. !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
  103. return 0;
  104. }
  105. /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
  106. uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
  107. /* Computing public-key from private: */
  108. if (EccPoint_compute_public_key(_public, _private, curve)) {
  109. /* Converting buffers to correct bit order: */
  110. uECC_vli_nativeToBytes(private_key,
  111. BITS_TO_BYTES(curve->num_n_bits),
  112. _private);
  113. uECC_vli_nativeToBytes(public_key,
  114. curve->num_bytes,
  115. _public);
  116. uECC_vli_nativeToBytes(public_key + curve->num_bytes,
  117. curve->num_bytes,
  118. _public + curve->num_words);
  119. /* erasing temporary buffer that stored secret: */
  120. _set_secure(_private, 0, NUM_ECC_BYTES);
  121. return 1;
  122. }
  123. }
  124. return 0;
  125. }
  126. int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
  127. uint8_t *secret, uECC_Curve curve)
  128. {
  129. uECC_word_t _public[NUM_ECC_WORDS * 2];
  130. uECC_word_t _private[NUM_ECC_WORDS];
  131. uECC_word_t tmp[NUM_ECC_WORDS];
  132. uECC_word_t *p2[2] = {_private, tmp};
  133. uECC_word_t *initial_Z = 0;
  134. uECC_word_t carry;
  135. wordcount_t num_words = curve->num_words;
  136. wordcount_t num_bytes = curve->num_bytes;
  137. int r;
  138. /* Converting buffers to correct bit order: */
  139. uECC_vli_bytesToNative(_private,
  140. private_key,
  141. BITS_TO_BYTES(curve->num_n_bits));
  142. uECC_vli_bytesToNative(_public,
  143. public_key,
  144. num_bytes);
  145. uECC_vli_bytesToNative(_public + num_words,
  146. public_key + num_bytes,
  147. num_bytes);
  148. /* Regularize the bitcount for the private key so that attackers cannot use a
  149. * side channel attack to learn the number of leading zeros. */
  150. carry = regularize_k(_private, _private, tmp, curve);
  151. /* If an RNG function was specified, try to get a random initial Z value to
  152. * improve protection against side-channel attacks. */
  153. if (g_rng_function) {
  154. if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
  155. r = 0;
  156. goto clear_and_out;
  157. }
  158. initial_Z = p2[carry];
  159. }
  160. EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
  161. curve);
  162. uECC_vli_nativeToBytes(secret, num_bytes, _public);
  163. r = !EccPoint_isZero(_public, curve);
  164. clear_and_out:
  165. /* erasing temporary buffer used to store secret: */
  166. _set_secure(p2, 0, sizeof(p2));
  167. _set_secure(tmp, 0, sizeof(tmp));
  168. _set_secure(_private, 0, sizeof(_private));
  169. return r;
  170. }