Migrated padlock plugin to INIT/METHOD macros
authorMartin Willi <martin@revosec.ch>
Fri, 13 Aug 2010 09:27:54 +0000 (11:27 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 13 Aug 2010 15:11:53 +0000 (17:11 +0200)
src/libstrongswan/plugins/padlock/padlock_aes_crypter.c
src/libstrongswan/plugins/padlock/padlock_aes_crypter.h
src/libstrongswan/plugins/padlock/padlock_plugin.c
src/libstrongswan/plugins/padlock/padlock_rng.c
src/libstrongswan/plugins/padlock/padlock_sha1_hasher.c
src/libstrongswan/plugins/padlock/padlock_sha1_hasher.h

index 9edea4b..f7a521e 100644 (file)
@@ -78,8 +78,8 @@ static void padlock_crypt(void *key, void *ctrl, void *src, void *dst,
                : "eax", "ecx", "edx", "esi", "edi");
 }
 
-/*
- * Implementation of crypter_t.crypt
+/**
+ * Do encryption/decryption operation using Padlock control word
  */
 static void crypt(private_padlock_aes_crypter_t *this, char *iv,
                                  chunk_t src, chunk_t *dst, bool enc)
@@ -107,53 +107,38 @@ static void crypt(private_padlock_aes_crypter_t *this, char *iv,
                                  src.len / AES_BLOCK_SIZE, iv_aligned);
 }
 
