Header And Logo

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

pgp-mpi.c

Go to the documentation of this file.
00001 /*
00002  * pgp-mpi.c
00003  *    OpenPGP MPI helper functions.
00004  *
00005  * Copyright (c) 2005 Marko Kreen
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * contrib/pgcrypto/pgp-mpi.c
00030  */
00031 #include "postgres.h"
00032 
00033 #include "px.h"
00034 #include "mbuf.h"
00035 #include "pgp.h"
00036 
00037 int
00038 pgp_mpi_alloc(int bits, PGP_MPI **mpi)
00039 {
00040     PGP_MPI    *n;
00041     int         len = (bits + 7) / 8;
00042 
00043     if (bits < 0 || bits > 0xFFFF)
00044     {
00045         px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
00046         return PXE_PGP_CORRUPT_DATA;
00047     }
00048     n = px_alloc(sizeof(*n) + len);
00049     n->bits = bits;
00050     n->bytes = len;
00051     n->data = (uint8 *) (n) + sizeof(*n);
00052     *mpi = n;
00053     return 0;
00054 }
00055 
00056 int
00057 pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
00058 {
00059     int         res;
00060     PGP_MPI    *n;
00061 
00062     res = pgp_mpi_alloc(bits, &n);
00063     if (res < 0)
00064         return res;
00065     memcpy(n->data, data, n->bytes);
00066     *mpi = n;
00067     return 0;
00068 }
00069 
00070 int
00071 pgp_mpi_free(PGP_MPI *mpi)
00072 {
00073     if (mpi == NULL)
00074         return 0;
00075     memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
00076     px_free(mpi);
00077     return 0;
00078 }
00079 
00080 int
00081 pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
00082 {
00083     int         res;
00084     uint8       hdr[2];
00085     int         bits;
00086     PGP_MPI    *n;
00087 
00088     res = pullf_read_fixed(src, 2, hdr);
00089     if (res < 0)
00090         return res;
00091     bits = ((unsigned) hdr[0] << 8) + hdr[1];
00092 
00093     res = pgp_mpi_alloc(bits, &n);
00094     if (res < 0)
00095         return res;
00096 
00097     res = pullf_read_fixed(src, n->bytes, n->data);
00098     if (res < 0)
00099         pgp_mpi_free(n);
00100     else
00101         *mpi = n;
00102     return res;
00103 }
00104 
00105 int
00106 pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
00107 {
00108     int         res;
00109     uint8       buf[2];
00110 
00111     buf[0] = n->bits >> 8;
00112     buf[1] = n->bits & 0xFF;
00113     res = pushf_write(dst, buf, 2);
00114     if (res >= 0)
00115         res = pushf_write(dst, n->data, n->bytes);
00116     return res;
00117 }
00118 
00119 int
00120 pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
00121 {
00122     uint8       buf[2];
00123 
00124     buf[0] = n->bits >> 8;
00125     buf[1] = n->bits & 0xFF;
00126     px_md_update(md, buf, 2);
00127     px_md_update(md, n->data, n->bytes);
00128 
00129     return 0;
00130 }
00131 
00132 unsigned
00133 pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
00134 {
00135     int         i;
00136 
00137     cksum += n->bits >> 8;
00138     cksum += n->bits & 0xFF;
00139     for (i = 0; i < n->bytes; i++)
00140         cksum += n->data[i];
00141 
00142     return cksum & 0xFFFF;
00143 }