ecc_dh.c 7.2 KB

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