1
0
This commit is contained in:
Denis Monnerat 2025-03-19 18:31:09 +01:00
parent 9d0c424e84
commit 7e0b94ddd8
18 changed files with 981 additions and 0 deletions

@ -12,5 +12,8 @@ Cryptographie - Outils et algorithmes
- cm : [Chiffrements par bloc, algorithmes à clefs publiques](cours/crypto.pdf).
- tp : [Substitution-Permutation-Network](td_tp/tp2)
#### Semaine 3
- cm : [Algorithmes à clefs publiques](cours/crypto.pdf).
- tp : [Diffie-Hellman](td_tp/tp3)

Binary file not shown.

103
td_tp/tp3/README.md Normal file

@ -0,0 +1,103 @@
## TP3
> Le but du tp est de mettre en oeuvre une communication sécurisée entre
> un client et un serveur, en deux phases :
> - Echange d'une clé.
> - Cryptage de la communication par un algorithme de chiffrement
> symétrique à l'aide de la clé echangée. (XTEA pour ce tp)
[Rappel de cours](../cours/crypto.pdf)
### Diffie-Hellman
<div align="center">
<img src="./dh.png">
</div>
Le système (et sa sécurité) de Diffie-Hellman repose sur la difficulté
pratique, pour des grands nombres, de calculer le logarithme discret
dans un groupe multiplicatif $\mathbb{Z}_{p}^{*}$ avec $p$
premier.
On rappelle que lorsque $p$ est premier, $\mathbb{Z}_{p}^{*}$ est cyclique. Si $g$ est un générateur,
tout élément $x\in \mathbb{Z}_{p}^{*}$ s'écrit (de manière
unique) sous la forme
\[
x=g^a,\ \ 1\leq a \leq p-1
\]
<!--\\((\\mathbb{Z}_{p}^{\*},.)\\) est ismorphe au groupe additif
\\((\\mathbb{Z}_{p-1},+)\\)-->
Par définition, $a$ est le logarithme (discret) de $x$ dans la
base $g$ noté
\[
a=\log_{g}(x)
\]
Le schéma en haut de la page illustre comment deux entités peuvent
échangés une clef. Alice génére $p$, un générateur $g$. Il
choisit un nombre aléatoire $a$ ($2\leq a\leq p-2$), et envoie
à Bob le triplet $((p,g,g^a)$. Bob recoit le triplet, et choisit un
nombre $b$. Il envoie à Alice $g^b$. La clé calculée par Alice,
et Bob est
\|
(g^a)^b = (g^b)^a= g^{ab}
\]
Pour calculer la clé, il faut connaître $ab$. Or seuls $g^a$ et
$g^b$ ont été échangés. Ce qui nous ramène au problème du
logarithme discret qui est difficile en pratique pour des grands
nombres.
##### Votre travail
Le but du tp est d'écrire un client/serveur TCP :
- Le client et le serveur utilise Diffie-Hellman pour échanger une clé
sur 16 octets. On utilisera des nombres premiers de Sophie Germain.
- Le client saisit un message de l'utilisateur, le chiffre avec XTEA,
et envoit le message chiffré.
- Le serveur reçoit le message chiffré, le déchiffre, et l'affiche.
1. Trouver un générateur peut-être long. En effet, tout ce qu'on l'on
sait sur l'ordre d'un élément de $\mathbb{Z}_p^{*}$, c'est
qu'il divise l'ordre du groupe, i.e $p-1$. Pour que la
vérification soit plus rapide, on s'interesse aux nombres premiers
dit de Sophie Germain. Ils ont la particularité d'être sous la
forme $2q+1$, avec $q$ premier. Ainsi, l'odre de
$\mathbb{Z}_{p}^{*}$ est $2q$. L'odre possible d'un
élément ne peut être que $2$, $q$ ou $2q$. Ce qui fait peu (combien) de
test à effectuer quand on cherche un générateur.
Ecrire un script à l'aide de la commande `primesieve` qui donne
tous les nombres premiers de Sophie Germain dans
$[2^{62},2^{62}+20000]$. (il faut quelques minutes. Assurez-vous
que votre shell permet de représenter de tels nombres. Comment ?)
2. Ecrire les [fonctions](src/df.c) suivantes :
```c
typedef unsigned __int128 uint128;
typedef unsigned long long int uint64;
uint64 expm(uint64 m, uint64 e, uint64 n);
// calcule m^e modulo n
uint64 generateur(uint64 p);
// calcule un generateur de (Zp)*
// en supposant que p est un nombre premier
// de Sophie Germain
```
Testez ([test_df.c](src/test_df.c)) avec $p = 4611686018427402023$ pour trouver un
générateur.
Remarque : pour la génération d'un grand nombre
premier, utilisez le programme `primesieve` qsui se trouve dans
```
/export/documents/is1+2/asr
```
.

