Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_sphincs.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2025, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19/*
20 * This code is derived in parts from the code distribution provided with
21 * https://github.com/pq-crystals/sphincs
22 *
23 * That code is released under Public Domain
24 * (https://creativecommons.org/share-your-work/public-domain/cc0/);
25 * or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
26 */
27
28#ifndef LC_SPHINCS_H
29#define LC_SPHINCS_H
30
31#include "ext_headers.h"
32
33#if defined __has_include
34#if __has_include("lc_sphincs_shake_256s.h")
36#define LC_SPHINCS_SHAKE_256s_ENABLED
37#endif
38#if __has_include("lc_sphincs_shake_256f.h")
40#define LC_SPHINCS_SHAKE_256f_ENABLED
41#endif
42#if __has_include("lc_sphincs_shake_192s.h")
44#define LC_SPHINCS_SHAKE_192s_ENABLED
45#endif
46#if __has_include("lc_sphincs_shake_192f.h")
48#define LC_SPHINCS_SHAKE_192f_ENABLED
49#endif
50#if __has_include("lc_sphincs_shake_128s.h")
52#define LC_SPHINCS_SHAKE_128s_ENABLED
53#endif
54#if __has_include("lc_sphincs_shake_128f.h")
56#define LC_SPHINCS_SHAKE_128f_ENABLED
57#endif
58#else
59#error "Compiler misses __has_include"
60#endif
61
62#ifdef __cplusplus
63extern "C" {
64#endif
65
82
123
129 union {
130#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
131 struct lc_sphincs_shake_256s_sk sk_shake_256s;
132#endif
133#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
134 struct lc_sphincs_shake_256f_sk sk_shake_256f;
135#endif
136#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
137 struct lc_sphincs_shake_192s_sk sk_shake_192s;
138#endif
139#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
140 struct lc_sphincs_shake_192f_sk sk_shake_192f;
141#endif
142#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
143 struct lc_sphincs_shake_128s_sk sk_shake_128s;
144#endif
145#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
146 struct lc_sphincs_shake_128f_sk sk_shake_128f;
147#endif
149};
150
156 union {
157#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
158 struct lc_sphincs_shake_256s_pk pk_shake_256s;
159#endif
160#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
161 struct lc_sphincs_shake_256f_pk pk_shake_256f;
162#endif
163#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
164 struct lc_sphincs_shake_192s_pk pk_shake_192s;
165#endif
166#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
167 struct lc_sphincs_shake_192f_pk pk_shake_192f;
168#endif
169#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
170 struct lc_sphincs_shake_128s_pk pk_shake_128s;
171#endif
172#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
173 struct lc_sphincs_shake_128f_pk pk_shake_128f;
174#endif
176};
177
183 union {
184#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
185 struct lc_sphincs_shake_256s_sig sig_shake_256s;
186#endif
187#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
188 struct lc_sphincs_shake_256f_sig sig_shake_256f;
189#endif
190#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
191 struct lc_sphincs_shake_192s_sig sig_shake_192s;
192#endif
193#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
194 struct lc_sphincs_shake_192f_sig sig_shake_192f;
195#endif
196#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
197 struct lc_sphincs_shake_128s_sig sig_shake_128s;
198#endif
199#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
200 struct lc_sphincs_shake_128f_sig sig_shake_128f;
201#endif
203};
204
214
222
230
239
251 const struct lc_hash *hash);
252
262void lc_sphincs_ctx_userctx(struct lc_sphincs_ctx *ctx, const uint8_t *userctx,
263 size_t userctxlen);
264
274
284
294
303LC_PURE unsigned int lc_sphincs_sk_size(enum lc_sphincs_type sphincs_type);
304
313LC_PURE unsigned int lc_sphincs_pk_size(enum lc_sphincs_type sphincs_type);
314
323LC_PURE unsigned int lc_sphincs_sig_size(enum lc_sphincs_type sphincs_type);
324
336int lc_sphincs_sk_load(struct lc_sphincs_sk *sk, const uint8_t *src_key,
337 size_t src_key_len);
338
353
368
380int lc_sphincs_pk_load(struct lc_sphincs_pk *pk, const uint8_t *src_key,
381 size_t src_key_len);
382
397
412
424int lc_sphincs_sig_load(struct lc_sphincs_sig *sig, const uint8_t *src_sig,
425 size_t src_sig_len);
426
440int lc_sphincs_sk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len,
441 struct lc_sphincs_sk *sk);
442
456int lc_sphincs_pk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len,
457 struct lc_sphincs_pk *pk);
458
473int lc_sphincs_sig_ptr(uint8_t **sphincs_sig, size_t *sphincs_sig_len,
474 struct lc_sphincs_sig *sig);
475
488 struct lc_rng_ctx *rng_ctx,
489 enum lc_sphincs_type sphincs_type);
490
509 struct lc_sphincs_sk *sk, const uint8_t *seed,
510 size_t seedlen,
511 enum lc_sphincs_type sphincs_type);
512
523int lc_sphincs_pct(const struct lc_sphincs_pk *pk,
524 const struct lc_sphincs_sk *sk);
525
540int lc_sphincs_sign(struct lc_sphincs_sig *sig, const uint8_t *m, size_t mlen,
541 const struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx);
542
574 const uint8_t *m, size_t mlen,
575 const struct lc_sphincs_sk *sk,
576 struct lc_rng_ctx *rng_ctx);
577
612 const struct lc_sphincs_sk *sk);
613
629int lc_sphincs_sign_update(struct lc_sphincs_ctx *ctx, const uint8_t *m,
630 size_t mlen);
631
648 struct lc_sphincs_ctx *ctx,
649 const struct lc_sphincs_sk *sk,
650 struct lc_rng_ctx *rng_ctx);
651
664int lc_sphincs_verify(const struct lc_sphincs_sig *sig, const uint8_t *m,
665 size_t mlen, const struct lc_sphincs_pk *pk);
666
684 struct lc_sphincs_ctx *ctx, const uint8_t *m,
685 size_t mlen, const struct lc_sphincs_pk *pk);
686
722 const struct lc_sphincs_pk *pk);
723
740int lc_sphincs_verify_update(struct lc_sphincs_ctx *ctx, const uint8_t *m,
741 size_t mlen);
742
757 struct lc_sphincs_ctx *ctx,
758 const struct lc_sphincs_pk *pk);
759
760#ifdef __cplusplus
761}
762#endif
763
764#endif /* LC_SPHINCS_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_sphincs_sign_init(struct lc_sphincs_ctx *ctx, const struct lc_sphincs_sk *sk)
Initializes a signature operation.
int lc_sphincs_verify(const struct lc_sphincs_sig *sig, const uint8_t *m, size_t mlen, const struct lc_sphincs_pk *pk)
Verifies signature in one shot.
enum lc_sphincs_type lc_sphincs_sk_type(const struct lc_sphincs_sk *sk)
Obtain Sphincs type from secret key.
int lc_sphincs_pk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len, struct lc_sphincs_pk *pk)
Obtain the reference to the Sphincs key and its length.
int lc_sphincs_verify_final(const struct lc_sphincs_sig *sig, struct lc_sphincs_ctx *ctx, const struct lc_sphincs_pk *pk)
Verifies signature.
int lc_sphincs_keypair(struct lc_sphincs_pk *pk, struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx, enum lc_sphincs_type sphincs_type)
Generates Sphincs public and private key.
int lc_sphincs_sign_update(struct lc_sphincs_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
int lc_sphincs_verify_ctx(const struct lc_sphincs_sig *sig, struct lc_sphincs_ctx *ctx, const uint8_t *m, size_t mlen, const struct lc_sphincs_pk *pk)
Verifies signature with Sphincs context in one shot.
int lc_sphincs_pk_load(struct lc_sphincs_pk *pk, const uint8_t *src_key, size_t src_key_len)
Load a Sphincs public key provided with a buffer into the leancrypto data structure.
int lc_sphincs_sig_ptr(uint8_t **sphincs_sig, size_t *sphincs_sig_len, struct lc_sphincs_sig *sig)
Obtain the reference to the Sphincs signature and its length.
int lc_sphincs_sk_set_keytype_fast(struct lc_sphincs_sk *sk)
Set Sphincs key type to fast.
int lc_sphincs_sign_ctx(struct lc_sphincs_sig *sig, struct lc_sphincs_ctx *ctx, const uint8_t *m, size_t mlen, const struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature woth user context in one shot.
void lc_sphincs_ctx_hash(struct lc_sphincs_ctx *ctx, const struct lc_hash *hash)
Set the hash type that was used for pre-hashing the message. The message digest is used with the Hash...
int lc_sphincs_sk_set_keytype_small(struct lc_sphincs_sk *sk)
Set Sphincs key type to small.
LC_PURE unsigned int lc_sphincs_sk_size(enum lc_sphincs_type sphincs_type)
Return the size of the Sphincs secret key.
void lc_sphincs_ctx_userctx(struct lc_sphincs_ctx *ctx, const uint8_t *userctx, size_t userctxlen)
Specify the optional user context string to be applied with the Sphincs signature operation.
int lc_sphincs_sig_load(struct lc_sphincs_sig *sig, const uint8_t *src_sig, size_t src_sig_len)
Load a Sphincs signature provided with a buffer into the leancrypto data structure.
int lc_sphincs_sk_load(struct lc_sphincs_sk *sk, const uint8_t *src_key, size_t src_key_len)
Load a Sphincs secret key provided with a buffer into the leancrypto data structure.
int lc_sphincs_sign_final(struct lc_sphincs_sig *sig, struct lc_sphincs_ctx *ctx, const struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature.
int lc_sphincs_keypair_from_seed(struct lc_sphincs_pk *pk, struct lc_sphincs_sk *sk, const uint8_t *seed, size_t seedlen, enum lc_sphincs_type sphincs_type)
Generates Sphincs public and private key from a given seed.
int lc_sphincs_pk_set_keytype_small(struct lc_sphincs_pk *pk)
Set Sphincs key type to small.
void lc_sphincs_ctx_zero_free(struct lc_sphincs_ctx *ctx)
Zeroizes and frees Sphincs context on heap.
int lc_sphincs_pk_set_keytype_fast(struct lc_sphincs_pk *pk)
Set Sphincs key type to fast.
int lc_sphincs_sk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len, struct lc_sphincs_sk *sk)
Obtain the reference to the Sphincs key and its length.
int lc_sphincs_ctx_alloc(struct lc_sphincs_ctx **ctx)
Allocates Sphincs context on heap.
int lc_sphincs_verify_init(struct lc_sphincs_ctx *ctx, const struct lc_sphincs_pk *pk)
Initializes a signature verification operation.
int lc_sphincs_sign(struct lc_sphincs_sig *sig, const uint8_t *m, size_t mlen, const struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx)
Computes signature in one shot.
LC_PURE unsigned int lc_sphincs_pk_size(enum lc_sphincs_type sphincs_type)
Return the size of the Sphincs public key.
int lc_sphincs_verify_update(struct lc_sphincs_ctx *ctx, const uint8_t *m, size_t mlen)
Add more data to an already initialized signature state.
void lc_sphincs_ctx_zero(struct lc_sphincs_ctx *ctx)
Zeroizes Sphincs context either on heap or on stack.
enum lc_sphincs_type lc_sphincs_pk_type(const struct lc_sphincs_pk *pk)
Obtain Sphincs type from public key.
void lc_sphincs_ctx_internal(struct lc_sphincs_ctx *ctx)
Mark the Sphincs context to execute SLH-DSA.Sign_internal / SLH-DSA.Verify_internal.
LC_PURE unsigned int lc_sphincs_sig_size(enum lc_sphincs_type sphincs_type)
Return the size of the Sphincs signature.
enum lc_sphincs_type lc_sphincs_sig_type(const struct lc_sphincs_sig *sig)
Obtain Sphincs type from signature.
enum lc_sphincs_type sphincs_type
Definition lc_sphincs.h:128
union lc_sphincs_pk::@326231214261337372240341247366131072053320224026 key
lc_sphincs_type
Definition lc_sphincs.h:66
@ LC_SPHINCS_SHAKE_192f
Definition lc_sphincs.h:76
@ LC_SPHINCS_SHAKE_128s
Definition lc_sphincs.h:78
@ LC_SPHINCS_SHAKE_256s
Definition lc_sphincs.h:70
@ LC_SPHINCS_SHAKE_192s
Definition lc_sphincs.h:74
@ LC_SPHINCS_SHAKE_256f
Definition lc_sphincs.h:72
@ LC_SPHINCS_SHAKE_128f
Definition lc_sphincs.h:80
@ LC_SPHINCS_UNKNOWN
Definition lc_sphincs.h:68
union lc_sphincs_sk::@264262264322156246104063265222263154340010311235 key
enum lc_sphincs_type sphincs_type
Definition lc_sphincs.h:182
union lc_sphincs_sig::@053032136164360237044025143141001047257273346115 sig
int lc_sphincs_pct(const struct lc_sphincs_pk *pk, const struct lc_sphincs_sk *sk)
Pairwise consistency check as per FIPS 140 IG.
enum lc_sphincs_type sphincs_type
Definition lc_sphincs.h:155
Sphincs public key.
Definition lc_sphincs.h:154
Sphincs signature.
Definition lc_sphincs.h:181
Sphincs secret key.
Definition lc_sphincs.h:127