Header And Logo

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

Data Structures | Defines | Typedefs | Functions | Variables

openssl.c File Reference

#include "postgres.h"
#include "px.h"
#include <openssl/evp.h>
#include <openssl/blowfish.h>
#include <openssl/cast.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include "rijndael.c"
#include "sha2.c"
#include "internal-sha2.c"
Include dependency graph for openssl.c:

Go to the source code of this file.

Data Structures

struct  OSSLDigest
struct  ossl_cipher
struct  ossldata
struct  ossl_cipher_lookup

Defines

#define MAX_KEY   (512/8)
#define MAX_IV   (128/8)
#define AES_ENCRYPT   1
#define AES_DECRYPT   0
#define AES_KEY   rijndael_ctx
#define DES_key_schedule   des_key_schedule
#define DES_cblock   des_cblock
#define DES_set_key(k, ks)   des_set_key((k), *(ks))
#define DES_ecb_encrypt(i, o, k, e)   des_ecb_encrypt((i), (o), *(k), (e))
#define DES_ncbc_encrypt(i, o, l, k, iv, e)   des_ncbc_encrypt((i), (o), (l), *(k), (iv), (e))
#define DES_ecb3_encrypt(i, o, k1, k2, k3, e)
#define DES_ede3_cbc_encrypt(i, o, l, k1, k2, k3, iv, e)

Typedefs

typedef void(* init_f )(PX_MD *md)
typedef struct OSSLDigest OSSLDigest

Functions

static int AES_set_encrypt_key (const uint8 *key, int kbits, AES_KEY *ctx)
static int AES_set_decrypt_key (const uint8 *key, int kbits, AES_KEY *ctx)
static void AES_ecb_encrypt (const uint8 *src, uint8 *dst, AES_KEY *ctx, int enc)
static void AES_cbc_encrypt (const uint8 *src, uint8 *dst, int len, AES_KEY *ctx, uint8 *iv, int enc)
static void EVP_MD_CTX_init (EVP_MD_CTX *ctx)
static int EVP_MD_CTX_cleanup (EVP_MD_CTX *ctx)
static int EVP_DigestInit_ex (EVP_MD_CTX *ctx, const EVP_MD *md, void *engine)
static int EVP_DigestFinal_ex (EVP_MD_CTX *ctx, unsigned char *res, unsigned int *len)
static int compat_find_digest (const char *name, PX_MD **res)
static unsigned digest_result_size (PX_MD *h)
static unsigned digest_block_size (PX_MD *h)
static void digest_reset (PX_MD *h)
static void digest_update (PX_MD *h, const uint8 *data, unsigned dlen)
static void digest_finish (PX_MD *h, uint8 *dst)
static void digest_free (PX_MD *h)
int px_find_digest (const char *name, PX_MD **res)
static unsigned gen_ossl_block_size (PX_Cipher *c)
static unsigned gen_ossl_key_size (PX_Cipher *c)
static unsigned gen_ossl_iv_size (PX_Cipher *c)
static void gen_ossl_free (PX_Cipher *c)
static int bf_check_supported_key_len (void)
static int bf_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
static int bf_ecb_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int bf_ecb_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int bf_cbc_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int bf_cbc_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int bf_cfb64_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int bf_cfb64_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
static int ossl_des_ecb_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des_ecb_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des_cbc_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des_cbc_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des3_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
static int ossl_des3_ecb_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des3_ecb_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des3_cbc_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_des3_cbc_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_cast_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
static int ossl_cast_ecb_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_cast_ecb_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_cast_cbc_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_cast_cbc_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_aes_init (PX_Cipher *c, const uint8 *key, unsigned klen, const uint8 *iv)
static int ossl_aes_key_init (ossldata *od, int type)
static int ossl_aes_ecb_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_aes_ecb_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_aes_cbc_encrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
static int ossl_aes_cbc_decrypt (PX_Cipher *c, const uint8 *data, unsigned dlen, uint8 *res)
int px_find_cipher (const char *name, PX_Cipher **res)
static void init_openssl_rand (void)
int px_get_random_bytes (uint8 *dst, unsigned count)
int px_get_pseudo_random_bytes (uint8 *dst, unsigned count)
int px_add_entropy (const uint8 *data, unsigned count)

Variables

static int px_openssl_initialized = 0
static PX_Alias ossl_aliases []
static struct ossl_cipher ossl_bf_cbc
static struct ossl_cipher ossl_bf_ecb
static struct ossl_cipher ossl_bf_cfb
static struct ossl_cipher ossl_des_ecb
static struct ossl_cipher ossl_des_cbc
static struct ossl_cipher ossl_des3_ecb
static struct ossl_cipher ossl_des3_cbc
static struct ossl_cipher ossl_cast_ecb
static struct ossl_cipher ossl_cast_cbc
static struct ossl_cipher ossl_aes_ecb
static struct ossl_cipher ossl_aes_cbc
static struct ossl_cipher_lookup ossl_cipher_types []
static int openssl_random_init = 0

