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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_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 */
45static 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 */
67void 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 */
93void 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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
unsigned int uint32_t
Definition stdint.h:12
unsigned char uint8_t
Definition stdint.h:10
static const void * src
Definition string.h:48
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
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
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:45
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
Cipher-block chaining.
ring len
Length.
Definition dwmac.h:226
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Cryptographic API.
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:261
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:251
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
A cipher algorithm.
Definition crypto.h:51
size_t blocksize
Block size.
Definition crypto.h:61