#include "config.h"
#endif
#include "ext/ed25519/crypto_sign.h"
+#include "ext/ed25519/crypto_hash_sha512.h"
}
#include "dnssecinfra.hh"
#include <boost/scoped_ptr.hpp>
using boost::scoped_ptr;
+#define SECRETBYTES SECRETKEYBYTES-PUBLICKEYBYTES
+
class ED25519DNSCryptoKeyEngine : public DNSCryptoKeyEngine
{
public:
}
private:
- unsigned int d_algorithm;
unsigned char d_pubkey[PUBLICKEYBYTES];
unsigned char d_seckey[SECRETKEYBYTES];
DNSCryptoKeyEngine::storvector_t ED25519DNSCryptoKeyEngine::convertToISCVector() const
{
- /*Algorithm: 13 (ED25519P256SHA256)
- PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ= */
+ /*
+ Private-key-format: v1.2
+ Algorithm: 250 (ED25519SHA512)
+ PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ=
+ */
+
storvector_t storvector;
- string algorithm = "250 (ED25519)";
-
+ string algorithm = "250 (ED25519SHA512)";
+
storvector.push_back(make_pair("Algorithm", algorithm));
vector<unsigned char> buffer;
- storvector.push_back(make_pair("PrivateKey", string((char*)d_seckey, (char*)d_seckey+SECRETKEYBYTES)));
+ storvector.push_back(make_pair("PrivateKey", string((char*)d_seckey, SECRETBYTES)));
return storvector;
}
void ED25519DNSCryptoKeyEngine::fromISCMap(DNSKEYRecordContent& drc, std::map<std::string, std::string>& stormap )
{
- /*Private-key-format: v1.2
- Algorithm: 250 (ED25519)
- PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ= */
-
- d_algorithm = drc.d_algorithm = atoi(stormap["algorithm"].c_str());
+ /*
+ Private-key-format: v1.2
+ Algorithm: 250 (ED25519SHA512)
+ PrivateKey: GU6SnQ/Ou+xC5RumuIUIuJZteXT2z0O/ok1s38Et6mQ=
+ */
+
+ drc.d_algorithm = atoi(stormap["algorithm"].c_str());
string privateKey = stormap["privatekey"];
- memcpy(d_seckey, privateKey.c_str(), SECRETKEYBYTES);
- memcpy(d_pubkey, privateKey.c_str() + PUBLICKEYBYTES, PUBLICKEYBYTES);
- // need to set d_pubkey too..
+ memcpy(d_seckey, privateKey.c_str(), SECRETBYTES);
+ crypto_sign_publickey(d_pubkey, d_seckey, d_seckey);
+ //memcpy(d_pubkey, privateKey.c_str() + SECRETBYTES, PUBLICKEYBYTES);
}
// used for the cache, nothing external
std::string ED25519DNSCryptoKeyEngine::sign(const std::string& msg) const
{
- // full signature, including us making the hash from the message
- unsigned long long smlen = msg.length() + SIGNATUREBYTES;
- scoped_ptr<unsigned char> sm(new unsigned char[smlen]);
+ string hash=this->hash(msg);
+ unsigned long long smlen = hash.length() + SIGNATUREBYTES;
+
+ scoped_ptr<unsigned char> sm(new unsigned char[smlen]);
+ crypto_sign(sm.get(), &smlen, (const unsigned char*)hash.c_str(), hash.length(), d_seckey);
- crypto_sign(sm.get(), &smlen, (const unsigned char*)msg.c_str(), msg.length(), d_seckey);
-
return string((const char*)sm.get(), SIGNATUREBYTES);
}
std::string ED25519DNSCryptoKeyEngine::hash(const std::string& orig) const
{
- throw runtime_error("hash not implemented");
- return ""; // probably SHA512 for ED25519
+ unsigned char out[crypto_hash_sha512_BYTES];
+ crypto_hash_sha512(out, (const unsigned char*)orig.c_str(), orig.length());
+
+ return string((char*)out, crypto_hash_sha512_BYTES);
}
bool ED25519DNSCryptoKeyEngine::verify(const std::string& msg, const std::string& signature) const
{
- // we have to do the hash too
- // full signature, including us making the hash from the message
- unsigned long long smlen = msg.length() + SIGNATUREBYTES;
- scoped_ptr<unsigned char> sm(new unsigned char[smlen]);
+ string hash=this->hash(msg);
+ unsigned long long smlen = hash.length() + SIGNATUREBYTES;
+ scoped_ptr<unsigned char> sm(new unsigned char[smlen]);
memcpy(sm.get(), signature.c_str(), SIGNATUREBYTES);
- memcpy(sm.get() + SIGNATUREBYTES, msg.c_str(), msg.length());
-
- scoped_ptr<unsigned char> m(new unsigned char[smlen]);
+ memcpy(sm.get() + SIGNATUREBYTES, hash.c_str(), hash.length());
+
+ scoped_ptr<unsigned char> m(new unsigned char[smlen]);
return crypto_sign_open(m.get(), &smlen, sm.get(), smlen, d_pubkey) == 0;
}