PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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().

355 {
356  uint32 xL,
357  xR,
358  out[2],
359  iv0,
360  iv1;
361 
362  Assert((len & 7) == 0);
363 
364  iv0 = ctx->iv0;
365  iv1 = ctx->iv1;
366 
367  while (len > 0)
368  {
369  xL = GET_32BIT_MSB_FIRST(blk);
370  xR = GET_32BIT_MSB_FIRST(blk + 4);
371  blowfish_decrypt(xL, xR, out, ctx);
372  iv0 ^= out[0];
373  iv1 ^= out[1];
374  PUT_32BIT_MSB_FIRST(blk, iv0);
375  PUT_32BIT_MSB_FIRST(blk + 4, iv1);
376  iv0 = xL;
377  iv1 = xR;
378  blk += 8;
379  len -= 8;
380  }
381 
382  ctx->iv0 = iv0;
383  ctx->iv1 = iv1;
384 }
static void blowfish_decrypt(uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
Definition: blf.c:287
uint32 iv1
Definition: blf.h:37
unsigned int uint32
Definition: c.h:265
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:670
#define PUT_32BIT_MSB_FIRST(p, v)
Definition: blf.c:47
uint32 iv0
Definition: blf.h:37
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().

409 {
410  uint32 xL,
411  xR,
412  out[2];
413 
414  Assert((len & 7) == 0);
415 
416  while (len > 0)
417  {
418  xL = GET_32BIT_MSB_FIRST(blk);
419  xR = GET_32BIT_MSB_FIRST(blk + 4);
420  blowfish_decrypt(xL, xR, out, ctx);
421  PUT_32BIT_MSB_FIRST(blk, out[0]);
422  PUT_32BIT_MSB_FIRST(blk + 4, out[1]);
423  blk += 8;
424  len -= 8;
425  }
426 }
static void blowfish_decrypt(uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
Definition: blf.c:287
unsigned int uint32
Definition: c.h:265
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:670
#define PUT_32BIT_MSB_FIRST(p, v)
Definition: blf.c:47
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().

322 {
323  uint32 xL,
324  xR,
325  out[2],
326  iv0,
327  iv1;
328 
329  Assert((len & 7) == 0);
330 
331  iv0 = ctx->iv0;
332  iv1 = ctx->iv1;
333 
334  while (len > 0)
335  {
336  xL = GET_32BIT_MSB_FIRST(blk);
337  xR = GET_32BIT_MSB_FIRST(blk + 4);
338  iv0 ^= xL;
339  iv1 ^= xR;
340  blowfish_encrypt(iv0, iv1, out, ctx);
341  iv0 = out[0];
342  iv1 = out[1];
343  PUT_32BIT_MSB_FIRST(blk, iv0);
344  PUT_32BIT_MSB_FIRST(blk + 4, iv1);
345  blk += 8;
346  len -= 8;
347  }
348 
349  ctx->iv0 = iv0;
350  ctx->iv1 = iv1;
351 }
uint32 iv1
Definition: blf.h:37
unsigned int uint32
Definition: c.h:265
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:670
static void blowfish_encrypt(uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
Definition: blf.c:253
#define PUT_32BIT_MSB_FIRST(p, v)
Definition: blf.c:47
uint32 iv0
Definition: blf.h:37
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().

388 {
389  uint32 xL,
390  xR,
391  out[2];
392 
393  Assert((len & 7) == 0);
394 
395  while (len > 0)
396  {
397  xL = GET_32BIT_MSB_FIRST(blk);
398  xR = GET_32BIT_MSB_FIRST(blk + 4);
399  blowfish_encrypt(xL, xR, out, ctx);
400  PUT_32BIT_MSB_FIRST(blk, out[0]);
401  PUT_32BIT_MSB_FIRST(blk + 4, out[1]);
402  blk += 8;
403  len -= 8;
404  }
405 }
unsigned int uint32
Definition: c.h:265
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:670
static void blowfish_encrypt(uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
Definition: blf.c:253
#define PUT_32BIT_MSB_FIRST(p, v)
Definition: blf.c:47
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().

496 {
497  ctx->iv0 = GET_32BIT_MSB_FIRST(iv);
498  ctx->iv1 = GET_32BIT_MSB_FIRST(iv + 4);
499 }
uint32 iv1
Definition: blf.h:37
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
uint32 iv0
Definition: blf.h:37
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().

431 {
432  uint32 *S0 = ctx->S0;
433  uint32 *S1 = ctx->S1;
434  uint32 *S2 = ctx->S2;
435  uint32 *S3 = ctx->S3;
436  uint32 *P = ctx->P;
437  uint32 str[2];
438  int i;
439 
440  Assert(keybytes > 0 && keybytes <= (448 / 8));
441 
442  for (i = 0; i < 18; i++)
443  {
444  P[i] = parray[i];
445  P[i] ^= ((uint32) key[(i * 4 + 0) % keybytes]) << 24;
446  P[i] ^= ((uint32) key[(i * 4 + 1) % keybytes]) << 16;
447  P[i] ^= ((uint32) key[(i * 4 + 2) % keybytes]) << 8;
448  P[i] ^= ((uint32) key[(i * 4 + 3) % keybytes]);
449  }
450 
451  for (i = 0; i < 256; i++)
452  {
453  S0[i] = sbox0[i];
454  S1[i] = sbox1[i];
455  S2[i] = sbox2[i];
456  S3[i] = sbox3[i];
457  }
458 
459  str[0] = str[1] = 0;
460 
461  for (i = 0; i < 18; i += 2)
462  {
463  blowfish_encrypt(str[0], str[1], str, ctx);
464  P[i] = str[0];
465  P[i + 1] = str[1];
466  }
467 
468  for (i = 0; i < 256; i += 2)
469  {
470  blowfish_encrypt(str[0], str[1], str, ctx);
471  S0[i] = str[0];
472  S0[i + 1] = str[1];
473  }
474  for (i = 0; i < 256; i += 2)
475  {
476  blowfish_encrypt(str[0], str[1], str, ctx);
477  S1[i] = str[0];
478  S1[i + 1] = str[1];
479  }
480  for (i = 0; i < 256; i += 2)
481  {
482  blowfish_encrypt(str[0], str[1], str, ctx);
483  S2[i] = str[0];
484  S2[i + 1] = str[1];
485  }
486  for (i = 0; i < 256; i += 2)
487  {
488  blowfish_encrypt(str[0], str[1], str, ctx);
489  S3[i] = str[0];
490  S3[i + 1] = str[1];
491  }
492 }
static const uint32 sbox2[]
Definition: blf.c:156
static const uint32 sbox1[]
Definition: blf.c:110
unsigned int uint32
Definition: c.h:265
uint32 P[18]
Definition: blf.h:32
static const uint32 parray[]
Definition: blf.c:58
uint32 S1[256]
Definition: blf.h:32
static const uint32 sbox3[]
Definition: blf.c:202
#define Assert(condition)
Definition: c.h:670
uint32 S0[256]
Definition: blf.h:32
static void blowfish_encrypt(uint32 xL, uint32 xR, uint32 *output, BlowfishContext *ctx)
Definition: blf.c:253
uint32 S3[256]
Definition: blf.h:32
int i
static const uint32 sbox0[]
Definition: blf.c:64
uint32 S2[256]
Definition: blf.h:32