ecc.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2013, Kenneth MacKay
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _CRYPTO_ECC_H
  27. #define _CRYPTO_ECC_H
  28. #define ECC_MAX_DIGITS 4 /* 256 */
  29. #define ECC_DIGITS_TO_BYTES_SHIFT 3
  30. /**
  31. * ecc_is_key_valid() - Validate a given ECDH private key
  32. *
  33. * @curve_id: id representing the curve to use
  34. * @ndigits: curve's number of digits
  35. * @private_key: private key to be used for the given curve
  36. * @private_key_len: private key length
  37. *
  38. * Returns 0 if the key is acceptable, a negative value otherwise
  39. */
  40. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  41. const u64 *private_key, unsigned int private_key_len);
  42. /**
  43. * ecc_gen_privkey() - Generates an ECC private key.
  44. * The private key is a random integer in the range 0 < random < n, where n is a
  45. * prime that is the order of the cyclic subgroup generated by the distinguished
  46. * point G.
  47. * @curve_id: id representing the curve to use
  48. * @ndigits: curve number of digits
  49. * @private_key: buffer for storing the generated private key
  50. *
  51. * Returns 0 if the private key was generated successfully, a negative value
  52. * if an error occurred.
  53. */
  54. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
  55. /**
  56. * ecc_make_pub_key() - Compute an ECC public key
  57. *
  58. * @curve_id: id representing the curve to use
  59. * @ndigits: curve's number of digits
  60. * @private_key: pregenerated private key for the given curve
  61. * @public_key: buffer for storing the generated public key
  62. *
  63. * Returns 0 if the public key was generated successfully, a negative value
  64. * if an error occurred.
  65. */
  66. int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
  67. const u64 *private_key, u64 *public_key);
  68. /**
  69. * crypto_ecdh_shared_secret() - Compute a shared secret
  70. *
  71. * @curve_id: id representing the curve to use
  72. * @ndigits: curve's number of digits
  73. * @private_key: private key of part A
  74. * @public_key: public key of counterpart B
  75. * @secret: buffer for storing the calculated shared secret
  76. *
  77. * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
  78. * before using it for symmetric encryption or HMAC.
  79. *
  80. * Returns 0 if the shared secret was generated successfully, a negative value
  81. * if an error occurred.
  82. */
  83. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  84. const u64 *private_key, const u64 *public_key,
  85. u64 *secret);
  86. #endif