diff options
author | lloyd <[email protected]> | 2008-10-01 14:36:27 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-01 14:36:27 +0000 |
commit | 06e35ed5375028f246f57b99ccf639e8ed317b35 (patch) | |
tree | 32021a79d1617a147f7fa7ac29c1eae60f9024ce /src/pubkey/dl_algo | |
parent | 9320b5e5c1b64894a6ff8797f392b57dfd72dea3 (diff) |
Rename pk dir to pubkey, avoids tab-completion collision with pk_pad
Diffstat (limited to 'src/pubkey/dl_algo')
-rw-r--r-- | src/pubkey/dl_algo/dl_algo.cpp | 165 | ||||
-rw-r--r-- | src/pubkey/dl_algo/dl_algo.h | 61 | ||||
-rw-r--r-- | src/pubkey/dl_algo/info.txt | 16 |
3 files changed, 242 insertions, 0 deletions
diff --git a/src/pubkey/dl_algo/dl_algo.cpp b/src/pubkey/dl_algo/dl_algo.cpp new file mode 100644 index 000000000..2b59a334e --- /dev/null +++ b/src/pubkey/dl_algo/dl_algo.cpp @@ -0,0 +1,165 @@ +/************************************************* +* DL Scheme Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/dl_algo.h> +#include <botan/numthry.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> + +namespace Botan { + +/************************************************* +* Return the X.509 public key encoder * +*************************************************/ +X509_Encoder* DL_Scheme_PublicKey::x509_encoder() const + { + class DL_Scheme_Encoder : public X509_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + MemoryVector<byte> group = + key->group.DER_encode(key->group_format()); + + return AlgorithmIdentifier(key->get_oid(), group); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder().encode(key->y).get_contents(); + } + + DL_Scheme_Encoder(const DL_Scheme_PublicKey* k) : key(k) {} + private: + const DL_Scheme_PublicKey* key; + }; + + return new DL_Scheme_Encoder(this); + } + +/************************************************* +* Return the X.509 public key decoder * +*************************************************/ +X509_Decoder* DL_Scheme_PublicKey::x509_decoder() + { + class DL_Scheme_Decoder : public X509_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + DataSource_Memory source(alg_id.parameters); + key->group.BER_decode(source, key->group_format()); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits).decode(key->y); + key->X509_load_hook(); + } + + DL_Scheme_Decoder(DL_Scheme_PublicKey* k) : key(k) {} + private: + DL_Scheme_PublicKey* key; + }; + + return new DL_Scheme_Decoder(this); + } + +/************************************************* +* Return the PKCS #8 private key encoder * +*************************************************/ +PKCS8_Encoder* DL_Scheme_PrivateKey::pkcs8_encoder() const + { + class DL_Scheme_Encoder : public PKCS8_Encoder + { + public: + AlgorithmIdentifier alg_id() const + { + MemoryVector<byte> group = + key->group.DER_encode(key->group_format()); + + return AlgorithmIdentifier(key->get_oid(), group); + } + + MemoryVector<byte> key_bits() const + { + return DER_Encoder().encode(key->x).get_contents(); + } + + DL_Scheme_Encoder(const DL_Scheme_PrivateKey* k) : key(k) {} + private: + const DL_Scheme_PrivateKey* key; + }; + + return new DL_Scheme_Encoder(this); + } + +/************************************************* +* Return the PKCS #8 private key decoder * +*************************************************/ +PKCS8_Decoder* DL_Scheme_PrivateKey::pkcs8_decoder(RandomNumberGenerator& rng) + { + class DL_Scheme_Decoder : public PKCS8_Decoder + { + public: + void alg_id(const AlgorithmIdentifier& alg_id) + { + DataSource_Memory source(alg_id.parameters); + key->group.BER_decode(source, key->group_format()); + } + + void key_bits(const MemoryRegion<byte>& bits) + { + BER_Decoder(bits).decode(key->x); + key->PKCS8_load_hook(rng); + } + + DL_Scheme_Decoder(DL_Scheme_PrivateKey* k, RandomNumberGenerator& r) : + key(k), rng(r) {} + private: + DL_Scheme_PrivateKey* key; + RandomNumberGenerator& rng; + }; + + return new DL_Scheme_Decoder(this, rng); + } + +/************************************************* +* Check Public DL Parameters * +*************************************************/ +bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + if(y < 2 || y >= group_p()) + return false; + if(!group.verify_group(rng, strong)) + return false; + return true; + } + +/************************************************* +* Check DL Scheme Private Parameters * +*************************************************/ +bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng, + bool strong) const + { + const BigInt& p = group_p(); + const BigInt& g = group_g(); + + if(y < 2 || y >= p || x < 2 || x >= p) + return false; + if(!group.verify_group(rng, strong)) + return false; + + if(!strong) + return true; + + if(y != power_mod(g, x, p)) + return false; + + return true; + } + +} diff --git a/src/pubkey/dl_algo/dl_algo.h b/src/pubkey/dl_algo/dl_algo.h new file mode 100644 index 000000000..a8d8d1d51 --- /dev/null +++ b/src/pubkey/dl_algo/dl_algo.h @@ -0,0 +1,61 @@ +/************************************************* +* DL Scheme Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_DL_ALGO_H__ +#define BOTAN_DL_ALGO_H__ + +#include <botan/dl_group.h> +#include <botan/x509_key.h> +#include <botan/pkcs8.h> +#include <botan/rng.h> + +namespace Botan { + +/************************************************* +* DL Public Key * +*************************************************/ +class BOTAN_DLL DL_Scheme_PublicKey : public virtual Public_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + const DL_Group& get_domain() const { return group; } + const BigInt& get_y() const { return y; } + const BigInt& group_p() const { return group.get_p(); } + const BigInt& group_q() const { return group.get_q(); } + const BigInt& group_g() const { return group.get_g(); } + virtual DL_Group::Format group_format() const = 0; + + X509_Encoder* x509_encoder() const; + X509_Decoder* x509_decoder(); + protected: + BigInt y; + DL_Group group; + private: + virtual void X509_load_hook() {} + }; + +/************************************************* +* DL Private Key * +*************************************************/ +class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey, + public virtual Private_Key + { + public: + bool check_key(RandomNumberGenerator& rng, bool) const; + + const BigInt& get_x() const { return x; } + + PKCS8_Encoder* pkcs8_encoder() const; + PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&); + protected: + BigInt x; + private: + virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false) {} + }; + +} + +#endif diff --git a/src/pubkey/dl_algo/info.txt b/src/pubkey/dl_algo/info.txt new file mode 100644 index 000000000..d3368765a --- /dev/null +++ b/src/pubkey/dl_algo/info.txt @@ -0,0 +1,16 @@ +realname "Discrete Logarithm PK Algorithms" + +define DL_PUBLIC_KEY_FAMILY + +load_on auto + +<add> +dl_algo.cpp +dl_algo.h +</add> + +<requires> +asn1 +bigint +dl_group +</requires> |