From 4e07a84cb9f0f962175e7e5c11f717c5c24eb961 Mon Sep 17 00:00:00 2001 From: David Shaw Date: Fri, 13 Oct 2006 03:44:34 +0000 Subject: [PATCH] * parse-packet.c (parse_symkeyenc): Show the unpacked as well as the packed s2k iteration count. * main.h, options.h, gpg.c (encode_s2k_iterations, main), passphrase.c (hash_passphrase): Add --s2k-count option to specify the number of s2k hash iterations. --- g10/gpg.c | 32 ++++++++++++++++++++++++++++++++ g10/main.h | 2 ++ g10/options.h | 6 ++++-- g10/parse-packet.c | 4 +++- g10/passphrase.c | 4 ++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/g10/gpg.c b/g10/gpg.c index 2b14feaa2..a3265239a 100644 --- a/g10/gpg.c +++ b/g10/gpg.c @@ -274,6 +274,7 @@ enum cmd_and_opt_values oS2KMode, oS2KDigest, oS2KCipher, + oS2KCount, oSimpleSKChecksum, oDisplayCharset, oNotDashEscaped, @@ -534,6 +535,7 @@ static ARGPARSE_OPTS opts[] = { { oS2KMode, "s2k-mode", 1, "@"}, { oS2KDigest, "s2k-digest-algo", 2, "@"}, { oS2KCipher, "s2k-cipher-algo", 2, "@"}, + { oS2KCount, "s2k-count", 1, "@"}, { oSimpleSKChecksum, "simple-sk-checksum", 0, "@"}, { oCipherAlgo, "cipher-algo", 2, "@"}, { oDigestAlgo, "digest-algo", 2, "@"}, @@ -1688,6 +1690,32 @@ reopen_std(void) #endif /* HAVE_STAT && !HAVE_W32_SYSTEM */ } +/* Pack an s2k iteration count into the form specified in 2440. If + we're in between valid values, round up. */ +static unsigned char +encode_s2k_iterations(int iterations) +{ + unsigned char c=0,result; + unsigned int count; + + if(iterations<=1024) + return 0; + + if(iterations>=65011712) + return 255; + + /* Need count to be in the range 16-31 */ + for(count=iterations>>6;count>=32;count>>=1) + c++; + + result=(c<<4)|(count-16); + + if(S2K_DECODE_COUNT(result)> 4) + 6)) + #endif /*G10_MAIN_H*/ diff --git a/g10/options.h b/g10/options.h index 05f993db2..c881196e9 100644 --- a/g10/options.h +++ b/g10/options.h @@ -120,8 +120,10 @@ struct int s2k_mode; int s2k_digest_algo; int s2k_cipher_algo; - int simple_sk_checksum; /* create the deprecated rfc2440 secret - key protection*/ + unsigned char s2k_count; /* This is the encoded form, not the raw + count */ + int simple_sk_checksum; /* create the deprecated rfc2440 secret key + protection */ int not_dash_escaped; int escape_from; int lock_once; diff --git a/g10/parse-packet.c b/g10/parse-packet.c index 363ade99f..31f21bc92 100644 --- a/g10/parse-packet.c +++ b/g10/parse-packet.c @@ -721,7 +721,9 @@ parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) for(i=0; i < 8; i++ ) fprintf (listfp, "%02x", k->s2k.salt[i]); if( s2kmode == 3 ) - fprintf (listfp, ", count %lu", (ulong)k->s2k.count ); + fprintf (listfp, ", count %lu (%lu)", + S2K_DECODE_COUNT((ulong)k->s2k.count), + (ulong)k->s2k.count ); fprintf (listfp, "\n"); } } diff --git a/g10/passphrase.c b/g10/passphrase.c index 01e9166b5..db1080739 100644 --- a/g10/passphrase.c +++ b/g10/passphrase.c @@ -1036,11 +1036,11 @@ hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create ) if( create && !pass ) { randomize_buffer(s2k->salt, 8, 1); if( s2k->mode == 3 ) - s2k->count = 96; /* 65536 iterations */ + s2k->count = opt.s2k_count; } if( s2k->mode == 3 ) { - count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6); + count = S2K_DECODE_COUNT(s2k->count); if( count < len2 ) count = len2; }