400-eng_devcrypto-save-ioctl-if-EVP_MD_.FLAG_ONESHOT.patch 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <[email protected]>
  3. Date: Mon, 5 Nov 2018 15:54:17 -0200
  4. Subject: eng_devcrypto: save ioctl if EVP_MD_..FLAG_ONESHOT
  5. Since each ioctl causes a context switch, slowing things down, if
  6. EVP_MD_CTX_FLAG_ONESHOT is set, then:
  7. - call the ioctl in digest_update, saving the result; and
  8. - just copy the result in digest_final, instead of using another ioctl.
  9. Signed-off-by: Eneas U de Queiroz <[email protected]>
  10. Reviewed-by: Matthias St. Pierre <[email protected]>
  11. Reviewed-by: Richard Levitte <[email protected]>
  12. (Merged from https://github.com/openssl/openssl/pull/7585)
  13. --- a/crypto/engine/eng_devcrypto.c
  14. +++ b/crypto/engine/eng_devcrypto.c
  15. @@ -461,6 +461,7 @@ struct digest_ctx {
  16. struct session_op sess;
  17. /* This signals that the init function was called, not that it succeeded. */
  18. int init_called;
  19. + unsigned char digest_res[HASH_MAX_LEN];
  20. };
  21. static const struct digest_data_st {
  22. @@ -564,12 +565,15 @@ static int digest_update(EVP_MD_CTX *ctx
  23. if (digest_ctx == NULL)
  24. return 0;
  25. - if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) < 0) {
  26. - SYSerr(SYS_F_IOCTL, errno);
  27. - return 0;
  28. + if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  29. + if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0)
  30. + return 1;
  31. + } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) {
  32. + return 1;
  33. }
  34. - return 1;
  35. + SYSerr(SYS_F_IOCTL, errno);
  36. + return 0;
  37. }
  38. static int digest_final(EVP_MD_CTX *ctx, unsigned char *md)
  39. @@ -579,7 +583,10 @@ static int digest_final(EVP_MD_CTX *ctx,
  40. if (md == NULL || digest_ctx == NULL)
  41. return 0;
  42. - if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  43. +
  44. + if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) {
  45. + memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_size(ctx));
  46. + } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) {
  47. SYSerr(SYS_F_IOCTL, errno);
  48. return 0;
  49. }