aboutsummaryrefslogtreecommitdiff
path: root/src/Signature.cpp
blob: 946d754236a7fdf7d25c8db9d7e8a853d1222148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "../include/Signature.hpp"
#include <sodium/crypto_sign_ed25519.h>
#include <sodium/utils.h>
#include <cstring>

using namespace std;

namespace odhtdb
{
    namespace Signature
    {
        PublicKey PublicKey::ZERO("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", PUBLIC_KEY_NUM_BYTES);
        
        PublicKey::PublicKey(const char *_data, size_t size)
        {
            if(size != PUBLIC_KEY_NUM_BYTES)
            {
                string errMsg = "Expected public key size to be ";
                errMsg += to_string(PUBLIC_KEY_NUM_BYTES);
                errMsg += " bytes, was: ";
                errMsg += to_string(size);
                throw InvalidSignatureKeySize(errMsg);
            }
            memmove(data, _data, PUBLIC_KEY_NUM_BYTES);
        }
        
        PublicKey::PublicKey(const PublicKey &other)
        {
            memmove(data, other.data, PUBLIC_KEY_NUM_BYTES);
        }
        
        PublicKey& PublicKey::operator=(const PublicKey &other)
        {
            memmove(data, other.data, PUBLIC_KEY_NUM_BYTES);
            return *this;
        }
        
        string PublicKey::toString() const
        {
            string result;
            result.resize(PUBLIC_KEY_NUM_BYTES * 2);
            sodium_bin2hex(&result[0], PUBLIC_KEY_NUM_BYTES * 2 + 1, (const unsigned char*)data, PUBLIC_KEY_NUM_BYTES);
            return result;
        }
        
        PrivateKey::PrivateKey(const char *_data, size_t size)
        {
            if(size != PRIVATE_KEY_NUM_BYTES)
            {
                string errMsg = "Expected private key size to be ";
                errMsg += to_string(PRIVATE_KEY_NUM_BYTES);
                errMsg += " bytes, was: ";
                errMsg += to_string(size);
                throw InvalidSignatureKeySize(errMsg);
            }
            memmove(data, _data, PRIVATE_KEY_NUM_BYTES);
        }
        
        PrivateKey::PrivateKey(const PrivateKey &other)
        {
            memmove(data, other.data, PRIVATE_KEY_NUM_BYTES);
        }
        
        PrivateKey& PrivateKey::operator=(const PrivateKey &other)
        {
            memmove(data, other.data, PRIVATE_KEY_NUM_BYTES);
            return *this;
        }
        
        string PrivateKey::sign(const string &dataToSign) const
        {
            string result;
            result.resize(crypto_sign_ed25519_BYTES + dataToSign.size());
            unsigned long long resultSize;
            
            if(crypto_sign_ed25519((unsigned char*)&result[0], &resultSize, (unsigned char*)dataToSign.data(), dataToSign.size(), (unsigned char*)data) != 0)
                throw DataSignException("Failed to sign data. Is private key invalid?");
            
            if(resultSize != result.size())
                throw DataSignException("Failed to sign data. The signed data is not of expected size (bug)");
            
            return result;
        }
        
        string PrivateKey::toString() const
        {
            string result;
            result.resize(PRIVATE_KEY_NUM_BYTES * 2);
            sodium_bin2hex(&result[0], PRIVATE_KEY_NUM_BYTES * 2 + 1, (const unsigned char*)data, PRIVATE_KEY_NUM_BYTES);
            return result;
        }
        
        KeyPair::KeyPair()
        {
            if(crypto_sign_ed25519_keypair((unsigned char*)publicKey.data, (unsigned char*)privateKey.data) != 0)
                throw SignatureGenerationException("Failed to generate signature keypair");
        }
    }
}