Skip to content

Commit 5b69792

Browse files
venkatesh921Yogaraj-Alamenda
authored andcommitted
Add Option to do Lenstra verify using QAT HW instead of OpenSSL SW.
Lenstra verifcation using QAT Hardware when enabled via configure flag "enable-qat_hw_lenstra_verify_hw". Default is to do lenstra verify using OpenSSL SW. Signed-off-by: Yogaraj Alamenda <yogarajx.alamenda@intel.com>
1 parent c8731e1 commit 5b69792

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ AM_CFLAGS = $(cflags) $(cflags_cc_opt) $(cflags_qat_debug_file) \
8181
$(enable_qat_hw_hkdf) $(enable_qat_hw_ecx) \
8282
$(enable_qat_hw_small_pkt_offload) \
8383
$(enable_qat_hw_lenstra_protection) \
84+
$(enable_qat_hw_lenstra_verify_hw) \
8485
$(enable_qat_sw_gcm) $(enable_qat_sw_rsa) \
8586
$(enable_qat_sw_ecx) $(enable_qat_sw_ecdsa) \
8687
$(enable_qat_sw_ecdh) $(enable_qat_sw_heuristic_timeout) \

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,10 @@ Optional
11221122
https://security-center.intel.com/advisory.aspx?intelid=INTEL-SA-00071&languageid=en-fr
11231123
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-5681
11241124
1125+
--enable-qat_hw_lenstra_verify_hw
1126+
Enable Lenstra Verify using QAT HW instead of OpenSSL Software method.
1127+
(disabled by default).
1128+
11251129
--disable-qat_auto_engine_init_on_fork/--enable-qat_auto_engine_init_on_fork
11261130
Disable/Enable the engine from being initialized automatically following a
11271131
fork operation. This is useful in a situation where you want to tightly

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ AC_ARG_ENABLE(qat_hw_lenstra_protection,
207207
[Disable protection against Lenstra attack]))
208208
AC_SUBST(enable_qat_hw_lenstra_protection)
209209

210+
AC_ARG_ENABLE(qat_hw_lenstra_verify_hw,
211+
AS_HELP_STRING([--enable-qat_hw_lenstra_verify_hw],
212+
[Enable Lenstra Verify using QAT HW instead of OpenSSL SW]))
213+
AC_SUBST(enable_qat_hw_lenstra_verify_hw)
214+
210215
AC_ARG_ENABLE(qat_auto_engine_init_on_fork,
211216
AS_HELP_STRING([--disable-qat_auto_engine_init_on_fork],
212217
[Disable auto initialization of the engine following a fork]))
@@ -527,6 +532,15 @@ else
527532
AC_MSG_NOTICE([Lenstra attack protection enabled (default).])
528533
fi
529534

535+
if test "x$enable_qat_hw_lenstra_verify_hw" = "xyes" -a "x$cflags_qat_hw" != "x"
536+
then
537+
if test "x$enable_qat_hw_lenstra_protection" != "xno"
538+
then
539+
enable_qat_hw_lenstra_verify_hw="-DENABLE_QAT_HW_LENSTRA_VERIFY_HW"
540+
AC_MSG_NOTICE([Lenstra check via QAT HW enabled.])
541+
fi
542+
fi
543+
530544
if test "x$enable_qat_debug" = "xyes"
531545
then
532546
enable_qat_debug="-DQAT_DEBUG"

qat_hw_rsa.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ int qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to,
912912
const BIGNUM *n = NULL;
913913
const BIGNUM *e = NULL;
914914
const BIGNUM *d = NULL;
915+
int lenstra_ret = 0;
915916
#endif
916917

917918
DEBUG("- Started.\n");
@@ -978,19 +979,27 @@ int qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to,
978979

