diff --git a/README.md b/README.md index e9933f5..d46f659 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,3 @@ Cryptographie - Outils et algorithmes - td : [Généralités, chiffrements par flot](td/td1.pdf) - tp : [lfsr, xtea, hachages](tp/tp1) - diff --git a/tp/tp1/README.md b/tp/tp1/README.md new file mode 100644 index 0000000..1fa2b08 --- /dev/null +++ b/tp/tp1/README.md @@ -0,0 +1,41 @@ +# TP1 +## Ex1 +L’objectif de cet exercice est de chiffrer une image en ligne de commande à +l’aide des outils fournis par la librairie openssl. + +1. Prenez une image de votre choix et convertissez la dans le format ppm à l’aide de la commande +`convert`. + +2. Avec la commande `head`, mettez les 3 premières lignes de votre fichier ppm dans un fichier +header.txt. + +3. Avec la commande tail mettez tout le fichier ppm sauf les 3 premières lignes dans un fichier +body.bin. + +4. Chiffrez avec openssl en AES-ECB le fichier body.bin. +5. Remettez l’en-tête header.txt au début du fichier obtenu à la question précédente pour avoir +une image chiffrée, et observez le résultat. + +## EX2 +Le but est d'implanter un registre à décalage linéaire, sur un octet. + + +À chaque étape, le registre (un octet) $(b_7,b_6,b_5,b_4,b_3,b_2,b_1,b_0)$ est décalé à gauche, et +le bit $b_0$ est remplacé par un bit, calculé par une fonction linéaire $f$. + + + +Vous disposez d'un [fichier](src/ex1/file.crypt) crypté avec un lfsr, en faisant +un XOR de chacun des octets avec les valeurs successives du registre. L'état +initial du registre était `0xa7`, et la fonction utilisée + + +\[ + f(b_7,b_6,b_5,b_4,b_3,b_2,b_1,b_0) = b_7\oplus b_6\oplus b_5\oplus b_4\oplus b_3\oplus b_1 +\] + +Retrouver le fichier initial. + +Vous pouvez utiliser la fonction interne `__builtin_parity` de `gcc`. + + diff --git a/tp/tp1/aide.md b/tp/tp1/aide.md new file mode 100644 index 0000000..422bef6 --- /dev/null +++ b/tp/tp1/aide.md @@ -0,0 +1,41 @@ +### Setting a bit + +Use the bitwise OR operator (|) to set a bit. + +`number |= 1 << x;` + +That will set bit x. + +### Clearing a bit + +Use the bitwise AND operator (&) to clear a bit. + +`number &= ~(1 << x);` + +That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it. + +### Toggling a bit + +The XOR operator (^) can be used to toggle a bit. + +`number ^= 1 << x;` + +That will toggle bit x. + +### Checking a bit + +You didn't ask for this but I might as well add it. + +To check a bit, shift the number x to the right, then bitwise AND it: + +`bit = (number >> x) & 1;` + +That will put the value of bit x into the variable bit. + +### Changing the nth bit to x + +Setting the nth bit to either 1 or 0 can be achieved with the following: + +`number ^= (-x ^ number) & (1 << n);` + +Bit n will be set if x is 1, and cleared if x is 0. diff --git a/tp/tp1/img/TEA.png b/tp/tp1/img/TEA.png new file mode 100644 index 0000000..e6e7f45 Binary files /dev/null and b/tp/tp1/img/TEA.png differ diff --git a/tp/tp1/img/feistel.png b/tp/tp1/img/feistel.png new file mode 100644 index 0000000..dbf149e Binary files /dev/null and b/tp/tp1/img/feistel.png differ diff --git a/tp/tp1/img/feistel.svg b/tp/tp1/img/feistel.svg new file mode 100644 index 0000000..ef5585a --- /dev/null +++ b/tp/tp1/img/feistel.svg @@ -0,0 +1,1263 @@ + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Chiffrement + Clair + Chiffré + Déchiffrement + Chiffré + Clair + + + + + + + diff --git a/tp/tp1/img/xtea.png b/tp/tp1/img/xtea.png new file mode 100644 index 0000000..554ebfd Binary files /dev/null and b/tp/tp1/img/xtea.png differ diff --git a/tp/tp1/src/ex2/file.crypt b/tp/tp1/src/ex2/file.crypt new file mode 100644 index 0000000..1f1e6c2 Binary files /dev/null and b/tp/tp1/src/ex2/file.crypt differ diff --git a/tp/tp1/src/ex2/lfsr.c b/tp/tp1/src/ex2/lfsr.c new file mode 100644 index 0000000..f2037ef --- /dev/null +++ b/tp/tp1/src/ex2/lfsr.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +unsigned char next( + unsigned char lfsr, // le registre + unsigned char retroaction_f // les bits selectionnés par la + ) // fonction de retroaction +{ + // TODO +} + +int main(int argc, char *argv[]) +{ + int fd_in,fd_out; + unsigned char w,buf,f; + + + assert(argc >= 4); + fd_in = open(argv[1],O_RDONLY); + fd_out = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600); + w = (unsigned char)strtol(argv[3],NULL,0); + f = ; // TODO + + while(1){ + ssize_t nb = read(fd_in,&buf,1); + if (nb <=0) + break; + buf ^= w; + write(fd_out,&buf,1); + w=next(w,f); + } + return 0; +}