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, 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 61 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 }
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
union @376 key
Sense key.
Definition: scsi.h:18
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:187

References cipher_setkey(), ctx, and key.

◆ cbc_setiv()

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

Set initialisation vector.

Parameters
ctxContext
ivInitialisation vector
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context

Definition at line 39 of file cbc.h.

41  {
42  memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
43 }
size_t blocksize
Block size.
Definition: crypto.h:54
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
void * memcpy(void *dest, const void *src, size_t len) __nonnull

References cipher_algorithm::blocksize, 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 
)

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:54
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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:202
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
uint32_t len
Length.
Definition: ena.h:14

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 
)

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:54
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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)
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
unsigned char uint8_t
Definition: stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:212
uint32_t len
Length.
Definition: ena.h:14

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