Header And Logo

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

Defines | Functions

rijndael.c File Reference

#include "postgres.h"
#include <sys/param.h>
#include "px.h"
#include "rijndael.h"
#include "rijndael.tbl"
Include dependency graph for rijndael.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define PRE_CALC_TABLES
#define LARGE_TABLES
#define rotr(x, n)   (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
#define rotl(x, n)   (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
#define bswap(x)   ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
#define byte(x, n)   ((u1byte)((x) >> (8 * (n))))
#define io_swap(x)   (x)
#define tab_gen   1
#define ff_mult(a, b)   ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
#define f_rn(bo, bi, n, k)
#define i_rn(bo, bi, n, k)
#define ls_box(x)
#define f_rl(bo, bi, n, k)
#define i_rl(bo, bi, n, k)
#define star_x(x)   (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
#define imix_col(y, x)
#define loop4(i)
#define loop6(i)
#define loop8(i)
#define f_nround(bo, bi, k)
#define f_lround(bo, bi, k)
#define i_nround(bo, bi, k)
#define i_lround(bo, bi, k)

Functions

static void gen_tabs (void)
rijndael_ctxrijndael_set_key (rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len, int encrypt)
void rijndael_encrypt (rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
void rijndael_decrypt (rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
void aes_set_key (rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
void aes_ecb_encrypt (rijndael_ctx *ctx, uint8 *data, unsigned len)
void aes_ecb_decrypt (rijndael_ctx *ctx, uint8 *data, unsigned len)
void aes_cbc_encrypt (rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
void aes_cbc_decrypt (rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)

Define Documentation

#define bswap (   x  )     ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))

Definition at line 64 of file rijndael.c.

#define byte (   x,
  n 
)    ((u1byte)((x) >> (8 * (n))))

Definition at line 68 of file rijndael.c.

Referenced by inetmi(), nocache_index_getattr(), and nocachegetattr().

#define f_lround (   bo,
  bi,
  k 
)
Value:
do { \
    f_rl(bo, bi, 0, k);     \
    f_rl(bo, bi, 1, k);     \
    f_rl(bo, bi, 2, k);     \
    f_rl(bo, bi, 3, k);     \
} while (0)

Definition at line 378 of file rijndael.c.

Referenced by rijndael_encrypt().

#define f_nround (   bo,
  bi,
  k 
)
Value:
do { \
    f_rn(bo, bi, 0, k);     \
    f_rn(bo, bi, 1, k);     \
    f_rn(bo, bi, 2, k);     \
    f_rn(bo, bi, 3, k);     \
    k += 4;                 \
} while (0)

Definition at line 369 of file rijndael.c.

Referenced by rijndael_encrypt().

#define f_rl (   bo,
  bi,
  n,
  k 
)
Value:
(bo)[n] =  fl_tab[0][byte((bi)[n],0)] ^             \
             fl_tab[1][byte((bi)[((n) + 1) & 3],1)] ^   \
             fl_tab[2][byte((bi)[((n) + 2) & 3],2)] ^   \
             fl_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))

Definition at line 124 of file rijndael.c.

#define f_rn (   bo,
  bi,
  n,
  k 
)
Value:
(bo)[n] =  ft_tab[0][byte((bi)[n],0)] ^             \
             ft_tab[1][byte((bi)[((n) + 1) & 3],1)] ^   \
             ft_tab[2][byte((bi)[((n) + 2) & 3],2)] ^   \
             ft_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))

Definition at line 104 of file rijndael.c.

#define ff_mult (   a,
  b 
)    ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)

Definition at line 102 of file rijndael.c.

Referenced by gen_tabs().

#define i_lround (   bo,
  bi,
  k 
)
Value:
do { \
    i_rl(bo, bi, 0, k);     \
    i_rl(bo, bi, 1, k);     \
    i_rl(bo, bi, 2, k);     \
    i_rl(bo, bi, 3, k);     \
} while (0)

Definition at line 442 of file rijndael.c.

Referenced by rijndael_decrypt().

