/* * Public Key Base * (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ #include #include #include #include #include #include #include #include #include namespace Botan { /* * Encrypt a message */ SecureVector PK_Encryptor::encrypt(const byte in[], u32bit len, RandomNumberGenerator& rng) const { return enc(in, len, rng); } /* * Encrypt a message */ SecureVector PK_Encryptor::encrypt(const MemoryRegion& in, RandomNumberGenerator& rng) const { return enc(in.begin(), in.size(), rng); } /* * Decrypt a message */ SecureVector PK_Decryptor::decrypt(const byte in[], u32bit len) const { return dec(in, len); } /* * Decrypt a message */ SecureVector PK_Decryptor::decrypt(const MemoryRegion& in) const { return dec(in.begin(), in.size()); } /* * PK_Encryptor_MR_with_EME Constructor */ PK_Encryptor_MR_with_EME::PK_Encryptor_MR_with_EME(const PK_Encrypting_Key& k, EME* eme_obj) : key(k), encoder(eme_obj) { } /* * Encrypt a message */ SecureVector PK_Encryptor_MR_with_EME::enc(const byte msg[], u32bit length, RandomNumberGenerator& rng) const { SecureVector message; if(encoder) message = encoder->encode(msg, length, key.max_input_bits(), rng); else message.set(msg, length); if(8*(message.size() - 1) + high_bit(message[0]) > key.max_input_bits()) throw Invalid_Argument("PK_Encryptor_MR_with_EME: Input is too large"); return key.encrypt(message, message.size(), rng); } /* * Return the max size, in bytes, of a message */ u32bit PK_Encryptor_MR_with_EME::maximum_input_size() const { if(!encoder) return (key.max_input_bits() / 8); else return encoder->maximum_input_size(key.max_input_bits()); } /* * PK_Decryptor_MR_with_EME Constructor */ PK_Decryptor_MR_with_EME::PK_Decryptor_MR_with_EME(const PK_Decrypting_Key& k, EME* eme_obj) : key(k), encoder(eme_obj) { } /* * Decrypt a message */ SecureVector PK_Decryptor_MR_with_EME::dec(const byte msg[], u32bit length) const { try { SecureVector decrypted = key.decrypt(msg, length); if(encoder) return encoder->decode(decrypted, key.max_input_bits()); else return decrypted; } catch(Invalid_Argument) { throw Decoding_Error("PK_Decryptor_MR_with_EME: Input is invalid"); } } /* * PK_Signer Constructor */ PK_Signer::PK_Signer(const Private_Key& key, EMSA* emsa_obj) : emsa(emsa_obj) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_signature_op(key); if(op) break; } if(op == 0) throw Lookup_Error("PK_Signer: No working engine for " + key.algo_name()); sig_format = IEEE_1363; } /* * Sign a message */ SecureVector PK_Signer::sign_message(const byte msg[], u32bit length, RandomNumberGenerator& rng) { update(msg, length); return signature(rng); } /* * Add more to the message to be signed */ void PK_Signer::update(const byte in[], u32bit length) { emsa->update(in, length); } /* * Create a signature */ SecureVector PK_Signer::signature(RandomNumberGenerator& rng) { SecureVector encoded = emsa->encoding_of(emsa->raw_data(), op->max_input_bits(), rng); SecureVector plain_sig = op->sign(encoded, encoded.size(), rng); if(op->message_parts() == 1 || sig_format == IEEE_1363) return plain_sig; if(sig_format == DER_SEQUENCE) { if(plain_sig.size() % op->message_parts()) throw Encoding_Error("PK_Signer: strange signature size found"); const u32bit SIZE_OF_PART = plain_sig.size() / op->message_parts(); std::vector sig_parts(op->message_parts()); for(u32bit j = 0; j != sig_parts.size(); ++j) sig_parts[j].binary_decode(plain_sig + SIZE_OF_PART*j, SIZE_OF_PART); return DER_Encoder() .start_cons(SEQUENCE) .encode_list(sig_parts) .end_cons() .get_contents(); } else throw Encoding_Error("PK_Signer: Unknown signature format " + to_string(sig_format)); } /* * PK_Verifier Constructor */ PK_Verifier::PK_Verifier(const Public_Key& key, EMSA* emsa_obj) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_verify_op(key); if(op) break; } if(op == 0) throw Lookup_Error("PK_Verifier: No working engine for " + key.algo_name()); emsa = emsa_obj; sig_format = IEEE_1363; } /* * Set the signature format */ void PK_Verifier::set_input_format(Signature_Format format) { if(op->message_parts() == 1 && format != IEEE_1363) throw Invalid_State("PK_Verifier: This algorithm always uses IEEE 1363"); sig_format = format; } /* * Verify a message */ bool PK_Verifier::verify_message(const byte msg[], u32bit msg_length, const byte sig[], u32bit sig_length) { update(msg, msg_length); return check_signature(sig, sig_length); } /* * Append to the message */ void PK_Verifier::update(const byte in[], u32bit length) { emsa->update(in, length); } /* * Check a signature */ bool PK_Verifier::check_signature(const byte sig[], u32bit length) { try { if(sig_format == IEEE_1363) return validate_signature(emsa->raw_data(), sig, length); else if(sig_format == DER_SEQUENCE) { BER_Decoder decoder(sig, length); BER_Decoder ber_sig = decoder.start_cons(SEQUENCE); u32bit count = 0; SecureVector real_sig; while(ber_sig.more_items()) { BigInt sig_part; ber_sig.decode(sig_part); real_sig.append(BigInt::encode_1363(sig_part, op->message_part_size())); ++count; } if(count != op->message_parts()) throw Decoding_Error("PK_Verifier: signature size invalid"); return validate_signature(emsa->raw_data(), real_sig, real_sig.size()); } else throw Decoding_Error("PK_Verifier: Unknown signature format " + to_string(sig_format)); } catch(Invalid_Argument) { return false; } } /* * Verify a signature */ bool PK_Verifier::validate_signature(const MemoryRegion& msg, const byte sig[], u32bit sig_len) { if(op->with_recovery()) { SecureVector output_of_key = op->verify_mr(sig, sig_len); return emsa->verify(output_of_key, msg, op->max_input_bits()); } else { Null_RNG rng; SecureVector encoded = emsa->encoding_of(msg, op->max_input_bits(), rng); return op->verify(encoded, encoded.size(), sig, sig_len); } } /* * PK_Key_Agreement Constructor */ PK_Key_Agreement::PK_Key_Agreement(const PK_Key_Agreement_Key& key, KDF* kdf_obj) : kdf(kdf_obj) { Algorithm_Factory::Engine_Iterator i(global_state().algorithm_factory()); while(const Engine* engine = i.next()) { op = engine->get_key_agreement_op(key); if(op) break; } if(op == 0) throw Lookup_Error("PK_Key_Agreement: No working engine for " + key.algo_name()); } SymmetricKey PK_Key_Agreement::derive_key(u32bit key_len, const byte in[], u32bit in_len, const byte params[], u32bit params_len) const { SecureVector z = op->agree(in, in_len); if(!kdf) return z; return kdf->derive_key(key_len, z, params, params_len); } }