BIN
td_tp/tp3/dh.png Normal file

Binary file not shown.

After

(image error) Size: 51 KiB

294
td_tp/tp3/dh.svg Normal file

@ -0,0 +1,294 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/ svg10.dtd">
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.0"
width="820"
height="450"
id="svg2">
<defs
id="defs4" />
<g
transform="translate(-46.31577,-632.20074)"
id="layer1">
<path
d="M 318.11284,802.35195 L 581.38211,802.35195 L 580.10046,798.35195 L 596.15877,803.93363 L 580.10046,809.51532 L 581.38211,805.51532 L 318.11284,805.51532 L 318.11284,802.35195 z"
id="rect3239"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<rect
width="262.83951"
height="365.70755"
rx="38.638252"
ry="37.143986"
x="47.13364"
y="676.1059"
transform="matrix(1,0,7.79566e-3,0.9999696,0,0)"
id="rect2453"
style="opacity:1;fill:#ffff7f;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.75401145;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<rect
width="262.83951"
height="365.70755"
rx="38.638252"
ry="37.143986"
x="590.46265"
y="676.1059"
transform="matrix(1,0,7.7956625e-3,0.9999696,0,0)"
id="rect3225"
style="opacity:1;fill:#ffff7f;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.75401145;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<rect
width="151.51535"
height="61.515339"
rx="0"
ry="0"
x="383.09946"
y="773.17596"
id="rect3227"
style="opacity:1;fill:#ffffbf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.66723782;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<path
d="M 596.22925,904.80102 L 332.95998,904.80102 L 334.24163,908.80102 L 318.18332,903.21934 L 334.24163,897.63765 L 332.95998,901.63765 L 596.22925,901.63765 L 596.22925,904.80102 z"
id="path3244"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<rect
width="151.51535"
height="61.515339"
rx="0"
ry="0"
x="383.09946"
y="872.46167"
id="rect3229"
style="opacity:1;fill:#ffffbf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.66723782;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<text
x="135.3013"
y="744.38751"
id="text3266"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="135.3013"
y="744.38751"
id="tspan3268"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a, g, p</tspan></text>
<text
x="446.49091"
y="916.10413"
id="text3278"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="446.49091"
y="916.10413"
id="tspan3280"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">B</tspan></text>
<text
x="406.237"
y="813.03033"
id="text3282"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="406.237"
y="813.03033"
id="tspan3284"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">g, p, A</tspan></text>
<text
x="718.12823"
y="751.3045"
id="text3294"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="718.12823"
y="751.3045"
id="tspan3296"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
<text
x="146.97318"
y="665.97028"
id="text3302"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="146.97318"
y="665.97028"
id="tspan3304"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">Alice</tspan></text>
<text
x="695.94464"
y="665.97028"
id="text3308"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="695.94464"
y="665.97028"
id="tspan3310"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">Bob</tspan></text>
<text
x="83.419472"
y="814.86749"
id="text3270"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="83.419472"
y="814.86749"
id="tspan3272"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">A = g mod p</tspan></text>
<text
x="169.43015"
y="794.96851"
id="text3312"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="169.43015"
y="794.96851"
id="tspan3314"
style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a</tspan></text>
<text
x="79.051758"
y="1073.0292"
id="text3352"
xml:space="preserve"
style="font-size:22px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="79.051758"
y="1073.0292"
id="tspan3354"
style="font-size:22px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">K = A mod p = (g mod p) mod p = g mod p = (g mod p) mod p = B mod p</tspan></text>
<g
transform="translate(-8.573848,79.690866)"
id="g3322">
<text
x="88.644684"
y="834.67328"
id="text3324"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="88.644684"
y="834.67328"
id="tspan3326"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">K = B mod p</tspan></text>
<text
x="176.65536"
y="814.77429"
id="text3328"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="176.65536"
y="814.77429"
id="tspan3330"
style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a</tspan></text>
<text
x="139.62561"
y="980.89594"
id="text3356"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="139.62561"
y="980.89594"
id="tspan3358"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
<text
x="259.09045"
y="979.78265"
id="text3360"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="259.09045"
y="979.78265"
id="tspan3362"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a</tspan></text>
<text
x="339.73691"
y="980.89594"
id="text3364"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="339.73691"
y="980.89594"
id="tspan3366"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
<text
x="451.09045"
y="980.89594"
id="text3368"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="451.09045"
y="980.89594"
id="tspan3370"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">ab</tspan></text>
<text
x="574.79761"
y="980.89594"
id="text3372"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="574.79761"
y="980.89594"
id="tspan3374"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
<text
x="653.44403"
y="979.78265"
id="text3376"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="653.44403"
y="979.78265"
id="tspan3378"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a</tspan></text>
<text
x="768.09045"
y="979.78265"
id="text3380"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Arial;-inkscape-font-specification:Arial"><tspan
x="768.09045"
y="979.78265"
id="tspan3382"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">a</tspan></text>
</g>
<g
transform="translate(534.75505,81.360788)"
id="g3332">
<text
x="88.644684"
y="834.67328"
id="text3334"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="88.644684"
y="834.67328"
id="tspan3336"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">K = A mod p</tspan></text>
<text
x="174.65536"
y="814.77429"
id="text3338"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="174.65536"
y="814.77429"
id="tspan3340"
style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
</g>
<g
transform="translate(536.75896,-18.135855)"
id="g3342">
<text
x="88.644684"
y="834.67328"
id="text3344"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="88.644684"
y="834.67328"
id="tspan3346"
style="font-size:36px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">B = g mod p</tspan></text>
<text
x="174.65536"
y="814.77429"
id="text3348"
xml:space="preserve"
style="font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Times New Roman;-inkscape-font-specification:Times New Roman"><tspan
x="174.65536"
y="814.77429"
id="tspan3350"
style="font-size:18px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:100%;writing-mode:lr-tb;text-anchor:start;font-family:Arial;-inkscape-font-specification:Arial">b</tspan></text>
</g>
</g>
</svg>

