2007-01-31 19:09:20 +01:00
|
|
|
#include "crypto/axtls/crypto.h"
|
|
|
|
#include <string.h>
|
2007-07-24 18:11:31 +02:00
|
|
|
#include <errno.h>
|
2007-01-31 19:09:20 +01:00
|
|
|
#include <gpxe/crypto.h>
|
|
|
|
#include <gpxe/aes.h>
|
|
|
|
|
2009-02-18 22:33:54 +01:00
|
|
|
struct aes_cbc_context {
|
|
|
|
AES_CTX ctx;
|
|
|
|
int decrypting;
|
|
|
|
};
|
|
|
|
|
2009-02-18 22:25:14 +01:00
|
|
|
static int aes_cbc_setkey ( void *ctx, const void *key, size_t keylen ) {
|
2009-02-18 22:33:54 +01:00
|
|
|
struct aes_cbc_context *aesctx = ctx;
|
2007-01-31 19:09:20 +01:00
|
|
|
AES_MODE mode;
|
|
|
|
|
|
|
|
switch ( keylen ) {
|
|
|
|
case ( 128 / 8 ):
|
|
|
|
mode = AES_MODE_128;
|
|
|
|
break;
|
|
|
|
case ( 256 / 8 ):
|
|
|
|
mode = AES_MODE_256;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-02-18 22:33:54 +01:00
|
|
|
AES_set_key ( &aesctx->ctx, key, aesctx->ctx.iv, mode );
|
|
|
|
|
|
|
|
aesctx->decrypting = 0;
|
|
|
|
|
2007-01-31 19:09:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-18 22:25:14 +01:00
|
|
|
static void aes_cbc_setiv ( void *ctx, const void *iv ) {
|
2009-02-18 22:33:54 +01:00
|
|
|
struct aes_cbc_context *aesctx = ctx;
|
2007-01-31 19:09:20 +01:00
|
|
|
|
2009-02-18 22:33:54 +01:00
|
|
|
memcpy ( aesctx->ctx.iv, iv, sizeof ( aesctx->ctx.iv ) );
|
2007-01-31 19:09:20 +01:00
|
|
|
}
|
|
|
|
|
2009-02-18 22:25:14 +01:00
|
|
|
static void aes_cbc_encrypt ( void *ctx, const void *data, void *dst,
|
|
|
|
size_t len ) {
|
2009-02-18 22:33:54 +01:00
|
|
|
struct aes_cbc_context *aesctx = ctx;
|
2007-01-31 19:09:20 +01:00
|
|
|
|
2009-02-18 22:33:54 +01:00
|
|
|
if ( aesctx->decrypting )
|
|
|
|
assert ( 0 );
|
|
|
|
|
|
|
|
AES_cbc_encrypt ( &aesctx->ctx, data, dst, len );
|
2007-01-31 19:09:20 +01:00
|
|
|
}
|
|
|
|
|
2009-02-18 22:25:14 +01:00
|
|
|
static void aes_cbc_decrypt ( void *ctx, const void *data, void *dst,
|
|
|
|
size_t len ) {
|
2009-02-18 22:33:54 +01:00
|
|
|
struct aes_cbc_context *aesctx = ctx;
|
|
|
|
|
|
|
|
if ( ! aesctx->decrypting ) {
|
|
|
|
AES_convert_key ( &aesctx->ctx );
|
|
|
|
aesctx->decrypting = 1;
|
|
|
|
}
|
2007-01-31 19:09:20 +01:00
|
|
|
|
2009-02-18 22:33:54 +01:00
|
|
|
AES_cbc_decrypt ( &aesctx->ctx, data, dst, len );
|
2007-01-31 19:09:20 +01:00
|
|
|
}
|
|
|
|
|
2009-02-18 22:56:02 +01:00
|
|
|
struct cipher_algorithm aes_cbc_algorithm = {
|
2009-02-18 22:25:14 +01:00
|
|
|
.name = "aes_cbc",
|
2009-02-18 22:33:54 +01:00
|
|
|
.ctxsize = sizeof ( struct aes_cbc_context ),
|
2007-01-31 19:09:20 +01:00
|
|
|
.blocksize = 16,
|
2009-02-18 22:25:14 +01:00
|
|
|
.setkey = aes_cbc_setkey,
|
|
|
|
.setiv = aes_cbc_setiv,
|
2009-02-18 22:56:02 +01:00
|
|
|
.encrypt = aes_cbc_encrypt,
|
|
|
|
.decrypt = aes_cbc_decrypt,
|
2007-01-31 19:09:20 +01:00
|
|
|
};
|