Blame SOURCES/openssl-1.0.2e-wrap-pad.patch

450916
diff -up openssl-1.0.2e/crypto/evp/c_allc.c.wrap openssl-1.0.2e/crypto/evp/c_allc.c
450916
--- openssl-1.0.2e/crypto/evp/c_allc.c.wrap	2015-12-04 13:33:42.118550036 +0100
450916
+++ openssl-1.0.2e/crypto/evp/c_allc.c	2015-12-04 13:33:42.190551722 +0100
450916
@@ -179,6 +179,7 @@ void OpenSSL_add_all_ciphers(void)
450916
     EVP_add_cipher(EVP_aes_128_xts());
450916
     EVP_add_cipher(EVP_aes_128_ccm());
450916
     EVP_add_cipher(EVP_aes_128_wrap());
450916
+    EVP_add_cipher(EVP_aes_128_wrap_pad());
450916
     EVP_add_cipher_alias(SN_aes_128_cbc, "AES128");
450916
     EVP_add_cipher_alias(SN_aes_128_cbc, "aes128");
450916
     EVP_add_cipher(EVP_aes_192_ecb());
450916
@@ -191,6 +192,7 @@ void OpenSSL_add_all_ciphers(void)
450916
     EVP_add_cipher(EVP_aes_192_gcm());
450916
     EVP_add_cipher(EVP_aes_192_ccm());
450916
     EVP_add_cipher(EVP_aes_192_wrap());
450916
+    EVP_add_cipher(EVP_aes_192_wrap_pad());
450916
     EVP_add_cipher_alias(SN_aes_192_cbc, "AES192");
450916
     EVP_add_cipher_alias(SN_aes_192_cbc, "aes192");
450916
     EVP_add_cipher(EVP_aes_256_ecb());
450916
@@ -204,6 +206,7 @@ void OpenSSL_add_all_ciphers(void)
450916
     EVP_add_cipher(EVP_aes_256_xts());
450916
     EVP_add_cipher(EVP_aes_256_ccm());
450916
     EVP_add_cipher(EVP_aes_256_wrap());
450916
+    EVP_add_cipher(EVP_aes_256_wrap_pad());
450916
     EVP_add_cipher_alias(SN_aes_256_cbc, "AES256");
450916
     EVP_add_cipher_alias(SN_aes_256_cbc, "aes256");
450916
 # if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
450916
@@ -258,6 +261,7 @@ void OpenSSL_add_all_ciphers(void)
450916
 
450916
         EVP_add_cipher(EVP_des_ede());
450916
         EVP_add_cipher(EVP_des_ede3());
450916
+        EVP_add_cipher(EVP_des_ede3_wrap());
450916
 # endif
450916
 
450916
 # ifndef OPENSSL_NO_AES
450916
@@ -272,6 +276,7 @@ void OpenSSL_add_all_ciphers(void)
450916
         EVP_add_cipher(EVP_aes_128_xts());
450916
         EVP_add_cipher(EVP_aes_128_ccm());
450916
         EVP_add_cipher(EVP_aes_128_wrap());
450916
+        EVP_add_cipher(EVP_aes_128_wrap_pad());
450916
         EVP_add_cipher_alias(SN_aes_128_cbc, "AES128");
450916
         EVP_add_cipher_alias(SN_aes_128_cbc, "aes128");
450916
         EVP_add_cipher(EVP_aes_192_ecb());
450916
@@ -284,6 +289,7 @@ void OpenSSL_add_all_ciphers(void)
450916
         EVP_add_cipher(EVP_aes_192_gcm());
450916
         EVP_add_cipher(EVP_aes_192_ccm());
450916
         EVP_add_cipher(EVP_aes_192_wrap());
450916
+        EVP_add_cipher(EVP_aes_192_wrap_pad());
450916
         EVP_add_cipher_alias(SN_aes_192_cbc, "AES192");
450916
         EVP_add_cipher_alias(SN_aes_192_cbc, "aes192");
450916
         EVP_add_cipher(EVP_aes_256_ecb());
450916
@@ -297,6 +303,7 @@ void OpenSSL_add_all_ciphers(void)
450916
         EVP_add_cipher(EVP_aes_256_xts());
450916
         EVP_add_cipher(EVP_aes_256_ccm());
450916
         EVP_add_cipher(EVP_aes_256_wrap());
