Header And Logo

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

Defines | Functions | Variables

blf.c File Reference

#include "postgres.h"
#include "blf.h"
Include dependency graph for blf.c:

Go to the source code of this file.

Defines

#define GET_32BIT_MSB_FIRST(p)
#define PUT_32BIT_MSB_FIRST(p, v)
#define Fprime(a, b, c, d)   ( ( (S0[a] + S1[b]) ^ S2[c] ) + S3[d] )
#define F(x)   Fprime( ((x>>24)&0xFF), ((x>>16)&0xFF), ((x>>8)&0xFF), (x&0xFF) )
#define ROUND(n)   ( xL ^= P[n], t = xL, xL = F(xL) ^ xR, xR = t )

Functions

static void blowfish_encrypt (uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
static void blowfish_decrypt (uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
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)
void blowfish_setkey (BlowfishContext *ctx, const uint8 *key, short keybytes)
void blowfish_setiv (BlowfishContext *ctx, const uint8 *iv)

Variables

static const uint32 parray []
static const uint32 sbox0 []
static const uint32 sbox1 []
static const uint32 sbox2 []
static const uint32 sbox3 []

Define Documentation

#define F (   x  )     Fprime( ((x>>24)&0xFF), ((x>>16)&0xFF), ((x>>8)&0xFF), (x&0xFF) )

Definition at line 249 of file blf.c.

#define Fprime (   a,
  b,
  c,
  d 
)    ( ( (S0[a] + S1[b]) ^ S2[c] ) + S3[d] )

Definition at line 248 of file blf.c.

#define GET_32BIT_MSB_FIRST (   p  ) 
Value:
( \
    ((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | ((p)[3]) )

Definition at line 44 of file blf.c.

Referenced by blowfish_decrypt_cbc(), blowfish_decrypt_ecb(), blowfish_encrypt_cbc(), blowfish_encrypt_ecb(), and blowfish_setiv().

#define PUT_32BIT_MSB_FIRST (   p,
  v 
)
Value:
do { \
    (p)[0] = v >> 24; \
    (p)[1] = v >> 16; \
    (p)[2] = v >> 8; \
    (p)[3] = v; \
} while (0)

Definition at line 47 of file blf.c.

Referenced by blowfish_decrypt_cbc(), blowfish_decrypt_ecb(), blowfish_encrypt_cbc(), and blowfish_encrypt_ecb().

#define ROUND (   n  )     ( xL ^= P[n], t = xL, xL = F(xL) ^ xR, xR = t )

Definition at line 250 of file blf.c.

Referenced by blowfish_decrypt(), and blowfish_encrypt().


Function Documentation

static void blowfish_decrypt ( uint32  xL,
uint32  xR,
uint32 output,
BlowfishContext ctx 
) [static]

Definition at line 287 of file blf.c.

References BlowfishContext::P, ROUND, BlowfishContext::S0, BlowfishContext::S1, BlowfishContext::S2, and BlowfishContext::S3.

Referenced by blowfish_decrypt_cbc(), and blowfish_decrypt_ecb().

{
    uint32     *S0 = ctx->S0;
    uint32     *S1 = ctx->S1;
    uint32     *S2 = ctx->S2;
    uint32     *S3 = ctx->S3;
    uint32     *P = ctx->P;
    uint32      t;

    ROUND(17);
    ROUND(16);
    ROUND(15);
    ROUND(14);
    ROUND(13);
    ROUND(12);
    ROUND(11);
    ROUND(10);
    ROUND(9);
    ROUND(8);
    ROUND(7);
    ROUND(6);
    ROUND(5);
    ROUND(4);
    ROUND(3);
    ROUND(2);
    xL ^= P[1];
    xR ^= P[0];

    output[0] = xR;
    output[1] = xL;
}

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;
    }
}

static void blowfish_encrypt ( uint32  xL,
uint32  xR,
uint32 output,
BlowfishContext ctx 
) [static]

Definition at line 253 of file blf.c.

References BlowfishContext::P, ROUND, BlowfishContext::S0, BlowfishContext::S1, BlowfishContext::S2, and BlowfishContext::S3.

Referenced by blowfish_encrypt_cbc(), blowfish_encrypt_ecb(), and blowfish_setkey().

{
    uint32     *S0 = ctx->S0;
    uint32     *S1 = ctx->S1;
    uint32     *S2 = ctx->S2;
    uint32     *S3 = ctx->S3;
    uint32     *P = ctx->P;
    uint32      t;

    ROUND(0);
    ROUND(1);
    ROUND(2);
    ROUND(3);
    ROUND(4);
    ROUND(5);
    ROUND(6);
    ROUND(7);
    ROUND(8);
    ROUND(9);
    ROUND(10);
    ROUND(11);
    ROUND(12);
    ROUND(13);
    ROUND(14);
    ROUND(15);
    xL ^= P[16];
    xR ^= P[17];

    output[0] = xR;
    output[1] = xL;
}

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];
    }
}


Variable Documentation

const uint32 parray[] [static]
Initial value:
 {
    0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0,
    0x082EFA98, 0xEC4E6C89, 0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C,
    0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917, 0x9216D5D9, 0x8979FB1B,
}

Definition at line 58 of file blf.c.

Referenced by blowfish_setkey().

const uint32 sbox0[] [static]

Definition at line 64 of file blf.c.

Referenced by blowfish_setkey().

const uint32 sbox1[] [static]

Definition at line 110 of file blf.c.

Referenced by blowfish_setkey().

const uint32 sbox2[] [static]

Definition at line 156 of file blf.c.

Referenced by blowfish_setkey().

const uint32 sbox3[] [static]

Definition at line 202 of file blf.c.

Referenced by blowfish_setkey().