Skip to content

Commit

Permalink
Add LAC-x86 Optimized Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rk committed Feb 23, 2019
1 parent c26e445 commit dfc0431
Show file tree
Hide file tree
Showing 28 changed files with 5,486 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LAC-x86/Optimized_Implementation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CC = gcc
CFLAGS = -g -Wall -Wextra -O3 -DNDEBUG

HEADERS = api.h bch.h bin-lwe.h ecc.h lac_param.h rand.h fips202.h bch128.h bch192.h bch256.h
SOURCES = ake.c bch.c bin-lwe.c ecc.c encrypt.c ke.c kem.c rand.c fips202.c test_correctness.c test_cpucycles.c test_speed.c main.c
OBJECTS = ake.o ke.o kem.o encrypt.o ecc.o bch.o bin-lwe.o rand.o fips202.o test_correctness.o test_cpucycles.o test_speed.o main.o

all: lac $(OBJECTS)

lac : $(OBJECTS)
$(CC) -o lac $(OBJECTS)

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

.PHONY: clean
clean:
-$(RM) lac
-$(RM) *.o
98 changes: 98 additions & 0 deletions LAC-x86/Optimized_Implementation/ake.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "api.h"
#include "rand.h"
#include <string.h>

//Alice send: generate pk and sk, and send pk and cca kem ciphertext of pk_b to Bob
int crypto_ake_alice_send(unsigned char *pk,unsigned char *sk, unsigned char *pk_b, unsigned char *sk_a, unsigned char *c, unsigned char *k1)
{
unsigned char seed[SEED_LEN],buf[CRYPTO_SECRETKEYBYTES+SEED_LEN];
//check pointer
if(pk==NULL || sk==NULL || pk_b==NULL || k1==NULL)
{
return -1;
}
//call key generation algorithm to get pk and sk
kg(pk,(char*)sk);
// compute seed=hash(random_seed|sk_a)
random_bytes(buf,SEED_LEN);
memcpy(buf+SEED_LEN,sk_a,CRYPTO_SECRETKEYBYTES);
gen_seed(buf,CRYPTO_SECRETKEYBYTES+SEED_LEN,seed);
// call cca secure kem with seed to generate k1
kem_enc_fo_seed(pk_b,k1,c,seed);

return 0;
}
// Bob receive: receive pk, randomly choose m, and encryrpt m with pk to generate c, k1. k=HASH(pk_a,pk_b,pk,c3,k1,k2,k3)
int crypto_ake_bob_receive(unsigned char *pk_b, unsigned char *sk_b, unsigned char *pk_a, unsigned char *pk, unsigned char *c_in,unsigned char *c_out , unsigned char *k)
{
unsigned char k1[MESSAGE_LEN],k2[MESSAGE_LEN],k3[MESSAGE_LEN];
unsigned char in[3*MESSAGE_LEN+3*PK_LEN+CIPHER_LEN],seed[SEED_LEN];
unsigned char buf[CRYPTO_SECRETKEYBYTES+SEED_LEN];
//check pointer
if(pk_b==NULL || sk_b==NULL|| pk_a==NULL || pk==NULL|| c_in==NULL || c_out==NULL || k==NULL)
{
return -1;
}

// compute seed=hash(random_seed|sk_b)
random_bytes(buf,SEED_LEN);
memcpy(buf+SEED_LEN,sk_b,CRYPTO_SECRETKEYBYTES);
gen_seed(buf,CRYPTO_SECRETKEYBYTES+SEED_LEN,seed);
//call cca secure kem to generate k2
kem_enc_fo_seed(pk_a,k2,c_out,seed);

//call cpa kem algorithm to generate k3
random_bytes(k3,MESSAGE_LEN);
pke_enc(pk,k3,MESSAGE_LEN,c_out+CIPHER_LEN);

//decrypt c_in to get k1
kem_dec_fo(pk_b,(char *)sk_b,c_in,k1);

//compy pk_a,pk_b,pk to buf
memcpy(in,pk_a,PK_LEN);
memcpy(in+PK_LEN,pk_b,PK_LEN);
memcpy(in+2*PK_LEN,pk,PK_LEN);

//copy c3 to to buffer
memcpy(in+3*PK_LEN,c_out+CIPHER_LEN,CIPHER_LEN);
//copy k1,k2,k3 to buf
memcpy(in+3*PK_LEN+CIPHER_LEN,k1,MESSAGE_LEN);
memcpy(in+3*PK_LEN+CIPHER_LEN+MESSAGE_LEN,k2,MESSAGE_LEN);
memcpy(in+3*PK_LEN+CIPHER_LEN+2*MESSAGE_LEN,k3,MESSAGE_LEN);
// compute session key k=HASH(pk_a,pk_b,pk,c3,k1,k2,k3)
hash(in,3*MESSAGE_LEN+3*PK_LEN+CIPHER_LEN,k);

return 0;
}
//Alice receive: receive c, and decrypt to get k2, k3 and comute k=HASH(pk_a,pk_b,pk,c3,k1,k2,k3)
int crypto_ake_alice_receive(unsigned char *pk_a, unsigned char *sk_a, unsigned char *pk_b, unsigned char *pk, unsigned char *sk, unsigned char *c_in, unsigned char *k1, unsigned char *k)
{
unsigned char k2[MESSAGE_LEN],k3[MESSAGE_LEN];
unsigned char in[3*MESSAGE_LEN+3*PK_LEN+CIPHER_LEN];
unsigned char mlen;
//check pointer
if(pk_a==NULL || sk_a==NULL|| pk==NULL || sk==NULL || c_in==NULL || k1==NULL || k==NULL)
{
return -1;
}
//decrypt c of cca kem to get k2
kem_dec_fo(pk_a,(char *)sk_a,c_in,k2);

//decrypt c of cpa pke to get k3
pke_dec((char *)sk,c_in+CIPHER_LEN,k3,&mlen);

//copy pk_a,pk_b,pk to buf
memcpy(in,pk_a,PK_LEN);
memcpy(in+PK_LEN,pk_b,PK_LEN);
memcpy(in+2*PK_LEN,pk,PK_LEN);
//copy c3 to buf
memcpy(in+3*PK_LEN,c_in+CIPHER_LEN,CIPHER_LEN);
// copy k1,k2,k3 to buf
memcpy(in+3*PK_LEN+CIPHER_LEN,k1,MESSAGE_LEN);
memcpy(in+3*PK_LEN+CIPHER_LEN+MESSAGE_LEN,k2,MESSAGE_LEN);
memcpy(in+3*PK_LEN+CIPHER_LEN+2*MESSAGE_LEN,k3,MESSAGE_LEN);
// compute session key k=HASH(pk_a,pk_b,pk,c3,k1,k2,k3)
hash(in,3*MESSAGE_LEN+3*PK_LEN+CIPHER_LEN,k);

return 0;
}
55 changes: 55 additions & 0 deletions LAC-x86/Optimized_Implementation/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef api_h
#define api_h

