~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pgp-cfb.c
 
3
 *        Implements both normal and PGP-specific CFB mode.
 
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
 * $PostgreSQL$
 
30
 */
 
31
 
 
32
#include "postgres.h"
 
33
 
 
34
#include "mbuf.h"
 
35
#include "px.h"
 
36
#include "pgp.h"
 
37
 
 
38
typedef int (*mix_data_t) (PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst);
 
39
 
 
40
struct PGP_CFB
 
41
{
 
42
        PX_Cipher  *ciph;
 
43
        int                     block_size;
 
44
        int                     pos;
 
45
        int                     block_no;
 
46
        int                     resync;
 
47
        uint8           fr[PGP_MAX_BLOCK];
 
48
        uint8           fre[PGP_MAX_BLOCK];
 
49
        uint8           encbuf[PGP_MAX_BLOCK];
 
50
};
 
51
 
 
52
int
 
53
pgp_cfb_create(PGP_CFB ** ctx_p, int algo, const uint8 *key, int key_len,
 
54
                           int resync, uint8 *iv)
 
55
{
 
56
        int                     res;
 
57
        PX_Cipher  *ciph;
 
58
        PGP_CFB    *ctx;
 
59
 
 
60
        res = pgp_load_cipher(algo, &ciph);
 
61
        if (res < 0)
 
62
                return res;
 
63
 
 
64
        res = px_cipher_init(ciph, key, key_len, NULL);
 
65
        if (res < 0)
 
66
        {
 
67
                px_cipher_free(ciph);
 
68
                return res;
 
69
        }
 
70
 
 
71
        ctx = px_alloc(sizeof(*ctx));
 
72
        memset(ctx, 0, sizeof(*ctx));
 
73
        ctx->ciph = ciph;
 
74
        ctx->block_size = px_cipher_block_size(ciph);
 
75
        ctx->resync = resync;
 
76
 
 
77
        if (iv)
 
78
                memcpy(ctx->fr, iv, ctx->block_size);
 
79
 
 
80
        *ctx_p = ctx;
 
81
        return 0;
 
82
}
 
83
 
 
84
void
 
85
pgp_cfb_free(PGP_CFB * ctx)
 
86
{
 
87
        px_cipher_free(ctx->ciph);
 
88
        memset(ctx, 0, sizeof(*ctx));
 
89
        px_free(ctx);
 
90
}
 
91
 
 
92
/*
 
93
 * Data processing for normal CFB.      (PGP_PKT_SYMENCRYPTED_DATA_MDC)
 
94
 */
 
95
static int
 
