Path: blob/main/crypto/libecc/src/examples/sss/sss.h
34889 views
/*1* Copyright (C) 2021 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#ifndef __SSS_H__11#define __SSS_H__1213/* NOTE: we redefine some attributes if they are not already defined */14#ifndef ATTRIBUTE_PACKED15#ifdef __GNUC__16#define ATTRIBUTE_PACKED __attribute__((packed))17#else18#define ATTRIBUTE_PACKED19#endif20#endif21#ifndef ATTRIBUTE_WARN_UNUSED_RET22#ifdef __GNUC__23#ifdef USE_WARN_UNUSED_RET24#define ATTRIBUTE_WARN_UNUSED_RET __attribute__((warn_unused_result))25#else26#define ATTRIBUTE_WARN_UNUSED_RET27#endif28#else29#define ATTRIBUTE_WARN_UNUSED_RET30#endif31#endif323334typedef enum { SSS_FALSE = 0, SSS_TRUE = 1 } boolean;3536/* The final secret size in bytes, corresponding to the37* size of an element in Fp with ~256 bit prime.38*/39#define SSS_SECRET_SIZE 324041/* Secrets and shares typedefs for "raw" SSS */42typedef struct ATTRIBUTE_PACKED {43unsigned char secret[SSS_SECRET_SIZE];44} sss_secret;45typedef struct ATTRIBUTE_PACKED {46/* Index x of the share on two byts (a short) */47unsigned char index[2];48/* Value of the share */49unsigned char share[SSS_SECRET_SIZE];50} _sss_raw_share;5152#define SSS_SESSION_ID_SIZE 1653/* We use SHA-256 for HMAC, so the size is 32 bytes */54#define SSS_HMAC_SIZE 325556/* Security wrapper for the secret for "secured" SSS */57typedef struct ATTRIBUTE_PACKED {58_sss_raw_share raw_share;59/* 128 bits session id */60unsigned char session_id[SSS_SESSION_ID_SIZE];61unsigned char raw_share_hmac[SSS_HMAC_SIZE];62} sss_share;6364/* SSS shares and secret generation:65* Inputs:66* - n: is the number of shares to generate67* - k: the quorum of shares to regenerate the secret (of course k <= n)68* - secret: the secret value when input_secret is set to 'true'69* Output:70* - shares: a pointer to the generated n shares71* - secret: the secret value when input_secret is set to 'false', this72* value being randomly generated73*/74ATTRIBUTE_WARN_UNUSED_RET int sss_generate(sss_share *shares, unsigned short k, unsigned short n, sss_secret *secret, boolean input_secret);7576/* SSS shares and secret combination77* Inputs:78* - k: the quorum of shares to regenerate the secret79* - shares: a pointer to the k shares80* Output:81* - secret: the secret value computed from the k shares82*/83ATTRIBUTE_WARN_UNUSED_RET int sss_combine(const sss_share *shares, unsigned short k, sss_secret *secret);8485/* SSS shares regeneration from existing shares86* Inputs:87* - shares: a pointer to the input k shares allowing the regeneration88* - n: is the number of shares to regenerate89* - k: the input shares (of course k <= n)90* Output:91* - shares: a pointer to the generated n shares (among which the k first are92* the ones provided as inputs)93* - secret: the recomputed secret value94*/95ATTRIBUTE_WARN_UNUSED_RET int sss_regenerate(sss_share *shares, unsigned short k, unsigned short n, sss_secret *secret);9697#endif /* __SSS_H__ */9899100