Skip to content

Commit

Permalink
replace libtomcrypt with an embedded AES implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jan2642 committed Jan 24, 2018
1 parent db60a4a commit 19189cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
10 changes: 3 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@ NDK_BUNDLE ?= $(HOME)/Library/Android/sdk/ndk-bundle
HOST ?= darwin-x86_64

ifeq ($(ARCH), arm)
LTC_TOP = ../libtomcrypt-arm
CROSS_COMPILE = $(NDK_BUNDLE)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(HOST)/bin/arm-linux-androideabi-
CFLAGS = -I$(NDK_BUNDLE)/sysroot/usr/include/arm-linux-androideabi
CFLAGS += -I$(NDK_BUNDLE)/sysroot/usr/include
CFLAGS += -Wno-multichar -Wno-attributes
#LDFLAGS = -L/Volumes/bionic/android_build/out/target/product/generic/system/lib/
LDFLAGS += --sysroot=$(NDK_BUNDLE)/platforms/android-16/arch-arm
LDFLAGS += -s
else
LTC_TOP = ../libtomcrypt
endif

CC = $(CROSS_COMPILE)gcc
LTC_INC = $(LTC_TOP)/src/headers
CFLAGS += -I. -I$(LTC_INC) -O3
CFLAGS += -I. -O3

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

og_verify: verify.o rsa.o sha.o sha256.o
$(CC) $(CFLAGS) -o $@ $^ $(LTC_TOP)/libtomcrypt.a $(LDFLAGS)
og_verify: verify.o rsa.o sha.o sha256.o aes.o
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:
rm -f og_verify *.o
Expand Down
33 changes: 16 additions & 17 deletions verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <sys/stat.h>
#include <sys/mman.h>

#include <tomcrypt.h>
#include <mincrypt/rsa.h>
#include <mincrypt/sha256.h>
#include "mincrypt/rsa.h"
#include "mincrypt/sha256.h"
#include "aes.h"

#define min(a, b) ((a) < (b) ? (a) : (b))

Expand Down Expand Up @@ -379,20 +379,21 @@ int main(int argc, const char **argv) {
}
else {
uint8_t scram_key[16];
symmetric_key key;
unsigned char iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
AesCtx ctx;

aes_setup(enc_key, 16, 0, &key);
aes_ecb_decrypt(hdr->scram_key, scram_key, &key);
aes_done(&key);
if( AesCtxIni(&ctx, NULL, enc_key, KEY128, EBC) < 0) {
printf("Failed to init AES\n");
exit(1);
}

symmetric_CBC cbc;
int cipher;
const unsigned char iv[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
if (AesDecrypt(&ctx, hdr->scram_key, scram_key, sizeof(scram_key)) < 0) {
printf("Failed to decrypt\n");
exit(1);
}

cipher = register_cipher(&aes_desc);
ret = cbc_start(cipher, iv, scram_key, 16, 0, &cbc);
if (ret != CRYPT_OK) {
printf("Failed to init CBC\n");
if( AesCtxIni(&ctx, iv, scram_key, KEY128, CBC) < 0) {
printf("Failed to init AES\n");
exit(1);
}

Expand All @@ -406,8 +407,7 @@ int main(int argc, const char **argv) {
int pos = 0;
while (padded_len) {
int n = min(padded_len, 1024);
ret = cbc_decrypt(payload + pos, outbuf, n, &cbc);
if (ret != CRYPT_OK) {
if (AesDecrypt(&ctx, payload + pos, outbuf, n) < 0) {
printf("Failed to decrypt\n");
exit(1);
}
Expand All @@ -418,7 +418,6 @@ int main(int argc, const char **argv) {
n = hdr->chunk[0].size - (pos - n);
write(fd2, outbuf, n);
}
cbc_done(&cbc);
}
close(fd2);
}
Expand Down

0 comments on commit 19189cf

Please sign in to comment.