Define Documentation

#define AES_DECRYPT   0

Definition at line 70 of file openssl.c.

Referenced by ossl_aes_cbc_decrypt(), and ossl_aes_ecb_decrypt().

#define AES_ENCRYPT   1

Definition at line 69 of file openssl.c.

Referenced by ossl_aes_cbc_encrypt(), ossl_aes_ecb_encrypt(), and ossl_aes_key_init().

#define AES_KEY   rijndael_ctx

Definition at line 71 of file openssl.c.

#define DES_cblock   des_cblock
#define DES_ecb3_encrypt (   i,
  o,
  k1,
  k2,
  k3,
  e 
)
Value:
des_ecb3_encrypt((des_cblock *)(i), (des_cblock *)(o), \
                *(k1), *(k2), *(k3), (e))

Definition at line 125 of file openssl.c.

Referenced by ossl_des3_ecb_decrypt(), and ossl_des3_ecb_encrypt().

#define DES_ecb_encrypt (   i,
  o,
  k,
  e 
)    des_ecb_encrypt((i), (o), *(k), (e))

Definition at line 121 of file openssl.c.

Referenced by ossl_des_ecb_decrypt(), and ossl_des_ecb_encrypt().

#define DES_ede3_cbc_encrypt (   i,
  o,
  l,
  k1,
  k2,
  k3,
  iv,
  e 
)
Value:
des_ede3_cbc_encrypt((i), (o), \
                (l), *(k1), *(k2), *(k3), (iv), (e))

Definition at line 128 of file openssl.c.

Referenced by ossl_des3_cbc_decrypt(), and ossl_des3_cbc_encrypt().

#define DES_key_schedule   des_key_schedule

Definition at line 117 of file openssl.c.

#define DES_ncbc_encrypt (   i,
  o,
  l,
  k,
  iv,
  e 
)    des_ncbc_encrypt((i), (o), (l), *(k), (iv), (e))

Definition at line 123 of file openssl.c.

Referenced by ossl_des_cbc_decrypt(), and ossl_des_cbc_encrypt().

#define DES_set_key (   k,
  ks 
)    des_set_key((k), *(ks))

Definition at line 119 of file openssl.c.

Referenced by ossl_des3_init(), and ossl_des_init().

#define MAX_IV   (128/8)

Definition at line 47 of file openssl.c.

#define MAX_KEY   (512/8)

Definition at line 46 of file openssl.c.


Typedef Documentation

typedef void(* init_f)(PX_MD *md)

Definition at line 172 of file openssl.c.

typedef struct OSSLDigest OSSLDigest

Function Documentation

static void AES_cbc_encrypt ( const uint8 src,
uint8 dst,
int  len,
AES_KEY *  ctx,
uint8 iv,
int  enc 
) [static]

Definition at line 98 of file openssl.c.

References aes_cbc_decrypt(), and aes_cbc_encrypt().

Referenced by ossl_aes_cbc_decrypt(), and ossl_aes_cbc_encrypt().

{
    memcpy(dst, src, len);
    if (enc)
    {
        aes_cbc_encrypt(ctx, iv, dst, len);
        memcpy(iv, dst + len - 16, 16);
    }
    else
    {
        aes_cbc_decrypt(ctx, iv, dst, len);
        memcpy(iv, src + len - 16, 16);
    }
}

static void AES_ecb_encrypt ( const uint8 src,
uint8 dst,
AES_KEY *  ctx,
int  enc 
) [static]

Definition at line 88 of file openssl.c.

References aes_ecb_decrypt(), and aes_ecb_encrypt().

Referenced by ossl_aes_ecb_decrypt(), and ossl_aes_ecb_encrypt().

{
    memcpy(dst, src, 16);
    if (enc)
        aes_ecb_encrypt(ctx, dst, 16);
    else
        aes_ecb_decrypt(ctx, dst, 16);
}

static int AES_set_decrypt_key ( const uint8 key,
int  kbits,
AES_KEY *  ctx 
) [static]

Definition at line 81 of file openssl.c.

References aes_set_key().

Referenced by ossl_aes_key_init().

{
    aes_set_key(ctx, key, kbits, 0);
    return 0;
}

static int AES_set_encrypt_key ( const uint8 key,
int  kbits,
AES_KEY *  ctx 
) [static]

Definition at line 74 of file openssl.c.

References aes_set_key().

Referenced by ossl_aes_key_init().

{
    aes_set_key(ctx, key, kbits, 1);
    return 0;
}

static int bf_cbc_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 486 of file openssl.c.

