[PATCH] Extend OAEP support

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Extend OAEP support

Stefan Strogin
Provide methods: EVP_PKEY_CTX_{g,s}et_rsa_oaep_md,
EVP_PKEY_CTX_{g,s}et0_rsa_oaep_label.

Based on Stephen Henson's patches for OpenSSL 1.1.0:
https://github.com/openssl/openssl/commit/271fef0ef39a1c0cb5233a5adf3ff8733abb375e
https://github.com/openssl/openssl/commit/211a14f6279f127f7a5a59948819bd939131b0b6
---
 src/lib/libcrypto/rsa/rsa.h       |  31 ++++++++
 src/lib/libcrypto/rsa/rsa_err.c   |   1 +
 src/lib/libcrypto/rsa/rsa_oaep.c  | 117 +++++++++++++++++------------
 src/lib/libcrypto/rsa/rsa_pmeth.c | 121 ++++++++++++++++++++++++++++--
 4 files changed, 218 insertions(+), 52 deletions(-)

diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
index 2aa472f50..afa995e5d 100644
--- a/src/lib/libcrypto/rsa/rsa.h
+++ b/src/lib/libcrypto/rsa/rsa.h
@@ -230,6 +230,22 @@ struct rsa_st {
  EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_SIG,  \
  EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)pmd)
 
+#define EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md) \
+ EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \
+ EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md))
+
+#define EVP_PKEY_CTX_get_rsa_oaep_md(ctx, pmd) \
+ EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \
+ EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)(pmd))
+
+#define EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, l, llen) \
+ EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \
+ EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen, (void *)(l))
+
+#define EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, l) \
+ EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, \
+ EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, (void *)(l))
+
 #define EVP_PKEY_CTRL_RSA_PADDING (EVP_PKEY_ALG_CTRL + 1)
 #define EVP_PKEY_CTRL_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 2)
 
@@ -241,6 +257,12 @@ struct rsa_st {
 #define EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN (EVP_PKEY_ALG_CTRL + 7)
 #define EVP_PKEY_CTRL_GET_RSA_MGF1_MD (EVP_PKEY_ALG_CTRL + 8)
 
+#define EVP_PKEY_CTRL_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 9)
+#define EVP_PKEY_CTRL_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 10)
+
+#define EVP_PKEY_CTRL_GET_RSA_OAEP_MD (EVP_PKEY_ALG_CTRL + 11)
+#define EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL (EVP_PKEY_ALG_CTRL + 12)
+
 #define RSA_PKCS1_PADDING 1
 #define RSA_SSLV23_PADDING 2
 #define RSA_NO_PADDING 3
@@ -368,6 +390,14 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
 int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
     const unsigned char *f, int fl, int rsa_len,
     const unsigned char *p, int pl);
