Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_sphincs.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2026, 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#include "lc_status.h"
33
34#if defined __has_include
35#if __has_include("lc_sphincs_shake_256s.h")
37#define LC_SPHINCS_SHAKE_256s_ENABLED
38#endif
39#if __has_include("lc_sphincs_shake_256f.h")
41#define LC_SPHINCS_SHAKE_256f_ENABLED
42#endif
43#if __has_include("lc_sphincs_shake_192s.h")
45#define LC_SPHINCS_SHAKE_192s_ENABLED
46#endif
47#if __has_include("lc_sphincs_shake_192f.h")
49#define LC_SPHINCS_SHAKE_192f_ENABLED
50#endif
51#if __has_include("lc_sphincs_shake_128s.h")
53#define LC_SPHINCS_SHAKE_128s_ENABLED
54#endif
55#if __has_include("lc_sphincs_shake_128f.h")
57#define LC_SPHINCS_SHAKE_128f_ENABLED
58#endif
59#else
60#error "Compiler misses __has_include"
61#endif
62
63#ifdef __cplusplus
64extern "C" {
65#endif
66
82};
83
130 union {
131#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
132 struct lc_sphincs_shake_256s_sk sk_shake_256s;
133#endif
134#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
135 struct lc_sphincs_shake_256f_sk sk_shake_256f;
136#endif
137#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
138 struct lc_sphincs_shake_192s_sk sk_shake_192s;
139#endif
140#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
141 struct lc_sphincs_shake_192f_sk sk_shake_192f;
142#endif
143#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
144 struct lc_sphincs_shake_128s_sk sk_shake_128s;
145#endif
146#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
147 struct lc_sphincs_shake_128f_sk sk_shake_128f;
148#endif
150};
151
157 union {
158#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
159 struct lc_sphincs_shake_256s_pk pk_shake_256s;
160#endif
161#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
162 struct lc_sphincs_shake_256f_pk pk_shake_256f;
163#endif
164#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
165 struct lc_sphincs_shake_192s_pk pk_shake_192s;
166#endif
167#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
168 struct lc_sphincs_shake_192f_pk pk_shake_192f;
169#endif
170#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
171 struct lc_sphincs_shake_128s_pk pk_shake_128s;
172#endif
173#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
174 struct lc_sphincs_shake_128f_pk pk_shake_128f;
175#endif
177};
178
184 union {
185#ifdef LC_SPHINCS_SHAKE_256s_ENABLED
186 struct lc_sphincs_shake_256s_sig sig_shake_256s;
187#endif
188#ifdef LC_SPHINCS_SHAKE_256f_ENABLED
189 struct lc_sphincs_shake_256f_sig sig_shake_256f;
190#endif
191#ifdef LC_SPHINCS_SHAKE_192s_ENABLED
192 struct lc_sphincs_shake_192s_sig sig_shake_192s;
193#endif
194#ifdef LC_SPHINCS_SHAKE_192f_ENABLED
195 struct lc_sphincs_shake_192f_sig sig_shake_192f;
196#endif
197#ifdef LC_SPHINCS_SHAKE_128s_ENABLED
198 struct lc_sphincs_shake_128s_sig sig_shake_128s;
199#endif
200#ifdef LC_SPHINCS_SHAKE_128f_ENABLED
201 struct lc_sphincs_shake_128f_sig sig_shake_128f;
202#endif
204};
205
215
223
231
240
252 const struct lc_hash *hash);
253
263void lc_sphincs_ctx_userctx(struct lc_sphincs_ctx *ctx, const uint8_t *userctx,
264 size_t userctxlen);
265
275
285
295
304LC_PURE unsigned int lc_sphincs_sk_size(enum lc_sphincs_type sphincs_type);
305
314LC_PURE unsigned int lc_sphincs_pk_size(enum lc_sphincs_type sphincs_type);
315
324LC_PURE unsigned int lc_sphincs_sig_size(enum lc_sphincs_type sphincs_type);
325
337int lc_sphincs_sk_load(struct lc_sphincs_sk *sk, const uint8_t *src_key,
338 size_t src_key_len);
339
354
369
381int lc_sphincs_pk_load(struct lc_sphincs_pk *pk, const uint8_t *src_key,
382 size_t src_key_len);
383
398
413
425int lc_sphincs_sig_load(struct lc_sphincs_sig *sig, const uint8_t *src_sig,
426 size_t src_sig_len);
427
441int lc_sphincs_sk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len,
442 struct lc_sphincs_sk *sk);
443
457int lc_sphincs_pk_ptr(uint8_t **sphincs_key, size_t *sphincs_key_len,
458 struct lc_sphincs_pk *pk);
459
474int lc_sphincs_sig_ptr(uint8_t **sphincs_sig, size_t *sphincs_sig_len,
475 struct lc_sphincs_sig *sig);
476
489 struct lc_rng_ctx *rng_ctx,
490 enum lc_sphincs_type sphincs_type);
491
510 struct lc_sphincs_sk *sk, const uint8_t *seed,
511 size_t seedlen,
512 enum lc_sphincs_type sphincs_type);
513
524 const struct lc_sphincs_sk *sk);
525
536int lc_sphincs_pct(const struct lc_sphincs_pk *pk,
537 const struct lc_sphincs_sk *sk);
538
553int lc_sphincs_sign(struct lc_sphincs_sig *sig, const uint8_t *m, size_t mlen,
554 const struct lc_sphincs_sk *sk, struct lc_rng_ctx *rng_ctx);
555
587 const uint8_t *m, size_t mlen,
588 const struct lc_sphincs_sk *sk,
589 struct lc_rng_ctx *rng_ctx);
590
625 const struct lc_sphincs_sk *sk);
626
642int lc_sphincs_sign_update(struct lc_sphincs_ctx *ctx, const uint8_t *m,
643 size_t mlen);
644
661 struct lc_sphincs_ctx *ctx,
662 const struct lc_sphincs_sk *sk,
663 struct lc_rng_ctx *rng_ctx);
664
677int lc_sphincs_verify(const struct lc_sphincs_sig *sig, const uint8_t *m,
678 size_t mlen, const struct lc_sphincs_pk *pk);
679
697 struct lc_sphincs_ctx *ctx, const uint8_t *m,
698 size_t mlen, const struct lc_sphincs_pk *pk);
699
735 const struct lc_sphincs_pk *pk);
736
753int lc_sphincs_verify_update(struct lc_sphincs_ctx *ctx, const uint8_t *m,
754 size_t mlen);
755
770 struct lc_sphincs_ctx *ctx,
771 const struct lc_sphincs_pk *pk);
772
782};
783
795 const enum lc_sphincs_alg_operation operation);
796
797#ifdef __cplusplus
798}
799#endif
800
801#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_from_sk(struct lc_sphincs_pk *pk, const struct lc_sphincs_sk *sk)
Generates Sphincs public from private key.
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.
enum lc_alg_status_val lc_sphincs_alg_status(const enum lc_sphincs_type sphincs_type, const enum lc_sphincs_alg_operation operation)
Obtain algorithm status.
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:129
lc_sphincs_alg_operation
Definition: lc_sphincs.h:773
@ lc_alg_operation_sphincs_siggen
Definition: lc_sphincs.h:779
@ lc_alg_operation_sphincs_keygen
Definition: lc_sphincs.h:777
@ lc_alg_operation_sphincs_sigver
Definition: lc_sphincs.h:781
@ lc_alg_operation_sphincs_unknown
Definition: lc_sphincs.h:775
union lc_sphincs_sig::@27 sig
lc_sphincs_type
Definition: lc_sphincs.h:67
@ LC_SPHINCS_SHAKE_192f
Definition: lc_sphincs.h:77
@ LC_SPHINCS_SHAKE_128s
Definition: lc_sphincs.h:79
@ LC_SPHINCS_SHAKE_256s
Definition: lc_sphincs.h:71
@ LC_SPHINCS_SHAKE_192s
Definition: lc_sphincs.h:75
@ LC_SPHINCS_SHAKE_256f
Definition: lc_sphincs.h:73
@ LC_SPHINCS_SHAKE_128f
Definition: lc_sphincs.h:81
@ LC_SPHINCS_UNKNOWN
Definition: lc_sphincs.h:69
union lc_sphincs_sk::@25 key
enum lc_sphincs_type sphincs_type
Definition: lc_sphincs.h:183
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:156
union lc_sphincs_pk::@26 key
Sphincs public key.
Definition: lc_sphincs.h:155
Sphincs signature.
Definition: lc_sphincs.h:182
Sphincs secret key.
Definition: lc_sphincs.h:128
lc_alg_status_val
Definition: lc_status.h:63