[fdt] Add ability to parse a MAC address from a flattened device tree
[ipxe.git] / src / crypto / cbc.c
1 /*
2 * Copyright (C) 2009 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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <assert.h>
28 #include <ipxe/crypto.h>
29 #include <ipxe/cbc.h>
30
31 /** @file
32 *
33 * Cipher-block chaining
34 *
35 */
36
37 /**
38 * XOR data blocks
39 *
40 * @v src Input data
41 * @v dst Second input data and output data buffer
42 * @v len Length of data
43 */
44 static void cbc_xor ( const void *src, void *dst, size_t len ) {
45 const uint32_t *srcl = src;
46 uint32_t *dstl = dst;
47 unsigned int i;
48
49 /* Assume that block sizes will always be dword-aligned, for speed */
50 assert ( ( len % sizeof ( *srcl ) ) == 0 );
51
52 for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
53 dstl[i] ^= srcl[i];
54 }
55
56 /**
57 * Encrypt data
58 *
59 * @v ctx Context
60 * @v src Data to encrypt
61 * @v dst Buffer for encrypted data
62 * @v len Length of data
63 * @v raw_cipher Underlying cipher algorithm
64 * @v cbc_ctx CBC context
65 */
66 void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
67 struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
68 size_t blocksize = raw_cipher->blocksize;
69
70 assert ( ( len % blocksize ) == 0 );
71
72 while ( len ) {
73 cbc_xor ( src, cbc_ctx, blocksize );
74 cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
75 memcpy ( cbc_ctx, dst, blocksize );
76 dst += blocksize;
77 src += blocksize;
78 len -= blocksize;
79 }
80 }
81
82 /**
83 * Decrypt data
84 *
85 * @v ctx Context
86 * @v src Data to decrypt
87 * @v dst Buffer for decrypted data
88 * @v len Length of data
89 * @v raw_cipher Underlying cipher algorithm
90 * @v cbc_ctx CBC context
91 */
92 void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
93 struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
94 size_t blocksize = raw_cipher->blocksize;
95 uint8_t next_cbc_ctx[blocksize];
96
97 assert ( ( len % blocksize ) == 0 );
98
99 while ( len ) {
100 memcpy ( next_cbc_ctx, src, blocksize );
101 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
102 cbc_xor ( cbc_ctx, dst, blocksize );
103 memcpy ( cbc_ctx, next_cbc_ctx, blocksize );
104 dst += blocksize;
105 src += blocksize;
106 len -= blocksize;
107 }
108 }