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

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