#define i_nround (   bo,
  bi,
  k 
)
Value:
do { \
    i_rn(bo, bi, 0, k);     \
    i_rn(bo, bi, 1, k);     \
    i_rn(bo, bi, 2, k);     \
    i_rn(bo, bi, 3, k);     \
    k -= 4;                 \
} while (0)

Definition at line 433 of file rijndael.c.

Referenced by rijndael_decrypt().

#define i_rl (   bo,
  bi,
  n,
  k 
)
Value:
(bo)[n] =  il_tab[0][byte((bi)[n],0)] ^             \
             il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^   \
             il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^   \
             il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))

Definition at line 130 of file rijndael.c.

#define i_rn (   bo,
  bi,
  n,
  k 
)
Value:
(bo)[n] =  it_tab[0][byte((bi)[n],0)] ^             \
             it_tab[1][byte((bi)[((n) + 3) & 3],1)] ^   \
             it_tab[2][byte((bi)[((n) + 2) & 3],2)] ^   \
             it_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))

Definition at line 110 of file rijndael.c.

#define imix_col (   y,
  x 
)
Value:
do { \
    u   = star_x(x);        \
    v   = star_x(u);        \
    w   = star_x(v);        \
    t   = w ^ (x);          \
   (y)  = u ^ v ^ w;        \
   (y) ^= rotr(u ^ t,  8) ^ \
          rotr(v ^ t, 16) ^ \
          rotr(t,24);       \
} while (0)

Definition at line 259 of file rijndael.c.

Referenced by rijndael_set_key().

#define io_swap (   x  )     (x)

Definition at line 73 of file rijndael.c.

Referenced by rijndael_decrypt(), rijndael_encrypt(), and rijndael_set_key().

#define LARGE_TABLES

Definition at line 51 of file rijndael.c.

#define loop4 (   i  ) 
Value:
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];           \
    t ^= e_key[4 * i];     e_key[4 * i + 4] = t;    \
    t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t;    \
    t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t;    \
    t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t;    \
} while (0)

Definition at line 273 of file rijndael.c.

Referenced by rijndael_set_key().

#define loop6 (   i  ) 
Value:
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];           \
    t ^= e_key[6 * (i)];       e_key[6 * (i) + 6] = t;  \
    t ^= e_key[6 * (i) + 1]; e_key[6 * (i) + 7] = t;    \
    t ^= e_key[6 * (i) + 2]; e_key[6 * (i) + 8] = t;    \
    t ^= e_key[6 * (i) + 3]; e_key[6 * (i) + 9] = t;    \
    t ^= e_key[6 * (i) + 4]; e_key[6 * (i) + 10] = t;   \
    t ^= e_key[6 * (i) + 5]; e_key[6 * (i) + 11] = t;   \
} while (0)

Definition at line 281 of file rijndael.c.

Referenced by rijndael_set_key().

#define loop8 (   i  ) 
Value:
do {   t = ls_box(rotr(t,  8)) ^ rco_tab[i];           \
    t ^= e_key[8 * (i)];     e_key[8 * (i) + 8] = t;    \
    t ^= e_key[8 * (i) + 1]; e_key[8 * (i) + 9] = t;    \
    t ^= e_key[8 * (i) + 2]; e_key[8 * (i) + 10] = t;   \
    t ^= e_key[8 * (i) + 3]; e_key[8 * (i) + 11] = t;   \
    t  = e_key[8 * (i) + 4] ^ ls_box(t);                \
    e_key[8 * (i) + 12] = t;                            \
    t ^= e_key[8 * (i) + 5]; e_key[8 * (i) + 13] = t;   \
    t ^= e_key[8 * (i) + 6]; e_key[8 * (i) + 14] = t;   \
    t ^= e_key[8 * (i) + 7]; e_key[8 * (i) + 15] = t;   \
} while (0)

Definition at line 291 of file rijndael.c.

Referenced by rijndael_set_key().

#define ls_box (   x  ) 
Value:
( fl_tab[0][byte(x, 0)] ^    \
      fl_tab[1][byte(x, 1)] ^    \
      fl_tab[2][byte(x, 2)] ^    \
      fl_tab[3][byte(x, 3)] )

