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.
21 #include <gpxe/crypto.h>
26 * Cipher-block chaining
34 * @v dst Second input data and output data buffer
35 * @v len Length of data
37 static void cbc_xor ( const void *src
, void *dst
, size_t len
) {
38 const uint32_t *srcl
= src
;
42 /* Assume that block sizes will always be dword-aligned, for speed */
43 assert ( ( len
% sizeof ( *srcl
) ) == 0 );
45 for ( i
= 0 ; i
< ( len
/ sizeof ( *srcl
) ) ; i
++ )
53 * @v src Data to encrypt
54 * @v dst Buffer for encrypted data
55 * @v len Length of data
56 * @v raw_cipher Underlying cipher algorithm
57 * @v cbc_ctx CBC context
59 void cbc_encrypt ( void *ctx
, const void *src
, void *dst
, size_t len
,
60 struct cipher_algorithm
*raw_cipher
, void *cbc_ctx
) {
61 size_t blocksize
= raw_cipher
->blocksize
;
63 assert ( ( len
% blocksize
) == 0 );
66 cbc_xor ( src
, cbc_ctx
, blocksize
);
67 cipher_encrypt ( raw_cipher
, ctx
, cbc_ctx
, dst
, blocksize
);
68 memcpy ( cbc_ctx
, dst
, blocksize
);
79 * @v src Data to decrypt
80 * @v dst Buffer for decrypted data
81 * @v len Length of data
82 * @v raw_cipher Underlying cipher algorithm
83 * @v cbc_ctx CBC context
85 void cbc_decrypt ( void *ctx
, const void *src
, void *dst
, size_t len
,
86 struct cipher_algorithm
*raw_cipher
, void *cbc_ctx
) {
87 size_t blocksize
= raw_cipher
->blocksize
;
89 assert ( ( len
% blocksize
) == 0 );
92 cipher_decrypt ( raw_cipher
, ctx
, src
, dst
, blocksize
);
93 cbc_xor ( cbc_ctx
, dst
, blocksize
);
94 memcpy ( cbc_ctx
, src
, blocksize
);