mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:57:44 +00:00
LibCrypto: Implement Cipher and AES_CBC
Also adds a test program to userland
This commit is contained in:
parent
cf5941c972
commit
899ca245ae
7 changed files with 3487 additions and 0 deletions
97
Libraries/LibCrypto/Cipher/Mode/Mode.h
Normal file
97
Libraries/LibCrypto/Cipher/Mode/Mode.h
Normal file
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <LibCrypto/Cipher/Cipher.h>
|
||||
|
||||
namespace Crypto {
|
||||
|
||||
template <typename T>
|
||||
class Mode {
|
||||
public:
|
||||
// FIXME: Somehow communicate that encrypt returns the last initialization vector (if the mode supports it)
|
||||
virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
|
||||
const T& cipher() const { return m_cipher; }
|
||||
|
||||
ByteBuffer create_aligned_buffer(size_t input_size) const
|
||||
{
|
||||
size_t remainder = (input_size + T::block_size()) % T::block_size();
|
||||
if (remainder == 0)
|
||||
return ByteBuffer::create_uninitialized(input_size);
|
||||
else
|
||||
return ByteBuffer::create_uninitialized(input_size + T::block_size() - remainder);
|
||||
}
|
||||
|
||||
protected:
|
||||
T& cipher() { return m_cipher; }
|
||||
|
||||
virtual void prune_padding(ByteBuffer& data)
|
||||
{
|
||||
auto size = data.size();
|
||||
switch (m_cipher.padding_mode()) {
|
||||
case PaddingMode::CMS: {
|
||||
auto maybe_padding_length = data[size - 1];
|
||||
if (maybe_padding_length >= T::block_size()) {
|
||||
// cannot be padding (the entire block cannot be padding)
|
||||
return;
|
||||
}
|
||||
for (auto i = maybe_padding_length; i > 0; --i) {
|
||||
if (data[size - i] != maybe_padding_length) {
|
||||
// not padding, part of data
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.trim(size - maybe_padding_length);
|
||||
break;
|
||||
}
|
||||
case PaddingMode::Null: {
|
||||
while (data[size - 1] == 0)
|
||||
--size;
|
||||
data.trim(size);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// FIXME: support other padding modes
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Somehow add a reference version of this
|
||||
template <typename... Args>
|
||||
Mode(Args... args)
|
||||
: m_cipher(args...)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
T m_cipher;
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue