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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
24 #include <gpxe/crypto.h>
27 #include "crypto/axtls/crypto.h"
35 /** Basic AES blocksize */
36 #define AES_BLOCKSIZE 16
40 /** AES context for AXTLS */
42 /** Cipher is being used for decrypting */
51 * @v keylen Key length
52 * @ret rc Return status code
54 static int aes_setkey ( void *ctx
, const void *key
, size_t keylen
) {
55 struct aes_context
*aes_ctx
= ctx
;
70 /* IV is not a relevant concept at this stage; use a dummy
71 * value that will have no side-effects.
73 iv
= &aes_ctx
->axtls_ctx
.iv
;
75 AES_set_key ( &aes_ctx
->axtls_ctx
, key
, iv
, mode
);
77 aes_ctx
->decrypting
= 0;
83 * Set initialisation vector
86 * @v iv Initialisation vector
88 static void aes_setiv ( void *ctx __unused
, const void *iv __unused
) {
93 * Call AXTLS' AES_encrypt() or AES_decrypt() functions
95 * @v axtls_ctx AXTLS AES context
96 * @v src Data to process
97 * @v dst Buffer for output
98 * @v func AXTLS AES function to call
100 static void aes_call_axtls ( AES_CTX
*axtls_ctx
, const void *src
, void *dst
,
101 void ( * func
) ( const AES_CTX
*axtls_ctx
,
103 const uint32_t *srcl
= src
;
104 uint32_t *dstl
= dst
;
107 /* AXTLS' AES_encrypt() and AES_decrypt() functions both
108 * expect to deal with an array of four dwords in host-endian
111 for ( i
= 0 ; i
< 4 ; i
++ )
112 dstl
[i
] = ntohl ( srcl
[i
] );
113 func ( axtls_ctx
, dstl
);
114 for ( i
= 0 ; i
< 4 ; i
++ )
115 dstl
[i
] = htonl ( dstl
[i
] );
122 * @v src Data to encrypt
123 * @v dst Buffer for encrypted data
124 * @v len Length of data
126 static void aes_encrypt ( void *ctx
, const void *src
, void *dst
,
128 struct aes_context
*aes_ctx
= ctx
;
130 assert ( len
== AES_BLOCKSIZE
);
131 if ( aes_ctx
->decrypting
)
133 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_encrypt
);
140 * @v src Data to decrypt
141 * @v dst Buffer for decrypted data
142 * @v len Length of data
144 static void aes_decrypt ( void *ctx
, const void *src
, void *dst
,
146 struct aes_context
*aes_ctx
= ctx
;
148 assert ( len
== AES_BLOCKSIZE
);
149 if ( ! aes_ctx
->decrypting
) {
150 AES_convert_key ( &aes_ctx
->axtls_ctx
);
151 aes_ctx
->decrypting
= 1;
153 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_decrypt
);
156 /** Basic AES algorithm */
157 static struct cipher_algorithm aes_algorithm
= {
159 .ctxsize
= sizeof ( struct aes_context
),
160 .blocksize
= AES_BLOCKSIZE
,
161 .setkey
= aes_setkey
,
163 .encrypt
= aes_encrypt
,
164 .decrypt
= aes_decrypt
,
167 /* AES with cipher-block chaining */
168 CBC_CIPHER ( aes_cbc
, aes_cbc_algorithm
,
169 aes_algorithm
, struct aes_context
, AES_BLOCKSIZE
);