+
+#ifndef OPENSSL_NO_DH
+ for (testnum = 0; testnum < FFDH_NUM; testnum++) {
+ int ffdh_checks = 1;
+
+ if (!ffdh_doit[testnum])
+ continue;
+
+ for (i = 0; i < loopargs_len; i++) {
+ EVP_PKEY *pkey_A = NULL;
+ EVP_PKEY *pkey_B = NULL;
+ EVP_PKEY_CTX *ffdh_ctx = NULL;
+ EVP_PKEY_CTX *test_ctx = NULL;
+ size_t secret_size;
+ size_t test_out;
+
+ /* Ensure that the error queue is empty */
+ if (ERR_peek_error()) {
+ BIO_printf(bio_err,
+ "WARNING: the error queue contains previous unhandled errors.\n");
+ ERR_print_errors(bio_err);
+ }
+
+ pkey_A = EVP_PKEY_new();
+ if (!pkey_A) {
+ BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ pkey_B = EVP_PKEY_new();
+ if (!pkey_B) {
+ BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
+ if (!ffdh_ctx) {
+ BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
+ BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
+ BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
+ EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
+ BIO_printf(bio_err, "FFDH key generation failure.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ EVP_PKEY_CTX_free(ffdh_ctx);
+
+ /* check if the derivation works correctly both ways so that
+ * we know if future derive calls will fail, and we can skip
+ * error checking in benchmarked code */
+ ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
+ if (!ffdh_ctx) {
+ BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
+ BIO_printf(bio_err, "FFDH derivation context init failure.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
+ BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
+ BIO_printf(bio_err, "Checking size of shared secret failed.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (secret_size > MAX_FFDH_SIZE) {
+ BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (EVP_PKEY_derive(ffdh_ctx,
+ loopargs[i].secret_ff_a,
+ &secret_size) <= 0) {
+ BIO_printf(bio_err, "Shared secret derive failure.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ /* Now check from side B */
+ test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
+ if (!test_ctx) {
+ BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+ if (!EVP_PKEY_derive_init(test_ctx) ||
+ !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
+ !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
+ !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
+ test_out != secret_size) {
+ BIO_printf(bio_err, "FFDH computation failure.\n");
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ /* compare the computed secrets */
+ if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
+ loopargs[i].secret_ff_b, secret_size)) {
+ BIO_printf(bio_err, "FFDH computations don't match.\n");
+ ERR_print_errors(bio_err);
+ rsa_count = 1;
+ ffdh_checks = 0;
+ break;
+ }
+
+ loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
+
+ EVP_PKEY_free(pkey_A);
+ pkey_A = NULL;
+ EVP_PKEY_free(pkey_B);
+ pkey_B = NULL;
+ EVP_PKEY_CTX_free(test_ctx);
+ test_ctx = NULL;
+ }
+ if (ffdh_checks != 0) {
+ pkey_print_message("", "ffdh", ffdh_c[testnum][0],
+ ffdh_params[testnum].bits, seconds.ffdh);
+ Time_F(START);
+ count =
+ run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
+ d = Time_F(STOP);
+ BIO_printf(bio_err,
+ mr ? "+R12:%ld:%d:%.2f\n" :
+ "%ld %u-bits FFDH ops in %.2fs\n", count,
+ ffdh_params[testnum].bits, d);
+ ffdh_results[testnum][0] = (double)count / d;
+ rsa_count = count;
+ };
+ if (rsa_count <= 1) {
+ /* if longer than 10s, don't do any more */
+ stop_it(ffdh_doit, testnum);
+ }
+ }
+#endif /* OPENSSL_NO_DH */