Repair memory leaks in plpython.
[pgsql.git] / contrib / pgcrypto / pgp-cfb.c
blobde41e825b0ce85289b9de1763ca8900c3f1e182f
1 /*
2 * pgp-cfb.c
3 * Implements both normal and PGP-specific CFB mode.
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
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.
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.
29 * contrib/pgcrypto/pgp-cfb.c
32 #include "postgres.h"
34 #include "pgp.h"
35 #include "px.h"
37 typedef int (*mix_data_t) (PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst);
39 struct PGP_CFB
41 PX_Cipher *ciph;
42 int block_size;
43 int pos;
44 int block_no;
45 int resync;
46 uint8 fr[PGP_MAX_BLOCK];
47 uint8 fre[PGP_MAX_BLOCK];
48 uint8 encbuf[PGP_MAX_BLOCK];
51 int
52 pgp_cfb_create(PGP_CFB **ctx_p, int algo, const uint8 *key, int key_len,
53 int resync, uint8 *iv)
55 int res;
56 PX_Cipher *ciph;
57 PGP_CFB *ctx;
59 res = pgp_load_cipher(algo, &ciph);
60 if (res < 0)
61 return res;
63 res = px_cipher_init(ciph, key, key_len, NULL);
64 if (res < 0)
66 px_cipher_free(ciph);
67 return res;
70 ctx = palloc0(sizeof(*ctx));
71 ctx->ciph = ciph;
72 ctx->block_size = px_cipher_block_size(ciph);
73 ctx->resync = resync;
75 if (iv)
76 memcpy(ctx->fr, iv, ctx->block_size);
78 *ctx_p = ctx;
79 return 0;
82 void
83 pgp_cfb_free(PGP_CFB *ctx)
85 px_cipher_free(ctx->ciph);
86 px_memset(ctx, 0, sizeof(*ctx));
87 pfree(ctx);
91 * Data processing for normal CFB. (PGP_PKT_SYMENCRYPTED_DATA_MDC)
93 static int
94 mix_encrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
96 int i;
98 for (i = ctx->pos; i < ctx->pos + len; i++)
99 *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
100 ctx->pos += len;
101 return len;
104 static int
105 mix_decrypt_normal(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
107 int i;
109 for (i = ctx->pos; i < ctx->pos + len; i++)
111 ctx->encbuf[i] = *data++;
112 *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
114 ctx->pos += len;
115 return len;
119 * Data processing for old PGP CFB mode. (PGP_PKT_SYMENCRYPTED_DATA)
121 * The goal is to hide the horror from the rest of the code,
122 * thus its all concentrated here.
124 static int
125 mix_encrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
127 int i,
130 /* block #2 is 2 bytes long */
131 if (ctx->block_no == 2)
133 n = 2 - ctx->pos;
134 if (len < n)
135 n = len;
136 for (i = ctx->pos; i < ctx->pos + n; i++)
137 *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
139 ctx->pos += n;
140 len -= n;
142 if (ctx->pos == 2)
144 memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
145 memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
146 ctx->pos = 0;
147 return n;
150 for (i = ctx->pos; i < ctx->pos + len; i++)
151 *dst++ = ctx->encbuf[i] = ctx->fre[i] ^ (*data++);
152 ctx->pos += len;
153 return len;
156 static int
157 mix_decrypt_resync(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
159 int i,
162 /* block #2 is 2 bytes long */
163 if (ctx->block_no == 2)
165 n = 2 - ctx->pos;
166 if (len < n)
167 n = len;
168 for (i = ctx->pos; i < ctx->pos + n; i++)
170 ctx->encbuf[i] = *data++;
171 *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
173 ctx->pos += n;
174 len -= n;
176 if (ctx->pos == 2)
178 memcpy(ctx->fr, ctx->encbuf + 2, ctx->block_size - 2);
179 memcpy(ctx->fr + ctx->block_size - 2, ctx->encbuf, 2);
180 ctx->pos = 0;
181 return n;
184 for (i = ctx->pos; i < ctx->pos + len; i++)
186 ctx->encbuf[i] = *data++;
187 *dst++ = ctx->fre[i] ^ ctx->encbuf[i];
189 ctx->pos += len;
190 return len;
194 * common code for both encrypt and decrypt.
196 static int
197 cfb_process(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst,
198 mix_data_t mix_data)
200 int n;
201 int res;
203 while (len > 0 && ctx->pos > 0)
205 n = ctx->block_size - ctx->pos;
206 if (len < n)
207 n = len;
209 n = mix_data(ctx, data, n, dst);
210 data += n;
211 dst += n;
212 len -= n;
214 if (ctx->pos == ctx->block_size)
216 memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
217 ctx->pos = 0;
221 while (len > 0)
223 unsigned rlen;
225 px_cipher_encrypt(ctx->ciph, 0, ctx->fr, ctx->block_size, ctx->fre, &rlen);
226 if (ctx->block_no < 5)
227 ctx->block_no++;
229 n = ctx->block_size;
230 if (len < n)
231 n = len;
233 res = mix_data(ctx, data, n, dst);
234 data += res;
235 dst += res;
236 len -= res;
238 if (ctx->pos == ctx->block_size)
240 memcpy(ctx->fr, ctx->encbuf, ctx->block_size);
241 ctx->pos = 0;
244 return 0;
248 * public interface
252 pgp_cfb_encrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
254 mix_data_t mix = ctx->resync ? mix_encrypt_resync : mix_encrypt_normal;
256 return cfb_process(ctx, data, len, dst, mix);
260 pgp_cfb_decrypt(PGP_CFB *ctx, const uint8 *data, int len, uint8 *dst)
262 mix_data_t mix = ctx->resync ? mix_decrypt_resync : mix_decrypt_normal;
264 return cfb_process(ctx, data, len, dst, mix);