[3c90x] Remove src/drivers/3c90x.txt
[gpxe.git] / src / crypto / axtls_aes.c
blob51e1924ed0ab2259668a0e3c2034a123f3f17a2e
1 /*
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 #include <string.h>
20 #include <errno.h>
21 #include <byteswap.h>
22 #include <gpxe/crypto.h>
23 #include <gpxe/cbc.h>
24 #include <gpxe/aes.h>
25 #include "crypto/axtls/crypto.h"
27 /** @file
29 * AES algorithm
33 /** Basic AES blocksize */
34 #define AES_BLOCKSIZE 16
36 /** AES context */
37 struct aes_context {
38 /** AES context for AXTLS */
39 AES_CTX axtls_ctx;
40 /** Cipher is being used for decrypting */
41 int decrypting;
44 /**
45 * Set key
47 * @v ctx Context
48 * @v key Key
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;
54 AES_MODE mode;
55 void *iv;
57 switch ( keylen ) {
58 case ( 128 / 8 ):
59 mode = AES_MODE_128;
60 break;
61 case ( 256 / 8 ):
62 mode = AES_MODE_256;
63 break;
64 default:
65 return -EINVAL;
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;
77 return 0;
80 /**
81 * Set initialisation vector
83 * @v ctx Context
84 * @v iv Initialisation vector
86 static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
87 /* Nothing to do */
90 /**
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,
100 uint32_t *data ) ){
101 const uint32_t *srcl = src;
102 uint32_t *dstl = dst;
103 unsigned int i;
105 /* AXTLS' AES_encrypt() and AES_decrypt() functions both
106 * expect to deal with an array of four dwords in host-endian
107 * order.
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] );
117 * Encrypt data
119 * @v ctx Context
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,
125 size_t len ) {
126 struct aes_context *aes_ctx = ctx;
128 assert ( len == AES_BLOCKSIZE );
129 if ( aes_ctx->decrypting )
130 assert ( 0 );
131 aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, AES_encrypt );
135 * Decrypt data
137 * @v ctx Context
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,
143 size_t len ) {
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 = {
156 .name = "aes",
157 .ctxsize = sizeof ( struct aes_context ),
158 .blocksize = AES_BLOCKSIZE,
159 .setkey = aes_setkey,
160 .setiv = aes_setiv,
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 );