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.
22 #include <gpxe/crypto.h>
25 #include "crypto/axtls/crypto.h"
33 /** Basic AES blocksize */
34 #define AES_BLOCKSIZE 16
38 /** AES context for AXTLS */
40 /** Cipher is being used for decrypting */
49 * @v keylen Key length
50 * @ret rc Return status code
52 static int aes_setkey ( void *ctx
, const void *key
, size_t keylen
) {
53 struct aes_context
*aes_ctx
= ctx
;
68 /* IV is not a relevant concept at this stage; use a dummy
69 * value that will have no side-effects.
71 iv
= &aes_ctx
->axtls_ctx
.iv
;
73 AES_set_key ( &aes_ctx
->axtls_ctx
, key
, iv
, mode
);
75 aes_ctx
->decrypting
= 0;
81 * Set initialisation vector
84 * @v iv Initialisation vector
86 static void aes_setiv ( void *ctx __unused
, const void *iv __unused
) {
91 * Call AXTLS' AES_encrypt() or AES_decrypt() functions
93 * @v axtls_ctx AXTLS AES context
94 * @v src Data to process
95 * @v dst Buffer for output
96 * @v func AXTLS AES function to call
98 static void aes_call_axtls ( AES_CTX
*axtls_ctx
, const void *src
, void *dst
,
99 void ( * func
) ( const AES_CTX
*axtls_ctx
,
101 const uint32_t *srcl
= src
;
102 uint32_t *dstl
= dst
;
105 /* AXTLS' AES_encrypt() and AES_decrypt() functions both
106 * expect to deal with an array of four dwords in host-endian
109 for ( i
= 0 ; i
< 4 ; i
++ )
110 dstl
[i
] = ntohl ( srcl
[i
] );
111 func ( axtls_ctx
, dstl
);
112 for ( i
= 0 ; i
< 4 ; i
++ )
113 dstl
[i
] = htonl ( dstl
[i
] );
120 * @v src Data to encrypt
121 * @v dst Buffer for encrypted data
122 * @v len Length of data
124 static void aes_encrypt ( void *ctx
, const void *src
, void *dst
,
126 struct aes_context
*aes_ctx
= ctx
;
128 assert ( len
== AES_BLOCKSIZE
);
129 if ( aes_ctx
->decrypting
)
131 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_encrypt
);
138 * @v src Data to decrypt
139 * @v dst Buffer for decrypted data
140 * @v len Length of data
142 static void aes_decrypt ( void *ctx
, const void *src
, void *dst
,
144 struct aes_context
*aes_ctx
= ctx
;
146 assert ( len
== AES_BLOCKSIZE
);
147 if ( ! aes_ctx
->decrypting
) {
148 AES_convert_key ( &aes_ctx
->axtls_ctx
);
149 aes_ctx
->decrypting
= 1;
151 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_decrypt
);
154 /** Basic AES algorithm */
155 static struct cipher_algorithm aes_algorithm
= {
157 .ctxsize
= sizeof ( struct aes_context
),
158 .blocksize
= AES_BLOCKSIZE
,
159 .setkey
= aes_setkey
,
161 .encrypt
= aes_encrypt
,
162 .decrypt
= aes_decrypt
,
165 /* AES with cipher-block chaining */
166 CBC_CIPHER ( aes_cbc
, aes_cbc_algorithm
,
167 aes_algorithm
, struct aes_context
, AES_BLOCKSIZE
);