Migrated sha2_hasher to INIT/METHOD macros

This commit is contained in:
Andreas Steffen
2011-09-28 04:41:45 +02:00
parent 47edccd17c
commit 13f90f833f
+116 -123
View File
@@ -426,11 +426,8 @@ static void sha512_final(private_sha512_hasher_t *ctx)
} while(++j < 8); } while(++j < 8);
} }
/** METHOD(hasher_t, get_hash224, void,
* Implementation of hasher_t.get_hash for SHA224. private_sha256_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
*/
static void get_hash224(private_sha256_hasher_t *this,
chunk_t chunk, u_int8_t *buffer)
{ {
sha256_write(this, chunk.ptr, chunk.len); sha256_write(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -441,11 +438,8 @@ static void get_hash224(private_sha256_hasher_t *this,
} }
} }
/** METHOD(hasher_t, get_hash256, void,
* Implementation of hasher_t.get_hash for SHA256. private_sha256_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
*/
static void get_hash256(private_sha256_hasher_t *this,
chunk_t chunk, u_int8_t *buffer)
{ {
sha256_write(this, chunk.ptr, chunk.len); sha256_write(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -456,11 +450,8 @@ static void get_hash256(private_sha256_hasher_t *this,
} }
} }
/** METHOD(hasher_t, get_hash384, void,
* Implementation of hasher_t.get_hash for SHA384. private_sha512_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
*/
static void get_hash384(private_sha512_hasher_t *this,
chunk_t chunk, u_int8_t *buffer)
{ {
sha512_write(this, chunk.ptr, chunk.len); sha512_write(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -471,11 +462,8 @@ static void get_hash384(private_sha512_hasher_t *this,
} }
} }
/** METHOD(hasher_t, get_hash512, void,
* Implementation of hasher_t.get_hash for SHA512. private_sha512_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
*/
static void get_hash512(private_sha512_hasher_t *this,
chunk_t chunk, u_int8_t *buffer)
{ {
sha512_write(this, chunk.ptr, chunk.len); sha512_write(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -486,11 +474,8 @@ static void get_hash512(private_sha512_hasher_t *this,
} }
} }
/** METHOD(hasher_t, allocate_hash224, void,
* Implementation of hasher_t.allocate_hash for SHA224. private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
*/
static void allocate_hash224(private_sha256_hasher_t *this,
chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -505,11 +490,8 @@ static void allocate_hash224(private_sha256_hasher_t *this,
} }
} }
/** METHOD(hasher_t, allocate_hash256, void,
* Implementation of hasher_t.allocate_hash for SHA256. private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
*/
static void allocate_hash256(private_sha256_hasher_t *this,
chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -524,11 +506,8 @@ static void allocate_hash256(private_sha256_hasher_t *this,
} }
} }
/** METHOD(hasher_t, allocate_hash384, void,
* Implementation of hasher_t.allocate_hash for SHA384. private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
*/
static void allocate_hash384(private_sha512_hasher_t *this,
chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -543,11 +522,8 @@ static void allocate_hash384(private_sha512_hasher_t *this,
} }
} }
/** METHOD(hasher_t, allocate_hash512, void,
* Implementation of hasher_t.allocate_hash for SHA512. private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
*/
static void allocate_hash512(private_sha512_hasher_t *this,
chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -562,84 +538,66 @@ static void allocate_hash512(private_sha512_hasher_t *this,
} }
} }
/** METHOD(hasher_t, get_hash_size224, size_t,
* Implementation of hasher_t.get_hash_size for SHA224. private_sha256_hasher_t *this)
*/
static size_t get_hash_size224(private_sha256_hasher_t *this)
{ {
return HASH_SIZE_SHA224; return HASH_SIZE_SHA224;
} }
/** METHOD(hasher_t, get_hash_size256, size_t,
* Implementation of hasher_t.get_hash_size for SHA256. private_sha256_hasher_t *this)
*/
static size_t get_hash_size256(private_sha256_hasher_t *this)
{ {
return HASH_SIZE_SHA256; return HASH_SIZE_SHA256;
} }
/** METHOD(hasher_t, get_hash_size384, size_t,
* Implementation of hasher_t.get_hash_size for SHA384. private_sha512_hasher_t *this)
*/
static size_t get_hash_size384(private_sha512_hasher_t *this)
{ {
return HASH_SIZE_SHA384; return HASH_SIZE_SHA384;
} }
/** METHOD(hasher_t, get_hash_size512, size_t,
* Implementation of hasher_t.get_hash_size for SHA512. private_sha512_hasher_t *this)
*/
static size_t get_hash_size512(private_sha512_hasher_t *this)
{ {
return HASH_SIZE_SHA512; return HASH_SIZE_SHA512;
} }
/** METHOD(hasher_t, reset224, void,
* Implementation of hasher_t.reset for SHA224 private_sha256_hasher_t *this)
*/
static void reset224(private_sha256_hasher_t *ctx)
{ {
memcpy(&ctx->sha_H[0], &sha224_hashInit[0], sizeof(ctx->sha_H)); memcpy(&this->sha_H[0], &sha224_hashInit[0], sizeof(this->sha_H));
ctx->sha_blocks = 0; this->sha_blocks = 0;
ctx->sha_bufCnt = 0; this->sha_bufCnt = 0;
} }
/** METHOD(hasher_t, reset256, void,
* Implementation of hasher_t.reset for SHA256 private_sha256_hasher_t *this)
*/
static void reset256(private_sha256_hasher_t *ctx)
{ {
memcpy(&ctx->sha_H[0], &sha256_hashInit[0], sizeof(ctx->sha_H)); memcpy(&this->sha_H[0], &sha256_hashInit[0], sizeof(this->sha_H));
ctx->sha_blocks = 0; this->sha_blocks = 0;
ctx->sha_bufCnt = 0; this->sha_bufCnt = 0;
} }
/** METHOD(hasher_t, reset384, void,
* Implementation of hasher_t.reset for SHA384 private_sha512_hasher_t *this)
*/
static void reset384(private_sha512_hasher_t *ctx)
{ {
memcpy(&ctx->sha_H[0], &sha384_hashInit[0], sizeof(ctx->sha_H)); memcpy(&this->sha_H[0], &sha384_hashInit[0], sizeof(this->sha_H));
ctx->sha_blocks = 0; this->sha_blocks = 0;
ctx->sha_blocksMSB = 0; this->sha_blocksMSB = 0;
ctx->sha_bufCnt = 0; this->sha_bufCnt = 0;
} }
/** METHOD(hasher_t, reset512, void,
* Implementation of hasher_t.reset for SHA512 private_sha512_hasher_t *this)
*/
static void reset512(private_sha512_hasher_t *ctx)
{ {
memcpy(&ctx->sha_H[0], &sha512_hashInit[0], sizeof(ctx->sha_H)); memcpy(&this->sha_H[0], &sha512_hashInit[0], sizeof(this->sha_H));
ctx->sha_blocks = 0; this->sha_blocks = 0;
ctx->sha_blocksMSB = 0; this->sha_blocksMSB = 0;
ctx->sha_bufCnt = 0; this->sha_bufCnt = 0;
} }
/** METHOD(hasher_t, destroy, void,
* Implementation of hasher_t.destroy. sha2_hasher_t *this)
*/
static void destroy(sha2_hasher_t *this)
{ {
free(this); free(this);
} }
@@ -649,46 +607,81 @@ static void destroy(sha2_hasher_t *this)
*/ */
sha2_hasher_t *sha2_hasher_create(hash_algorithm_t algorithm) sha2_hasher_t *sha2_hasher_create(hash_algorithm_t algorithm)
{ {
sha2_hasher_t *this;
switch (algorithm) switch (algorithm)
{ {
case HASH_SHA224: case HASH_SHA224:
this = (sha2_hasher_t*)malloc_thing(private_sha256_hasher_t); {
this->hasher_interface.reset = (void(*)(hasher_t*))reset224; private_sha256_hasher_t *this;
this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size224;
this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash224; INIT(this,
this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash224; .public = {
break; .hasher_interface = {
.reset = _reset224,
.get_hash_size = _get_hash_size224,
.get_hash = _get_hash224,
.allocate_hash = _allocate_hash224,
.destroy = _destroy,
},
},
);
reset224(this);
return &this->public;
}
case HASH_SHA256: case HASH_SHA256:
this = (sha2_hasher_t*)malloc_thing(private_sha256_hasher_t); {
this->hasher_interface.reset = (void(*)(hasher_t*))reset256; private_sha256_hasher_t *this;
this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size256;
this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash256; INIT(this,
this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash256; .public = {
break; .hasher_interface = {
.reset = _reset256,
.get_hash_size = _get_hash_size256,
.get_hash = _get_hash256,
.allocate_hash = _allocate_hash256,
.destroy = _destroy,
},
},
);
reset256(this);
return &this->public;
}
case HASH_SHA384: case HASH_SHA384:
/* uses SHA512 data structure */ {
this = (sha2_hasher_t*)malloc_thing(private_sha512_hasher_t); private_sha512_hasher_t *this;
this->hasher_interface.reset = (void(*)(hasher_t*))reset384;
this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size384; INIT(this,
this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash384; .public = {
this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash384; .hasher_interface = {
break; .reset = _reset384,
.get_hash_size = _get_hash_size384,
.get_hash = _get_hash384,
.allocate_hash = _allocate_hash384,
.destroy = _destroy,
},
},
);
reset384(this);
return &this->public;
}
case HASH_SHA512: case HASH_SHA512:
this = (sha2_hasher_t*)malloc_thing(private_sha512_hasher_t); {
this->hasher_interface.reset = (void(*)(hasher_t*))reset512; private_sha512_hasher_t *this;
this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size512;
this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash512; INIT(this,
this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash512; .public = {
break; .hasher_interface = {
.reset = _reset512,
.get_hash_size = _get_hash_size512,
.get_hash = _get_hash512,
.allocate_hash = _allocate_hash512,
.destroy = _destroy,
},
},
);
reset512(this);
return &this->public;
}
default: default:
return NULL; return NULL;
} }
this->hasher_interface.destroy = (void(*)(hasher_t*))destroy;
/* initialize */
this->hasher_interface.reset(&this->hasher_interface);
return this;
} }