Definition at line 118 of file rijndael.c.

#define PRE_CALC_TABLES

Definition at line 50 of file rijndael.c.

#define rotl (   x,
  n 
)    (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))

Definition at line 60 of file rijndael.c.

Referenced by gen_tabs().

#define rotr (   x,
  n 
)    (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))

Definition at line 59 of file rijndael.c.

#define star_x (   x  )     (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)

Definition at line 257 of file rijndael.c.

#define tab_gen   1

Definition at line 83 of file rijndael.c.

Referenced by gen_tabs(), and rijndael_set_key().


Function Documentation

void aes_cbc_decrypt ( rijndael_ctx ctx,
uint8 iva,
uint8 data,
unsigned  len 
)

Definition at line 567 of file rijndael.c.

References buf, and rijndael_decrypt().

Referenced by AES_cbc_encrypt(), and rj_decrypt().

{
    uint32     *d = (uint32 *) data;
    unsigned    bs = 16;
    uint32      buf[4],
                iv[4];

    memcpy(iv, iva, bs);
    while (len >= bs)
    {
        buf[0] = d[0];
        buf[1] = d[1];
        buf[2] = d[2];
        buf[3] = d[3];

        rijndael_decrypt(ctx, buf, d);

        d[0] ^= iv[0];
        d[1] ^= iv[1];
        d[2] ^= iv[2];
        d[3] ^= iv[3];

        iv[0] = buf[0];
        iv[1] = buf[1];
        iv[2] = buf[2];
        iv[3] = buf[3];
        d += 4;
        len -= bs;
    }
}

void aes_cbc_encrypt ( rijndael_ctx ctx,
uint8 iva,
uint8 data,
unsigned  len 
)

Definition at line 545 of file rijndael.c.

References rijndael_encrypt().

Referenced by AES_cbc_encrypt(), and rj_encrypt().

{
    uint32     *iv = (uint32 *) iva;
    uint32     *d = (uint32 *) data;
    unsigned    bs = 16;

    while (len >= bs)
    {
        d[0] ^= iv[0];
        d[1] ^= iv[1];
        d[2] ^= iv[2];
        d[3] ^= iv[3];

        rijndael_encrypt(ctx, d, d);

        iv = d;
        d += bs / 4;
        len -= bs;
    }
}

void aes_ecb_decrypt ( rijndael_ctx ctx,
uint8 data,
unsigned  len 
)

Definition at line 529 of file rijndael.c.

References rijndael_decrypt().

Referenced by AES_ecb_encrypt(), and rj_decrypt().

{
    unsigned    bs = 16;
    uint32     *d;

    while (len >= bs)
    {
        d = (uint32 *) data;
        rijndael_decrypt(ctx, d, d);

        len -= bs;
        data += bs;
    }
}

void aes_ecb_encrypt ( rijndael_ctx ctx,
uint8 data,
unsigned  len 
)

Definition at line 513 of file rijndael.c.

References rijndael_encrypt().

Referenced by AES_ecb_encrypt(), and rj_encrypt().

{
    unsigned    bs = 16;
    uint32     *d;

    while (len >= bs)
    {
        d = (uint32 *) data;
        rijndael_encrypt(ctx, d, d);

        len -= bs;
        data += bs;
    }
}

void aes_set_key ( rijndael_ctx ctx,
const uint8 key,
unsigned  keybits,
int  enc 
)

Definition at line 504 of file rijndael.c.

References rijndael_set_key().

Referenced by AES_set_decrypt_key(), AES_set_encrypt_key(), and rj_real_init().

{
    uint32     *k;

    k = (uint32 *) key;
    rijndael_set_key(ctx, k, keybits, enc);
}

static void gen_tabs ( void   )  [static]

Definition at line 157 of file rijndael.c.

References ff_mult, i, rotl, and tab_gen.

Referenced by rijndael_set_key().

