00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __DSL_SODIUM_KEYS_ENCRYPTION__
00012 #define __DSL_SODIUM_KEYS_ENCRYPTION__
00013
00018 #define ENC_PUBKEY_SIZE_BYTES (crypto_box_PUBLICKEYBYTES + 1)
00019 #define ENC_PRIVKEY_SIZE_BYTES (crypto_box_SECRETKEYBYTES + 1)
00020 #define ENC_NONCE_SIZE_BYTES crypto_box_NONCEBYTES
00021 #define ENC_SHARED_KEY_BYTE_SIZE crypto_box_BEFORENMBYTES
00022 #define ENC_NONCE_INT_TYPE uint32_t
00023
00024 #if ENC_PUBKEY_SIZE_BYTES < ENC_NONCE_SIZE_BYTES
00025 #error Uh-oh
00026 #endif
00027 #if ENC_PUBKEY_SIZE_BYTES < randombytes_SEEDBYTES
00028 #error Uh-oh
00029 #endif
00030
00031
00032 #define ENC_VER_PUBKEY 0x5A
00033 #define ENC_VER_PRIVKEY 0x43
00034
00035 class DSL_SODIUM_API_CLASS DS_EncNonce {
00036 public:
00037 uint8_t data[ENC_NONCE_SIZE_BYTES];
00038
00039 DS_EncNonce();
00040 void SetNull();
00041 bool IsValid();
00042
00043 void Generate();
00044 ENC_NONCE_INT_TYPE GetIncrement();
00045 void SetIncrement(ENC_NONCE_INT_TYPE x);
00046 void Increment();
00047
00048 bool SetFromHexString(string str);
00049 bool SetFromBinaryData(const uint8_t * pdata, size_t len);
00050
00051 bool operator == (const DS_EncNonce &b) const {
00052 return (sodium_memcmp(data, b.data, ENC_NONCE_SIZE_BYTES) == 0) ? true : false;
00053 }
00054 bool operator != (const DS_EncNonce &b) const {
00055 return (sodium_memcmp(data, b.data, ENC_NONCE_SIZE_BYTES) != 0) ? true : false;
00056 }
00057 bool operator < (const DS_EncNonce &b) const {
00058 if (memcmp(data, b.data, ENC_NONCE_SIZE_BYTES) < 0) {
00059 return true;
00060 }
00061 return false;
00062 }
00063 };
00064
00065 class DS_EncPubKey;
00066 class DS_EncPrivKey;
00067
00068 class DSL_SODIUM_API_CLASS DS_EncSharedKey {
00069 private:
00070 bool fLocked;
00071 void checkLocking(bool fForce = false);
00072
00073 public:
00074 uint8_t data[ENC_SHARED_KEY_BYTE_SIZE];
00075
00076 DS_EncSharedKey();
00077 ~DS_EncSharedKey();
00078
00079 void SetNull();
00080 bool IsValid();
00081
00082 bool Calculate(DS_EncPrivKey& privkey, DS_EncPubKey& pubkey);
00083 bool Encrypt(DS_EncNonce& nonce, DSL_BUFFER * buf);
00084 bool Decrypt(DS_EncNonce& nonce, DSL_BUFFER * buf);
00085
00086 bool operator == (const DS_EncSharedKey &b) const {
00087 return (sodium_memcmp(data, b.data, ENC_SHARED_KEY_BYTE_SIZE) == 0) ? true : false;
00088 }
00089 bool operator != (const DS_EncSharedKey &b) const {
00090 return (sodium_memcmp(data, b.data, ENC_SHARED_KEY_BYTE_SIZE) != 0) ? true : false;
00091 }
00092 bool operator < (const DS_EncSharedKey &b) const {
00093 if (memcmp(data, b.data, ENC_SHARED_KEY_BYTE_SIZE) < 0) {
00094 return true;
00095 }
00096 return false;
00097 }
00098 };
00099
00100 class DSL_SODIUM_API_CLASS DS_EncPubKey {
00101 public:
00102 uint8_t key[ENC_PUBKEY_SIZE_BYTES];
00103
00104 DS_EncPubKey();
00105
00106 string GetString();
00107 void SetNull();
00108 bool IsValid();
00109
00110 bool SetFromHexString(string str);
00111 bool SetFromBinaryData(const uint8_t * pdata, size_t len);
00112
00113 bool operator == (const DS_EncPubKey &b) const {
00114 return (sodium_memcmp(key, b.key, ENC_PUBKEY_SIZE_BYTES) == 0) ? true : false;
00115 }
00116 bool operator != (const DS_EncPubKey &b) const {
00117 return (sodium_memcmp(key, b.key, ENC_PUBKEY_SIZE_BYTES) != 0) ? true : false;
00118 }
00119 bool operator < (const DS_EncPubKey &b) const {
00120 if (memcmp(key, b.key, ENC_PUBKEY_SIZE_BYTES) < 0) {
00121 return true;
00122 }
00123 return false;
00124 }
00125 };
00126
00127 class DSL_SODIUM_API_CLASS DS_EncPrivKey {
00128 private:
00129 void updatePubKey();
00130 bool fLocked;
00131 void checkLocking(bool fForce = false);
00132
00133 public:
00134 uint8_t key[ENC_PRIVKEY_SIZE_BYTES];
00135 DS_EncPubKey pubkey;
00136
00137 DS_EncPrivKey();
00138 ~DS_EncPrivKey();
00139
00140 string GetString();
00141 void SetNull();
00142 bool IsValid();
00143
00144 bool Encrypt(DS_EncPubKey& recpt_key, DS_EncNonce& nonce, DSL_BUFFER * buf);
00145 bool Decrypt(DS_EncPubKey& sender_key, DS_EncNonce& nonce, DSL_BUFFER * buf);
00146 bool Generate();
00147
00148 bool SetFromHexString(string str);
00149 bool SetFromBinaryData(const uint8_t * pdata, size_t len);
00150 bool SetBothFromBinaryData(const uint8_t * sdata, size_t slen, const uint8_t * pdata, size_t plen);
00151
00152 bool operator == (const DS_EncPrivKey &b) const {
00153 return (sodium_memcmp(key, b.key, ENC_PRIVKEY_SIZE_BYTES) == 0) ? true : false;
00154 }
00155 bool operator != (const DS_EncPrivKey &b) const {
00156 return (sodium_memcmp(key, b.key, ENC_PRIVKEY_SIZE_BYTES) != 0) ? true : false;
00157 }
00158 bool operator < (const DS_EncPrivKey &b) const {
00159 if (memcmp(key, b.key, ENC_PRIVKEY_SIZE_BYTES) < 0) {
00160 return true;
00161 }
00162 return false;
00163 }
00164 };
00165
00169 #endif // __DSL_SODIUM_KEYS_ENCRYPTION__