Migrated des plugin to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-08-13 17:11:53 +02:00
parent 00c7e9af17
commit af403cafa1
3 changed files with 47 additions and 72 deletions
+39 -64
View File
@@ -1416,11 +1416,8 @@ static void des_ede3_cbc_encrypt(des_cblock *input, des_cblock *output, long len
tin[0]=tin[1]=0; tin[0]=tin[1]=0;
} }
/** METHOD(crypter_t, decrypt, void,
* Implementation of crypter_t.decrypt for DES. private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted)
*/
static void decrypt(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *decrypted)
{ {
des_cblock ivb; des_cblock ivb;
u_int8_t *out; u_int8_t *out;
@@ -1437,11 +1434,8 @@ static void decrypt(private_des_crypter_t *this, chunk_t data, chunk_t iv,
} }
/** METHOD(crypter_t, encrypt, void,
* Implementation of crypter_t.decrypt for DES. private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted)
*/
static void encrypt(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *encrypted)
{ {
des_cblock ivb; des_cblock ivb;
u_int8_t *out; u_int8_t *out;
@@ -1457,11 +1451,8 @@ static void encrypt(private_des_crypter_t *this, chunk_t data, chunk_t iv,
data.len, this->ks, &ivb, DES_ENCRYPT); data.len, this->ks, &ivb, DES_ENCRYPT);
} }
/** METHOD(crypter_t, decrypt_ecb, void,
* Implementation of crypter_t.decrypt for DES (ECB). private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted)
*/
static void decrypt_ecb(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *decrypted)
{ {
u_int8_t *out; u_int8_t *out;
@@ -1475,11 +1466,8 @@ static void decrypt_ecb(private_des_crypter_t *this, chunk_t data, chunk_t iv,
data.len, this->ks, DES_DECRYPT); data.len, this->ks, DES_DECRYPT);
} }
/** METHOD(crypter_t, encrypt_ecb, void,
* Implementation of crypter_t.decrypt for DES (ECB). private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted)
*/
static void encrypt_ecb(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *encrypted)
{ {
u_int8_t *out; u_int8_t *out;
@@ -1493,11 +1481,8 @@ static void encrypt_ecb(private_des_crypter_t *this, chunk_t data, chunk_t iv,
data.len, this->ks, DES_ENCRYPT); data.len, this->ks, DES_ENCRYPT);
} }
/** METHOD(crypter_t, decrypt3, void,
* Implementation of crypter_t.decrypt for 3DES. private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted)
*/
static void decrypt3(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *decrypted)
{ {
des_cblock ivb; des_cblock ivb;
u_int8_t *out; u_int8_t *out;
@@ -1514,11 +1499,8 @@ static void decrypt3(private_des_crypter_t *this, chunk_t data, chunk_t iv,
&ivb, DES_DECRYPT); &ivb, DES_DECRYPT);
} }
/** METHOD(crypter_t, encrypt3, void,
* Implementation of crypter_t.decrypt for 3DES. private_des_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted)
*/
static void encrypt3(private_des_crypter_t *this, chunk_t data, chunk_t iv,
chunk_t *encrypted)
{ {
des_cblock ivb; des_cblock ivb;
u_int8_t *out; u_int8_t *out;
@@ -1535,44 +1517,34 @@ static void encrypt3(private_des_crypter_t *this, chunk_t data, chunk_t iv,
&ivb, DES_ENCRYPT); &ivb, DES_ENCRYPT);
} }
/** METHOD(crypter_t, get_block_size, size_t,
* Implementation of crypter_t.get_block_size. private_des_crypter_t *this)
*/
static size_t get_block_size (private_des_crypter_t *this)
{ {
return sizeof(des_cblock); return sizeof(des_cblock);
} }
/** METHOD(crypter_t, get_key_size, size_t,
* Implementation of crypter_t.get_key_size. private_des_crypter_t *this)
*/
static size_t get_key_size (private_des_crypter_t *this)
{ {
return this->key_size; return this->key_size;
} }
/** METHOD(crypter_t, set_key, void,
* Implementation of crypter_t.set_key for DES. private_des_crypter_t *this, chunk_t key)
*/
static void set_key(private_des_crypter_t *this, chunk_t key)
{ {
des_set_key((des_cblock*)(key.ptr), &this->ks); des_set_key((des_cblock*)(key.ptr), &this->ks);
} }
/** METHOD(crypter_t, set_key3, void,
* Implementation of crypter_t.set_key for 3DES. private_des_crypter_t *this, chunk_t key)
*/
static void set_key3(private_des_crypter_t *this, chunk_t key)
{ {
des_set_key((des_cblock*)(key.ptr) + 0, &this->ks3[0]); des_set_key((des_cblock*)(key.ptr) + 0, &this->ks3[0]);
des_set_key((des_cblock*)(key.ptr) + 1, &this->ks3[1]); des_set_key((des_cblock*)(key.ptr) + 1, &this->ks3[1]);
des_set_key((des_cblock*)(key.ptr) + 2, &this->ks3[2]); des_set_key((des_cblock*)(key.ptr) + 2, &this->ks3[2]);
} }
/** METHOD(crypter_t, destroy, void,
* Implementation of crypter_t.destroy and des_crypter_t.destroy. private_des_crypter_t *this)
*/
static void destroy(private_des_crypter_t *this)
{ {
free(this); free(this);
} }
@@ -1582,33 +1554,36 @@ static void destroy(private_des_crypter_t *this)
*/ */
des_crypter_t *des_crypter_create(encryption_algorithm_t algo) des_crypter_t *des_crypter_create(encryption_algorithm_t algo)
{ {
private_des_crypter_t *this = malloc_thing(private_des_crypter_t); private_des_crypter_t *this;
/* functions of crypter_t interface */ INIT(this,
this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *)) get_block_size; .public.crypter = {
this->public.crypter_interface.get_key_size = (size_t (*) (crypter_t *)) get_key_size; .get_block_size = _get_block_size,
this->public.crypter_interface.destroy = (void (*) (crypter_t *)) destroy; .get_key_size = _get_key_size,
.destroy = _destroy,
},
);
/* use functions depending on algorithm */ /* use functions depending on algorithm */
switch (algo) switch (algo)
{ {
case ENCR_DES: case ENCR_DES:
this->key_size = sizeof(des_cblock); this->key_size = sizeof(des_cblock);
this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t)) set_key; this->public.crypter.set_key = _set_key;
this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *)) encrypt; this->public.crypter.encrypt = _encrypt;
this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt; this->public.crypter.decrypt = _decrypt;
break; break;
case ENCR_3DES: case ENCR_3DES:
this->key_size = 3 * sizeof(des_cblock); this->key_size = 3 * sizeof(des_cblock);
this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t)) set_key3; this->public.crypter.set_key = _set_key3;
this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *)) encrypt3; this->public.crypter.encrypt = _encrypt3;
this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt3; this->public.crypter.decrypt = _decrypt3;
break; break;
case ENCR_DES_ECB: case ENCR_DES_ECB:
this->key_size = sizeof(des_cblock); this->key_size = sizeof(des_cblock);
this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t)) set_key; this->public.crypter.set_key = _set_key;
this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *)) encrypt_ecb; this->public.crypter.encrypt = _encrypt_ecb;
this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt_ecb; this->public.crypter.decrypt = _decrypt_ecb;
break; break;
default: default:
free(this); free(this);
+2 -2
View File
@@ -32,9 +32,9 @@ typedef struct des_crypter_t des_crypter_t;
struct des_crypter_t { struct des_crypter_t {
/** /**
* The crypter_t interface. * Implements crypter_t interface.
*/ */
crypter_t crypter_interface; crypter_t crypter;
}; };
/** /**
+6 -6
View File
@@ -31,10 +31,8 @@ struct private_des_plugin_t {
des_plugin_t public; des_plugin_t public;
}; };
/** METHOD(plugin_t, destroy, void,
* Implementation of des_plugin_t.destroy private_des_plugin_t *this)
*/
static void destroy(private_des_plugin_t *this)
{ {
lib->crypto->remove_crypter(lib->crypto, lib->crypto->remove_crypter(lib->crypto,
(crypter_constructor_t)des_crypter_create); (crypter_constructor_t)des_crypter_create);
@@ -46,9 +44,11 @@ static void destroy(private_des_plugin_t *this)
*/ */
plugin_t *des_plugin_create() plugin_t *des_plugin_create()
{ {
private_des_plugin_t *this = malloc_thing(private_des_plugin_t); private_des_plugin_t *this;
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; INIT(this,
.public.plugin.destroy = _destroy,
);
lib->crypto->add_crypter(lib->crypto, ENCR_3DES, lib->crypto->add_crypter(lib->crypto, ENCR_3DES,
(crypter_constructor_t)des_crypter_create); (crypter_constructor_t)des_crypter_create);