[crypto] Split crypto_algorithm into {digest,cipher,pubkey}_algorithm

The various types of cryptographic algorithm are fundamentally
different, and it was probably a mistake to try to handle them via a
single common type.

pubkey_algorithm is a placeholder type for now.
This commit is contained in:
Michael Brown
2009-02-18 21:56:02 +00:00
parent 5de8305feb
commit a3219b24a8
16 changed files with 169 additions and 130 deletions

View File

@@ -59,12 +59,12 @@ static void aes_cbc_decrypt ( void *ctx, const void *data, void *dst,
AES_cbc_decrypt ( &aesctx->ctx, data, dst, len );
}
struct crypto_algorithm aes_cbc_algorithm = {
struct cipher_algorithm aes_cbc_algorithm = {
.name = "aes_cbc",
.ctxsize = sizeof ( struct aes_cbc_context ),
.blocksize = 16,
.setkey = aes_cbc_setkey,
.setiv = aes_cbc_setiv,
.encode = aes_cbc_encrypt,
.decode = aes_cbc_decrypt,
.encrypt = aes_cbc_encrypt,
.decrypt = aes_cbc_decrypt,
};

View File

@@ -6,8 +6,7 @@ static void sha1_init ( void *ctx ) {
SHA1Init ( ctx );
}
static void sha1_update ( void *ctx, const void *data, void *dst __unused,
size_t len ) {
static void sha1_update ( void *ctx, const void *data, size_t len ) {
SHA1Update ( ctx, data, len );
}
@@ -15,12 +14,12 @@ static void sha1_final ( void *ctx, void *out ) {
SHA1Final ( ctx, out );
}
struct crypto_algorithm sha1_algorithm = {
struct digest_algorithm sha1_algorithm = {
.name = "sha1",
.ctxsize = SHA1_CTX_SIZE,
.blocksize = 64,
.digestsize = SHA1_DIGEST_SIZE,
.init = sha1_init,
.encode = sha1_update,
.update = sha1_update,
.final = sha1_final,
};

View File

@@ -42,7 +42,7 @@
* eventually be freed by a call to chap_finish().
*/
int chap_init ( struct chap_response *chap,
struct crypto_algorithm *digest ) {
struct digest_algorithm *digest ) {
size_t state_len;
void *state;

View File

@@ -2,23 +2,23 @@
#include <errno.h>
#include <gpxe/crypto.h>
int cipher_encrypt ( struct crypto_algorithm *crypto,
int cipher_encrypt ( struct cipher_algorithm *cipher,
void *ctx, const void *src, void *dst,
size_t len ) {
if ( ( len & ( crypto->blocksize - 1 ) ) ) {
if ( ( len & ( cipher->blocksize - 1 ) ) ) {
return -EINVAL;
}
crypto->encode ( ctx, src, dst, len );
cipher->encrypt ( ctx, src, dst, len );
return 0;
}
int cipher_decrypt ( struct crypto_algorithm *crypto,
int cipher_decrypt ( struct cipher_algorithm *cipher,
void *ctx, const void *src, void *dst,
size_t len ) {
if ( ( len & ( crypto->blocksize - 1 ) ) ) {
if ( ( len & ( cipher->blocksize - 1 ) ) ) {
return -EINVAL;
}
crypto->decode ( ctx, src, dst, len );
cipher->decrypt ( ctx, src, dst, len );
return 0;
}

View File

@@ -25,45 +25,61 @@
#include <string.h>
#include <gpxe/crypto.h>
static void null_init ( void *ctx __unused ) {
static void digest_null_init ( void *ctx __unused ) {
/* Do nothing */
}
static int null_setkey ( void *ctx __unused, const void *key __unused,
size_t keylen __unused ) {
/* Do nothing */
return 0;
}
static void null_setiv ( void *ctx __unused, const void *iv __unused ) {
static void digest_null_update ( void *ctx __unused, const void *src __unused,
size_t len __unused ) {
/* Do nothing */
}
static void null_encode ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
if ( dst )
memcpy ( dst, src, len );
}
static void null_decode ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
if ( dst )
memcpy ( dst, src, len );
}
static void null_final ( void *ctx __unused, void *out __unused ) {
static void digest_null_final ( void *ctx __unused, void *out __unused ) {
/* Do nothing */
}
struct crypto_algorithm crypto_null = {
struct digest_algorithm digest_null = {
.name = "null",
.ctxsize = 0,
.blocksize = 1,
.digestsize = 0,
.init = null_init,
.setkey = null_setkey,
.setiv = null_setiv,
.encode = null_encode,
.decode = null_decode,
.final = null_final,
.init = digest_null_init,
.update = digest_null_update,
.final = digest_null_final,
};
static int cipher_null_setkey ( void *ctx __unused, const void *key __unused,
size_t keylen __unused ) {
/* Do nothing */
return 0;
}
static void cipher_null_setiv ( void *ctx __unused,
const void *iv __unused ) {
/* Do nothing */
}
static void cipher_null_encrypt ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
memcpy ( dst, src, len );
}
static void cipher_null_decrypt ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
memcpy ( dst, src, len );
}
struct cipher_algorithm cipher_null = {
.name = "null",
.ctxsize = 0,
.blocksize = 1,
.setkey = cipher_null_setkey,
.setiv = cipher_null_setiv,
.encrypt = cipher_null_encrypt,
.decrypt = cipher_null_decrypt,
};
struct pubkey_algorithm pubkey_null = {
.name = "null",
.ctxsize = 0,
};

View File

@@ -35,7 +35,7 @@
* @v key Key
* @v key_len Length of key
*/
static void hmac_reduce_key ( struct crypto_algorithm *digest,
static void hmac_reduce_key ( struct digest_algorithm *digest,
void *key, size_t *key_len ) {
uint8_t digest_ctx[digest->ctxsize];
@@ -58,7 +58,7 @@ static void hmac_reduce_key ( struct crypto_algorithm *digest,
* will be replaced with its own digest, and key_len will be updated
* accordingly).
*/
void hmac_init ( struct crypto_algorithm *digest, void *digest_ctx,
void hmac_init ( struct digest_algorithm *digest, void *digest_ctx,
void *key, size_t *key_len ) {
unsigned char k_ipad[digest->blocksize];
unsigned int i;
@@ -93,7 +93,7 @@ void hmac_init ( struct crypto_algorithm *digest, void *digest_ctx,
* will be replaced with its own digest, and key_len will be updated
* accordingly).
*/
void hmac_final ( struct crypto_algorithm *digest, void *digest_ctx,
void hmac_final ( struct digest_algorithm *digest, void *digest_ctx,
void *key, size_t *key_len, void *hmac ) {
unsigned char k_opad[digest->blocksize];
unsigned int i;

View File

@@ -167,8 +167,7 @@ static void md5_init(void *context)
mctx->byte_count = 0;
}
static void md5_update(void *context, const void *data, void *dst __unused,
size_t len)
static void md5_update(void *context, const void *data, size_t len)
{
struct md5_ctx *mctx = context;
const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
@@ -224,12 +223,12 @@ static void md5_final(void *context, void *out)
memset(mctx, 0, sizeof(*mctx));
}
struct crypto_algorithm md5_algorithm = {
struct digest_algorithm md5_algorithm = {
.name = "md5",
.ctxsize = MD5_CTX_SIZE,
.blocksize = ( MD5_BLOCK_WORDS * 4 ),
.digestsize = MD5_DIGEST_SIZE,
.init = md5_init,
.encode = md5_update,
.update = md5_update,
.final = md5_final,
};