After

(image error) Size: 20 KiB

13
td_tp/tp3/src/Makefile Normal file

@ -0,0 +1,13 @@
all : test_df
./lib_tea/tea.o : ./lib_tea/tea.c
gcc -c ./lib_tea/tea.c -o ./lib_tea/tea.o
df.o : df.c
gcc -c df.c
test_df.o : test_df.c
gcc -c test_df.c
test_df : test_df.o df.o ./lib_tea/tea.o
gcc -o test_df df.o test_df.o ./lib_tea/tea.o

23
td_tp/tp3/src/df.c Normal file

@ -0,0 +1,23 @@
#include "df.h"
uint64 expm(uint64 m, uint64 e, uint64 mod)
{
uint128 _r=1;
uint128 _m=(uint128)m;
uint128 _mod = (uint128)mod;
// TODO
return (uint64)_r;
}
uint64 generateur(uint64 p)
{
uint64 g=2;
uint64 r=1;
// TODO
}

15
td_tp/tp3/src/df.h Normal file

@ -0,0 +1,15 @@
#ifndef _DF_H
#define _DF_H
typedef unsigned __int128 uint128;
typedef unsigned long long int uint64;
uint64 expm(uint64 m, uint64 e, uint64 n);
// calcule m^e modulo n
uint64 generateur(uint64 p);
// calcule un generateur de (Zp)*
// en supposant que p est un nombre premier
// de Sophie Germain
#endif

