Commit deb2c1a1 authored by Dr. Stephen Henson's avatar Dr. Stephen Henson
Browse files

Fix AES code.

Update Rijndael source to v3.0

Add AES OIDs.

Change most references of Rijndael to AES.

Add new draft AES ciphersuites.
parent d4219c48
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -3,6 +3,12 @@

 Changes between 0.9.6 and 0.9.7  [xx XXX 2000]

  *) Update Rijndael code to version 3.0 and change EVP AES ciphers to
     handle the new API. Currently only ECB, CBC modes supported. Add new
     AES OIDs. Add TLS AES ciphersuites as described in the "AES Ciphersuites
     for TLS" draft-ietf-tls-ciphersuite-03.txt.
     [Ben Laurie, Steve Henson]

  *) In the NCONF_...-based implementations for CONF_... queries
     (crypto/conf/conf_lib.c), if the input LHASH is NULL, avoid using
     a temporary CONF structure with the data component set to NULL
+19 −19
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC= encode.c digest.c evp_enc.c evp_key.c \
	e_des.c e_bf.c e_idea.c e_des3.c \
	e_rc4.c e_rd.c names.c \
	e_rc4.c e_aes.c names.c \
	e_xcbc_d.c e_rc2.c e_cast.c e_rc5.c \
	m_null.c m_md2.c m_md4.c m_md5.c m_sha.c m_sha1.c \
	m_dss.c m_dss1.c m_mdc2.c m_ripemd.c \
@@ -35,7 +35,7 @@ LIBSRC= encode.c digest.c evp_enc.c evp_key.c \

LIBOBJ=	encode.o digest.o evp_enc.o evp_key.o \
	e_des.o e_bf.o e_idea.o e_des3.o \
	e_rc4.o e_rd.o names.o \
	e_rc4.o e_aes.o names.o \
	e_xcbc_d.o e_rc2.o e_cast.o e_rc5.o \
	m_null.o m_md2.o m_md4.o m_md5.o m_sha.o m_sha1.o \
	m_dss.o m_dss1.o m_mdc2.o m_ripemd.o \
@@ -254,6 +254,23 @@ digest.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
digest.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
digest.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
digest.o: ../cryptlib.h
e_aes.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_aes.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
e_aes.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h
e_aes.o: ../../include/openssl/des.h ../../include/openssl/dh.h
e_aes.o: ../../include/openssl/dsa.h ../../include/openssl/e_os.h
e_aes.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
e_aes.o: ../../include/openssl/evp.h ../../include/openssl/idea.h
e_aes.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h
e_aes.o: ../../include/openssl/md4.h ../../include/openssl/md5.h
e_aes.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h
e_aes.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
e_aes.o: ../../include/openssl/opensslv.h ../../include/openssl/rc2.h
e_aes.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
e_aes.o: ../../include/openssl/rd_fst.h ../../include/openssl/rijndael.h
e_aes.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
e_aes.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
e_aes.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
e_bf.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_bf.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
e_bf.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
@@ -425,23 +442,6 @@ e_rc5.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
e_rc5.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
e_rc5.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
e_rc5.o: ../cryptlib.h evp_locl.h
e_rd.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_rd.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
e_rd.o: ../../include/openssl/cast.h ../../include/openssl/crypto.h
e_rd.o: ../../include/openssl/des.h ../../include/openssl/dh.h
e_rd.o: ../../include/openssl/dsa.h ../../include/openssl/e_os.h
e_rd.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
e_rd.o: ../../include/openssl/evp.h ../../include/openssl/idea.h
e_rd.o: ../../include/openssl/lhash.h ../../include/openssl/md2.h
e_rd.o: ../../include/openssl/md4.h ../../include/openssl/md5.h
e_rd.o: ../../include/openssl/mdc2.h ../../include/openssl/obj_mac.h
e_rd.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
e_rd.o: ../../include/openssl/opensslv.h ../../include/openssl/rc2.h
e_rd.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h
e_rd.o: ../../include/openssl/rd_fst.h ../../include/openssl/rijndael.h
e_rd.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h
e_rd.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
e_rd.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
e_xcbc_d.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
e_xcbc_d.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h
e_xcbc_d.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h
+7 −8
Original line number Diff line number Diff line
@@ -64,7 +64,6 @@

