Skip to content

Commit

Permalink
Updated aes and sha1 libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Mar 18, 2017
1 parent 0ec3c00 commit d9a9324
Show file tree
Hide file tree
Showing 16 changed files with 664 additions and 457 deletions.
96 changes: 84 additions & 12 deletions aes/aes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
Expand Down Expand Up @@ -44,7 +44,6 @@ extern "C"

#define AES_ENCRYPT /* if support for encryption is needed */
#define AES_DECRYPT /* if support for decryption is needed */
#define AES_REV_DKS /* define to reverse decryption key schedule */

#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
#define N_COLS 4 /* the number of columns in the state */
Expand All @@ -69,20 +68,36 @@ extern "C"
/* elements can be used by code that implements additional modes */

typedef union
{ uint_32t l;
uint_8t b[4];
{ uint32_t l;
uint8_t b[4];
} aes_inf;

typedef struct
{ uint_32t ks[KS_LENGTH];
#ifdef _MSC_VER
# pragma warning( disable : 4324 )
#endif

#if defined(_MSC_VER) && defined(_WIN64)
#define ALIGNED_(x) __declspec(align(x))
#elif defined(__GNUC__) && defined(__x86_64__)
#define ALIGNED_(x) __attribute__ ((aligned(x)))
#else
#define ALIGNED_(x)
#endif

typedef struct ALIGNED_(16)
{ uint32_t ks[KS_LENGTH];
aes_inf inf;
} aes_encrypt_ctx;

typedef struct
{ uint_32t ks[KS_LENGTH];
typedef struct ALIGNED_(16)
{ uint32_t ks[KS_LENGTH];
aes_inf inf;
} aes_decrypt_ctx;

#ifdef _MSC_VER
# pragma warning( default : 4324 )
#endif

/* This routine must be called before first use if non-static */
/* tables are being used */

Expand Down Expand Up @@ -139,14 +154,14 @@ AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_de

/* Multiple calls to the following subroutines for multiple block */
/* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
/* long messages incremantally provided that the context AND the iv */
/* long messages incrementally provided that the context AND the iv */
/* are preserved between all such calls. For the ECB and CBC modes */
/* each individual call within a series of incremental calls must */
/* process only full blocks (i.e. len must be a multiple of 16) but */
/* the CFB, OFB and CTR mode calls can handle multiple incremental */
/* calls of any length. Each mode is reset when a new AES key is */
/* set but ECB and CBC operations can be reset without setting a */
/* new key by setting a new IV value. To reset CFB, OFB and CTR */
/* calls of any length. Each mode is reset when a new AES key is */
/* set but ECB needs no reset and CBC can be reset without setting */
/* a new key by setting a new IV value. To reset CFB, OFB and CTR */
/* without setting the key, aes_mode_reset() must be called and the */
/* IV must be set. NOTE: All these calls update the IV on exit so */
/* this has to be reset if a new operation with the same IV as the */
Expand Down Expand Up @@ -191,6 +206,63 @@ AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,

#endif

#if 0
# define ADD_AESNI_MODE_CALLS
#endif

#if 0 && defined( ADD_AESNI_MODE_CALLS )
# define USE_AES_CONTEXT
#endif

#ifdef ADD_AESNI_MODE_CALLS
# ifdef USE_AES_CONTEXT

AES_RETURN aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_encrypt_ctx cx[1]);

AES_RETURN aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_decrypt_ctx cx[1]);

AES_RETURN AES_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const aes_encrypt_ctx cx[1]);

# else

void aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds);

void aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds);

void AES_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const unsigned char *key,
int number_of_rounds);

# endif
#endif

#if defined(__cplusplus)
}
#endif
Expand Down
10 changes: 8 additions & 2 deletions aes/aes_via_ace.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
Expand Down Expand Up @@ -350,7 +350,10 @@ INLINE int has_cpuid(void)

