- code cleaned up

This commit is contained in:
Jan Hutter
2005-12-06 15:37:56 +00:00
parent 62abe4a37d
commit 6f36717843
11 changed files with 119 additions and 79 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
/** /**
* @file crypter.h * @file crypter.h
* *
* @brief Interface of crypter_t * @brief Interface crypter_t
* *
*/ */
+3 -3
View File
@@ -1,7 +1,7 @@
/** /**
* @file hasher.c * @file hasher.c
* *
* @brief Generic constructor for hasher_t * @brief Generic constructor for hasher_t.
* *
*/ */
@@ -27,7 +27,7 @@
#include <transforms/hashers/md5_hasher.h> #include <transforms/hashers/md5_hasher.h>
/** /**
* mappings for hash_algorithm_t * String mappings for hash_algorithm_t.
*/ */
mapping_t hash_algorithm_m[] = { mapping_t hash_algorithm_m[] = {
{HASH_MD2,"HASH_MD2"}, {HASH_MD2,"HASH_MD2"},
@@ -40,7 +40,7 @@ mapping_t hash_algorithm_m[] = {
}; };
/* /*
* Described in header * Described in header.
*/ */
hasher_t *hasher_create(hash_algorithm_t hash_algorithm) hasher_t *hasher_create(hash_algorithm_t hash_algorithm)
{ {
+38 -18
View File
@@ -1,7 +1,7 @@
/** /**
* @file hasher.h * @file hasher.h
* *
* @brief Interface for hasher_t. * @brief Interface hasher_t.
* *
*/ */
@@ -26,22 +26,36 @@
#include <types.h> #include <types.h>
typedef enum hash_algorithm_t hash_algorithm_t; typedef enum hash_algorithm_t hash_algorithm_t;
/** /**
* @brief Algorithms to use for hashing. * @brief Algorithms to use for hashing.
*
* Currently only the following algorithms are implemented and therefore supported:
* - HASH_MD5
* - HASH_SHA1
*
* @ingroup hashers
*
*/ */
enum hash_algorithm_t { enum hash_algorithm_t {
HASH_MD2, HASH_MD2,
HASH_MD5, /* supported */ /**
HASH_SHA1, /* supported */ * Implemented in class md5_hasher_t.
*/
HASH_MD5,
/**
* Implemented in class sha1_hasher_t.
*/
HASH_SHA1,
HASH_SHA256, HASH_SHA256,
HASH_SHA384, HASH_SHA384,
HASH_SHA512, HASH_SHA512,
}; };
/** /**
* string mappings for hash_algorithm_t * String mappings for hash_algorithm_t.
*/ */
extern mapping_t hash_algorithm_m[]; extern mapping_t hash_algorithm_m[];
@@ -52,37 +66,43 @@ typedef struct hasher_t hasher_t;
* @brief Generic interface for all hash functions. * @brief Generic interface for all hash functions.
* *
* @b Constructors: * @b Constructors:
* - hasher_create() * - hasher_create()
* - md5_hasher_create()
* - sha1_hasher_create()
* *
* @see md5_hasher_t, sha1_hasher_t * @see
* - md5_hasher_t
* - sha1_hasher_t
*
* @todo Implement more hash algorithms
* *
* @ingroup hashers * @ingroup hashers
*/ */
struct hasher_t { struct hasher_t {
/** /**
* @brief hash data and write it in the buffer * @brief Hash data and write it in the buffer.
* *
* If the parameter hash is NULL, no result is written back * If the parameter hash is NULL, no result is written back
* an more data can be appended to already hashed data. * an more data can be appended to already hashed data.
* If not, the result is written back and the hasher is reset. * If not, the result is written back and the hasher is reseted.
* *
* @warning: the hash output parameter must hold at least * @warning: the hash output parameter must hold at least
* hash_t.get_block_size bytes. * hash_t.get_block_size bytes.
* *
* @param this calling hasher * @param this calling object
* @param data data to hash * @param data data to hash
* @param [out]buffer pointer where the hash will be written * @param [out]hash pointer where the hash will be written
*/ */
void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash); void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
/** /**
* @brief hash data and allocate space for the hash * @brief Hash data and allocate space for the hash.
* *
* If the parameter hash is NULL, no result is written back * If the parameter hash is NULL, no result is written back
* an more data can be appended to already hashed data. * an more data can be appended to already hashed data.
* If not, the result is written back and the hasher is reset. * If not, the result is written back and the hasher is reseted.
* *
* @param this calling hasher * @param this calling object
* @param data chunk with data to hash * @param data chunk with data to hash
* @param [out]hash chunk which will hold allocated hash * @param [out]hash chunk which will hold allocated hash
*/ */
@@ -91,23 +111,23 @@ struct hasher_t {
/** /**
* @brief Get the block size of this hashing function. * @brief Get the block size of this hashing function.
* *
* @param this calling hasher * @param this calling object
* @return block size in bytes * @return block size in bytes
*/ */
size_t (*get_block_size) (hasher_t *this); size_t (*get_block_size) (hasher_t *this);
/** /**
* @brief Resets the hashers state, which allows * @brief Resets the hashers state, which allows
* computation of a completly new hash. * computation of a completely new hash.
* *
* @param this calling hasher * @param this calling object
*/ */
void (*reset) (hasher_t *this); void (*reset) (hasher_t *this);
/** /**
* @brief Destroys a hasher object. * @brief Destroys a hasher object.
* *
* @param this hasher_t object to destroy * @param this calling object
*/ */
void (*destroy) (hasher_t *this); void (*destroy) (hasher_t *this);
}; };
@@ -117,7 +137,7 @@ struct hasher_t {
* *
* @param hash_algorithm Algorithm to use for hashing * @param hash_algorithm Algorithm to use for hashing
* @return * @return
* - hasher_t if successfully * - hasher_t object
* - NULL if algorithm not supported * - NULL if algorithm not supported
* *
* @ingroup hashers * @ingroup hashers
@@ -100,16 +100,16 @@ Rotation is separate from addition to prevent recomputation.
typedef struct private_md5_hasher_t private_md5_hasher_t; typedef struct private_md5_hasher_t private_md5_hasher_t;
/** /**
* private data structure with hasing context * Private data structure with hasing context.
*/ */
struct private_md5_hasher_t { struct private_md5_hasher_t {
/** /**
* public interface for this hasher * Public interface for this hasher.
*/ */
md5_hasher_t public; md5_hasher_t public;
/* /*
* state of the hasher * State of the hasher.
*/ */
u_int32_t state[5]; u_int32_t state[5];
u_int32_t count[2]; u_int32_t count[2];
@@ -311,7 +311,7 @@ static void MD5Final (private_md5_hasher_t *this, u_int8_t digest[16])
/** /**
* implementation of hasher_t.get_hash for md5 * Implementation of hasher_t.get_hash.
*/ */
static void get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *buffer) static void get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
{ {
@@ -325,7 +325,7 @@ static void get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *buffer
/** /**
* implementation of hasher_t.allocate_hash for md5 * Implementation of hasher_t.allocate_hash.
*/ */
static void allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t *hash) static void allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t *hash)
{ {
@@ -345,7 +345,7 @@ static void allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t *ha
} }
/** /**
* implementation of hasher_t.get_block_size for md5 * Implementation of hasher_t.get_block_size.
*/ */
static size_t get_block_size(private_md5_hasher_t *this) static size_t get_block_size(private_md5_hasher_t *this)
{ {
@@ -353,7 +353,7 @@ static size_t get_block_size(private_md5_hasher_t *this)
} }
/** /**
* implementation of hasher_t.reset for md5 * Implementation of hasher_t.reset.
*/ */
static void reset(private_md5_hasher_t *this) static void reset(private_md5_hasher_t *this)
{ {
@@ -366,7 +366,7 @@ static void reset(private_md5_hasher_t *this)
} }
/** /**
* implementation of hasher_t.destroy for md5 * Implementation of hasher_t.destroy.
*/ */
static void destroy(private_md5_hasher_t *this) static void destroy(private_md5_hasher_t *this)
{ {
@@ -374,7 +374,7 @@ static void destroy(private_md5_hasher_t *this)
} }
/* /*
* Described in header * Described in header.
*/ */
md5_hasher_t *md5_hasher_create() md5_hasher_t *md5_hasher_create()
{ {
@@ -33,7 +33,7 @@ typedef struct md5_hasher_t md5_hasher_t;
* MD5 algorithm. * MD5 algorithm.
* *
* @b Constructors: * @b Constructors:
* - hasher_create() using HASH_MD5 * - hasher_create() using HASH_MD5 as algorithm
* - md5_hasher_create() * - md5_hasher_create()
* *
* @see hasher_t * @see hasher_t
@@ -43,7 +43,7 @@ typedef struct md5_hasher_t md5_hasher_t;
struct md5_hasher_t { struct md5_hasher_t {
/** /**
* generic hasher_t interface for this hasher * Generic hasher_t interface for this hasher.
*/ */
hasher_t hasher_interface; hasher_t hasher_interface;
}; };
+9 -13
View File
@@ -55,16 +55,16 @@
typedef struct private_sha1_hasher_t private_sha1_hasher_t; typedef struct private_sha1_hasher_t private_sha1_hasher_t;
/** /**
* private data structure with hasing context * Private data structure with hasing context.
*/ */
struct private_sha1_hasher_t { struct private_sha1_hasher_t {
/** /**
* public interface for this hasher * Public interface for this hasher.
*/ */
sha1_hasher_t public; sha1_hasher_t public;
/* /*
* state of the hasher * State of the hasher.
*/ */
u_int32_t state[5]; u_int32_t state[5];
u_int32_t count[2]; u_int32_t count[2];
@@ -185,7 +185,7 @@ static void SHA1Final(private_sha1_hasher_t *this, u_int8_t *digest)
/** /**
* implementation of hasher_t.get_hash for sha1 * Implementation of hasher_t.get_hash.
*/ */
static void get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffer) static void get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
{ {
@@ -199,7 +199,7 @@ static void get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffe
/** /**
* implementation of hasher_t.allocate_hash for sha1 * Implementation of hasher_t.allocate_hash.
*/ */
static void allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash) static void allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash)
{ {
@@ -219,7 +219,7 @@ static void allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *h
} }
/** /**
* implementation of hasher_t.get_block_size for sha1 * Implementation of hasher_t.get_block_size.
*/ */
static size_t get_block_size(private_sha1_hasher_t *this) static size_t get_block_size(private_sha1_hasher_t *this)
{ {
@@ -227,7 +227,7 @@ static size_t get_block_size(private_sha1_hasher_t *this)
} }
/** /**
* implementation of hasher_t.reset for sha1 * Implementation of hasher_t.reset.
*/ */
static void reset(private_sha1_hasher_t *this) static void reset(private_sha1_hasher_t *this)
{ {
@@ -240,7 +240,7 @@ static void reset(private_sha1_hasher_t *this)
this->count[1] = 0; this->count[1] = 0;
} }
/** /**
* implementation of hasher_t.destroy for sha1 * Implementation of hasher_t.destroy.
*/ */
static void destroy(private_sha1_hasher_t *this) static void destroy(private_sha1_hasher_t *this)
{ {
@@ -249,15 +249,11 @@ static void destroy(private_sha1_hasher_t *this)
/* /*
* Described in header * Described in header.
*/ */
sha1_hasher_t *sha1_hasher_create() sha1_hasher_t *sha1_hasher_create()
{ {
private_sha1_hasher_t *this = allocator_alloc_thing(private_sha1_hasher_t); private_sha1_hasher_t *this = allocator_alloc_thing(private_sha1_hasher_t);
if (this == NULL)
{
return NULL;
}
this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash; 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.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
@@ -33,7 +33,7 @@ typedef struct sha1_hasher_t sha1_hasher_t;
* SHA1 algorithm. * SHA1 algorithm.
* *
* @b Constructors: * @b Constructors:
* - hasher_create() using HASH_SHA1 * - hasher_create() using HASH_SHA1 as algorithm
* - sha1_hasher_create() * - sha1_hasher_create()
* *
* @see hasher_t * @see hasher_t
@@ -43,7 +43,7 @@ typedef struct sha1_hasher_t sha1_hasher_t;
struct sha1_hasher_t { struct sha1_hasher_t {
/** /**
* generic hasher_t interface for this hasher * Generic hasher_t interface for this hasher.
*/ */
hasher_t hasher_interface; hasher_t hasher_interface;
}; };
+12 -8
View File
@@ -25,22 +25,26 @@
#include <utils/allocator.h> #include <utils/allocator.h>
#include <transforms/hmac.h> #include <transforms/hmac.h>
typedef struct private_hmac_prf_t private_hmac_prf_t; typedef struct private_hmac_prf_t private_hmac_prf_t;
/**
* Private data of a hma_prf_t object.
*/
struct private_hmac_prf_t { struct private_hmac_prf_t {
/** /**
* public interface for this prf * Public hmac_prf_t interface.
*/ */
hmac_prf_t public; hmac_prf_t public;
/** /**
* hmac to use for generation * Hmac to use for generation.
*/ */
hmac_t *hmac; hmac_t *hmac;
}; };
/** /**
* implementation of prf_t.get_bytes * Implementation of prf_t.get_bytes.
*/ */
static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer) static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
{ {
@@ -48,7 +52,7 @@ static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
} }
/** /**
* implementation of prf_t.allocate_bytes * Implementation of prf_t.allocate_bytes.
*/ */
static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk) static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
{ {
@@ -56,7 +60,7 @@ static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chun
} }
/** /**
* implementation of prf_t.get_block_size * Implementation of prf_t.get_block_size.
*/ */
static size_t get_block_size(private_hmac_prf_t *this) static size_t get_block_size(private_hmac_prf_t *this)
{ {
@@ -64,7 +68,7 @@ static size_t get_block_size(private_hmac_prf_t *this)
} }
/** /**
* implementation of prf_t.set_key * Implementation of prf_t.set_key.
*/ */
static void set_key(private_hmac_prf_t *this, chunk_t key) static void set_key(private_hmac_prf_t *this, chunk_t key)
{ {
@@ -72,7 +76,7 @@ static void set_key(private_hmac_prf_t *this, chunk_t key)
} }
/** /**
* implementation of prf_t.destroy * Implementation of prf_t.destroy.
*/ */
static void destroy(private_hmac_prf_t *this) static void destroy(private_hmac_prf_t *this)
{ {
@@ -81,7 +85,7 @@ static void destroy(private_hmac_prf_t *this)
} }
/* /*
* Described in header * Described in header.
*/ */
hmac_prf_t *hmac_prf_create(hash_algorithm_t hash_algorithm) hmac_prf_t *hmac_prf_create(hash_algorithm_t hash_algorithm)
{ {
+8 -5
View File
@@ -1,7 +1,7 @@
/** /**
* @file hmac_prf.h * @file hmac_prf.h
* *
* @brief Interface for hmac_prf_t. * @brief Interface of hmac_prf_t.
* *
*/ */
@@ -31,10 +31,13 @@ typedef struct hmac_prf_t hmac_prf_t;
/** /**
* @brief Implementation of prf_t interface using the * @brief Implementation of prf_t interface using the
* a HMAC algorithm. * HMAC algorithm.
* *
* This simply wraps a hmac_t in a prf_t. More a question of * This simply wraps a hmac_t in a prf_t. More a question of
* interface matchig. * interface matching.
*
* @b Constructors:
* - hmac_prf_create()
* *
* @ingroup prfs * @ingroup prfs
*/ */
@@ -47,11 +50,11 @@ struct hmac_prf_t {
}; };
/** /**
* @brief Creates a new hmac_prf_t object * @brief Creates a new hmac_prf_t object.
* *
* @param hash_algorithm hmac's hash algorithm * @param hash_algorithm hmac's hash algorithm
* @return * @return
* - hmac_prf_t if successfully * - hmac_prf_t object
* - NULL if hash not supported * - NULL if hash not supported
* *
* @ingroup prfs * @ingroup prfs
+2 -3
View File
@@ -28,7 +28,7 @@
/** /**
* string mappings for encryption_algorithm_t * String mappings for encryption_algorithm_t.
*/ */
mapping_t pseudo_random_function_m[] = { mapping_t pseudo_random_function_m[] = {
{PRF_UNDEFINED, "PRF_UNDEFINED"}, {PRF_UNDEFINED, "PRF_UNDEFINED"},
@@ -39,9 +39,8 @@ mapping_t pseudo_random_function_m[] = {
{MAPPING_END, NULL} {MAPPING_END, NULL}
}; };
/* /*
* Described in header * Described in header.
*/ */
prf_t *prf_create(pseudo_random_function_t pseudo_random_function) prf_t *prf_create(pseudo_random_function_t pseudo_random_function)
{ {
+33 -15
View File
@@ -1,7 +1,7 @@
/** /**
* @file prf.h * @file prf.h
* *
* @brief Interface of prf_t. * @brief Interface prf_t.
* *
*/ */
@@ -29,17 +29,29 @@ typedef enum pseudo_random_function_t pseudo_random_function_t;
/** /**
* @brief Pseudo random function, as in IKEv2 draft 3.3.2. * @brief Pseudo random function, as in IKEv2 draft 3.3.2.
*
* Currently only the following algorithms are implemented and therefore supported:
* - PRF_HMAC_MD5
* - PRF_HMAC_SHA1
*
* @ingroup prfs
*/ */
enum pseudo_random_function_t { enum pseudo_random_function_t {
PRF_UNDEFINED = 1024, PRF_UNDEFINED = 1024,
/**
* Implemented in class hmac_prf_t.
*/
PRF_HMAC_MD5 = 1, PRF_HMAC_MD5 = 1,
/**
* Implemented in class hmac_prf_t.
*/
PRF_HMAC_SHA1 = 2, PRF_HMAC_SHA1 = 2,
PRF_HMAC_TIGER = 3, PRF_HMAC_TIGER = 3,
PRF_AES128_CBC = 4 PRF_AES128_CBC = 4
}; };
/** /**
* string mappings for encryption_algorithm_t * String mappings for encryption_algorithm_t.
*/ */
extern mapping_t pseudo_random_function_m[]; extern mapping_t pseudo_random_function_m[];
@@ -49,59 +61,65 @@ typedef struct prf_t prf_t;
/** /**
* @brief Generic interface for pseudo-random-functions. * @brief Generic interface for pseudo-random-functions.
* *
* @b Constructors:
* - prf_create()
* - hmac_prf_create()
*
* @todo Implement more prf algorithms
*
* @ingroup prfs * @ingroup prfs
*/ */
struct prf_t { struct prf_t {
/** /**
* @brief generates pseudo random bytes and writes them * @brief Generates pseudo random bytes and writes them
* in the buffer. * in the buffer.
* *
* @param this calling prf * @param this calling object
* @param seed a chunk containing the seed for the next bytes * @param seed a chunk containing the seed for the next bytes
* @param[out] buffer pointer where the generated bytes will be written * @param[out] buffer pointer where the generated bytes will be written
*/ */
void (*get_bytes) (prf_t *this, chunk_t seed, u_int8_t *buffer); void (*get_bytes) (prf_t *this, chunk_t seed, u_int8_t *buffer);
/** /**
* @brief generates pseudo random bytes and allocate space for them. * @brief Generates pseudo random bytes and allocate space for them.
* *
* @param this calling prf * @param this calling object
* @param seed a chunk containing the seed for the next bytes * @param seed a chunk containing the seed for the next bytes
* @param[out] chunk chunk which will hold generated bytes * @param[out] chunk chunk which will hold generated bytes
*/ */
void (*allocate_bytes) (prf_t *this, chunk_t seed, chunk_t *chunk); void (*allocate_bytes) (prf_t *this, chunk_t seed, chunk_t *chunk);
/** /**
* @brief get the block size of this prf. * @brief Get the block size of this prf_t object.
* *
* @param this calling prf * @param this calling object
* @return block size in bytes * @return block size in bytes
*/ */
size_t (*get_block_size) (prf_t *this); size_t (*get_block_size) (prf_t *this);
/** /**
* @brief Set the key for this prf. * @brief Set the key for this prf_t object.
* *
* @param this calling prf * @param this calling object
* @param key key to set * @param key key to set
*/ */
void (*set_key) (prf_t *this, chunk_t key); void (*set_key) (prf_t *this, chunk_t key);
/** /**
* @brief Destroys a prf object.. * @brief Destroys a prf object.
* *
* @param this prf_t object to destroy * @param this calling object
*/ */
void (*destroy) (prf_t *this); void (*destroy) (prf_t *this);
}; };
/** /**
* @brief Generic constructor for a prf_t. * @brief Generic constructor for a prf_t oject.
* *
* @param pseudo_random_function Algorithm to use * @param pseudo_random_function Algorithm to use
* @return * @return
* - prf_t if successfully * - prf_t object
* - NULL if prf not supported * - NULL if prf algorithm not supported
* *
* @ingroup prfs * @ingroup prfs
*/ */