2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/crypto.h>
29 #include "crypto/axtls/crypto.h"
42 * @v keylen Key length
43 * @ret rc Return status code
45 static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
46 struct aes_context *aes_ctx = ctx;
61 /* IV is not a relevant concept at this stage; use a dummy
62 * value that will have no side-effects.
64 iv = &aes_ctx->axtls_ctx.iv;
66 AES_set_key ( &aes_ctx->axtls_ctx, key, iv, mode );
68 aes_ctx->decrypting = 0;
74 * Set initialisation vector
77 * @v iv Initialisation vector
79 static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
84 * Call AXTLS' AES_encrypt() or AES_decrypt() functions
86 * @v axtls_ctx AXTLS AES context
87 * @v src Data to process
88 * @v dst Buffer for output
89 * @v func AXTLS AES function to call
91 static void aes_call_axtls ( AES_CTX *axtls_ctx, const void *src, void *dst,
92 void ( * func ) ( const AES_CTX *axtls_ctx,
94 const uint32_t *srcl = src;
98 /* AXTLS' AES_encrypt() and AES_decrypt() functions both
99 * expect to deal with an array of four dwords in host-endian
102 for ( i = 0 ; i < 4 ; i++ )
103 dstl[i] = ntohl ( srcl[i] );
104 func ( axtls_ctx, dstl );
105 for ( i = 0 ; i < 4 ; i++ )
106 dstl[i] = htonl ( dstl[i] );
113 * @v src Data to encrypt
114 * @v dst Buffer for encrypted data
115 * @v len Length of data
117 static void aes_encrypt ( void *ctx, const void *src, void *dst,
119 struct aes_context *aes_ctx = ctx;
121 assert ( len == AES_BLOCKSIZE );
122 if ( aes_ctx->decrypting )
124 aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_encrypt );
131 * @v src Data to decrypt
132 * @v dst Buffer for decrypted data
133 * @v len Length of data
135 static void aes_decrypt ( void *ctx, const void *src, void *dst,
137 struct aes_context *aes_ctx = ctx;
139 assert ( len == AES_BLOCKSIZE );
140 if ( ! aes_ctx->decrypting ) {
141 AES_convert_key ( &aes_ctx->axtls_ctx );
142 aes_ctx->decrypting = 1;
144 aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_decrypt );
147 /** Basic AES algorithm */
148 struct cipher_algorithm aes_algorithm = {
150 .ctxsize = sizeof ( struct aes_context ),
151 .blocksize = AES_BLOCKSIZE,
152 .setkey = aes_setkey,
154 .encrypt = aes_encrypt,
155 .decrypt = aes_decrypt,
158 /* AES with cipher-block chaining */
159 CBC_CIPHER ( aes_cbc, aes_cbc_algorithm,
160 aes_algorithm, struct aes_context, AES_BLOCKSIZE );