450916
+        EVP_add_cipher(EVP_aes_256_wrap_pad());
450916
         EVP_add_cipher_alias(SN_aes_256_cbc, "AES256");
450916
         EVP_add_cipher_alias(SN_aes_256_cbc, "aes256");
450916
 # endif
450916
diff -up openssl-1.0.2e/crypto/evp/e_aes.c.wrap openssl-1.0.2e/crypto/evp/e_aes.c
450916
--- openssl-1.0.2e/crypto/evp/e_aes.c.wrap	2015-12-04 13:33:42.119550059 +0100
450916
+++ openssl-1.0.2e/crypto/evp/e_aes.c	2015-12-04 13:33:42.190551722 +0100
450916
@@ -1,5 +1,5 @@
450916
 /* ====================================================================
450916
- * Copyright (c) 2001-2011 The OpenSSL Project.  All rights reserved.
450916
+ * Copyright (c) 2001-2014 The OpenSSL Project.  All rights reserved.
450916
  *
450916
  * Redistribution and use in source and binary forms, with or without
450916
  * modification, are permitted provided that the following conditions
450916
@@ -1953,7 +1953,7 @@ static int aes_wrap_init_key(EVP_CIPHER_
450916
             wctx->iv = NULL;
450916
     }
450916
     if (iv) {
450916
-        memcpy(ctx->iv, iv, 8);
450916
+        memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
450916
         wctx->iv = ctx->iv;
450916
     }
450916
     return 1;
450916
@@ -1964,30 +1964,57 @@ static int aes_wrap_cipher(EVP_CIPHER_CT
450916
 {
450916
     EVP_AES_WRAP_CTX *wctx = ctx->cipher_data;
450916
     size_t rv;
450916
+    /* AES wrap with padding has IV length of 4, without padding 8 */
450916
+    int pad = EVP_CIPHER_CTX_iv_length(ctx) == 4;
450916
+    /* No final operation so always return zero length */
450916
     if (!in)
450916
         return 0;
450916
-    if (inlen % 8)
450916
+    /* Input length must always be non-zero */
450916
+    if (!inlen)
450916
         return -1;
450916
-    if (ctx->encrypt && inlen < 8)
450916
+    /* If decrypting need at least 16 bytes and multiple of 8 */
450916
+    if (!ctx->encrypt && (inlen < 16 || inlen & 0x7))
450916
         return -1;
450916
-    if (!ctx->encrypt && inlen < 16)
450916
+    /* If not padding input must be multiple of 8 */
450916
+    if (!pad && inlen & 0x7)
450916
         return -1;
450916
     if (!out) {
450916
-        if (ctx->encrypt)
450916
+        if (ctx->encrypt) {
450916
+            /* If padding round up to multiple of 8 */
450916
+            if (pad)
450916
+                inlen = (inlen + 7) / 8 * 8;
450916
+            /* 8 byte prefix */
450916
             return inlen + 8;
450916
-        else
450916
+        } else {
450916
+            /* If not padding output will be exactly 8 bytes
450916
+             * smaller than input. If padding it will be at
450916
+             * least 8 bytes smaller but we don't know how
450916
+             * much.
450916
+             */
450916
             return inlen - 8;
450916
     }
450916
+    }
450916
+    if (pad) {
450916
     if (ctx->encrypt)
450916
-        rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
450916
+            rv = CRYPTO_128_wrap_pad(&wctx->ks.ks, wctx->iv,
450916
+                                     out, in, inlen,
450916
                              (block128_f) AES_encrypt);
450916
     else
450916
-        rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv, out, in, inlen,
450916
+            rv = CRYPTO_128_unwrap_pad(&wctx->ks.ks, wctx->iv,
450916
+                                       out, in, inlen,
450916
                                (block128_f) AES_decrypt);
450916
+    } else {
450916
+        if (ctx->encrypt)
450916
+            rv = CRYPTO_128_wrap(&wctx->ks.ks, wctx->iv,
450916
+                                 out, in, inlen, (block128_f) AES_encrypt);
450916
+        else
450916
+            rv = CRYPTO_128_unwrap(&wctx->ks.ks, wctx->iv,
450916
+                                   out, in, inlen, (block128_f) AES_decrypt);
450916
+    }
450916
     return rv ? (int)rv : -1;
450916
 }
450916
 
450916
-#define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE \
450916
+# define WRAP_FLAGS      (EVP_CIPH_WRAP_MODE | EVP_CIPH_FLAG_FIPS \
450916
                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
