diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01dec77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +massif* +dhat* +.vimspector.json diff --git a/.vimspector.json b/.vimspector.json index 3be5c18..ee838ce 100644 --- a/.vimspector.json +++ b/.vimspector.json @@ -6,7 +6,7 @@ "configuration": { "request": "launch", "program": "./build/main.o", - "cwd": "/home/madeleine/classes/S9/crypto/", + "cwd": "/home/violette/classes/S9/crypto/", "externalConsole": true, "MIMode": "gdb" } diff --git a/Makefile b/Makefile index b89a720..ec6f900 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,10 @@ OJB = $(SRC:.c=.o) OUT = build CC = /usr/bin/gcc -CFLAGS = -ansi -Wall -std=c99 +CFLAGS = -ansi -Wall -std=c99 -O0 CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99 RM = /bin/rm -f $(OUT) +PART = -D PART %.o: %.c $(CC) -c $(CFLAGS) @@ -14,10 +15,10 @@ build: $(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o debug: - $(CC) $(SRC) $(CFDEBUG) -o $(OUT)/main.o + $(CC) $(SRC) $(PART)$(part) $(CFDEBUG) -o $(OUT)/main.o clean: $(RM) $(OBJ) $(OUT)/main.o -run: $(SRC) - $(CC) $(SRC) $(CFLAGS) -o $(OUT)/main.o && ./build/main.o +run: + $(CC) $(SRC) $(PART)$(part) $(CFLAGS) -o $(OUT)/main.o && ./build/main.o diff --git a/main.pdf b/main.pdf new file mode 100644 index 0000000..195e6c6 Binary files /dev/null and b/main.pdf differ diff --git a/main.typ b/main.typ index 4a56d2c..231735e 100644 --- a/main.typ +++ b/main.typ @@ -26,8 +26,103 @@ #line(length: 100%) = Notes -Le code présenté est retrouvable sur ssh://bigblase.xyz:/srv/git/crypto1 +Le code présenté est retrouvable sur ssh://bigblase.xyz:/srv/git/crypto1 ; +mot de passe FDS8EbKiDNoJh2QN #set heading(numbering: "I) a) i)") - +k = Chiffre de César +On lance le code si dessus 3 fois: (`make run part=1`) et on observe les sortie. +On obtient quelque chose comme: +#align(center)[ +` +-------PART 1------- +key: 5 +msg before: coding w/ freebsd style :) +cypher: htinsl b/ kwjjgxi xydqj :) +msg after: coding w/ freebsd style :) +key: 5 +msg before: ceciestlemessageclairadechiffre +cypher: hjhnjxyqjrjxxfljhqfnwfijhmnkkwj +msg after: ceciestlemessageclairadechiffre + +-------PART 1------- +key: 15 +msg before: ceciestlemessageclairadechiffre +cypher: rtrxthiatbthhpvtrapxgpstrwxuugt +msg after: ceciestlemessageclairadechiffre + +-------PART 1------- +key: 3 +msg before: ceciestlemessageclairadechiffre +cypher: fhflhvwohphvvdjhfodludghfkliiuh +msg after: ceciestlemessageclairadechiffre +` +] +J'ai laissé le premier message pour monter que l'on peut quand m^eme envoyer des +symboles. Je l'ai enlevé après par soucis de place / redondance. + +De leurs c^oté, Alice et Bob voient la clef key, le message msg et le cryptogramme +cypher. + +== + +Dans cette situation, Eve recevrait uniquement le cypher. On voit que quand on a +des symboles autre que des lettre, il devient facile de voir des motifs, des mots. +De plus, une analyse fréquentielle nous montrerais qu'il peut s'agir de texte +français. Comme Eve n'a pas la clef, elle pourrait alors éssayer de bruteforce +toutes les clefs du chiffrement de césar, ce que lui retournerait une solution +rapidement (il n'y a que 26 clef possible). + += OTP + +== +Comme on veut coder un message en char (1 octet) sur 512 bits (64 octets), j'ai +pris la liberté de tester avec un message de cette taille, plutot que 32 octets. + +Pour faire l'OTP, on fait un `XOR` entre le message et une chaine de 512 bits +aléatoire. En faisant ceci, on doit faire attention de bien terminer la string +avec un nullbyte, pour que notre programme fonctionne bien. Cela nous laisse donc +63 octets pour faire un message. + +J'ai représenté dans mon programme les valeurs des cryptogrammes et messages en +clair ainsi que leur représentation en hexa, pour pouvoir comparer et montrer que +nous avons bien fait un `XOR` Cependant, le compilateur à décider l'allouer des int, +plutot que des char, et nous nous retrouvons avec des choses de la forme de FFFFFFD6 +#align(center)[```` + + +-------PART 2------- +key: �2������v����_\�jXS��9�#��0�O|�1�T�2��d���BM�Ͽ�wi�u�z�) +msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd +msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 6 + 6C 6F 6E 67 78 64 +cypher: �W������Ϻ>;� 42��X�F��Y�)�X�7h�B��ܕ�Pڋ�0.}�ʺ��l� +cypher as hex: FFFFFF81 57 FFFFFFD6 FFFFFFBF FFFFFFBE FFFFFFAD FFFFFF99 FFFFFF91 13 FFFF +FCB FFFFFFC1 59 FFFFFF98 29 0E FFFFFFFA 58 FFFFFFC2 37 68 FFFFFFE7 42 FFFFFF9A FFFFFFF5 +FFFFFA3 FFFFFFCA FFFFFFBA 18 1D FFFFFF92 1A FFFFFFD6 1D 6C FFFFFF8D +msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd + + +-------PART 2------- +˺������py�F)SG��HP�-�D��� ��ƱZ�f���� �c��� +msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd +msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 68 69 66 66 72 65 69 6C 63 6F 6D 70 6F 72 74 65 36 34 63 68 61 72 63 65 73 74 70 6C 75 74 6F 74 6C 6F 6E 67 78 64 +cypher: ��9�P]?"��;#�J���%����j����(���ؠ��Ro�W��l}~�ʫ��z��� +cypher as hex: FFFFFFD6 19 FFFFFFAE 39 FFFFFF93 50 5D 3F 22 FFFFFFC8 FFFFFFE0 3B 23 FFFFFFB8 4A FFFFFF96 FFFFFFF3 FFFFFFE4 25 FFFFFFD5 FFFFFFD0 FFFFFFD8 7F FFFFFFAC 6A FFFFFF90 FFFFFFFB FFFFFFA0 FFFFFFD7 28 FFFFFF86 0F FFFFFFBA FFFFFFD3 FFFFFFD8 FFFFFFA0 FFFFFFA4 FFFFFF8F 52 6F FFFFFFE0 FFFFFF93 57 FFFFFFB6 17 FFFFFFF1 FFFFFFBB 6C 7D 7E FFFFFFBF FFFFFFCA FFFFFFAB FFFFFF93 FFFFFFF0 FFFFFFBF FFFFFFAA 7A FFFFFFF9 FFFFFFE1 17 01 FFFFFF92 +msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd + + +-------PART 2------- +key: ��� + ڍ-�.)�<�W9~vs`� + uN��5�# + ������m��Ek���J8��� 6k�(w� +msg before: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd +msg as hex: 63 65 63 69 65 73 74 6C 65 6D 65 73 73 61 67 65 63 6C 61 69 72 61 64 65 63 68 69 66 66 72 65 69 6C 63 6F 6D 70 6F 72 74 65 36 34 63 68 61 72 63 65 73 74 70 6C 75 74 6F 74 6C 6F 6E 67 78 64 +cypher: �fûnoe��@�]Z�[�4U�n&�S�Fb�������y��&���t9L�ǝ�OBuj�P +cypher as hex: FFFFFFB5 66 FFFFFFC3 FFFFFFBB 6E 6F 65 FFFFFFB6 FFFFFFE8 40 FFFFFF84 5D 5A FFFFFF80 5B FFFFFFE2 34 55 1F 1F 01 01 FFFFFFCB 6E 16 26 FFFFFFE3 FFFFFF83 53 FFFFFFB2 46 62 FFFFFFAF FFFFFFA0 FFFFFFB1 FFFFFFA2 FFFFFF90 FFFFFF80 FFFFFFDB 19 79 FFFFFFBC FFFFFFAF 26 03 FFFFFFB6 FFFFFFBE FFFFFFA1 74 39 4C FFFFFFF4 FFFFFFC7 FFFFFF9D FFFFFFFB 4F 42 75 6A 05 FFFFFFBE 50 13 +msg after: ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd + +````] + diff --git a/src/main.c b/src/main.c index 2eaf93b..5f4a4a3 100644 --- a/src/main.c +++ b/src/main.c @@ -3,8 +3,9 @@ #include #include +/* nuke sign from signed char */ #define REMOVE_SIGN(x) ( \ - (x) & ~ (1u << 8)\ + ((x) & ~ (1u << 8))\ ) #define SIZE_ALPH 26 @@ -15,16 +16,17 @@ static char *e1(char *, char); static char *d1(char *, char); static inline char gen1(); -static char *e2(char *, char*); -static char *d2(char *, char*); +static char *e2(char *, char *); +static char *d2(char *, char *); static char *gen2(); -static char *e3(char *, char*); -static char *d3(char *, char*); +static char *e3(char *, char *); +static char *d3(char *, char *); static char *gen3(); static inline char genChar(); static inline char genByte(); +static void printHex(char *); int main(int argc, char *argv[]) @@ -32,6 +34,7 @@ main(int argc, char *argv[]) srand(time(0)); /* init rand */ +#ifdef PART1 printf("-------PART 1-------\n"); int key = gen1(); /* SYMBOLS */ @@ -58,7 +61,9 @@ main(int argc, char *argv[]) free(cypher32); free(res32); +#endif +#ifdef PART2 /* PART 2 */ printf("-------PART 2-------\n"); char *key_otp = gen2(); @@ -68,8 +73,12 @@ main(int argc, char *argv[]) "ceciestlemessageclairadechiffreilcomporte64charcestplutotlongxd"; printf("key: %s\n", key_otp); printf("msg before: %s\n", msg_otp); + printf("msg as hex: "); + printHex(msg_otp); char *cypher_otp = e2(msg_otp, key_otp); printf("cypher: %s\n", cypher_otp); + printf("cypher as hex: "); + printHex(cypher_otp); char *res_otp = d2(cypher_otp, key_otp); printf("msg after: %s\n", res_otp); @@ -77,6 +86,9 @@ main(int argc, char *argv[]) free(res_otp); free(key_otp); +#endif + +#ifdef PART3 /* PART 3 */ printf("-------PART 3-------\n"); char *key_vignere = gen3(); @@ -92,6 +104,7 @@ main(int argc, char *argv[]) free(res_vignere); free(cypher_vignere); free(key_vignere); +#endif return 0; } @@ -99,8 +112,8 @@ main(int argc, char *argv[]) char* e1(char *clearText, char key) { - char *res = calloc(strlen(clearText), 1); - for (short k = 0 ; clearText[k] != '\0' ; ++k) { + char *res = (char *)calloc(strlen(clearText) + 1, 1); + for (unsigned short k = 0 ; clearText[k] != '\0' ; ++k) { if (clearText[k] >= 'a' && clearText[k] <= 'z') /* we encrypt a-z */ res[k] = (clearText[k] - 'a' + key) % 26 + 'a'; /* sign always + */ else @@ -112,12 +125,12 @@ e1(char *clearText, char key) char* d1(char *cypher, char key) { - char *res = calloc(strlen(cypher), 1); - for (short k = 0; cypher[k] != '\0' ; ++k) { + char *res = (char *)calloc(strlen(cypher) + 1, 1); + for (unsigned short k = 0; cypher[k] != '\0' ; ++k) { if (cypher[k] >= 'a' && cypher[k] <= 'z') {/* we decrypt a-z */ /* in C, % is the rest, not modulo. We take care of the sign by * always setting the signed bit to false :) */ - res[k] = 'a' + REMOVE_SIGN((cypher[k] - 'a' - key)) % 26; + res[k] = 'a' + REMOVE_SIGN(cypher[k] - 'a' - key) % 26; } else res[k] = cypher[k]; @@ -134,9 +147,9 @@ gen1() static inline char* e2(char *cypher, char *pad) { - char *res = calloc(SIZE_OTP, 1); + char *res = (char *)calloc(SIZE_OTP, 1); /* loop unwraping, might help cpu / compilator to use parallelisation */ - for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { + for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { res[k] = cypher[k] ^ pad[k]; res[k + 1] = cypher[k + 1] ^ pad[k + 1]; res[k + 2] = cypher[k + 2] ^ pad[k + 2]; @@ -153,10 +166,10 @@ e2(char *cypher, char *pad) static inline char* d2(char *clearText, char *pad) { - char *res = calloc(SIZE_OTP, 1); + char *res = (char *)calloc(SIZE_OTP, 1); /* loop unwraping, might help cpu / compilator to use parallelisation * NOTE prob gcc / clag already does that as a default, now we sure */ - for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { + for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { res[k] = clearText[k] ^ pad[k]; res[k + 1] = clearText[k + 1] ^ pad[k + 1]; res[k + 2] = clearText[k + 2] ^ pad[k + 2]; @@ -173,8 +186,8 @@ d2(char *clearText, char *pad) static char* gen2() { - char *res = calloc(sizeof(char) * SIZE_OTP, 1); - for (char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { + char *res = (char *)calloc(sizeof(char) * SIZE_OTP, 1); + for (unsigned char k = 0 ; k <= SIZE_OTP - 8 ; k = k + 8) { res[k] = genByte(); res[k + 1] = genByte(); res[k + 2] = genByte(); @@ -185,6 +198,8 @@ gen2() res[k + 7] = genByte(); } + res[SIZE_OTP - 1] = '\0'; + return res; } @@ -197,8 +212,8 @@ genByte() static char* e3(char *cypher, char *key) { - char *res = calloc(sizeof(char) * SIZE_MSG, 1); - for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { + char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1); + for(unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { /* we remove 2*a since cypher AND key contain 'a' */ res[k] = (cypher[k] + key[k] - 2 * 'a') % SIZE_ALPH + 'a'; res[k + 1] = (cypher[k + 1] + key[k + 1] - 2 * 'a') % SIZE_ALPH + 'a'; @@ -217,8 +232,8 @@ e3(char *cypher, char *key) static char* d3(char *cypher, char *key) { - char *res = calloc(sizeof(char) * SIZE_MSG, 1); - for(char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { + char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1); + for(unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { /* we DONT remove 'a' since cypher AND key contain 'a' */ res[k] = REMOVE_SIGN(cypher[k] - key[k]) % SIZE_ALPH + 'a'; res[k + 1] = REMOVE_SIGN(cypher[k + 1] - key[k + 1]) % SIZE_ALPH + 'a'; @@ -243,8 +258,8 @@ genChar() static char* gen3() { - char *res = calloc(sizeof(char) * SIZE_MSG, 1); - for (char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { + char *res = (char *)calloc(sizeof(char) * SIZE_MSG, 1); + for (unsigned char k = 0 ; k <= SIZE_MSG - 8 ; k = k + 8) { res[k] = genChar() + 'a'; res[k + 1] = genChar() + 'a'; res[k + 2] = genChar() + 'a'; @@ -258,3 +273,14 @@ gen3() res[SIZE_MSG - 1] = '\0'; return res; } + +static void +printHex(char *str) +{ + short k = 0; + while (str[k] != '\0') { + printf("%02X ", str[k]); + ++k; + } + printf("\n"); +}