References ossldata::bf, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    BF_cbc_encrypt(data, res, dlen, &od->u.bf.key, od->iv, BF_DECRYPT);
    return 0;
}

static int bf_cbc_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 477 of file openssl.c.

References ossldata::bf, BF_ENCRYPT, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    BF_cbc_encrypt(data, res, dlen, &od->u.bf.key, od->iv, BF_ENCRYPT);
    return 0;
}

static int bf_cfb64_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 505 of file openssl.c.

References ossldata::bf, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    BF_cfb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv,
                     &od->u.bf.num, BF_DECRYPT);
    return 0;
}

static int bf_cfb64_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 495 of file openssl.c.

References ossldata::bf, BF_ENCRYPT, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    BF_cfb64_encrypt(data, res, dlen, &od->u.bf.key, od->iv,
                     &od->u.bf.num, BF_ENCRYPT);
    return 0;
}

static int bf_check_supported_key_len ( void   )  [static]

Definition at line 397 of file openssl.c.

References BF_ENCRYPT, BF_set_key(), and memcmp().

Referenced by bf_init().

{
    static const uint8 key[56] = {
        0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69,
        0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f, 0x00, 0x11, 0x22, 0x33,
        0x44, 0x55, 0x66, 0x77, 0x04, 0x68, 0x91, 0x04, 0xc2, 0xfd,
        0x3b, 0x2f, 0x58, 0x40, 0x23, 0x64, 0x1a, 0xba, 0x61, 0x76,
        0x1f, 0x1f, 0x1f, 0x1f, 0x0e, 0x0e, 0x0e, 0x0e, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    };

    static const uint8 data[8] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
    static const uint8 res[8] = {0xc0, 0x45, 0x04, 0x01, 0x2e, 0x4e, 0x1f, 0x53};
    static uint8 out[8];

    BF_KEY      bf_key;

    /* encrypt with 448bits key and verify output */
    BF_set_key(&bf_key, 56, key);
    BF_ecb_encrypt(data, out, &bf_key, BF_ENCRYPT);

    if (memcmp(out, res, 8) != 0)
        return 0;               /* Output does not match -> strong cipher is
                                 * not supported */
    return 1;
}

static int bf_ecb_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 465 of file openssl.c.

References ossldata::bf, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c),
                i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        BF_ecb_encrypt(data + i * bs, res + i * bs, &od->u.bf.key, BF_DECRYPT);
    return 0;
}

static int bf_ecb_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 453 of file openssl.c.

References ossldata::bf, BF_ENCRYPT, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    unsigned    i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        BF_ecb_encrypt(data + i * bs, res + i * bs, &od->u.bf.key, BF_ENCRYPT);
    return 0;
}

static int bf_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
) [static]

Definition at line 425 of file openssl.c.

References ossldata::bf, bf_check_supported_key_len(), BF_set_key(), ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    static int  bf_is_strong = -1;

    /*
     * Test if key len is supported. BF_set_key silently cut large keys and it
     * could be be a problem when user transfer crypted data from one server
     * to another.
     */

    if (bf_is_strong == -1)
        bf_is_strong = bf_check_supported_key_len();

    if (!bf_is_strong && klen > 16)
        return PXE_KEY_TOO_BIG;

    /* Key len is supported. We can use it. */
    BF_set_key(&od->u.bf.key, klen, key);
    if (iv)
        memcpy(od->iv, iv, BF_BLOCK);
    else
        memset(od->iv, 0, BF_BLOCK);
    od->u.bf.num = 0;
    return 0;
}

static int compat_find_digest ( const char *  name,
PX_MD **  res 
) [static]

Definition at line 175 of file openssl.c.

References init(), pg_strcasecmp(), and px_alloc.

Referenced by px_find_digest().

{
    init_f      init = NULL;

    if (pg_strcasecmp(name, "sha224") == 0)
        init = init_sha224;
    else if (pg_strcasecmp(name, "sha256") == 0)
        init = init_sha256;
    else if (pg_strcasecmp(name, "sha384") == 0)
        init = init_sha384;
    else if (pg_strcasecmp(name, "sha512") == 0)
        init = init_sha512;
    else
        return PXE_NO_HASH;

    *res = px_alloc(sizeof(PX_MD));
    init(*res);
    return 0;
}

static unsigned digest_block_size ( PX_MD h  )  [static]

Definition at line 217 of file openssl.c.

References OSSLDigest::ctx, px_digest::p, and px_digest::ptr.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    return EVP_MD_CTX_block_size(&digest->ctx);
}

static void digest_finish ( PX_MD h,
uint8 dst 
) [static]

Definition at line 241 of file openssl.c.

