iPXE
cbc.h File Reference

Cipher-block chaining. More...

#include <ipxe/crypto.h>

Go to the source code of this file.

Macros

#define cbc_context_t(blocksize, ctxsize)
 A cipher-block chaining mode context.
#define CBC_CIPHER(_cbc_name, _cbc_cipher, _raw_cipher, _raw_context, _blocksize)
 Create a cipher-block chaining mode of behaviour of an existing cipher.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int cbc_setkey (struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
 Set key.
int cbc_setiv (struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
 Set initialisation vector.
void cbc_encrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 Encrypt data.
void cbc_decrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 Decrypt data.

Detailed Description

Cipher-block chaining.

Definition in file cbc.h.

Macro Definition Documentation

◆ cbc_context_t

#define cbc_context_t ( blocksize,
ctxsize )
Value:
struct { \
uint8_t cbc[ (blocksize) ]; \
uint8_t raw[ (ctxsize) - (blocksize) ]; \
}
__be32 raw[7]
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10

A cipher-block chaining mode context.

Definition at line 16 of file cbc.h.

16#define cbc_context_t( blocksize, ctxsize ) \
17 struct { \
18 uint8_t cbc[ (blocksize) ]; \
19 uint8_t raw[ (ctxsize) - (blocksize) ]; \
20 }

Referenced by cbc_decrypt(), cbc_encrypt(), cbc_setiv(), and cbc_setkey().

◆ CBC_CIPHER

#define CBC_CIPHER ( _cbc_name,
_cbc_cipher,
_raw_cipher,
_raw_context,
_blocksize )
Value:
struct cipher_algorithm _cbc_cipher = { \
.name = #_cbc_name, \
.ctxsize = ( _blocksize + sizeof ( _raw_context ) ), \
.blocksize = _blocksize, \
.alignsize = _blocksize, \
.authsize = 0, \
.confidential = 1, \
.setkey = cbc_setkey, \
.setiv = cbc_setiv, \
.encrypt = cbc_encrypt, \
.decrypt = cbc_decrypt, \
.auth = cipher_null_auth, \
.priv = &_raw_cipher, \
};
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
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
void cipher_null_auth(struct cipher_algorithm *cipher __unused, void *ctx __unused, void *auth __unused)
Definition crypto_null.c:89
A cipher algorithm.
Definition crypto.h:58
static struct tlan_private * priv
Definition tlan.c:225

Create a cipher-block chaining mode of behaviour of an existing cipher.

Parameters
_cbc_nameName for the new CBC cipher
_cbc_cipherNew cipher algorithm
_raw_cipherUnderlying cipher algorithm
_raw_contextContext structure for the underlying cipher
_blocksizeCipher block size

Definition at line 40 of file cbc.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ cbc_setkey()

int cbc_setkey ( struct cipher_algorithm * cipher,
void * ctx,
const void * key,
size_t keylen )
extern

Set key.

Parameters
cipherCipher algorithm
ctxContext
keyKey
keylenKey length
Return values
rcReturn status code

Definition at line 48 of file cbc.c.

49 {
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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
#define cbc_context_t(blocksize, ctxsize)
A cipher-block chaining mode context.
Definition cbc.h:16
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
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

References cipher_algorithm::blocksize, cbc_context_t, cipher_setkey(), ctx, cipher_algorithm::ctxsize, key, and cipher_algorithm::priv.

◆ cbc_setiv()

int cbc_setiv ( struct cipher_algorithm * cipher,
void * ctx,
const void * iv,
size_t ivlen )
extern

Set initialisation vector.

Parameters
cipherCipher algorithm
ctxContext
ivInitialisation vector
ivlenInitialisation vector length
Return values
rcReturn status code

Definition at line 67 of file cbc.c.

68 {
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}
#define ENOTSUP
Operation not supported.
Definition errno.h:590
void * memcpy(void *dest, const void *src, size_t len) __nonnull
u8 iv[16]
Initialization vector.
Definition wpa.h:33

References cipher_algorithm::blocksize, cbc_context_t, ctx, cipher_algorithm::ctxsize, ENOTSUP, iv, and memcpy().

◆ cbc_encrypt()

void cbc_encrypt ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )
extern

Encrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to encrypt
dstBuffer for encrypted data
lenLength of data

Definition at line 111 of file cbc.c.

112 {
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}
static const void * src
Definition string.h:48
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:90
ring len
Length.
Definition dwmac.h:226
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326

References assert, cipher_algorithm::blocksize, cbc_context_t, cbc_xor(), cipher_encrypt, ctx, cipher_algorithm::ctxsize, len, memcpy(), cipher_algorithm::priv, and src.

◆ cbc_decrypt()

void cbc_decrypt ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )
extern

Decrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to decrypt
dstBuffer for decrypted data
lenLength of data

Definition at line 140 of file cbc.c.

141 {
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}
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:336

References assert, cipher_algorithm::blocksize, cbc_context_t, cbc_xor(), cipher_decrypt, ctx, cipher_algorithm::ctxsize, len, memcpy(), cipher_algorithm::priv, and src.