Selaa lähdekoodia

added wrapper.c

wangyu- 8 vuotta sitten
vanhempi
sitoutus
fb54df66e4
6 muutettua tiedostoa jossa 106 lisäystä ja 2 poistoa
  1. 6 0
      encrypt.h
  2. 1 0
      lib/aes_faster_c/aes.c
  3. 0 0
      lib/aes_faster_c/aes.h
  4. 65 0
      lib/aes_faster_c/wrapper.c
  5. 30 0
      main.cpp
  6. 4 2
      makefile

+ 6 - 0
encrypt.h

@@ -32,4 +32,10 @@ extern cipher_mode_t cipher_mode;
 extern unordered_map<int, const char *> auth_mode_tostring;
 extern unordered_map<int, const char *> cipher_mode_tostring;
 
+
+
+
+int cipher_decrypt(const char *data,char *output,int &len,char * key);
+int cipher_encrypt(const char *data,char *output,int &len,char * key);
+
 #endif

+ 1 - 0
lib/aes_c/aes.c → lib/aes_faster_c/aes.c

@@ -1221,6 +1221,7 @@ int aes_self_test( int verbose )
 
         memset( buf, 0, 16 );
 
+
         if( v == AES_DECRYPT )
         {
             aes_setkey_dec( &ctx, key, 128 + u * 64 );

+ 0 - 0
lib/aes_c/aes.h → lib/aes_faster_c/aes.h


+ 65 - 0
lib/aes_faster_c/wrapper.c

@@ -0,0 +1,65 @@
+#include "aes.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(AES256) && (AES256 == 1)
+#define AES_KEYSIZE 256
+#elif defined(AES192) && (AES192 == 1)
+#define AES_KEYSIZE 192
+#else
+#define AES_KEYSIZE 128
+#endif
+
+
+static aes_context ctx_de;
+static aes_context ctx_en;
+inline void init_de()
+{
+	static int done=0;
+	if(done==0)
+	{
+		aes_init( &ctx_de );
+		done=1;
+	}
+}
+inline void init_en()
+{
+	static int done=0;
+	if(done==0)
+	{
+		aes_init( &ctx_en);
+		done=1;
+	}
+}
+void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
+{
+	init_en();
+	printf("AES_ECB_encrypt not implemented\n");
+	exit(-1);
+}
+void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
+{
+	init_de();
+	printf("AES_ECB_encrypt not implemented\n");
+	exit(-1);
+}
+
+void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
+{
+	char tmp_iv[16];
+	init_en();
+	if(key!=0) aes_setkey_enc(&ctx_en,key,AES_KEYSIZE);
+
+	memcpy(tmp_iv,iv,16);
+	aes_crypt_cbc( &ctx_en, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
+	return ;
+}
+void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
+{
+	char tmp_iv[16];
+	init_de();
+	if(key!=0) aes_setkey_dec(&ctx_de,key,AES_KEYSIZE);
+	memcpy(tmp_iv,iv,16);
+	aes_crypt_cbc( &ctx_de, AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
+	return;
+}

+ 30 - 0
main.cpp

@@ -2677,6 +2677,7 @@ int load_config(char *file_name, int &argc, vector<string> &argv) //load conf fi
 
 	return 0;
 }
+
 int unit_test()
 {
 	printf("running unit test\n");
@@ -2703,6 +2704,35 @@ int unit_test()
 
 	printf("%x %x\n",(int)c1,(int)c2);
 
+	const char buf[]={1,2,3,4,5,6,7,8,9,10,11,2,13,14,15,16};
+	char key[100]={0};
+	char buf2[100]={0};
+	char buf3[100]={0};
+	char buf4[100]={0};
+	int len=16;
+	for(int i=0;i<len;i++)
+	{
+		printf("<%d>",buf[i]);
+	}
+	printf("\n");
+	cipher_encrypt(buf,buf2,len,key);
+	for(int i=0;i<len;i++)
+	{
+		printf("<%d>",buf2[i]);
+	}
+	printf("\n");
+	int temp_len=len;
+	cipher_decrypt(buf2,buf3,len,key);
+	for(int i=0;i<len;i++)
+	{
+		printf("<%d>",buf3[i]);
+	}
+	printf("\n");
+	cipher_encrypt(buf2,buf4,temp_len,key);
+	for(int i=0;i<temp_len;i++)
+	{
+		printf("<%d>",buf4[i]);
+	}
 	return 0;
 }
 

+ 4 - 2
makefile

@@ -8,8 +8,10 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
 #cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ 
 FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers
 
-SOURCES=main.cpp lib/aes.c lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp  -lpthread
-SOURCES_AES_ACC=$(filter-out lib/aes.c,$(SOURCES)) $(wildcard lib/aes_acc/aes*.c)
+COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp  -lpthread
+SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c
+SOURCES_TINY_AES= $(COMMON) lib/aes.c
+SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c)
 
 NAME=udp2raw
 TARGETS=amd64 arm amd64_hw_aes arm_asm_aes mips24kc_be mips24kc_be_asm_aes x86 x86_asm_aes mips24kc_le mips24kc_le_asm_aes