References OSSLDigest::ctx, EVP_DigestFinal_ex(), NULL, px_digest::p, and px_digest::ptr.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    EVP_DigestFinal_ex(&digest->ctx, dst, NULL);
}

static void digest_free ( PX_MD h  )  [static]

Definition at line 249 of file openssl.c.

References OSSLDigest::ctx, EVP_MD_CTX_cleanup(), px_digest::p, px_digest::ptr, and px_free.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    EVP_MD_CTX_cleanup(&digest->ctx);

    px_free(digest);
    px_free(h);
}

static void digest_reset ( PX_MD h  )  [static]

Definition at line 225 of file openssl.c.

References OSSLDigest::algo, OSSLDigest::ctx, EVP_DigestInit_ex(), NULL, px_digest::p, and px_digest::ptr.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    EVP_DigestInit_ex(&digest->ctx, digest->algo, NULL);
}

static unsigned digest_result_size ( PX_MD h  )  [static]

Definition at line 209 of file openssl.c.

References OSSLDigest::ctx, px_digest::p, and px_digest::ptr.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    return EVP_MD_CTX_size(&digest->ctx);
}

static void digest_update ( PX_MD h,
const uint8 data,
unsigned  dlen 
) [static]

Definition at line 233 of file openssl.c.

References OSSLDigest::ctx, px_digest::p, and px_digest::ptr.

{
    OSSLDigest *digest = (OSSLDigest *) h->p.ptr;

    EVP_DigestUpdate(&digest->ctx, data, dlen);
}

static int EVP_DigestFinal_ex ( EVP_MD_CTX *  ctx,
unsigned char *  res,
unsigned int *  len 
) [static]

Definition at line 157 of file openssl.c.

Referenced by digest_finish().

{
    EVP_DigestFinal(ctx, res, len);
    return 1;
}

static int EVP_DigestInit_ex ( EVP_MD_CTX *  ctx,
const EVP_MD *  md,
void *  engine 
) [static]

Definition at line 150 of file openssl.c.

Referenced by digest_reset(), and px_find_digest().

{
    EVP_DigestInit(ctx, md);
    return 1;
}

static int EVP_MD_CTX_cleanup ( EVP_MD_CTX *  ctx  )  [static]

Definition at line 143 of file openssl.c.

Referenced by digest_free().

{
    memset(ctx, 0, sizeof(*ctx));
    return 1;
}

static void EVP_MD_CTX_init ( EVP_MD_CTX *  ctx  )  [static]

Definition at line 137 of file openssl.c.

Referenced by px_find_digest().

{
    memset(ctx, 0, sizeof(*ctx));
}

static unsigned gen_ossl_block_size ( PX_Cipher c  )  [static]
static void gen_ossl_free ( PX_Cipher c  )  [static]

Definition at line 380 of file openssl.c.

References px_cipher::ptr, and px_free.

{
    ossldata   *od = (ossldata *) c->ptr;

    memset(od, 0, sizeof(*od));
    px_free(od);
    px_free(c);
}

static unsigned gen_ossl_iv_size ( PX_Cipher c  )  [static]

Definition at line 370 of file openssl.c.

References ossl_cipher::block_size, ossldata::ciph, and px_cipher::ptr.

{
    unsigned    ivlen;
    ossldata   *od = (ossldata *) c->ptr;

    ivlen = od->ciph->block_size;
    return ivlen;
}

static unsigned gen_ossl_key_size ( PX_Cipher c  )  [static]

Definition at line 362 of file openssl.c.

References ossldata::ciph, ossl_cipher::max_key_size, and px_cipher::ptr.

{
    ossldata   *od = (ossldata *) c->ptr;

    return od->ciph->max_key_size;
}

static void init_openssl_rand ( void   )  [static]

Definition at line 987 of file openssl.c.

References NULL, and openssl_random_init.

Referenced by px_get_pseudo_random_bytes(), and px_get_random_bytes().

{
    if (RAND_get_rand_method() == NULL)
        RAND_set_rand_method(RAND_SSLeay());
    openssl_random_init = 1;
}

static int ossl_aes_cbc_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 830 of file openssl.c.

References AES_cbc_encrypt(), AES_DECRYPT, ossldata::aes_key, ossldata::init, ossldata::iv, ossl_aes_key_init(), px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    int         err;

    if (!od->init)
        if ((err = ossl_aes_key_init(od, AES_DECRYPT)) != 0)
            return err;

    AES_cbc_encrypt(data, res, dlen, &od->u.aes_key, od->iv, AES_DECRYPT);
    return 0;
}

static int ossl_aes_cbc_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 815 of file openssl.c.

References AES_cbc_encrypt(), AES_ENCRYPT, ossldata::aes_key, ossldata::init, ossldata::iv, ossl_aes_key_init(), px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    int         err;

    if (!od->init)
        if ((err = ossl_aes_key_init(od, AES_ENCRYPT)) != 0)
            return err;

    AES_cbc_encrypt(data, res, dlen, &od->u.aes_key, od->iv, AES_ENCRYPT);
    return 0;
}

