iPXE
Functions
cbc.c File Reference

Cipher-block chaining. More...

#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/cbc.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void cbc_xor (const void *src, void *dst, size_t len)
 XOR data blocks. 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.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ cbc_xor()

static void cbc_xor ( const void *  src,
void *  dst,
size_t  len 
)
static

XOR data blocks.

Parameters
srcInput data
dstSecond input data and output data buffer
lenLength of data

Definition at line 44 of file cbc.c.

44  {
45  const uint32_t *srcl = src;
46  uint32_t *dstl = dst;
47  unsigned int i;
48 
49  /* Assume that block sizes will always be dword-aligned, for speed */
50  assert ( ( len % sizeof ( *srcl ) ) == 0 );
51 
52  for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
53  dstl[i] ^= srcl[i];
54 }
static const void * src
Definition: string.h:47
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int uint32_t
Definition: stdint.h:12
uint32_t len
Length.
Definition: ena.h:14

References assert(), len, and src.

Referenced by cbc_decrypt(), and cbc_encrypt().

◆ 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:60
static const void * src
Definition: string.h:47
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:233
void * memcpy(void *dest, const void *src, size_t len) __nonnull
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
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:60
static const void * src
Definition: string.h:47
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)
unsigned char uint8_t
Definition: stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:243
uint32_t len
Length.
Definition: ena.h:14

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