Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Finished AES 128 GCM implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Sep 23, 2018
1 parent 74377d1 commit 26893bb
Show file tree
Hide file tree
Showing 16 changed files with 1,003 additions and 1,457 deletions.
10 changes: 0 additions & 10 deletions AES_GCM/AES_GCM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,11 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AesGcmWrapper.h" />
<ClInclude Include="c\aes-gcm.h" />
<ClInclude Include="c\aes.h" />
<ClInclude Include="c\detect_platform.h" />
<ClInclude Include="c\gcm.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AesGcmWrapper.cpp" />
<ClCompile Include="c\aes-gcm.c">
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsWinRT>
</ClCompile>
<ClCompile Include="c\aes.c">
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT>
<CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT>
Expand Down
7 changes: 2 additions & 5 deletions AES_GCM/AES_GCM.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="AesGcmWrapper.cpp" />
<ClCompile Include="c\gcm.c" />
<ClCompile Include="c\aes.c" />
<ClCompile Include="c\aes-gcm.c" />
<ClCompile Include="c\gcm.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AesGcmWrapper.h" />
<ClInclude Include="c\detect_platform.h" />
<ClInclude Include="c\gcm.h" />
<ClInclude Include="c\aes.h" />
<ClInclude Include="c\aes-gcm.h" />
<ClInclude Include="c\gcm.h" />
</ItemGroup>
</Project>
92 changes: 72 additions & 20 deletions AES_GCM/AesGcmWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,92 @@
using namespace AES_GCM;
using namespace Platform;

AesGcmWrapper::AesGcmWrapper() {
gcm_initialize();
}
AesGcmWrapper::AesGcmWrapper() { }