static int ossl_aes_ecb_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 797 of file openssl.c.

References AES_DECRYPT, AES_ecb_encrypt(), ossldata::aes_key, end, gen_ossl_block_size(), ossldata::init, ossl_aes_key_init(), px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    ossldata   *od = c->ptr;
    const uint8 *end = data + dlen - bs;
    int         err;

    if (!od->init)
        if ((err = ossl_aes_key_init(od, AES_DECRYPT)) != 0)
            return err;

    for (; data <= end; data += bs, res += bs)
        AES_ecb_encrypt(data, res, &od->u.aes_key, AES_DECRYPT);
    return 0;
}

static int ossl_aes_ecb_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 779 of file openssl.c.

References AES_ecb_encrypt(), AES_ENCRYPT, ossldata::aes_key, end, gen_ossl_block_size(), ossldata::init, ossl_aes_key_init(), px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    ossldata   *od = c->ptr;
    const uint8 *end = data + dlen - bs;
    int         err;

    if (!od->init)
        if ((err = ossl_aes_key_init(od, AES_ENCRYPT)) != 0)
            return err;

    for (; data <= end; data += bs, res += bs)
        AES_ecb_encrypt(data, res, &od->u.aes_key, AES_ENCRYPT);
    return 0;
}

static int ossl_aes_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
) [static]

Definition at line 732 of file openssl.c.

References gen_ossl_block_size(), ossldata::iv, ossldata::key, ossldata::klen, and px_cipher::ptr.

{
    ossldata   *od = c->ptr;
    unsigned    bs = gen_ossl_block_size(c);

    if (klen <= 128 / 8)
        od->klen = 128 / 8;
    else if (klen <= 192 / 8)
        od->klen = 192 / 8;
    else if (klen <= 256 / 8)
        od->klen = 256 / 8;
    else
        return PXE_KEY_TOO_BIG;

    memcpy(od->key, key, klen);

    if (iv)
        memcpy(od->iv, iv, bs);
    else
        memset(od->iv, 0, bs);
    return 0;
}

static int ossl_aes_key_init ( ossldata od,
int  type 
) [static]

Definition at line 756 of file openssl.c.

References AES_ENCRYPT, ossldata::aes_key, AES_set_decrypt_key(), AES_set_encrypt_key(), ossldata::init, ossldata::key, ossldata::klen, and ossldata::u.

Referenced by ossl_aes_cbc_decrypt(), ossl_aes_cbc_encrypt(), ossl_aes_ecb_decrypt(), and ossl_aes_ecb_encrypt().

{
    int         err;

    /*
     * Strong key support could be missing on some openssl installations. We
     * must check return value from set key function.
     */
    if (type == AES_ENCRYPT)
        err = AES_set_encrypt_key(od->key, od->klen * 8, &od->u.aes_key);
    else
        err = AES_set_decrypt_key(od->key, od->klen * 8, &od->u.aes_key);

    if (err == 0)
    {
        od->init = 1;
        return 0;
    }
    od->init = 0;
    return PXE_KEY_TOO_BIG;
}

static int ossl_cast_cbc_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 721 of file openssl.c.

References ossldata::cast_key, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    CAST_cbc_encrypt(data, res, dlen, &od->u.cast_key, od->iv, CAST_DECRYPT);
    return 0;
}

static int ossl_cast_cbc_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 712 of file openssl.c.

References ossldata::cast_key, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    CAST_cbc_encrypt(data, res, dlen, &od->u.cast_key, od->iv, CAST_ENCRYPT);
    return 0;
}

static int ossl_cast_ecb_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 700 of file openssl.c.

References ossldata::cast_key, end, gen_ossl_block_size(), px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    ossldata   *od = c->ptr;
    const uint8 *end = data + dlen - bs;

    for (; data <= end; data += bs, res += bs)
        CAST_ecb_encrypt(data, res, &od->u.cast_key, CAST_DECRYPT);
    return 0;
}

static int ossl_cast_ecb_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 688 of file openssl.c.

References ossldata::cast_key, end, gen_ossl_block_size(), px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    ossldata   *od = c->ptr;
    const uint8 *end = data + dlen - bs;

    for (; data <= end; data += bs, res += bs)
        CAST_ecb_encrypt(data, res, &od->u.cast_key, CAST_ENCRYPT);
    return 0;
}

static int ossl_cast_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
) [static]

Definition at line 674 of file openssl.c.

