hrss.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2018, 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_HRSS_H
  15. #define OPENSSL_HEADER_HRSS_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. // HRSS
  21. //
  22. // HRSS is a structured-lattice-based post-quantum key encapsulation mechanism.
  23. // The best exposition is https://eprint.iacr.org/2017/667.pdf although this
  24. // implementation uses a different KEM construction based on
  25. // https://eprint.iacr.org/2017/1005.pdf.
  26. struct HRSS_private_key {
  27. uint8_t opaque[1808];
  28. };
  29. struct HRSS_public_key {
  30. uint8_t opaque[1424];
  31. };
  32. // HRSS_SAMPLE_BYTES is the number of bytes of entropy needed to generate a
  33. // short vector. There are 701 coefficients, but the final one is always set to
  34. // zero when sampling. Otherwise, we need one byte of input per coefficient.
  35. #define HRSS_SAMPLE_BYTES (701 - 1)
  36. // HRSS_GENERATE_KEY_BYTES is the number of bytes of entropy needed to generate
  37. // an HRSS key pair.
  38. #define HRSS_GENERATE_KEY_BYTES (HRSS_SAMPLE_BYTES + HRSS_SAMPLE_BYTES + 32)
  39. // HRSS_ENCAP_BYTES is the number of bytes of entropy needed to encapsulate a
  40. // session key.
  41. #define HRSS_ENCAP_BYTES (HRSS_SAMPLE_BYTES + HRSS_SAMPLE_BYTES)
  42. // HRSS_PUBLIC_KEY_BYTES is the number of bytes in a public key.
  43. #define HRSS_PUBLIC_KEY_BYTES 1138
  44. // HRSS_CIPHERTEXT_BYTES is the number of bytes in a ciphertext.
  45. #define HRSS_CIPHERTEXT_BYTES 1138
  46. // HRSS_KEY_BYTES is the number of bytes in a shared key.
  47. #define HRSS_KEY_BYTES 32
  48. // HRSS_POLY3_BYTES is the number of bytes needed to serialise a mod 3
  49. // polynomial.
  50. #define HRSS_POLY3_BYTES 140
  51. #define HRSS_PRIVATE_KEY_BYTES \
  52. (HRSS_POLY3_BYTES * 2 + HRSS_PUBLIC_KEY_BYTES + 2 + 32)
  53. // HRSS_generate_key is a deterministic function that outputs a public and
  54. // private key based on the given entropy. It returns one on success or zero
  55. // on malloc failure.
  56. OPENSSL_EXPORT int HRSS_generate_key(
  57. struct HRSS_public_key *out_pub, struct HRSS_private_key *out_priv,
  58. const uint8_t input[HRSS_GENERATE_KEY_BYTES]);
  59. // HRSS_encap is a deterministic function the generates and encrypts a random
  60. // session key from the given entropy, writing those values to |out_shared_key|
  61. // and |out_ciphertext|, respectively. It returns one on success or zero on
  62. // malloc failure.
  63. OPENSSL_EXPORT int HRSS_encap(uint8_t out_ciphertext[HRSS_CIPHERTEXT_BYTES],
  64. uint8_t out_shared_key[HRSS_KEY_BYTES],
  65. const struct HRSS_public_key *in_pub,
  66. const uint8_t in[HRSS_ENCAP_BYTES]);
  67. // HRSS_decap decrypts a session key from |ciphertext_len| bytes of
  68. // |ciphertext|. If the ciphertext is valid, the decrypted key is written to
  69. // |out_shared_key|. Otherwise the HMAC of |ciphertext| under a secret key (kept
  70. // in |in_priv|) is written. If the ciphertext is the wrong length then it will
  71. // leak which was done via side-channels. Otherwise it should perform either
  72. // action in constant-time. It returns one on success (whether the ciphertext
  73. // was valid or not) and zero on malloc failure.
  74. OPENSSL_EXPORT int HRSS_decap(uint8_t out_shared_key[HRSS_KEY_BYTES],
  75. const struct HRSS_private_key *in_priv,
  76. const uint8_t *ciphertext, size_t ciphertext_len);
  77. // HRSS_marshal_public_key serialises |in_pub| to |out|.
  78. OPENSSL_EXPORT void HRSS_marshal_public_key(
  79. uint8_t out[HRSS_PUBLIC_KEY_BYTES], const struct HRSS_public_key *in_pub);
  80. // HRSS_parse_public_key sets |*out| to the public-key encoded in |in|. It
  81. // returns true on success and zero on error.
  82. OPENSSL_EXPORT int HRSS_parse_public_key(
  83. struct HRSS_public_key *out, const uint8_t in[HRSS_PUBLIC_KEY_BYTES]);
  84. #if defined(__cplusplus)
  85. } // extern C
  86. #endif
  87. #endif // OPENSSL_HEADER_HRSS_H