1 /*
2 * Copyright (c) 2004 Beeyond Software Holding BV
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "beecrypt/c++/adapter.h"
24 using beecrypt::randomGeneratorContextAdapter;
25 #include "beecrypt/c++/provider/DHParameterGenerator.h"
26 #include "beecrypt/c++/security/AlgorithmParameters.h"
27 using beecrypt::security::AlgorithmParameters;
28 #include "beecrypt/c++/crypto/spec/DHParameterSpec.h"
29 using beecrypt::crypto::spec::DHParameterSpec;
30
31 using namespace beecrypt::provider;
32
DHParameterGenerator()33 DHParameterGenerator::DHParameterGenerator()
34 {
35 _size = 0;
36 _spec = 0;
37 _srng = 0;
38 }
39
~DHParameterGenerator()40 DHParameterGenerator::~DHParameterGenerator()
41 {
42 delete _spec;
43 }
44
engineGenerateParameters()45 AlgorithmParameters* DHParameterGenerator::engineGenerateParameters()
46 {
47 if (!_spec)
48 {
49 dldp_p param;
50
51 if (_srng)
52 {
53 randomGeneratorContextAdapter rngc(_srng);
54 if (dldp_pgonMakeSafe(¶m, &rngc, _size))
55 throw "unexpected error in dldp_pMake";
56 }
57 else
58 {
59 randomGeneratorContext rngc(randomGeneratorDefault());
60 if (dldp_pgonMakeSafe(¶m, &rngc, _size))
61 throw "unexpected error in dldp_pMake";
62 }
63
64 _spec = new DHParameterSpec(BigInteger(param.p), BigInteger(param.g));
65 }
66
67 try
68 {
69 AlgorithmParameters* param = AlgorithmParameters::getInstance("DH");
70
71 param->init(*_spec);
72
73 return param;
74 }
75 catch (Exception&)
76 {
77 }
78
79 return 0;
80 }
81
engineInit(const AlgorithmParameterSpec & spec,SecureRandom * random)82 void DHParameterGenerator::engineInit(const AlgorithmParameterSpec& spec, SecureRandom* random) throw (InvalidAlgorithmParameterException)
83 {
84 const DHParameterSpec* dhspec = dynamic_cast<const DHParameterSpec*>(&spec);
85
86 if (dhspec)
87 {
88 delete _spec;
89
90 _spec = new DHParameterSpec(*dhspec);
91 _srng = random;
92 }
93 else
94 throw InvalidAlgorithmParameterException("expected DHParameterSpec");
95 }
96
engineInit(int keysize,SecureRandom * random)97 void DHParameterGenerator::engineInit(int keysize, SecureRandom* random) throw (InvalidParameterException)
98 {
99 if ((keysize < 768) || ((keysize & 0x3f) != 0))
100 throw InvalidParameterException("Prime size must be greater than 768 and be a multiple of 64");
101
102 delete _spec;
103
104 _size = keysize;
105 _spec = 0;
106 _srng = random;
107 }
108