Botan 1.10.17

if_algo.cpp
Go to the documentation of this file.
1 /*
2 * IF Scheme
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/if_algo.h>
9 #include <botan/numthry.h>
10 #include <botan/der_enc.h>
11 #include <botan/ber_dec.h>
12
13 namespace Botan {
14
15 AlgorithmIdentifier IF_Scheme_PublicKey::algorithm_identifier() const
16 {
17 return AlgorithmIdentifier(get_oid(),
18 AlgorithmIdentifier::USE_NULL_PARAM);
19 }
20
21 MemoryVector<byte> IF_Scheme_PublicKey::x509_subject_public_key() const
22 {
23 return DER_Encoder()
24 .start_cons(SEQUENCE)
25 .encode(n)
26 .encode(e)
27 .end_cons()
28 .get_contents();
29 }
30
31 IF_Scheme_PublicKey::IF_Scheme_PublicKey(const AlgorithmIdentifier&,
32 const MemoryRegion<byte>& key_bits)
33 {
34 BER_Decoder(key_bits)
35 .start_cons(SEQUENCE)
36 .decode(n)
37 .decode(e)
38 .verify_end()
39 .end_cons();
40 }
41
42 /*
43 * Check IF Scheme Public Parameters
44 */
45 bool IF_Scheme_PublicKey::check_key(RandomNumberGenerator&, bool) const
46 {
47 if(n < 35 || n.is_even() || e < 2)
48 return false;
49 return true;
50 }
51
52 MemoryVector<byte> IF_Scheme_PrivateKey::pkcs8_private_key() const
53 {
54 return DER_Encoder()
55 .start_cons(SEQUENCE)
56 .encode(static_cast<size_t>(0))
57 .encode(n)
58 .encode(e)
59 .encode(d)
60 .encode(p)
61 .encode(q)
62 .encode(d1)
63 .encode(d2)
64 .encode(c)
65 .end_cons()
66 .get_contents();
67 }
68
69 IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
70 const AlgorithmIdentifier&,
71 const MemoryRegion<byte>& key_bits)
72 {
73 BER_Decoder(key_bits)
74 .start_cons(SEQUENCE)
75 .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version")
76 .decode(n)
77 .decode(e)
78 .decode(d)
79 .decode(p)
80 .decode(q)
81 .decode(d1)
82 .decode(d2)
83 .decode(c)
84 .end_cons();
85
86 load_check(rng);
87 }
88
89 IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(RandomNumberGenerator& rng,
90 const BigInt& prime1,
91 const BigInt& prime2,
92 const BigInt& exp,
93 const BigInt& d_exp,
94 const BigInt& mod)
95 {
96 p = prime1;
97 q = prime2;
98 e = exp;
99 d = d_exp;
100 n = mod.is_nonzero() ? mod : p * q;
101
102 if(d == 0)
103 {
104 BigInt inv_for_d = lcm(p - 1, q - 1);
105 if(e.is_even())
106 inv_for_d >>= 1;
107
108 d = inverse_mod(e, inv_for_d);
109 }
110
111 d1 = d % (p - 1);
112 d2 = d % (q - 1);
113 c = inverse_mod(q, p);
114
115 load_check(rng);
116 }
117
118 /*
119 * Check IF Scheme Private Parameters
120 */
121 bool IF_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng,
122 bool strong) const
123 {
124 if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
125 return false;
126
127 if(!strong)
128 return true;
129
130 if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
131 return false;
132 if(!check_prime(p, rng) || !check_prime(q, rng))
133 return false;
134 return true;
135 }
136
137 }
Botan::IF_Scheme_PublicKey::n
BigInt n
Definition: if_algo.h:51
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey
IF_Scheme_PrivateKey()
Definition: if_algo.h:99
Botan::BigInt
Definition: bigint.h:22
Botan::IF_Scheme_PublicKey::algorithm_identifier
AlgorithmIdentifier algorithm_identifier() const
Definition: if_algo.cpp:15
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::IF_Scheme_PrivateKey::d2
BigInt d2
Definition: if_algo.h:101
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::BER_Decoder::decode_and_check
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
Botan::IF_Scheme_PublicKey::e
BigInt e
Definition: if_algo.h:51
Botan::BigInt::is_even
bool is_even() const
Definition: bigint.h:158
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::PEM_Code::decode
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:17
Botan::IF_Scheme_PrivateKey::pkcs8_private_key
MemoryVector< byte > pkcs8_private_key() const
Definition: if_algo.cpp:52
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:252
Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey
IF_Scheme_PublicKey()
Definition: if_algo.h:49
Botan::IF_Scheme_PrivateKey::d1
BigInt d1
Definition: if_algo.h:101
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::IF_Scheme_PublicKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:45
Botan::Private_Key::load_check
void load_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:40
Botan::IF_Scheme_PrivateKey::q
BigInt q
Definition: if_algo.h:101
Botan::IF_Scheme_PublicKey::x509_subject_public_key
MemoryVector< byte > x509_subject_public_key() const
Definition: if_algo.cpp:21
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:314
Botan::IF_Scheme_PrivateKey::d
BigInt d
Definition: if_algo.h:101
Botan::IF_Scheme_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:121
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
Botan::IF_Scheme_PrivateKey::c
BigInt c
Definition: if_algo.h:101
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:23
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::lcm
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:195
Botan::check_prime
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:143
Botan::IF_Scheme_PrivateKey::p
BigInt p
Definition: if_algo.h:101