+int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+    const unsigned char *from, int flen,
+    const unsigned char *param, int plen,
+    const EVP_MD *md, const EVP_MD *mgf1md);
+int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+    const unsigned char *from, int flen, int num,
+    const unsigned char *param, int plen,
+    const EVP_MD *md, const EVP_MD *mgf1md);
 int RSA_padding_add_none(unsigned char *to, int tlen,
     const unsigned char *f, int fl);
 int RSA_padding_check_none(unsigned char *to, int tlen,
@@ -574,6 +604,7 @@ void ERR_load_RSA_strings(void);
 #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
 #define RSA_R_FIRST_OCTET_INVALID 133
 #define RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE 144
+#define RSA_R_INVALID_DIGEST 157
 #define RSA_R_INVALID_DIGEST_LENGTH 143
 #define RSA_R_INVALID_HEADER 137
 #define RSA_R_INVALID_KEYBITS 145
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
index c2b197c58..c7e7470f3 100644
--- a/src/lib/libcrypto/rsa/rsa_err.c
+++ b/src/lib/libcrypto/rsa/rsa_err.c
@@ -96,6 +96,7 @@ static ERR_STRING_DATA RSA_str_reasons[] = {
  {ERR_REASON(RSA_R_D_E_NOT_CONGRUENT_TO_1), "d e not congruent to 1"},
  {ERR_REASON(RSA_R_FIRST_OCTET_INVALID)   , "first octet invalid"},
  {ERR_REASON(RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE), "illegal or unsupported padding mode"},
+ {ERR_REASON(RSA_R_INVALID_DIGEST)        , "invalid digest"},
  {ERR_REASON(RSA_R_INVALID_DIGEST_LENGTH) , "invalid digest length"},
  {ERR_REASON(RSA_R_INVALID_HEADER)        , "invalid header"},
  {ERR_REASON(RSA_R_INVALID_KEYBITS)       , "invalid keybits"},
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c
index c463a885d..30e8353d7 100644
--- a/src/lib/libcrypto/rsa/rsa_oaep.c
+++ b/src/lib/libcrypto/rsa/rsa_oaep.c
@@ -32,63 +32,75 @@
 #include <openssl/rsa.h>
 #include <openssl/sha.h>
 
-static int MGF1(unsigned char *mask, long len, const unsigned char *seed,
-    long seedlen);
-
 int
 RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
     const unsigned char *from, int flen, const unsigned char *param, int plen)
+{
+ return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
+    param, plen, NULL, NULL);
+}
+
+int
+RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+    const unsigned char *from, int flen, const unsigned char *param, int plen,
+    const EVP_MD *md, const EVP_MD *mgf1md)
 {
  int i, emlen = tlen - 1;
  unsigned char *db, *seed;
- unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
+ unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
+ int mdlen, dbmasklen = 0;
+
+ if (md == NULL)
+ md = EVP_sha1();
+ if (mgf1md == NULL)
+ mgf1md = md;
 
- if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) {
+ mdlen = EVP_MD_size(md);
+
+ if (flen > emlen - 2 * mdlen - 1) {
  RSAerror(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  return 0;
  }
 
- if (emlen < 2 * SHA_DIGEST_LENGTH + 1) {
+ if (emlen < 2 * mdlen + 1) {
  RSAerror(RSA_R_KEY_SIZE_TOO_SMALL);
  return 0;
  }
 
  to[0] = 0;
  seed = to + 1;
- db = to + SHA_DIGEST_LENGTH + 1;
+ db = to + mdlen + 1;
 
- if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
+ if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
  return 0;
- memset(db + SHA_DIGEST_LENGTH, 0,
-    emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
- db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
- memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, flen);
- arc4random_buf(seed, SHA_DIGEST_LENGTH);
+ memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
+ db[emlen - flen - mdlen - 1] = 0x01;
+ memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
+ arc4random_buf(seed, mdlen);
 
- dbmask = malloc(emlen - SHA_DIGEST_LENGTH);
+ dbmasklen = emlen - mdlen;
+ dbmask = malloc(dbmasklen);
  if (dbmask == NULL) {
  RSAerror(ERR_R_MALLOC_FAILURE);
  return 0;
  }
 
- if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed,
-    SHA_DIGEST_LENGTH) < 0) {
- free(dbmask);
- return 0;
- }
- for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
+ if (PKCS1_MGF1(dbmask, dbmasklen, seed, mdlen, mgf1md) < 0)
+ goto err;
+ for (i = 0; i < dbmasklen; i++)
  db[i] ^= dbmask[i];
 
- if (MGF1(seedmask, SHA_DIGEST_LENGTH, db,
-    emlen - SHA_DIGEST_LENGTH) < 0) {
- free(dbmask);
- return 0;
- }
- for (i = 0; i < SHA_DIGEST_LENGTH; i++)
+ if (PKCS1_MGF1(seedmask, mdlen, db, dbmasklen, mgf1md) < 0)
+ goto err;
+ for (i = 0; i < mdlen; i++)
  seed[i] ^= seedmask[i];
 
- free(dbmask);
+ freezero(dbmask, dbmasklen);
  return 1;
+
+err:
+ freezero(dbmask, dbmasklen);
+ return 0;
 }
 
 int