979980
/* Note: not checking 'd' as it is not used */
980981
if (e != NULL) { /* then a public key exists and we can effect Lenstra attack protection*/
981-
ver_msg = OPENSSL_zalloc(flen);
982-
if (ver_msg == NULL) {
983-
WARN("ver_msg zalloc failed.\n");
984-
QATerr(QAT_F_QAT_RSA_PRIV_ENC, ERR_R_MALLOC_FAILURE);
985-
sts = 0;
986-
goto exit_lenstra;
987-
}
988-
if ((RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
989-
(rsa_len, (const unsigned char *)to, ver_msg, rsa, padding) <= 0)
990-
|| (CRYPTO_memcmp(from, ver_msg, flen) != 0)) {
991-
WARN("- Verify failed - redoing sign operation in s/w\n");
982+
ver_msg = OPENSSL_zalloc(flen);
983+
if (ver_msg == NULL) {
984+
WARN("ver_msg zalloc failed.\n");
985+
QATerr(QAT_F_QAT_RSA_PRIV_ENC, ERR_R_MALLOC_FAILURE);
986+
sts = 0;
987+
goto exit_lenstra;
988+
}
989+
# ifdef ENABLE_QAT_HW_LENSTRA_VERIFY_HW
990+
lenstra_ret = qat_rsa_pub_dec(rsa_len, (const unsigned char *)to,
991+
ver_msg, rsa, padding);
992+
# else
993+
lenstra_ret = RSA_meth_get_pub_dec(RSA_PKCS1_OpenSSL())
994+
(rsa_len,
995+
(const unsigned char *)to,
996+
ver_msg, rsa, padding);
997+
# endif
998+
if ((lenstra_ret <= 0) || (CRYPTO_memcmp(from, ver_msg, flen) != 0)) {
999+
WARN("QAT RSA Verify failed - redoing sign operation in s/w\n");
9921000
OPENSSL_free(ver_msg);
993-
return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())(flen, from, to, rsa, padding);
1001+
return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
1002+
(flen, from, to, rsa, padding);
9941003
}
9951004
OPENSSL_free(ver_msg);
9961005
}
@@ -1050,6 +1059,7 @@ int qat_rsa_priv_dec(int flen, const unsigned char *from,
10501059
const BIGNUM *n = NULL;
10511060
const BIGNUM *e = NULL;
10521061
const BIGNUM *d = NULL;
1062+
int lenstra_ret = 0;
10531063
#endif
10541064

10551065
DEBUG("- Started.\n");
@@ -1110,10 +1120,18 @@ int qat_rsa_priv_dec(int flen, const unsigned char *from,
11101120
sts = 0;
11111121
goto exit;
11121122
}
1113-
if ((RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
1114-
(rsa_len, (const unsigned char *)output_buffer->pData, ver_msg, rsa, RSA_NO_PADDING) <= 0)
1115-
|| (CRYPTO_memcmp(from, ver_msg, flen) != 0)) {
1116-
WARN("- Verify of offloaded decrypt operation failed - redoing decrypt operation in s/w\n");
1123+
# ifdef ENABLE_QAT_HW_LENSTRA_VERIFY_HW
1124+
lenstra_ret = qat_rsa_pub_enc(rsa_len,
1125+
(const unsigned char *)output_buffer->pData,
1126+
ver_msg, rsa, RSA_NO_PADDING);
1127+
# else
1128+
lenstra_ret = RSA_meth_get_pub_enc(RSA_PKCS1_OpenSSL())
1129+
(rsa_len,
1130+
(const unsigned char *)output_buffer->pData,
1131+
ver_msg, rsa, RSA_NO_PADDING);
1132+
# endif
1133+
if ((lenstra_ret <= 0) || (CRYPTO_memcmp(from, ver_msg, flen) != 0)) {
1134+
WARN("- QAT RSA sign failed - redoing decrypt operation in s/w\n");
11171135
OPENSSL_free(ver_msg);
11181136
rsa_decrypt_op_buf_free(dec_op_data, output_buffer);
11191137
return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())(flen, from, to, rsa, padding);

0 commit comments

Comments
 (0)