Migrated xcbc plugin to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-08-13 17:11:53 +02:00
parent 5ab7d9c296
commit 7156b951f5
6 changed files with 112 additions and 143 deletions
+52 -56
View File
@@ -27,10 +27,11 @@ typedef struct private_xcbc_t private_xcbc_t;
* The variable names are the same as in the RFC.
*/
struct private_xcbc_t {
/**
* Public xcbc_t interface.
*/
xcbc_t xcbc;
xcbc_t public;
/**
* Block size, in bytes
@@ -135,9 +136,9 @@ static void final(private_xcbc_t *this, u_int8_t *out)
if (this->remaining_bytes == this->b && !this->zero)
{
/* a) If the blocksize of M[n] is 128 bits:
* XOR M[n] with E[n-1] and Key K2, then encrypt the result with
* Key K1, yielding E[n].
*/
* XOR M[n] with E[n-1] and Key K2, then encrypt the result with
* Key K1, yielding E[n].
*/
memxor(this->e, this->remaining, this->b);
memxor(this->e, this->k2, this->b);
this->k1->encrypt(this->k1, chunk_create(this->e, this->b), iv, NULL);
@@ -147,20 +148,20 @@ static void final(private_xcbc_t *this, u_int8_t *out)
/* b) If the blocksize of M[n] is less than 128 bits:
*
* i) Pad M[n] with a single "1" bit, followed by the number of
* "0" bits (possibly none) required to increase M[n]'s
* blocksize to 128 bits.
*/
if (this->remaining_bytes < this->b)
{
this->remaining[this->remaining_bytes] = 0x80;
while (++this->remaining_bytes < this->b)
{
this->remaining[this->remaining_bytes] = 0x00;
}
}
/* ii) XOR M[n] with E[n-1] and Key K3, then encrypt the result
* with Key K1, yielding E[n].
*/
* "0" bits (possibly none) required to increase M[n]'s
* blocksize to 128 bits.
*/
if (this->remaining_bytes < this->b)
{
this->remaining[this->remaining_bytes] = 0x80;
while (++this->remaining_bytes < this->b)
{
this->remaining[this->remaining_bytes] = 0x00;
}
}
/* ii) XOR M[n] with E[n-1] and Key K3, then encrypt the result
* with Key K1, yielding E[n].
*/
memxor(this->e, this->remaining, this->b);
memxor(this->e, this->k3, this->b);
this->k1->encrypt(this->k1, chunk_create(this->e, this->b), iv, NULL);
@@ -174,10 +175,8 @@ static void final(private_xcbc_t *this, u_int8_t *out)
this->zero = TRUE;
}
/**
* Implementation of xcbc_t.get_mac.
*/
static void get_mac(private_xcbc_t *this, chunk_t data, u_int8_t *out)
METHOD(xcbc_t, get_mac, void,
private_xcbc_t *this, chunk_t data, u_int8_t *out)
{
/* update E, do not process last block */
update(this, data);
@@ -188,18 +187,14 @@ static void get_mac(private_xcbc_t *this, chunk_t data, u_int8_t *out)
}
}
/**
* Implementation of xcbc_t.get_block_size.
*/
static size_t get_block_size(private_xcbc_t *this)
METHOD(xcbc_t, get_block_size, size_t,
private_xcbc_t *this)
{
return this->b;
}
/**
* Implementation of xcbc_t.set_key.
*/
static void set_key(private_xcbc_t *this, chunk_t key)
METHOD(xcbc_t, set_key, void,
private_xcbc_t *this, chunk_t key)
{
chunk_t iv, k1, lengthened;
@@ -228,11 +223,11 @@ static void set_key(private_xcbc_t *this, chunk_t key)
/*
* (1) Derive 3 128-bit keys (K1, K2 and K3) from the 128-bit secret
* key K, as follows:
* K1 = 0x01010101010101010101010101010101 encrypted with Key K
* K2 = 0x02020202020202020202020202020202 encrypted with Key K
* K3 = 0x03030303030303030303030303030303 encrypted with Key K
*/
* key K, as follows:
* K1 = 0x01010101010101010101010101010101 encrypted with Key K
* K2 = 0x02020202020202020202020202020202 encrypted with Key K
* K3 = 0x03030303030303030303030303030303 encrypted with Key K
*/
this->k1->set_key(this->k1, lengthened);
memset(this->k2, 0x02, this->b);
this->k1->encrypt(this->k1, chunk_create(this->k2, this->b), iv, NULL);
@@ -243,10 +238,8 @@ static void set_key(private_xcbc_t *this, chunk_t key)
this->k1->set_key(this->k1, k1);
}
/**
* Implementation of xcbc_t.destroy.
*/
static void destroy(private_xcbc_t *this)
METHOD(xcbc_t, destroy, void,
private_xcbc_t *this)
{
this->k1->destroy(this->k1);
free(this->k2);
@@ -263,35 +256,38 @@ xcbc_t *xcbc_create(encryption_algorithm_t algo, size_t key_size)
{
private_xcbc_t *this;
crypter_t *crypter;
u_int8_t b;
crypter = lib->crypto->create_crypter(lib->crypto, algo, key_size);
if (!crypter)
{
return NULL;
}
b = crypter->get_block_size(crypter);
/* input and output of crypter must be equal for xcbc */
if (crypter->get_block_size(crypter) != key_size)
if (b != key_size)
{
crypter->destroy(crypter);
return NULL;
}
this = malloc_thing(private_xcbc_t);
this->xcbc.get_mac = (void (*)(xcbc_t *,chunk_t,u_int8_t*))get_mac;
this->xcbc.get_block_size = (size_t (*)(xcbc_t *))get_block_size;
this->xcbc.set_key = (void (*)(xcbc_t *,chunk_t))set_key;
this->xcbc.destroy = (void (*)(xcbc_t *))destroy;
INIT(this,
.public = {
.get_mac = _get_mac,
.get_block_size = _get_block_size,
.set_key = _set_key,
.destroy = _destroy,
},
.b = b,
.k1 = crypter,
.k2 = malloc(b),
.k3 = malloc(b),
.e = malloc(b),
.remaining = malloc(b),
.zero = TRUE,
);
memset(this->e, 0, b);
this->b = crypter->get_block_size(crypter);
this->k1 = crypter;
this->k2 = malloc(this->b);
this->k3 = malloc(this->b);
this->e = malloc(this->b);
memset(this->e, 0, this->b);
this->remaining = malloc(this->b);
this->remaining_bytes = 0;
this->zero = TRUE;
return &this->xcbc;
return &this->public;
}
+6 -6
View File
@@ -32,10 +32,8 @@ struct private_xcbc_plugin_t {
xcbc_plugin_t public;
};
/**
* Implementation of xcbc_plugin_t.xcbctroy
*/
static void destroy(private_xcbc_plugin_t *this)
METHOD(plugin_t, destroy, void,
private_xcbc_plugin_t *this)
{
lib->crypto->remove_prf(lib->crypto,
(prf_constructor_t)xcbc_prf_create);
@@ -49,9 +47,11 @@ static void destroy(private_xcbc_plugin_t *this)
*/
plugin_t *xcbc_plugin_create()
{
private_xcbc_plugin_t *this = malloc_thing(private_xcbc_plugin_t);
private_xcbc_plugin_t *this;
this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
INIT(this,
.public.plugin.destroy = _destroy,
);
lib->crypto->add_prf(lib->crypto, PRF_AES128_XCBC,
(prf_constructor_t)xcbc_prf_create);
+23 -33
View File
@@ -35,18 +35,14 @@ struct private_xcbc_prf_t {
xcbc_t *xcbc;
};
/**
* Implementation of prf_t.get_bytes.
*/
static void get_bytes(private_xcbc_prf_t *this, chunk_t seed, u_int8_t *buffer)
METHOD(prf_t, get_bytes, void,
private_xcbc_prf_t *this, chunk_t seed, u_int8_t *buffer)
{
this->xcbc->get_mac(this->xcbc, seed, buffer);
}
/**
* Implementation of prf_t.allocate_bytes.
*/
static void allocate_bytes(private_xcbc_prf_t *this, chunk_t seed, chunk_t *chunk)
METHOD(prf_t, allocate_bytes, void,
private_xcbc_prf_t *this, chunk_t seed, chunk_t *chunk)
{
if (chunk)
{
@@ -59,35 +55,27 @@ static void allocate_bytes(private_xcbc_prf_t *this, chunk_t seed, chunk_t *chun
}
}
/**
* Implementation of prf_t.get_block_size.
*/
static size_t get_block_size(private_xcbc_prf_t *this)
METHOD(prf_t, get_block_size, size_t,
private_xcbc_prf_t *this)
{
return this->xcbc->get_block_size(this->xcbc);
}
/**
* Implementation of prf_t.get_block_size.
*/
static size_t get_key_size(private_xcbc_prf_t *this)
METHOD(prf_t, get_key_size, size_t,
private_xcbc_prf_t *this)
{
/* in xcbc, block and key size are always equal */
return this->xcbc->get_block_size(this->xcbc);
}
/**
* Implementation of prf_t.set_key.
*/
static void set_key(private_xcbc_prf_t *this, chunk_t key)
METHOD(prf_t, set_key, void,
private_xcbc_prf_t *this, chunk_t key)
{
this->xcbc->set_key(this->xcbc, key);
}
/**
* Implementation of prf_t.destroy.
*/
static void destroy(private_xcbc_prf_t *this)
METHOD(prf_t, destroy, void,
private_xcbc_prf_t *this)
{
this->xcbc->destroy(this->xcbc);
free(this);
@@ -114,15 +102,17 @@ xcbc_prf_t *xcbc_prf_create(pseudo_random_function_t algo)
return NULL;
}
this = malloc_thing(private_xcbc_prf_t);
this->xcbc = xcbc;
this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
INIT(this,
.public.prf = {
.get_bytes = _get_bytes,
.allocate_bytes = _allocate_bytes,
.get_block_size = _get_block_size,
.get_key_size = _get_key_size,
.set_key = _set_key,
.destroy = _destroy,
},
.xcbc = xcbc,
);
return &this->public;
}
+2 -2
View File
@@ -34,9 +34,9 @@ typedef struct xcbc_prf_t xcbc_prf_t;
struct xcbc_prf_t {
/**
* Generic prf_t interface for this xcbc_prf_t class.
* Implements prf_t interface.
*/
prf_t prf_interface;
prf_t prf;
};
/**
+27 -44
View File
@@ -41,11 +41,8 @@ struct private_xcbc_signer_t {
size_t block_size;
};
/**
* Implementation of signer_t.get_signature.
*/
static void get_signature(private_xcbc_signer_t *this,
chunk_t data, u_int8_t *buffer)
METHOD(signer_t, get_signature, void,
private_xcbc_signer_t *this, chunk_t data, u_int8_t *buffer)
{
if (buffer == NULL)
{ /* append mode */
@@ -60,11 +57,8 @@ static void get_signature(private_xcbc_signer_t *this,
}
}
/**
* Implementation of signer_t.allocate_signature.
*/
static void allocate_signature (private_xcbc_signer_t *this,
chunk_t data, chunk_t *chunk)
METHOD(signer_t, allocate_signature, void,
private_xcbc_signer_t *this, chunk_t data, chunk_t *chunk)
{
if (chunk == NULL)
{ /* append mode */
@@ -83,11 +77,8 @@ static void allocate_signature (private_xcbc_signer_t *this,
}
}
/**
* Implementation of signer_t.verify_signature.
*/
static bool verify_signature(private_xcbc_signer_t *this,
chunk_t data, chunk_t signature)
METHOD(signer_t, verify_signature, bool,
private_xcbc_signer_t *this, chunk_t data, chunk_t signature)
{
u_int8_t mac[this->xcbc->get_block_size(this->xcbc)];
@@ -100,38 +91,29 @@ static bool verify_signature(private_xcbc_signer_t *this,
return memeq(signature.ptr, mac, this->block_size);
}
/**
* Implementation of signer_t.get_key_size.
*/
static size_t get_key_size(private_xcbc_signer_t *this)
METHOD(signer_t, get_key_size, size_t,
private_xcbc_signer_t *this)
{
return this->xcbc->get_block_size(this->xcbc);
}
/**
* Implementation of signer_t.get_block_size.
*/
static size_t get_block_size(private_xcbc_signer_t *this)
METHOD(signer_t, get_block_size, size_t,
private_xcbc_signer_t *this)
{
return this->block_size;
}
/**
* Implementation of signer_t.set_key.
*/
static void set_key(private_xcbc_signer_t *this, chunk_t key)
METHOD(signer_t, set_key, void,
private_xcbc_signer_t *this, chunk_t key)
{
this->xcbc->set_key(this->xcbc, key);
}
/**
* Implementation of signer_t.destroy.
*/
static status_t destroy(private_xcbc_signer_t *this)
METHOD(signer_t, destroy, void,
private_xcbc_signer_t *this)
{
this->xcbc->destroy(this->xcbc);
free(this);
return SUCCESS;
}
/*
@@ -157,18 +139,19 @@ xcbc_signer_t *xcbc_signer_create(integrity_algorithm_t algo)
return NULL;
}
this = malloc_thing(private_xcbc_signer_t);
this->xcbc = xcbc;
this->block_size = min(trunc, xcbc->get_block_size(xcbc));
/* interface functions */
this->public.signer_interface.get_signature = (void (*) (signer_t*, chunk_t, u_int8_t*))get_signature;
this->public.signer_interface.allocate_signature = (void (*) (signer_t*, chunk_t, chunk_t*))allocate_signature;
this->public.signer_interface.verify_signature = (bool (*) (signer_t*, chunk_t, chunk_t))verify_signature;
this->public.signer_interface.get_key_size = (size_t (*) (signer_t*))get_key_size;
this->public.signer_interface.get_block_size = (size_t (*) (signer_t*))get_block_size;
this->public.signer_interface.set_key = (void (*) (signer_t*,chunk_t))set_key;
this->public.signer_interface.destroy = (void (*) (signer_t*))destroy;
INIT(this,
.public.signer = {
.get_signature = _get_signature,
.allocate_signature = _allocate_signature,
.verify_signature = _verify_signature,
.get_key_size = _get_key_size,
.get_block_size = _get_block_size,
.set_key = _set_key,
.destroy = _destroy,
},
.xcbc = xcbc,
.block_size = min(trunc, xcbc->get_block_size(xcbc)),
);
return &this->public;
}
+2 -2
View File
@@ -31,9 +31,9 @@ typedef struct xcbc_signer_t xcbc_signer_t;
struct xcbc_signer_t {
/**
* generic signer_t interface for this signer
* Implements signer_t interface.
*/
signer_t signer_interface;
signer_t signer;
};
/**