450916
                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
450916
 
450916
@@ -2032,3 +2059,45 @@ const EVP_CIPHER *EVP_aes_256_wrap(void)
450916
 {
450916
     return &aes_256_wrap;
450916
 }
450916
+
450916
+static const EVP_CIPHER aes_128_wrap_pad = {
450916
+    NID_id_aes128_wrap_pad,
450916
+    8, 16, 4, WRAP_FLAGS,
450916
+    aes_wrap_init_key, aes_wrap_cipher,
450916
+    NULL,
450916
+    sizeof(EVP_AES_WRAP_CTX),
450916
+    NULL, NULL, NULL, NULL
450916
+};
450916
+
450916
+const EVP_CIPHER *EVP_aes_128_wrap_pad(void)
450916
+{
450916
+    return &aes_128_wrap_pad;
450916
+}
450916
+
450916
+static const EVP_CIPHER aes_192_wrap_pad = {
450916
+    NID_id_aes192_wrap_pad,
450916
+    8, 24, 4, WRAP_FLAGS,
450916
+    aes_wrap_init_key, aes_wrap_cipher,
450916
+    NULL,
450916
+    sizeof(EVP_AES_WRAP_CTX),
450916
+    NULL, NULL, NULL, NULL
450916
+};
450916
+
450916
+const EVP_CIPHER *EVP_aes_192_wrap_pad(void)
450916
+{
450916
+    return &aes_192_wrap_pad;
450916
+}
450916
+
450916
+static const EVP_CIPHER aes_256_wrap_pad = {
450916
+    NID_id_aes256_wrap_pad,
450916
+    8, 32, 4, WRAP_FLAGS,
450916
+    aes_wrap_init_key, aes_wrap_cipher,
450916
+    NULL,
450916
+    sizeof(EVP_AES_WRAP_CTX),
450916
+    NULL, NULL, NULL, NULL
450916
+};
450916
+
450916
+const EVP_CIPHER *EVP_aes_256_wrap_pad(void)
450916
+{
450916
+    return &aes_256_wrap_pad;
450916
+}
450916
diff -up openssl-1.0.2e/crypto/evp/e_des3.c.wrap openssl-1.0.2e/crypto/evp/e_des3.c
450916
--- openssl-1.0.2e/crypto/evp/e_des3.c.wrap	2015-12-04 13:33:42.119550059 +0100
450916
+++ openssl-1.0.2e/crypto/evp/e_des3.c	2015-12-04 13:33:42.191551745 +0100
450916
@@ -474,7 +474,7 @@ static const EVP_CIPHER des3_wrap = {
450916
     NID_id_smime_alg_CMS3DESwrap,
450916
     8, 24, 0,
450916
     EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
450916
-        | EVP_CIPH_FLAG_DEFAULT_ASN1,
450916
+        | EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_FLAG_FIPS,
450916
     des_ede3_init_key, des_ede3_wrap_cipher,
450916
     NULL,
450916
     sizeof(DES_EDE_KEY),
450916
diff -up openssl-1.0.2e/crypto/evp/evp.h.wrap openssl-1.0.2e/crypto/evp/evp.h
450916
--- openssl-1.0.2e/crypto/evp/evp.h.wrap	2015-12-04 13:33:42.120550083 +0100
450916
+++ openssl-1.0.2e/crypto/evp/evp.h	2015-12-04 13:33:42.191551745 +0100
450916
@@ -834,6 +834,7 @@ const EVP_CIPHER *EVP_aes_128_ccm(void);
450916
 const EVP_CIPHER *EVP_aes_128_gcm(void);
450916
 const EVP_CIPHER *EVP_aes_128_xts(void);
450916
 const EVP_CIPHER *EVP_aes_128_wrap(void);
450916
+const EVP_CIPHER *EVP_aes_128_wrap_pad(void);
450916
 const EVP_CIPHER *EVP_aes_192_ecb(void);
450916
 const EVP_CIPHER *EVP_aes_192_cbc(void);
450916
 const EVP_CIPHER *EVP_aes_192_cfb1(void);
450916
@@ -845,6 +846,7 @@ const EVP_CIPHER *EVP_aes_192_ctr(void);
450916
 const EVP_CIPHER *EVP_aes_192_ccm(void);
450916
 const EVP_CIPHER *EVP_aes_192_gcm(void);
450916
 const EVP_CIPHER *EVP_aes_192_wrap(void);
450916
+const EVP_CIPHER *EVP_aes_192_wrap_pad(void);
450916
 const EVP_CIPHER *EVP_aes_256_ecb(void);
450916
 const EVP_CIPHER *EVP_aes_256_cbc(void);
450916
 const EVP_CIPHER *EVP_aes_256_cfb1(void);
450916
@@ -857,6 +859,7 @@ const EVP_CIPHER *EVP_aes_256_ccm(void);
450916
 const EVP_CIPHER *EVP_aes_256_gcm(void);
450916
 const EVP_CIPHER *EVP_aes_256_xts(void);
450916
 const EVP_CIPHER *EVP_aes_256_wrap(void);
450916
+const EVP_CIPHER *EVP_aes_256_wrap_pad(void);
450916
 #  if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
450916
 const EVP_CIPHER *EVP_aes_128_cbc_hmac_sha1(void);
450916
 const EVP_CIPHER *EVP_aes_256_cbc_hmac_sha1(void);
450916
diff -up openssl-1.0.2e/crypto/evp/evptests.txt.wrap openssl-1.0.2e/crypto/evp/evptests.txt
450916
--- openssl-1.0.2e/crypto/evp/evptests.txt.wrap	2015-12-03 15:04:23.000000000 +0100
450916
+++ openssl-1.0.2e/crypto/evp/evptests.txt	2015-12-04 13:33:42.191551745 +0100
450916
@@ -399,3 +399,7 @@ id-aes256-wrap:000102030405060708090A0B0
450916
 id-aes192-wrap:000102030405060708090A0B0C0D0E0F1011121314151617::00112233445566778899AABBCCDDEEFF0001020304050607:031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2
450916
 id-aes256-wrap:000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F::00112233445566778899AABBCCDDEEFF0001020304050607:A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1
450916
 id-aes256-wrap:000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F::00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F:28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21
450916
+# AES wrap tests from RFC5649
450916
+id-aes192-wrap-pad:5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8::c37b7e6492584340bed12207808941155068f738:138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a
450916
+id-aes192-wrap-pad:5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8::466f7250617369:afbeb0f07dfbf5419200f2ccb50bb24f
450916
+
450916
diff -up openssl-1.0.2e/crypto/modes/modes.h.wrap openssl-1.0.2e/crypto/modes/modes.h
450916
--- openssl-1.0.2e/crypto/modes/modes.h.wrap	2015-12-04 13:33:41.770541886 +0100
450916
+++ openssl-1.0.2e/crypto/modes/modes.h	2015-12-04 13:33:42.191551745 +0100
450916
@@ -157,6 +157,12 @@ size_t CRYPTO_128_unwrap(void *key, cons
450916
                          unsigned char *out,
450916
                          const unsigned char *in, size_t inlen,
450916
                          block128_f block);
450916
+size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv,
450916
+                           unsigned char *out, const unsigned char *in,
450916
+                           size_t inlen, block128_f block);
450916
+size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv,
450916
+                             unsigned char *out, const unsigned char *in,
450916
+                             size_t inlen, block128_f block);
450916
 
