diff options
author | Jack Lloyd <[email protected]> | 2021-04-07 11:32:31 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2021-04-07 11:44:06 -0400 |
commit | 833aa251c7ebf0011699ca8d3d854ed118a17af4 (patch) | |
tree | 90c71ffd4298880c140ac4162272d0ac10e50219 | |
parent | b8e392971a16189eb46d58a51d259a725b7c814e (diff) |
Move RFC 4880 encoding to rfc4880.h
This allows making pgp_s2k.h internal in the future while leaving the
RFC 4880 logic available.
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/info.txt | 2 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp | 58 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/pgp_s2k.h | 26 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/rfc4880.cpp | 73 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/rfc4880.h | 39 |
5 files changed, 115 insertions, 83 deletions
diff --git a/src/lib/pbkdf/pgp_s2k/info.txt b/src/lib/pbkdf/pgp_s2k/info.txt index 4868dd070..a1c048eaf 100644 --- a/src/lib/pbkdf/pgp_s2k/info.txt +++ b/src/lib/pbkdf/pgp_s2k/info.txt @@ -1,5 +1,6 @@ <defines> PGP_S2K -> 20170527 +RFC4880 -> 20210407 </defines> <requires> @@ -8,4 +9,5 @@ hash <header:public> pgp_s2k.h +rfc4880.h </header:public> diff --git a/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp index 11409417a..0d78f4358 100644 --- a/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp +++ b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp @@ -13,64 +13,6 @@ namespace Botan { -/* -PGP stores the iteration count as a single byte -Thus it can only actually take on one of 256 values, based on the -formula in RFC 4880 section 3.6.1.3 -*/ -static const uint32_t OPENPGP_S2K_ITERS[256] = { - 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600, - 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2176, 2304, 2432, - 2560, 2688, 2816, 2944, 3072, 3200, 3328, 3456, 3584, 3712, - 3840, 3968, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, - 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8704, - 9216, 9728, 10240, 10752, 11264, 11776, 12288, 12800, 13312, - 13824, 14336, 14848, 15360, 15872, 16384, 17408, 18432, 19456, - 20480, 21504, 22528, 23552, 24576, 25600, 26624, 27648, 28672, - 29696, 30720, 31744, 32768, 34816, 36864, 38912, 40960, 43008, - 45056, 47104, 49152, 51200, 53248, 55296, 57344, 59392, 61440, - 63488, 65536, 69632, 73728, 77824, 81920, 86016, 90112, 94208, - 98304, 102400, 106496, 110592, 114688, 118784, 122880, 126976, - 131072, 139264, 147456, 155648, 163840, 172032, 180224, 188416, - 196608, 204800, 212992, 221184, 229376, 237568, 245760, 253952, - 262144, 278528, 294912, 311296, 327680, 344064, 360448, 376832, - 393216, 409600, 425984, 442368, 458752, 475136, 491520, 507904, - 524288, 557056, 589824, 622592, 655360, 688128, 720896, 753664, - 786432, 819200, 851968, 884736, 917504, 950272, 983040, 1015808, - 1048576, 1114112, 1179648, 1245184, 1310720, 1376256, 1441792, - 1507328, 1572864, 1638400, 1703936, 1769472, 1835008, 1900544, - 1966080, 2031616, 2097152, 2228224, 2359296, 2490368, 2621440, - 2752512, 2883584, 3014656, 3145728, 3276800, 3407872, 3538944, - 3670016, 3801088, 3932160, 4063232, 4194304, 4456448, 4718592, - 4980736, 5242880, 5505024, 5767168, 6029312, 6291456, 6553600, - 6815744, 7077888, 7340032, 7602176, 7864320, 8126464, 8388608, - 8912896, 9437184, 9961472, 10485760, 11010048, 11534336, - 12058624, 12582912, 13107200, 13631488, 14155776, 14680064, - 15204352, 15728640, 16252928, 16777216, 17825792, 18874368, - 19922944, 20971520, 22020096, 23068672, 24117248, 25165824, - 26214400, 27262976, 28311552, 29360128, 30408704, 31457280, - 32505856, 33554432, 35651584, 37748736, 39845888, 41943040, - 44040192, 46137344, 48234496, 50331648, 52428800, 54525952, - 56623104, 58720256, 60817408, 62914560, 65011712 }; - -uint8_t RFC4880_encode_count(size_t desired_iterations) - { - if(desired_iterations <= OPENPGP_S2K_ITERS[0]) - return 0; - - if(desired_iterations >= OPENPGP_S2K_ITERS[255]) - return 255; - - auto i = std::lower_bound(OPENPGP_S2K_ITERS, OPENPGP_S2K_ITERS + 256, desired_iterations); - - return static_cast<uint8_t>(i - OPENPGP_S2K_ITERS); - } - -size_t RFC4880_decode_count(uint8_t iter) - { - return OPENPGP_S2K_ITERS[iter]; - } - namespace { void pgp_s2k(HashFunction& hash, diff --git a/src/lib/pbkdf/pgp_s2k/pgp_s2k.h b/src/lib/pbkdf/pgp_s2k/pgp_s2k.h index 8d148e725..c56142a1d 100644 --- a/src/lib/pbkdf/pgp_s2k/pgp_s2k.h +++ b/src/lib/pbkdf/pgp_s2k/pgp_s2k.h @@ -12,37 +12,13 @@ #include <botan/pbkdf.h> #include <botan/pwdhash.h> #include <botan/hash.h> - -/* -This header will not be fully internal - the RFC4880 count -encoding functions will remain here. But the definition of -OpenPGP_S2K will be made internal -*/ +#include <botan/rfc4880.h> //BOTAN_FUTURE_INTERNAL_HEADER(pgp_s2k.h) namespace Botan { /** -* RFC 4880 encodes the iteration count to a single-byte value -*/ -uint8_t BOTAN_PUBLIC_API(2,8) RFC4880_encode_count(size_t iterations); - -/** -* Decode the iteration count from RFC 4880 encoding -*/ -size_t BOTAN_PUBLIC_API(2,8) RFC4880_decode_count(uint8_t encoded_iter); - -/** -* Round an arbitrary iteration count to next largest iteration count -* supported by RFC4880 encoding. -*/ -inline size_t RFC4880_round_iterations(size_t iterations) - { - return RFC4880_decode_count(RFC4880_encode_count(iterations)); - } - -/** * OpenPGP's S2K * * See RFC 4880 sections 3.7.1.1, 3.7.1.2, and 3.7.1.3 diff --git a/src/lib/pbkdf/pgp_s2k/rfc4880.cpp b/src/lib/pbkdf/pgp_s2k/rfc4880.cpp new file mode 100644 index 000000000..5256eff17 --- /dev/null +++ b/src/lib/pbkdf/pgp_s2k/rfc4880.cpp @@ -0,0 +1,73 @@ +/* +* (C) 2021 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/rfc4880.h> + +namespace Botan { + +namespace { + +/* +PGP stores the iteration count as a single byte +Thus it can only actually take on one of 256 values, based on the +formula in RFC 4880 section 3.6.1.3 +*/ +static const uint32_t OPENPGP_S2K_ITERS[256] = { + 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600, + 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2176, 2304, 2432, + 2560, 2688, 2816, 2944, 3072, 3200, 3328, 3456, 3584, 3712, + 3840, 3968, 4096, 4352, 4608, 4864, 5120, 5376, 5632, 5888, + 6144, 6400, 6656, 6912, 7168, 7424, 7680, 7936, 8192, 8704, + 9216, 9728, 10240, 10752, 11264, 11776, 12288, 12800, 13312, + 13824, 14336, 14848, 15360, 15872, 16384, 17408, 18432, 19456, + 20480, 21504, 22528, 23552, 24576, 25600, 26624, 27648, 28672, + 29696, 30720, 31744, 32768, 34816, 36864, 38912, 40960, 43008, + 45056, 47104, 49152, 51200, 53248, 55296, 57344, 59392, 61440, + 63488, 65536, 69632, 73728, 77824, 81920, 86016, 90112, 94208, + 98304, 102400, 106496, 110592, 114688, 118784, 122880, 126976, + 131072, 139264, 147456, 155648, 163840, 172032, 180224, 188416, + 196608, 204800, 212992, 221184, 229376, 237568, 245760, 253952, + 262144, 278528, 294912, 311296, 327680, 344064, 360448, 376832, + 393216, 409600, 425984, 442368, 458752, 475136, 491520, 507904, + 524288, 557056, 589824, 622592, 655360, 688128, 720896, 753664, + 786432, 819200, 851968, 884736, 917504, 950272, 983040, 1015808, + 1048576, 1114112, 1179648, 1245184, 1310720, 1376256, 1441792, + 1507328, 1572864, 1638400, 1703936, 1769472, 1835008, 1900544, + 1966080, 2031616, 2097152, 2228224, 2359296, 2490368, 2621440, + 2752512, 2883584, 3014656, 3145728, 3276800, 3407872, 3538944, + 3670016, 3801088, 3932160, 4063232, 4194304, 4456448, 4718592, + 4980736, 5242880, 5505024, 5767168, 6029312, 6291456, 6553600, + 6815744, 7077888, 7340032, 7602176, 7864320, 8126464, 8388608, + 8912896, 9437184, 9961472, 10485760, 11010048, 11534336, + 12058624, 12582912, 13107200, 13631488, 14155776, 14680064, + 15204352, 15728640, 16252928, 16777216, 17825792, 18874368, + 19922944, 20971520, 22020096, 23068672, 24117248, 25165824, + 26214400, 27262976, 28311552, 29360128, 30408704, 31457280, + 32505856, 33554432, 35651584, 37748736, 39845888, 41943040, + 44040192, 46137344, 48234496, 50331648, 52428800, 54525952, + 56623104, 58720256, 60817408, 62914560, 65011712 }; + +} + +uint8_t RFC4880_encode_count(size_t desired_iterations) + { + if(desired_iterations <= OPENPGP_S2K_ITERS[0]) + return 0; + + if(desired_iterations >= OPENPGP_S2K_ITERS[255]) + return 255; + + auto i = std::lower_bound(OPENPGP_S2K_ITERS, OPENPGP_S2K_ITERS + 256, desired_iterations); + + return static_cast<uint8_t>(i - OPENPGP_S2K_ITERS); + } + +size_t RFC4880_decode_count(uint8_t iter) + { + return OPENPGP_S2K_ITERS[iter]; + } + +} diff --git a/src/lib/pbkdf/pgp_s2k/rfc4880.h b/src/lib/pbkdf/pgp_s2k/rfc4880.h new file mode 100644 index 000000000..8d751985c --- /dev/null +++ b/src/lib/pbkdf/pgp_s2k/rfc4880.h @@ -0,0 +1,39 @@ +/* +* (C) 2021 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_RFC4880_H_ +#define BOTAN_RFC4880_H_ + +#include <botan/types.h> + +namespace Botan { + +/* +Helpers for encoding PGP S2K values (see RFC 4880) +*/ + +/** +* RFC 4880 encodes the iteration count to a single-byte value +*/ +uint8_t BOTAN_PUBLIC_API(2,8) RFC4880_encode_count(size_t iterations); + +/** +* Decode the iteration count from RFC 4880 encoding +*/ +size_t BOTAN_PUBLIC_API(2,8) RFC4880_decode_count(uint8_t encoded_iter); + +/** +* Round an arbitrary iteration count to next largest iteration count +* supported by RFC4880 encoding. +*/ +inline size_t RFC4880_round_iterations(size_t iterations) + { + return RFC4880_decode_count(RFC4880_encode_count(iterations)); + } + +} + +#endif |