mirror of https://github.com/ClassiCube/ClassiCube
BearSSL: Use AES big backend when AES X86 NI isn't used
This commit is contained in:
parent
c440ff84cc
commit
fa687e9e99
|
|
@ -54,11 +54,6 @@ jobs:
|
|||
x86_64-freebsd11-clang ${{ env.SRCS }} ${{ env.COMMON_FLAGS }} ${{ env.PLAT64_FLAGS }} $LATEST_FLAG -o cc-fbsd64-gl1 ${{ env.LIBS }}
|
||||
|
||||
|
||||
# otherwise notify_failure doesn't work
|
||||
- name: Install curl when necessary
|
||||
if: ${{ always() && steps.compile.outcome == 'failure' }}
|
||||
run: apk add curl
|
||||
|
||||
- uses: ./.github/actions/notify_failure
|
||||
if: ${{ always() && steps.compile.outcome == 'failure' }}
|
||||
with:
|
||||
|
|
|
|||
|
|
@ -87,6 +87,9 @@ $(TARGET).elf: $(OBJS)
|
|||
#---------------------------------------------------------------------------------
|
||||
$(BUILD_DIR)/%.o : src/%.c
|
||||
$(VITA_CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
|
||||
|
||||
$(BUILD_DIR)/%.o : src/psvita/%.c
|
||||
$(VITA_CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
|
||||
|
||||
$(BUILD_DIR)/%.o : third_party/bearssl/src/%.c
|
||||
$(VITA_CC) $(CFLAGS) -c $< -o $@
|
||||
|
|
@ -95,7 +98,6 @@ $(BUILD_DIR)/%.o : third_party/bearssl/src/%.c
|
|||
#---------------------------------------------------------------------------------
|
||||
# Dependency tracking
|
||||
#---------------------------------------------------------------------------------
|
||||
# Dependency tracking
|
||||
$(DEPFILES):
|
||||
|
||||
include $(wildcard $(DEPFILES))
|
||||
|
|
|
|||
|
|
@ -62,19 +62,19 @@ static void* gxm_shader_patcher_fragment_usse_addr;
|
|||
static unsigned int shader_patcher_fragment_usse_offset;
|
||||
|
||||
|
||||
#include "../misc/vita/colored_f.h"
|
||||
#include "../misc/vita/colored_v.h"
|
||||
#include "../../misc/vita/colored_f.h"
|
||||
#include "../../misc/vita/colored_v.h"
|
||||
static SceGxmProgram* gxm_colored_VP = (SceGxmProgram *)&colored_v;
|
||||
static SceGxmProgram* gxm_colored_FP = (SceGxmProgram *)&colored_f;
|
||||
|
||||
#include "../misc/vita/textured_f.h"
|
||||
#include "../misc/vita/textured_v.h"
|
||||
#include "../../misc/vita/textured_f.h"
|
||||
#include "../../misc/vita/textured_v.h"
|
||||
static SceGxmProgram* gxm_textured_VP = (SceGxmProgram *)&textured_v;
|
||||
static SceGxmProgram* gxm_textured_FP = (SceGxmProgram *)&textured_f;
|
||||
|
||||
#include "../misc/vita/colored_alpha_f.h"
|
||||
#include "../../misc/vita/colored_alpha_f.h"
|
||||
static SceGxmProgram* gxm_colored_alpha_FP = (SceGxmProgram *)&colored_alpha_f;
|
||||
#include "../misc/vita/textured_alpha_f.h"
|
||||
#include "../../misc/vita/textured_alpha_f.h"
|
||||
static SceGxmProgram* gxm_textured_alpha_FP = (SceGxmProgram *)&textured_alpha_f;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -295,35 +295,12 @@ extern "C" {
|
|||
* | Name | Function | Block Size (bytes) | Key lengths (bytes) |
|
||||
* | :-------- | :------- | :----------------: | :-----------------: |
|
||||
* | aes_big | AES | 16 | 16, 24 and 32 |
|
||||
* | aes_small | AES | 16 | 16, 24 and 32 |
|
||||
* | aes_ct | AES | 16 | 16, 24 and 32 |
|
||||
* | aes_ct64 | AES | 16 | 16, 24 and 32 |
|
||||
* | aes_x86ni | AES | 16 | 16, 24 and 32 |
|
||||
*
|
||||
* `aes_big` is a "classical" AES implementation, using tables. It
|
||||
* is fast but not constant-time, since it makes data-dependent array
|
||||
* accesses.
|
||||
*
|
||||
* `aes_small` is an AES implementation optimized for code size. It
|
||||
* is substantially slower than `aes_big`; it is not constant-time
|
||||
* either.
|
||||
*
|
||||
* `aes_ct` is a constant-time implementation of AES; its code is about
|
||||
* as big as that of `aes_big`, while its performance is comparable to
|
||||
* that of `aes_small`. However, it is constant-time. This
|
||||
* implementation should thus be considered to be the "default" AES in
|
||||
* BearSSL, to be used unless the operational context guarantees that a
|
||||
* non-constant-time implementation is safe, or an architecture-specific
|
||||
* constant-time implementation can be used (e.g. using dedicated
|
||||
* hardware opcodes).
|
||||
*
|
||||
* `aes_ct64` is another constant-time implementation of AES. It is
|
||||
* similar to `aes_ct` but uses 64-bit values. On 32-bit machines,
|
||||
* `aes_ct64` is not faster than `aes_ct`, often a bit slower, and has
|
||||
* a larger footprint; however, on 64-bit architectures, `aes_ct64`
|
||||
* is typically twice faster than `aes_ct` for modes that allow parallel
|
||||
* operations (i.e. CTR, and CBC decryption, but not CBC encryption).
|
||||
*
|
||||
* `aes_x86ni` exists only on x86 architectures (32-bit and 64-bit). It
|
||||
* uses the AES-NI opcodes when available.
|
||||
*
|
||||
|
|
@ -881,666 +858,6 @@ void br_aes_big_ctrcbc_ctr(const br_aes_big_ctrcbc_keys *ctx,
|
|||
void br_aes_big_ctrcbc_mac(const br_aes_big_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len);
|
||||
|
||||
/*
|
||||
* AES implementation optimized for size. It is slower than the
|
||||
* traditional table-based AES implementation, but requires much less
|
||||
* code. It still uses data-dependent table accesses (albeit within a
|
||||
* much smaller 256-byte table), which makes it conceptually vulnerable
|
||||
* to cache-timing attacks.
|
||||
*/
|
||||
|
||||
/** \brief AES block size (16 bytes). */
|
||||
#define br_aes_small_BLOCK_SIZE 16
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_small` implementation, CBC encryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcenc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_small_cbcenc_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_small` implementation, CBC decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcdec_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_small_cbcdec_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_small` implementation, CTR encryption
|
||||
* and decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctr_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_small_ctr_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_small` implementation, CTR encryption
|
||||
* and decryption + CBC-MAC).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctrcbc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_small_ctrcbc_keys;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC encryption (`aes_small` implementation).
|
||||
*/
|
||||
extern const br_block_cbcenc_class br_aes_small_cbcenc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC decryption (`aes_small` implementation).
|
||||
*/
|
||||
extern const br_block_cbcdec_class br_aes_small_cbcdec_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption and decryption
|
||||
* (`aes_small` implementation).
|
||||
*/
|
||||
extern const br_block_ctr_class br_aes_small_ctr_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption/decryption + CBC-MAC
|
||||
* (`aes_small` implementation).
|
||||
*/
|
||||
extern const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC encryption
|
||||
* (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC decryption
|
||||
* (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR encryption
|
||||
* and decryption (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
|
||||
* (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC encryption with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC decryption with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption and decryption with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (constant, 12 bytes).
|
||||
* \param cc initial block counter value.
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes).
|
||||
* \return new block counter value.
|
||||
*/
|
||||
uint32_t br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption + CBC-MAC with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR decryption + CBC-MAC with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption/decryption with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param data data to MAC (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC-MAC with AES (`aes_small` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to MAC (unmodified).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len);
|
||||
|
||||
/*
|
||||
* Constant-time AES implementation. Its size is similar to that of
|
||||
* 'aes_big', and its performance is similar to that of 'aes_small' (faster
|
||||
* decryption, slower encryption). However, it is constant-time, i.e.
|
||||
* immune to cache-timing and similar attacks.
|
||||
*/
|
||||
|
||||
/** \brief AES block size (16 bytes). */
|
||||
#define br_aes_ct_BLOCK_SIZE 16
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct` implementation, CBC encryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcenc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct_cbcenc_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct` implementation, CBC decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcdec_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct_cbcdec_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption
|
||||
* and decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctr_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct_ctr_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct` implementation, CTR encryption
|
||||
* and decryption + CBC-MAC).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctrcbc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint32_t skey[60];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct_ctrcbc_keys;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC encryption (`aes_ct` implementation).
|
||||
*/
|
||||
extern const br_block_cbcenc_class br_aes_ct_cbcenc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC decryption (`aes_ct` implementation).
|
||||
*/
|
||||
extern const br_block_cbcdec_class br_aes_ct_cbcdec_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption and decryption
|
||||
* (`aes_ct` implementation).
|
||||
*/
|
||||
extern const br_block_ctr_class br_aes_ct_ctr_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption/decryption + CBC-MAC
|
||||
* (`aes_ct` implementation).
|
||||
*/
|
||||
extern const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC encryption
|
||||
* (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC decryption
|
||||
* (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR encryption
|
||||
* and decryption (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
|
||||
* (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC encryption with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC decryption with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption and decryption with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (constant, 12 bytes).
|
||||
* \param cc initial block counter value.
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes).
|
||||
* \return new block counter value.
|
||||
*/
|
||||
uint32_t br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption + CBC-MAC with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR decryption + CBC-MAC with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption/decryption with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param data data to MAC (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC-MAC with AES (`aes_ct` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to MAC (unmodified).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len);
|
||||
|
||||
/*
|
||||
* 64-bit constant-time AES implementation. It is similar to 'aes_ct'
|
||||
* but uses 64-bit registers, making it about twice faster than 'aes_ct'
|
||||
* on 64-bit platforms, while remaining constant-time and with a similar
|
||||
* code size. (The doubling in performance is only for CBC decryption
|
||||
* and CTR mode; CBC encryption is non-parallel and cannot benefit from
|
||||
* the larger registers.)
|
||||
*/
|
||||
|
||||
/** \brief AES block size (16 bytes). */
|
||||
#define br_aes_ct64_BLOCK_SIZE 16
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct64` implementation, CBC encryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcenc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint64_t skey[30];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct64_cbcenc_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct64` implementation, CBC decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_cbcdec_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint64_t skey[30];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct64_cbcdec_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption
|
||||
* and decryption).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctr_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint64_t skey[30];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct64_ctr_keys;
|
||||
|
||||
/**
|
||||
* \brief Context for AES subkeys (`aes_ct64` implementation, CTR encryption
|
||||
* and decryption + CBC-MAC).
|
||||
*
|
||||
* First field is a pointer to the vtable; it is set by the initialisation
|
||||
* function. Other fields are not supposed to be accessed by user code.
|
||||
*/
|
||||
typedef struct {
|
||||
/** \brief Pointer to vtable for this context. */
|
||||
const br_block_ctrcbc_class *vtable;
|
||||
#ifndef BR_DOXYGEN_IGNORE
|
||||
uint64_t skey[30];
|
||||
unsigned num_rounds;
|
||||
#endif
|
||||
} br_aes_ct64_ctrcbc_keys;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC encryption (`aes_ct64` implementation).
|
||||
*/
|
||||
extern const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CBC decryption (`aes_ct64` implementation).
|
||||
*/
|
||||
extern const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption and decryption
|
||||
* (`aes_ct64` implementation).
|
||||
*/
|
||||
extern const br_block_ctr_class br_aes_ct64_ctr_vtable;
|
||||
|
||||
/**
|
||||
* \brief Class instance for AES CTR encryption/decryption + CBC-MAC
|
||||
* (`aes_ct64` implementation).
|
||||
*/
|
||||
extern const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable;
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC encryption
|
||||
* (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CBC decryption
|
||||
* (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR encryption
|
||||
* and decryption (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief Context initialisation (key schedule) for AES CTR + CBC-MAC
|
||||
* (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context to initialise.
|
||||
* \param key secret key.
|
||||
* \param len secret key length (in bytes).
|
||||
*/
|
||||
void br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC encryption with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC decryption with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx, void *iv,
|
||||
void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption and decryption with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param iv IV (constant, 12 bytes).
|
||||
* \param cc initial block counter value.
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes).
|
||||
* \return new block counter value.
|
||||
*/
|
||||
uint32_t br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption + CBC-MAC with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to encrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR decryption + CBC-MAC with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to decrypt (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CTR encryption/decryption with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param ctr counter for CTR (16 bytes, updated).
|
||||
* \param data data to MAC (updated).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len);
|
||||
|
||||
/**
|
||||
* \brief CBC-MAC with AES (`aes_ct64` implementation).
|
||||
*
|
||||
* \param ctx context (already initialised).
|
||||
* \param cbcmac IV for CBC-MAC (updated).
|
||||
* \param data data to MAC (unmodified).
|
||||
* \param len data length (in bytes, MUST be a multiple of 16).
|
||||
*/
|
||||
void br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len);
|
||||
|
||||
/*
|
||||
* AES implementation using AES-NI opcodes (x86 platform).
|
||||
*/
|
||||
|
|
@ -1840,9 +1157,6 @@ const br_block_ctrcbc_class *br_aes_x86ni_ctrcbc_get_vtable(void);
|
|||
typedef union {
|
||||
const br_block_cbcenc_class *vtable;
|
||||
br_aes_big_cbcenc_keys c_big;
|
||||
br_aes_small_cbcenc_keys c_small;
|
||||
br_aes_ct_cbcenc_keys c_ct;
|
||||
br_aes_ct64_cbcenc_keys c_ct64;
|
||||
br_aes_x86ni_cbcenc_keys c_x86ni;
|
||||
} br_aes_gen_cbcenc_keys;
|
||||
|
||||
|
|
@ -1853,9 +1167,6 @@ typedef union {
|
|||
typedef union {
|
||||
const br_block_cbcdec_class *vtable;
|
||||
br_aes_big_cbcdec_keys c_big;
|
||||
br_aes_small_cbcdec_keys c_small;
|
||||
br_aes_ct_cbcdec_keys c_ct;
|
||||
br_aes_ct64_cbcdec_keys c_ct64;
|
||||
br_aes_x86ni_cbcdec_keys c_x86ni;
|
||||
} br_aes_gen_cbcdec_keys;
|
||||
|
||||
|
|
@ -1866,9 +1177,6 @@ typedef union {
|
|||
typedef union {
|
||||
const br_block_ctr_class *vtable;
|
||||
br_aes_big_ctr_keys c_big;
|
||||
br_aes_small_ctr_keys c_small;
|
||||
br_aes_ct_ctr_keys c_ct;
|
||||
br_aes_ct64_ctr_keys c_ct64;
|
||||
br_aes_x86ni_ctr_keys c_x86ni;
|
||||
} br_aes_gen_ctr_keys;
|
||||
|
||||
|
|
@ -1879,9 +1187,6 @@ typedef union {
|
|||
typedef union {
|
||||
const br_block_ctrcbc_class *vtable;
|
||||
br_aes_big_ctrcbc_keys c_big;
|
||||
br_aes_small_ctrcbc_keys c_small;
|
||||
br_aes_ct_ctrcbc_keys c_ct;
|
||||
br_aes_ct64_ctrcbc_keys c_ct64;
|
||||
br_aes_x86ni_ctrcbc_keys c_x86ni;
|
||||
} br_aes_gen_ctrcbc_keys;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,328 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_Sbox(uint32_t *q)
|
||||
{
|
||||
/*
|
||||
* This S-box implementation is a straightforward translation of
|
||||
* the circuit described by Boyar and Peralta in "A new
|
||||
* combinational logic minimization technique with applications
|
||||
* to cryptology" (https://eprint.iacr.org/2009/191.pdf).
|
||||
*
|
||||
* Note that variables x* (input) and s* (output) are numbered
|
||||
* in "reverse" order (x0 is the high bit, x7 is the low bit).
|
||||
*/
|
||||
|
||||
uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
|
||||
uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
|
||||
uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
|
||||
uint32_t y20, y21;
|
||||
uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
|
||||
uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
|
||||
uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
|
||||
uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
|
||||
uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
|
||||
uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
|
||||
uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
|
||||
uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
|
||||
uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
|
||||
uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
x0 = q[7];
|
||||
x1 = q[6];
|
||||
x2 = q[5];
|
||||
x3 = q[4];
|
||||
x4 = q[3];
|
||||
x5 = q[2];
|
||||
x6 = q[1];
|
||||
x7 = q[0];
|
||||
|
||||
/*
|
||||
* Top linear transformation.
|
||||
*/
|
||||
y14 = x3 ^ x5;
|
||||
y13 = x0 ^ x6;
|
||||
y9 = x0 ^ x3;
|
||||
y8 = x0 ^ x5;
|
||||
t0 = x1 ^ x2;
|
||||
y1 = t0 ^ x7;
|
||||
y4 = y1 ^ x3;
|
||||
y12 = y13 ^ y14;
|
||||
y2 = y1 ^ x0;
|
||||
y5 = y1 ^ x6;
|
||||
y3 = y5 ^ y8;
|
||||
t1 = x4 ^ y12;
|
||||
y15 = t1 ^ x5;
|
||||
y20 = t1 ^ x1;
|
||||
y6 = y15 ^ x7;
|
||||
y10 = y15 ^ t0;
|
||||
y11 = y20 ^ y9;
|
||||
y7 = x7 ^ y11;
|
||||
y17 = y10 ^ y11;
|
||||
y19 = y10 ^ y8;
|
||||
y16 = t0 ^ y11;
|
||||
y21 = y13 ^ y16;
|
||||
y18 = x0 ^ y16;
|
||||
|
||||
/*
|
||||
* Non-linear section.
|
||||
*/
|
||||
t2 = y12 & y15;
|
||||
t3 = y3 & y6;
|
||||
t4 = t3 ^ t2;
|
||||
t5 = y4 & x7;
|
||||
t6 = t5 ^ t2;
|
||||
t7 = y13 & y16;
|
||||
t8 = y5 & y1;
|
||||
t9 = t8 ^ t7;
|
||||
t10 = y2 & y7;
|
||||
t11 = t10 ^ t7;
|
||||
t12 = y9 & y11;
|
||||
t13 = y14 & y17;
|
||||
t14 = t13 ^ t12;
|
||||
t15 = y8 & y10;
|
||||
t16 = t15 ^ t12;
|
||||
t17 = t4 ^ t14;
|
||||
t18 = t6 ^ t16;
|
||||
t19 = t9 ^ t14;
|
||||
t20 = t11 ^ t16;
|
||||
t21 = t17 ^ y20;
|
||||
t22 = t18 ^ y19;
|
||||
t23 = t19 ^ y21;
|
||||
t24 = t20 ^ y18;
|
||||
|
||||
t25 = t21 ^ t22;
|
||||
t26 = t21 & t23;
|
||||
t27 = t24 ^ t26;
|
||||
t28 = t25 & t27;
|
||||
t29 = t28 ^ t22;
|
||||
t30 = t23 ^ t24;
|
||||
t31 = t22 ^ t26;
|
||||
t32 = t31 & t30;
|
||||
t33 = t32 ^ t24;
|
||||
t34 = t23 ^ t33;
|
||||
t35 = t27 ^ t33;
|
||||
t36 = t24 & t35;
|
||||
t37 = t36 ^ t34;
|
||||
t38 = t27 ^ t36;
|
||||
t39 = t29 & t38;
|
||||
t40 = t25 ^ t39;
|
||||
|
||||
t41 = t40 ^ t37;
|
||||
t42 = t29 ^ t33;
|
||||
t43 = t29 ^ t40;
|
||||
t44 = t33 ^ t37;
|
||||
t45 = t42 ^ t41;
|
||||
z0 = t44 & y15;
|
||||
z1 = t37 & y6;
|
||||
z2 = t33 & x7;
|
||||
z3 = t43 & y16;
|
||||
z4 = t40 & y1;
|
||||
z5 = t29 & y7;
|
||||
z6 = t42 & y11;
|
||||
z7 = t45 & y17;
|
||||
z8 = t41 & y10;
|
||||
z9 = t44 & y12;
|
||||
z10 = t37 & y3;
|
||||
z11 = t33 & y4;
|
||||
z12 = t43 & y13;
|
||||
z13 = t40 & y5;
|
||||
z14 = t29 & y2;
|
||||
z15 = t42 & y9;
|
||||
z16 = t45 & y14;
|
||||
z17 = t41 & y8;
|
||||
|
||||
/*
|
||||
* Bottom linear transformation.
|
||||
*/
|
||||
t46 = z15 ^ z16;
|
||||
t47 = z10 ^ z11;
|
||||
t48 = z5 ^ z13;
|
||||
t49 = z9 ^ z10;
|
||||
t50 = z2 ^ z12;
|
||||
t51 = z2 ^ z5;
|
||||
t52 = z7 ^ z8;
|
||||
t53 = z0 ^ z3;
|
||||
t54 = z6 ^ z7;
|
||||
t55 = z16 ^ z17;
|
||||
t56 = z12 ^ t48;
|
||||
t57 = t50 ^ t53;
|
||||
t58 = z4 ^ t46;
|
||||
t59 = z3 ^ t54;
|
||||
t60 = t46 ^ t57;
|
||||
t61 = z14 ^ t57;
|
||||
t62 = t52 ^ t58;
|
||||
t63 = t49 ^ t58;
|
||||
t64 = z4 ^ t59;
|
||||
t65 = t61 ^ t62;
|
||||
t66 = z1 ^ t63;
|
||||
s0 = t59 ^ t63;
|
||||
s6 = t56 ^ ~t62;
|
||||
s7 = t48 ^ ~t60;
|
||||
t67 = t64 ^ t65;
|
||||
s3 = t53 ^ t66;
|
||||
s4 = t51 ^ t66;
|
||||
s5 = t47 ^ t65;
|
||||
s1 = t64 ^ ~s3;
|
||||
s2 = t55 ^ ~t67;
|
||||
|
||||
q[7] = s0;
|
||||
q[6] = s1;
|
||||
q[5] = s2;
|
||||
q[4] = s3;
|
||||
q[3] = s4;
|
||||
q[2] = s5;
|
||||
q[1] = s6;
|
||||
q[0] = s7;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_ortho(uint32_t *q)
|
||||
{
|
||||
#define SWAPN(cl, ch, s, x, y) do { \
|
||||
uint32_t a, b; \
|
||||
a = (x); \
|
||||
b = (y); \
|
||||
(x) = (a & (uint32_t)cl) | ((b & (uint32_t)cl) << (s)); \
|
||||
(y) = ((a & (uint32_t)ch) >> (s)) | (b & (uint32_t)ch); \
|
||||
} while (0)
|
||||
|
||||
#define SWAP2(x, y) SWAPN(0x55555555, 0xAAAAAAAA, 1, x, y)
|
||||
#define SWAP4(x, y) SWAPN(0x33333333, 0xCCCCCCCC, 2, x, y)
|
||||
#define SWAP8(x, y) SWAPN(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
|
||||
|
||||
SWAP2(q[0], q[1]);
|
||||
SWAP2(q[2], q[3]);
|
||||
SWAP2(q[4], q[5]);
|
||||
SWAP2(q[6], q[7]);
|
||||
|
||||
SWAP4(q[0], q[2]);
|
||||
SWAP4(q[1], q[3]);
|
||||
SWAP4(q[4], q[6]);
|
||||
SWAP4(q[5], q[7]);
|
||||
|
||||
SWAP8(q[0], q[4]);
|
||||
SWAP8(q[1], q[5]);
|
||||
SWAP8(q[2], q[6]);
|
||||
SWAP8(q[3], q[7]);
|
||||
}
|
||||
|
||||
static const unsigned char Rcon[] = {
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
|
||||
};
|
||||
|
||||
static uint32_t
|
||||
sub_word(uint32_t x)
|
||||
{
|
||||
uint32_t q[8];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] = x;
|
||||
}
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
br_aes_ct_ortho(q);
|
||||
return q[0];
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_ct_keysched(uint32_t *comp_skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, j, k, nk, nkf;
|
||||
uint32_t tmp;
|
||||
uint32_t skey[120];
|
||||
|
||||
switch (key_len) {
|
||||
case 16:
|
||||
num_rounds = 10;
|
||||
break;
|
||||
case 24:
|
||||
num_rounds = 12;
|
||||
break;
|
||||
case 32:
|
||||
num_rounds = 14;
|
||||
break;
|
||||
default:
|
||||
/* abort(); */
|
||||
return 0;
|
||||
}
|
||||
nk = (int)(key_len >> 2);
|
||||
nkf = (int)((num_rounds + 1) << 2);
|
||||
tmp = 0;
|
||||
for (i = 0; i < nk; i ++) {
|
||||
tmp = br_dec32le((const unsigned char *)key + (i << 2));
|
||||
skey[(i << 1) + 0] = tmp;
|
||||
skey[(i << 1) + 1] = tmp;
|
||||
}
|
||||
for (i = nk, j = 0, k = 0; i < nkf; i ++) {
|
||||
if (j == 0) {
|
||||
tmp = (tmp << 24) | (tmp >> 8);
|
||||
tmp = sub_word(tmp) ^ Rcon[k];
|
||||
} else if (nk > 6 && j == 4) {
|
||||
tmp = sub_word(tmp);
|
||||
}
|
||||
tmp ^= skey[(i - nk) << 1];
|
||||
skey[(i << 1) + 0] = tmp;
|
||||
skey[(i << 1) + 1] = tmp;
|
||||
if (++ j == nk) {
|
||||
j = 0;
|
||||
k ++;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < nkf; i += 4) {
|
||||
br_aes_ct_ortho(skey + (i << 1));
|
||||
}
|
||||
for (i = 0, j = 0; i < nkf; i ++, j += 2) {
|
||||
comp_skey[i] = (skey[j + 0] & 0x55555555)
|
||||
| (skey[j + 1] & 0xAAAAAAAA);
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_skey_expand(uint32_t *skey,
|
||||
unsigned num_rounds, const uint32_t *comp_skey)
|
||||
{
|
||||
unsigned u, v, n;
|
||||
|
||||
n = (num_rounds + 1) << 2;
|
||||
for (u = 0, v = 0; u < n; u ++, v += 2) {
|
||||
uint32_t x, y;
|
||||
|
||||
x = y = comp_skey[u];
|
||||
x &= 0x55555555;
|
||||
skey[v + 0] = x | (x << 1);
|
||||
y &= 0xAAAAAAAA;
|
||||
skey[v + 1] = y | (y >> 1);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,398 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_Sbox(uint64_t *q)
|
||||
{
|
||||
/*
|
||||
* This S-box implementation is a straightforward translation of
|
||||
* the circuit described by Boyar and Peralta in "A new
|
||||
* combinational logic minimization technique with applications
|
||||
* to cryptology" (https://eprint.iacr.org/2009/191.pdf).
|
||||
*
|
||||
* Note that variables x* (input) and s* (output) are numbered
|
||||
* in "reverse" order (x0 is the high bit, x7 is the low bit).
|
||||
*/
|
||||
|
||||
uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
|
||||
uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
|
||||
uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
|
||||
uint64_t y20, y21;
|
||||
uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
|
||||
uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
|
||||
uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
|
||||
uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
|
||||
uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
|
||||
uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
|
||||
uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
|
||||
uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
|
||||
uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
|
||||
uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
x0 = q[7];
|
||||
x1 = q[6];
|
||||
x2 = q[5];
|
||||
x3 = q[4];
|
||||
x4 = q[3];
|
||||
x5 = q[2];
|
||||
x6 = q[1];
|
||||
x7 = q[0];
|
||||
|
||||
/*
|
||||
* Top linear transformation.
|
||||
*/
|
||||
y14 = x3 ^ x5;
|
||||
y13 = x0 ^ x6;
|
||||
y9 = x0 ^ x3;
|
||||
y8 = x0 ^ x5;
|
||||
t0 = x1 ^ x2;
|
||||
y1 = t0 ^ x7;
|
||||
y4 = y1 ^ x3;
|
||||
y12 = y13 ^ y14;
|
||||
y2 = y1 ^ x0;
|
||||
y5 = y1 ^ x6;
|
||||
y3 = y5 ^ y8;
|
||||
t1 = x4 ^ y12;
|
||||
y15 = t1 ^ x5;
|
||||
y20 = t1 ^ x1;
|
||||
y6 = y15 ^ x7;
|
||||
y10 = y15 ^ t0;
|
||||
y11 = y20 ^ y9;
|
||||
y7 = x7 ^ y11;
|
||||
y17 = y10 ^ y11;
|
||||
y19 = y10 ^ y8;
|
||||
y16 = t0 ^ y11;
|
||||
y21 = y13 ^ y16;
|
||||
y18 = x0 ^ y16;
|
||||
|
||||
/*
|
||||
* Non-linear section.
|
||||
*/
|
||||
t2 = y12 & y15;
|
||||
t3 = y3 & y6;
|
||||
t4 = t3 ^ t2;
|
||||
t5 = y4 & x7;
|
||||
t6 = t5 ^ t2;
|
||||
t7 = y13 & y16;
|
||||
t8 = y5 & y1;
|
||||
t9 = t8 ^ t7;
|
||||
t10 = y2 & y7;
|
||||
t11 = t10 ^ t7;
|
||||
t12 = y9 & y11;
|
||||
t13 = y14 & y17;
|
||||
t14 = t13 ^ t12;
|
||||
t15 = y8 & y10;
|
||||
t16 = t15 ^ t12;
|
||||
t17 = t4 ^ t14;
|
||||
t18 = t6 ^ t16;
|
||||
t19 = t9 ^ t14;
|
||||
t20 = t11 ^ t16;
|
||||
t21 = t17 ^ y20;
|
||||
t22 = t18 ^ y19;
|
||||
t23 = t19 ^ y21;
|
||||
t24 = t20 ^ y18;
|
||||
|
||||
t25 = t21 ^ t22;
|
||||
t26 = t21 & t23;
|
||||
t27 = t24 ^ t26;
|
||||
t28 = t25 & t27;
|
||||
t29 = t28 ^ t22;
|
||||
t30 = t23 ^ t24;
|
||||
t31 = t22 ^ t26;
|
||||
t32 = t31 & t30;
|
||||
t33 = t32 ^ t24;
|
||||
t34 = t23 ^ t33;
|
||||
t35 = t27 ^ t33;
|
||||
t36 = t24 & t35;
|
||||
t37 = t36 ^ t34;
|
||||
t38 = t27 ^ t36;
|
||||
t39 = t29 & t38;
|
||||
t40 = t25 ^ t39;
|
||||
|
||||
t41 = t40 ^ t37;
|
||||
t42 = t29 ^ t33;
|
||||
t43 = t29 ^ t40;
|
||||
t44 = t33 ^ t37;
|
||||
t45 = t42 ^ t41;
|
||||
z0 = t44 & y15;
|
||||
z1 = t37 & y6;
|
||||
z2 = t33 & x7;
|
||||
z3 = t43 & y16;
|
||||
z4 = t40 & y1;
|
||||
z5 = t29 & y7;
|
||||
z6 = t42 & y11;
|
||||
z7 = t45 & y17;
|
||||
z8 = t41 & y10;
|
||||
z9 = t44 & y12;
|
||||
z10 = t37 & y3;
|
||||
z11 = t33 & y4;
|
||||
z12 = t43 & y13;
|
||||
z13 = t40 & y5;
|
||||
z14 = t29 & y2;
|
||||
z15 = t42 & y9;
|
||||
z16 = t45 & y14;
|
||||
z17 = t41 & y8;
|
||||
|
||||
/*
|
||||
* Bottom linear transformation.
|
||||
*/
|
||||
t46 = z15 ^ z16;
|
||||
t47 = z10 ^ z11;
|
||||
t48 = z5 ^ z13;
|
||||
t49 = z9 ^ z10;
|
||||
t50 = z2 ^ z12;
|
||||
t51 = z2 ^ z5;
|
||||
t52 = z7 ^ z8;
|
||||
t53 = z0 ^ z3;
|
||||
t54 = z6 ^ z7;
|
||||
t55 = z16 ^ z17;
|
||||
t56 = z12 ^ t48;
|
||||
t57 = t50 ^ t53;
|
||||
t58 = z4 ^ t46;
|
||||
t59 = z3 ^ t54;
|
||||
t60 = t46 ^ t57;
|
||||
t61 = z14 ^ t57;
|
||||
t62 = t52 ^ t58;
|
||||
t63 = t49 ^ t58;
|
||||
t64 = z4 ^ t59;
|
||||
t65 = t61 ^ t62;
|
||||
t66 = z1 ^ t63;
|
||||
s0 = t59 ^ t63;
|
||||
s6 = t56 ^ ~t62;
|
||||
s7 = t48 ^ ~t60;
|
||||
t67 = t64 ^ t65;
|
||||
s3 = t53 ^ t66;
|
||||
s4 = t51 ^ t66;
|
||||
s5 = t47 ^ t65;
|
||||
s1 = t64 ^ ~s3;
|
||||
s2 = t55 ^ ~t67;
|
||||
|
||||
q[7] = s0;
|
||||
q[6] = s1;
|
||||
q[5] = s2;
|
||||
q[4] = s3;
|
||||
q[3] = s4;
|
||||
q[2] = s5;
|
||||
q[1] = s6;
|
||||
q[0] = s7;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_ortho(uint64_t *q)
|
||||
{
|
||||
#define SWAPN(cl, ch, s, x, y) do { \
|
||||
uint64_t a, b; \
|
||||
a = (x); \
|
||||
b = (y); \
|
||||
(x) = (a & (uint64_t)cl) | ((b & (uint64_t)cl) << (s)); \
|
||||
(y) = ((a & (uint64_t)ch) >> (s)) | (b & (uint64_t)ch); \
|
||||
} while (0)
|
||||
|
||||
#define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)
|
||||
#define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)
|
||||
#define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y)
|
||||
|
||||
SWAP2(q[0], q[1]);
|
||||
SWAP2(q[2], q[3]);
|
||||
SWAP2(q[4], q[5]);
|
||||
SWAP2(q[6], q[7]);
|
||||
|
||||
SWAP4(q[0], q[2]);
|
||||
SWAP4(q[1], q[3]);
|
||||
SWAP4(q[4], q[6]);
|
||||
SWAP4(q[5], q[7]);
|
||||
|
||||
SWAP8(q[0], q[4]);
|
||||
SWAP8(q[1], q[5]);
|
||||
SWAP8(q[2], q[6]);
|
||||
SWAP8(q[3], q[7]);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w)
|
||||
{
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = w[0];
|
||||
x1 = w[1];
|
||||
x2 = w[2];
|
||||
x3 = w[3];
|
||||
x0 |= (x0 << 16);
|
||||
x1 |= (x1 << 16);
|
||||
x2 |= (x2 << 16);
|
||||
x3 |= (x3 << 16);
|
||||
x0 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x1 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x2 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x3 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x0 |= (x0 << 8);
|
||||
x1 |= (x1 << 8);
|
||||
x2 |= (x2 << 8);
|
||||
x3 |= (x3 << 8);
|
||||
x0 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x1 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x2 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
x3 &= (uint64_t)0x00FF00FF00FF00FF;
|
||||
*q0 = x0 | (x2 << 8);
|
||||
*q1 = x1 | (x3 << 8);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1)
|
||||
{
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
|
||||
x0 |= (x0 >> 8);
|
||||
x1 |= (x1 >> 8);
|
||||
x2 |= (x2 >> 8);
|
||||
x3 |= (x3 >> 8);
|
||||
x0 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x1 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x2 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
x3 &= (uint64_t)0x0000FFFF0000FFFF;
|
||||
w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
|
||||
w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
|
||||
w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
|
||||
w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
|
||||
}
|
||||
|
||||
static const unsigned char Rcon[] = {
|
||||
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
|
||||
};
|
||||
|
||||
static uint32_t
|
||||
sub_word(uint32_t x)
|
||||
{
|
||||
uint64_t q[8];
|
||||
|
||||
memset(q, 0, sizeof q);
|
||||
q[0] = x;
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
br_aes_ct64_ortho(q);
|
||||
return (uint32_t)q[0];
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
unsigned
|
||||
br_aes_ct64_keysched(uint64_t *comp_skey, const void *key, size_t key_len)
|
||||
{
|
||||
unsigned num_rounds;
|
||||
int i, j, k, nk, nkf;
|
||||
uint32_t tmp;
|
||||
uint32_t skey[60];
|
||||
|
||||
switch (key_len) {
|
||||
case 16:
|
||||
num_rounds = 10;
|
||||
break;
|
||||
case 24:
|
||||
num_rounds = 12;
|
||||
break;
|
||||
case 32:
|
||||
num_rounds = 14;
|
||||
break;
|
||||
default:
|
||||
/* abort(); */
|
||||
return 0;
|
||||
}
|
||||
nk = (int)(key_len >> 2);
|
||||
nkf = (int)((num_rounds + 1) << 2);
|
||||
br_range_dec32le(skey, (key_len >> 2), key);
|
||||
tmp = skey[(key_len >> 2) - 1];
|
||||
for (i = nk, j = 0, k = 0; i < nkf; i ++) {
|
||||
if (j == 0) {
|
||||
tmp = (tmp << 24) | (tmp >> 8);
|
||||
tmp = sub_word(tmp) ^ Rcon[k];
|
||||
} else if (nk > 6 && j == 4) {
|
||||
tmp = sub_word(tmp);
|
||||
}
|
||||
tmp ^= skey[i - nk];
|
||||
skey[i] = tmp;
|
||||
if (++ j == nk) {
|
||||
j = 0;
|
||||
k ++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0, j = 0; i < nkf; i += 4, j += 2) {
|
||||
uint64_t q[8];
|
||||
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);
|
||||
q[1] = q[0];
|
||||
q[2] = q[0];
|
||||
q[3] = q[0];
|
||||
q[5] = q[4];
|
||||
q[6] = q[4];
|
||||
q[7] = q[4];
|
||||
br_aes_ct64_ortho(q);
|
||||
comp_skey[j + 0] =
|
||||
(q[0] & (uint64_t)0x1111111111111111)
|
||||
| (q[1] & (uint64_t)0x2222222222222222)
|
||||
| (q[2] & (uint64_t)0x4444444444444444)
|
||||
| (q[3] & (uint64_t)0x8888888888888888);
|
||||
comp_skey[j + 1] =
|
||||
(q[4] & (uint64_t)0x1111111111111111)
|
||||
| (q[5] & (uint64_t)0x2222222222222222)
|
||||
| (q[6] & (uint64_t)0x4444444444444444)
|
||||
| (q[7] & (uint64_t)0x8888888888888888);
|
||||
}
|
||||
return num_rounds;
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_skey_expand(uint64_t *skey,
|
||||
unsigned num_rounds, const uint64_t *comp_skey)
|
||||
{
|
||||
unsigned u, v, n;
|
||||
|
||||
n = (num_rounds + 1) << 1;
|
||||
for (u = 0, v = 0; u < n; u ++, v += 4) {
|
||||
uint64_t x0, x1, x2, x3;
|
||||
|
||||
x0 = x1 = x2 = x3 = comp_skey[u];
|
||||
x0 &= (uint64_t)0x1111111111111111;
|
||||
x1 &= (uint64_t)0x2222222222222222;
|
||||
x2 &= (uint64_t)0x4444444444444444;
|
||||
x3 &= (uint64_t)0x8888888888888888;
|
||||
x1 >>= 1;
|
||||
x2 >>= 2;
|
||||
x3 >>= 3;
|
||||
skey[v + 0] = (x0 << 4) - x0;
|
||||
skey[v + 1] = (x1 << 4) - x1;
|
||||
skey[v + 2] = (x2 << 4) - x2;
|
||||
skey[v + 3] = (x3 << 4) - x3;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcdec_init(br_aes_ct64_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcdec_run(const br_aes_ct64_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint64_t sk_exp[120];
|
||||
uint32_t ivw[4];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 4, iv);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint64_t q[8];
|
||||
uint32_t w1[16], w2[16];
|
||||
int i;
|
||||
|
||||
if (len >= 64) {
|
||||
br_range_dec32le(w1, 16, buf);
|
||||
} else {
|
||||
br_range_dec32le(w1, len >> 2, buf);
|
||||
}
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_in(
|
||||
&q[i], &q[i + 4], w1 + (i << 2));
|
||||
}
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_out(
|
||||
w2 + (i << 2), q[i], q[i + 4]);
|
||||
}
|
||||
for (i = 0; i < 4; i ++) {
|
||||
w2[i] ^= ivw[i];
|
||||
}
|
||||
if (len >= 64) {
|
||||
for (i = 4; i < 16; i ++) {
|
||||
w2[i] ^= w1[i - 4];
|
||||
}
|
||||
memcpy(ivw, w1 + 12, sizeof ivw);
|
||||
br_range_enc32le(buf, w2, 16);
|
||||
} else {
|
||||
int j;
|
||||
|
||||
j = (int)(len >> 2);
|
||||
for (i = 4; i < j; i ++) {
|
||||
w2[i] ^= w1[i - 4];
|
||||
}
|
||||
memcpy(ivw, w1 + j - 4, sizeof ivw);
|
||||
br_range_enc32le(buf, w2, j);
|
||||
break;
|
||||
}
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
}
|
||||
br_range_enc32le(iv, ivw, 4);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_ct64_cbcdec_vtable = {
|
||||
sizeof(br_aes_ct64_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_ct64_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct64_cbcdec_run
|
||||
};
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcenc_init(br_aes_ct64_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_cbcenc_run(const br_aes_ct64_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint64_t sk_exp[120];
|
||||
uint32_t ivw[4];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 4, iv);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t w[4];
|
||||
uint64_t q[8];
|
||||
|
||||
w[0] = ivw[0] ^ br_dec32le(buf);
|
||||
w[1] = ivw[1] ^ br_dec32le(buf + 4);
|
||||
w[2] = ivw[2] ^ br_dec32le(buf + 8);
|
||||
w[3] = ivw[3] ^ br_dec32le(buf + 12);
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
memcpy(ivw, w, sizeof w);
|
||||
br_enc32le(buf, w[0]);
|
||||
br_enc32le(buf + 4, w[1]);
|
||||
br_enc32le(buf + 8, w[2]);
|
||||
br_enc32le(buf + 12, w[3]);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
br_range_enc32le(iv, ivw, 4);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_ct64_cbcenc_vtable = {
|
||||
sizeof(br_aes_ct64_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_ct64_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct64_cbcenc_run
|
||||
};
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctr_init(br_aes_ct64_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_ct64_ctr_run(const br_aes_ct64_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
uint32_t ivw[16];
|
||||
uint64_t sk_exp[120];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
br_range_dec32le(ivw, 3, iv);
|
||||
memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t));
|
||||
memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t));
|
||||
memcpy(ivw + 12, ivw, 3 * sizeof(uint32_t));
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint64_t q[8];
|
||||
uint32_t w[16];
|
||||
unsigned char tmp[64];
|
||||
int i;
|
||||
|
||||
/*
|
||||
* TODO: see if we can save on the first br_aes_ct64_ortho()
|
||||
* call, since iv0/iv1/iv2 are constant for the whole run.
|
||||
*/
|
||||
memcpy(w, ivw, sizeof ivw);
|
||||
w[3] = br_swap32(cc);
|
||||
w[7] = br_swap32(cc + 1);
|
||||
w[11] = br_swap32(cc + 2);
|
||||
w[15] = br_swap32(cc + 3);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_in(
|
||||
&q[i], &q[i + 4], w + (i << 2));
|
||||
}
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_out(
|
||||
w + (i << 2), q[i], q[i + 4]);
|
||||
}
|
||||
br_range_enc32le(tmp, w, 16);
|
||||
if (len <= 64) {
|
||||
xorbuf(buf, tmp, len);
|
||||
cc += (uint32_t)len >> 4;
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 64);
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
cc += 4;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_ct64_ctr_vtable = {
|
||||
sizeof(br_aes_ct64_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_ct64_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_ct64_ctr_run
|
||||
};
|
||||
|
|
@ -1,433 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctrcbc_init(br_aes_ct64_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct64_ctrcbc_vtable;
|
||||
ctx->num_rounds = br_aes_ct64_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctrcbc_ctr(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint64_t sk_exp[120];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint64_t q[8];
|
||||
uint32_t w[16];
|
||||
unsigned char tmp[64];
|
||||
int i, j;
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
j = (len >= 64) ? 16 : (int)(len >> 2);
|
||||
for (i = 0; i < j; i += 4) {
|
||||
uint32_t carry;
|
||||
|
||||
w[i + 0] = br_swap32(iv0);
|
||||
w[i + 1] = br_swap32(iv1);
|
||||
w[i + 2] = br_swap32(iv2);
|
||||
w[i + 3] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
}
|
||||
memset(w + i, 0, (16 - i) * sizeof(uint32_t));
|
||||
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_in(
|
||||
&q[i], &q[i + 4], w + (i << 2));
|
||||
}
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
for (i = 0; i < 4; i ++) {
|
||||
br_aes_ct64_interleave_out(
|
||||
w + (i << 2), q[i], q[i + 4]);
|
||||
}
|
||||
|
||||
br_range_enc32le(tmp, w, 16);
|
||||
if (len <= 64) {
|
||||
xorbuf(buf, tmp, len);
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 64);
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
}
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctrcbc_mac(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len)
|
||||
{
|
||||
const unsigned char *buf;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint64_t q[8];
|
||||
uint64_t sk_exp[120];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
|
||||
buf = data;
|
||||
memset(q, 0, sizeof q);
|
||||
while (len > 0) {
|
||||
uint32_t w[4];
|
||||
|
||||
w[0] = cm0 ^ br_dec32le(buf + 0);
|
||||
w[1] = cm1 ^ br_dec32le(buf + 4);
|
||||
w[2] = cm2 ^ br_dec32le(buf + 8);
|
||||
w[3] = cm3 ^ br_dec32le(buf + 12);
|
||||
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
|
||||
cm0 = w[0];
|
||||
cm1 = w[1];
|
||||
cm2 = w[2];
|
||||
cm3 = w[3];
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctrcbc_encrypt(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
/*
|
||||
* When encrypting, the CBC-MAC processing must be lagging by
|
||||
* one block, since it operates on the encrypted values, so
|
||||
* it must wait for that encryption to complete.
|
||||
*/
|
||||
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint64_t sk_exp[120];
|
||||
uint64_t q[8];
|
||||
int first_iter;
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
/*
|
||||
* The current CBC-MAC value is kept in little-endian convention.
|
||||
*/
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
|
||||
buf = data;
|
||||
first_iter = 1;
|
||||
memset(q, 0, sizeof q);
|
||||
while (len > 0) {
|
||||
uint32_t w[8], carry;
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
w[0] = br_swap32(iv0);
|
||||
w[1] = br_swap32(iv1);
|
||||
w[2] = br_swap32(iv2);
|
||||
w[3] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
|
||||
/*
|
||||
* The block for CBC-MAC.
|
||||
*/
|
||||
w[4] = cm0;
|
||||
w[5] = cm1;
|
||||
w[6] = cm2;
|
||||
w[7] = cm3;
|
||||
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_interleave_in(&q[1], &q[5], w + 4);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
br_aes_ct64_interleave_out(w + 4, q[1], q[5]);
|
||||
|
||||
/*
|
||||
* We do the XOR with the plaintext in 32-bit registers,
|
||||
* so that the value are available for CBC-MAC processing
|
||||
* as well.
|
||||
*/
|
||||
w[0] ^= br_dec32le(buf + 0);
|
||||
w[1] ^= br_dec32le(buf + 4);
|
||||
w[2] ^= br_dec32le(buf + 8);
|
||||
w[3] ^= br_dec32le(buf + 12);
|
||||
br_enc32le(buf + 0, w[0]);
|
||||
br_enc32le(buf + 4, w[1]);
|
||||
br_enc32le(buf + 8, w[2]);
|
||||
br_enc32le(buf + 12, w[3]);
|
||||
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
|
||||
/*
|
||||
* We set the cm* values to the block to encrypt in the
|
||||
* next iteration.
|
||||
*/
|
||||
if (first_iter) {
|
||||
first_iter = 0;
|
||||
cm0 ^= w[0];
|
||||
cm1 ^= w[1];
|
||||
cm2 ^= w[2];
|
||||
cm3 ^= w[3];
|
||||
} else {
|
||||
cm0 = w[0] ^ w[4];
|
||||
cm1 = w[1] ^ w[5];
|
||||
cm2 = w[2] ^ w[6];
|
||||
cm3 = w[3] ^ w[7];
|
||||
}
|
||||
|
||||
/*
|
||||
* If this was the last iteration, then compute the
|
||||
* extra block encryption to complete CBC-MAC.
|
||||
*/
|
||||
if (len == 0) {
|
||||
w[0] = cm0;
|
||||
w[1] = cm1;
|
||||
w[2] = cm2;
|
||||
w[3] = cm3;
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(
|
||||
ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
cm0 = w[0];
|
||||
cm1 = w[1];
|
||||
cm2 = w[2];
|
||||
cm3 = w[3];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct64_ctrcbc_decrypt(const br_aes_ct64_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint64_t sk_exp[120];
|
||||
uint64_t q[8];
|
||||
|
||||
br_aes_ct64_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
/*
|
||||
* The current CBC-MAC value is kept in little-endian convention.
|
||||
*/
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
|
||||
buf = data;
|
||||
memset(q, 0, sizeof q);
|
||||
while (len > 0) {
|
||||
uint32_t w[8], carry;
|
||||
unsigned char tmp[16];
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
w[0] = br_swap32(iv0);
|
||||
w[1] = br_swap32(iv1);
|
||||
w[2] = br_swap32(iv2);
|
||||
w[3] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
|
||||
/*
|
||||
* The block for CBC-MAC.
|
||||
*/
|
||||
w[4] = cm0 ^ br_dec32le(buf + 0);
|
||||
w[5] = cm1 ^ br_dec32le(buf + 4);
|
||||
w[6] = cm2 ^ br_dec32le(buf + 8);
|
||||
w[7] = cm3 ^ br_dec32le(buf + 12);
|
||||
|
||||
br_aes_ct64_interleave_in(&q[0], &q[4], w);
|
||||
br_aes_ct64_interleave_in(&q[1], &q[5], w + 4);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct64_ortho(q);
|
||||
br_aes_ct64_interleave_out(w, q[0], q[4]);
|
||||
br_aes_ct64_interleave_out(w + 4, q[1], q[5]);
|
||||
|
||||
br_enc32le(tmp + 0, w[0]);
|
||||
br_enc32le(tmp + 4, w[1]);
|
||||
br_enc32le(tmp + 8, w[2]);
|
||||
br_enc32le(tmp + 12, w[3]);
|
||||
xorbuf(buf, tmp, 16);
|
||||
cm0 = w[4];
|
||||
cm1 = w[5];
|
||||
cm2 = w[6];
|
||||
cm3 = w[7];
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctrcbc_class br_aes_ct64_ctrcbc_vtable = {
|
||||
sizeof(br_aes_ct64_ctrcbc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctrcbc_class **, const void *, size_t))
|
||||
&br_aes_ct64_ctrcbc_init,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_ct64_ctrcbc_encrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_ct64_ctrcbc_decrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, size_t))
|
||||
&br_aes_ct64_ctrcbc_ctr,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, const void *, size_t))
|
||||
&br_aes_ct64_ctrcbc_mac
|
||||
};
|
||||
|
|
@ -1,159 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_invSbox(uint64_t *q)
|
||||
{
|
||||
/*
|
||||
* See br_aes_ct_bitslice_invSbox(). This is the natural extension
|
||||
* to 64-bit registers.
|
||||
*/
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
}
|
||||
|
||||
static void
|
||||
add_round_key(uint64_t *q, const uint64_t *sk)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] ^= sk[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(uint64_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint64_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & (uint64_t)0x000000000000FFFF)
|
||||
| ((x & (uint64_t)0x000000000FFF0000) << 4)
|
||||
| ((x & (uint64_t)0x00000000F0000000) >> 12)
|
||||
| ((x & (uint64_t)0x000000FF00000000) << 8)
|
||||
| ((x & (uint64_t)0x0000FF0000000000) >> 8)
|
||||
| ((x & (uint64_t)0x000F000000000000) << 12)
|
||||
| ((x & (uint64_t)0xFFF0000000000000) >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
rotr32(uint64_t x)
|
||||
{
|
||||
return (x << 32) | (x >> 32);
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(uint64_t *q)
|
||||
{
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 16) | (q0 << 48);
|
||||
r1 = (q1 >> 16) | (q1 << 48);
|
||||
r2 = (q2 >> 16) | (q2 << 48);
|
||||
r3 = (q3 >> 16) | (q3 << 48);
|
||||
r4 = (q4 >> 16) | (q4 << 48);
|
||||
r5 = (q5 >> 16) | (q5 << 48);
|
||||
r6 = (q6 >> 16) | (q6 << 48);
|
||||
r7 = (q7 >> 16) | (q7 << 48);
|
||||
|
||||
q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr32(q0 ^ q5 ^ q6 ^ r0 ^ r5);
|
||||
q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
|
||||
q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr32(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
|
||||
q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr32(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
|
||||
q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
|
||||
q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr32(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
|
||||
q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr32(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
|
||||
q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr32(q4 ^ q5 ^ q7 ^ r4 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
|
||||
const uint64_t *skey, uint64_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct64_bitslice_invSbox(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
inv_mix_columns(q);
|
||||
}
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct64_bitslice_invSbox(q);
|
||||
add_round_key(q, skey);
|
||||
}
|
||||
|
|
@ -1,115 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
static inline void
|
||||
add_round_key(uint64_t *q, const uint64_t *sk)
|
||||
{
|
||||
q[0] ^= sk[0];
|
||||
q[1] ^= sk[1];
|
||||
q[2] ^= sk[2];
|
||||
q[3] ^= sk[3];
|
||||
q[4] ^= sk[4];
|
||||
q[5] ^= sk[5];
|
||||
q[6] ^= sk[6];
|
||||
q[7] ^= sk[7];
|
||||
}
|
||||
|
||||
static inline void
|
||||
shift_rows(uint64_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint64_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & (uint64_t)0x000000000000FFFF)
|
||||
| ((x & (uint64_t)0x00000000FFF00000) >> 4)
|
||||
| ((x & (uint64_t)0x00000000000F0000) << 12)
|
||||
| ((x & (uint64_t)0x0000FF0000000000) >> 8)
|
||||
| ((x & (uint64_t)0x000000FF00000000) << 8)
|
||||
| ((x & (uint64_t)0xF000000000000000) >> 12)
|
||||
| ((x & (uint64_t)0x0FFF000000000000) << 4);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
rotr32(uint64_t x)
|
||||
{
|
||||
return (x << 32) | (x >> 32);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mix_columns(uint64_t *q)
|
||||
{
|
||||
uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 16) | (q0 << 48);
|
||||
r1 = (q1 >> 16) | (q1 << 48);
|
||||
r2 = (q2 >> 16) | (q2 << 48);
|
||||
r3 = (q3 >> 16) | (q3 << 48);
|
||||
r4 = (q4 >> 16) | (q4 << 48);
|
||||
r5 = (q5 >> 16) | (q5 << 48);
|
||||
r6 = (q6 >> 16) | (q6 << 48);
|
||||
r7 = (q7 >> 16) | (q7 << 48);
|
||||
|
||||
q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
|
||||
q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
|
||||
q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
|
||||
q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
|
||||
q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
|
||||
q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
|
||||
q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
|
||||
q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
|
||||
const uint64_t *skey, uint64_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
mix_columns(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
}
|
||||
br_aes_ct64_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
}
|
||||
|
|
@ -1,111 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcdec_init(br_aes_ct_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcdec_run(const br_aes_ct_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
iv3 = br_dec32le(ivbuf + 12);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8], sq[8];
|
||||
|
||||
q[0] = br_dec32le(buf);
|
||||
q[2] = br_dec32le(buf + 4);
|
||||
q[4] = br_dec32le(buf + 8);
|
||||
q[6] = br_dec32le(buf + 12);
|
||||
if (len >= 32) {
|
||||
q[1] = br_dec32le(buf + 16);
|
||||
q[3] = br_dec32le(buf + 20);
|
||||
q[5] = br_dec32le(buf + 24);
|
||||
q[7] = br_dec32le(buf + 28);
|
||||
} else {
|
||||
q[1] = 0;
|
||||
q[3] = 0;
|
||||
q[5] = 0;
|
||||
q[7] = 0;
|
||||
}
|
||||
memcpy(sq, q, sizeof q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_decrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_enc32le(buf, q[0] ^ iv0);
|
||||
br_enc32le(buf + 4, q[2] ^ iv1);
|
||||
br_enc32le(buf + 8, q[4] ^ iv2);
|
||||
br_enc32le(buf + 12, q[6] ^ iv3);
|
||||
if (len < 32) {
|
||||
iv0 = sq[0];
|
||||
iv1 = sq[2];
|
||||
iv2 = sq[4];
|
||||
iv3 = sq[6];
|
||||
break;
|
||||
}
|
||||
br_enc32le(buf + 16, q[1] ^ sq[0]);
|
||||
br_enc32le(buf + 20, q[3] ^ sq[2]);
|
||||
br_enc32le(buf + 24, q[5] ^ sq[4]);
|
||||
br_enc32le(buf + 28, q[7] ^ sq[6]);
|
||||
iv0 = sq[1];
|
||||
iv1 = sq[3];
|
||||
iv2 = sq[5];
|
||||
iv3 = sq[7];
|
||||
buf += 32;
|
||||
len -= 32;
|
||||
}
|
||||
br_enc32le(ivbuf, iv0);
|
||||
br_enc32le(ivbuf + 4, iv1);
|
||||
br_enc32le(ivbuf + 8, iv2);
|
||||
br_enc32le(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_ct_cbcdec_vtable = {
|
||||
sizeof(br_aes_ct_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_ct_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct_cbcdec_run
|
||||
};
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcenc_init(br_aes_ct_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_cbcenc_run(const br_aes_ct_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
uint32_t q[8];
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
q[1] = 0;
|
||||
q[3] = 0;
|
||||
q[5] = 0;
|
||||
q[7] = 0;
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
iv3 = br_dec32le(ivbuf + 12);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
q[0] = iv0 ^ br_dec32le(buf);
|
||||
q[2] = iv1 ^ br_dec32le(buf + 4);
|
||||
q[4] = iv2 ^ br_dec32le(buf + 8);
|
||||
q[6] = iv3 ^ br_dec32le(buf + 12);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
iv0 = q[0];
|
||||
iv1 = q[2];
|
||||
iv2 = q[4];
|
||||
iv3 = q[6];
|
||||
br_enc32le(buf, iv0);
|
||||
br_enc32le(buf + 4, iv1);
|
||||
br_enc32le(buf + 8, iv2);
|
||||
br_enc32le(buf + 12, iv3);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
br_enc32le(ivbuf, iv0);
|
||||
br_enc32le(ivbuf + 4, iv1);
|
||||
br_enc32le(ivbuf + 8, iv2);
|
||||
br_enc32le(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_ct_cbcenc_vtable = {
|
||||
sizeof(br_aes_ct_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_ct_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_ct_cbcenc_run
|
||||
};
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctr_init(br_aes_ct_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_ct_ctr_run(const br_aes_ct_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
const unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
ivbuf = iv;
|
||||
iv0 = br_dec32le(ivbuf);
|
||||
iv1 = br_dec32le(ivbuf + 4);
|
||||
iv2 = br_dec32le(ivbuf + 8);
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8];
|
||||
unsigned char tmp[32];
|
||||
|
||||
/*
|
||||
* TODO: see if we can save on the first br_aes_ct_ortho()
|
||||
* call, since iv0/iv1/iv2 are constant for the whole run.
|
||||
*/
|
||||
q[0] = q[1] = iv0;
|
||||
q[2] = q[3] = iv1;
|
||||
q[4] = q[5] = iv2;
|
||||
q[6] = br_swap32(cc);
|
||||
q[7] = br_swap32(cc + 1);
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
br_enc32le(tmp, q[0]);
|
||||
br_enc32le(tmp + 4, q[2]);
|
||||
br_enc32le(tmp + 8, q[4]);
|
||||
br_enc32le(tmp + 12, q[6]);
|
||||
br_enc32le(tmp + 16, q[1]);
|
||||
br_enc32le(tmp + 20, q[3]);
|
||||
br_enc32le(tmp + 24, q[5]);
|
||||
br_enc32le(tmp + 28, q[7]);
|
||||
|
||||
if (len <= 32) {
|
||||
xorbuf(buf, tmp, len);
|
||||
cc ++;
|
||||
if (len > 16) {
|
||||
cc ++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 32);
|
||||
buf += 32;
|
||||
len -= 32;
|
||||
cc += 2;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_ct_ctr_vtable = {
|
||||
sizeof(br_aes_ct_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_ct_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_ct_ctr_run
|
||||
};
|
||||
|
|
@ -1,422 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctrcbc_init(br_aes_ct_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_ct_ctrcbc_vtable;
|
||||
ctx->num_rounds = br_aes_ct_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctrcbc_ctr(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8], carry;
|
||||
unsigned char tmp[32];
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
q[0] = br_swap32(iv0);
|
||||
q[2] = br_swap32(iv1);
|
||||
q[4] = br_swap32(iv2);
|
||||
q[6] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
q[1] = br_swap32(iv0);
|
||||
q[3] = br_swap32(iv1);
|
||||
q[5] = br_swap32(iv2);
|
||||
q[7] = br_swap32(iv3);
|
||||
if (len > 16) {
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
}
|
||||
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
|
||||
br_enc32le(tmp, q[0]);
|
||||
br_enc32le(tmp + 4, q[2]);
|
||||
br_enc32le(tmp + 8, q[4]);
|
||||
br_enc32le(tmp + 12, q[6]);
|
||||
br_enc32le(tmp + 16, q[1]);
|
||||
br_enc32le(tmp + 20, q[3]);
|
||||
br_enc32le(tmp + 24, q[5]);
|
||||
br_enc32le(tmp + 28, q[7]);
|
||||
|
||||
if (len <= 32) {
|
||||
xorbuf(buf, tmp, len);
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 32);
|
||||
buf += 32;
|
||||
len -= 32;
|
||||
}
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctrcbc_mac(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len)
|
||||
{
|
||||
const unsigned char *buf;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint32_t q[8];
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
buf = data;
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
q[1] = 0;
|
||||
q[3] = 0;
|
||||
q[5] = 0;
|
||||
q[7] = 0;
|
||||
|
||||
while (len > 0) {
|
||||
q[0] = cm0 ^ br_dec32le(buf + 0);
|
||||
q[2] = cm1 ^ br_dec32le(buf + 4);
|
||||
q[4] = cm2 ^ br_dec32le(buf + 8);
|
||||
q[6] = cm3 ^ br_dec32le(buf + 12);
|
||||
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
|
||||
cm0 = q[0];
|
||||
cm1 = q[2];
|
||||
cm2 = q[4];
|
||||
cm3 = q[6];
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctrcbc_encrypt(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
/*
|
||||
* When encrypting, the CBC-MAC processing must be lagging by
|
||||
* one block, since it operates on the encrypted values, so
|
||||
* it must wait for that encryption to complete.
|
||||
*/
|
||||
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint32_t sk_exp[120];
|
||||
int first_iter;
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
/*
|
||||
* The current CBC-MAC value is kept in little-endian convention.
|
||||
*/
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
|
||||
buf = data;
|
||||
first_iter = 1;
|
||||
while (len > 0) {
|
||||
uint32_t q[8], carry;
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
q[0] = br_swap32(iv0);
|
||||
q[2] = br_swap32(iv1);
|
||||
q[4] = br_swap32(iv2);
|
||||
q[6] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
|
||||
/*
|
||||
* The odd values are used for CBC-MAC.
|
||||
*/
|
||||
q[1] = cm0;
|
||||
q[3] = cm1;
|
||||
q[5] = cm2;
|
||||
q[7] = cm3;
|
||||
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
|
||||
/*
|
||||
* We do the XOR with the plaintext in 32-bit registers,
|
||||
* so that the value are available for CBC-MAC processing
|
||||
* as well.
|
||||
*/
|
||||
q[0] ^= br_dec32le(buf + 0);
|
||||
q[2] ^= br_dec32le(buf + 4);
|
||||
q[4] ^= br_dec32le(buf + 8);
|
||||
q[6] ^= br_dec32le(buf + 12);
|
||||
br_enc32le(buf + 0, q[0]);
|
||||
br_enc32le(buf + 4, q[2]);
|
||||
br_enc32le(buf + 8, q[4]);
|
||||
br_enc32le(buf + 12, q[6]);
|
||||
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
|
||||
/*
|
||||
* We set the cm* values to the block to encrypt in the
|
||||
* next iteration.
|
||||
*/
|
||||
if (first_iter) {
|
||||
first_iter = 0;
|
||||
cm0 ^= q[0];
|
||||
cm1 ^= q[2];
|
||||
cm2 ^= q[4];
|
||||
cm3 ^= q[6];
|
||||
} else {
|
||||
cm0 = q[0] ^ q[1];
|
||||
cm1 = q[2] ^ q[3];
|
||||
cm2 = q[4] ^ q[5];
|
||||
cm3 = q[6] ^ q[7];
|
||||
}
|
||||
|
||||
/*
|
||||
* If this was the last iteration, then compute the
|
||||
* extra block encryption to complete CBC-MAC.
|
||||
*/
|
||||
if (len == 0) {
|
||||
q[0] = cm0;
|
||||
q[2] = cm1;
|
||||
q[4] = cm2;
|
||||
q[6] = cm3;
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
cm0 = q[0];
|
||||
cm1 = q[2];
|
||||
cm2 = q[4];
|
||||
cm3 = q[6];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_ct_ctrcbc_decrypt(const br_aes_ct_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned char *ivbuf;
|
||||
uint32_t iv0, iv1, iv2, iv3;
|
||||
uint32_t cm0, cm1, cm2, cm3;
|
||||
uint32_t sk_exp[120];
|
||||
|
||||
br_aes_ct_skey_expand(sk_exp, ctx->num_rounds, ctx->skey);
|
||||
|
||||
/*
|
||||
* We keep the counter as four 32-bit values, with big-endian
|
||||
* convention, because that's what is expected for purposes of
|
||||
* incrementing the counter value.
|
||||
*/
|
||||
ivbuf = ctr;
|
||||
iv0 = br_dec32be(ivbuf + 0);
|
||||
iv1 = br_dec32be(ivbuf + 4);
|
||||
iv2 = br_dec32be(ivbuf + 8);
|
||||
iv3 = br_dec32be(ivbuf + 12);
|
||||
|
||||
/*
|
||||
* The current CBC-MAC value is kept in little-endian convention.
|
||||
*/
|
||||
cm0 = br_dec32le((unsigned char *)cbcmac + 0);
|
||||
cm1 = br_dec32le((unsigned char *)cbcmac + 4);
|
||||
cm2 = br_dec32le((unsigned char *)cbcmac + 8);
|
||||
cm3 = br_dec32le((unsigned char *)cbcmac + 12);
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
uint32_t q[8], carry;
|
||||
unsigned char tmp[16];
|
||||
|
||||
/*
|
||||
* The bitslice implementation expects values in
|
||||
* little-endian convention, so we have to byteswap them.
|
||||
*/
|
||||
q[0] = br_swap32(iv0);
|
||||
q[2] = br_swap32(iv1);
|
||||
q[4] = br_swap32(iv2);
|
||||
q[6] = br_swap32(iv3);
|
||||
iv3 ++;
|
||||
carry = ~(iv3 | -iv3) >> 31;
|
||||
iv2 += carry;
|
||||
carry &= -(~(iv2 | -iv2) >> 31);
|
||||
iv1 += carry;
|
||||
carry &= -(~(iv1 | -iv1) >> 31);
|
||||
iv0 += carry;
|
||||
|
||||
/*
|
||||
* The odd values are used for CBC-MAC.
|
||||
*/
|
||||
q[1] = cm0 ^ br_dec32le(buf + 0);
|
||||
q[3] = cm1 ^ br_dec32le(buf + 4);
|
||||
q[5] = cm2 ^ br_dec32le(buf + 8);
|
||||
q[7] = cm3 ^ br_dec32le(buf + 12);
|
||||
|
||||
br_aes_ct_ortho(q);
|
||||
br_aes_ct_bitslice_encrypt(ctx->num_rounds, sk_exp, q);
|
||||
br_aes_ct_ortho(q);
|
||||
|
||||
br_enc32le(tmp + 0, q[0]);
|
||||
br_enc32le(tmp + 4, q[2]);
|
||||
br_enc32le(tmp + 8, q[4]);
|
||||
br_enc32le(tmp + 12, q[6]);
|
||||
xorbuf(buf, tmp, 16);
|
||||
cm0 = q[1];
|
||||
cm1 = q[3];
|
||||
cm2 = q[5];
|
||||
cm3 = q[7];
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
|
||||
br_enc32be(ivbuf + 0, iv0);
|
||||
br_enc32be(ivbuf + 4, iv1);
|
||||
br_enc32be(ivbuf + 8, iv2);
|
||||
br_enc32be(ivbuf + 12, iv3);
|
||||
br_enc32le((unsigned char *)cbcmac + 0, cm0);
|
||||
br_enc32le((unsigned char *)cbcmac + 4, cm1);
|
||||
br_enc32le((unsigned char *)cbcmac + 8, cm2);
|
||||
br_enc32le((unsigned char *)cbcmac + 12, cm3);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctrcbc_class br_aes_ct_ctrcbc_vtable = {
|
||||
sizeof(br_aes_ct_ctrcbc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctrcbc_class **, const void *, size_t))
|
||||
&br_aes_ct_ctrcbc_init,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_ct_ctrcbc_encrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_ct_ctrcbc_decrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, size_t))
|
||||
&br_aes_ct_ctrcbc_ctr,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, const void *, size_t))
|
||||
&br_aes_ct_ctrcbc_mac
|
||||
};
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_invSbox(uint32_t *q)
|
||||
{
|
||||
/*
|
||||
* AES S-box is:
|
||||
* S(x) = A(I(x)) ^ 0x63
|
||||
* where I() is inversion in GF(256), and A() is a linear
|
||||
* transform (0 is formally defined to be its own inverse).
|
||||
* Since inversion is an involution, the inverse S-box can be
|
||||
* computed from the S-box as:
|
||||
* iS(x) = B(S(B(x ^ 0x63)) ^ 0x63)
|
||||
* where B() is the inverse of A(). Indeed, for any y in GF(256):
|
||||
* iS(S(y)) = B(A(I(B(A(I(y)) ^ 0x63 ^ 0x63))) ^ 0x63 ^ 0x63) = y
|
||||
*
|
||||
* Note: we reuse the implementation of the forward S-box,
|
||||
* instead of duplicating it here, so that total code size is
|
||||
* lower. By merging the B() transforms into the S-box circuit
|
||||
* we could make faster CBC decryption, but CBC decryption is
|
||||
* already quite faster than CBC encryption because we can
|
||||
* process two blocks in parallel.
|
||||
*/
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
|
||||
q0 = ~q[0];
|
||||
q1 = ~q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = ~q[5];
|
||||
q6 = ~q[6];
|
||||
q7 = q[7];
|
||||
q[7] = q1 ^ q4 ^ q6;
|
||||
q[6] = q0 ^ q3 ^ q5;
|
||||
q[5] = q7 ^ q2 ^ q4;
|
||||
q[4] = q6 ^ q1 ^ q3;
|
||||
q[3] = q5 ^ q0 ^ q2;
|
||||
q[2] = q4 ^ q7 ^ q1;
|
||||
q[1] = q3 ^ q6 ^ q0;
|
||||
q[0] = q2 ^ q5 ^ q7;
|
||||
}
|
||||
|
||||
static void
|
||||
add_round_key(uint32_t *q, const uint32_t *sk)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
q[i] ^= sk[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(uint32_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint32_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & 0x000000FF)
|
||||
| ((x & 0x00003F00) << 2) | ((x & 0x0000C000) >> 6)
|
||||
| ((x & 0x000F0000) << 4) | ((x & 0x00F00000) >> 4)
|
||||
| ((x & 0x03000000) << 6) | ((x & 0xFC000000) >> 2);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
rotr16(uint32_t x)
|
||||
{
|
||||
return (x << 16) | (x >> 16);
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(uint32_t *q)
|
||||
{
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 8) | (q0 << 24);
|
||||
r1 = (q1 >> 8) | (q1 << 24);
|
||||
r2 = (q2 >> 8) | (q2 << 24);
|
||||
r3 = (q3 >> 8) | (q3 << 24);
|
||||
r4 = (q4 >> 8) | (q4 << 24);
|
||||
r5 = (q5 >> 8) | (q5 << 24);
|
||||
r6 = (q6 >> 8) | (q6 << 24);
|
||||
r7 = (q7 >> 8) | (q7 << 24);
|
||||
|
||||
q[0] = q5 ^ q6 ^ q7 ^ r0 ^ r5 ^ r7 ^ rotr16(q0 ^ q5 ^ q6 ^ r0 ^ r5);
|
||||
q[1] = q0 ^ q5 ^ r0 ^ r1 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q5 ^ q7 ^ r1 ^ r5 ^ r6);
|
||||
q[2] = q0 ^ q1 ^ q6 ^ r1 ^ r2 ^ r6 ^ r7 ^ rotr16(q0 ^ q2 ^ q6 ^ r2 ^ r6 ^ r7);
|
||||
q[3] = q0 ^ q1 ^ q2 ^ q5 ^ q6 ^ r0 ^ r2 ^ r3 ^ r5 ^ rotr16(q0 ^ q1 ^ q3 ^ q5 ^ q6 ^ q7 ^ r0 ^ r3 ^ r5 ^ r7);
|
||||
q[4] = q1 ^ q2 ^ q3 ^ q5 ^ r1 ^ r3 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q1 ^ q2 ^ q4 ^ q5 ^ q7 ^ r1 ^ r4 ^ r5 ^ r6);
|
||||
q[5] = q2 ^ q3 ^ q4 ^ q6 ^ r2 ^ r4 ^ r5 ^ r6 ^ r7 ^ rotr16(q2 ^ q3 ^ q5 ^ q6 ^ r2 ^ r5 ^ r6 ^ r7);
|
||||
q[6] = q3 ^ q4 ^ q5 ^ q7 ^ r3 ^ r5 ^ r6 ^ r7 ^ rotr16(q3 ^ q4 ^ q6 ^ q7 ^ r3 ^ r6 ^ r7);
|
||||
q[7] = q4 ^ q5 ^ q6 ^ r4 ^ r6 ^ r7 ^ rotr16(q4 ^ q5 ^ q7 ^ r4 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_decrypt(unsigned num_rounds,
|
||||
const uint32_t *skey, uint32_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct_bitslice_invSbox(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
inv_mix_columns(q);
|
||||
}
|
||||
inv_shift_rows(q);
|
||||
br_aes_ct_bitslice_invSbox(q);
|
||||
add_round_key(q, skey);
|
||||
}
|
||||
|
|
@ -1,112 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
static inline void
|
||||
add_round_key(uint32_t *q, const uint32_t *sk)
|
||||
{
|
||||
q[0] ^= sk[0];
|
||||
q[1] ^= sk[1];
|
||||
q[2] ^= sk[2];
|
||||
q[3] ^= sk[3];
|
||||
q[4] ^= sk[4];
|
||||
q[5] ^= sk[5];
|
||||
q[6] ^= sk[6];
|
||||
q[7] ^= sk[7];
|
||||
}
|
||||
|
||||
static inline void
|
||||
shift_rows(uint32_t *q)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 8; i ++) {
|
||||
uint32_t x;
|
||||
|
||||
x = q[i];
|
||||
q[i] = (x & 0x000000FF)
|
||||
| ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6)
|
||||
| ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4)
|
||||
| ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
rotr16(uint32_t x)
|
||||
{
|
||||
return (x << 16) | (x >> 16);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mix_columns(uint32_t *q)
|
||||
{
|
||||
uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
|
||||
uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
|
||||
|
||||
q0 = q[0];
|
||||
q1 = q[1];
|
||||
q2 = q[2];
|
||||
q3 = q[3];
|
||||
q4 = q[4];
|
||||
q5 = q[5];
|
||||
q6 = q[6];
|
||||
q7 = q[7];
|
||||
r0 = (q0 >> 8) | (q0 << 24);
|
||||
r1 = (q1 >> 8) | (q1 << 24);
|
||||
r2 = (q2 >> 8) | (q2 << 24);
|
||||
r3 = (q3 >> 8) | (q3 << 24);
|
||||
r4 = (q4 >> 8) | (q4 << 24);
|
||||
r5 = (q5 >> 8) | (q5 << 24);
|
||||
r6 = (q6 >> 8) | (q6 << 24);
|
||||
r7 = (q7 >> 8) | (q7 << 24);
|
||||
|
||||
q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0);
|
||||
q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1);
|
||||
q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2);
|
||||
q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3);
|
||||
q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4);
|
||||
q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5);
|
||||
q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6);
|
||||
q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7);
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_ct_bitslice_encrypt(unsigned num_rounds,
|
||||
const uint32_t *skey, uint32_t *q)
|
||||
{
|
||||
unsigned u;
|
||||
|
||||
add_round_key(q, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
mix_columns(q);
|
||||
add_round_key(q, skey + (u << 3));
|
||||
}
|
||||
br_aes_ct_bitslice_Sbox(q);
|
||||
shift_rows(q);
|
||||
add_round_key(q, skey + (num_rounds << 3));
|
||||
}
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcdec_init(br_aes_small_cbcdec_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_cbcdec_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcdec_run(const br_aes_small_cbcdec_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
int i;
|
||||
|
||||
memcpy(tmp, buf, 16);
|
||||
br_aes_small_decrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
memcpy(ivbuf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcdec_class br_aes_small_cbcdec_vtable = {
|
||||
sizeof(br_aes_small_cbcdec_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcdec_class **, const void *, size_t))
|
||||
&br_aes_small_cbcdec_init,
|
||||
(void (*)(const br_block_cbcdec_class *const *, void *, void *, size_t))
|
||||
&br_aes_small_cbcdec_run
|
||||
};
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcenc_init(br_aes_small_cbcenc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_cbcenc_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_cbcenc_run(const br_aes_small_cbcenc_keys *ctx,
|
||||
void *iv, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *ivbuf;
|
||||
|
||||
ivbuf = iv;
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
buf[i] ^= ivbuf[i];
|
||||
}
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, buf);
|
||||
memcpy(ivbuf, buf, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_cbcenc_class br_aes_small_cbcenc_vtable = {
|
||||
sizeof(br_aes_small_cbcenc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_cbcenc_class **, const void *, size_t))
|
||||
&br_aes_small_cbcenc_init,
|
||||
(void (*)(const br_block_cbcenc_class *const *, void *, void *, size_t))
|
||||
&br_aes_small_cbcenc_run
|
||||
};
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctr_init(br_aes_small_ctr_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_ctr_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
uint32_t
|
||||
br_aes_small_ctr_run(const br_aes_small_ctr_keys *ctx,
|
||||
const void *iv, uint32_t cc, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf;
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
|
||||
memcpy(tmp, iv, 12);
|
||||
br_enc32be(tmp + 12, cc ++);
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);
|
||||
if (len <= 16) {
|
||||
xorbuf(buf, tmp, len);
|
||||
break;
|
||||
}
|
||||
xorbuf(buf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctr_class br_aes_small_ctr_vtable = {
|
||||
sizeof(br_aes_small_ctr_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctr_class **, const void *, size_t))
|
||||
&br_aes_small_ctr_init,
|
||||
(uint32_t (*)(const br_block_ctr_class *const *,
|
||||
const void *, uint32_t, void *, size_t))
|
||||
&br_aes_small_ctr_run
|
||||
};
|
||||
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctrcbc_init(br_aes_small_ctrcbc_keys *ctx,
|
||||
const void *key, size_t len)
|
||||
{
|
||||
ctx->vtable = &br_aes_small_ctrcbc_vtable;
|
||||
ctx->num_rounds = br_aes_keysched(ctx->skey, key, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xorbuf(void *dst, const void *src, size_t len)
|
||||
{
|
||||
unsigned char *d;
|
||||
const unsigned char *s;
|
||||
|
||||
d = dst;
|
||||
s = src;
|
||||
while (len -- > 0) {
|
||||
*d ++ ^= *s ++;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctrcbc_ctr(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *data, size_t len)
|
||||
{
|
||||
unsigned char *buf, *bctr;
|
||||
uint32_t cc0, cc1, cc2, cc3;
|
||||
|
||||
buf = data;
|
||||
bctr = ctr;
|
||||
cc3 = br_dec32be(bctr + 0);
|
||||
cc2 = br_dec32be(bctr + 4);
|
||||
cc1 = br_dec32be(bctr + 8);
|
||||
cc0 = br_dec32be(bctr + 12);
|
||||
while (len > 0) {
|
||||
unsigned char tmp[16];
|
||||
uint32_t carry;
|
||||
|
||||
br_enc32be(tmp + 0, cc3);
|
||||
br_enc32be(tmp + 4, cc2);
|
||||
br_enc32be(tmp + 8, cc1);
|
||||
br_enc32be(tmp + 12, cc0);
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, tmp);
|
||||
xorbuf(buf, tmp, 16);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
cc0 ++;
|
||||
carry = (~(cc0 | -cc0)) >> 31;
|
||||
cc1 += carry;
|
||||
carry &= (~(cc1 | -cc1)) >> 31;
|
||||
cc2 += carry;
|
||||
carry &= (~(cc2 | -cc2)) >> 31;
|
||||
cc3 += carry;
|
||||
}
|
||||
br_enc32be(bctr + 0, cc3);
|
||||
br_enc32be(bctr + 4, cc2);
|
||||
br_enc32be(bctr + 8, cc1);
|
||||
br_enc32be(bctr + 12, cc0);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctrcbc_mac(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *cbcmac, const void *data, size_t len)
|
||||
{
|
||||
const unsigned char *buf;
|
||||
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
xorbuf(cbcmac, buf, 16);
|
||||
br_aes_small_encrypt(ctx->num_rounds, ctx->skey, cbcmac);
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctrcbc_encrypt(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
br_aes_small_ctrcbc_ctr(ctx, ctr, data, len);
|
||||
br_aes_small_ctrcbc_mac(ctx, cbcmac, data, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
void
|
||||
br_aes_small_ctrcbc_decrypt(const br_aes_small_ctrcbc_keys *ctx,
|
||||
void *ctr, void *cbcmac, void *data, size_t len)
|
||||
{
|
||||
br_aes_small_ctrcbc_mac(ctx, cbcmac, data, len);
|
||||
br_aes_small_ctrcbc_ctr(ctx, ctr, data, len);
|
||||
}
|
||||
|
||||
/* see bearssl_block.h */
|
||||
const br_block_ctrcbc_class br_aes_small_ctrcbc_vtable = {
|
||||
sizeof(br_aes_small_ctrcbc_keys),
|
||||
16,
|
||||
4,
|
||||
(void (*)(const br_block_ctrcbc_class **, const void *, size_t))
|
||||
&br_aes_small_ctrcbc_init,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_small_ctrcbc_encrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, void *, size_t))
|
||||
&br_aes_small_ctrcbc_decrypt,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, void *, size_t))
|
||||
&br_aes_small_ctrcbc_ctr,
|
||||
(void (*)(const br_block_ctrcbc_class *const *,
|
||||
void *, const void *, size_t))
|
||||
&br_aes_small_ctrcbc_mac
|
||||
};
|
||||
|
|
@ -1,176 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
/*
|
||||
* Inverse S-box.
|
||||
*/
|
||||
static const unsigned char iS[] = {
|
||||
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E,
|
||||
0x81, 0xF3, 0xD7, 0xFB, 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
|
||||
0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 0x54, 0x7B, 0x94, 0x32,
|
||||
0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
|
||||
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49,
|
||||
0x6D, 0x8B, 0xD1, 0x25, 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16,
|
||||
0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 0x6C, 0x70, 0x48, 0x50,
|
||||
0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
|
||||
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05,
|
||||
0xB8, 0xB3, 0x45, 0x06, 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02,
|
||||
0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 0x3A, 0x91, 0x11, 0x41,
|
||||
0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
|
||||
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8,
|
||||
0x1C, 0x75, 0xDF, 0x6E, 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89,
|
||||
0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 0xFC, 0x56, 0x3E, 0x4B,
|
||||
0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
|
||||
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59,
|
||||
0x27, 0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D,
|
||||
0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0, 0xE0, 0x3B, 0x4D,
|
||||
0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63,
|
||||
0x55, 0x21, 0x0C, 0x7D
|
||||
};
|
||||
|
||||
static void
|
||||
add_round_key(unsigned *state, const uint32_t *skeys)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
uint32_t k;
|
||||
|
||||
k = *skeys ++;
|
||||
state[i + 0] ^= (unsigned)(k >> 24);
|
||||
state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
|
||||
state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
|
||||
state[i + 3] ^= (unsigned)k & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_sub_bytes(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
state[i] = iS[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
inv_shift_rows(unsigned *state)
|
||||
{
|
||||
unsigned tmp;
|
||||
|
||||
tmp = state[13];
|
||||
state[13] = state[9];
|
||||
state[9] = state[5];
|
||||
state[5] = state[1];
|
||||
state[1] = tmp;
|
||||
|
||||
tmp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = tmp;
|
||||
tmp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = tmp;
|
||||
|
||||
tmp = state[3];
|
||||
state[3] = state[7];
|
||||
state[7] = state[11];
|
||||
state[11] = state[15];
|
||||
state[15] = tmp;
|
||||
}
|
||||
|
||||
static inline unsigned
|
||||
gf256red(unsigned x)
|
||||
{
|
||||
unsigned y;
|
||||
|
||||
y = x >> 8;
|
||||
return (x ^ y ^ (y << 1) ^ (y << 3) ^ (y << 4)) & 0xFF;
|
||||
}
|
||||
|
||||
static void
|
||||
inv_mix_columns(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
unsigned s0, s1, s2, s3;
|
||||
unsigned t0, t1, t2, t3;
|
||||
|
||||
s0 = state[i + 0];
|
||||
s1 = state[i + 1];
|
||||
s2 = state[i + 2];
|
||||
s3 = state[i + 3];
|
||||
t0 = (s0 << 1) ^ (s0 << 2) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 1) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 2) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 3);
|
||||
t1 = s0 ^ (s0 << 3)
|
||||
^ (s1 << 1) ^ (s1 << 2) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 1) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 2) ^ (s3 << 3);
|
||||
t2 = s0 ^ (s0 << 2) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 3)
|
||||
^ (s2 << 1) ^ (s2 << 2) ^ (s2 << 3)
|
||||
^ s3 ^ (s3 << 1) ^ (s3 << 3);
|
||||
t3 = s0 ^ (s0 << 1) ^ (s0 << 3)
|
||||
^ s1 ^ (s1 << 2) ^ (s1 << 3)
|
||||
^ s2 ^ (s2 << 3)
|
||||
^ (s3 << 1) ^ (s3 << 2) ^ (s3 << 3);
|
||||
state[i + 0] = gf256red(t0);
|
||||
state[i + 1] = gf256red(t1);
|
||||
state[i + 2] = gf256red(t2);
|
||||
state[i + 3] = gf256red(t3);
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_small_decrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned state[16];
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
for (u = 0; u < 16; u ++) {
|
||||
state[u] = buf[u];
|
||||
}
|
||||
add_round_key(state, skey + (num_rounds << 2));
|
||||
for (u = num_rounds - 1; u > 0; u --) {
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, skey + (u << 2));
|
||||
inv_mix_columns(state);
|
||||
}
|
||||
inv_shift_rows(state);
|
||||
inv_sub_bytes(state);
|
||||
add_round_key(state, skey);
|
||||
for (u = 0; u < 16; u ++) {
|
||||
buf[u] = state[u];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,129 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "inner.h"
|
||||
|
||||
#define S br_aes_S
|
||||
|
||||
static void
|
||||
add_round_key(unsigned *state, const uint32_t *skeys)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
uint32_t k;
|
||||
|
||||
k = *skeys ++;
|
||||
state[i + 0] ^= (unsigned)(k >> 24);
|
||||
state[i + 1] ^= (unsigned)(k >> 16) & 0xFF;
|
||||
state[i + 2] ^= (unsigned)(k >> 8) & 0xFF;
|
||||
state[i + 3] ^= (unsigned)k & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sub_bytes(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i ++) {
|
||||
state[i] = S[state[i]];
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
shift_rows(unsigned *state)
|
||||
{
|
||||
unsigned tmp;
|
||||
|
||||
tmp = state[1];
|
||||
state[1] = state[5];
|
||||
state[5] = state[9];
|
||||
state[9] = state[13];
|
||||
state[13] = tmp;
|
||||
|
||||
tmp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = tmp;
|
||||
tmp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = tmp;
|
||||
|
||||
tmp = state[15];
|
||||
state[15] = state[11];
|
||||
state[11] = state[7];
|
||||
state[7] = state[3];
|
||||
state[3] = tmp;
|
||||
}
|
||||
|
||||
static void
|
||||
mix_columns(unsigned *state)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i += 4) {
|
||||
unsigned s0, s1, s2, s3;
|
||||
unsigned t0, t1, t2, t3;
|
||||
|
||||
s0 = state[i + 0];
|
||||
s1 = state[i + 1];
|
||||
s2 = state[i + 2];
|
||||
s3 = state[i + 3];
|
||||
t0 = (s0 << 1) ^ s1 ^ (s1 << 1) ^ s2 ^ s3;
|
||||
t1 = s0 ^ (s1 << 1) ^ s2 ^ (s2 << 1) ^ s3;
|
||||
t2 = s0 ^ s1 ^ (s2 << 1) ^ s3 ^ (s3 << 1);
|
||||
t3 = s0 ^ (s0 << 1) ^ s1 ^ s2 ^ (s3 << 1);
|
||||
state[i + 0] = t0 ^ ((unsigned)(-(int)(t0 >> 8)) & 0x11B);
|
||||
state[i + 1] = t1 ^ ((unsigned)(-(int)(t1 >> 8)) & 0x11B);
|
||||
state[i + 2] = t2 ^ ((unsigned)(-(int)(t2 >> 8)) & 0x11B);
|
||||
state[i + 3] = t3 ^ ((unsigned)(-(int)(t3 >> 8)) & 0x11B);
|
||||
}
|
||||
}
|
||||
|
||||
/* see inner.h */
|
||||
void
|
||||
br_aes_small_encrypt(unsigned num_rounds, const uint32_t *skey, void *data)
|
||||
{
|
||||
unsigned char *buf;
|
||||
unsigned state[16];
|
||||
unsigned u;
|
||||
|
||||
buf = data;
|
||||
for (u = 0; u < 16; u ++) {
|
||||
state[u] = buf[u];
|
||||
}
|
||||
add_round_key(state, skey);
|
||||
for (u = 1; u < num_rounds; u ++) {
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
mix_columns(state);
|
||||
add_round_key(state, skey + (u << 2));
|
||||
}
|
||||
sub_bytes(state);
|
||||
shift_rows(state);
|
||||
add_round_key(state, skey + (num_rounds << 2));
|
||||
for (u = 0; u < 16; u ++) {
|
||||
buf[u] = state[u];
|
||||
}
|
||||
}
|
||||
|
|
@ -44,13 +44,7 @@ br_ssl_engine_set_default_aes_cbc(br_ssl_engine_context *cc)
|
|||
return;
|
||||
}
|
||||
#endif
|
||||
#if BR_64
|
||||
br_ssl_engine_set_aes_cbc(cc,
|
||||
&br_aes_ct64_cbcenc_vtable,
|
||||
&br_aes_ct64_cbcdec_vtable);
|
||||
#else
|
||||
br_ssl_engine_set_aes_cbc(cc,
|
||||
&br_aes_ct_cbcenc_vtable,
|
||||
&br_aes_ct_cbcdec_vtable);
|
||||
#endif
|
||||
&br_aes_big_cbcenc_vtable,
|
||||
&br_aes_big_cbcdec_vtable);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,18 +39,8 @@ br_ssl_engine_set_default_aes_ccm(br_ssl_engine_context *cc)
|
|||
ictrcbc = br_aes_x86ni_ctrcbc_get_vtable();
|
||||
if (ictrcbc != NULL) {
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, ictrcbc);
|
||||
} else {
|
||||
#if BR_64
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct64_ctrcbc_vtable);
|
||||
#else
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct_ctrcbc_vtable);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
#else
|
||||
#if BR_64
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct64_ctrcbc_vtable);
|
||||
#else
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_ct_ctrcbc_vtable);
|
||||
#endif
|
||||
#endif
|
||||
br_ssl_engine_set_aes_ctrcbc(cc, &br_aes_big_ctrcbc_vtable);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,19 +41,12 @@ br_ssl_engine_set_default_aes_gcm(br_ssl_engine_context *cc)
|
|||
if (ictr != NULL) {
|
||||
br_ssl_engine_set_aes_ctr(cc, ictr);
|
||||
} else {
|
||||
#if BR_64
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_ct64_ctr_vtable);
|
||||
#else
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_ct_ctr_vtable);
|
||||
#endif
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_big_ctr_vtable);
|
||||
}
|
||||
#else
|
||||
#if BR_64
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_ct64_ctr_vtable);
|
||||
#else
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_ct_ctr_vtable);
|
||||
#endif
|
||||
br_ssl_engine_set_aes_ctr(cc, &br_aes_big_ctr_vtable);
|
||||
#endif
|
||||
|
||||
#if BR_AES_X86NI
|
||||
ighash = br_ghash_pclmul_get();
|
||||
if (ighash != 0) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue