/* ====================================================================
* Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
#include <openssl/opensslconf.h>
#ifndef OPENSSL_NO_AES
# include <string.h>
# include <assert.h>
# include "evp_locl.h"
# include "modes_lcl.h"
typedef struct {
union {
double align;
} ks;
union {
} stream;
} EVP_AES_KEY;
typedef struct {
union {
double align;
int taglen;
typedef struct {
union {
double align;
const unsigned char iv[16]);
typedef struct {
union {
double align;
int L, M; /* L and M parameters from RFC3610 */
# ifdef VPAES_ASM
void vpaes_cbc_encrypt(const unsigned char *in,
unsigned char *out,
# endif
# ifdef BSAES_ASM
const unsigned char ivec[16]);
# endif
# ifdef AES_CTR_ASM
const unsigned char ivec[AES_BLOCK_SIZE]);
# endif
# ifdef AES_XTS_ASM
const unsigned char iv[16]);
const unsigned char iv[16]);
# endif
# include "ppc_arch.h"
# ifdef VPAES_ASM
# endif
# endif
defined(_M_IX86)) && defined(OPENSSL_IA32_SSE2))|| \
defined(__x86_64) || defined(__x86_64__) || \
defined(__INTEL__) )
extern unsigned int OPENSSL_ia32cap_P[];
# ifdef VPAES_ASM
# endif
# ifdef BSAES_ASM
# endif
/*
* AES-NI section
*/
void aesni_ecb_encrypt(const unsigned char *in,
unsigned char *out,
void aesni_cbc_encrypt(const unsigned char *in,
unsigned char *out,
void aesni_ctr32_encrypt_blocks(const unsigned char *in,
unsigned char *out,
void aesni_xts_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char iv[16]);
void aesni_xts_decrypt(const unsigned char *in,
unsigned char *out,
const unsigned char iv[16]);
void aesni_ccm64_encrypt_blocks(const unsigned char *in,
unsigned char *out,
const void *key,
const unsigned char ivec[16],
unsigned char cmac[16]);
void aesni_ccm64_decrypt_blocks(const unsigned char *in,
unsigned char *out,
const void *key,
const unsigned char ivec[16],
unsigned char cmac[16]);
unsigned char *out,
unsigned char *out,
# endif
{
&& !enc) {
} else {
if (mode == EVP_CIPH_CBC_MODE)
else if (mode == EVP_CIPH_CTR_MODE)
else
}
if (ret < 0) {
return 0;
}
return 1;
}
{
return 1;
}
{
return 1;
return 1;
}
{
return 1;
if (key) {
/*
* If we have an iv can set it directly, otherwise use saved IV.
*/
if (iv) {
}
} else {
/* If key set use IV, otherwise copy */
else
}
return 1;
}
{
return 1;
if (key) {
/* key_len is two AES keys */
if (enc) {
} else {
}
}
if (iv) {
}
return 1;
}
{
return 1;
if (key) {
}
if (iv) {
}
return 1;
}
NULL, \
sizeof(EVP_AES_KEY), \
aes_init_key, \
NULL, \
sizeof(EVP_AES_KEY), \
# include "sparc_arch.h"
extern unsigned int OPENSSL_sparcv9cap_P[];
/*
* Key-length specific subroutines were chosen for following reason.
* Each SPARC T4 core can execute up to 8 threads which share core's
* resources. Loading as much key material to registers allows to
* minimize references to shared memory interface, as well as amount
* of instructions in inner loops [much needed on T4]. But then having
* non-key-length specific routines would require conditional branches
* either in inner loops or on subroutines' entries. Former is hardly
* acceptable, while latter means code size increase to size occupied
* by multiple key-length specfic subroutines, so why fight?
*/
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
unsigned char *ivec);
{
&& !enc) {
ret = 0;
switch (bits) {
case 128:
break;
case 192:
break;
case 256:
break;
default:
ret = -1;
}
} else {
ret = 0;
switch (bits) {
case 128:
if (mode == EVP_CIPH_CBC_MODE)
else if (mode == EVP_CIPH_CTR_MODE)
else
break;
case 192:
if (mode == EVP_CIPH_CBC_MODE)
else if (mode == EVP_CIPH_CTR_MODE)
else
break;
case 256:
if (mode == EVP_CIPH_CBC_MODE)
else if (mode == EVP_CIPH_CTR_MODE)
else
break;
default:
ret = -1;
}
}
if (ret < 0) {
return 0;
}
return 1;
}
{
return 1;
if (key) {
switch (bits) {
case 128:
break;
case 192:
break;
case 256:
break;
default:
return 0;
}
/*
* If we have an iv can set it directly, otherwise use saved IV.
*/
if (iv) {
}
} else {
/* If key set use IV, otherwise copy */
else
}
return 1;
}
{
return 1;
if (key) {
/* key_len is two AES keys */
if (enc) {
switch (bits) {
case 128:
break;
# if 0 /* not yet */
case 192:
break;
# endif
case 256:
break;
default:
return 0;
}
} else {
switch (bits) {
case 128:
break;
# if 0 /* not yet */
case 192:
break;
# endif
case 256:
break;
default:
return 0;
}
}
}
if (iv) {
}
return 1;
}
{
return 1;
if (key) {
# if 0 /* not yet */
switch (bits) {
case 128:
break;
case 192:
break;
case 256:
break;
default:
return 0;
}
# else
# endif
}
if (iv) {
}
return 1;
}
NULL, \
sizeof(EVP_AES_KEY), \
aes_init_key, \
NULL, \
sizeof(EVP_AES_KEY), \
# else
aes_init_key, \
NULL, \
sizeof(EVP_AES_KEY), \
# endif
# include "arm_arch.h"
# if __ARM_MAX_ARCH__>=7
# if defined(BSAES_ASM)
# endif
# endif
# endif
# if defined(HWAES_CAPABLE)
const unsigned char ivec[16]);
# endif
{
&& !enc)
# ifdef HWAES_CAPABLE
if (HWAES_CAPABLE) {
# ifdef HWAES_cbc_encrypt
if (mode == EVP_CIPH_CBC_MODE)
# endif
} else
# endif
# ifdef BSAES_CAPABLE
} else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
} else
# endif
{
} else
# ifdef HWAES_CAPABLE
if (HWAES_CAPABLE) {
# ifdef HWAES_cbc_encrypt
if (mode == EVP_CIPH_CBC_MODE)
else
# endif
# ifdef HWAES_ctr32_encrypt_blocks
if (mode == EVP_CIPH_CTR_MODE)
else
# endif
(void)0; /* terminate potentially open 'else' */
} else
# endif
# ifdef BSAES_CAPABLE
} else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
} else
# endif
{
# ifdef AES_CTR_ASM
if (mode == EVP_CIPH_CTR_MODE)
# endif
}
if (ret < 0) {
return 0;
}
return 1;
}
{
return 1;
else
return 1;
}
{
size_t i;
return 1;
return 1;
}
{
return 1;
}
{
return 1;
}
{
return 1;
}
{
return 1;
}
while (len >= MAXBITCHUNK) {
len -= MAXBITCHUNK;
}
if (len)
return 1;
}
{
else
return 1;
}
{
return 1;
}
/* increment counter (64-bit int) by 1 */
{
int n = 8;
unsigned char c;
do {
--n;
c = counter[n];
++c;
counter[n] = c;
if (c)
return;
} while (n);
}
{
switch (type) {
case EVP_CTRL_INIT:
return 1;
case EVP_CTRL_GCM_SET_IVLEN:
if (arg <= 0)
return 0;
/* Allocate memory for IV if needed */
return 0;
}
return 1;
case EVP_CTRL_GCM_SET_TAG:
return 0;
return 1;
case EVP_CTRL_GCM_GET_TAG:
return 0;
return 1;
/* Special case: -1 length restores whole IV */
if (arg == -1) {
return 1;
}
/*
* Fixed field must be at least 4 bytes and invocation field at least
* 8.
*/
return 0;
if (arg)
return 0;
return 1;
case EVP_CTRL_GCM_IV_GEN:
return 0;
/*
* Invocation field will be at least 8 bytes in size and so no need
* to check wrap around or increment more than last 8 bytes.
*/
return 1;
case EVP_CTRL_GCM_SET_IV_INV:
return 0;
return 1;
case EVP_CTRL_AEAD_TLS1_AAD:
/* Save the AAD for later use */
if (arg != 13)
return 0;
{
/* Correct length for explicit IV */
/* If decrypting correct for tag too */
if (!c->encrypt)
}
/* Extra padding: tag appended to record */
return EVP_GCM_TLS_TAG_LEN;
case EVP_CTRL_COPY:
{
return 0;
}
else {
return 0;
}
return 1;
}
default:
return -1;
}
}
{
return 1;
if (key) {
do {
# ifdef HWAES_CAPABLE
if (HWAES_CAPABLE) {
# ifdef HWAES_ctr32_encrypt_blocks
# else
# endif
break;
} else
# endif
# ifdef BSAES_CAPABLE
if (BSAES_CAPABLE) {
break;
} else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
break;
} else
# endif
(void)0; /* terminate potentially open 'else' */
# ifdef AES_CTR_ASM
# else
# endif
} while (0);
/*
* If we have an iv can set it directly, otherwise use saved IV.
*/
if (iv) {
}
} else {
/* If key set use IV, otherwise copy */
else
}
return 1;
}
/*
* Handle TLS GCM packet format. This consists of the last portion of the IV
* followed by the payload and finally the tag. On encrypt generate IV,
* encrypt payload and write the tag. On verify retrieve IV, decrypt payload
* and verify tag.
*/
{
return -1;
/*
* Set IV from start of buffer or generate IV and write to start of
* buffer.
*/
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
goto err;
/* Use saved AAD */
goto err;
/* Fix buffer and length to point to payload */
/* Encrypt payload */
# if defined(AES_GCM_ASM)
return -1;
}
# endif
goto err;
} else {
# if defined(AES_GCM_ASM2)
return -1;
}
# endif
goto err;
}
/* Finally write tag */
} else {
/* Decrypt */
# if defined(AES_GCM_ASM)
return -1;
}
# endif
goto err;
} else {
# if defined(AES_GCM_ASM2)
return -1;
}
# endif
goto err;
}
/* Retrieve tag */
/* If tag mismatch wipe buffer */
goto err;
}
}
err:
return rv;
}
{
/* If not set up, return error */
return -1;
if (gctx->tls_aad_len >= 0)
return -1;
if (in) {
return -1;
# if defined(AES_GCM_ASM)
return -1;
}
# endif
return -1;
} else {
# if defined(AES_GCM_ASM2)
return -1;
}
# endif
return -1;
}
} else {
# if defined(AES_GCM_ASM)
return -1;
}
# endif
return -1;
} else {
# if defined(AES_GCM_ASM2)
return -1;
}
# endif
return -1;
}
}
return len;
} else {
return -1;
return -1;
return 0;
}
/* Don't reuse the IV */
return 0;
}
}
{
if (type == EVP_CTRL_COPY) {
return 0;
}
return 0;
}
return 1;
} else if (type != EVP_CTRL_INIT)
return -1;
/* key1 and key2 are used as an indicator both key and IV are set */
return 1;
}
{
return 1;
if (key)
do {
# ifdef AES_XTS_ASM
# else
# endif
/* key_len is two AES keys */
# ifdef HWAES_CAPABLE
if (HWAES_CAPABLE) {
if (enc) {
} else {
}
break;
} else
# endif
# ifdef BSAES_CAPABLE
if (BSAES_CAPABLE)
else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
if (enc) {
} else {
}
break;
} else
# endif
(void)0; /* terminate potentially open 'else' */
if (enc) {
} else {
}
} while (0);
if (iv) {
}
return 1;
}
{
return 0;
return 0;
return 0;
return 1;
}
{
switch (type) {
case EVP_CTRL_INIT:
cctx->L = 8;
cctx->M = 12;
return 1;
case EVP_CTRL_CCM_SET_IVLEN:
case EVP_CTRL_CCM_SET_L:
return 0;
return 1;
case EVP_CTRL_CCM_SET_TAG:
return 0;
return 0;
if (ptr) {
}
return 1;
case EVP_CTRL_CCM_GET_TAG:
return 0;
return 0;
return 1;
case EVP_CTRL_COPY:
{
return 0;
}
return 1;
}
default:
return -1;
}
}
{
return 1;
if (key)
do {
# ifdef HWAES_CAPABLE
if (HWAES_CAPABLE) {
break;
} else
# endif
# ifdef VPAES_CAPABLE
if (VPAES_CAPABLE) {
break;
}
# endif
} while (0);
if (iv) {
}
return 1;
}
{
/* If not set up, return error */
return -1;
return -1;
if (!out) {
if (!in) {
return -1;
return len;
}
/* If have AAD need message length */
return -1;
return len;
}
/* EVP_*Final() doesn't return any data */
if (!in)
return 0;
/* If not set length yet do it */
return -1;
}
return -1;
return len;
} else {
}
}
if (rv == -1)
return rv;
}
}
#endif