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
13namespace Botan {
14
20
30
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*/
46 {
47 if(n < 35 || n.is_even() || e < 2)
48 return false;
49 return true;
50 }
51
53 {
54 return DER_Encoder()
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
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
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*/
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}
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
bool is_even() const
Definition bigint.h:158
bool is_nonzero() const
Definition bigint.h:170
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
SecureVector< byte > get_contents()
Definition der_enc.cpp:122
DER_Encoder & end_cons()
Definition der_enc.cpp:145
DER_Encoder & encode(bool b)
Definition der_enc.cpp:209
bool check_key(RandomNumberGenerator &rng, bool) const
Definition if_algo.cpp:121
MemoryVector< byte > pkcs8_private_key() const
Definition if_algo.cpp:52
MemoryVector< byte > x509_subject_public_key() const
Definition if_algo.cpp:21
AlgorithmIdentifier algorithm_identifier() const
Definition if_algo.cpp:15
bool check_key(RandomNumberGenerator &rng, bool) const
Definition if_algo.cpp:45
void load_check(RandomNumberGenerator &rng) const
Definition pk_keys.cpp:40
virtual OID get_oid() const
Definition pk_keys.cpp:17
BigInt lcm(const BigInt &a, const BigInt &b)
Definition numthry.cpp:195
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition numthry.cpp:314
@ SEQUENCE
Definition asn1_int.h:35
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition numthry.h:143