#ifndef __RSA_H__
#define __RSA_H__
#include <libecc/lib_ecc_config.h>
#include "../../hash/hash.h"
typedef struct {
nn n;
nn e;
} rsa_pub_key;
typedef enum {
RSA_SIMPLE = 0,
RSA_SIMPLE_PQ = 1,
RSA_CRT = 2,
} rsa_priv_key_type;
typedef struct {
nn n;
nn d;
} rsa_priv_key_simple;
typedef struct {
nn n;
nn d;
nn p;
nn q;
} rsa_priv_key_simple_pq;
typedef struct {
nn r;
nn d;
nn t;
} rsa_priv_key_crt_coeffs;
#define MAX_CRT_COEFFS 5
typedef struct {
nn p;
nn q;
nn dP;
nn dQ;
nn qInv;
u8 u;
rsa_priv_key_crt_coeffs coeffs[MAX_CRT_COEFFS];
} rsa_priv_key_crt;
typedef struct {
rsa_priv_key_type type;
union {
rsa_priv_key_simple s;
rsa_priv_key_simple_pq s_pq;
rsa_priv_key_crt crt;
} key;
} rsa_priv_key;
ATTRIBUTE_WARN_UNUSED_RET int rsa_i2osp(nn_src_t x, u8 *buf, u32 buflen);
ATTRIBUTE_WARN_UNUSED_RET int rsa_os2ip(nn_t x, const u8 *buf, u32 buflen);
ATTRIBUTE_WARN_UNUSED_RET int rsa_import_pub_key(rsa_pub_key *pub, const u8 *n,
u16 nlen, const u8 *e, u16 elen);
ATTRIBUTE_WARN_UNUSED_RET int rsa_import_simple_priv_key(rsa_priv_key *priv,
const u8 *n, u16 nlen, const u8 *d,
u16 dlen, const u8 *p, u16 plen, const u8 *q, u16 qlen);
ATTRIBUTE_WARN_UNUSED_RET int rsa_import_crt_priv_key(rsa_priv_key *priv,
const u8 *p, u16 plen,
const u8 *q, u16 qlen,
const u8 *dP, u16 dPlen,
const u8 *dQ, u16 dQlen,
const u8 *qInv, u16 qInvlen,
const u8 **coeffs, u16 *coeffslens, u8 u);
ATTRIBUTE_WARN_UNUSED_RET int rsaep(const rsa_pub_key *pub, nn_src_t m, nn_t c);
ATTRIBUTE_WARN_UNUSED_RET int rsadp(const rsa_priv_key *priv, nn_src_t c, nn_t m);
ATTRIBUTE_WARN_UNUSED_RET int rsadp_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, nn_src_t c, nn_t m);
ATTRIBUTE_WARN_UNUSED_RET int rsasp1(const rsa_priv_key *priv, nn_src_t m, nn_t s);
ATTRIBUTE_WARN_UNUSED_RET int rsasp1_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, nn_src_t m, nn_t s);
ATTRIBUTE_WARN_UNUSED_RET int rsavp1(const rsa_pub_key *pub, nn_src_t s, nn_t m);
ATTRIBUTE_WARN_UNUSED_RET int emsa_pkcs1_v1_5_encode(const u8 *m, u32 mlen, u8 *em, u16 emlen,
gen_hash_alg_type rsa_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int emsa_pss_encode(const u8 *m, u32 mlen, u8 *em, u32 embits,
u16 *eminlen,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
u32 saltlen, const u8 *forced_salt);
ATTRIBUTE_WARN_UNUSED_RET int emsa_pss_verify(const u8 *m, u32 mlen, const u8 *em,
u32 embits, u16 emlen,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
u32 slen);
ATTRIBUTE_WARN_UNUSED_RET int rsaes_pkcs1_v1_5_encrypt(const rsa_pub_key *pub, const u8 *m, u32 mlen,
u8 *c, u32 *clen, u32 modbits,
const u8 *forced_seed, u32 seedlen);
ATTRIBUTE_WARN_UNUSED_RET int rsaes_pkcs1_v1_5_decrypt(const rsa_priv_key *priv, const u8 *c, u32 clen,
u8 *m, u32 *mlen, u32 modbits);
ATTRIBUTE_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,
u8 *m, u32 *mlen, u32 modbits);
ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_encrypt(const rsa_pub_key *pub, const u8 *m, u32 mlen,
u8 *c, u32 *clen, u32 modbits, const u8 *label, u32 label_len,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
const u8 *forced_seed, u32 seedlen);
ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_decrypt(const rsa_priv_key *priv, const u8 *c, u32 clen,
u8 *m, u32 *mlen, u32 modbits, const u8 *label, u32 label_len,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsaes_oaep_decrypt_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *c, u32 clen,
u8 *m, u32 *mlen, u32 modbits, const u8 *label, u32 label_len,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsassa_pkcs1_v1_5_sign(const rsa_priv_key *priv, const u8 *m, u32 mlen,
u8 *s, u16 *slen, u32 modbits, gen_hash_alg_type rsa_hash_type);
ATTRIBUTE_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,
u8 *s, u16 *slen, u32 modbits, gen_hash_alg_type rsa_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsassa_pkcs1_v1_5_verify(const rsa_pub_key *pub, const u8 *m, u32 mlen,
const u8 *s, u16 slen, u32 modbits, gen_hash_alg_type rsa_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_sign(const rsa_priv_key *priv, const u8 *m, u32 mlen,
u8 *s, u16 *slen, u32 modbits,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
u32 saltlen, const u8 *forced_salt);
ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_sign_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub, const u8 *m, u32 mlen,
u8 *s, u16 *slen, u32 modbits,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
u32 saltlen, const u8 *forced_salt);
ATTRIBUTE_WARN_UNUSED_RET int rsassa_pss_verify(const rsa_pub_key *pub, const u8 *m, u32 mlen,
const u8 *s, u16 slen, u32 modbits,
gen_hash_alg_type rsa_hash_type, gen_hash_alg_type mgf_hash_type,
u32 saltlen);
ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_sign_recover(const rsa_priv_key *priv, const u8 *m, u32 mlen, u32 *m1len,
u32 *m2len, u8 *s, u16 *slen,
u32 modbits, gen_hash_alg_type gen_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_sign_recover_hardened(const rsa_priv_key *priv, const rsa_pub_key *pub,
const u8 *m, u32 mlen, u32 *m1len, u32 *m2len, u8 *s, u16 *slen,
u32 modbits, gen_hash_alg_type gen_hash_type);
ATTRIBUTE_WARN_UNUSED_RET int rsa_iso9796_2_verify_recover(const rsa_pub_key *pub, const u8 *m2, u32 m2len, u8 *m1, u32 *m1len,
const u8 *s, u16 slen, u32 modbits, gen_hash_alg_type gen_hash_type);
#endif