PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
blf.c File Reference
#include "postgres.h"
#include "blf.h"
Include dependency graph for blf.c:

Go to the source code of this file.

Macros

#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 []
 

Macro Definition 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,
 
)    ( ( (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,
 
)
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().

289 {
290  uint32 *S0 = ctx->S0;
291  uint32 *S1 = ctx->S1;
292  uint32 *S2 = ctx->S2;
293  uint32 *S3 = ctx->S3;
294  uint32 *P = ctx->P;
295  uint32 t;
296 
297  ROUND(17);
298  ROUND(16);
299  ROUND(15);
300  ROUND(14);
301  ROUND(13);
302  ROUND(12);
303  ROUND(11);
304  ROUND(10);
305  ROUND(9);
306  ROUND(8);
307  ROUND(7);
308  ROUND(6);
309  ROUND(5);
310  ROUND(4);
311  ROUND(3);
312  ROUND(2);
313  xL ^= P[1];
314  xR ^= P[0];
315 
316  output[0] = xR;
317  output[1] = xL;
318 }
static void output(uint64 loop_count)
unsigned int uint32
Definition: c.h:268
uint32 P[18]
Definition: blf.h:32
uint32 S1[256]
Definition: blf.h:32
#define ROUND(n)
Definition: blf.c:250
uint32 S0[256]
Definition: blf.h:32
uint32 S3[256]
Definition: blf.h:32
uint32 S2[256]
Definition: blf.h:32
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:268
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:675
#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:268
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:675
#define PUT_32BIT_MSB_FIRST(p, v)
Definition: blf.c:47
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().

255 {
256  uint32 *S0 = ctx->S0;
257  uint32 *S1 = ctx->S1;
258  uint32 *S2 = ctx->S2;
259  uint32 *S3 = ctx->S3;
260  uint32 *P = ctx->P;
261  uint32 t;
262 
263  ROUND(0);
264  ROUND(1);
265  ROUND(2);
266  ROUND(3);
267  ROUND(4);
268  ROUND(5);
269  ROUND(6);
270  ROUND(7);
271  ROUND(8);
272  ROUND(9);
273  ROUND(10);
274  ROUND(11);
275  ROUND(12);
276  ROUND(13);
277  ROUND(14);
278  ROUND(15);
279  xL ^= P[16];
280  xR ^= P[17];
281 
282  output[0] = xR;
283  output[1] = xL;
284 }
static void output(uint64 loop_count)
unsigned int uint32
Definition: c.h:268
uint32 P[18]
Definition: blf.h:32
uint32 S1[256]
Definition: blf.h:32
#define ROUND(n)
Definition: blf.c:250
uint32 S0[256]
Definition: blf.h:32
uint32 S3[256]
Definition: blf.h:32
uint32 S2[256]
Definition: blf.h:32
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:268
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:675
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:268
#define GET_32BIT_MSB_FIRST(p)
Definition: blf.c:44
#define Assert(condition)
Definition: c.h:675
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:268
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:675
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

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().