iPXE
cbc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <string.h>
28 #include <assert.h>
29 #include <ipxe/crypto.h>
30 #include <ipxe/cbc.h>
31 
32 /** @file
33  *
34  * Cipher-block chaining
35  *
36  */
37 
38 /**
39  * XOR data blocks
40  *
41  * @v src Input data
42  * @v dst Second input data and output data buffer
43  * @v len Length of data
44  */
45 static void cbc_xor ( const void *src, void *dst, size_t len ) {
46  const uint32_t *srcl = src;
47  uint32_t *dstl = dst;
48  unsigned int i;
49 
50  /* Assume that block sizes will always be dword-aligned, for speed */
51  assert ( ( len % sizeof ( *srcl ) ) == 0 );
52 
53  for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
54  dstl[i] ^= srcl[i];
55 }
56 
57 /**
58  * Encrypt data
59  *
60  * @v ctx Context
61  * @v src Data to encrypt
62  * @v dst Buffer for encrypted data
63  * @v len Length of data
64  * @v raw_cipher Underlying cipher algorithm
65  * @v cbc_ctx CBC context
66  */
67 void cbc_encrypt ( void *ctx, const void *src, void *dst, size_t len,
68  struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
69  size_t blocksize = raw_cipher->blocksize;
70 
71  assert ( ( len % blocksize ) == 0 );
72 
73  while ( len ) {
74  cbc_xor ( src, cbc_ctx, blocksize );
75  cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
76  memcpy ( cbc_ctx, dst, blocksize );
77  dst += blocksize;
78  src += blocksize;
79  len -= blocksize;
80  }
81 }
82 
83 /**
84  * Decrypt data
85  *
86  * @v ctx Context
87  * @v src Data to decrypt
88  * @v dst Buffer for decrypted data
89  * @v len Length of data
90  * @v raw_cipher Underlying cipher algorithm
91  * @v cbc_ctx CBC context
92  */
93 void cbc_decrypt ( void *ctx, const void *src, void *dst, size_t len,
94  struct cipher_algorithm *raw_cipher, void *cbc_ctx ) {
95  size_t blocksize = raw_cipher->blocksize;
96  uint8_t next_cbc_ctx[blocksize];
97 
98  assert ( ( len % blocksize ) == 0 );
99 
100  while ( len ) {
101  memcpy ( next_cbc_ctx, src, blocksize );
102  cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
103  cbc_xor ( cbc_ctx, dst, blocksize );
104  memcpy ( cbc_ctx, next_cbc_ctx, blocksize );
105  dst += blocksize;
106  src += blocksize;
107  len -= blocksize;
108  }
109 }
size_t blocksize
Block size.
Definition: crypto.h:61
FILE_SECBOOT(PERMITTED)
Cryptographic API.
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
void cbc_decrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Decrypt data.
Definition: cbc.c:93
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition: cbc.c:45
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:251
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static const void * src
Definition: string.h:48
ring len
Length.
Definition: dwmac.h:231
unsigned char uint8_t
Definition: stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:261
Cipher-block chaining.
unsigned int uint32_t
Definition: stdint.h:12
void cbc_encrypt(void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
Encrypt data.
Definition: cbc.c:67
A cipher algorithm.
Definition: crypto.h:51
String functions.