450916
 #ifdef  __cplusplus
450916
 }
450916
diff -up openssl-1.0.2e/crypto/modes/wrap128.c.wrap openssl-1.0.2e/crypto/modes/wrap128.c
450916
--- openssl-1.0.2e/crypto/modes/wrap128.c.wrap	2015-12-03 15:04:23.000000000 +0100
450916
+++ openssl-1.0.2e/crypto/modes/wrap128.c	2015-12-04 13:37:51.486366984 +0100
450916
@@ -2,6 +2,7 @@
450916
 /*
450916
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
450916
  * project.
450916
+ * Mode with padding contributed by Petr Spacek (pspacek@redhat.com).
450916
  */
450916
 /* ====================================================================
450916
  * Copyright (c) 2013 The OpenSSL Project.  All rights reserved.
450916
@@ -52,19 +53,44 @@
450916
  * ====================================================================
450916
  */
450916
 
450916
+/**  Beware!
450916
+ *
450916
+ *  Following wrapping modes were designed for AES but this implementation
450916
+ *  allows you to use them for any 128 bit block cipher.
450916
+ */
450916
+
450916
 #include "cryptlib.h"
450916
 #include <openssl/modes.h>
450916
 
450916
+/** RFC 3394 section 2.2.3.1 Default Initial Value */
450916
 static const unsigned char default_iv[] = {
450916
     0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
450916
 };
