Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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.
  • Loading branch information
Michael Brown committed Feb 18, 2009
1 parent 5de8305 commit a3219b2
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 121 deletions.
6 changes: 3 additions & 3 deletions src/crypto/axtls_aes.c
Expand Up @@ -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,
};
7 changes: 3 additions & 4 deletions src/crypto/axtls_sha1.c
Expand Up @@ -6,21 +6,20 @@ 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 );
}

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,
};
2 changes: 1 addition & 1 deletion src/crypto/chap.c
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions src/crypto/cipher.c
Expand Up @@ -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;
}

62 changes: 39 additions & 23 deletions src/crypto/crypto_null.c
Expand Up @@ -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 ) {
static void digest_null_update ( void *ctx __unused, const void *src __unused,
size_t len __unused ) {
/* Do nothing */
return 0;
}

static void null_setiv ( void *ctx __unused, const void *iv __unused ) {
static void digest_null_final ( void *ctx __unused, void *out __unused ) {
/* Do nothing */
}

static void null_encode ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
if ( dst )
memcpy ( dst, src, len );
}
struct digest_algorithm digest_null = {
.name = "null",
.ctxsize = 0,
.blocksize = 1,
.digestsize = 0,
.init = digest_null_init,
.update = digest_null_update,
.final = digest_null_final,
};

static void null_decode ( void *ctx __unused, const void *src,
void *dst, size_t len ) {
if ( dst )
memcpy ( dst, src, len );
static int cipher_null_setkey ( void *ctx __unused, const void *key __unused,
size_t keylen __unused ) {
/* Do nothing */
return 0;
}

static void null_final ( void *ctx __unused, void *out __unused ) {
static void cipher_null_setiv ( void *ctx __unused,
const void *iv __unused ) {
/* Do nothing */
}

struct crypto_algorithm crypto_null = {
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,
.digestsize = 0,
.init = null_init,
.setkey = null_setkey,
.setiv = null_setiv,
.encode = null_encode,
.decode = null_decode,
.final = null_final,
.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,
};
6 changes: 3 additions & 3 deletions src/crypto/hmac.c
Expand Up @@ -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];

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/crypto/md5.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
};
4 changes: 2 additions & 2 deletions src/include/gpxe/aes.h
@@ -1,8 +1,8 @@
#ifndef _GPXE_AES_H
#define _GPXE_AES_H

struct crypto_algorithm;
struct cipher_algorithm;

extern struct crypto_algorithm aes_cbc_algorithm;
extern struct cipher_algorithm aes_cbc_algorithm;

#endif /* _GPXE_AES_H */
6 changes: 3 additions & 3 deletions src/include/gpxe/chap.h
Expand Up @@ -10,12 +10,12 @@
#include <stdint.h>
#include <gpxe/md5.h>

struct crypto_algorithm;
struct digest_algorithm;

/** A CHAP response */
struct chap_response {
/** Digest algorithm used for the response */
struct crypto_algorithm *digest;
struct digest_algorithm *digest;
/** Context used by the digest algorithm */
uint8_t *digest_context;
/** CHAP response */
Expand All @@ -25,7 +25,7 @@ struct chap_response {
};

extern int chap_init ( struct chap_response *chap,
struct crypto_algorithm *digest );
struct digest_algorithm *digest );
extern void chap_update ( struct chap_response *chap, const void *data,
size_t len );
extern void chap_respond ( struct chap_response *chap );
Expand Down

0 comments on commit a3219b2

Please sign in to comment.