3 * Various utility stuff.
5 * Copyright (c) 2005 Marko Kreen
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
29 * $PostgreSQL: pgsql/contrib/pgcrypto/pgp.c,v 1.4 2009/06/11 14:48:52 momjian Exp $
41 static int def_cipher_algo = PGP_SYM_AES_128;
42 static int def_s2k_cipher_algo = -1;
43 static int def_s2k_mode = PGP_S2K_ISALTED;
44 static int def_s2k_digest_algo = PGP_DIGEST_SHA1;
45 static int def_compress_algo = PGP_COMPR_NONE;
46 static int def_compress_level = 6;
47 static int def_disable_mdc = 0;
48 static int def_use_sess_key = 0;
49 static int def_text_mode = 0;
50 static int def_unicode_mode = 0;
51 static int def_convert_crlf = 0;
69 static const struct digest_info digest_list[] = {
70 {"md5", PGP_DIGEST_MD5},
71 {"sha1", PGP_DIGEST_SHA1},
72 {"sha-1", PGP_DIGEST_SHA1},
73 {"ripemd160", PGP_DIGEST_RIPEMD160},
74 {"sha256", PGP_DIGEST_SHA256},
75 {"sha384", PGP_DIGEST_SHA384},
76 {"sha512", PGP_DIGEST_SHA512},
80 static const struct cipher_info cipher_list[] = {
81 {"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
82 {"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
83 {"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
84 {"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
85 {"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
86 {"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
87 {"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
88 {"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
89 {"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
93 static const struct cipher_info *
94 get_cipher_info(int code)
96 const struct cipher_info *i;
98 for (i = cipher_list; i->name; i++)
105 pgp_get_digest_code(const char *name)
107 const struct digest_info *i;
109 for (i = digest_list; i->name; i++)
110 if (pg_strcasecmp(i->name, name) == 0)
112 return PXE_PGP_UNSUPPORTED_HASH;
116 pgp_get_cipher_code(const char *name)
118 const struct cipher_info *i;
120 for (i = cipher_list; i->name; i++)
121 if (pg_strcasecmp(i->name, name) == 0)
123 return PXE_PGP_UNSUPPORTED_CIPHER;
127 pgp_get_digest_name(int code)
129 const struct digest_info *i;
131 for (i = digest_list; i->name; i++)
138 pgp_get_cipher_name(int code)
140 const struct cipher_info *i = get_cipher_info(code);
148 pgp_get_cipher_key_size(int code)
150 const struct cipher_info *i = get_cipher_info(code);
158 pgp_get_cipher_block_size(int code)
160 const struct cipher_info *i = get_cipher_info(code);
168 pgp_load_cipher(int code, PX_Cipher **res)
171 const struct cipher_info *i = get_cipher_info(code);
174 return PXE_PGP_CORRUPT_DATA;
176 err = px_find_cipher(i->int_name, res);
180 return PXE_PGP_UNSUPPORTED_CIPHER;
184 pgp_load_digest(int code, PX_MD **res)
187 const char *name = pgp_get_digest_name(code);
190 return PXE_PGP_CORRUPT_DATA;
192 err = px_find_digest(name, res);
196 return PXE_PGP_UNSUPPORTED_HASH;
200 pgp_init(PGP_Context **ctx_p)
204 ctx = px_alloc(sizeof *ctx);
205 memset(ctx, 0, sizeof *ctx);
207 ctx->cipher_algo = def_cipher_algo;
208 ctx->s2k_cipher_algo = def_s2k_cipher_algo;
209 ctx->s2k_mode = def_s2k_mode;
210 ctx->s2k_digest_algo = def_s2k_digest_algo;
211 ctx->compress_algo = def_compress_algo;
212 ctx->compress_level = def_compress_level;
213 ctx->disable_mdc = def_disable_mdc;
214 ctx->use_sess_key = def_use_sess_key;
215 ctx->unicode_mode = def_unicode_mode;
216 ctx->convert_crlf = def_convert_crlf;
217 ctx->text_mode = def_text_mode;
224 pgp_free(PGP_Context *ctx)
227 pgp_key_free(ctx->pub_key);
228 memset(ctx, 0, sizeof *ctx);
234 pgp_disable_mdc(PGP_Context *ctx, int disable)
236 ctx->disable_mdc = disable ? 1 : 0;
241 pgp_set_sess_key(PGP_Context *ctx, int use)
243 ctx->use_sess_key = use ? 1 : 0;
248 pgp_set_convert_crlf(PGP_Context *ctx, int doit)
250 ctx->convert_crlf = doit ? 1 : 0;
255 pgp_set_s2k_mode(PGP_Context *ctx, int mode)
263 case PGP_S2K_ISALTED:
264 ctx->s2k_mode = mode;
267 err = PXE_ARGUMENT_ERROR;
274 pgp_set_compress_algo(PGP_Context *ctx, int algo)
281 case PGP_COMPR_BZIP2:
282 ctx->compress_algo = algo;
285 return PXE_ARGUMENT_ERROR;
289 pgp_set_compress_level(PGP_Context *ctx, int level)
291 if (level >= 0 && level <= 9)
293 ctx->compress_level = level;
296 return PXE_ARGUMENT_ERROR;
300 pgp_set_text_mode(PGP_Context *ctx, int mode)
302 ctx->text_mode = mode;
307 pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
309 int code = pgp_get_cipher_code(name);
313 ctx->cipher_algo = code;
318 pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
320 int code = pgp_get_cipher_code(name);
324 ctx->s2k_cipher_algo = code;
329 pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
331 int code = pgp_get_digest_code(name);
335 ctx->s2k_digest_algo = code;
340 pgp_get_unicode_mode(PGP_Context *ctx)
342 return ctx->unicode_mode;
346 pgp_set_unicode_mode(PGP_Context *ctx, int mode)
348 ctx->unicode_mode = mode ? 1 : 0;
353 pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
355 if (key == NULL || len < 1)
356 return PXE_ARGUMENT_ERROR;
358 ctx->sym_key_len = len;