References ossldata::cast_key, gen_ossl_block_size(), ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    unsigned    bs = gen_ossl_block_size(c);

    CAST_set_key(&od->u.cast_key, klen, key);
    if (iv)
        memcpy(od->iv, iv, bs);
    else
        memset(od->iv, 0, bs);
    return 0;
}

static int ossl_des3_cbc_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 660 of file openssl.c.

References ossldata::des3, DES_cblock, DES_ede3_cbc_encrypt, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    DES_ede3_cbc_encrypt(data, res, dlen,
                         &od->u.des3.k1, &od->u.des3.k2, &od->u.des3.k3,
                         (DES_cblock *) od->iv, 0);
    return 0;
}

static int ossl_des3_cbc_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 648 of file openssl.c.

References ossldata::des3, DES_cblock, DES_ede3_cbc_encrypt, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    DES_ede3_cbc_encrypt(data, res, dlen,
                         &od->u.des3.k1, &od->u.des3.k2, &od->u.des3.k3,
                         (DES_cblock *) od->iv, 1);
    return 0;
}

static int ossl_des3_ecb_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 634 of file openssl.c.

References ossldata::des3, DES_ecb3_encrypt, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    unsigned    i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        DES_ecb3_encrypt((void *) (data + i * bs), (void *) (res + i * bs),
                         &od->u.des3.k1, &od->u.des3.k2, &od->u.des3.k3, 0);
    return 0;
}

static int ossl_des3_ecb_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 620 of file openssl.c.

References ossldata::des3, DES_ecb3_encrypt, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    unsigned    i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        DES_ecb3_encrypt((void *) (data + i * bs), (void *) (res + i * bs),
                         &od->u.des3.k1, &od->u.des3.k2, &od->u.des3.k3, 1);
    return 0;
}

static int ossl_des3_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
) [static]

Definition at line 589 of file openssl.c.

References ossldata::des3, DES_cblock, DES_set_key, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    DES_cblock  xkey1,
                xkey2,
                xkey3;

    memset(&xkey1, 0, sizeof(xkey1));
    memset(&xkey2, 0, sizeof(xkey2));
    memset(&xkey3, 0, sizeof(xkey3));
    memcpy(&xkey1, key, klen > 8 ? 8 : klen);
    if (klen > 8)
        memcpy(&xkey2, key + 8, (klen - 8) > 8 ? 8 : (klen - 8));
    if (klen > 16)
        memcpy(&xkey3, key + 16, (klen - 16) > 8 ? 8 : (klen - 16));

    DES_set_key(&xkey1, &od->u.des3.k1);
    DES_set_key(&xkey2, &od->u.des3.k2);
    DES_set_key(&xkey3, &od->u.des3.k3);
    memset(&xkey1, 0, sizeof(xkey1));
    memset(&xkey2, 0, sizeof(xkey2));
    memset(&xkey3, 0, sizeof(xkey3));

    if (iv)
        memcpy(od->iv, iv, 8);
    else
        memset(od->iv, 0, 8);
    return 0;
}

static int ossl_des_cbc_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 576 of file openssl.c.

References ossldata::des, DES_cblock, DES_ncbc_encrypt, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    DES_ncbc_encrypt(data, res, dlen, &od->u.des.key_schedule,
                     (DES_cblock *) od->iv, 0);
    return 0;
}

static int ossl_des_cbc_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 565 of file openssl.c.

References ossldata::des, DES_cblock, DES_ncbc_encrypt, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;

    DES_ncbc_encrypt(data, res, dlen, &od->u.des.key_schedule,
                     (DES_cblock *) od->iv, 1);
    return 0;
}

static int ossl_des_ecb_decrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 550 of file openssl.c.

References ossldata::des, DES_cblock, DES_ecb_encrypt, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    unsigned    i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        DES_ecb_encrypt((DES_cblock *) (data + i * bs),
                        (DES_cblock *) (res + i * bs),
                        &od->u.des.key_schedule, 0);
    return 0;
}

static int ossl_des_ecb_encrypt ( PX_Cipher c,
const uint8 data,
unsigned  dlen,
uint8 res 
) [static]

Definition at line 535 of file openssl.c.

References ossldata::des, DES_cblock, DES_ecb_encrypt, gen_ossl_block_size(), i, px_cipher::ptr, and ossldata::u.

{
    unsigned    bs = gen_ossl_block_size(c);
    unsigned    i;
    ossldata   *od = c->ptr;

    for (i = 0; i < dlen / bs; i++)
        DES_ecb_encrypt((DES_cblock *) (data + i * bs),
                        (DES_cblock *) (res + i * bs),
                        &od->u.des.key_schedule, 1);
    return 0;
}

static int ossl_des_init ( PX_Cipher c,
const uint8 key,
unsigned  klen,
const uint8 iv 
) [static]

Definition at line 517 of file openssl.c.

