[tcp] Enable AF_INET6 transport for tcp connections
[gpxe.git] / src / crypto / cbc.c
blob1710203cac13e27f76aade6b3e93fc0fd8625503
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 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <string.h>
22 #include <assert.h>
23 #include <gpxe/crypto.h>
24 #include <gpxe/cbc.h>
26 /** @file
28 * Cipher-block chaining
32 /**
33 * XOR data blocks
35 * @v src Input data
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;
41 uint32_t *dstl = dst;
42 unsigned int i;
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++ )
48 dstl[i] ^= srcl[i];
51 /**
52 * Encrypt data
54 * @v ctx Context
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 );
67 while ( len ) {
68 cbc_xor ( src, cbc_ctx, blocksize );
69 cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
70 memcpy ( cbc_ctx, dst, blocksize );
71 dst += blocksize;
72 src += blocksize;
73 len -= blocksize;
77 /**
78 * Decrypt data
80 * @v ctx Context
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 );
93 while ( len ) {
94 cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
95 cbc_xor ( cbc_ctx, dst, blocksize );
96 memcpy ( cbc_ctx, src, blocksize );
97 dst += blocksize;
98 src += blocksize;
99 len -= blocksize;