@ -0,0 +1,39 @@
#include "tea.h"
#include<string.h>
#define TOUR 32
void encrypt_tea (uint32_t * v, uint32_t* k)
{
uint32_t v0=v[0], v1=v[1], sum=0, i; /* initialisation */
uint32_t delta=0x9e3779b9; /* constantes de clef */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* mise en cache de la clef */
for (i=0; i < TOUR; i++) { /* boucle principale */
sum += delta;
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
}
v[0]=v0; v[1]=v1;
}
void decrypt_tea (uint32_t* v, uint32_t* k)
{
uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* initialisation */
uint32_t delta=0x9e3779b9; /* constantes de clefs */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* mise en cache de la clef */
for (i=0; i<TOUR; i++) { /* boucle principale */
v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
sum -= delta;
}
v[0]=v0; v[1]=v1;
}
uint64_t hash_block_tea(char b[24])
{
uint32_t v[2];
uint32_t k[4];
memcpy(v,b,2*sizeof(uint32_t));
memcpy(k,b+2*sizeof(uint32_t),4*sizeof(uint32_t));
encrypt_tea(v,k);
return ((uint64_t)v[0])<<32 | v[1];
}

@ -0,0 +1,8 @@
#ifndef _TEA_H
#define _TEA_H
#include <stdint.h>
void encrypt_tea (uint32_t* v, uint32_t* k);
void decrypt_tea (uint32_t* v, uint32_t* k);
uint64_t hash_block_tea(char b[24]);
#endif

@ -0,0 +1,34 @@
#include "xtea.h"
#include<string.h>
#define TOUR 32
void encrypt_xtea(uint32_t v[2], uint32_t const key[4]) {
unsigned int i;
uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
for (i=0; i < TOUR; i++) {
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
}
v[0]=v0; v[1]=v1;
}
void decrypt_xtea(uint32_t v[2], uint32_t const key[4]) {
unsigned int i;
uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*TOUR;
for (i=0; i < TOUR; i++) {
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
}
v[0]=v0; v[1]=v1;
}
uint64_t hash_block_xtea(char b[24])
{
uint32_t v[2];
uint32_t k[4];
memcpy(v,b,2*sizeof(uint32_t));
memcpy(k,b+2*sizeof(uint32_t),4*sizeof(uint32_t));
encrypt_xtea(v,k);
return ((uint64_t)v[0])<<32 | v[1];
}

@ -0,0 +1,8 @@
#ifndef _TEA_H
#define _TEA_H
#include <stdint.h>
void encrypt_xtea (uint32_t* v, uint32_t const k[]);
void decrypt_xtea (uint32_t* v, uint32_t const k[]);
uint64_t hash_block_xtea(char b[24]);
#endif

@ -0,0 +1,13 @@
all : clt srv
clt.o:clt.c
gcc -c clt.c
clt: clt.o
gcc -o clt clt.o
srv.o : srv.c
gcc -c srv.c
srv : srv.o
gcc -o srv srv.c

