aesni: Add a common key schedule class for AES
This commit is contained in:
@@ -2,6 +2,7 @@ AM_CPPFLAGS = \
|
|||||||
-I$(top_srcdir)/src/libstrongswan
|
-I$(top_srcdir)/src/libstrongswan
|
||||||
|
|
||||||
AM_CFLAGS = \
|
AM_CFLAGS = \
|
||||||
|
-maes \
|
||||||
$(PLUGIN_CFLAGS)
|
$(PLUGIN_CFLAGS)
|
||||||
|
|
||||||
if MONOLITHIC
|
if MONOLITHIC
|
||||||
@@ -11,6 +12,7 @@ plugin_LTLIBRARIES = libstrongswan-aesni.la
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
libstrongswan_aesni_la_SOURCES = \
|
libstrongswan_aesni_la_SOURCES = \
|
||||||
|
aesni_key.h aesni_key.c \
|
||||||
aesni_plugin.h aesni_plugin.c
|
aesni_plugin.h aesni_plugin.c
|
||||||
|
|
||||||
libstrongswan_aesni_la_LDFLAGS = -module -avoid-version
|
libstrongswan_aesni_la_LDFLAGS = -module -avoid-version
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Martin Willi
|
||||||
|
* Copyright (C) 2015 revosec AG
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "aesni_key.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rounds used for each AES key size
|
||||||
|
*/
|
||||||
|
#define AES128_ROUNDS 10
|
||||||
|
|
||||||
|
typedef struct private_aesni_key_t private_aesni_key_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private data of an aesni_key_t object.
|
||||||
|
*/
|
||||||
|
struct private_aesni_key_t {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public aesni_key_t interface.
|
||||||
|
*/
|
||||||
|
aesni_key_t public;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invert round encryption keys to get a decryption key schedule
|
||||||
|
*/
|
||||||
|
static void reverse_key(aesni_key_t *this)
|
||||||
|
{
|
||||||
|
__m128i t[this->rounds + 1];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i <= this->rounds; i++)
|
||||||
|
{
|
||||||
|
t[i] = this->schedule[i];
|
||||||
|
}
|
||||||
|
this->schedule[this->rounds] = t[0];
|
||||||
|
for (i = 1; i < this->rounds; i++)
|
||||||
|
{
|
||||||
|
this->schedule[this->rounds - i] = _mm_aesimc_si128(t[i]);
|
||||||
|
}
|
||||||
|
this->schedule[0] = t[this->rounds];
|
||||||
|
|
||||||
|
memwipe(t, sizeof(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
METHOD(aesni_key_t, destroy, void,
|
||||||
|
private_aesni_key_t *this)
|
||||||
|
{
|
||||||
|
memwipe(this, sizeof(*this) + (this->public.rounds + 1) * AES_BLOCK_SIZE);
|
||||||
|
free(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See header
|
||||||
|
*/
|
||||||
|
aesni_key_t *aesni_key_create(bool encrypt, chunk_t key)
|
||||||
|
{
|
||||||
|
private_aesni_key_t *this;
|
||||||
|
int rounds;
|
||||||
|
|
||||||
|
switch (key.len)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
INIT_EXTRA(this, (rounds + 1) * AES_BLOCK_SIZE,
|
||||||
|
.public = {
|
||||||
|
.destroy = _destroy,
|
||||||
|
.rounds = rounds,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (key.len)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!encrypt)
|
||||||
|
{
|
||||||
|
reverse_key(&this->public);
|
||||||
|
}
|
||||||
|
|
||||||
|
return &this->public;
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Martin Willi
|
||||||
|
* Copyright (C) 2015 revosec AG
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation; either version 2 of the License, or (at your
|
||||||
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @defgroup aesni_key aesni_key
|
||||||
|
* @{ @ingroup aesni
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef AESNI_KEY_H_
|
||||||
|
#define AESNI_KEY_H_
|
||||||
|
|
||||||
|
#include <library.h>
|
||||||
|
|
||||||
|
#include <wmmintrin.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AES block size, in bytes
|
||||||
|
*/
|
||||||
|
#define AES_BLOCK_SIZE 16
|
||||||
|
|
||||||
|
typedef struct aesni_key_t aesni_key_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key schedule for encryption/decryption using on AES-NI.
|
||||||
|
*/
|
||||||
|
struct aesni_key_t {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy a aesni_key_t.
|
||||||
|
*/
|
||||||
|
void (*destroy)(aesni_key_t *this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of AES rounds (10, 12, 14)
|
||||||
|
*/
|
||||||
|
int rounds;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key schedule, for each round + the round 0 (whitening)
|
||||||
|
*/
|
||||||
|
__attribute__((aligned(sizeof(__m128i)))) __m128i schedule[];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a AESNI key schedule instance.
|
||||||
|
*
|
||||||
|
* @param encrypt TRUE for encryption schedule, FALSE for decryption
|
||||||
|
* @param key non-expanded crypto key, 16, 24 or 32 bytes
|
||||||
|
* @return key schedule, NULL on invalid key size
|
||||||
|
*/
|
||||||
|
aesni_key_t *aesni_key_create(bool encrypt, chunk_t key);
|
||||||
|
|
||||||
|
#endif /** AESNI_KEY_H_ @}*/
|
||||||
Reference in New Issue
Block a user