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 <errno.h>
30#include <ipxe/crypto.h>
31#include <ipxe/cbc.h>
32
33/** @file
34 *
35 * Cipher-block chaining
36 *
37 */
38
39/**
40 * Set key
41 *
42 * @v cipher Cipher algorithm
43 * @v ctx Context
44 * @v key Key
45 * @v keylen Key length
46 * @ret rc Return status code
47 */
48int cbc_setkey ( struct cipher_algorithm *cipher, void *ctx,
49 const void *key, size_t keylen ) {
50 struct cipher_algorithm *raw_cipher = cipher->priv;
51 size_t blocksize = cipher->blocksize;
52 size_t ctxsize = cipher->ctxsize;
53 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
54
55 return cipher_setkey ( raw_cipher, context->raw, key, keylen );
56}
57
58/**
59 * Set initialisation vector
60 *
61 * @v cipher Cipher algorithm
62 * @v ctx Context
63 * @v iv Initialisation vector
64 * @v ivlen Initialisation vector length
65 * @ret rc Return status code
66 */
67int cbc_setiv ( struct cipher_algorithm *cipher, void *ctx,
68 const void *iv, size_t ivlen ) {
69 size_t blocksize = cipher->blocksize;
70 size_t ctxsize = cipher->ctxsize;
71 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
72
73 /* Check length */
74 if ( ivlen != sizeof ( context->cbc ) )
75 return -ENOTSUP;
76
77 /* Record IV */
78 memcpy ( context->cbc, iv, sizeof ( context->cbc ) );
79
80 return 0;
81}
82
83/**
84 * XOR data blocks
85 *
86 * @v src Input data
87 * @v dst Second input data and output data buffer
88 * @v len Length of data
89 */
90static void cbc_xor ( const void *src, void *dst, size_t len ) {
91 const uint32_t *srcl = src;
92 uint32_t *dstl = dst;
93 unsigned int i;
94
95 /* Assume that block sizes will always be dword-aligned, for speed */
96 assert ( ( len % sizeof ( *srcl ) ) == 0 );
97
98 for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
99 dstl[i] ^= srcl[i];
100}
101
102/**
103 * Encrypt data
104 *
105 * @v cipher Cipher algorithm
106 * @v ctx Context
107 * @v src Data to encrypt
108 * @v dst Buffer for encrypted data
109 * @v len Length of data
110 */
111void cbc_encrypt ( struct cipher_algorithm *cipher, void *ctx,
112 const void *src, void *dst, size_t len ) {
113 struct cipher_algorithm *raw_cipher = cipher->priv;
114 size_t blocksize = cipher->blocksize;
115 size_t ctxsize = cipher->ctxsize;
116 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
117
118 assert ( ( len % blocksize ) == 0 );
119
120 while ( len ) {
121 cbc_xor ( src, context->cbc, blocksize );
122 cipher_encrypt ( raw_cipher, context->raw, context->cbc, dst,
123 blocksize );
124 memcpy ( context->cbc, dst, blocksize );
125 dst += blocksize;
126 src += blocksize;
127 len -= blocksize;
128 }
129}
130
131/**
132 * Decrypt data
133 *
134 * @v cipher Cipher algorithm
135 * @v ctx Context
136 * @v src Data to decrypt
137 * @v dst Buffer for decrypted data
138 * @v len Length of data
139 */
140void cbc_decrypt ( struct cipher_algorithm *cipher, void *ctx,
141 const void *src, void *dst, size_t len ) {
142 struct cipher_algorithm *raw_cipher = cipher->priv;
143 size_t blocksize = cipher->blocksize;
144 size_t ctxsize = cipher->ctxsize;
145 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
146 uint8_t next_cbc_ctx[blocksize];
147
148 assert ( ( len % blocksize ) == 0 );
149
150 while ( len ) {
151 memcpy ( next_cbc_ctx, src, blocksize );
152 cipher_decrypt ( raw_cipher, context->raw, src, dst,
153 blocksize );
154 cbc_xor ( context->cbc, dst, blocksize );
155 memcpy ( context->cbc, next_cbc_ctx, blocksize );
156 dst += blocksize;
157 src += blocksize;
158 len -= blocksize;
159 }
160}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
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:61
void cbc_encrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition cbc.c:111
int cbc_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Set key.
Definition cbc.c:48
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:90
void cbc_decrypt(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition cbc.c:140
int cbc_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Set initialisation vector.
Definition cbc.c:67
Cipher-block chaining.
#define cbc_context_t(blocksize, ctxsize)
A cipher-block chaining mode context.
Definition cbc.h:16
ring len
Length.
Definition dwmac.h:226
Error codes.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOTSUP
Operation not supported.
Definition errno.h:633
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
Cryptographic API.
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:336
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
A cipher algorithm.
Definition crypto.h:58
void * priv
Algorithm private data.
Definition crypto.h:138
size_t blocksize
Block size.
Definition crypto.h:68
size_t ctxsize
Context size.
Definition crypto.h:62
u8 iv[16]
Initialization vector.
Definition wpa.h:33