[sundance] Add reset completion check
[gpxe.git] / src / crypto / cbc.c
blobc7116ea9552449d491b99abf52ab96284567ad58
1 /*
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 #include <string.h>
20 #include <assert.h>
21 #include <gpxe/crypto.h>
22 #include <gpxe/cbc.h>
24 /** @file
26 * Cipher-block chaining
30 /**
31 * XOR data blocks
33 * @v src Input data
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;
39 uint32_t *dstl = dst;
40 unsigned int i;
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++ )
46 dstl[i] ^= srcl[i];
49 /**
50 * Encrypt data
52 * @v ctx Context
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 );
65 while ( len ) {
66 cbc_xor ( src, cbc_ctx, blocksize );
67 cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
68 memcpy ( cbc_ctx, dst, blocksize );
69 dst += blocksize;
70 src += blocksize;
71 len -= blocksize;
75 /**
76 * Decrypt data
78 * @v ctx Context
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 );
91 while ( len ) {
92 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
93 cbc_xor ( cbc_ctx, dst, blocksize );
94 memcpy ( cbc_ctx, src, blocksize );
95 dst += blocksize;
96 src += blocksize;
97 len -= blocksize;