INLINE int is_via_cpu(void)
{ int val;
asm("pushl %eax\n\t");
asm("pushl %ebx\n\t");
asm("pushl %ecx\n\t");
asm("pushl %edx\n\t");
asm("xorl %eax,%eax\n\t");
asm("cpuid\n\t");
asm("xorl %eax,%eax\n\t");
Expand All @@ -361,7 +364,10 @@ INLINE int is_via_cpu(void)
asm("subl $0x736c7561,%ecx\n\t");
asm("orl %ecx,%eax\n\t");
asm("movl %%eax,%0\n\t" : "=m" (val));
asm("popl %edx\n\t");
asm("popl %ecx\n\t");
asm("popl %ebx\n\t");
asm("popl %eax\n\t");
val = (val ? 0 : 1);
via_flags = (val | NEH_CPU_READ);
return val;
Expand Down Expand Up @@ -538,4 +544,4 @@ INLINE int read_via_rng(void *buf, int count)
return count - lcnt;
}

#endif
#endif
37 changes: 22 additions & 15 deletions aes/aescrypt.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
Expand All @@ -21,6 +21,13 @@ Issue Date: 20/12/2007
#include "aesopt.h"
#include "aestab.h"

#if defined( USE_INTEL_AES_IF_PRESENT )
# include "aes_ni.h"

This comment has been minimized.

Copy link
@Coeur

Coeur Mar 20, 2017

Contributor

This breaks on macOS.
screen shot 2017-03-20 at 17 10 23

#else
/* map names here to provide the external API ('name' -> 'aes_name') */
# define aes_xi(x) aes_ ## x
#endif

#if defined(__cplusplus)
extern "C"
{
Expand Down Expand Up @@ -87,17 +94,17 @@ extern "C"
#define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))
#endif

AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
{ uint_32t locals(b0, b1);
const uint_32t *kp;
AES_RETURN aes_xi(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
{ uint32_t locals(b0, b1);
const uint32_t *kp;
#if defined( dec_fmvars )
dec_fmvars; /* declare variables for fwd_mcol() if needed */
#endif

if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
return EXIT_FAILURE;
if(cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16)
return EXIT_FAILURE;

kp = cx->ks;
kp = cx->ks;
state_in(b0, in, kp);

#if (ENC_UNROLL == FULL)
Expand Down Expand Up @@ -128,7 +135,7 @@ AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_en
#else

#if (ENC_UNROLL == PARTIAL)
{ uint_32t rnd;
{ uint32_t rnd;
for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
{
kp += N_COLS;
Expand All @@ -139,7 +146,7 @@ AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_en
kp += N_COLS;
round(fwd_rnd, b1, b0, kp);
#else
{ uint_32t rnd;
{ uint32_t rnd;
for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
{
kp += N_COLS;
Expand Down Expand Up @@ -219,14 +226,14 @@ AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_en
#define rnd_key(n) (kp - n * N_COLS)
#endif

AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
{ uint_32t locals(b0, b1);
AES_RETURN aes_xi(decrypt)(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
{ uint32_t locals(b0, b1);
#if defined( dec_imvars )
dec_imvars; /* declare variables for inv_mcol() if needed */
#endif
const uint_32t *kp;
const uint32_t *kp;

if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
if(cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16)
return EXIT_FAILURE;

kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0);
Expand Down Expand Up @@ -259,7 +266,7 @@ AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_de
#else

#if (DEC_UNROLL == PARTIAL)
{ uint_32t rnd;
{ uint32_t rnd;
for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
{
kp = rnd_key(1);
Expand All @@ -270,7 +277,7 @@ AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_de
kp = rnd_key(1);
round(inv_rnd, b1, b0, kp);
#else
{ uint_32t rnd;
{ uint32_t rnd;
for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
{
kp = rnd_key(1);
Expand Down
Loading

1 comment on commit d9a9324

@Coeur
Copy link
Contributor

@Coeur Coeur commented on d9a9324 Mar 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't build on macOS. Please either deliver aes_ni.h in the project, or change the conditions of the macro to include it.

Please sign in to comment.