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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ cbc_xor()

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 45 of file cbc.c.

45 {
46 const uint32_t *srcl = src;
47 uint32_t *dstl = dst;
48 unsigned int i;
49
50 /* Assume that block sizes will always be dword-aligned, for speed */
51 assert ( ( len % sizeof ( *srcl ) ) == 0 );
52
53 for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
54 dstl[i] ^= srcl[i];
55}
unsigned int uint32_t
Definition stdint.h:12
static const void * src
Definition string.h:48
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226

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 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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:45
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:251
void * memcpy(void *dest, const void *src, size_t len) __nonnull
size_t blocksize
Block size.
Definition crypto.h:61

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 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.