3 * OpenPGP string2key functions.
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 * contrib/pgcrypto/pgp-s2k.c
57 memset(
buf, 0, preload);
67 memcpy(dst,
buf, md_rlen);
73 memcpy(dst,
buf, remain);
100 memset(
buf, 0, preload);
109 if (remain > md_rlen)
111 memcpy(dst,
buf, md_rlen);
117 memcpy(dst,
buf, remain);
132 unsigned preload = 0;
150 memset(
buf, 0, preload);
159 while (curcnt < count)
168 if (curcnt + key_len < count)
170 else if (curcnt < count)
179 if (remain > md_rlen)
181 memcpy(dst,
buf, md_rlen);
187 memcpy(dst,
buf, remain);
196 * Decide PGP_S2K_ISALTED iteration count (in OpenPGP one-byte representation)
200 * gpg defaults to 96 => 65536 iters
202 * For our default (count=-1) we let it float a bit: 96 + 32 => between 65536
203 * and 262144 iterations.
205 * Otherwise, find the smallest number which provides at least the specified
214 return 96 + (rand_byte & 0x1F);
215 /* this is a bit brute-force, but should be quick enough */
216 for (iter = 0; iter <= 255; iter++)
int pullf_read_fixed(PullFilter *src, int len, uint8 *dst)
static PgChecksumMode mode
int pgp_s2k_process(PGP_S2K *s2k, int cipher, const uint8 *key, int key_len)
static int calc_s2k_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
int pgp_s2k_fill(PGP_S2K *s2k, int mode, int digest_algo, int count)
int pgp_s2k_read(PullFilter *src, PGP_S2K *s2k)
static uint8 decide_s2k_iter(unsigned rand_byte, int count)
static int calc_s2k_simple(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
static int calc_s2k_iter_salted(PGP_S2K *s2k, PX_MD *md, const uint8 *key, unsigned key_len)
int pgp_get_cipher_key_size(int code)
int pgp_load_digest(int code, PX_MD **res)
#define s2k_decode_count(cval)
bool pg_strong_random(void *buf, size_t len)
void px_memset(void *ptr, int c, size_t len)
#define PXE_PGP_BAD_S2K_MODE
#define px_md_finish(md, buf)
#define px_md_update(md, data, dlen)
#define PXE_PGP_UNSUPPORTED_CIPHER
#define px_md_result_size(md)