96
mix_encrypt_normal(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
97
{
 
98
        int                     i;
 
99
 
 
100
        for (i = ctx->pos; i < ctx->pos + len; i++)
 
101
                *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
 
102
        ctx->pos += len;
 
103
        return len;
 
104
}
 
105
 
 
106
static int
 
107
mix_decrypt_normal(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
108
{
 
109
        int                     i;
 
110
 
 
111
        for (i = ctx->pos; i < ctx->pos + len; i++)
 
112
        {
 
113
                ctx->encbuf[i] = *data++;
 
114
                *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
 
115
        }
 
116
        ctx->pos += len;
 
117
        return len;
 
118
}
 
119
 
 
120
/*
 
121
 * Data processing for old PGP CFB mode. (PGP_PKT_SYMENCRYPTED_DATA)
 
122
 *
 
123
 * The goal is to hide the horror from the rest of the code,
 
124
 * thus its all concentrated here.
 
125
 */
 
126
static int
 
127
mix_encrypt_resync(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
128
{
 
129
        int                     i,
 
130
                                n;
 
131
 
 
132
        /* block #2 is 2 bytes long */
 
133
        if (ctx->block_no == 2)
 
134
        {
 
135
                n = 2 - ctx->pos;
 
136
                if (len < n)
 
137
                        n = len;
 
138
                for (i = ctx->pos; i < ctx->pos + n; i++)
 
139
                        *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
 
140
 
 
141
                ctx->pos += n;
 
142
                len -= n;
 
143
 
 
144
                if (ctx->pos == 2)
 
145
                {
 
146
                        memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
 
147
                        memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
 
148
                        ctx->pos = 0;
 
149
                        return n;
 
150
                }
 
151
        }
 
152
        for (i = ctx->pos; i < ctx->pos + len; i++)
 
153
                *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
 
154
        ctx->pos += len;
 
155
        return len;
 
156
}
 
157
 
 
158
static int
 
159
mix_decrypt_resync(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
160
{
 
161
        int                     i,
 
162
                                n;
 
163
 
 
164
        /* block #2 is 2 bytes long */
 
165
        if (ctx->block_no == 2)
 
166
        {
 
167
                n = 2 - ctx->pos;
 
168
                if (len < n)
 
169
                        n = len;
 
170
                for (i = ctx->pos; i < ctx->pos + n; i++)
 
171
                {
 
172
                        ctx->encbuf[i] = *data++;
 
173
                        *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
 
174
                }
 
175
                ctx->pos += n;
 
176
                len -= n;
 
177
 
 
178
                if (ctx->pos == 2)
 
179
                {
 
180
                        memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
 
181
                        memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
 
182
                        ctx->pos = 0;
 
183
                        return n;
 
184
                }
 
185
        }
 
186
        for (i = ctx->pos; i < ctx->pos + len; i++)
 
187
        {
 
188
                ctx->encbuf[i] = *data++;
 
189
                *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
 
190
        }
 
191
        ctx->pos += len;
 
192
        return len;
 
193
}
 
194
 
 
195
/*
 
196
 * common code for both encrypt and decrypt.
 
197
 */
 
198
static int
 
199
cfb_process(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst,
 
200
                        mix_data_t mix_data)
 
201
{
 
202
        int                     n;
 
203
        int                     res;
 
204
 
 
205
        while (len > 0 && ctx->pos > 0)
 
206
        {
 
207
                n = ctx->block_size - ctx->pos;
 
208
                if (len < n)
 
209
                        n = len;
 
210
 
 
211
                n = mix_data(ctx, data, n, dst);
 
212
                data += n;
 
213
                dst += n;
 
214
                len -= n;
 
215
 
 
216
                if (ctx->pos == ctx->block_size)
 
217
                {
 
218
                        memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
 
219
                        ctx->pos = 0;
 
220
                }
 
221
        }
 
222
 
 
223
        while (len > 0)
 
224
        {
 
225
                px_cipher_encrypt(ctx->ciph, ctx->fr, ctx->block_size, ctx->fre);
 
226
                if (ctx->block_no < 5)
 
227
                        ctx->block_no++;
 
228
 
 
229
                n = ctx->block_size;
 
230
                if (len < n)
 
231
                        n = len;
 
232
 
 
233
                res = mix_data(ctx, data, n, dst);
 
234
                data += res;
 
235
                dst += res;
 
236
                len -= res;
 
237
 
 
238
                if (ctx->pos == ctx->block_size)
 
239
                {
 
240
                        memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
 
241
                        ctx->pos = 0;
 
242
                }
 
243
        }
 
244
        return 0;
 
245
}
 
246
 
 
247
/*
 
248
 * public interface
 
249
 */
 
250
 
 
251
int
 
252
pgp_cfb_encrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
253
{
 
254
        mix_data_t      mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;
 
255
 
 
256
        return cfb_process(ctx, data, len, dst, mix);
 
257
}
 
258
 
 
259
int
 
260
pgp_cfb_decrypt(PGP_CFB * ctx, const uint8 *data, int len, uint8 *dst)
 
261
{
 
262
        mix_data_t      mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;
 
263
 
 
264
        return cfb_process(ctx, data, len, dst, mix);
 
265
}