xref: /aosp_15_r20/external/boringssl/src/include/openssl/experimental/spx.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2023, Google LLC
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 
15 #ifndef OPENSSL_HEADER_SPX_H
16 #define OPENSSL_HEADER_SPX_H
17 
18 #include <openssl/base.h>
19 
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23 
24 
25 #if defined(OPENSSL_UNSTABLE_EXPERIMENTAL_SPX)
26 // This header implements experimental, draft versions of not-yet-standardized
27 // primitives. When the standard is complete, these functions will be removed
28 // and replaced with the final, incompatible standard version. They are
29 // available now for short-lived experiments, but must not be deployed anywhere
30 // durable, such as a long-lived key store. To use these functions define
31 // OPENSSL_UNSTABLE_EXPERIMENTAL_SPX
32 
33 // SPX_N is the number of bytes in the hash output
34 #define SPX_N 16
35 
36 // SPX_PUBLIC_KEY_BYTES is the nNumber of bytes in the public key of
37 // SPHINCS+-SHA2-128s
38 #define SPX_PUBLIC_KEY_BYTES 32
39 
40 // SPX_SECRET_KEY_BYTES is the number of bytes in the private key of
41 // SPHINCS+-SHA2-128s
42 #define SPX_SECRET_KEY_BYTES 64
43 
44 // SPX_SIGNATURE_BYTES is the number of bytes in a signature of
45 // SPHINCS+-SHA2-128s
46 #define SPX_SIGNATURE_BYTES 7856
47 
48 // SPX_generate_key generates a SPHINCS+-SHA2-128s key pair and writes the
49 // result to |out_public_key| and |out_secret_key|.
50 // Private key: SK.seed || SK.prf || PK.seed || PK.root
51 // Public key: PK.seed || PK.root
52 OPENSSL_EXPORT void SPX_generate_key(
53     uint8_t out_public_key[SPX_PUBLIC_KEY_BYTES],
54     uint8_t out_secret_key[SPX_SECRET_KEY_BYTES]);
55 
56 // SPX_generate_key_from_seed generates a SPHINCS+-SHA2-128s key pair from a
57 // 48-byte seed and writes the result to |out_public_key| and |out_secret_key|.
58 // Secret key: SK.seed || SK.prf || PK.seed || PK.root
59 // Public key: PK.seed || PK.root
60 OPENSSL_EXPORT void SPX_generate_key_from_seed(
61     uint8_t out_public_key[SPX_PUBLIC_KEY_BYTES],
62     uint8_t out_secret_key[SPX_SECRET_KEY_BYTES],
63     const uint8_t seed[3 * SPX_N]);
64 
65 // SPX_sign generates a SPHINCS+-SHA2-128s signature over |msg| or length
66 // |msg_len| using |secret_key| and writes the output to |out_signature|.
67 //
68 // if |randomized| is 0, deterministic signing is performed, otherwise,
69 // non-deterministic signing is performed.
70 OPENSSL_EXPORT void SPX_sign(
71     uint8_t out_snignature[SPX_SIGNATURE_BYTES],
72     const uint8_t secret_key[SPX_SECRET_KEY_BYTES], const uint8_t *msg,
73     size_t msg_len, int randomized);
74 
75 // SPX_verify verifies a SPHINCS+-SHA2-128s signature in |signature| over |msg|
76 // or length |msg_len| using |public_key|. 1 is returned if the signature
77 // matches, 0 otherwise.
78 OPENSSL_EXPORT int SPX_verify(
79     const uint8_t signature[SPX_SIGNATURE_BYTES],
80     const uint8_t public_key[SPX_SECRET_KEY_BYTES], const uint8_t *msg,
81     size_t msg_len);
82 
83 #endif //OPENSSL_UNSTABLE_EXPERIMENTAL_SPX
84 
85 
86 #if defined(__cplusplus)
87 }  // extern C
88 #endif
89 
90 #endif  // OPENSSL_HEADER_SPX_H
91