diff options
author | lloyd <lloyd@randombit.net> | 2011-02-22 15:56:49 +0000 |
---|---|---|
committer | lloyd <lloyd@randombit.net> | 2011-02-22 15:56:49 +0000 |
commit | 57e757ab680acbe0a9845c5b9f396cfd5eed60c9 (patch) | |
tree | 92932780f59a69f31d7f06ec8fc6f67852e0b90a | |
parent | 9326c35f54bf64f1d5d7cdec92e0375ae00326c2 (diff) | |
parent | 17d1cb0d352477bba9ec4e8a237b1cff21cb5d96 (diff) |
propagate from branch 'net.randombit.botan' (head 8453f801979d78b448a2aff80d2042715a42e843)
to branch 'net.randombit.botan.x509-path-validation' (head 084e8139f4b131b5aab6b6359e59324d7681488f)
-rw-r--r-- | checks/pk_valid.dat | 55 | ||||
-rw-r--r-- | checks/validate.cpp | 138 | ||||
-rwxr-xr-x | configure.py | 48 | ||||
-rw-r--r-- | doc/log.txt | 3 | ||||
-rw-r--r-- | src/block/aes/aes.cpp | 4 | ||||
-rw-r--r-- | src/block/blowfish/blowfish.cpp | 57 | ||||
-rw-r--r-- | src/block/blowfish/blowfish.h | 19 | ||||
-rw-r--r-- | src/cert/x509cert/x509cert.cpp | 24 | ||||
-rw-r--r-- | src/constructs/aont/package.cpp | 8 | ||||
-rw-r--r-- | src/constructs/rfc3394/info.txt | 1 | ||||
-rw-r--r-- | src/constructs/rfc3394/rfc3394.cpp | 122 | ||||
-rw-r--r-- | src/constructs/rfc3394/rfc3394.h | 45 | ||||
-rw-r--r-- | src/filters/bzip2/bzip2.cpp | 24 | ||||
-rw-r--r-- | src/filters/zlib/zlib.cpp | 16 | ||||
-rw-r--r-- | src/passhash/bcrypt/bcrypt.cpp | 154 | ||||
-rw-r--r-- | src/passhash/bcrypt/bcrypt.h | 37 | ||||
-rw-r--r-- | src/passhash/bcrypt/info.txt | 9 | ||||
-rw-r--r-- | src/passhash/passhash9/info.txt (renamed from src/constructs/passhash/info.txt) | 0 | ||||
-rw-r--r-- | src/passhash/passhash9/passhash9.cpp (renamed from src/constructs/passhash/passhash9.cpp) | 0 | ||||
-rw-r--r-- | src/passhash/passhash9/passhash9.h (renamed from src/constructs/passhash/passhash9.h) | 0 | ||||
-rw-r--r-- | src/stream/ctr/ctr.cpp | 4 |
21 files changed, 719 insertions, 49 deletions
diff --git a/checks/pk_valid.dat b/checks/pk_valid.dat index 9a19ddc78..7298b3730 100644 --- a/checks/pk_valid.dat +++ b/checks/pk_valid.dat @@ -4257,6 +4257,13 @@ brainpool160r1:\ 0F49C076BB26178277D8E490D0C77F7A9649DE31 [ECDSA/EMSA1(SHA-224)] +secp224r1:\ +42D126D0E51F3D6AA9B4D60BD1290853AA964A9C8698D5D5BDBAADEB:\ +45434453412074657374206D657373616765203230313130323135:\ +E1F6B207B4FC896879A51F65E85DB94CEB633FEC765739E689847D64:\ +A4C80AAF3D7B61200E66D6F41EC66D3D65E9E38DC06A88FE3B7F6C4C\ +8A5CEE4E04FE240464EA2DBB52489D3FAC1CDE6DA24A0E4C6598BCD2 + brainpool224r1:\ 47B5CCE9EED463CED28666DA57DA9D0A8BDD3F000CCFC0AE6054F1AD:\ 43727970746F2B2B20352E362E312045434453412074657374206D7367:\ @@ -4265,6 +4272,20 @@ brainpool224r1:\ 40FEF076FC8D610EC12AFC9CC43A150BD0190E507622E6623906D6B8 [ECDSA/EMSA1(SHA-256)] +secp256r1:\ +368E89CC30AE7A3B4B4903C30C238C010257FE97DB85AF35982A7960A0DBD2F3:\ +43727970746F2B2B20352E362E312045434453412074657374206D7367:\ +E2AAB3BD3AB1999651CD903F5385B8EC2EDA84C43B7801F08608C179DD373369:\ +CBED1CE0D581020D2F89174EC2DE450C1D547BAC3DCEECCDD476A6AADF46D24F\ +C456F43F351605CC40FC2A000B4D291042B5AEEA7A783DF89FC86666D832DACF + +secp256r1:\ +6CC691616D2C996A8F00A31C2EBF4E35C5EFFEAAFA2266F800768D5BF8EA2C1B:\ +45434453412074657374206D657373616765203230313130323135:\ +C1DDAA59A4E0B5D95EB873C33BC465C6782EBF7BC43DB18058C9EC4816AD2A11:\ +A8369164EF54A67303760B77AA62C4DE8122396908EA5B06DBCC2BC48264C832\ +ADB3A8855019D5AFF789EC1F276AD38A03AAF41F88593B74E5CB9DF7E4BD4922 + brainpool256r1:\ 4EC702404A8047A08206721DE33F02E1F06B14E09A5582171EA9BB8AB3C9BC14:\ 43727970746F2B2B20352E362E312045434453412074657374206D7367:\ @@ -4280,6 +4301,26 @@ A07978494C1B301C1E44467853CD367624549E0E9F5092C0100A53F877AD2EF6:\ A4A9C66485C02BC2DCC858364173FAE00EA02529BA21B56BBBB2EAA4B811416D [ECDSA/EMSA1(SHA-384)] +secp384r1:\ +100CC52F0263DCB12FDB9E50D44A4C84831A98756265DF0CBFD092D27A739821\ +043BFE282E2C8FAD46948C1F0365DD0C:\ +45434453412074657374206D657373616765203230313130323135:\ +C27CC4947F7CA7AF386AF5BEA88582685A043BB3C83C0C8B2A4BB1E53A3971FA\ +8161168E332B2F3735A50BB3E8694F43:\ +C8B93B3C4B97B87A918522F423E26194F1AEA2B83FE890893C15928B79BCAC75\ +F66AB47309378A54771ED46AF6AA453BFD2404EECFCCE19ACE11E5D5883EE40A\ +300A42BD9AC79E77E507DE9EAE0B54034DB17355EE2111990ED226701D4ED7EC + +secp384r1:\ +4AF67D00B7A8D964B38CC52CBC808D4693595A5B330E0B3EA52BDCD619D41B85\ +6961BDAC571D9BC93D16A9B1C4D5CC2F:\ +45434453412074657374206D657373616765203230313130323135:\ +8C5D8DDCF8AF127174577A60F9B5512813E33EA8E45B471F343806FBF68663E9\ +915B81A33F6AD22007D57818023AF982:\ +FF83C10E8D84777D17B724957B83E1500F578F1096C48BE2BCACE73E6681CDD6\ +A34F66CA2AF31241FAF85AEE2528438DA6BED934D75ACCF2E41176D8B661AB58\ +B7B867D802C38B39E8227F9CF0865072D381948FFFF637D8FB9B37BEC6AE0772 + brainpool384r1:\ 19AD48ECFB30F115AEF41CAFD29B265A586399C0F95166017AA7DB894413A2AE\ 821B7BE4F4E7B6BBC22A4E2EB1CC0865:\ @@ -4292,6 +4333,20 @@ brainpool384r1:\ 9D5398DDECCBEAAF3FD53D554AA1FADD [ECDSA/EMSA1(SHA-512)] + +secp521r1:\ +1511908E830069DAD59E8BA8F1BD4045612A4844805F61F7ECD92A1DEE1877B7\ +E62A57860314820C97FFC972732E3C4C0AE837103692E85B3A11B49EB3E20EF1599:\ +45434453412F53484128353132292074657374206D6573736167652032303131\ +30323135:\ +01C352020AAA6D14B6FC2B78FD46209A9EEF6A357CD8B5D53738E3D655FE7A80\ +8396E1DC5742058D05F2D76C8CBF4832BE0580A6FD7B4C7426656D17680DEAAEEEC2:\ +0138A515C79EECAAB50139FB5D9EF5A771CC1C0999F2E54B5A1A9370EA8ADCFD\ +DDD6E9933A39EDA0862F3ECAEBD49EA5ED58D93DA8F72B1CFB11E52A1528AEC8\ +63870060D717B29AD6D36DE953A4753FAC58629429EF4DD8F98B5A4F5504C5B2\ +29C23C609905632CD8D839DC472693698D7A149E8F3F17462F86BA0A7A895D80\ +583A46E9 + brainpool512r1:\ 1433AE89858BE7DD9346AF015FEC69F0556982FFEB9CCEF7FB1CE71155F7620C\ ED4A6ACD0F35461A17C8370C4E600BECBACC0F7C1D2D1A2C00203A0E6626C21C:\ diff --git a/checks/validate.cpp b/checks/validate.cpp index dd23eadc3..9acf73419 100644 --- a/checks/validate.cpp +++ b/checks/validate.cpp @@ -23,10 +23,18 @@ #include <botan/passhash9.h> #endif +#if defined(BOTAN_HAS_BCRYPT) + #include <botan/bcrypt.h> +#endif + #if defined(BOTAN_HAS_CRYPTO_BOX) #include <botan/cryptobox.h> #endif +#if defined(BOTAN_HAS_RFC3394_KEYWRAP) + #include <botan/rfc3394.h> +#endif + using namespace Botan; #include "validate.h" @@ -94,6 +102,124 @@ bool test_cryptobox(RandomNumberGenerator& rng) return true; } +bool keywrap_test(const char* key_str, + const char* expected_str, + const char* kek_str) + { + std::cout << '.' << std::flush; + + bool ok = true; + + try + { + SymmetricKey key(key_str); + SymmetricKey expected(expected_str); + SymmetricKey kek(kek_str); + + Algorithm_Factory& af = global_state().algorithm_factory(); + + SecureVector<byte> enc = rfc3394_keywrap(key.bits_of(), kek, af); + + if(enc != expected.bits_of()) + { + std::cout << "NIST key wrap encryption failure: " + << hex_encode(enc) << " != " << hex_encode(expected.bits_of()) << "\n"; + ok = false; + } + + SecureVector<byte> dec = rfc3394_keyunwrap(expected.bits_of(), kek, af); + + if(dec != key.bits_of()) + { + std::cout << "NIST key wrap decryption failure: " + << hex_encode(dec) << " != " << hex_encode(key.bits_of()) << "\n"; + ok = false; + } + } + catch(std::exception& e) + { + std::cout << e.what() << "\n"; + } + + return ok; + } + +bool test_keywrap() + { + std::cout << "Testing NIST keywrap: " << std::flush; + + bool ok = true; + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF", + "1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5", + "000102030405060708090A0B0C0D0E0F"); + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF", + "96778B25AE6CA435F92B5B97C050AED2468AB8A17AD84E5D", + "000102030405060708090A0B0C0D0E0F1011121314151617"); + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF", + "64E8C3F9CE0F5BA263E9777905818A2A93C8191E7D6E8AE7", + "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF0001020304050607", + "031D33264E15D33268F24EC260743EDCE1C6C7DDEE725A936BA814915C6762D2", + "000102030405060708090A0B0C0D0E0F1011121314151617"); + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF0001020304050607", + "A8F9BC1612C68B3FF6E6F4FBE30E71E4769C8B80A32CB8958CD5D17D6B254DA1", + "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); + + ok &= keywrap_test("00112233445566778899AABBCCDDEEFF000102030405060708090A0B0C0D0E0F", + "28C9F404C4B810F4CBCCB35CFB87F8263F5786E2D80ED326CBC7F0E71A99F43BFB988B9B7A02DD21", + "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"); + + std::cout << "\n"; + return ok; + } + +bool test_bcrypt(RandomNumberGenerator& rng) + { +#if defined(BOTAN_HAS_BCRYPT) + std::cout << "Testing Bcrypt: " << std::flush; + + const std::string input = "abc"; + + // Generated by jBCrypt 0.3 + const std::string fixed_hash = + "$2a$05$DfPyLs.G6.To9fXEFgUL1O6HpYw3jIXgPcl/L3Qt3jESuWmhxtmpS"; + + std::cout << "." << std::flush; + + bool ok = true; + + if(!check_bcrypt(input, fixed_hash)) + { + std::cout << "Fixed bcrypt test failed\n"; + ok = false; + } + + std::cout << "." << std::flush; + + for(u16bit level = 1; level != 5; ++level) + { + std::string gen_hash = generate_bcrypt(input, rng, level); + + if(!check_bcrypt(input, gen_hash)) + { + std::cout << "Gen and check for bcrypt failed: " + << gen_hash << " not valid\n"; + ok = false; + } + + std::cout << "." << std::flush; + } + + std::cout << std::endl; + return ok; +#endif + } + bool test_passhash(RandomNumberGenerator& rng) { #if defined(BOTAN_HAS_PASSHASH9) @@ -265,6 +391,18 @@ u32bit do_validation_tests(const std::string& filename, errors++; } + if(should_pass && !test_bcrypt(rng)) + { + std::cout << "BCrypt tests failed" << std::endl; + errors++; + } + + if(should_pass && !test_keywrap()) + { + std::cout << "NIST keywrap tests failed" << std::endl; + errors++; + } + if(should_pass && !test_cryptobox(rng)) { std::cout << "Cryptobox tests failed" << std::endl; diff --git a/configure.py b/configure.py index e91dc2934..b27f6250a 100755 --- a/configure.py +++ b/configure.py @@ -2,7 +2,7 @@ """ Configuration program for botan (http://botan.randombit.net/) - (C) 2009-2010 Jack Lloyd + (C) 2009-2011 Jack Lloyd Distributed under the terms of the Botan license Tested with @@ -225,6 +225,10 @@ def process_command_line(args): action='store_false', default=True, help=SUPPRESS_HELP) + build_group.add_option('--link-method', + default=None, + help=SUPPRESS_HELP) + build_group.add_option('--distribution-info', metavar='STRING', help='set distribution specific versioning', default='unspecified') @@ -1213,15 +1217,34 @@ Perform the filesystem operations needed to setup the build def setup_build(build_config, options, template_vars): """ + Choose the link method based on system availablity and user request + """ + def choose_link_method(req_method): + + def useable_methods(): + if 'symlink' in os.__dict__: + yield 'symlink' + if 'link' in os.__dict__: + yield 'hardlink' + yield 'copy' + + for method in useable_methods(): + if req_method is None or req_method == method: + return method + + logging.info('Could not use requested link method %s' % (req_method)) + return 'copy' + + """ Copy or link the file, depending on what the platform offers """ - def portable_symlink(filename, target_dir): + def portable_symlink(filename, target_dir, method): if not os.access(filename, os.R_OK): logging.warning('Missing file %s' % (filename)) return - if 'symlink' in os.__dict__: + if method == 'symlink': def count_dirs(dir, accum = 0): if dir in ['', '/', os.path.curdir]: return accum @@ -1237,13 +1260,16 @@ def setup_build(build_config, options, template_vars): os.symlink(source, target) - elif 'link' in os.__dict__: + elif method == 'hardlink': os.link(filename, os.path.join(target_dir, os.path.basename(filename))) - else: + elif method == 'copy': shutil.copy(filename, target_dir) + else: + raise Exception('Unknown link method %s' % (method)) + def choose_makefile_template(style): if style == 'nmake': return 'nmake.in' @@ -1304,13 +1330,16 @@ def setup_build(build_config, options, template_vars): finally: f.close() + link_method = choose_link_method(options.link_method) + logging.info('Using %s to link files into build directory' % (link_method)) + def link_headers(header_list, type, dir): logging.debug('Linking %d %s header files in %s' % ( len(header_list), type, dir)) for header_file in header_list: try: - portable_symlink(header_file, dir) + portable_symlink(header_file, dir, link_method) except OSError, e: if e.errno != errno.EEXIST: logging.error('Error linking %s into %s: %s' % ( @@ -1509,7 +1538,12 @@ def main(argv = None): if options.os is None: options.os = platform.system().lower() - logging.info('Guessing taget OS is %s (--os to set)' % (options.os)) + + if re.match('^cygwin_.*', options.os): + logging.debug("Converting '%s' to 'cygwin'", options.os) + options.os = 'cygwin' + + logging.info('Guessing target OS is %s (--os to set)' % (options.os)) if options.compiler is None: if options.os == 'windows': diff --git a/doc/log.txt b/doc/log.txt index 3fa6e6d6b..d86aba45f 100644 --- a/doc/log.txt +++ b/doc/log.txt @@ -1,5 +1,8 @@ * 1.9.14-dev, ????-??-?? + - Add support for bcrypt, OpenBSD's password hashing scheme + - Add support for NIST's AES key wrapping algorithm + - Fix an infinite loop in zlib filters introduced in 1.9.11 (PR 142) * 1.9.13, 2011-02-19 - Update Keccak to the round 3 variant diff --git a/src/block/aes/aes.cpp b/src/block/aes/aes.cpp index 7f32d243c..9fb12cd11 100644 --- a/src/block/aes/aes.cpp +++ b/src/block/aes/aes.cpp @@ -457,7 +457,7 @@ void aes_encrypt_n(const byte in[], byte out[], rotate_right(TE[get_byte(2, T1)], 16) ^ rotate_right(TE[get_byte(3, T2)], 24) ^ EK[7]; - for(u32bit r = 2*4; r < EK.size(); r += 2*4) + for(size_t r = 2*4; r < EK.size(); r += 2*4) { T0 = TE0[get_byte(0, B0)] ^ TE1[get_byte(1, B1)] ^ TE2[get_byte(2, B2)] ^ TE3[get_byte(3, B3)] ^ EK[r]; @@ -560,7 +560,7 @@ void aes_decrypt_n(const byte in[], byte out[], size_t blocks, rotate_right(TD[get_byte(2, T1)], 16) ^ rotate_right(TD[get_byte(3, T0)], 24) ^ DK[7]; - for(u32bit r = 2*4; r < DK.size(); r += 2*4) + for(size_t r = 2*4; r < DK.size(); r += 2*4) { T0 = TD0[get_byte(0, B0)] ^ TD1[get_byte(1, B3)] ^ TD2[get_byte(2, B2)] ^ TD3[get_byte(3, B1)] ^ DK[r]; diff --git a/src/block/blowfish/blowfish.cpp b/src/block/blowfish/blowfish.cpp index ea227e93e..b6319eec0 100644 --- a/src/block/blowfish/blowfish.cpp +++ b/src/block/blowfish/blowfish.cpp @@ -1,6 +1,6 @@ /* * Blowfish -* (C) 1999-2009 Jack Lloyd +* (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -87,20 +87,66 @@ void Blowfish::key_schedule(const byte key[], size_t length) { clear(); + const byte null_salt[16] = { 0 }; + + key_expansion(key, length, null_salt); + } + +void Blowfish::key_expansion(const byte key[], + size_t length, + const byte salt[16]) + { for(size_t i = 0, j = 0; i != 18; ++i, j += 4) P[i] ^= make_u32bit(key[(j ) % length], key[(j+1) % length], key[(j+2) % length], key[(j+3) % length]); u32bit L = 0, R = 0; - generate_sbox(P, L, R); - generate_sbox(S, L, R); + generate_sbox(P, L, R, salt, 0); + generate_sbox(S, L, R, salt, 2); + } + +/* +* Modified key schedule used for bcrypt password hashing +*/ +void Blowfish::eks_key_schedule(const byte key[], size_t length, + const byte salt[16], size_t workfactor) + { + if(length == 0 || length >= 56) + throw Invalid_Key_Length("EKSBlowfish", length); + + if(workfactor == 0) + throw std::invalid_argument("Bcrypt work factor must be at least 1"); + + /* + * On a 2.8 GHz Core-i7, workfactor == 18 takes about 25 seconds to + * hash a password. This seems like a reasonable upper bound for the + * time being. + */ + if(workfactor > 18) + throw std::invalid_argument("Requested Bcrypt work factor too large"); + + clear(); + + const byte null_salt[16] = { 0 }; + + key_expansion(key, length, salt); + + const size_t rounds = 1 << workfactor; + + for(size_t r = 0; r != rounds; ++r) + { + key_expansion(key, length, null_salt); + key_expansion(salt, 16, null_salt); + } } /* * Generate one of the Sboxes */ void Blowfish::generate_sbox(MemoryRegion<u32bit>& box, - u32bit& L, u32bit& R) const + u32bit& L, u32bit& R, + const byte salt[16], + size_t salt_off) const { const u32bit* S1 = &S[0]; const u32bit* S2 = &S[256]; @@ -109,6 +155,9 @@ void Blowfish::generate_sbox(MemoryRegion<u32bit>& box, for(size_t i = 0; i != box.size(); i += 2) { + L ^= load_be<u32bit>(salt, (i + salt_off) % 4); + R ^= load_be<u32bit>(salt, (i + salt_off + 1) % 4); + for(size_t j = 0; j != 16; j += 2) { L ^= P[j]; diff --git a/src/block/blowfish/blowfish.h b/src/block/blowfish/blowfish.h index b89ffcaaa..13706d21e 100644 --- a/src/block/blowfish/blowfish.h +++ b/src/block/blowfish/blowfish.h @@ -1,6 +1,6 @@ /* * Blowfish -* (C) 1999-2009 Jack Lloyd +* (C) 1999-2011 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -21,15 +21,28 @@ class BOTAN_DLL Blowfish : public Block_Cipher_Fixed_Params<8, 1, 56> void encrypt_n(const byte in[], byte out[], size_t blocks) const; void decrypt_n(const byte in[], byte out[], size_t blocks) const; + /** + * Modified EKSBlowfish key schedule, used for bcrypt password hashing + */ + void eks_key_schedule(const byte key[], size_t key_length, + const byte salt[16], size_t workfactor); + void clear(); std::string name() const { return "Blowfish"; } BlockCipher* clone() const { return new Blowfish; } Blowfish() : S(1024), P(18) {} private: - void key_schedule(const byte[], size_t); + void key_schedule(const byte key[], size_t length); + + void key_expansion(const byte key[], + size_t key_length, + const byte salt[16]); + void generate_sbox(MemoryRegion<u32bit>& box, - u32bit& L, u32bit& R) const; + u32bit& L, u32bit& R, + const byte salt[16], + size_t salt_off) const; static const u32bit P_INIT[18]; static const u32bit S_INIT[1024]; diff --git a/src/cert/x509cert/x509cert.cpp b/src/cert/x509cert/x509cert.cpp index b7c399078..38775271e 100644 --- a/src/cert/x509cert/x509cert.cpp +++ b/src/cert/x509cert/x509cert.cpp @@ -378,16 +378,16 @@ std::string X509_Certificate::to_string() const if(policies.size()) { out << "Policies: " << "\n"; - for(u32bit j = 0; j != policies.size(); j++) - out << " " << policies[j] << "\n"; + for(size_t i = 0; i != policies.size(); i++) + out << " " << policies[i] << "\n"; } std::vector<std::string> ex_constraints = this->ex_constraints(); if(ex_constraints.size()) { out << "Extended Constraints:\n"; - for(u32bit j = 0; j != ex_constraints.size(); j++) - out << " " << ex_constraints[j] << "\n"; + for(size_t i = 0; i != ex_constraints.size(); i++) + out << " " << ex_constraints[i] << "\n"; } out << "Signature algorithm: " << @@ -425,9 +425,9 @@ X509_DN create_dn(const Data_Store& info) X509_DN dn; - std::multimap<std::string, std::string>::iterator j; - for(j = names.begin(); j != names.end(); ++j) - dn.add_attribute(j->first, j->second); + std::multimap<std::string, std::string>::iterator i; + for(i = names.begin(); i != names.end(); ++i) + dn.add_attribute(i->first, i->second); return dn; } @@ -442,8 +442,8 @@ AlternativeName create_alt_name(const Data_Store& info) public: bool operator()(const std::string& key, const std::string&) const { - for(u32bit j = 0; j != matches.size(); ++j) - if(key.compare(matches[j]) == 0) + for(size_t i = 0; i != matches.size(); ++i) + if(key.compare(matches[i]) == 0) return true; return false; } @@ -461,9 +461,9 @@ AlternativeName create_alt_name(const Data_Store& info) AlternativeName alt_name; - std::multimap<std::string, std::string>::iterator j; - for(j = names.begin(); j != names.end(); ++j) - alt_name.add_attribute(j->first, j->second); + std::multimap<std::string, std::string>::iterator i; + for(i = names.begin(); i != names.end(); ++i) + alt_name.add_attribute(i->first, i->second); return alt_name; } diff --git a/src/constructs/aont/package.cpp b/src/constructs/aont/package.cpp index 4d92a789c..4d46f6b61 100644 --- a/src/constructs/aont/package.cpp +++ b/src/constructs/aont/package.cpp @@ -46,13 +46,13 @@ void aont_package(RandomNumberGenerator& rng, clear_mem(final_block, BLOCK_SIZE); // XOR the hash blocks into the final block - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { const size_t left = std::min<size_t>(BLOCK_SIZE, input_len - BLOCK_SIZE * i); zeroise(buf); - copy_mem(&buf[0], output + BLOCK_SIZE * i, left); + copy_mem(&buf[0], output + (BLOCK_SIZE * i), left); for(size_t j = 0; j != sizeof(i); ++j) buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i); @@ -94,13 +94,13 @@ void aont_unpackage(BlockCipher* cipher, const size_t blocks = ((input_len - 1) / BLOCK_SIZE); // XOR the blocks into the package key bits - for(u32bit i = 0; i != blocks; ++i) + for(size_t i = 0; i != blocks; ++i) { const size_t left = std::min<size_t>(BLOCK_SIZE, input_len - BLOCK_SIZE * (i+1)); zeroise(buf); - copy_mem(&buf[0], input + BLOCK_SIZE * i, left); + copy_mem(&buf[0], input + (BLOCK_SIZE * i), left); for(size_t j = 0; j != sizeof(i); ++j) buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i); diff --git a/src/constructs/rfc3394/info.txt b/src/constructs/rfc3394/info.txt new file mode 100644 index 000000000..496f3e138 --- /dev/null +++ b/src/constructs/rfc3394/info.txt @@ -0,0 +1 @@ +define RFC3394_KEYWRAP diff --git a/src/constructs/rfc3394/rfc3394.cpp b/src/constructs/rfc3394/rfc3394.cpp new file mode 100644 index 000000000..b000873fd --- /dev/null +++ b/src/constructs/rfc3394/rfc3394.cpp @@ -0,0 +1,122 @@ +/* +* AES Key Wrap (RFC 3394) +* (C) 2011 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/rfc3394.h> +#include <botan/algo_factory.h> +#include <botan/block_cipher.h> +#include <botan/loadstor.h> +#include <botan/exceptn.h> +#include <botan/internal/xor_buf.h> +#include <memory> + +namespace Botan { + +namespace { + +BlockCipher* make_aes(size_t keylength, + Algorithm_Factory& af) + { + if(keylength == 16) + return af.make_block_cipher("AES-128"); + else if(keylength == 24) + return af.make_block_cipher("AES-192"); + else if(keylength == 32) + return af.make_block_cipher("AES-256"); + else + throw std::invalid_argument("Bad KEK length for NIST keywrap"); + } + +} + +SecureVector<byte> rfc3394_keywrap(const MemoryRegion<byte>& key, + const SymmetricKey& kek, + Algorithm_Factory& af) + { + if(key.size() % 8 != 0) + throw std::invalid_argument("Bad input key size for NIST key wrap"); + + std::auto_ptr<BlockCipher> aes(make_aes(kek.length(), af)); + aes->set_key(kek); + + const size_t n = key.size() / 8; + + SecureVector<byte> R((n + 1) * 8); + SecureVector<byte> A(16); + + for(size_t i = 0; i != 8; ++i) + A[i] = 0xA6; + + copy_mem(&R[8], key.begin(), key.size()); + + for(size_t j = 0; j <= 5; ++j) + { + for(size_t i = 1; i <= n; ++i) + { + const u32bit t = (n * j) + i; + + copy_mem(&A[8], &R[8*i], 8); + + aes->encrypt(&A[0]); + copy_mem(&R[8*i], &A[8], 8); + + byte t_buf[4] = { 0 }; + store_be(t, t_buf); + xor_buf(&A[4], &t_buf[0], 4); + } + } + + copy_mem(&R[0], &A[0], 8); + + return R; + } + +SecureVector<byte> rfc3394_keyunwrap(const MemoryRegion<byte>& key, + const SymmetricKey& kek, + Algorithm_Factory& af) + { + if(key.size() < 16 || key.size() % 8 != 0) + throw std::invalid_argument("Bad input key size for NIST key unwrap"); + + std::auto_ptr<BlockCipher> aes(make_aes(kek.length(), af)); + aes->set_key(kek); + + const size_t n = (key.size() - 8) / 8; + + SecureVector<byte> R(n * 8); + SecureVector<byte> A(16); + + for(size_t i = 0; i != 8; ++i) + A[i] = key[i]; + + copy_mem(&R[0], key.begin() + 8, key.size() - 8); + + for(size_t j = 0; j <= 5; ++j) + { + for(size_t i = n; i != 0; --i) + { + const u32bit t = (5 - j) * n + i; + + byte t_buf[4] = { 0 }; + store_be(t, t_buf); + + xor_buf(&A[4], &t_buf[0], 4); + + copy_mem(&A[8], &R[8*(i-1)], 8); + + aes->decrypt(&A[0]); + + copy_mem(&R[8*(i-1)], &A[8], 8); + } + } + + if(load_be<u64bit>(&A[0], 0) != 0xA6A6A6A6A6A6A6A6) + throw Integrity_Failure("NIST key unwrap failed"); + + return R; + } + +} diff --git a/src/constructs/rfc3394/rfc3394.h b/src/constructs/rfc3394/rfc3394.h new file mode 100644 index 000000000..645586ee2 --- /dev/null +++ b/src/constructs/rfc3394/rfc3394.h @@ -0,0 +1,45 @@ +/* +* AES Key Wrap (RFC 3394) +* (C) 2011 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_AES_KEY_WRAP_H__ +#define BOTAN_AES_KEY_WRAP_H__ + +#include <botan/symkey.h> + +namespace Botan { + +class Algorithm_Factory; + +/** +* Encrypt a key under a key encryption key using the algorithm +* described in RFC 3394 +* +* @param key the plaintext key to encrypt +* @param kek the key encryption key +* @param af an algorithm factory +* @return key encrypted under kek +*/ +SecureVector<byte> BOTAN_DLL rfc3394_keywrap(const MemoryRegion<byte>& key, + const SymmetricKey& kek, + Algorithm_Factory& af); + +/** +* Decrypt a key under a key encryption key using the algorithm +* described in RFC 3394 +* +* @param key the encrypted key to decrypt +* @param kek the key encryption key +* @param af an algorithm factory +* @return key decrypted under kek +*/ +SecureVector<byte> BOTAN_DLL rfc3394_keyunwrap(const MemoryRegion<byte>& key, + const SymmetricKey& kek, + Algorithm_Factory& af); + +} + +#endif diff --git a/src/filters/bzip2/bzip2.cpp b/src/filters/bzip2/bzip2.cpp index b166017c3..a291c1173 100644 --- a/src/filters/bzip2/bzip2.cpp +++ b/src/filters/bzip2/bzip2.cpp @@ -168,10 +168,14 @@ void Bzip_Compression::flush() */ void Bzip_Compression::clear() { - if(!bz) return; - BZ2_bzCompressEnd(&(bz->stream)); - delete bz; - bz = 0; + zeroise(buffer); + + if(bz) + { + BZ2_bzCompressEnd(&(bz->stream)); + delete bz; + bz = 0; + } } /* @@ -278,10 +282,14 @@ void Bzip_Decompression::end_msg() */ void Bzip_Decompression::clear() { - if(!bz) return; - BZ2_bzDecompressEnd(&(bz->stream)); - delete bz; - bz = 0; + zeroise(buffer); + + if(bz) + { + BZ2_bzDecompressEnd(&(bz->stream)); + delete bz; + bz = 0; + } } } diff --git a/src/filters/zlib/zlib.cpp b/src/filters/zlib/zlib.cpp index 30dee0225..0f88b5558 100644 --- a/src/filters/zlib/zlib.cpp +++ b/src/filters/zlib/zlib.cpp @@ -138,9 +138,11 @@ void Zlib_Compression::end_msg() { zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); zlib->stream.avail_out = buffer.size(); + rc = deflate(&(zlib->stream), Z_FINISH); send(buffer.begin(), buffer.size() - zlib->stream.avail_out); } + clear(); } @@ -155,13 +157,13 @@ void Zlib_Compression::flush() while(true) { zlib->stream.avail_out = buffer.size(); - zlib->stream.next_out = reinterpret_cast<Bytef*>(buffer.begin()); - deflate(&(zlib->stream), Z_FULL_FLUSH); send(buffer.begin(), buffer.size() - zlib->stream.avail_out); - if(zlib->stream.avail_out == buffer.size()) break; + + if(zlib->stream.avail_out == buffer.size()) + break; } } @@ -170,14 +172,14 @@ void Zlib_Compression::flush() */ void Zlib_Compression::clear() { + zeroise(buffer); + if(zlib) { deflateEnd(&(zlib->stream)); delete zlib; zlib = 0; } - - buffer.clear(); } /* @@ -283,6 +285,8 @@ void Zlib_Decompression::end_msg() */ void Zlib_Decompression::clear() { + zeroise(buffer); + no_writes = true; if(zlib) @@ -291,8 +295,6 @@ void Zlib_Decompression::clear() delete zlib; zlib = 0; } - - buffer.clear(); } } diff --git a/src/passhash/bcrypt/bcrypt.cpp b/src/passhash/bcrypt/bcrypt.cpp new file mode 100644 index 000000000..e533c6081 --- /dev/null +++ b/src/passhash/bcrypt/bcrypt.cpp @@ -0,0 +1,154 @@ +/* +* Bcrypt Password Hashing +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/bcrypt.h> +#include <botan/loadstor.h> +#include <botan/libstate.h> +#include <botan/blowfish.h> +#include <botan/base64.h> + +#include <botan/pipe.h> +#include <botan/b64_filt.h> +#include <iostream> +#include <stdio.h> + +namespace Botan { + +namespace { + +std::string bcrypt_base64_encode(const byte input[], size_t length) + { + // Bcrypt uses a non-standard base64 alphabet + const byte OPENBSD_BASE64_SUB[256] = { + 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x38, 0x80, 0x80, 0x80, 0x39, + 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x2E, 0x2F, 0x41, 0x42, 0x43, 0x44, 0x45, + 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, + 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80 + }; + + std::string b64 = base64_encode(input, length); + + while(b64.size() && b64[b64.size()-1] == '=') + b64 = b64.substr(0, b64.size() - 1); + + for(size_t i = 0; i != b64.size(); ++i) + b64[i] = OPENBSD_BASE64_SUB[static_cast<byte>(b64[i])]; + + return b64; + } + +MemoryVector<byte> bcrypt_base64_decode(std::string input) + { + const byte OPENBSD_BASE64_SUB[256] = { + 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x41, 0x42, + 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, + 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, + 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, + 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, + 0x7A, 0x30, 0x31, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80 + }; + + for(size_t i = 0; i != input.size(); ++i) + input[i] = OPENBSD_BASE64_SUB[static_cast<byte>(input[i])]; + + //return base64_decode(input); + Pipe pipe(new Base64_Decoder); + pipe.process_msg(input); + return pipe.read_all(); + } + +std::string make_bcrypt(const std::string& pass, + const MemoryRegion<byte>& salt, + u16bit work_factor) + { + const byte magic[24] = { + 0x4F, 0x72, 0x70, 0x68, 0x65, 0x61, 0x6E, 0x42, + 0x65, 0x68, 0x6F, 0x6C, 0x64, 0x65, 0x72, 0x53, + 0x63, 0x72, 0x79, 0x44, 0x6F, 0x75, 0x62, 0x74 + }; + + MemoryVector<byte> ctext(magic, 24); + + Blowfish blowfish; + + // Include the trailing NULL byte + blowfish.eks_key_schedule(reinterpret_cast<const byte*>(pass.c_str()), + pass.length() + 1, + salt, + work_factor); + + for(size_t i = 0; i != 64; ++i) + blowfish.encrypt_n(&ctext[0], &ctext[0], 3); + + std::string salt_b64 = bcrypt_base64_encode(&salt[0], salt.size()); + + return "$2a$" + to_string(work_factor, 2) + "$" + salt_b64.substr(0, 22) + + bcrypt_base64_encode(&ctext[0], ctext.size() - 1); + } + +} + +std::string generate_bcrypt(const std::string& pass, + RandomNumberGenerator& rng, + u16bit work_factor) + { + return make_bcrypt(pass, rng.random_vec(16), work_factor); + } + +bool check_bcrypt(const std::string& pass, const std::string& hash) + { + if(hash.size() != 60 || + hash[0] != '$' || hash[1] != '2' || hash[2] != 'a' || + hash[3] != '$' || hash[6] != '$') + { + return false; + } + + const u16bit workfactor = to_u32bit(hash.substr(4, 2)); + + MemoryVector<byte> salt = bcrypt_base64_decode(hash.substr(7, 22)); + + const std::string compare = make_bcrypt(pass, salt, workfactor); + + return (hash == compare); + } + +} diff --git a/src/passhash/bcrypt/bcrypt.h b/src/passhash/bcrypt/bcrypt.h new file mode 100644 index 000000000..8a6ab58ea --- /dev/null +++ b/src/passhash/bcrypt/bcrypt.h @@ -0,0 +1,37 @@ +/* +* Bcrypt Password Hashing +* (C) 2011 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BCRYPT_H__ +#define BOTAN_BCRYPT_H__ + +#include <botan/rng.h> + +namespace Botan { + +/** +* Create a password hash using Bcrypt +* @param password the password +* @param rng a random number generator +* @param work_factor how much work to do to slow down guessing attacks +* +* @see http://www.usenix.org/events/usenix99/provos/provos_html/ +*/ +std::string BOTAN_DLL generate_bcrypt(const std::string& password, + RandomNumberGenerator& rng, + u16bit work_factor = 10); + +/** +* Check a previously created password hash +* @param password the password to check against +* @param hash the stored hash to check against +*/ +bool BOTAN_DLL check_bcrypt(const std::string& password, + const std::string& hash); + +} + +#endif diff --git a/src/passhash/bcrypt/info.txt b/src/passhash/bcrypt/info.txt new file mode 100644 index 000000000..91ab92e88 --- /dev/null +++ b/src/passhash/bcrypt/info.txt @@ -0,0 +1,9 @@ +define BCRYPT + +<requires> +libstate +blowfish +rng +base64 +</requires> + diff --git a/src/constructs/passhash/info.txt b/src/passhash/passhash9/info.txt index f96809f29..f96809f29 100644 --- a/src/constructs/passhash/info.txt +++ b/src/passhash/passhash9/info.txt diff --git a/src/constructs/passhash/passhash9.cpp b/src/passhash/passhash9/passhash9.cpp index e5f379052..e5f379052 100644 --- a/src/constructs/passhash/passhash9.cpp +++ b/src/passhash/passhash9/passhash9.cpp diff --git a/src/constructs/passhash/passhash9.h b/src/passhash/passhash9/passhash9.h index 92cc391dc..92cc391dc 100644 --- a/src/constructs/passhash/passhash9.h +++ b/src/passhash/passhash9/passhash9.h diff --git a/src/stream/ctr/ctr.cpp b/src/stream/ctr/ctr.cpp index 0de0b7b84..d221dc441 100644 --- a/src/stream/ctr/ctr.cpp +++ b/src/stream/ctr/ctr.cpp @@ -97,7 +97,7 @@ void CTR_BE::set_iv(const byte iv[], size_t iv_len) &counter[(i-1)*BLOCK_SIZE], BLOCK_SIZE); - for(u32bit j = 0; j != BLOCK_SIZE; ++j) + for(size_t j = 0; j != BLOCK_SIZE; ++j) if(++counter[i*BLOCK_SIZE + (BLOCK_SIZE-1-j)]) break; } @@ -115,7 +115,7 @@ void CTR_BE::increment_counter() for(size_t i = 0; i != 256; ++i) { - for(u32bit j = 1; j != BLOCK_SIZE; ++j) + for(size_t j = 1; j != BLOCK_SIZE; ++j) if(++counter[i*BLOCK_SIZE + (BLOCK_SIZE-1-j)]) break; } |