monotone

monotone Mtn Source Tree

Root/botan/dl_algo.cpp

1/*************************************************
2* DL Scheme Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/dl_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* DL_Scheme_PublicKey::x509_encoder() const
17 {
18 class DL_Scheme_Encoder : public X509_Encoder
19 {
20 public:
21 AlgorithmIdentifier alg_id() const
22 {
23 MemoryVector<byte> group =
24 key->group.DER_encode(key->group_format());
25
26 return AlgorithmIdentifier(key->get_oid(), group);
27 }
28
29 MemoryVector<byte> key_bits() const
30 {
31 return DER_Encoder().encode(key->y).get_contents();
32 }
33
34 DL_Scheme_Encoder(const DL_Scheme_PublicKey* k) : key(k) {}
35 private:
36 const DL_Scheme_PublicKey* key;
37 };
38
39 return new DL_Scheme_Encoder(this);
40 }
41
42/*************************************************
43* Return the X.509 public key decoder *
44*************************************************/
45X509_Decoder* DL_Scheme_PublicKey::x509_decoder()
46 {
47 class DL_Scheme_Decoder : public X509_Decoder
48 {
49 public:
50 void alg_id(const AlgorithmIdentifier& alg_id)
51 {
52 DataSource_Memory source(alg_id.parameters);
53 key->group.BER_decode(source, key->group_format());
54 }
55
56 void key_bits(const MemoryRegion<byte>& bits)
57 {
58 BER_Decoder(bits).decode(key->y);
59 key->X509_load_hook();
60 }
61
62 DL_Scheme_Decoder(DL_Scheme_PublicKey* k) : key(k) {}
63 private:
64 DL_Scheme_PublicKey* key;
65 };
66
67 return new DL_Scheme_Decoder(this);
68 }
69
70/*************************************************
71* Return the PKCS #8 private key encoder *
72*************************************************/
73PKCS8_Encoder* DL_Scheme_PrivateKey::pkcs8_encoder() const
74 {
75 class DL_Scheme_Encoder : public PKCS8_Encoder
76 {
77 public:
78 AlgorithmIdentifier alg_id() const
79 {
80 MemoryVector<byte> group =
81 key->group.DER_encode(key->group_format());
82
83 return AlgorithmIdentifier(key->get_oid(), group);
84 }
85
86 MemoryVector<byte> key_bits() const
87 {
88 return DER_Encoder().encode(key->x).get_contents();
89 }
90
91 DL_Scheme_Encoder(const DL_Scheme_PrivateKey* k) : key(k) {}
92 private:
93 const DL_Scheme_PrivateKey* key;
94 };
95
96 return new DL_Scheme_Encoder(this);
97 }
98
99/*************************************************
100* Return the PKCS #8 private key decoder *
101*************************************************/
102PKCS8_Decoder* DL_Scheme_PrivateKey::pkcs8_decoder()
103 {
104 class DL_Scheme_Decoder : public PKCS8_Decoder
105 {
106 public:
107 void alg_id(const AlgorithmIdentifier& alg_id)
108 {
109 DataSource_Memory source(alg_id.parameters);
110 key->group.BER_decode(source, key->group_format());
111 }
112
113 void key_bits(const MemoryRegion<byte>& bits)
114 {
115 BER_Decoder(bits).decode(key->x);
116 key->PKCS8_load_hook();
117 }
118
119 DL_Scheme_Decoder(DL_Scheme_PrivateKey* k) : key(k) {}
120 private:
121 DL_Scheme_PrivateKey* key;
122 };
123
124 return new DL_Scheme_Decoder(this);
125 }
126
127/*************************************************
128* Check Public DL Parameters *
129*************************************************/
130bool DL_Scheme_PublicKey::check_key(bool strong) const
131 {
132 if(y < 2 || y >= group_p())
133 return false;
134 if(!group.verify_group(strong))
135 return false;
136 return true;
137 }
138
139/*************************************************
140* Check DL Scheme Private Parameters *
141*************************************************/
142bool DL_Scheme_PrivateKey::check_key(bool strong) const
143 {
144 const BigInt& p = group_p();
145 const BigInt& g = group_g();
146
147 if(y < 2 || y >= p || x < 2 || x >= p)
148 return false;
149 if(!group.verify_group(strong))
150 return false;
151
152 if(!strong)
153 return true;
154
155 if(y != power_mod(g, x, p))
156 return false;
157
158 return true;
159 }
160
161}

Archive Download this file

Branches

Tags

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