3838#include " CryptoFactory.h"
3939#include " BotanCryptoFactory.h"
4040#include " ECParameters.h"
41+ #include " EDDSAMechanismParam.h"
4142#include " BotanEDKeyPair.h"
4243#include " BotanUtil.h"
4344#include < algorithm>
@@ -62,22 +63,56 @@ BotanEDDSA::~BotanEDDSA()
6263 delete verifier;
6364}
6465
66+ // Select the Botan EMSA for the requested EdDSA instance
67+ bool BotanEDDSA::selectEmsa (std::string& emsa, size_t orderLength, const MechanismParam* mechanismParam)
68+ {
69+ emsa = " Pure" ;
70+
71+ if (mechanismParam == NULL || !mechanismParam->isOfType (EDDSAMechanismParam::type))
72+ {
73+ return true ;
74+ }
75+
76+ const EDDSAMechanismParam* eddsaParam = (const EDDSAMechanismParam*) mechanismParam;
77+
78+ if (eddsaParam->contextData .size () > 0 )
79+ {
80+ ERROR_MSG (" EDDSA: Context data is not supported in the Botan 2 EDDSA implementation" );
81+ return false ;
82+ }
83+
84+ if (!eddsaParam->flag )
85+ {
86+ return true ;
87+ }
88+
89+ // Ed25519: orderLength = 32, Ed448: orderLength = 57
90+ if (orderLength == 32 )
91+ {
92+ emsa = " Ed25519ph" ;
93+ return true ;
94+ }
95+
96+ if (orderLength == 57 )
97+ {
98+ ERROR_MSG (" EDDSA: Ed448 is not supported in the Botan 2 EDDSA implementation" );
99+ return false ;
100+ }
101+
102+ ERROR_MSG (" EDDSA: Unknown EdDSA key size: %lu" , (unsigned long ) orderLength);
103+ return false ;
104+ }
105+
65106// Signing functions
66107bool BotanEDDSA::sign (PrivateKey* privateKey, const ByteString& dataToSign,
67108 ByteString& signature, const AsymMech::Type mechanism,
68- const MechanismParam* /* mechanismParam */ )
109+ const MechanismParam* mechanismParam)
69110{
70- std::string emsa;
71-
72- if (mechanism == AsymMech::EDDSA )
111+ if (mechanism != AsymMech::EDDSA )
73112 {
74- emsa = " Pure" ;
75- }
76- else
77- {
78113 ERROR_MSG (" Invalid mechanism supplied (%i)" , mechanism);
79114 return false ;
80- }
115+ }
81116
82117 // Check if the private key is the right type
83118 if (!privateKey->isOfType (BotanEDPrivateKey::type))
@@ -87,24 +122,31 @@ bool BotanEDDSA::sign(PrivateKey* privateKey, const ByteString& dataToSign,
87122 return false ;
88123 }
89124
90- BotanEDPrivateKey* pk = (BotanEDPrivateKey*) privateKey;
91- Botan::Ed25519_PrivateKey* botanKey = dynamic_cast <Botan::Ed25519_PrivateKey*>(pk->getBotanKey ());
125+ BotanEDPrivateKey* pk = (BotanEDPrivateKey*) privateKey;
126+ Botan::Ed25519_PrivateKey* botanKey = dynamic_cast <Botan::Ed25519_PrivateKey*>(pk->getBotanKey ());
92127
93- if (botanKey == NULL )
94- {
128+ if (botanKey == NULL )
129+ {
95130 ERROR_MSG (" Could not get the Botan private key" );
96131
97132 return false ;
98133 }
99134
135+ std::string emsa;
136+ if (!selectEmsa (emsa, pk->getOrderLength (), mechanismParam))
137+ {
138+ ERROR_MSG (" Could not select the EMSA for the Botan 2 EDDSA implementation" );
139+ return false ;
140+ }
141+
100142 try
101143 {
102144 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i ()->getRNG ();
103145 signer = new Botan::PK_Signer (*botanKey, *rng->getRNG (), emsa);
104146 }
105147 catch (...)
106148 {
107- ERROR_MSG (" Could not create the signer token" );
149+ ERROR_MSG (" Could not create the signer token. emsa: %s " , emsa. c_str () );
108150
109151 return false ;
110152 }
@@ -162,16 +204,10 @@ bool BotanEDDSA::signFinal(ByteString& /*signature*/)
162204// Verification functions
163205bool BotanEDDSA::verify (PublicKey* publicKey, const ByteString& originalData,
164206 const ByteString& signature, const AsymMech::Type mechanism,
165- const MechanismParam* /* mechanismParam */ )
207+ const MechanismParam* mechanismParam)
166208{
167- std::string emsa;
168-
169- if (mechanism == AsymMech::EDDSA )
209+ if (mechanism != AsymMech::EDDSA )
170210 {
171- emsa = " Pure" ;
172- }
173- else
174- {
175211 ERROR_MSG (" Invalid mechanism supplied (%i)" , mechanism);
176212
177213 return false ;
@@ -195,13 +231,20 @@ bool BotanEDDSA::verify(PublicKey* publicKey, const ByteString& originalData,
195231 return false ;
196232 }
197233
234+ std::string emsa;
235+ if (!selectEmsa (emsa, pk->getOrderLength (), mechanismParam))
236+ {
237+ ERROR_MSG (" Could not select the EMSA for the Botan 2 EDDSA implementation" );
238+ return false ;
239+ }
240+
198241 try
199242 {
200243 verifier = new Botan::PK_Verifier (*botanKey, emsa);
201244 }
202245 catch (...)
203246 {
204- ERROR_MSG (" Could not create the verifier token" );
247+ ERROR_MSG (" Could not create the verifier token. emsa: %s " , emsa. c_str () );
205248
206249 return false ;
207250 }
0 commit comments