From 5ca765b420d772f256a9f7ee73a8e4b9398d0d77 Mon Sep 17 00:00:00 2001 From: couturie Date: Sun, 27 May 2018 16:18:30 +0200 Subject: [PATCH] ida_gf64 --- Arduino/sketch_AES/sketch_AES.ino | 2 +- IDA/Makefile | 6 +- IDA/ida.cpp | 53 ++- IDA/ida_gf64.cpp | 538 ++++++++++++++++++++++++++++++ IDA/lena_small.png | Bin 2525 -> 0 bytes IDA/lena_small2.png | Bin 308 -> 0 bytes SboxAES/IOT/main.c | 59 +++- 7 files changed, 628 insertions(+), 30 deletions(-) create mode 100644 IDA/ida_gf64.cpp delete mode 100644 IDA/lena_small.png delete mode 100644 IDA/lena_small2.png diff --git a/Arduino/sketch_AES/sketch_AES.ino b/Arduino/sketch_AES/sketch_AES.ino index dcd8883..998f763 100644 --- a/Arduino/sketch_AES/sketch_AES.ino +++ b/Arduino/sketch_AES/sketch_AES.ino @@ -6,7 +6,7 @@ AES aes ; byte *key = (unsigned char*)"0123456789010123"; -const int size_mesg=16; +const int size_mesg=64; diff --git a/IDA/Makefile b/IDA/Makefile index a2afb98..8d7d811 100644 --- a/IDA/Makefile +++ b/IDA/Makefile @@ -11,5 +11,9 @@ test_mat2: test_mat2.cpp ida: ida.cpp g++ -std=c++11 $(CPP_FLAGS) -o $@ $< $(LIBS) + +ida_gf64: ida_gf64.cpp + g++ -o $@ $< -std=c++11 -O3 -lm -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -g -O3 -Wall -I/home/couturie/ajeter/jerasure/include /home/couturie/ajeter/jerasure/src/.libs/jerasure.o /home/couturie/ajeter/jerasure/src/.libs/galois.o -lgf_complete -fpermissive + clean: - rm test_mat2 ida + rm test_mat2 ida ida_gf64 diff --git a/IDA/ida.cpp b/IDA/ida.cpp index dd7791a..e854c3a 100644 --- a/IDA/ida.cpp +++ b/IDA/ida.cpp @@ -1,6 +1,3 @@ -// g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat2.cpp -o test_mat2 -I /home/couturie/tools/armadillo-6.200.5/include/ -lc -lm -lpthread -lgfortran -DMAX_STACK_ALLOC=2048 -Wall -m64 -DF_INTERFACE_GFORT -fPIC -DSMP_SERVER -DNO_WARMUP -DMAX_CPU_NUMBER=8 -DNO_AFFINITY -UCOMPLEX -DDOUBLE -I/home/couturie/tools/openblas/include -I/home/couturie/Downloads/OpenBLAS-0.2.15/ /home/couturie/tools/openblas/lib/libopenblas_haswellp-r0.2.15.a - - #include #include @@ -158,13 +155,16 @@ convert_vec250_to_vec256(vector vec250){ Mat readFullFile(int n, int k, long& sizeFile, int &lc) { // ifstream stream("lena.png", ios::in | ios::binary | ios::ate); - ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); -// ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); + ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); +// ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); + sizeFile=stream.tellg(); cout< readFullFile(int n, int k, long& sizeFile, int &lc) { tmpSize>>=8; } +//we rebuild the size in order to check that the routine is correct cout<<"rebuild"<=0;i--) { @@ -193,6 +194,14 @@ Mat readFullFile(int n, int k, long& sizeFile, int &lc) { // for(int i=0;i<8;i++) // contents.insert(i,mysize[i]); contents.insert (contents.begin(), mysize, mysize+8); + + cout<<"start of the vector"< contents2=convert_vec256_to_vec250(contents); cout<<"res contents2 "< matData(&contents2[0],1,contents2.size()); @@ -242,12 +251,9 @@ int main( int argc, char *argv[] ) { - int full=0; int n=8; int k=4; - int Tb=64; - int l=10;//399*Tb; @@ -292,15 +298,21 @@ int main( int argc, char *argv[] ) { cout<<"tatat"< C3; + + int off=0; //read k files among n - for(int i=0;i=0;i--) { + for(int i=0+off;i d2=readFile(str.c_str()); - C3.insert_rows(i,d2); + C3.insert_rows(l,d2); + l++; } @@ -311,6 +323,7 @@ int main( int argc, char *argv[] ) { Mat Cs=conv_to>::from(C3); cout< S2=conv_to>::from(SS2); S2=mod(S2,mm); -// cout< res =conv_to< vector >::from(S2.row(0)); cout<<"res size "< res2=convert_vec250_to_vec256(res); cout<<"res2 size "< +#include +#include +#include +#include +#include +#include +#include +#include // std::random_shuffle +#include // std::vector +extern "C" { + #include "jerasure.h" +} + +typedef unsigned long mylong; +#define LLUI (long long unsigned int) + + +using namespace std; + + +void display(mylong *mat, int r, int c) { + for(int i=0;imultiply.w64(gf,m1[i*c1+k], m2[k*c2+j]); + } + } + } + return product; +} + + + +int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows) +{ + int cols, i, j, k, x, rs2; + int row_start; + mylong tmp, inverse; + + cols = rows; + + k = 0; + for (i = 0; i < rows; i++) { + for (j = 0; j < cols; j++) { + inv[k] = (i == j) ? 1 : 0; + k++; + } + } +// display(inv, rows, rows); +// printf("\n"); + + /* First -- convert into upper triangular */ + for (i = 0; i < cols; i++) { + row_start = cols*i; + + /* Swap rows if we ave a zero i,i element. If we can't swap, then the + matrix was not invertible */ + + if (mat[row_start+i] == 0) { + for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ; + if (j == rows) return -1; + rs2 = j*cols; + for (k = 0; k < cols; k++) { + tmp = mat[row_start+k]; + mat[row_start+k] = mat[rs2+k]; + mat[rs2+k] = tmp; + tmp = inv[row_start+k]; + inv[row_start+k] = inv[rs2+k]; + inv[rs2+k] = tmp; + } + } + + /* Multiply the row by 1/element i,i */ + tmp = mat[row_start+i]; + if (tmp != 1) { + inverse = gf->divide.w64(gf,1, tmp); + for (j = 0; j < cols; j++) { + mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse); + inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse); + } + } + + /* Now for each j>i, add A_ji*Ai to Aj */ + k = row_start+i; + for (j = i+1; j != cols; j++) { + k += cols; + if (mat[k] != 0) { + if (mat[k] == 1) { + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= mat[row_start+x]; + inv[rs2+x] ^= inv[row_start+x]; + } + } else { + tmp = mat[k]; + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]); + inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]); + } + } + } + } + } + + /* Now the matrix is upper triangular. Start at the top and multiply down */ + + for (i = rows-1; i >= 0; i--) { + row_start = i*cols; + for (j = 0; j < i; j++) { + rs2 = j*cols; + if (mat[rs2+i] != 0) { + tmp = mat[rs2+i]; + mat[rs2+i] = 0; + for (k = 0; k < cols; k++) { + inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]); + } + } + } + } + +/* printf("mat\n"); + display(mat, rows, rows); + printf("\n"); + printf("inv\n"); + display(inv, rows, rows); + printf("\n"); +*/ + return 0; +} + + + + +int invertible_matrix(gf_t *gf, int *mat, int rows, int w) +{ + int cols, i, j, k, x, rs2; + int row_start; + mylong tmp, inverse; + + cols = rows; + + /* First -- convert into upper triangular */ + for (i = 0; i < cols; i++) { + row_start = cols*i; + + /* Swap rows if we ave a zero i,i element. If we can't swap, then the + matrix was not invertible */ + + if (mat[row_start+i] == 0) { + for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ; + if (j == rows) return 0; + rs2 = j*cols; + for (k = 0; k < cols; k++) { + tmp = mat[row_start+k]; + mat[row_start+k] = mat[rs2+k]; + mat[rs2+k] = tmp; + } + } + + /* Multiply the row by 1/element i,i */ + tmp = mat[row_start+i]; + if (tmp != 1) { + inverse = gf->divide.w64(gf,1, tmp); + for (j = 0; j < cols; j++) { + mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse); + } + } + + /* Now for each j>i, add A_ji*Ai to Aj */ + k = row_start+i; + for (j = i+1; j != cols; j++) { + k += cols; + if (mat[k] != 0) { + if (mat[k] == 1) { + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= mat[row_start+x]; + } + } else { + tmp = mat[k]; + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]); + } + } + } + } + } + return 1; +} + + + + + +mylong* readFullFile(int n, int t, mylong& sizeFile, mylong & padded_size) { + +// ifstream stream("lena.png", ios::in | ios::binary | ios::ate); +// ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate); + ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); + + sizeFile=stream.tellg(); + std::cout << sizeFile << std::endl; + stream.seekg(0, ios::beg); + + + + + + + vector contents((istreambuf_iterator(stream)), istreambuf_iterator()); + + + + + + + + //make padding, we need to pad to be divisible by 8*t, we + if((sizeFile+8)%(8*t)!=0) { + cout<<(int)(sizeFile/(8*t))<(p_contents); + + padded_size=contents.size()/8; + + mylong *p_contents2=new mylong[padded_size]; + memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size); + //mylong *p_contents2=(mylong*)p_contents; + + p_contents2[0]=sizeFile; + + + + +/* for(int i=0;i=0;i--) { + res<<=8; + res+=p_contents[i]; + } + + cout << "convert val " << (long)res << endl; + + res=0; + for(int i=16-1;i>=8;i--) { + res<<=8; + res+=p_contents[i]; + } + + cout << "convert val " << (long)res << endl; + */ + + return p_contents2; +} + + +void saveFile(uint8_t *data, const char *fileName,long size_file) { + cout<<"size file "< elapsed_seconds = end-start; + std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n"; + +// display(matC,t,t); + + + //Save trunks + for(int i=0;i myvector; + + // set some values: + for (int i=0; i=t;i--) { +// for(int i=0;i(&matS2[1]); + saveFile(reconstucted_data, "lena2.png",new_size); + return 0; +} + + diff --git a/IDA/lena_small.png b/IDA/lena_small.png deleted file mode 100644 index 872ef3ae8beed21e1eeb6558607a29a5849c2784..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2525 zcmV<32_p81P)*Spe|odEUc! zm~UowW)I$7uh-6Ijoln-J4!?Awup+H1nEggO}T&zqzZ9`6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i>up;@L&Gnl^W>D1#s=qni<^*oN-L zKfk=IlJ!_|+D`kou1BeP{>4x1e&;98eCpB9dWaS~)#d9IWncL8>5D(TbWlK&C5X83 zaK}3iK;mUG^{r*Sc`l`cQ8mgvFan4RfbETI_k7%38|~eh9`+jUUeMtm}eQY^qhAh@vc zh?b~D%{hvpUh1YSh}v>Bannids&X*w99u_2w}(ltoF z*OS^`+|8Q_m4!cbvg162fXrcGMNyQ2&5$A*&1!8UAVLMGqMB$tI^=5H$U19l$DV)T z^o7^1My_%K%q*@h9`uXDVPk#CJAd(JUnxi|`Ks=AOb8MfLf}S*khW0~YDF!E83ojWx7CvID{g`5Ra7CgS4MS_8pY z)W}LaL)7Vjj!7BxBi*l{)1Xj013Vgz?p0A;*FZdfy9R(= zJ*;ZgSzcIQGY@u3Cnck0;kY?;o~p{_YUBB{E7dPXAMHRCM_PKb_7Ft z@w=D5`^_u#>>oZe^#VR%aI>OTN_Cdz_qKm^xZBf7WA(^F?g^9zV~jDKhH6>`Wv&z4 zJdytGKmOj+fAqP7{V^hTjxROlvdJKC4R@3=AN=3-cV51E<~O=@`__K@d{vQA9y0z&sCw+4OMy#TUSbCTzNHVd^s`_$>_Xj)eRBb!k@>+Sd6y*+aV z0^{l7)8i;96a)(Z0{Z^~-|hb>Di_|)-3ayd=I-+o77QRX9s~dk3+M9Z1p5>N`TqV5 z13OJI^6~e}sLBEZ3I_T2`Rd~0J0uJN@9r-)B=+v?&&TI$f@R;vv4~>;0000 #include #include +#include #include"aes.h" #define CTR 1 -const int size_mesg=64; +const int size_mesg=1024; typedef unsigned char byte; @@ -19,6 +20,22 @@ typedef unsigned char byte; +double TimeStart() +{ + struct timeval tstart; + gettimeofday(&tstart,0); + return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) ); +} + +double TimeStop(double t) +{ + struct timeval tend; + + gettimeofday(&tend,0); + t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t; + return (t); +} + @@ -63,26 +80,46 @@ int main(int argc, char** argv) { for(int i=0;i