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