aboutsummaryrefslogtreecommitdiffstats
path: root/src/if_algo.cpp
blob: 2a96163f7b540e426f5daff08cf1f5e59fa55c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*************************************************
* IF Scheme Source File                          *
* (C) 1999-2007 The Botan Project                *
*************************************************/

#include <botan/if_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* IF_Scheme_PublicKey::x509_encoder() const
   {
   class IF_Scheme_Encoder : public X509_Encoder
      {
      public:
         AlgorithmIdentifier alg_id() const
            {
            return AlgorithmIdentifier(key->get_oid(),
                                       AlgorithmIdentifier::USE_NULL_PARAM);
            }

         MemoryVector<byte> key_bits() const
            {
            return DER_Encoder()
               .start_cons(SEQUENCE)
                  .encode(key->n)
                  .encode(key->e)
               .end_cons()
            .get_contents();
            }

         IF_Scheme_Encoder(const IF_Scheme_PublicKey* k) : key(k) {}
      private:
         const IF_Scheme_PublicKey* key;
      };

   return new IF_Scheme_Encoder(this);
   }

/*************************************************
* Return the X.509 public key decoder            *
*************************************************/
X509_Decoder* IF_Scheme_PublicKey::x509_decoder()
   {
   class IF_Scheme_Decoder : public X509_Decoder
      {
      public:
         void alg_id(const AlgorithmIdentifier&) {}

         void key_bits(const MemoryRegion<byte>& bits)
            {
            BER_Decoder(bits)
               .start_cons(SEQUENCE)
               .decode(key->n)
               .decode(key->e)
               .verify_end()
               .end_cons();

            key->X509_load_hook();
            }

         IF_Scheme_Decoder(IF_Scheme_PublicKey* k) : key(k) {}
      private:
         IF_Scheme_PublicKey* key;
      };

   return new IF_Scheme_Decoder(this);
   }

/*************************************************
* Return the PKCS #8 public key encoder          *
*************************************************/
PKCS8_Encoder* IF_Scheme_PrivateKey::pkcs8_encoder() const
   {
   class IF_Scheme_Encoder : public PKCS8_Encoder
      {
      public:
         AlgorithmIdentifier alg_id() const
            {
            return AlgorithmIdentifier(key->get_oid(),
                                       AlgorithmIdentifier::USE_NULL_PARAM);
            }

         MemoryVector<byte> key_bits() const
            {
            return DER_Encoder()
               .start_cons(SEQUENCE)
                  .encode((u32bit)0)
                  .encode(key->n)
                  .encode(key->e)
                  .encode(key->d)
                  .encode(key->p)
                  .encode(key->q)
                  .encode(key->d1)
                  .encode(key->d2)
                  .encode(key->c)
               .end_cons()
            .get_contents();
            }

         IF_Scheme_Encoder(const IF_Scheme_PrivateKey* k) : key(k) {}
      private:
         const IF_Scheme_PrivateKey* key;
      };

   return new IF_Scheme_Encoder(this);
   }

/*************************************************
* Return the PKCS #8 public key decoder          *
*************************************************/
PKCS8_Decoder* IF_Scheme_PrivateKey::pkcs8_decoder()
   {
   class IF_Scheme_Decoder : public PKCS8_Decoder
      {
      public:
         void alg_id(const AlgorithmIdentifier&) {}

         void key_bits(const MemoryRegion<byte>& bits)
            {
            u32bit version;

            BER_Decoder(bits)
               .start_cons(SEQUENCE)
                  .decode(version)
                  .decode(key->n)
                  .decode(key->e)
                  .decode(key->d)
                  .decode(key->p)
                  .decode(key->q)
                  .decode(key->d1)
                  .decode(key->d2)
                  .decode(key->c)
               .end_cons();

            if(version != 0)
               throw Decoding_Error("Unknown PKCS #1 key format version");

            key->PKCS8_load_hook();
            }

         IF_Scheme_Decoder(IF_Scheme_PrivateKey* k) : key(k) {}
      private:
         IF_Scheme_PrivateKey* key;
      };

   return new IF_Scheme_Decoder(this);
   }

/*************************************************
* Algorithm Specific X.509 Initialization Code   *
*************************************************/
void IF_Scheme_PublicKey::X509_load_hook()
   {
   core = IF_Core(e, n);
   load_check();
   }

/*************************************************
* Algorithm Specific PKCS #8 Initialization Code *
*************************************************/
void IF_Scheme_PrivateKey::PKCS8_load_hook(bool generated)
   {
   if(n == 0)  n = p * q;
   if(d1 == 0) d1 = d % (p - 1);
   if(d2 == 0) d2 = d % (q - 1);
   if(c == 0)  c = inverse_mod(q, p);

   core = IF_Core(e, n, d, p, q, d1, d2, c);

   if(generated)
      gen_check();
   else
      load_check();
   }

/*************************************************
* Check IF Scheme Public Parameters              *
*************************************************/
bool IF_Scheme_PublicKey::check_key(bool) const
   {
   if(n < 35 || n.is_even() || e < 2)
      return false;
   return true;
   }

/*************************************************
* Check IF Scheme Private Parameters             *
*************************************************/
bool IF_Scheme_PrivateKey::check_key(bool strong) const
   {
   if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
      return false;

   if(!strong)
      return true;

   if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
      return false;
   if(!check_prime(p) || !check_prime(q))
      return false;
   return true;
   }

}