iPXE
cbc.h File Reference

Cipher-block chaining. More...

#include <ipxe/crypto.h>

Go to the source code of this file.

Macros

#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)
static int cbc_setkey (void *ctx, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher, void *cbc_ctx __unused)
 Set key.
static void cbc_setiv (void *ctx __unused, const void *iv, size_t ivlen, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Set initialisation vector.
void cbc_encrypt (void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Encrypt data.
void cbc_decrypt (void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Decrypt data.

Detailed Description

Cipher-block chaining.

Definition in file cbc.h.

Macro Definition Documentation

◆ CBC_CIPHER

#define CBC_CIPHER ( _cbc_name,
_cbc_cipher,
_raw_cipher,
_raw_context,
_blocksize )

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 65 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 ( void * ctx,
const void * key,
size_t keylen,
struct cipher_algorithm * raw_cipher,
void *cbc_ctx __unused )
inlinestatic

Set key.

Parameters
ctxContext
keyKey
keylenKey length
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context
Return values
rcReturn status code

Definition at line 25 of file cbc.h.

27 {
28
29 return cipher_setkey ( raw_cipher, ctx, key, keylen );
30}
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eq_context ctx
Definition CIB_PRM.h:0
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:235

References __unused, cipher_setkey(), ctx, and key.

◆ cbc_setiv()

void cbc_setiv ( void *ctx __unused,
const void * iv,
size_t ivlen,
struct cipher_algorithm * raw_cipher,
void * cbc_ctx )
inlinestatic

Set initialisation vector.

Parameters
ctxContext
ivInitialisation vector
ivlenInitialisation vector length
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context

Definition at line 41 of file cbc.h.

44 {
45 assert ( ivlen == raw_cipher->blocksize );
46 memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
47}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t blocksize
Block size.
Definition crypto.h:61
u8 iv[16]
Initialization vector.
Definition wpa.h:33

References __unused, assert, cipher_algorithm::blocksize, ctx, iv, and memcpy().

◆ cbc_encrypt()

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

Encrypt data.

Parameters
ctxContext
srcData to encrypt
dstBuffer for encrypted data
lenLength of data
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context

Definition at line 67 of file cbc.c.

68 {
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}
static const void * src
Definition string.h:48
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:45
ring len
Length.
Definition dwmac.h:226
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:251

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

◆ cbc_decrypt()

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

Decrypt data.

Parameters
ctxContext
srcData to decrypt
dstBuffer for decrypted data
lenLength of data
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context

Definition at line 93 of file cbc.c.

94 {
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}
unsigned char uint8_t
Definition stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:261

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