Skip to content

Commit 69f289d

Browse files
Tom St Denissjaeckel
authored andcommitted
added libtomcrypt-0.98
1 parent a21f63b commit 69f289d

39 files changed

Lines changed: 1657 additions & 222 deletions

changes

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
August 6th, 2004
2+
v0.98 -- Update to hmac_init to free all allocated memory on error
3+
-- Update to PRNG API to fix import/export functions of Fortuna and Yarrow
4+
-- Added test functions to PRNG api, RC4 now conforms ;-) [was a minor issue]
5+
-- Added the SOBER-128 PRNG based off of code donated by Greg Rose.
6+
-- Added Tech Note #4 [notes/tech0004.txt]
7+
-- Changed RC4 back [due to request]. It will now XOR the output so you can use it like
8+
a stream cipher easily.
9+
-- Update Fortuna's export() to emit a hash of each pool. This means that the accumulated
10+
entropy that was spread over all the pools isn't entirely lost when you export/import.
11+
-- Zhi Chen suggested a comment for rsa_encrypt_key() to let users know [easily] that it was
12+
PKCS #1 v2.0 padding. (updated other rsa_* functions)
13+
-- Cleaned up Noekeon to remove unrolling [wasn't required, was messy and actually slower with GCC/ICC]
14+
-- Updated RC4 so that when you feed it >256 bytes of entropy it quietly ignores additional
15+
bytes. Also removed the % from the key setup to speed it up a bit.
16+
-- Added cipher/hash/prng tests to x86_prof to help catch bugs while testing
17+
-- Made the PRNG "done" return int, fixed sprng_done to not require prng* to be non-null
18+
-- Spruced up mycrypt_custom.h to trap more errors and also help prevent LTMSSE from being defined
19+
on non-i386 platforms by accident.
20+
-- Added RSA/ECC/DH speed tests to x86_prof and cleaned it up to build with zero warnings
21+
-- Changed Fortuna to count only entropy [not the 2 byte header] added to pool[0] into the
22+
reseed mechanism.
23+
-- Added "export_size" member to prng_descriptor tables so you can know in advance the size of
24+
the exported state for any given PRNG.
25+
-- Ported over patch on LTM 0.30 [not ready to release LTM 0.31] that fixes bug in mp_mul()/mp_div()
26+
that used to result in negative zeroes when you multiplied zero by a negative integer.
27+
(patch due to "Wolfgang Ehrhardt" <Wolfgang.Ehrhardt@munich.netsurf.de>)
28+
-- Fixed rsa_*decrypt_key() and rsa_*verify_hash() to default to invalid "stat" or "res". This way
29+
if any of the higher level functions fail [before you get to the padding] the result will be in
30+
a known state]. Applied to both v2 and v1.5 padding helpers.
31+
-- Added MACs to x86_prof
32+
-- Fixed up "warnings" in x86_prof and tv_gen
33+
-- Added a "profiled" target back [for GCC 3.4 and ICC v8]. Doesn't seem to help but might be worth
34+
tinkering with.
35+
-- Beefed up load/store test in demos/test
36+
37+
++ New note, in order to use the optimized LOAD/STORE macros your platform
38+
must support unaligned 32/64 bit load/stores. The x86s support this
39+
but some [ARM for instance] do not. If your platform cannot perform
40+
unaligned operations you must use the endian neutral code which is safe for
41+
any sort of platform.
42+
143
July 23rd, 2004
244
v0.97b -- Added PKCS #1 v1.5 RSA encrypt/sign helpers (like rsa_sign_hash, etc...)
345
-- Added missing prng check to rsa_decrypt_key() [not critical as I don't use
@@ -62,7 +104,7 @@ v0.96 -- Removed GF and Keyring code
62104
-- Changed PSS/OAEP API slightly to be more consistent with other PK functions (order of arguments)
63105
-- rsa_exptmod() now pads with leading zeroes as per I2OSP.
64106
-- added error checking to yarrow code
65-
-- Mike Frysinger pointed out that tommath.h from this distro will overwrite tommath.h
107+
-- pointed out that tommath.h from this distro will overwrite tommath.h
66108
from libtommath. I changed this to ltc_tommath.h to avoid any such problems.
67109
-- Fixed bug in PSS encoder/decoder that didn't handle the MSB properly
68110
-- refactored AES, now sports an "encrypt only" descriptor which uses half as much code space.

crypt.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ const char *crypt_build_settings =
141141
" CTR\n"
142142
#endif
143143

144+
"\nMACs:\n"
145+
#if defined(HMAC)
146+
" HMAC\n"
147+
#endif
148+
#if defined(OMAC)
149+
" OMAC\n"
150+
#endif
151+
#if defined(PMAC)
152+
" PMAC\n"
153+
#endif
154+
155+
"\nENC + AUTH modes:\n"
156+
#if defined(EAX_MODE)
157+
" EAX_MODE\n"
158+
#endif
159+
#if defined(OCB_MODE)
160+
" OCB_MODE\n"
161+
#endif
162+
163+
144164
"\nPRNG:\n"
145165
#if defined(YARROW)
146166
" Yarrow\n"
@@ -151,6 +171,12 @@ const char *crypt_build_settings =
151171
#if defined(RC4)
152172
" RC4\n"
153173
#endif
174+
#if defined(FORTUNA)
175+
" Fortuna\n"
176+
#endif
177+
#if defined(SOBER128)
178+
" SOBER128\n"
179+
#endif
154180

155181
"\nPK Algs:\n"
156182
#if defined(MRSA)
@@ -197,21 +223,6 @@ const char *crypt_build_settings =
197223
#if defined(MPI)
198224
" MPI "
199225
#endif
200-
#if defined(HMAC)
201-
" HMAC "
202-
#endif
203-
#if defined(OMAC)
204-
" OMAC "
205-
#endif
206-
#if defined(PMAC)
207-
" PMAC "
208-
#endif
209-
#if defined(EAX_MODE)
210-
" EAX_MODE "
211-
#endif
212-
#if defined(OCB_MODE)
213-
" OCB_MODE "
214-
#endif
215226
#if defined(TRY_UNRANDOM_FIRST)
216227
" TRY_UNRANDOM_FIRST "
217228
#endif
@@ -229,6 +240,9 @@ const char *crypt_build_settings =
229240
#endif
230241
#if defined(NO_FILE)
231242
" NO_FILE "
243+
#endif
244+
#if defined(LTMSSE)
245+
" LTMSSE "
232246
#endif
233247
"\n"
234248
"\n\n\n"

crypt.tex

Lines changed: 148 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
\def\gap{\vspace{0.5ex}}
4848
\makeindex
4949
\begin{document}
50-
\title{LibTomCrypt \\ Version 0.97b}
50+
\title{LibTomCrypt \\ Version 0.98}
5151
\author{Tom St Denis \\
5252
\\
5353
tomstdenis@iahu.ca \\
@@ -1687,37 +1687,101 @@ \section{PMAC Support}
16871687
Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code.
16881688

16891689

1690+
1691+
1692+
16901693
\chapter{Pseudo-Random Number Generators}
16911694
\section{Core Functions}
1692-
16931695
The library provides an array of core functions for Pseudo-Random Number Generators (PRNGs) as well. A cryptographic PRNG is
16941696
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)
16951697
key generation. There is a universal structure called ``prng\_state''. To initialize a PRNG call:
1698+
\index{PRNG start}
16961699
\begin{verbatim}
16971700
int XXX_start(prng_state *prng);
16981701
\end{verbatim}
16991702

17001703
This will setup the PRNG for future use and not seed it. In order
17011704
for the PRNG to be cryptographically useful you must give it entropy. Ideally you'd have some OS level source to tap
17021705
like in UNIX (see section 5.3). To add entropy to the PRNG call:
1706+
\index{PRNG add\_entropy}
17031707
\begin{verbatim}
17041708
int XXX_add_entropy(const unsigned char *in, unsigned long len,
17051709
prng_state *prng);
17061710
\end{verbatim}
17071711

17081712
Which returns {\bf CRYPTO\_OK} if the entropy was accepted. Once you think you have enough entropy you call another
17091713
function to put the entropy into action.
1714+
\index{PRNG ready}
17101715
\begin{verbatim}
17111716
int XXX_ready(prng_state *prng);
17121717
\end{verbatim}
17131718

17141719
Which returns {\bf CRYPTO\_OK} if it is ready. Finally to actually read bytes call:
1720+
\index{PRNG read}
17151721
\begin{verbatim}
17161722
unsigned long XXX_read(unsigned char *out, unsigned long len,
17171723
prng_state *prng);
17181724
\end{verbatim}
17191725

1720-
Which returns the number of bytes read from the PRNG.
1726+
Which returns the number of bytes read from the PRNG. When you are finished with a PRNG state you call
1727+
the following.
1728+
1729+
\index{PRNG done}
1730+
\begin{verbatim}
1731+
void XXX_done(prng_state *prng);
1732+
\end{verbatim}
1733+
1734+
This will terminate a PRNG state and free any memory (if any) allocated. To export a PRNG state
1735+
so that you can later resume the PRNG call the following.
1736+
1737+
\index{PRNG export}
1738+
\begin{verbatim}
1739+
int XXX_export(unsigned char *out, unsigned long *outlen,
1740+
prng_state *prng);
1741+
\end{verbatim}
1742+
1743+
This will write a ``PRNG state'' to the buffer ``out'' of length ``outlen'' bytes. The idea of
1744+
the export is meant to be used as a ``seed file''. That is, when the program starts up there will not likely
1745+
be that much entropy available. To import a state to seed a PRNG call the following function.
1746+
1747+
\index{PRNG import}
1748+
\begin{verbatim}
1749+
int XXX_import(const unsigned char *in, unsigned long inlen,
1750+
prng_state *prng);
1751+
\end{verbatim}
1752+
1753+
This will call the start and add\_entropy functions of the given PRNG. It will use the state in
1754+
``in'' of length ``inlen'' as the initial seed. You must pass the same seed length as was exported
1755+
by the corresponding export function.
1756+
1757+
Note that importing a state will not ``resume'' the PRNG from where it left off. That is, if you export
1758+
a state, emit (say) 8 bytes and then import the previously exported state the next 8 bytes will not
1759+
specifically equal the 8 bytes you generated previously.
1760+
1761+
When a program is first executed the normal course of operation is
1762+
1763+
\begin{enumerate}
1764+
\item Gather entropy from your sources for a given period of time or number of events.
1765+
\item Start, use your entropy via add\_entropy and ready the PRNG yourself.
1766+
\end{enumerate}
1767+
1768+
When your program is finished you simply call the export function and save the state to a medium (disk,
1769+
flash memory, etc). The next time your application starts up you can detect the state, feed it to the
1770+
import function and go on your way. It is ideal that (as soon as possible) after startup you export a
1771+
fresh state. This helps in the case that the program aborts or the machine is powered down without
1772+
being given a chance to exit properly.
1773+
1774+
Note that even if you have a state to import it is important to add new entropy to the state. However,
1775+
there is less pressure to do so.
1776+
1777+
To test a PRNG for operational conformity call the following functions.
1778+
1779+
\index{PRNG test}
1780+
\begin{verbatim}
1781+
int XXX_test(void);
1782+
\end{verbatim}
1783+
1784+
This will return \textbf{CRYPT\_OK} if PRNG is operating properly.
17211785

17221786
\subsection{Remarks}
17231787

@@ -1728,8 +1792,8 @@ \subsection{Remarks}
17281792

17291793
\subsection{Example}
17301794

1731-
Below is a simple snippet to read 10 bytes from yarrow. Its important to note that this snippet is {\bf NOT} secure since
1732-
the entropy added is not random.
1795+
Below is a simple snippet to read 10 bytes from yarrow. Its important to note that this snippet is
1796+
{\bf NOT} secure since the entropy added is not random.
17331797

17341798
\begin{verbatim}
17351799
#include <mycrypt.h>
@@ -1762,10 +1826,15 @@ \section{PRNG Descriptors}
17621826
\begin{verbatim}
17631827
struct _prng_descriptor {
17641828
char *name;
1829+
int export_size; /* size in bytes of exported state */
17651830
int (*start) (prng_state *);
17661831
int (*add_entropy)(const unsigned char *, unsigned long, prng_state *);
17671832
int (*ready) (prng_state *);
17681833
unsigned long (*read)(unsigned char *, unsigned long len, prng_state *);
1834+
void (*done)(prng_state *);
1835+
int (*export)(unsigned char *, unsigned long *, prng_state *);
1836+
int (*import)(const unsigned char *, unsigned long, prng_state *);
1837+
int (*test)(void);
17691838
};
17701839
\end{verbatim}
17711840

@@ -1779,16 +1848,82 @@ \section{PRNG Descriptors}
17791848
int unregister_prng(const struct _prng_descriptor *prng);
17801849
\end{verbatim}
17811850

1782-
\subsubsection{PRNGs Provided}
1783-
Currently Yarrow (yarrow\_desc), RC4 (rc4\_desc) and the secure RNG (sprng\_desc) are provided as PRNGs within the
1784-
library.
1851+
\subsection{PRNGs Provided}
1852+
\begin{figure}[here]
1853+
\begin{center}
1854+
\begin{small}
1855+
\begin{tabular}{|c|c|l|}
1856+
\hline \textbf{Name} & \textbf{Descriptor} & \textbf{Usage} \\
1857+
\hline Yarrow & yarrow\_desc & Fast short-term PRNG \\
1858+
\hline Fortuna & fortuna\_desc & Fast long-term PRNG (recommended) \\
1859+
\hline RC4 & rc4\_desc & Stream Cipher \\
1860+
\hline SOBER-128 & sober128\_desc & Stream Cipher (also very fast PRNG) \\
1861+
\hline
1862+
\end{tabular}
1863+
\end{small}
1864+
\end{center}
1865+
\caption{List of Provided PRNGs}
1866+
\end{figure}
1867+
1868+
\subsubsection{Yarrow}
1869+
Yarrow is fast PRNG meant to collect an unspecified amount of entropy from sources
1870+
(keyboard, mouse, interrupts, etc) and produce an unbounded string of random bytes.
1871+
1872+
\textit{Note:} This PRNG is still secure for most taskings but is no longer recommended. Users
1873+
should use Fortuna instead.
1874+
1875+
\subsubsection{Fortuna}
1876+
1877+
Fortuna is a fast attack tolerant and more thoroughly designed PRNG suitable for long term
1878+
usage. It is faster than the default implementation of Yarrow\footnote{Yarrow has been implemented
1879+
to work with most cipher and hash combos based on which you have chosen to build into the library.} while
1880+
providing more security.
1881+
1882+
Fortuna is slightly less flexible than Yarrow in the sense that it only works with the AES block cipher
1883+
and SHA--256 hash function. Technically Fortuna will work with any block cipher that accepts a 256--bit
1884+
key and any hash that produces at least a 256--bit output. However, to make the implementation simpler
1885+
it has been fixed to those choices.
1886+
1887+
Fortuna is more secure than Yarrow in the sense that attackers who learn parts of the entropy being
1888+
added to the PRNG learn far less about the state than that of Yarrow. Without getting into to many
1889+
details Fortuna has the ability to recover from state determination attacks where the attacker starts
1890+
to learn information from the PRNGs output about the internal state. Yarrow on the other hand cannot
1891+
recover from that problem until new entropy is added to the pool and put to use through the ready() function.
1892+
1893+
\subsubsection{RC4}
1894+
1895+
RC4 is an old stream cipher that can also double duty as a PRNG in a pinch. You ``key'' it by
1896+
calling add\_entropy() and setup the key by calling ready(). You can only add upto 256 bytes via
1897+
add\_entropy().
1898+
1899+
When you read from RC4 the output of the RC4 algorithm is XOR'd against your buffer you provide. In this
1900+
manner you can use rc4\_read() as an encrypt (and decrypt) function.
1901+
1902+
You really shouldn't use RC4 anymore. This isn't because RC4 is weak (though biases are known to exist) just
1903+
simply that faster alternatives exist.
1904+
1905+
\subsubsection{SOBER-128}
1906+
1907+
SOBER-128 is a stream cipher designed by the QUALCOMM Australia team. Like RC4 you ``key'' it by
1908+
calling add\_entropy(). There is no need to call ready() for this PRNG as it does not do anything.
1909+
1910+
Note that this cipher has several oddities about how it operates. The first time you call
1911+
add\_entropy() that sets the cipher's key. Every other time you call the same function it sets
1912+
the cipher's IV variable. The IV mechanism allows you to encrypt several messages with the same
1913+
key and not re--use the same key material.
1914+
1915+
Unlike Yarrow and Fortuna all of the entropy (and hence security) of this algorithm rests in the data
1916+
you pass it on the first call to add\_entropy(). All buffers sent to add\_entropy() must have a length
1917+
that is a multiple of four bytes.
1918+
1919+
Like RC4 the output of SOBER--128 is XOR'ed against the buffer you provide it. In this manner you can use
1920+
sober128\_read() as an encrypt (and decrypt) function.
17851921

1786-
RC4 is provided with a PRNG interface because it is a stream cipher and not well suited for the symmetric block cipher
1787-
interface. You provide the key for RC4 via the rc4\_add\_entropy() function. By calling rc4\_ready() the key will be used
1788-
to setup the RC4 state for encryption or decryption. The rc4\_read() function has been modified from RC4 since it will
1789-
XOR the output of the RC4 keystream generator against the input buffer you provide. The following snippet will demonstrate
1790-
how to encrypt a buffer with RC4:
1922+
Since SOBER-128 has a fixed keying scheme and is very fast (faster than RC4) the ideal usage of SOBER-128 is to
1923+
key it from the output of Fortuna (or Yarrow) and use it to encrypt messages. It is also ideal for
1924+
simulations which need a high quality (and fast) stream of bytes.
17911925

1926+
\subsubsection{Example Usage}
17921927
\begin{small}
17931928
\begin{verbatim}
17941929
#include <mycrypt.h>

demos/test/cipher_hash_test.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
int cipher_hash_test(void)
66
{
7-
int x;
7+
int x;
8+
unsigned char buf[4096];
9+
unsigned long n;
10+
prng_state nprng;
811

912
/* test ciphers */
1013
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
@@ -15,6 +18,24 @@ int cipher_hash_test(void)
1518
for (x = 0; hash_descriptor[x].name != NULL; x++) {
1619
DO(hash_descriptor[x].test());
1720
}
21+
22+
/* test prngs (test, import/export */
23+
for (x = 0; prng_descriptor[x].name != NULL; x++) {
24+
DO(prng_descriptor[x].test());
25+
DO(prng_descriptor[x].start(&nprng));
26+
DO(prng_descriptor[x].add_entropy("helloworld12", 12, &nprng));
27+
DO(prng_descriptor[x].ready(&nprng));
28+
n = sizeof(buf);
29+
DO(prng_descriptor[x].export(buf, &n, &nprng));
30+
prng_descriptor[x].done(&nprng);
31+
DO(prng_descriptor[x].import(buf, n, &nprng));
32+
DO(prng_descriptor[x].ready(&nprng));
33+
if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
34+
fprintf(stderr, "Error reading from imported PRNG!\n");
35+
exit(EXIT_FAILURE);
36+
}
37+
prng_descriptor[x].done(&nprng);
38+
}
1839

1940
return 0;
2041
}

0 commit comments

Comments
 (0)