From: Ummon Date: Tue, 25 Nov 2014 12:15:49 +0000 (+0100) Subject: First commit. X-Git-Url: http://git.euphorik.ch/?p=crypto_lab2.git;a=commitdiff_plain;h=8ba0c13cac9a84ef690dd09b92b846e85b7b02dd First commit. --- 8ba0c13cac9a84ef690dd09b92b846e85b7b02dd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e5f24b --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +bin/ +obj/ + +# Tex +*.log +*.aux +*.bbl +*.blg +*.dvi +*.backup diff --git a/icr14_lab_02.pdf b/icr14_lab_02.pdf new file mode 100644 index 0000000..5095cb7 Binary files /dev/null and b/icr14_lab_02.pdf differ diff --git a/labo2-fsharp/labo2-fsharp.sln b/labo2-fsharp/labo2-fsharp.sln new file mode 100644 index 0000000..c1285dd --- /dev/null +++ b/labo2-fsharp/labo2-fsharp.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{4925A630-B079-445d-BCD4-3A9C94FE9307}") = "labo2-fsharp", "labo2-fsharp\labo2-fsharp.fsproj", "{CDB168EA-04F9-4A8B-A3B4-27D9A6390269}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CDB168EA-04F9-4A8B-A3B4-27D9A6390269}.Debug|x86.ActiveCfg = Debug|x86 + {CDB168EA-04F9-4A8B-A3B4-27D9A6390269}.Debug|x86.Build.0 = Debug|x86 + {CDB168EA-04F9-4A8B-A3B4-27D9A6390269}.Release|x86.ActiveCfg = Release|x86 + {CDB168EA-04F9-4A8B-A3B4-27D9A6390269}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = labo2-fsharp\labo2-fsharp.fsproj + EndGlobalSection +EndGlobal diff --git a/labo2-fsharp/labo2-fsharp.userprefs b/labo2-fsharp/labo2-fsharp.userprefs new file mode 100644 index 0000000..f0b4897 --- /dev/null +++ b/labo2-fsharp/labo2-fsharp.userprefs @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/labo2-fsharp/labo2-fsharp/AssemblyInfo.fs b/labo2-fsharp/labo2-fsharp/AssemblyInfo.fs new file mode 100644 index 0000000..43c5dfa --- /dev/null +++ b/labo2-fsharp/labo2-fsharp/AssemblyInfo.fs @@ -0,0 +1,22 @@ +module Labo2.AssemblyInfo +open System.Reflection +open System.Runtime.CompilerServices + + +[] +[] +[] +[] +[] +[] +[] + +// The assembly version has the format {Major}.{Minor}.{Build}.{Revision} + +[] + +//[] +//[] + +() + diff --git a/labo2-fsharp/labo2-fsharp/Crypto.fs b/labo2-fsharp/labo2-fsharp/Crypto.fs new file mode 100644 index 0000000..d8377d1 --- /dev/null +++ b/labo2-fsharp/labo2-fsharp/Crypto.fs @@ -0,0 +1,82 @@ +module Labo2.Crypto + +open System.Security.Cryptography +open System.IO + +type Key = string +type Data = byte[] + +let rsaKeySize = 2048 + +let generate256Key : Key = + null + +/// Generate a new RSA key pair: (public * private). +let generateRSAKeysPair : Key * Key = + use rsa = new RSACryptoServiceProvider (rsaKeySize) + try + rsa.ToXmlString false, rsa.ToXmlString true + finally + rsa.PersistKeyInCsp <- false + +let encryptRSA (publicKey : Key) (plaindata : Data) : Data = + use rsa = new RSACryptoServiceProvider (rsaKeySize) + try + rsa.FromXmlString publicKey + rsa.Encrypt (plaindata, false) // Uses PKCS#1 v1.5 padding. + finally + rsa.PersistKeyInCsp <- false + +let decryptRSA (privateKey : Key) (cipherdata : Data) : Data = + use rsa = new RSACryptoServiceProvider (rsaKeySize) + try + rsa.FromXmlString privateKey + rsa.Decrypt (cipherdata, false) // Uses PKCS#1 v1.5 padding. + finally + rsa.PersistKeyInCsp <- false + +/// Produces a signature from a given hash. +let signRSA (privKey : Key) (sha256 : Data) : Data = + use rsa = new RSACryptoServiceProvider (rsaKeySize) + try + rsa.FromXmlString privKey + rsa.SignHash (sha256, CryptoConfig.MapNameToOID "SHA256") + finally + rsa.PersistKeyInCsp <- false + +/// Verify a signature against a given hash. +let verifySignRSA (pubKey : Key) (sha256 : Data) (signature : Data) : bool = + use rsa = new RSACryptoServiceProvider (rsaKeySize) + try + rsa.FromXmlString pubKey + rsa.VerifyHash (sha256, CryptoConfig.MapNameToOID "SHA256", signature) + finally + rsa.PersistKeyInCsp <- false + +let decryptAES (key : Key) (inputStream : Stream) (outputStream : Stream) = + () + +open System.Text + +let testRSA = lazy ( + let kpub, kpriv = generateRSAKeysPair + let plaintext = "Hello, World!" + printfn "plaintext: %A" plaintext + let cipherdata = encryptRSA kpub (Encoding.UTF8.GetBytes plaintext) + printfn "cipherdata: (size: %A) %A" cipherdata.Length cipherdata + let decryptedData = decryptRSA kpriv cipherdata + let decryptedText = Encoding.UTF8.GetString decryptedData + printfn "decryptedtext: %A" decryptedText + assert (plaintext = decryptedText) + printfn "testRSA OK" + ) + +let testRSASignature = lazy ( + let kpub, kpriv = generateRSAKeysPair + let plaintext = "Hello, World!" + let sha256 = new SHA256Managed () + let signature = signRSA kpriv (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext)) + assert verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext)) signature + assert not (verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes "Hello!")) signature) + printfn "testRSASignature OK" + ) \ No newline at end of file diff --git a/labo2-fsharp/labo2-fsharp/Program.fs b/labo2-fsharp/labo2-fsharp/Program.fs new file mode 100644 index 0000000..a3d2e56 --- /dev/null +++ b/labo2-fsharp/labo2-fsharp/Program.fs @@ -0,0 +1,17 @@ +(* +Crypto - Labo n°2. +*) + +module Labo2.Main + +open System +open Crypto + +[] +let main args = + printfn "Labo n°2" + + if Array.exists ((=) "tests") args then + testRSA.Force () + testRSASignature.Force () + 0 diff --git a/labo2-fsharp/labo2-fsharp/labo2-fsharp.fsproj b/labo2-fsharp/labo2-fsharp/labo2-fsharp.fsproj new file mode 100644 index 0000000..4ee07a3 --- /dev/null +++ b/labo2-fsharp/labo2-fsharp/labo2-fsharp.fsproj @@ -0,0 +1,55 @@ + + + + Debug + x86 + {CDB168EA-04F9-4A8B-A3B4-27D9A6390269} + Exe + labo2fsharp + labo2-fsharp + v4.5 + + + true + full + bin\Debug + DEBUG + prompt + x86 + true + false + false + + + + + + tests + + + true + pdbonly + true + bin\Release + prompt + x86 + true + true + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..c577848 --- /dev/null +++ b/notes.txt @@ -0,0 +1,85 @@ +Labo 2 +====== + +* Intégrité +* Authenticité +* Confidentialité + +chiffrer -> signer -> stocker + +* Signer les données de manière asymétrique (pair de clefs) +* Clef symétrique unique par fichier? +* Pair de clefs asymétrique pour chiffrer les clefs symétriques +* Vérifier l'intégrité +* Un seul fichier à protéger +* Définir un format d'un 'container' avec header et tout le bordel +* (pour la dernière partie), Comment stocker les clefs? + +Attention aux méta-données des fichiers!: + * Nom + * Taille + * Date de modification/création + + +Inputs +------ + +Chiffrement : + * Le fichier + * La clef publique + +Déchiffrer : + * Le container + * La clef privée + +Algorithmes et paramètres : + RSA-2048 pour la signature + RSA-2048 pour le chiffrage des clefs + HMAC-SHA256 pour l'intégrité + AES-CBC-256 pour le chiffrement symétrique + + +Processus de chiffrement +------------------------ + +inputs: + d : contenu du fichier + metas : métas données associées au fichier + kpub : clef publique RSA + ksignpriv : clef privé de signature DSA + +* Génération d'une clef 256 bits pour AES -> kc +* Génération d'une clef 256 bits pour MAC -> ka +* Construction du plaintext, voir format ci dessous +* Chiffrement du plaintext avec AES-CBC256 et kc -> ciphertext +* Calcul de MAC de ciphertext -> mac +* Signature de mac -> sig +* Chiffrement de kc + ka avec kpub (RSA) -> keys +* Renvoie mac + sig + keys + ciphertext + + +Processus de déchiffrement +-------------------------- + +inputs. + d : contenu chiffrées + kpriv : clef privée + ksignpub : la clef publique de signature + +* Déchiffrement de kc + ka avec kpriv (ECDSA) +* Vérification de l'intégrité avec MAC +* Vérification de la signature avec ksignpub +* Déchiffrement du ciphertext + + +Format du container +=================== + +container = mac[256], signature[2048], keys[2048], ciphertext +ciphertext = AES(plaintext) +plaintext = header, file-content ; +header = file-size[64], meta-data-size[16], meta-data ; +meta-data = { key-value-pair } ; +key-value-pair = key[string], value[string] +string = size[8], content-utf8 ; + diff --git a/rapport/main.pdf b/rapport/main.pdf new file mode 100644 index 0000000..e787e0b Binary files /dev/null and b/rapport/main.pdf differ diff --git a/rapport/main.tex b/rapport/main.tex new file mode 100644 index 0000000..97745a2 --- /dev/null +++ b/rapport/main.tex @@ -0,0 +1,97 @@ +\documentclass[a4paper,10pt]{article} + +\usepackage[francais]{babel} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{lmodern} + +\usepackage{graphicx} +\usepackage{listings} +\usepackage{url} +\usepackage{upquote} +\usepackage{color} + +\title{ICR - Labo \#2 : \textit{Conception et implémentation d'un container sécurisé pour des données médicales}} +\author{G.Burri} + +\lstdefinelanguage{FSharp}% +{morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, % +and, for, while, true, false, in, do, begin, end, fun, function, return, yield, try, % +mutable, if, then, else, cloud, async, static, use, abstract, interface, inherit, finally }, + otherkeywords={ let!, return!, do!, yield!, use!, var, from, select, where, order, by }, + keywordstyle=\color{blue}\bfseries, + sensitive=true, + basicstyle=\ttfamily, + breaklines=true, + xleftmargin=\parindent, + aboveskip=\bigskipamount, + tabsize=4, + morecomment=[l][\color{greencomments}]{///}, + morecomment=[l][\color{greencomments}]{//}, + morecomment=[s][\color{greencomments}]{{(*}{*)}}, + morestring=[b]", + showstringspaces=false, + literate={`}{\`}1, + stringstyle=\color{redstrings}, +} + +\begin{document} + +\nocite{*} + +\maketitle + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\section{Introduction} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\section{processus} + +\subsection{chiffrement} + +\subsection{déchiffrement} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\section{format du container} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\section{Niveaux de sécurité} + +\subsection{Quel est le niveau de sécurité que l'on souhaite atteindre ?} + +\begin{itemize} + \item Confidentialité : les données chiffrées ne doivent pas pouvoir être décryptées par un attaquant. + \item Authentification : un attaquant ne doit pas pouvoir forger un container, une signature est réalisée à l'aide d'une paire de clef publique-privée. + \item Intégrité : il ne faut pas que les données chiffrée aient pu être altérées par un attaquant. +\end{itemize} + + +\subsection{Comment s'assure-t-on que les données sont stockées de manière confidentielle ? En particulier ce qui concerne les méta-données} + +Les méta-données ainsi que les données sont chiffrées ensemble. Voir le format du container décrit précédemment. + + +\subsection{Comment s'assure-t-on que les données stockées sont authentiques ? Quels sont les risques à prendre en compte ?} + +L'empreinte des données est signée à l'aide d'une clef privée donnée en paramètre de l'\emph{API}, ceci représente la signature qui est placée dans le container. Lors du déchiffrement, la clef publique correspondante est donnée puis utilisée pour déchiffrer l'empreinte qui est comparée à l'empreinte des données. + + +\subsection{Comment s'assure-t-on que les données stockées sont intègres ?} + +Cela est réalisé avec un \emph{MAC}, dans notre nous utilisons \emph{HMAC-SHA256} sur les données chiffrées (\emph{Encrypt-then-MAC}). + + +\subsection{} + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\section{Conclusion} + +% http://stephenhaunts.com/2013/03/04/cryptography-in-net-advanced-encryption-standard-aes/ +% http://stephenhaunts.com/2013/03/26/cryptography-in-net-rsa/ +% http://en.wikipedia.org/wiki/Digital_signature +%\bibliographystyle{plain} +%\bibliography{main} + +\end{document}