void OpenSSL_add_all_ciphers(void)
	{
	int i,j;

#ifndef NO_DES
	EVP_add_cipher(EVP_des_cfb());
@@ -147,13 +146,13 @@ void OpenSSL_add_all_ciphers(void)
	EVP_add_cipher_alias(SN_rc5_cbc,"RC5");
#endif

#ifndef NO_RIJNDAEL
	for(i=0 ; i < 3 ; ++i)
	    for(j=0 ; j < 3 ; ++j)
		{
		EVP_add_cipher(EVP_rijndael_ecb(i,j));
		EVP_add_cipher(EVP_rijndael_cbc(i,j));
		}
#ifndef NO_AES
	EVP_add_cipher(EVP_aes_128_ecb());
	EVP_add_cipher(EVP_aes_128_cbc());
	EVP_add_cipher(EVP_aes_192_ecb());
	EVP_add_cipher(EVP_aes_192_cbc());
	EVP_add_cipher(EVP_aes_256_ecb());
	EVP_add_cipher(EVP_aes_256_cbc());
#endif
	PKCS12_PBE_add();
	PKCS5_PBE_add();
+153 −0
Original line number Diff line number Diff line
/* ====================================================================
 * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
 * Copyright (c) 2001 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
@@ -50,52 +50,67 @@

#include <openssl/evp.h>
#include <openssl/err.h>
#include <memory.h>
#include <string.h>
#include <assert.h>

static EVP_CIPHER rd_cipher[3][3];
static int aes_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
					const unsigned char *iv, int enc);
static int aes_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
				const unsigned char *in, unsigned int inl);
static int aes_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
				const unsigned char *in, unsigned int inl);

#define IMPLEMENT_AES_CIPHER(name, ciph_func, keylen, ivlen, mode) \
static EVP_CIPHER name##_cipher_st = \
	{ \
	NID_##name, \
	16,keylen,ivlen, \
	mode, \
	aes_init, \
	ciph_func, \
	NULL, \
	sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+ \
		sizeof((((EVP_CIPHER_CTX *)NULL)->c.rijndael)), \
	EVP_CIPHER_set_asn1_iv, \
	EVP_CIPHER_get_asn1_iv, \
	NULL, \
	NULL \
	}; \
EVP_CIPHER * EVP_##name(void) \
	{ \
	return &name##_cipher_st; \
	}

static int anSizes[]={16,24,32};
static int anECBNIDs[3][3]=
    {
    { NID_rijndael_ecb_k128_b128,NID_rijndael_ecb_k192_b128,NID_rijndael_ecb_k256_b128 },
    { NID_rijndael_ecb_k128_b192,NID_rijndael_ecb_k192_b192,NID_rijndael_ecb_k256_b192 },
    { NID_rijndael_ecb_k128_b256,NID_rijndael_ecb_k192_b256,NID_rijndael_ecb_k256_b256 }
    };
IMPLEMENT_AES_CIPHER(aes_128_ecb, aes_ecb, 16, 0, EVP_CIPH_ECB_MODE)
IMPLEMENT_AES_CIPHER(aes_192_ecb, aes_ecb, 24, 0, EVP_CIPH_ECB_MODE)
IMPLEMENT_AES_CIPHER(aes_256_ecb, aes_ecb, 32, 0, EVP_CIPH_ECB_MODE)

static int anCBCNIDs[3][3]=
    {
    { NID_rd128_cbc_b128,NID_rd192_cbc_b128,NID_rd256_cbc_b128 },
    { NID_rd128_cbc_b192,NID_rd192_cbc_b192,NID_rd256_cbc_b192 },
    { NID_rd128_cbc_b256,NID_rd192_cbc_b256,NID_rd256_cbc_b256 }
    };
IMPLEMENT_AES_CIPHER(aes_128_cbc, aes_cbc, 16, 16, EVP_CIPH_CBC_MODE)
IMPLEMENT_AES_CIPHER(aes_192_cbc, aes_cbc, 24, 24, EVP_CIPH_CBC_MODE)
IMPLEMENT_AES_CIPHER(aes_256_cbc, aes_cbc, 32, 32, EVP_CIPH_CBC_MODE)

static int rd_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
static int aes_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
		   const unsigned char *iv, int enc)
	{
	RIJNDAEL_KEY *k=&ctx->c.rijndael;

    k->enc=enc;
    k->rounds=ctx->cipher->key_len/4+6;
    rijndaelKeySched((const word8 (*)[4])key,k->keySched,k->rounds);
    if(!k->enc)
	rijndaelKeyEncToDec(k->keySched,k->rounds);
    memcpy(k->iv,iv,ctx->cipher->iv_len);
	if (enc) 
		k->rounds = rijndaelKeySetupEnc(k->rd_key, key, ctx->key_len * 8);
	else
		k->rounds = rijndaelKeySetupDec(k->rd_key, key, ctx->key_len * 8);

	return 1;
	}

static int rd_cipher_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
static int aes_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
			 const unsigned char *in, unsigned int inl)
	{
	RIJNDAEL_KEY *k=&ctx->c.rijndael;
	while(inl > 0)
		{
	if(ctx->c.rijndael.enc)
	    rijndaelEncrypt(in,out,ctx->c.rijndael.keySched,
			    ctx->c.rijndael.rounds);
		if(ctx->encrypt)
	    		rijndaelEncrypt(k->rd_key,k->rounds, in, out);
		else
	    rijndaelDecrypt(in,out,ctx->c.rijndael.keySched,
			    ctx->c.rijndael.rounds);
	    		rijndaelDecrypt(k->rd_key,k->rounds, in, out);
		inl-=16;
		in+=16;
		out+=16;
@@ -105,99 +120,34 @@ static int rd_cipher_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
	return 1;
	}

static int rd_cipher_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
static int aes_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
			 const unsigned char *in, unsigned int inl)
	{
	int n;
	unsigned char tmp[16];

	RIJNDAEL_KEY *k=&ctx->c.rijndael;
	while(inl > 0)
		{
	if(ctx->c.rijndael.enc)
		if(ctx->encrypt)
			{
	    for(n=0 ; n < 16 ; ++n)
		tmp[n]=in[n]^ctx->c.rijndael.iv[n];
	    rijndaelEncrypt(tmp,out,ctx->c.rijndael.keySched,
			    ctx->c.rijndael.rounds);
	    memcpy(ctx->c.rijndael.iv,out,16);
			for(n=0 ; n < 16 ; n++)
				tmp[n] = in[n] ^ ctx->iv[n];
			rijndaelEncrypt(k->rd_key,k->rounds, tmp, out);
			memcpy(ctx->iv,out,16);
			}
		else
			{
	    rijndaelDecrypt(in,out,ctx->c.rijndael.keySched,
			    ctx->c.rijndael.rounds);
	    for(n=0 ; n < 16 ; ++n)
		out[n]^=ctx->c.rijndael.iv[n];
	    memcpy(ctx->c.rijndael.iv,in,16);
			memcpy(tmp, in, 16);
			rijndaelDecrypt(k->rd_key,k->rounds, in, out);
			for(n=0 ; n < 16 ; n++)
				out[n] ^= ctx->iv[n];
			memcpy(ctx->iv,tmp,16);
			}
		inl-=16;
		in+=16;
		out+=16;
		}
	assert(inl == 0);

	return 1;
	}
EVP_CIPHER *EVP_rijndael_ecb(int nBlockLength,int nKeyLength)
    {
    EVP_CIPHER *c;

    if(nBlockLength < 0 || nBlockLength > 2)
	{
	EVPerr(EVP_F_EVP_RIJNDAEL,EVP_R_BAD_BLOCK_LENGTH);
	return NULL;
	}
    if(nKeyLength < 0 || nKeyLength > 2)
	{
	EVPerr(EVP_F_EVP_RIJNDAEL,EVP_R_BAD_KEY_LENGTH);
	return NULL;
	}

    c=&rd_cipher[nKeyLength][nBlockLength];

    memset(c,'\0',sizeof *c);

    c->nid=anECBNIDs[nBlockLength][nKeyLength];
    c->block_size=anSizes[nBlockLength];
    c->key_len=anSizes[nKeyLength];
    c->iv_len=16;
    c->flags=EVP_CIPH_ECB_MODE;
    c->init=rd_init;
    c->do_cipher=rd_cipher_ecb;
    c->ctx_size=sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+
		sizeof((((EVP_CIPHER_CTX *)NULL)->c.rijndael));

    return c;
    }

EVP_CIPHER *EVP_rijndael_cbc(int nBlockLength,int nKeyLength)
    {
    EVP_CIPHER *c;

    if(nBlockLength < 0 || nBlockLength > 2)
	{
	EVPerr(EVP_F_EVP_RIJNDAEL,EVP_R_BAD_BLOCK_LENGTH);
	return NULL;
	}
    if(nKeyLength < 0 || nKeyLength > 2)
	{
	EVPerr(EVP_F_EVP_RIJNDAEL,EVP_R_BAD_KEY_LENGTH);
	return NULL;
	}

    c=&rd_cipher[nKeyLength][nBlockLength];

    memset(c,'\0',sizeof *c);

    c->nid=anCBCNIDs[nBlockLength][nKeyLength];
    c->block_size=anSizes[nBlockLength];
    c->key_len=anSizes[nKeyLength];
    c->iv_len=16;
    c->flags=EVP_CIPH_CBC_MODE;
    c->init=rd_init;
    c->do_cipher=rd_cipher_cbc;
    c->ctx_size=sizeof(EVP_CIPHER_CTX)-sizeof((((EVP_CIPHER_CTX *)NULL)->c))+
		sizeof((((EVP_CIPHER_CTX *)NULL)->c.rijndael));

    return c;
    }
+9 −5
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@
#ifndef NO_MDC2
#include <openssl/mdc2.h>
#endif
#ifndef NO_RIJNDAEL
#ifndef NO_AES
#include <openssl/rijndael.h>
#endif

@@ -449,7 +449,7 @@ struct evp_cipher_ctx_st
#ifndef NO_CAST
		CAST_KEY cast_ks;/* key schedule */
#endif
#ifndef NO_RIJNDAEL
#ifndef NO_AES
		RIJNDAEL_KEY rijndael;
#endif
		} c;
@@ -705,9 +705,13 @@ EVP_CIPHER *EVP_rc5_32_12_16_ecb(void);
EVP_CIPHER *EVP_rc5_32_12_16_cfb(void);
EVP_CIPHER *EVP_rc5_32_12_16_ofb(void);
#endif
#ifndef NO_RIJNDAEL
EVP_CIPHER *EVP_rijndael_ecb(int nBlockLength,int nKeyLength);
EVP_CIPHER *EVP_rijndael_cbc(int nBlockLength,int nKeyLength);
#ifndef NO_AES
EVP_CIPHER *EVP_aes_128_ecb(void);
EVP_CIPHER *EVP_aes_128_cbc(void);
EVP_CIPHER *EVP_aes_192_ecb(void);
EVP_CIPHER *EVP_aes_192_cbc(void);
EVP_CIPHER *EVP_aes_256_ecb(void);
EVP_CIPHER *EVP_aes_256_cbc(void);
#endif

void OpenSSL_add_all_algorithms(void);
Loading