You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

148 lines
6.4 KiB

  1. /* Copyright (c) 2016, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_NEWHOPE_H
  15. #define OPENSSL_HEADER_NEWHOPE_H
  16. #include <openssl/base.h>
  17. #include <openssl/sha.h>
  18. #if defined(__cplusplus)
  19. extern "C" {
  20. #endif
  21. /* Post-quantum key agreement, based upon the reference
  22. * implementation. Note: this implementation does not interoperate
  23. * with the reference implementation!
  24. *
  25. * Source: https://github.com/tpoeppelmann/newhope
  26. *
  27. * The authors' permission to use their code is gratefully acknowledged. */
  28. /* NEWHOPE_POLY_new returns a new |NEWHOPE_POLY| object, or NULL on error. */
  29. OPENSSL_EXPORT NEWHOPE_POLY *NEWHOPE_POLY_new(void);
  30. /* NEWHOPE_POLY_free frees |p|. */
  31. OPENSSL_EXPORT void NEWHOPE_POLY_free(NEWHOPE_POLY *p);
  32. /* NEWHOPE_POLY_LENGTH is the size in bytes of the packed representation of a
  33. * polynomial, encoded with 14 bits per coefficient. */
  34. #define NEWHOPE_POLY_LENGTH ((1024 * 14) / 8)
  35. /* NEWHOPE_RECONCILIATION_LENGTH is the size in bytes of the packed
  36. * representation of the reconciliation data, encoded as 2 bits per
  37. * coefficient. */
  38. #define NEWHOPE_RECONCILIATION_LENGTH ((1024 * 2) / 8)
  39. /* NEWHOPE_OFFERMSG_LENGTH is the length of the offering party's message to the
  40. * accepting party. */
  41. #define NEWHOPE_OFFERMSG_LENGTH (NEWHOPE_POLY_LENGTH + 32)
  42. /* NEWHOPE_ACCEPTMSG_LENGTH is the length of the accepting party's message to
  43. * the offering party. */
  44. #define NEWHOPE_ACCEPTMSG_LENGTH \
  45. (NEWHOPE_POLY_LENGTH + NEWHOPE_RECONCILIATION_LENGTH)
  46. /* NEWHOPE_KEY_LENGTH is the size of the result of the key agreement. This
  47. * result is not exposed to callers: instead, it is whitened with SHA-256, whose
  48. * output happens to be the same size. */
  49. #define NEWHOPE_KEY_LENGTH 32
  50. /* NEWHOPE_offer initializes |out_msg| and |out_sk| for a new key
  51. * exchange. |msg| must have room for |NEWHOPE_OFFERMSG_LENGTH| bytes. Neither
  52. * output may be cached. */
  53. OPENSSL_EXPORT void NEWHOPE_offer(uint8_t out_msg[NEWHOPE_OFFERMSG_LENGTH],
  54. NEWHOPE_POLY *out_sk);
  55. /* NEWHOPE_accept completes a key exchange given an offer message |msg|. The
  56. * result of the key exchange is written to |out_key|, which must have space for
  57. * |SHA256_DIGEST_LENGTH| bytes. The message to be send to the offering party is
  58. * written to |out_msg|, which must have room for |NEWHOPE_ACCEPTMSG_LENGTH|
  59. * bytes. Returns 1 on success and 0 on error. */
  60. OPENSSL_EXPORT int NEWHOPE_accept(uint8_t out_key[SHA256_DIGEST_LENGTH],
  61. uint8_t out_msg[NEWHOPE_ACCEPTMSG_LENGTH],
  62. const uint8_t msg[NEWHOPE_OFFERMSG_LENGTH],
  63. size_t msg_len);
  64. /* NEWHOPE_finish completes a key exchange for the offering party, given an
  65. * accept message |msg| and the previously generated secret |sk|. The result of
  66. * the key exchange is written to |out_key|, which must have space for
  67. * |SHA256_DIGEST_LENGTH| bytes. Returns 1 on success and 0 on error. */
  68. OPENSSL_EXPORT int NEWHOPE_finish(uint8_t out_key[SHA256_DIGEST_LENGTH],
  69. const NEWHOPE_POLY *sk,
  70. const uint8_t msg[NEWHOPE_ACCEPTMSG_LENGTH],
  71. size_t msg_len);
  72. /* Lower-level functions. */
  73. /* NEWHOPE_POLY_noise sets |r| to a random polynomial where the coefficients are
  74. * sampled from the noise distribution. */
  75. OPENSSL_EXPORT void NEWHOPE_POLY_noise(NEWHOPE_POLY* r);
  76. /* NEWHOPE_POLY_noise_ntt sets |r| to an output of NEWHOPE_POLY_noise, and then
  77. * applies NTT(r) in-place. */
  78. OPENSSL_EXPORT void NEWHOPE_POLY_noise_ntt(NEWHOPE_POLY* r);
  79. /* NEWHOPE_offer_computation is the work of |NEWHOPE_offer|, less the encoding
  80. * parts. The inputs are the noise polynomials |s| and |e|, and random
  81. * polynomial |a|. The output is the polynomial |pk|. */
  82. OPENSSL_EXPORT void NEWHOPE_offer_computation(
  83. NEWHOPE_POLY *out_pk,
  84. const NEWHOPE_POLY *s, const NEWHOPE_POLY *e, const NEWHOPE_POLY *a);
  85. /* NEWHOPE_accept_computation is the work of |NEWHOPE_accept|, less the encoding
  86. * parts. The inputs from the peer are |pk| and |a|. The locally-generated
  87. * inputs are the noise polynomials |sp|, |ep|, and |epp|, and the random bytes
  88. * |rand|. The outputs are |out_bp| and |out_reconciliation|, and the result of
  89. * key agreement |key|. Returns 1 on success and 0 on failure. */
  90. OPENSSL_EXPORT void NEWHOPE_accept_computation(
  91. uint8_t out_key[NEWHOPE_KEY_LENGTH], NEWHOPE_POLY *out_bp,
  92. NEWHOPE_POLY *out_reconciliation,
  93. const NEWHOPE_POLY *sp, const NEWHOPE_POLY *ep, const NEWHOPE_POLY *epp,
  94. const uint8_t rand[32],
  95. const NEWHOPE_POLY *pk, const NEWHOPE_POLY *a);
  96. /* NEWHOPE_finish_computation is the work of |NEWHOPE_finish|, less the encoding
  97. * parts. Given the peer's |bp| and |reconciliation|, and locally-generated
  98. * noise |noise|, the result of the key agreement is written to out_key.
  99. * Returns 1 on success and 0 on failure. */
  100. OPENSSL_EXPORT void NEWHOPE_finish_computation(
  101. uint8_t out_key[NEWHOPE_KEY_LENGTH], const NEWHOPE_POLY *noise,
  102. const NEWHOPE_POLY *bp, const NEWHOPE_POLY *reconciliation);
  103. /* NEWHOPE_POLY_frombytes decodes |a| into |r|. */
  104. OPENSSL_EXPORT void NEWHOPE_POLY_frombytes(
  105. NEWHOPE_POLY *r, const uint8_t a[NEWHOPE_POLY_LENGTH]);
  106. /* NEWHOPE_POLY_tobytes packs the polynomial |p| into the compact representation
  107. * |r|. */
  108. OPENSSL_EXPORT void NEWHOPE_POLY_tobytes(uint8_t r[NEWHOPE_POLY_LENGTH],
  109. const NEWHOPE_POLY* p);
  110. /* NEWHOPE_offer_frommsg decodes an offer message |msg| into its constituent
  111. * polynomials |out_pk| and |a|. */
  112. OPENSSL_EXPORT void NEWHOPE_offer_frommsg(
  113. NEWHOPE_POLY *out_pk, NEWHOPE_POLY *out_a,
  114. const uint8_t msg[NEWHOPE_OFFERMSG_LENGTH]);
  115. #if defined(__cplusplus)
  116. } /* extern "C" */
  117. #endif
  118. #endif /* OPENSSL_HEADER_NEWHOPE_H */