1*f0865ec9SKyle Evans /* 2*f0865ec9SKyle Evans * Copyright (C) 2022 - This file is part of libecc project 3*f0865ec9SKyle Evans * 4*f0865ec9SKyle Evans * Authors: 5*f0865ec9SKyle Evans * Ryad BENADJILA <ryadbenadjila@gmail.com> 6*f0865ec9SKyle Evans * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 7*f0865ec9SKyle Evans * 8*f0865ec9SKyle Evans * This software is licensed under a dual BSD and GPL v2 license. 9*f0865ec9SKyle Evans * See LICENSE file at the root folder of the project. 10*f0865ec9SKyle Evans */ 11*f0865ec9SKyle Evans #include <libecc/lib_ecc_config.h> 12*f0865ec9SKyle Evans #include <libecc/lib_ecc_types.h> 13*f0865ec9SKyle Evans #if defined(WITH_SIG_BIGN) || defined(WITH_SIG_DBIGN) 14*f0865ec9SKyle Evans 15*f0865ec9SKyle Evans #ifndef __BIGN_COMMON_H__ 16*f0865ec9SKyle Evans #define __BIGN_COMMON_H__ 17*f0865ec9SKyle Evans 18*f0865ec9SKyle Evans #include <libecc/words/words.h> 19*f0865ec9SKyle Evans #include <libecc/sig/ec_key.h> 20*f0865ec9SKyle Evans #include <libecc/hash/hash_algs.h> 21*f0865ec9SKyle Evans #include <libecc/curves/curves.h> 22*f0865ec9SKyle Evans #include <libecc/utils/utils.h> 23*f0865ec9SKyle Evans 24*f0865ec9SKyle Evans 25*f0865ec9SKyle Evans /* NOTE: BIGN uses per its standard the BELT-HASH hash function as its "internal" 26*f0865ec9SKyle Evans * hash function, as well as the BELT encryption block cipher during the deterministic 27*f0865ec9SKyle Evans * computation of the nonce for the deterministic version of BIGN. 28*f0865ec9SKyle Evans * Hence the sanity check below. 29*f0865ec9SKyle Evans */ 30*f0865ec9SKyle Evans #if !defined(WITH_HASH_BELT_HASH) 31*f0865ec9SKyle Evans #error "BIGN and DBIGN need BELT-HASH, please activate it!" 32*f0865ec9SKyle Evans #endif 33*f0865ec9SKyle Evans 34*f0865ec9SKyle Evans #define BIGN_S0_LEN(q_bit_len) (BYTECEIL(q_bit_len) / 2) 35*f0865ec9SKyle Evans #define BIGN_S1_LEN(q_bit_len) (BYTECEIL(q_bit_len)) 36*f0865ec9SKyle Evans #define BIGN_SIGLEN(q_bit_len) (BIGN_S0_LEN(q_bit_len) + \ 37*f0865ec9SKyle Evans BIGN_S1_LEN(q_bit_len)) 38*f0865ec9SKyle Evans #define BIGN_MAX_SIGLEN BIGN_SIGLEN(CURVES_MAX_Q_BIT_LEN) 39*f0865ec9SKyle Evans 40*f0865ec9SKyle Evans /* 41*f0865ec9SKyle Evans * Compute max signature length for all the mechanisms enabled 42*f0865ec9SKyle Evans * in the library (see lib_ecc_config.h). Having that done during 43*f0865ec9SKyle Evans * preprocessing sadly requires some verbosity. 44*f0865ec9SKyle Evans */ 45*f0865ec9SKyle Evans #ifndef EC_MAX_SIGLEN 46*f0865ec9SKyle Evans #define EC_MAX_SIGLEN 0 47*f0865ec9SKyle Evans #endif 48*f0865ec9SKyle Evans #if ((EC_MAX_SIGLEN) < (BIGN_MAX_SIGLEN)) 49*f0865ec9SKyle Evans #undef EC_MAX_SIGLEN 50*f0865ec9SKyle Evans #define EC_MAX_SIGLEN BIGN_MAX_SIGLEN 51*f0865ec9SKyle Evans #endif 52*f0865ec9SKyle Evans 53*f0865ec9SKyle Evans 54*f0865ec9SKyle Evans /* The additional data for bign are specific. We provide 55*f0865ec9SKyle Evans * helpers to extract them from an adata pointer. 56*f0865ec9SKyle Evans */ 57*f0865ec9SKyle Evans int bign_get_oid_from_adata(const u8 *adata, u16 adata_len, const u8 **oid_ptr, u16 *oid_len); 58*f0865ec9SKyle Evans 59*f0865ec9SKyle Evans int bign_get_t_from_adata(const u8 *adata, u16 adata_len, const u8 **t_ptr, u16 *t_len); 60*f0865ec9SKyle Evans 61*f0865ec9SKyle Evans int bign_set_adata(u8 *adata, u16 adata_len, const u8 *oid, u16 oid_len, const u8 *t, u16 t_len); 62*f0865ec9SKyle Evans 63*f0865ec9SKyle Evans 64*f0865ec9SKyle Evans typedef struct { 65*f0865ec9SKyle Evans hash_context h_ctx; 66*f0865ec9SKyle Evans word_t magic; 67*f0865ec9SKyle Evans } bign_sign_data; 68*f0865ec9SKyle Evans 69*f0865ec9SKyle Evans struct ec_sign_context; 70*f0865ec9SKyle Evans 71*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_init_pub_key(ec_pub_key *out_pub, const ec_priv_key *in_priv, ec_alg_type key_type); 72*f0865ec9SKyle Evans 73*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_siglen(u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize, u8 *siglen); 74*f0865ec9SKyle Evans 75*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_sign_init(struct ec_sign_context *ctx, ec_alg_type key_type); 76*f0865ec9SKyle Evans 77*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_sign_update(struct ec_sign_context *ctx, 78*f0865ec9SKyle Evans const u8 *chunk, u32 chunklen, ec_alg_type key_type); 79*f0865ec9SKyle Evans 80*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_sign_finalize(struct ec_sign_context *ctx, u8 *sig, u8 siglen, ec_alg_type key_type); 81*f0865ec9SKyle Evans 82*f0865ec9SKyle Evans typedef struct { 83*f0865ec9SKyle Evans u8 s0_sig[BIGN_S0_LEN(CURVES_MAX_Q_BIT_LEN)]; 84*f0865ec9SKyle Evans nn s0; 85*f0865ec9SKyle Evans nn s1; 86*f0865ec9SKyle Evans hash_context h_ctx; 87*f0865ec9SKyle Evans word_t magic; 88*f0865ec9SKyle Evans } bign_verify_data; 89*f0865ec9SKyle Evans 90*f0865ec9SKyle Evans struct ec_verify_context; 91*f0865ec9SKyle Evans 92*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_verify_init(struct ec_verify_context *ctx, 93*f0865ec9SKyle Evans const u8 *sig, u8 siglen, ec_alg_type key_type); 94*f0865ec9SKyle Evans 95*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_verify_update(struct ec_verify_context *ctx, 96*f0865ec9SKyle Evans const u8 *chunk, u32 chunklen, ec_alg_type key_type); 97*f0865ec9SKyle Evans 98*f0865ec9SKyle Evans ATTRIBUTE_WARN_UNUSED_RET int __bign_verify_finalize(struct ec_verify_context *ctx, ec_alg_type key_type); 99*f0865ec9SKyle Evans 100*f0865ec9SKyle Evans #endif /* __BIGN_COMMON_H__ */ 101*f0865ec9SKyle Evans #endif /* defined(WITH_SIG_BIGN) || defined(WITH_SIG_DBIGN) */ 102