monotone

monotone Mtn Source Tree

Root/botan/if_algo.cpp

1/*************************************************
2* IF Scheme Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/if_algo.h>
7#include <botan/numthry.h>
8#include <botan/der_enc.h>
9#include <botan/ber_dec.h>
10
11namespace Botan {
12
13/*************************************************
14* Return the X.509 public key encoder *
15*************************************************/
16X509_Encoder* IF_Scheme_PublicKey::x509_encoder() const
17 {
18 class IF_Scheme_Encoder : public X509_Encoder
19 {
20 public:
21 AlgorithmIdentifier alg_id() const
22 {
23 return AlgorithmIdentifier(key->get_oid(),
24 AlgorithmIdentifier::USE_NULL_PARAM);
25 }
26
27 MemoryVector<byte> key_bits() const
28 {
29 return DER_Encoder()
30 .start_cons(SEQUENCE)
31 .encode(key->n)
32 .encode(key->e)
33 .end_cons()
34 .get_contents();
35 }
36
37 IF_Scheme_Encoder(const IF_Scheme_PublicKey* k) : key(k) {}
38 private:
39 const IF_Scheme_PublicKey* key;
40 };
41
42 return new IF_Scheme_Encoder(this);
43 }
44
45/*************************************************
46* Return the X.509 public key decoder *
47*************************************************/
48X509_Decoder* IF_Scheme_PublicKey::x509_decoder()
49 {
50 class IF_Scheme_Decoder : public X509_Decoder
51 {
52 public:
53 void alg_id(const AlgorithmIdentifier&) {}
54
55 void key_bits(const MemoryRegion<byte>& bits)
56 {
57 BER_Decoder(bits)
58 .start_cons(SEQUENCE)
59 .decode(key->n)
60 .decode(key->e)
61 .verify_end()
62 .end_cons();
63
64 key->X509_load_hook();
65 }
66
67 IF_Scheme_Decoder(IF_Scheme_PublicKey* k) : key(k) {}
68 private:
69 IF_Scheme_PublicKey* key;
70 };
71
72 return new IF_Scheme_Decoder(this);
73 }
74
75/*************************************************
76* Return the PKCS #8 public key encoder *
77*************************************************/
78PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const
79 {
80 class IF_Scheme_Encoder : public PKCS8_Encoder
81 {
82 public:
83 AlgorithmIdentifier alg_id() const
84 {
85 return AlgorithmIdentifier(key->get_oid(),
86 AlgorithmIdentifier::USE_NULL_PARAM);
87 }
88
89 MemoryVector<byte> key_bits() const
90 {
91 return DER_Encoder()
92 .start_cons(SEQUENCE)
93 .encode((u32bit)0)
94 .encode(key->n)
95 .encode(key->e)
96 .encode(key->d)
97 .encode(key->p)
98 .encode(key->q)
99 .encode(key->d1)
100 .encode(key->d2)
101 .encode(key->c)
102 .end_cons()
103 .get_contents();
104 }
105
106 IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {}
107 private:
108 const IF_Scheme_PrivateKey* key;
109 };
110
111 return new IF_Scheme_Encoder(this);
112 }
113
114/*************************************************
115* Return the PKCS #8 public key decoder *
116*************************************************/
117PKCS8_Decoder* IF_Scheme_PrivateKey::pkcs8_decoder()
118 {
119 class IF_Scheme_Decoder : public PKCS8_Decoder
120 {
121 public:
122 void alg_id(const AlgorithmIdentifier&) {}
123
124 void key_bits(const MemoryRegion<byte>& bits)
125 {
126 u32bit version;
127
128 BER_Decoder(bits)
129 .start_cons(SEQUENCE)
130 .decode(version)
131 .decode(key->n)
132 .decode(key->e)
133 .decode(key->d)
134 .decode(key->p)
135 .decode(key->q)
136 .decode(key->d1)
137 .decode(key->d2)
138 .decode(key->c)
139 .end_cons();
140
141 if(version != 0)
142 throw Decoding_Error("Unknown PKCS #1 key format version");
143
144 key->PKCS8_load_hook();
145 }
146
147 IF_Scheme_Decoder(IF_Scheme_PrivateKey* k) : key(k) {}
148 private:
149 IF_Scheme_PrivateKey* key;
150 };
151
152 return new IF_Scheme_Decoder(this);
153 }
154
155/*************************************************
156* Algorithm Specific X.509 Initialization Code *
157*************************************************/
158void IF_Scheme_PublicKey::X509_load_hook()
159 {
160 core = IF_Core(e, n);
161 load_check();
162 }
163
164/*************************************************
165* Algorithm Specific PKCS #8 Initialization Code *
166*************************************************/
167void IF_Scheme_PrivateKey::PKCS8_load_hook(bool generated)
168 {
169 if(n == 0) n = p * q;
170 if(d1 == 0) d1 = d % (p - 1);
171 if(d2 == 0) d2 = d % (q - 1);
172 if(c == 0) c = inverse_mod(q, p);
173
174 core = IF_Core(e, n, d, p, q, d1, d2, c);
175
176 if(generated)
177 gen_check();
178 else
179 load_check();
180 }
181
182/*************************************************
183* Check IF Scheme Public Parameters *
184*************************************************/
185bool IF_Scheme_PublicKey::check_key(bool) const
186 {
187 if(n < 35 || n.is_even() || e < 2)
188 return false;
189 return true;
190 }
191
192/*************************************************
193* Check IF Scheme Private Parameters *
194*************************************************/
195bool IF_Scheme_PrivateKey::check_key(bool strong) const
196 {
197 if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
198 return false;
199
200 if(!strong)
201 return true;
202
203 if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
204 return false;
205 if(!check_prime(p) || !check_prime(q))
206 return false;
207 return true;
208 }
209
210}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status