monotone

monotone Mtn Source Tree

Root/botan/if_algo.cpp

1/*************************************************
2* IF Scheme Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/if_algo.h>
7#include <botan/numthry.h>
8#include <botan/asn1.h>
9
10namespace Botan {
11
12/*************************************************
13* Return the X.509 public key encoding *
14*************************************************/
15MemoryVector<byte> IF_Scheme_PublicKey::DER_encode_pub() const
16 {
17 DER_Encoder encoder;
18 encoder.start_sequence();
19 DER::encode(encoder, n);
20 DER::encode(encoder, e);
21 encoder.end_sequence();
22 return encoder.get_contents();
23 }
24
25/*************************************************
26* Return the X.509 parameters encoding *
27*************************************************/
28MemoryVector<byte> IF_Scheme_PublicKey::DER_encode_params() const
29 {
30 DER_Encoder encoder;
31 DER::encode_null(encoder);
32 return encoder.get_contents();
33 }
34
35/*************************************************
36* Decode X.509 public key encoding *
37*************************************************/
38void IF_Scheme_PublicKey::BER_decode_pub(DataSource& source)
39 {
40 BER_Decoder decoder(source);
41 BER_Decoder sequence = BER::get_subsequence(decoder);
42 BER::decode(sequence, n);
43 BER::decode(sequence, e);
44 sequence.verify_end();
45
46 X509_load_hook();
47 }
48
49/*************************************************
50* Decode X.509 algorithm parameters *
51*************************************************/
52void IF_Scheme_PublicKey::BER_decode_params(DataSource& source)
53 {
54 byte dummy = 0;
55 while(!source.end_of_data())
56 source.read_byte(dummy);
57 }
58
59/*************************************************
60* Return the PKCS #1 private key encoding *
61*************************************************/
62SecureVector<byte> IF_Scheme_PrivateKey::DER_encode_priv() const
63 {
64 DER_Encoder encoder;
65 encoder.start_sequence();
66 DER::encode(encoder, 0);
67 DER::encode(encoder, n);
68 DER::encode(encoder, e);
69 DER::encode(encoder, d);
70 DER::encode(encoder, p);
71 DER::encode(encoder, q);
72 DER::encode(encoder, d1);
73 DER::encode(encoder, d2);
74 DER::encode(encoder, c);
75 encoder.end_sequence();
76 return encoder.get_contents();
77 }
78
79/*************************************************
80* Decode a PKCS #1 private key encoding *
81*************************************************/
82void IF_Scheme_PrivateKey::BER_decode_priv(DataSource& source)
83 {
84 u32bit version;
85
86 BER_Decoder decoder(source);
87 BER_Decoder sequence = BER::get_subsequence(decoder);
88 BER::decode(sequence, version);
89 if(version != 0)
90 throw Decoding_Error(algo_name() + ": Unknown PKCS #1 key version");
91 BER::decode(sequence, n);
92 BER::decode(sequence, e);
93 BER::decode(sequence, d);
94 BER::decode(sequence, p);
95 BER::decode(sequence, q);
96 BER::decode(sequence, d1);
97 BER::decode(sequence, d2);
98 BER::decode(sequence, c);
99 sequence.verify_end();
100
101 PKCS8_load_hook();
102 check_loaded_private();
103 }
104
105/*************************************************
106* Algorithm Specific X.509 Initialization Code *
107*************************************************/
108void IF_Scheme_PublicKey::X509_load_hook()
109 {
110 core = IF_Core(e, n);
111 check_loaded_public();
112 }
113
114/*************************************************
115* Algorithm Specific PKCS #8 Initialization Code *
116*************************************************/
117void IF_Scheme_PrivateKey::PKCS8_load_hook()
118 {
119 if(n == 0) n = p * q;
120 if(d1 == 0) d1 = d % (p - 1);
121 if(d2 == 0) d2 = d % (q - 1);
122 if(c == 0) c = inverse_mod(q, p);
123
124 core = IF_Core(e, n, d, p, q, d1, d2, c);
125 }
126
127/*************************************************
128* Check IF Scheme Public Parameters *
129*************************************************/
130bool IF_Scheme_PublicKey::check_key(bool) const
131 {
132 if(n < 35 || n.is_even() || e < 2)
133 return false;
134 return true;
135 }
136
137/*************************************************
138* Check IF Scheme Private Parameters *
139*************************************************/
140bool IF_Scheme_PrivateKey::check_key(bool strong) const
141 {
142 if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
143 return false;
144
145 if(!strong)
146 return true;
147
148 if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
149 return false;
150 if(!check_prime(p) || !check_prime(q))
151 return false;
152 return true;
153 }
154
155}

Archive Download this file

Branches

Tags

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