References ossldata::des, DES_cblock, DES_set_key, ossldata::iv, px_cipher::ptr, and ossldata::u.

{
    ossldata   *od = c->ptr;
    DES_cblock  xkey;

    memset(&xkey, 0, sizeof(xkey));
    memcpy(&xkey, key, klen > 8 ? 8 : klen);
    DES_set_key(&xkey, &od->u.des.key_schedule);
    memset(&xkey, 0, sizeof(xkey));

    if (iv)
        memcpy(od->iv, iv, 8);
    else
        memset(od->iv, 0, 8);
    return 0;
}

int px_add_entropy ( const uint8 data,
unsigned  count 
)

Definition at line 1025 of file openssl.c.

References fortuna_add_entropy(), and system_reseed().

{
    /*
     * estimate 0 bits
     */
    RAND_add(data, count, 0);
    return 0;
}

int px_find_cipher ( const char *  name,
PX_Cipher **  res 
)

Definition at line 948 of file openssl.c.

References px_cipher::block_size, ossl_cipher_lookup::ciph, ossldata::ciph, ossl_cipher::decrypt, px_cipher::decrypt, ossl_cipher::encrypt, px_cipher::encrypt, px_cipher::free, i, ossl_cipher::init, px_cipher::init, px_cipher::iv_size, px_cipher::key_size, int_cipher::load, ossl_cipher_lookup::name, int_cipher::name, NULL, px_cipher::ptr, px_alloc, and px_resolve_alias().

{
    const struct ossl_cipher_lookup *i;
    PX_Cipher  *c = NULL;
    ossldata   *od;

    name = px_resolve_alias(ossl_aliases, name);
    for (i = ossl_cipher_types; i->name; i++)
        if (strcmp(i->name, name) == 0)
            break;
    if (i->name == NULL)
        return PXE_NO_CIPHER;

    od = px_alloc(sizeof(*od));
    memset(od, 0, sizeof(*od));
    od->ciph = i->ciph;

    c = px_alloc(sizeof(*c));
    c->block_size = gen_ossl_block_size;
    c->key_size = gen_ossl_key_size;
    c->iv_size = gen_ossl_iv_size;
    c->free = gen_ossl_free;
    c->init = od->ciph->init;
    c->encrypt = od->ciph->encrypt;
    c->decrypt = od->ciph->decrypt;
    c->ptr = od;

    *res = c;
    return 0;
}

int px_find_digest ( const char *  name,
PX_MD **  res 
)

Definition at line 264 of file openssl.c.

References OSSLDigest::algo, px_digest::block_size, compat_find_digest(), OSSLDigest::ctx, EVP_DigestInit_ex(), EVP_MD_CTX_init(), px_digest::finish, px_digest::free, int_digest::init, int_digest::name, NULL, px_digest::p, pg_strcasecmp(), px_digest::ptr, px_alloc, px_openssl_initialized, px_digest::reset, px_digest::result_size, and px_digest::update.

{
    const EVP_MD *md;
    PX_MD      *h;
    OSSLDigest *digest;

    if (!px_openssl_initialized)
    {
        px_openssl_initialized = 1;
        OpenSSL_add_all_algorithms();
    }

    md = EVP_get_digestbyname(name);
    if (md == NULL)
        return compat_find_digest(name, res);

    digest = px_alloc(sizeof(*digest));
    digest->algo = md;

    EVP_MD_CTX_init(&digest->ctx);
    if (EVP_DigestInit_ex(&digest->ctx, digest->algo, NULL) == 0)
        return -1;

    h = px_alloc(sizeof(*h));
    h->result_size = digest_result_size;
    h->block_size = digest_block_size;
    h->reset = digest_reset;
    h->update = digest_update;
    h->finish = digest_finish;
    h->free = digest_free;
    h->p.ptr = (void *) digest;

    *res = h;
    return 0;
}

int px_get_pseudo_random_bytes ( uint8 dst,
unsigned  count 
)

Definition at line 1010 of file openssl.c.

References init_openssl_rand(), openssl_random_init, and px_get_random_bytes().

{
    int         res;

    if (!openssl_random_init)
        init_openssl_rand();

    res = RAND_pseudo_bytes(dst, count);
    if (res == 0 || res == 1)
        return count;

    return PXE_OSSL_RAND_ERROR;
}

int px_get_random_bytes ( uint8 dst,
unsigned  count 
)

Definition at line 995 of file openssl.c.

References fortuna_get_bytes(), init_openssl_rand(), openssl_random_init, and system_reseed().

{
    int         res;

    if (!openssl_random_init)
        init_openssl_rand();

    res = RAND_bytes(dst, count);
    if (res == 1)
        return count;

    return PXE_OSSL_RAND_ERROR;
}


Variable Documentation

int openssl_random_init = 0 [static]

