2013-03-05 07:36:32 +01:00
|
|
|
|
2008-01-07 09:09:04 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2013-03-05 07:36:32 +01:00
|
|
|
* Copyright (c) 2013 Valient Gough
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
2013-03-05 07:36:32 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
2013-10-20 00:35:26 +02:00
|
|
|
* later version.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
2013-03-05 07:36:32 +01:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
* details.
|
2008-01-07 09:09:04 +01:00
|
|
|
*
|
2012-10-03 07:12:17 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-07 09:09:04 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-29 04:07:54 +01:00
|
|
|
#include "cipher/CipherKey.h"
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-03-05 07:36:32 +01:00
|
|
|
#include "base/shared_ptr.h"
|
|
|
|
#include "base/types.h"
|
|
|
|
#include "cipher/MemoryPool.h"
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
namespace encfs {
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
CipherKey::CipherKey() : _valid(false) {}
|
2013-03-05 07:36:32 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
CipherKey::CipherKey(int length) : _valid(true) {
|
|
|
|
if (length > 0) _mem.reset(new SecureMem(length));
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
CipherKey::CipherKey(const byte *data, int length) : _valid(true) {
|
2013-03-05 07:36:32 +01:00
|
|
|
_mem.reset(new SecureMem(length));
|
2013-03-06 09:02:23 +01:00
|
|
|
memcpy(_mem->data(), data, length);
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
CipherKey::CipherKey(const CipherKey &src)
|
|
|
|
: _valid(src._valid), _mem(src._mem) {}
|
2013-03-05 07:36:32 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
CipherKey::~CipherKey() {}
|
2008-01-07 09:09:04 +01:00
|
|
|
|
2013-10-21 07:38:54 +02:00
|
|
|
CipherKey &CipherKey::operator=(const CipherKey &src) {
|
2013-03-05 07:36:32 +01:00
|
|
|
_mem = src._mem;
|
|
|
|
_valid = src._valid;
|
2013-10-21 07:38:54 +02:00
|
|
|
return *this;
|
2013-03-05 07:36:32 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
byte *CipherKey::data() const { return !_mem ? NULL : _mem->data(); }
|
2013-03-05 07:36:32 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int CipherKey::size() const { return !_mem ? 0 : _mem->size(); }
|
2013-03-05 07:36:32 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
void CipherKey::reset() {
|
2013-03-05 07:36:32 +01:00
|
|
|
_mem.reset();
|
|
|
|
_valid = false;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool CipherKey::valid() const { return _valid; }
|
2013-03-05 07:36:32 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool operator==(const CipherKey &a, const CipherKey &b) {
|
|
|
|
if (a.size() != b.size()) return false;
|
2013-03-05 07:36:32 +01:00
|
|
|
return memcmp(a.data(), b.data(), a.size()) == 0;
|
2008-01-07 09:09:04 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
} // namespace encfs
|