ecc_dh.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ecc_dh.h - TinyCrypt interface to EC-DH implementation */
  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. *
  9. * * Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions are met:
  32. *
  33. * - Redistributions of source code must retain the above copyright notice,
  34. * this list of conditions and the following disclaimer.
  35. *
  36. * - Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. *
  40. * - Neither the name of Intel Corporation nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  48. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. * POSSIBILITY OF SUCH DAMAGE.
  55. */
  56. /**
  57. * @file
  58. * @brief -- Interface to EC-DH implementation.
  59. *
  60. * Overview: This software is an implementation of EC-DH. This implementation
  61. * uses curve NIST p-256.
  62. *
  63. * Security: The curve NIST p-256 provides approximately 128 bits of security.
  64. */
  65. #ifndef __TC_ECC_DH_H__
  66. #define __TC_ECC_DH_H__
  67. #include <tinycrypt/ecc.h>
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. /**
  72. * @brief Create a public/private key pair.
  73. * @return returns TC_CRYPTO_SUCCESS (1) if the key pair was generated successfully
  74. * returns TC_CRYPTO_FAIL (0) if error while generating key pair
  75. *
  76. * @param p_public_key OUT -- Will be filled in with the public key. Must be at
  77. * least 2 * the curve size (in bytes) long. For curve secp256r1, p_public_key
  78. * must be 64 bytes long.
  79. * @param p_private_key OUT -- Will be filled in with the private key. Must be as
  80. * long as the curve order (for secp256r1, p_private_key must be 32 bytes long).
  81. *
  82. * @note side-channel countermeasure: algorithm strengthened against timing
  83. * attack.
  84. * @warning A cryptographically-secure PRNG function must be set (using
  85. * uECC_set_rng()) before calling uECC_make_key().
  86. */
  87. int uECC_make_key(uint8_t *p_public_key, uint8_t *p_private_key, uECC_Curve curve);
  88. #ifdef ENABLE_TESTS
  89. /**
  90. * @brief Create a public/private key pair given a specific d.
  91. *
  92. * @note THIS FUNCTION SHOULD BE CALLED ONLY FOR TEST PURPOSES. Refer to
  93. * uECC_make_key() function for real applications.
  94. */
  95. int uECC_make_key_with_d(uint8_t *p_public_key, uint8_t *p_private_key,
  96. unsigned int *d, uECC_Curve curve);
  97. #endif
  98. /**
  99. * @brief Compute a shared secret given your secret key and someone else's
  100. * public key.
  101. * @return returns TC_CRYPTO_SUCCESS (1) if the shared secret was computed successfully
  102. * returns TC_CRYPTO_FAIL (0) otherwise
  103. *
  104. * @param p_secret OUT -- Will be filled in with the shared secret value. Must be
  105. * the same size as the curve size (for curve secp256r1, secret must be 32 bytes
  106. * long.
  107. * @param p_public_key IN -- The public key of the remote party.
  108. * @param p_private_key IN -- Your private key.
  109. *
  110. * @warning It is recommended to use the output of uECC_shared_secret() as the
  111. * input of a recommended Key Derivation Function (see NIST SP 800-108) in
  112. * order to produce a cryptographically secure symmetric key.
  113. */
  114. int uECC_shared_secret(const uint8_t *p_public_key, const uint8_t *p_private_key,
  115. uint8_t *p_secret, uECC_Curve curve);
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* __TC_ECC_DH_H__ */