 apps/enc.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/apps/enc.c b/apps/enc.c
index 520ee47..7908bf6 100644
--- a/apps/enc.c
+++ b/apps/enc.c
@@ -404,6 +404,9 @@ int enc_main(int argc, char **argv)
     }
 
     if (cipher != NULL) {
+        int wrap = 0;
+        if (EVP_CIPHER_mode(cipher) == EVP_CIPH_WRAP_MODE) wrap = 1;
+
         /*
          * Note that str is NULL if a key was passed on the command line, so
          * we get no salt in that case. Is this a bug?
@@ -466,6 +469,7 @@ int enc_main(int argc, char **argv)
             else
                 OPENSSL_cleanse(str, strlen(str));
         }
+
         if (hiv != NULL) {
             int siz = EVP_CIPHER_iv_length(cipher);
             if (siz == 0) {
@@ -476,7 +480,8 @@ int enc_main(int argc, char **argv)
             }
         }
         if ((hiv == NULL) && (str == NULL)
-            && EVP_CIPHER_iv_length(cipher) != 0) {
+            && EVP_CIPHER_iv_length(cipher) != 0
+            && wrap == 0) {
             /*
              * No IV was explicitly set and no IV was generated during
              * EVP_BytesToKey. Hence the IV is undefined, making correct
@@ -500,6 +505,9 @@ int enc_main(int argc, char **argv)
 
         BIO_get_cipher_ctx(benc, &ctx);
 
+        if (wrap == 1)
+            EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
+
         if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) {
             BIO_printf(bio_err, "Error setting cipher %s\n",
                        EVP_CIPHER_name(cipher));
@@ -510,7 +518,8 @@ int enc_main(int argc, char **argv)
         if (nopad)
             EVP_CIPHER_CTX_set_padding(ctx, 0);
 
-        if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) {
+        if (!EVP_CipherInit_ex(ctx, NULL, NULL, key,
+                               (hiv == NULL && wrap == 1 ? NULL : iv), enc)) {
             BIO_printf(bio_err, "Error setting cipher %s\n",
                        EVP_CIPHER_name(cipher));
             ERR_print_errors(bio_err);
