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"
40 * @v keylen Key length
41 * @ret rc Return status code
43 static int aes_setkey ( void *ctx
, const void *key
, size_t keylen
) {
44 struct aes_context
*aes_ctx
= ctx
;
59 /* IV is not a relevant concept at this stage; use a dummy
60 * value that will have no side-effects.
62 iv
= &aes_ctx
->axtls_ctx
.iv
;
64 AES_set_key ( &aes_ctx
->axtls_ctx
, key
, iv
, mode
);
66 aes_ctx
->decrypting
= 0;
72 * Set initialisation vector
75 * @v iv Initialisation vector
77 static void aes_setiv ( void *ctx __unused
, const void *iv __unused
) {
82 * Call AXTLS' AES_encrypt() or AES_decrypt() functions
84 * @v axtls_ctx AXTLS AES context
85 * @v src Data to process
86 * @v dst Buffer for output
87 * @v func AXTLS AES function to call
89 static void aes_call_axtls ( AES_CTX
*axtls_ctx
, const void *src
, void *dst
,
90 void ( * func
) ( const AES_CTX
*axtls_ctx
,
92 const uint32_t *srcl
= src
;
96 /* AXTLS' AES_encrypt() and AES_decrypt() functions both
97 * expect to deal with an array of four dwords in host-endian
100 for ( i
= 0 ; i
< 4 ; i
++ )
101 dstl
[i
] = ntohl ( srcl
[i
] );
102 func ( axtls_ctx
, dstl
);
103 for ( i
= 0 ; i
< 4 ; i
++ )
104 dstl
[i
] = htonl ( dstl
[i
] );
111 * @v src Data to encrypt
112 * @v dst Buffer for encrypted data
113 * @v len Length of data
115 static void aes_encrypt ( void *ctx
, const void *src
, void *dst
,
117 struct aes_context
*aes_ctx
= ctx
;
119 assert ( len
== AES_BLOCKSIZE
);
120 if ( aes_ctx
->decrypting
)
122 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_encrypt
);
129 * @v src Data to decrypt
130 * @v dst Buffer for decrypted data
131 * @v len Length of data
133 static void aes_decrypt ( void *ctx
, const void *src
, void *dst
,
135 struct aes_context
*aes_ctx
= ctx
;
137 assert ( len
== AES_BLOCKSIZE
);
138 if ( ! aes_ctx
->decrypting
) {
139 AES_convert_key ( &aes_ctx
->axtls_ctx
);
140 aes_ctx
->decrypting
= 1;
142 aes_call_axtls ( &aes_ctx
->axtls_ctx
, src
, dst
, AES_decrypt
);
145 /** Basic AES algorithm */
146 struct cipher_algorithm aes_algorithm
= {
148 .ctxsize
= sizeof ( struct aes_context
),
149 .blocksize
= AES_BLOCKSIZE
,
150 .setkey
= aes_setkey
,
152 .encrypt
= aes_encrypt
,
153 .decrypt
= aes_decrypt
,
156 /* AES with cipher-block chaining */
157 CBC_CIPHER ( aes_cbc
, aes_cbc_algorithm
,
158 aes_algorithm
, struct aes_context
, AES_BLOCKSIZE
);