iPXE
Macros | Functions
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. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int cbc_setkey (void *ctx, const void *key, size_t keylen, struct cipher_algorithm *raw_cipher, void *cbc_ctx __unused)
 Set key. More...
 
static void cbc_setiv (void *ctx __unused, const void *iv, size_t ivlen, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Set initialisation vector. More...
 
void cbc_encrypt (void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Encrypt data. More...
 
void cbc_decrypt (void *ctx, const void *src, void *dst, size_t len, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Decrypt data. More...
 

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 64 of file cbc.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cbc_setkey()

static 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 24 of file cbc.h.

26  {
27 
28  return cipher_setkey ( raw_cipher, ctx, key, keylen );
29 }
static void const void size_t keylen
Definition: crypto.h:233
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
union @382 key
Sense key.
Definition: crypto.h:284

References ctx, key, and keylen.

◆ cbc_setiv()

static 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 40 of file cbc.h.

43  {
44  assert ( ivlen == raw_cipher->blocksize );
45  memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
46 }
size_t blocksize
Block size.
Definition: crypto.h:59
static void const void size_t ivlen
Definition: crypto.h:239
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void const void * iv
Definition: crypto.h:238

References assert(), cipher_algorithm::blocksize, iv, ivlen, 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 
)

Encrypt data.

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

Definition at line 66 of file cbc.c.

67  {
68  size_t blocksize = raw_cipher->blocksize;
69 
70  assert ( ( len % blocksize ) == 0 );
71 
72  while ( len ) {
73  cbc_xor ( src, cbc_ctx, blocksize );
74  cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
75  memcpy ( cbc_ctx, dst, blocksize );
76  dst += blocksize;
77  src += blocksize;
78  len -= blocksize;
79  }
80 }
size_t blocksize
Block size.
Definition: crypto.h:59
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition: cbc.c:44
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:248
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
uint32_t len
Length.
Definition: ena.h:14

References assert(), cipher_algorithm::blocksize, cbc_xor(), cipher_encrypt, ctx, dst, 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 
)

Decrypt data.

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

Definition at line 92 of file cbc.c.

93  {
94  size_t blocksize = raw_cipher->blocksize;
95  uint8_t next_cbc_ctx[blocksize];
96 
97  assert ( ( len % blocksize ) == 0 );
98 
99  while ( len ) {
100  memcpy ( next_cbc_ctx, src, blocksize );
101  cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
102  cbc_xor ( cbc_ctx, dst, blocksize );
103  memcpy ( cbc_ctx, next_cbc_ctx, blocksize );
104  dst += blocksize;
105  src += blocksize;
106  len -= blocksize;
107  }
108 }
size_t blocksize
Block size.
Definition: crypto.h:59
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition: cbc.c:44
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:258
uint32_t len
Length.
Definition: ena.h:14

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