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

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