ecc_dh.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
  61. unsigned int *d, uECC_Curve curve)
  62. {
  63. uECC_word_t _private[NUM_ECC_WORDS];
  64. uECC_word_t _public[NUM_ECC_WORDS * 2];
  65. /* This function is designed for test purposes-only (such as validating NIST
  66. * test vectors) as it uses a provided value for d instead of generating
  67. * it uniformly at random. */
  68. memcpy (_private, d, NUM_ECC_BYTES);
  69. /* Computing public-key from private: */
  70. if (EccPoint_compute_public_key(_public, _private, curve)) {
  71. /* Converting buffers to correct bit order: */
  72. uECC_vli_nativeToBytes(private_key,
  73. BITS_TO_BYTES(curve->num_n_bits),
  74. _private);
  75. uECC_vli_nativeToBytes(public_key,
  76. curve->num_bytes,
  77. _public);
  78. uECC_vli_nativeToBytes(public_key + curve->num_bytes,
  79. curve->num_bytes,
  80. _public + curve->num_words);
  81. /* erasing temporary buffer used to store secret: */
  82. _set_secure(_private, 0, NUM_ECC_BYTES);
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
  88. {
  89. uECC_word_t _random[NUM_ECC_WORDS * 2];
  90. uECC_word_t _private[NUM_ECC_WORDS];
  91. uECC_word_t _public[NUM_ECC_WORDS * 2];
  92. uECC_word_t tries;
  93. for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
  94. /* Generating _private uniformly at random: */
  95. uECC_RNG_Function rng_function = uECC_get_rng();
  96. if (!rng_function ||
  97. !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
  98. return 0;
  99. }
  100. /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
  101. uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
  102. /* Computing public-key from private: */
  103. if (EccPoint_compute_public_key(_public, _private, curve)) {
  104. /* Converting buffers to correct bit order: */
  105. uECC_vli_nativeToBytes(private_key,
  106. BITS_TO_BYTES(curve->num_n_bits),
  107. _private);
  108. uECC_vli_nativeToBytes(public_key,
  109. curve->num_bytes,
  110. _public);
  111. uECC_vli_nativeToBytes(public_key + curve->num_bytes,
  112. curve->num_bytes,
  113. _public + curve->num_words);
  114. /* erasing temporary buffer that stored secret: */
  115. _set_secure(_private, 0, NUM_ECC_BYTES);
  116. return 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
  122. uint8_t *secret, uECC_Curve curve)
  123. {
  124. uECC_word_t _public[NUM_ECC_WORDS * 2];
  125. uECC_word_t _private[NUM_ECC_WORDS];
  126. uECC_word_t tmp[NUM_ECC_WORDS];
  127. uECC_word_t *p2[2] = {_private, tmp};
  128. uECC_word_t *initial_Z = 0;
  129. uECC_word_t carry;
  130. wordcount_t num_words = curve->num_words;
  131. wordcount_t num_bytes = curve->num_bytes;
  132. int r;
  133. /* Converting buffers to correct bit order: */
  134. uECC_vli_bytesToNative(_private,
  135. private_key,
  136. BITS_TO_BYTES(curve->num_n_bits));
  137. uECC_vli_bytesToNative(_public,
  138. public_key,
  139. num_bytes);
  140. uECC_vli_bytesToNative(_public + num_words,
  141. public_key + num_bytes,
  142. num_bytes);
  143. /* Regularize the bitcount for the private key so that attackers cannot use a
  144. * side channel attack to learn the number of leading zeros. */
  145. carry = regularize_k(_private, _private, tmp, curve);
  146. /* If an RNG function was specified, try to get a random initial Z value to
  147. * improve protection against side-channel attacks. */
  148. if (uECC_get_rng()) {
  149. if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
  150. r = 0;
  151. goto clear_and_out;
  152. }
  153. initial_Z = p2[carry];
  154. }
  155. EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
  156. curve);
  157. uECC_vli_nativeToBytes(secret, num_bytes, _public);
  158. r = !EccPoint_isZero(_public, curve);
  159. clear_and_out:
  160. /* erasing temporary buffer used to store secret: */
  161. _set_secure(p2, 0, sizeof(p2));
  162. _set_secure(tmp, 0, sizeof(tmp));
  163. _set_secure(_private, 0, sizeof(_private));
  164. return r;
  165. }