123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <ipxe/crypto.h>
- #include <ipxe/cbc.h>
- #include <ipxe/aes.h>
- #include "crypto/axtls/crypto.h"
-
-
-
-
- static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
- struct aes_context *aes_ctx = ctx;
- AES_MODE mode;
- void *iv;
-
- switch ( keylen ) {
- case ( 128 / 8 ):
- mode = AES_MODE_128;
- break;
- case ( 256 / 8 ):
- mode = AES_MODE_256;
- break;
- default:
- return -EINVAL;
- }
-
-
-
- iv = &aes_ctx->axtls_ctx.iv;
-
- AES_set_key ( &aes_ctx->axtls_ctx, key, iv, mode );
-
- aes_ctx->decrypting = 0;
-
- return 0;
- }
-
-
- static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
-
- }
-
-
- static void aes_call_axtls ( AES_CTX *axtls_ctx, const void *src, void *dst,
- void ( * func ) ( const AES_CTX *axtls_ctx,
- uint32_t *data ) ){
- const uint32_t *srcl = src;
- uint32_t *dstl = dst;
- unsigned int i;
-
-
-
- for ( i = 0 ; i < 4 ; i++ )
- dstl[i] = ntohl ( srcl[i] );
- func ( axtls_ctx, dstl );
- for ( i = 0 ; i < 4 ; i++ )
- dstl[i] = htonl ( dstl[i] );
- }
-
-
- static void aes_encrypt ( void *ctx, const void *src, void *dst,
- size_t len ) {
- struct aes_context *aes_ctx = ctx;
-
- assert ( len == AES_BLOCKSIZE );
- if ( aes_ctx->decrypting )
- assert ( 0 );
- aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_encrypt );
- }
-
-
- static void aes_decrypt ( void *ctx, const void *src, void *dst,
- size_t len ) {
- struct aes_context *aes_ctx = ctx;
-
- assert ( len == AES_BLOCKSIZE );
- if ( ! aes_ctx->decrypting ) {
- AES_convert_key ( &aes_ctx->axtls_ctx );
- aes_ctx->decrypting = 1;
- }
- aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_decrypt );
- }
-
-
- struct cipher_algorithm aes_algorithm = {
- .name = "aes",
- .ctxsize = sizeof ( struct aes_context ),
- .blocksize = AES_BLOCKSIZE,
- .setkey = aes_setkey,
- .setiv = aes_setiv,
- .encrypt = aes_encrypt,
- .decrypt = aes_decrypt,
- };
-
-
- CBC_CIPHER ( aes_cbc, aes_cbc_algorithm,
- aes_algorithm, struct aes_context, AES_BLOCKSIZE );
|