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