uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit 2044092c4a68f3cdad2a7cf33ae8eec09e014302
parent 9513d81c339b37a53ec6ba33b741438ba481e23e
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun, 17 Apr 2022 00:04:40 +0300

stuff

Diffstat:
Dc_security/ex1/Makefile | 13-------------
Ac_security/ex1/doc/doc.pdf | 0
Ac_security/ex1/doc/doc.tex | 252+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_security/ex1/doc/doc.toc | 16++++++++++++++++
Ac_security/ex1/doc/res/uniwalogo.png | 0
Dc_security/ex1/priv.c | 32--------------------------------
Ac_security/ex1/src/Makefile | 21+++++++++++++++++++++
Ac_security/ex1/src/atoh | 3+++
Ac_security/ex1/src/dat/decrypt.in | 4++++
Ac_security/ex1/src/dat/encrypt.in | 3+++
Ac_security/ex1/src/dat/priv.in | 3+++
Ac_security/ex1/src/dat/verify1_cor.in | 3+++
Ac_security/ex1/src/dat/verify1_inc.in | 3+++
Ac_security/ex1/src/dat/verify2.in | 3+++
Ac_security/ex1/src/decrypt.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_security/ex1/src/encrypt.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_security/ex1/src/htoa | 6++++++
Ac_security/ex1/src/priv.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_security/ex1/src/sign.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_security/ex1/src/tests | 28++++++++++++++++++++++++++++
Ac_security/ex1/src/verify.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rlex_bison_compilers/fsm -> lex_bison_compilers/part1/fsm | 0
Rlex_bison_compilers/fsm.c -> lex_bison_compilers/part1/fsm.c | 0
23 files changed, 749 insertions(+), 45 deletions(-)

