diff --git a/Makefile b/Makefile index 6be6ca2..cd54b20 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ .PHONY: all clean -ALL:= AEStest AESCMACtest AESKDFtest main +CFLAGS= -g +ALL:= AEStest AESCMACtest AESKDFtest apitest #main all: ${ALL} AEStest: AEStest.o aes128.o @@ -9,6 +10,7 @@ main: main.o utility.o utility.o: utility.c +apitest: apitest.o aes128.o api.o aes_cmac.o sw_driver.o md5.o clean: rm -f *.o ${ALL} diff --git a/SensorBoard_API.c b/SensorBoard_API.c deleted file mode 100644 index 64e0df7..0000000 --- a/SensorBoard_API.c +++ /dev/null @@ -1,39 +0,0 @@ -#include "api.h" - -#define NULL 0 - -ODAT* SB_B_ENC_ECB(IDATNUM n, KEY_ID keyid, IDAT idat) -{ - return NULL; -}; -ODAT* SB_B_ENC_CBC(IDATNUM n, KEY_ID keyid, IV initial, IDAT idat) -{ - return NULL; -}; - -ODAT* SB_B_DEC_ECB(IDATNUM n, KEY_ID keyid, IDAT idat) -{ - return NULL; -}; - -ODAT* SB_B_DEC_CBC(IDATNUM n, KEY_ID keyid, IV initial, IDAT idat) -{ - return NULL; -}; - -SB_B_GENERATE_MAC -SB_B_VERIFY_MAC - -SB_B_LOAD_KEY -SB_B_LOAD_PLAIN_KEY -SB_B_EXPORT_RAM_KEY -SB_B_INIT_RNG -SB_B_EXTEND_SEED -SB_B_RND - -SB_B_GET_STATUS -SB_B_GET_ID -SB_B_CANCEL -SB_B_IMPORT_KEY -SB_B_EXPORT_KEY - diff --git a/aes128.h b/aes128.h index 984cd1a..cf2188f 100644 --- a/aes128.h +++ b/aes128.h @@ -4,11 +4,11 @@ #include "secure_types.h" static const int aes_datalen_in_bit = 128; -static const int aes_datalen = 32; //aes_datalen_in_bit/8; +static const int aes_datalen = 16; //aes_datalen_in_bit/8; static const int aes_datalen_i = 4; //aes_datalen/sizeof(int); static const int aes_keylen_in_bit = 128; -static const int aes_keylen = 32; //aes_keylen_in_bit/8; +static const int aes_keylen = 16; //aes_keylen_in_bit/8; static const int aes_keylen_i = 4; //aes_keylen/sizeof(int); diff --git a/api.c b/api.c new file mode 100644 index 0000000..b70aaad --- /dev/null +++ b/api.c @@ -0,0 +1,171 @@ +#include +#include +#include "api.h" +#include "aes128.h" +#include "aes_cmac.h" + +#include "driver.h" + +static u128 buf[AES_BUFSIZE+1]; + +////// key table real +// ERROR(15) is for error indication +// should be always 0 +KEY_TABLE key_table[16]; + +////// Error Handling +// +int ERROR_FLAG = 0; +static void error_on(int val) +{ + ERROR_FLAG |= val; +} + +static void error_off(int val) +{ + ERROR_FLAG &= ~val; +} + +////// utils +// +static KEY_TABLE *key_entry(KEY_ID keyid) +{ + return &key_table[keyid]; +} + +static KEY128 *key(KEY_ID keyid) +{ + if ((keyid == RAMKEY && + key_table[keyid].key_flag== RAM_PLAIN_KEY) || + key_table[keyid].key_flag & KEY_PRESENT) { + return &key_table[keyid].key; + } else { + error_on(ERC_KEY_EMPTY); + return &key_table[ERROR].key; + } +} + +static void xor(u128 *p, u128 *q) +{ + for (int i = 0; i< 4; i++) + p->ul[i] ^= q->ul[i]; +} + +void SB_B_SET_WORKMEM() +{ + // dummy for future extension +} + +//////// APIs +// +ODAT* SB_B_ENC_ECB(int n, KEY_ID keyid, u128 *idat) +{ + memcpy(buf, idat, n*sizeof(u128)); + for (int i =0; i< n; i++) { + drv_AES_crypt((uint *)(&buf[i]), (uint8*)key(keyid)); + } + return buf; +}; + +ODAT* SB_B_DEC_ECB(int n, KEY_ID keyid, u128 *idat) +{ + memcpy(buf, idat, n*sizeof(u128)); + for (int i =0; i< n; i++) { + drv_AES_decrypt((uint *)(&buf[i]), (uint8*)key(keyid)); + } + return buf; +}; + +ODAT* SB_B_ENC_CBC(int n, KEY_ID keyid, u128 iv, u128 *idat) +{ + memcpy(buf, idat, n*sizeof(u128)); + for (int i =0; i< n; i++) { + if (i == 0) + xor(&buf[i], &iv); + else + xor(&buf[i], &buf[i-1]); + AES128_crypt((uint *)(&buf[i]), (uint8*)key(keyid)); + } + return buf; +}; + +ODAT* SB_B_DEC_CBC(int n, KEY_ID keyid, u128 iv, u128 *idat) +{ + memcpy(buf, idat, n*sizeof(u128)); + for (int i =0; i< n; i++) { + AES128_decrypt((uint *)(&buf[i]), (uint8*)key(keyid)); + if (i == 0) + xor(&buf[i], &iv); + else + xor(&buf[i], &idat[i-1]); + } + return buf; +}; + +ODAT* SB_B_GENERATE_MAC(int n, KEY_ID keyid, u128 *idat) +{ + return (ODAT*)AES_cmac((uint8*)key(keyid), (uint *)idat, n); +} + +uint SB_B_VERIFY_MAC(int n, int nbits, KEY_ID keyid, u128 *idat3, u128 *idat4) +{ + uint8 *p = + (uint8*)AES_cmac((uint8*)key(keyid), (uint *)idat3, n); + uint8 *q = (uint8*) idat4; + + for (int i = 0; i< nbits/8; i++) { + if (*p++ != *q++) return 1; + } + + int r = (nbits & 0x7); + if (r) { + int mask = ((1< md5 + //md5( val); + //*/ + return NULL; + +} +//SB_B_EXTEND_SEED + //SB_B_RND() +//{ + //return 0; +//} + +//SB_B_GET_STATUS +//SB_B_GET_ID +void SB_B_CANCEL() +{ + //dummy +} + +//SB_B_IMPORT_KEY +//SB_B_EXPORT_KEY + +// vi:expandtab:foldmethod=syntax sw=2 ts=2 diff --git a/api.h b/api.h index 3b89cd3..fa28011 100644 --- a/api.h +++ b/api.h @@ -3,19 +3,19 @@ #include "secure_types.h" // blocking functions -ODAT* SB_B_ENC_ECB(IDATNUM, KEY_ID, IDAT) ; -ODAT* SB_B_ENC_CBC(IDATNUM, KEY_ID, IV, IDAT) ; +ODAT* SB_B_ENC_ECB(int, KEY_ID, u128*) ; +ODAT* SB_B_ENC_CBC(int, KEY_ID, IV, u128*) ; -ODAT* SB_B_DEC_ECB(IDATNUM, KEY_ID, IDAT) ; -ODAT* SB_B_DEC_CBC(IDATNUM, KEY_ID, IV, IDAT) ; +ODAT* SB_B_DEC_ECB(int, KEY_ID, u128*) ; +ODAT* SB_B_DEC_CBC(int, KEY_ID, IV, u128*) ; -void SB_B_GENERATE_MAC(IDAT, KEY_ID, IDAT) ; -void SB_B_VERIFY_MAC(IDAT, KEY_ID, IDAT) ; +ODAT* SB_B_GENERATE_MAC(int, KEY_ID, u128*) ; +uint SB_B_VERIFY_MAC(int, int, KEY_ID, u128*, u128*) ; //void SB_B_LOAD_KEY(M1, M2, M3) ; -//void SB_B_LOAD_PLAIN_KEY(IDAT1) ; -void SB_B_EXPORT_RAM_KEY(void) ; -void SB_B_INIT_RNG(void) ; +void SB_B_LOAD_PLAIN_KEY(KEY128) ; +KEY128 SB_B_EXPORT_RAM_KEY(void) ; +ODAT* SB_B_INIT_RNG(void) ; void SB_B_EXTEND_SEED(void) ; void SB_B_RND(void) ; @@ -28,6 +28,37 @@ ODAT* SB_NB_ENC_ECB(IDATNUM, KEY_ID, IDAT) ; ODAT* SB_NB_ENC_CBC(IDATNUM, KEY_ID, IV, IDAT) ; +#define AES_BUFSIZE (1024) +// error flags +enum { + ERC_GENERA_ERROR = (1<<11), + ERC_BUSY = (1<<9), + ERC_RNG_SEED = (1<<7), + ERC_KEY_UPDATE_ERROR= (1<<6), + ERC_KEY_EMPTY = (1<<3), + ERC_KEY_INVALID = (1<<2), +}; + +// key index +enum { + SECRET_KEY = 0, + MASTER_ECU_KEY , MAC_KEY , + MAC , KEY1, + KEY2 , KEY3, + KEY4 , KEY5, + KEY6 , KEY7, + KEY8 , KEY9, + KEY10 , RAMKEY, + ERROR, +}; + +// key flags +enum { + KEY_USAGE = 0x01, + WILDCARD_PROTECTION = 0x02, + RAM_PLAIN_KEY = 0x04, + KEY_PRESENT = 0x08, +}; #endif/*__SECURE_API__*/ diff --git a/apitest.c b/apitest.c new file mode 100644 index 0000000..e566566 --- /dev/null +++ b/apitest.c @@ -0,0 +1,110 @@ +#include +#include + +#include "api.h" +#include "secure_types.h" + +extern KEY_TABLE key_table[15]; +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define NORMAL "\x1b[0m" + +//////////////////////////////////////////////////////////////////////////////// +void dump(const char *label,void *d,int len) +{ + int i; + uint8 *dt = (uint8*)d; + printf("%s",label); + for(i=0;i +#include "aes128.h" +#include "driver.h" +#include "md5.h" + +void drv_reset(int mask) +{ + // dummy +} + + +static int BCD(int d) +{ + return d+(int)(d/10)*6; +} + +void convert(int year, int month, int date, int dow, int hour, int minutes, int sec, uint8 *p) +{ + *p++ = BCD(year/100); + *p++ = BCD(year%100); + *p++ = BCD(month); + *p++ = BCD(date); + *p++ = BCD(dow); + *p++ = BCD(hour); + *p++ = BCD(minutes); + *p = BCD(sec); +} + +uint8 *drv_rtc_get_bin() +{ + struct tm *tm; + time_t t; + static uint8 buf[8]; + + // for software use POSIX stdlib + time(&t); + tm = localtime(&t); + + convert(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, + tm->tm_hour, tm->tm_min, tm->tm_sec, buf); + + return buf; +} + +void drv_AES_crypt(uint *msg, uint8 *key) +{ + AES128_crypt(msg, key); +} + +void drv_AES_decrypt(uint *msg, uint8 *key) +{ + AES128_decrypt(msg, key); +} + +static md5_context ctx; +static unsigned char md5sum[16]; + +uint8 *drv_MD5(int n, uint8 *msg, uint8 *key) +{ + md5_starts(&ctx); + md5_update(&ctx, msg, n); + md5_finish(&ctx, md5sum); + return md5sum; +} diff --git a/utility.c b/utility.c index 9a459ca..e782856 100644 --- a/utility.c +++ b/utility.c @@ -18,6 +18,7 @@ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } ; +static uint16 error_reg; /* "FPGA全暗号モジュールのSWリセット・割込み許可指定、及び入力データの全0設定" in secure-C-lib do nothing */ @@ -30,6 +31,9 @@ memset(key_table, 0, sizeof(key_table)); key_table[0].key = UID; + // for C-base lib only + error_reg = 0; + return; } //}}} @@ -91,7 +95,8 @@ /* SW実装したAESデコーダを実行 */ ODAT sb_b_aes_dec(KEY key, IDAT dat) //{{{1 { - memcpy(ret.uc, AES128_decrypt(dat.ul, (uint8*)key), 32); + AES128_decrypt(dat.ul, (uint8*)key); + memcpy(ret.uc, dat.ul, 32); return ret; } //}}} @@ -125,11 +130,12 @@ /* 関数をコールする事で、sb_gen_uidが新たなUIDを1値度だけ生成可能となる */ void sb_b_modify_uid() //{{{1 { + uint8 s[] = "hoge"; md5_context ctx; unsigned char md5sum[16]; md5_starts( &ctx ); - md5_update( &ctx, (uint8 *) &argv[i][2], strlen( &argv[i][2] ) ); + md5_update( &ctx, s, strlen(s) ); md5_finish( &ctx, md5sum ); @@ -183,7 +189,7 @@ // エラーレジスタの値を返す uint32 sb_b_read_error_reg() //{{{1 { - + return error_reg; } //}}} @@ -222,35 +228,43 @@ //}}} // UID,KEY_ID、AuthKEY_IDを引数として、計算式に従いM1(128bit)を返す -M1 sb_b_create_M1(u128 UID, KEY_ID kid, AUTHKEY_ID akid) //{{{1 +u128 sb_b_create_M1(u128 UID, KEY_ID kid, AUTHKEY_ID auth_kid) //{{{1 { - + memcpy(&(key_table[kid].M1.uc), UID.uc, 15); + key_table[kid].M1.uc[15] = ((kid&0xf)<<4)|(auth_kid&0xf); + return key_table[kid].M1; } //}}} // COUNTER、KEY FRAG、KEY(128bit)を引数として、計算式に従いM2(256bit)を返す -M2 sb_b_create_M2(uint32 counter, KEY_FLG flg, KEY key, KEY_ID kid) //{{{1 +u256 sb_b_create_M2(uint32 counter, KEY_FLG flg, KEY key, KEY_ID kid) //{{{1 { + key_table[kid].M2.ul[0] = ((counter&0xffffff) << 4) | ((flg >> 1) &1) ; + key_table[kid].M2.ul[1] = ((flg&1) << 30) ; + key_table[kid].M2.ul[2] = 0 ; + key_table[kid].M2.ul[3] = 0 ; + memcpy(key_table[kid].M2.ul+4, key, 16); + return key_table[kid].M2; } //}}} // M1とM2を引数として、計算式に従いM3(128bit)を返す -M3 sb_b_create_M3(M1 m1, M2 m2) //{{{1 +u128 sb_b_create_M3(M1 m1, M2 m2) //{{{1 { } //}}} // UID、KEY_ID、AuthKEY_ID、COUNTERを引数として、計算式に従いM4(256bit)を返す -M4 sb_b_create_M4(u128 uid, KEY_ID kid, AUTHKEY_ID akid, uint32 counter) //{{{1 +u128 sb_b_create_M4(u128 uid, KEY_ID kid, AUTHKEY_ID akid, uint32 counter) //{{{1 { } //}}} // M4を引数として、計算式に従いM5(128bit)を返す -M5 sb_b_create_M5(u128 key, M4 m4) //{{{1 +u128 sb_b_create_M5(u128 key, M4 m4) //{{{1 { } diff --git a/utility.h b/utility.h index e5fff49..1a594ed 100644 --- a/utility.h +++ b/utility.h @@ -26,11 +26,11 @@ ODAT sb_b_set_rand_seed(uint32 seed); void sb_b_kdf(KEY, IDAT); void sb_b_cmac(uint32, IDAT, IDAT); -M1 sb_b_create_M1(u128, KEY_ID, AUTHKEY_ID); -M2 sb_b_create_M2(uint32, KEY_FLG, KEY, KEY_ID); -M3 sb_b_create_M3(M1, M2); -M4 sb_b_create_M4(u128, KEY_ID, AUTHKEY_ID, uint32); -M5 sb_b_create_M5(u128, M4); +u128 sb_b_create_M1(u128, KEY_ID, AUTHKEY_ID); +u256 sb_b_create_M2(uint32, KEY_FLG, KEY, KEY_ID); +u128 sb_b_create_M3(M1, M2); +u128 sb_b_create_M4(u128, KEY_ID, AUTHKEY_ID, uint32); +u128 sb_b_create_M5(u128, M4); void sb_b_import_plain_key(uint32, KEY_FLG, KEY); M2 sb_b_export_plain_key(); void sb_b_import_each_plain_key(KEY_ID, uint32, KEY_FLG, KEY);