repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
hwcrhk_mod_exp_dh
static int hwcrhk_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return hwcrhk_mod_exp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the dh and mont dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_chil.c#L1095-L1100
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
hwcrhk_rand_bytes
static int hwcrhk_rand_bytes(unsigned char *buf, int num) { char tempbuf[1024]; HWCryptoHook_ErrMsgBuf rmsg; int to_return = 0; /* assume failure */ int ret; rmsg.buf = tempbuf; rmsg.size = sizeof(tempbuf); if(!hwcrhk_context) { HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES,HWCRHK_R_NOT_INITIALISED); goto err; } ret = p_hwcrhk_RandomBytes(hwcrhk_context, buf, num, &rmsg); if (ret < 0) { /* FIXME: When this error is returned, HWCryptoHook is telling us that falling back to software computation might be a good thing. */ if(ret == HWCRYPTOHOOK_ERROR_FALLBACK) { HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES, HWCRHK_R_REQUEST_FALLBACK); } else { HWCRHKerr(HWCRHK_F_HWCRHK_RAND_BYTES, HWCRHK_R_REQUEST_FAILED); } ERR_add_error_data(1,rmsg.buf); goto err; } to_return = 1; err: return to_return; }
/* Random bytes are good */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_chil.c#L1104-L1142
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
hwcrhk_mutex_init
static int hwcrhk_mutex_init(HWCryptoHook_Mutex* mt, HWCryptoHook_CallerContext *cactx) { mt->lockid = CRYPTO_get_new_dynlockid(); if (mt->lockid == 0) return 1; /* failure */ return 0; /* success */ }
/* Mutex calls: since the HWCryptoHook model closely follows the POSIX model * these just wrap the POSIX functions and add some logging. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_chil.c#L1153-L1160
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bind_helper
static int bind_helper(ENGINE *e) { #ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1; #endif #ifndef OPENSSL_NO_DH const DH_METHOD *meth2; #endif if(!ENGINE_set_id(e, engine_cswift_id) || !ENGINE_set_name(e, engine_cswift_name) || #ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &cswift_rsa) || #endif #ifndef OPENSSL_NO_DSA !ENGINE_set_DSA(e, &cswift_dsa) || #endif #ifndef OPENSSL_NO_DH !ENGINE_set_DH(e, &cswift_dh) || #endif !ENGINE_set_RAND(e, &cswift_random) || !ENGINE_set_destroy_function(e, cswift_destroy) || !ENGINE_set_init_function(e, cswift_init) || !ENGINE_set_finish_function(e, cswift_finish) || !ENGINE_set_ctrl_function(e, cswift_ctrl) || !ENGINE_set_cmd_defns(e, cswift_cmd_defns)) return 0; #ifndef OPENSSL_NO_RSA /* We know that the "PKCS1_SSLeay()" functions hook properly * to the cswift-specific mod_exp and mod_exp_crt so we use * those functions. NB: We don't use ENGINE_openssl() or * anything "more generic" because something like the RSAref * code may not hook properly, and if you own one of these * cards then you have the right to do RSA operations on it * anyway! */ meth1 = RSA_PKCS1_SSLeay(); cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc; cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec; cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc; cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec; #endif #ifndef OPENSSL_NO_DH /* Much the same for Diffie-Hellman */ meth2 = DH_OpenSSL(); cswift_dh.generate_key = meth2->generate_key; cswift_dh.compute_key = meth2->compute_key; #endif /* Ensure the cswift error handling is set up */ ERR_load_CSWIFT_strings(); return 1; }
/* This internal function is used by ENGINE_cswift() and possibly by the * "dynamic" ENGINE support too */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L231-L283
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_context
static int get_context(SW_CONTEXT_HANDLE *hac) { SW_STATUS status; status = p_CSwift_AcquireAccContext(hac); if(status != SW_OK) return 0; return 1; }
/* CryptoSwift library functions and mechanics - these are used by the * higher-level functions further down. NB: As and where there's no * error checking, take a look lower down where these functions are * called, the checking and error handling is probably down there. */ /* utility function to obtain a context */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L355-L363
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
release_context
static void release_context(SW_CONTEXT_HANDLE hac) { p_CSwift_ReleaseAccContext(hac); }
/* similarly to release one. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L366-L369
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_destroy
static int cswift_destroy(ENGINE *e) { free_CSWIFT_LIBNAME(); ERR_unload_CSWIFT_strings(); return 1; }
/* Destructor (complements the "ENGINE_cswift()" constructor) */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L372-L377
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_init
static int cswift_init(ENGINE *e) { SW_CONTEXT_HANDLE hac; t_swAcquireAccContext *p1; t_swAttachKeyParam *p2; t_swSimpleRequest *p3; t_swReleaseAccContext *p4; if(cswift_dso != NULL) { CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED); goto err; } /* Attempt to load libswift.so/swift.dll/whatever. */ cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0); if(cswift_dso == NULL) { CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED); goto err; } if(!(p1 = (t_swAcquireAccContext *) DSO_bind_func(cswift_dso, CSWIFT_F1)) || !(p2 = (t_swAttachKeyParam *) DSO_bind_func(cswift_dso, CSWIFT_F2)) || !(p3 = (t_swSimpleRequest *) DSO_bind_func(cswift_dso, CSWIFT_F3)) || !(p4 = (t_swReleaseAccContext *) DSO_bind_func(cswift_dso, CSWIFT_F4))) { CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED); goto err; } /* Copy the pointers */ p_CSwift_AcquireAccContext = p1; p_CSwift_AttachKeyParam = p2; p_CSwift_SimpleRequest = p3; p_CSwift_ReleaseAccContext = p4; /* Try and get a context - if not, we may have a DSO but no * accelerator! */ if(!get_context(&hac)) { CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE); goto err; } release_context(hac); /* Everything's fine. */ return 1; err: if(cswift_dso) { DSO_free(cswift_dso); cswift_dso = NULL; } p_CSwift_AcquireAccContext = NULL; p_CSwift_AttachKeyParam = NULL; p_CSwift_SimpleRequest = NULL; p_CSwift_ReleaseAccContext = NULL; return 0; }
/* (de)initialisation functions. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L380-L438
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_mod_exp
static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx) { /* I need somewhere to store temporary serialised values for * use with the CryptoSwift API calls. A neat cheat - I'll use * BIGNUMs from the BN_CTX but access their arrays directly as * byte arrays <grin>. This way I don't have to clean anything * up. */ BIGNUM *modulus; BIGNUM *exponent; BIGNUM *argument; BIGNUM *result; SW_STATUS sw_status; SW_LARGENUMBER arg, res; SW_PARAM sw_param; SW_CONTEXT_HANDLE hac; int to_return, acquired; modulus = exponent = argument = result = NULL; to_return = 0; /* expect failure */ acquired = 0; if(!get_context(&hac)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE); goto err; } acquired = 1; /* Prepare the params */ BN_CTX_start(ctx); modulus = BN_CTX_get(ctx); exponent = BN_CTX_get(ctx); argument = BN_CTX_get(ctx); result = BN_CTX_get(ctx); if(!result) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL); goto err; } if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) || !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL); goto err; } sw_param.type = SW_ALG_EXP; sw_param.up.exp.modulus.nbytes = BN_bn2bin(m, (unsigned char *)modulus->d); sw_param.up.exp.modulus.value = (unsigned char *)modulus->d; sw_param.up.exp.exponent.nbytes = BN_bn2bin(p, (unsigned char *)exponent->d); sw_param.up.exp.exponent.value = (unsigned char *)exponent->d; /* Attach the key params */ sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); switch(sw_status) { case SW_OK: break; case SW_ERR_INPUT_SIZE: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE); goto err; default: { char tmpbuf[DECIMAL_SIZE(sw_status)+1]; CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", sw_status); ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); } goto err; } /* Prepare the argument and response */ arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); arg.value = (unsigned char *)argument->d; res.nbytes = BN_num_bytes(m); memset(result->d, 0, res.nbytes); res.value = (unsigned char *)result->d; /* Perform the operation */ if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1, &res, 1)) != SW_OK) { char tmpbuf[DECIMAL_SIZE(sw_status)+1]; CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", sw_status); ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); goto err; } /* Convert the response */ BN_bin2bn((unsigned char *)result->d, res.nbytes, r); to_return = 1; err: if(acquired) release_context(hac); BN_CTX_end(ctx); return to_return; }
/* Un petit mod_exp */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L486-L580
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_mod_exp_crt
static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx) { SW_STATUS sw_status; SW_LARGENUMBER arg, res; SW_PARAM sw_param; SW_CONTEXT_HANDLE hac; BIGNUM *result = NULL; BIGNUM *argument = NULL; int to_return = 0; /* expect failure */ int acquired = 0; sw_param.up.crt.p.value = NULL; sw_param.up.crt.q.value = NULL; sw_param.up.crt.dmp1.value = NULL; sw_param.up.crt.dmq1.value = NULL; sw_param.up.crt.iqmp.value = NULL; if(!get_context(&hac)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE); goto err; } acquired = 1; /* Prepare the params */ argument = BN_new(); result = BN_new(); if(!result || !argument) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL); goto err; } sw_param.type = SW_ALG_CRT; /************************************************************************/ /* 04/02/2003 */ /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ /* limitation of cswift with values not a multiple of 32 */ /************************************************************************/ if(!cswift_bn_32copy(&sw_param.up.crt.p, p)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } if(!cswift_bn_32copy(&sw_param.up.crt.q, q)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } if( !bn_wexpand(argument, a->top) || !bn_wexpand(result, p->top + q->top)) { CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); goto err; } /* Attach the key params */ sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); switch(sw_status) { case SW_OK: break; case SW_ERR_INPUT_SIZE: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE); goto err; default: { char tmpbuf[DECIMAL_SIZE(sw_status)+1]; CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", sw_status); ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); } goto err; } /* Prepare the argument and response */ arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); arg.value = (unsigned char *)argument->d; res.nbytes = 2 * BN_num_bytes(p); memset(result->d, 0, res.nbytes); res.value = (unsigned char *)result->d; /* Perform the operation */ if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1, &res, 1)) != SW_OK) { char tmpbuf[DECIMAL_SIZE(sw_status)+1]; CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", sw_status); ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); goto err; } /* Convert the response */ BN_bin2bn((unsigned char *)result->d, res.nbytes, r); to_return = 1; err: if(sw_param.up.crt.p.value) OPENSSL_free(sw_param.up.crt.p.value); if(sw_param.up.crt.q.value) OPENSSL_free(sw_param.up.crt.q.value); if(sw_param.up.crt.dmp1.value) OPENSSL_free(sw_param.up.crt.dmp1.value); if(sw_param.up.crt.dmq1.value) OPENSSL_free(sw_param.up.crt.dmq1.value); if(sw_param.up.crt.iqmp.value) OPENSSL_free(sw_param.up.crt.iqmp.value); if(result) BN_free(result); if(argument) BN_free(argument); if(acquired) release_context(hac); return to_return; }
/* Un petit mod_exp chinois */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L609-L738
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_mod_exp_mont
static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { const RSA_METHOD * def_rsa_method; /* Try the limits of RSA (2048 bits) */ if(BN_num_bytes(r) > 256 || BN_num_bytes(a) > 256 || BN_num_bytes(m) > 256) { #ifdef RSA_NULL def_rsa_method=RSA_null_method(); #else #if 0 def_rsa_method=RSA_PKCS1_RSAref(); #else def_rsa_method=RSA_PKCS1_SSLeay(); #endif #endif if(def_rsa_method) return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx); } return cswift_mod_exp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the mont stuff dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L780-L804
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_mod_exp_dh
static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return cswift_mod_exp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the dh and mont dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L1032-L1037
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cswift_rand_bytes
static int cswift_rand_bytes(unsigned char *buf, int num) { SW_CONTEXT_HANDLE hac; SW_STATUS swrc; SW_LARGENUMBER largenum; int acquired = 0; int to_return = 0; /* assume failure */ unsigned char buf32[1024]; if (!get_context(&hac)) { CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE); goto err; } acquired = 1; /************************************************************************/ /* 04/02/2003 */ /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ /* limitation of cswift with values not a multiple of 32 */ /************************************************************************/ while(num >= (int)sizeof(buf32)) { largenum.value = buf; largenum.nbytes = sizeof(buf32); /* tell CryptoSwift how many bytes we want and where we want it. * Note: - CryptoSwift cannot do more than 4096 bytes at a time. * - CryptoSwift can only do multiple of 32-bits. */ swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); if (swrc != SW_OK) { char tmpbuf[20]; CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", swrc); ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); goto err; } buf += sizeof(buf32); num -= sizeof(buf32); } if(num) { largenum.nbytes = sizeof(buf32); largenum.value = buf32; swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); if (swrc != SW_OK) { char tmpbuf[20]; CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); sprintf(tmpbuf, "%ld", swrc); ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); goto err; } memcpy(buf, largenum.value, num); } to_return = 1; /* success */ err: if (acquired) release_context(hac); return to_return; }
/* Random bytes are good */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_cswift.c#L1041-L1105
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bind_helper
static int bind_helper(ENGINE *e) { #ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1; #endif if(!ENGINE_set_id(e, engine_e_gmp_id) || !ENGINE_set_name(e, engine_e_gmp_name) || #ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &e_gmp_rsa) || #endif !ENGINE_set_destroy_function(e, e_gmp_destroy) || !ENGINE_set_init_function(e, e_gmp_init) || !ENGINE_set_finish_function(e, e_gmp_finish) || !ENGINE_set_ctrl_function(e, e_gmp_ctrl) || !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns)) return 0; #ifndef OPENSSL_NO_RSA meth1 = RSA_PKCS1_SSLeay(); e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc; e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec; e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc; e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec; e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp; #endif /* Ensure the e_gmp error handling is set up */ ERR_load_GMP_strings(); return 1; }
/* This internal function is used by ENGINE_gmp() and possibly by the * "dynamic" ENGINE support too */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_gmp.c#L153-L182
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
e_gmp_init
static int e_gmp_init(ENGINE *e) { #ifndef OPENSSL_NO_RSA if (hndidx_rsa == -1) hndidx_rsa = RSA_get_ex_new_index(0, "GMP-based RSA key handle", NULL, NULL, NULL); #endif if (hndidx_rsa == -1) return 0; return 1; }
/* (de)initialisation functions. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_gmp.c#L219-L230
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bn2gmp
static int bn2gmp(const BIGNUM *bn, mpz_t g) { bn_check_top(bn); if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) && (BN_BITS2 == GMP_NUMB_BITS)) { /* The common case */ if(!_mpz_realloc (g, bn->top)) return 0; memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0])); g->_mp_size = bn->top; if(bn->neg) g->_mp_size = -g->_mp_size; return 1; } else { int toret; char *tmpchar = BN_bn2hex(bn); if(!tmpchar) return 0; toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0); OPENSSL_free(tmpchar); return toret; } }
/* Most often limb sizes will be the same. If not, we use hex conversion * which is neat, but extremely inefficient. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_gmp.c#L261-L285
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
nuron_dsa_mod_exp
static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) { BIGNUM t; int to_return = 0; BN_init(&t); /* let rr = a1 ^ p1 mod m */ if (!nuron_mod_exp(rr,a1,p1,m,ctx)) goto end; /* let t = a2 ^ p2 mod m */ if (!nuron_mod_exp(&t,a2,p2,m,ctx)) goto end; /* let rr = rr * t mod m */ if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end; to_return = 1; end: BN_free(&t); return to_return; }
/* This code was liberated and adapted from the commented-out code in * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration * (it doesn't have a CRT form for RSA), this function means that an * Atalla system running with a DSA server certificate can handshake * around 5 or 6 times faster/more than an equivalent system running with * RSA. Just check out the "signs" statistics from the RSA and DSA parts * of "openssl speed -engine atalla dsa1024 rsa1024". */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_nuron.c#L218-L239
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
nuron_mod_exp_dh
static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return nuron_mod_exp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the dh and mont dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_nuron.c#L261-L266
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bind_helper
static int bind_helper(ENGINE *e) { #ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1; #endif #ifndef OPENSSL_NO_DSA const DSA_METHOD *meth2; #endif #ifndef OPENSSL_NO_DH const DH_METHOD *meth3; #endif if(!ENGINE_set_id(e, engine_nuron_id) || !ENGINE_set_name(e, engine_nuron_name) || #ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &nuron_rsa) || #endif #ifndef OPENSSL_NO_DSA !ENGINE_set_DSA(e, &nuron_dsa) || #endif #ifndef OPENSSL_NO_DH !ENGINE_set_DH(e, &nuron_dh) || #endif !ENGINE_set_destroy_function(e, nuron_destroy) || !ENGINE_set_init_function(e, nuron_init) || !ENGINE_set_finish_function(e, nuron_finish) || !ENGINE_set_ctrl_function(e, nuron_ctrl) || !ENGINE_set_cmd_defns(e, nuron_cmd_defns)) return 0; #ifndef OPENSSL_NO_RSA /* We know that the "PKCS1_SSLeay()" functions hook properly * to the nuron-specific mod_exp and mod_exp_crt so we use * those functions. NB: We don't use ENGINE_openssl() or * anything "more generic" because something like the RSAref * code may not hook properly, and if you own one of these * cards then you have the right to do RSA operations on it * anyway! */ meth1=RSA_PKCS1_SSLeay(); nuron_rsa.rsa_pub_enc=meth1->rsa_pub_enc; nuron_rsa.rsa_pub_dec=meth1->rsa_pub_dec; nuron_rsa.rsa_priv_enc=meth1->rsa_priv_enc; nuron_rsa.rsa_priv_dec=meth1->rsa_priv_dec; #endif #ifndef OPENSSL_NO_DSA /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish * bits. */ meth2=DSA_OpenSSL(); nuron_dsa.dsa_do_sign=meth2->dsa_do_sign; nuron_dsa.dsa_sign_setup=meth2->dsa_sign_setup; nuron_dsa.dsa_do_verify=meth2->dsa_do_verify; #endif #ifndef OPENSSL_NO_DH /* Much the same for Diffie-Hellman */ meth3=DH_OpenSSL(); nuron_dh.generate_key=meth3->generate_key; nuron_dh.compute_key=meth3->compute_key; #endif /* Ensure the nuron error handling is set up */ ERR_load_NURON_strings(); return 1; }
/* This internal function is used by ENGINE_nuron() and possibly by the * "dynamic" ENGINE support too */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_nuron.c#L328-L391
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
padlock_bswapl
static inline void padlock_bswapl(AES_KEY *ks) { size_t i = sizeof(ks->rd_key)/sizeof(ks->rd_key[0]); unsigned int *key = ks->rd_key; while (i--) { asm volatile ("bswapl %0" : "+r"(*key)); key++; } }
/* Our own htonl()/ntohl() */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_padlock.c#L382-L392
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
padlock_verify_context
static inline void padlock_verify_context(struct padlock_cipher_data *cdata) { asm volatile ( "pushfl\n" " btl $30,(%%esp)\n" " jnc 1f\n" " cmpl %2,%1\n" " je 1f\n" " popfl\n" " subl $4,%%esp\n" "1: addl $4,%%esp\n" " movl %2,%0" :"+m"(padlock_saved_context) : "r"(padlock_saved_context), "r"(cdata) : "cc"); }
/* * This is heuristic key context tracing. At first one * believes that one should use atomic swap instructions, * but it's not actually necessary. Point is that if * padlock_saved_context was changed by another thread * after we've read it and before we compare it with cdata, * our key *shall* be reloaded upon thread context switch * and we are therefore set in either case... */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_padlock.c#L414-L429
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_mod_exp_mont
static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return surewarehk_modexp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the mont stuff dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L117-L121
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_modexp_dh
static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return surewarehk_modexp(r, a, p, m, ctx); }
/* Our internal DH_METHOD that we provide pointers to */ /* This function is aliased to mod_exp (with the dh and mont dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L146-L150
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bind_sureware
static int bind_sureware(ENGINE *e) { #ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1; #endif #ifndef OPENSSL_NO_DSA const DSA_METHOD *meth2; #endif #ifndef OPENSSL_NO_DH const DH_METHOD *meth3; #endif if(!ENGINE_set_id(e, engine_sureware_id) || !ENGINE_set_name(e, engine_sureware_name) || #ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &surewarehk_rsa) || #endif #ifndef OPENSSL_NO_DSA !ENGINE_set_DSA(e, &surewarehk_dsa) || #endif #ifndef OPENSSL_NO_DH !ENGINE_set_DH(e, &surewarehk_dh) || #endif !ENGINE_set_RAND(e, &surewarehk_rand) || !ENGINE_set_destroy_function(e, surewarehk_destroy) || !ENGINE_set_init_function(e, surewarehk_init) || !ENGINE_set_finish_function(e, surewarehk_finish) || !ENGINE_set_ctrl_function(e, surewarehk_ctrl) || !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) || !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey)) return 0; #ifndef OPENSSL_NO_RSA /* We know that the "PKCS1_SSLeay()" functions hook properly * to the cswift-specific mod_exp and mod_exp_crt so we use * those functions. NB: We don't use ENGINE_openssl() or * anything "more generic" because something like the RSAref * code may not hook properly, and if you own one of these * cards then you have the right to do RSA operations on it * anyway! */ meth1 = RSA_PKCS1_SSLeay(); if (meth1) { surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc; surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec; } #endif #ifndef OPENSSL_NO_DSA /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish * bits. */ meth2 = DSA_OpenSSL(); if (meth2) { surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify; } #endif #ifndef OPENSSL_NO_DH /* Much the same for Diffie-Hellman */ meth3 = DH_OpenSSL(); if (meth3) { surewarehk_dh.generate_key = meth3->generate_key; surewarehk_dh.compute_key = meth3->compute_key; } #endif /* Ensure the sureware error handling is set up */ ERR_load_SUREWARE_strings(); return 1; }
/* Now, to our own code */ /* As this is only ever called once, there's no need for locking * (indeed - the lock will already be held by our caller!!!) */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L223-L294
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_destroy
static int surewarehk_destroy(ENGINE *e) { ERR_unload_SUREWARE_strings(); return 1; }
/* Destructor (complements the "ENGINE_surewarehk()" constructor) */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L429-L433
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_init
static int surewarehk_init(ENGINE *e) { char msg[64]="ENGINE_init"; SureWareHook_Init_t *p1=NULL; SureWareHook_Finish_t *p2=NULL; SureWareHook_Rand_Bytes_t *p3=NULL; SureWareHook_Rand_Seed_t *p4=NULL; SureWareHook_Load_Privkey_t *p5=NULL; SureWareHook_Load_Rsa_Pubkey_t *p6=NULL; SureWareHook_Free_t *p7=NULL; SureWareHook_Rsa_Priv_Dec_t *p8=NULL; SureWareHook_Rsa_Sign_t *p9=NULL; SureWareHook_Dsa_Sign_t *p12=NULL; SureWareHook_Info_Pubkey_t *p13=NULL; SureWareHook_Load_Dsa_Pubkey_t *p14=NULL; SureWareHook_Mod_Exp_t *p15=NULL; if(surewarehk_dso != NULL) { SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_ALREADY_LOADED); goto err; } /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */ surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0); if(surewarehk_dso == NULL) { SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE); goto err; } if(!(p1=(SureWareHook_Init_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Init)) || !(p2=(SureWareHook_Finish_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Finish)) || !(p3=(SureWareHook_Rand_Bytes_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Bytes)) || !(p4=(SureWareHook_Rand_Seed_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Seed)) || !(p5=(SureWareHook_Load_Privkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Privkey)) || !(p6=(SureWareHook_Load_Rsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Rsa_Pubkey)) || !(p7=(SureWareHook_Free_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Free)) || !(p8=(SureWareHook_Rsa_Priv_Dec_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Priv_Dec)) || !(p9=(SureWareHook_Rsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Sign)) || !(p12=(SureWareHook_Dsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Dsa_Sign)) || !(p13=(SureWareHook_Info_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Info_Pubkey)) || !(p14=(SureWareHook_Load_Dsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Dsa_Pubkey)) || !(p15=(SureWareHook_Mod_Exp_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Mod_Exp))) { SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE); goto err; } /* Copy the pointers */ p_surewarehk_Init = p1; p_surewarehk_Finish = p2; p_surewarehk_Rand_Bytes = p3; p_surewarehk_Rand_Seed = p4; p_surewarehk_Load_Privkey = p5; p_surewarehk_Load_Rsa_Pubkey = p6; p_surewarehk_Free = p7; p_surewarehk_Rsa_Priv_Dec = p8; p_surewarehk_Rsa_Sign = p9; p_surewarehk_Dsa_Sign = p12; p_surewarehk_Info_Pubkey = p13; p_surewarehk_Load_Dsa_Pubkey = p14; p_surewarehk_Mod_Exp = p15; /* Contact the hardware and initialises it. */ if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE) { SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE); goto err; } if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE) { SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE); goto err; } /* try to load the default private key, if failed does not return a failure but wait for an explicit ENGINE_load_privakey */ surewarehk_load_privkey(e,NULL,NULL,NULL); /* Everything's fine. */ #ifndef OPENSSL_NO_RSA if (rsaHndidx == -1) rsaHndidx = RSA_get_ex_new_index(0, "SureWareHook RSA key handle", NULL, NULL, surewarehk_ex_free); #endif #ifndef OPENSSL_NO_DSA if (dsaHndidx == -1) dsaHndidx = DSA_get_ex_new_index(0, "SureWareHook DSA key handle", NULL, NULL, surewarehk_ex_free); #endif return 1; err: if(surewarehk_dso) DSO_free(surewarehk_dso); surewarehk_dso = NULL; p_surewarehk_Init = NULL; p_surewarehk_Finish = NULL; p_surewarehk_Rand_Bytes = NULL; p_surewarehk_Rand_Seed = NULL; p_surewarehk_Load_Privkey = NULL; p_surewarehk_Load_Rsa_Pubkey = NULL; p_surewarehk_Free = NULL; p_surewarehk_Rsa_Priv_Dec = NULL; p_surewarehk_Rsa_Sign = NULL; p_surewarehk_Dsa_Sign = NULL; p_surewarehk_Info_Pubkey = NULL; p_surewarehk_Load_Dsa_Pubkey = NULL; p_surewarehk_Mod_Exp = NULL; return 0; }
/* (de)initialisation functions. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L436-L544
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_ex_free
static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,long argl, void *argp) { if(!p_surewarehk_Free) { SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE,ENGINE_R_NOT_INITIALISED); } else p_surewarehk_Free((char *)item,0); }
/* This cleans up an RSA/DSA KM key(do not destroy the key into the hardware) , called when ex_data is freed */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L831-L840
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_dh_ex_free
static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad, int idx,long argl, void *argp) { if(!p_surewarehk_Free) { SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,ENGINE_R_NOT_INITIALISED); } else p_surewarehk_Free((char *)item,1); }
/* not currently used (bug?) */ /* This cleans up an DH KM key (destroys the key into hardware), called when ex_data is freed */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L846-L855
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
surewarehk_rsa_sign
static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to, RSA *rsa,int padding) { int ret=0,tlen; char *hptr=NULL; char msg[64]="ENGINE_rsa_sign"; if (!p_surewarehk_Rsa_Sign) { SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,ENGINE_R_NOT_INITIALISED); } /* extract ref to private key */ else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx))) { SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS); } else { switch (padding) { case RSA_PKCS1_PADDING: /* do it in one shot */ ret=p_surewarehk_Rsa_Sign(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD); surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_SIGN,ret); break; case RSA_NO_PADDING: default: SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_UNKNOWN_PADDING_TYPE); } } return ret==1 ? tlen : ret; }
/* * Does what OpenSSL rsa_priv_enc does. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_sureware.c#L933-L962
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
bind_helper
static int bind_helper(ENGINE *e) { #ifndef OPENSSL_NO_RSA const RSA_METHOD *meth1; #endif #ifndef OPENSSL_NO_DH #ifndef HAVE_UBSEC_DH const DH_METHOD *meth3; #endif /* HAVE_UBSEC_DH */ #endif if(!ENGINE_set_id(e, engine_ubsec_id) || !ENGINE_set_name(e, engine_ubsec_name) || #ifndef OPENSSL_NO_RSA !ENGINE_set_RSA(e, &ubsec_rsa) || #endif #ifndef OPENSSL_NO_DSA !ENGINE_set_DSA(e, &ubsec_dsa) || #endif #ifndef OPENSSL_NO_DH !ENGINE_set_DH(e, &ubsec_dh) || #endif !ENGINE_set_destroy_function(e, ubsec_destroy) || !ENGINE_set_init_function(e, ubsec_init) || !ENGINE_set_finish_function(e, ubsec_finish) || !ENGINE_set_ctrl_function(e, ubsec_ctrl) || !ENGINE_set_cmd_defns(e, ubsec_cmd_defns)) return 0; #ifndef OPENSSL_NO_RSA /* We know that the "PKCS1_SSLeay()" functions hook properly * to the Broadcom-specific mod_exp and mod_exp_crt so we use * those functions. NB: We don't use ENGINE_openssl() or * anything "more generic" because something like the RSAref * code may not hook properly, and if you own one of these * cards then you have the right to do RSA operations on it * anyway! */ meth1 = RSA_PKCS1_SSLeay(); ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc; ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec; ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc; ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec; #endif #ifndef OPENSSL_NO_DH #ifndef HAVE_UBSEC_DH /* Much the same for Diffie-Hellman */ meth3 = DH_OpenSSL(); ubsec_dh.generate_key = meth3->generate_key; ubsec_dh.compute_key = meth3->compute_key; #endif /* HAVE_UBSEC_DH */ #endif /* Ensure the ubsec error handling is set up */ ERR_load_UBSEC_strings(); return 1; }
/* This internal function is used by ENGINE_ubsec() and possibly by the * "dynamic" ENGINE support too */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_ubsec.c#L203-L258
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ubsec_destroy
static int ubsec_destroy(ENGINE *e) { free_UBSEC_LIBNAME(); ERR_unload_UBSEC_strings(); return 1; }
/* Destructor (complements the "ENGINE_ubsec()" constructor) */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_ubsec.c#L363-L368
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ubsec_init
static int ubsec_init(ENGINE *e) { t_UBSEC_ubsec_bytes_to_bits *p1; t_UBSEC_ubsec_bits_to_bytes *p2; t_UBSEC_ubsec_open *p3; t_UBSEC_ubsec_close *p4; #ifndef OPENSSL_NO_DH t_UBSEC_diffie_hellman_generate_ioctl *p5; t_UBSEC_diffie_hellman_agree_ioctl *p6; #endif /* #ifndef OPENSSL_NO_RSA */ t_UBSEC_rsa_mod_exp_ioctl *p7; t_UBSEC_rsa_mod_exp_crt_ioctl *p8; /* #endif */ #ifndef OPENSSL_NO_DSA t_UBSEC_dsa_sign_ioctl *p9; t_UBSEC_dsa_verify_ioctl *p10; #endif t_UBSEC_math_accelerate_ioctl *p11; t_UBSEC_rng_ioctl *p12; t_UBSEC_max_key_len_ioctl *p13; int fd = 0; if(ubsec_dso != NULL) { UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED); goto err; } /* * Attempt to load libubsec.so/ubsec.dll/whatever. */ ubsec_dso = DSO_load(NULL, get_UBSEC_LIBNAME(), NULL, 0); if(ubsec_dso == NULL) { UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE); goto err; } if ( !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) || !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) || !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) || !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) || #ifndef OPENSSL_NO_DH !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F5)) || !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F6)) || #endif /* #ifndef OPENSSL_NO_RSA */ !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) || !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) || /* #endif */ #ifndef OPENSSL_NO_DSA !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) || !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) || #endif !(p11 = (t_UBSEC_math_accelerate_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F11)) || !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) || !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13))) { UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE); goto err; } /* Copy the pointers */ p_UBSEC_ubsec_bytes_to_bits = p1; p_UBSEC_ubsec_bits_to_bytes = p2; p_UBSEC_ubsec_open = p3; p_UBSEC_ubsec_close = p4; #ifndef OPENSSL_NO_DH p_UBSEC_diffie_hellman_generate_ioctl = p5; p_UBSEC_diffie_hellman_agree_ioctl = p6; #endif #ifndef OPENSSL_NO_RSA p_UBSEC_rsa_mod_exp_ioctl = p7; p_UBSEC_rsa_mod_exp_crt_ioctl = p8; #endif #ifndef OPENSSL_NO_DSA p_UBSEC_dsa_sign_ioctl = p9; p_UBSEC_dsa_verify_ioctl = p10; #endif p_UBSEC_math_accelerate_ioctl = p11; p_UBSEC_rng_ioctl = p12; p_UBSEC_max_key_len_ioctl = p13; /* Perform an open to see if there's actually any unit running. */ if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0)) { p_UBSEC_ubsec_close(fd); return 1; } else { UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE); } err: if(ubsec_dso) DSO_free(ubsec_dso); ubsec_dso = NULL; p_UBSEC_ubsec_bytes_to_bits = NULL; p_UBSEC_ubsec_bits_to_bytes = NULL; p_UBSEC_ubsec_open = NULL; p_UBSEC_ubsec_close = NULL; #ifndef OPENSSL_NO_DH p_UBSEC_diffie_hellman_generate_ioctl = NULL; p_UBSEC_diffie_hellman_agree_ioctl = NULL; #endif #ifndef OPENSSL_NO_RSA p_UBSEC_rsa_mod_exp_ioctl = NULL; p_UBSEC_rsa_mod_exp_crt_ioctl = NULL; #endif #ifndef OPENSSL_NO_DSA p_UBSEC_dsa_sign_ioctl = NULL; p_UBSEC_dsa_verify_ioctl = NULL; #endif p_UBSEC_math_accelerate_ioctl = NULL; p_UBSEC_rng_ioctl = NULL; p_UBSEC_max_key_len_ioctl = NULL; return 0; }
/* (de)initialisation functions. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_ubsec.c#L371-L494
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ubsec_mod_exp_mont
static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { int ret = 0; /* Do in software if the key is too large for the hardware. */ if (BN_num_bits(m) > max_key_len) { const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx); } else { ret = ubsec_mod_exp(r, a, p, m, ctx); } return ret; }
/* * This function is aliased to mod_exp (with the mont stuff dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_ubsec.c#L710-L727
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
ubsec_mod_exp_dh
static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) { return ubsec_mod_exp(r, a, p, m, ctx); }
/* This function is aliased to mod_exp (with the dh and mont dropped). */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/e_ubsec.c#L732-L737
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
fill_GOST2001_params
int fill_GOST2001_params(EC_KEY *eckey, int nid) { R3410_2001_params *params = R3410_2001_paramset; EC_GROUP *grp=NULL; BIGNUM *p=NULL,*q=NULL,*a=NULL,*b=NULL,*x=NULL,*y=NULL; EC_POINT *P=NULL; BN_CTX *ctx=BN_CTX_new(); int ok=0; BN_CTX_start(ctx); p=BN_CTX_get(ctx); a=BN_CTX_get(ctx); b=BN_CTX_get(ctx); x=BN_CTX_get(ctx); y=BN_CTX_get(ctx); q=BN_CTX_get(ctx); while (params->nid!=NID_undef && params->nid != nid) params++; if (params->nid == NID_undef) { GOSTerr(GOST_F_FILL_GOST2001_PARAMS,GOST_R_UNSUPPORTED_PARAMETER_SET); goto err; } BN_hex2bn(&p,params->p); BN_hex2bn(&a,params->a); BN_hex2bn(&b,params->b); grp = EC_GROUP_new_curve_GFp(p,a,b,ctx); P = EC_POINT_new(grp); BN_hex2bn(&x,params->x); BN_hex2bn(&y,params->y); EC_POINT_set_affine_coordinates_GFp(grp,P,x,y,ctx); BN_hex2bn(&q,params->q); #ifdef DEBUG_KEYS fprintf(stderr,"Set params index %d oid %s\nq=", (params-R3410_2001_paramset),OBJ_nid2sn(params->nid)); BN_print_fp(stderr,q); fprintf(stderr,"\n"); #endif EC_GROUP_set_generator(grp,P,q,NULL); EC_GROUP_set_curve_name(grp,params->nid); EC_KEY_set_group(eckey,grp); ok=1; err: EC_POINT_free(P); EC_GROUP_free(grp); BN_CTX_end(ctx); BN_CTX_free(ctx); return ok; }
/* * Fills EC_KEY structure hidden in the app_data field of DSA structure * with parameter information, extracted from parameter array in * params.c file. * * Also fils DSA->q field with copy of EC_GROUP order field to make * DSA_size function work */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001.c#L34-L86
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost2001_do_verify
int gost2001_do_verify(const unsigned char *dgst,int dgst_len, DSA_SIG *sig, EC_KEY *ec) { BN_CTX *ctx=BN_CTX_new(); const EC_GROUP *group = EC_KEY_get0_group(ec); BIGNUM *order; BIGNUM *md = NULL,*e=NULL,*R=NULL,*v=NULL,*z1=NULL,*z2=NULL; BIGNUM *X=NULL,*tmp=NULL; EC_POINT *C = NULL; const EC_POINT *pub_key=NULL; int ok=0; BN_CTX_start(ctx); order = BN_CTX_get(ctx); e = BN_CTX_get(ctx); z1 = BN_CTX_get(ctx); z2 = BN_CTX_get(ctx); tmp = BN_CTX_get(ctx); X= BN_CTX_get(ctx); R=BN_CTX_get(ctx); v=BN_CTX_get(ctx); EC_GROUP_get_order(group,order,ctx); pub_key = EC_KEY_get0_public_key(ec); if (BN_is_zero(sig->s) || BN_is_zero(sig->r) || (BN_cmp(sig->s,order)>=1) || (BN_cmp(sig->r,order)>=1)) { GOSTerr(GOST_F_GOST2001_DO_VERIFY,GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q); goto err; } md = hashsum2bn(dgst); BN_mod(e,md,order,ctx); #ifdef DEBUG_SIGN fprintf(stderr,"digest as bignum: "); BN_print_fp(stderr,md); fprintf(stderr,"\ndigest mod q: "); BN_print_fp(stderr,e); #endif if (BN_is_zero(e)) BN_one(e); v=BN_mod_inverse(v,e,order,ctx); BN_mod_mul(z1,sig->s,v,order,ctx); BN_sub(tmp,order,sig->r); BN_mod_mul(z2,tmp,v,order,ctx); #ifdef DEBUG_SIGN fprintf(stderr,"\nInverted digest value: "); BN_print_fp(stderr,v); fprintf(stderr,"\nz1: "); BN_print_fp(stderr,z1); fprintf(stderr,"\nz2: "); BN_print_fp(stderr,z2); #endif C = EC_POINT_new(group); if (!EC_POINT_mul(group,C,z1,pub_key,z2,ctx)) { GOSTerr(GOST_F_GOST2001_DO_VERIFY,ERR_R_EC_LIB); goto err; } if (!EC_POINT_get_affine_coordinates_GFp(group,C,X,NULL,ctx)) { GOSTerr(GOST_F_GOST2001_DO_VERIFY,ERR_R_EC_LIB); goto err; } BN_mod(R,X,order,ctx); #ifdef DEBUG_SIGN fprintf(stderr,"\nX="); BN_print_fp(stderr,X); fprintf(stderr,"\nX mod q="); BN_print_fp(stderr,R); fprintf(stderr,"\n"); #endif if (BN_cmp(R,sig->r)!=0) { GOSTerr(GOST_F_GOST2001_DO_VERIFY,GOST_R_SIGNATURE_MISMATCH); } else { ok = 1; } err: EC_POINT_free(C); BN_CTX_end(ctx); BN_CTX_free(ctx); BN_free(md); return ok; }
/* * Verifies gost 2001 signature * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001.c#L184-L270
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost2001_compute_public
int gost2001_compute_public(EC_KEY *ec) { const EC_GROUP *group = EC_KEY_get0_group(ec); EC_POINT *pub_key=NULL; const BIGNUM *priv_key=NULL; BN_CTX *ctx=NULL; int ok=0; if (!group) { GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,GOST_R_KEY_IS_NOT_INITIALIZED); return 0; } ctx=BN_CTX_new(); BN_CTX_start(ctx); if (!(priv_key=EC_KEY_get0_private_key(ec))) { GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB); goto err; } pub_key = EC_POINT_new(group); if (!EC_POINT_mul(group,pub_key,priv_key,NULL,NULL,ctx)) { GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB); goto err; } if (!EC_KEY_set_public_key(ec,pub_key)) { GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,ERR_R_EC_LIB); goto err; } ok = 256; err: BN_CTX_end(ctx); EC_POINT_free(pub_key); BN_CTX_free(ctx); return ok; }
/* * Computes GOST R 34.10-2001 public key * * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001.c#L276-L314
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost2001_keygen
int gost2001_keygen(EC_KEY *ec) { BIGNUM *order = BN_new(),*d=BN_new(); const EC_GROUP *group = EC_KEY_get0_group(ec); EC_GROUP_get_order(group,order,NULL); do { if (!BN_rand_range(d,order)) { GOSTerr(GOST_F_GOST2001_KEYGEN,GOST_R_RANDOM_NUMBER_GENERATOR_FAILED); BN_free(d); BN_free(order); return 0; } } while (BN_is_zero(d)); EC_KEY_set_private_key(ec,d); BN_free(d); BN_free(order); return gost2001_compute_public(ec); }
/* * * Generates GOST R 34.10-2001 keypair * * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001.c#L321-L342
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
VKO_compute_key
static int VKO_compute_key(unsigned char *shared_key,size_t shared_key_size,const EC_POINT *pub_key,EC_KEY *priv_key,const unsigned char *ukm) { unsigned char ukm_be[8],databuf[64],hashbuf[64]; BIGNUM *UKM=NULL,*p=NULL,*order=NULL,*X=NULL,*Y=NULL; const BIGNUM* key=EC_KEY_get0_private_key(priv_key); EC_POINT *pnt=EC_POINT_new(EC_KEY_get0_group(priv_key)); int i; gost_hash_ctx hash_ctx; BN_CTX *ctx = BN_CTX_new(); for (i=0;i<8;i++) { ukm_be[7-i]=ukm[i]; } BN_CTX_start(ctx); UKM=getbnfrombuf(ukm_be,8); p=BN_CTX_get(ctx); order = BN_CTX_get(ctx); X=BN_CTX_get(ctx); Y=BN_CTX_get(ctx); EC_GROUP_get_order(EC_KEY_get0_group(priv_key),order,ctx); BN_mod_mul(p,key,UKM,order,ctx); EC_POINT_mul(EC_KEY_get0_group(priv_key),pnt,NULL,pub_key,p,ctx); EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key), pnt,X,Y,ctx); /*Serialize elliptic curve point same way as we do it when saving * key */ store_bignum(Y,databuf,32); store_bignum(X,databuf+32,32); /* And reverse byte order of whole buffer */ for (i=0;i<64;i++) { hashbuf[63-i]=databuf[i]; } init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet); start_hash(&hash_ctx); hash_block(&hash_ctx,hashbuf,64); finish_hash(&hash_ctx,shared_key); done_gost_hash_ctx(&hash_ctx); BN_free(UKM); BN_CTX_end(ctx); BN_CTX_free(ctx); EC_POINT_free(pnt); return 32; }
/* Implementation of CryptoPro VKO 34.10-2001 algorithm */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001_keyx.c#L24-L68
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost2001_derive
int pkey_gost2001_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen) { /* Public key of peer in the ctx field peerkey * Our private key in the ctx pkey * ukm is in the algorithm specific context data */ EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx); EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx); struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); if (!data->shared_ukm) { GOSTerr(GOST_F_PKEY_GOST2001_DERIVE, GOST_R_UKM_NOT_SET); return 0; } if (key == NULL) { *keylen = 32; return 32; } *keylen=VKO_compute_key(key, 32, EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)), (EC_KEY *)EVP_PKEY_get0(my_key),data->shared_ukm); return 1; }
/* * EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-2001 * algorithm */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001_keyx.c#L75-L98
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_GOST01cp_encrypt
int pkey_GOST01cp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out, size_t *out_len, const unsigned char *key,size_t key_len) { GOST_KEY_TRANSPORT *gkt=NULL; EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx); struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx); const struct gost_cipher_info *param=get_encryption_params(NULL); unsigned char ukm[8], shared_key[32], crypted_key[44]; int ret=0; int key_is_ephemeral=1; gost_ctx cctx; EVP_PKEY *sec_key=EVP_PKEY_CTX_get0_peerkey(pctx); if (data->shared_ukm) { memcpy(ukm, data->shared_ukm,8); } else if (out) { if (RAND_bytes(ukm,8)<=0) { GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT, GOST_R_RANDOM_GENERATOR_FAILURE); return 0; } } /* Check for private key in the peer_key of context */ if (sec_key) { key_is_ephemeral=0; if (!gost_get0_priv_key(sec_key)) { GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT, GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR); goto err; } } else { key_is_ephemeral=1; if (out) { sec_key = EVP_PKEY_new(); EVP_PKEY_assign(sec_key,EVP_PKEY_base_id(pubk),EC_KEY_new()); EVP_PKEY_copy_parameters(sec_key,pubk); if (!gost2001_keygen(EVP_PKEY_get0(sec_key))) { goto err; } } } if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS) && param == gost_cipher_list) { param= gost_cipher_list+1; } if (out) { VKO_compute_key(shared_key,32,EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),EVP_PKEY_get0(sec_key),ukm); gost_init(&cctx,param->sblock); keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key); } gkt = GOST_KEY_TRANSPORT_new(); if (!gkt) { goto err; } if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm,8)) { goto err; } if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4)) { goto err; } if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32)) { goto err; } if (key_is_ephemeral) { if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,out?sec_key:pubk)) { GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY); goto err; } } ASN1_OBJECT_free(gkt->key_agreement_info->cipher); gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid); if (key_is_ephemeral && sec_key) EVP_PKEY_free(sec_key); if (!key_is_ephemeral) { /* Set control "public key from client certificate used" */ if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <= 0) { GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED); goto err; } } if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt,out?&out:NULL))>0) ret =1; GOST_KEY_TRANSPORT_free(gkt); return ret; err: if (key_is_ephemeral && sec_key) EVP_PKEY_free(sec_key); GOST_KEY_TRANSPORT_free(gkt); return -1; }
/* * EVP_PKEY_METHOD callback encrypt * Implementation of GOST2001 key transport, cryptocom variation */ /* Generates ephemeral key based on pubk algorithm * computes shared key using VKO and returns filled up * GOST_KEY_TRANSPORT structure */ /* * EVP_PKEY_METHOD callback encrypt * Implementation of GOST2001 key transport, cryptopo variation */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001_keyx.c#L117-L223
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_GOST01cp_decrypt
int pkey_GOST01cp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key, size_t * key_len, const unsigned char *in, size_t in_len) { const unsigned char *p = in; EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx); GOST_KEY_TRANSPORT *gkt = NULL; int ret=0; unsigned char wrappedKey[44]; unsigned char sharedKey[32]; gost_ctx ctx; const struct gost_cipher_info *param=NULL; EVP_PKEY *eph_key=NULL, *peerkey=NULL; if (!key) { *key_len = 32; return 1; } gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p, in_len); if (!gkt) { GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO); return -1; } /* If key transport structure contains public key, use it */ eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key); if (eph_key) { if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) { GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_INCOMPATIBLE_PEER_KEY); goto err; } } else { /* Set control "public key from client certificate used" */ if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <= 0) { GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_CTRL_CALL_FAILED); goto err; } } peerkey = EVP_PKEY_CTX_get0_peerkey(pctx); if (!peerkey) { GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_NO_PEER_KEY); goto err; } param = get_encryption_params(gkt->key_agreement_info->cipher); gost_init(&ctx,param->sblock); OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8); memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8); OPENSSL_assert(gkt->key_info->encrypted_key->length==32); memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32); OPENSSL_assert(gkt->key_info->imit->length==4); memcpy(wrappedKey+40,gkt->key_info->imit->data,4); VKO_compute_key(sharedKey,32,EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)), EVP_PKEY_get0(priv),wrappedKey); if (!keyUnwrapCryptoPro(&ctx,sharedKey,wrappedKey,key)) { GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_ERROR_COMPUTING_SHARED_KEY); goto err; } ret=1; err: if (eph_key) EVP_PKEY_free(eph_key); if (gkt) GOST_KEY_TRANSPORT_free(gkt); return ret; }
/* * EVP_PKEY_METHOD callback decrypt * Implementation of GOST2001 key transport, cryptopo variation */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost2001_keyx.c#L228-L304
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
kboxinit
static void kboxinit(gost_ctx *c, const gost_subst_block *b) { int i; for (i = 0; i < 256; i++) { c->k87[i] = (b->k8[i>>4] <<4 | b->k7 [i &15])<<24; c->k65[i] = (b->k6[i>>4] << 4 | b->k5 [i &15])<<16; c->k43[i] = (b->k4[i>>4] <<4 | b->k3 [i &15])<<8; c->k21[i] = b->k2[i>>4] <<4 | b->k1 [i &15]; } }
/* Initialization of gost_ctx subst blocks*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L117-L129
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
f
static word32 f(gost_ctx *c,word32 x) { x = c->k87[x>>24 & 255] | c->k65[x>>16 & 255]| c->k43[x>> 8 & 255] | c->k21[x & 255]; /* Rotate left 11 bits */ return x<<11 | x>>(32-11); }
/* Part of GOST 28147 algorithm moved into separate function */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L132-L138
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gostcrypt
void gostcrypt(gost_ctx *c, const byte *in, byte *out) { register word32 n1, n2; /* As named in the GOST */ n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24); n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24); /* Instead of swapping halves, swap names each round */ n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]); n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]); n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]); n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]); out[0] = (byte)(n2&0xff); out[1] = (byte)((n2>>8)&0xff); out[2] = (byte)((n2>>16)&0xff); out[3]=(byte)(n2>>24); out[4] = (byte)(n1&0xff); out[5] = (byte)((n1>>8)&0xff); out[6] = (byte)((n1>>16)&0xff); out[7] = (byte)(n1>>24); }
/* Low-level encryption routine - encrypts one 64 bit block*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L140-L171
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gostdecrypt
void gostdecrypt(gost_ctx *c, const byte *in,byte *out) { register word32 n1, n2; /* As named in the GOST */ n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24); n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24); n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]); n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]); n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]); n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]); n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]); n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]); n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]); n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]); n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]); n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]); n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]); n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]); out[0] = (byte)(n2&0xff); out[1] = (byte)((n2>>8)&0xff); out[2] = (byte)((n2>>16)&0xff); out[3]=(byte)(n2>>24); out[4] = (byte)(n1&0xff); out[5] = (byte)((n1>>8)&0xff); out[6] = (byte)((n1>>16)&0xff); out[7] = (byte)(n1>>24); }
/* Low-level decryption routine. Decrypts one 64-bit block */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L173-L203
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_enc
void gost_enc(gost_ctx *c,const byte *clear,byte *cipher, int blocks) { int i; for(i=0;i<blocks;i++) { gostcrypt(c,clear,cipher); clear+=8; cipher+=8; } }
/* Encrypts several blocks in ECB mode */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L206-L215
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_dec
void gost_dec(gost_ctx *c, const byte *cipher,byte *clear, int blocks) { int i; for(i=0;i<blocks;i++) { gostdecrypt(c,cipher,clear); clear+=8; cipher+=8; } }
/* Decrypts several blocks in ECB mode */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L217-L226
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_enc_cfb
void gost_enc_cfb(gost_ctx *ctx,const byte *iv,const byte *clear,byte *cipher, int blocks) { byte cur_iv[8]; byte gamma[8]; int i,j; const byte *in; byte *out; memcpy(cur_iv,iv,8); for(i=0,in=clear,out=cipher;i<blocks;i++,in+=8,out+=8) { gostcrypt(ctx,cur_iv,gamma); for (j=0;j<8;j++) { cur_iv[j]=out[j]=in[j]^gamma[j]; } } }
/* Encrypts several full blocks in CFB mode using 8byte IV */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L229-L245
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_dec_cfb
void gost_dec_cfb(gost_ctx *ctx,const byte *iv,const byte *cipher,byte *clear, int blocks) { byte cur_iv[8]; byte gamma[8]; int i,j; const byte *in; byte *out; memcpy(cur_iv,iv,8); for(i=0,in=cipher,out=clear;i<blocks;i++,in+=8,out+=8) { gostcrypt(ctx,cur_iv,gamma); for (j=0;j<8;j++) { out[j]=(cur_iv[j]=in[j])^gamma[j]; } } }
/* Decrypts several full blocks in CFB mode using 8byte IV */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L247-L263
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_enc_with_key
void gost_enc_with_key(gost_ctx *c,byte *key,byte *inblock,byte *outblock) { gost_key(c,key); gostcrypt(c,inblock,outblock); }
/* Encrypts one block using specified key */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L266-L270
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_key
void gost_key(gost_ctx *c, const byte *k) { int i,j; for(i=0,j=0;i<8;i++,j+=4) { c->k[i]=k[j]|(k[j+1]<<8)|(k[j+2]<<16)|(k[j+3]<<24); } }
/* Set 256 bit key into context */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L273-L280
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_get_key
void gost_get_key(gost_ctx *c, byte *k) { int i,j; for(i=0,j=0;i<8;i++,j+=4) { k[j]=(byte)(c->k[i]& 0xFF); k[j+1]=(byte)((c->k[i]>>8 )&0xFF); k[j+2]=(byte)((c->k[i]>>16) &0xFF); k[j+3]=(byte)((c->k[i]>>24) &0xFF); } }
/* Retrieve 256-bit key from context */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L283-L293
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_init
void gost_init(gost_ctx *c, const gost_subst_block *b) { if(!b) { b=&GostR3411_94_TestParamSet; } kboxinit(c,b); }
/* Initalize context. Provides default value for subst_block */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L296-L303
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_destroy
void gost_destroy(gost_ctx *c) { int i; for(i=0;i<8;i++) c->k[i]=0; }
/* Cleans up key from context */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L306-L309
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
mac_block
void mac_block(gost_ctx *c,byte *buffer,const byte *block) { register word32 n1, n2; /* As named in the GOST */ int i; for (i=0; i<8; i++) { buffer[i]^=block[i]; } n1 = buffer[0]|(buffer[1]<<8)|(buffer[2]<<16)|(buffer[3]<<24); n2 = buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24); /* Instead of swapping halves, swap names each round */ n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]); n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]); buffer[0] = (byte)(n1&0xff); buffer[1] = (byte)((n1>>8)&0xff); buffer[2] = (byte)((n1>>16)&0xff); buffer[3] = (byte)(n1>>24); buffer[4] = (byte)(n2&0xff); buffer[5] = (byte)((n2>>8)&0xff); buffer[6] = (byte)((n2>>16)&0xff); buffer[7] = (byte)(n2>>24); }
/* Compute GOST 28147 mac block * * Parameters * gost_ctx *c - context initalized with substitution blocks and key * buffer - 8-byte mac state buffer * block 8-byte block to process. * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L318-L344
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
get_mac
void get_mac(byte *buffer,int nbits,byte *out) { int nbytes= nbits >> 3; int rembits = nbits & 7; int mask =rembits?((1<rembits)-1):0; int i; for (i=0;i<nbytes;i++) out[i]=buffer[i]; if (rembits) out[i]=buffer[i]&mask; }
/* Get mac with specified number of bits from MAC state buffer */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L347-L355
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_mac
int gost_mac(gost_ctx *ctx,int mac_len,const unsigned char *data, unsigned int data_len,unsigned char *mac) { byte buffer[8]={0,0,0,0,0,0,0,0}; byte buf2[8]; unsigned int i; for (i=0;i+8<=data_len;i+=8) mac_block(ctx,buffer,data+i); if (i<data_len) { memset(buf2,0,8); memcpy(buf2,data+i,data_len-i); mac_block(ctx,buffer,buf2); } get_mac(buffer,mac_len,mac); return 1; }
/* Compute mac of specified length (in bits) from data. * Context should be initialized with key and subst blocks */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L359-L375
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_mac_iv
int gost_mac_iv(gost_ctx *ctx,int mac_len,const unsigned char *iv,const unsigned char *data, unsigned int data_len,unsigned char *mac) { byte buffer[8]; byte buf2[8]; unsigned int i; memcpy (buffer,iv,8); for (i=0;i+8<=data_len;i+=8) mac_block(ctx,buffer,data+i); if (i<data_len) { memset(buf2,0,8); memcpy(buf2,data+i,data_len-i); mac_block(ctx,buffer,buf2); } get_mac(buffer,mac_len,mac); return 1; }
/* Compute MAC with non-zero IV. Used in some RFC 4357 algorithms */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L378-L395
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
cryptopro_key_meshing
void cryptopro_key_meshing(gost_ctx *ctx, unsigned char *iv) { unsigned char newkey[32],newiv[8]; /* Set static keymeshing key */ /* "Decrypt" key with keymeshing key */ gost_dec(ctx,CryptoProKeyMeshingKey,newkey,4); /* set new key */ gost_key(ctx,newkey); /* Encrypt iv with new key */ gostcrypt(ctx,iv,newiv); memcpy(iv,newiv,8); }
/* Implements key meshing algorithm by modifing ctx and IV in place */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost89.c#L398-L409
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
compute_pair_key_le
static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh) { unsigned char be_key[128]; int i,key_size; key_size=DH_compute_key(be_key,pub_key,dh); if (!key_size) return 0; memset(pair_key,0,128); for (i=0;i<key_size;i++) { pair_key[i]=be_key[key_size-1-i]; } return key_size; }
/* Common functions for both 94 and 2001 key exchange schemes */ /* Implementation of the Diffi-Hellman key agreement scheme based on * GOST-94 keys */ /* Computes Diffie-Hellman key and stores it into buffer in * little-endian byte order as expected by both versions of GOST 94 * algorithm */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost94_keyx.c#L30-L42
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
make_cp_exchange_key
static int make_cp_exchange_key(BIGNUM *priv_key,EVP_PKEY *pubk, unsigned char *shared_key) { unsigned char dh_key [128]; int ret; gost_hash_ctx hash_ctx; DH *dh = DH_new(); if (!dh) return 0; memset(dh_key,0,128); dh->g = BN_dup(pubk->pkey.dsa->g); dh->p = BN_dup(pubk->pkey.dsa->p); dh->priv_key = BN_dup(priv_key); ret=compute_pair_key_le(dh_key,((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,dh) ; DH_free(dh); if (!ret) return 0; init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet); start_hash(&hash_ctx); hash_block(&hash_ctx,dh_key,128); finish_hash(&hash_ctx,shared_key); done_gost_hash_ctx(&hash_ctx); return 1; }
/* * Computes 256 bit Key exchange key as specified in RFC 4357 */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost94_keyx.c#L47-L69
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost94_derive
int pkey_gost94_derive(EVP_PKEY_CTX *ctx,unsigned char *key,size_t *keylen) { EVP_PKEY *pubk = EVP_PKEY_CTX_get0_peerkey(ctx); EVP_PKEY *mykey = EVP_PKEY_CTX_get0_pkey(ctx); *keylen = 32; if (key == NULL) return 1; return make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, key); }
/* EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-94 */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost94_keyx.c#L73-L81
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_GOST94cp_encrypt
int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ) { GOST_KEY_TRANSPORT *gkt=NULL; unsigned char shared_key[32], ukm[8],crypted_key[44]; const struct gost_cipher_info *param=get_encryption_params(NULL); EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx); struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); gost_ctx cctx; int key_is_ephemeral=1; EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx); /* Do not use vizir cipher parameters with cryptopro */ if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS) && param == gost_cipher_list) { param= gost_cipher_list+1; } if (mykey) { /* If key already set, it is not ephemeral */ key_is_ephemeral=0; if (!gost_get0_priv_key(mykey)) { GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR); goto err; } } else { /* Otherwise generate ephemeral key */ key_is_ephemeral = 1; if (out) { mykey = EVP_PKEY_new(); EVP_PKEY_assign(mykey, EVP_PKEY_base_id(pubk),DSA_new()); EVP_PKEY_copy_parameters(mykey,pubk); if (!gost_sign_keygen(EVP_PKEY_get0(mykey))) { goto err; } } } if (out) make_cp_exchange_key(gost_get0_priv_key(mykey),pubk,shared_key); if (data->shared_ukm) { memcpy(ukm,data->shared_ukm,8); } else if (out) { if (RAND_bytes(ukm,8)<=0) { GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_RANDOM_GENERATOR_FAILURE); goto err; } } if (out) { gost_init(&cctx,param->sblock); keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key); } gkt = GOST_KEY_TRANSPORT_new(); if (!gkt) { goto memerr; } if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm,8)) { goto memerr; } if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4)) { goto memerr; } if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32)) { goto memerr; } if (key_is_ephemeral) { if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,out?mykey:pubk)) { GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY); goto err; } if (out) EVP_PKEY_free(mykey); } ASN1_OBJECT_free(gkt->key_agreement_info->cipher); gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid); *outlen = i2d_GOST_KEY_TRANSPORT(gkt,out?&out:NULL); if (*outlen <= 0) { GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO); goto err; } if (!key_is_ephemeral) { /* Set control "public key from client certificate used" */ if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <= 0) { GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED); goto err; } } GOST_KEY_TRANSPORT_free(gkt); return 1; memerr: if (key_is_ephemeral) { EVP_PKEY_free(mykey); } GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_MALLOC_FAILURE); err: GOST_KEY_TRANSPORT_free(gkt); return -1; }
/* EVP_PKEY_METHOD callback encrypt for * GOST R 34.10-94 cryptopro modification */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost94_keyx.c#L88-L206
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_GOST94cp_decrypt
int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) { const unsigned char *p = in; GOST_KEY_TRANSPORT *gkt = NULL; unsigned char wrappedKey[44]; unsigned char sharedKey[32]; gost_ctx cctx; const struct gost_cipher_info *param=NULL; EVP_PKEY *eph_key=NULL, *peerkey=NULL; EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx); if (!key) { *key_len = 32; return 1; } gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p, in_len); if (!gkt) { GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO); return 0; } eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key); if (eph_key) { if (EVP_PKEY_derive_set_peer(ctx, eph_key) <= 0) { GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_INCOMPATIBLE_PEER_KEY); goto err; } } else { /* Set control "public key from client certificate used" */ if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <= 0) { GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_CTRL_CALL_FAILED); goto err; } } peerkey = EVP_PKEY_CTX_get0_peerkey(ctx); if (!peerkey) { GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_NO_PEER_KEY); goto err; } param = get_encryption_params(gkt->key_agreement_info->cipher); gost_init(&cctx,param->sblock); OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8); memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8); OPENSSL_assert(gkt->key_info->encrypted_key->length==32); memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32); OPENSSL_assert(gkt->key_info->imit->length==4); memcpy(wrappedKey+40,gkt->key_info->imit->data,4); make_cp_exchange_key(gost_get0_priv_key(priv),peerkey,sharedKey); if (!keyUnwrapCryptoPro(&cctx,sharedKey,wrappedKey,key)) { GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_ERROR_COMPUTING_SHARED_KEY); goto err; } EVP_PKEY_free(eph_key); GOST_KEY_TRANSPORT_free(gkt); return 1; err: EVP_PKEY_free(eph_key); GOST_KEY_TRANSPORT_free(gkt); return -1; }
/* EVP_PLEY_METHOD callback decrypt for * GOST R 34.10-94 cryptopro modification */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost94_keyx.c#L212-L286
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
decode_gost_algor_params
static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) { ASN1_OBJECT *palg_obj =NULL; int ptype = V_ASN1_UNDEF; int pkey_nid = NID_undef,param_nid = NID_undef; void *_pval; ASN1_STRING *pval = NULL; const unsigned char *p; GOST_KEY_PARAMS *gkp = NULL; X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg); pval = _pval; if (ptype != V_ASN1_SEQUENCE) { GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, GOST_R_BAD_KEY_PARAMETERS_FORMAT); return 0; } p=pval->data; pkey_nid = OBJ_obj2nid(palg_obj); gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length); if (!gkp) { GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, GOST_R_BAD_PKEY_PARAMETERS_FORMAT); return 0; } param_nid = OBJ_obj2nid(gkp->key_params); GOST_KEY_PARAMS_free(gkp); EVP_PKEY_set_type(pkey,pkey_nid); switch (pkey_nid) { case NID_id_GostR3410_94: { DSA *dsa= EVP_PKEY_get0(pkey); if (!dsa) { dsa = DSA_new(); if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0; } if (!fill_GOST94_params(dsa,param_nid)) return 0; break; } case NID_id_GostR3410_2001: { EC_KEY *ec = EVP_PKEY_get0(pkey); if (!ec) { ec = EC_KEY_new(); if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0; } if (!fill_GOST2001_params(ec,param_nid)) return 0; } } return 1; }
/* Parses GOST algorithm parameters from X509_ALGOR and * modifies pkey setting NID and parameters */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L89-L146
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_free_gost94
static void pkey_free_gost94(EVP_PKEY *key) { if (key->pkey.dsa) { DSA_free(key->pkey.dsa); } }
/*----------------------- free functions * ------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L255-L261
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
priv_decode_gost
static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf) { const unsigned char *pkey_buf = NULL,*p=NULL; int priv_len = 0; BIGNUM *pk_num=NULL; int ret =0; X509_ALGOR *palg =NULL; ASN1_OBJECT *palg_obj = NULL; ASN1_INTEGER *priv_key=NULL; if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf)) return 0; p = pkey_buf; if (!decode_gost_algor_params(pk,palg)) { return 0; } if (V_ASN1_OCTET_STRING == *p) { /* New format - Little endian octet string */ unsigned char rev_buf[32]; int i; ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len); if (!s||s->length !=32) { GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR); return 0; } for (i=0;i<32;i++) { rev_buf[31-i]=s->data[i]; } ASN1_STRING_free(s); pk_num = getbnfrombuf(rev_buf,32); } else { priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len); if (!priv_key) return 0; ret= ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL))!=NULL) ; ASN1_INTEGER_free(priv_key); if (!ret) { GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR); return 0; } } ret= gost_set_priv_key(pk,pk_num); BN_free(pk_num); return ret; }
/* ------------------ private key functions -----------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L272-L325
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
priv_encode_gost
static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk) { ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk)); ASN1_STRING *params = encode_gost_algor_params(pk); unsigned char *priv_buf = NULL; int priv_len; ASN1_INTEGER *asn1key=NULL; if (!params) { return 0; } asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk),NULL); priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf); ASN1_INTEGER_free(asn1key); return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params, priv_buf,priv_len); }
/* ----------------------------------------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L328-L345
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
print_gost_94
static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx, int type) { int param_nid = NID_undef; if (type == 2) { BIGNUM *key; if (!BIO_indent(out,indent,128)) return 0; BIO_printf(out,"Private key: "); key = gost_get0_priv_key(pkey); if (!key) BIO_printf(out,"<undefined>"); else BN_print(out,key); BIO_printf(out,"\n"); } if (type >= 1) { BIGNUM *pubkey; pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key; BIO_indent(out,indent,128); BIO_printf(out,"Public key: "); BN_print(out,pubkey); BIO_printf(out,"\n"); } param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey)); BIO_indent(out,indent,128); BIO_printf(out, "Parameter set: %s\n",OBJ_nid2ln(param_nid)); return 1; }
/* --------- printing keys --------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L347-L380
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
param_missing_gost94
static int param_missing_gost94(const EVP_PKEY *pk) { const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk); if (!dsa) return 1; if (!dsa->q) return 1; return 0; }
/* ---------------------------------------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L474-L480
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pub_decode_gost94
static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub) { X509_ALGOR *palg = NULL; const unsigned char *pubkey_buf = NULL; unsigned char *databuf; ASN1_OBJECT *palgobj = NULL; int pub_len,i,j; DSA *dsa; ASN1_OCTET_STRING *octet= NULL; if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len, &palg, pub)) return 0; EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL); if (!decode_gost_algor_params(pk,palg)) return 0; octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len); if (!octet) { GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE); return 0; } databuf = OPENSSL_malloc(octet->length); for (i=0,j=octet->length-1;i<octet->length;i++,j--) { databuf[j]=octet->data[i]; } dsa = EVP_PKEY_get0(pk); dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL); ASN1_OCTET_STRING_free(octet); OPENSSL_free(databuf); return 1; }
/* ---------- Public key functions * --------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L569-L600
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
mackey_free_gost
static void mackey_free_gost(EVP_PKEY *pk) { if (pk->pkey.ptr) { OPENSSL_free(pk->pkey.ptr); } }
/*------------------------ ASN1 METHOD for GOST MAC -------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L791-L796
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
register_ameth_gost
int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info) { *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info); if (!*ameth) return 0; switch (nid) { case NID_id_GostR3410_94: EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94); EVP_PKEY_asn1_set_private (*ameth, priv_decode_gost, priv_encode_gost, priv_print_gost94); EVP_PKEY_asn1_set_param (*ameth, gost94_param_decode, gost94_param_encode, param_missing_gost94, param_copy_gost94, param_cmp_gost94,param_print_gost94 ); EVP_PKEY_asn1_set_public (*ameth, pub_decode_gost94, pub_encode_gost94, pub_cmp_gost94, pub_print_gost94, pkey_size_gost, pkey_bits_gost); EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost); break; case NID_id_GostR3410_2001: EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01); EVP_PKEY_asn1_set_private (*ameth, priv_decode_gost, priv_encode_gost, priv_print_gost01); EVP_PKEY_asn1_set_param (*ameth, gost2001_param_decode, gost2001_param_encode, param_missing_gost01, param_copy_gost01, param_cmp_gost01, param_print_gost01); EVP_PKEY_asn1_set_public (*ameth, pub_decode_gost01, pub_encode_gost01, pub_cmp_gost01, pub_print_gost01, pkey_size_gost, pkey_bits_gost); EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost); break; case NID_id_Gost28147_89_MAC: EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost); EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost); break; } return 1; }
/* ----------------------------------------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_ameth.c#L861-L908
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_set_param
static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c,int nid) { const struct gost_cipher_info *param; param=get_encryption_params((nid==NID_undef?NULL:OBJ_nid2obj(nid))); if (!param) return 0; c->paramNID = param->nid; c->key_meshing=param->key_meshing; c->count=0; gost_init(&(c->cctx), param->sblock); return 1; }
/* Sets cipher param from paramset NID. */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L155-L166
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_init_param
static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc, int paramNID,int mode) { struct ossl_gost_cipher_ctx *c=ctx->cipher_data; if (ctx->app_data == NULL) { if (!gost_cipher_set_param(c,paramNID)) return 0; ctx->app_data = ctx->cipher_data; } if (key) gost_key(&(c->cctx),key); if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); return 1; }
/* Initializes EVP_CIPHER_CTX by paramset NID */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L169-L182
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_init
int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc) { return gost_cipher_init_param(ctx,key,iv,enc,NID_undef,EVP_CIPH_CFB_MODE); }
/* Initializes EVP_CIPHER_CTX with default values */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L198-L202
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_crypt_mesh
static void gost_crypt_mesh (void *ctx,unsigned char *iv,unsigned char *buf) { struct ossl_gost_cipher_ctx *c = ctx; if (c->count&&c->key_meshing && c->count%1024==0) { cryptopro_key_meshing(&(c->cctx),iv); } gostcrypt(&(c->cctx),iv,buf); c->count+=8; }
/* Wrapper around gostcrypt function from gost89.c which perform * key meshing when nesseccary */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L206-L215
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_do_cfb
int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { const unsigned char *in_ptr=in; unsigned char *out_ptr=out; size_t i=0; size_t j=0; /* process partial block if any */ if (ctx->num) { for (j=ctx->num,i=0;j<8 && i<inl;j++,i++,in_ptr++,out_ptr++) { if (!ctx->encrypt) ctx->buf[j+8]=*in_ptr; *out_ptr=ctx->buf[j]^(*in_ptr); if (ctx->encrypt) ctx->buf[j+8]=*out_ptr; } if (j==8) { memcpy(ctx->iv,ctx->buf+8,8); ctx->num=0; } else { ctx->num=j; return 1; } } for (;i+8<inl;i+=8,in_ptr+=8,out_ptr+=8) { /*block cipher current iv */ gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf); /*xor next block of input text with it and output it*/ /*output this block */ if (!ctx->encrypt) memcpy(ctx->iv,in_ptr,8); for (j=0;j<8;j++) { out_ptr[j]=ctx->buf[j]^in_ptr[j]; } /* Encrypt */ /* Next iv is next block of cipher text*/ if (ctx->encrypt) memcpy(ctx->iv,out_ptr,8); } /* Process rest of buffer */ if (i<inl) { gost_crypt_mesh(ctx->cipher_data,ctx->iv,ctx->buf); if (!ctx->encrypt) memcpy(ctx->buf+8,in_ptr,inl-i); for (j=0;i<inl;j++,i++) { out_ptr[j]=ctx->buf[j]^in_ptr[j]; } ctx->num = j; if (ctx->encrypt) memcpy(ctx->buf+8,out_ptr,j); } else { ctx->num = 0; } return 1; }
/* GOST encryption in CFB mode */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L255-L315
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_cleanup
int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx) { gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx); ctx->app_data = NULL; return 1; }
/* Cleaning up of EVP_CIPHER_CTX */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L372-L377
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_cipher_ctl
int gost_cipher_ctl(EVP_CIPHER_CTX *ctx,int type,int arg,void *ptr) { switch (type) { case EVP_CTRL_RAND_KEY: { if (RAND_bytes((unsigned char *)ptr,ctx->key_len)<=0) { GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_RANDOM_GENERATOR_ERROR); return -1; } break; } case EVP_CTRL_PBE_PRF_NID: if (ptr) { *((int *)ptr)= NID_id_HMACGostR3411_94; return 1; } else { return 0; } default: GOSTerr(GOST_F_GOST_CIPHER_CTL,GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND); return -1; } return 1; }
/* Control function for gost cipher */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L380-L406
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost89_set_asn1_parameters
int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params) { int len=0; unsigned char *buf=NULL; unsigned char *p=NULL; struct ossl_gost_cipher_ctx *c = ctx->cipher_data; GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new(); ASN1_OCTET_STRING *os = NULL; if (!gcp) { GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY); return 0; } if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) { GOST_CIPHER_PARAMS_free(gcp); GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY); return 0; } ASN1_OBJECT_free(gcp->enc_param_set); gcp->enc_param_set = OBJ_nid2obj(c->paramNID); len = i2d_GOST_CIPHER_PARAMS(gcp, NULL); p = buf = (unsigned char*)OPENSSL_malloc(len); if (!buf) { GOST_CIPHER_PARAMS_free(gcp); GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY); return 0; } i2d_GOST_CIPHER_PARAMS(gcp, &p); GOST_CIPHER_PARAMS_free(gcp); os = ASN1_OCTET_STRING_new(); if(!os || !ASN1_OCTET_STRING_set(os, buf, len)) { OPENSSL_free(buf); GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY); return 0; } OPENSSL_free(buf); ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os); return 1; }
/* Set cipher parameters from ASN1 structure */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L409-L454
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost89_get_asn1_parameters
int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx,ASN1_TYPE *params) { int ret = -1; int len; GOST_CIPHER_PARAMS *gcp = NULL; unsigned char *p; struct ossl_gost_cipher_ctx *c=ctx->cipher_data; if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) { return ret; } p = params->value.sequence->data; gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p, params->value.sequence->length); len = gcp->iv->length; if (len != ctx->cipher->iv_len) { GOST_CIPHER_PARAMS_free(gcp); GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH); return -1; } if (!gost_cipher_set_param(c,OBJ_obj2nid(gcp->enc_param_set))) { GOST_CIPHER_PARAMS_free(gcp); return -1; } memcpy(ctx->oiv, gcp->iv->data, len); GOST_CIPHER_PARAMS_free(gcp); return 1; }
/* Store parameters into ASN1 structure */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L457-L492
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
gost_imit_cleanup
int gost_imit_cleanup(EVP_MD_CTX *ctx) { memset(ctx->md_data,0,sizeof(struct ossl_gost_imit_ctx)); return 1; }
/* Clean up imit ctx */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_crypt.c#L611-L615
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
keyDiversifyCryptoPro
void keyDiversifyCryptoPro(gost_ctx *ctx,const unsigned char *inputKey, const unsigned char *ukm, unsigned char *outputKey) { u4 k,s1,s2; int i,j,mask; unsigned char S[8]; memcpy(outputKey,inputKey,32); for (i=0;i<8;i++) { /* Make array of integers from key */ /* Compute IV S*/ s1=0,s2=0; for (j=0,mask=1;j<8;j++,mask<<=1) { k=((u4)outputKey[4*j])|(outputKey[4*j+1]<<8)| (outputKey[4*j+2]<<16)|(outputKey[4*j+3]<<24); if (mask & ukm[i]) { s1+=k; } else { s2+=k; } } S[0]=(unsigned char)(s1&0xff); S[1]=(unsigned char)((s1>>8)&0xff); S[2]=(unsigned char)((s1>>16)&0xff); S[3]=(unsigned char)((s1>>24)&0xff); S[4]=(unsigned char)(s2&0xff); S[5]=(unsigned char)((s2>>8)&0xff); S[6]=(unsigned char)((s2>>16)&0xff); S[7]=(unsigned char)((s2>>24)&0xff); gost_key(ctx,outputKey); gost_enc_cfb(ctx,S,outputKey,outputKey,4); } }
/* Diversifies key using random UserKey Material * Implements RFC 4357 p 6.5 key diversification algorithm * * inputKey - 32byte key to be diversified * ukm - 8byte user key material * outputKey - 32byte buffer to store diversified key * */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_keywrap.c#L22-L58
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
keyWrapCryptoPro
int keyWrapCryptoPro(gost_ctx *ctx,const unsigned char *keyExchangeKey, const unsigned char *ukm, const unsigned char *sessionKey, unsigned char *wrappedKey) { unsigned char kek_ukm[32]; keyDiversifyCryptoPro(ctx,keyExchangeKey,ukm,kek_ukm); gost_key(ctx,kek_ukm); memcpy(wrappedKey,ukm,8); gost_enc(ctx,sessionKey,wrappedKey+8,4); gost_mac_iv(ctx,32,ukm,sessionKey,32,wrappedKey+40); return 1; }
/* * Wraps key using RFC 4357 6.3 * ctx - gost encryption context, initialized with some S-boxes * keyExchangeKey (KEK) 32-byte (256-bit) shared key * ukm - 8 byte (64 bit) user key material, * sessionKey - 32-byte (256-bit) key to be wrapped * wrappedKey - 44-byte buffer to store wrapped key */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_keywrap.c#L70-L80
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
keyUnwrapCryptoPro
int keyUnwrapCryptoPro(gost_ctx *ctx,const unsigned char *keyExchangeKey, const unsigned char *wrappedKey, unsigned char *sessionKey) { unsigned char kek_ukm[32],cek_mac[4]; keyDiversifyCryptoPro(ctx,keyExchangeKey,wrappedKey /* First 8 bytes of wrapped Key is ukm */ ,kek_ukm); gost_key(ctx,kek_ukm); gost_dec(ctx,wrappedKey+8,sessionKey,4); gost_mac_iv(ctx,32,wrappedKey,sessionKey,32,cek_mac); if (memcmp(cek_mac,wrappedKey+40,4)) { return 0; } return 1; }
/* * Unwraps key using RFC 4357 6.4 * ctx - gost encryption context, initialized with some S-boxes * keyExchangeKey 32-byte shared key * wrappedKey 44 byte key to be unwrapped (concatenation of 8-byte UKM, * 32 byte encrypted key and 4 byte MAC * * sessionKEy - 32byte buffer to store sessionKey in * Returns 1 if key is decrypted successfully, and 0 if MAC doesn't match */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_keywrap.c#L92-L107
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_init
static int pkey_gost_init(EVP_PKEY_CTX *ctx) { struct gost_pmeth_data *data; EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); data = OPENSSL_malloc(sizeof(struct gost_pmeth_data)); if (!data) return 0; memset(data,0,sizeof(struct gost_pmeth_data)); if (pkey && EVP_PKEY_get0(pkey)) { switch (EVP_PKEY_base_id(pkey)) { case NID_id_GostR3410_94: data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey)); break; case NID_id_GostR3410_2001: data->sign_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey))); break; default: return 0; } } EVP_PKEY_CTX_set_data(ctx,data); return 1; }
/*-------init, cleanup, copy - uniform for all algs ---------------*/ /* Allocates new gost_pmeth_data structure and assigns it as data */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L22-L44
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_copy
static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { struct gost_pmeth_data *dst_data,*src_data; if (!pkey_gost_init(dst)) { return 0; } src_data = EVP_PKEY_CTX_get_data(src); dst_data = EVP_PKEY_CTX_get_data(dst); *dst_data = *src_data; if (src_data -> shared_ukm) { dst_data->shared_ukm=NULL; } return 1; }
/* Copies contents of gost_pmeth_data structure */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L47-L61
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_cleanup
static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx) { struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); if (data->shared_ukm) OPENSSL_free(data->shared_ukm); OPENSSL_free(data); }
/* Frees up gost_pmeth_data structure */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L64-L69
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_ctrl
static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx); switch (type) { case EVP_PKEY_CTRL_MD: { if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) { GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE); return 0; } pctx->md = (EVP_MD *)p2; return 1; } break; case EVP_PKEY_CTRL_PKCS7_ENCRYPT: case EVP_PKEY_CTRL_PKCS7_DECRYPT: case EVP_PKEY_CTRL_PKCS7_SIGN: return 1; case EVP_PKEY_CTRL_GOST_PARAMSET: pctx->sign_param_nid = (int)p1; return 1; case EVP_PKEY_CTRL_SET_IV: pctx->shared_ukm=OPENSSL_malloc((int)p1); memcpy(pctx->shared_ukm,p2,(int) p1); return 1; case EVP_PKEY_CTRL_PEER_KEY: if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */ return 1; if (p1 == 2) /* TLS: peer key used? */ return pctx->peer_key_used; if (p1 == 3) /* TLS: peer key used! */ return (pctx->peer_key_used = 1); return -2; } return -2; }
/* --------------------- control functions ------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L72-L111
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_paramgen_init
static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx) { return 1; }
/* --------------------- key generation --------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L263-L265
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost94cp_keygen
static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { DSA *dsa; if (!pkey_gost94_paramgen(ctx,pkey)) return 0; dsa = EVP_PKEY_get0(pkey); gost_sign_keygen(dsa); return 1; }
/* Generates Gost_R3410_94_cp key */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L308-L315
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost01cp_keygen
static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { EC_KEY *ec; if (!pkey_gost01_paramgen(ctx,pkey)) return 0; ec = EVP_PKEY_get0(pkey); gost2001_keygen(ec); return 1; }
/* Generates GOST_R3410 2001 key and assigns it using specified type */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L318-L325
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost94_cp_sign
static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbs_len) { DSA_SIG *unpacked_sig=NULL; EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); if (!siglen) return 0; if (!sig) { *siglen= 64; /* better to check size of pkey->pkey.dsa-q */ return 1; } unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey)); if (!unpacked_sig) { return 0; } return pack_sign_cp(unpacked_sig,32,sig,siglen); }
/* ----------- sign callbacks --------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L331-L348
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost94_cp_verify
static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, const unsigned char *tbs, size_t tbs_len) { int ok = 0; EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx); DSA_SIG *s=unpack_cp_signature(sig,siglen); if (!s) return 0; if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key)); DSA_SIG_free(s); return ok; }
/* ------------------- verify callbacks ---------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L371-L381
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_encrypt_init
static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx) { return 1; }
/* ------------- encrypt init -------------------------------------*/ /* Generates ephermeral key */
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L405-L408
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_derive_init
static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx) { return 1; }
/* --------------- Derive init ------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L410-L413
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
pkey_gost_mac_init
static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx) { struct gost_mac_pmeth_data *data; data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data)); if (!data) return 0; memset(data,0,sizeof(struct gost_mac_pmeth_data)); EVP_PKEY_CTX_set_data(ctx,data); return 1; }
/* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L415-L423
4389085c8ce35cff887a4cc18fc47d1133d89ffb
BigWorld-Engine-14.4.1
github_2023
v2v3v4
c
register_pmeth_gost
int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags) { *pmeth = EVP_PKEY_meth_new(id, flags); if (!*pmeth) return 0; switch (id) { case NID_id_GostR3410_94: EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str); EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen); EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign); EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify); EVP_PKEY_meth_set_encrypt(*pmeth, pkey_gost_encrypt_init, pkey_GOST94cp_encrypt); EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt); EVP_PKEY_meth_set_derive(*pmeth, pkey_gost_derive_init, pkey_gost94_derive); EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost94_paramgen); break; case NID_id_GostR3410_2001: EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str); EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign); EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify); EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen); EVP_PKEY_meth_set_encrypt(*pmeth, pkey_gost_encrypt_init, pkey_GOST01cp_encrypt); EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt); EVP_PKEY_meth_set_derive(*pmeth, pkey_gost_derive_init, pkey_gost2001_derive); EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost01_paramgen); break; case NID_id_Gost28147_89_MAC: EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_mac_ctrl, pkey_gost_mac_ctrl_str); EVP_PKEY_meth_set_signctx(*pmeth,pkey_gost_mac_signctx_init, pkey_gost_mac_signctx); EVP_PKEY_meth_set_keygen(*pmeth,NULL, pkey_gost_mac_keygen); EVP_PKEY_meth_set_init(*pmeth,pkey_gost_mac_init); EVP_PKEY_meth_set_cleanup(*pmeth,pkey_gost_mac_cleanup); EVP_PKEY_meth_set_copy(*pmeth,pkey_gost_mac_copy); return 1; default: /*Unsupported method*/ return 0; } EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init); EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup); EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy); /*FIXME derive etc...*/ return 1; }
/* ----------------------------------------------------------------*/
https://github.com/v2v3v4/BigWorld-Engine-14.4.1/blob/4389085c8ce35cff887a4cc18fc47d1133d89ffb/programming/bigworld/third_party/openssl/engines/ccgost/gost_pmeth.c#L569-L620
4389085c8ce35cff887a4cc18fc47d1133d89ffb