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.
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 ( GPL2_OR_LATER_OR_UBDL  )
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.

References assert, and src.

Referenced by cbc_decrypt(), and cbc_encrypt().

                                                               {
        const uint32_t *srcl = src;
        uint32_t *dstl = dst;
        unsigned int i;

        /* Assume that block sizes will always be dword-aligned, for speed */
        assert ( ( len % sizeof ( *srcl ) ) == 0 );

        for ( i = 0 ; i < ( len / sizeof ( *srcl ) ) ; i++ )
                dstl[i] ^= srcl[i];
}
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;
        }
}