sphincs.h (6252B)
1 /* sphincs.h 2 * 3 * Copyright (C) 2006-2025 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 /*! 23 \file wolfssl/wolfcrypt/sphincs.h 24 */ 25 26 /* Interfaces for Sphincs: 27 * - SPHINCS_FAST_LEVEL1 (AKA SPHINCS+-SHAKE-128f-simple) 28 * - SPHINCS_FAST_LEVEL3 (AKA SPHINCS+-SHAKE-192f-simple) 29 * - SPHINCS_FAST_LEVEL5 (AKA SPHINCS+-SHAKE-256f-simple) 30 * - SPHINCS_SMALL_LEVEL1 (AKA SPHINCS+-SHAKE-128s-simple) 31 * - SPHINCS_SMALL_LEVEL3 (AKA SPHINCS+-SHAKE-192s-simple) 32 * - SPHINCS_SMALL_LEVEL5 (AKA SPHINCS+-SHAKE-256s-simple) 33 */ 34 35 #ifndef WOLF_CRYPT_SPHINCS_H 36 #define WOLF_CRYPT_SPHINCS_H 37 38 #include <wolfssl/wolfcrypt/types.h> 39 40 #if defined(HAVE_PQC) && defined(HAVE_SPHINCS) 41 42 #ifdef HAVE_LIBOQS 43 #include <oqs/oqs.h> 44 #include <wolfssl/wolfcrypt/port/liboqs/liboqs.h> 45 #endif 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 /* Macros Definitions */ 52 53 #ifdef HAVE_LIBOQS 54 55 #define SPHINCS_FAST_LEVEL1_SIG_SIZE OQS_SIG_sphincs_shake_128f_simple_length_signature 56 #define SPHINCS_FAST_LEVEL3_SIG_SIZE OQS_SIG_sphincs_shake_192f_simple_length_signature 57 #define SPHINCS_FAST_LEVEL5_SIG_SIZE OQS_SIG_sphincs_shake_256f_simple_length_signature 58 #define SPHINCS_SMALL_LEVEL1_SIG_SIZE OQS_SIG_sphincs_shake_128s_simple_length_signature 59 #define SPHINCS_SMALL_LEVEL3_SIG_SIZE OQS_SIG_sphincs_shake_192s_simple_length_signature 60 #define SPHINCS_SMALL_LEVEL5_SIG_SIZE OQS_SIG_sphincs_shake_256s_simple_length_signature 61 62 #define SPHINCS_LEVEL1_KEY_SIZE OQS_SIG_sphincs_shake_128f_simple_length_secret_key 63 #define SPHINCS_LEVEL1_PUB_KEY_SIZE OQS_SIG_sphincs_shake_128f_simple_length_public_key 64 #define SPHINCS_LEVEL1_PRV_KEY_SIZE (SPHINCS_LEVEL1_PUB_KEY_SIZE+SPHINCS_LEVEL1_KEY_SIZE) 65 66 #define SPHINCS_LEVEL3_KEY_SIZE OQS_SIG_sphincs_shake_192f_simple_length_secret_key 67 #define SPHINCS_LEVEL3_PUB_KEY_SIZE OQS_SIG_sphincs_shake_192f_simple_length_public_key 68 #define SPHINCS_LEVEL3_PRV_KEY_SIZE (SPHINCS_LEVEL3_PUB_KEY_SIZE+SPHINCS_LEVEL3_KEY_SIZE) 69 70 #define SPHINCS_LEVEL5_KEY_SIZE OQS_SIG_sphincs_shake_256f_simple_length_secret_key 71 #define SPHINCS_LEVEL5_PUB_KEY_SIZE OQS_SIG_sphincs_shake_256f_simple_length_public_key 72 #define SPHINCS_LEVEL5_PRV_KEY_SIZE (SPHINCS_LEVEL5_PUB_KEY_SIZE+SPHINCS_LEVEL5_KEY_SIZE) 73 #endif 74 75 #define SPHINCS_MAX_SIG_SIZE SPHINCS_FAST_LEVEL5_SIG_SIZE 76 #define SPHINCS_MAX_KEY_SIZE SPHINCS_LEVEL5_PRV_KEY_SIZE 77 #define SPHINCS_MAX_PUB_KEY_SIZE SPHINCS_LEVEL5_PUB_KEY_SIZE 78 #define SPHINCS_MAX_PRV_KEY_SIZE SPHINCS_LEVEL5_PRV_KEY_SIZE 79 80 #define FAST_VARIANT 1 81 #define SMALL_VARIANT 2 82 83 /* Structs */ 84 85 struct sphincs_key { 86 bool pubKeySet; 87 bool prvKeySet; 88 byte level; /* 1,3 or 5 */ 89 byte optim; /* FAST_VARIANT or SMALL_VARIANT */ 90 byte p[SPHINCS_MAX_PUB_KEY_SIZE]; 91 byte k[SPHINCS_MAX_PRV_KEY_SIZE]; 92 }; 93 94 #ifndef WC_SPHINCSKEY_TYPE_DEFINED 95 typedef struct sphincs_key sphincs_key; 96 #define WC_SPHINCSKEY_TYPE_DEFINED 97 #endif 98 99 /* Functions */ 100 101 WOLFSSL_API 102 int wc_sphincs_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen, 103 sphincs_key* key, WC_RNG* rng); 104 WOLFSSL_API 105 int wc_sphincs_verify_msg(const byte* sig, word32 sigLen, const byte* msg, 106 word32 msgLen, int* res, sphincs_key* key); 107 108 WOLFSSL_API 109 int wc_sphincs_init(sphincs_key* key); 110 WOLFSSL_API 111 int wc_sphincs_set_level_and_optim(sphincs_key* key, byte level, byte optim); 112 WOLFSSL_API 113 int wc_sphincs_get_level_and_optim(sphincs_key* key, byte* level, byte *optim); 114 WOLFSSL_API 115 void wc_sphincs_free(sphincs_key* key); 116 117 WOLFSSL_API 118 int wc_sphincs_import_public(const byte* in, word32 inLen, sphincs_key* key); 119 WOLFSSL_API 120 int wc_sphincs_import_private_only(const byte* priv, word32 privSz, 121 sphincs_key* key); 122 WOLFSSL_API 123 int wc_sphincs_import_private_key(const byte* priv, word32 privSz, 124 const byte* pub, word32 pubSz, 125 sphincs_key* key); 126 127 WOLFSSL_API 128 int wc_sphincs_export_public(sphincs_key* key, byte* out, word32* outLen); 129 WOLFSSL_API 130 int wc_sphincs_export_private_only(sphincs_key* key, byte* out, word32* outLen); 131 WOLFSSL_API 132 int wc_sphincs_export_private(sphincs_key* key, byte* out, word32* outLen); 133 WOLFSSL_API 134 int wc_sphincs_export_key(sphincs_key* key, byte* priv, word32 *privSz, 135 byte* pub, word32 *pubSz); 136 137 WOLFSSL_API 138 int wc_sphincs_check_key(sphincs_key* key); 139 140 WOLFSSL_API 141 int wc_sphincs_size(sphincs_key* key); 142 WOLFSSL_API 143 int wc_sphincs_priv_size(sphincs_key* key); 144 WOLFSSL_API 145 int wc_sphincs_pub_size(sphincs_key* key); 146 WOLFSSL_API 147 int wc_sphincs_sig_size(sphincs_key* key); 148 149 WOLFSSL_API int wc_Sphincs_PrivateKeyDecode(const byte* input, 150 word32* inOutIdx, 151 sphincs_key* key, word32 inSz); 152 WOLFSSL_API int wc_Sphincs_PublicKeyDecode(const byte* input, 153 word32* inOutIdx, 154 sphincs_key* key, word32 inSz); 155 WOLFSSL_API int wc_Sphincs_KeyToDer(sphincs_key* key, byte* output, 156 word32 inLen); 157 WOLFSSL_API int wc_Sphincs_PrivateKeyToDer(sphincs_key* key, byte* output, 158 word32 inLen); 159 WOLFSSL_API int wc_Sphincs_PublicKeyToDer(sphincs_key* key, byte* output, 160 word32 inLen, int withAlg); 161 162 #ifdef __cplusplus 163 } /* extern "C" */ 164 #endif 165 166 #endif /* HAVE_PQC && HAVE_SPHINCS */ 167 #endif /* WOLF_CRYPT_SPHINCS_H */