@@ -96,15 +108,34 @@ RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
     const unsigned char *from, int flen, int num, const unsigned char *param,
     int plen)
 {
- int i, dblen, mlen = -1;
+ return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
+    param, plen, NULL, NULL);
+}
+
+int
+RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+                                      const unsigned char *from, int flen,
+                                      int num, const unsigned char *param,
+                                      int plen, const EVP_MD *md,
+                                      const EVP_MD *mgf1md)
+{
+ int i, dblen = 0, mlen = -1;
  const unsigned char *maskeddb;
  int lzero;
  unsigned char *db = NULL;
- unsigned char seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
+ unsigned char seed[EVP_MAX_MD_SIZE], phash[EVP_MAX_MD_SIZE];
  unsigned char *padded_from;
  int bad = 0;
+ int mdlen;
+
+ if (md == NULL)
+ md = EVP_sha1();
+ if (mgf1md == NULL)
+ mgf1md = md;
+
+ mdlen = EVP_MD_size(md);
 
- if (--num < 2 * SHA_DIGEST_LENGTH + 1)
+ if (--num < 2 * mdlen + 1)
  /*
  * 'num' is the length of the modulus, i.e. does not depend
  * on the particular ciphertext.
@@ -125,7 +156,7 @@ RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  flen = num; /* don't overflow the memcpy to padded_from */
  }
 
- dblen = num - SHA_DIGEST_LENGTH;
+ dblen = num - mdlen;
  if ((db = malloc(dblen + num)) == NULL) {
  RSAerror(ERR_R_MALLOC_FAILURE);
  return -1;
@@ -139,25 +170,25 @@ RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  memset(padded_from, 0, lzero);
  memcpy(padded_from + lzero, from, flen);
 
- maskeddb = padded_from + SHA_DIGEST_LENGTH;
+ maskeddb = padded_from + mdlen;
 
- if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
+ if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
  goto err;
- for (i = 0; i < SHA_DIGEST_LENGTH; i++)
+ for (i = 0; i < mdlen; i++)
  seed[i] ^= padded_from[i];
 
- if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
+ if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
  goto err;
  for (i = 0; i < dblen; i++)
  db[i] ^= maskeddb[i];
 
- if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
+ if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
  goto err;
 
- if (timingsafe_memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
+ if (timingsafe_memcmp(db, phash, mdlen) != 0 || bad)
  goto decoding_err;
  else {
- for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
+ for (i = mdlen; i < dblen; i++)
  if (db[i] != 0x00)
  break;
  if (i == dblen || db[i] != 0x01)
@@ -173,7 +204,7 @@ RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  memcpy(to, db + i, mlen);
  }
  }
- free(db);
+ freezero(db, dblen);
  return mlen;
 
  decoding_err:
@@ -183,7 +214,7 @@ RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
  */
  RSAerror(RSA_R_OAEP_DECODING_ERROR);
  err:
- free(db);
+ freezero(db, dblen);
  return -1;
 }
 
@@ -227,10 +258,4 @@ PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed,
  EVP_MD_CTX_cleanup(&c);
  return rv;
 }
-
-static int
-MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen)
-{
- return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
-}
 #endif
diff --git a/src/lib/libcrypto/rsa/rsa_pmeth.c b/src/lib/libcrypto/rsa/rsa_pmeth.c
index ea6401b3d..113ae0916 100644
--- a/src/lib/libcrypto/rsa/rsa_pmeth.c
+++ b/src/lib/libcrypto/rsa/rsa_pmeth.c
@@ -68,6 +68,7 @@
 #include <openssl/evp.h>
 #include <openssl/rsa.h>
 #include <openssl/x509.h>
+#include <openssl/x509v3.h>
 
 
 #include "evp_locl.h"
