Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,57 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "CrySystem_precompiled.h"
#include <Cryptography/Crypto.h>
#include <Cryptography/StreamCipher.h>
//-----------------------------------------------------------------------------
void Crypto::EncryptBuffer(uint8* pOutput, const uint8* pInput, uint32 bufferLength, const uint8* pKey, uint32 keyLength)
{
if (pKey && (keyLength > 0))
{
StreamCipherState cipher;
m_streamCipher.Init(cipher, pKey, keyLength);
if (pInput && pOutput && (bufferLength > 0))
{
m_streamCipher.Encrypt(cipher, pInput, bufferLength, pOutput);
}
}
}
//-----------------------------------------------------------------------------
void Crypto::DecryptBuffer(uint8* pOutput, const uint8* pInput, uint32 bufferLength, const uint8* pKey, uint32 keyLength)
{
if (pKey && (keyLength > 0))
{
StreamCipherState cipher;
m_streamCipher.Init(cipher, pKey, keyLength);
if (pInput && pOutput && (bufferLength > 0))
{
m_streamCipher.Decrypt(cipher, pInput, bufferLength, pOutput);
}
}
}
//-----------------------------------------------------------------------------
IRijndael* Crypto::GetRijndael()
{
return &m_rijndael;
}
//-----------------------------------------------------------------------------
IStreamCipher* Crypto::GetStreamCipher()
{
return &m_streamCipher;
}
@@ -0,0 +1,43 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#ifndef CRYINCLUDE_CRYSYSTEM_CRYPTO_H
#define CRYINCLUDE_CRYSYSTEM_CRYPTO_H
#pragma once
#include <ICrypto.h>
#include <Cryptography/rijndael.h>
#include <Cryptography/StreamCipher.h>
#include <Cryptography/Whirlpool.h>
class Crypto
: public ICrypto
{
public:
// Exposed block encryption
void EncryptBuffer(uint8* pOutput, const uint8* pInput, uint32 bufferLength, const uint8* pKey, uint32 keyLength) override;
void DecryptBuffer(uint8* pOutput, const uint8* pInput, uint32 bufferLength, const uint8* pKey, uint32 keyLength) override;
// Crypto implementations
IRijndael* GetRijndael() override;
IStreamCipher* GetStreamCipher() override;
void InitWhirlpoolHash(uint8* hash);
void InitWhirlpoolHash(uint8* hash, const string& str);
void InitWhirlpoolHash(uint8* hash, const uint8* input, size_t length);
protected:
Rijndael m_rijndael;
CStreamCipher m_streamCipher;
};
#endif // CRYINCLUDE_CRYSYSTEM_CRYPTO_H
@@ -0,0 +1,84 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "CrySystem_precompiled.h"
#include "StreamCipher.h"
StreamCipherState CStreamCipher::BeginCipher(const uint8* pKey, uint32 keyLength)
{
StreamCipherState cipher;
Init(cipher, pKey, keyLength);
return cipher;
}
void CStreamCipher::Init(StreamCipherState& state, const uint8* key, int keyLen)
{
int i, j;
for (i = 0; i < 256; i++)
{
state.m_S[i] = i;
}
if (key)
{
for (i = j = 0; i < 256; i++)
{
uint8 temp;
j = (j + key[i % keyLen] + state.m_S[i]) & 255;
temp = state.m_S[i];
state.m_S[i] = state.m_S[j];
state.m_S[j] = temp;
}
}
state.m_I = state.m_J = 0;
for (i = 0; i < 1024; i++)
{
GetNext(state);
}
memcpy(state.m_StartS, state.m_S, sizeof(state.m_StartS));
state.m_StartI = state.m_I;
state.m_StartJ = state.m_J;
}
uint8 CStreamCipher::GetNext(StreamCipherState& state)
{
uint8 tmp;
state.m_I = (state.m_I + 1) & 0xff;
state.m_J = (state.m_J + state.m_S[state.m_I]) & 0xff;
tmp = state.m_S[state.m_J];
state.m_S[state.m_J] = state.m_S[state.m_I];
state.m_S[state.m_I] = tmp;
return state.m_S[(state.m_S[state.m_I] + state.m_S[state.m_J]) & 0xff];
}
void CStreamCipher::ProcessBuffer(StreamCipherState& state, const uint8* input, int inputLen, uint8* output, bool resetKey)
{
if (resetKey)
{
memcpy(state.m_S, state.m_StartS, sizeof(state.m_S));
state.m_I = state.m_StartI;
state.m_J = state.m_StartJ;
}
for (int i = 0; i < inputLen; i++)
{
output[i] = input[i] ^ GetNext(state);
}
}
@@ -0,0 +1,38 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
/****************************************************
A simple stream cipher based on RC4
****************************************************/
#ifndef CRYINCLUDE_CRYPTOGRAPHY_STREAMCIPHER_H
#define CRYINCLUDE_CRYPTOGRAPHY_STREAMCIPHER_H
#pragma once
#include <ICrypto.h>
class CStreamCipher
: public IStreamCipher
{
public:
StreamCipherState BeginCipher(const uint8* pKey, uint32 keyLength);
void Init(StreamCipherState& state, const uint8* key, int keyLen);
void Encrypt(StreamCipherState& state, const uint8* input, int inputLen, uint8* output) { ProcessBuffer(state, input, inputLen, output, true); }
void Decrypt(StreamCipherState& state, const uint8* input, int inputLen, uint8* output) { ProcessBuffer(state, input, inputLen, output, true); }
void EncryptStream(StreamCipherState& state, const uint8* input, int inputLen, uint8* output) { ProcessBuffer(state, input, inputLen, output, false); }
void DecryptStream(StreamCipherState& state, const uint8* input, int inputLen, uint8* output) { ProcessBuffer(state, input, inputLen, output, false); }
private:
uint8 GetNext(StreamCipherState& state);
void ProcessBuffer(StreamCipherState& state, const uint8* input, int inputLen, uint8* output, bool resetKey);
};
#endif // CRYINCLUDE_CRYPTOGRAPHY_STREAMCIPHER_H
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,18 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#ifndef CRYINCLUDE_CRYPTOGRAPHY_WHIRLPOOL_H
#define CRYINCLUDE_CRYPTOGRAPHY_WHIRLPOOL_H
#pragma once
bool WhirlpoolHash_Test();
#endif // CRYINCLUDE_CRYPTOGRAPHY_WHIRLPOOL_H
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,133 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#ifndef CRYINCLUDE_CRYPTOGRAPHY_RIJNDAEL_H
#define CRYINCLUDE_CRYPTOGRAPHY_RIJNDAEL_H
#pragma once
#include <ICrypto.h>
//
// File : rijndael.h
// Creation date : Sun Nov 5 2000 03:21:05 CEST
// Author : Szymon Stefanek (stefanek@tin.it)
//
// Another implementation of the Rijndael cipher.
// This is intended to be an easily usable library file.
// This code is public domain.
// Based on the Vincent Rijmen and K.U.Leuven implementation 2.4.
//
//
// Original Copyright notice:
//
// rijndael-alg-fst.c v2.4 April '2000
// rijndael-alg-fst.h
// rijndael-api-fst.c
// rijndael-api-fst.h
//
// Optimised ANSI C code
//
// authors: v1.0: Antoon Bosselaers
// v2.0: Vincent Rijmen, K.U.Leuven
// v2.3: Paulo Barreto
// v2.4: Vincent Rijmen, K.U.Leuven
//
// This code is placed in the public domain.
//
//
// This implementation works on 128 , 192 , 256 bit keys
// and on 128 bit blocks
//
//
// Example of usage:
//
// // Input data
// unsigned char key[32]; // The key
// initializeYour256BitKey(); // Obviously initialized with sth
// const unsigned char * plainText = getYourPlainText(); // Your plain text
// int plainTextLen = strlen(plainText); // Plain text length
//
// // Encrypting
// Rijndael rin;
// unsigned char output[plainTextLen + 16];
//
// rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes);
// // It is a good idea to check the error code
// int len = rin.padEncrypt(plainText,len,output);
// if(len >= 0)useYourEncryptedText();
// else encryptError(len);
//
// // Decrypting: we can reuse the same object
// unsigned char output2[len];
// rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes));
// len = rin.padDecrypt(output,len,output2);
// if(len >= 0)useYourDecryptedText();
// else decryptError(len);
//
class Rijndael
: public IRijndael
{
public:
//////////////////////////////////////////////////////////////////////////////////////////
// API
//////////////////////////////////////////////////////////////////////////////////////////
// init(): Initializes the crypt session
// Returns RIJNDAEL_SUCCESS or an error code
// mode : Rijndael::ECB, Rijndael::CBC or Rijndael::CFB1
// You have to use the same mode for encrypting and decrypting
// dir : Rijndael::Encrypt or Rijndael::Decrypt
// A cipher instance works only in one direction
// (Well , it could be easily modified to work in both
// directions with a single init() call, but it looks
// useless to me...anyway , it is a matter of generating
// two expanded keys)
// key : array of unsigned octets , it can be 16 , 24 or 32 bytes long
// this CAN be binary data (it is not expected to be null terminated)
// keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes
// initVector: initialization vector, you will usually use 0 here
int init(RijndaelState& state, RijndaelMode mode, RijndaelDirection dir, const uint8* key, RijndaelKeyLength keyLen, uint8* initVector = 0);
// Encrypts the input array (can be binary data)
// The input array length must be a multiple of 16 bytes, the remaining part
// is DISCARDED.
// so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer
// Input len is in BITS!
// outBuffer must be at least inputLen / 8 bytes long.
// Returns the encrypted buffer length in BITS or an error code < 0 in case of error
int blockEncrypt(RijndaelState& state, const uint8* input, int inputLen, uint8* outBuffer);
// Encrypts the input array (can be binary data)
// The input array can be any length , it is automatically padded on a 16 byte boundary.
// Input len is in BYTES!
// outBuffer must be at least (inputLen + 16) bytes long
// Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
int padEncrypt(RijndaelState& state, const uint8* input, int inputOctets, uint8* outBuffer);
// Decrypts the input vector
// Input len is in BITS!
// outBuffer must be at least inputLen / 8 bytes long
// Returns the decrypted buffer length in BITS and an error code < 0 in case of error
int blockDecrypt(RijndaelState& state, const uint8* input, int inputLen, uint8* outBuffer);
// Decrypts the input vector
// Input len is in BYTES!
// outBuffer must be at least inputLen bytes long
// Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
int padDecrypt(RijndaelState& state, const uint8* input, int inputOctets, uint8* outBuffer);
protected:
void keySched(RijndaelState & state, uint8 key[_MAX_KEY_COLUMNS][4]);
void keyEncToDec(RijndaelState& state);
void encrypt(RijndaelState & state, const uint8 a[16], uint8 b[16]);
void decrypt(RijndaelState & state, const uint8 a[16], uint8 b[16]);
};
#endif // CRYINCLUDE_CRYPTOGRAPHY_RIJNDAEL_H