diff --git a/c_security/ex1/Makefile b/c_security/ex1/Makefile @@ -1,13 +0,0 @@ -TARGS = priv - -CC = cc -CFLAGS = -std=c99 -pedantic -Wall -Os -Iinclude -LIBS = -Llib -lcrypto - -all: - for targ in ${TARGS} ; do \ - ${CC} $${targ}.c ${LIBS} -o $${targ} ; \ - done - -clean: - rm -f ${TARGS} *.o *.core diff --git a/c_security/ex1/doc/doc.pdf b/c_security/ex1/doc/doc.pdf Binary files differ. diff --git a/c_security/ex1/doc/doc.tex b/c_security/ex1/doc/doc.tex @@ -0,0 +1,252 @@ +\documentclass[12pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage{float} +\usepackage{siunitx} +\usepackage[margin=0.5in]{geometry} +\usepackage[backend=bibtex]{biblatex} + +\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true} + +\title{Εργαστήριο Ασφάλειας στην Τεχνολογία της Πληροφορίας -- Εργασία 1} +\author{Χρήστος Μαργιώλης -- 19390133} +\date{Απρίλιος 2022} + +\begin{document} + +\begin{titlepage} + \maketitle + \begin{figure}[t!] + \begin{center} + \includegraphics[scale=0.3]{./res/uniwalogo.png} \\ + \Large + \textbf{Πανεπιστήμιο Δυτικής Αττικής} \\ + \large + Τμήμα Μηχανικών Πληροφορικής και Ηλεκτρονικών Υπολογιστών + \end{center} + \end{figure} +\end{titlepage} + +\renewcommand{\contentsname}{Περιεχόμενα} +\tableofcontents +\pagebreak + +\section{Δομή αρχείων} + +\subsection{Αρχεία C} + +Ο κώδικας C, για δική μου διευκόλυνση στην δοκιμή διαφόρων εισόδων, περιέχει και +ρουτίνες διαβάσματος αρχείων, πέρα από την επίλυση των προβλημάτων. Τα περισσότερα +προγράμματα χρησιμοποιούνται σε συνδυασμό με κάποιο από τα scripts που αναλύονται +παρακάτω. Αυτό προσθέτει μεν πολυπλοκότητα, αλλά κάνει τα προγράμματα πιο ευέλικτα. +Σε κάθε δραστηριότητα εξηγώ πως πρέπει να τρέξουμε το πρόγραμμα. + +\subsection{Scripts} + +Υπάρχουν τα εξής 3 βοηθητικά scripts: +\begin{itemize} + \item \lstinline{atoh}: Μετατρέπει ένα string από ASCII σε Hex. + \item \lstinline{htoa}: Μετατρέπει ένα string από Hex σε ASCII. + \item \lstinline{tests}: Αναδεικνύει/αυτοματοποιεί την λειτουργία + όλων των προγραμμάτων. +\end{itemize} + +\subsection{Αρχεία δεδομένων} + +Στο directory \lstinline{src/dat} βρίσκονται αρχεία δεδομένων που +χρησιμοποιούνται από τα προγράμματα. 'Ολα τα αρχεία ακολουθούν την ονομασία +\lstinline{<program>.in} όπου \lstinline{program} το όνομα του προγράμματος που +το χρησιμοποιεί. Προκειμένου να αποφευχθεί τυχόν περιττή πολυπλοκότητα, τα +προγράμματα δεν κάνουν ελέγχους εγκυρότητας των αρχείων εισόδου. + +\subsection{Makefile} + +Το \lstinline{Makefile} διαθέτει τις παρακάτω επιλογές: +\begin{itemize} + \item \lstinline{make}: Κάνει compile όλα τα προγράμματα. + \item \lstinline{make clean}: Σβήνει τα εκτελέσιμα αρχεία που έχουν + παραχθεί. +\end{itemize} + +\section{Δραστηριότητα 1: Δημιουργία ιδιωτικού κλειδιού} + +Κώδικας: \lstinline{priv.c} \\ +Χρήση: \lstinline{priv [-v] input} \\ + +Για να δημιουργήσουμε ένα ιδιωτικό κλειδί RSA, πρέπει αρχικά να υπολογίσουμε +την συνάρτηση: +\[\phi(n) = (p-1)(q-1)\] +Στην συνέχεια, θα υπολογίσουμε την εξίσωση: +\[e \cdot d \mod \phi(n) = 1\] +Λύνοντας ως προς $d$. Ο υπολογισμός του ιδιωτικού κλειδιού μέσα στο πρόγραμμα +γίνεται με τις εξής εντολές: \\ + +\begin{lstlisting}[language=C] + ... + BN_dec2bn(&one, "1"); + BN_sub(foo, p, one); + BN_sub(bar, q, one); + BN_mul(phi, foo, bar, ctx); + BN_mod_inverse(d, e, phi, ctx); +\end{lstlisting} + +Το πρόγραμμα, όταν το τρέξουμε απλώς με το αρχείο εισόδου, τυπώνει το +ιδιωτικό κλειδί. Αν του δώσουμε και την επιλογή \lstinline{-v}, τυπώνει +αναλυτικά τα $e$, $n$ και $d$. Η επιλογή αυτή είναι χρήσιμη για την +παραγωγή του αρχείου εισόδου που χρησιμοποιείται για την κρυπτογράφηση +μηνυμάτων. + +Ενδεικτικά τρεξίματα: \\ + +\begin{lstlisting} + $ ./priv dat/priv.in + 63F67E805D8DEB0B4182C57C3DC24F3C1350CF182E8ABF85FD24062A3BC7F2EB + + $ ./priv -v dat/priv.in + e: 0D88C3 + n: 71D9BBC5C01F9B50DDFE5F2EC331FAB21081009D014E9615C277670C61591ECF + d: 63F67E805D8DEB0B4182C57C3DC24F3C1350CF182E8ABF85FD24062A3BC7F2EB +\end{lstlisting} + +\section{Δραστηριότητα 2: Κρυπτογράφηση μηνύματος} + +Κώδικας: \lstinline{encrypt.c} \\ +Χρήση: \lstinline{./atoh 'msg' | encrypt input} \\ + +Η κρυπτογράφηση ενός μηνύματος γίνεται με τον τύπο: +\[C = P^e \mod n\] +Και η αποκρυπτογράφηση του: +\[P = C^d \mod n\] + +Η συνάρτηση OpenSSL για την πράξη αυτή είναι η \lstinline{BN_mod_exp()}. Οι +παρακάτω εντολές στον κώδικα εκτελούν την (απο)κρυπτογράφηση: \\ + +\begin{lstlisting}[language=C] + ... + /* Encrypt message */ + BN_mod_exp(encrstr, str, e, n, ctx); + /* Decrypt message */ + BN_mod_exp(decrstr, encrstr, d, n, ctx); +\end{lstlisting} + +Παρακάτω φαίνεται ένα ενδεικτικό τρέξιμο. Το μήνυμα μετατρέπεται σε Hex με την +χρήση του \lstinline{atoh} script. Αξίζει να σημειωθεί ότι στην υλοποίησή μου, +τα $e$, $n$ και $d$ υπολογίζονται από το \lstinline{priv.c} και +χρησιμοποιούνται κατευθείαν από το \lstinline{encrypt.c} ώστε να αποφευχθεί η +επανάληψη κώδικα, εξ'ου και η χρήση του \lstinline{priv} στην αρχή: \\ + +\begin{lstlisting} + $ ./priv -v dat/priv.in | awk '{print $2}' > dat/encrypt.in + $ ./atoh 'Christos Margiolis' | ./encrypt dat/encrypt.in + received: 69726843736F747372614D206C6F69677369 + encrypted: 192FDF7BCA2F402253E344F1A25476D4276523750DED4BE6FC058F179219BA6D + decrypted: 69726843736F747372614D206C6F69677369 +\end{lstlisting} + +\section{Δραστηριότητα 3: Αποκρυπτογράφηση μηνύματος} + +Κώδικας: \lstinline{decrypt.c} \\ +Χρήση: \lstinline{decrypt input | htoa} \\ + +'Οπως και στην δραστηριότητα 3, χρησιμοποιείται ο ίδιος τύπος για την +αποκρυπτογράφηση ενός μηνύματος. Η υλοποίηση είναι η ίδια, με την διαφορά ότι +τα δεδομένα εισόδου είναι διαφορετικά. Το πρόγραμμα τυπώνει το +αποκρυπτογραφημένο μήνυμα σε Hex, οπότε διοχετεύουμε την έξοδό του στο +\lstinline{htoa} script ώστε να μετατραπεί σε ASCII: \\ + +\begin{lstlisting} + $ ./decrypt dat/decrypt.in | ./htoa + INFOSEC Spring 2022 +\end{lstlisting} + +\section{Δραστηριότητα 4: Υπογραφή μηνύματος} + +Κώδικας: \lstinline{sign.c} \\ +Χρήση: \lstinline{atoh 'msg' | sign input} \\ + +Η υπογραφή ενός μηνύματος γίνεται με τον τύπο: +\[S = H(P)^d \mod n\] + +Στον κώδικα, υλοποίηση είναι γίνεται ως εξής: \\ +\begin{lstlisting}[language=C] + ... + BN_mod_exp(sign, str, d, n, ctx); +\end{lstlisting} + +Στο παρακάτω ενδεικτικό τρέξιμο, παρατηρούμε ότι μία πολύ μικρή αλλαγή στο +μήνυμα θα παράξει τελείως διαφορετική υπογραφή, οπότε είμαστε και σίγουροι ότι +τα μηνύματα δεν ήτανε ίδια: \\ + +\begin{lstlisting} + $ ./atoh 'This is a message' | ./sign dat/decrypt.in + 96798DC95A5ECBBEF35D6D68588157CA2DAD163B45453B4B6D80FCC3BE15E8ED + + $ ./atoh 'This iz a message' | ./sign dat/decrypt.in + D7562984FFA684E2850A2763F4DD1DA045EFD2DB4CFBF6F40E6579F3E3AD9536 +\end{lstlisting} + +\section{Δραστηριότητα 5: Επαλήθευση υπογραφής} + +Η επαλήθευση της υπογραφής γίνεται με τον τύπο: +\[Digest = S^e \mod n\] + +Στον κώδικα, υλοποίηση είναι γίνεται ως εξής: \\ +\begin{lstlisting}[language=C] + ... + BN_mod_exp(str, sign, e, n, ctx); +\end{lstlisting} + +\subsection{Περίπτωση Α} + +Αρχικά θα δώσουμε ως είσοδο την έγκυρη υπογραφή: + +\begin{lstlisting} + $ ./verify dat/verify1_cor.in + e: 010001 + n: AE1CD4DC432798D933779FBD46C6E1247F0CF1233595113AA51B450F18116115 + sign: 643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6802F + str: 4C61756E63682061206D697373696C652E + + $ ./verify dat/verify1_cor.in | tail -1 | awk '{print $2}' | ./htoa + Launch a missile. +\end{lstlisting} + +'Οταν αλλάξουμε το τελευταίο byte της υπογραφής, παρατηρούμε ότι η επαλήθευση +δεν είναι έγκυρη: + +\begin{lstlisting} + $ ./verify dat/verify1_inc.in + e: 010001 + n: AE1CD4DC432798D933779FBD46C6E1247F0CF1233595113AA51B450F18116115 + sign: 643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6803F + str: 91471927C80DF1E42C154FB4638CE8BC726D3D66C83A4EB6B7BE0203B41AC294 + + $ ./verify dat/verify1_cor.in | tail -1 | awk '{print $2}' | ./htoa + ??,O?c??rm=f?:N????? +\end{lstlisting} + +\subsection{Περίπτωση Β} + +Από το παρακάτω τρέξιμο, βλέπουμε ότι η υπόγραφη είναι πράγματι της Alice: + +\begin{lstlisting} + $ ./verify dat/verify2.in + e: 010001 + n: DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5 + sign: DB3F7CDB93483FC1E70E4EACA650E3C6505A3E5F49EA6EDF3E95E9A7C6C7A320 + str: 506C65617365207472616E73666572206D652024323030302E416C6963652E + + $ ./verify dat/verify2.in | tail -1 | awk '{print $2}' | ./htoa + Please transfer me $2000.Alice. +\end{lstlisting} + +\section{Δραστηριότητα 6: Μη-αυτόματη επαλήθευση πιστοποιητικού X.509} + +<++> + +\end{document} diff --git a/c_security/ex1/doc/doc.toc b/c_security/ex1/doc/doc.toc @@ -0,0 +1,16 @@ +\boolfalse {citerequest}\boolfalse {citetracker}\boolfalse {pagetracker}\boolfalse {backtracker}\relax +\defcounter {refsection}{0}\relax +\select@language {english} +\contentsline {section}{\numberline {1}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\textomicron }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\acctonos \texteta } \IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textchi }{\mathchi }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\textnu }{\mathnu }}}{2} +\contentsline {subsection}{\numberline {1.1}\IeC {\textAlpha }\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textchi }{\mathchi }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textalpha }{\mathalpha }} C}{2} +\contentsline {subsection}{\numberline {1.2}Scripts}{2} +\contentsline {subsection}{\numberline {1.3}\IeC {\textAlpha }\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textchi }{\mathchi }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textalpha }{\mathalpha }} \IeC {\TextOrMath {\textdelta }{\mathdelta }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textdelta }{\mathdelta }}\IeC {\textomicron }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\acctonos \textepsilon }\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\textnu }{\mathnu }}}{2} +\contentsline {subsection}{\numberline {1.4}Makefile}{2} +\contentsline {section}{\numberline {2}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 1: \IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\textomicron }\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textalpha }{\mathalpha }} \IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textdelta }{\mathdelta }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\textomicron }\IeC {\acctonos \textupsilon } \IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textdelta }{\mathdelta }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\textomicron }\IeC {\acctonos \textupsilon }}{2} +\contentsline {section}{\numberline {3}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 2: \IeC {\textKappa }\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\acctonos \textalpha }\IeC {\TextOrMath {\textphi }{\mathphi }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\acctonos \textupsilon }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{3} +\contentsline {section}{\numberline {4}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 3: \IeC {\textAlpha }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\textomicron }\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\acctonos \textalpha }\IeC {\TextOrMath {\textphi }{\mathphi }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\acctonos \textupsilon }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{3} +\contentsline {section}{\numberline {5}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 4: \IeC {\TextOrMath {\textUpsilon }{\mathUpsilon }}\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\textomicron }\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textphi }{\mathphi }}\IeC {\acctonos \texteta } \IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textnu }{\mathnu }}\IeC {\acctonos \textupsilon }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{4} +\contentsline {section}{\numberline {6}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 5: \IeC {\textEpsilon }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\acctonos \texteta }\IeC {\TextOrMath {\texttheta }{\maththeta }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\textomicron }\IeC {\TextOrMath {\textgamma }{\mathgamma }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textphi }{\mathphi }}\IeC {\acctonos \texteta }\IeC {\TextOrMath {\textvarsigma }{\mathvarsigma }}}{4} +\contentsline {subsection}{\numberline {6.1}\IeC {\TextOrMath {\textPi }{\mathPi }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\textAlpha }}{4} +\contentsline {subsection}{\numberline {6.2}\IeC {\TextOrMath {\textPi }{\mathPi }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\acctonos \textiota }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textomega }{\mathomega }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\textBeta }}{5} +\contentsline {section}{\numberline {7}\IeC {\TextOrMath {\textDelta }{\mathDelta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\textrho }{\mathrho }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textalpha }{\mathalpha }} 6: \IeC {\textMu }\IeC {\TextOrMath {\texteta }{\matheta }}-\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\acctonos \textomicron }\IeC {\TextOrMath {\textmu }{\mathmu }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\textalpha }{\mathalpha }}\IeC {\TextOrMath {\textlambda }{\mathlambda }}\IeC {\acctonos \texteta }\IeC {\TextOrMath {\texttheta }{\maththeta }}\IeC {\TextOrMath {\textepsilon }{\mathepsilon }}\IeC {\TextOrMath {\textupsilon }{\mathupsilon }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texteta }{\matheta }} \IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textsigma }{\mathsigma }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\textomicron }\IeC {\TextOrMath {\textpi }{\mathpi }}\IeC {\textomicron }\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\texteta }{\matheta }}\IeC {\TextOrMath {\texttau }{\mathtau }}\IeC {\TextOrMath {\textiota }{\mathiota }}\IeC {\TextOrMath {\textkappa }{\mathkappa }}\IeC {\textomicron }\IeC {\acctonos \textupsilon } X.509}{5} diff --git a/c_security/ex1/doc/res/uniwalogo.png b/c_security/ex1/doc/res/uniwalogo.png Binary files differ. diff --git a/c_security/ex1/priv.c b/c_security/ex1/priv.c @@ -1,32 +0,0 @@ -#include <stdio.h> -#include <openssl/bn.h> - -int -main(int argc, char *argv[]) -{ - BN_CTX *ctx; - BIGNUM *p, *q, *e, *n, *d; - - ctx = BN_CTX_new(); - p = BN_new(); - q = BN_new(); - e = BN_new(); - n = BN_new(); - d = BN_new(); - - BN_hex2bn(&p, "953AAB9B3F23ED593FBDC690CA10E703"); - BN_hex2bn(&q, "C34EFC7C4C2369164E953553CDF94945"); - BN_hex2bn(&e, "0D88C3"); - BN_mul(n, p, q, ctx); - - /* Calculate private key */ - - OPENSSL_free(p); - OPENSSL_free(q); - OPENSSL_free(e); - OPENSSL_free(n); - OPENSSL_free(q); - OPENSSL_free(ctx); - - return (0); -} diff --git a/c_security/ex1/src/Makefile b/c_security/ex1/src/Makefile @@ -0,0 +1,21 @@ +TARGS = priv \ + encrypt \ + decrypt \ + sign \ + verify + +SCRIPTS = atoh \ + htoa + +CC = cc +CFLAGS = -std=c99 -pedantic -Wall -Os -Iinclude +LIBS = -Llib -lcrypto + +all: + for targ in ${TARGS} ; do \ + ${CC} $${targ}.c ${LIBS} -o $${targ} ; \ + done + chmod +x ${SCRIPTS} + +clean: + rm -f ${TARGS} *.o *.core diff --git a/c_security/ex1/src/atoh b/c_security/ex1/src/atoh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo -n ${1} | hexdump -e '"%02X"' | sed 's/$/\n/' diff --git a/c_security/ex1/src/dat/decrypt.in b/c_security/ex1/src/dat/decrypt.in @@ -0,0 +1,4 @@ +010001 +DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5 +74D806F9F3A62BAE331FFE3F0A68AFE35B3D2E4794148AACBC26AA381CD7D30D +CAF7D72776AFEFBAC8269E1A8B76CE44A3B28015CA9A54E22C239EF38FCFAFFA diff --git a/c_security/ex1/src/dat/encrypt.in b/c_security/ex1/src/dat/encrypt.in @@ -0,0 +1,3 @@ +0D88C3 +71D9BBC5C01F9B50DDFE5F2EC331FAB21081009D014E9615C277670C61591ECF +63F67E805D8DEB0B4182C57C3DC24F3C1350CF182E8ABF85FD24062A3BC7F2EB diff --git a/c_security/ex1/src/dat/priv.in b/c_security/ex1/src/dat/priv.in @@ -0,0 +1,3 @@ +953AAB9B3F23ED593FBDC690CA10E703 +C34EFC7C4C2369164E953553CDF94945 +0D88C3 diff --git a/c_security/ex1/src/dat/verify1_cor.in b/c_security/ex1/src/dat/verify1_cor.in @@ -0,0 +1,3 @@ +010001 +AE1CD4DC432798D933779FBD46C6E1247F0CF1233595113AA51B450F18116115 +643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6802F diff --git a/c_security/ex1/src/dat/verify1_inc.in b/c_security/ex1/src/dat/verify1_inc.in @@ -0,0 +1,3 @@ +010001 +AE1CD4DC432798D933779FBD46C6E1247F0CF1233595113AA51B450F18116115 +643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6803F diff --git a/c_security/ex1/src/dat/verify2.in b/c_security/ex1/src/dat/verify2.in @@ -0,0 +1,3 @@ +010001 +DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5 +DB3F7CDB93483FC1E70E4EACA650E3C6505A3E5F49EA6EDF3E95E9A7C6C7A320 diff --git a/c_security/ex1/src/decrypt.c b/c_security/ex1/src/decrypt.c @@ -0,0 +1,65 @@ +#include <err.h> +#include <stdio.h> +#include <string.h> + +#include <openssl/bn.h> + +static const char * +read_line(FILE *fp) +{ + char buf[BUFSIZ]; + + if (fgets(buf, sizeof(buf), fp) == NULL) + err(1, "fgets"); + return (strdup(buf)); +} + +static void +printbn(char *str, BIGNUM *bn) +{ + char *s; + + s = BN_bn2hex(bn); + printf("%s%s\n", str, s); + OPENSSL_free(s); +} + +int +main(int argc, char *argv[]) +{ + BN_CTX *ctx; + BIGNUM *e, *n, *d, *c, *decrstr; + FILE *fp; + + if (argc < 2) { + fprintf(stderr, "usage: %s input\n", *argv); + return (-1); + } + if ((fp = fopen(argv[1], "r")) == NULL) + err(1, "fopen(%s)", argv[1]); + + ctx = BN_CTX_new(); + e = BN_new(); + n = BN_new(); + d = BN_new(); + c = BN_new(); + decrstr = BN_new(); + + BN_hex2bn(&e, read_line(fp)); + BN_hex2bn(&n, read_line(fp)); + BN_hex2bn(&d, read_line(fp)); + BN_hex2bn(&c, read_line(fp)); + + BN_mod_exp(decrstr, c, d, n, ctx); + printbn("", decrstr); + + fclose(fp); + OPENSSL_free(e); + OPENSSL_free(n); + OPENSSL_free(d); + OPENSSL_free(c); + OPENSSL_free(decrstr); + OPENSSL_free(ctx); + + return (0); +} diff --git a/c_security/ex1/src/encrypt.c b/c_security/ex1/src/encrypt.c @@ -0,0 +1,83 @@ +#include <err.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +#include <openssl/bn.h> + +static const char * +read_line(FILE *fp) +{ + char buf[BUFSIZ]; + + if (fgets(buf, sizeof(buf), fp) == NULL) + err(1, "fgets"); + return (strdup(buf)); +} + +static void +printbn(char *str, BIGNUM *bn) +{ + char *s; + + s = BN_bn2hex(bn); + printf("%s%s\n", str, s); + OPENSSL_free(s); +} + +int +main(int argc, char *argv[]) +{ + BN_CTX *ctx; + BIGNUM *str, *encrstr, *decrstr; + BIGNUM *e, *n, *d; + FILE *fp; + int len = 0; + char buf[BUFSIZ]; + + if (argc < 2) { + fprintf(stderr, "usage: %s input\n", *argv); + return (-1); + } + if ((fp = fopen(argv[1], "r")) == NULL) + err(1, "fopen(%s)", argv[1]); + + /* Read string from stdin */ + while (read(STDIN_FILENO, &buf[len++], 1) > 0) + ; + buf[--len] = '\0'; + + ctx = BN_CTX_new(); + str = BN_new(); + encrstr = BN_new(); + decrstr = BN_new(); + e = BN_new(); + n = BN_new(); + d = BN_new(); + + BN_hex2bn(&str, buf); + /* + * Assumes input from file produced by `priv -v`, so that we + * avoid duplicating code to recalculate p, q, e, n, d. + */ + BN_hex2bn(&e, read_line(fp)); + BN_hex2bn(&n, read_line(fp)); + BN_hex2bn(&d, read_line(fp)); + + /* Encrypt message */ + BN_mod_exp(encrstr, str, e, n, ctx); + /* Decrypt message */ + BN_mod_exp(decrstr, encrstr, d, n, ctx); + + printbn("received: ", str); + printbn("encrypted: ", encrstr); + printbn("decrypted: ", decrstr); + + fclose(fp); + OPENSSL_free(e); + OPENSSL_free(n); + OPENSSL_free(d); + OPENSSL_free(ctx); + + return (0); +} diff --git a/c_security/ex1/src/htoa b/c_security/ex1/src/htoa @@ -0,0 +1,6 @@ +#!/bin/sh + +while read hex; do + echo "16i ${hex} P" | dc +done +echo diff --git a/c_security/ex1/src/priv.c b/c_security/ex1/src/priv.c @@ -0,0 +1,116 @@ +#include <err.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <openssl/bn.h> + +static char *argv0; + +static const char * +read_line(FILE *fp) +{ + char buf[BUFSIZ]; + + if (fgets(buf, sizeof(buf), fp) == NULL) + err(1, "fgets"); + return (strdup(buf)); +} + +static void +printbn(char *str, BIGNUM *bn) +{ + char *s; + + s = BN_bn2hex(bn); + printf("%s%s\n", str, s); + OPENSSL_free(s); +} + +static void +usage(void) +{ + fprintf(stderr, "usage: %s [-v] input\n", argv0); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + BN_CTX *ctx; + BIGNUM *p, *q, *e, *n, *d; + BIGNUM *phi, *one, *foo, *bar; + FILE *fp; + int verbose = 0; + char ch; + + argv0 = *argv; + + while ((ch = getopt(argc, argv, "v")) != -1) { + switch (ch) { + case 'v': + verbose = 1; + break; + case '?': /* FALLTHROUGH */ + default: + usage(); + } + } + argc -= optind; + argv += optind; + + if (!argc) + usage(); + if ((fp = fopen(*argv, "r")) == NULL) + err(1, "fopen(%s)", *argv); + + ctx = BN_CTX_new(); + p = BN_new(); + q = BN_new(); + e = BN_new(); + n = BN_new(); + d = BN_new(); + + phi = BN_new(); + one = BN_new(); + foo = BN_new(); + bar = BN_new(); + + /* We assume the file has at least 3 lines */ + BN_hex2bn(&p, read_line(fp)); + BN_hex2bn(&q, read_line(fp)); + BN_hex2bn(&e, read_line(fp)); + BN_mul(n, p, q, ctx); + + /* + * Calculate private key: + * 1. phi(n) = (p-1) * (q-1) + * 2. (e * d mod phi(n) = 1), solve for d + */ + BN_dec2bn(&one, "1"); + BN_sub(foo, p, one); + BN_sub(bar, q, one); + BN_mul(phi, foo, bar, ctx); + BN_mod_inverse(d, e, phi, ctx); + + if (verbose) { + printbn("e: ", e); + printbn("n: ", n); + printbn("d: ", d); + } else + printbn("", d); + + fclose(fp); + OPENSSL_free(p); + OPENSSL_free(q); + OPENSSL_free(e); + OPENSSL_free(n); + OPENSSL_free(q); + OPENSSL_free(phi); + OPENSSL_free(one); + OPENSSL_free(foo); + OPENSSL_free(bar); + OPENSSL_free(ctx); + + return (0); +} diff --git a/c_security/ex1/src/sign.c b/c_security/ex1/src/sign.c @@ -0,0 +1,75 @@ +#include <err.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <openssl/bn.h> + +static const char * +read_line(FILE *fp) +{ + char buf[BUFSIZ]; + + if (fgets(buf, sizeof(buf), fp) == NULL) + err(1, "fgets"); + return (strdup(buf)); +} + +static void +printbn(char *str, BIGNUM *bn) +{ + char *s; + + s = BN_bn2hex(bn); + printf("%s%s\n", str, s); + OPENSSL_free(s); +} + +int +main(int argc, char *argv[]) +{ + BN_CTX *ctx; + BIGNUM *e, *n, *d, *c, *str, *sign; + FILE *fp; + int len = 0; + char buf[BUFSIZ]; + + if (argc < 2) { + fprintf(stderr, "usage: %s input\n", *argv); + return (-1); + } + if ((fp = fopen(argv[1], "r")) == NULL) + err(1, "fopen(%s)", argv[1]); + + /* Read string from stdin */ + while (read(STDIN_FILENO, &buf[len++], 1) > 0) + ; + buf[--len] = '\0'; + + ctx = BN_CTX_new(); + e = BN_new(); + n = BN_new(); + d = BN_new(); + c = BN_new(); + str = BN_new(); + sign = BN_new(); + + BN_hex2bn(&e, read_line(fp)); + BN_hex2bn(&n, read_line(fp)); + BN_hex2bn(&d, read_line(fp)); + + BN_hex2bn(&str, buf); + BN_mod_exp(sign, str, d, n, ctx); + printbn("", sign); + + fclose(fp); + OPENSSL_free(e); + OPENSSL_free(n); + OPENSSL_free(d); + OPENSSL_free(c); + OPENSSL_free(str); + OPENSSL_free(sign); + OPENSSL_free(ctx); + + return (0); +} diff --git a/c_security/ex1/src/tests b/c_security/ex1/src/tests @@ -0,0 +1,28 @@ +#!/bin/sh + +echo "exercise 1:" +./priv dat/priv.in +echo + +echo "exercise 2:" +./priv -v dat/priv.in | awk '{print $2}' > dat/encrypt.in +./atoh 'Christos Margiolis' | ./encrypt dat/encrypt.in +echo + +echo "exercise 3:" +./decrypt dat/decrypt.in | ./htoa +echo + +echo "exercise 4:" +printf "sign1: "; ./atoh 'This is a message' | ./sign dat/decrypt.in +printf "sign2: "; ./atoh 'This iz a message' | ./sign dat/decrypt.in +echo + +echo "exercise 5a:" +./verify dat/verify1_cor.in | tail -1 | awk '{print $2}' | ./htoa +./verify dat/verify1_inc.in | tail -1 | awk '{print $2}' | ./htoa +echo + +echo "exercise 5b:" +./verify dat/verify2.in | tail -1 | awk '{print $2}' | ./htoa +echo diff --git a/c_security/ex1/src/verify.c b/c_security/ex1/src/verify.c @@ -0,0 +1,65 @@ +#include <err.h> +#include <stdio.h> +#include <string.h> + +#include <openssl/bn.h> + +static const char * +read_line(FILE *fp) +{ + char buf[BUFSIZ]; + + if (fgets(buf, sizeof(buf), fp) == NULL) + err(1, "fgets"); + return (strdup(buf)); +} + +static void +printbn(char *str, BIGNUM *bn) +{ + char *s; + + s = BN_bn2hex(bn); + printf("%s%s\n", str, s); + OPENSSL_free(s); +} + +int +main(int argc, char *argv[]) +{ + BN_CTX *ctx; + BIGNUM *e, *n, *sign, *str; + FILE *fp; + + if (argc < 2) { + fprintf(stderr, "usage: %s input\n", *argv); + return (-1); + } + if ((fp = fopen(argv[1], "r")) == NULL) + err(1, "fopen(%s)", argv[1]); + + ctx = BN_CTX_new(); + e = BN_new(); + n = BN_new(); + sign = BN_new(); + str = BN_new(); + + BN_hex2bn(&e, read_line(fp)); + BN_hex2bn(&n, read_line(fp)); + BN_hex2bn(&sign, read_line(fp)); + + BN_mod_exp(str, sign, e, n, ctx); + printbn("e: ", e); + printbn("n: ", n); + printbn("sign: ", sign); + printbn("str: ", str); + + fclose(fp); + OPENSSL_free(e); + OPENSSL_free(n); + OPENSSL_free(sign); + OPENSSL_free(str); + OPENSSL_free(ctx); + + return (0); +} diff --git a/lex_bison_compilers/fsm b/lex_bison_compilers/part1/fsm Binary files differ. diff --git a/lex_bison_compilers/fsm.c b/lex_bison_compilers/part1/fsm.c