mco_crypt_block

Encrypts or decrypts a block of data using the encryption context.

For an overview see page Encryption C API

Prototype

    void mco_crypt_block(mco_crypt_ctx_t const* crypt_ctx, uint1* block, uint4 block_size, uint8 block_offs);

Arguments

crypt_ctx
The address of the initialized encryption context (see mco_crypt_init_ctx()).
block
The address of the data array to be encrypted and receive the encrypted result.
block_size
The size of the block.
block_offs
The offset into the data array where the block will be read or written.

Description

This function encrypts or decrypts a block of data using the specified pre-initialized encryption context crypt_ctx. Where block and block_size are features of the block itself while block_offs is an offset of current block in whole array of data (see usage in the example below). This function is useful for block-based operations like disk drive storage.

(Note that instead of block-based operations the functions mco_crypt_stream() and mco_crypt_init_state() provide stream-based encryption or decryption.)

Return Codes

void
No value returned.

Example

The following code snippet demonstrates encrypting / decrypting an external file:

    /* Code sample for block-based encryption */
    FILE *fin, *fout;
    char block[512];
    const int block_size = sizeof(block);
    const char *cipher = "my cipher key";
    mco_crypt_ctx_t crypt_ctx;
    size_t n;
    uint8 pos = 0;

    mco_crypt_init_ctx(&crypt_ctx, cipher);
    fin = fopen("unencrypted.file", "rb");
    fout = fopen("encrypted.file", "wb");
    do
    {
        n = fread(block, block_size, 1, fin);
        mco_crypt_block(&crypt_ctx, block, n, pos);

        fwrite(block, n, 1, fout);
        pos += block_size;
    } while (n == block_size);
    fclose(fin);
    fclose(fout);

Files

Header file:
mco.h
Source file:
mcocryptaes.c
Library:
libmcocryptaes.a
Header file:
mco.h
Source file:
mcocryptstub.c
Library:
libmcocryptstub.a