@ -0,0 +1,70 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <fcntl.h>
#include <sys/time.h>
#include <assert.h>
#define MAXBUFF 4096
int main(int argc, char * argv[]) {
int sock, z=0, fd;
char buff[MAXBUFF];
struct sockaddr_in server_addr;
int addrlen;
if (argc != 4) {
printf("Usage: %s <IP-address> <port number> <file to send>\n",argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock != -1);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
assert( inet_aton(argv[1], (struct in_addr*)&server_addr.sin_addr.s_addr) != 0 );
addrlen = sizeof(server_addr);
fd = open(argv[3],O_RDONLY,0666);
assert(fd != -1);
assert(connect(sock,(struct sockaddr *)&server_addr, addrlen) != -1);
while(1) {
ssize_t mbytes, nbytes;
nbytes=read(fd,buff,MAXBUFF);
if (nbytes==-1) {
perror("read");
exit(1);
}
if (nbytes==0) {
break;
}
mbytes = write(sock,buff,nbytes);
if (mbytes == -1) {
perror("write");
exit(1);
}
z=z+mbytes;
}
printf("file %s sent (%d bytes)",argv[3],z);
close(sock);
exit(0);
}

@ -0,0 +1,73 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <fcntl.h>
#include <sys/time.h>
#include <assert.h>
#define MAXBUFF 4096
int main(int argc, char * argv[]) {
int sock, z=0, fd;
char buff[MAXBUFF];
struct sockaddr_in server_addr;
int addrlen;
if (argc != 4) {
printf("Usage: %s <IP-address> <port number> <file to send>\n",argv[0]);
exit(1);
}
sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock != -1);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
assert( inet_aton(argv[1], (struct in_addr*)&server_addr.sin_addr.s_addr) != 0 );
addrlen = sizeof(server_addr);
//fd = open(argv[3],O_RDONLY,0666);
//assert(fd != -1);
assert(connect(sock,(struct sockaddr *)&server_addr, addrlen) != -1);
while(1) {
ssize_t mbytes, nbytes;
nbytes=read(0,buff,MAXBUFF);
if (nbytes==-1) {
perror("read");
exit(1);
}
if (nbytes==0) {
break;
}
mbytes = write(sock,buff,nbytes);
if (mbytes == -1) {
perror("write");
exit(1);
}
nbytes=read(sock,buff,MAXBUFF);
write(1,buff,nbytes);
z=z+mbytes;
}
printf("file %s sent (%d bytes)",argv[3],z);
close(sock);
exit(0);
}

113
td_tp/tp3/src/reseau/srv.c Normal file

@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <assert.h>
#define MAXBUFF 4096
int handle_transferred_file(int sock,struct sockaddr_in * client_addr)
{
char bufname[256];
char buff[MAXBUFF];
int fd;
ssize_t nbytes,z;
char addr[INET_ADDRSTRLEN];
snprintf(bufname,256,"%s.%d",inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
fd = open(bufname,O_WRONLY|O_CREAT|O_TRUNC,0666);
if (fd == -1) {
perror("open");
exit(1);
}
while (1) {
//bzero(buff,MAXBUFF);
nbytes = read(sock,buff,MAXBUFF);
if (nbytes == -1) {
perror("read");
exit(1);
}
if (nbytes==0) break;
write(fd,buff,nbytes);
z=z+nbytes;
}
inet_ntop(AF_INET,&(client_addr->sin_addr.s_addr),addr,INET_ADDRSTRLEN);
printf("%d bytes recieved from %s\n", z, addr);
shutdown(sock,SHUT_RDWR);
close(sock);
exit(0);
}
int main(int argc, char * argv[])
{
int /* file descriptor for the tranferred file */
listen_sock, /* socket for listenning connection */
service_sock, /* socket for a connected client */
nbytes, z=0;
int yes=1;
struct sockaddr_in server_addr,
client_addr;
int addr_len;
struct sigaction sa;
if (argc != 2) {
printf("Usage: %s <port number>\n",argv[0]);
exit(1);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDWAIT;
sa.sa_handler = SIG_DFL;
assert (sigaction(SIGCHLD, &sa, NULL) != -1);
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
assert( listen_sock > 0);
assert (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != -1);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[1]));
server_addr.sin_addr.s_addr = INADDR_ANY;
addr_len = sizeof(server_addr);
assert(bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != -1);
assert(listen(listen_sock,10) == 0);
while(1){
service_sock = accept(listen_sock,
(struct sockaddr *)&client_addr ,
&addr_len);
assert(service_sock >= 0);
pid_t p = fork();
if (p==0){
close(listen_sock);
handle_transferred_file(service_sock,&client_addr);
}
}
}