450916
 
450916
-/*
450916
- * Input size limit: lower than maximum of standards but far larger than
450916
+/** RFC 5649 section 3 Alternative Initial Value 32-bit constant */
450916
+static const unsigned char default_aiv[] = {
450916
+    0xA6, 0x59, 0x59, 0xA6
450916
+};
450916
+
450916
+/** Input size limit: lower than maximum of standards but far larger than
450916
  * anything that will be used in practice.
450916
  */
450916
 #define CRYPTO128_WRAP_MAX (1UL << 31)
450916
 
450916
+/** Wrapping according to RFC 3394 section 2.2.1.
450916
+ *
450916
+ *  @param[in]  key    Key value. 
450916
+ *  @param[in]  iv     IV value. Length = 8 bytes. NULL = use default_iv.
450916
+ *  @param[in]  in     Plain text as n 64-bit blocks, n >= 2.
450916
+ *  @param[in]  inlen  Length of in.
450916
+ *  @param[out] out    Cipher text. Minimal buffer length = (inlen + 8) bytes.
450916
+ *                     Input and output buffers can overlap if block function
450916
+ *                     supports that.
450916
+ *  @param[in]  block  Block processing function.
450916
+ *  @return            0 if inlen does not consist of n 64-bit blocks, n >= 2.
450916
+ *                     or if inlen > CRYPTO128_WRAP_MAX.
450916
+ *                     Output length if wrapping succeeded.
450916
+ */
450916
 size_t CRYPTO_128_wrap(void *key, const unsigned char *iv,
450916
                        unsigned char *out,
450916
                        const unsigned char *in, size_t inlen,
450916
@@ -72,7 +98,7 @@ size_t CRYPTO_128_wrap(void *key, const
450916
 {
450916
     unsigned char *A, B[16], *R;
450916
     size_t i, j, t;
450916
-    if ((inlen & 0x7) || (inlen < 8) || (inlen > CRYPTO128_WRAP_MAX))
450916
+    if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX))
450916
         return 0;
450916
     A = B;
450916
     t = 1;
450916
@@ -100,7 +126,23 @@ size_t CRYPTO_128_wrap(void *key, const
450916
     return inlen + 8;
450916
 }
450916
 
450916
-size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv,
450916
+/** Unwrapping according to RFC 3394 section 2.2.2 steps 1-2.
450916
+ *  IV check (step 3) is responsibility of the caller.
450916
+ *
450916
+ *  @param[in]  key    Key value. 
450916
+ *  @param[out] iv     Unchecked IV value. Minimal buffer length = 8 bytes.
450916
+ *  @param[out] out    Plain text without IV.
450916
+ *                     Minimal buffer length = (inlen - 8) bytes.
450916
+ *                     Input and output buffers can overlap if block function
450916
+ *                     supports that.
450916
+ *  @param[in]  in     Ciphertext text as n 64-bit blocks
450916
+ *  @param[in]  inlen  Length of in.
450916
+ *  @param[in]  block  Block processing function.
450916
+ *  @return            0 if inlen is out of range [24, CRYPTO128_WRAP_MAX]
450916
+ *                     or if inlen is not multiply of 8.
450916
+ *                     Output length otherwise.
450916
+ */
450916
+static size_t crypto_128_unwrap_raw(void *key, unsigned char *iv,
450916
                          unsigned char *out,
450916
                          const unsigned char *in, size_t inlen,
450916
                          block128_f block)
450916
@@ -128,11 +170,190 @@ size_t CRYPTO_128_unwrap(void *key, cons
450916
             memcpy(R, B + 8, 8);
450916
         }
450916
     }