Definition at line 980 of file openssl.c.

Referenced by init_openssl_rand(), px_get_pseudo_random_bytes(), and px_get_random_bytes().

struct ossl_cipher ossl_aes_cbc [static]
Initial value:
 {
    ossl_aes_init, ossl_aes_cbc_encrypt, ossl_aes_cbc_decrypt,
    128 / 8, 256 / 8, 0
}

Definition at line 916 of file openssl.c.

struct ossl_cipher ossl_aes_ecb [static]
Initial value:
 {
    ossl_aes_init, ossl_aes_ecb_encrypt, ossl_aes_ecb_decrypt,
    128 / 8, 256 / 8, 0
}

Definition at line 911 of file openssl.c.

PX_Alias ossl_aliases[] [static]
Initial value:
 {
    {"bf", "bf-cbc"},
    {"blowfish", "bf-cbc"},
    {"blowfish-cbc", "bf-cbc"},
    {"blowfish-ecb", "bf-ecb"},
    {"blowfish-cfb", "bf-cfb"},
    {"des", "des-cbc"},
    {"3des", "des3-cbc"},
    {"3des-ecb", "des3-ecb"},
    {"3des-cbc", "des3-cbc"},
    {"cast5", "cast5-cbc"},
    {"aes", "aes-cbc"},
    {"rijndael", "aes-cbc"},
    {"rijndael-cbc", "aes-cbc"},
    {"rijndael-ecb", "aes-ecb"},
    {NULL}
}

Definition at line 848 of file openssl.c.

struct ossl_cipher ossl_bf_cbc [static]
Initial value:
 {
    bf_init, bf_cbc_encrypt, bf_cbc_decrypt,
    64 / 8, 448 / 8, 0
}

Definition at line 866 of file openssl.c.

struct ossl_cipher ossl_bf_cfb [static]
Initial value:
 {
    bf_init, bf_cfb64_encrypt, bf_cfb64_decrypt,
    64 / 8, 448 / 8, 1
}

Definition at line 876 of file openssl.c.

struct ossl_cipher ossl_bf_ecb [static]
Initial value:
 {
    bf_init, bf_ecb_encrypt, bf_ecb_decrypt,
    64 / 8, 448 / 8, 0
}

Definition at line 871 of file openssl.c.

struct ossl_cipher ossl_cast_cbc [static]
Initial value:
 {
    ossl_cast_init, ossl_cast_cbc_encrypt, ossl_cast_cbc_decrypt,
    64 / 8, 128 / 8, 0
}

Definition at line 906 of file openssl.c.

struct ossl_cipher ossl_cast_ecb [static]
Initial value:
 {
    ossl_cast_init, ossl_cast_ecb_encrypt, ossl_cast_ecb_decrypt,
    64 / 8, 128 / 8, 0
}

Definition at line 901 of file openssl.c.

Initial value:
 {
    {"bf-cbc", &ossl_bf_cbc},
    {"bf-ecb", &ossl_bf_ecb},
    {"bf-cfb", &ossl_bf_cfb},
    {"des-ecb", &ossl_des_ecb},
    {"des-cbc", &ossl_des_cbc},
    {"des3-ecb", &ossl_des3_ecb},
    {"des3-cbc", &ossl_des3_cbc},
    {"cast5-ecb", &ossl_cast_ecb},
    {"cast5-cbc", &ossl_cast_cbc},
    {"aes-ecb", &ossl_aes_ecb},
    {"aes-cbc", &ossl_aes_cbc},
    {NULL}
}

Definition at line 930 of file openssl.c.

struct ossl_cipher ossl_des3_cbc [static]
Initial value:
 {
    ossl_des3_init, ossl_des3_cbc_encrypt, ossl_des3_cbc_decrypt,
    64 / 8, 192 / 8, 0
}

Definition at line 896 of file openssl.c.

struct ossl_cipher ossl_des3_ecb [static]
Initial value:
 {
    ossl_des3_init, ossl_des3_ecb_encrypt, ossl_des3_ecb_decrypt,
    64 / 8, 192 / 8, 0
}

Definition at line 891 of file openssl.c.

struct ossl_cipher ossl_des_cbc [static]
Initial value:
 {
    ossl_des_init, ossl_des_cbc_encrypt, ossl_des_cbc_decrypt,
    64 / 8, 64 / 8, 0
}

Definition at line 886 of file openssl.c.

struct ossl_cipher ossl_des_ecb [static]
Initial value:
 {
    ossl_des_init, ossl_des_ecb_encrypt, ossl_des_ecb_decrypt,
    64 / 8, 64 / 8, 0
}

Definition at line 881 of file openssl.c.

int px_openssl_initialized = 0 [static]

Definition at line 259 of file openssl.c.

Referenced by px_find_digest().