iPXE
Defines | Functions
cbc.h File Reference

Cipher-block chaining. More...

#include <ipxe/crypto.h>

Go to the source code of this file.

Defines

#define CBC_CIPHER(_cbc_name, _cbc_cipher, _raw_cipher, _raw_context, _blocksize)
 Create a cipher-block chaining mode of behaviour of an existing cipher.

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.
static void cbc_setiv (void *ctx __unused, const void *iv, struct cipher_algorithm *raw_cipher, void *cbc_ctx)
 Set initialisation vector.
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.h.


Define Documentation

#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 ( 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 
) [inline, static]

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.

References cipher_setkey().

                                                        {

        return cipher_setkey ( raw_cipher, ctx, key, keylen );
}
static void cbc_setiv ( void *ctx  __unused,
const void *  iv,
struct cipher_algorithm raw_cipher,
void *  cbc_ctx 
) [inline, static]

Set initialisation vector.

Parameters:
ctxContext
ivInitialisation vector
raw_cipherUnderlying cipher algorithm
cbc_ctxCBC context

Definition at line 39 of file cbc.h.

References cipher_algorithm::blocksize, and memcpy().

                                               {
        memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
}
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.

References assert, cipher_algorithm::blocksize, cbc_xor(), cipher_encrypt, and memcpy().

                                                                        {
        size_t blocksize = raw_cipher->blocksize;

        assert ( ( len % blocksize ) == 0 );

        while ( len ) {
                cbc_xor ( src, cbc_ctx, blocksize );
                cipher_encrypt ( raw_cipher, ctx, cbc_ctx, dst, blocksize );
                memcpy ( cbc_ctx, dst, blocksize );
                dst += blocksize;
                src += blocksize;
                len -= blocksize;
        }
}
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.

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

                                                                        {
        size_t blocksize = raw_cipher->blocksize;
        uint8_t next_cbc_ctx[blocksize];

        assert ( ( len % blocksize ) == 0 );

        while ( len ) {
                memcpy ( next_cbc_ctx, src, blocksize );
                cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
                cbc_xor ( cbc_ctx, dst, blocksize );
                memcpy ( cbc_ctx, next_cbc_ctx, blocksize );
                dst += blocksize;
                src += blocksize;
                len -= blocksize;
        }
}