int AesGcmWrapper::encrypt(WriteOnlyArray<uint8>^ output, const Array<uint8>^ input, const Array<uint8>^ key, const Array<uint8>^ iv) {
int AesGcmWrapper::encrypt(WriteOnlyArray<uint8>^ output, WriteOnlyArray<uint8>^ tag, const Array<uint8>^ input, const Array<uint8>^ key, const Array<uint8>^ iv) {
// Create temp arrays:
unsigned char* _output = new unsigned char[output->Length];
unsigned char* _input = new unsigned char[input->Length];
unsigned char* _key = new unsigned char[key->Length];
unsigned char* _iv = new unsigned char[iv->Length];
UCHAR* _output = new UCHAR[output->Length];
UCHAR* _input = new UCHAR[input->Length];
UCHAR* _key = new UCHAR[key->Length];
UCHAR* _iv = new UCHAR[iv->Length];
UCHAR* _tag = new UCHAR[tag->Length];

// Copy data in temp arrays:
winArrToCharArr(input, _input);
winArrToCharArr(key, _key);
winArrToCharArr(iv, _iv);

// Call function:
int ret = aes_gcm_encrypt(_output, const_cast<const unsigned char*>(_input), input->Length, const_cast<const unsigned char*>(_key), key->Length, const_cast<const unsigned char*>(_iv), iv->Length);
// Call lib:
int ret = 1;
void* context = gcm_init();
if (!context) {
printf("Failed to create encrypt context for AES 128 GCM!");
ret = -1;
}
else
{
operation_result flag = gcm_setkey(context, const_cast<const UCHAR*>(_key), key->Length * 8);
if (flag == OPERATION_FAIL) {
printf("Failed to set key for encrypt AES 128 GCM!");
ret = -2;
}
else
{
flag = gcm_crypt_and_tag(context, const_cast<const UCHAR*>(_iv), iv->Length, NULL, 0, const_cast<UCHAR*>(_input), input->Length, _output, _tag, tag->Length);
if (flag == OPERATION_FAIL) {
printf("Failed to encrypt AES 128 GCM!");
ret = -3;
}
}
gcm_free(context);
}

// Copy output, cleanup and return:
charArrToWinArr(_output, output);
charArrToWinArr(_tag, tag);
delete[] _output;
delete[] _input;
delete[] _key;
delete[] _iv;
delete[] _tag;
return ret;
}

int AesGcmWrapper::decrypt(WriteOnlyArray<uint8>^ output, const Array<uint8>^ input, const Array<uint8>^ key, const Array<uint8>^ iv) {
int AesGcmWrapper::decrypt(WriteOnlyArray<uint8>^ output, const Array<uint8>^ tag, const Array<uint8>^ input, const Array<uint8>^ key, const Array<uint8>^ iv) {
// Create temp arrays:
unsigned char* _output = new unsigned char[output->Length];
unsigned char* _input = new unsigned char[input->Length];
unsigned char* _key = new unsigned char[key->Length];
unsigned char* _iv = new unsigned char[iv->Length];
UCHAR* _output = new UCHAR[output->Length];
UCHAR* _input = new UCHAR[input->Length];
UCHAR* _key = new UCHAR[key->Length];
UCHAR* _iv = new UCHAR[iv->Length];
UCHAR* _tag = new UCHAR[tag->Length];

// Copy data in temp arrays:
winArrToCharArr(tag, _tag);
winArrToCharArr(input, _input);
winArrToCharArr(key, _key);
winArrToCharArr(iv, _iv);

// Call function:
int ret = aes_gcm_decrypt(_output, const_cast<const unsigned char*>(_input), input->Length, const_cast<const unsigned char*>(_key), key->Length, const_cast<const unsigned char*>(_iv), iv->Length);
int ret = 1;
void* context = gcm_init();
if (!context) {
printf("Failed to create decrypt context for AES 128 GCM!");
ret = -1;
}
else
{
operation_result flag = gcm_setkey(context, const_cast<const UCHAR*>(_key), key->Length * 8);
if (flag == OPERATION_FAIL) {
printf("Failed to set key for decrypt AES 128 GCM!");
ret = -2;
}
flag = gcm_auth_decrypt(context, const_cast<const UCHAR*>(_iv), iv->Length, NULL, 0, const_cast<UCHAR*>(_tag), tag->Length, const_cast<UCHAR*>(_input), input->Length, _output);
if (flag == OPERATION_FAIL) {
printf("Failed to decrypt AES 128 GCM!");
ret = -3;
}
// gcm_free(context);
}

// Copy output, cleanup and return:
charArrToWinArr(_output, output);
Expand All @@ -55,18 +99,26 @@ int AesGcmWrapper::decrypt(WriteOnlyArray<uint8>^ output, const Array<uint8>^ in
return ret;
}

void AesGcmWrapper::winArrToCharArr(const Platform::Array<uint8>^ input, unsigned char* output) {
for (int i = 0; i < input->Length; i++) {
void AesGcmWrapper::winArrToCharArr(const Platform::Array<uint8>^ input, UCHAR* output) {
for (UINT i = 0; i < input->Length; i++) {
output[i] = input[i];
}
}

void AesGcmWrapper::charArrToWinArr(unsigned char* input, Platform::WriteOnlyArray<uint8>^ output) {
for (int i = 0; i < output->Length; i++) {
void AesGcmWrapper::charArrToWinArr(UCHAR* input, Platform::WriteOnlyArray<uint8>^ output) {
for (UINT i = 0; i < output->Length; i++) {
output[i] = input[i];
}
}

UINT AesGcmWrapper::calcEncryptSize(UINT inputSize) {
return inputSize;
}

UINT AesGcmWrapper::calcDecryptSize(UINT inputSize) {
return inputSize;
}

void AesGcmWrapper::test() {

}
15 changes: 9 additions & 6 deletions AES_GCM/AesGcmWrapper.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once

extern "C" {
// #include "c/aes.h"
#include "c/aes-gcm.h"
#include "c\gcm.h"
}

#include <collection.h>
Expand All @@ -13,13 +12,17 @@ namespace AES_GCM
{
public:
AesGcmWrapper();
int encrypt(Platform::WriteOnlyArray<uint8>^ output, const Platform::Array<uint8>^ input, const Platform::Array<uint8>^ key, const Platform::Array<uint8>^ iv);
int decrypt(Platform::WriteOnlyArray<uint8>^ output, const Platform::Array<uint8>^ input, const Platform::Array<uint8>^ key, const Platform::Array<uint8>^ iv);
int encrypt(Platform::WriteOnlyArray<uint8>^ output, Platform::WriteOnlyArray<uint8>^ tag, const Platform::Array<uint8>^ input, const Platform::Array<uint8>^ key, const Platform::Array<uint8>^ iv);
int decrypt(Platform::WriteOnlyArray<uint8>^ output, const Platform::Array<uint8>^ tag, const Platform::Array<uint8>^ input, const Platform::Array<uint8>^ key, const Platform::Array<uint8>^ iv);

UINT calcEncryptSize(UINT inputSize);
UINT calcDecryptSize(UINT inputSize);

void test();

private:
void winArrToCharArr(const Platform::Array<uint8>^ input, unsigned char* output);
void charArrToWinArr(unsigned char* input, Platform::WriteOnlyArray<uint8>^ output);
void winArrToCharArr(const Platform::Array<uint8>^ input, UCHAR* output);
void charArrToWinArr(UCHAR* input, Platform::WriteOnlyArray<uint8>^ output);
};
}

46 changes: 0 additions & 46 deletions AES_GCM/c/aes-gcm.c

This file was deleted.

18 changes: 0 additions & 18 deletions AES_GCM/c/aes-gcm.h

This file was deleted.

Loading

0 comments on commit 26893bb

Please sign in to comment.