@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <assert.h>
#define MAXBUFF 4096
int handle_transferred_file(int sock,struct sockaddr_in * client_addr)
{
char bufname[256];
char buff[MAXBUFF];
int fd;
ssize_t nbytes,z;
char addr[INET_ADDRSTRLEN];
snprintf(bufname,256,"%s.%d",inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
//fd = open(bufname,O_WRONLY|O_CREAT|O_TRUNC,0666);
//if (fd == -1) {
// perror("open");
// exit(1);
//}
while (1) {
//bzero(buff,MAXBUFF);
nbytes = read(sock,buff,MAXBUFF);
if (nbytes == -1) {
perror("read");
exit(1);
}
if (nbytes==0) break;
write(sock,buff,nbytes);
z=z+nbytes;
}
inet_ntop(AF_INET,&(client_addr->sin_addr.s_addr),addr,INET_ADDRSTRLEN);
printf("%d bytes recieved from %s\n", z, addr);
shutdown(sock,SHUT_RDWR);
close(sock);
exit(0);
}
int main(int argc, char * argv[])
{
int /* file descriptor for the tranferred file */
listen_sock, /* socket for listenning connection */
service_sock, /* socket for a connected client */
nbytes, z=0;
int yes=1;
struct sockaddr_in server_addr,
client_addr;
int addr_len;
struct sigaction sa;
if (argc != 2) {
printf("Usage: %s <port number>\n",argv[0]);
exit(1);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDWAIT;
sa.sa_handler = SIG_DFL;
assert (sigaction(SIGCHLD, &sa, NULL) != -1);
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
assert( listen_sock > 0);
assert (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != -1);
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[1]));
server_addr.sin_addr.s_addr = INADDR_ANY;
addr_len = sizeof(server_addr);
assert(bind(listen_sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != -1);
assert(listen(listen_sock,10) == 0);
while(1){
service_sock = accept(listen_sock,
(struct sockaddr *)&client_addr ,
&addr_len);
assert(service_sock >= 0);
pid_t p = fork();
if (p==0){
close(listen_sock);
handle_transferred_file(service_sock,&client_addr);
}
}
}

59
td_tp/tp3/src/test_df.c Normal file

@ -0,0 +1,59 @@
#include "df.h"
#include "./lib_tea/tea.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
uint64 p = 4611686018427402023,
a,b,
A,B,AB,BA,
g;
uint32_t k[4]; // for tea
//
g=generateur(p);
srand(getpid()^time(NULL));
for (int i = 0;i < 2; i++){
a = 2 + rand()%(p-3);
A=expm(g,a,p);
b = 2 + rand()%(p-3);
B = expm(g,b,p);
AB = expm(A,b,p);
BA = expm(B,a,p);
//printf("%lx %lx %lx %lx %lx %lx\n",a,b,A,B,AB,BA);
assert(AB == BA);
memcpy((void*)(k+2*i),(void*)&AB,sizeof(uint32_t)*2);
}
printf("key for tea = (%x,%x,%x,%x)\n",k[0],k[1],k[2],k[3]);
while(1){
uint32_t v[2]={0};
ssize_t nb_read = read(0,v,sizeof(v));
if (nb_read <= 0)
break;
encrypt_tea(v,k);
decrypt_tea(v,k);
write(1,v,sizeof(v));
}
return 0;
}