2016-05-18 02:51:04 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2008-12-27 21:40:45 +08:00
|
|
|
*
|
2018-12-06 20:46:12 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2008-12-27 21:40:45 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2019-07-16 07:46:14 +08:00
|
|
|
#include <openssl/crypto.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/modes.h"
|
2008-12-27 21:40:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Trouble with Ciphertext Stealing, CTS, mode is that there is no
|
|
|
|
* common official specification, but couple of cipher/application
|
|
|
|
* specific ones: RFC2040 and RFC3962. Then there is 'Proposal to
|
|
|
|
* Extend CBC Mode By "Ciphertext Stealing"' at NIST site, which
|
|
|
|
* deviates from mentioned RFCs. Most notably it allows input to be
|
|
|
|
* of block length and it doesn't flip the order of the last two
|
|
|
|
* blocks. CTS is being discussed even in ECB context, but it's not
|
2010-04-10 22:01:02 +08:00
|
|
|
* adopted for any known application. This implementation provides
|
|
|
|
* two interfaces: one compliant with above mentioned RFCs and one
|
|
|
|
* compliant with the NIST proposal, both extending CBC mode.
|
2008-12-27 21:40:45 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t CRYPTO_cts128_encrypt_block(const unsigned char *in,
|
|
|
|
unsigned char *out, size_t len,
|
|
|
|
const void *key, unsigned char ivec[16],
|
|
|
|
block128_f block)
|
|
|
|
{
|
|
|
|
size_t residue, n;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len <= 16)
|
|
|
|
return 0;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((residue = len % 16) == 0)
|
|
|
|
residue = 16;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
in += len;
|
|
|
|
out += len;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (n = 0; n < residue; ++n)
|
|
|
|
ivec[n] ^= in[n];
|
|
|
|
(*block) (ivec, ivec, key);
|
|
|
|
memcpy(out, out - 16, residue);
|
|
|
|
memcpy(out - 16, ivec, 16);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return len + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in,
|
|
|
|
unsigned char *out, size_t len,
|
|
|
|
const void *key,
|
|
|
|
unsigned char ivec[16],
|
|
|
|
block128_f block)
|
|
|
|
{
|
|
|
|
size_t residue, n;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len < 16)
|
|
|
|
return 0;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
residue = len % 16;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (residue == 0)
|
|
|
|
return len;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
in += len;
|
|
|
|
out += len;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (n = 0; n < residue; ++n)
|
|
|
|
ivec[n] ^= in[n];
|
|
|
|
(*block) (ivec, ivec, key);
|
|
|
|
memcpy(out - 16 + residue, ivec, 16);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return len + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
}
|
|
|
|
|
2008-12-27 21:40:45 +08:00
|
|
|
size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t len, const void *key,
|
|
|
|
unsigned char ivec[16], cbc128_f cbc)
|
|
|
|
{
|
|
|
|
size_t residue;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[16];
|
|
|
|
} tmp;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len <= 16)
|
|
|
|
return 0;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((residue = len % 16) == 0)
|
|
|
|
residue = 16;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (in, out, len, key, ivec, 1);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
in += len;
|
|
|
|
out += len;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
|
|
|
#if defined(CBC_HANDLES_TRUNCATED_IO)
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(tmp.c, out - 16, 16);
|
|
|
|
(*cbc) (in, out - 16, residue, key, ivec, 1);
|
|
|
|
memcpy(out, tmp.c, residue);
|
2008-12-27 21:40:45 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
memset(tmp.c, 0, sizeof(tmp));
|
|
|
|
memcpy(tmp.c, in, residue);
|
|
|
|
memcpy(out, out - 16, residue);
|
|
|
|
(*cbc) (tmp.c, out - 16, 16, key, ivec, 1);
|
2008-12-27 21:40:45 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
return len + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
}
|
|
|
|
|
2010-04-10 22:01:02 +08:00
|
|
|
size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out,
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t len, const void *key,
|
|
|
|
unsigned char ivec[16], cbc128_f cbc)
|
|
|
|
{
|
|
|
|
size_t residue;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[16];
|
|
|
|
} tmp;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len < 16)
|
|
|
|
return 0;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
residue = len % 16;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (in, out, len, key, ivec, 1);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (residue == 0)
|
|
|
|
return len;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
in += len;
|
|
|
|
out += len;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
|
|
|
#if defined(CBC_HANDLES_TRUNCATED_IO)
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (in, out - 16 + residue, residue, key, ivec, 1);
|
2010-04-10 22:01:02 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
memset(tmp.c, 0, sizeof(tmp));
|
|
|
|
memcpy(tmp.c, in, residue);
|
|
|
|
(*cbc) (tmp.c, out - 16 + residue, 16, key, ivec, 1);
|
2010-04-10 22:01:02 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
return len + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t CRYPTO_cts128_decrypt_block(const unsigned char *in,
|
|
|
|
unsigned char *out, size_t len,
|
|
|
|
const void *key, unsigned char ivec[16],
|
|
|
|
block128_f block)
|
|
|
|
{
|
|
|
|
size_t residue, n;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[32];
|
|
|
|
} tmp;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len <= 16)
|
|
|
|
return 0;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((residue = len % 16) == 0)
|
|
|
|
residue = 16;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= 16 + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len) {
|
|
|
|
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
|
|
|
|
in += len;
|
|
|
|
out += len;
|
|
|
|
}
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
(*block) (in, tmp.c + 16, key);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(tmp.c, tmp.c + 16, 16);
|
|
|
|
memcpy(tmp.c, in + 16, residue);
|
|
|
|
(*block) (tmp.c, tmp.c, key);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (n = 0; n < 16; ++n) {
|
|
|
|
unsigned char c = in[n];
|
|
|
|
out[n] = tmp.c[n] ^ ivec[n];
|
|
|
|
ivec[n] = c;
|
|
|
|
}
|
|
|
|
for (residue += 16; n < residue; ++n)
|
|
|
|
out[n] = tmp.c[n] ^ in[n];
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 16 + len + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in,
|
|
|
|
unsigned char *out, size_t len,
|
|
|
|
const void *key,
|
|
|
|
unsigned char ivec[16],
|
|
|
|
block128_f block)
|
|
|
|
{
|
|
|
|
size_t residue, n;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[32];
|
|
|
|
} tmp;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len < 16)
|
|
|
|
return 0;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
residue = len % 16;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (residue == 0) {
|
|
|
|
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
|
|
|
|
return len;
|
|
|
|
}
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= 16 + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len) {
|
|
|
|
CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block);
|
|
|
|
in += len;
|
|
|
|
out += len;
|
|
|
|
}
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
(*block) (in + residue, tmp.c + 16, key);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(tmp.c, tmp.c + 16, 16);
|
|
|
|
memcpy(tmp.c, in, residue);
|
|
|
|
(*block) (tmp.c, tmp.c, key);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (n = 0; n < 16; ++n) {
|
|
|
|
unsigned char c = in[n];
|
|
|
|
out[n] = tmp.c[n] ^ ivec[n];
|
|
|
|
ivec[n] = in[n + residue];
|
|
|
|
tmp.c[n] = c;
|
|
|
|
}
|
|
|
|
for (residue += 16; n < residue; ++n)
|
|
|
|
out[n] = tmp.c[n] ^ tmp.c[n - 16];
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 16 + len + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t len, const void *key,
|
|
|
|
unsigned char ivec[16], cbc128_f cbc)
|
|
|
|
{
|
|
|
|
size_t residue;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[32];
|
|
|
|
} tmp;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len <= 16)
|
|
|
|
return 0;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((residue = len % 16) == 0)
|
|
|
|
residue = 16;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= 16 + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len) {
|
|
|
|
(*cbc) (in, out, len, key, ivec, 0);
|
|
|
|
in += len;
|
|
|
|
out += len;
|
|
|
|
}
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memset(tmp.c, 0, sizeof(tmp));
|
|
|
|
/*
|
|
|
|
* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0]
|
|
|
|
*/
|
|
|
|
(*cbc) (in, tmp.c, 16, key, tmp.c + 16, 0);
|
2008-12-27 21:40:45 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(tmp.c, in + 16, residue);
|
2008-12-27 21:40:45 +08:00
|
|
|
#if defined(CBC_HANDLES_TRUNCATED_IO)
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (tmp.c, out, 16 + residue, key, ivec, 0);
|
2008-12-27 21:40:45 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (tmp.c, tmp.c, 32, key, ivec, 0);
|
|
|
|
memcpy(out, tmp.c, 16 + residue);
|
2008-12-27 21:40:45 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
return 16 + len + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out,
|
2015-01-22 11:40:55 +08:00
|
|
|
size_t len, const void *key,
|
|
|
|
unsigned char ivec[16], cbc128_f cbc)
|
|
|
|
{
|
|
|
|
size_t residue;
|
|
|
|
union {
|
|
|
|
size_t align;
|
|
|
|
unsigned char c[32];
|
|
|
|
} tmp;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len < 16)
|
|
|
|
return 0;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
residue = len % 16;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (residue == 0) {
|
|
|
|
(*cbc) (in, out, len, key, ivec, 0);
|
|
|
|
return len;
|
|
|
|
}
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
len -= 16 + residue;
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len) {
|
|
|
|
(*cbc) (in, out, len, key, ivec, 0);
|
|
|
|
in += len;
|
|
|
|
out += len;
|
|
|
|
}
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memset(tmp.c, 0, sizeof(tmp));
|
|
|
|
/*
|
|
|
|
* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0]
|
|
|
|
*/
|
|
|
|
(*cbc) (in + residue, tmp.c, 16, key, tmp.c + 16, 0);
|
2010-04-10 22:01:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
memcpy(tmp.c, in, residue);
|
2010-04-10 22:01:02 +08:00
|
|
|
#if defined(CBC_HANDLES_TRUNCATED_IO)
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (tmp.c, out, 16 + residue, key, ivec, 0);
|
2010-04-10 22:01:02 +08:00
|
|
|
#else
|
2015-01-22 11:40:55 +08:00
|
|
|
(*cbc) (tmp.c, tmp.c, 32, key, ivec, 0);
|
|
|
|
memcpy(out, tmp.c, 16 + residue);
|
2010-04-10 22:01:02 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
return 16 + len + residue;
|
2008-12-27 21:40:45 +08:00
|
|
|
}
|