aes.c File Reference

Implements the AES cipher (with 128-bit keys and blocks), and a counter-mode stream cipher on top of AES. This code is taken from the main Rijndael distribution. (We include this because many people are running older versions of OpenSSL without AES support.). More...

#include "orconfig.h"
#include <openssl/opensslv.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "aes.h"
#include "util.h"
#include "log.h"

Data Structures

struct  aes_cnt_cipher

Defines

#define USE_BUILTIN_AES
#define USE_RIJNDAEL_COUNTER_OPTIMIZATION
#define MAXNR   14
#define USING_COUNTER_VARS
#define COUNTER(c, n)   ((c)->counter ## n)
#define UPDATE_CTR_BUF(c, n)
#define SWAP(x)   (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
#define GETU32(pt)   (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
#define PUTU32(ct, st)   { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }

Typedefs

typedef uint64_t u64
typedef uint32_t u32
typedef uint8_t u8

Functions

static int rijndaelKeySetupEnc (u32 rk[], const u8 cipherKey[], int keyBits)
static void rijndaelEncrypt (const u32 rk[], int Nr, u32 ctr3, u32 ctr2, u32 ctr1, u32 ctr0, u8 ct[16])
static INLINE void _aes_fill_buf (aes_cnt_cipher_t *cipher)
aes_cnt_cipher_taes_new_cipher (void)
void aes_set_key (aes_cnt_cipher_t *cipher, const char *key, int key_bits)
void aes_free_cipher (aes_cnt_cipher_t *cipher)
void aes_crypt (aes_cnt_cipher_t *cipher, const char *input, size_t len, char *output)
void aes_crypt_inplace (aes_cnt_cipher_t *cipher, char *data, size_t len)
void aes_set_iv (aes_cnt_cipher_t *cipher, const char *iv)

Variables

static const u32 Te0 [256]
static const u32 Te1 [256]
static const u32 Te2 [256]
static const u32 Te3 [256]
static const u32 Te4 [256]
static const u32 rcon []


Detailed Description

Implements the AES cipher (with 128-bit keys and blocks), and a counter-mode stream cipher on top of AES. This code is taken from the main Rijndael distribution. (We include this because many people are running older versions of OpenSSL without AES support.).


Define Documentation

#define USE_BUILTIN_AES

Defined iff we're using OpenSSL's AES functions for AES. Defined iff we're using OpenSSL's EVP code for AES. Defined iff we're using Tor's internal AES implementation, defined below.


Function Documentation

static INLINE void _aes_fill_buf ( aes_cnt_cipher_t cipher  )  [static]

Helper function: set cipher's internal buffer to the encrypted value of the current counter.

References aes_cnt_cipher::buf, aes_cnt_cipher::counter0, aes_cnt_cipher::counter1, aes_cnt_cipher::counter2, aes_cnt_cipher::counter3, aes_cnt_cipher::nr, and aes_cnt_cipher::rk.

Referenced by aes_crypt(), aes_crypt_inplace(), aes_set_iv(), and aes_set_key().

void aes_crypt ( aes_cnt_cipher_t cipher,
const char *  input,
size_t  len,
char *  output 
)

Encrypt len bytes from input, storing the result in output. Uses the key in cipher, and advances the counter by len bytes as it encrypts.

References _aes_fill_buf(), aes_cnt_cipher::buf, and aes_cnt_cipher::pos.

Referenced by crypto_cipher_decrypt(), and crypto_cipher_encrypt().

void aes_crypt_inplace ( aes_cnt_cipher_t cipher,
char *  data,
size_t  len 
)

Encrypt len bytes from input, storing the results in place. Uses the key in cipher, and advances the counter by len bytes as it encrypts.

References _aes_fill_buf(), aes_cnt_cipher::buf, and aes_cnt_cipher::pos.

Referenced by crypto_cipher_crypt_inplace().

void aes_free_cipher ( aes_cnt_cipher_t cipher  ) 

Release storage held by cipher

References tor_free.

Referenced by crypto_free_cipher_env().

aes_cnt_cipher_t* aes_new_cipher ( void   ) 

Return a newly allocated counter-mode AES128 cipher implementation.

Referenced by crypto_new_cipher_env().

void aes_set_iv ( aes_cnt_cipher_t cipher,
const char *  iv 
)

Reset the 128-bit counter of cipher to the 16-bit big-endian value in iv.

References _aes_fill_buf(), aes_cnt_cipher::buf, aes_cnt_cipher::counter0, aes_cnt_cipher::counter1, aes_cnt_cipher::counter2, aes_cnt_cipher::counter3, get_uint32(), and aes_cnt_cipher::pos.

Referenced by crypto_cipher_set_iv().

void aes_set_key ( aes_cnt_cipher_t cipher,
const char *  key,
int  key_bits 
)

static int rijndaelKeySetupEnc ( u32  rk[],
const u8  cipherKey[],
int  keyBits 
) [static]

Expand the cipher key into the encryption key schedule.

Returns:
the number of rounds for the given cipher key size.

Referenced by aes_set_key().


Variable Documentation

const u32 rcon[] [static]

Initial value:

 {
        0x01000000, 0x02000000, 0x04000000, 0x08000000,
        0x10000000, 0x20000000, 0x40000000, 0x80000000,
        0x1B000000, 0x36000000, 
}

const u32 Te0[256] [static]

rijndael-alg-fst.c

Version:
3.0 (December 2000)
Optimized ANSI C code for the Rijndael cipher (now AES)

Author:
Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>

Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>

Paulo Barreto <paulo.barreto@terra.com.br>

This code is hereby placed in the public domain.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS 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 AUTHORS OR 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.


Generated on Tue May 25 00:30:39 2010 for tor by  doxygen 1.5.6