210 lines
6.6 KiB
C++
210 lines
6.6 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
Cryptographic class using lib Sodium
|
|
Copyright (C) 2017 Andrey Bychkov work.a.b@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "picrypt.h"
|
|
#ifdef PIP_CRYPT
|
|
# include "sodium.h"
|
|
#endif
|
|
|
|
|
|
const char hash_def_key[] = "_picrypt_";
|
|
|
|
|
|
PICrypt::PICrypt() {
|
|
#ifdef PIP_CRYPT
|
|
if (!sodium_init()) piCout << "[PICrypt]" << "Error while initialize sodium!";
|
|
nonce_.resize(crypto_secretbox_NONCEBYTES);
|
|
key_.resize(crypto_secretbox_KEYBYTES);
|
|
randombytes_buf(key_.data(), key_.size());
|
|
randombytes_buf(nonce_.data(), nonce_.size());
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install libsodium-dev library and build pip with -DCRYPT=1";
|
|
#endif
|
|
}
|
|
|
|
|
|
bool PICrypt::setKey(const PIByteArray & _key) {
|
|
if (_key.size() != key_.size()) return false;
|
|
key_ = _key;
|
|
return true;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::setKey(const PIString & secret) {
|
|
PIByteArray hash;
|
|
#ifdef PIP_CRYPT
|
|
hash.resize(crypto_generichash_BYTES);
|
|
PIByteArray s(secret.data(), secret.size());
|
|
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar*)hash_def_key, sizeof(hash_def_key) - 1);
|
|
hash.resize(key_.size());
|
|
setKey(hash);
|
|
#endif
|
|
return hash;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::crypt(const PIByteArray & data) {
|
|
PIByteArray ret;
|
|
#ifdef PIP_CRYPT
|
|
ret.resize(data.size() + crypto_secretbox_MACBYTES);
|
|
randombytes_buf(nonce_.data(), nonce_.size());
|
|
crypto_secretbox_easy(ret.data(), data.data(), data.size(), nonce_.data(), key_.data());
|
|
ret.append(nonce_);
|
|
#endif
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::crypt(const PIByteArray & data, PIByteArray key) {
|
|
PIByteArray retba;
|
|
#ifdef PIP_CRYPT
|
|
if (key.size() != crypto_secretbox_KEYBYTES)
|
|
key.resize(crypto_secretbox_KEYBYTES, ' ');
|
|
//return PIByteArray();
|
|
if (!sodium_init()) return retba;
|
|
PIByteArray n;
|
|
retba.resize(data.size() + crypto_secretbox_MACBYTES);
|
|
n.resize(crypto_secretbox_NONCEBYTES);
|
|
randombytes_buf(n.data(), n.size());
|
|
crypto_secretbox_easy(retba.data(), data.data(), data.size(), n.data(), key.data());
|
|
retba.append(n);
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=1";
|
|
#endif
|
|
return retba;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, bool *ok) {
|
|
PIByteArray ret;
|
|
#ifdef PIP_CRYPT
|
|
if (crypt_data.size() < nonce_.size() + crypto_secretbox_MACBYTES) {
|
|
if (ok) *ok = false;
|
|
return PIByteArray();
|
|
}
|
|
ret.resize(crypt_data.size() - nonce_.size() - crypto_secretbox_MACBYTES);
|
|
memcpy(nonce_.data(), crypt_data.data(crypt_data.size() - nonce_.size()), nonce_.size());
|
|
if (crypto_secretbox_open_easy(ret.data(), crypt_data.data(), crypt_data.size() - nonce_.size(), nonce_.data(), key_.data()) != 0) {
|
|
if (ok) *ok = false;
|
|
// piCout << "[PICrypt]" << "bad key_";
|
|
return PIByteArray();
|
|
}
|
|
#endif
|
|
if (ok) *ok = true;
|
|
return ret;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::decrypt(const PIByteArray & crypt_data, PIByteArray key, bool *ok) {
|
|
PIByteArray retba;
|
|
#ifdef PIP_CRYPT
|
|
if (key.size() != crypto_secretbox_KEYBYTES)
|
|
key.resize(crypto_secretbox_KEYBYTES, ' ');
|
|
/*if (ok) *ok = false;
|
|
return PIByteArray();
|
|
}*/
|
|
if (crypt_data.size() < crypto_secretbox_NONCEBYTES + crypto_secretbox_MACBYTES) {
|
|
if (ok) *ok = false;
|
|
return PIByteArray();
|
|
}
|
|
if (!sodium_init()) return retba;
|
|
PIByteArray n;
|
|
n.resize(crypto_secretbox_NONCEBYTES);
|
|
retba.resize(crypt_data.size() - n.size() - crypto_secretbox_MACBYTES);
|
|
memcpy(n.data(), crypt_data.data(crypt_data.size() - n.size()), n.size());
|
|
if (crypto_secretbox_open_easy(retba.data(), crypt_data.data(), crypt_data.size() - n.size(), n.data(), key.data()) != 0) {
|
|
if (ok) *ok = false;
|
|
// piCout << "[PICrypt]" << "bad key_";
|
|
return PIByteArray();
|
|
}
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
if (ok) *ok = true;
|
|
return retba;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::hash(const PIString & secret) {
|
|
PIByteArray hash;
|
|
#ifdef PIP_CRYPT
|
|
if (!sodium_init()) return hash;
|
|
hash.resize(crypto_generichash_BYTES);
|
|
PIByteArray s(secret.data(), secret.size());
|
|
crypto_generichash(hash.data(), hash.size(), s.data(), s.size(), (const uchar*)hash_def_key, sizeof(hash_def_key) - 1);
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
return hash;
|
|
}
|
|
|
|
|
|
ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
|
|
ullong hash = 0;
|
|
#ifdef PIP_CRYPT
|
|
if (crypto_shorthash_BYTES != sizeof(hash)) piCout << "[PICrypt]" << "internal error: bad hash size";
|
|
if (!sodium_init()) return hash;
|
|
if (key.size() != crypto_shorthash_KEYBYTES) {
|
|
piCout << "[PICrypt]" << "invalid key size" << key.size() << ", shoud be" << crypto_shorthash_KEYBYTES << ", filled zeros";
|
|
key.resize(crypto_shorthash_KEYBYTES, 0);
|
|
}
|
|
PIByteArray in(s.data(), s.size());
|
|
crypto_shorthash((uchar *)&hash, in.data(), in.size(), key.data());
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
return hash;
|
|
}
|
|
|
|
|
|
PIByteArray PICrypt::generateKey() {
|
|
PIByteArray hash;
|
|
#ifdef PIP_CRYPT
|
|
if (!sodium_init()) return hash;
|
|
hash.resize(crypto_secretbox_KEYBYTES);
|
|
randombytes_buf(hash.data(), hash.size());
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
return hash;
|
|
}
|
|
|
|
|
|
size_t PICrypt::sizeKey() {
|
|
#ifdef PIP_CRYPT
|
|
return crypto_secretbox_KEYBYTES;
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
|
|
size_t PICrypt::sizeCrypt() {
|
|
#ifdef PIP_CRYPT
|
|
return crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
|
|
#else
|
|
piCout << "[PICrypt]" << "Warning: PICrypt is disabled, to enable install sodium library and build pip with -DCRYPT=";
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
|
|
|