~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to contrib/pgcrypto/pgp-mpi.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pgp-mpi.c
 
3
 *        OpenPGP MPI helper functions.
 
4
 *
 
5
 * Copyright (c) 2005 Marko Kreen
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 * 1. Redistributions of source code must retain the above copyright
 
12
 *        notice, this list of conditions and the following disclaimer.
 
13
 * 2. Redistributions in binary form must reproduce the above copyright
 
14
 *        notice, this list of conditions and the following disclaimer in the
 
15
 *        documentation and/or other materials provided with the distribution.
 
16
 *
 
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
20
 * ARE DISCLAIMED.      IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
27
 * SUCH DAMAGE.
 
28
 *
 
29
 * contrib/pgcrypto/pgp-mpi.c
 
30
 */
 
31
#include "postgres.h"
 
32
 
 
33
#include "px.h"
 
34
#include "mbuf.h"
 
35
#include "pgp.h"
 
36
 
 
37
int
 
38
pgp_mpi_alloc(int bits, PGP_MPI **mpi)
 
39
{
 
40
        PGP_MPI    *n;
 
41
        int                     len = (bits + 7) / 8;
 
42
 
 
43
        if (bits < 0 || bits > 0xFFFF)
 
44
        {
 
45
                px_debug("pgp_mpi_alloc: unreasonable request: bits=%d", bits);
 
46
                return PXE_PGP_CORRUPT_DATA;
 
47
        }
 
48
        n = px_alloc(sizeof(*n) + len);
 
49
        n->bits = bits;
 
50
        n->bytes = len;
 
51
        n->data = (uint8 *) (n) + sizeof(*n);
 
52
        *mpi = n;
 
53
        return 0;
 
54
}
 
55
 
 
56
int
 
57
pgp_mpi_create(uint8 *data, int bits, PGP_MPI **mpi)
 
58
{
 
59
        int                     res;
 
60
        PGP_MPI    *n;
 
61
 
 
62
        res = pgp_mpi_alloc(bits, &n);
 
63
        if (res < 0)
 
64
                return res;
 
65
        memcpy(n->data, data, n->bytes);
 
66
        *mpi = n;
 
67
        return 0;
 
68
}
 
69
 
 
70
int
 
71
pgp_mpi_free(PGP_MPI *mpi)
 
72
{
 
73
        if (mpi == NULL)
 
74
                return 0;
 
75
        memset(mpi, 0, sizeof(*mpi) + mpi->bytes);
 
76
        px_free(mpi);
 
77
        return 0;
 
78
}
 
79
 
 
80
int
 
81
pgp_mpi_read(PullFilter *src, PGP_MPI **mpi)
 
82
{
 
83
        int                     res;
 
84
        uint8           hdr[2];
 
85
        int                     bits;
 
86
        PGP_MPI    *n;
 
87
 
 
88
        res = pullf_read_fixed(src, 2, hdr);
 
89
        if (res < 0)
 
90
                return res;
 
91
        bits = ((unsigned) hdr[0] << 8) + hdr[1];
 
92
 
 
93
        res = pgp_mpi_alloc(bits, &n);
 
94
        if (res < 0)
 
95
                return res;
 
96
 
 
97
        res = pullf_read_fixed(src, n->bytes, n->data);
 
98
        if (res < 0)
 
99
                pgp_mpi_free(n);
 
100
        else
 
101
                *mpi = n;
 
102
        return res;
 
103
}
 
104
 
 
105
int
 
106
pgp_mpi_write(PushFilter *dst, PGP_MPI *n)
 
107
{
 
108
        int                     res;
 
109
        uint8           buf[2];
 
110
 
 
111
        buf[0] = n->bits >> 8;
 
112
        buf[1] = n->bits & 0xFF;
 
113
        res = pushf_write(dst, buf, 2);
 
114
        if (res >= 0)
 
115
                res = pushf_write(dst, n->data, n->bytes);
 
116
        return res;
 
117
}
 
118
 
 
119
int
 
120
pgp_mpi_hash(PX_MD *md, PGP_MPI *n)
 
121
{
 
122
        uint8           buf[2];
 
123
 
 
124
        buf[0] = n->bits >> 8;
 
125
        buf[1] = n->bits & 0xFF;
 
126
        px_md_update(md, buf, 2);
 
127
        px_md_update(md, n->data, n->bytes);
 
128
 
 
129
        return 0;
 
130
}
 
131
 
 
132
unsigned
 
133
pgp_mpi_cksum(unsigned cksum, PGP_MPI *n)
 
134
{
 
135
        int                     i;
 
136
 
 
137
        cksum += n->bits >> 8;
 
138
        cksum += n->bits & 0xFF;
 
139
        for (i = 0; i < n->bytes; i++)
 
140
                cksum += n->data[i];
 
141
 
 
142
        return cksum & 0xFFFF;
 
143
}