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 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 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,16 +122,22 @@ 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+ return false ;
139+ }
140+
100141 try
101142 {
102143 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i ()->getRNG ();
@@ -162,16 +203,10 @@ bool BotanEDDSA::signFinal(ByteString& /*signature*/)
162203// Verification functions
163204bool BotanEDDSA::verify (PublicKey* publicKey, const ByteString& originalData,
164205 const ByteString& signature, const AsymMech::Type mechanism,
165- const MechanismParam* /* mechanismParam */ )
206+ const MechanismParam* mechanismParam)
166207{
167- std::string emsa;
168-
169- if (mechanism == AsymMech::EDDSA )
208+ if (mechanism != AsymMech::EDDSA )
170209 {
171- emsa = " Pure" ;
172- }
173- else
174- {
175210 ERROR_MSG (" Invalid mechanism supplied (%i)" , mechanism);
176211
177212 return false ;
@@ -195,6 +230,12 @@ bool BotanEDDSA::verify(PublicKey* publicKey, const ByteString& originalData,
195230 return false ;
196231 }
197232
233+ std::string emsa;
234+ if (!selectEmsa (emsa, pk->getOrderLength (), mechanismParam))
235+ {
236+ return false ;
237+ }
238+
198239 try
199240 {
200241 verifier = new Botan::PK_Verifier (*botanKey, emsa);
0 commit comments