Skip to content

Commit

Permalink
add bzip2 lib
Browse files Browse the repository at this point in the history
update aes lib
  • Loading branch information
ponapalt committed Jun 24, 2022
1 parent c9e58f3 commit acc110d
Show file tree
Hide file tree
Showing 28 changed files with 7,256 additions and 218 deletions.
66 changes: 37 additions & 29 deletions aes/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
Issue Date: 02/09/2018
This file contains the definitions required to use AES in C. See aesopt.h
for optimisation details.
Expand All @@ -26,31 +26,38 @@ Issue Date: 20/12/2007

#include <stdlib.h>

/* This include is used to find 8 & 32 bit unsigned integer types */
/* This include is used to find 8 & 32 bit unsigned integer types */
#include "brg_types.h"

#if defined(__cplusplus)
extern "C"
{
#endif

#define AES_128 /* if a fast 128 bit key scheduler is needed */
#define AES_192 /* if a fast 192 bit key scheduler is needed */
#define AES_256 /* if a fast 256 bit key scheduler is needed */
#define AES_VAR /* if variable key size scheduler is needed */
#define AES_MODES /* if support is needed for modes */
#define AES_128 /* if a fast 128 bit key scheduler is needed */
#define AES_192 /* if a fast 192 bit key scheduler is needed */
#define AES_256 /* if a fast 256 bit key scheduler is needed */
#define AES_VAR /* if variable key size scheduler is needed */
#if 1
# define AES_MODES /* if support is needed for modes in the C code */
#endif /* (these will use AES_NI if it is present) */
#if 0 /* add this to make direct calls to the AES_NI */
# /* implemented CBC and CTR modes available */
# define ADD_AESNI_MODE_CALLS
#endif

/* The following must also be set in assembler files if being used */
/* The following must also be set in assembler files if being used */

#define AES_ENCRYPT /* if support for encryption is needed */
#define AES_DECRYPT /* if support for decryption is needed */
#define AES_ENCRYPT /* if support for encryption is needed */
#define AES_DECRYPT /* if support for decryption is needed */

#define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
#define N_COLS 4 /* the number of columns in the state */
#define AES_BLOCK_SIZE_P2 4 /* AES block size as a power of 2 */
#define AES_BLOCK_SIZE (1 << AES_BLOCK_SIZE_P2) /* AES block size */
#define N_COLS 4 /* the number of columns in the state */

/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
/* or 44, 52 or 60 32-bit words. */
/* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
/* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
/* or 44, 52 or 60 32-bit words. */

#if defined( AES_VAR ) || defined( AES_256 )
#define KS_LENGTH 60
Expand All @@ -62,16 +69,23 @@ extern "C"

#define AES_RETURN INT_RETURN

/* the character array 'inf' in the following structures is used */
/* to hold AES context information. This AES code uses cx->inf.b[0] */
/* to hold the number of rounds multiplied by 16. The other three */
/* elements can be used by code that implements additional modes */
/* the character array 'inf' in the following structures is used */
/* to hold AES context information. This AES code uses cx->inf.b[0] */
/* to hold the number of rounds multiplied by 16. The other three */
/* elements can be used by code that implements additional modes */

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

/* Macros for detecting whether a given context was initialized for */
/* use with encryption or decryption code. These should only be used */
/* by e.g. language bindings which lose type information when the */
/* context pointer is passed to the calling language's runtime. */
#define IS_ENCRYPTION_CTX(cx) (((cx)->inf.b[2] & (uint8_t)0x01) == 1)
#define IS_DECRYPTION_CTX(cx) (((cx)->inf.b[2] & (uint8_t)0x01) == 0)

#ifdef _MSC_VER
# pragma warning( disable : 4324 )
#endif
Expand All @@ -87,12 +101,10 @@ typedef union
typedef struct ALIGNED_(16)
{ uint32_t ks[KS_LENGTH];
aes_inf inf;
} aes_encrypt_ctx;
} aes_crypt_ctx;

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

#ifdef _MSC_VER
# pragma warning( default : 4324 )
Expand Down Expand Up @@ -206,10 +218,6 @@ 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
Expand Down Expand Up @@ -252,7 +260,7 @@ void aes_CBC_decrypt(const unsigned char *in,
unsigned char *key,
int number_of_rounds);