{
#ifndef PRE_CALC_TABLES
    u4byte      i,
                t;
    u1byte      p,
                q;

    /* log and power tables for GF(2**8) finite field with  */
    /* 0x11b as modular polynomial - the simplest prmitive  */
    /* root is 0x11, used here to generate the tables       */

    for (i = 0, p = 1; i < 256; ++i)
    {
        pow_tab[i] = (u1byte) p;
        log_tab[p] = (u1byte) i;

        p = p ^ (p << 1) ^ (p & 0x80 ? 0x01b : 0);
    }

    log_tab[1] = 0;
    p = 1;

    for (i = 0; i < 10; ++i)
    {
        rco_tab[i] = p;

        p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);
    }

    /* note that the affine byte transformation matrix in   */
    /* rijndael specification is in big endian format with  */
    /* bit 0 as the most significant bit. In the remainder  */
    /* of the specification the bits are numbered from the  */
    /* least significant end of a byte.                     */

    for (i = 0; i < 256; ++i)
    {
        p = (i ? pow_tab[255 - log_tab[i]] : 0);
        q = p;
        q = (q >> 7) | (q << 1);
        p ^= q;
        q = (q >> 7) | (q << 1);
        p ^= q;
        q = (q >> 7) | (q << 1);
        p ^= q;
        q = (q >> 7) | (q << 1);
        p ^= q ^ 0x63;
        sbx_tab[i] = (u1byte) p;
        isb_tab[p] = (u1byte) i;
    }

    for (i = 0; i < 256; ++i)
    {
        p = sbx_tab[i];

#ifdef  LARGE_TABLES

        t = p;
        fl_tab[0][i] = t;
        fl_tab[1][i] = rotl(t, 8);
        fl_tab[2][i] = rotl(t, 16);
        fl_tab[3][i] = rotl(t, 24);
#endif
        t = ((u4byte) ff_mult(2, p)) |
            ((u4byte) p << 8) |
            ((u4byte) p << 16) |
            ((u4byte) ff_mult(3, p) << 24);

        ft_tab[0][i] = t;
        ft_tab[1][i] = rotl(t, 8);
        ft_tab[2][i] = rotl(t, 16);
        ft_tab[3][i] = rotl(t, 24);

        p = isb_tab[i];

#ifdef  LARGE_TABLES

        t = p;
        il_tab[0][i] = t;
        il_tab[1][i] = rotl(t, 8);
        il_tab[2][i] = rotl(t, 16);
        il_tab[3][i] = rotl(t, 24);
#endif
        t = ((u4byte) ff_mult(14, p)) |
            ((u4byte) ff_mult(9, p) << 8) |
            ((u4byte) ff_mult(13, p) << 16) |
            ((u4byte) ff_mult(11, p) << 24);

        it_tab[0][i] = t;
        it_tab[1][i] = rotl(t, 8);
        it_tab[2][i] = rotl(t, 16);
        it_tab[3][i] = rotl(t, 24);
    }

    tab_gen = 1;
#endif   /* !PRE_CALC_TABLES */
}

void rijndael_decrypt ( rijndael_ctx ctx,
const u4byte in_blk,
u4byte out_blk 
)

Definition at line 451 of file rijndael.c.

References _rijndael_ctx::d_key, _rijndael_ctx::e_key, i_lround, i_nround, io_swap, and _rijndael_ctx::k_len.

Referenced by aes_cbc_decrypt(), and aes_ecb_decrypt().

{
    u4byte      b0[4],
                b1[4],
               *kp;
    u4byte      k_len = ctx->k_len;
    u4byte     *e_key = ctx->e_key;
    u4byte     *d_key = ctx->d_key;

    b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
    b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
    b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
    b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];

    kp = d_key + 4 * (k_len + 5);

    if (k_len > 6)
    {
        i_nround(b1, b0, kp);
        i_nround(b0, b1, kp);
    }

    if (k_len > 4)
    {
        i_nround(b1, b0, kp);
        i_nround(b0, b1, kp);
    }

    i_nround(b1, b0, kp);
    i_nround(b0, b1, kp);
    i_nround(b1, b0, kp);
    i_nround(b0, b1, kp);
    i_nround(b1, b0, kp);
    i_nround(b0, b1, kp);
    i_nround(b1, b0, kp);
    i_nround(b0, b1, kp);
    i_nround(b1, b0, kp);
    i_lround(b0, b1, kp);

    out_blk[0] = io_swap(b0[0]);
    out_blk[1] = io_swap(b0[1]);
    out_blk[2] = io_swap(b0[2]);
    out_blk[3] = io_swap(b0[3]);
}