-/**
- * Implementation of crypter_t.decrypt.
- */
-static void decrypt(private_padlock_aes_crypter_t *this, chunk_t data,
-                                               chunk_t iv, chunk_t *dst)
+METHOD(crypter_t, decrypt, void,
+       private_padlock_aes_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
 {
        crypt(this, iv.ptr, data, dst, TRUE);
 }
 
-
-/**
- * Implementation of crypter_t.encrypt.
- */
-static void encrypt (private_padlock_aes_crypter_t *this, chunk_t data,
-                                                       chunk_t iv, chunk_t *dst)
+METHOD(crypter_t, encrypt, void,
+       private_padlock_aes_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
 {
        crypt(this, iv.ptr, data, dst, FALSE);
 }
 
-/**
- * Implementation of crypter_t.get_block_size.
- */
-static size_t get_block_size(private_padlock_aes_crypter_t *this)
+METHOD(crypter_t, get_block_size, size_t,
+       private_padlock_aes_crypter_t *this)
 {
        return AES_BLOCK_SIZE;
 }
 
-/**
- * Implementation of crypter_t.get_key_size.
- */
-static size_t get_key_size(private_padlock_aes_crypter_t *this)
+METHOD(crypter_t, get_key_size, size_t,
+       private_padlock_aes_crypter_t *this)
 {
        return this->key.len;
 }
 
-/**
- * Implementation of crypter_t.set_key.
- */
-static void set_key(private_padlock_aes_crypter_t *this, chunk_t key)
+METHOD(crypter_t, set_key, void,
+       private_padlock_aes_crypter_t *this, chunk_t key)
 {
        memcpy(this->key.ptr, key.ptr, min(key.len, this->key.len));
 }
 
-/**
- * Implementation of crypter_t.destroy and aes_crypter_t.destroy.
- */
-static void destroy (private_padlock_aes_crypter_t *this)
+METHOD(crypter_t, destroy, void,
+       private_padlock_aes_crypter_t *this)
 {
        free(this->key.ptr);
        free(this);
@@ -171,9 +156,6 @@ padlock_aes_crypter_t *padlock_aes_crypter_create(encryption_algorithm_t algo,
        {
                return NULL;
        }
-
-       this = malloc_thing(private_padlock_aes_crypter_t);
-
        switch (key_size)
        {
                case 16:        /* AES 128 */
@@ -182,18 +164,19 @@ padlock_aes_crypter_t *padlock_aes_crypter_create(encryption_algorithm_t algo,
                case 32:        /* AES-256 */
                        /* These need an expanded key, currently not supported, FALL */
                default:
-                       free(this);
                        return NULL;
        }
 
-       this->key = chunk_alloc(key_size);
-
-       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;
-
+       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,
+               },
+               .key = chunk_alloc(key_size),
+       );
        return &this->public;
 }
index d4c7a75..1c80486 100644 (file)
@@ -32,9 +32,9 @@ typedef struct padlock_aes_crypter_t padlock_aes_crypter_t;
 struct padlock_aes_crypter_t {
 
        /**
-        * The crypter_t interface.
+        * Implements crypter_t interface.
         */
-       crypter_t crypter_interface;
+       crypter_t crypter;
 };
 
 /**
index c9606ae..d846108 100644 (file)
@@ -101,10 +101,8 @@ static padlock_feature_t get_padlock_features()
        return 0;
 }
 
-/**
- * Implementation of aes_plugin_t.destroy
- */
-static void destroy(private_padlock_plugin_t *this)
+METHOD(plugin_t, destroy, void,
+       private_padlock_plugin_t *this)
 {
        if (this->features & PADLOCK_RNG_ENABLED)
        {
@@ -133,11 +131,13 @@ static void destroy(private_padlock_plugin_t *this)
  */
 plugin_t *padlock_plugin_create()
 {
-       private_padlock_plugin_t *this = malloc_thing(private_padlock_plugin_t);
+       private_padlock_plugin_t *this;
 
-       this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
+       INIT(this,
+               .public.plugin.destroy = _destroy,
+               .features = get_padlock_features(),
+       );
 
-       this->features = get_padlock_features();
        if (!this->features)
        {
                free(this);
index 8ff4608..2f26a3a 100644 (file)
@@ -53,15 +53,15 @@ struct private_padlock_rng_t {
  */
 static void rng(char *buf, int len, int quality)
 {
-       while (len > 0)\r
+       while (len > 0)
        {
                int status;
 
                /* run XSTORE until we have all bytes needed. We do not use REP, as
                 * this should not be performance critical and it's easier this way. */
                asm volatile (
-                       ".byte 0x0F,0xA7,0xC0 \n\t"\r
-                       : "=D"(buf), "=a"(status)\r
+                       ".byte 0x0F,0xA7,0xC0 \n\t"
+                       : "=D"(buf), "=a"(status)
                        : "d"(quality), "D"(buf));
 
                /* bits[0..4] of status word contains the number of bytes read */
@@ -69,11 +69,8 @@ static void rng(char *buf, int len, int quality)
        }
 }
 
-/**
- * Implementation of padlock_rng_t.allocate_bytes.
- */
-static void allocate_bytes(private_padlock_rng_t *this, size_t bytes,
-                                                  chunk_t *chunk)
+METHOD(rng_t, allocate_bytes, void,
+       private_padlock_rng_t *this, size_t bytes, chunk_t *chunk)
 {
        chunk->len = bytes;
        /* padlock requires some additional bytes */
@@ -82,11 +79,8 @@ static void allocate_bytes(private_padlock_rng_t *this, size_t bytes,
        rng(chunk->ptr, chunk->len, this->quality);
 }
 
-/**
- * Implementation of padlock_rng_t.get_bytes.
- */
-static void get_bytes(private_padlock_rng_t *this, size_t bytes,
-                                         u_int8_t *buffer)
+METHOD(rng_t, get_bytes, void,
+       private_padlock_rng_t *this, size_t bytes, u_int8_t *buffer)
 {
        chunk_t chunk;
 
@@ -96,10 +90,8 @@ static void get_bytes(private_padlock_rng_t *this, size_t bytes,
        chunk_clear(&chunk);
 }
 
-/**
- * Implementation of padlock_rng_t.destroy.
- */
-static void destroy(private_padlock_rng_t *this)
+METHOD(rng_t, destroy, void,
+       private_padlock_rng_t *this)
 {
        free(this);
 }
@@ -109,11 +101,15 @@ static void destroy(private_padlock_rng_t *this)
  */
 padlock_rng_t *padlock_rng_create(rng_quality_t quality)
 {
-       private_padlock_rng_t *this = malloc_thing(private_padlock_rng_t);
+       private_padlock_rng_t *this;
 
-       this->public.rng.get_bytes = (void (*) (rng_t *, size_t, u_int8_t*)) get_bytes;
-       this->public.rng.allocate_bytes = (void (*) (rng_t *, size_t, chunk_t*)) allocate_bytes;
-       this->public.rng.destroy = (void (*) (rng_t *))destroy;
+       INIT(this,
+               .public.rng = {
+                       .get_bytes = _get_bytes,
+                       .allocate_bytes = _allocate_bytes,
+                       .destroy = _destroy,
+               },
+       );
 
        /* map RNG quality to Padlock quality factor */
        switch (quality)
@@ -127,8 +123,10 @@ padlock_rng_t *padlock_rng_create(rng_quality_t quality)
                case RNG_TRUE:
                        this->quality = PADLOCK_QF3;
                        break;
+               default:
+                       free(this);
+                       return NULL;
        }
-
        return &this->public;
 }
 
index 60b5166..ed331d9 100644 (file)
@@ -83,19 +83,14 @@ static void append_data(private_padlock_sha1_hasher_t *this, chunk_t data)
        this->data.len += data.len;
 }
 
-/**
- * Implementation of hasher_t.reset.
- */
-static void reset(private_padlock_sha1_hasher_t *this)
+METHOD(hasher_t, reset, void,
+       private_padlock_sha1_hasher_t *this)
 {
        chunk_free(&this->data);
 }
 
-/**
- * Implementation of hasher_t.get_hash.
- */
-static void get_hash(private_padlock_sha1_hasher_t *this, chunk_t chunk,
-                                        u_int8_t *hash)
+METHOD(hasher_t, get_hash, void,
+       private_padlock_sha1_hasher_t *this, chunk_t chunk, u_int8_t *hash)
 {
        if (hash)
        {
@@ -116,11 +111,8 @@ static void get_hash(private_padlock_sha1_hasher_t *this, chunk_t chunk,
        }
 }
 
-/**
- * Implementation of hasher_t.allocate_hash.
- */
-static void allocate_hash(private_padlock_sha1_hasher_t *this, chunk_t chunk,
-                                                 chunk_t *hash)
+METHOD(hasher_t, allocate_hash, void,
+       private_padlock_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash)
 {
        if (hash)
        {
@@ -133,18 +125,14 @@ static void allocate_hash(private_padlock_sha1_hasher_t *this, chunk_t chunk,
        }
 }
 
-/**
- * Implementation of hasher_t.get_hash_size.
- */
-static size_t get_hash_size(private_padlock_sha1_hasher_t *this)
+METHOD(hasher_t, get_hash_size, size_t,
+       private_padlock_sha1_hasher_t *this)
 {
        return HASH_SIZE_SHA1;
 }
 
-/**
- * Implementation of hasher_t.destroy.
- */
-static void destroy(private_padlock_sha1_hasher_t *this)
+METHOD(hasher_t, destroy, void,
+       private_padlock_sha1_hasher_t *this)
 {
        free(this->data.ptr);
        free(this);
@@ -161,15 +149,14 @@ padlock_sha1_hasher_t *padlock_sha1_hasher_create(hash_algorithm_t algo)
        {
                return NULL;
        }
-
-       this = malloc_thing(private_padlock_sha1_hasher_t);
-       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;
-
-       this->data = chunk_empty;
-
-       return &(this->public);
+       INIT(this,
+               .public.hasher = {
+                       .get_hash = _get_hash,
+                       .allocate_hash = _allocate_hash,
+                       .get_hash_size = _get_hash_size,
+                       .reset = _reset,
+                       .destroy = _destroy,
+               },
+       );
+       return &this->public;
 }
index 740bdfe..2d2b2b4 100644 (file)
@@ -34,7 +34,7 @@ struct padlock_sha1_hasher_t {
        /**
         * Implements hasher_t interface.
         */
-       hasher_t hasher_interface;
+       hasher_t hasher;
 };
 
 /**