void AES_CTR_encrypt(const unsigned char *in,
void aes_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
Expand Down
45 changes: 27 additions & 18 deletions aes/aes_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 09/09/2014
Issue Date: 03/08/2018
*/

#include "aes_ni.h"

#if defined( USE_INTEL_AES_IF_PRESENT )

#include <assert.h>

#if defined(_MSC_VER)

#include <intrin.h>
Expand All @@ -42,13 +44,9 @@ INLINE int has_aes_ni(void)
#elif defined( __GNUC__ )

#include <cpuid.h>

#if !defined(__clang__)
#pragma GCC target ("ssse3")
#pragma GCC target ("sse4.1")
#pragma GCC target ("aes")
#endif

#include <x86intrin.h>
#define INLINE static __inline

Expand Down Expand Up @@ -93,6 +91,7 @@ AES_RETURN aes_ni(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1
{
return aes_xi(encrypt_key128)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

t1 = _mm_loadu_si128((__m128i*)key);

Expand Down Expand Up @@ -139,7 +138,7 @@ AES_RETURN aes_ni(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1
ks[10] = t1;

cx->inf.l = 0;
cx->inf.b[0] = 10 * 16;
cx->inf.b[0] = 10 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -169,6 +168,7 @@ AES_RETURN aes_ni(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1
{
return aes_xi(encrypt_key192)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
Expand Down Expand Up @@ -217,7 +217,7 @@ AES_RETURN aes_ni(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1
ks[12] = t1;

cx->inf.l = 0;
cx->inf.b[0] = 12 * 16;
cx->inf.b[0] = 12 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -257,6 +257,7 @@ AES_RETURN aes_ni(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1
{
return aes_xi(encrypt_key256)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
Expand Down Expand Up @@ -305,7 +306,7 @@ AES_RETURN aes_ni(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1
ks[14] = t1;

cx->inf.l = 0;
cx->inf.b[0] = 14 * 16;
cx->inf.b[0] = 14 * AES_BLOCK_SIZE;
return EXIT_SUCCESS;
}

Expand All @@ -324,6 +325,7 @@ AES_RETURN aes_ni(decrypt_key128)(const unsigned char *key, aes_decrypt_ctx cx[1
{
return aes_xi(decrypt_key128)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(aes_ni(encrypt_key128)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
Expand All @@ -341,6 +343,7 @@ AES_RETURN aes_ni(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1
{
return aes_xi(decrypt_key192)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(aes_ni(encrypt_key192)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
Expand All @@ -357,6 +360,7 @@ AES_RETURN aes_ni(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1
{
return aes_xi(decrypt_key256)(key, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(aes_ni(encrypt_key256)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
Expand All @@ -371,25 +375,26 @@ AES_RETURN aes_ni(encrypt)(const unsigned char *in, unsigned char *out, const ae
{
__m128i *key = (__m128i*)cx->ks, t;

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 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE && cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
return EXIT_FAILURE;

if(!has_aes_ni())
{
return aes_xi(encrypt)(in, out, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);

switch(cx->inf.b[0])
{
case 14 * 16:
case 14 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 12 * 16:
case 12 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 10 * 16:
case 10 * AES_BLOCK_SIZE:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
Expand All @@ -410,25 +415,26 @@ AES_RETURN aes_ni(decrypt)(const unsigned char *in, unsigned char *out, const ae
{
__m128i *key = (__m128i*)cx->ks + (cx->inf.b[0] >> 4), t;

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 * AES_BLOCK_SIZE && cx->inf.b[0] != 12 * AES_BLOCK_SIZE && cx->inf.b[0] != 14 * AES_BLOCK_SIZE)
return EXIT_FAILURE;

if(!has_aes_ni())
{
return aes_xi(decrypt)(in, out, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);

switch(cx->inf.b[0])
{
case 14 * 16:
case 14 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 12 * 16:
case 12 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 10 * 16:
case 10 * AES_BLOCK_SIZE:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
Expand Down Expand Up @@ -465,6 +471,7 @@ AES_RETURN aes_CBC_encrypt(const unsigned char *in,
{
return aes_cbc_encrypt(in, out, length, ivec, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(length % 16)
length = length / 16 + 1;
Expand Down Expand Up @@ -500,6 +507,7 @@ AES_RETURN aes_CBC_decrypt(const unsigned char *in,
{
return aes_cbc_decrypt(in, out, length, ivec, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(length % 16)
length = length / 16 + 1;
Expand Down Expand Up @@ -533,7 +541,7 @@ static void ctr_inc(unsigned char *ctr_blk)
*(uint32_t*)(ctr_blk + 12) = *(uint32_t*)(ctr_blk + 12) + 1;
}

AES_RETURN AES_CTR_encrypt(const unsigned char *in,
AES_RETURN aes_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
Expand All @@ -554,6 +562,7 @@ AES_RETURN AES_CTR_encrypt(const unsigned char *in,
*(uint32_t*)(ctr_blk + 8) = *(uint32_t*)nonce;
return aes_ctr_crypt(in, out, length, (unsigned char*)ctr_blk, ctr_inc, cx);
}
assert(ALIGN_OFFSET(cx, 16) == 0);

if(length % 16)
length = length / 16 + 1;
Expand Down Expand Up @@ -642,7 +651,7 @@ void aes_CBC_decrypt(const unsigned char *in,
}
}

void AES_CTR_encrypt(const unsigned char *in,
void aes_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
Expand Down
Loading

0 comments on commit acc110d

Please sign in to comment.