Path: blob/main/crypto/libecc/src/examples/sig/rsa/rsa.h
34927 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 __RSA_H__11#define __RSA_H__1213/*14* NOTE: although we only need libarith for RSA as we15* manipulate a ring of integers, we include libsig for16* the hash algorithms.17*/18#include <libecc/lib_ecc_config.h>1920/* The hash algorithms wrapper */21#include "../../hash/hash.h"2223/* We define hereafter the types and functions for RSA.24* The notations are taken from RFC 8017 and should be compliant25* with it.26*/2728/* RSA public key, composed of:29* n the RSA modulus, a positive integer30* e the RSA public exponent, a positive integer31*/32typedef struct {33nn n;34nn e;35} rsa_pub_key;3637/* RSA private key, composed of:38* n the RSA modulus, a positive integer39* d the RSA private exponent, a positive integer40* p (OPTIONAL) the first factor, a positive integer41* q (OPTIONAL) the secod factor, a positive integer42*43* OR when using CRT:44* p the first factor, a positive integer45* q the second factor, a positive integer46* dP the first factor's CRT exponent, a positive integer47* dQ the second factor's CRT exponent, a positive integer48* qInv the (first) CRT coefficient, a positive integer49* r_i the i-th factor, a positive integer50* d_i the i-th factor's CRT exponent, a positive integer51* t_i the i-th factor's CRT coefficient, a positive integer52* u is the number of (r_i, d_i, t_i) triplets.53*/54typedef enum {55RSA_SIMPLE = 0,56RSA_SIMPLE_PQ = 1,57RSA_CRT = 2,58} rsa_priv_key_type;5960/*** RSA "simple" private key ***/61typedef struct {62nn n;63nn d;64} rsa_priv_key_simple;6566/*** RSA "simple" private key with optional p and q ***/67typedef struct {68nn n;69nn d;70nn p;71nn q;72} rsa_priv_key_simple_pq;7374/*** RSA CRT private key *******/75typedef struct {76nn r;77nn d;78nn t;79} rsa_priv_key_crt_coeffs;8081/* A maximum of 5 triplets are allowed in our implementation */82#define MAX_CRT_COEFFS 583typedef struct {84nn p;85nn q;86nn dP;87nn dQ;88nn qInv;89/* u is the number of additional CRT (r, d, t) triplets */90u8 u;91rsa_priv_key_crt_coeffs coeffs[MAX_CRT_COEFFS];92} rsa_priv_key_crt;9394typedef struct {95rsa_priv_key_type type;96union {97rsa_priv_key_simple s;98rsa_priv_key_simple_pq s_pq;99rsa_priv_key_crt crt;100} key;101} rsa_priv_key;102103ATTRIBUTE_WARN_UNUSED_RET int rsa_i2osp(nn_src_t x, u8 *buf, u32 buflen);104ATTRIBUTE_WARN_UNUSED_RET int rsa_os2ip(nn_t x, const u8 *buf, u32 buflen);105106ATTRIBUTE_WARN_UNUSED_RET int rsa_import_pub_key(rsa_pub_key *pub, const u8 *n,107u16 nlen, const u8 *e, u16 elen);108ATTRIBUTE_WARN_UNUSED_RET int rsa_import_simple_priv_key(rsa_priv_key *priv,109const u8 *n, u16 nlen, const u8 *d,110u16 dlen, const u8 *p, u16 plen, const u8 *q, u16 qlen);111ATTRIBUTE_WARN_UNUSED_RET int rsa_import_crt_priv_key(rsa_priv_key *priv,112const u8 *p, u16 plen,113const u8 *q, u16 qlen,114const u8 *dP, u16 dPlen,115const u8 *dQ, u16 dQlen,116const u8 *qInv, u16 qInvlen,117const u8 **coeffs, u16 *coeffslens, u8 u);118119ATTRIBUTE_WARN_UNUSED_RET int rsaep(const rsa_pub_key *pub, nn_src_t m, nn_t c);120ATTRIBUTE_WARN_UNUSED_RET int rsadp(const rsa_priv_key *priv, nn_src_t c, nn_t m);121ATTRIBUTE_WARN_UNUSED_RET int rsadp_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, nn_src_t c, nn_t m);122123ATTRIBUTE_WARN_UNUSED_RET int rsasp1(const rsa_priv_key *priv, nn_src_t m, nn_t s);124ATTRIBUTE_WARN_UNUSED_RET int rsasp1_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, nn_src_t m, nn_t s);125ATTRIBUTE_WARN_UNUSED_RET int rsavp1(const rsa_pub_key *pub, nn_src_t s, nn_t m);126127ATTRIBUTE_WARN_UNUSED_RET int emsa_pkcs1_v1_5_encode(const u8 *m, u32 mlen, u8 *em, u16 emlen,128gen_hash_alg_type rsa_hash_type);129ATTRIBUTE_WARN_UNUSED_RET int emsa_pss_encode(const u8 *m, u32 mlen, u8 *em, u32 embits,130u16 *eminlen,131gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,132u32 saltlen, const u8 *forced_salt);133ATTRIBUTE_WARN_UNUSED_RET int emsa_pss_verify(const u8 *m, u32 mlen, const u8 *em,134u32 embits, u16 emlen,135gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,136u32 slen);137138ATTRIBUTE_WARN_UNUSED_RET int rsaes_pkcs1_v1_5_encrypt(const rsa_pub_key *pub, const u8 *m, u32 mlen,139u8 *c, u32 *clen, u32 modbits,140const u8 *forced_seed, u32 seedlen);141ATTRIBUTE_WARN_UNUSED_RET int rsaes_pkcs1_v1_5_decrypt(const rsa_priv_key *priv, const u8 *c, u32 clen,142u8 *m, u32 *mlen, u32 modbits);143ATTRIBUTE_WARN_UNUSED_RET int rsaes_pkcs1_v1_5_decrypt_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *c, u32 clen,144u8 *m, u32 *mlen, u32 modbits);145146ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_encrypt(const rsa_pub_key *pub, const u8 *m, u32 mlen,147u8 *c, u32 *clen, u32 modbits, const u8 *label, u32 label_len,148gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,149const u8 *forced_seed, u32 seedlen);150ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_decrypt(const rsa_priv_key *priv, const u8 *c, u32 clen,151u8 *m, u32 *mlen, u32 modbits, const u8 *label, u32 label_len,152gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type);153ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_decrypt_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *c, u32 clen,154u8 *m, u32 *mlen, u32 modbits, const u8 *label, u32 label_len,155gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type);156157ATTRIBUTE_WARN_UNUSED_RET int rsassa_pkcs1_v1_5_sign(const rsa_priv_key *priv, const u8 *m, u32 mlen,158u8 *s, u16 *slen, u32 modbits, gen_hash_alg_type rsa_hash_type);159ATTRIBUTE_WARN_UNUSED_RET int rsassa_pkcs1_v1_5_sign_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *m, u32 mlen,160u8 *s, u16 *slen, u32 modbits, gen_hash_alg_type rsa_hash_type);161ATTRIBUTE_WARN_UNUSED_RET int rsassa_pkcs1_v1_5_verify(const rsa_pub_key *pub, const u8 *m, u32 mlen,162const u8 *s, u16 slen, u32 modbits, gen_hash_alg_type rsa_hash_type);163164ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_sign(const rsa_priv_key *priv, const u8 *m, u32 mlen,165u8 *s, u16 *slen, u32 modbits,166gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,167u32 saltlen, const u8 *forced_salt);168ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_sign_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *m, u32 mlen,169u8 *s, u16 *slen, u32 modbits,170gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,171u32 saltlen, const u8 *forced_salt);172ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_verify(const rsa_pub_key *pub, const u8 *m, u32 mlen,173const u8 *s, u16 slen, u32 modbits,174gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,175u32 saltlen);176177ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_sign_recover(const rsa_priv_key *priv, const u8 *m, u32 mlen, u32 *m1len,178u32 *m2len, u8 *s, u16 *slen,179u32 modbits, gen_hash_alg_type gen_hash_type);180181ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_sign_recover_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub,182const u8 *m, u32 mlen, u32 *m1len, u32 *m2len, u8 *s, u16 *slen,183u32 modbits, gen_hash_alg_type gen_hash_type);184ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_verify_recover(const rsa_pub_key *pub, const u8 *m2, u32 m2len, u8 *m1, u32 *m1len,185const u8 *s, u16 slen, u32 modbits, gen_hash_alg_type gen_hash_type);186#endif /* __RSA_H__ */187188189