Mercurial > dropbear
annotate crypt.tex @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | 5d99163f7e32 |
children | 39d5d58461d6 |
rev | line source |
---|---|
143 | 1 \documentclass[a4paper]{book} |
3 | 2 \usepackage{hyperref} |
3 \usepackage{makeidx} | |
4 \usepackage{amssymb} | |
5 \usepackage{color} | |
6 \usepackage{alltt} | |
7 \usepackage{graphicx} | |
8 \usepackage{layout} | |
9 \def\union{\cup} | |
10 \def\intersect{\cap} | |
11 \def\getsrandom{\stackrel{\rm R}{\gets}} | |
12 \def\cross{\times} | |
13 \def\cat{\hspace{0.5em} \| \hspace{0.5em}} | |
14 \def\catn{$\|$} | |
15 \def\divides{\hspace{0.3em} | \hspace{0.3em}} | |
16 \def\nequiv{\not\equiv} | |
17 \def\approx{\raisebox{0.2ex}{\mbox{\small $\sim$}}} | |
18 \def\lcm{{\rm lcm}} | |
19 \def\gcd{{\rm gcd}} | |
20 \def\log{{\rm log}} | |
21 \def\ord{{\rm ord}} | |
22 \def\abs{{\mathit abs}} | |
23 \def\rep{{\mathit rep}} | |
24 \def\mod{{\mathit\ mod\ }} | |
25 \renewcommand{\pmod}[1]{\ ({\rm mod\ }{#1})} | |
26 \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor} | |
27 \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil} | |
28 \def\Or{{\rm\ or\ }} | |
29 \def\And{{\rm\ and\ }} | |
30 \def\iff{\hspace{1em}\Longleftrightarrow\hspace{1em}} | |
31 \def\implies{\Rightarrow} | |
32 \def\undefined{{\rm ``undefined"}} | |
33 \def\Proof{\vspace{1ex}\noindent {\bf Proof:}\hspace{1em}} | |
34 \let\oldphi\phi | |
35 \def\phi{\varphi} | |
36 \def\Pr{{\rm Pr}} | |
37 \newcommand{\str}[1]{{\mathbf{#1}}} | |
38 \def\F{{\mathbb F}} | |
39 \def\N{{\mathbb N}} | |
40 \def\Z{{\mathbb Z}} | |
41 \def\R{{\mathbb R}} | |
42 \def\C{{\mathbb C}} | |
43 \def\Q{{\mathbb Q}} | |
44 | |
45 \def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}} | |
46 | |
47 \def\gap{\vspace{0.5ex}} | |
48 \makeindex | |
49 \begin{document} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
50 \title{LibTomCrypt \\ Version 1.02} |
3 | 51 \author{Tom St Denis \\ |
52 \\ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
53 [email protected] \\ |
143 | 54 http://libtomcrypt.org |
3 | 55 } |
56 \maketitle | |
57 This text and source code library are both hereby placed in the public domain. This book has been | |
143 | 58 formatted for A4 paper using the \LaTeX{} {\em book} macro package. |
3 | 59 |
60 \vspace{10cm} | |
61 | |
62 \begin{flushright}Open Source. Open Academia. Open Minds. | |
63 | |
64 \mbox{ } | |
65 | |
66 Tom St Denis, | |
67 | |
143 | 68 Phone: 1-613-836-3160 |
69 | |
70 111 Banning Rd | |
71 | |
72 Kanata, Ontario | |
73 | |
74 K2L 1C3 | |
75 | |
76 Canada | |
3 | 77 \end{flushright} |
78 \newpage | |
79 \tableofcontents | |
80 \chapter{Introduction} | |
81 \section{What is the LibTomCrypt?} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
82 LibTomCrypt is a portable ISO C cryptographic library that is meant to be a toolset for cryptographers who are |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
83 designing a cryptosystem. It supports symmetric ciphers, one-way hashes, pseudo-random number generators, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
84 public key cryptography (via PKCS \#1 RSA, DH or ECCDH) and a plethora of support |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
85 routines. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
86 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
87 The library was designed such that new ciphers/hashes/PRNGs can be added at runtime and the existing API |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
88 (and helper API functions) are able to use the new designs automatically. There exists self-check functions for each |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
89 block cipher and hash function to ensure that they compile and execute to the published design specifications. The library |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
90 also performs extensive parameter error checking to prevent any number of runtime exploits or errors. |
3 | 91 |
92 \subsection{What the library IS for?} | |
93 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
94 The library serves as a toolkit for developers who have to solve cryptographic problems. Out of the box LibTomCrypt |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
95 does not process SSL or OpenPGP messages, it doesn't read x.591 certificates or write PEM encoded data. It does, however, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
96 provide all of the tools required to build such functionality. LibTomCrypt was designed to be a flexible library that |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
97 was not tied to any particular cryptographic problem. |
3 | 98 |
99 \section{Why did I write it?} | |
100 You may be wondering, ``Tom, why did you write a crypto library. I already have one.''. Well the reason falls into | |
101 two categories: | |
102 \begin{enumerate} | |
103 \item I am too lazy to figure out someone else's API. I'd rather invent my own simpler API and use that. | |
104 \item It was (still is) good coding practice. | |
105 \end{enumerate} | |
106 | |
107 The idea is that I am not striving to replace OpenSSL or Crypto++ or Cryptlib or etc. I'm trying to write my | |
108 {\bf own} crypto library and hopefully along the way others will appreciate the work. | |
109 | |
110 With this library all core functions (ciphers, hashes, prngs) have the {\bf exact} same prototype definition. They all load | |
111 and store data in a format independent of the platform. This means if you encrypt with Blowfish on a PPC it should decrypt | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
112 on an x86 with zero problems. The consistent API also means that if you learn how to use Blowfish with my library you |
3 | 113 know how to use Safer+ or RC6 or Serpent or ... as well. With all of the core functions there are central descriptor tables |
114 that can be used to make a program automatically pick between ciphers, hashes and PRNGs at runtime. That means your | |
115 application can support all ciphers/hashes/prngs without changing the source code. | |
116 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
117 Not only did I strive to make a consistent and simple API to work with but I also strived to make the library |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
118 configurable in terms of its build options. Out of the box the library will build with any modern version of GCC |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
119 without having to use configure scripts. This means that the library will work with platforms where development |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
120 tools may be limited (e.g. no autoconf). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
121 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
122 On top of making the build simple and the API approachable I've also strived for a reasonably high level of |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
123 robustness and efficiency. LibTomCrypt traps and returns a series of errors ranging from invalid |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
124 arguments to buffer overflows/overruns. It is mostly thread safe and has been clocked on various platforms |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
125 with ``cycles per byte'' timings that are comparable (and often favourable) to other libraries such as OpenSSL and |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
126 Crypto++. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
127 |
3 | 128 \subsection{Modular} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
129 The LibTomCrypt package has also been written to be very modular. The block ciphers, one--way hashes and |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
130 pseudo--random number generators (PRNG) are all used within the API through ``descriptor'' tables which |
3 | 131 are essentially structures with pointers to functions. While you can still call particular functions |
132 directly (\textit{e.g. sha256\_process()}) this descriptor interface allows the developer to customize their | |
133 usage of the library. | |
134 | |
135 For example, consider a hardware platform with a specialized RNG device. Obviously one would like to tap | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
136 that for the PRNG needs within the library (\textit{e.g. making a RSA key}). All the developer has to do |
3 | 137 is write a descriptor and the few support routines required for the device. After that the rest of the |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
138 API can make use of it without change. Similiarly imagine a few years down the road when AES2 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
139 (\textit{or whatever they call it}) has been invented. It can be added to the library and used within applications |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
140 with zero modifications to the end applications provided they are written properly. |
3 | 141 |
142 This flexibility within the library means it can be used with any combination of primitive algorithms and | |
143 unlike libraries like OpenSSL is not tied to direct routines. For instance, in OpenSSL there are CBC block | |
144 mode routines for every single cipher. That means every time you add or remove a cipher from the library | |
145 you have to update the associated support code as well. In LibTomCrypt the associated code (\textit{chaining modes in this case}) | |
146 are not directly tied to the ciphers. That is a new cipher can be added to the library by simply providing | |
147 the key setup, ECB decrypt and encrypt and test vector routines. After that all five chaining mode routines | |
148 can make use of the cipher right away. | |
149 | |
150 \section{License} | |
151 | |
152 All of the source code except for the following files have been written by the author or donated to the project | |
153 under a public domain license: | |
154 | |
155 \begin{enumerate} | |
156 \item rc2.c | |
157 \end{enumerate} | |
158 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
159 `mpi.c'' was originally written by Michael Fromberger ([email protected]) but has since been replaced with |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
160 my LibTomMath library which is public domain. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
161 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
162 ``rc2.c'' is based on publicly available code that is not attributed to a person from the given source. |
3 | 163 |
164 The project is hereby released as public domain. | |
165 | |
166 \section{Patent Disclosure} | |
167 | |
168 The author (Tom St Denis) is not a patent lawyer so this section is not to be treated as legal advice. To the best | |
169 of the authors knowledge the only patent related issues within the library are the RC5 and RC6 symmetric block ciphers. | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
170 They can be removed from a build by simply commenting out the two appropriate lines in ``tomcrypt\_custom.h''. The rest |
3 | 171 of the ciphers and hashes are patent free or under patents that have since expired. |
172 | |
173 The RC2 and RC4 symmetric ciphers are not under patents but are under trademark regulations. This means you can use | |
174 the ciphers you just can't advertise that you are doing so. | |
175 | |
176 \section{Thanks} | |
143 | 177 I would like to give thanks to the following people (in no particular order) for helping me develop this project from |
178 early on: | |
3 | 179 \begin{enumerate} |
180 \item Richard van de Laarschot | |
181 \item Richard Heathfield | |
182 \item Ajay K. Agrawal | |
183 \item Brian Gladman | |
184 \item Svante Seleborg | |
185 \item Clay Culver | |
186 \item Jason Klapste | |
187 \item Dobes Vandermeer | |
188 \item Daniel Richards | |
189 \item Wayne Scott | |
190 \item Andrew Tyler | |
191 \item Sky Schulz | |
192 \item Christopher Imes | |
193 \end{enumerate} | |
194 | |
143 | 195 There have been quite a few other people as well. Please check the change log to see who else has contributed from |
196 time to time. | |
197 | |
3 | 198 \chapter{The Application Programming Interface (API)} |
199 \section{Introduction} | |
200 \index{CRYPT\_ERROR} \index{CRYPT\_OK} | |
201 | |
202 In general the API is very simple to memorize and use. Most of the functions return either {\bf void} or {\bf int}. Functions | |
203 that return {\bf int} will return {\bf CRYPT\_OK} if the function was successful or one of the many error codes | |
204 if it failed. Certain functions that return int will return $-1$ to indicate an error. These functions will be explicitly | |
205 commented upon. When a function does return a CRYPT error code it can be translated into a string with | |
206 | |
15 | 207 \index{error\_to\_string()} |
3 | 208 \begin{verbatim} |
15 | 209 const char *error_to_string(int err); |
3 | 210 \end{verbatim} |
211 | |
212 An example of handling an error is: | |
213 \begin{verbatim} | |
214 void somefunc(void) | |
215 { | |
15 | 216 int err; |
3 | 217 |
218 /* call a cryptographic function */ | |
15 | 219 if ((err = some_crypto_function(...)) != CRYPT_OK) { |
220 printf("A crypto error occured, %s\n", error_to_string(err)); | |
3 | 221 /* perform error handling */ |
222 } | |
223 /* continue on if no error occured */ | |
224 } | |
225 \end{verbatim} | |
226 | |
227 There is no initialization routine for the library and for the most part the code is thread safe. The only thread | |
228 related issue is if you use the same symmetric cipher, hash or public key state data in multiple threads. Normally | |
229 that is not an issue. | |
230 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
231 To include the prototypes for ``LibTomCrypt.a'' into your own program simply include ``tomcrypt.h'' like so: |
3 | 232 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
233 #include <tomcrypt.h> |
3 | 234 int main(void) { |
235 return 0; | |
236 } | |
237 \end{verbatim} | |
238 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
239 The header file ``tomcrypt.h'' also includes ``stdio.h'', ``string.h'', ``stdlib.h'', ``time.h'', ``ctype.h'' and |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
240 ``ltc\_tommath.h'' (the bignum library routines). |
3 | 241 |
242 \section{Macros} | |
243 | |
244 There are a few helper macros to make the coding process a bit easier. The first set are related to loading and storing | |
245 32/64-bit words in little/big endian format. The macros are: | |
246 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
247 \index{STORE32L} \index{STORE64L} \index{LOAD32L} \index{LOAD64L} \index{STORE32H} \index{STORE64H} \index{LOAD32H} \index{LOAD64H} \index{BSWAP} |
3 | 248 \begin{small} |
249 \begin{center} | |
250 \begin{tabular}{|c|c|c|} | |
251 \hline STORE32L(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $x \to y[0 \ldots 3]$ \\ | |
252 \hline STORE64L(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $x \to y[0 \ldots 7]$ \\ | |
253 \hline LOAD32L(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $y[0 \ldots 3] \to x$ \\ | |
254 \hline LOAD64L(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $y[0 \ldots 7] \to x$ \\ | |
255 \hline STORE32H(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $x \to y[3 \ldots 0]$ \\ | |
256 \hline STORE64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $x \to y[7 \ldots 0]$ \\ | |
257 \hline LOAD32H(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $y[3 \ldots 0] \to x$ \\ | |
258 \hline LOAD64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $y[7 \ldots 0] \to x$ \\ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
259 \hline BSWAP(x) & {\bf unsigned long} x & Swaps byte order (32--bits only) \\ |
3 | 260 \hline |
261 \end{tabular} | |
262 \end{center} | |
263 \end{small} | |
264 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
265 There are 32 and 64-bit cyclic rotations as well: |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
266 \index{ROL} \index{ROR} \index{ROL64} \index{ROR64} \index{ROLc} \index{RORc} \index{ROL64c} \index{ROR64c} |
3 | 267 \begin{center} |
268 \begin{tabular}{|c|c|c|} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
269 \hline ROL(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x << y, 0 \le y \le 31$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
270 \hline ROLc(x, y) & {\bf unsigned long} x, {\bf const unsigned long} y & $x << y, 0 \le y \le 31$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
271 \hline ROR(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x >> y, 0 \le y \le 31$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
272 \hline RORc(x, y) & {\bf unsigned long} x, {\bf const unsigned long} y & $x >> y, 0 \le y \le 31$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
273 \hline && \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
274 \hline ROL64(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x << y, 0 \le y \le 63$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
275 \hline ROL64c(x, y) & {\bf unsigned long} x, {\bf const unsigned long} y & $x << y, 0 \le y \le 63$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
276 \hline ROR64(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x >> y, 0 \le y \le 63$ \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
277 \hline ROR64c(x, y) & {\bf unsigned long} x, {\bf const unsigned long} y & $x >> y, 0 \le y \le 63$ \\ |
3 | 278 \hline |
279 \end{tabular} | |
280 \end{center} | |
281 | |
282 \section{Functions with Variable Length Output} | |
283 Certain functions such as (for example) ``rsa\_export()'' give an output that is variable length. To prevent buffer overflows you | |
284 must pass it the length of the buffer\footnote{Extensive error checking is not in place but it will be in future releases so it is a good idea to follow through with these guidelines.} where | |
285 the output will be stored. For example: | |
286 \begin{small} | |
287 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
288 #include <tomcrypt.h> |
3 | 289 int main(void) { |
290 rsa_key key; | |
291 unsigned char buffer[1024]; | |
292 unsigned long x; | |
15 | 293 int err; |
3 | 294 |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
295 /* ... Make up the RSA key somehow ... */ |
3 | 296 |
297 /* lets export the key, set x to the size of the output buffer */ | |
298 x = sizeof(buffer); | |
15 | 299 if ((err = rsa_export(buffer, &x, PK_PUBLIC, &key)) != CRYPT_OK) { |
300 printf("Export error: %s\n", error_to_string(err)); | |
3 | 301 return -1; |
302 } | |
303 | |
304 /* if rsa_export() was successful then x will have the size of the output */ | |
305 printf("RSA exported key takes %d bytes\n", x); | |
306 | |
307 /* ... do something with the buffer */ | |
308 | |
309 return 0; | |
310 } | |
311 \end{verbatim} | |
312 \end{small} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
313 In the above example if the size of the RSA public key was more than 1024 bytes this function would return an error code |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
314 indicating a buffer overflow would have occurred. If the function succeeds it stores the length of the output |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
315 back into ``x'' so that the calling application will know how many bytes were used. |
3 | 316 |
317 \section{Functions that need a PRNG} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
318 \index{Pseudo Random Number Generator} \index{PRNG} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
319 Certain functions such as ``rsa\_make\_key()'' require a Pseudo Random Number Generator (PRNG). These functions do not setup |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
320 the PRNG themselves so it is the responsibility of the calling function to initialize the PRNG before calling them. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
321 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
322 Certain PRNG algorithms do not require a ``prng\_state'' argument (sprng for example). The ``prng\_state'' argument |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
323 may be passed as \textbf{NULL} in such situations. |
3 | 324 |
325 \section{Functions that use Arrays of Octets} | |
326 Most functions require inputs that are arrays of the data type ``unsigned char''. Whether it is a symmetric key, IV | |
327 for a chaining mode or public key packet it is assumed that regardless of the actual size of ``unsigned char'' only the | |
328 lower eight bits contain data. For example, if you want to pass a 256 bit key to a symmetric ciphers setup routine | |
329 you must pass it in (a pointer to) an array of 32 ``unsigned char'' variables. Certain routines | |
330 (such as SAFER+) take special care to work properly on platforms where an ``unsigned char'' is not eight bits. | |
331 | |
332 For the purposes of this library the term ``byte'' will refer to an octet or eight bit word. Typically an array of | |
333 type ``byte'' will be synonymous with an array of type ``unsigned char''. | |
334 | |
335 \chapter{Symmetric Block Ciphers} | |
336 \section{Core Functions} | |
337 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
338 LibTomCrypt provides several block ciphers with an ECB block mode interface. It's important to first note that you |
3 | 339 should never use the ECB modes directly to encrypt data. Instead you should use the ECB functions to make a chaining mode |
340 or use one of the provided chaining modes. All of the ciphers are written as ECB interfaces since it allows the rest of | |
341 the API to grow in a modular fashion. | |
342 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
343 \subsection{Key Scheduling} |
3 | 344 All ciphers store their scheduled keys in a single data type called ``symmetric\_key''. This allows all ciphers to |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
345 have the same prototype and store their keys as naturally as possible. This also removes the need for dynamic memory |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
346 allocation and allows you to allocate a fixed sized buffer for storing scheduled keys. All ciphers provide five visible |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
347 functions which are (given that XXX is the name of the cipher): |
3 | 348 \index{Cipher Setup} |
349 \begin{verbatim} | |
350 int XXX_setup(const unsigned char *key, int keylen, int rounds, | |
351 symmetric_key *skey); | |
352 \end{verbatim} | |
353 | |
354 The XXX\_setup() routine will setup the cipher to be used with a given number of rounds and a given key length (in bytes). | |
355 The number of rounds can be set to zero to use the default, which is generally a good idea. | |
356 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
357 If the function returns successfully the variable ``skey'' will have a scheduled key stored in it. It's important to note |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
358 that you should only used this scheduled key with the intended cipher. For example, if you call ``blowfish\_setup()'' do not |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
359 pass the scheduled key onto ``rc5\_ecb\_encrypt()''. All setup functions do not allocate memory off the heap so when you are |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
360 done with a key you can simply discard it (e.g. they can be on the stack). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
361 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
362 \subsection{ECB Encryption and Decryption} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
363 To encrypt or decrypt a block in ECB mode there are these two function classes |
3 | 364 \index{Cipher Encrypt} \index{Cipher Decrypt} |
365 \begin{verbatim} | |
366 void XXX_ecb_encrypt(const unsigned char *pt, unsigned char *ct, | |
367 symmetric_key *skey); | |
368 | |
369 void XXX_ecb_decrypt(const unsigned char *ct, unsigned char *pt, | |
370 symmetric_key *skey); | |
371 \end{verbatim} | |
372 These two functions will encrypt or decrypt (respectively) a single block of text\footnote{The size of which depends on | |
373 which cipher you are using.} and store the result where you want it. It is possible that the input and output buffer are | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
374 the same buffer. For the encrypt function ``pt''\footnote{pt stands for plaintext.} is the input and |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
375 ``ct''\footnote{ct stands for ciphertext.} is the output. For the decryption function it's the opposite. To test a particular |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
376 cipher against test vectors\footnote{As published in their design papers.} call the self-test function |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
377 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
378 \subsection{Self--Testing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
379 \index{Cipher Testing} |
3 | 380 \begin{verbatim} |
381 int XXX_test(void); | |
382 \end{verbatim} | |
383 This function will return {\bf CRYPT\_OK} if the cipher matches the test vectors from the design publication it is | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
384 based upon. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
385 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
386 \subsection{Key Sizing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
387 For each cipher there is a function which will help find a desired key size: |
3 | 388 \begin{verbatim} |
389 int XXX_keysize(int *keysize); | |
390 \end{verbatim} | |
391 Essentially it will round the input keysize in ``keysize'' down to the next appropriate key size. This function | |
392 return {\bf CRYPT\_OK} if the key size specified is acceptable. For example: | |
393 \begin{small} | |
394 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
395 #include <tomcrypt.h> |
3 | 396 int main(void) |
397 { | |
15 | 398 int keysize, err; |
3 | 399 |
400 /* now given a 20 byte key what keysize does Twofish want to use? */ | |
401 keysize = 20; | |
15 | 402 if ((err = twofish_keysize(&keysize)) != CRYPT_OK) { |
403 printf("Error getting key size: %s\n", error_to_string(err)); | |
3 | 404 return -1; |
405 } | |
406 printf("Twofish suggested a key size of %d\n", keysize); | |
407 return 0; | |
408 } | |
409 \end{verbatim} | |
410 \end{small} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
411 This should indicate a keysize of sixteen bytes is suggested. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
412 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
413 \subsection{Cipher Termination} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
414 When you are finished with a cipher you can de--initialize it with the done function. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
415 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
416 void XXX_done(symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
417 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
418 For the software based ciphers within LibTomCrypt this function will not do anything. However, user supplied |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
419 cipher descriptors may require calls to it for resource management. To be compliant all functions which call a cipher |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
420 setup function must also call the respective cipher done function when finished. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
421 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
422 \subsection{Simple Encryption Demonstration} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
423 An example snippet that encodes a block with Blowfish in ECB mode is below. |
3 | 424 |
425 \begin{small} | |
426 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
427 #include <tomcrypt.h> |
3 | 428 int main(void) |
429 { | |
430 unsigned char pt[8], ct[8], key[8]; | |
431 symmetric_key skey; | |
15 | 432 int err; |
3 | 433 |
434 /* ... key is loaded appropriately in ``key'' ... */ | |
435 /* ... load a block of plaintext in ``pt'' ... */ | |
436 | |
437 /* schedule the key */ | |
15 | 438 if ((err = blowfish_setup(key, /* the key we will use */ |
439 8, /* key is 8 bytes (64-bits) long */ | |
440 0, /* 0 == use default # of rounds */ | |
441 &skey) /* where to put the scheduled key */ | |
442 ) != CRYPT_OK) { | |
443 printf("Setup error: %s\n", error_to_string(err)); | |
3 | 444 return -1; |
445 } | |
446 | |
447 /* encrypt the block */ | |
15 | 448 blowfish_ecb_encrypt(pt, /* encrypt this 8-byte array */ |
449 ct, /* store encrypted data here */ | |
450 &skey); /* our previously scheduled key */ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
451 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
452 /* now ct holds the encrypted version of pt */ |
3 | 453 |
454 /* decrypt the block */ | |
15 | 455 blowfish_ecb_decrypt(ct, /* decrypt this 8-byte array */ |
456 pt, /* store decrypted data here */ | |
457 &skey); /* our previously scheduled key */ | |
3 | 458 |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
459 /* now we have decrypted ct to the original plaintext in pt */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
460 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
461 /* Terminate the cipher context */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
462 blowfish_done(&skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
463 |
3 | 464 return 0; |
465 } | |
466 \end{verbatim} | |
467 \end{small} | |
468 | |
469 \section{Key Sizes and Number of Rounds} | |
470 \index{Symmetric Keys} | |
471 As a general rule of thumb do not use symmetric keys under 80 bits if you can. Only a few of the ciphers support smaller | |
472 keys (mainly for test vectors anyways). Ideally your application should be making at least 256 bit keys. This is not | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
473 because you're supposed to be paranoid. It's because if your PRNG has a bias of any sort the more bits the better. For |
3 | 474 example, if you have $\mbox{Pr}\left[X = 1\right] = {1 \over 2} \pm \gamma$ where $\vert \gamma \vert > 0$ then the |
475 total amount of entropy in N bits is $N \cdot -log_2\left ({1 \over 2} + \vert \gamma \vert \right)$. So if $\gamma$ | |
476 were $0.25$ (a severe bias) a 256-bit string would have about 106 bits of entropy whereas a 128-bit string would have | |
477 only 53 bits of entropy. | |
478 | |
479 The number of rounds of most ciphers is not an option you can change. Only RC5 allows you to change the number of | |
480 rounds. By passing zero as the number of rounds all ciphers will use their default number of rounds. Generally the | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
481 ciphers are configured such that the default number of rounds provide adequate security for the given block and key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
482 size. |
3 | 483 |
484 \section{The Cipher Descriptors} | |
485 \index{Cipher Descriptor} | |
486 To facilitate automatic routines an array of cipher descriptors is provided in the array ``cipher\_descriptor''. An element | |
487 of this array has the following format: | |
488 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
489 \begin{small} |
3 | 490 \begin{verbatim} |
491 struct _cipher_descriptor { | |
492 char *name; | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
493 unsigned char ID; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
494 int min_key_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
495 max_key_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
496 block_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
497 default_rounds; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
498 int (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
499 void (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
500 void (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
501 int (*test)(void); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
502 void (*done)(symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
503 int (*keysize)(int *keysize); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
504 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
505 void (*accel_ecb_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
506 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
507 unsigned long blocks, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
508 void (*accel_ecb_decrypt)(const unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
509 unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
510 unsigned long blocks, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
511 void (*accel_cbc_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
512 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
513 unsigned long blocks, unsigned char *IV, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
514 symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
515 void (*accel_cbc_decrypt)(const unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
516 unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
517 unsigned long blocks, unsigned char *IV, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
518 symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
519 void (*accel_ctr_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
520 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
521 unsigned long blocks, unsigned char *IV, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
522 int mode, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
523 void (*accel_ccm_memory)( |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
524 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
525 const unsigned char *nonce, unsigned long noncelen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
526 const unsigned char *header, unsigned long headerlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
527 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
528 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
529 unsigned char *tag, unsigned long *taglen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
530 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
531 |
3 | 532 }; |
533 \end{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
534 \end{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
535 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
536 Where ``name'' is the lower case ASCII version of the name. The fields ``min\_key\_length'' and ``max\_key\_length'' |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
537 are the minimum and maximum key sizes in bytes. The ``block\_length'' member is the block size of the cipher |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
538 in bytes. As a good rule of thumb it is assumed that the cipher supports |
3 | 539 the min and max key lengths but not always everything in between. The ``default\_rounds'' field is the default number |
540 of rounds that will be used. | |
541 | |
542 The remaining fields are all pointers to the core functions for each cipher. The end of the cipher\_descriptor array is | |
543 marked when ``name'' equals {\bf NULL}. | |
544 | |
545 As of this release the current cipher\_descriptors elements are | |
546 | |
15 | 547 \index{Cipher descriptor table} |
3 | 548 \begin{small} |
549 \begin{center} | |
550 \begin{tabular}{|c|c|c|c|c|c|} | |
551 \hline Name & Descriptor Name & Block Size & Key Range & Rounds \\ | |
552 \hline Blowfish & blowfish\_desc & 8 & 8 $\ldots$ 56 & 16 \\ | |
553 \hline X-Tea & xtea\_desc & 8 & 16 & 32 \\ | |
554 \hline RC2 & rc2\_desc & 8 & 8 $\ldots$ 128 & 16 \\ | |
555 \hline RC5-32/12/b & rc5\_desc & 8 & 8 $\ldots$ 128 & 12 $\ldots$ 24 \\ | |
556 \hline RC6-32/20/b & rc6\_desc & 16 & 8 $\ldots$ 128 & 20 \\ | |
557 \hline SAFER+ & saferp\_desc &16 & 16, 24, 32 & 8, 12, 16 \\ | |
558 \hline AES & aes\_desc & 16 & 16, 24, 32 & 10, 12, 14 \\ | |
15 | 559 & aes\_enc\_desc & 16 & 16, 24, 32 & 10, 12, 14 \\ |
3 | 560 \hline Twofish & twofish\_desc & 16 & 16, 24, 32 & 16 \\ |
561 \hline DES & des\_desc & 8 & 7 & 16 \\ | |
562 \hline 3DES (EDE mode) & des3\_desc & 8 & 21 & 16 \\ | |
563 \hline CAST5 (CAST-128) & cast5\_desc & 8 & 5 $\ldots$ 16 & 12, 16 \\ | |
564 \hline Noekeon & noekeon\_desc & 16 & 16 & 16 \\ | |
565 \hline Skipjack & skipjack\_desc & 8 & 10 & 32 \\ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
566 \hline Anubis & anubis\_desc & 16 & 16 $\ldots$ 40 & 12 $\ldots$ 18 \\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
567 \hline Khazad & khazad\_desc & 8 & 16 & 8 \\ |
3 | 568 \hline |
569 \end{tabular} | |
570 \end{center} | |
571 \end{small} | |
572 | |
573 \subsection{Notes} | |
15 | 574 \begin{small} |
575 \begin{enumerate} | |
576 \item | |
577 For AES (also known as Rijndael) there are four descriptors which complicate issues a little. The descriptors | |
578 rijndael\_desc and rijndael\_enc\_desc provide the cipher named ``rijndael''. The descriptors aes\_desc and | |
579 aes\_enc\_desc provide the cipher name ``aes''. Functionally both ``rijndael'' and ``aes'' are the same cipher. The | |
580 only difference is when you call find\_cipher() you have to pass the correct name. The cipher descriptors with ``enc'' | |
581 in the middle (e.g. rijndael\_enc\_desc) are related to an implementation of Rijndael with only the encryption routine | |
582 and tables. The decryption and self--test function pointers of both ``encrypt only'' descriptors are set to \textbf{NULL} and | |
583 should not be called. | |
584 | |
585 The ``encrypt only'' descriptors are useful for applications that only use the encryption function of the cipher. Algorithms such | |
586 as EAX, PMAC and OMAC only require the encryption function. So far this ``encrypt only'' functionality has only been implemented for | |
587 Rijndael as it makes the most sense for this cipher. | |
588 | |
589 \item | |
3 | 590 Note that for ``DES'' and ``3DES'' they use 8 and 24 byte keys but only 7 and 21 [respectively] bytes of the keys are in |
591 fact used for the purposes of encryption. My suggestion is just to use random 8/24 byte keys instead of trying to make a 8/24 | |
592 byte string from the real 7/21 byte key. | |
593 | |
15 | 594 \item |
3 | 595 Note that ``Twofish'' has additional configuration options that take place at build time. These options are found in |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
596 the file ``tomcrypt\_cfg.h''. The first option is ``TWOFISH\_SMALL'' which when defined will force the Twofish code |
3 | 597 to not pre-compute the Twofish ``$g(X)$'' function as a set of four $8 \times 32$ s-boxes. This means that a scheduled |
598 key will require less ram but the resulting cipher will be slower. The second option is ``TWOFISH\_TABLES'' which when | |
599 defined will force the Twofish code to use pre-computed tables for the two s-boxes $q_0, q_1$ as well as the multiplication | |
600 by the polynomials 5B and EF used in the MDS multiplication. As a result the code is faster and slightly larger. The | |
601 speed increase is useful when ``TWOFISH\_SMALL'' is defined since the s-boxes and MDS multiply form the heart of the | |
602 Twofish round function. | |
603 | |
15 | 604 \index{Twofish build options} |
3 | 605 \begin{small} |
606 \begin{center} | |
607 \begin{tabular}{|l|l|l|} | |
608 \hline TWOFISH\_SMALL & TWOFISH\_TABLES & Speed and Memory (per key) \\ | |
609 \hline undefined & undefined & Very fast, 4.2KB of ram. \\ | |
143 | 610 \hline undefined & defined & Faster keysetup, larger code. \\ |
3 | 611 \hline defined & undefined & Very slow, 0.2KB of ram. \\ |
143 | 612 \hline defined & defined & Faster, 0.2KB of ram, larger code. \\ |
3 | 613 \hline |
614 \end{tabular} | |
615 \end{center} | |
616 \end{small} | |
617 | |
15 | 618 \end{enumerate} |
619 \end{small} | |
620 | |
3 | 621 To work with the cipher\_descriptor array there is a function: |
15 | 622 \index{find\_cipher()} |
3 | 623 \begin{verbatim} |
624 int find_cipher(char *name) | |
625 \end{verbatim} | |
626 Which will search for a given name in the array. It returns negative one if the cipher is not found, otherwise it returns | |
627 the location in the array where the cipher was found. For example, to indirectly setup Blowfish you can also use: | |
628 \begin{small} | |
629 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
630 #include <tomcrypt.h> |
3 | 631 int main(void) |
632 { | |
633 unsigned char key[8]; | |
634 symmetric_key skey; | |
15 | 635 int err; |
3 | 636 |
637 /* you must register a cipher before you use it */ | |
638 if (register_cipher(&blowfish_desc)) == -1) { | |
639 printf("Unable to register Blowfish cipher."); | |
640 return -1; | |
641 } | |
642 | |
643 /* generic call to function (assuming the key in key[] was already setup) */ | |
143 | 644 if ((err = cipher_descriptor[find_cipher("blowfish")].setup(key, 8, 0, &skey)) != |
645 CRYPT_OK) { | |
15 | 646 printf("Error setting up Blowfish: %s\n", error_to_string(err)); |
3 | 647 return -1; |
648 } | |
649 | |
650 /* ... use cipher ... */ | |
651 } | |
652 \end{verbatim} | |
653 \end{small} | |
654 | |
655 A good safety would be to check the return value of ``find\_cipher()'' before accessing the desired function. In order | |
656 to use a cipher with the descriptor table you must register it first using: | |
15 | 657 \index{register\_cipher()} |
3 | 658 \begin{verbatim} |
659 int register_cipher(const struct _cipher_descriptor *cipher); | |
660 \end{verbatim} | |
661 Which accepts a pointer to a descriptor and returns the index into the global descriptor table. If an error occurs such | |
662 as there is no more room (it can have 32 ciphers at most) it will return {\bf{-1}}. If you try to add the same cipher more | |
663 than once it will just return the index of the first copy. To remove a cipher call: | |
15 | 664 \index{unregister\_cipher()} |
3 | 665 \begin{verbatim} |
666 int unregister_cipher(const struct _cipher_descriptor *cipher); | |
667 \end{verbatim} | |
668 Which returns {\bf CRYPT\_OK} if it removes it otherwise it returns {\bf CRYPT\_ERROR}. Consider: | |
669 \begin{small} | |
670 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
671 #include <tomcrypt.h> |
3 | 672 int main(void) |
673 { | |
15 | 674 int err; |
3 | 675 |
676 /* register the cipher */ | |
677 if (register_cipher(&rijndael_desc) == -1) { | |
678 printf("Error registering Rijndael\n"); | |
679 return -1; | |
680 } | |
681 | |
682 /* use Rijndael */ | |
683 | |
684 /* remove it */ | |
15 | 685 if ((err = unregister_cipher(&rijndael_desc)) != CRYPT_OK) { |
686 printf("Error removing Rijndael: %s\n", error_to_string(err)); | |
3 | 687 return -1; |
688 } | |
689 | |
690 return 0; | |
691 } | |
692 \end{verbatim} | |
693 \end{small} | |
694 This snippet is a small program that registers only Rijndael only. | |
695 | |
696 \section{Symmetric Modes of Operations} | |
697 \subsection{Background} | |
698 A typical symmetric block cipher can be used in chaining modes to effectively encrypt messages larger than the block | |
699 size of the cipher. Given a key $k$, a plaintext $P$ and a cipher $E$ we shall denote the encryption of the block | |
700 $P$ under the key $k$ as $E_k(P)$. In some modes there exists an initial vector denoted as $C_{-1}$. | |
701 | |
702 \subsubsection{ECB Mode} | |
15 | 703 \index{ECB mode} |
3 | 704 ECB or Electronic Codebook Mode is the simplest method to use. It is given as: |
705 \begin{equation} | |
706 C_i = E_k(P_i) | |
707 \end{equation} | |
708 This mode is very weak since it allows people to swap blocks and perform replay attacks if the same key is used more | |
709 than once. | |
710 | |
711 \subsubsection{CBC Mode} | |
15 | 712 \index{CBC mode} |
3 | 713 CBC or Cipher Block Chaining mode is a simple mode designed to prevent trivial forms of replay and swap attacks on ciphers. |
714 It is given as: | |
715 \begin{equation} | |
716 C_i = E_k(P_i \oplus C_{i - 1}) | |
717 \end{equation} | |
718 It is important that the initial vector be unique and preferably random for each message encrypted under the same key. | |
719 | |
720 \subsubsection{CTR Mode} | |
15 | 721 \index{CTR mode} |
3 | 722 CTR or Counter Mode is a mode which only uses the encryption function of the cipher. Given a initial vector which is |
723 treated as a large binary counter the CTR mode is given as: | |
724 \begin{eqnarray} | |
725 C_{-1} = C_{-1} + 1\mbox{ }(\mbox{mod }2^W) \nonumber \\ | |
726 C_i = P_i \oplus E_k(C_{-1}) | |
727 \end{eqnarray} | |
728 Where $W$ is the size of a block in bits (e.g. 64 for Blowfish). As long as the initial vector is random for each message | |
729 encrypted under the same key replay and swap attacks are infeasible. CTR mode may look simple but it is as secure | |
730 as the block cipher is under a chosen plaintext attack (provided the initial vector is unique). | |
731 | |
732 \subsubsection{CFB Mode} | |
15 | 733 \index{CFB mode} |
3 | 734 CFB or Ciphertext Feedback Mode is a mode akin to CBC. It is given as: |
735 \begin{eqnarray} | |
736 C_i = P_i \oplus C_{-1} \nonumber \\ | |
737 C_{-1} = E_k(C_i) | |
738 \end{eqnarray} | |
739 Note that in this library the output feedback width is equal to the size of the block cipher. That is this mode is used | |
740 to encrypt whole blocks at a time. However, the library will buffer data allowing the user to encrypt or decrypt partial | |
741 blocks without a delay. When this mode is first setup it will initially encrypt the initial vector as required. | |
742 | |
743 \subsubsection{OFB Mode} | |
15 | 744 \index{OFB mode} |
3 | 745 OFB or Output Feedback Mode is a mode akin to CBC as well. It is given as: |
746 \begin{eqnarray} | |
747 C_{-1} = E_k(C_{-1}) \nonumber \\ | |
748 C_i = P_i \oplus C_{-1} | |
749 \end{eqnarray} | |
750 Like the CFB mode the output width in CFB mode is the same as the width of the block cipher. OFB mode will also | |
751 buffer the output which will allow you to encrypt or decrypt partial blocks without delay. | |
752 | |
753 \subsection{Choice of Mode} | |
754 My personal preference is for the CTR mode since it has several key benefits: | |
755 \begin{enumerate} | |
756 \item No short cycles which is possible in the OFB and CFB modes. | |
757 \item Provably as secure as the block cipher being used under a chosen plaintext attack. | |
758 \item Technically does not require the decryption routine of the cipher. | |
759 \item Allows random access to the plaintext. | |
760 \item Allows the encryption of block sizes that are not equal to the size of the block cipher. | |
761 \end{enumerate} | |
762 The CTR, CFB and OFB routines provided allow you to encrypt block sizes that differ from the ciphers block size. They | |
763 accomplish this by buffering the data required to complete a block. This allows you to encrypt or decrypt any size | |
764 block of memory with either of the three modes. | |
765 | |
766 The ECB and CBC modes process blocks of the same size as the cipher at a time. Therefore they are less flexible than the | |
767 other modes. | |
768 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
769 \subsection{Initialization} |
3 | 770 \index{CBC Mode} \index{CTR Mode} |
771 \index{OFB Mode} \index{CFB Mode} | |
772 The library provides simple support routines for handling CBC, CTR, CFB, OFB and ECB encoded messages. Assuming the mode | |
773 you want is XXX there is a structure called ``symmetric\_XXX'' that will contain the information required to | |
774 use that mode. They have identical setup routines (except ECB mode for obvious reasons): | |
15 | 775 \index{ecb\_start()} \index{cfb\_start()} \index{cbc\_start()} \index{ofb\_start()} \index{ctr\_start()} |
3 | 776 \begin{verbatim} |
777 int XXX_start(int cipher, const unsigned char *IV, | |
778 const unsigned char *key, int keylen, | |
779 int num_rounds, symmetric_XXX *XXX); | |
780 | |
781 int ecb_start(int cipher, const unsigned char *key, int keylen, | |
782 int num_rounds, symmetric_ECB *ecb); | |
783 \end{verbatim} | |
784 | |
785 In each case ``cipher'' is the index into the cipher\_descriptor array of the cipher you want to use. The ``IV'' value is | |
786 the initialization vector to be used with the cipher. You must fill the IV yourself and it is assumed they are the same | |
787 length as the block size\footnote{In otherwords the size of a block of plaintext for the cipher, e.g. 8 for DES, 16 for AES, etc.} | |
788 of the cipher you choose. It is important that the IV be random for each unique message you want to encrypt. The | |
789 parameters ``key'', ``keylen'' and ``num\_rounds'' are the same as in the XXX\_setup() function call. The final parameter | |
790 is a pointer to the structure you want to hold the information for the mode of operation. | |
791 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
792 Both routines return {\bf CRYPT\_OK} if the cipher initialized correctly, otherwise they return an error code. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
793 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
794 \subsection{Encryption and Decryption} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
795 To actually encrypt or decrypt the following routines are provided: |
15 | 796 \index{ecb\_encrypt()} \index{ecb\_decrypt()} \index{cfb\_encrypt()} \index{cfb\_decrypt()} |
797 \index{cbc\_encrypt()} \index{cbc\_decrypt()} \index{ofb\_encrypt()} \index{ofb\_decrypt()} \index{ctr\_encrypt()} \index{ctr\_decrypt()} | |
3 | 798 \begin{verbatim} |
799 int XXX_encrypt(const unsigned char *pt, unsigned char *ct, | |
800 unsigned long len, symmetric_YYY *YYY); | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
801 int XXX_decrypt(const unsigned char *ct, unsigned char *pt, |
3 | 802 unsigned long len, symmetric_YYY *YYY); |
803 \end{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
804 Where ``XXX'' is one of $\lbrace ecb, cbc, ctr, cfb, ofb \rbrace$. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
805 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
806 In all cases ``len'' is the size of the buffer (as number of octets) to encrypt or decrypt. The CTR, OFB and CFB modes are order sensitive but not |
3 | 807 chunk sensitive. That is you can encrypt ``ABCDEF'' in three calls like ``AB'', ``CD'', ``EF'' or two like ``ABCDE'' and ``F'' |
808 and end up with the same ciphertext. However, encrypting ``ABC'' and ``DABC'' will result in different ciphertexts. All | |
809 five of the modes will return {\bf CRYPT\_OK} on success from the encrypt or decrypt functions. | |
810 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
811 In the ECB and CBC cases ``len'' must be a multiple of the ciphers block size. In the CBC case you must manually pad the end of your message (either with |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
812 zeroes or with whatever your protocol requires). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
813 |
3 | 814 To decrypt in either mode you simply perform the setup like before (recall you have to fetch the IV value you used) |
15 | 815 and use the decrypt routine on all of the blocks. |
816 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
817 \subsection{IV Manipulation} |
15 | 818 To change or read the IV of a previously initialized chaining mode use the following two functions. |
819 | |
820 \index{cbc\_setiv()} \index{cbc\_getiv()} \index{ofb\_setiv()} \index{ofb\_getiv()} \index{cfb\_setiv()} \index{cfb\_getiv()} | |
821 \index{ctr\_setiv()} \index{ctr\_getiv()} | |
822 \begin{verbatim} | |
823 int XXX_getiv(unsigned char *IV, unsigned long *len, symmetric_XXX *XXX); | |
824 int XXX_setiv(const unsigned char *IV, unsigned long len, symmetric_XXX *XXX); | |
825 \end{verbatim} | |
826 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
827 The XXX\_getiv() functions will read the IV out of the chaining mode and store it into ``IV'' along with the length of the IV |
15 | 828 stored in ``len''. The XXX\_setiv will initialize the chaining mode state as if the original IV were the new IV specified. The length |
829 of the IV passed in must be the size of the ciphers block size. | |
830 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
831 The XXX\_setiv() functions are handy if you wish to change the IV without re--keying the cipher. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
832 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
833 \subsection{Stream Termination} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
834 To terminate an open stream call the done function. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
835 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
836 \index{ecb\_done()} \index{cbc\_done()}\index{cfb\_done()}\index{ofb\_done()} \index{ctr\_done()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
837 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
838 int XXX_done(symmetric_XXX *XXX); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
839 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
840 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
841 This will terminate the stream (by terminating the cipher) and return \textbf{CRYPT\_OK} if successful. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
842 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
843 \subsection{Examples} |
15 | 844 |
3 | 845 \newpage |
846 \begin{small} | |
847 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
848 #include <tomcrypt.h> |
3 | 849 int main(void) |
850 { | |
851 unsigned char key[16], IV[16], buffer[512]; | |
852 symmetric_CTR ctr; | |
15 | 853 int x, err; |
3 | 854 |
855 /* register twofish first */ | |
856 if (register_cipher(&twofish_desc) == -1) { | |
857 printf("Error registering cipher.\n"); | |
858 return -1; | |
859 } | |
860 | |
861 /* somehow fill out key and IV */ | |
862 | |
863 /* start up CTR mode */ | |
143 | 864 if ((err = ctr_start( |
865 find_cipher("twofish"), /* index of desired cipher */ | |
866 IV, /* the initial vector */ | |
867 key, /* the secret key */ | |
868 16, /* length of secret key (16 bytes, 128 bits) */ | |
869 0, /* 0 == default # of rounds */ | |
870 &ctr) /* where to store initialized CTR state */ | |
15 | 871 ) != CRYPT_OK) { |
872 printf("ctr_start error: %s\n", error_to_string(err)); | |
3 | 873 return -1; |
874 } | |
875 | |
876 /* somehow fill buffer than encrypt it */ | |
15 | 877 if ((err = ctr_encrypt( buffer, /* plaintext */ |
878 buffer, /* ciphertext */ | |
879 sizeof(buffer), /* length of data to encrypt */ | |
880 &ctr) /* previously initialized CTR state */ | |
881 ) != CRYPT_OK) { | |
882 printf("ctr_encrypt error: %s\n", error_to_string(err)); | |
3 | 883 return -1; |
884 } | |
885 | |
886 /* make use of ciphertext... */ | |
887 | |
15 | 888 /* now we want to decrypt so let's use ctr_setiv */ |
889 if ((err = ctr_setiv( IV, /* the initial IV we gave to ctr_start */ | |
890 16, /* the IV is 16 bytes long */ | |
891 &ctr) /* the ctr state we wish to modify */ | |
892 ) != CRYPT_OK) { | |
893 printf("ctr_setiv error: %s\n", error_to_string(err)); | |
894 return -1; | |
895 } | |
896 | |
897 if ((err = ctr_decrypt( buffer, /* ciphertext */ | |
898 buffer, /* plaintext */ | |
899 sizeof(buffer), /* length of data to encrypt */ | |
900 &ctr) /* previously initialized CTR state */ | |
901 ) != CRYPT_OK) { | |
902 printf("ctr_decrypt error: %s\n", error_to_string(err)); | |
903 return -1; | |
904 } | |
905 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
906 /* terminate the stream */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
907 if ((err = ctr_done(&ctr)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
908 printf("ctr_done error: %s\n", error_to_string(err)); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
909 return -1; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
910 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
911 |
3 | 912 /* clear up and return */ |
913 zeromem(key, sizeof(key)); | |
914 zeromem(&ctr, sizeof(ctr)); | |
915 | |
916 return 0; | |
917 } | |
918 \end{verbatim} | |
919 \end{small} | |
920 | |
921 \section{Encrypt and Authenticate Modes} | |
922 | |
923 \subsection{EAX Mode} | |
924 LibTomCrypt provides support for a mode called EAX\footnote{See | |
925 M. Bellare, P. Rogaway, D. Wagner, A Conventional Authenticated-Encryption Mode.} in a manner similar to the | |
15 | 926 way it was intended to be used by the designers. First a short description of what EAX mode is before I explain how to use it. |
927 EAX is a mode that requires a cipher, CTR and OMAC support and provides encryption and authentication\footnote{Note that since EAX only requires OMAC and CTR you may use ``encrypt only'' cipher descriptors with this mode.}. | |
928 It is initialized with a random ``nonce'' that can be shared publicly as well as a ``header'' which can be fixed and public as well as a random | |
929 secret symmetric key. | |
3 | 930 |
931 The ``header'' data is meant to be meta-data associated with a stream that isn't private (e.g. protocol messages). It can | |
932 be added at anytime during an EAX stream and is part of the authentication tag. That is, changes in the meta-data can | |
15 | 933 be detected by changes in the output tag. |
3 | 934 |
935 The mode can then process plaintext producing ciphertext as well as compute a partial checksum. The actual checksum | |
936 called a ``tag'' is only emitted when the message is finished. In the interim though the user can process any arbitrary | |
937 sized message block to send to the recipient as ciphertext. This makes the EAX mode especially suited for streaming modes | |
938 of operation. | |
939 | |
940 The mode is initialized with the following function. | |
15 | 941 \index{eax\_init()} |
3 | 942 \begin{verbatim} |
943 int eax_init(eax_state *eax, int cipher, | |
944 const unsigned char *key, unsigned long keylen, | |
945 const unsigned char *nonce, unsigned long noncelen, | |
946 const unsigned char *header, unsigned long headerlen); | |
947 \end{verbatim} | |
948 | |
949 Where ``eax'' is the EAX state. ``cipher'' is the index of the desired cipher in the descriptor table. | |
950 ``key'' is the shared secret symmetric key of length ``keylen''. ``nonce'' is the random public string of | |
951 length ``noncelen''. ``header'' is the random (or fixed or \textbf{NULL}) header for the message of length | |
952 ``headerlen''. | |
953 | |
954 When this function completes ``eax'' will be initialized such that you can now either have data decrypted or | |
15 | 955 encrypted in EAX mode. Note that if ``headerlen'' is zero you may pass ``header'' as \textbf{NULL} to indicate |
956 there is no initial header data. | |
3 | 957 |
958 To encrypt or decrypt data in a streaming mode use the following. | |
15 | 959 \index{eax\_encrypt()} \index{eax\_decrypt()} |
3 | 960 \begin{verbatim} |
961 int eax_encrypt(eax_state *eax, const unsigned char *pt, | |
962 unsigned char *ct, unsigned long length); | |
963 | |
964 int eax_decrypt(eax_state *eax, const unsigned char *ct, | |
965 unsigned char *pt, unsigned long length); | |
966 \end{verbatim} | |
967 The function ``eax\_encrypt'' will encrypt the bytes in ``pt'' of ``length'' bytes and store the ciphertext in | |
968 ``ct''. Note that ``ct'' and ``pt'' may be the same region in memory. This function will also send the ciphertext | |
969 through the OMAC function. The function ``eax\_decrypt'' decrypts ``ct'' and stores it in ``pt''. This also allows | |
970 ``pt'' and ``ct'' to be the same region in memory. | |
971 | |
15 | 972 You cannot both encrypt or decrypt with the same ``eax'' context. For bi-directional communication you |
973 will need to initialize two EAX contexts (preferably with different headers and nonces). | |
974 | |
3 | 975 Note that both of these functions allow you to send the data in any granularity but the order is important. While |
976 the eax\_init() function allows you to add initial header data to the stream you can also add header data during the | |
977 EAX stream with the following. | |
978 | |
15 | 979 \index{eax\_addheader()} |
3 | 980 \begin{verbatim} |
981 int eax_addheader(eax_state *eax, | |
982 const unsigned char *header, unsigned long length); | |
983 \end{verbatim} | |
984 | |
985 This will add the ``length'' bytes from ``header'' to the given ``eax'' stream. Once the message is finished the | |
986 ``tag'' (checksum) may be computed with the following function. | |
987 | |
15 | 988 \index{eax\_done()} |
3 | 989 \begin{verbatim} |
990 int eax_done(eax_state *eax, | |
991 unsigned char *tag, unsigned long *taglen); | |
992 \end{verbatim} | |
993 This will terminate the EAX state ``eax'' and store upto ``taglen'' bytes of the message tag in ``tag''. The function | |
994 then stores how many bytes of the tag were written out back into ``taglen''. | |
995 | |
996 The EAX mode code can be tested to ensure it matches the test vectors by calling the following function. | |
15 | 997 \index{eax\_test()} |
3 | 998 \begin{verbatim} |
999 int eax_test(void); | |
1000 \end{verbatim} | |
1001 This requires that the AES (or Rijndael) block cipher be registered with the cipher\_descriptor table first. | |
1002 | |
15 | 1003 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1004 #include <tomcrypt.h> |
15 | 1005 int main(void) |
1006 { | |
1007 int err; | |
1008 eax_state eax; | |
1009 unsigned char pt[64], ct[64], nonce[16], key[16], tag[16]; | |
1010 unsigned long taglen; | |
1011 | |
1012 if (register_cipher(&rijndael_desc) == -1) { | |
1013 printf("Error registering Rijndael"); | |
1014 return EXIT_FAILURE; | |
1015 } | |
1016 | |
1017 /* ... make up random nonce and key ... */ | |
1018 | |
1019 /* initialize context */ | |
1020 if ((err = eax_init( &eax, /* the context */ | |
1021 find_cipher("rijndael"), /* cipher we want to use */ | |
1022 nonce, /* our state nonce */ | |
1023 16, /* none is 16 bytes */ | |
1024 "TestApp", /* example header, identifies this program */ | |
1025 7) /* length of the header */ | |
1026 ) != CRYPT_OK) { | |
1027 printf("Error eax_init: %s", error_to_string(err)); | |
1028 return EXIT_FAILURE; | |
1029 } | |
1030 | |
1031 /* now encrypt data, say in a loop or whatever */ | |
1032 if ((err = eax_encrypt( &eax, /* eax context */ | |
1033 pt, /* plaintext (source) */ | |
1034 ct, /* ciphertext (destination) */ | |
1035 sizeof(pt) /* size of plaintext */ | |
1036 ) != CRYPT_OK) { | |
1037 printf("Error eax_encrypt: %s", error_to_string(err)); | |
1038 return EXIT_FAILURE; | |
1039 } | |
1040 | |
1041 /* finish message and get authentication tag */ | |
1042 taglen = sizeof(tag); | |
1043 if ((err = eax_done( &eax, /* eax context */ | |
1044 tag, /* where to put tag */ | |
1045 &taglen /* length of tag space */ | |
1046 ) != CRYPT_OK) { | |
1047 printf("Error eax_done: %s", error_to_string(err)); | |
1048 return EXIT_FAILURE; | |
1049 } | |
1050 | |
1051 /* now we have the authentication tag in "tag" and it's taglen bytes long */ | |
1052 | |
1053 } | |
1054 \end{verbatim} | |
1055 | |
1056 You can also perform an entire EAX state on a block of memory in a single function call with the | |
1057 following functions. | |
1058 | |
1059 | |
1060 \index{eax\_encrypt\_authenticate\_memory} \index{eax\_decrypt\_verify\_memory} | |
1061 \begin{verbatim} | |
1062 int eax_encrypt_authenticate_memory(int cipher, | |
1063 const unsigned char *key, unsigned long keylen, | |
1064 const unsigned char *nonce, unsigned long noncelen, | |
1065 const unsigned char *header, unsigned long headerlen, | |
1066 const unsigned char *pt, unsigned long ptlen, | |
1067 unsigned char *ct, | |
1068 unsigned char *tag, unsigned long *taglen); | |
1069 | |
1070 int eax_decrypt_verify_memory(int cipher, | |
1071 const unsigned char *key, unsigned long keylen, | |
1072 const unsigned char *nonce, unsigned long noncelen, | |
1073 const unsigned char *header, unsigned long headerlen, | |
1074 const unsigned char *ct, unsigned long ctlen, | |
1075 unsigned char *pt, | |
1076 unsigned char *tag, unsigned long taglen, | |
1077 int *res); | |
1078 \end{verbatim} | |
1079 | |
1080 Both essentially just call eax\_init() followed by eax\_encrypt() (or eax\_decrypt() respectively) and eax\_done(). The parameters | |
1081 have the same meaning as with those respective functions. | |
1082 | |
1083 The only difference is eax\_decrypt\_verify\_memory() does not emit a tag. Instead you pass it a tag as input and it compares it against | |
1084 the tag it computed while decrypting the message. If the tags match then it stores a $1$ in ``res'', otherwise it stores a $0$. | |
1085 | |
3 | 1086 \subsection{OCB Mode} |
1087 LibTomCrypt provides support for a mode called OCB\footnote{See | |
1088 P. Rogaway, M. Bellare, J. Black, T. Krovetz, ``OCB: A Block Cipher Mode of Operation for Efficient Authenticated Encryption''.} | |
15 | 1089 . OCB is an encryption protocol that simultaneously provides authentication. It is slightly faster to use than EAX mode |
3 | 1090 but is less flexible. Let's review how to initialize an OCB context. |
1091 | |
15 | 1092 \index{ocb\_init()} |
3 | 1093 \begin{verbatim} |
1094 int ocb_init(ocb_state *ocb, int cipher, | |
1095 const unsigned char *key, unsigned long keylen, | |
1096 const unsigned char *nonce); | |
1097 \end{verbatim} | |
1098 | |
1099 This will initialize the ``ocb'' context using cipher descriptor ``cipher''. It will use a ``key'' of length ``keylen'' | |
1100 and the random ``nonce''. Note that ``nonce'' must be a random (public) string the same length as the block ciphers | |
15 | 1101 block size (e.g. 16 bytes for AES). |
3 | 1102 |
1103 This mode has no ``Associated Data'' like EAX mode does which means you cannot authenticate metadata along with the stream. | |
1104 To encrypt or decrypt data use the following. | |
1105 | |
15 | 1106 \index{ocb\_encrypt()} \index{ocb\_decrypt()} |
3 | 1107 \begin{verbatim} |
1108 int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct); | |
1109 int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt); | |
1110 \end{verbatim} | |
1111 | |
1112 This will encrypt (or decrypt for the latter) a fixed length of data from ``pt'' to ``ct'' (vice versa for the latter). | |
1113 They assume that ``pt'' and ``ct'' are the same size as the block cipher's block size. Note that you cannot call | |
1114 both functions given a single ``ocb'' state. For bi-directional communication you will have to initialize two ``ocb'' | |
1115 states (with different nonces). Also ``pt'' and ``ct'' may point to the same location in memory. | |
1116 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1117 \subsubsection{State Termination} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1118 |
3 | 1119 When you are finished encrypting the message you call the following function to compute the tag. |
1120 | |
15 | 1121 \index{ocb\_done\_encrypt()} |
3 | 1122 \begin{verbatim} |
1123 int ocb_done_encrypt(ocb_state *ocb, | |
1124 const unsigned char *pt, unsigned long ptlen, | |
1125 unsigned char *ct, | |
1126 unsigned char *tag, unsigned long *taglen); | |
1127 \end{verbatim} | |
1128 | |
1129 This will terminate an encrypt stream ``ocb''. If you have trailing bytes of plaintext that will not complete a block | |
1130 you can pass them here. This will also encrypt the ``ptlen'' bytes in ``pt'' and store them in ``ct''. It will also | |
1131 store upto ``taglen'' bytes of the tag into ``tag''. | |
1132 | |
1133 Note that ``ptlen'' must be less than or equal to the block size of block cipher chosen. Also note that if you have | |
1134 an input message equal to the length of the block size then you pass the data here (not to ocb\_encrypt()) only. | |
1135 | |
1136 To terminate a decrypt stream and compared the tag you call the following. | |
1137 | |
15 | 1138 \index{ocb\_done\_decrypt()} |
3 | 1139 \begin{verbatim} |
1140 int ocb_done_decrypt(ocb_state *ocb, | |
1141 const unsigned char *ct, unsigned long ctlen, | |
1142 unsigned char *pt, | |
1143 const unsigned char *tag, unsigned long taglen, | |
1144 int *res); | |
1145 \end{verbatim} | |
1146 | |
1147 Similarly to the previous function you can pass trailing message bytes into this function. This will compute the | |
1148 tag of the message (internally) and then compare it against the ``taglen'' bytes of ``tag'' provided. By default | |
1149 ``res'' is set to zero. If all ``taglen'' bytes of ``tag'' can be verified then ``res'' is set to one (authenticated | |
1150 message). | |
1151 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1152 \subsubsection{Packet Functions} |
3 | 1153 To make life simpler the following two functions are provided for memory bound OCB. |
1154 | |
15 | 1155 \index{ocb\_encrypt\_authenticate\_memory()} |
3 | 1156 \begin{verbatim} |
1157 int ocb_encrypt_authenticate_memory(int cipher, | |
1158 const unsigned char *key, unsigned long keylen, | |
1159 const unsigned char *nonce, | |
1160 const unsigned char *pt, unsigned long ptlen, | |
1161 unsigned char *ct, | |
1162 unsigned char *tag, unsigned long *taglen); | |
1163 \end{verbatim} | |
1164 | |
1165 This will OCB encrypt the message ``pt'' of length ``ptlen'' and store the ciphertext in ``ct''. The length ``ptlen'' | |
1166 can be any arbitrary length. | |
1167 | |
15 | 1168 \index{ocb\_decrypt\_verify\_memory()} |
3 | 1169 \begin{verbatim} |
1170 int ocb_decrypt_verify_memory(int cipher, | |
1171 const unsigned char *key, unsigned long keylen, | |
1172 const unsigned char *nonce, | |
1173 const unsigned char *ct, unsigned long ctlen, | |
1174 unsigned char *pt, | |
1175 const unsigned char *tag, unsigned long taglen, | |
1176 int *res); | |
1177 \end{verbatim} | |
1178 | |
1179 Similarly this will OCB decrypt and compare the internally computed tag against the tag provided. ``res'' is set | |
1180 appropriately. | |
1181 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1182 \subsection{CCM Mode} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1183 CCM is a NIST proposal for Encrypt+Authenticate that is centered around using AES (or any 16--byte cipher) as a primitive. Unlike EAX and OCB mode |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1184 it is only meant for ``packet'' mode where the length of the input is known in advance. Since it is a packet mode function CCM only has one |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1185 function that performs the protocol. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1186 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1187 \index{ccm\_memory()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1188 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1189 int ccm_memory(int cipher, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1190 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1191 const unsigned char *nonce, unsigned long noncelen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1192 const unsigned char *header, unsigned long headerlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1193 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1194 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1195 unsigned char *tag, unsigned long *taglen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1196 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1197 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1198 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1199 This performs the ``CCM'' operation on the data. The ``cipher'' variable indicates which cipher in the descriptor table to use. It must have a |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1200 16--byte block size for CCM. The key is ``key'' with a length of ``keylen'' octets. The nonce or salt is ``nonce'' of |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1201 length ``noncelen'' octets. The header is meta--data you want to send with the message but not have encrypted, it is stored in ``header'' |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1202 of length ``headerlen'' octets. The header can be zero octets long (if $headerlen = 0$ then you can pass ``header'' as \textbf{NULL}). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1203 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1204 The plaintext is stored in ``pt'' and the ciphertext in ``ct''. The length of both are expected to be equal and is passed in as ``ptlen''. It is |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1205 allowable that $pt = ct$. The ``direction'' variable indicates whether encryption (direction $=$ \textbf{CCM\_ENCRYPT}) or |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1206 decryption (direction $=$ \textbf{CCM\_DECRYPT}) is to be performed. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1207 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1208 As implemented this copy of CCM cannot handle a header or plaintext longer than $2^{32} - 1$ octets long. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1209 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1210 You can test the implementation of CCM with the following function. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1211 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1212 \index{ccm\_test()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1213 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1214 int ccm_test(void); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1215 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1216 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1217 This will return \textbf{CRYPT\_OK} if the CCM routine passes known test vectors. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1218 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1219 \subsection{GCM Mode} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1220 Galois counter mode is an IEEE proposal for authenticated encryption. Like EAX and OCB it can be used in a streaming capacity however, unlike EAX it cannot |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1221 accept ``additional authentication data'' (meta--data) after plaintext has been processed. This mode also only works with block ciphers with a sixteen |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1222 byte block. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1223 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1224 A GCM stream is meant to be processed in three modes each one sequential serial. First the initial vector (per session) data is processed. This should be |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1225 unique to every session. Next the the optional additional authentication data is processed and finally the plaintext. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1226 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1227 \subsubsection{Initialization} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1228 To initialize the GCM context with a secret key call the following function. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1229 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1230 \index{gcm\_init()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1231 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1232 int gcm_init(gcm_state *gcm, int cipher, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1233 const unsigned char *key, int keylen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1234 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1235 This initializes the GCM state ``gcm'' for the given cipher indexed by ``cipher'' with a secret key ``key'' of length ``keylen'' octets. The cipher chosen |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1236 must have a 16--byte block size (e.g. AES). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1237 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1238 \subsubsection{Initial Vector} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1239 After the state has been initialized (or reset) the next step is to add the session (or packet) initial vector. It should be unique per packet encrypted. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1240 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1241 \index{gcm\_add\_iv()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1242 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1243 int gcm_add_iv(gcm_state *gcm, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1244 const unsigned char *IV, unsigned long IVlen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1245 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1246 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1247 This adds the initial vector octets from ``IV'' of length ``IVlen'' to the GCM state ``gcm''. You can call this function as many times as required |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1248 to process the entire IV. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1249 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1250 Note that the GCM protocols provides a ``shortcut'' for 12--byte IVs where no preprocessing is to be done. If you want to minimize per packet latency it's ideal |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1251 to only use 12--byte IVs. You can just increment it like a counter for each packet and the CTR [privacy] will be ensured. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1252 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1253 \subsubsection{Additional Authentication Data} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1254 After the entire IV has been processed the additional authentication data can be processed. Unlike the IV a packet/session does not require additional |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1255 authentication data (AAD) for security. The AAD is meant to be used as side--channel data you want to be authenticated with the packet. Note that once |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1256 you begin adding AAD to the GCM state you cannot return to adding IV data until the state is reset. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1257 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1258 \index{gcm\_add\_aad()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1259 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1260 int gcm_add_aad(gcm_state *gcm, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1261 const unsigned char *adata, unsigned long adatalen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1262 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1263 This adds the additional authentication data ``adata'' of length ``adatalen'' to the GCM state ``gcm''. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1264 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1265 \subsubsection{Plaintext Processing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1266 After the AAD has been processed the plaintext (or ciphertext depending on the direction) can be processed. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1267 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1268 \index{gcm\_process()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1269 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1270 int gcm_process(gcm_state *gcm, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1271 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1272 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1273 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1274 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1275 This processes message data where ``pt'' is the plaintext and ``ct'' is the ciphertext. The length of both are equal and stored in ``ptlen''. Depending on the |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1276 mode ``pt'' is the input and ``ct'' is the output (or vice versa). When ``direction'' equals \textbf{GCM\_ENCRYPT} the plaintext is read, encrypted and stored |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1277 in the ciphertext buffer. When ``direction'' equals \textbf{GCM\_DECRYPT} the opposite occurs. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1278 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1279 \subsubsection{State Termination} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1280 To terminate a GCM state and retrieve the message authentication tag call the following function. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1281 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1282 \index{gcm\_done()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1283 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1284 int gcm_done(gcm_state *gcm, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1285 unsigned char *tag, unsigned long *taglen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1286 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1287 This terminates the GCM state ``gcm'' and stores the tag in ``tag'' of length ``taglen'' octets. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1288 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1289 \subsubsection{State Reset} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1290 The call to gcm\_init() will perform considerable pre--computation (when \textbf{GCM\_TABLES} is defined) and if you're going to be dealing with a lot of packets |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1291 it is very costly to have to call it repeatedly. To aid in this endeavour the reset function has been provided. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1292 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1293 \index{gcm\_reset()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1294 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1295 int gcm_reset(gcm_state *gcm); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1296 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1297 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1298 This will reset the GCM state ``gcm'' to the state that gcm\_init() left it. The user would then call gcm\_add\_iv(), gcm\_add\_aad(), etc. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1299 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1300 \subsubsection{One--Shot Packet} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1301 To process a single packet under any given key the following helper function can be used. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1302 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1303 \index{gcm\_memory()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1304 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1305 int gcm_memory( int cipher, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1306 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1307 const unsigned char *IV, unsigned long IVlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1308 const unsigned char *adata, unsigned long adatalen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1309 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1310 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1311 unsigned char *tag, unsigned long *taglen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1312 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1313 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1314 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1315 This will initialize the GCM state with the given key, IV and AAD value then proceed to encrypt or decrypt the message text and store the final |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1316 message tag. The definition of the variables is the same as it is for all the manual functions. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1317 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1318 If you are processing many packets under the same key you shouldn't use this function as it invokes the pre--computation with each call. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1319 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1320 \subsubsection{Example Usage} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1321 The following is an example usage of how to use GCM over multiple packets with a shared secret key. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1322 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1323 \begin{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1324 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1325 #include <tomcrypt.h> |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1326 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1327 int send_packet(const unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1328 const unsigned char *iv, unsigned long ivlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1329 const unsigned char *aad, unsigned long aadlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1330 gcm_state *gcm) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1331 { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1332 int err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1333 unsigned long taglen; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1334 unsigned char tag[16]; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1335 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1336 /* reset the state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1337 if ((err = gcm_reset(gcm)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1338 return err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1339 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1340 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1341 /* Add the IV */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1342 if ((err = gcm_add_iv(gcm, iv, ivlen)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1343 return err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1344 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1345 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1346 /* Add the AAD (note: aad can be NULL if aadlen == 0) */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1347 if ((err = gcm_add_aad(gcm, aad, aadlen)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1348 return err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1349 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1350 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1351 /* process the plaintext */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1352 if ((err = gcm_add_process(gcm, pt, ptlen, pt, GCM_ENCRYPT)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1353 return err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1354 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1355 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1356 /* Finish up and get the MAC tag */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1357 taglen = sizeof(tag); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1358 if ((err = gcm_done(gcm, tag, &taglen)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1359 return err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1360 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1361 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1362 /* depending on the protocol and how IV is generated you may have to send it too... */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1363 send(socket, iv, ivlen, 0); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1364 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1365 /* send the aad */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1366 send(socket, aad, aadlen, 0); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1367 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1368 /* send the ciphertext */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1369 send(socket, pt, ptlen, 0); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1370 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1371 /* send the tag */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1372 send(socket, tag, taglen, 0); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1373 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1374 return CRYPT_OK; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1375 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1376 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1377 int main(void) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1378 { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1379 gcm_state gcm; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1380 unsigned char key[16], IV[12], pt[PACKET_SIZE]; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1381 int err, x; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1382 unsigned long ptlen; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1383 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1384 /* somehow fill key/IV with random values */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1385 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1386 /* register AES */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1387 register_cipher(&aes_desc); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1388 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1389 /* init the GCM state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1390 if ((err = gcm_init(&gcm, find_cipher("aes"), key, 16)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1391 whine_and_pout(err); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1392 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1393 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1394 /* handle us some packets */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1395 for (;;) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1396 ptlen = make_packet_we_want_to_send(pt); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1397 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1398 /* use IV as counter (12 byte counter) */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1399 for (x = 11; x >= 0; x--) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1400 if (++IV[x]) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1401 break; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1402 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1403 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1404 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1405 if ((err = send_packet(pt, ptlen, iv, 12, NULL, 0, &gcm)) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1406 whine_and_pout(err); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1407 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1408 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1409 return EXIT_SUCCESS; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1410 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1411 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1412 \end{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1413 |
3 | 1414 \chapter{One-Way Cryptographic Hash Functions} |
1415 \section{Core Functions} | |
1416 | |
1417 Like the ciphers there are hash core functions and a universal data type to hold the hash state called ``hash\_state''. | |
1418 To initialize hash XXX (where XXX is the name) call: | |
1419 \index{Hash Functions} | |
1420 \begin{verbatim} | |
1421 void XXX_init(hash_state *md); | |
1422 \end{verbatim} | |
1423 | |
1424 This simply sets up the hash to the default state governed by the specifications of the hash. To add data to the | |
1425 message being hashed call: | |
1426 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1427 int XXX_process(hash_state *md, const unsigned char *in, unsigned long inlen); |
3 | 1428 \end{verbatim} |
1429 | |
1430 Essentially all hash messages are virtually infinitely\footnote{Most hashes are limited to $2^{64}$ bits or 2,305,843,009,213,693,952 bytes.} long message which | |
1431 are buffered. The data can be passed in any sized chunks as long as the order of the bytes are the same the message digest | |
1432 (hash output) will be the same. For example, this means that: | |
1433 \begin{verbatim} | |
1434 md5_process(&md, "hello ", 6); | |
1435 md5_process(&md, "world", 5); | |
1436 \end{verbatim} | |
1437 Will produce the same message digest as the single call: | |
1438 \index{Message Digest} | |
1439 \begin{verbatim} | |
1440 md5_process(&md, "hello world", 11); | |
1441 \end{verbatim} | |
1442 | |
1443 To finally get the message digest (the hash) call: | |
1444 \begin{verbatim} | |
1445 int XXX_done(hash_state *md, | |
1446 unsigned char *out); | |
1447 \end{verbatim} | |
1448 | |
1449 This function will finish up the hash and store the result in the ``out'' array. You must ensure that ``out'' is long | |
1450 enough for the hash in question. Often hashes are used to get keys for symmetric ciphers so the ``XXX\_done()'' functions | |
1451 will wipe the ``md'' variable before returning automatically. | |
1452 | |
1453 To test a hash function call: | |
1454 \begin{verbatim} | |
1455 int XXX_test(void); | |
1456 \end{verbatim} | |
1457 | |
1458 This will return {\bf CRYPTO\_OK} if the hash matches the test vectors, otherwise it returns an error code. An | |
1459 example snippet that hashes a message with md5 is given below. | |
1460 \begin{small} | |
1461 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1462 #include <tomcrypt.h> |
3 | 1463 int main(void) |
1464 { | |
1465 hash_state md; | |
1466 unsigned char *in = "hello world", out[16]; | |
1467 | |
1468 /* setup the hash */ | |
1469 md5_init(&md); | |
1470 | |
1471 /* add the message */ | |
1472 md5_process(&md, in, strlen(in)); | |
1473 | |
1474 /* get the hash in out[0..15] */ | |
1475 md5_done(&md, out); | |
1476 | |
1477 return 0; | |
1478 } | |
1479 \end{verbatim} | |
1480 \end{small} | |
1481 | |
1482 \section{Hash Descriptors} | |
1483 Like the set of ciphers the set of hashes have descriptors too. They are stored in an array called ``hash\_descriptor'' and | |
1484 are defined by: | |
1485 \begin{verbatim} | |
1486 struct _hash_descriptor { | |
1487 char *name; | |
1488 unsigned long hashsize; /* digest output size in bytes */ | |
1489 unsigned long blocksize; /* the block size the hash uses */ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1490 void (*init) (hash_state *hash); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1491 int (*process)(hash_state *hash, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1492 const unsigned char *in, unsigned long inlen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1493 int (*done) (hash_state *hash, unsigned char *out); |
3 | 1494 int (*test) (void); |
1495 }; | |
1496 \end{verbatim} | |
1497 | |
1498 Similarly ``name'' is the name of the hash function in ASCII (all lowercase). ``hashsize'' is the size of the digest output | |
1499 in bytes. The remaining fields are pointers to the functions that do the respective tasks. There is a function to | |
1500 search the array as well called ``int find\_hash(char *name)''. It returns -1 if the hash is not found, otherwise the | |
1501 position in the descriptor table of the hash. | |
1502 | |
1503 You can use the table to indirectly call a hash function that is chosen at runtime. For example: | |
1504 \begin{small} | |
1505 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1506 #include <tomcrypt.h> |
3 | 1507 int main(void) |
1508 { | |
1509 unsigned char buffer[100], hash[MAXBLOCKSIZE]; | |
1510 int idx, x; | |
1511 hash_state md; | |
1512 | |
1513 /* register hashes .... */ | |
1514 if (register_hash(&md5_desc) == -1) { | |
1515 printf("Error registering MD5.\n"); | |
1516 return -1; | |
1517 } | |
1518 | |
1519 /* register other hashes ... */ | |
1520 | |
1521 /* prompt for name and strip newline */ | |
1522 printf("Enter hash name: \n"); | |
1523 fgets(buffer, sizeof(buffer), stdin); | |
1524 buffer[strlen(buffer) - 1] = 0; | |
1525 | |
1526 /* get hash index */ | |
1527 idx = find_hash(buffer); | |
1528 if (idx == -1) { | |
1529 printf("Invalid hash name!\n"); | |
1530 return -1; | |
1531 } | |
1532 | |
1533 /* hash input until blank line */ | |
1534 hash_descriptor[idx].init(&md); | |
1535 while (fgets(buffer, sizeof(buffer), stdin) != NULL) | |
1536 hash_descriptor[idx].process(&md, buffer, strlen(buffer)); | |
1537 hash_descriptor[idx].done(&md, hash); | |
1538 | |
1539 /* dump to screen */ | |
1540 for (x = 0; x < hash_descriptor[idx].hashsize; x++) | |
1541 printf("%02x ", hash[x]); | |
1542 printf("\n"); | |
1543 return 0; | |
1544 } | |
1545 \end{verbatim} | |
1546 \end{small} | |
1547 | |
1548 Note the usage of ``MAXBLOCKSIZE''. In Libtomcrypt no symmetric block, key or hash digest is larger than MAXBLOCKSIZE in | |
1549 length. This provides a simple size you can set your automatic arrays to that will not get overrun. | |
1550 | |
1551 There are three helper functions as well: | |
1552 \index{hash\_memory()} \index{hash\_file()} | |
1553 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1554 int hash_memory(int hash, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1555 const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1556 unsigned char *out, unsigned long *outlen); |
3 | 1557 |
1558 int hash_file(int hash, const char *fname, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1559 unsigned char *out, unsigned long *outlen); |
3 | 1560 |
1561 int hash_filehandle(int hash, FILE *in, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1562 unsigned char *out, unsigned long *outlen); |
3 | 1563 \end{verbatim} |
1564 | |
1565 The ``hash'' parameter is the location in the descriptor table of the hash (\textit{e.g. the return of find\_hash()}). | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1566 The ``*outlen'' variable is used to keep track of the output size. You must set it to the size of your output buffer before |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1567 calling the functions. When they complete succesfully they store the length of the message digest back in it. The functions |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1568 are otherwise straightforward. The ``hash\_filehandle'' function assumes that ``in'' is an file handle opened in binary mode. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1569 It will hash to the end of file and not reset the file position when finished. |
3 | 1570 |
1571 To perform the above hash with md5 the following code could be used: | |
1572 \begin{small} | |
1573 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1574 #include <tomcrypt.h> |
3 | 1575 int main(void) |
1576 { | |
15 | 1577 int idx, err; |
3 | 1578 unsigned long len; |
1579 unsigned char out[MAXBLOCKSIZE]; | |
1580 | |
1581 /* register the hash */ | |
1582 if (register_hash(&md5_desc) == -1) { | |
1583 printf("Error registering MD5.\n"); | |
1584 return -1; | |
1585 } | |
1586 | |
1587 /* get the index of the hash */ | |
1588 idx = find_hash("md5"); | |
1589 | |
1590 /* call the hash */ | |
1591 len = sizeof(out); | |
15 | 1592 if ((err = hash_memory(idx, "hello world", 11, out, &len)) != CRYPT_OK) { |
1593 printf("Error hashing data: %s\n", error_to_string(err)); | |
3 | 1594 return -1; |
1595 } | |
1596 return 0; | |
1597 } | |
1598 \end{verbatim} | |
1599 \end{small} | |
1600 | |
1601 The following hashes are provided as of this release: | |
15 | 1602 \index{Hash descriptor table} |
3 | 1603 \begin{center} |
1604 \begin{tabular}{|c|c|c|} | |
1605 \hline Name & Descriptor Name & Size of Message Digest (bytes) \\ | |
1606 \hline WHIRLPOOL & whirlpool\_desc & 64 \\ | |
1607 \hline SHA-512 & sha512\_desc & 64 \\ | |
1608 \hline SHA-384 & sha384\_desc & 48 \\ | |
1609 \hline SHA-256 & sha256\_desc & 32 \\ | |
1610 \hline SHA-224 & sha224\_desc & 28 \\ | |
1611 \hline TIGER-192 & tiger\_desc & 24 \\ | |
1612 \hline SHA-1 & sha1\_desc & 20 \\ | |
1613 \hline RIPEMD-160 & rmd160\_desc & 20 \\ | |
1614 \hline RIPEMD-128 & rmd128\_desc & 16 \\ | |
1615 \hline MD5 & md5\_desc & 16 \\ | |
1616 \hline MD4 & md4\_desc & 16 \\ | |
1617 \hline MD2 & md2\_desc & 16 \\ | |
1618 \hline | |
1619 \end{tabular} | |
1620 \end{center} | |
1621 | |
1622 Similar to the cipher descriptor table you must register your hash algorithms before you can use them. These functions | |
1623 work exactly like those of the cipher registration code. The functions are: | |
15 | 1624 \index{register\_hash()} \index{unregister\_hash()} |
3 | 1625 \begin{verbatim} |
1626 int register_hash(const struct _hash_descriptor *hash); | |
1627 int unregister_hash(const struct _hash_descriptor *hash); | |
1628 \end{verbatim} | |
1629 | |
143 | 1630 \section{Cipher Hash Construction} |
1631 \index{Cipher Hash Construction} | |
1632 An addition to the suite of hash functions is the ``Cipher Hash Construction'' or ``CHC'' mode. In this mode | |
1633 applicable block ciphers (such as AES) can be turned into hash functions that other LTC functions can use. In | |
1634 particular this allows a cryptosystem to be designed using very few moving parts. | |
1635 | |
1636 In order to use the CHC system the developer will have to take a few extra steps. First the ``chc\_desc'' hash | |
1637 descriptor must be registered with register\_hash(). At this point the CHC hash cannot be used to hash | |
1638 data. While it is in the hash system you still have to tell the CHC code which cipher to use. This is accomplished | |
1639 via the chc\_register() function. | |
1640 | |
1641 \index{chc\_register()} | |
1642 \begin{verbatim} | |
1643 int chc_register(int cipher); | |
1644 \end{verbatim} | |
1645 | |
1646 A cipher has to be registered with CHC (and also in the cipher descriptor tables with | |
1647 register\_cipher()). The chc\_register() function will bind a cipher to the CHC system. Only one cipher can | |
1648 be bound to the CHC hash at a time. There are additional requirements for the system to work. | |
1649 | |
1650 \begin{enumerate} | |
1651 \item The cipher must have a block size greater than 64--bits. | |
1652 \item The cipher must allow an input key the size of the block size. | |
1653 \end{enumerate} | |
1654 | |
1655 Example of using CHC with the AES block cipher. | |
1656 | |
1657 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1658 #include <tomcrypt.h> |
143 | 1659 int main(void) |
1660 { | |
1661 int err; | |
1662 | |
1663 /* register cipher and hash */ | |
1664 if (register_cipher(&aes_enc_desc) == -1) { | |
1665 printf("Could not register cipher\n"); | |
1666 return EXIT_FAILURE; | |
1667 } | |
1668 if (register_hash(&chc_desc) == -1) { | |
1669 printf("Could not register hash\n"); | |
1670 return EXIT_FAILURE; | |
1671 } | |
1672 | |
1673 /* start chc with AES */ | |
1674 if ((err = chc_register(find_cipher("aes"))) != CRYPT_OK) { | |
1675 printf("Error binding AES to CHC: %s\n", error_to_string(err)); | |
1676 } | |
1677 | |
1678 /* now you can use chc_hash in any LTC function [aside from pkcs...] */ | |
1679 /* ... */ | |
1680 \end{verbatim} | |
1681 | |
1682 | |
1683 \section{Notice} | |
3 | 1684 It is highly recommended that you \textbf{not} use the MD4 or MD5 hashes for the purposes of digital signatures or authentication codes. |
1685 These hashes are provided for completeness and they still can be used for the purposes of password hashing or one-way accumulators | |
1686 (e.g. Yarrow). | |
1687 | |
1688 The other hashes such as the SHA-1, SHA-2 (that includes SHA-512, SHA-384 and SHA-256) and TIGER-192 are still considered secure | |
1689 for all purposes you would normally use a hash for. | |
1690 | |
1691 \chapter{Message Authentication Codes} | |
1692 \section{HMAC Protocol} | |
1693 Thanks to Dobes Vandermeer the library now includes support for hash based message authenication codes or HMAC for short. An HMAC | |
1694 of a message is a keyed authenication code that only the owner of a private symmetric key will be able to verify. The purpose is | |
1695 to allow an owner of a private symmetric key to produce an HMAC on a message then later verify if it is correct. Any impostor or | |
1696 eavesdropper will not be able to verify the authenticity of a message. | |
1697 | |
1698 The HMAC support works much like the normal hash functions except that the initialization routine requires you to pass a key | |
1699 and its length. The key is much like a key you would pass to a cipher. That is, it is simply an array of octets stored in | |
1700 chars. The initialization routine is: | |
15 | 1701 \index{hmac\_init()} |
3 | 1702 \begin{verbatim} |
1703 int hmac_init(hmac_state *hmac, int hash, | |
1704 const unsigned char *key, unsigned long keylen); | |
1705 \end{verbatim} | |
1706 The ``hmac'' parameter is the state for the HMAC code. ``hash'' is the index into the descriptor table of the hash you want | |
1707 to use to authenticate the message. ``key'' is the pointer to the array of chars that make up the key. ``keylen'' is the | |
1708 length (in octets) of the key you want to use to authenticate the message. To send octets of a message through the HMAC system you must use the following function: | |
15 | 1709 \index{hmac\_process()} |
3 | 1710 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1711 int hmac_process(hmac_state *hmac, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1712 const unsigned char *in, unsigned long inlen); |
3 | 1713 \end{verbatim} |
1714 ``hmac'' is the HMAC state you are working with. ``buf'' is the array of octets to send into the HMAC process. ``len'' is the | |
1715 number of octets to process. Like the hash process routines you can send the data in arbitrarly sized chunks. When you | |
1716 are finished with the HMAC process you must call the following function to get the HMAC code: | |
15 | 1717 \index{hmac\_done()} |
3 | 1718 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1719 int hmac_done(hmac_state *hmac, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1720 unsigned char *out, unsigned long *outlen); |
3 | 1721 \end{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1722 ``hmac'' is the HMAC state you are working with. ``out'' is the array of octets where the HMAC code should be stored. You must |
3 | 1723 set ``outlen'' to the size of the destination buffer before calling this function. It is updated with the length of the HMAC code |
1724 produced (depending on which hash was picked). If ``outlen'' is less than the size of the message digest (and ultimately | |
1725 the HMAC code) then the HMAC code is truncated as per FIPS-198 specifications (e.g. take the first ``outlen'' bytes). | |
1726 | |
1727 There are two utility functions provided to make using HMACs easier todo. They accept the key and information about the | |
1728 message (file pointer, address in memory) and produce the HMAC result in one shot. These are useful if you want to avoid | |
1729 calling the three step process yourself. | |
1730 | |
15 | 1731 \index{hmac\_memory()} |
3 | 1732 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1733 int hmac_memory(int hash, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1734 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1735 const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1736 unsigned char *out, unsigned long *outlen); |
3 | 1737 \end{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1738 This will produce an HMAC code for the array of octets in ``in'' of length ``inlen''. The index into the hash descriptor |
3 | 1739 table must be provided in ``hash''. It uses the key from ``key'' with a key length of ``keylen''. |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1740 The result is stored in the array of octets ``out'' and the length in ``outlen''. The value of ``outlen'' must be set |
3 | 1741 to the size of the destination buffer before calling this function. Similarly for files there is the following function: |
15 | 1742 \index{hmac\_file()} |
3 | 1743 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1744 int hmac_file(int hash, const char *fname, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1745 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1746 unsigned char *out, unsigned long *outlen); |
3 | 1747 \end{verbatim} |
1748 ``hash'' is the index into the hash descriptor table of the hash you want to use. ``fname'' is the filename to process. | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1749 ``key'' is the array of octets to use as the key of length ``keylen''. ``out'' is the array of octets where the |
3 | 1750 result should be stored. |
1751 | |
1752 To test if the HMAC code is working there is the following function: | |
15 | 1753 \index{hmac\_test()} |
3 | 1754 \begin{verbatim} |
1755 int hmac_test(void); | |
1756 \end{verbatim} | |
1757 Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code. Some example code for using the | |
1758 HMAC system is given below. | |
1759 | |
1760 \begin{small} | |
1761 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1762 #include <tomcrypt.h> |
3 | 1763 int main(void) |
1764 { | |
15 | 1765 int idx, err; |
3 | 1766 hmac_state hmac; |
1767 unsigned char key[16], dst[MAXBLOCKSIZE]; | |
1768 unsigned long dstlen; | |
1769 | |
1770 /* register SHA-1 */ | |
1771 if (register_hash(&sha1_desc) == -1) { | |
1772 printf("Error registering SHA1\n"); | |
1773 return -1; | |
1774 } | |
1775 | |
1776 /* get index of SHA1 in hash descriptor table */ | |
1777 idx = find_hash("sha1"); | |
1778 | |
1779 /* we would make up our symmetric key in "key[]" here */ | |
1780 | |
1781 /* start the HMAC */ | |
15 | 1782 if ((err = hmac_init(&hmac, idx, key, 16)) != CRYPT_OK) { |
1783 printf("Error setting up hmac: %s\n", error_to_string(err)); | |
3 | 1784 return -1; |
1785 } | |
1786 | |
1787 /* process a few octets */ | |
15 | 1788 if((err = hmac_process(&hmac, "hello", 5) != CRYPT_OK) { |
1789 printf("Error processing hmac: %s\n", error_to_string(err)); | |
3 | 1790 return -1; |
1791 } | |
1792 | |
1793 /* get result (presumably to use it somehow...) */ | |
1794 dstlen = sizeof(dst); | |
15 | 1795 if ((err = hmac_done(&hmac, dst, &dstlen)) != CRYPT_OK) { |
1796 printf("Error finishing hmac: %s\n", error_to_string(err)); | |
3 | 1797 return -1; |
1798 } | |
1799 printf("The hmac is %lu bytes long\n", dstlen); | |
1800 | |
1801 /* return */ | |
1802 return 0; | |
1803 } | |
1804 \end{verbatim} | |
1805 \end{small} | |
1806 | |
1807 \section{OMAC Support} | |
1808 OMAC\footnote{\url{http://crypt.cis.ibaraki.ac.jp/omac/omac.html}}, which stands for \textit{One-Key CBC MAC} is an | |
1809 algorithm which produces a Message Authentication Code (MAC) using only a block cipher such as AES. From an API | |
1810 standpoint the OMAC routines work much like the HMAC routines do. Instead in this case a cipher is used instead of a hash. | |
1811 | |
1812 To start an OMAC state you call | |
15 | 1813 \index{omac\_init()} |
3 | 1814 \begin{verbatim} |
1815 int omac_init(omac_state *omac, int cipher, | |
1816 const unsigned char *key, unsigned long keylen); | |
1817 \end{verbatim} | |
1818 The ``omac'' variable is the state for the OMAC algorithm. ``cipher'' is the index into the cipher\_descriptor table | |
1819 of the cipher\footnote{The cipher must have a 64 or 128 bit block size. Such as CAST5, Blowfish, DES, AES, Twofish, etc.} you | |
1820 wish to use. ``key'' and ``keylen'' are the keys used to authenticate the data. | |
1821 | |
1822 To send data through the algorithm call | |
15 | 1823 \index{omac\_process()} |
3 | 1824 \begin{verbatim} |
1825 int omac_process(omac_state *state, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1826 const unsigned char *in, unsigned long inlen); |
3 | 1827 \end{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1828 This will send ``inlen'' bytes from ``in'' through the active OMAC state ``state''. Returns \textbf{CRYPT\_OK} if the |
3 | 1829 function succeeds. The function is not sensitive to the granularity of the data. For example, |
1830 | |
1831 \begin{verbatim} | |
1832 omac_process(&mystate, "hello", 5); | |
1833 omac_process(&mystate, " world", 6); | |
1834 \end{verbatim} | |
1835 | |
1836 Would produce the same result as, | |
1837 | |
1838 \begin{verbatim} | |
1839 omac_process(&mystate, "hello world", 11); | |
1840 \end{verbatim} | |
1841 | |
1842 When you are done processing the message you can call the following to compute the message tag. | |
1843 | |
15 | 1844 \index{omac\_done()} |
3 | 1845 \begin{verbatim} |
1846 int omac_done(omac_state *state, | |
1847 unsigned char *out, unsigned long *outlen); | |
1848 \end{verbatim} | |
1849 Which will terminate the OMAC and output the \textit{tag} (MAC) to ``out''. Note that unlike the HMAC and other code | |
1850 ``outlen'' can be smaller than the default MAC size (for instance AES would make a 16-byte tag). Part of the OMAC | |
1851 specification states that the output may be truncated. So if you pass in $outlen = 5$ and use AES as your cipher than | |
1852 the output MAC code will only be five bytes long. If ``outlen'' is larger than the default size it is set to the default | |
1853 size to show how many bytes were actually used. | |
1854 | |
1855 Similar to the HMAC code the file and memory functions are also provided. To OMAC a buffer of memory in one shot use the | |
1856 following function. | |
1857 | |
15 | 1858 \index{omac\_memory()} |
3 | 1859 \begin{verbatim} |
1860 int omac_memory(int cipher, | |
1861 const unsigned char *key, unsigned long keylen, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1862 const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1863 unsigned char *out, unsigned long *outlen); |
3 | 1864 \end{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1865 This will compute the OMAC of ``inlen'' bytes of ``in'' using the key ``key'' of length ``keylen'' bytes and the cipher |
3 | 1866 specified by the ``cipher'''th entry in the cipher\_descriptor table. It will store the MAC in ``out'' with the same |
1867 rules as omac\_done. | |
1868 | |
1869 To OMAC a file use | |
15 | 1870 \index{omac\_file()} |
3 | 1871 \begin{verbatim} |
1872 int omac_file(int cipher, | |
1873 const unsigned char *key, unsigned long keylen, | |
1874 const char *filename, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1875 unsigned char *out, unsigned long *outlen); |
3 | 1876 \end{verbatim} |
1877 | |
1878 Which will OMAC the entire contents of the file specified by ``filename'' using the key ``key'' of length ``keylen'' bytes | |
1879 and the cipher specified by the ``cipher'''th entry in the cipher\_descriptor table. It will store the MAC in ``out'' with | |
1880 the same rules as omac\_done. | |
1881 | |
1882 To test if the OMAC code is working there is the following function: | |
15 | 1883 \index{omac\_test()} |
3 | 1884 \begin{verbatim} |
1885 int omac_test(void); | |
1886 \end{verbatim} | |
1887 Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code. Some example code for using the | |
1888 OMAC system is given below. | |
1889 | |
1890 \begin{small} | |
1891 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1892 #include <tomcrypt.h> |
3 | 1893 int main(void) |
1894 { | |
1895 int idx, err; | |
1896 omac_state omac; | |
1897 unsigned char key[16], dst[MAXBLOCKSIZE]; | |
1898 unsigned long dstlen; | |
1899 | |
1900 /* register Rijndael */ | |
1901 if (register_cipher(&rijndael_desc) == -1) { | |
1902 printf("Error registering Rijndael\n"); | |
1903 return -1; | |
1904 } | |
1905 | |
1906 /* get index of Rijndael in cipher descriptor table */ | |
1907 idx = find_cipher("rijndael"); | |
1908 | |
1909 /* we would make up our symmetric key in "key[]" here */ | |
1910 | |
1911 /* start the OMAC */ | |
1912 if ((err = omac_init(&omac, idx, key, 16)) != CRYPT_OK) { | |
1913 printf("Error setting up omac: %s\n", error_to_string(err)); | |
1914 return -1; | |
1915 } | |
1916 | |
1917 /* process a few octets */ | |
1918 if((err = omac_process(&omac, "hello", 5) != CRYPT_OK) { | |
1919 printf("Error processing omac: %s\n", error_to_string(err)); | |
1920 return -1; | |
1921 } | |
1922 | |
1923 /* get result (presumably to use it somehow...) */ | |
1924 dstlen = sizeof(dst); | |
1925 if ((err = omac_done(&omac, dst, &dstlen)) != CRYPT_OK) { | |
1926 printf("Error finishing omac: %s\n", error_to_string(err)); | |
1927 return -1; | |
1928 } | |
1929 printf("The omac is %lu bytes long\n", dstlen); | |
1930 | |
1931 /* return */ | |
1932 return 0; | |
1933 } | |
1934 \end{verbatim} | |
1935 \end{small} | |
1936 | |
1937 \section{PMAC Support} | |
1938 The PMAC\footnote{J.Black, P.Rogaway, ``A Block--Cipher Mode of Operation for Parallelizable Message Authentication''} | |
1939 protocol is another MAC algorithm that relies solely on a symmetric-key block cipher. It uses essentially the same | |
1940 API as the provided OMAC code. | |
1941 | |
1942 A PMAC state is initialized with the following. | |
1943 | |
15 | 1944 \index{pmac\_init()} |
3 | 1945 \begin{verbatim} |
1946 int pmac_init(pmac_state *pmac, int cipher, | |
1947 const unsigned char *key, unsigned long keylen); | |
1948 \end{verbatim} | |
1949 Which initializes the ``pmac'' state with the given ``cipher'' and ``key'' of length ``keylen'' bytes. The chosen cipher | |
1950 must have a 64 or 128 bit block size (e.x. AES). | |
1951 | |
1952 To MAC data simply send it through the process function. | |
1953 | |
15 | 1954 \index{pmac\_process()} |
3 | 1955 \begin{verbatim} |
1956 int pmac_process(pmac_state *state, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1957 const unsigned char *in, unsigned long inlen); |
3 | 1958 \end{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1959 This will process ``inlen'' bytes of ``in'' in the given ``state''. The function is not sensitive to the granularity of the |
3 | 1960 data. For example, |
1961 | |
1962 \begin{verbatim} | |
1963 pmac_process(&mystate, "hello", 5); | |
1964 pmac_process(&mystate, " world", 6); | |
1965 \end{verbatim} | |
1966 | |
1967 Would produce the same result as, | |
1968 | |
1969 \begin{verbatim} | |
1970 pmac_process(&mystate, "hello world", 11); | |
1971 \end{verbatim} | |
1972 | |
1973 When a complete message has been processed the following function can be called to compute the message tag. | |
1974 | |
15 | 1975 \index{pmac\_done()} |
3 | 1976 \begin{verbatim} |
1977 int pmac_done(pmac_state *state, | |
1978 unsigned char *out, unsigned long *outlen); | |
1979 \end{verbatim} | |
1980 This will store upto ``outlen'' bytes of the tag for the given ``state'' into ``out''. Note that if ``outlen'' is larger | |
1981 than the size of the tag it is set to the amount of bytes stored in ``out''. | |
1982 | |
1983 Similar to the PMAC code the file and memory functions are also provided. To PMAC a buffer of memory in one shot use the | |
1984 following function. | |
1985 | |
15 | 1986 \index{pmac\_memory()} |
3 | 1987 \begin{verbatim} |
1988 int pmac_memory(int cipher, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1989 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1990 const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
1991 unsigned char *out, unsigned long *outlen); |
3 | 1992 \end{verbatim} |
1993 This will compute the PMAC of ``msglen'' bytes of ``msg'' using the key ``key'' of length ``keylen'' bytes and the cipher | |
1994 specified by the ``cipher'''th entry in the cipher\_descriptor table. It will store the MAC in ``out'' with the same | |
1995 rules as omac\_done. | |
1996 | |
1997 To PMAC a file use | |
15 | 1998 \index{pmac\_file()} |
3 | 1999 \begin{verbatim} |
2000 int pmac_file(int cipher, | |
2001 const unsigned char *key, unsigned long keylen, | |
2002 const char *filename, | |
2003 unsigned char *out, unsigned long *outlen); | |
2004 \end{verbatim} | |
2005 | |
2006 Which will PMAC the entire contents of the file specified by ``filename'' using the key ``key'' of length ``keylen'' bytes | |
2007 and the cipher specified by the ``cipher'''th entry in the cipher\_descriptor table. It will store the MAC in ``out'' with | |
2008 the same rules as omac\_done. | |
2009 | |
2010 To test if the PMAC code is working there is the following function: | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2011 \index{pmac\_test()} |
3 | 2012 \begin{verbatim} |
2013 int pmac_test(void); | |
2014 \end{verbatim} | |
2015 Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code. | |
2016 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2017 \section{Pelican MAC} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2018 Pelican MAC is a new (experimental) MAC by the AES team that uses four rounds of AES as a ``mixing function''. It achieves a very high |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2019 rate of processing and is potentially very secure. It requires AES to be enabled to function. You do not have to register\_cipher() AES first though |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2020 as it calls AES directly. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2021 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2022 \index{pelican\_init()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2023 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2024 int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2025 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2026 This will initialize the Pelican state with the given AES key. Once this has been done you can begin processing data. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2027 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2028 \index{pelican\_process()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2029 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2030 int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2031 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2032 This will process ``inlen'' bytes of ``in'' through the Pelican MAC. It's best that you pass in multiples of 16 bytes as it makes the |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2033 routine more efficient but you may pass in any length of text. You can call this function as many times as required to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2034 an entire message. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2035 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2036 \index{pelican\_done()} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2037 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2038 int pelican_done(pelican_state *pelmac, unsigned char *out); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2039 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2040 This terminates a Pelican MAC and writes the 16--octet tag to ``out''. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2041 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2042 \subsection{Example} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2043 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2044 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2045 #include <tomcrypt.h> |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2046 int main(void) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2047 { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2048 pelican_state pelstate; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2049 unsigned char key[32], tag[16]; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2050 int err; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2051 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2052 /* somehow initialize a key */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2053 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2054 /* initialize pelican mac */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2055 if ((err = pelican_init(&pelstate, /* the state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2056 key, /* user key */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2057 32 /* key length in octets */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2058 )) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2059 printf("Error initializing Pelican: %s", error_to_string(err)); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2060 return EXIT_FAILURE; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2061 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2062 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2063 /* MAC some data */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2064 if ((err = pelican_process(&pelstate, /* the state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2065 "hello world", /* data to mac */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2066 11 /* length of data */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2067 )) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2068 printf("Error processing Pelican: %s", error_to_string(err)); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2069 return EXIT_FAILURE; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2070 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2071 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2072 /* Terminate the MAC */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2073 if ((err = pelican_done(&pelstate, /* the state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2074 tag /* where to store the tag */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2075 )) != CRYPT_OK) { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2076 printf("Error terminating Pelican: %s", error_to_string(err)); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2077 return EXIT_FAILURE; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2078 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2079 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2080 /* tag[0..15] has the MAC output now */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2081 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2082 return EXIT_SUCCESS; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2083 } |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2084 \end{verbatim} |
143 | 2085 |
2086 | |
3 | 2087 \chapter{Pseudo-Random Number Generators} |
2088 \section{Core Functions} | |
2089 The library provides an array of core functions for Pseudo-Random Number Generators (PRNGs) as well. A cryptographic PRNG is | |
2090 used to expand a shorter bit string into a longer bit string. PRNGs are used wherever random data is required such as Public Key (PK) | |
2091 key generation. There is a universal structure called ``prng\_state''. To initialize a PRNG call: | |
143 | 2092 \index{PRNG start} |
3 | 2093 \begin{verbatim} |
2094 int XXX_start(prng_state *prng); | |
2095 \end{verbatim} | |
2096 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2097 This will setup the PRNG for future use and not seed it. In order for the PRNG to be cryptographically useful you must give it |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2098 entropy. Ideally you'd have some OS level source to tap like in UNIX. To add entropy to the PRNG call: |
143 | 2099 \index{PRNG add\_entropy} |
3 | 2100 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2101 int XXX_add_entropy(const unsigned char *in, unsigned long inlen, |
3 | 2102 prng_state *prng); |
2103 \end{verbatim} | |
2104 | |
2105 Which returns {\bf CRYPTO\_OK} if the entropy was accepted. Once you think you have enough entropy you call another | |
2106 function to put the entropy into action. | |
143 | 2107 \index{PRNG ready} |
3 | 2108 \begin{verbatim} |
2109 int XXX_ready(prng_state *prng); | |
2110 \end{verbatim} | |
2111 | |
2112 Which returns {\bf CRYPTO\_OK} if it is ready. Finally to actually read bytes call: | |
143 | 2113 \index{PRNG read} |
3 | 2114 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2115 unsigned long XXX_read(unsigned char *out, unsigned long outlen, |
3 | 2116 prng_state *prng); |
2117 \end{verbatim} | |
2118 | |
143 | 2119 Which returns the number of bytes read from the PRNG. When you are finished with a PRNG state you call |
2120 the following. | |
2121 | |
2122 \index{PRNG done} | |
2123 \begin{verbatim} | |
2124 void XXX_done(prng_state *prng); | |
2125 \end{verbatim} | |
2126 | |
2127 This will terminate a PRNG state and free any memory (if any) allocated. To export a PRNG state | |
2128 so that you can later resume the PRNG call the following. | |
2129 | |
2130 \index{PRNG export} | |
2131 \begin{verbatim} | |
2132 int XXX_export(unsigned char *out, unsigned long *outlen, | |
2133 prng_state *prng); | |
2134 \end{verbatim} | |
2135 | |
2136 This will write a ``PRNG state'' to the buffer ``out'' of length ``outlen'' bytes. The idea of | |
2137 the export is meant to be used as a ``seed file''. That is, when the program starts up there will not likely | |
2138 be that much entropy available. To import a state to seed a PRNG call the following function. | |
2139 | |
2140 \index{PRNG import} | |
2141 \begin{verbatim} | |
2142 int XXX_import(const unsigned char *in, unsigned long inlen, | |
2143 prng_state *prng); | |
2144 \end{verbatim} | |
2145 | |
2146 This will call the start and add\_entropy functions of the given PRNG. It will use the state in | |
2147 ``in'' of length ``inlen'' as the initial seed. You must pass the same seed length as was exported | |
2148 by the corresponding export function. | |
2149 | |
2150 Note that importing a state will not ``resume'' the PRNG from where it left off. That is, if you export | |
2151 a state, emit (say) 8 bytes and then import the previously exported state the next 8 bytes will not | |
2152 specifically equal the 8 bytes you generated previously. | |
2153 | |
2154 When a program is first executed the normal course of operation is | |
2155 | |
2156 \begin{enumerate} | |
2157 \item Gather entropy from your sources for a given period of time or number of events. | |
2158 \item Start, use your entropy via add\_entropy and ready the PRNG yourself. | |
2159 \end{enumerate} | |
2160 | |
2161 When your program is finished you simply call the export function and save the state to a medium (disk, | |
2162 flash memory, etc). The next time your application starts up you can detect the state, feed it to the | |
2163 import function and go on your way. It is ideal that (as soon as possible) after startup you export a | |
2164 fresh state. This helps in the case that the program aborts or the machine is powered down without | |
2165 being given a chance to exit properly. | |
2166 | |
2167 Note that even if you have a state to import it is important to add new entropy to the state. However, | |
2168 there is less pressure to do so. | |
2169 | |
2170 To test a PRNG for operational conformity call the following functions. | |
2171 | |
2172 \index{PRNG test} | |
2173 \begin{verbatim} | |
2174 int XXX_test(void); | |
2175 \end{verbatim} | |
2176 | |
2177 This will return \textbf{CRYPT\_OK} if PRNG is operating properly. | |
3 | 2178 |
2179 \subsection{Remarks} | |
2180 | |
2181 It is possible to be adding entropy and reading from a PRNG at the same time. For example, if you first seed the PRNG | |
2182 and call ready() you can now read from it. You can also keep adding new entropy to it. The new entropy will not be used | |
2183 in the PRNG until ready() is called again. This allows the PRNG to be used and re-seeded at the same time. No real error | |
2184 checking is guaranteed to see if the entropy is sufficient or if the PRNG is even in a ready state before reading. | |
2185 | |
2186 \subsection{Example} | |
2187 | |
143 | 2188 Below is a simple snippet to read 10 bytes from yarrow. Its important to note that this snippet is |
2189 {\bf NOT} secure since the entropy added is not random. | |
3 | 2190 |
2191 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2192 #include <tomcrypt.h> |
3 | 2193 int main(void) |
2194 { | |
2195 prng_state prng; | |
2196 unsigned char buf[10]; | |
2197 int err; | |
2198 | |
2199 /* start it */ | |
2200 if ((err = yarrow_start(&prng)) != CRYPT_OK) { | |
2201 printf("Start error: %s\n", error_to_string(err)); | |
2202 } | |
2203 /* add entropy */ | |
2204 if ((err = yarrow_add_entropy("hello world", 11, &prng)) != CRYPT_OK) { | |
2205 printf("Add_entropy error: %s\n", error_to_string(err)); | |
2206 } | |
2207 /* ready and read */ | |
2208 if ((err = yarrow_ready(&prng)) != CRYPT_OK) { | |
2209 printf("Ready error: %s\n", error_to_string(err)); | |
2210 } | |
2211 printf("Read %lu bytes from yarrow\n", yarrow_read(buf, 10, &prng)); | |
2212 return 0; | |
2213 } | |
2214 \end{verbatim} | |
2215 | |
2216 \section{PRNG Descriptors} | |
2217 \index{PRNG Descriptor} | |
2218 PRNGs have descriptors too (surprised?). Stored in the structure ``prng\_descriptor''. The format of an element is: | |
2219 \begin{verbatim} | |
2220 struct _prng_descriptor { | |
2221 char *name; | |
143 | 2222 int export_size; /* size in bytes of exported state */ |
3 | 2223 int (*start) (prng_state *); |
2224 int (*add_entropy)(const unsigned char *, unsigned long, prng_state *); | |
2225 int (*ready) (prng_state *); | |
2226 unsigned long (*read)(unsigned char *, unsigned long len, prng_state *); | |
143 | 2227 void (*done)(prng_state *); |
2228 int (*export)(unsigned char *, unsigned long *, prng_state *); | |
2229 int (*import)(const unsigned char *, unsigned long, prng_state *); | |
2230 int (*test)(void); | |
3 | 2231 }; |
2232 \end{verbatim} | |
2233 | |
2234 There is a ``int find\_prng(char *name)'' function as well. Returns -1 if the PRNG is not found, otherwise it returns | |
2235 the position in the prng\_descriptor array. | |
2236 | |
2237 Just like the ciphers and hashes you must register your prng before you can use it. The two functions provided work | |
2238 exactly as those for the cipher registry functions. They are: | |
2239 \begin{verbatim} | |
2240 int register_prng(const struct _prng_descriptor *prng); | |
2241 int unregister_prng(const struct _prng_descriptor *prng); | |
2242 \end{verbatim} | |
2243 | |
143 | 2244 \subsection{PRNGs Provided} |
2245 \begin{figure}[here] | |
2246 \begin{center} | |
2247 \begin{small} | |
2248 \begin{tabular}{|c|c|l|} | |
2249 \hline \textbf{Name} & \textbf{Descriptor} & \textbf{Usage} \\ | |
2250 \hline Yarrow & yarrow\_desc & Fast short-term PRNG \\ | |
2251 \hline Fortuna & fortuna\_desc & Fast long-term PRNG (recommended) \\ | |
2252 \hline RC4 & rc4\_desc & Stream Cipher \\ | |
2253 \hline SOBER-128 & sober128\_desc & Stream Cipher (also very fast PRNG) \\ | |
2254 \hline | |
2255 \end{tabular} | |
2256 \end{small} | |
2257 \end{center} | |
2258 \caption{List of Provided PRNGs} | |
2259 \end{figure} | |
2260 | |
2261 \subsubsection{Yarrow} | |
2262 Yarrow is fast PRNG meant to collect an unspecified amount of entropy from sources | |
2263 (keyboard, mouse, interrupts, etc) and produce an unbounded string of random bytes. | |
2264 | |
2265 \textit{Note:} This PRNG is still secure for most taskings but is no longer recommended. Users | |
2266 should use Fortuna instead. | |
2267 | |
2268 \subsubsection{Fortuna} | |
2269 | |
2270 Fortuna is a fast attack tolerant and more thoroughly designed PRNG suitable for long term | |
2271 usage. It is faster than the default implementation of Yarrow\footnote{Yarrow has been implemented | |
2272 to work with most cipher and hash combos based on which you have chosen to build into the library.} while | |
2273 providing more security. | |
2274 | |
2275 Fortuna is slightly less flexible than Yarrow in the sense that it only works with the AES block cipher | |
2276 and SHA--256 hash function. Technically Fortuna will work with any block cipher that accepts a 256--bit | |
2277 key and any hash that produces at least a 256--bit output. However, to make the implementation simpler | |
2278 it has been fixed to those choices. | |
2279 | |
2280 Fortuna is more secure than Yarrow in the sense that attackers who learn parts of the entropy being | |
2281 added to the PRNG learn far less about the state than that of Yarrow. Without getting into to many | |
2282 details Fortuna has the ability to recover from state determination attacks where the attacker starts | |
2283 to learn information from the PRNGs output about the internal state. Yarrow on the other hand cannot | |
2284 recover from that problem until new entropy is added to the pool and put to use through the ready() function. | |
2285 | |
2286 \subsubsection{RC4} | |
2287 | |
2288 RC4 is an old stream cipher that can also double duty as a PRNG in a pinch. You ``key'' it by | |
2289 calling add\_entropy() and setup the key by calling ready(). You can only add upto 256 bytes via | |
2290 add\_entropy(). | |
2291 | |
2292 When you read from RC4 the output of the RC4 algorithm is XOR'd against your buffer you provide. In this | |
2293 manner you can use rc4\_read() as an encrypt (and decrypt) function. | |
2294 | |
2295 You really shouldn't use RC4 anymore. This isn't because RC4 is weak (though biases are known to exist) just | |
2296 simply that faster alternatives exist. | |
2297 | |
2298 \subsubsection{SOBER-128} | |
2299 | |
2300 SOBER-128 is a stream cipher designed by the QUALCOMM Australia team. Like RC4 you ``key'' it by | |
2301 calling add\_entropy(). There is no need to call ready() for this PRNG as it does not do anything. | |
2302 | |
2303 Note that this cipher has several oddities about how it operates. The first time you call | |
2304 add\_entropy() that sets the cipher's key. Every other time you call the same function it sets | |
2305 the cipher's IV variable. The IV mechanism allows you to encrypt several messages with the same | |
2306 key and not re--use the same key material. | |
2307 | |
2308 Unlike Yarrow and Fortuna all of the entropy (and hence security) of this algorithm rests in the data | |
2309 you pass it on the first call to add\_entropy(). All buffers sent to add\_entropy() must have a length | |
2310 that is a multiple of four bytes. | |
2311 | |
2312 Like RC4 the output of SOBER--128 is XOR'ed against the buffer you provide it. In this manner you can use | |
2313 sober128\_read() as an encrypt (and decrypt) function. | |
2314 | |
2315 Since SOBER-128 has a fixed keying scheme and is very fast (faster than RC4) the ideal usage of SOBER-128 is to | |
2316 key it from the output of Fortuna (or Yarrow) and use it to encrypt messages. It is also ideal for | |
2317 simulations which need a high quality (and fast) stream of bytes. | |
2318 | |
2319 \subsubsection{Example Usage} | |
3 | 2320 \begin{small} |
2321 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2322 #include <tomcrypt.h> |
3 | 2323 int main(void) |
2324 { | |
2325 prng_state prng; | |
2326 unsigned char buf[32]; | |
2327 int err; | |
2328 | |
2329 if ((err = rc4_start(&prng)) != CRYPT_OK) { | |
2330 printf("RC4 init error: %s\n", error_to_string(err)); | |
2331 exit(-1); | |
2332 } | |
2333 | |
2334 /* use ``key'' as the key */ | |
2335 if ((err = rc4_add_entropy("key", 3, &prng)) != CRYPT_OK) { | |
2336 printf("RC4 add entropy error: %s\n", error_to_string(err)); | |
2337 exit(-1); | |
2338 } | |
2339 | |
2340 /* setup RC4 for use */ | |
2341 if ((err = rc4_ready(&prng)) != CRYPT_OK) { | |
2342 printf("RC4 ready error: %s\n", error_to_string(err)); | |
2343 exit(-1); | |
2344 } | |
2345 | |
2346 /* encrypt buffer */ | |
2347 strcpy(buf,"hello world"); | |
2348 if (rc4_read(buf, 11, &prng) != 11) { | |
2349 printf("RC4 read error\n"); | |
2350 exit(-1); | |
2351 } | |
2352 return 0; | |
2353 } | |
2354 \end{verbatim} | |
2355 \end{small} | |
2356 To decrypt you have to do the exact same steps. | |
2357 | |
2358 \section{The Secure RNG} | |
2359 \index{Secure RNG} | |
2360 An RNG is related to a PRNG except that it doesn't expand a smaller seed to get the data. They generate their random bits | |
2361 by performing some computation on fresh input bits. Possibly the hardest thing to get correctly in a cryptosystem is the | |
2362 PRNG. Computers are deterministic beasts that try hard not to stray from pre-determined paths. That makes gathering | |
2363 entropy needed to seed the PRNG a hard task. | |
2364 | |
2365 There is one small function that may help on certain platforms: | |
2366 \index{rng\_get\_bytes()} | |
2367 \begin{verbatim} | |
2368 unsigned long rng_get_bytes(unsigned char *buf, unsigned long len, | |
2369 void (*callback)(void)); | |
2370 \end{verbatim} | |
2371 | |
2372 Which will try one of three methods of getting random data. The first is to open the popular ``/dev/random'' device which | |
2373 on most *NIX platforms provides cryptographic random bits\footnote{This device is available in Windows through the Cygwin compiler suite. It emulates ``/dev/random'' via the Microsoft CSP.}. | |
2374 The second method is to try the Microsoft Cryptographic Service Provider and read the RNG. The third method is an ANSI C | |
2375 clock drift method that is also somewhat popular but gives bits of lower entropy. The ``callback'' parameter is a pointer to a function that returns void. Its used when the slower ANSI C RNG must be | |
2376 used so the calling application can still work. This is useful since the ANSI C RNG has a throughput of three | |
2377 bytes a second. The callback pointer may be set to {\bf NULL} to avoid using it if you don't want to. The function | |
2378 returns the number of bytes actually read from any RNG source. There is a function to help setup a PRNG as well: | |
2379 \index{rng\_make\_prng()} | |
2380 \begin{verbatim} | |
2381 int rng_make_prng(int bits, int wprng, prng_state *prng, | |
2382 void (*callback)(void)); | |
2383 \end{verbatim} | |
2384 This will try to setup the prng with a state of at least ``bits'' of entropy. The ``callback'' parameter works much like | |
2385 the callback in ``rng\_get\_bytes()''. It is highly recommended that you use this function to setup your PRNGs unless you have a | |
2386 platform where the RNG doesn't work well. Example usage of this function is given below. | |
2387 | |
2388 \begin{small} | |
2389 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2390 #include <tomcrypt.h> |
3 | 2391 int main(void) |
2392 { | |
2393 ecc_key mykey; | |
2394 prng_state prng; | |
2395 int err; | |
2396 | |
2397 /* register yarrow */ | |
2398 if (register_prng(&yarrow_desc) == -1) { | |
2399 printf("Error registering Yarrow\n"); | |
2400 return -1; | |
2401 } | |
2402 | |
2403 /* setup the PRNG */ | |
2404 if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) { | |
2405 printf("Error setting up PRNG, %s\n", error_to_string(err)); | |
2406 return -1; | |
2407 } | |
2408 | |
2409 /* make a 192-bit ECC key */ | |
2410 if ((err = ecc_make_key(&prng, find_prng("yarrow"), 24, &mykey)) != CRYPT_OK) { | |
2411 printf("Error making key: %s\n", error_to_string(err)); | |
2412 return -1; | |
2413 } | |
2414 return 0; | |
2415 } | |
2416 \end{verbatim} | |
2417 \end{small} | |
2418 | |
2419 \subsection{The Secure PRNG Interface} | |
2420 It is possible to access the secure RNG through the PRNG interface and in turn use it within dependent functions such | |
2421 as the PK API. This simplifies the cryptosystem on platforms where the secure RNG is fast. The secure PRNG never | |
2422 requires to be started, that is you need not call the start, add\_entropy or ready functions. For example, consider | |
2423 the previous example using this PRNG. | |
2424 | |
2425 \begin{small} | |
2426 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2427 #include <tomcrypt.h> |
3 | 2428 int main(void) |
2429 { | |
2430 ecc_key mykey; | |
2431 int err; | |
2432 | |
2433 /* register SPRNG */ | |
2434 if (register_prng(&sprng_desc) == -1) { | |
2435 printf("Error registering SPRNG\n"); | |
2436 return -1; | |
2437 } | |
2438 | |
2439 /* make a 192-bit ECC key */ | |
2440 if ((err = ecc_make_key(NULL, find_prng("sprng"), 24, &mykey)) != CRYPT_OK) { | |
2441 printf("Error making key: %s\n", error_to_string(err)); | |
2442 return -1; | |
2443 } | |
2444 return 0; | |
2445 } | |
2446 \end{verbatim} | |
2447 \end{small} | |
2448 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2449 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2450 |
3 | 2451 \chapter{RSA Public Key Cryptography} |
15 | 2452 |
2453 \section{Introduction} | |
2454 RSA wrote the PKCS \#1 specifications which detail RSA Public Key Cryptography. In the specifications are | |
2455 padding algorithms for encryption and signatures. The standard includes ``v1.5'' and ``v2.0'' algorithms. | |
2456 To simplify matters a little the v2.0 encryption and signature padding algorithms are called OAEP and PSS | |
2457 respectively. | |
3 | 2458 |
2459 \section{PKCS \#1 Encryption} | |
2460 | |
2461 PKCS \#1 RSA Encryption amounts to OAEP padding of the input message followed by the modular exponentiation. As far as this portion of | |
2462 the library is concerned we are only dealing with th OAEP padding of the message. | |
2463 | |
2464 \subsection{OAEP Encoding} | |
2465 | |
15 | 2466 \index{pkcs\_1\_oaep\_encode()} |
3 | 2467 \begin{alltt} |
2468 int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, | |
2469 const unsigned char *lparam, unsigned long lparamlen, | |
15 | 2470 unsigned long modulus_bitlen, prng_state *prng, |
2471 int prng_idx, int hash_idx, | |
3 | 2472 unsigned char *out, unsigned long *outlen); |
2473 \end{alltt} | |
2474 | |
2475 This accepts ``msg'' as input of length ``msglen'' which will be OAEP padded. The ``lparam'' variable is an additional system specific | |
2476 tag that can be applied to the encoding. This is useful to identify which system encoded the message. If no variance is desired then | |
2477 ``lparam'' can be set to \textbf{NULL}. | |
2478 | |
2479 OAEP encoding requires the length of the modulus in bits in order to calculate the size of the output. This is passed as the parameter | |
2480 ``modulus\_bitlen''. ``hash\_idx'' is the index into the hash descriptor table of the hash desired. PKCS \#1 allows any hash to be | |
2481 used but both the encoder and decoder must use the same hash in order for this to succeed. The size of hash output affects the maximum | |
2482 sized input message. ``prng\_idx'' and ``prng'' are the random number generator arguments required to randomize the padding process. | |
2483 The padded message is stored in ``out'' along with the length in ``outlen''. | |
2484 | |
2485 If $h$ is the length of the hash and $m$ the length of the modulus (both in octets) then the maximum payload for ``msg'' is | |
2486 $m - 2h - 2$. For example, with a $1024$--bit RSA key and SHA--1 as the hash the maximum payload is $86$ bytes. | |
2487 | |
2488 Note that when the message is padded it still has not been RSA encrypted. You must pass the output of this function to | |
2489 rsa\_exptmod() to encrypt it. | |
2490 | |
2491 \subsection{OAEP Decoding} | |
2492 | |
15 | 2493 \index{pkcs\_1\_oaep\_decode()} |
3 | 2494 \begin{alltt} |
2495 int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, | |
2496 const unsigned char *lparam, unsigned long lparamlen, | |
2497 unsigned long modulus_bitlen, int hash_idx, | |
15 | 2498 unsigned char *out, unsigned long *outlen, |
2499 int *res); | |
3 | 2500 \end{alltt} |
2501 | |
2502 This function decodes an OAEP encoded message and outputs the original message that was passed to the OAEP encoder. ``msg'' is the | |
2503 output of pkcs\_1\_oaep\_encode() of length ``msglen''. ``lparam'' is the same system variable passed to the OAEP encoder. If it does not | |
2504 match what was used during encoding this function will not decode the packet. ``modulus\_bitlen'' is the size of the RSA modulus in bits | |
2505 and must match what was used during encoding. Similarly the ``hash\_idx'' index into the hash descriptor table must match what was used | |
2506 during encoding. | |
2507 | |
15 | 2508 If the function succeeds it decodes the OAEP encoded message into ``out'' of length ``outlen'' and stores a |
2509 $1$ in ``res''. If the packet is invalid it stores $0$ in ``res'' and if the function fails for another reason | |
2510 it returns an error code. | |
2511 | |
2512 \subsection{PKCS \#1 v1.5 Encoding} | |
2513 | |
2514 \index{pkcs\_1\_v15\_es\_encode()} | |
2515 \begin{verbatim} | |
2516 int pkcs_1_v15_es_encode(const unsigned char *msg, unsigned long msglen, | |
2517 unsigned long modulus_bitlen, | |
2518 prng_state *prng, int prng_idx, | |
2519 unsigned char *out, unsigned long *outlen); | |
2520 \end{verbatim} | |
2521 | |
2522 This will PKCS v1.5 encode the data in ``msg'' of length ``msglen''. Pass the length (in bits) of your | |
2523 RSA modulus in ``modulus\_bitlen''. The encoded data will be stored in ``out'' of length ``outlen''. | |
2524 | |
2525 \subsection{PKCS \#1 v1.5 Decoding} | |
2526 \index{pkcs\_1\_v15\_es\_decode()} | |
2527 \begin{verbatim} | |
2528 int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen, | |
2529 unsigned long modulus_bitlen, | |
2530 unsigned char *out, unsigned long outlen, | |
2531 int *res); | |
2532 \end{verbatim} | |
2533 | |
2534 This will PKCS v1.5 decode the message in ``msg'' of length ``msglen''. It will store the output in ``out''. Note | |
2535 that the length of the output ``outlen'' is a constant. This decoder cannot determine the original message | |
2536 length. If the data in ``msg'' is a valid packet then a $1$ is stored in ``res'', otherwise a $0$ is | |
2537 stored. | |
3 | 2538 |
2539 \section{PKCS \#1 Digital Signatures} | |
2540 | |
2541 \subsection{PSS Encoding} | |
2542 PSS encoding is the second half of the PKCS \#1 standard which is padding to be applied to messages that are signed. | |
2543 | |
15 | 2544 \index{pkcs\_1\_pss\_encode()} |
3 | 2545 \begin{alltt} |
2546 int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, | |
15 | 2547 unsigned long saltlen, prng_state *prng, |
2548 int prng_idx, int hash_idx, | |
3 | 2549 unsigned long modulus_bitlen, |
2550 unsigned char *out, unsigned long *outlen); | |
2551 \end{alltt} | |
2552 | |
2553 This function assumes the message to be PSS encoded has previously been hashed. The input hash ``msghash'' is of length | |
2554 ``msghashlen''. PSS allows a variable length random salt (it can be zero length) to be introduced in the signature process. | |
2555 ``hash\_idx'' is the index into the hash descriptor table of the hash to use. ``prng\_idx'' and ``prng'' are the random | |
2556 number generator information required for the salt. | |
2557 | |
15 | 2558 Similar to OAEP encoding ``modulus\_bitlen'' is the size of the RSA modulus (in bits). It limits the size of the salt. If $m$ is the length |
3 | 2559 of the modulus $h$ the length of the hash output (in octets) then there can be $m - h - 2$ bytes of salt. |
2560 | |
2561 This function does not actually sign the data it merely pads the hash of a message so that it can be processed by rsa\_exptmod(). | |
2562 | |
2563 \subsection{PSS Decoding} | |
2564 | |
2565 To decode a PSS encoded signature block you have to use the following. | |
2566 | |
15 | 2567 \index{pkcs\_1\_pss\_decode()} |
3 | 2568 \begin{alltt} |
2569 int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, | |
2570 const unsigned char *sig, unsigned long siglen, | |
2571 unsigned long saltlen, int hash_idx, | |
2572 unsigned long modulus_bitlen, int *res); | |
2573 \end{alltt} | |
2574 This will decode the PSS encoded message in ``sig'' of length ``siglen'' and compare it to values in ``msghash'' of length | |
2575 ``msghashlen''. If the block is a valid PSS block and the decoded hash equals the hash supplied ``res'' is set to non--zero. Otherwise, | |
2576 it is set to zero. The rest of the parameters are as in the PSS encode call. | |
2577 | |
2578 It's important to use the same ``saltlen'' and hash for both encoding and decoding as otherwise the procedure will not work. | |
2579 | |
15 | 2580 \subsection{PKCS \#1 v1.5 Encoding} |
2581 | |
2582 \index{pkcs\_1\_v15\_sa\_encode()} | |
2583 \begin{verbatim} | |
2584 int pkcs_1_v15_sa_encode(const unsigned char *msghash, unsigned long msghashlen, | |
2585 int hash_idx, unsigned long modulus_bitlen, | |
2586 unsigned char *out, unsigned long *outlen); | |
2587 \end{verbatim} | |
2588 | |
2589 This will PKCS \#1 v1.5 signature encode the message hash ``msghash'' of length ``msghashlen''. You have | |
2590 to tell this routine which hash produced the message hash in ``hash\_idx''. The encoded hash is stored | |
2591 in ``out'' of length ``outlen''. | |
2592 | |
2593 \subsection{PKCS \#1 v1.5 Decoding} | |
2594 | |
2595 \index{pkcs\_1\_v15\_sa\_decode()} | |
2596 \begin{verbatim} | |
2597 int pkcs_1_v15_sa_decode(const unsigned char *msghash, unsigned long msghashlen, | |
2598 const unsigned char *sig, unsigned long siglen, | |
2599 int hash_idx, unsigned long modulus_bitlen, | |
2600 int *res); | |
2601 \end{verbatim} | |
2602 | |
2603 This will PKCS \#1 v1.5 signature decode the data in ``sig'' of length ``siglen'' and compare the extracted | |
2604 hash against ``msghash'' of length ``msghashlen''. You have to tell this routine which hash produced the | |
2605 message digest in ``hash\_idx''. If the packet is valid and the hashes match ``res'' is set to $1$. Otherwise, | |
2606 it is set to $0$. | |
2607 | |
2608 \section{RSA Operations} | |
2609 \subsection{Background} | |
2610 | |
2611 RSA is a public key algorithm that is based on the inability to find the ``e-th'' root modulo a composite of unknown | |
2612 factorization. Normally the difficulty of breaking RSA is associated with the integer factoring problem but they are | |
2613 not strictly equivalent. | |
2614 | |
2615 The system begins with with two primes $p$ and $q$ and their product $N = pq$. The order or ``Euler totient'' of the | |
2616 multiplicative sub-group formed modulo $N$ is given as $\phi(N) = (p - 1)(q - 1)$ which can be reduced to | |
2617 $\mbox{lcm}(p - 1, q - 1)$. The public key consists of the composite $N$ and some integer $e$ such that | |
2618 $\mbox{gcd}(e, \phi(N)) = 1$. The private key consists of the composite $N$ and the inverse of $e$ modulo $\phi(N)$ | |
2619 often simply denoted as $de \equiv 1\mbox{ }(\mbox{mod }\phi(N))$. | |
2620 | |
2621 A person who wants to encrypt with your public key simply forms an integer (the plaintext) $M$ such that | |
2622 $1 < M < N-2$ and computes the ciphertext $C = M^e\mbox{ }(\mbox{mod }N)$. Since finding the inverse exponent $d$ | |
2623 given only $N$ and $e$ appears to be intractable only the owner of the private key can decrypt the ciphertext and compute | |
2624 $C^d \equiv \left (M^e \right)^d \equiv M^1 \equiv M\mbox{ }(\mbox{mod }N)$. Similarly the owner of the private key | |
2625 can sign a message by ``decrypting'' it. Others can verify it by ``encrypting'' it. | |
2626 | |
2627 Currently RSA is a difficult system to cryptanalyze provided that both primes are large and not close to each other. | |
2628 Ideally $e$ should be larger than $100$ to prevent direct analysis. For example, if $e$ is three and you do not pad | |
2629 the plaintext to be encrypted than it is possible that $M^3 < N$ in which case finding the cube-root would be trivial. | |
2630 The most often suggested value for $e$ is $65537$ since it is large enough to make such attacks impossible and also well | |
2631 designed for fast exponentiation (requires 16 squarings and one multiplication). | |
2632 | |
2633 It is important to pad the input to RSA since it has particular mathematical structure. For instance | |
2634 $M_1^dM_2^d = (M_1M_2)^d$ which can be used to forge a signature. Suppose $M_3 = M_1M_2$ is a message you want | |
2635 to have a forged signature for. Simply get the signatures for $M_1$ and $M_2$ on their own and multiply the result | |
2636 together. Similar tricks can be used to deduce plaintexts from ciphertexts. It is important not only to sign | |
2637 the hash of documents only but also to pad the inputs with data to remove such structure. | |
2638 | |
2639 \subsection{RSA Key Generation} | |
2640 | |
2641 For RSA routines a single ``rsa\_key'' structure is used. To make a new RSA key call: | |
2642 \index{rsa\_make\_key()} | |
2643 \begin{verbatim} | |
2644 int rsa_make_key(prng_state *prng, | |
2645 int wprng, int size, | |
2646 long e, rsa_key *key); | |
2647 \end{verbatim} | |
2648 | |
2649 Where ``wprng'' is the index into the PRNG descriptor array. ``size'' is the size in bytes of the RSA modulus desired. | |
2650 ``e'' is the encryption exponent desired, typical values are 3, 17, 257 and 65537. I suggest you stick with 65537 since its big | |
2651 enough to prevent trivial math attacks and not super slow. ``key'' is where the key is placed. All keys must be at | |
2652 least 128 bytes and no more than 512 bytes in size (\textit{that is from 1024 to 4096 bits}). | |
2653 | |
2654 Note that the ``rsa\_make\_key()'' function allocates memory at runtime when you make the key. Make sure to call | |
2655 ``rsa\_free()'' (see below) when you are finished with the key. If ``rsa\_make\_key()'' fails it will automatically | |
2656 free the ram allocated itself. | |
2657 | |
143 | 2658 \index{PK\_PRIVATE} \index{PK\_PUBLIC} |
2659 There are two types of RSA keys. The types are {\bf PK\_PRIVATE} and {\bf PK\_PUBLIC}. The first type is a private | |
2660 RSA key which includes the CRT parameters\footnote{As of v0.99 the PK\_PRIVATE\_OPTIMIZED type has been deprecated | |
2661 and has been replaced by the PK\_PRIVATE type.} in the form of a RSAPrivateKey. The second type is a public RSA key | |
2662 which only includes the modulus and public exponent. It takes the form of a RSAPublicKey. | |
15 | 2663 |
2664 \subsection{RSA Exponentiation} | |
2665 | |
2666 To do raw work with the RSA function call: | |
2667 \index{rsa\_exptmod()} | |
2668 \begin{verbatim} | |
2669 int rsa_exptmod(const unsigned char *in, unsigned long inlen, | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2670 unsigned char *out, unsigned long *outlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2671 int which, prng_state *prng, int prng_idx, |
15 | 2672 rsa_key *key); |
2673 \end{verbatim} | |
2674 This loads the bignum from ``in'' as a big endian word in the format PKCS specifies, raises it to either ``e'' or ``d'' and stores the result | |
2675 in ``out'' and the size of the result in ``outlen''. ``which'' is set to {\bf PK\_PUBLIC} to use ``e'' | |
2676 (i.e. for encryption/verifying) and set to {\bf PK\_PRIVATE} to use ``d'' as the exponent (i.e. for decrypting/signing). | |
2677 | |
2678 Note that the output of his function is zero-padded as per PKCS \#1 specifications. This allows this routine to | |
2679 interoprate with PKCS \#1 padding functions properly. | |
2680 | |
2681 \subsection{RSA Key Encryption} | |
2682 Normally RSA is used to encrypt short symmetric keys which are then used in block ciphers to encrypt a message. | |
2683 To facilitate encrypting short keys the following functions have been provided. | |
2684 | |
2685 \index{rsa\_encrypt\_key()} | |
2686 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2687 int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2688 unsigned char *out, unsigned long *outlen, |
15 | 2689 const unsigned char *lparam, unsigned long lparamlen, |
2690 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); | |
2691 \end{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2692 This function will OAEP pad ``in'' of length inlen bytes then RSA encrypt it and store the ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2693 in ``out'' of length ``outlen''. The ``lparam'' and ``lparamlen'' are the same parameters you would pass |
15 | 2694 to pkcs\_1\_oaep\_encode(). |
2695 | |
2696 \index{rsa\_decrypt\_key()} | |
2697 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2698 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2699 unsigned char *out, unsigned long *outlen, |
15 | 2700 const unsigned char *lparam, unsigned long lparamlen, |
2701 prng_state *prng, int prng_idx, | |
2702 int hash_idx, int *res, | |
2703 rsa_key *key); | |
2704 \end{verbatim} | |
2705 This function will RSA decrypt ``in'' of length ``inlen'' then OAEP depad the resulting data and store it in | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2706 ``out'' of length ``outlen''. The ``lparam'' and ``lparamlen'' are the same parameters you would pass |
15 | 2707 to pkcs\_1\_oaep\_decode(). |
2708 | |
2709 If the RSA decrypted data isn't a valid OAEP packet then ``res'' is set to $0$. Otherwise, it is set to $1$. | |
2710 | |
2711 \subsection{RSA Hash Signatures} | |
2712 Similar to RSA key encryption RSA is also used to ``digitally sign'' message digests (hashes). To facilitate this | |
2713 process the following functions have been provided. | |
2714 | |
2715 \index{rsa\_sign\_hash()} | |
2716 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2717 int rsa_sign_hash(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2718 unsigned char *out, unsigned long *outlen, |
15 | 2719 prng_state *prng, int prng_idx, |
2720 int hash_idx, unsigned long saltlen, | |
2721 rsa_key *key); | |
2722 \end{verbatim} | |
2723 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2724 This will PSS encode the message hash ``in'' of length ``inlen''. Next the PSS encoded message will be RSA ``signed'' and |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2725 the output is stored in ``out'' of length ``outlen''. |
15 | 2726 |
2727 | |
2728 \index{rsa\_verify\_hash()} | |
2729 \begin{verbatim} | |
2730 int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
2731 const unsigned char *msghash, unsigned long msghashlen, | |
2732 prng_state *prng, int prng_idx, | |
2733 int hash_idx, unsigned long saltlen, | |
2734 int *stat, rsa_key *key); | |
2735 \end{verbatim} | |
2736 | |
2737 This will RSA ``verify'' the signature in ``sig'' of length ``siglen''. Next the RSA decoded data is PSS decoded | |
2738 and the extracted hash is compared against the message hash ``msghash'' of length ``msghashlen''. | |
2739 | |
2740 If the RSA decoded data is not a valid PSS message or if the PSS decoded hash does not match the ``msghash'' | |
2741 the value ``res'' is set to $0$. Otherwise, if the function succeeds and signature is valid ``res'' is set | |
2742 to $1$. | |
2743 | |
2744 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
2745 #include <tomcrypt.h> |
15 | 2746 int main(void) |
2747 { | |
2748 int err, hash_idx, prng_idx, res; | |
2749 unsigned long l1, l2; | |
2750 unsigned char pt[16], pt2[16], out[1024]; | |
2751 rsa_key key; | |
2752 | |
2753 /* register prng/hash */ | |
2754 if (register_prng(&sprng_desc) == -1) { | |
2755 printf("Error registering sprng"); | |
2756 return EXIT_FAILURE; | |
2757 } | |
2758 | |
2759 if (register_hash(&sha1_desc) == -1) { | |
2760 printf("Error registering sha1"); | |
2761 return EXIT_FAILURE; | |
2762 } | |
2763 hash_idx = find_hash("sha1"); | |
2764 prng_idx = find_prng("sprng"); | |
2765 | |
2766 /* make an RSA-1024 key */ | |
2767 if ((err = rsa_make_key(NULL, /* PRNG state */ | |
2768 prng_idx, /* PRNG idx */ | |
2769 1024/8, /* 1024-bit key */ | |
2770 65537, /* we like e=65537 */ | |
2771 &key) /* where to store the key */ | |
2772 ) != CRYPT_OK) { | |
2773 printf("rsa_make_key %s", error_to_string(err)); | |
2774 return EXIT_FAILURE; | |
2775 } | |
2776 | |
2777 /* fill in pt[] with a key we want to send ... */ | |
2778 l1 = sizeof(out); | |
2779 if ((err = rsa_encrypt_key(pt, /* data we wish to encrypt */ | |
2780 16, /* data is 16 bytes long */ | |
2781 out, /* where to store ciphertext */ | |
2782 &l1, /* length of ciphertext */ | |
2783 "TestApp", /* our lparam for this program */ | |
2784 7, /* lparam is 7 bytes long */ | |
2785 NULL, /* PRNG state */ | |
2786 prng_idx, /* prng idx */ | |
2787 hash_idx, /* hash idx */ | |
2788 &key) /* our RSA key */ | |
2789 ) != CRYPT_OK) { | |
2790 printf("rsa_encrypt_key %s", error_to_string(err)); | |
2791 return EXIT_FAILURE; | |
2792 } | |
2793 | |
2794 /* now let's decrypt the encrypted key */ | |
2795 l2 = sizeof(pt2); | |
2796 if ((err = rsa_decrypt_key(out, /* encrypted data */ | |
2797 l1, /* length of ciphertext */ | |
2798 pt2, /* where to put plaintext */ | |
2799 &l2, /* plaintext length */ | |
2800 "TestApp", /* lparam for this program */ | |
2801 7, /* lparam is 7 bytes long */ | |
2802 NULL, /* PRNG state */ | |
2803 prng_idx, /* prng idx */ | |
2804 hash_idx, /* hash idx */ | |
2805 &res, /* validity of data */ | |
2806 &key) /* our RSA key */ | |
2807 ) != CRYPT_OK) { | |
2808 printf("rsa_decrypt_key %s", error_to_string(err)); | |
2809 return EXIT_FAILURE; | |
2810 } | |
2811 /* if all went well pt == pt2, l2 == 16, res == 1 */ | |
2812 } | |
2813 \end{verbatim} | |
2814 | |
3 | 2815 |
2816 \chapter{Diffie-Hellman Key Exchange} | |
2817 | |
2818 \section{Background} | |
2819 | |
2820 Diffie-Hellman was the original public key system proposed. The system is based upon the group structure | |
2821 of finite fields. For Diffie-Hellman a prime $p$ is chosen and a ``base'' $b$ such that $b^x\mbox{ }(\mbox{mod }p)$ | |
2822 generates a large sub-group of prime order (for unique values of $x$). | |
2823 | |
2824 A secret key is an exponent $x$ and a public key is the value of $y \equiv g^x\mbox{ }(\mbox{mod }p)$. The term | |
2825 ``discrete logarithm'' denotes the action of finding $x$ given only $y$, $g$ and $p$. The key exchange part of | |
2826 Diffie-Hellman arises from the fact that two users A and B with keys $(A_x, A_y)$ and $(B_x, B_y)$ can exchange | |
2827 a shared key $K \equiv B_y^{A_x} \equiv A_y^{B_x} \equiv g^{A_xB_x}\mbox{ }(\mbox{mod }p)$. | |
2828 | |
2829 From this public encryption and signatures can be developed. The trivial way to encrypt (for example) using a public key | |
2830 $y$ is to perform the key exchange offline. The sender invents a key $k$ and its public copy | |
2831 $k' \equiv g^k\mbox{ }(\mbox{mod }p)$ and uses $K \equiv k'^{A_x}\mbox{ }(\mbox{mod }p)$ as a key to encrypt | |
2832 the message with. Typically $K$ would be sent to a one-way hash and the message digested used as a key in a | |
2833 symmetric cipher. | |
2834 | |
2835 It is important that the order of the sub-group that $g$ generates not only be large but also prime. There are | |
2836 discrete logarithm algorithms that take $\sqrt r$ time given the order $r$. The discrete logarithm can be computed | |
2837 modulo each prime factor of $r$ and the results combined using the Chinese Remainder Theorem. In the cases where | |
2838 $r$ is ``B-Smooth'' (e.g. all small factors or powers of small prime factors) the solution is trivial to find. | |
2839 | |
2840 To thwart such attacks the primes and bases in the library have been designed and fixed. Given a prime $p$ the order of | |
2841 the sub-group generated is a large prime namely ${p - 1} \over 2$. Such primes are known as ``strong primes'' and the | |
2842 smaller prime (e.g. the order of the base) are known as Sophie-Germaine primes. | |
2843 | |
2844 \section{Core Functions} | |
2845 | |
2846 This library also provides core Diffie-Hellman functions so you can negotiate keys over insecure mediums. The routines | |
2847 provided are relatively easy to use and only take two function calls to negotiate a shared key. There is a structure | |
2848 called ``dh\_key'' which stores the Diffie-Hellman key in a format these routines can use. The first routine is to | |
2849 make a Diffie-Hellman private key pair: | |
2850 \index{dh\_make\_key()} | |
2851 \begin{verbatim} | |
2852 int dh_make_key(prng_state *prng, int wprng, | |
2853 int keysize, dh_key *key); | |
2854 \end{verbatim} | |
2855 The ``keysize'' is the size of the modulus you want in bytes. Currently support sizes are 96 to 512 bytes which correspond | |
2856 to key sizes of 768 to 4096 bits. The smaller the key the faster it is to use however it will be less secure. When | |
2857 specifying a size not explicitly supported by the library it will round {\em up} to the next key size. If the size is | |
2858 above 512 it will return an error. So if you pass ``keysize == 32'' it will use a 768 bit key but if you pass | |
2859 ``keysize == 20000'' it will return an error. The primes and generators used are built-into the library and were designed | |
2860 to meet very specific goals. The primes are strong primes which means that if $p$ is the prime then | |
2861 $p-1$ is equal to $2r$ where $r$ is a large prime. The bases are chosen to generate a group of order $r$ to prevent | |
2862 leaking a bit of the key. This means the bases generate a very large prime order group which is good to make cryptanalysis | |
2863 hard. | |
2864 | |
2865 The next two routines are for exporting/importing Diffie-Hellman keys in a binary format. This is useful for transport | |
2866 over communication mediums. | |
2867 | |
2868 \index{dh\_export()} \index{dh\_import()} | |
2869 \begin{verbatim} | |
2870 int dh_export(unsigned char *out, unsigned long *outlen, | |
2871 int type, dh_key *key); | |
2872 | |
2873 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); | |
2874 \end{verbatim} | |
2875 | |
2876 These two functions work just like the ``rsa\_export()'' and ``rsa\_import()'' functions except these work with | |
2877 Diffie-Hellman keys. Its important to note you do not have to free the ram for a ``dh\_key'' if an import fails. You can free a | |
2878 ``dh\_key'' using: | |
2879 \begin{verbatim} | |
2880 void dh_free(dh_key *key); | |
2881 \end{verbatim} | |
2882 After you have exported a copy of your public key (using {\bf PK\_PUBLIC} as ``type'') you can now create a shared secret | |
2883 with the other user using: | |
2884 \index{dh\_shared\_secret()} | |
2885 \begin{verbatim} | |
2886 int dh_shared_secret(dh_key *private_key, | |
2887 dh_key *public_key, | |
2888 unsigned char *out, unsigned long *outlen); | |
2889 \end{verbatim} | |
2890 | |
2891 Where ``private\_key'' is the key you made and ``public\_key'' is the copy of the public key the other user sent you. The result goes | |
2892 into ``out'' and the length into ``outlen''. If all went correctly the data in ``out'' should be identical for both parties. It is important to | |
2893 note that the two keys have to be the same size in order for this to work. There is a function to get the size of a | |
2894 key: | |
2895 \index{dh\_get\_size()} | |
2896 \begin{verbatim} | |
2897 int dh_get_size(dh_key *key); | |
2898 \end{verbatim} | |
2899 This returns the size in bytes of the modulus chosen for that key. | |
2900 | |
2901 \subsection{Remarks on Usage} | |
2902 Its important that you hash the shared key before trying to use it as a key for a symmetric cipher or something. An | |
2903 example program that communicates over sockets, using MD5 and 1024-bit DH keys is\footnote{This function is a small example. It is suggested that proper packaging be used. For example, if the public key sent is truncated these routines will not detect that.}: | |
2904 \newpage | |
2905 \begin{small} | |
2906 \begin{verbatim} | |
2907 int establish_secure_socket(int sock, int mode, unsigned char *key, | |
2908 prng_state *prng, int wprng) | |
2909 { | |
2910 unsigned char buf[4096], buf2[4096]; | |
2911 unsigned long x, len; | |
2912 int res, err, inlen; | |
2913 dh_key mykey, theirkey; | |
2914 | |
2915 /* make up our private key */ | |
2916 if ((err = dh_make_key(prng, wprng, 128, &mykey)) != CRYPT_OK) { | |
2917 return err; | |
2918 } | |
2919 | |
2920 /* export our key as public */ | |
2921 x = sizeof(buf); | |
2922 if ((err = dh_export(buf, &x, PK_PUBLIC, &mykey)) != CRYPT_OK) { | |
2923 res = err; | |
2924 goto done2; | |
2925 } | |
2926 | |
2927 if (mode == 0) { | |
2928 /* mode 0 so we send first */ | |
2929 if (send(sock, buf, x, 0) != x) { | |
2930 res = CRYPT_ERROR; | |
2931 goto done2; | |
2932 } | |
2933 | |
2934 /* get their key */ | |
2935 if ((inlen = recv(sock, buf2, sizeof(buf2), 0)) <= 0) { | |
2936 res = CRYPT_ERROR; | |
2937 goto done2; | |
2938 } | |
2939 } else { | |
2940 /* mode >0 so we send second */ | |
2941 if ((inlen = recv(sock, buf2, sizeof(buf2), 0)) <= 0) { | |
2942 res = CRYPT_ERROR; | |
2943 goto done2; | |
2944 } | |
2945 | |
2946 if (send(sock, buf, x, 0) != x) { | |
2947 res = CRYPT_ERROR; | |
2948 goto done2; | |
2949 } | |
2950 } | |
2951 | |
2952 if ((err = dh_import(buf2, inlen, &theirkey)) != CRYPT_OK) { | |
2953 res = err; | |
2954 goto done2; | |
2955 } | |
2956 | |
2957 /* make shared secret */ | |
2958 x = sizeof(buf); | |
2959 if ((err = dh_shared_secret(&mykey, &theirkey, buf, &x)) != CRYPT_OK) { | |
2960 res = err; | |
2961 goto done; | |
2962 } | |
2963 | |
2964 /* hash it */ | |
2965 len = 16; /* default is MD5 so "key" must be at least 16 bytes long */ | |
2966 if ((err = hash_memory(find_hash("md5"), buf, x, key, &len)) != CRYPT_OK) { | |
2967 res = err; | |
2968 goto done; | |
2969 } | |
2970 | |
2971 /* clean up and return */ | |
2972 res = CRYPT_OK; | |
2973 done: | |
2974 dh_free(&theirkey); | |
2975 done2: | |
2976 dh_free(&mykey); | |
2977 zeromem(buf, sizeof(buf)); | |
2978 zeromem(buf2, sizeof(buf2)); | |
2979 return res; | |
2980 } | |
2981 \end{verbatim} | |
2982 \end{small} | |
2983 \newpage | |
2984 \subsection{Remarks on The Snippet} | |
2985 When the above code snippet is done (assuming all went well) their will be a shared 128-bit key in the ``key'' array | |
2986 passed to ``establish\_secure\_socket()''. | |
2987 | |
2988 \section{Other Diffie-Hellman Functions} | |
2989 In order to test the Diffie-Hellman function internal workings (e.g. the primes and bases) their is a test function made | |
2990 available: | |
2991 \index{dh\_test()} | |
2992 \begin{verbatim} | |
2993 int dh_test(void); | |
2994 \end{verbatim} | |
2995 | |
2996 This function returns {\bf CRYPT\_OK} if the bases and primes in the library are correct. There is one last helper | |
2997 function: | |
2998 \index{dh\_sizes()} | |
2999 \begin{verbatim} | |
3000 void dh_sizes(int *low, int *high); | |
3001 \end{verbatim} | |
3002 Which stores the smallest and largest key sizes support into the two variables. | |
3003 | |
3004 \section{DH Packet} | |
3005 Similar to the RSA related functions there are functions to encrypt or decrypt symmetric keys using the DH public key | |
3006 algorithms. | |
15 | 3007 \index{dh\_encrypt\_key()} \index{dh\_decrypt\_key()} |
3 | 3008 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3009 int dh_encrypt_key(const unsigned char *in, unsigned long inlen, |
3 | 3010 unsigned char *out, unsigned long *len, |
3011 prng_state *prng, int wprng, int hash, | |
3012 dh_key *key); | |
3013 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3014 int dh_decrypt_key(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3015 unsigned char *out, unsigned long *outlen, |
3 | 3016 dh_key *key); |
3017 \end{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3018 Where ``in'' is an input symmetric key of no more than 32 bytes. Essentially these routines created a random public key |
3 | 3019 and find the hash of the shared secret. The message digest is than XOR'ed against the symmetric key. All of the |
3020 required data is placed in ``out'' by ``dh\_encrypt\_key()''. The hash must produce a message digest at least as large | |
3021 as the symmetric key you are trying to share. | |
3022 | |
3023 Similar to the RSA system you can sign and verify a hash of a message. | |
15 | 3024 \index{dh\_sign\_hash()} \index{dh\_verify\_hash()} |
3 | 3025 \begin{verbatim} |
3026 int dh_sign_hash(const unsigned char *in, unsigned long inlen, | |
3027 unsigned char *out, unsigned long *outlen, | |
3028 prng_state *prng, int wprng, dh_key *key); | |
3029 | |
3030 int dh_verify_hash(const unsigned char *sig, unsigned long siglen, | |
3031 const unsigned char *hash, unsigned long hashlen, | |
3032 int *stat, dh_key *key); | |
3033 \end{verbatim} | |
3034 | |
3035 The ``dh\_sign\_hash'' function signs the message hash in ``in'' of length ``inlen'' and forms a DH packet in ``out''. | |
3036 The ``dh\_verify\_hash'' function verifies the DH signature in ``sig'' against the hash in ``hash''. It sets ``stat'' | |
3037 to non-zero if the signature passes or zero if it fails. | |
3038 | |
3039 \chapter{Elliptic Curve Cryptography} | |
3040 | |
3041 \section{Background} | |
3042 The library provides a set of core ECC functions as well that are designed to be the Elliptic Curve analogy of all of the | |
3043 Diffie-Hellman routines in the previous chapter. Elliptic curves (of certain forms) have the benefit that they are harder | |
3044 to attack (no sub-exponential attacks exist unlike normal DH crypto) in fact the fastest attack requires the square root | |
3045 of the order of the base point in time. That means if you use a base point of order $2^{192}$ (which would represent a | |
3046 192-bit key) then the work factor is $2^{96}$ in order to find the secret key. | |
3047 | |
3048 The curves in this library are taken from the following website: | |
3049 \begin{verbatim} | |
3050 http://csrc.nist.gov/cryptval/dss.htm | |
3051 \end{verbatim} | |
3052 | |
3053 They are all curves over the integers modulo a prime. The curves have the basic equation that is: | |
3054 \begin{equation} | |
3055 y^2 = x^3 - 3x + b\mbox{ }(\mbox{mod }p) | |
3056 \end{equation} | |
3057 | |
3058 The variable $b$ is chosen such that the number of points is nearly maximal. In fact the order of the base points $\beta$ | |
3059 provided are very close to $p$ that is $\vert \vert \phi(\beta) \vert \vert \approx \vert \vert p \vert \vert$. The curves | |
3060 range in order from $\approx 2^{192}$ points to $\approx 2^{521}$. According to the source document any key size greater | |
3061 than or equal to 256-bits is sufficient for long term security. | |
3062 | |
3063 \section{Core Functions} | |
3064 | |
3065 Like the DH routines there is a key structure ``ecc\_key'' used by the functions. There is a function to make a key: | |
3066 \index{ecc\_make\_key()} | |
3067 \begin{verbatim} | |
3068 int ecc_make_key(prng_state *prng, int wprng, | |
3069 int keysize, ecc_key *key); | |
3070 \end{verbatim} | |
3071 | |
3072 The ``keysize'' is the size of the modulus in bytes desired. Currently directly supported values are 20, 24, 28, 32, 48 and 65 bytes which | |
3073 correspond to key sizes of 160, 192, 224, 256, 384 and 521 bits respectively. If you pass a key size that is between any key size | |
3074 it will round the keysize up to the next available one. The rest of the parameters work like they do in the ``dh\_make\_key()'' function. | |
3075 To free the ram allocated by a key call: | |
3076 \index{ecc\_free()} | |
3077 \begin{verbatim} | |
3078 void ecc_free(ecc_key *key); | |
3079 \end{verbatim} | |
3080 | |
3081 To import and export a key there are: | |
3082 \index{ecc\_export()} | |
3083 \index{ecc\_import()} | |
3084 \begin{verbatim} | |
3085 int ecc_export(unsigned char *out, unsigned long *outlen, | |
3086 int type, ecc_key *key); | |
3087 | |
3088 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); | |
3089 \end{verbatim} | |
3090 These two work exactly like there DH counterparts. Finally when you share your public key you can make a shared secret | |
3091 with: | |
3092 \index{ecc\_shared\_secret()} | |
3093 \begin{verbatim} | |
3094 int ecc_shared_secret(ecc_key *private_key, | |
3095 ecc_key *public_key, | |
3096 unsigned char *out, unsigned long *outlen); | |
3097 \end{verbatim} | |
3098 Which works exactly like the DH counterpart, the ``private\_key'' is your own key and ``public\_key'' is the key the other | |
3099 user sent you. Note that this function stores both $x$ and $y$ co-ordinates of the shared | |
3100 elliptic point. You should hash the output to get a shared key in a more compact and useful form (most of the entropy is | |
3101 in $x$ anyways). Both keys have to be the same size for this to work, to help there is a function to get the size in bytes | |
3102 of a key. | |
3103 \index{ecc\_get\_size()} | |
3104 \begin{verbatim} | |
3105 int ecc_get_size(ecc_key *key); | |
3106 \end{verbatim} | |
3107 | |
3108 To test the ECC routines and to get the minimum and maximum key sizes there are these two functions: | |
3109 \index{ecc\_test()} | |
3110 \begin{verbatim} | |
3111 int ecc_test(void); | |
3112 void ecc_sizes(int *low, int *high); | |
3113 \end{verbatim} | |
3114 Which both work like their DH counterparts. | |
3115 | |
3116 \section{ECC Packet} | |
3117 Similar to the RSA API there are two functions which encrypt and decrypt symmetric keys using the ECC public key | |
3118 algorithms. | |
15 | 3119 |
3120 \index{ecc\_encrypt\_key()} \index{ecc\_decrypt\_key()} | |
3 | 3121 \begin{verbatim} |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3122 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3123 unsigned char *out, unsigned long *outlen, |
3 | 3124 prng_state *prng, int wprng, int hash, |
3125 ecc_key *key); | |
3126 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3127 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3128 unsigned char *out, unsigned long *outlen, |
3 | 3129 ecc_key *key); |
3130 \end{verbatim} | |
3131 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3132 Where ``in'' is an input symmetric key of no more than 32 bytes. Essentially these routines created a random public key |
3 | 3133 and find the hash of the shared secret. The message digest is than XOR'ed against the symmetric key. All of the required |
3134 data is placed in ``out'' by ``ecc\_encrypt\_key()''. The hash chosen must produce a message digest at least as large | |
3135 as the symmetric key you are trying to share. | |
3136 | |
3137 There are also functions to sign and verify the hash of a message. | |
15 | 3138 \index{ecc\_sign\_hash()} \index{ecc\_verify\_hash()} |
3 | 3139 \begin{verbatim} |
3140 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, | |
3141 unsigned char *out, unsigned long *outlen, | |
3142 prng_state *prng, int wprng, ecc_key *key); | |
3143 | |
3144 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, | |
3145 const unsigned char *hash, unsigned long hashlen, | |
3146 int *stat, ecc_key *key); | |
3147 \end{verbatim} | |
3148 | |
3149 The ``ecc\_sign\_hash'' function signs the message hash in ``in'' of length ``inlen'' and forms a ECC packet in ``out''. | |
3150 The ``ecc\_verify\_hash'' function verifies the ECC signature in ``sig'' against the hash in ``hash''. It sets ``stat'' | |
3151 to non-zero if the signature passes or zero if it fails. | |
3152 | |
3153 | |
3154 \section{ECC Keysizes} | |
3155 With ECC if you try and sign a hash that is bigger than your ECC key you can run into problems. The math will still work | |
3156 and in effect the signature will still work. With ECC keys the strength of the signature is limited by the size of | |
3157 the hash or the size of they key, whichever is smaller. For example, if you sign with SHA256 and a ECC-160 key in effect | |
3158 you have 160-bits of security (e.g. as if you signed with SHA-1). | |
3159 | |
3160 The library will not warn you if you make this mistake so it is important to check yourself before using the | |
3161 signatures. | |
3162 | |
3163 \chapter{Digital Signature Algorithm} | |
3164 \section{Introduction} | |
3165 The Digital Signature Algorithm (or DSA) is a variant of the ElGamal Signature scheme which has been modified to | |
3166 reduce the bandwidth of a signature. For example, to have ``80-bits of security'' with ElGamal you need a group of | |
3167 order at least 1024-bits. With DSA you need a group of order at least 160-bits. By comparison the ElGamal signature | |
3168 would require at least 256 bytes where as the DSA signature would require only at least 40 bytes. | |
3169 | |
3170 The API for the DSA is essentially the same as the other PK algorithms. Except in the case of DSA no encryption or | |
3171 decryption routines are provided. | |
3172 | |
3173 \section{Key Generation} | |
3174 To make a DSA key you must call the following function | |
3175 \begin{verbatim} | |
3176 int dsa_make_key(prng_state *prng, int wprng, | |
3177 int group_size, int modulus_size, | |
3178 dsa_key *key); | |
3179 \end{verbatim} | |
3180 The variable ``prng'' is an active PRNG state and ``wprng'' the index to the descriptor. ``group\_size'' and | |
3181 ``modulus\_size'' control the difficulty of forging a signature. Both parameters are in bytes. The larger the | |
3182 ``group\_size'' the more difficult a forgery becomes upto a limit. The value of $group\_size$ is limited by | |
3183 $15 < group\_size < 1024$ and $modulus\_size - group\_size < 512$. Suggested values for the pairs are as follows. | |
3184 | |
3185 \begin{center} | |
3186 \begin{tabular}{|c|c|c|} | |
3187 \hline \textbf{Bits of Security} & \textbf{group\_size} & \textbf{modulus\_size} \\ | |
3188 \hline 80 & 20 & 128 \\ | |
3189 \hline 120 & 30 & 256 \\ | |
3190 \hline 140 & 35 & 384 \\ | |
3191 \hline 160 & 40 & 512 \\ | |
3192 \hline | |
3193 \end{tabular} | |
3194 \end{center} | |
3195 | |
3196 When you are finished with a DSA key you can call the following function to free the memory used. | |
15 | 3197 \index{dsa\_free()} |
3 | 3198 \begin{verbatim} |
3199 void dsa_free(dsa_key *key); | |
3200 \end{verbatim} | |
3201 | |
3202 \section{Key Verification} | |
3203 Each DSA key is composed of the following variables. | |
3204 | |
3205 \begin{enumerate} | |
3206 \item $q$ a small prime of magnitude $256^{group\_size}$. | |
3207 \item $p = qr + 1$ a large prime of magnitude $256^{modulus\_size}$ where $r$ is a random even integer. | |
3208 \item $g = h^r \mbox{ (mod }p\mbox{)}$ a generator of order $q$ modulo $p$. $h$ can be any non-trivial random | |
3209 value. For this library they start at $h = 2$ and step until $g$ is not $1$. | |
3210 \item $x$ a random secret (the secret key) in the range $1 < x < q$ | |
3211 \item $y = g^x \mbox{ (mod }p\mbox{)}$ the public key. | |
3212 \end{enumerate} | |
3213 | |
3214 A DSA key is considered valid if it passes all of the following tests. | |
3215 | |
3216 \begin{enumerate} | |
3217 \item $q$ must be prime. | |
3218 \item $p$ must be prime. | |
3219 \item $g$ cannot be one of $\lbrace -1, 0, 1 \rbrace$ (modulo $p$). | |
3220 \item $g$ must be less than $p$. | |
3221 \item $(p-1) \equiv 0 \mbox{ (mod }q\mbox{)}$. | |
3222 \item $g^q \equiv 1 \mbox{ (mod }p\mbox{)}$. | |
3223 \item $1 < y < p - 1$ | |
3224 \item $y^q \equiv 1 \mbox{ (mod }p\mbox{)}$. | |
3225 \end{enumerate} | |
3226 | |
3227 Tests one and two ensure that the values will at least form a field which is required for the signatures to | |
3228 function. Tests three and four ensure that the generator $g$ is not set to a trivial value which would make signature | |
3229 forgery easier. Test five ensures that $q$ divides the order of multiplicative sub-group of $\Z/p\Z$. Test six | |
3230 ensures that the generator actually generates a prime order group. Tests seven and eight ensure that the public key | |
3231 is within range and belongs to a group of prime order. Note that test eight does not prove that $g$ generated $y$ only | |
3232 that $y$ belongs to a multiplicative sub-group of order $q$. | |
3233 | |
3234 The following function will perform these tests. | |
3235 | |
15 | 3236 \index{dsa\_verify\_key()} |
3 | 3237 \begin{verbatim} |
3238 int dsa_verify_key(dsa_key *key, int *stat); | |
3239 \end{verbatim} | |
3240 | |
3241 This will test ``key'' and store the result in ``stat''. If the result is $stat = 0$ the DSA key failed one of the tests | |
3242 and should not be used at all. If the result is $stat = 1$ the DSA key is valid (as far as valid mathematics are concerned). | |
3243 | |
3244 \section{Signatures} | |
3245 To generate a DSA signature call the following function | |
3246 | |
15 | 3247 \index{dsa\_sign\_hash()} |
3 | 3248 \begin{verbatim} |
3249 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, | |
3250 unsigned char *out, unsigned long *outlen, | |
3251 prng_state *prng, int wprng, dsa_key *key); | |
3252 \end{verbatim} | |
3253 | |
3254 Which will sign the data in ``in'' of length ``inlen'' bytes. The signature is stored in ``out'' and the size | |
3255 of the signature in ``outlen''. If the signature is longer than the size you initially specify in ``outlen'' nothing | |
3256 is stored and the function returns an error code. The DSA ``key'' must be of the \textbf{PK\_PRIVATE} persuasion. | |
3257 | |
3258 To verify a hash created with that function use the following function | |
3259 | |
15 | 3260 \index{dsa\_verify\_hash()} |
3 | 3261 \begin{verbatim} |
3262 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
3263 const unsigned char *hash, unsigned long inlen, | |
3264 int *stat, dsa_key *key); | |
3265 \end{verbatim} | |
3266 Which will verify the data in ``hash'' of length ``inlen'' against the signature stored in ``sig'' of length ``siglen''. | |
3267 It will set ``stat'' to $1$ if the signature is valid, otherwise it sets ``stat'' to $0$. | |
3268 | |
3269 \section{Import and Export} | |
3270 | |
3271 To export a DSA key so that it can be transported use the following function | |
15 | 3272 \index{dsa\_export()} |
3 | 3273 \begin{verbatim} |
3274 int dsa_export(unsigned char *out, unsigned long *outlen, | |
3275 int type, | |
3276 dsa_key *key); | |
3277 \end{verbatim} | |
3278 This will export the DSA ``key'' to the buffer ``out'' and set the length in ``outlen'' (which must have been previously | |
3279 initialized to the maximum buffer size). The ``type`` variable may be either \textbf{PK\_PRIVATE} or \textbf{PK\_PUBLIC} | |
3280 depending on whether you want to export a private or public copy of the DSA key. | |
3281 | |
3282 To import an exported DSA key use the following function | |
3283 | |
15 | 3284 \index{dsa\_import()} |
3 | 3285 \begin{verbatim} |
3286 int dsa_import(const unsigned char *in, unsigned long inlen, | |
3287 dsa_key *key); | |
3288 \end{verbatim} | |
3289 | |
3290 This will import the DSA key from the buffer ``in'' of length ``inlen'' to the ``key''. If the process fails the function | |
3291 will automatically free all of the heap allocated in the process (you don't have to call dsa\_free()). | |
3292 | |
143 | 3293 \chapter{Standards Support} |
3294 \section{DER Support} | |
3295 DER or ``Distinguished Encoding Rules'' is a subset of the ASN.1 encoding rules that is fully deterministic and | |
3296 ideal for cryptography. In particular ASN.1 specifies an INTEGER type for storing arbitrary sized integers. DER | |
3297 further limits the ASN.1 specifications to a deterministic encoding. | |
3298 | |
3299 \subsection{Storing INTEGER types} | |
3300 \index{der\_encode\_integer()} | |
3301 \begin{alltt} | |
3302 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen); | |
3303 \end{alltt} | |
3304 | |
3305 This will store the integer in ``num'' to the output buffer ``out'' of length ``outlen''. It only stores | |
3306 non--negative numbers. It stores the number of octets used back in ``outlen''. | |
3307 | |
3308 \subsection{Reading INTEGER types} | |
3309 \index{der\_decode\_integer()} | |
3310 \begin{alltt} | |
3311 int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num); | |
3312 \end{alltt} | |
3313 This will decode the DER encoded INTEGER in ``in'' of length ``inlen'' and store the resulting integer | |
3314 in ``num''. It will store the bytes read in ``inlen'' which is handy if you have to parse multiple | |
3315 data items out of a binary packet. | |
3316 | |
3317 \subsection{INTEGER length} | |
3318 \index{der\_length\_integer()} | |
3319 \begin{alltt} | |
3320 int der_length_integer(mp_int *num, unsigned long *len); | |
3321 \end{alltt} | |
3322 This will determine the length of the DER encoding of the integer ``num'' and store it in ``len''. | |
3323 | |
3324 \subsection{Multiple INTEGER types} | |
3325 To simplify the DER encoding/decoding there are two functions two handle multple types at once. | |
3326 | |
3327 \index{der\_put\_multi\_integer()} | |
3328 \index{der\_get\_multi\_integer()} | |
3329 \begin{alltt} | |
3330 int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...); | |
3331 int der_get_multi_integer(const unsigned char *src, unsigned long *inlen, mp_int *num, ...); | |
3332 \end{alltt} | |
3333 | |
3334 These will handle multiple encodings/decodings at once. They work like their single operand counterparts | |
3335 except they handle a \textbf{NULL} terminated list of operands. | |
3336 | |
3337 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3338 #include <tomcrypt.h> |
143 | 3339 int main(void) |
3340 { | |
3341 mp_int a, b, c, d; | |
3342 unsigned char buffer[1000]; | |
3343 unsigned long len; | |
3344 int err; | |
3345 | |
3346 /* init a,b,c,d with some values ... */ | |
3347 | |
3348 /* ok we want to store them now... */ | |
3349 len = sizeof(buffer); | |
3350 if ((err = der_put_multi_integer(buffer, &len, | |
3351 &a, &b, &c, &d, NULL)) != CRYPT_OK) { | |
3352 // error | |
3353 } | |
3354 printf("I stored %lu bytes in buf\n", len); | |
3355 | |
3356 /* ok say we want to get them back for fun */ | |
3357 /* len set previously...otherwise set it to the size of the packet */ | |
3358 if ((err = der_get_multi_integer(buffer, &len, | |
3359 &a, &b, &c, &d, NULL)) != CRYPT_OK) { | |
3360 // error | |
3361 } | |
3362 printf("I read %lu bytes from buf\n", len); | |
3363 } | |
3364 \end{verbatim} | |
3365 \section{Password Based Cryptography} | |
3366 \subsection{PKCS \#5} | |
3367 In order to securely handle user passwords for the purposes of creating session keys and chaining IVs the PKCS \#5 was drafted. PKCS \#5 | |
3368 is made up of two algorithms, Algorithm One and Algorithm Two. Algorithm One is the older fairly limited algorithm which has been implemented | |
3369 for completeness. Algorithm Two is a bit more modern and more flexible to work with. | |
3370 | |
3371 \subsection{Algorithm One} | |
3372 Algorithm One accepts as input a password, an 8--byte salt and an iteration counter. The iteration counter is meant to act as delay for | |
3373 people trying to brute force guess the password. The higher the iteration counter the longer the delay. This algorithm also requires a hash | |
3374 algorithm and produces an output no longer than the output of the hash. | |
3375 | |
3376 \index{pkcs\_5\_alg1()} | |
3377 \begin{alltt} | |
3378 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, | |
3379 const unsigned char *salt, | |
3380 int iteration_count, int hash_idx, | |
3381 unsigned char *out, unsigned long *outlen) | |
3382 \end{alltt} | |
3383 Where ``password'' is the users password. Since the algorithm allows binary passwords you must also specify the length in ``password\_len''. | |
3384 The ``salt'' is a fixed size 8--byte array which should be random for each user and session. The ``iteration\_count'' is the delay desired | |
3385 on the password. The ``hash\_idx'' is the index of the hash you wish to use in the descriptor table. | |
3386 | |
3387 The output of length upto ``outlen'' is stored in ``out''. If ``outlen'' is initially larger than the size of the hash functions output | |
3388 it is set to the number of bytes stored. If it is smaller than not all of the hash output is stored in ``out''. | |
3389 | |
3390 \subsection{Algorithm Two} | |
3391 | |
3392 Algorithm Two is the recommended algorithm for this task. It allows variable length salts and can produce outputs larger than the | |
3393 hash functions output. As such it can easily be used to derive session keys for ciphers and MACs as well initial vectors as required | |
3394 from a single password and invokation of this algorithm. | |
3395 | |
3396 \index{pkcs\_5\_alg2()} | |
3397 \begin{alltt} | |
3398 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, | |
3399 const unsigned char *salt, unsigned long salt_len, | |
3400 int iteration_count, int hash_idx, | |
3401 unsigned char *out, unsigned long *outlen) | |
3402 \end{alltt} | |
3403 Where ``password'' is the users password. Since the algorithm allows binary passwords you must also specify the length in ``password\_len''. | |
3404 The ``salt'' is an array of size ``salt\_len''. It should be random for each user and session. The ``iteration\_count'' is the delay desired | |
3405 on the password. The ``hash\_idx'' is the index of the hash you wish to use in the descriptor table. The output of length upto | |
3406 ``outlen'' is stored in ``out''. | |
3407 | |
3408 \begin{alltt} | |
3409 /* demo to show how to make session state material from a password */ | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3410 #include <tomcrypt.h> |
143 | 3411 int main(void) |
3412 \{ | |
3413 unsigned char password[100], salt[100], | |
3414 cipher_key[16], cipher_iv[16], | |
3415 mac_key[16], outbuf[48]; | |
3416 int err, hash_idx; | |
3417 unsigned long outlen, password_len, salt_len; | |
3418 | |
3419 /* register hash and get it's idx .... */ | |
3420 | |
3421 /* get users password and make up a salt ... */ | |
3422 | |
3423 /* create the material (100 iterations in algorithm) */ | |
3424 outlen = sizeof(outbuf); | |
3425 if ((err = pkcs_5_alg2(password, password_len, salt, salt_len, | |
3426 100, hash_idx, outbuf, &outlen)) != CRYPT_OK) \{ | |
3427 /* error handle */ | |
3428 \} | |
3429 | |
3430 /* now extract it */ | |
3431 memcpy(cipher_key, outbuf, 16); | |
3432 memcpy(cipher_iv, outbuf+16, 16); | |
3433 memcpy(mac_key, outbuf+32, 16); | |
3434 | |
3435 /* use material (recall to store the salt in the output) */ | |
3436 \} | |
3437 \end{alltt} | |
3438 | |
3439 | |
3 | 3440 \chapter{Miscellaneous} |
3441 \section{Base64 Encoding and Decoding} | |
3442 The library provides functions to encode and decode a RFC1521 base64 coding scheme. This means that it can decode what it | |
3443 encodes but the format used does not comply to any known standard. The characters used in the mappings are: | |
3444 \begin{verbatim} | |
3445 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ | |
3446 \end{verbatim} | |
3447 Those characters should are supported in virtually any 7-bit ASCII system which means they can be used for transport over | |
3448 common e-mail, usenet and HTTP mediums. The format of an encoded stream is just a literal sequence of ASCII characters | |
3449 where a group of four represent 24-bits of input. The first four chars of the encoders output is the length of the | |
3450 original input. After the first four characters is the rest of the message. | |
3451 | |
3452 Often it is desirable to line wrap the output to fit nicely in an e-mail or usenet posting. The decoder allows you to | |
3453 put any character (that is not in the above sequence) in between any character of the encoders output. You may not however, | |
3454 break up the first four characters. | |
3455 | |
3456 To encode a binary string in base64 call: | |
3457 \index{base64\_encode()} \index{base64\_decode()} | |
3458 \begin{verbatim} | |
3459 int base64_encode(const unsigned char *in, unsigned long len, | |
3460 unsigned char *out, unsigned long *outlen); | |
3461 \end{verbatim} | |
3462 Where ``in'' is the binary string and ``out'' is where the ASCII output is placed. You must set the value of ``outlen'' prior | |
3463 to calling this function and it sets the length of the base64 output in ``outlen'' when it is done. To decode a base64 | |
3464 string call: | |
3465 \begin{verbatim} | |
3466 int base64_decode(const unsigned char *in, unsigned long len, | |
3467 unsigned char *out, unsigned long *outlen); | |
3468 \end{verbatim} | |
3469 | |
3470 \section{The Multiple Precision Integer Library (MPI)} | |
3471 The library comes with a copy of LibTomMath which is a multiple precision integer library written by the | |
3472 author of LibTomCrypt. LibTomMath is a trivial to use ANSI C compatible large integer library which is free | |
3473 for all uses and is distributed freely. | |
3474 | |
3475 At the heart of all the functions is the data type ``mp\_int'' (defined in tommath.h). This data type is what | |
3476 will hold all large integers. In order to use an mp\_int one must initialize it first, for example: | |
3477 \begin{verbatim} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3478 #include <tomcrypt.h> /* tomcrypt.h includes mpi.h automatically */ |
3 | 3479 int main(void) |
3480 { | |
3481 mp_int bignum; | |
3482 | |
3483 /* initialize it */ | |
3484 mp_init(&bignum); | |
3485 | |
3486 return 0; | |
3487 } | |
3488 \end{verbatim} | |
3489 If you are unfamiliar with the syntax of C the \& symbol is used to pass the address of ``bignum'' to the function. All | |
3490 LibTomMath functions require the address of the parameters. To free the memory of a mp\_int use (for example): | |
3491 \begin{verbatim} | |
3492 mp_clear(&bignum); | |
3493 \end{verbatim} | |
3494 | |
3495 The functions also have the basic form of one of the following: | |
3496 \begin{verbatim} | |
3497 mp_XXX(mp_int *a); | |
3498 mp_XXX(mp_int *a, mp_int *b, mp_int *c); | |
3499 mp_XXX(mp_int *a, mp_int *b, mp_int *c, mp_int *d); | |
3500 \end{verbatim} | |
3501 | |
3502 Where they perform some operation and store the result in the mp\_int variable passed on the far right. | |
3503 For example, to compute $c = a + b \mbox{ }(\mbox{mod }m)$ you would call: | |
3504 \begin{verbatim} | |
3505 mp_addmod(&a, &b, &m, &c); | |
3506 \end{verbatim} | |
3507 | |
3508 \subsection{Binary Forms of ``mp\_int'' Variables} | |
3509 | |
3510 Often it is required to store a ``mp\_int'' in binary form for transport (e.g. exporting a key, packet | |
3511 encryption, etc.). LibTomMath includes two functions to help when exporting numbers: | |
3512 \begin{verbatim} | |
3513 int mp_raw_size(mp_int *num); | |
3514 mp_toraw(&num, buf); | |
3515 \end{verbatim} | |
3516 | |
3517 The former function gives the size in bytes of the raw format and the latter function actually stores the raw data. All | |
3518 ``mp\_int'' numbers are stored in big endian form (like PKCS demands) with the first byte being the sign of the number. The | |
3519 ``rsa\_exptmod()'' function differs slightly since it will take the input in the form exactly as PKCS demands (without the | |
3520 leading sign byte). All other functions include the sign byte (since its much simpler just to include it). The sign byte | |
3521 must be zero for positive numbers and non-zero for negative numbers. For example, | |
3522 the sequence: | |
3523 \begin{verbatim} | |
3524 00 FF 30 04 | |
3525 \end{verbatim} | |
3526 Represents the integer $255 \cdot 256^2 + 48 \cdot 256^1 + 4 \cdot 256^0$ or 16,723,972. | |
3527 | |
3528 To read a binary string back into a ``mp\_int'' call: | |
3529 \begin{verbatim} | |
3530 mp_read_raw(mp_int *num, unsigned char *str, int len); | |
3531 \end{verbatim} | |
3532 Where ``num'' is where to store it, ``str'' is the binary string (including the leading sign byte) and ``len'' is the | |
3533 length of the binary string. | |
3534 | |
3535 \subsection{Primality Testing} | |
3536 \index{Primality Testing} | |
3537 The library includes primality testing and random prime functions as well. The primality tester will perform the test in | |
3538 two phases. First it will perform trial division by the first few primes. Second it will perform eight rounds of the | |
3539 Rabin-Miller primality testing algorithm. If the candidate passes both phases it is declared prime otherwise it is declared | |
3540 composite. No prime number will fail the two phases but composites can. Each round of the Rabin-Miller algorithm reduces | |
3541 the probability of a pseudo-prime by $1 \over 4$ therefore after sixteen rounds the probability is no more than | |
3542 $\left ( { 1 \over 4 } \right )^{8} = 2^{-16}$. In practice the probability of error is in fact much lower than that. | |
3543 | |
3544 When making random primes the trial division step is in fact an optimized implementation of ``Implementation of Fast RSA Key Generation on Smart Cards''\footnote{Chenghuai Lu, Andre L. M. dos Santos and Francisco R. Pimentel}. | |
3545 In essence a table of machine-word sized residues are kept of a candidate modulo a set of primes. When the candiate | |
3546 is rejected and ultimately incremented to test the next number the residues are updated without using multi-word precision | |
3547 math operations. As a result the routine can scan ahead to the next number required for testing with very little work | |
3548 involved. | |
3549 | |
3550 In the event that a composite did make it through it would most likely cause the the algorithm trying to use it to fail. For | |
3551 instance, in RSA two primes $p$ and $q$ are required. The order of the multiplicative sub-group (modulo $pq$) is given | |
3552 as $\phi(pq)$ or $(p - 1)(q - 1)$. The decryption exponent $d$ is found as $de \equiv 1\mbox{ }(\mbox{mod } \phi(pq))$. If either $p$ or $q$ is composite the value of $d$ will be incorrect and the user | |
3553 will not be able to sign or decrypt messages at all. Suppose $p$ was prime and $q$ was composite this is just a variation of | |
3554 the multi-prime RSA. Suppose $q = rs$ for two primes $r$ and $s$ then $\phi(pq) = (p - 1)(r - 1)(s - 1)$ which clearly is | |
3555 not equal to $(p - 1)(rs - 1)$. | |
3556 | |
3557 These are not technically part of the LibTomMath library but this is the best place to document them. | |
3558 To test if a ``mp\_int'' is prime call: | |
3559 \begin{verbatim} | |
3560 int is_prime(mp_int *N, int *result); | |
3561 \end{verbatim} | |
3562 This puts a one in ``result'' if the number is probably prime, otherwise it places a zero in it. It is assumed that if | |
3563 it returns an error that the value in ``result'' is undefined. To make | |
3564 a random prime call: | |
3565 \begin{verbatim} | |
3566 int rand_prime(mp_int *N, unsigned long len, prng_state *prng, int wprng); | |
3567 \end{verbatim} | |
3568 Where ``len'' is the size of the prime in bytes ($2 \le len \le 256$). You can set ``len'' to the negative size you want | |
3569 to get a prime of the form $p \equiv 3\mbox{ }(\mbox{mod } 4)$. So if you want a 1024-bit prime of this sort pass | |
3570 ``len = -128'' to the function. Upon success it will return {\bf CRYPT\_OK} and ``N'' will contain an integer which | |
3571 is very likely prime. | |
3572 | |
3573 \chapter{Programming Guidelines} | |
3574 | |
3575 \section{Secure Pseudo Random Number Generators} | |
3576 Probably the singal most vulnerable point of any cryptosystem is the PRNG. Without one generating and protecting secrets | |
3577 would be impossible. The requirement that one be setup correctly is vitally important and to address this point the library | |
3578 does provide two RNG sources that will address the largest amount of end users as possible. The ``sprng'' PRNG provided | |
3579 provides and easy to access source of entropy for any application on a *NIX or Windows computer. | |
3580 | |
3581 However, when the end user is not on one of these platforms the application developer must address the issue of finding | |
3582 entropy. This manual is not designed to be a text on cryptography. I would just like to highlight that when you design | |
3583 a cryptosystem make sure the first problem you solve is getting a fresh source of entropy. | |
3584 | |
3585 \section{Preventing Trivial Errors} | |
3586 Two simple ways to prevent trivial errors is to prevent overflows and to check the return values. All of the functions | |
3587 which output variable length strings will require you to pass the length of the destination. If the size of your output | |
3588 buffer is smaller than the output it will report an error. Therefore, make sure the size you pass is correct! | |
3589 | |
3590 Also virtually all of the functions return an error code or {\bf CRYPT\_OK}. You should detect all errors as simple | |
3591 typos or such can cause algorithms to fail to work as desired. | |
3592 | |
3593 \section{Registering Your Algorithms} | |
3594 To avoid linking and other runtime errors it is important to register the ciphers, hashes and PRNGs you intend to use | |
3595 before you try to use them. This includes any function which would use an algorithm indirectly through a descriptor table. | |
3596 | |
3597 A neat bonus to the registry system is that you can add external algorithms that are not part of the library without | |
3598 having to hack the library. For example, suppose you have a hardware specific PRNG on your system. You could easily | |
3599 write the few functions required plus a descriptor. After registering your PRNG all of the library functions that | |
3600 need a PRNG can instantly take advantage of it. | |
3601 | |
3602 \section{Key Sizes} | |
3603 | |
3604 \subsection{Symmetric Ciphers} | |
3605 For symmetric ciphers use as large as of a key as possible. For the most part ``bits are cheap'' so using a 256-bit key | |
3606 is not a hard thing todo. | |
3607 | |
3608 \subsection{Assymetric Ciphers} | |
3609 The following chart gives the work factor for solving a DH/RSA public key using the NFS. The work factor for a key of order | |
3610 $n$ is estimated to be | |
3611 \begin{equation} | |
3612 e^{1.923 \cdot ln(n)^{1 \over 3} \cdot ln(ln(n))^{2 \over 3}} | |
3613 \end{equation} | |
3614 | |
3615 Note that $n$ is not the bit-length but the magnitude. For example, for a 1024-bit key $n = 2^{1024}$. The work required | |
3616 is: | |
3617 \begin{center} | |
3618 \begin{tabular}{|c|c|} | |
3619 \hline RSA/DH Key Size (bits) & Work Factor ($log_2$) \\ | |
3620 \hline 512 & 63.92 \\ | |
3621 \hline 768 & 76.50 \\ | |
3622 \hline 1024 & 86.76 \\ | |
3623 \hline 1536 & 103.37 \\ | |
3624 \hline 2048 & 116.88 \\ | |
3625 \hline 2560 & 128.47 \\ | |
3626 \hline 3072 & 138.73 \\ | |
3627 \hline 4096 & 156.49 \\ | |
3628 \hline | |
3629 \end{tabular} | |
3630 \end{center} | |
3631 | |
3632 The work factor for ECC keys is much higher since the best attack is still fully exponentional. Given a key of magnitude | |
3633 $n$ it requires $\sqrt n$ work. The following table sumarizes the work required: | |
3634 \begin{center} | |
3635 \begin{tabular}{|c|c|} | |
3636 \hline ECC Key Size (bits) & Work Factor ($log_2$) \\ | |
3637 \hline 160 & 80 \\ | |
3638 \hline 192 & 96 \\ | |
3639 \hline 224 & 112 \\ | |
3640 \hline 256 & 128 \\ | |
3641 \hline 384 & 192 \\ | |
3642 \hline 521 & 260.5 \\ | |
3643 \hline | |
3644 \end{tabular} | |
3645 \end{center} | |
3646 | |
3647 Using the above tables the following suggestions for key sizes seems appropriate: | |
3648 \begin{center} | |
3649 \begin{tabular}{|c|c|c|} | |
3650 \hline Security Goal & RSA/DH Key Size (bits) & ECC Key Size (bits) \\ | |
3651 \hline Short term (less than a year) & 1024 & 160 \\ | |
3652 \hline Short term (less than five years) & 1536 & 192 \\ | |
3653 \hline Long Term (less than ten years) & 2560 & 256 \\ | |
3654 \hline | |
3655 \end{tabular} | |
3656 \end{center} | |
3657 | |
3658 \section{Thread Safety} | |
3659 The library is not thread safe but several simple precautions can be taken to avoid any problems. The registry functions | |
3660 such as register\_cipher() are not thread safe no matter what you do. Its best to call them from your programs initializtion | |
3661 code before threads are initiated. | |
3662 | |
3663 The rest of the code uses state variables you must pass it such as hash\_state, hmac\_state, etc. This means that if each | |
3664 thread has its own state variables then they will not affect each other. This is fairly simple with symmetric ciphers | |
3665 and hashes. However, the keyring and PRNG support is something the threads will want to share. The simplest workaround | |
3666 is create semaphores or mutexes around calls to those functions. | |
3667 | |
3668 Since C does not have standard semaphores this support is not native to Libtomcrypt. Even a C based semaphore is not entire | |
3669 possible as some compilers may ignore the ``volatile'' keyword or have multiple processors. Provide your host application | |
3670 is modular enough putting the locks in the right place should not bloat the code significantly and will solve all thread | |
3671 safety issues within the library. | |
3672 | |
143 | 3673 \chapter{Configuring and Building the Library} |
3 | 3674 \section{Introduction} |
3675 The library is fairly flexible about how it can be built, used and generally distributed. Additions are being made with | |
143 | 3676 each new release that will make the library even more flexible. Each of the classes of functions can be disabled during |
3677 the build process to make a smaller library. This is particularly useful for shared libraries. | |
3678 | |
3679 \section{Building a Static Library} | |
3680 The library can be built as a static library which is generally the simplest and most portable method of | |
3681 building the library. With a CC or GCC equipped platform you can issue the following | |
3682 | |
3683 \begin{alltt} | |
3684 make install_lib | |
3685 \end{alltt} | |
3686 | |
3687 Which will build the library and install it in /usr/lib (as well as the headers in /usr/include). The destination | |
3688 directory of the library and headers can be changed by editing ``makefile''. The variable LIBNAME controls | |
3689 where the library is to be installed and INCNAME controls where the headers are to be installed. A developer can | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3690 then use the library by including ``tomcrypt.h'' in their program and linking against ``libtomcrypt.a''. |
143 | 3691 |
3692 A static library can also be built with the Intel C Compiler (ICC) by issuing the following | |
3693 | |
3694 \begin{alltt} | |
3695 make -f makefile.icc install | |
3696 \end{alltt} | |
3697 | |
3698 This will also build ``libtomcrypt.a'' except that it will use ICC. Additionally Microsoft's Visual C 6.00 can be used | |
3699 by issuing | |
3700 | |
3701 \begin{alltt} | |
3702 nmake -f makefile.msvc | |
3703 \end{alltt} | |
3704 | |
3705 You will have to manually copy ``tomcrypt.lib'' and the headers to your MSVC lib/inc directories. | |
3706 | |
3707 \subsection{MPI Control} | |
3708 If you already have LibTomMath installed you can safely remove it from the build. By commenting the line | |
3709 in the appropriate makefile which starts with | |
3710 | |
3711 \begin{alltt} | |
3712 MPIOBJECT=mpi | |
3713 \end{alltt} | |
3714 | |
3715 Simply place a \# at the start and re-build the library. To properly link applications you will have to also | |
3716 link in LibTomMath. Removing MPI has the benefit of cutting down the library size as well potentially have access | |
3717 to the latest mpi. | |
3718 | |
3719 \section{Building a Shared Library} | |
3720 LibTomCrypt can also be built as a shared library (.so, .dll, etc...). With non-Windows platforms the assumption | |
3721 of the presence of gcc and ``libtool'' has been made. These are fairly common on Unix/Linux/BSD platforms. To | |
3722 build a .so shared library issue | |
3723 | |
3724 \begin{alltt} | |
3725 make -f makefile.shared | |
3726 \end{alltt} | |
3727 This will use libtool and gcc to build a shared library ``libtomcrypt.la'' as well as a static library ``libtomcrypt.a'' | |
3728 and install them into /usr/lib (and the headers into /usr/include). To link your application you should use the | |
3729 libtool program in ``--mode=link''. | |
3730 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3731 \section{tomcrypt\_cfg.h} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3732 The file ``tomcrypt\_cfg.h'' is what lets you control various high level macros which control the behaviour |
143 | 3733 of the library. |
3 | 3734 |
3735 \subsubsection{ARGTYPE} | |
3736 This lets you control how the \_ARGCHK macro will behave. The macro is used to check pointers inside the functions against | |
3737 NULL. There are three settings for ARGTYPE. When set to 0 it will have the default behaviour of printing a message to | |
3738 stderr and raising a SIGABRT signal. This is provided so all platforms that use libtomcrypt can have an error that functions | |
3739 similarly. When set to 1 it will simply pass on to the assert() macro. When set to 2 it will resolve to a empty macro | |
3740 and no error checking will be performed. | |
3741 | |
3742 \subsubsection{Endianess} | |
3743 There are five macros related to endianess issues. For little endian platforms define, ENDIAN\_LITTLE. For big endian | |
3744 platforms define ENDIAN\_BIG. Similarly when the default word size of an ``unsigned long'' is 32-bits define ENDIAN\_32BITWORD | |
3745 or define ENDIAN\_64BITWORD when its 64-bits. If you do not define any of them the library will automatically use ENDIAN\_NEUTRAL | |
143 | 3746 which will work on all platforms. |
3747 | |
3748 Currently LibTomCrypt will detect x86-32 and x86-64 running GCC as well as x86-32 running MSVC. | |
3 | 3749 |
3750 \section{The Configure Script} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3751 There are also options you can specify from the configure script or ``tomcrypt\_custom.h''. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3752 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3753 \subsection{X memory routines} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3754 At the top of tomcrypt\_custom.h are four macros denoted as XMALLOC, XCALLOC, XREALLOC and XFREE which resolve to |
143 | 3755 the name of the respective functions. This lets you substitute in your own memory routines. If you substitute in |
3756 your own functions they must behave like the standard C library functions in terms of what they expect as input and | |
3757 output. By default the library uses the standard C routines. | |
3 | 3758 |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3759 \subsection{X clock routines} |
3 | 3760 The rng\_get\_bytes() function can call a function that requires the clock() function. These macros let you override |
3761 the default clock() used with a replacement. By default the standard C library clock() function is used. | |
3762 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3763 \subsection{NO\_FILE} |
3 | 3764 During the build if NO\_FILE is defined then any function in the library that uses file I/O will not call the file I/O |
143 | 3765 functions and instead simply return CRYPT\_NOP. This should help resolve any linker errors stemming from a lack of |
3 | 3766 file I/O on embedded platforms. |
3767 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3768 \subsection{CLEAN\_STACK} |
143 | 3769 When this functions is defined the functions that store key material on the stack will clean up afterwards. |
3770 Assumes that you have no memory paging with the stack. | |
3771 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3772 \subsection{LTC\_TEST} |
143 | 3773 When this has been defined the various self--test functions (for ciphers, hashes, prngs, etc) are included in the build. |
3774 When this has been undefined the tests are removed and if called will return CRYPT\_NOP. | |
3 | 3775 |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3776 \subsection{Symmetric Ciphers, One-way Hashes, PRNGS and Public Key Functions} |
143 | 3777 There are a plethora of macros for the ciphers, hashes, PRNGs and public key functions which are fairly |
3778 self-explanatory. When they are defined the functionality is included otherwise it is not. There are some | |
3779 dependency issues which are noted in the file. For instance, Yarrow requires CTR chaining mode, a block | |
3780 cipher and a hash function. | |
3 | 3781 |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3782 \subsection{TWOFISH\_SMALL and TWOFISH\_TABLES} |
3 | 3783 Twofish is a 128-bit symmetric block cipher that is provided within the library. The cipher itself is flexible enough |
3784 to allow some tradeoffs in the implementation. When TWOFISH\_SMALL is defined the scheduled symmetric key for Twofish | |
3785 requires only 200 bytes of memory. This is achieved by not pre-computing the substitution boxes. Having this | |
3786 defined will also greatly slow down the cipher. When this macro is not defined Twofish will pre-compute the | |
3787 tables at a cost of 4KB of memory. The cipher will be much faster as a result. | |
3788 | |
3789 When TWOFISH\_TABLES is defined the cipher will use pre-computed (and fixed in code) tables required to work. This is | |
3790 useful when TWOFISH\_SMALL is defined as the table values are computed on the fly. When this is defined the code size | |
3791 will increase by approximately 500 bytes. If this is defined but TWOFISH\_SMALL is not the cipher will still work but | |
3792 it will not speed up the encryption or decryption functions. | |
3793 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3794 \subsection{GCM\_TABLES} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3795 When defined GCM will use a 64KB table (per GCM state) which will greatly lower up the per--packet latency. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3796 It also increases the initialization time. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3797 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3798 \subsection{SMALL\_CODE} |
3 | 3799 When this is defined some of the code such as the Rijndael and SAFER+ ciphers are replaced with smaller code variants. |
3800 These variants are slower but can save quite a bit of code space. | |
3801 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3802 \subsection{LTC\_FAST} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3803 This mode (autodetected with x86\_32,x86\_64 platforms with GCC or MSVC) configures various routines such as ctr\_encrypt() or |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3804 cbc\_encrypt() that it can safely XOR multiple octets in one step by using a larger data type. This has the benefit of |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3805 cutting down the overhead of the respective functions. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3806 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3807 This mode does have one downside. It can cause unaligned reads from memory if you are not careful with the functions. This is why |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3808 it has been enabled by default only for the x86 class of processors where unaligned accesses are allowed. Technically LTC\_FAST |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3809 is not ``portable'' since unaligned accesses are not covered by the ISO C specifications. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3810 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3811 In practice however, you can use it on pretty much any platform (even MIPS) with care. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3812 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3813 By design the ``fast'' mode functions won't get unaligned on their own. For instance, if you call ctr\_encrypt() right after calling |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3814 ctr\_start() and all the inputs you gave are aligned than ctr\_encrypt() will perform aligned memory operations only. However, if you |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3815 call ctr\_encrypt() with an odd amount of plaintext then call it again the CTR pad (the IV) will be partially used. This will |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3816 cause the ctr routine to first use up the remaining pad bytes. Then if there are enough plaintext bytes left it will use |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3817 whole word XOR operations. These operations will be unaligned. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3818 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3819 The simplest precaution is to make sure you process all data in power of two blocks and handle ``remainder'' at the end. e.g. If you are |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3820 CTR'ing a long stream process it in blocks of (say) four kilobytes and handle any remaining incomplete blocks at the end of the stream. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3821 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3822 If you do plan on using the ``LTC\_FAST'' mode you have to also define a ``LTC\_FAST\_TYPE'' macro which resolves to an optimal sized |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3823 data type you can perform integer operations with. Ideally it should be four or eight bytes since it must properly divide the size |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3824 of your block cipher (e.g. 16 bytes for AES). This means sadly if you're on a platform with 57--bit words (or something) you can't |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3825 use this mode. So sad. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3826 |
143 | 3827 \section{MPI Tweaks} |
3828 \subsection{RSA Only Tweak} | |
3829 If you plan on only using RSA with moduli in the range of 1024 to 2560 bits you can enable a series of tweaks | |
3830 to reduce the library size. Follow these steps | |
3831 | |
3832 \begin{enumerate} | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3833 \item Undefine MDSA, MECC and MDH from tomcrypt\_custom.h |
143 | 3834 \item Undefine LTM\_ALL from tommath\_superclass.h |
3835 \item Define SC\_RSA\_1 from tommath\_superclass.h | |
3836 \item Rebuild the library. | |
3837 \end{enumerate} | |
3838 | |
191
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3839 \chapter{Optimizations} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3840 \section{Introduction} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3841 The entire API was designed with plug and play in mind at the low level. That is you can swap out any cipher, hash or PRNG and dependent API will not require |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3842 updating. This has the nice benefit that I can add ciphers not have to re--write large portions of the API. For the most part LibTomCrypt has also been written |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3843 to be highly portable and easy to build out of the box on pretty much any platform. As such there are no assembler inlines throughout the code, I make no assumptions |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3844 about the platform, etc... |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3845 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3846 That works well for most cases but there are times where time is of the essence. This API also allows optimized routines to be dropped in--place of the existing |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3847 portable routines. For instance, hand optimized assembler versions of AES could be provided and any existing function that uses the cipher could automatically use |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3848 the optimized code without re--writing. This also paves the way for hardware drivers that can access hardware accelerated cryptographic devices. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3849 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3850 At the heart of this flexibility is the ``descriptor'' system. A descriptor is essentially just a C ``struct'' which describes the algorithm and provides pointers |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3851 to functions that do the work. For a given class of operation (e.g. cipher, hash, prng) the functions have identical prototypes which makes development simple. In most |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3852 dependent routines all a developer has to do is register\_XXX() the descriptor and they're set. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3853 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3854 \section{Ciphers} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3855 The ciphers in LibTomCrypt are accessed through the ltc\_cipher\_descriptor structure. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3856 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3857 \begin{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3858 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3859 struct ltc_cipher_descriptor { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3860 /** name of cipher */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3861 char *name; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3862 /** internal ID */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3863 unsigned char ID; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3864 /** min keysize (octets) */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3865 int min_key_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3866 /** max keysize (octets) */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3867 max_key_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3868 /** block size (octets) */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3869 block_length, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3870 /** default number of rounds */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3871 default_rounds; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3872 /** Setup the cipher |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3873 @param key The input symmetric key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3874 @param keylen The length of the input key (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3875 @param num_rounds The requested number of rounds (0==default) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3876 @param skey [out] The destination of the scheduled key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3877 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3878 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3879 int (*setup)(const unsigned char *key, int keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3880 int num_rounds, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3881 /** Encrypt a block |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3882 @param pt The plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3883 @param ct [out] The ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3884 @param skey The scheduled key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3885 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3886 void (*ecb_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3887 unsigned char *ct, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3888 /** Decrypt a block |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3889 @param ct The ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3890 @param pt [out] The plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3891 @param skey The scheduled key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3892 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3893 void (*ecb_decrypt)(const unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3894 unsigned char *pt, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3895 /** Test the block cipher |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3896 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3897 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3898 int (*test)(void); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3899 /** Determine a key size |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3900 @param keysize [in/out] The size of the key desired and the suggested size |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3901 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3902 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3903 int (*keysize)(int *keysize); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3904 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3905 /** Accelerators **/ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3906 /** Accelerated ECB encryption |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3907 @param pt Plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3908 @param ct Ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3909 @param blocks The number of complete blocks to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3910 @param skey The scheduled key context |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3911 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3912 void (*accel_ecb_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3913 unsigned char *ct, unsigned long blocks, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3914 symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3915 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3916 /** Accelerated ECB decryption |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3917 @param pt Plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3918 @param ct Ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3919 @param blocks The number of complete blocks to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3920 @param skey The scheduled key context |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3921 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3922 void (*accel_ecb_decrypt)(const unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3923 unsigned char *pt, unsigned long blocks, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3924 symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3925 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3926 /** Accelerated CBC encryption |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3927 @param pt Plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3928 @param ct Ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3929 @param blocks The number of complete blocks to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3930 @param IV The initial value (input/output) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3931 @param skey The scheduled key context |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3932 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3933 void (*accel_cbc_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3934 unsigned char *ct, unsigned long blocks, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3935 unsigned char *IV, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3936 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3937 /** Accelerated CBC decryption |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3938 @param pt Plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3939 @param ct Ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3940 @param blocks The number of complete blocks to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3941 @param IV The initial value (input/output) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3942 @param skey The scheduled key context |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3943 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3944 void (*accel_cbc_decrypt)(const unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3945 unsigned char *pt, unsigned long blocks, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3946 unsigned char *IV, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3947 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3948 /** Accelerated CTR encryption |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3949 @param pt Plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3950 @param ct Ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3951 @param blocks The number of complete blocks to process |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3952 @param IV The initial value (input/output) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3953 @param mode little or big endian counter (mode=0 or mode=1) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3954 @param skey The scheduled key context |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3955 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3956 void (*accel_ctr_encrypt)(const unsigned char *pt, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3957 unsigned char *ct, unsigned long blocks, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3958 unsigned char *IV, int mode, symmetric_key *skey); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3959 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3960 /** Accelerated CCM packet (one-shot) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3961 @param key The secret key to use |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3962 @param keylen The length of the secret key (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3963 @param nonce The session nonce [use once] |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3964 @param noncelen The length of the nonce |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3965 @param header The header for the session |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3966 @param headerlen The length of the header (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3967 @param pt [out] The plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3968 @param ptlen The length of the plaintext (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3969 @param ct [out] The ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3970 @param tag [out] The destination tag |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3971 @param taglen [in/out] The max size and resulting size of the authentication tag |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3972 @param direction Encrypt or Decrypt direction (0 or 1) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3973 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3974 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3975 void (*accel_ccm_memory)( |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3976 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3977 const unsigned char *nonce, unsigned long noncelen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3978 const unsigned char *header, unsigned long headerlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3979 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3980 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3981 unsigned char *tag, unsigned long *taglen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3982 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3983 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3984 /** Accelerated GCM packet (one shot) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3985 @param key The secret key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3986 @param keylen The length of the secret key |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3987 @param IV The initial vector |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3988 @param IVlen The length of the initial vector |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3989 @param adata The additional authentication data (header) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3990 @param adatalen The length of the adata |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3991 @param pt The plaintext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3992 @param ptlen The length of the plaintext (ciphertext length is the same) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3993 @param ct The ciphertext |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3994 @param tag [out] The MAC tag |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3995 @param taglen [in/out] The MAC tag length |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3996 @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3997 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3998 void (*accel_gcm_memory)( |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
3999 const unsigned char *key, unsigned long keylen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4000 const unsigned char *IV, unsigned long IVlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4001 const unsigned char *adata, unsigned long adatalen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4002 unsigned char *pt, unsigned long ptlen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4003 unsigned char *ct, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4004 unsigned char *tag, unsigned long *taglen, |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4005 int direction); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4006 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4007 }; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4008 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4009 \end{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4010 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4011 \subsection{Name} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4012 The ``name'' parameter specifies the name of the cipher. This is what a developer would pass to find\_cipher() to find the cipher in the descriptor |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4013 tables. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4014 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4015 \subsection{Internal ID} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4016 This is a single byte Internal ID you can use to distingish ciphers from each other. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4017 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4018 \subsection{Key Lengths} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4019 The minimum key length is ``min\_key\_length'' and is measured in octets. Similarly the maximum key length is ``max\_key\_length''. They can be equal |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4020 and both must valid key sizes for the cipher. Values in between are not assumed to be valid though they may be. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4021 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4022 \subsection{Block Length} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4023 The size of the ciphers plaintext or ciphertext is ``block\_length'' and is measured in octets. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4024 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4025 \subsection{Rounds} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4026 Some ciphers allow different number of rounds to be used. Usually you just use the default. The default round count is ``default\_rounds''. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4027 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4028 \subsection{Setup} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4029 To initialize a cipher (for ECB mode) the function setup() was provided. It accepts an array of key octets ``key'' of length ``keylen'' octets. The user |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4030 can specify the number of rounds they want through ``num\_rounds'' where $num\_rounds = 0$ means use the default. The destination of a scheduled key is stored |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4031 in ``skey''. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4032 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4033 This is where things get tricky. Currently there is no provision to allocate memory during initialization since there is no ``cipher done'' function. So you have |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4034 to either use an existing member of the symmetric\_key union or alias your own structure over top of it provided symmetric\_key is not smaller. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4035 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4036 \subsection{Single block ECB} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4037 To process a single block in ECB mode the ecb\_encrypt() and ecb\_decrypt() functions were provided. The plaintext and ciphertext buffers are allowed to overlap so you |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4038 must make sure you do not overwrite the output before you are finished with the input. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4039 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4040 \subsection{Testing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4041 The test() function is used to self--test the ``device''. It takes no arguments and returns \textbf{CRYPT\_OK} if all is working properly. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4042 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4043 \subsection{Key Sizing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4044 Occasionally a function will want to find a suitable key size to use since the input is oddly sized. The keysize() function is for this case. It accepts a |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4045 pointer to an integer which represents the desired size. The function then has to match it to the exact or a lower key size that is valid for the cipher. For |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4046 example, if the input is $25$ and $24$ is valid then it stores $24$ back in the pointed to integer. It must not round up and must return an error if the keysize |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4047 cannot be mapped to a valid key size for the cipher. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4048 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4049 \subsection{Acceleration} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4050 The next set of functions cover the accelerated functionality of the cipher descriptor. Any combination of these functions may be set to \textbf{NULL} to indicate |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4051 it is not supported. In those cases the software fallbacks are used (using the single ECB block routines). |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4052 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4053 \subsubsection{Accelerated ECB} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4054 These two functions are meant for cases where a user wants to encrypt (in ECB mode no less) an array of blocks. These functions are accessed |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4055 through the accel\_ecb\_encrypt and accel\_ecb\_decrypt pointers. The ``blocks'' count is the number of complete blocks to process. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4056 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4057 \subsubsection{Accelerated CBC} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4058 These two functions are meant for accelerated CBC encryption. These functions are accessed through the accel\_cbc\_encrypt and accel\_cbc\_decrypt pointers. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4059 The ``blocks'' value is the number of complete blocks to process. The ``IV'' is the CBC initial vector. It is an input upon calling this function and must be |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4060 updated by the function before returning. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4061 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4062 \subsubsection{Accelerated CTR} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4063 This function is meant for accelerated CTR encryption. It is accessible through the accel\_ctr\_encrypt pointer. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4064 The ``blocks'' value is the number of complete blocks to process. The ``IV'' is the CTR counter vector. It is an input upon calling this function and must be |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4065 updated by the function before returning. The ``mode'' value indicates whether the counter is big ($mode = 1$) or little ($mode = 0$) endian. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4066 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4067 This function (and the way it's called) differs from the other two since ctr\_encrypt() allows any size input plaintext. The accelerator will only be |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4068 called if the following conditions are met. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4069 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4070 \begin{enumerate} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4071 \item The accelerator is present |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4072 \item The CTR pad is empty |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4073 \item The remaining length of the input to process is greater than or equal to the block size. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4074 \end{enumerate} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4075 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4076 The ``CTR pad'' is empty when a multiple (including zero) blocks of text have been processed. That is, if you pass in seven bytes to AES--CTR mode you would have to |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4077 pass in a minimum of nine extra bytes before the accelerator could be called. The CTR accelerator must increment the counter (and store it back into the |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4078 buffer provided) before encrypting it to create the pad. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4079 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4080 The accelerator will only be used to encrypt whole blocks. Partial blocks are always handled in software. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4081 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4082 \subsubsection{Accelerated CCM} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4083 This function is meant for accelerated CCM encryption or decryption. It processes the entire packet in one call. Note that the setup() function will not |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4084 be called prior to this. This function must handle scheduling the key provided on its own. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4085 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4086 \subsubsection{Accelerated GCM} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4087 This function is meant for accelerated GCM encryption or decryption. It processes the entire packet in one call. Note that the setup() function will not |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4088 be called prior to this. This function must handle scheduling the key provided on its own. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4089 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4090 \section{One--Way Hashes} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4091 The hash functions are accessed through the ltc\_hash\_descriptor structure. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4092 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4093 \begin{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4094 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4095 struct ltc_hash_descriptor { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4096 /** name of hash */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4097 char *name; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4098 /** internal ID */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4099 unsigned char ID; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4100 /** Size of digest in octets */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4101 unsigned long hashsize; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4102 /** Input block size in octets */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4103 unsigned long blocksize; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4104 /** ASN.1 DER identifier */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4105 unsigned char DER[64]; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4106 /** Length of DER encoding */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4107 unsigned long DERlen; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4108 /** Init a hash state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4109 @param hash The hash to initialize |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4110 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4111 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4112 int (*init)(hash_state *hash); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4113 /** Process a block of data |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4114 @param hash The hash state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4115 @param in The data to hash |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4116 @param inlen The length of the data (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4117 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4118 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4119 int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4120 /** Produce the digest and store it |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4121 @param hash The hash state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4122 @param out [out] The destination of the digest |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4123 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4124 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4125 int (*done)(hash_state *hash, unsigned char *out); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4126 /** Self-test |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4127 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4128 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4129 int (*test)(void); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4130 }; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4131 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4132 \end{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4133 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4134 \subsection{Name} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4135 This is the name the hash is known by and what find\_hash() will look for. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4136 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4137 \subsection{Internal ID} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4138 This is the internal ID byte used to distinguish the hash from other hashes. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4139 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4140 \subsection{Digest Size} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4141 The ``hashsize'' variable indicates the length of the output in octets. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4142 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4143 \subsection{Block Size} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4144 The `blocksize'' variable indicates the length of input (in octets) that the hash processes in a given |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4145 invokation. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4146 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4147 \subsection{DER Identifier} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4148 This is the DER identifier (including the SEQUENCE header). This is used solely for PKCS \#1 style signatures. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4149 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4150 \subsection{Initialization} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4151 The init function initializes the hash and prepares it to process message bytes. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4152 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4153 \subsection{Process} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4154 This processes message bytes. The algorithm must accept any length of input that the hash would allow. The input is not |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4155 guaranteed to be a multiple of the block size in length. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4156 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4157 \subsection{Done} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4158 The done function terminates the hash and returns the message digest. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4159 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4160 \subsection{Acceleration} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4161 A compatible accelerator must allow processing data in any granularity which may require internal padding on the driver side. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4162 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4163 \section{Pseudo--Random Number Generators} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4164 The pseudo--random number generators are accessible through the ltc\_prng\_descriptor structure. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4165 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4166 \begin{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4167 \begin{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4168 struct ltc_prng_descriptor { |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4169 /** Name of the PRNG */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4170 char *name; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4171 /** size in bytes of exported state */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4172 int export_size; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4173 /** Start a PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4174 @param prng [out] The state to initialize |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4175 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4176 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4177 int (*start)(prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4178 /** Add entropy to the PRNG |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4179 @param in The entropy |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4180 @param inlen Length of the entropy (octets)\ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4181 @param prng The PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4182 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4183 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4184 int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4185 /** Ready a PRNG state to read from |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4186 @param prng The PRNG state to ready |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4187 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4188 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4189 int (*ready)(prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4190 /** Read from the PRNG |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4191 @param out [out] Where to store the data |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4192 @param outlen Length of data desired (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4193 @param prng The PRNG state to read from |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4194 @return Number of octets read |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4195 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4196 unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4197 /** Terminate a PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4198 @param prng The PRNG state to terminate |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4199 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4200 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4201 int (*done)(prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4202 /** Export a PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4203 @param out [out] The destination for the state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4204 @param outlen [in/out] The max size and resulting size of the PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4205 @param prng The PRNG to export |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4206 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4207 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4208 int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4209 /** Import a PRNG state |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4210 @param in The data to import |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4211 @param inlen The length of the data to import (octets) |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4212 @param prng The PRNG to initialize/import |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4213 @return CRYPT_OK if successful |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4214 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4215 int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4216 /** Self-test the PRNG |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4217 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4218 */ |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4219 int (*test)(void); |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4220 }; |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4221 \end{verbatim} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4222 \end{small} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4223 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4224 \subsection{Name} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4225 The name by which find\_prng() will find the PRNG. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4226 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4227 \subsection{Export Size} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4228 When an PRNG state is to be exported for future use you specify the space required in this variable. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4229 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4230 \subsection{Start} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4231 Initialize the PRNG and make it ready to accept entropy. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4232 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4233 \subsection{Entropy Addition} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4234 Add entropy to the PRNG state. The exact behaviour of this function depends on the particulars of the PRNG. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4235 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4236 \subsection{Ready} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4237 This function makes the PRNG ready to read from by processing the entropy added. The behaviour of this function depends |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4238 on the specific PRNG used. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4239 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4240 \subsection{Read} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4241 Read from the PRNG and return the number of bytes read. This function does not have to fill the buffer but it is best |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4242 if it does as many protocols do not retry reads and will fail on the first try. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4243 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4244 \subsection{Done} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4245 Terminate a PRNG state. The behaviour of this function depends on the particular PRNG used. |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4246 |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4247 \subsection{Exporting and Importing} |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4248 An exported PRNG state is data that the PRNG can later import to resume activity. They're not meant to resume ``the same session'' |
1c15b283127b
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
143
diff
changeset
|
4249 but should at least maintain the same level of state entropy. |
143 | 4250 |
15 | 4251 \input{crypt.ind} |
4252 | |
3 | 4253 \end{document} |