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., 51 Franklin Street, Fifth Floor, Boston, MA
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.
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28
#include <ipxe/crypto.h>
33
* Cipher-block chaining
41
* @v dst Second input data and output data buffer
42
* @v len Length of data
44
static void cbc_xor ( const void *src, void *dst, size_t len ) {
45
const uint32_t *srcl = src;
49
/* Assume that block sizes will always be dword-aligned, for speed */
50
assert ( ( len % sizeof ( *srcl ) ) == 0 );
52
for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
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
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;
70
assert ( ( len % blocksize ) == 0 );
73
cbc_xor ( src, cbc_ctx, blocksize );
74
cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
75
memcpy ( cbc_ctx, dst, blocksize );
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
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];
97
assert ( ( len % blocksize ) == 0 );
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 );