ecc_dsa.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* ecc_dh.h - TinyCrypt interface to EC-DSA 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. /*
  29. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * - Redistributions of source code must retain the above copyright notice,
  35. * this list of conditions and the following disclaimer.
  36. *
  37. * - Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in the
  39. * documentation and/or other materials provided with the distribution.
  40. *
  41. * - Neither the name of Intel Corporation nor the names of its contributors
  42. * may be used to endorse or promote products derived from this software
  43. * without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  46. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  49. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  50. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  51. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  52. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  53. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  54. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  55. * POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. /**
  58. * @file
  59. * @brief -- Interface to EC-DSA implementation.
  60. *
  61. * Overview: This software is an implementation of EC-DSA. This implementation
  62. * uses curve NIST p-256.
  63. *
  64. * Security: The curve NIST p-256 provides approximately 128 bits of security.
  65. *
  66. * Usage: - To sign: Compute a hash of the data you wish to sign (SHA-2 is
  67. * recommended) and pass it in to ecdsa_sign function along with your
  68. * private key and a random number. You must use a new non-predictable
  69. * random number to generate each new signature.
  70. * - To verify a signature: Compute the hash of the signed data using
  71. * the same hash as the signer and pass it to this function along with
  72. * the signer's public key and the signature values (r and s).
  73. */
  74. #ifndef __TC_ECC_DSA_H__
  75. #define __TC_ECC_DSA_H__
  76. #include <tinycrypt/ecc.h>
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /**
  81. * @brief Generate an ECDSA signature for a given hash value.
  82. * @return returns TC_CRYPTO_SUCCESS (1) if the signature generated successfully
  83. * returns TC_CRYPTO_FAIL (0) if an error occurred.
  84. *
  85. * @param p_private_key IN -- Your private key.
  86. * @param p_message_hash IN -- The hash of the message to sign.
  87. * @param p_hash_size IN -- The size of p_message_hash in bytes.
  88. * @param p_signature OUT -- Will be filled in with the signature value. Must be
  89. * at least 2 * curve size long (for secp256r1, signature must be 64 bytes long).
  90. *
  91. * @warning A cryptographically-secure PRNG function must be set (using
  92. * uECC_set_rng()) before calling uECC_sign().
  93. * @note Usage: Compute a hash of the data you wish to sign (SHA-2 is
  94. * recommended) and pass it in to this function along with your private key.
  95. * @note side-channel countermeasure: algorithm strengthened against timing
  96. * attack.
  97. */
  98. int uECC_sign(const uint8_t *p_private_key, const uint8_t *p_message_hash,
  99. unsigned p_hash_size, uint8_t *p_signature, uECC_Curve curve);
  100. #ifdef ENABLE_TESTS
  101. /*
  102. * THIS FUNCTION SHOULD BE CALLED FOR TEST PURPOSES ONLY.
  103. * Refer to uECC_sign() function for real applications.
  104. */
  105. int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
  106. unsigned int hash_size, uECC_word_t *k, uint8_t *signature,
  107. uECC_Curve curve);
  108. #endif
  109. /**
  110. * @brief Verify an ECDSA signature.
  111. * @return returns TC_SUCCESS (1) if the signature is valid
  112. * returns TC_FAIL (0) if the signature is invalid.
  113. *
  114. * @param p_public_key IN -- The signer's public key.
  115. * @param p_message_hash IN -- The hash of the signed data.
  116. * @param p_hash_size IN -- The size of p_message_hash in bytes.
  117. * @param p_signature IN -- The signature values.
  118. *
  119. * @note Usage: Compute the hash of the signed data using the same hash as the
  120. * signer and pass it to this function along with the signer's public key and
  121. * the signature values (hash_size and signature).
  122. */
  123. int uECC_verify(const uint8_t *p_public_key, const uint8_t *p_message_hash,
  124. unsigned int p_hash_size, const uint8_t *p_signature, uECC_Curve curve);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif /* __TC_ECC_DSA_H__ */