forked from dazjo/mkey
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C version re-added and made a release for it
Might be more noob friendly than a python script.
- Loading branch information
Showing
22 changed files
with
8,597 additions
and
0 deletions.
There are no files selected for viewing
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,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2012 neimod | ||
Copyright (c) 2014 3DSGuy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,18 @@ | ||
SOURCES = source source/polarssl | ||
|
||
CFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.c)) | ||
CPPFILES := $(foreach dir, $(SOURCES), $(wildcard $(dir)/*.cpp)) | ||
|
||
OBJS = $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) | ||
|
||
LIBS = -static-libstdc++ -static | ||
CXXFLAGS = -Isource | ||
CFLAGS = -std=c11 -Wall -Wextra -Os -posix -Isource -D_GNU_SOURCE | ||
OUTPUT = mkey | ||
CC = gcc | ||
|
||
main: $(OBJS) | ||
g++ -o $(OUTPUT) $(LIBS) $(OBJS) | ||
|
||
clean: | ||
rm -f $(OUTPUT) $(OUTPUT).exe $(OBJS) |
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
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,155 @@ | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "ctr.h" | ||
|
||
void ctr_set_iv( ctr_aes_context* ctx, | ||
u8 iv[16] ) | ||
{ | ||
memcpy(ctx->iv, iv, 16); | ||
} | ||
|
||
void ctr_add_counter( ctr_aes_context* ctx, | ||
u32 carry ) | ||
{ | ||
u32 counter[4]; | ||
u32 sum; | ||
int i; | ||
|
||
for(i=0; i<4; i++) | ||
counter[i] = (ctx->ctr[i*4+0]<<24) | (ctx->ctr[i*4+1]<<16) | (ctx->ctr[i*4+2]<<8) | (ctx->ctr[i*4+3]<<0); | ||
|
||
for(i=3; i>=0; i--) | ||
{ | ||
sum = counter[i] + carry; | ||
|
||
if (sum < counter[i]) | ||
carry = 1; | ||
else | ||
carry = 0; | ||
|
||
counter[i] = sum; | ||
} | ||
|
||
for(i=0; i<4; i++) | ||
{ | ||
ctx->ctr[i*4+0] = counter[i]>>24; | ||
ctx->ctr[i*4+1] = counter[i]>>16; | ||
ctx->ctr[i*4+2] = counter[i]>>8; | ||
ctx->ctr[i*4+3] = counter[i]>>0; | ||
} | ||
} | ||
|
||
void ctr_set_counter( ctr_aes_context* ctx, | ||
u8 ctr[16] ) | ||
{ | ||
memcpy(ctx->ctr, ctr, 16); | ||
} | ||
|
||
|
||
void ctr_init_counter( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 ctr[16] ) | ||
{ | ||
aes_setkey_enc(&ctx->aes, key, 128); | ||
ctr_set_counter(ctx, ctr); | ||
} | ||
|
||
|
||
void ctr_crypt_counter_block( ctr_aes_context* ctx, | ||
u8 input[16], | ||
u8 output[16] ) | ||
{ | ||
int i; | ||
u8 stream[16]; | ||
|
||
|
||
aes_crypt_ecb(&ctx->aes, AES_ENCRYPT, ctx->ctr, stream); | ||
|
||
|
||
if (input) | ||
{ | ||
for(i=0; i<16; i++) | ||
{ | ||
output[i] = stream[i] ^ input[i]; | ||
} | ||
} | ||
else | ||
{ | ||
for(i=0; i<16; i++) | ||
output[i] = stream[i]; | ||
} | ||
|
||
ctr_add_counter(ctx, 1); | ||
} | ||
|
||
|
||
void ctr_crypt_counter( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ) | ||
{ | ||
u8 stream[16]; | ||
u32 i; | ||
|
||
while(size >= 16) | ||
{ | ||
ctr_crypt_counter_block(ctx, input, output); | ||
|
||
if (input) | ||
input += 16; | ||
if (output) | ||
output += 16; | ||
|
||
size -= 16; | ||
} | ||
|
||
if (size) | ||
{ | ||
memset(stream, 0, 16); | ||
ctr_crypt_counter_block(ctx, stream, stream); | ||
|
||
if (input) | ||
{ | ||
for(i=0; i<size; i++) | ||
output[i] = input[i] ^ stream[i]; | ||
} | ||
else | ||
{ | ||
memcpy(output, stream, size); | ||
} | ||
} | ||
} | ||
|
||
void ctr_init_cbc_encrypt( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 iv[16] ) | ||
{ | ||
aes_setkey_enc(&ctx->aes, key, 128); | ||
ctr_set_iv(ctx, iv); | ||
} | ||
|
||
void ctr_init_cbc_decrypt( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 iv[16] ) | ||
{ | ||
aes_setkey_dec(&ctx->aes, key, 128); | ||
ctr_set_iv(ctx, iv); | ||
} | ||
|
||
void ctr_encrypt_cbc( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ) | ||
{ | ||
aes_crypt_cbc(&ctx->aes, AES_ENCRYPT, size, ctx->iv, input, output); | ||
} | ||
|
||
void ctr_decrypt_cbc( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ) | ||
{ | ||
aes_crypt_cbc(&ctx->aes, AES_DECRYPT, size, ctx->iv, input, output); | ||
} |
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,59 @@ | ||
#ifndef _CTR_H | ||
#define _CTR_H | ||
|
||
#include "utils.h" | ||
|
||
#include "polarssl/aes.h" | ||
|
||
typedef struct | ||
{ | ||
u8 ctr[16]; | ||
u8 iv[16]; | ||
aes_context aes; | ||
} ctr_aes_context; | ||
|
||
void ctr_set_iv( ctr_aes_context* ctx, | ||
u8 iv[16] ); | ||
|
||
void ctr_add_counter( ctr_aes_context* ctx, | ||
u32 carry ); | ||
|
||
void ctr_set_counter( ctr_aes_context* ctx, | ||
u8 ctr[16] ); | ||
|
||
|
||
void ctr_init_counter( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 ctr[16] ); | ||
|
||
|
||
void ctr_crypt_counter_block( ctr_aes_context* ctx, | ||
u8 input[16], | ||
u8 output[16] ); | ||
|
||
|
||
void ctr_crypt_counter( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ); | ||
|
||
|
||
void ctr_init_cbc_encrypt( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 iv[16] ); | ||
|
||
void ctr_init_cbc_decrypt( ctr_aes_context* ctx, | ||
u8 key[16], | ||
u8 iv[16] ); | ||
|
||
void ctr_encrypt_cbc( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ); | ||
|
||
void ctr_decrypt_cbc( ctr_aes_context* ctx, | ||
u8* input, | ||
u8* output, | ||
u32 size ); | ||
|
||
#endif |
Oops, something went wrong.