Header And Logo

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

Data Structures | Functions

blf.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BlowfishContext

Functions

void blowfish_setkey (BlowfishContext *ctx, const uint8 *key, short keybytes)
void blowfish_setiv (BlowfishContext *ctx, const uint8 *iv)
void blowfish_encrypt_cbc (uint8 *blk, int len, BlowfishContext *ctx)
void blowfish_decrypt_cbc (uint8 *blk, int len, BlowfishContext *ctx)
void blowfish_encrypt_ecb (uint8 *blk, int len, BlowfishContext *ctx)
void blowfish_decrypt_ecb (uint8 *blk, int len, BlowfishContext *ctx)

Function Documentation

void blowfish_decrypt_cbc ( uint8 blk,
int  len,
BlowfishContext ctx 
)

Definition at line 354 of file blf.c.

References Assert, blowfish_decrypt(), GET_32BIT_MSB_FIRST, BlowfishContext::iv0, BlowfishContext::iv1, and PUT_32BIT_MSB_FIRST.

Referenced by bf_decrypt().

{
    uint32      xL,
                xR,
                out[2],
                iv0,
                iv1;

    Assert((len & 7) == 0);

    iv0 = ctx->iv0;
    iv1 = ctx->iv1;

    while (len > 0)
    {
        xL = GET_32BIT_MSB_FIRST(blk);
        xR = GET_32BIT_MSB_FIRST(blk + 4);
        blowfish_decrypt(xL, xR, out, ctx);
        iv0 ^= out[0];
        iv1 ^= out[1];
        PUT_32BIT_MSB_FIRST(blk, iv0);
        PUT_32BIT_MSB_FIRST(blk + 4, iv1);
        iv0 = xL;
        iv1 = xR;
        blk += 8;
        len -= 8;
    }

    ctx->iv0 = iv0;
    ctx->iv1 = iv1;
}

void blowfish_decrypt_ecb ( uint8 blk,
int  len,
BlowfishContext ctx 
)

Definition at line 408 of file blf.c.

References Assert, blowfish_decrypt(), GET_32BIT_MSB_FIRST, and PUT_32BIT_MSB_FIRST.

Referenced by bf_decrypt().

{
    uint32      xL,
                xR,
                out[2];

    Assert((len & 7) == 0);

    while (len > 0)
    {
        xL = GET_32BIT_MSB_FIRST(blk);
        xR = GET_32BIT_MSB_FIRST(blk + 4);
        blowfish_decrypt(xL, xR, out, ctx);
        PUT_32BIT_MSB_FIRST(blk, out[0]);
        PUT_32BIT_MSB_FIRST(blk + 4, out[1]);
        blk += 8;
        len -= 8;
    }
}

void blowfish_encrypt_cbc ( uint8 blk,
int  len,
BlowfishContext ctx 
)

Definition at line 321 of file blf.c.

References Assert, blowfish_encrypt(), GET_32BIT_MSB_FIRST, BlowfishContext::iv0, BlowfishContext::iv1, and PUT_32BIT_MSB_FIRST.

Referenced by bf_encrypt().

{
    uint32      xL,
                xR,
                out[2],
                iv0,
                iv1;

    Assert((len & 7) == 0);

    iv0 = ctx->iv0;
    iv1 = ctx->iv1;

    while (len > 0)
    {
        xL = GET_32BIT_MSB_FIRST(blk);
        xR = GET_32BIT_MSB_FIRST(blk + 4);
        iv0 ^= xL;
        iv1 ^= xR;
        blowfish_encrypt(iv0, iv1, out, ctx);
        iv0 = out[0];
        iv1 = out[1];
        PUT_32BIT_MSB_FIRST(blk, iv0);
        PUT_32BIT_MSB_FIRST(blk + 4, iv1);
        blk += 8;
        len -= 8;
    }

    ctx->iv0 = iv0;
    ctx->iv1 = iv1;
}

void blowfish_encrypt_ecb ( uint8 blk,
int  len,
BlowfishContext ctx 
)

Definition at line 387 of file blf.c.

References Assert, blowfish_encrypt(), GET_32BIT_MSB_FIRST, and PUT_32BIT_MSB_FIRST.

Referenced by bf_encrypt().

{
    uint32      xL,
                xR,
                out[2];

    Assert((len & 7) == 0);

    while (len > 0)
    {
        xL = GET_32BIT_MSB_FIRST(blk);
        xR = GET_32BIT_MSB_FIRST(blk + 4);
        blowfish_encrypt(xL, xR, out, ctx);
        PUT_32BIT_MSB_FIRST(blk, out[0]);
        PUT_32BIT_MSB_FIRST(blk + 4, out[1]);
        blk += 8;
        len -= 8;
    }
}

void blowfish_setiv ( BlowfishContext ctx,
const uint8 iv 
)

Definition at line 495 of file blf.c.

References GET_32BIT_MSB_FIRST, BlowfishContext::iv0, and BlowfishContext::iv1.

Referenced by bf_init().

{
    ctx->iv0 = GET_32BIT_MSB_FIRST(iv);
    ctx->iv1 = GET_32BIT_MSB_FIRST(iv + 4);
}

void blowfish_setkey ( BlowfishContext ctx,
const uint8 key,
short  keybytes 
)

Definition at line 429 of file blf.c.

References Assert, blowfish_encrypt(), i, BlowfishContext::P, parray, BlowfishContext::S0, BlowfishContext::S1, BlowfishContext::S2, BlowfishContext::S3, sbox0, sbox1, sbox2, and sbox3.

Referenced by bf_init().

{
    uint32     *S0 = ctx->S0;
    uint32     *S1 = ctx->S1;
    uint32     *S2 = ctx->S2;
    uint32     *S3 = ctx->S3;
    uint32     *P = ctx->P;
    uint32      str[2];
    int         i;

    Assert(keybytes > 0 && keybytes <= (448 / 8));

    for (i = 0; i < 18; i++)
    {
        P[i] = parray[i];
        P[i] ^= ((uint32) key[(i * 4 + 0) % keybytes]) << 24;
        P[i] ^= ((uint32) key[(i * 4 + 1) % keybytes]) << 16;
        P[i] ^= ((uint32) key[(i * 4 + 2) % keybytes]) << 8;
        P[i] ^= ((uint32) key[(i * 4 + 3) % keybytes]);
    }

    for (i = 0; i < 256; i++)
    {
        S0[i] = sbox0[i];
        S1[i] = sbox1[i];
        S2[i] = sbox2[i];
        S3[i] = sbox3[i];
    }

    str[0] = str[1] = 0;

    for (i = 0; i < 18; i += 2)
    {
        blowfish_encrypt(str[0], str[1], str, ctx);
        P[i] = str[0];
        P[i + 1] = str[1];
    }

    for (i = 0; i < 256; i += 2)
    {
        blowfish_encrypt(str[0], str[1], str, ctx);
        S0[i] = str[0];
        S0[i + 1] = str[1];
    }
    for (i = 0; i < 256; i += 2)
    {
        blowfish_encrypt(str[0], str[1], str, ctx);
        S1[i] = str[0];
        S1[i + 1] = str[1];
    }
    for (i = 0; i < 256; i += 2)
    {
        blowfish_encrypt(str[0], str[1], str, ctx);
        S2[i] = str[0];
        S2[i + 1] = str[1];
    }
    for (i = 0; i < 256; i += 2)
    {
        blowfish_encrypt(str[0], str[1], str, ctx);
        S3[i] = str[0];
        S3[i + 1] = str[1];
    }
}