static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx) { int to_return = 0; int r_len = 0; AEP_CONNECTION_HNDL hConnection; AEP_RV rv; r_len = BN_num_bits(m); /* Perform in software if modulus is too large for hardware. */ if (r_len > max_key_len) { AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL); return BN_mod_exp(r, a, p, m, ctx); } /* * Grab a connection from the pool */ rv = aep_get_connection(&hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_GET_HANDLE_FAILED); return BN_mod_exp(r, a, p, m, ctx); } /* * To the card with the mod exp */ rv = p_AEP_ModExp(hConnection, (void *)a, (void *)p, (void *)m, (void *)r, NULL); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_MOD_EXP_FAILED); rv = aep_close_connection(hConnection); return BN_mod_exp(r, a, p, m, ctx); } /* * Return the connection to the pool */ rv = aep_return_connection(hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_RETURN_CONNECTION_FAILED); goto err; } to_return = 1; err: return to_return; }
static AEP_RV aep_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) { AEP_RV rv = AEP_R_OK; AEP_CONNECTION_HNDL hConnection; /* * Grab a connection from the pool */ rv = aep_get_connection(&hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT, AEPHK_R_GET_HANDLE_FAILED); return FAIL_TO_SW; } /* * To the card with the mod exp */ rv = p_AEP_ModExpCrt(hConnection, (void *)a, (void *)p, (void *)q, (void *)dmp1, (void *)dmq1, (void *)iqmp, (void *)r, NULL); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT, AEPHK_R_MOD_EXP_CRT_FAILED); rv = aep_close_connection(hConnection); return FAIL_TO_SW; } /* * Return the connection to the pool */ rv = aep_return_connection(hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT, AEPHK_R_RETURN_CONNECTION_FAILED); goto err; } err: return rv; }
static int aep_rand(unsigned char *buf,int len ) { AEP_RV rv = AEP_R_OK; AEP_CONNECTION_HNDL hConnection; CRYPTO_w_lock(CRYPTO_LOCK_RAND); /*Can the request be serviced with what's already in the buffer?*/ if (len <= rand_block_bytes) { memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len); rand_block_bytes -= len; CRYPTO_w_unlock(CRYPTO_LOCK_RAND); } else /*If not the get another block of random bytes*/ { CRYPTO_w_unlock(CRYPTO_LOCK_RAND); rv = aep_get_connection(&hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED); goto err_nounlock; } if (len > RAND_BLK_SIZE) { rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED); goto err_nounlock; } } else { CRYPTO_w_lock(CRYPTO_LOCK_RAND); rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED); goto err; } rand_block_bytes = RAND_BLK_SIZE; memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len); rand_block_bytes -= len; CRYPTO_w_unlock(CRYPTO_LOCK_RAND); } rv = aep_return_connection(hConnection); if (rv != AEP_R_OK) { AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); goto err_nounlock; } } return 1; err: CRYPTO_w_unlock(CRYPTO_LOCK_RAND); err_nounlock: return 0; }