iPXE
cbc.c File Reference

Cipher-block chaining. More...

#include <string.h>
#include <assert.h>
#include <errno.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)
int cbc_setkey (struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
 Set key.
int cbc_setiv (struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
 Set initialisation vector.
static void cbc_xor (const void *src, void *dst, size_t len)
 XOR data blocks.
void cbc_encrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 Encrypt data.
void cbc_decrypt (struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len)
 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_setkey()

int cbc_setkey ( struct cipher_algorithm * cipher,
void * ctx,
const void * key,
size_t keylen )

Set key.

Parameters
cipherCipher algorithm
ctxContext
keyKey
keylenKey length
Return values
rcReturn status code

Definition at line 48 of file cbc.c.

49 {
50 struct cipher_algorithm *raw_cipher = cipher->priv;
51 size_t blocksize = cipher->blocksize;
52 size_t ctxsize = cipher->ctxsize;
53 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
54
55 return cipher_setkey ( raw_cipher, context->raw, key, keylen );
56}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
#define cbc_context_t(blocksize, ctxsize)
A cipher-block chaining mode context.
Definition cbc.h:16
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
A cipher algorithm.
Definition crypto.h:58
void * priv
Algorithm private data.
Definition crypto.h:138
size_t blocksize
Block size.
Definition crypto.h:68
size_t ctxsize
Context size.
Definition crypto.h:62

References cipher_algorithm::blocksize, cbc_context_t, cipher_setkey(), ctx, cipher_algorithm::ctxsize, key, and cipher_algorithm::priv.

◆ cbc_setiv()

int cbc_setiv ( struct cipher_algorithm * cipher,
void * ctx,
const void * iv,
size_t ivlen )

Set initialisation vector.

Parameters
cipherCipher algorithm
ctxContext
ivInitialisation vector
ivlenInitialisation vector length
Return values
rcReturn status code

Definition at line 67 of file cbc.c.

68 {
69 size_t blocksize = cipher->blocksize;
70 size_t ctxsize = cipher->ctxsize;
71 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
72
73 /* Check length */
74 if ( ivlen != sizeof ( context->cbc ) )
75 return -ENOTSUP;
76
77 /* Record IV */
78 memcpy ( context->cbc, iv, sizeof ( context->cbc ) );
79
80 return 0;
81}
#define ENOTSUP
Operation not supported.
Definition errno.h:590
void * memcpy(void *dest, const void *src, size_t len) __nonnull
u8 iv[16]
Initialization vector.
Definition wpa.h:33

References cipher_algorithm::blocksize, cbc_context_t, ctx, cipher_algorithm::ctxsize, ENOTSUP, iv, and memcpy().

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

90 {
91 const uint32_t *srcl = src;
92 uint32_t *dstl = dst;
93 unsigned int i;
94
95 /* Assume that block sizes will always be dword-aligned, for speed */
96 assert ( ( len % sizeof ( *srcl ) ) == 0 );
97
98 for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
99 dstl[i] ^= srcl[i];
100}
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 ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )

Encrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to encrypt
dstBuffer for encrypted data
lenLength of data

Definition at line 111 of file cbc.c.

112 {
113 struct cipher_algorithm *raw_cipher = cipher->priv;
114 size_t blocksize = cipher->blocksize;
115 size_t ctxsize = cipher->ctxsize;
116 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
117
118 assert ( ( len % blocksize ) == 0 );
119
120 while ( len ) {
121 cbc_xor ( src, context->cbc, blocksize );
122 cipher_encrypt ( raw_cipher, context->raw, context->cbc, dst,
123 blocksize );
124 memcpy ( context->cbc, dst, blocksize );
125 dst += blocksize;
126 src += blocksize;
127 len -= blocksize;
128 }
129}
static void cbc_xor(const void *src, void *dst, size_t len)
XOR data blocks.
Definition cbc.c:90
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326

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

◆ cbc_decrypt()

void cbc_decrypt ( struct cipher_algorithm * cipher,
void * ctx,
const void * src,
void * dst,
size_t len )

Decrypt data.

Parameters
cipherCipher algorithm
ctxContext
srcData to decrypt
dstBuffer for decrypted data
lenLength of data

Definition at line 140 of file cbc.c.

141 {
142 struct cipher_algorithm *raw_cipher = cipher->priv;
143 size_t blocksize = cipher->blocksize;
144 size_t ctxsize = cipher->ctxsize;
145 cbc_context_t ( blocksize, ctxsize ) *context = ctx;
146 uint8_t next_cbc_ctx[blocksize];
147
148 assert ( ( len % blocksize ) == 0 );
149
150 while ( len ) {
151 memcpy ( next_cbc_ctx, src, blocksize );
152 cipher_decrypt ( raw_cipher, context->raw, src, dst,
153 blocksize );
154 cbc_xor ( context->cbc, dst, blocksize );
155 memcpy ( context->cbc, next_cbc_ctx, blocksize );
156 dst += blocksize;
157 src += blocksize;
158 len -= blocksize;
159 }
160}
unsigned char uint8_t
Definition stdint.h:10
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:336

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