Migraded gcrypt plugin to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-08-10 18:46:30 +02:00
parent 876b61e132
commit 646babd354
11 changed files with 164 additions and 241 deletions
@@ -42,11 +42,8 @@ struct private_gcrypt_crypter_t {
int alg; int alg;
}; };
/** METHOD(crypter_t, decrypt, void,
* Implementation of crypter_t.decrypt. private_gcrypt_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
*/
static void decrypt(private_gcrypt_crypter_t *this, chunk_t data,
chunk_t iv, chunk_t *dst)
{ {
gcry_cipher_setiv(this->h, iv.ptr, iv.len); gcry_cipher_setiv(this->h, iv.ptr, iv.len);
@@ -61,11 +58,8 @@ static void decrypt(private_gcrypt_crypter_t *this, chunk_t data,
} }
} }
/** METHOD(crypter_t, encrypt, void,
* Implementation of crypter_t.encrypt. private_gcrypt_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
*/
static void encrypt(private_gcrypt_crypter_t *this, chunk_t data,
chunk_t iv, chunk_t *dst)
{ {
gcry_cipher_setiv(this->h, iv.ptr, iv.len); gcry_cipher_setiv(this->h, iv.ptr, iv.len);
@@ -80,10 +74,8 @@ static void encrypt(private_gcrypt_crypter_t *this, chunk_t data,
} }
} }
/** METHOD(crypter_t, get_block_size, size_t,
* Implementation of crypter_t.get_block_size. private_gcrypt_crypter_t *this)
*/
static size_t get_block_size(private_gcrypt_crypter_t *this)
{ {
size_t len = 0; size_t len = 0;
@@ -91,10 +83,8 @@ static size_t get_block_size(private_gcrypt_crypter_t *this)
return len; return len;
} }
/** METHOD(crypter_t, get_key_size, size_t,
* Implementation of crypter_t.get_key_size. private_gcrypt_crypter_t *this)
*/
static size_t get_key_size(private_gcrypt_crypter_t *this)
{ {
size_t len = 0; size_t len = 0;
@@ -102,18 +92,14 @@ static size_t get_key_size(private_gcrypt_crypter_t *this)
return len; return len;
} }
/** METHOD(crypter_t, set_key, void,
* Implementation of crypter_t.set_key. private_gcrypt_crypter_t *this, chunk_t key)
*/
static void set_key(private_gcrypt_crypter_t *this, chunk_t key)
{ {
gcry_cipher_setkey(this->h, key.ptr, key.len); gcry_cipher_setkey(this->h, key.ptr, key.len);
} }
/** METHOD(crypter_t, destroy, void,
* Implementation of crypter_t.destroy. private_gcrypt_crypter_t *this)
*/
static void destroy (private_gcrypt_crypter_t *this)
{ {
gcry_cipher_close(this->h); gcry_cipher_close(this->h);
free(this); free(this);
@@ -228,9 +214,18 @@ gcrypt_crypter_t *gcrypt_crypter_create(encryption_algorithm_t algo,
return NULL; return NULL;
} }
this = malloc_thing(private_gcrypt_crypter_t); INIT(this,
.public.crypter = {
.encrypt = _encrypt,
.decrypt = _decrypt,
.get_block_size = _get_block_size,
.get_key_size = _get_key_size,
.set_key = _set_key,
.destroy = _destroy,
},
.alg = gcrypt_alg,
);
this->alg = gcrypt_alg;
err = gcry_cipher_open(&this->h, gcrypt_alg, mode, 0); err = gcry_cipher_open(&this->h, gcrypt_alg, mode, 0);
if (err) if (err)
{ {
@@ -239,14 +234,6 @@ gcrypt_crypter_t *gcrypt_crypter_create(encryption_algorithm_t algo,
free(this); free(this);
return NULL; return NULL;
} }
this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *))encrypt;
this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *))decrypt;
this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *))get_block_size;
this->public.crypter_interface.get_key_size = (size_t (*) (crypter_t *))get_key_size;
this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t))set_key;
this->public.crypter_interface.destroy = (void (*) (crypter_t *))destroy;
return &this->public; return &this->public;
} }
@@ -33,7 +33,7 @@ struct gcrypt_crypter_t {
/** /**
* The crypter_t interface. * The crypter_t interface.
*/ */
crypter_t crypter_interface; crypter_t crypter;
}; };
/** /**
+21 -32
View File
@@ -73,10 +73,8 @@ struct private_gcrypt_dh_t {
size_t p_len; size_t p_len;
}; };
/** METHOD(diffie_hellman_t, set_other_public_value, void,
* Implementation of gcrypt_dh_t.set_other_public_value. private_gcrypt_dh_t *this, chunk_t value)
*/
static void set_other_public_value(private_gcrypt_dh_t *this, chunk_t value)
{ {
gcry_mpi_t p_min_1; gcry_mpi_t p_min_1;
gcry_error_t err; gcry_error_t err;
@@ -134,18 +132,14 @@ static chunk_t export_mpi(gcry_mpi_t value, size_t len)
return chunk; return chunk;
} }
/** METHOD(diffie_hellman_t, get_my_public_value, void,
* Implementation of gcrypt_dh_t.get_my_public_value. private_gcrypt_dh_t *this, chunk_t *value)
*/
static void get_my_public_value(private_gcrypt_dh_t *this, chunk_t *value)
{ {
*value = export_mpi(this->ya, this->p_len); *value = export_mpi(this->ya, this->p_len);
} }
/** METHOD(diffie_hellman_t, get_shared_secret, status_t,
* Implementation of gcrypt_dh_t.get_shared_secret. private_gcrypt_dh_t *this, chunk_t *secret)
*/
static status_t get_shared_secret(private_gcrypt_dh_t *this, chunk_t *secret)
{ {
if (!this->zz) if (!this->zz)
{ {
@@ -155,18 +149,14 @@ static status_t get_shared_secret(private_gcrypt_dh_t *this, chunk_t *secret)
return SUCCESS; return SUCCESS;
} }
/** METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
* Implementation of gcrypt_dh_t.get_dh_group. private_gcrypt_dh_t *this)
*/
static diffie_hellman_group_t get_dh_group(private_gcrypt_dh_t *this)
{ {
return this->group; return this->group;
} }
/** METHOD(diffie_hellman_t, destroy, void,
* Implementation of gcrypt_dh_t.destroy. private_gcrypt_dh_t *this)
*/
static void destroy(private_gcrypt_dh_t *this)
{ {
gcry_mpi_release(this->p); gcry_mpi_release(this->p);
gcry_mpi_release(this->xa); gcry_mpi_release(this->xa);
@@ -194,16 +184,17 @@ gcrypt_dh_t *gcrypt_dh_create(diffie_hellman_group_t group)
return NULL; return NULL;
} }
this = malloc_thing(private_gcrypt_dh_t); INIT(this,
.public.dh = {
this->public.dh.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret; .get_shared_secret = _get_shared_secret,
this->public.dh.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value; .set_other_public_value = _set_other_public_value,
this->public.dh.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value; .get_my_public_value = _get_my_public_value,
this->public.dh.get_dh_group = (diffie_hellman_group_t (*)(diffie_hellman_t *)) get_dh_group; .get_dh_group = _get_dh_group,
this->public.dh.destroy = (void (*)(diffie_hellman_t *)) destroy; .destroy = _destroy,
},
this->group = group; .group = group,
this->p_len = params->prime.len; .p_len = params->prime.len,
);
err = gcry_mpi_scan(&this->p, GCRYMPI_FMT_USG, err = gcry_mpi_scan(&this->p, GCRYMPI_FMT_USG,
params->prime.ptr, params->prime.len, NULL); params->prime.ptr, params->prime.len, NULL);
if (err) if (err)
@@ -251,8 +242,6 @@ gcrypt_dh_t *gcrypt_dh_create(diffie_hellman_group_t group)
} }
this->ya = gcry_mpi_new(this->p_len * 8); this->ya = gcry_mpi_new(this->p_len * 8);
this->yb = NULL;
this->zz = NULL;
gcry_mpi_powm(this->ya, this->g, this->xa, this->p); gcry_mpi_powm(this->ya, this->g, this->xa, this->p);
@@ -37,27 +37,20 @@ struct private_gcrypt_hasher_t {
gcry_md_hd_t hd; gcry_md_hd_t hd;
}; };
/** METHOD(hasher_t, get_hash_size, size_t,
* Implementation of hasher_t.get_hash_size. private_gcrypt_hasher_t *this)
*/
static size_t get_hash_size(private_gcrypt_hasher_t *this)
{ {
return gcry_md_get_algo_dlen(gcry_md_get_algo(this->hd)); return gcry_md_get_algo_dlen(gcry_md_get_algo(this->hd));
} }
/** METHOD(hasher_t, reset, void,
* Implementation of hasher_t.reset. private_gcrypt_hasher_t *this)
*/
static void reset(private_gcrypt_hasher_t *this)
{ {
gcry_md_reset(this->hd); gcry_md_reset(this->hd);
} }
/** METHOD(hasher_t, get_hash, void,
* Implementation of hasher_t.get_hash. private_gcrypt_hasher_t *this, chunk_t chunk, u_int8_t *hash)
*/
static void get_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
u_int8_t *hash)
{ {
gcry_md_write(this->hd, chunk.ptr, chunk.len); gcry_md_write(this->hd, chunk.ptr, chunk.len);
if (hash) if (hash)
@@ -67,11 +60,8 @@ static void get_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
} }
} }
/** METHOD(hasher_t, allocate_hash, void,
* Implementation of hasher_t.allocate_hash. private_gcrypt_hasher_t *this, chunk_t chunk, chunk_t *hash)
*/
static void allocate_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
chunk_t *hash)
{ {
if (hash) if (hash)
{ {
@@ -84,10 +74,8 @@ static void allocate_hash(private_gcrypt_hasher_t *this, chunk_t chunk,
} }
} }
/** METHOD(hasher_t, destroy, void,
* Implementation of hasher_t.destroy. private_gcrypt_hasher_t *this)
*/
static void destroy (private_gcrypt_hasher_t *this)
{ {
gcry_md_close(this->hd); gcry_md_close(this->hd);
free(this); free(this);
@@ -132,7 +120,15 @@ gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo)
return NULL; return NULL;
} }
this = malloc_thing(private_gcrypt_hasher_t); INIT(this,
.public.hasher = {
.get_hash = _get_hash,
.allocate_hash = _allocate_hash,
.get_hash_size = _get_hash_size,
.reset = _reset,
.destroy = _destroy,
},
);
err = gcry_md_open(&this->hd, gcrypt_alg, 0); err = gcry_md_open(&this->hd, gcrypt_alg, 0);
if (err) if (err)
@@ -143,12 +139,6 @@ gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo)
return NULL; return NULL;
} }
this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
this->public.hasher_interface.get_hash_size = (size_t (*) (hasher_t*))get_hash_size;
this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
return &this->public; return &this->public;
} }
@@ -33,7 +33,7 @@ struct gcrypt_hasher_t {
/** /**
* The hasher_t interface. * The hasher_t interface.
*/ */
hasher_t hasher_interface; hasher_t hasher;
}; };
/** /**
@@ -93,10 +93,8 @@ static struct gcry_thread_cbs thread_functions = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
}; };
/** METHOD(plugin_t, destroy, void,
* Implementation of gcrypt_plugin_t.destroy private_gcrypt_plugin_t *this)
*/
static void destroy(private_gcrypt_plugin_t *this)
{ {
lib->crypto->remove_hasher(lib->crypto, lib->crypto->remove_hasher(lib->crypto,
(hasher_constructor_t)gcrypt_hasher_create); (hasher_constructor_t)gcrypt_hasher_create);
@@ -139,9 +137,9 @@ plugin_t *gcrypt_plugin_create()
} }
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
this = malloc_thing(private_gcrypt_plugin_t); INIT(this,
.public.plugin.destroy = _destroy,
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; );
/* hashers */ /* hashers */
lib->crypto->add_hasher(lib->crypto, HASH_SHA1, lib->crypto->add_hasher(lib->crypto, HASH_SHA1,
+14 -21
View File
@@ -35,11 +35,8 @@ struct private_gcrypt_rng_t {
rng_quality_t quality; rng_quality_t quality;
}; };
/** METHOD(rng_t, get_bytes, void,
* Implementation of gcrypt_rng_t.get_bytes. private_gcrypt_rng_t *this, size_t bytes, u_int8_t *buffer)
*/
static void get_bytes(private_gcrypt_rng_t *this, size_t bytes,
u_int8_t *buffer)
{ {
switch (this->quality) switch (this->quality)
{ {
@@ -55,20 +52,15 @@ static void get_bytes(private_gcrypt_rng_t *this, size_t bytes,
} }
} }
/** METHOD(rng_t, allocate_bytes, void,
* Implementation of gcrypt_rng_t.allocate_bytes. private_gcrypt_rng_t *this, size_t bytes, chunk_t *chunk)
*/
static void allocate_bytes(private_gcrypt_rng_t *this, size_t bytes,
chunk_t *chunk)
{ {
*chunk = chunk_alloc(bytes); *chunk = chunk_alloc(bytes);
get_bytes(this, chunk->len, chunk->ptr); get_bytes(this, chunk->len, chunk->ptr);
} }
/** METHOD(rng_t, destroy, void,
* Implementation of gcrypt_rng_t.destroy. private_gcrypt_rng_t *this)
*/
static void destroy(private_gcrypt_rng_t *this)
{ {
free(this); free(this);
} }
@@ -90,13 +82,14 @@ gcrypt_rng_t *gcrypt_rng_create(rng_quality_t quality)
return NULL; return NULL;
} }
this = malloc_thing(private_gcrypt_rng_t); INIT(this,
.public.rng = {
this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes; .get_bytes = _get_bytes,
this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes; .allocate_bytes = _allocate_bytes,
this->public.rng.destroy = (void (*) (rng_t *))destroy; .destroy = _destroy,
},
this->quality = quality; .quality = quality,
);
return &this->public; return &this->public;
} }
@@ -192,19 +192,15 @@ static bool sign_pkcs1(private_gcrypt_rsa_private_key_t *this,
return !!signature->len; return !!signature->len;
} }
/** METHOD(private_key_t, get_type, key_type_t,
* Implementation of gcrypt_rsa_private_key.destroy. private_gcrypt_rsa_private_key_t *this)
*/
static key_type_t get_type(private_gcrypt_rsa_private_key_t *this)
{ {
return KEY_RSA; return KEY_RSA;
} }
/** METHOD(private_key_t, sign, bool,
* Implementation of gcrypt_rsa_private_key.destroy. private_gcrypt_rsa_private_key_t *this, signature_scheme_t scheme,
*/ chunk_t data, chunk_t *sig)
static bool sign(private_gcrypt_rsa_private_key_t *this, signature_scheme_t scheme,
chunk_t data, chunk_t *sig)
{ {
switch (scheme) switch (scheme)
{ {
@@ -229,11 +225,8 @@ static bool sign(private_gcrypt_rsa_private_key_t *this, signature_scheme_t sche
} }
} }
/** METHOD(private_key_t, decrypt, bool,
* Implementation of gcrypt_rsa_private_key.destroy. private_gcrypt_rsa_private_key_t *this, chunk_t encrypted, chunk_t *plain)
*/
static bool decrypt(private_gcrypt_rsa_private_key_t *this,
chunk_t encrypted, chunk_t *plain)
{ {
gcry_error_t err; gcry_error_t err;
gcry_sexp_t in, out; gcry_sexp_t in, out;
@@ -277,18 +270,14 @@ static bool decrypt(private_gcrypt_rsa_private_key_t *this,
return TRUE; return TRUE;
} }
/** METHOD(private_key_t, get_keysize, size_t,
* Implementation of gcrypt_rsa_private_key.get_keysize. private_gcrypt_rsa_private_key_t *this)
*/
static size_t get_keysize(private_gcrypt_rsa_private_key_t *this)
{ {
return gcry_pk_get_nbits(this->key) / 8; return gcry_pk_get_nbits(this->key) / 8;
} }
/** METHOD(private_key_t, get_public_key, public_key_t*,
* Implementation of gcrypt_rsa_private_key.get_public_key. private_gcrypt_rsa_private_key_t *this)
*/
static public_key_t* get_public_key(private_gcrypt_rsa_private_key_t *this)
{ {
chunk_t n, e; chunk_t n, e;
public_key_t *public; public_key_t *public;
@@ -304,11 +293,9 @@ static public_key_t* get_public_key(private_gcrypt_rsa_private_key_t *this)
return public; return public;
} }
/** METHOD(private_key_t, get_encoding, bool,
* Implementation of private_key_t.get_encoding private_gcrypt_rsa_private_key_t *this, cred_encoding_type_t type,
*/ chunk_t *encoding)
static bool get_encoding(private_gcrypt_rsa_private_key_t *this,
cred_encoding_type_t type, chunk_t *encoding)
{ {
chunk_t cn, ce, cp, cq, cd, cu, cexp1 = chunk_empty, cexp2 = chunk_empty; chunk_t cn, ce, cp, cq, cd, cu, cexp1 = chunk_empty, cexp2 = chunk_empty;
gcry_mpi_t p = NULL, q = NULL, d = NULL, exp1, exp2; gcry_mpi_t p = NULL, q = NULL, d = NULL, exp1, exp2;
@@ -385,11 +372,9 @@ static bool get_encoding(private_gcrypt_rsa_private_key_t *this,
return success; return success;
} }
/** METHOD(private_key_t, get_fingerprint, bool,
* Implementation of private_key_t.get_fingerprint private_gcrypt_rsa_private_key_t *this, cred_encoding_type_t type,
*/ chunk_t *fp)
static bool get_fingerprint(private_gcrypt_rsa_private_key_t *this,
cred_encoding_type_t type, chunk_t *fp)
{ {
chunk_t n, e; chunk_t n, e;
bool success; bool success;
@@ -409,19 +394,15 @@ static bool get_fingerprint(private_gcrypt_rsa_private_key_t *this,
return success; return success;
} }
/** METHOD(private_key_t, get_ref, private_key_t*,
* Implementation of gcrypt_rsa_private_key.get_ref. private_gcrypt_rsa_private_key_t *this)
*/
static private_key_t* get_ref(private_gcrypt_rsa_private_key_t *this)
{ {
ref_get(&this->ref); ref_get(&this->ref);
return &this->public.interface; return &this->public.key;
} }
/** METHOD(private_key_t, destroy, void,
* Implementation of gcrypt_rsa_private_key.destroy. private_gcrypt_rsa_private_key_t *this)
*/
static void destroy(private_gcrypt_rsa_private_key_t *this)
{ {
if (ref_put(&this->ref)) if (ref_put(&this->ref))
{ {
@@ -434,25 +415,27 @@ static void destroy(private_gcrypt_rsa_private_key_t *this)
/** /**
* Internal generic constructor * Internal generic constructor
*/ */
static private_gcrypt_rsa_private_key_t *gcrypt_rsa_private_key_create_empty() static private_gcrypt_rsa_private_key_t *create_empty()
{ {
private_gcrypt_rsa_private_key_t *this = malloc_thing(private_gcrypt_rsa_private_key_t); private_gcrypt_rsa_private_key_t *this;
this->public.interface.get_type = (key_type_t (*)(private_key_t *this))get_type; INIT(this,
this->public.interface.sign = (bool (*)(private_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t *signature))sign; .public.key = {
this->public.interface.decrypt = (bool (*)(private_key_t *this, chunk_t crypto, chunk_t *plain))decrypt; .get_type = _get_type,
this->public.interface.get_keysize = (size_t (*) (private_key_t *this))get_keysize; .sign = _sign,
this->public.interface.get_public_key = (public_key_t* (*)(private_key_t *this))get_public_key; .decrypt = _decrypt,
this->public.interface.equals = private_key_equals; .get_keysize = _get_keysize,
this->public.interface.belongs_to = private_key_belongs_to; .get_public_key = _get_public_key,
this->public.interface.get_fingerprint = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint; .equals = private_key_equals,
this->public.interface.has_fingerprint = (bool(*)(private_key_t*, chunk_t fp))private_key_has_fingerprint; .belongs_to = private_key_belongs_to,
this->public.interface.get_encoding = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding; .get_fingerprint = _get_fingerprint,
this->public.interface.get_ref = (private_key_t* (*)(private_key_t *this))get_ref; .has_fingerprint = private_key_has_fingerprint,
this->public.interface.destroy = (void (*)(private_key_t *this))destroy; .get_encoding = _get_encoding,
.get_ref = _get_ref,
this->key = NULL; .destroy = _destroy,
this->ref = 1; },
.ref = 1,
);
return this; return this;
} }
@@ -493,7 +476,7 @@ gcrypt_rsa_private_key_t *gcrypt_rsa_private_key_gen(key_type_t type,
DBG1(DBG_LIB, "building S-expression failed: %s", gpg_strerror(err)); DBG1(DBG_LIB, "building S-expression failed: %s", gpg_strerror(err));
return NULL; return NULL;
} }
this = gcrypt_rsa_private_key_create_empty(); this = create_empty();
err = gcry_pk_genkey(&this->key, param); err = gcry_pk_genkey(&this->key, param);
gcry_sexp_release(param); gcry_sexp_release(param);
if (err) if (err)
@@ -552,7 +535,7 @@ gcrypt_rsa_private_key_t *gcrypt_rsa_private_key_load(key_type_t type,
break; break;
} }
this = gcrypt_rsa_private_key_create_empty(); this = create_empty();
err = gcry_sexp_build(&this->key, NULL, err = gcry_sexp_build(&this->key, NULL,
"(private-key(rsa(n %b)(e %b)(d %b)(p %b)(q %b)(u %b)))", "(private-key(rsa(n %b)(e %b)(d %b)(p %b)(q %b)(u %b)))",
n.len, n.ptr, e.len, e.ptr, d.len, d.ptr, n.len, n.ptr, e.len, e.ptr, d.len, d.ptr,
@@ -34,7 +34,7 @@ struct gcrypt_rsa_private_key_t {
/** /**
* Implements private_key_t interface * Implements private_key_t interface
*/ */
private_key_t interface; private_key_t key;
}; };
/** /**
@@ -159,19 +159,15 @@ static bool verify_pkcs1(private_gcrypt_rsa_public_key_t *this,
return TRUE; return TRUE;
} }
/** METHOD(public_key_t, get_type, key_type_t,
* Implementation of public_key_t.get_type. private_gcrypt_rsa_public_key_t *this)
*/
static key_type_t get_type(private_gcrypt_rsa_public_key_t *this)
{ {
return KEY_RSA; return KEY_RSA;
} }
/** METHOD(public_key_t, verify, bool,
* Implementation of public_key_t.verify. private_gcrypt_rsa_public_key_t *this, signature_scheme_t scheme,
*/ chunk_t data, chunk_t signature)
static bool verify(private_gcrypt_rsa_public_key_t *this,
signature_scheme_t scheme, chunk_t data, chunk_t signature)
{ {
switch (scheme) switch (scheme)
{ {
@@ -196,11 +192,8 @@ static bool verify(private_gcrypt_rsa_public_key_t *this,
} }
} }
/** METHOD(public_key_t, encrypt_, bool,
* Implementation of public_key_t.encrypt. private_gcrypt_rsa_public_key_t *this, chunk_t plain, chunk_t *encrypted)
*/
static bool encrypt_(private_gcrypt_rsa_public_key_t *this, chunk_t plain,
chunk_t *encrypted)
{ {
gcry_sexp_t in, out; gcry_sexp_t in, out;
gcry_error_t err; gcry_error_t err;
@@ -228,19 +221,15 @@ static bool encrypt_(private_gcrypt_rsa_public_key_t *this, chunk_t plain,
return !!encrypted->len; return !!encrypted->len;
} }
/** METHOD(public_key_t, get_keysize, size_t,
* Implementation of public_key_t.get_keysize. private_gcrypt_rsa_public_key_t *this)
*/
static size_t get_keysize(private_gcrypt_rsa_public_key_t *this)
{ {
return gcry_pk_get_nbits(this->key) / 8; return gcry_pk_get_nbits(this->key) / 8;
} }
/** METHOD(public_key_t, get_encoding, bool,
* Implementation of private_key_t.get_encoding private_gcrypt_rsa_public_key_t *this, cred_encoding_type_t type,
*/ chunk_t *encoding)
static bool get_encoding(private_gcrypt_rsa_public_key_t *this,
cred_encoding_type_t type, chunk_t *encoding)
{ {
chunk_t n, e; chunk_t n, e;
bool success; bool success;
@@ -256,11 +245,9 @@ static bool get_encoding(private_gcrypt_rsa_public_key_t *this,
return success; return success;
} }
/** METHOD(public_key_t, get_fingerprint, bool,
* Implementation of private_key_t.get_fingerprint private_gcrypt_rsa_public_key_t *this, cred_encoding_type_t type,
*/ chunk_t *fp)
static bool get_fingerprint(private_gcrypt_rsa_public_key_t *this,
cred_encoding_type_t type, chunk_t *fp)
{ {
chunk_t n, e; chunk_t n, e;
bool success; bool success;
@@ -280,19 +267,15 @@ static bool get_fingerprint(private_gcrypt_rsa_public_key_t *this,
return success; return success;
} }
/** METHOD(public_key_t, get_ref, public_key_t*,
* Implementation of public_key_t.get_ref. private_gcrypt_rsa_public_key_t *this)
*/
static public_key_t* get_ref(private_gcrypt_rsa_public_key_t *this)
{ {
ref_get(&this->ref); ref_get(&this->ref);
return &this->public.interface; return &this->public.key;
} }
/** METHOD(public_key_t, destroy, void,
* Implementation of gcrypt_rsa_public_key.destroy. private_gcrypt_rsa_public_key_t *this)
*/
static void destroy(private_gcrypt_rsa_public_key_t *this)
{ {
if (ref_put(&this->ref)) if (ref_put(&this->ref))
{ {
@@ -331,21 +314,21 @@ gcrypt_rsa_public_key_t *gcrypt_rsa_public_key_load(key_type_t type,
break; break;
} }
this = malloc_thing(private_gcrypt_rsa_public_key_t); INIT(this,
.public.key = {
this->public.interface.get_type = (key_type_t (*)(public_key_t *this))get_type; .get_type = _get_type,
this->public.interface.verify = (bool (*)(public_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t signature))verify; .verify = _verify,
this->public.interface.encrypt = (bool (*)(public_key_t *this, chunk_t crypto, chunk_t *plain))encrypt_; .encrypt = _encrypt_,
this->public.interface.equals = public_key_equals; .equals = public_key_equals,
this->public.interface.get_keysize = (size_t (*) (public_key_t *this))get_keysize; .get_keysize = _get_keysize,
this->public.interface.get_fingerprint = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint; .get_fingerprint = _get_fingerprint,
this->public.interface.has_fingerprint = (bool(*)(public_key_t*, chunk_t fp))public_key_has_fingerprint; .has_fingerprint = public_key_has_fingerprint,
this->public.interface.get_encoding = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding; .get_encoding = _get_encoding,
this->public.interface.get_ref = (public_key_t* (*)(public_key_t *this))get_ref; .get_ref = _get_ref,
this->public.interface.destroy = (void (*)(public_key_t *this))destroy; .destroy = _destroy,
},
this->key = NULL; .ref = 1,
this->ref = 1; );
err = gcry_sexp_build(&this->key, NULL, "(public-key(rsa(n %b)(e %b)))", err = gcry_sexp_build(&this->key, NULL, "(public-key(rsa(n %b)(e %b)))",
n.len, n.ptr, e.len, e.ptr); n.len, n.ptr, e.len, e.ptr);
@@ -34,7 +34,7 @@ struct gcrypt_rsa_public_key_t {
/** /**
* Implements the public_key_t interface * Implements the public_key_t interface
*/ */
public_key_t interface; public_key_t key;
}; };
/** /**