Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Typedefs | Functions

pgp-cfb.c File Reference

#include "postgres.h"
#include "mbuf.h"
#include "px.h"
#include "pgp.h"
Include dependency graph for pgp-cfb.c:

Go to the source code of this file.

Data Structures

struct  PGP_CFB

Typedefs

typedef int(* mix_data_t )(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)

Functions

int pgp_cfb_create (PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len, int resync, uint8 *iv)
void pgp_cfb_free (PGP_CFB *ctx)
static int mix_encrypt_normal (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int mix_decrypt_normal (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int mix_encrypt_resync (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int mix_decrypt_resync (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
static int cfb_process (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst, mix_data_t mix_data)
int pgp_cfb_encrypt (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
int pgp_cfb_decrypt (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)

Typedef Documentation

typedef int(* mix_data_t)(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)

Definition at line 38 of file pgp-cfb.c.


Function Documentation

static int cfb_process ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst,
mix_data_t  mix_data 
) [static]

Definition at line 199 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::ciph, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, PGP_CFB::pos, and px_cipher_encrypt.

Referenced by pgp_cfb_decrypt(), and pgp_cfb_encrypt().

{
    int         n;
    int         res;

    while (len > 0 && ctx->pos > 0)
    {
        n = ctx->block_size - ctx->pos;
        if (len < n)
            n = len;

        n = mix_data(ctx, data, n, dst);
        data += n;
        dst += n;
        len -= n;

        if (ctx->pos == ctx->block_size)
        {
            memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
            ctx->pos = 0;
        }
    }

    while (len > 0)
    {
        px_cipher_encrypt(ctx->ciph, ctx->fr, ctx->block_size, ctx->fre);
        if (ctx->block_no < 5)
            ctx->block_no++;

        n = ctx->block_size;
        if (len < n)
            n = len;

        res = mix_data(ctx, data, n, dst);
        data += res;
        dst += res;
        len -= res;

        if (ctx->pos == ctx->block_size)
        {
            memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
            ctx->pos = 0;
        }
    }
    return 0;
}

static int mix_decrypt_normal ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
) [static]

Definition at line 107 of file pgp-cfb.c.

References PGP_CFB::encbuf, PGP_CFB::fre, i, and PGP_CFB::pos.

{
    int         i;

    for (i = ctx->pos; i < ctx->pos + len; i++)
    {
        ctx->encbuf[i] = *data++;
        *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
    }
    ctx->pos += len;
    return len;
}

static int mix_decrypt_resync ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
) [static]

Definition at line 159 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_decrypt().

{
    int         i,
                n;

    /* block #2 is 2 bytes long */
    if (ctx->block_no == 2)
    {
        n = 2 - ctx->pos;
        if (len < n)
            n = len;
        for (i = ctx->pos; i < ctx->pos + n; i++)
        {
            ctx->encbuf[i] = *data++;
            *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
        }
        ctx->pos += n;
        len -= n;

        if (ctx->pos == 2)
        {
            memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
            memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
            ctx->pos = 0;
            return n;
        }
    }
    for (i = ctx->pos; i < ctx->pos + len; i++)
    {
        ctx->encbuf[i] = *data++;
        *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
    }
    ctx->pos += len;
    return len;
}

static int mix_encrypt_normal ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
) [static]

Definition at line 96 of file pgp-cfb.c.

References PGP_CFB::encbuf, PGP_CFB::fre, i, and PGP_CFB::pos.

{
    int         i;

    for (i = ctx->pos; i < ctx->pos + len; i++)
        *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
    ctx->pos += len;
    return len;
}

static int mix_encrypt_resync ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
) [static]

Definition at line 127 of file pgp-cfb.c.

References PGP_CFB::block_no, PGP_CFB::block_size, PGP_CFB::encbuf, PGP_CFB::fr, PGP_CFB::fre, i, and PGP_CFB::pos.

Referenced by pgp_cfb_encrypt().

{
    int         i,
                n;

    /* block #2 is 2 bytes long */
    if (ctx->block_no == 2)
    {
        n = 2 - ctx->pos;
        if (len < n)
            n = len;
        for (i = ctx->pos; i < ctx->pos + n; i++)
            *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);

        ctx->pos += n;
        len -= n;

        if (ctx->pos == 2)
        {
            memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
            memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
            ctx->pos = 0;
            return n;
        }
    }
    for (i = ctx->pos; i < ctx->pos + len; i++)
        *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
    ctx->pos += len;
    return len;
}

int pgp_cfb_create ( PGP_CFB **  ctx_p,
int  algo,
const uint8 key,
int  key_len,
int  resync,
uint8 iv 
)

Definition at line 53 of file pgp-cfb.c.

References PGP_CFB::block_size, PGP_CFB::ciph, PGP_CFB::fr, NULL, pgp_load_cipher(), px_alloc, px_cipher_block_size, px_cipher_free, px_cipher_init, and PGP_CFB::resync.

Referenced by decrypt_key(), encrypt_init(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().

{
    int         res;
    PX_Cipher  *ciph;
    PGP_CFB    *ctx;

    res = pgp_load_cipher(algo, &ciph);
    if (res < 0)
        return res;

    res = px_cipher_init(ciph, key, key_len, NULL);
    if (res < 0)
    {
        px_cipher_free(ciph);
        return res;
    }

    ctx = px_alloc(sizeof(*ctx));
    memset(ctx, 0, sizeof(*ctx));
    ctx->ciph = ciph;
    ctx->block_size = px_cipher_block_size(ciph);
    ctx->resync = resync;

    if (iv)
        memcpy(ctx->fr, iv, ctx->block_size);

    *ctx_p = ctx;
    return 0;
}

int pgp_cfb_decrypt ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)

Definition at line 260 of file pgp-cfb.c.

References cfb_process(), mix, mix_decrypt_resync(), and PGP_CFB::resync.

Referenced by decrypt_key(), and decrypt_read().

{
    mix_data_t  mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;

    return cfb_process(ctx, data, len, dst, mix);
}

int pgp_cfb_encrypt ( PGP_CFB ctx,
const uint8 data,
int  len,
uint8 dst 
)

Definition at line 252 of file pgp-cfb.c.

References cfb_process(), mix, mix_encrypt_resync(), and PGP_CFB::resync.

Referenced by encrypt_process(), and symencrypt_sesskey().

{
    mix_data_t  mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;

    return cfb_process(ctx, data, len, dst, mix);
}

void pgp_cfb_free ( PGP_CFB ctx  ) 

Definition at line 85 of file pgp-cfb.c.

References PGP_CFB::ciph, px_cipher_free, and px_free.

Referenced by decrypt_key(), parse_symenc_data(), parse_symenc_mdc_data(), process_secret_key(), and symencrypt_sesskey().

{
    px_cipher_free(ctx->ciph);
    memset(ctx, 0, sizeof(*ctx));
    px_free(ctx);
}