1*e71b7053SJung-uk Kim /* 2*e71b7053SJung-uk Kim * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved. 3*e71b7053SJung-uk Kim * 4*e71b7053SJung-uk Kim * Licensed under the OpenSSL license (the "License"). You may not use 5*e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy 6*e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at 7*e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html 8*e71b7053SJung-uk Kim */ 9*e71b7053SJung-uk Kim 10*e71b7053SJung-uk Kim #include <string.h> 11*e71b7053SJung-uk Kim #include "rsa_locl.h" 12*e71b7053SJung-uk Kim #include <openssl/err.h> 13*e71b7053SJung-uk Kim 14*e71b7053SJung-uk Kim RSA_METHOD *RSA_meth_new(const char *name, int flags) 15*e71b7053SJung-uk Kim { 16*e71b7053SJung-uk Kim RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth)); 17*e71b7053SJung-uk Kim 18*e71b7053SJung-uk Kim if (meth != NULL) { 19*e71b7053SJung-uk Kim meth->flags = flags; 20*e71b7053SJung-uk Kim 21*e71b7053SJung-uk Kim meth->name = OPENSSL_strdup(name); 22*e71b7053SJung-uk Kim if (meth->name != NULL) 23*e71b7053SJung-uk Kim return meth; 24*e71b7053SJung-uk Kim 25*e71b7053SJung-uk Kim OPENSSL_free(meth); 26*e71b7053SJung-uk Kim } 27*e71b7053SJung-uk Kim 28*e71b7053SJung-uk Kim RSAerr(RSA_F_RSA_METH_NEW, ERR_R_MALLOC_FAILURE); 29*e71b7053SJung-uk Kim return NULL; 30*e71b7053SJung-uk Kim } 31*e71b7053SJung-uk Kim 32*e71b7053SJung-uk Kim void RSA_meth_free(RSA_METHOD *meth) 33*e71b7053SJung-uk Kim { 34*e71b7053SJung-uk Kim if (meth != NULL) { 35*e71b7053SJung-uk Kim OPENSSL_free(meth->name); 36*e71b7053SJung-uk Kim OPENSSL_free(meth); 37*e71b7053SJung-uk Kim } 38*e71b7053SJung-uk Kim } 39*e71b7053SJung-uk Kim 40*e71b7053SJung-uk Kim RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) 41*e71b7053SJung-uk Kim { 42*e71b7053SJung-uk Kim RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); 43*e71b7053SJung-uk Kim 44*e71b7053SJung-uk Kim if (ret != NULL) { 45*e71b7053SJung-uk Kim memcpy(ret, meth, sizeof(*meth)); 46*e71b7053SJung-uk Kim 47*e71b7053SJung-uk Kim ret->name = OPENSSL_strdup(meth->name); 48*e71b7053SJung-uk Kim if (ret->name != NULL) 49*e71b7053SJung-uk Kim return ret; 50*e71b7053SJung-uk Kim 51*e71b7053SJung-uk Kim OPENSSL_free(ret); 52*e71b7053SJung-uk Kim } 53*e71b7053SJung-uk Kim 54*e71b7053SJung-uk Kim RSAerr(RSA_F_RSA_METH_DUP, ERR_R_MALLOC_FAILURE); 55*e71b7053SJung-uk Kim return NULL; 56*e71b7053SJung-uk Kim } 57*e71b7053SJung-uk Kim 58*e71b7053SJung-uk Kim const char *RSA_meth_get0_name(const RSA_METHOD *meth) 59*e71b7053SJung-uk Kim { 60*e71b7053SJung-uk Kim return meth->name; 61*e71b7053SJung-uk Kim } 62*e71b7053SJung-uk Kim 63*e71b7053SJung-uk Kim int RSA_meth_set1_name(RSA_METHOD *meth, const char *name) 64*e71b7053SJung-uk Kim { 65*e71b7053SJung-uk Kim char *tmpname = OPENSSL_strdup(name); 66*e71b7053SJung-uk Kim 67*e71b7053SJung-uk Kim if (tmpname == NULL) { 68*e71b7053SJung-uk Kim RSAerr(RSA_F_RSA_METH_SET1_NAME, ERR_R_MALLOC_FAILURE); 69*e71b7053SJung-uk Kim return 0; 70*e71b7053SJung-uk Kim } 71*e71b7053SJung-uk Kim 72*e71b7053SJung-uk Kim OPENSSL_free(meth->name); 73*e71b7053SJung-uk Kim meth->name = tmpname; 74*e71b7053SJung-uk Kim 75*e71b7053SJung-uk Kim return 1; 76*e71b7053SJung-uk Kim } 77*e71b7053SJung-uk Kim 78*e71b7053SJung-uk Kim int RSA_meth_get_flags(const RSA_METHOD *meth) 79*e71b7053SJung-uk Kim { 80*e71b7053SJung-uk Kim return meth->flags; 81*e71b7053SJung-uk Kim } 82*e71b7053SJung-uk Kim 83*e71b7053SJung-uk Kim int RSA_meth_set_flags(RSA_METHOD *meth, int flags) 84*e71b7053SJung-uk Kim { 85*e71b7053SJung-uk Kim meth->flags = flags; 86*e71b7053SJung-uk Kim return 1; 87*e71b7053SJung-uk Kim } 88*e71b7053SJung-uk Kim 89*e71b7053SJung-uk Kim void *RSA_meth_get0_app_data(const RSA_METHOD *meth) 90*e71b7053SJung-uk Kim { 91*e71b7053SJung-uk Kim return meth->app_data; 92*e71b7053SJung-uk Kim } 93*e71b7053SJung-uk Kim 94*e71b7053SJung-uk Kim int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data) 95*e71b7053SJung-uk Kim { 96*e71b7053SJung-uk Kim meth->app_data = app_data; 97*e71b7053SJung-uk Kim return 1; 98*e71b7053SJung-uk Kim } 99*e71b7053SJung-uk Kim 100*e71b7053SJung-uk Kim int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth)) 101*e71b7053SJung-uk Kim (int flen, const unsigned char *from, 102*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, int padding) 103*e71b7053SJung-uk Kim { 104*e71b7053SJung-uk Kim return meth->rsa_pub_enc; 105*e71b7053SJung-uk Kim } 106*e71b7053SJung-uk Kim 107*e71b7053SJung-uk Kim int RSA_meth_set_pub_enc(RSA_METHOD *meth, 108*e71b7053SJung-uk Kim int (*pub_enc) (int flen, const unsigned char *from, 109*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, 110*e71b7053SJung-uk Kim int padding)) 111*e71b7053SJung-uk Kim { 112*e71b7053SJung-uk Kim meth->rsa_pub_enc = pub_enc; 113*e71b7053SJung-uk Kim return 1; 114*e71b7053SJung-uk Kim } 115*e71b7053SJung-uk Kim 116*e71b7053SJung-uk Kim int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth)) 117*e71b7053SJung-uk Kim (int flen, const unsigned char *from, 118*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, int padding) 119*e71b7053SJung-uk Kim { 120*e71b7053SJung-uk Kim return meth->rsa_pub_dec; 121*e71b7053SJung-uk Kim } 122*e71b7053SJung-uk Kim 123*e71b7053SJung-uk Kim int RSA_meth_set_pub_dec(RSA_METHOD *meth, 124*e71b7053SJung-uk Kim int (*pub_dec) (int flen, const unsigned char *from, 125*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, 126*e71b7053SJung-uk Kim int padding)) 127*e71b7053SJung-uk Kim { 128*e71b7053SJung-uk Kim meth->rsa_pub_dec = pub_dec; 129*e71b7053SJung-uk Kim return 1; 130*e71b7053SJung-uk Kim } 131*e71b7053SJung-uk Kim 132*e71b7053SJung-uk Kim int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth)) 133*e71b7053SJung-uk Kim (int flen, const unsigned char *from, 134*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, int padding) 135*e71b7053SJung-uk Kim { 136*e71b7053SJung-uk Kim return meth->rsa_priv_enc; 137*e71b7053SJung-uk Kim } 138*e71b7053SJung-uk Kim 139*e71b7053SJung-uk Kim int RSA_meth_set_priv_enc(RSA_METHOD *meth, 140*e71b7053SJung-uk Kim int (*priv_enc) (int flen, const unsigned char *from, 141*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, 142*e71b7053SJung-uk Kim int padding)) 143*e71b7053SJung-uk Kim { 144*e71b7053SJung-uk Kim meth->rsa_priv_enc = priv_enc; 145*e71b7053SJung-uk Kim return 1; 146*e71b7053SJung-uk Kim } 147*e71b7053SJung-uk Kim 148*e71b7053SJung-uk Kim int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth)) 149*e71b7053SJung-uk Kim (int flen, const unsigned char *from, 150*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, int padding) 151*e71b7053SJung-uk Kim { 152*e71b7053SJung-uk Kim return meth->rsa_priv_dec; 153*e71b7053SJung-uk Kim } 154*e71b7053SJung-uk Kim 155*e71b7053SJung-uk Kim int RSA_meth_set_priv_dec(RSA_METHOD *meth, 156*e71b7053SJung-uk Kim int (*priv_dec) (int flen, const unsigned char *from, 157*e71b7053SJung-uk Kim unsigned char *to, RSA *rsa, 158*e71b7053SJung-uk Kim int padding)) 159*e71b7053SJung-uk Kim { 160*e71b7053SJung-uk Kim meth->rsa_priv_dec = priv_dec; 161*e71b7053SJung-uk Kim return 1; 162*e71b7053SJung-uk Kim } 163*e71b7053SJung-uk Kim 164*e71b7053SJung-uk Kim /* Can be null */ 165*e71b7053SJung-uk Kim int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth)) 166*e71b7053SJung-uk Kim (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) 167*e71b7053SJung-uk Kim { 168*e71b7053SJung-uk Kim return meth->rsa_mod_exp; 169*e71b7053SJung-uk Kim } 170*e71b7053SJung-uk Kim 171*e71b7053SJung-uk Kim int RSA_meth_set_mod_exp(RSA_METHOD *meth, 172*e71b7053SJung-uk Kim int (*mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, 173*e71b7053SJung-uk Kim BN_CTX *ctx)) 174*e71b7053SJung-uk Kim { 175*e71b7053SJung-uk Kim meth->rsa_mod_exp = mod_exp; 176*e71b7053SJung-uk Kim return 1; 177*e71b7053SJung-uk Kim } 178*e71b7053SJung-uk Kim 179*e71b7053SJung-uk Kim /* Can be null */ 180*e71b7053SJung-uk Kim int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth)) 181*e71b7053SJung-uk Kim (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 182*e71b7053SJung-uk Kim const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 183*e71b7053SJung-uk Kim { 184*e71b7053SJung-uk Kim return meth->bn_mod_exp; 185*e71b7053SJung-uk Kim } 186*e71b7053SJung-uk Kim 187*e71b7053SJung-uk Kim int RSA_meth_set_bn_mod_exp(RSA_METHOD *meth, 188*e71b7053SJung-uk Kim int (*bn_mod_exp) (BIGNUM *r, 189*e71b7053SJung-uk Kim const BIGNUM *a, 190*e71b7053SJung-uk Kim const BIGNUM *p, 191*e71b7053SJung-uk Kim const BIGNUM *m, 192*e71b7053SJung-uk Kim BN_CTX *ctx, 193*e71b7053SJung-uk Kim BN_MONT_CTX *m_ctx)) 194*e71b7053SJung-uk Kim { 195*e71b7053SJung-uk Kim meth->bn_mod_exp = bn_mod_exp; 196*e71b7053SJung-uk Kim return 1; 197*e71b7053SJung-uk Kim } 198*e71b7053SJung-uk Kim 199*e71b7053SJung-uk Kim /* called at new */ 200*e71b7053SJung-uk Kim int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa) 201*e71b7053SJung-uk Kim { 202*e71b7053SJung-uk Kim return meth->init; 203*e71b7053SJung-uk Kim } 204*e71b7053SJung-uk Kim 205*e71b7053SJung-uk Kim int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa)) 206*e71b7053SJung-uk Kim { 207*e71b7053SJung-uk Kim meth->init = init; 208*e71b7053SJung-uk Kim return 1; 209*e71b7053SJung-uk Kim } 210*e71b7053SJung-uk Kim 211*e71b7053SJung-uk Kim /* called at free */ 212*e71b7053SJung-uk Kim int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa) 213*e71b7053SJung-uk Kim { 214*e71b7053SJung-uk Kim return meth->finish; 215*e71b7053SJung-uk Kim } 216*e71b7053SJung-uk Kim 217*e71b7053SJung-uk Kim int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa)) 218*e71b7053SJung-uk Kim { 219*e71b7053SJung-uk Kim meth->finish = finish; 220*e71b7053SJung-uk Kim return 1; 221*e71b7053SJung-uk Kim } 222*e71b7053SJung-uk Kim 223*e71b7053SJung-uk Kim int (*RSA_meth_get_sign(const RSA_METHOD *meth)) 224*e71b7053SJung-uk Kim (int type, 225*e71b7053SJung-uk Kim const unsigned char *m, unsigned int m_length, 226*e71b7053SJung-uk Kim unsigned char *sigret, unsigned int *siglen, 227*e71b7053SJung-uk Kim const RSA *rsa) 228*e71b7053SJung-uk Kim { 229*e71b7053SJung-uk Kim return meth->rsa_sign; 230*e71b7053SJung-uk Kim } 231*e71b7053SJung-uk Kim 232*e71b7053SJung-uk Kim int RSA_meth_set_sign(RSA_METHOD *meth, 233*e71b7053SJung-uk Kim int (*sign) (int type, const unsigned char *m, 234*e71b7053SJung-uk Kim unsigned int m_length, 235*e71b7053SJung-uk Kim unsigned char *sigret, unsigned int *siglen, 236*e71b7053SJung-uk Kim const RSA *rsa)) 237*e71b7053SJung-uk Kim { 238*e71b7053SJung-uk Kim meth->rsa_sign = sign; 239*e71b7053SJung-uk Kim return 1; 240*e71b7053SJung-uk Kim } 241*e71b7053SJung-uk Kim 242*e71b7053SJung-uk Kim int (*RSA_meth_get_verify(const RSA_METHOD *meth)) 243*e71b7053SJung-uk Kim (int dtype, const unsigned char *m, 244*e71b7053SJung-uk Kim unsigned int m_length, const unsigned char *sigbuf, 245*e71b7053SJung-uk Kim unsigned int siglen, const RSA *rsa) 246*e71b7053SJung-uk Kim { 247*e71b7053SJung-uk Kim return meth->rsa_verify; 248*e71b7053SJung-uk Kim } 249*e71b7053SJung-uk Kim 250*e71b7053SJung-uk Kim int RSA_meth_set_verify(RSA_METHOD *meth, 251*e71b7053SJung-uk Kim int (*verify) (int dtype, const unsigned char *m, 252*e71b7053SJung-uk Kim unsigned int m_length, 253*e71b7053SJung-uk Kim const unsigned char *sigbuf, 254*e71b7053SJung-uk Kim unsigned int siglen, const RSA *rsa)) 255*e71b7053SJung-uk Kim { 256*e71b7053SJung-uk Kim meth->rsa_verify = verify; 257*e71b7053SJung-uk Kim return 1; 258*e71b7053SJung-uk Kim } 259*e71b7053SJung-uk Kim 260*e71b7053SJung-uk Kim int (*RSA_meth_get_keygen(const RSA_METHOD *meth)) 261*e71b7053SJung-uk Kim (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb) 262*e71b7053SJung-uk Kim { 263*e71b7053SJung-uk Kim return meth->rsa_keygen; 264*e71b7053SJung-uk Kim } 265*e71b7053SJung-uk Kim 266*e71b7053SJung-uk Kim int RSA_meth_set_keygen(RSA_METHOD *meth, 267*e71b7053SJung-uk Kim int (*keygen) (RSA *rsa, int bits, BIGNUM *e, 268*e71b7053SJung-uk Kim BN_GENCB *cb)) 269*e71b7053SJung-uk Kim { 270*e71b7053SJung-uk Kim meth->rsa_keygen = keygen; 271*e71b7053SJung-uk Kim return 1; 272*e71b7053SJung-uk Kim } 273*e71b7053SJung-uk Kim 274*e71b7053SJung-uk Kim int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth)) 275*e71b7053SJung-uk Kim (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb) 276*e71b7053SJung-uk Kim { 277*e71b7053SJung-uk Kim return meth->rsa_multi_prime_keygen; 278*e71b7053SJung-uk Kim } 279*e71b7053SJung-uk Kim 280*e71b7053SJung-uk Kim int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth, 281*e71b7053SJung-uk Kim int (*keygen) (RSA *rsa, int bits, 282*e71b7053SJung-uk Kim int primes, BIGNUM *e, 283*e71b7053SJung-uk Kim BN_GENCB *cb)) 284*e71b7053SJung-uk Kim { 285*e71b7053SJung-uk Kim meth->rsa_multi_prime_keygen = keygen; 286*e71b7053SJung-uk Kim return 1; 287*e71b7053SJung-uk Kim } 288