|
@@ -47,10 +47,10 @@
|
|
\def\gap{\vspace{0.5ex}}
|
|
\def\gap{\vspace{0.5ex}}
|
|
\makeindex
|
|
\makeindex
|
|
\begin{document}
|
|
\begin{document}
|
|
-\title{LibTomCrypt \\ Version 1.00}
|
|
|
|
|
|
+\title{LibTomCrypt \\ Version 1.01}
|
|
\author{Tom St Denis \\
|
|
\author{Tom St Denis \\
|
|
\\
|
|
\\
|
|
-tomstdenis@iahu.ca \\
|
|
|
|
|
|
+tomstdenis@gmail.com \\
|
|
http://libtomcrypt.org
|
|
http://libtomcrypt.org
|
|
}
|
|
}
|
|
\maketitle
|
|
\maketitle
|
|
@@ -154,14 +154,12 @@ under a public domain license:
|
|
|
|
|
|
\begin{enumerate}
|
|
\begin{enumerate}
|
|
\item rc2.c
|
|
\item rc2.c
|
|
- \item safer.c
|
|
|
|
\end{enumerate}
|
|
\end{enumerate}
|
|
|
|
|
|
`mpi.c'' was originally written by Michael Fromberger ([email protected]) but has since been replaced with
|
|
`mpi.c'' was originally written by Michael Fromberger ([email protected]) but has since been replaced with
|
|
my LibTomMath library which is public domain.
|
|
my LibTomMath library which is public domain.
|
|
|
|
|
|
-``rc2.c'' is based on publicly available code that is not attributed to a person from the given source. ``safer.c''
|
|
|
|
-was written by Richard De Moliner ([email protected]) and seems to be free for use.
|
|
|
|
|
|
+``rc2.c'' is based on publicly available code that is not attributed to a person from the given source.
|
|
|
|
|
|
The project is hereby released as public domain.
|
|
The project is hereby released as public domain.
|
|
|
|
|
|
@@ -246,8 +244,7 @@ The header file ``tomcrypt.h'' also includes ``stdio.h'', ``string.h'', ``stdlib
|
|
There are a few helper macros to make the coding process a bit easier. The first set are related to loading and storing
|
|
There are a few helper macros to make the coding process a bit easier. The first set are related to loading and storing
|
|
32/64-bit words in little/big endian format. The macros are:
|
|
32/64-bit words in little/big endian format. The macros are:
|
|
|
|
|
|
-\index{STORE32L} \index{STORE64L} \index{LOAD32L} \index{LOAD64L}
|
|
|
|
-\index{STORE32H} \index{STORE64H} \index{LOAD32H} \index{LOAD64H} \index{BSWAP}
|
|
|
|
|
|
+\index{STORE32L} \index{STORE64L} \index{LOAD32L} \index{LOAD64L} \index{STORE32H} \index{STORE64H} \index{LOAD32H} \index{LOAD64H} \index{BSWAP}
|
|
\begin{small}
|
|
\begin{small}
|
|
\begin{center}
|
|
\begin{center}
|
|
\begin{tabular}{|c|c|c|}
|
|
\begin{tabular}{|c|c|c|}
|
|
@@ -259,14 +256,14 @@ There are a few helper macros to make the coding process a bit easier. The firs
|
|
\hline STORE64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $x \to y[7 \ldots 0]$ \\
|
|
\hline STORE64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $x \to y[7 \ldots 0]$ \\
|
|
\hline LOAD32H(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $y[3 \ldots 0] \to x$ \\
|
|
\hline LOAD32H(x, y) & {\bf unsigned long} x, {\bf unsigned char} *y & $y[3 \ldots 0] \to x$ \\
|
|
\hline LOAD64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $y[7 \ldots 0] \to x$ \\
|
|
\hline LOAD64H(x, y) & {\bf unsigned long long} x, {\bf unsigned char} *y & $y[7 \ldots 0] \to x$ \\
|
|
- \hline BSWAP(x) & {\bf unsigned long} x & Swaps the byte order of x. \\
|
|
|
|
|
|
+ \hline BSWAP(x) & {\bf unsigned long} x & Swaps byte order (32--bits only) \\
|
|
\hline
|
|
\hline
|
|
\end{tabular}
|
|
\end{tabular}
|
|
\end{center}
|
|
\end{center}
|
|
\end{small}
|
|
\end{small}
|
|
|
|
|
|
There are 32 and 64-bit cyclic rotations as well:
|
|
There are 32 and 64-bit cyclic rotations as well:
|
|
-\index{ROL} \index{ROR}
|
|
|
|
|
|
+\index{ROL} \index{ROR} \index{ROL64} \index{ROR64} \index{ROLc} \index{RORc} \index{ROL64c} \index{ROR64c}
|
|
\begin{center}
|
|
\begin{center}
|
|
\begin{tabular}{|c|c|c|}
|
|
\begin{tabular}{|c|c|c|}
|
|
\hline ROL(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x << y, 0 \le y \le 31$ \\
|
|
\hline ROL(x, y) & {\bf unsigned long} x, {\bf unsigned long} y & $x << y, 0 \le y \le 31$ \\
|
|
@@ -295,7 +292,7 @@ int main(void) {
|
|
unsigned long x;
|
|
unsigned long x;
|
|
int err;
|
|
int err;
|
|
|
|
|
|
- /* ... Make up the RSA key somehow */
|
|
|
|
|
|
+ /* ... Make up the RSA key somehow ... */
|
|
|
|
|
|
/* lets export the key, set x to the size of the output buffer */
|
|
/* lets export the key, set x to the size of the output buffer */
|
|
x = sizeof(buffer);
|
|
x = sizeof(buffer);
|
|
@@ -343,6 +340,7 @@ should never use the ECB modes directly to encrypt data. Instead you should use
|
|
or use one of the provided chaining modes. All of the ciphers are written as ECB interfaces since it allows the rest of
|
|
or use one of the provided chaining modes. All of the ciphers are written as ECB interfaces since it allows the rest of
|
|
the API to grow in a modular fashion.
|
|
the API to grow in a modular fashion.
|
|
|
|
|
|
|
|
+\subsection{Key Scheduling}
|
|
All ciphers store their scheduled keys in a single data type called ``symmetric\_key''. This allows all ciphers to
|
|
All ciphers store their scheduled keys in a single data type called ``symmetric\_key''. This allows all ciphers to
|
|
have the same prototype and store their keys as naturally as possible. This also removes the need for dynamic memory
|
|
have the same prototype and store their keys as naturally as possible. This also removes the need for dynamic memory
|
|
allocation and allows you to allocate a fixed sized buffer for storing scheduled keys. All ciphers provide five visible
|
|
allocation and allows you to allocate a fixed sized buffer for storing scheduled keys. All ciphers provide five visible
|
|
@@ -361,6 +359,7 @@ that you should only used this scheduled key with the intended cipher. For exam
|
|
pass the scheduled key onto ``rc5\_ecb\_encrypt()''. All setup functions do not allocate memory off the heap so when you are
|
|
pass the scheduled key onto ``rc5\_ecb\_encrypt()''. All setup functions do not allocate memory off the heap so when you are
|
|
done with a key you can simply discard it (e.g. they can be on the stack).
|
|
done with a key you can simply discard it (e.g. they can be on the stack).
|
|
|
|
|
|
|
|
+\subsection{ECB Encryption and Decryption}
|
|
To encrypt or decrypt a block in ECB mode there are these two function classes
|
|
To encrypt or decrypt a block in ECB mode there are these two function classes
|
|
\index{Cipher Encrypt} \index{Cipher Decrypt}
|
|
\index{Cipher Encrypt} \index{Cipher Decrypt}
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
@@ -376,12 +375,16 @@ the same buffer. For the encrypt function ``pt''\footnote{pt stands for plainte
|
|
``ct''\footnote{ct stands for ciphertext.} is the output. For the decryption function it's the opposite. To test a particular
|
|
``ct''\footnote{ct stands for ciphertext.} is the output. For the decryption function it's the opposite. To test a particular
|
|
cipher against test vectors\footnote{As published in their design papers.} call the self-test function
|
|
cipher against test vectors\footnote{As published in their design papers.} call the self-test function
|
|
|
|
|
|
|
|
+\subsection{Self--Testing}
|
|
\index{Cipher Testing}
|
|
\index{Cipher Testing}
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
int XXX_test(void);
|
|
int XXX_test(void);
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
This function will return {\bf CRYPT\_OK} if the cipher matches the test vectors from the design publication it is
|
|
This function will return {\bf CRYPT\_OK} if the cipher matches the test vectors from the design publication it is
|
|
-based upon. Finally for each cipher there is a function which will help find a desired key size:
|
|
|
|
|
|
+based upon.
|
|
|
|
+
|
|
|
|
+\subsection{Key Sizing}
|
|
|
|
+For each cipher there is a function which will help find a desired key size:
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
int XXX_keysize(int *keysize);
|
|
int XXX_keysize(int *keysize);
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
@@ -407,6 +410,15 @@ int main(void)
|
|
\end{small}
|
|
\end{small}
|
|
This should indicate a keysize of sixteen bytes is suggested.
|
|
This should indicate a keysize of sixteen bytes is suggested.
|
|
|
|
|
|
|
|
+\subsection{Cipher Termination}
|
|
|
|
+When you are finished with a cipher you can de--initialize it with the done function.
|
|
|
|
+\begin{verbatim}
|
|
|
|
+void XXX_done(symmetric_key *skey);
|
|
|
|
+\end{verbatim}
|
|
|
|
+For the software based ciphers within LibTomCrypt this function will not do anything. However, user supplied
|
|
|
|
+cipher descriptors may require calls to it for resource management. To be compliant all functions which call a cipher
|
|
|
|
+setup function must also call the respective cipher done function when finished.
|
|
|
|
+
|
|
\subsection{Simple Encryption Demonstration}
|
|
\subsection{Simple Encryption Demonstration}
|
|
An example snippet that encodes a block with Blowfish in ECB mode is below.
|
|
An example snippet that encodes a block with Blowfish in ECB mode is below.
|
|
|
|
|
|
@@ -445,6 +457,10 @@ int main(void)
|
|
&skey); /* our previously scheduled key */
|
|
&skey); /* our previously scheduled key */
|
|
|
|
|
|
/* now we have decrypted ct to the original plaintext in pt */
|
|
/* now we have decrypted ct to the original plaintext in pt */
|
|
|
|
+
|
|
|
|
+ /* Terminate the cipher context */
|
|
|
|
+ blowfish_done(&skey);
|
|
|
|
+
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
@@ -470,6 +486,7 @@ size.
|
|
To facilitate automatic routines an array of cipher descriptors is provided in the array ``cipher\_descriptor''. An element
|
|
To facilitate automatic routines an array of cipher descriptors is provided in the array ``cipher\_descriptor''. An element
|
|
of this array has the following format:
|
|
of this array has the following format:
|
|
|
|
|
|
|
|
+\begin{small}
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
struct _cipher_descriptor {
|
|
struct _cipher_descriptor {
|
|
char *name;
|
|
char *name;
|
|
@@ -478,16 +495,43 @@ struct _cipher_descriptor {
|
|
max_key_length,
|
|
max_key_length,
|
|
block_length,
|
|
block_length,
|
|
default_rounds;
|
|
default_rounds;
|
|
- int (*setup)(const unsigned char *key, int keylen, int num_rounds,
|
|
|
|
- symmetric_key *skey);
|
|
|
|
- void (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct,
|
|
|
|
- symmetric_key *skey);
|
|
|
|
- void (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt,
|
|
|
|
- symmetric_key *skey);
|
|
|
|
|
|
+ int (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
|
|
|
|
+ void (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
|
|
|
|
+ void (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
|
|
int (*test)(void);
|
|
int (*test)(void);
|
|
|
|
+ void (*done)(symmetric_key *skey);
|
|
int (*keysize)(int *keysize);
|
|
int (*keysize)(int *keysize);
|
|
|
|
+
|
|
|
|
+ void (*accel_ecb_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned long blocks, symmetric_key *skey);
|
|
|
|
+ void (*accel_ecb_decrypt)(const unsigned char *ct,
|
|
|
|
+ unsigned char *pt,
|
|
|
|
+ unsigned long blocks, symmetric_key *skey);
|
|
|
|
+ void (*accel_cbc_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned long blocks, unsigned char *IV,
|
|
|
|
+ symmetric_key *skey);
|
|
|
|
+ void (*accel_cbc_decrypt)(const unsigned char *ct,
|
|
|
|
+ unsigned char *pt,
|
|
|
|
+ unsigned long blocks, unsigned char *IV,
|
|
|
|
+ symmetric_key *skey);
|
|
|
|
+ void (*accel_ctr_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned long blocks, unsigned char *IV,
|
|
|
|
+ int mode, symmetric_key *skey);
|
|
|
|
+ void (*accel_ccm_memory)(
|
|
|
|
+ const unsigned char *key, unsigned long keylen,
|
|
|
|
+ const unsigned char *nonce, unsigned long noncelen,
|
|
|
|
+ const unsigned char *header, unsigned long headerlen,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen,
|
|
|
|
+ int direction);
|
|
|
|
+
|
|
};
|
|
};
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
|
|
+\end{small}
|
|
|
|
|
|
Where ``name'' is the lower case ASCII version of the name. The fields ``min\_key\_length'' and ``max\_key\_length''
|
|
Where ``name'' is the lower case ASCII version of the name. The fields ``min\_key\_length'' and ``max\_key\_length''
|
|
are the minimum and maximum key sizes in bytes. The ``block\_length'' member is the block size of the cipher
|
|
are the minimum and maximum key sizes in bytes. The ``block\_length'' member is the block size of the cipher
|
|
@@ -722,7 +766,7 @@ block of memory with either of the three modes.
|
|
The ECB and CBC modes process blocks of the same size as the cipher at a time. Therefore they are less flexible than the
|
|
The ECB and CBC modes process blocks of the same size as the cipher at a time. Therefore they are less flexible than the
|
|
other modes.
|
|
other modes.
|
|
|
|
|
|
-\subsection{Implementation}
|
|
|
|
|
|
+\subsection{Initialization}
|
|
\index{CBC Mode} \index{CTR Mode}
|
|
\index{CBC Mode} \index{CTR Mode}
|
|
\index{OFB Mode} \index{CFB Mode}
|
|
\index{OFB Mode} \index{CFB Mode}
|
|
The library provides simple support routines for handling CBC, CTR, CFB, OFB and ECB encoded messages. Assuming the mode
|
|
The library provides simple support routines for handling CBC, CTR, CFB, OFB and ECB encoded messages. Assuming the mode
|
|
@@ -745,30 +789,32 @@ of the cipher you choose. It is important that the IV be random for each uniqu
|
|
parameters ``key'', ``keylen'' and ``num\_rounds'' are the same as in the XXX\_setup() function call. The final parameter
|
|
parameters ``key'', ``keylen'' and ``num\_rounds'' are the same as in the XXX\_setup() function call. The final parameter
|
|
is a pointer to the structure you want to hold the information for the mode of operation.
|
|
is a pointer to the structure you want to hold the information for the mode of operation.
|
|
|
|
|
|
-Both routines return {\bf CRYPT\_OK} if the cipher initialized correctly, otherwise they return an error code. To
|
|
|
|
-actually encrypt or decrypt the following routines are provided:
|
|
|
|
|
|
+Both routines return {\bf CRYPT\_OK} if the cipher initialized correctly, otherwise they return an error code.
|
|
|
|
+
|
|
|
|
+\subsection{Encryption and Decryption}
|
|
|
|
+To actually encrypt or decrypt the following routines are provided:
|
|
\index{ecb\_encrypt()} \index{ecb\_decrypt()} \index{cfb\_encrypt()} \index{cfb\_decrypt()}
|
|
\index{ecb\_encrypt()} \index{ecb\_decrypt()} \index{cfb\_encrypt()} \index{cfb\_decrypt()}
|
|
\index{cbc\_encrypt()} \index{cbc\_decrypt()} \index{ofb\_encrypt()} \index{ofb\_decrypt()} \index{ctr\_encrypt()} \index{ctr\_decrypt()}
|
|
\index{cbc\_encrypt()} \index{cbc\_decrypt()} \index{ofb\_encrypt()} \index{ofb\_decrypt()} \index{ctr\_encrypt()} \index{ctr\_decrypt()}
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
int XXX_encrypt(const unsigned char *pt, unsigned char *ct,
|
|
int XXX_encrypt(const unsigned char *pt, unsigned char *ct,
|
|
- symmetric_XXX *XXX);
|
|
|
|
-int XXX_decrypt(const unsigned char *ct, unsigned char *pt,
|
|
|
|
- symmetric_XXX *XXX);
|
|
|
|
-
|
|
|
|
-int YYY_encrypt(const unsigned char *pt, unsigned char *ct,
|
|
|
|
unsigned long len, symmetric_YYY *YYY);
|
|
unsigned long len, symmetric_YYY *YYY);
|
|
-int YYY_decrypt(const unsigned char *ct, unsigned char *pt,
|
|
|
|
|
|
+int XXX_decrypt(const unsigned char *ct, unsigned char *pt,
|
|
unsigned long len, symmetric_YYY *YYY);
|
|
unsigned long len, symmetric_YYY *YYY);
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
-Where ``XXX'' is one of (ecb, cbc) and ``YYY'' is one of (ctr, ofb, cfb). In the CTR, OFB and CFB cases ``len'' is the
|
|
|
|
-size of the buffer (as number of chars) to encrypt or decrypt. The CTR, OFB and CFB modes are order sensitive but not
|
|
|
|
|
|
+Where ``XXX'' is one of $\lbrace ecb, cbc, ctr, cfb, ofb \rbrace$.
|
|
|
|
+
|
|
|
|
+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
|
|
chunk sensitive. That is you can encrypt ``ABCDEF'' in three calls like ``AB'', ``CD'', ``EF'' or two like ``ABCDE'' and ``F''
|
|
chunk sensitive. That is you can encrypt ``ABCDEF'' in three calls like ``AB'', ``CD'', ``EF'' or two like ``ABCDE'' and ``F''
|
|
and end up with the same ciphertext. However, encrypting ``ABC'' and ``DABC'' will result in different ciphertexts. All
|
|
and end up with the same ciphertext. However, encrypting ``ABC'' and ``DABC'' will result in different ciphertexts. All
|
|
five of the modes will return {\bf CRYPT\_OK} on success from the encrypt or decrypt functions.
|
|
five of the modes will return {\bf CRYPT\_OK} on success from the encrypt or decrypt functions.
|
|
|
|
|
|
|
|
+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
|
|
|
|
+zeroes or with whatever your protocol requires).
|
|
|
|
+
|
|
To decrypt in either mode you simply perform the setup like before (recall you have to fetch the IV value you used)
|
|
To decrypt in either mode you simply perform the setup like before (recall you have to fetch the IV value you used)
|
|
and use the decrypt routine on all of the blocks.
|
|
and use the decrypt routine on all of the blocks.
|
|
|
|
|
|
|
|
+\subsection{IV Manipulation}
|
|
To change or read the IV of a previously initialized chaining mode use the following two functions.
|
|
To change or read the IV of a previously initialized chaining mode use the following two functions.
|
|
|
|
|
|
\index{cbc\_setiv()} \index{cbc\_getiv()} \index{ofb\_setiv()} \index{ofb\_getiv()} \index{cfb\_setiv()} \index{cfb\_getiv()}
|
|
\index{cbc\_setiv()} \index{cbc\_getiv()} \index{ofb\_setiv()} \index{ofb\_getiv()} \index{cfb\_setiv()} \index{cfb\_getiv()}
|
|
@@ -778,11 +824,23 @@ int XXX_getiv(unsigned char *IV, unsigned long *len, symmetric_XXX *XXX);
|
|
int XXX_setiv(const unsigned char *IV, unsigned long len, symmetric_XXX *XXX);
|
|
int XXX_setiv(const unsigned char *IV, unsigned long len, symmetric_XXX *XXX);
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
|
|
|
|
-The XXX\_getiv function will read the IV out of the chaining mode and store it into ``IV'' along with the length of the IV
|
|
|
|
|
|
+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
|
|
stored in ``len''. The XXX\_setiv will initialize the chaining mode state as if the original IV were the new IV specified. The length
|
|
stored in ``len''. The XXX\_setiv will initialize the chaining mode state as if the original IV were the new IV specified. The length
|
|
of the IV passed in must be the size of the ciphers block size.
|
|
of the IV passed in must be the size of the ciphers block size.
|
|
|
|
|
|
-The XXX\_setiv functions are handy if you wish to change the IV without re--keying the cipher.
|
|
|
|
|
|
+The XXX\_setiv() functions are handy if you wish to change the IV without re--keying the cipher.
|
|
|
|
+
|
|
|
|
+\subsection{Stream Termination}
|
|
|
|
+To terminate an open stream call the done function.
|
|
|
|
+
|
|
|
|
+\index{ecb\_done()} \index{cbc\_done()}\index{cfb\_done()}\index{ofb\_done()} \index{ctr\_done()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int XXX_done(symmetric_XXX *XXX);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+This will terminate the stream (by terminating the cipher) and return \textbf{CRYPT\_OK} if successful.
|
|
|
|
+
|
|
|
|
+\subsection{Examples}
|
|
|
|
|
|
\newpage
|
|
\newpage
|
|
\begin{small}
|
|
\begin{small}
|
|
@@ -845,6 +903,12 @@ int main(void)
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /* terminate the stream */
|
|
|
|
+ if ((err = ctr_done(&ctr)) != CRYPT_OK) {
|
|
|
|
+ printf("ctr_done error: %s\n", error_to_string(err));
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
/* clear up and return */
|
|
/* clear up and return */
|
|
zeromem(key, sizeof(key));
|
|
zeromem(key, sizeof(key));
|
|
zeromem(&ctr, sizeof(ctr));
|
|
zeromem(&ctr, sizeof(ctr));
|
|
@@ -1050,6 +1114,8 @@ They assume that ``pt'' and ``ct'' are the same size as the block cipher's block
|
|
both functions given a single ``ocb'' state. For bi-directional communication you will have to initialize two ``ocb''
|
|
both functions given a single ``ocb'' state. For bi-directional communication you will have to initialize two ``ocb''
|
|
states (with different nonces). Also ``pt'' and ``ct'' may point to the same location in memory.
|
|
states (with different nonces). Also ``pt'' and ``ct'' may point to the same location in memory.
|
|
|
|
|
|
|
|
+\subsubsection{State Termination}
|
|
|
|
+
|
|
When you are finished encrypting the message you call the following function to compute the tag.
|
|
When you are finished encrypting the message you call the following function to compute the tag.
|
|
|
|
|
|
\index{ocb\_done\_encrypt()}
|
|
\index{ocb\_done\_encrypt()}
|
|
@@ -1083,6 +1149,7 @@ tag of the message (internally) and then compare it against the ``taglen'' bytes
|
|
``res'' is set to zero. If all ``taglen'' bytes of ``tag'' can be verified then ``res'' is set to one (authenticated
|
|
``res'' is set to zero. If all ``taglen'' bytes of ``tag'' can be verified then ``res'' is set to one (authenticated
|
|
message).
|
|
message).
|
|
|
|
|
|
|
|
+\subsubsection{Packet Functions}
|
|
To make life simpler the following two functions are provided for memory bound OCB.
|
|
To make life simpler the following two functions are provided for memory bound OCB.
|
|
|
|
|
|
\index{ocb\_encrypt\_authenticate\_memory()}
|
|
\index{ocb\_encrypt\_authenticate\_memory()}
|
|
@@ -1112,6 +1179,238 @@ int ocb_decrypt_verify_memory(int cipher,
|
|
Similarly this will OCB decrypt and compare the internally computed tag against the tag provided. ``res'' is set
|
|
Similarly this will OCB decrypt and compare the internally computed tag against the tag provided. ``res'' is set
|
|
appropriately.
|
|
appropriately.
|
|
|
|
|
|
|
|
+\subsection{CCM Mode}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+function that performs the protocol.
|
|
|
|
+
|
|
|
|
+\index{ccm\_memory()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int ccm_memory(int cipher,
|
|
|
|
+ const unsigned char *key, unsigned long keylen,
|
|
|
|
+ const unsigned char *nonce, unsigned long noncelen,
|
|
|
|
+ const unsigned char *header, unsigned long headerlen,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen,
|
|
|
|
+ int direction);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+This performs the ``CCM'' operation on the data. The ``cipher'' variable indicates which cipher in the descriptor table to use. It must have a
|
|
|
|
+16--byte block size for CCM. The key is ``key'' with a length of ``keylen'' octets. The nonce or salt is ``nonce'' of
|
|
|
|
+length ``noncelen'' octets. The header is meta--data you want to send with the message but not have encrypted, it is stored in ``header''
|
|
|
|
+of length ``headerlen'' octets. The header can be zero octets long (if $headerlen = 0$ then you can pass ``header'' as \textbf{NULL}).
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+allowable that $pt = ct$. The ``direction'' variable indicates whether encryption (direction $=$ \textbf{CCM\_ENCRYPT}) or
|
|
|
|
+decryption (direction $=$ \textbf{CCM\_DECRYPT}) is to be performed.
|
|
|
|
+
|
|
|
|
+As implemented this copy of CCM cannot handle a header or plaintext longer than $2^{32} - 1$ octets long.
|
|
|
|
+
|
|
|
|
+You can test the implementation of CCM with the following function.
|
|
|
|
+
|
|
|
|
+\index{ccm\_test()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int ccm_test(void);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+This will return \textbf{CRYPT\_OK} if the CCM routine passes known test vectors.
|
|
|
|
+
|
|
|
|
+\subsection{GCM Mode}
|
|
|
|
+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
|
|
|
|
+accept ``additional authentication data'' (meta--data) after plaintext has been processed. This mode also only works with block ciphers with a sixteen
|
|
|
|
+byte block.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+unique to every session. Next the the optional additional authentication data is processed and finally the plaintext.
|
|
|
|
+
|
|
|
|
+\subsubsection{Initialization}
|
|
|
|
+To initialize the GCM context with a secret key call the following function.
|
|
|
|
+
|
|
|
|
+\index{gcm\_init()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_init(gcm_state *gcm, int cipher,
|
|
|
|
+ const unsigned char *key, int keylen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+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
|
|
|
|
+must have a 16--byte block size (e.g. AES).
|
|
|
|
+
|
|
|
|
+\subsubsection{Initial Vector}
|
|
|
|
+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.
|
|
|
|
+
|
|
|
|
+\index{gcm\_add\_iv()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_add_iv(gcm_state *gcm,
|
|
|
|
+ const unsigned char *IV, unsigned long IVlen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+to process the entire IV.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+to only use 12--byte IVs. You can just increment it like a counter for each packet and the CTR [privacy] will be ensured.
|
|
|
|
+
|
|
|
|
+\subsubsection{Additional Authentication Data}
|
|
|
|
+After the entire IV has been processed the additional authentication data can be processed. Unlike the IV a packet/session does not require additional
|
|
|
|
+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
|
|
|
|
+you begin adding AAD to the GCM state you cannot return to adding IV data until the state is reset.
|
|
|
|
+
|
|
|
|
+\index{gcm\_add\_aad()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_add_aad(gcm_state *gcm,
|
|
|
|
+ const unsigned char *adata, unsigned long adatalen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+This adds the additional authentication data ``adata'' of length ``adatalen'' to the GCM state ``gcm''.
|
|
|
|
+
|
|
|
|
+\subsubsection{Plaintext Processing}
|
|
|
|
+After the AAD has been processed the plaintext (or ciphertext depending on the direction) can be processed.
|
|
|
|
+
|
|
|
|
+\index{gcm\_process()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_process(gcm_state *gcm,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ int direction);
|
|
|
|
+\end{verbatim}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+in the ciphertext buffer. When ``direction'' equals \textbf{GCM\_DECRYPT} the opposite occurs.
|
|
|
|
+
|
|
|
|
+\subsubsection{State Termination}
|
|
|
|
+To terminate a GCM state and retrieve the message authentication tag call the following function.
|
|
|
|
+
|
|
|
|
+\index{gcm\_done()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_done(gcm_state *gcm,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+This terminates the GCM state ``gcm'' and stores the tag in ``tag'' of length ``taglen'' octets.
|
|
|
|
+
|
|
|
|
+\subsubsection{State Reset}
|
|
|
|
+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
|
|
|
|
+it is very costly to have to call it repeatedly. To aid in this endeavour the reset function has been provided.
|
|
|
|
+
|
|
|
|
+\index{gcm\_reset()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_reset(gcm_state *gcm);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+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.
|
|
|
|
+
|
|
|
|
+\subsubsection{One--Shot Packet}
|
|
|
|
+To process a single packet under any given key the following helper function can be used.
|
|
|
|
+
|
|
|
|
+\index{gcm\_memory()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int gcm_memory( int cipher,
|
|
|
|
+ const unsigned char *key, unsigned long keylen,
|
|
|
|
+ const unsigned char *IV, unsigned long IVlen,
|
|
|
|
+ const unsigned char *adata, unsigned long adatalen,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen,
|
|
|
|
+ int direction);
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+message tag. The definition of the variables is the same as it is for all the manual functions.
|
|
|
|
+
|
|
|
|
+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.
|
|
|
|
+
|
|
|
|
+\subsubsection{Example Usage}
|
|
|
|
+The following is an example usage of how to use GCM over multiple packets with a shared secret key.
|
|
|
|
+
|
|
|
|
+\begin{small}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+#include <tomcrypt.h>
|
|
|
|
+
|
|
|
|
+int send_packet(const unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ const unsigned char *iv, unsigned long ivlen,
|
|
|
|
+ const unsigned char *aad, unsigned long aadlen,
|
|
|
|
+ gcm_state *gcm)
|
|
|
|
+{
|
|
|
|
+ int err;
|
|
|
|
+ unsigned long taglen;
|
|
|
|
+ unsigned char tag[16];
|
|
|
|
+
|
|
|
|
+ /* reset the state */
|
|
|
|
+ if ((err = gcm_reset(gcm)) != CRYPT_OK) {
|
|
|
|
+ return err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* Add the IV */
|
|
|
|
+ if ((err = gcm_add_iv(gcm, iv, ivlen)) != CRYPT_OK) {
|
|
|
|
+ return err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* Add the AAD (note: aad can be NULL if aadlen == 0) */
|
|
|
|
+ if ((err = gcm_add_aad(gcm, aad, aadlen)) != CRYPT_OK) {
|
|
|
|
+ return err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* process the plaintext */
|
|
|
|
+ if ((err = gcm_add_process(gcm, pt, ptlen, pt, GCM_ENCRYPT)) != CRYPT_OK) {
|
|
|
|
+ return err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* Finish up and get the MAC tag */
|
|
|
|
+ taglen = sizeof(tag);
|
|
|
|
+ if ((err = gcm_done(gcm, tag, &taglen)) != CRYPT_OK) {
|
|
|
|
+ return err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* depending on the protocol and how IV is generated you may have to send it too... */
|
|
|
|
+ send(socket, iv, ivlen, 0);
|
|
|
|
+
|
|
|
|
+ /* send the aad */
|
|
|
|
+ send(socket, aad, aadlen, 0);
|
|
|
|
+
|
|
|
|
+ /* send the ciphertext */
|
|
|
|
+ send(socket, pt, ptlen, 0);
|
|
|
|
+
|
|
|
|
+ /* send the tag */
|
|
|
|
+ send(socket, tag, taglen, 0);
|
|
|
|
+
|
|
|
|
+ return CRYPT_OK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int main(void)
|
|
|
|
+{
|
|
|
|
+ gcm_state gcm;
|
|
|
|
+ unsigned char key[16], IV[12], pt[PACKET_SIZE];
|
|
|
|
+ int err, x;
|
|
|
|
+ unsigned long ptlen;
|
|
|
|
+
|
|
|
|
+ /* somehow fill key/IV with random values */
|
|
|
|
+
|
|
|
|
+ /* register AES */
|
|
|
|
+ register_cipher(&aes_desc);
|
|
|
|
+
|
|
|
|
+ /* init the GCM state */
|
|
|
|
+ if ((err = gcm_init(&gcm, find_cipher("aes"), key, 16)) != CRYPT_OK) {
|
|
|
|
+ whine_and_pout(err);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* handle us some packets */
|
|
|
|
+ for (;;) {
|
|
|
|
+ ptlen = make_packet_we_want_to_send(pt);
|
|
|
|
+
|
|
|
|
+ /* use IV as counter (12 byte counter) */
|
|
|
|
+ for (x = 11; x >= 0; x--) {
|
|
|
|
+ if (++IV[x]) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ((err = send_packet(pt, ptlen, iv, 12, NULL, 0, &gcm)) != CRYPT_OK) {
|
|
|
|
+ whine_and_pout(err);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return EXIT_SUCCESS;
|
|
|
|
+}
|
|
|
|
+\end{verbatim}
|
|
|
|
+\end{small}
|
|
|
|
+
|
|
\chapter{One-Way Cryptographic Hash Functions}
|
|
\chapter{One-Way Cryptographic Hash Functions}
|
|
\section{Core Functions}
|
|
\section{Core Functions}
|
|
|
|
|
|
@@ -1709,11 +2008,81 @@ and the cipher specified by the ``cipher'''th entry in the cipher\_descriptor ta
|
|
the same rules as omac\_done.
|
|
the same rules as omac\_done.
|
|
|
|
|
|
To test if the PMAC code is working there is the following function:
|
|
To test if the PMAC code is working there is the following function:
|
|
|
|
+\index{pmac\_test()}
|
|
\begin{verbatim}
|
|
\begin{verbatim}
|
|
int pmac_test(void);
|
|
int pmac_test(void);
|
|
\end{verbatim}
|
|
\end{verbatim}
|
|
Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code.
|
|
Which returns {\bf CRYPT\_OK} if the code passes otherwise it returns an error code.
|
|
|
|
|
|
|
|
+\section{Pelican MAC}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+as it calls AES directly.
|
|
|
|
+
|
|
|
|
+\index{pelican\_init()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+This will initialize the Pelican state with the given AES key. Once this has been done you can begin processing data.
|
|
|
|
+
|
|
|
|
+\index{pelican\_process()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen);
|
|
|
|
+\end{verbatim}
|
|
|
|
+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
|
|
|
|
+routine more efficient but you may pass in any length of text. You can call this function as many times as required to process
|
|
|
|
+an entire message.
|
|
|
|
+
|
|
|
|
+\index{pelican\_done()}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+int pelican_done(pelican_state *pelmac, unsigned char *out);
|
|
|
|
+\end{verbatim}
|
|
|
|
+This terminates a Pelican MAC and writes the 16--octet tag to ``out''.
|
|
|
|
+
|
|
|
|
+\subsection{Example}
|
|
|
|
+
|
|
|
|
+\begin{verbatim}
|
|
|
|
+#include <tomcrypt.h>
|
|
|
|
+int main(void)
|
|
|
|
+{
|
|
|
|
+ pelican_state pelstate;
|
|
|
|
+ unsigned char key[32], tag[16];
|
|
|
|
+ int err;
|
|
|
|
+
|
|
|
|
+ /* somehow initialize a key */
|
|
|
|
+
|
|
|
|
+ /* initialize pelican mac */
|
|
|
|
+ if ((err = pelican_init(&pelstate, /* the state */
|
|
|
|
+ key, /* user key */
|
|
|
|
+ 32 /* key length in octets */
|
|
|
|
+ )) != CRYPT_OK) {
|
|
|
|
+ printf("Error initializing Pelican: %s", error_to_string(err));
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* MAC some data */
|
|
|
|
+ if ((err = pelican_process(&pelstate, /* the state */
|
|
|
|
+ "hello world", /* data to mac */
|
|
|
|
+ 11 /* length of data */
|
|
|
|
+ )) != CRYPT_OK) {
|
|
|
|
+ printf("Error processing Pelican: %s", error_to_string(err));
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* Terminate the MAC */
|
|
|
|
+ if ((err = pelican_done(&pelstate, /* the state */
|
|
|
|
+ tag /* where to store the tag */
|
|
|
|
+ )) != CRYPT_OK) {
|
|
|
|
+ printf("Error terminating Pelican: %s", error_to_string(err));
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* tag[0..15] has the MAC output now */
|
|
|
|
+
|
|
|
|
+ return EXIT_SUCCESS;
|
|
|
|
+}
|
|
|
|
+\end{verbatim}
|
|
|
|
+
|
|
|
|
|
|
\chapter{Pseudo-Random Number Generators}
|
|
\chapter{Pseudo-Random Number Generators}
|
|
\section{Core Functions}
|
|
\section{Core Functions}
|
|
@@ -3359,16 +3728,6 @@ This will use libtool and gcc to build a shared library ``libtomcrypt.la'' as we
|
|
and install them into /usr/lib (and the headers into /usr/include). To link your application you should use the
|
|
and install them into /usr/lib (and the headers into /usr/include). To link your application you should use the
|
|
libtool program in ``--mode=link''.
|
|
libtool program in ``--mode=link''.
|
|
|
|
|
|
-You can also build LibTomCrypt as a shared library (DLL) in Windows with Cygwin. Issue the following
|
|
|
|
-
|
|
|
|
-\begin{alltt}
|
|
|
|
-make -f makefile.cygwin_dll
|
|
|
|
-\end{alltt}
|
|
|
|
-This will build ``libtomcrypt.dll.a'' which is an import library for ``libtomcrypt.dll''. You must copy
|
|
|
|
-``libtomcrypt.dll.a'' to your library directory, ``libtomcrypt.dll' to somewhere in your PATH and the header
|
|
|
|
-files to your include directory. So long as ``libtomcrypt.dll'' is in your system path you can run any LibTomCrypt
|
|
|
|
-program that uses it.
|
|
|
|
-
|
|
|
|
\section{mycrypt\_cfg.h}
|
|
\section{mycrypt\_cfg.h}
|
|
The file ``mycrypt\_cfg.h'' is what lets you control various high level macros which control the behaviour
|
|
The file ``mycrypt\_cfg.h'' is what lets you control various high level macros which control the behaviour
|
|
of the library.
|
|
of the library.
|
|
@@ -3391,36 +3750,36 @@ Currently LibTomCrypt will detect x86-32 and x86-64 running GCC as well as x86-3
|
|
\section{The Configure Script}
|
|
\section{The Configure Script}
|
|
There are also options you can specify from the configure script or ``mycrypt\_custom.h''.
|
|
There are also options you can specify from the configure script or ``mycrypt\_custom.h''.
|
|
|
|
|
|
-\subsubsection{X memory routines}
|
|
|
|
|
|
+\subsection{X memory routines}
|
|
At the top of mycrypt\_custom.h are four macros denoted as XMALLOC, XCALLOC, XREALLOC and XFREE which resolve to
|
|
At the top of mycrypt\_custom.h are four macros denoted as XMALLOC, XCALLOC, XREALLOC and XFREE which resolve to
|
|
the name of the respective functions. This lets you substitute in your own memory routines. If you substitute in
|
|
the name of the respective functions. This lets you substitute in your own memory routines. If you substitute in
|
|
your own functions they must behave like the standard C library functions in terms of what they expect as input and
|
|
your own functions they must behave like the standard C library functions in terms of what they expect as input and
|
|
output. By default the library uses the standard C routines.
|
|
output. By default the library uses the standard C routines.
|
|
|
|
|
|
-\subsubsection{X clock routines}
|
|
|
|
|
|
+\subsection{X clock routines}
|
|
The rng\_get\_bytes() function can call a function that requires the clock() function. These macros let you override
|
|
The rng\_get\_bytes() function can call a function that requires the clock() function. These macros let you override
|
|
the default clock() used with a replacement. By default the standard C library clock() function is used.
|
|
the default clock() used with a replacement. By default the standard C library clock() function is used.
|
|
|
|
|
|
-\subsubsection{NO\_FILE}
|
|
|
|
|
|
+\subsection{NO\_FILE}
|
|
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
|
|
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
|
|
functions and instead simply return CRYPT\_NOP. This should help resolve any linker errors stemming from a lack of
|
|
functions and instead simply return CRYPT\_NOP. This should help resolve any linker errors stemming from a lack of
|
|
file I/O on embedded platforms.
|
|
file I/O on embedded platforms.
|
|
|
|
|
|
-\subsubsection{CLEAN\_STACK}
|
|
|
|
|
|
+\subsection{CLEAN\_STACK}
|
|
When this functions is defined the functions that store key material on the stack will clean up afterwards.
|
|
When this functions is defined the functions that store key material on the stack will clean up afterwards.
|
|
Assumes that you have no memory paging with the stack.
|
|
Assumes that you have no memory paging with the stack.
|
|
|
|
|
|
-\subsubsection{LTC\_TEST}
|
|
|
|
|
|
+\subsection{LTC\_TEST}
|
|
When this has been defined the various self--test functions (for ciphers, hashes, prngs, etc) are included in the build.
|
|
When this has been defined the various self--test functions (for ciphers, hashes, prngs, etc) are included in the build.
|
|
When this has been undefined the tests are removed and if called will return CRYPT\_NOP.
|
|
When this has been undefined the tests are removed and if called will return CRYPT\_NOP.
|
|
|
|
|
|
-\subsubsection{Symmetric Ciphers, One-way Hashes, PRNGS and Public Key Functions}
|
|
|
|
|
|
+\subsection{Symmetric Ciphers, One-way Hashes, PRNGS and Public Key Functions}
|
|
There are a plethora of macros for the ciphers, hashes, PRNGs and public key functions which are fairly
|
|
There are a plethora of macros for the ciphers, hashes, PRNGs and public key functions which are fairly
|
|
self-explanatory. When they are defined the functionality is included otherwise it is not. There are some
|
|
self-explanatory. When they are defined the functionality is included otherwise it is not. There are some
|
|
dependency issues which are noted in the file. For instance, Yarrow requires CTR chaining mode, a block
|
|
dependency issues which are noted in the file. For instance, Yarrow requires CTR chaining mode, a block
|
|
cipher and a hash function.
|
|
cipher and a hash function.
|
|
|
|
|
|
-\subsubsection{TWOFISH\_SMALL and TWOFISH\_TABLES}
|
|
|
|
|
|
+\subsection{TWOFISH\_SMALL and TWOFISH\_TABLES}
|
|
Twofish is a 128-bit symmetric block cipher that is provided within the library. The cipher itself is flexible enough
|
|
Twofish is a 128-bit symmetric block cipher that is provided within the library. The cipher itself is flexible enough
|
|
to allow some tradeoffs in the implementation. When TWOFISH\_SMALL is defined the scheduled symmetric key for Twofish
|
|
to allow some tradeoffs in the implementation. When TWOFISH\_SMALL is defined the scheduled symmetric key for Twofish
|
|
requires only 200 bytes of memory. This is achieved by not pre-computing the substitution boxes. Having this
|
|
requires only 200 bytes of memory. This is achieved by not pre-computing the substitution boxes. Having this
|
|
@@ -3432,10 +3791,39 @@ useful when TWOFISH\_SMALL is defined as the table values are computed on the fl
|
|
will increase by approximately 500 bytes. If this is defined but TWOFISH\_SMALL is not the cipher will still work but
|
|
will increase by approximately 500 bytes. If this is defined but TWOFISH\_SMALL is not the cipher will still work but
|
|
it will not speed up the encryption or decryption functions.
|
|
it will not speed up the encryption or decryption functions.
|
|
|
|
|
|
-\subsubsection{SMALL\_CODE}
|
|
|
|
|
|
+\subsection{GCM\_TABLES}
|
|
|
|
+When defined GCM will use a 64KB table (per GCM state) which will greatly lower up the per--packet latency.
|
|
|
|
+It also increases the initialization time.
|
|
|
|
+
|
|
|
|
+\subsection{SMALL\_CODE}
|
|
When this is defined some of the code such as the Rijndael and SAFER+ ciphers are replaced with smaller code variants.
|
|
When this is defined some of the code such as the Rijndael and SAFER+ ciphers are replaced with smaller code variants.
|
|
These variants are slower but can save quite a bit of code space.
|
|
These variants are slower but can save quite a bit of code space.
|
|
|
|
|
|
|
|
+\subsection{LTC\_FAST}
|
|
|
|
+This mode (autodetected with x86\_32,x86\_64 platforms with GCC or MSVC) configures various routines such as ctr\_encrypt() or
|
|
|
|
+cbc\_encrypt() that it can safely XOR multiple octets in one step by using a larger data type. This has the benefit of
|
|
|
|
+cutting down the overhead of the respective functions.
|
|
|
|
+
|
|
|
|
+This mode does have one downside. It can cause unaligned reads from memory if you are not careful with the functions. This is why
|
|
|
|
+it has been enabled by default only for the x86 class of processors where unaligned accesses are allowed. Technically LTC\_FAST
|
|
|
|
+is not ``portable'' since unaligned accesses are not covered by the ISO C specifications.
|
|
|
|
+
|
|
|
|
+In practice however, you can use it on pretty much any platform (even MIPS) with care.
|
|
|
|
+
|
|
|
|
+By design the ``fast'' mode functions won't get unaligned on their own. For instance, if you call ctr\_encrypt() right after calling
|
|
|
|
+ctr\_start() and all the inputs you gave are aligned than ctr\_encrypt() will perform aligned memory operations only. However, if you
|
|
|
|
+call ctr\_encrypt() with an odd amount of plaintext then call it again the CTR pad (the IV) will be partially used. This will
|
|
|
|
+cause the ctr routine to first use up the remaining pad bytes. Then if there are enough plaintext bytes left it will use
|
|
|
|
+whole word XOR operations. These operations will be unaligned.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+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.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+data type you can perform integer operations with. Ideally it should be four or eight bytes since it must properly divide the size
|
|
|
|
+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
|
|
|
|
+use this mode. So sad.
|
|
|
|
+
|
|
\section{MPI Tweaks}
|
|
\section{MPI Tweaks}
|
|
\subsection{RSA Only Tweak}
|
|
\subsection{RSA Only Tweak}
|
|
If you plan on only using RSA with moduli in the range of 1024 to 2560 bits you can enable a series of tweaks
|
|
If you plan on only using RSA with moduli in the range of 1024 to 2560 bits you can enable a series of tweaks
|
|
@@ -3448,7 +3836,417 @@ to reduce the library size. Follow these steps
|
|
\item Rebuild the library.
|
|
\item Rebuild the library.
|
|
\end{enumerate}
|
|
\end{enumerate}
|
|
|
|
|
|
|
|
+\chapter{Optimizations}
|
|
|
|
+\section{Introduction}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+about the platform, etc...
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+portable routines. For instance, hand optimized assembler versions of AES could be provided and any existing function that uses the cipher could automatically use
|
|
|
|
+the optimized code without re--writing. This also paves the way for hardware drivers that can access hardware accelerated cryptographic devices.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+dependent routines all a developer has to do is register\_XXX() the descriptor and they're set.
|
|
|
|
+
|
|
|
|
+\section{Ciphers}
|
|
|
|
+The ciphers in LibTomCrypt are accessed through the ltc\_cipher\_descriptor structure.
|
|
|
|
+
|
|
|
|
+\begin{small}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+struct ltc_cipher_descriptor {
|
|
|
|
+ /** name of cipher */
|
|
|
|
+ char *name;
|
|
|
|
+ /** internal ID */
|
|
|
|
+ unsigned char ID;
|
|
|
|
+ /** min keysize (octets) */
|
|
|
|
+ int min_key_length,
|
|
|
|
+ /** max keysize (octets) */
|
|
|
|
+ max_key_length,
|
|
|
|
+ /** block size (octets) */
|
|
|
|
+ block_length,
|
|
|
|
+ /** default number of rounds */
|
|
|
|
+ default_rounds;
|
|
|
|
+ /** Setup the cipher
|
|
|
|
+ @param key The input symmetric key
|
|
|
|
+ @param keylen The length of the input key (octets)
|
|
|
|
+ @param num_rounds The requested number of rounds (0==default)
|
|
|
|
+ @param skey [out] The destination of the scheduled key
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*setup)(const unsigned char *key, int keylen,
|
|
|
|
+ int num_rounds, symmetric_key *skey);
|
|
|
|
+ /** Encrypt a block
|
|
|
|
+ @param pt The plaintext
|
|
|
|
+ @param ct [out] The ciphertext
|
|
|
|
+ @param skey The scheduled key
|
|
|
|
+ */
|
|
|
|
+ void (*ecb_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct, symmetric_key *skey);
|
|
|
|
+ /** Decrypt a block
|
|
|
|
+ @param ct The ciphertext
|
|
|
|
+ @param pt [out] The plaintext
|
|
|
|
+ @param skey The scheduled key
|
|
|
|
+ */
|
|
|
|
+ void (*ecb_decrypt)(const unsigned char *ct,
|
|
|
|
+ unsigned char *pt, symmetric_key *skey);
|
|
|
|
+ /** Test the block cipher
|
|
|
|
+ @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
|
|
|
+ */
|
|
|
|
+ int (*test)(void);
|
|
|
|
+ /** Determine a key size
|
|
|
|
+ @param keysize [in/out] The size of the key desired and the suggested size
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*keysize)(int *keysize);
|
|
|
|
+
|
|
|
|
+/** Accelerators **/
|
|
|
|
+ /** Accelerated ECB encryption
|
|
|
|
+ @param pt Plaintext
|
|
|
|
+ @param ct Ciphertext
|
|
|
|
+ @param blocks The number of complete blocks to process
|
|
|
|
+ @param skey The scheduled key context
|
|
|
|
+ */
|
|
|
|
+ void (*accel_ecb_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct, unsigned long blocks,
|
|
|
|
+ symmetric_key *skey);
|
|
|
|
+
|
|
|
|
+ /** Accelerated ECB decryption
|
|
|
|
+ @param pt Plaintext
|
|
|
|
+ @param ct Ciphertext
|
|
|
|
+ @param blocks The number of complete blocks to process
|
|
|
|
+ @param skey The scheduled key context
|
|
|
|
+ */
|
|
|
|
+ void (*accel_ecb_decrypt)(const unsigned char *ct,
|
|
|
|
+ unsigned char *pt, unsigned long blocks,
|
|
|
|
+ symmetric_key *skey);
|
|
|
|
+
|
|
|
|
+ /** Accelerated CBC encryption
|
|
|
|
+ @param pt Plaintext
|
|
|
|
+ @param ct Ciphertext
|
|
|
|
+ @param blocks The number of complete blocks to process
|
|
|
|
+ @param IV The initial value (input/output)
|
|
|
|
+ @param skey The scheduled key context
|
|
|
|
+ */
|
|
|
|
+ void (*accel_cbc_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct, unsigned long blocks,
|
|
|
|
+ unsigned char *IV, symmetric_key *skey);
|
|
|
|
+
|
|
|
|
+ /** Accelerated CBC decryption
|
|
|
|
+ @param pt Plaintext
|
|
|
|
+ @param ct Ciphertext
|
|
|
|
+ @param blocks The number of complete blocks to process
|
|
|
|
+ @param IV The initial value (input/output)
|
|
|
|
+ @param skey The scheduled key context
|
|
|
|
+ */
|
|
|
|
+ void (*accel_cbc_decrypt)(const unsigned char *ct,
|
|
|
|
+ unsigned char *pt, unsigned long blocks,
|
|
|
|
+ unsigned char *IV, symmetric_key *skey);
|
|
|
|
+
|
|
|
|
+ /** Accelerated CTR encryption
|
|
|
|
+ @param pt Plaintext
|
|
|
|
+ @param ct Ciphertext
|
|
|
|
+ @param blocks The number of complete blocks to process
|
|
|
|
+ @param IV The initial value (input/output)
|
|
|
|
+ @param mode little or big endian counter (mode=0 or mode=1)
|
|
|
|
+ @param skey The scheduled key context
|
|
|
|
+ */
|
|
|
|
+ void (*accel_ctr_encrypt)(const unsigned char *pt,
|
|
|
|
+ unsigned char *ct, unsigned long blocks,
|
|
|
|
+ unsigned char *IV, int mode, symmetric_key *skey);
|
|
|
|
+
|
|
|
|
+ /** Accelerated CCM packet (one-shot)
|
|
|
|
+ @param key The secret key to use
|
|
|
|
+ @param keylen The length of the secret key (octets)
|
|
|
|
+ @param nonce The session nonce [use once]
|
|
|
|
+ @param noncelen The length of the nonce
|
|
|
|
+ @param header The header for the session
|
|
|
|
+ @param headerlen The length of the header (octets)
|
|
|
|
+ @param pt [out] The plaintext
|
|
|
|
+ @param ptlen The length of the plaintext (octets)
|
|
|
|
+ @param ct [out] The ciphertext
|
|
|
|
+ @param tag [out] The destination tag
|
|
|
|
+ @param taglen [in/out] The max size and resulting size of the authentication tag
|
|
|
|
+ @param direction Encrypt or Decrypt direction (0 or 1)
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ void (*accel_ccm_memory)(
|
|
|
|
+ const unsigned char *key, unsigned long keylen,
|
|
|
|
+ const unsigned char *nonce, unsigned long noncelen,
|
|
|
|
+ const unsigned char *header, unsigned long headerlen,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen,
|
|
|
|
+ int direction);
|
|
|
|
+
|
|
|
|
+ /** Accelerated GCM packet (one shot)
|
|
|
|
+ @param key The secret key
|
|
|
|
+ @param keylen The length of the secret key
|
|
|
|
+ @param IV The initial vector
|
|
|
|
+ @param IVlen The length of the initial vector
|
|
|
|
+ @param adata The additional authentication data (header)
|
|
|
|
+ @param adatalen The length of the adata
|
|
|
|
+ @param pt The plaintext
|
|
|
|
+ @param ptlen The length of the plaintext (ciphertext length is the same)
|
|
|
|
+ @param ct The ciphertext
|
|
|
|
+ @param tag [out] The MAC tag
|
|
|
|
+ @param taglen [in/out] The MAC tag length
|
|
|
|
+ @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
|
|
|
|
+ */
|
|
|
|
+ void (*accel_gcm_memory)(
|
|
|
|
+ const unsigned char *key, unsigned long keylen,
|
|
|
|
+ const unsigned char *IV, unsigned long IVlen,
|
|
|
|
+ const unsigned char *adata, unsigned long adatalen,
|
|
|
|
+ unsigned char *pt, unsigned long ptlen,
|
|
|
|
+ unsigned char *ct,
|
|
|
|
+ unsigned char *tag, unsigned long *taglen,
|
|
|
|
+ int direction);
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+\end{verbatim}
|
|
|
|
+\end{small}
|
|
|
|
+
|
|
|
|
+\subsection{Name}
|
|
|
|
+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
|
|
|
|
+tables.
|
|
|
|
+
|
|
|
|
+\subsection{Internal ID}
|
|
|
|
+This is a single byte Internal ID you can use to distingish ciphers from each other.
|
|
|
|
+
|
|
|
|
+\subsection{Key Lengths}
|
|
|
|
+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
|
|
|
|
+and both must valid key sizes for the cipher. Values in between are not assumed to be valid though they may be.
|
|
|
|
+
|
|
|
|
+\subsection{Block Length}
|
|
|
|
+The size of the ciphers plaintext or ciphertext is ``block\_length'' and is measured in octets.
|
|
|
|
+
|
|
|
|
+\subsection{Rounds}
|
|
|
|
+Some ciphers allow different number of rounds to be used. Usually you just use the default. The default round count is ``default\_rounds''.
|
|
|
|
+
|
|
|
|
+\subsection{Setup}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+in ``skey''.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+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.
|
|
|
|
+
|
|
|
|
+\subsection{Single block ECB}
|
|
|
|
+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
|
|
|
|
+must make sure you do not overwrite the output before you are finished with the input.
|
|
|
|
+
|
|
|
|
+\subsection{Testing}
|
|
|
|
+The test() function is used to self--test the ``device''. It takes no arguments and returns \textbf{CRYPT\_OK} if all is working properly.
|
|
|
|
+
|
|
|
|
+\subsection{Key Sizing}
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+ cannot be mapped to a valid key size for the cipher.
|
|
|
|
+
|
|
|
|
+\subsection{Acceleration}
|
|
|
|
+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
|
|
|
|
+it is not supported. In those cases the software fallbacks are used (using the single ECB block routines).
|
|
|
|
+
|
|
|
|
+\subsubsection{Accelerated ECB}
|
|
|
|
+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
|
|
|
|
+through the accel\_ecb\_encrypt and accel\_ecb\_decrypt pointers. The ``blocks'' count is the number of complete blocks to process.
|
|
|
|
+
|
|
|
|
+\subsubsection{Accelerated CBC}
|
|
|
|
+These two functions are meant for accelerated CBC encryption. These functions are accessed through the accel\_cbc\_encrypt and accel\_cbc\_decrypt pointers.
|
|
|
|
+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
|
|
|
|
+updated by the function before returning.
|
|
|
|
+
|
|
|
|
+\subsubsection{Accelerated CTR}
|
|
|
|
+This function is meant for accelerated CTR encryption. It is accessible through the accel\_ctr\_encrypt pointer.
|
|
|
|
+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
|
|
|
|
+updated by the function before returning. The ``mode'' value indicates whether the counter is big ($mode = 1$) or little ($mode = 0$) endian.
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+called if the following conditions are met.
|
|
|
|
+
|
|
|
|
+\begin{enumerate}
|
|
|
|
+ \item The accelerator is present
|
|
|
|
+ \item The CTR pad is empty
|
|
|
|
+ \item The remaining length of the input to process is greater than or equal to the block size.
|
|
|
|
+\end{enumerate}
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+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
|
|
|
|
+buffer provided) before encrypting it to create the pad.
|
|
|
|
+
|
|
|
|
+The accelerator will only be used to encrypt whole blocks. Partial blocks are always handled in software.
|
|
|
|
+
|
|
|
|
+\subsubsection{Accelerated CCM}
|
|
|
|
+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
|
|
|
|
+be called prior to this. This function must handle scheduling the key provided on its own.
|
|
|
|
+
|
|
|
|
+\subsubsection{Accelerated GCM}
|
|
|
|
+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
|
|
|
|
+be called prior to this. This function must handle scheduling the key provided on its own.
|
|
|
|
+
|
|
|
|
+\section{One--Way Hashes}
|
|
|
|
+The hash functions are accessed through the ltc\_hash\_descriptor structure.
|
|
|
|
+
|
|
|
|
+\begin{small}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+struct ltc_hash_descriptor {
|
|
|
|
+ /** name of hash */
|
|
|
|
+ char *name;
|
|
|
|
+ /** internal ID */
|
|
|
|
+ unsigned char ID;
|
|
|
|
+ /** Size of digest in octets */
|
|
|
|
+ unsigned long hashsize;
|
|
|
|
+ /** Input block size in octets */
|
|
|
|
+ unsigned long blocksize;
|
|
|
|
+ /** ASN.1 DER identifier */
|
|
|
|
+ unsigned char DER[64];
|
|
|
|
+ /** Length of DER encoding */
|
|
|
|
+ unsigned long DERlen;
|
|
|
|
+ /** Init a hash state
|
|
|
|
+ @param hash The hash to initialize
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*init)(hash_state *hash);
|
|
|
|
+ /** Process a block of data
|
|
|
|
+ @param hash The hash state
|
|
|
|
+ @param in The data to hash
|
|
|
|
+ @param inlen The length of the data (octets)
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
|
|
|
|
+ /** Produce the digest and store it
|
|
|
|
+ @param hash The hash state
|
|
|
|
+ @param out [out] The destination of the digest
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*done)(hash_state *hash, unsigned char *out);
|
|
|
|
+ /** Self-test
|
|
|
|
+ @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
|
|
|
|
+ */
|
|
|
|
+ int (*test)(void);
|
|
|
|
+};
|
|
|
|
+\end{verbatim}
|
|
|
|
+\end{small}
|
|
|
|
+
|
|
|
|
+\subsection{Name}
|
|
|
|
+This is the name the hash is known by and what find\_hash() will look for.
|
|
|
|
+
|
|
|
|
+\subsection{Internal ID}
|
|
|
|
+This is the internal ID byte used to distinguish the hash from other hashes.
|
|
|
|
+
|
|
|
|
+\subsection{Digest Size}
|
|
|
|
+The ``hashsize'' variable indicates the length of the output in octets.
|
|
|
|
+
|
|
|
|
+\subsection{Block Size}
|
|
|
|
+The `blocksize'' variable indicates the length of input (in octets) that the hash processes in a given
|
|
|
|
+invokation.
|
|
|
|
+
|
|
|
|
+\subsection{DER Identifier}
|
|
|
|
+This is the DER identifier (including the SEQUENCE header). This is used solely for PKCS \#1 style signatures.
|
|
|
|
+
|
|
|
|
+\subsection{Initialization}
|
|
|
|
+The init function initializes the hash and prepares it to process message bytes.
|
|
|
|
+
|
|
|
|
+\subsection{Process}
|
|
|
|
+This processes message bytes. The algorithm must accept any length of input that the hash would allow. The input is not
|
|
|
|
+guaranteed to be a multiple of the block size in length.
|
|
|
|
+
|
|
|
|
+\subsection{Done}
|
|
|
|
+The done function terminates the hash and returns the message digest.
|
|
|
|
+
|
|
|
|
+\subsection{Acceleration}
|
|
|
|
+A compatible accelerator must allow processing data in any granularity which may require internal padding on the driver side.
|
|
|
|
+
|
|
|
|
+\section{Pseudo--Random Number Generators}
|
|
|
|
+The pseudo--random number generators are accessible through the ltc\_prng\_descriptor structure.
|
|
|
|
+
|
|
|
|
+\begin{small}
|
|
|
|
+\begin{verbatim}
|
|
|
|
+struct ltc_prng_descriptor {
|
|
|
|
+ /** Name of the PRNG */
|
|
|
|
+ char *name;
|
|
|
|
+ /** size in bytes of exported state */
|
|
|
|
+ int export_size;
|
|
|
|
+ /** Start a PRNG state
|
|
|
|
+ @param prng [out] The state to initialize
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*start)(prng_state *prng);
|
|
|
|
+ /** Add entropy to the PRNG
|
|
|
|
+ @param in The entropy
|
|
|
|
+ @param inlen Length of the entropy (octets)\
|
|
|
|
+ @param prng The PRNG state
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
|
|
|
+ /** Ready a PRNG state to read from
|
|
|
|
+ @param prng The PRNG state to ready
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*ready)(prng_state *prng);
|
|
|
|
+ /** Read from the PRNG
|
|
|
|
+ @param out [out] Where to store the data
|
|
|
|
+ @param outlen Length of data desired (octets)
|
|
|
|
+ @param prng The PRNG state to read from
|
|
|
|
+ @return Number of octets read
|
|
|
|
+ */
|
|
|
|
+ unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng);
|
|
|
|
+ /** Terminate a PRNG state
|
|
|
|
+ @param prng The PRNG state to terminate
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*done)(prng_state *prng);
|
|
|
|
+ /** Export a PRNG state
|
|
|
|
+ @param out [out] The destination for the state
|
|
|
|
+ @param outlen [in/out] The max size and resulting size of the PRNG state
|
|
|
|
+ @param prng The PRNG to export
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
|
|
|
+ /** Import a PRNG state
|
|
|
|
+ @param in The data to import
|
|
|
|
+ @param inlen The length of the data to import (octets)
|
|
|
|
+ @param prng The PRNG to initialize/import
|
|
|
|
+ @return CRYPT_OK if successful
|
|
|
|
+ */
|
|
|
|
+ int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
|
|
|
+ /** Self-test the PRNG
|
|
|
|
+ @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
|
|
|
+ */
|
|
|
|
+ int (*test)(void);
|
|
|
|
+};
|
|
|
|
+\end{verbatim}
|
|
|
|
+\end{small}
|
|
|
|
+
|
|
|
|
+\subsection{Name}
|
|
|
|
+The name by which find\_prng() will find the PRNG.
|
|
|
|
+
|
|
|
|
+\subsection{Export Size}
|
|
|
|
+When an PRNG state is to be exported for future use you specify the space required in this variable.
|
|
|
|
+
|
|
|
|
+\subsection{Start}
|
|
|
|
+Initialize the PRNG and make it ready to accept entropy.
|
|
|
|
+
|
|
|
|
+\subsection{Entropy Addition}
|
|
|
|
+Add entropy to the PRNG state. The exact behaviour of this function depends on the particulars of the PRNG.
|
|
|
|
+
|
|
|
|
+\subsection{Ready}
|
|
|
|
+This function makes the PRNG ready to read from by processing the entropy added. The behaviour of this function depends
|
|
|
|
+on the specific PRNG used.
|
|
|
|
+
|
|
|
|
+\subsection{Read}
|
|
|
|
+Read from the PRNG and return the number of bytes read. This function does not have to fill the buffer but it is best
|
|
|
|
+if it does as many protocols do not retry reads and will fail on the first try.
|
|
|
|
+
|
|
|
|
+\subsection{Done}
|
|
|
|
+Terminate a PRNG state. The behaviour of this function depends on the particular PRNG used.
|
|
|
|
|
|
|
|
+\subsection{Exporting and Importing}
|
|
|
|
+An exported PRNG state is data that the PRNG can later import to resume activity. They're not meant to resume ``the same session''
|
|
|
|
+but should at least maintain the same level of state entropy.
|
|
|
|
|
|
\input{crypt.ind}
|
|
\input{crypt.ind}
|
|
|
|
|