@@ -87,10 +88,13 @@ typedef struct {
  const EVP_MD *md;
  /* message digest for MGF1 */
  const EVP_MD *mgf1md;
- /* PSS/OAEP salt length */
+ /* PSS salt length */
  int saltlen;
  /* Temp buffer */
  unsigned char *tbuf;
+ /* OAEP label */
+ unsigned char *oaep_label;
+ size_t oaep_labellen;
 } RSA_PKEY_CTX;
 
 static int
@@ -110,6 +114,9 @@ pkey_rsa_init(EVP_PKEY_CTX *ctx)
 
  rctx->saltlen = -2;
 
+ rctx->oaep_label = NULL;
+ rctx->oaep_labellen = 0;
+
  ctx->data = rctx;
  ctx->keygen_info = rctx->gentmp;
  ctx->keygen_info_count = 2;
@@ -134,6 +141,16 @@ pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  }
  dctx->pad_mode = sctx->pad_mode;
  dctx->md = sctx->md;
+ dctx->mgf1md = sctx->mgf1md;
+ dctx->saltlen = sctx->saltlen;
+ if (sctx->oaep_label) {
+ free(dctx->oaep_label);
+ memcpy(dctx->oaep_label, sctx->oaep_label,
+    sctx->oaep_labellen);
+ if (!dctx->oaep_label)
+ return 0;
+ dctx->oaep_labellen = sctx->oaep_labellen;
+ }
  return 1;
 }
 
@@ -156,6 +173,7 @@ pkey_rsa_cleanup(EVP_PKEY_CTX *ctx)
  if (rctx) {
  BN_free(rctx->pub_exp);
  free(rctx->tbuf);
+ free(rctx->oaep_label);
  free(rctx);
  }
 }
@@ -309,8 +327,20 @@ pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  int ret;
  RSA_PKEY_CTX *rctx = ctx->data;
 
- ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
-    rctx->pad_mode);
+ if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
+ int klen = RSA_size(ctx->pkey->pkey.rsa);
+ if (!setup_tbuf(rctx, ctx))
+ return -1;
+ if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
+    in, inlen, rctx->oaep_label, rctx->oaep_labellen,
+    rctx->md, rctx->mgf1md))
+ return -1;
+ ret = RSA_public_encrypt(klen, rctx->tbuf, out,
+    ctx->pkey->pkey.rsa, RSA_NO_PADDING);
+ } else {
+ ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
+    rctx->pad_mode);
+ }
  if (ret < 0)
  return ret;
  *outlen = ret;
@@ -324,8 +354,20 @@ pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  int ret;
  RSA_PKEY_CTX *rctx = ctx->data;
 
- ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
-    rctx->pad_mode);
+ if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
+ if (!setup_tbuf(rctx, ctx))
+ return -1;
+ ret = RSA_private_decrypt(inlen, in, rctx->tbuf,
+    ctx->pkey->pkey.rsa, RSA_NO_PADDING);
+ if (ret <= 0)
+ return ret;
+ ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf,
+    ret, ret, rctx->oaep_label, rctx->oaep_labellen,
+    rctx->md, rctx->mgf1md);
+ } else {
+ ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,
+    rctx->pad_mode);
+ }
  if (ret < 0)
  return ret;
  *outlen = ret;
@@ -417,6 +459,18 @@ bad_pad:
  rctx->pub_exp = p2;
  return 1;
 
+ case EVP_PKEY_CTRL_RSA_OAEP_MD:
+ case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
+ if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
+ RSAerror(RSA_R_INVALID_PADDING_MODE);
+ return -2;
+ }
+ if (type == EVP_PKEY_CTRL_GET_RSA_OAEP_MD)
+ *(const EVP_MD **)p2 = rctx->md;
+ else
+ rctx->md = p2;
+ return 1;
+
  case EVP_PKEY_CTRL_MD:
  if (!check_padding_md(p2, rctx->pad_mode))
  return 0;
