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

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