xref: /aosp_15_r20/external/boringssl/include/openssl/experimental/kyber.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1*8fb009dcSAndroid Build Coastguard Worker /* Copyright (c) 2023, Google Inc.
2*8fb009dcSAndroid Build Coastguard Worker  *
3*8fb009dcSAndroid Build Coastguard Worker  * Permission to use, copy, modify, and/or distribute this software for any
4*8fb009dcSAndroid Build Coastguard Worker  * purpose with or without fee is hereby granted, provided that the above
5*8fb009dcSAndroid Build Coastguard Worker  * copyright notice and this permission notice appear in all copies.
6*8fb009dcSAndroid Build Coastguard Worker  *
7*8fb009dcSAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8*8fb009dcSAndroid Build Coastguard Worker  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9*8fb009dcSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10*8fb009dcSAndroid Build Coastguard Worker  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11*8fb009dcSAndroid Build Coastguard Worker  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12*8fb009dcSAndroid Build Coastguard Worker  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13*8fb009dcSAndroid Build Coastguard Worker  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14*8fb009dcSAndroid Build Coastguard Worker 
15*8fb009dcSAndroid Build Coastguard Worker #ifndef OPENSSL_HEADER_KYBER_H
16*8fb009dcSAndroid Build Coastguard Worker #define OPENSSL_HEADER_KYBER_H
17*8fb009dcSAndroid Build Coastguard Worker 
18*8fb009dcSAndroid Build Coastguard Worker #include <openssl/base.h>
19*8fb009dcSAndroid Build Coastguard Worker 
20*8fb009dcSAndroid Build Coastguard Worker #if defined(__cplusplus)
21*8fb009dcSAndroid Build Coastguard Worker extern "C" {
22*8fb009dcSAndroid Build Coastguard Worker #endif
23*8fb009dcSAndroid Build Coastguard Worker 
24*8fb009dcSAndroid Build Coastguard Worker 
25*8fb009dcSAndroid Build Coastguard Worker #if defined(OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER)
26*8fb009dcSAndroid Build Coastguard Worker // This header implements experimental, draft versions of not-yet-standardized
27*8fb009dcSAndroid Build Coastguard Worker // primitives. When the standard is complete, these functions will be removed
28*8fb009dcSAndroid Build Coastguard Worker // and replaced with the final, incompatible standard version. They are
29*8fb009dcSAndroid Build Coastguard Worker // available now for short-lived experiments, but must not be deployed anywhere
30*8fb009dcSAndroid Build Coastguard Worker // durable, such as a long-lived key store. To use these functions define
31*8fb009dcSAndroid Build Coastguard Worker // OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER
32*8fb009dcSAndroid Build Coastguard Worker 
33*8fb009dcSAndroid Build Coastguard Worker // Kyber768.
34*8fb009dcSAndroid Build Coastguard Worker //
35*8fb009dcSAndroid Build Coastguard Worker // This implements the round-3 specification of Kyber, defined at
36*8fb009dcSAndroid Build Coastguard Worker // https://pq-crystals.org/kyber/data/kyber-specification-round3-20210804.pdf
37*8fb009dcSAndroid Build Coastguard Worker 
38*8fb009dcSAndroid Build Coastguard Worker 
39*8fb009dcSAndroid Build Coastguard Worker // KYBER_public_key contains a Kyber768 public key. The contents of this
40*8fb009dcSAndroid Build Coastguard Worker // object should never leave the address space since the format is unstable.
41*8fb009dcSAndroid Build Coastguard Worker struct KYBER_public_key {
42*8fb009dcSAndroid Build Coastguard Worker   union {
43*8fb009dcSAndroid Build Coastguard Worker     uint8_t bytes[512 * (3 + 9) + 32 + 32];
44*8fb009dcSAndroid Build Coastguard Worker     uint16_t alignment;
45*8fb009dcSAndroid Build Coastguard Worker   } opaque;
46*8fb009dcSAndroid Build Coastguard Worker };
47*8fb009dcSAndroid Build Coastguard Worker 
48*8fb009dcSAndroid Build Coastguard Worker // KYBER_private_key contains a Kyber768 private key. The contents of this
49*8fb009dcSAndroid Build Coastguard Worker // object should never leave the address space since the format is unstable.
50*8fb009dcSAndroid Build Coastguard Worker struct KYBER_private_key {
51*8fb009dcSAndroid Build Coastguard Worker   union {
52*8fb009dcSAndroid Build Coastguard Worker     uint8_t bytes[512 * (3 + 3 + 9) + 32 + 32 + 32];
53*8fb009dcSAndroid Build Coastguard Worker     uint16_t alignment;
54*8fb009dcSAndroid Build Coastguard Worker   } opaque;
55*8fb009dcSAndroid Build Coastguard Worker };
56*8fb009dcSAndroid Build Coastguard Worker 
57*8fb009dcSAndroid Build Coastguard Worker // KYBER_PUBLIC_KEY_BYTES is the number of bytes in an encoded Kyber768 public
58*8fb009dcSAndroid Build Coastguard Worker // key.
59*8fb009dcSAndroid Build Coastguard Worker #define KYBER_PUBLIC_KEY_BYTES 1184
60*8fb009dcSAndroid Build Coastguard Worker 
61*8fb009dcSAndroid Build Coastguard Worker // KYBER_SHARED_SECRET_BYTES is the number of bytes in the Kyber768 shared
62*8fb009dcSAndroid Build Coastguard Worker // secret. Although the round-3 specification has a variable-length output, the
63*8fb009dcSAndroid Build Coastguard Worker // final ML-KEM construction is expected to use a fixed 32-byte output. To
64*8fb009dcSAndroid Build Coastguard Worker // simplify the future transition, we apply the same restriction.
65*8fb009dcSAndroid Build Coastguard Worker #define KYBER_SHARED_SECRET_BYTES 32
66*8fb009dcSAndroid Build Coastguard Worker 
67*8fb009dcSAndroid Build Coastguard Worker // KYBER_generate_key generates a random public/private key pair, writes the
68*8fb009dcSAndroid Build Coastguard Worker // encoded public key to |out_encoded_public_key| and sets |out_private_key| to
69*8fb009dcSAndroid Build Coastguard Worker // the private key.
70*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT void KYBER_generate_key(
71*8fb009dcSAndroid Build Coastguard Worker     uint8_t out_encoded_public_key[KYBER_PUBLIC_KEY_BYTES],
72*8fb009dcSAndroid Build Coastguard Worker     struct KYBER_private_key *out_private_key);
73*8fb009dcSAndroid Build Coastguard Worker 
74*8fb009dcSAndroid Build Coastguard Worker // KYBER_public_from_private sets |*out_public_key| to the public key that
75*8fb009dcSAndroid Build Coastguard Worker // corresponds to |private_key|. (This is faster than parsing the output of
76*8fb009dcSAndroid Build Coastguard Worker // |KYBER_generate_key| if, for some reason, you need to encapsulate to a key
77*8fb009dcSAndroid Build Coastguard Worker // that was just generated.)
78*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT void KYBER_public_from_private(
79*8fb009dcSAndroid Build Coastguard Worker     struct KYBER_public_key *out_public_key,
80*8fb009dcSAndroid Build Coastguard Worker     const struct KYBER_private_key *private_key);
81*8fb009dcSAndroid Build Coastguard Worker 
82*8fb009dcSAndroid Build Coastguard Worker // KYBER_CIPHERTEXT_BYTES is number of bytes in the Kyber768 ciphertext.
83*8fb009dcSAndroid Build Coastguard Worker #define KYBER_CIPHERTEXT_BYTES 1088
84*8fb009dcSAndroid Build Coastguard Worker 
85*8fb009dcSAndroid Build Coastguard Worker // KYBER_encap encrypts a random shared secret for |public_key|, writes the
86*8fb009dcSAndroid Build Coastguard Worker // ciphertext to |out_ciphertext|, and writes the random shared secret to
87*8fb009dcSAndroid Build Coastguard Worker // |out_shared_secret|.
88*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT void KYBER_encap(
89*8fb009dcSAndroid Build Coastguard Worker     uint8_t out_ciphertext[KYBER_CIPHERTEXT_BYTES],
90*8fb009dcSAndroid Build Coastguard Worker     uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES],
91*8fb009dcSAndroid Build Coastguard Worker     const struct KYBER_public_key *public_key);
92*8fb009dcSAndroid Build Coastguard Worker 
93*8fb009dcSAndroid Build Coastguard Worker // KYBER_decap decrypts a shared secret from |ciphertext| using |private_key|
94*8fb009dcSAndroid Build Coastguard Worker // and writes it to |out_shared_secret|. If |ciphertext| is invalid,
95*8fb009dcSAndroid Build Coastguard Worker // |out_shared_secret| is filled with a key that will always be the same for the
96*8fb009dcSAndroid Build Coastguard Worker // same |ciphertext| and |private_key|, but which appears to be random unless
97*8fb009dcSAndroid Build Coastguard Worker // one has access to |private_key|. These alternatives occur in constant time.
98*8fb009dcSAndroid Build Coastguard Worker // Any subsequent symmetric encryption using |out_shared_secret| must use an
99*8fb009dcSAndroid Build Coastguard Worker // authenticated encryption scheme in order to discover the decapsulation
100*8fb009dcSAndroid Build Coastguard Worker // failure.
101*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT void KYBER_decap(
102*8fb009dcSAndroid Build Coastguard Worker     uint8_t out_shared_secret[KYBER_SHARED_SECRET_BYTES],
103*8fb009dcSAndroid Build Coastguard Worker     const uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES],
104*8fb009dcSAndroid Build Coastguard Worker     const struct KYBER_private_key *private_key);
105*8fb009dcSAndroid Build Coastguard Worker 
106*8fb009dcSAndroid Build Coastguard Worker 
107*8fb009dcSAndroid Build Coastguard Worker // Serialisation of keys.
108*8fb009dcSAndroid Build Coastguard Worker 
109*8fb009dcSAndroid Build Coastguard Worker // KYBER_marshal_public_key serializes |public_key| to |out| in the standard
110*8fb009dcSAndroid Build Coastguard Worker // format for Kyber public keys. It returns one on success or zero on allocation
111*8fb009dcSAndroid Build Coastguard Worker // error.
112*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int KYBER_marshal_public_key(
113*8fb009dcSAndroid Build Coastguard Worker     CBB *out, const struct KYBER_public_key *public_key);
114*8fb009dcSAndroid Build Coastguard Worker 
115*8fb009dcSAndroid Build Coastguard Worker // KYBER_parse_public_key parses a public key, in the format generated by
116*8fb009dcSAndroid Build Coastguard Worker // |KYBER_marshal_public_key|, from |in| and writes the result to
117*8fb009dcSAndroid Build Coastguard Worker // |out_public_key|. It returns one on success or zero on parse error or if
118*8fb009dcSAndroid Build Coastguard Worker // there are trailing bytes in |in|.
119*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int KYBER_parse_public_key(
120*8fb009dcSAndroid Build Coastguard Worker     struct KYBER_public_key *out_public_key, CBS *in);
121*8fb009dcSAndroid Build Coastguard Worker 
122*8fb009dcSAndroid Build Coastguard Worker // KYBER_marshal_private_key serializes |private_key| to |out| in the standard
123*8fb009dcSAndroid Build Coastguard Worker // format for Kyber private keys. It returns one on success or zero on
124*8fb009dcSAndroid Build Coastguard Worker // allocation error.
125*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int KYBER_marshal_private_key(
126*8fb009dcSAndroid Build Coastguard Worker     CBB *out, const struct KYBER_private_key *private_key);
127*8fb009dcSAndroid Build Coastguard Worker 
128*8fb009dcSAndroid Build Coastguard Worker // KYBER_PRIVATE_KEY_BYTES is the length of the data produced by
129*8fb009dcSAndroid Build Coastguard Worker // |KYBER_marshal_private_key|.
130*8fb009dcSAndroid Build Coastguard Worker #define KYBER_PRIVATE_KEY_BYTES 2400
131*8fb009dcSAndroid Build Coastguard Worker 
132*8fb009dcSAndroid Build Coastguard Worker // KYBER_parse_private_key parses a private key, in the format generated by
133*8fb009dcSAndroid Build Coastguard Worker // |KYBER_marshal_private_key|, from |in| and writes the result to
134*8fb009dcSAndroid Build Coastguard Worker // |out_private_key|. It returns one on success or zero on parse error or if
135*8fb009dcSAndroid Build Coastguard Worker // there are trailing bytes in |in|.
136*8fb009dcSAndroid Build Coastguard Worker OPENSSL_EXPORT int KYBER_parse_private_key(
137*8fb009dcSAndroid Build Coastguard Worker     struct KYBER_private_key *out_private_key, CBS *in);
138*8fb009dcSAndroid Build Coastguard Worker 
139*8fb009dcSAndroid Build Coastguard Worker #endif // OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER
140*8fb009dcSAndroid Build Coastguard Worker 
141*8fb009dcSAndroid Build Coastguard Worker 
142*8fb009dcSAndroid Build Coastguard Worker #if defined(__cplusplus)
143*8fb009dcSAndroid Build Coastguard Worker }  // extern C
144*8fb009dcSAndroid Build Coastguard Worker #endif
145*8fb009dcSAndroid Build Coastguard Worker 
146*8fb009dcSAndroid Build Coastguard Worker #endif  // OPENSSL_HEADER_KYBER_H
147