Botan  1.11.15
src/lib/pubkey/dl_algo/dl_algo.cpp
Go to the documentation of this file.
00001 /*
00002 * DL Scheme
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/dl_algo.h>
00009 #include <botan/numthry.h>
00010 #include <botan/workfactor.h>
00011 #include <botan/der_enc.h>
00012 #include <botan/ber_dec.h>
00013 
00014 namespace Botan {
00015 
00016 size_t DL_Scheme_PublicKey::estimated_strength() const
00017    {
00018    return dl_work_factor(group.get_p().bits());
00019    }
00020 
00021 AlgorithmIdentifier DL_Scheme_PublicKey::algorithm_identifier() const
00022    {
00023    return AlgorithmIdentifier(get_oid(),
00024                               group.DER_encode(group_format()));
00025    }
00026 
00027 std::vector<byte> DL_Scheme_PublicKey::x509_subject_public_key() const
00028    {
00029    return DER_Encoder().encode(y).get_contents_unlocked();
00030    }
00031 
00032 DL_Scheme_PublicKey::DL_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
00033                                          const secure_vector<byte>& key_bits,
00034                                          DL_Group::Format format)
00035    {
00036    group.BER_decode(alg_id.parameters, format);
00037 
00038    BER_Decoder(key_bits).decode(y);
00039    }
00040 
00041 secure_vector<byte> DL_Scheme_PrivateKey::pkcs8_private_key() const
00042    {
00043    return DER_Encoder().encode(x).get_contents();
00044    }
00045 
00046 DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
00047                                            const secure_vector<byte>& key_bits,
00048                                            DL_Group::Format format)
00049    {
00050    group.BER_decode(alg_id.parameters, format);
00051 
00052    BER_Decoder(key_bits).decode(x);
00053    }
00054 
00055 /*
00056 * Check Public DL Parameters
00057 */
00058 bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng,
00059                                     bool strong) const
00060    {
00061    if(y < 2 || y >= group_p())
00062       return false;
00063    if(!group.verify_group(rng, strong))
00064       return false;
00065    return true;
00066    }
00067 
00068 /*
00069 * Check DL Scheme Private Parameters
00070 */
00071 bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng,
00072                                      bool strong) const
00073    {
00074    const BigInt& p = group_p();
00075    const BigInt& g = group_g();
00076 
00077    if(y < 2 || y >= p || x < 2 || x >= p)
00078       return false;
00079    if(!group.verify_group(rng, strong))
00080       return false;
00081 
00082    if(!strong)
00083       return true;
00084 
00085    if(y != power_mod(g, x, p))
00086       return false;
00087 
00088    return true;
00089    }
00090 
00091 }