@@ -425,7 +479,8 @@ bad_pad:
 
  case EVP_PKEY_CTRL_RSA_MGF1_MD:
  case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
- if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) {
+ if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING &&
+    rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
  RSAerror(RSA_R_INVALID_MGF1_MD);
  return -2;
  }
@@ -438,6 +493,29 @@ bad_pad:
  rctx->mgf1md = p2;
  return 1;
 
+ case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
+ if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
+ RSAerror(RSA_R_INVALID_PADDING_MODE);
+ return -2;
+ }
+ free(rctx->oaep_label);
+ if (p2 && p1 > 0) {
+ rctx->oaep_label = p2;
+ rctx->oaep_labellen = p1;
+ } else {
+ rctx->oaep_label = NULL;
+ rctx->oaep_labellen = 0;
+ }
+ return 1;
+
+ case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
+ if (rctx->pad_mode != RSA_PKCS1_OAEP_PADDING) {
+ RSAerror(RSA_R_INVALID_PADDING_MODE);
+ return -2;
+ }
+ *(unsigned char **)p2 = rctx->oaep_label;
+ return rctx->oaep_labellen;
+
  case EVP_PKEY_CTRL_DIGESTINIT:
  case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
  case EVP_PKEY_CTRL_PKCS7_DECRYPT:
@@ -525,6 +603,37 @@ pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
  return ret;
  }
 
+ if (!strcmp(type, "rsa_mgf1_md")) {
+ const EVP_MD *md;
+ if (!(md = EVP_get_digestbyname(value))) {
+ RSAerror(RSA_R_INVALID_DIGEST);
+ return 0;
+ }
+ return EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, md);
+ }
+
+ if (!strcmp(type, "rsa_oaep_md")) {
+ const EVP_MD *md;
+ if (!(md = EVP_get_digestbyname(value))) {
+ RSAerror(RSA_R_INVALID_DIGEST);
+ return 0;
+ }
+ return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md);
+ }
+
+ if (!strcmp(type, "rsa_oaep_label")) {
+ unsigned char *lab;
+ long lablen;
+ int ret;
+ lab = string_to_hex(value, &lablen);
+ if (!lab)
+ return 0;
+ ret = EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, lab, lablen);
+ if (ret <= 0)
+ free(lab);
+ return ret;
+ }
+
 not_a_number:
 out_of_range:
  return -2;
--
2.23.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Extend OAEP support

kinichiro inoguchi
I thought this patch could give an ability to handle OAEP label with
openssl(1) pkeyutl command, and encryption works fine, but decryption fails.
----------------------------------------------------------------------
openssl genrsa -out rsakey.pem

echo "abcd" | openssl pkeyutl -encrypt -inkey rsakey.pem \
    -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_label:0011223344556677 \
    -out rsaoaep.enc

openssl pkeyutl -decrypt -inkey rsakey.pem
    -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_label:0011223344556677 \
    -in rsaoaep.enc
----------------------------------------------------------------------

Last command fails with this message.
----------------------------------------------------------------------
Public Key operation error
2798804220048:error:04FFF079:rsa routines:CRYPTO_internal:oaep decoding error:rsa/rsa_oaep.c:215:
----------------------------------------------------------------------

These commands had succeeded with OpenSSL 1.0.2.
RSA_padding_check_PKCS1_OAEP_mgf1() appears to cause this.


On Wed, Sep 04, 2019 at 06:41:21AM +0300, Stefan Strogin wrote:
> Provide methods: EVP_PKEY_CTX_{g,s}et_rsa_oaep_md,
> EVP_PKEY_CTX_{g,s}et0_rsa_oaep_label.
>
> Based on Stephen Henson's patches for OpenSSL 1.1.0:
> https://github.com/openssl/openssl/commit/271fef0ef39a1c0cb5233a5adf3ff8733abb375e
> https://github.com/openssl/openssl/commit/211a14f6279f127f7a5a59948819bd939131b0b6