Header And Logo

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

Functions

pgp-mpi.c File Reference

#include "postgres.h"
#include "px.h"
#include "mbuf.h"
#include "pgp.h"
Include dependency graph for pgp-mpi.c:

Go to the source code of this file.

Functions

int pgp_mpi_alloc (int bits, PGP_MPI **mpi)
int pgp_mpi_create (uint8 *data, int bits, PGP_MPI **mpi)
int pgp_mpi_free (PGP_MPI *mpi)
int pgp_mpi_read (PullFilter *src, PGP_MPI **mpi)
int pgp_mpi_write (PushFilter *dst, PGP_MPI *n)
int pgp_mpi_hash (PX_MD *md, PGP_MPI *n)
unsigned pgp_mpi_cksum (unsigned cksum, PGP_MPI *n)

Function Documentation

int pgp_mpi_alloc ( int  bits,
PGP_MPI **  mpi 
)

Definition at line 38 of file pgp-mpi.c.

References PGP_MPI::bits, PGP_MPI::bytes, PGP_MPI::data, px_alloc, and px_debug().

Referenced by bn_to_mpi(), pgp_mpi_create(), and pgp_mpi_read().

{
    PGP_MPI    *n;
    int         len = (bits + 7) / 8;

    if (bits < 0 || bits > 0xFFFF)
    {
        px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
        return PXE_PGP_CORRUPT_DATA;
    }
    n = px_alloc(sizeof(*n) + len);
    n->bits = bits;
    n->bytes = len;
    n->data = (uint8 *) (n) + sizeof(*n);
    *mpi = n;
    return 0;
}

unsigned pgp_mpi_cksum ( unsigned  cksum,
PGP_MPI n 
)

Definition at line 133 of file pgp-mpi.c.

References PGP_MPI::bits, PGP_MPI::bytes, PGP_MPI::data, and i.

Referenced by check_key_cksum().

{
    int         i;

    cksum += n->bits >> 8;
    cksum += n->bits & 0xFF;
    for (i = 0; i < n->bytes; i++)
        cksum += n->data[i];

    return cksum & 0xFFFF;
}

int pgp_mpi_create ( uint8 data,
int  bits,
PGP_MPI **  mpi 
)

Definition at line 57 of file pgp-mpi.c.

References PGP_MPI::bytes, PGP_MPI::data, and pgp_mpi_alloc().

Referenced by create_secmsg().

{
    int         res;
    PGP_MPI    *n;

    res = pgp_mpi_alloc(bits, &n);
    if (res < 0)
        return res;
    memcpy(n->data, data, n->bytes);
    *mpi = n;
    return 0;
}

int pgp_mpi_free ( PGP_MPI mpi  ) 

Definition at line 71 of file pgp-mpi.c.

References PGP_MPI::bytes, NULL, and px_free.

Referenced by bn_to_mpi(), decrypt_elgamal(), decrypt_rsa(), encrypt_and_write_elgamal(), encrypt_and_write_rsa(), pgp_key_free(), pgp_mpi_read(), and pgp_parse_pubenc_sesskey().

{
    if (mpi == NULL)
        return 0;
    memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
    px_free(mpi);
    return 0;
}

int pgp_mpi_hash ( PX_MD md,
PGP_MPI n 
)

Definition at line 120 of file pgp-mpi.c.

References PGP_MPI::bits, buf, PGP_MPI::bytes, PGP_MPI::data, and px_md_update.

Referenced by calc_key_id(), and check_key_sha1().

{
    uint8       buf[2];

    buf[0] = n->bits >> 8;
    buf[1] = n->bits & 0xFF;
    px_md_update(md, buf, 2);
    px_md_update(md, n->data, n->bytes);

    return 0;
}

int pgp_mpi_read ( PullFilter src,
PGP_MPI **  mpi 
)

Definition at line 81 of file pgp-mpi.c.

References PGP_MPI::bytes, PGP_MPI::data, pgp_mpi_alloc(), pgp_mpi_free(), and pullf_read_fixed().

Referenced by _pgp_read_public_key(), decrypt_elgamal(), decrypt_rsa(), and process_secret_key().

{
    int         res;
    uint8       hdr[2];
    int         bits;
    PGP_MPI    *n;

    res = pullf_read_fixed(src, 2, hdr);
    if (res < 0)
        return res;
    bits = ((unsigned) hdr[0] << 8) + hdr[1];

    res = pgp_mpi_alloc(bits, &n);
    if (res < 0)
        return res;

    res = pullf_read_fixed(src, n->bytes, n->data);
    if (res < 0)
        pgp_mpi_free(n);
    else
        *mpi = n;
    return res;
}

int pgp_mpi_write ( PushFilter dst,
PGP_MPI n 
)

Definition at line 106 of file pgp-mpi.c.

References PGP_MPI::bits, buf, PGP_MPI::bytes, PGP_MPI::data, and pushf_write().

Referenced by encrypt_and_write_elgamal(), and encrypt_and_write_rsa().

{
    int         res;
    uint8       buf[2];

    buf[0] = n->bits >> 8;
    buf[1] = n->bits & 0xFF;
    res = pushf_write(dst, buf, 2);
    if (res >= 0)
        res = pushf_write(dst, n->data, n->bytes);
    return res;
}