void rijndael_encrypt ( rijndael_ctx ctx,
const u4byte in_blk,
u4byte out_blk 
)

Definition at line 387 of file rijndael.c.

References _rijndael_ctx::e_key, f_lround, f_nround, io_swap, and _rijndael_ctx::k_len.

Referenced by aes_cbc_encrypt(), aes_ecb_encrypt(), and ciph_encrypt().

{
    u4byte      k_len = ctx->k_len;
    u4byte     *e_key = ctx->e_key;
    u4byte      b0[4],
                b1[4],
               *kp;

    b0[0] = io_swap(in_blk[0]) ^ e_key[0];
    b0[1] = io_swap(in_blk[1]) ^ e_key[1];
    b0[2] = io_swap(in_blk[2]) ^ e_key[2];
    b0[3] = io_swap(in_blk[3]) ^ e_key[3];

    kp = e_key + 4;

    if (k_len > 6)
    {
        f_nround(b1, b0, kp);
        f_nround(b0, b1, kp);
    }

    if (k_len > 4)
    {
        f_nround(b1, b0, kp);
        f_nround(b0, b1, kp);
    }

    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_nround(b0, b1, kp);
    f_nround(b1, b0, kp);
    f_lround(b0, b1, kp);

    out_blk[0] = io_swap(b0[0]);
    out_blk[1] = io_swap(b0[1]);
    out_blk[2] = io_swap(b0[2]);
    out_blk[3] = io_swap(b0[3]);
}

rijndael_ctx* rijndael_set_key ( rijndael_ctx ctx,
const u4byte in_key,
const u4byte  key_len,
int  encrypt 
)

Definition at line 305 of file rijndael.c.

References _rijndael_ctx::d_key, _rijndael_ctx::decrypt, _rijndael_ctx::e_key, gen_tabs(), i, imix_col, io_swap, _rijndael_ctx::k_len, loop4, loop6, loop8, and tab_gen.

Referenced by aes_set_key(), and ciph_init().

{
    u4byte      i,
                t,
                u,
                v,
                w;
    u4byte     *e_key = ctx->e_key;
    u4byte     *d_key = ctx->d_key;

    ctx->decrypt = !encrypt;

    if (!tab_gen)
        gen_tabs();

    ctx->k_len = (key_len + 31) / 32;

    e_key[0] = io_swap(in_key[0]);
    e_key[1] = io_swap(in_key[1]);
    e_key[2] = io_swap(in_key[2]);
    e_key[3] = io_swap(in_key[3]);

    switch (ctx->k_len)
    {
        case 4:
            t = e_key[3];
            for (i = 0; i < 10; ++i)
                loop4(i);
            break;

        case 6:
            e_key[4] = io_swap(in_key[4]);
            t = e_key[5] = io_swap(in_key[5]);
            for (i = 0; i < 8; ++i)
                loop6(i);
            break;

        case 8:
            e_key[4] = io_swap(in_key[4]);
            e_key[5] = io_swap(in_key[5]);
            e_key[6] = io_swap(in_key[6]);
            t = e_key[7] = io_swap(in_key[7]);
            for (i = 0; i < 7; ++i)
                loop8(i);
            break;
    }

    if (!encrypt)
    {
        d_key[0] = e_key[0];
        d_key[1] = e_key[1];
        d_key[2] = e_key[2];
        d_key[3] = e_key[3];

        for (i = 4; i < 4 * ctx->k_len + 24; ++i)
            imix_col(d_key[i], e_key[i]);
    }

    return ctx;
}