Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / axtls_aes.c
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <byteswap.h>
26 #include <ipxe/crypto.h>
27 #include <ipxe/cbc.h>
28 #include <ipxe/aes.h>
29 #include "crypto/axtls/crypto.h"
30
31 /** @file
32  *
33  * AES algorithm
34  *
35  */
36
37 /**
38  * Set key
39  *
40  * @v ctx               Context
41  * @v key               Key
42  * @v keylen            Key length
43  * @ret rc              Return status code
44  */
45 static int aes_setkey ( void *ctx, const void *key, size_t keylen ) {
46         struct aes_context *aes_ctx = ctx;
47         AES_MODE mode;
48         void *iv;
49
50         switch ( keylen ) {
51         case ( 128 / 8 ):
52                 mode = AES_MODE_128;
53                 break;
54         case ( 256 / 8 ):
55                 mode = AES_MODE_256;
56                 break;
57         default:
58                 return -EINVAL;
59         }
60
61         /* IV is not a relevant concept at this stage; use a dummy
62          * value that will have no side-effects.
63          */
64         iv = &aes_ctx->axtls_ctx.iv;
65
66         AES_set_key ( &aes_ctx->axtls_ctx, key, iv, mode );
67
68         aes_ctx->decrypting = 0;
69
70         return 0;
71 }
72
73 /**
74  * Set initialisation vector
75  *
76  * @v ctx               Context
77  * @v iv                Initialisation vector
78  */
79 static void aes_setiv ( void *ctx __unused, const void *iv __unused ) {
80         /* Nothing to do */
81 }
82
83 /**
84  * Call AXTLS' AES_encrypt() or AES_decrypt() functions
85  *
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
90  */
91 static void aes_call_axtls ( AES_CTX *axtls_ctx, const void *src, void *dst,
92                              void ( * func ) ( const AES_CTX *axtls_ctx,
93                                                uint32_t *data ) ){
94         const uint32_t *srcl = src;
95         uint32_t *dstl = dst;
96         unsigned int i;
97
98         /* AXTLS' AES_encrypt() and AES_decrypt() functions both
99          * expect to deal with an array of four dwords in host-endian
100          * order.
101          */
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] );
107 }
108
109 /**
110  * Encrypt data
111  *
112  * @v ctx               Context
113  * @v src               Data to encrypt
114  * @v dst               Buffer for encrypted data
115  * @v len               Length of data
116  */
117 static void aes_encrypt ( void *ctx, const void *src, void *dst,
118                           size_t len ) {
119         struct aes_context *aes_ctx = ctx;
120
121         assert ( len == AES_BLOCKSIZE );
122         if ( aes_ctx->decrypting )
123                 assert ( 0 );
124         aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_encrypt );
125 }
126
127 /**
128  * Decrypt data
129  *
130  * @v ctx               Context
131  * @v src               Data to decrypt
132  * @v dst               Buffer for decrypted data
133  * @v len               Length of data
134  */
135 static void aes_decrypt ( void *ctx, const void *src, void *dst,
136                           size_t len ) {
137         struct aes_context *aes_ctx = ctx;
138
139         assert ( len == AES_BLOCKSIZE );
140         if ( ! aes_ctx->decrypting ) {
141                 AES_convert_key ( &aes_ctx->axtls_ctx );
142                 aes_ctx->decrypting = 1;
143         }
144         aes_call_axtls ( &aes_ctx->axtls_ctx, src, dst, axtls_aes_decrypt );
145 }
146
147 /** Basic AES algorithm */
148 struct cipher_algorithm aes_algorithm = {
149         .name = "aes",
150         .ctxsize = sizeof ( struct aes_context ),
151         .blocksize = AES_BLOCKSIZE,
152         .setkey = aes_setkey,
153         .setiv = aes_setiv,
154         .encrypt = aes_encrypt,
155         .decrypt = aes_decrypt,
156 };
157
158 /* AES with cipher-block chaining */
159 CBC_CIPHER ( aes_cbc, aes_cbc_algorithm,
160              aes_algorithm, struct aes_context, AES_BLOCKSIZE );