2 * Copyright (C) 2009 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
);
23 #include <gpxe/crypto.h>
28 * Cipher-block chaining
36 * @v dst Second input data and output data buffer
37 * @v len Length of data
39 static void cbc_xor ( const void *src
, void *dst
, size_t len
) {
40 const uint32_t *srcl
= src
;
44 /* Assume that block sizes will always be dword-aligned, for speed */
45 assert ( ( len
% sizeof ( *srcl
) ) == 0 );
47 for ( i
= 0 ; i
< ( len
/ sizeof ( *srcl
) ) ; i
++ )
55 * @v src Data to encrypt
56 * @v dst Buffer for encrypted data
57 * @v len Length of data
58 * @v raw_cipher Underlying cipher algorithm
59 * @v cbc_ctx CBC context
61 void cbc_encrypt ( void *ctx
, const void *src
, void *dst
, size_t len
,
62 struct cipher_algorithm
*raw_cipher
, void *cbc_ctx
) {
63 size_t blocksize
= raw_cipher
->blocksize
;
65 assert ( ( len
% blocksize
) == 0 );
68 cbc_xor ( src
, cbc_ctx
, blocksize
);
69 cipher_encrypt ( raw_cipher
, ctx
, cbc_ctx
, dst
, blocksize
);
70 memcpy ( cbc_ctx
, dst
, blocksize
);
81 * @v src Data to decrypt
82 * @v dst Buffer for decrypted data
83 * @v len Length of data
84 * @v raw_cipher Underlying cipher algorithm
85 * @v cbc_ctx CBC context
87 void cbc_decrypt ( void *ctx
, const void *src
, void *dst
, size_t len
,
88 struct cipher_algorithm
*raw_cipher
, void *cbc_ctx
) {
89 size_t blocksize
= raw_cipher
->blocksize
;
91 assert ( ( len
% blocksize
) == 0 );
94 cipher_decrypt ( raw_cipher
, ctx
, src
, dst
, blocksize
);
95 cbc_xor ( cbc_ctx
, dst
, blocksize
);
96 memcpy ( cbc_ctx
, src
, blocksize
);