450916
+    memcpy(iv, A, 8);
450916
+    return inlen;
450916
+}
450916
+
450916
+/** Unwrapping according to RFC 3394 section 2.2.2 including IV check.
450916
+ *  First block of plain text have to match supplied IV otherwise an error is
450916
+ *  returned.
450916
+ *
450916
+ *  @param[in]  key    Key value. 
450916
+ *  @param[out] iv     Unchecked IV value. Minimal buffer length = 8 bytes.
450916
+ *  @param[out] out    Plain text without IV.
450916
+ *                     Minimal buffer length = (inlen - 8) bytes.
450916
+ *                     Input and output buffers can overlap if block function
450916
+ *                     supports that.
450916
+ *  @param[in]  in     Ciphertext text as n 64-bit blocks
450916
+ *  @param[in]  inlen  Length of in.
450916
+ *  @param[in]  block  Block processing function.
450916
+ *  @return            0 if inlen is out of range [24, CRYPTO128_WRAP_MAX]
450916
+ *                     or if inlen is not multiply of 8
450916
+ *                     or if IV doesn't match expected value.
450916
+ *                     Output length otherwise.
450916
+ */
450916
+size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv,
450916
+                         unsigned char *out, const unsigned char *in,
450916
+                         size_t inlen, block128_f block)
450916
+{
450916
+    size_t ret;
450916
+    unsigned char got_iv[8];
450916
+
450916
+    ret = crypto_128_unwrap_raw(key, got_iv, out, in, inlen, block);
450916
+    if (ret == 0)
450916
+        return 0;
450916
+
450916
     if (!iv)
450916
         iv = default_iv;
450916
-    if (memcmp(A, iv, 8)) {
450916
+    if (CRYPTO_memcmp(got_iv, iv, 8)) {
450916
+        OPENSSL_cleanse(out, ret);
450916
+        return 0;
450916
+    }
450916
+    return ret;
450916
+}
450916
+
450916
+/** Wrapping according to RFC 5649 section 4.1.
450916
+ *
450916
+ *  @param[in]  key    Key value. 
450916
+ *  @param[in]  icv    (Non-standard) IV, 4 bytes. NULL = use default_aiv.
450916
+ *  @param[out] out    Cipher text. Minimal buffer length = (inlen + 15) bytes.
450916
+ *                     Input and output buffers can overlap if block function
450916
+ *                     supports that.
450916
+ *  @param[in]  in     Plain text as n 64-bit blocks, n >= 2.
450916
+ *  @param[in]  inlen  Length of in.
450916
+ *  @param[in]  block  Block processing function.
450916
+ *  @return            0 if inlen is out of range [1, CRYPTO128_WRAP_MAX].
450916
+ *                     Output length if wrapping succeeded.
450916
+ */
450916
+size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv,
450916
+                           unsigned char *out,
450916
+                           const unsigned char *in, size_t inlen,
450916
+                           block128_f block)
450916
+{
450916
+    /* n: number of 64-bit blocks in the padded key data */
450916
+    const size_t blocks_padded = (inlen + 7) / 8;
450916
+    const size_t padded_len = blocks_padded * 8;
450916
+    const size_t padding_len = padded_len - inlen;
450916
+    /* RFC 5649 section 3: Alternative Initial Value */
450916
+    unsigned char aiv[8];
450916
+    int ret;
450916
+
450916
+    /* Section 1: use 32-bit fixed field for plaintext octet length */
450916
+    if (inlen == 0 || inlen >= CRYPTO128_WRAP_MAX)
450916
+        return 0;
450916
+
450916
+    /* Section 3: Alternative Initial Value */
450916
+    if (!icv)
450916
+        memcpy(aiv, default_aiv, 4);
450916
+    else
450916
+        memcpy(aiv, icv, 4);    /* Standard doesn't mention this. */
450916
+
450916
+    aiv[4] = (inlen >> 24) & 0xFF;
450916
+    aiv[5] = (inlen >> 16) & 0xFF;
450916
+    aiv[6] = (inlen >> 8) & 0xFF;
450916
+    aiv[7] = inlen & 0xFF;
450916
+
450916
+    if (padded_len == 8) {
450916
+        /* Section 4.1 - special case in step 2:
450916
+         * If the padded plaintext contains exactly eight octets, then
450916
+         * prepend the AIV and encrypt the resulting 128-bit block
450916
+         * using AES in ECB mode. */
450916
+        memmove(out + 8, in, inlen);
450916
+        memcpy(out, aiv, 8);
450916
+        memset(out + 8 + inlen, 0, padding_len);
450916
+        block(out, out, key);
450916
+        ret = 16;               /* AIV + padded input */
450916
+    } else {
450916
+        memmove(out, in, inlen);
450916
+        memset(out + inlen, 0, padding_len); /* Section 4.1 step 1 */
450916
+        ret = CRYPTO_128_wrap(key, aiv, out, out, padded_len, block);
450916
+    }
450916
+
450916
+    return ret;
450916
+}
450916
+
450916
+/** Unwrapping according to RFC 5649 section 4.2.
450916
+ *
450916
+ *  @param[in]  key    Key value. 
450916
+ *  @param[in]  icv    (Non-standard) IV, 4 bytes. NULL = use default_aiv.
450916
+ *  @param[out] out    Plain text. Minimal buffer length = inlen bytes.
450916
+ *                     Input and output buffers can overlap if block function
450916
+ *                     supports that.
450916
+ *  @param[in]  in     Ciphertext text as n 64-bit blocks
450916
+ *  @param[in]  inlen  Length of in.
450916
+ *  @param[in]  block  Block processing function.
450916
+ *  @return            0 if inlen is out of range [16, CRYPTO128_WRAP_MAX],
450916
+ *                     or if inlen is not multiply of 8
450916
+ *                     or if IV and message length indicator doesn't match.
450916
+ *                     Output length if unwrapping succeeded and IV matches.
450916
+ */
450916
+size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv,
450916
+                             unsigned char *out,
450916
+                             const unsigned char *in, size_t inlen,
450916
+                             block128_f block)
450916
+{
450916
+    /* n: number of 64-bit blocks in the padded key data */
450916
+    size_t n = inlen / 8 - 1;
450916
+    size_t padded_len;
450916
+    size_t padding_len;
450916
+    size_t ptext_len;
450916
+    /* RFC 5649 section 3: Alternative Initial Value */
450916
+    unsigned char aiv[8];
450916
+    static unsigned char zeros[8] = { 0x0 };
450916
+    size_t ret;
450916
+
450916
+    /* Section 4.2: Cipher text length has to be (n+1) 64-bit blocks. */
450916
+    if ((inlen & 0x7) != 0 || inlen < 16 || inlen >= CRYPTO128_WRAP_MAX)
450916
+        return 0;
450916
+
450916
+    memmove(out, in, inlen);
450916
+    if (inlen == 16) {
450916
+        /* Section 4.2 - special case in step 1:
450916
+         * When n=1, the ciphertext contains exactly two 64-bit
450916
+         * blocks and they are decrypted as a single AES
450916
+         * block using AES in ECB mode:
450916
+         * AIV | P[1] = DEC(K, C[0] | C[1])
450916
+         */
450916
+        block(out, out, key);
450916
+        memcpy(aiv, out, 8);
450916
+        /* Remove AIV */
450916
+        memmove(out, out + 8, 8);
450916
+        padded_len = 8;
450916
+    } else {
450916
+        padded_len = inlen - 8;
450916
+        ret = crypto_128_unwrap_raw(key, aiv, out, out, inlen, block);
450916
+        if (padded_len != ret) {
450916
         OPENSSL_cleanse(out, inlen);
450916
         return 0;
450916
     }
450916
-    return inlen;
450916
+    }
450916
+
450916
+    /* Section 3: AIV checks: Check that MSB(32,A) = A65959A6.
450916
+     * Optionally a user-supplied value can be used
450916
+     * (even if standard doesn't mention this). */
450916
+    if ((!icv && CRYPTO_memcmp(aiv, default_aiv, 4))
450916
+        || (icv && CRYPTO_memcmp(aiv, icv, 4))) {
450916
+        OPENSSL_cleanse(out, inlen);
450916
+        return 0;
450916
+    }
450916
+
450916
+    /* Check that 8*(n-1) < LSB(32,AIV) <= 8*n.
450916
+     * If so, let ptext_len = LSB(32,AIV). */
450916
+
450916
+    ptext_len = (aiv[4] << 24) | (aiv[5] << 16) | (aiv[6] << 8) | aiv[7];
450916
+    if (8 * (n - 1) >= ptext_len || ptext_len > 8 * n) {
450916
+        OPENSSL_cleanse(out, inlen);
450916
+        return 0;
450916
+    }
450916
+
450916
+    /* Check that the rightmost padding_len octets of the output data
450916
+     * are zero. */
450916
+    padding_len = padded_len - ptext_len;
450916
+    if (CRYPTO_memcmp(out + ptext_len, zeros, padding_len) != 0) {
450916
+        OPENSSL_cleanse(out, inlen);
450916
+        return 0;
450916
+    }
450916
+
450916
+    /* Section 4.2 step 3: Remove padding */
450916
+    return ptext_len;
450916
 }