-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
5,114 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# User-specific files | ||
*.suo | ||
*.user | ||
*.userosscache | ||
*.sln.docstates | ||
|
||
# Visual Studio 2015 cache/options directory | ||
.vs/ | ||
|
||
# Build results | ||
[Dd]ebug/ | ||
[Dd]ebugPublic/ | ||
[Rr]elease/ | ||
[Rr]eleases/ | ||
x64/ | ||
x86/ | ||
bld/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
[Ll]og | ||
|
||
|
||
*.vcxproj.filters | ||
|
||
# Visual C++ cache files | ||
ipch/ | ||
*.aps | ||
*.ncb | ||
*.opendb | ||
*.opensdf | ||
*.sdf | ||
*.cachefile | ||
*.VC.db | ||
*.VC.VC.opendb | ||
|
||
# Visual Studio profiler | ||
*.psess | ||
*.vsp | ||
*.vspx | ||
*.sap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright 2017 Sathyanesh Krishnan | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* | ||
Modifications made by Arman, 2024 - Some changes in functionality and structure of the original code. | ||
*/ | ||
|
||
extern "C" { | ||
#include "openssl/aes.h" | ||
#include <stdio.h> | ||
} | ||
|
||
#include "AesModeCTR.h" | ||
#include <chrono> | ||
#include <iostream> | ||
|
||
AesModeCTR::AesModeCTR(const unsigned char *key, AesKeySize ks, const unsigned char *iv) | ||
{ | ||
int i = 0; | ||
|
||
i = sizeof(AESkey); | ||
i = sizeof(AES_KEY); | ||
for (i = 0; i < IV_SIZE; ++i) | ||
{ | ||
BuffIV[i] = *(iv + i); | ||
} | ||
AES_set_encrypt_key((const unsigned char *)key, ks, &AESkey); | ||
} | ||
|
||
void AesModeCTR::GetIvCtrMode(size_t c, unsigned char IvCtr[IV_SIZE]) | ||
{ | ||
const unsigned char *iv = piv; | ||
size_t *data = (size_t *)IvCtr; | ||
size_t d = 0; | ||
size_t n = 0; | ||
|
||
const union | ||
{ | ||
long one; | ||
char little; | ||
} is_endian = {1}; | ||
|
||
if (is_endian.little || ((size_t)iv % sizeof(size_t)) != 0) | ||
{ | ||
n = IV_SIZE; | ||
do | ||
{ | ||
--n; | ||
c += iv[n]; | ||
IvCtr[n] = (u8)c; | ||
|
||
c >>= 8; | ||
} while (n); | ||
return; | ||
} | ||
|
||
n = IV_SIZE / sizeof(size_t); | ||
do | ||
{ | ||
--n; | ||
d = data[n] += c; | ||
|
||
c = ((d - c) ^ d) >> (sizeof(size_t) * 8 - 1); | ||
} while (n); | ||
|
||
return; | ||
} | ||
|
||
void AesModeCTR::Encrypt(const unsigned char *in, unsigned char *out, size_t len) | ||
{ | ||
unsigned char IvCtr[IV_SIZE]; | ||
unsigned char AesCipherOut[BLOCK_SIZE]; | ||
size_t c = 0; | ||
size_t n = 0; | ||
|
||
while (len >= BLOCK_SIZE) | ||
{ | ||
GetIvCtrMode(c, IvCtr); | ||
AES_encrypt(IvCtr, AesCipherOut, (const AES_KEY *)&AESkey); | ||
|
||
for (n = 0; n < BLOCK_SIZE; n += sizeof(size_t)) | ||
{ | ||
*(size_t *)(out + n) = *(size_t *)(in + n) ^ *(size_t *)(AesCipherOut + n); | ||
} | ||
len -= BLOCK_SIZE; | ||
out += BLOCK_SIZE; | ||
in += BLOCK_SIZE; | ||
n = 0; | ||
++c; | ||
} | ||
|
||
if (len) | ||
{ | ||
GetIvCtrMode(c, IvCtr); | ||
AES_encrypt(IvCtr, AesCipherOut, (const AES_KEY *)&AESkey); | ||
while (len--) | ||
{ | ||
out[n] = in[n] ^ AesCipherOut[n]; | ||
++n; | ||
} | ||
} | ||
|
||
return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Copyright 2017 Sathyanesh Krishnan | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef MY_AES_MODE_CTR_H | ||
# define MY_AES_MODE_CTR_H | ||
|
||
extern "C" { | ||
#include "openssl/aes.h" | ||
#include <stdio.h> | ||
} | ||
|
||
#define BLOCK_SIZE 16 | ||
#define IV_SIZE 16 | ||
|
||
typedef unsigned int u32; | ||
typedef unsigned char u8; | ||
enum AesKeySize { AES128=128, AES192=192, AES256=256 }; | ||
|
||
class AesModeCTR | ||
{ | ||
private: | ||
AES_KEY AESkey; | ||
unsigned char BuffIV[IV_SIZE]; | ||
const unsigned char *piv = BuffIV; | ||
|
||
public : | ||
AesModeCTR(const unsigned char *key, AesKeySize ks, const unsigned char *iv); | ||
void GetIvCtrMode(size_t c, unsigned char BuffCtr[IV_SIZE]); | ||
void Encrypt(const unsigned char *in, unsigned char *out, size_t len); | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright 2017 Sathyanesh Krishnan | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2017 Sathyanesh Krishnan | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* | ||
Modifications made by Arman Riasi, 2024 - Many changes in functionality and structure of the original code. | ||
*/ | ||
|
||
extern "C" { | ||
#include "openssl/aes.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
} | ||
|
||
#include <chrono> | ||
#include "AesModeCTR.h" | ||
#include <iostream> | ||
using namespace std; | ||
|
||
void MyMemSet(unsigned char *p, unsigned int t); | ||
void MyPrintBinData(const unsigned char *BinData, size_t len); | ||
|
||
int main() | ||
{ | ||
string input; | ||
getline(cin, input); | ||
|
||
const char* input_char = input.c_str(); | ||
unsigned char key[44]; | ||
strncpy((char*)key, input_char, 43); | ||
key[43] = '\0'; | ||
|
||
unsigned char BuffEncrypted[1024]; | ||
unsigned char BuffDecrypted[1024]; | ||
size_t len = 0; | ||
|
||
MyMemSet(BuffEncrypted, sizeof(BuffEncrypted)); | ||
MyMemSet(BuffDecrypted, sizeof(BuffDecrypted)); | ||
|
||
unsigned char *OriginalData = (unsigned char *)"1370000000000000"; | ||
|
||
len = strlen((const char *)OriginalData); | ||
|
||
long long iv_int = 11111007890123456; | ||
unsigned char iv[16]; | ||
long long durationList[4000]; | ||
long long totalDuration = 0; | ||
|
||
for (int i = 0; i < 4000; ++i) { | ||
iv_int += 10; | ||
sprintf(reinterpret_cast<char*>(iv), "%ld", iv_int); | ||
|
||
AesModeCTR aesctr(key, AesKeySize::AES256, iv); | ||
|
||
auto start = std::chrono::high_resolution_clock::now(); | ||
aesctr.Encrypt(OriginalData, BuffEncrypted, len); | ||
auto end = std::chrono::high_resolution_clock::now(); | ||
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start); | ||
durationList[i] = duration.count(); | ||
|
||
MyPrintBinData(BuffEncrypted, len); | ||
} | ||
for(int i = 1; i < 4000; i++) | ||
totalDuration += durationList[i]; | ||
std::cout << totalDuration << " nanoseconds" << std::endl; | ||
|
||
return(0); | ||
} | ||
|
||
void MyMemSet(unsigned char *p, unsigned int t) | ||
{ | ||
unsigned int i = 0; | ||
for (i = 0; i < t; ++i) | ||
{ | ||
*p++ = 0; | ||
} | ||
} | ||
|
||
void MyPrintBinData(const unsigned char *BinData, size_t len) | ||
{ | ||
size_t i; | ||
int DisplayBlockSeparation = 0; | ||
|
||
for (i = 0; i < len; i++) | ||
{ | ||
printf("%X", BinData[i] / 16); | ||
printf("%X", BinData[i] % 16); | ||
|
||
++DisplayBlockSeparation; | ||
if (DisplayBlockSeparation == 4) | ||
{ | ||
DisplayBlockSeparation = 0; | ||
printf(" "); | ||
} | ||
} | ||
printf("\n"); | ||
} |
Oops, something went wrong.