#include "lac_param.h"
// Set these three values apropriately for your algorithm
#define CRYPTO_SECRETKEYBYTES DIM_N+PK_LEN
#define CRYPTO_PUBLICKEYBYTES PK_LEN
#define CRYPTO_BYTES MESSAGE_LEN
#define CRYPTO_CIPHERTEXTBYTES CIPHER_LEN

// Change the algorithm name
#define CRYPTO_ALGNAME STRENGTH
//functions for pke
int crypto_encrypt_keypair( unsigned char *pk, unsigned char *sk);
int crypto_encrypt( unsigned char *c, unsigned long long *clen, const unsigned char *m, unsigned long long mlen, const unsigned char *pk);
int crypto_encrypt_open(unsigned char *m, unsigned long long *mlen,const unsigned char *c, unsigned long long clen,const unsigned char *sk);
//key generation
int kg(unsigned char *pk, char *sk);
//key generation with seed
int kg_seed(unsigned char *pk, char *sk, unsigned char *seed);
// encryption
int pke_enc(const unsigned char *pk, const unsigned char *m, unsigned char mlen, unsigned char *c);
// encryption with seed
int pke_enc_seed(const unsigned char *pk, const unsigned char *m, unsigned char mlen, unsigned char *c, unsigned char *seed);
// decrypt
int pke_dec(const char *sk, const unsigned char *c, unsigned char *m, unsigned char *mlen);

//functions for kem
int crypto_kem_keypair( unsigned char *pk, unsigned char *sk);
int crypto_kem_enc( unsigned char *ct, unsigned char *ss, const unsigned char *pk);
int crypto_kem_dec( unsigned char *ss, const unsigned char *ct, const unsigned char *sk);

int kem_enc_fo(const unsigned char *pk, unsigned char *k, unsigned char *c);
// fo encryption for cca security with seed
int kem_enc_fo_seed(const unsigned char *pk, unsigned char *k, unsigned char *c, unsigned char *seed);
// decrypt of fo mode
int kem_dec_fo(const unsigned char *pk, const char *sk, const unsigned char *c, unsigned char *k);

//functions for ke
//Alice send: generate pk and sk, and send pk to Bob
int crypto_ke_alice_send(unsigned char *pk,unsigned char *sk);
// Bob receive: receive pk, randomly choose m, and encryrpt m with pk to generate c, k=HASH(pk,m).
int crypto_ke_bob_receive(unsigned char *pk, unsigned char *c, unsigned char *k);
//Alice receive: receive c, and decrypt to get m and comute k=HASH(pk,m)
int crypto_ke_alice_receive(unsigned char *pk, unsigned char *sk, unsigned char *c, unsigned char *k);

//functions for ake
//Alice send: generate pk and sk, and send pk to Bob
int crypto_ake_alice_send(unsigned char *pk,unsigned char *sk, unsigned char *pk_b, unsigned char *sk_a, unsigned char *c, unsigned char *k1);
// Bob receive: receive pk, randomly choose m, and encryrpt m with pk to generate c1 c2, k=HASH(pk,m).
int crypto_ake_bob_receive(unsigned char *pk_b, unsigned char *sk_b, unsigned char *pk_a, unsigned char *pk, unsigned char *c_in, unsigned char *c_out, unsigned char *k);
//Alice receive: receive c1,c2, and decrypt to get m and comute k=HASH(pk,m)
int crypto_ake_alice_receive(unsigned char *pk_a, unsigned char *sk_a,unsigned char *pk_b, unsigned char *pk, unsigned char *sk, unsigned char *c_in, unsigned char *k1, unsigned char *k);

#endif /* api_h */
Loading

0 comments on commit dfc0431

Please sign in to comment.