added libtomcrypt-0.97

This commit is contained in:
Tom St Denis 2004-06-20 02:41:49 +00:00 committed by Steffen Jaeckel
parent 3fe312ccef
commit 1a1141627d
89 changed files with 2028 additions and 2640 deletions

View File

@ -418,7 +418,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
/* extend the key as required */ /* extend the key as required */
zeromem(buf, sizeof(buf)); zeromem(buf, sizeof(buf));
memcpy(buf, key, (size_t)keylen); XMEMCPY(buf, key, (size_t)keylen);
/* load and start the awful looking network */ /* load and start the awful looking network */
for (y = 0; y < 4; y++) { for (y = 0; y < 4; y++) {

View File

@ -21,7 +21,7 @@ int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc)
if ((unsigned long)cbc->blocklen > *len) { if ((unsigned long)cbc->blocklen > *len) {
return CRYPT_BUFFER_OVERFLOW; return CRYPT_BUFFER_OVERFLOW;
} }
memcpy(IV, cbc->IV, cbc->blocklen); XMEMCPY(IV, cbc->IV, cbc->blocklen);
*len = cbc->blocklen; *len = cbc->blocklen;
return CRYPT_OK; return CRYPT_OK;

View File

@ -20,7 +20,7 @@ int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc)
if (len != (unsigned long)cbc->blocklen) { if (len != (unsigned long)cbc->blocklen) {
return CRYPT_INVALID_ARG; return CRYPT_INVALID_ARG;
} }
memcpy(cbc->IV, IV, len); XMEMCPY(cbc->IV, IV, len);
return CRYPT_OK; return CRYPT_OK;
} }

View File

@ -21,7 +21,7 @@ int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb)
if ((unsigned long)cfb->blocklen > *len) { if ((unsigned long)cfb->blocklen > *len) {
return CRYPT_BUFFER_OVERFLOW; return CRYPT_BUFFER_OVERFLOW;
} }
memcpy(IV, cfb->IV, cfb->blocklen); XMEMCPY(IV, cfb->IV, cfb->blocklen);
*len = cfb->blocklen; *len = cfb->blocklen;
return CRYPT_OK; return CRYPT_OK;

20
changes
View File

@ -1,3 +1,23 @@
June 19th, 2004
v0.97 -- Removed spurious unused files [arrg!]
-- Patched buffer overflow in tim_exptmod()
-- Fixed buffer overrun bug in pkcs_1_v15_es_decode()
-- Reduced stack usage in PKCS #1 v2.0 padding functions (by several KBs)
-- Removed useless extern's that were an artifact from the project start... ;-)
-- Replaced memcpy/memset with XMEMCPY and XMEMSET for greater flexibility
-- fixed bugs in hmac_done()/hmac_init()/[various others()] where I didn't trap errors
-- Reduced stack usage in OMAC/PMAC/HMAC/EAX/OCB/PKCS#5 by mallocing any significant sized
arrays (e.g. > 100 bytes or so). Only in non-critical functions (e.g. eax_init())
-- "Zhi Chen" <zhi@massiveincorporated.com> pointed out that rsa_decrypt_key() requires
an incorrect output size (too large). Fixed.
-- Added a "pretty" target to the GCC makefile. Requires PERL. It is NEAT!
-- Minor updates to ch1 of the manual.
-- Cleaned up the indentation and added comments to rsa_make_key(), rsa_exptmod() and
rsa_verify_hash()
-- Updated makefile.icc so the "install" target would work ;-)
-- Removed demos/test.c [deprecated from demos/test/test.c]
-- Changed MAXBLOCKSIZE from 128 to 64 to reflect the true size...
May 30th, 2004 May 30th, 2004
v0.96 -- Removed GF and Keyring code v0.96 -- Removed GF and Keyring code
-- Extended OAEP decoder to distinguish better [and use a more uniform API] -- Extended OAEP decoder to distinguish better [and use a more uniform API]

25
crypt Normal file
View File

@ -0,0 +1,25 @@
%PDF-1.3
%Çì<C387>¢
3 0 obj
<< /Type /Pages /Kids [
] /Count 0
>>
endobj
1 0 obj
<</Type /Catalog /Pages 3 0 R
>>
endobj
2 0 obj
<</Producer(ESP Ghostscript 7.07)>>endobj
xref
0 4
0000000000 65535 f
0000000068 00000 n
0000000116 00000 n
0000000015 00000 n
trailer
<< /Size 4 /Root 1 0 R /Info 2 0 R
>>
startxref
166
%%EOF

View File

@ -1,4 +1,4 @@
\documentclass[b5paper]{book} \documentclass[a4paper]{book}
\usepackage{hyperref} \usepackage{hyperref}
\usepackage{makeidx} \usepackage{makeidx}
\usepackage{amssymb} \usepackage{amssymb}
@ -47,20 +47,15 @@
\def\gap{\vspace{0.5ex}} \def\gap{\vspace{0.5ex}}
\makeindex \makeindex
\begin{document} \begin{document}
\title{A Tiny Crypto Library, \\ LibTomCrypt \\ Version 0.96} \title{LibTomCrypt \\ Version 0.97}
\author{Tom St Denis \\ \author{Tom St Denis \\
\\ \\
tomstdenis@iahu.ca \\ tomstdenis@iahu.ca \\
http://libtomcrypt.org \\ \\ http://libtomcrypt.org
Phone: 1-613-836-3160\\
111 Banning Rd \\
Kanata, Ontario \\
K2L 1C3 \\
Canada
} }
\maketitle \maketitle
This text and source code library are both hereby placed in the public domain. This book has been This text and source code library are both hereby placed in the public domain. This book has been
formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package. formatted for A4 paper using the \LaTeX{} {\em book} macro package.
\vspace{10cm} \vspace{10cm}
@ -70,7 +65,15 @@ formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.
Tom St Denis, Tom St Denis,
Ontario, Canada Phone: 1-613-836-3160
111 Banning Rd
Kanata, Ontario
K2L 1C3
Canada
\end{flushright} \end{flushright}
\newpage \newpage
\tableofcontents \tableofcontents
@ -182,7 +185,7 @@ under a public domain license:
library. library.
``rc2.c'' is based on publicly available code that is not attributed to a person from the given source. ``safer.c'' ``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 (demoliner@isi.ee.ethz.ch) and is public domain. was written by Richard De Moliner (demoliner@isi.ee.ethz.ch) and seems to be free for use.
The project is hereby released as public domain. The project is hereby released as public domain.
@ -190,7 +193,7 @@ The project is hereby released as public domain.
The author (Tom St Denis) is not a patent lawyer so this section is not to be treated as legal advice. To the best The author (Tom St Denis) is not a patent lawyer so this section is not to be treated as legal advice. To the best
of the authors knowledge the only patent related issues within the library are the RC5 and RC6 symmetric block ciphers. of the authors knowledge the only patent related issues within the library are the RC5 and RC6 symmetric block ciphers.
They can be removed from a build by simply commenting out the two appropriate lines in the makefile script. The rest They can be removed from a build by simply commenting out the two appropriate lines in ``mycrypt\_custom.h''. The rest
of the ciphers and hashes are patent free or under patents that have since expired. of the ciphers and hashes are patent free or under patents that have since expired.
The RC2 and RC4 symmetric ciphers are not under patents but are under trademark regulations. This means you can use The RC2 and RC4 symmetric ciphers are not under patents but are under trademark regulations. This means you can use
@ -212,11 +215,11 @@ under where VC98 is installed).
In the recent versions the build steps have changed. The build options are now stored in ``mycrypt\_custom.h'' and In the recent versions the build steps have changed. The build options are now stored in ``mycrypt\_custom.h'' and
no longer in the makefile. If you change a build option in that file you must re-build the library from clean to no longer in the makefile. If you change a build option in that file you must re-build the library from clean to
ensure the build is intact. The perl script ``config.pl'' will help setup the custom header and a custom makefile ensure the build is intact.
if you want one (the provided ``makefile'' will work with custom configs).
\section{Thanks} \section{Thanks}
I would like to give thanks to the following people (in no particular order) for helping me develop this project: I would like to give thanks to the following people (in no particular order) for helping me develop this project from
early on:
\begin{enumerate} \begin{enumerate}
\item Richard van de Laarschot \item Richard van de Laarschot
\item Richard Heathfield \item Richard Heathfield
@ -233,6 +236,10 @@ I would like to give thanks to the following people (in no particular order) for
\item Christopher Imes \item Christopher Imes
\end{enumerate} \end{enumerate}
There have been quite a few other people as well. Please check the change log to see who else has contributed from
time to time.
\chapter{The Application Programming Interface (API)} \chapter{The Application Programming Interface (API)}
\section{Introduction} \section{Introduction}
\index{CRYPT\_ERROR} \index{CRYPT\_OK} \index{CRYPT\_ERROR} \index{CRYPT\_OK}
@ -581,9 +588,9 @@ Twofish round function.
\begin{tabular}{|l|l|l|} \begin{tabular}{|l|l|l|}
\hline TWOFISH\_SMALL & TWOFISH\_TABLES & Speed and Memory (per key) \\ \hline TWOFISH\_SMALL & TWOFISH\_TABLES & Speed and Memory (per key) \\
\hline undefined & undefined & Very fast, 4.2KB of ram. \\ \hline undefined & undefined & Very fast, 4.2KB of ram. \\
\hline undefined & defined & As above, faster keysetup, larger code (1KB more). \\ \hline undefined & defined & Faster keysetup, larger code. \\
\hline defined & undefined & Very slow, 0.2KB of ram. \\ \hline defined & undefined & Very slow, 0.2KB of ram. \\
\hline defined & defined & Somewhat faster, 0.2KB of ram, larger code. \\ \hline defined & defined & Faster, 0.2KB of ram, larger code. \\
\hline \hline
\end{tabular} \end{tabular}
\end{center} \end{center}
@ -615,7 +622,8 @@ int main(void)
} }
/* generic call to function (assuming the key in key[] was already setup) */ /* generic call to function (assuming the key in key[] was already setup) */
if ((err = cipher_descriptor[find_cipher("blowfish")].setup(key, 8, 0, &skey)) != CRYPT_OK) { if ((err = cipher_descriptor[find_cipher("blowfish")].setup(key, 8, 0, &skey)) !=
CRYPT_OK) {
printf("Error setting up Blowfish: %s\n", error_to_string(err)); printf("Error setting up Blowfish: %s\n", error_to_string(err));
return -1; return -1;
} }
@ -820,12 +828,13 @@ int main(void)
/* somehow fill out key and IV */ /* somehow fill out key and IV */
/* start up CTR mode */ /* start up CTR mode */
if ((err = ctr_start(find_cipher("twofish"), /* index of desired cipher */ if ((err = ctr_start(
IV, /* the initial vector */ find_cipher("twofish"), /* index of desired cipher */
key, /* the secret key */ IV, /* the initial vector */
16, /* length of secret key (16 bytes, 128 bits) */ key, /* the secret key */
0, /* 0 == default # of rounds */ 16, /* length of secret key (16 bytes, 128 bits) */
&ctr) /* where to store initialized CTR state */ 0, /* 0 == default # of rounds */
&ctr) /* where to store initialized CTR state */
) != CRYPT_OK) { ) != CRYPT_OK) {
printf("ctr_start error: %s\n", error_to_string(err)); printf("ctr_start error: %s\n", error_to_string(err));
return -1; return -1;

View File

@ -26,7 +26,7 @@ int register_cipher(const struct _cipher_descriptor *cipher)
/* find a blank spot */ /* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) { for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].name == NULL) { if (cipher_descriptor[x].name == NULL) {
memcpy(&cipher_descriptor[x], cipher, sizeof(struct _cipher_descriptor)); XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct _cipher_descriptor));
return x; return x;
} }
} }

View File

@ -26,7 +26,7 @@ int register_hash(const struct _hash_descriptor *hash)
/* find a blank spot */ /* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) { for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].name == NULL) { if (hash_descriptor[x].name == NULL) {
memcpy(&hash_descriptor[x], hash, sizeof(struct _hash_descriptor)); XMEMCPY(&hash_descriptor[x], hash, sizeof(struct _hash_descriptor));
return x; return x;
} }
} }

View File

@ -26,7 +26,7 @@ int register_prng(const struct _prng_descriptor *prng)
/* find a blank spot */ /* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) { for (x = 0; x < TAB_SIZE; x++) {
if (prng_descriptor[x].name == NULL) { if (prng_descriptor[x].name == NULL) {
memcpy(&prng_descriptor[x], prng, sizeof(struct _prng_descriptor)); XMEMCPY(&prng_descriptor[x], prng, sizeof(struct _prng_descriptor));
return x; return x;
} }
} }

View File

@ -21,7 +21,7 @@ int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr)
if ((unsigned long)ctr->blocklen > *len) { if ((unsigned long)ctr->blocklen > *len) {
return CRYPT_BUFFER_OVERFLOW; return CRYPT_BUFFER_OVERFLOW;
} }
memcpy(IV, ctr->ctr, ctr->blocklen); XMEMCPY(IV, ctr->ctr, ctr->blocklen);
*len = ctr->blocklen; *len = ctr->blocklen;
return CRYPT_OK; return CRYPT_OK;

View File

@ -30,7 +30,7 @@ int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
} }
/* set IV */ /* set IV */
memcpy(ctr->ctr, IV, len); XMEMCPY(ctr->ctr, IV, len);
/* force next block */ /* force next block */
ctr->padlen = 0; ctr->padlen = 0;

File diff suppressed because it is too large Load Diff

356
demos/test/.ccmalloc Normal file
View File

@ -0,0 +1,356 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% generic configuration file for %%%%
%%%% the ccmalloc memory profiler %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-----------------------------------------------------------------%
% COPY THIS FILE TO '.ccmalloc' in your project or home directory %
%-----------------------------------------------------------------%
##############################################################################
## (C) 1997-2003 Armin Biere, 1998 Johannes Keukelaar
## $Id: ccmalloc.cfg,v 1.6 2003/02/03 08:03:54 biere Exp $
##############################################################################
%%% '%' and '#' are comments !!!!!!!
% This file must be called '.ccmalloc' and is searched for in the
% current directory and in the home directory of the user. If it
% does not exist then the default values mentioned below are used.
% It is also the only available user manual yet ;-) So here is a reading
% hint. First have a look at the short one line descriptions of each option
% ...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% with 'file' the executable is specified [a.out]
% ----------------------------------------------------------------------
% This should not be necessary for Linux and Solaris because the proc
% file system can be used to find argv[0].
%
% (the rest of this comment only applies to other OS)
%
% For other OS you should use this option unless the executable is
% in the current directory or its name is 'a.out'.
%
% If you do not specify this then ccmalloc tries to find an executable
% in the current directory that matches the running program starting
% with 'a.out'. For this process it must call 'nm' on each executable
% file in the directory which may be time consuming. With this option
% you can speed up this process.
%
% You can also specify absolute or relative path names. This is
% necessary if you do not start your program from the current directory.
% But you can also simply link or name your program to 'a.out'.
%file FILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'log' specify the logfile [stderr]
% ----------------------------------------------------------------------
% The default is to use stderr. The argument to 'log' is the name of
% the file you want to write to. It can also be 'stdout' or '-' which
% sets stdout as logfile. If the logfile is stdout or stderr and is
% connected to a terminal then the output is slightly different.
%
% For big programs the logfile can be really big. To reduce the size
% you can use a small chain length (see 'chain-length' below). The other
% possibility is to use compressed logfiles. This can be done by
% specifying a logfile name with a '.gz' (or a '.Z') suffix. This means
% that gnuzip (resp. compress) is used to compress the output.
%log FILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'logpid' specify the logfile
% ----------------------------------------------------------------------
% Can be used alternatively to the 'log' command if you want to use
% ccmalloc for debugging parallel applications where several copies of
% the program you are debugging must be run simoultaneously. In this
% case you can not use 'log' because you do not want to write to the same
% log file. Using 'logpid' uses a file name ending with the <pid> of
% the process which means the name is unique even if several copies of
% your program are run simoultaneously.
%
% If you use the compressing suffixes then the <pid> is inserted before
% the suffix (e.g. 'logpid ccmalloc.log.gz' uses 'ccmalloc.log.<pid>.gz'
% as the name for the log file).
%logpid FILE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'dont-log-chain' skip info about certain chains []
% ----------------------------------------------------------------------
% This command may be repeated any number of times. The argument to this
% command is a comma-separated list of function-or-file-and-line
% specifications. Garbage allocated from a callchain that contains this
% subchain anywhere will _not_ be logged.
%
% The ';'-separated list should not contain any spaces. E.g. not:
%
% main ; foo ; bar
%
% but:
%
% main;foo;bar
%
% A function-or-file-and-line specification is a string followed by an
% optional colon and number, for example: main or main:14 or main.c or
% main.c:15. Note that the string is compared with both the function and the
% file name, if available. If main.c happens to be a function name, that
% will cause a match (for that string at least). Not specifying a line
% number will match any line number. If line number information is not
% available, anything will match! Not specifying a name (e.g. ;;;) will
% match an unknown function name. Not giving any parameters at all, will
% match a chain containing at least one unknown function.
%
% Note that if you say 'dont-log-chain wrapper.c' nothing will be logged.
%dont-log-chain
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'only-log-chain' skip info about other chains []
% ----------------------------------------------------------------------
% The obvious counterpart to dont-log-chain. In this case, only matching
% chains will be reported. Non-matching chains will not be reported.
% Can be repeated any number of times; if the chain matches any of the
% instances, it will be reported.
%only-log-chain
########################################################################
# #
# This is the 'flag' section #
# #
# 'set FLAG' is the same as 'set FLAG 1' #
# #
# The default values are those set below. If 'silent' is disabled #
# then you will find the banner in the log file (or it is listed on #
# stdout or stderr). The banner describes the current settings of all #
# these flags. #
# #
########################################################################
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% with 'only-count' ccmalloc only counts garbage - no call chains [0]
% ----------------------------------------------------------------------
% If only-count is set to one then only one additional pointer for
% each allocated data is used and no call chain is generated. This is
% the fasted and most space efficient mode ccmalloc can operate
% in. In this mode you get at least the size of garbage produced.
%
% Note that 'check-free-space' does not work at all with 'only-count'
% set and over writes ('check-overwrites') are only checked when
% calling free.
%set only-count 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'load-dynlibs' load dynamic linked libraries into gdb [0]
% ----------------------------------------------------------------------
% If your program is linked with dynamic libraries, function and file
% name information is not available for addresses in those libraries,
% unless you set 'load-dynlibs' to 1.
%set load-dynlibs 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'keep-deallocated-data' does not recycle deallocated data [0]
% ----------------------------------------------------------------------
% If you enable keep-deallocated-data then all data deallocated with
% 'free' (or 'delete' in C++) is not given back to the free store
% but stays associated with the call chain of its allocation. This is
% very useful if your program does multiple deallocation of the
% same data.
%set keep-deallocated-data 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-overwrites' detect overwrites [0]
% ----------------------------------------------------------------------
% If you want to detect 'off by n bytes' errors you should set
% 'checking-overwrites' to n/4 (on 32-Bit machines).
%
% ccmalloc inserts a boundary above allocated data. This boundary
% consists of 'check-overwrites' words. If your program writes to
% this area then ccmalloc can detect this (see also check-start
% and check-interval). 'ccmalloc' also does checking for overwrites
% at non word boundaries (e.g. strcpy(malloc(strlen("hello")),"hello");)
set check-overwrites 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-underwrites' detect underwrites [0]
% ----------------------------------------------------------------------
% same with writes below allocated data. You do not have to set this
% option if you only want detect 'off (below) by one' errors because
% ccmalloc keeps a magic value just before the user data.
set check-underwrites 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-free-space' can be used to find dangling pointers. [0]
% ----------------------------------------------------------------------
% A very serious type of bug is to write on data that has already been
% freed. If this happens the free space management of malloc is in
% trouble and you will perhaps encounter non deterministic behaviour of
% your program. To test this first enable 'keep-deallocated-data' and
% restart your program. If the problem goes away and ccmalloc does not
% report anything then you should *also* enable 'check-free-space'. Now
% ccmalloc checks already deallocated data for corruption.
%
% Note that to perform this check 'keep-deallocated-data' also must
% be enabled and 'only-count' disabled.
set check-free-space 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-interval' can be used to speed up checks [0]
% ----------------------------------------------------------------------
% If check-overwrite, check-underwrites or check-free-space is set then
% the default is to do 'write checks' when data is deallocated and
% to do 'free space checks' when reporting together with
% 'write checks' for garbage. When you want these checks to be
% performed more often then you should set 'check-interval' to a
% positive number. This number is the interval between the number of
% calls to free or malloc without performing the checks.
%set check-interval 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-start' can be used to speed up checks [0]
% ----------------------------------------------------------------------
% The flag 'check-start' delays the start of checks until the given
% number of calls to free and malloc have occured. Together with
% 'check-interval' you can use a binary search to find an aproximation
% when a corruption occured! If you simply set check-interval to 1 and
% check-start to 0 then this will slow done your program too much.
%set check-start 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'silent' disables banner [0]
% ----------------------------------------------------------------------
% If you don't want to see the banner of ccmalloc then set
% 'silent' to 1 (f.e. when logging to stderr)
%set silent
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'file-info' en/disables file and line number information [1]
% ----------------------------------------------------------------------
% If your program was compiled with debugging information (-g) then
% ccmalloc can generate line number and file info for call chains opening
% a pipe to gdb. For very big programs this method is slow. In this case
% you can set 'file-info' to zero and you will only get the function
% names. For SunOS 4.3.1 'nm' does not 'demangle' C++ identifiers
% very well. So gdb is called instead but only if 'file-info' is
% not set to 0.
%set file-info 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'continue' if ccmalloc aborts when something weired happened [0]
% ----------------------------------------------------------------------
% If the free function of ccmalloc is called with an argument that does
% not make sense to ccmalloc or that has already been freed then you
% probably want the program to stop at this point. This is also
% the default behaviour. But you can force ccmalloc also to ignore
% this if you set 'continue' to 1. This flag also controls the behaviour
% of ccmalloc when free space is found to be corrupted or a write
% boundary has been overwritten.
%set continue 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'chain-length' is the length of the maximal call chain [0 = infinite]
% ----------------------------------------------------------------------
% You can restrict the length of call chains by setting 'chain-length'
% to a number greater than zero. If 'chain-length' is zero (the default)
% then chains are as long as possible (on a non x86 system only call
% chains with a finite maximal length can be generated). For big
% programs especially if keep-deallocated-data is enabled this can
% reduce the size of the log file from over 100MB to several MB!
%set chain-length 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'print-addresses' of data [0]
% ----------------------------------------------------------------------
% If you want to see the addresses of the allocated data (and
% deallocated data if keep-deallocated-data is set to 1) set
% 'print-addresses' to 1.
%set print-addresses 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'print-on-one-line' shortens log file [0]
% ----------------------------------------------------------------------
% The default is to print function names and file/line number info
% on separate lines. With 'print-on-one-line' set 1 all are printed
% on one line.
%set print-on-one-line 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'additional-line' enlarges readability [1]
% ----------------------------------------------------------------------
% When printing call chains an empty line is printed between to
% call points. Set 'additional-line' to 0 to disable this feature.
%set additional-line 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'statistics' enables more accurate profiling [0]
% ----------------------------------------------------------------------
% Calculate number of allocations and deallocations and bytes also on
% a per call chain basis. This uses 4 additional pointers for each
% call chain.
set statistics 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set order for sorting of call chains [1] [1]
% ----------------------------------------------------------------------
% When printing the report to the log file the call chains are sorted by
% default with respect to the largest accumulated garbage produced by
% that call chain. This can be changed with setting 'sort-by-wasted'
% to 0. In this case they are sorted by the number of allocated bytes.
% If you want the number of allocations (only possible if 'statistics'
% is enabled) as sorting criteria instead then set 'sort-by-size' to 0.
%set sort-by-wasted 1
%set sort-by-size 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% report library chains [0]
% ----------------------------------------------------------------------
% Some external libraries (like libg++) have memory leaks. On some
% systems even a call to printf produces a leak. ccmalloc tries to
% detect this (only heuristically!) and with this flag you can control
% if leaks produced by such library calls are reported.
%
% Since version 0.2.1 some similar effect can be achieved by using
% 'dont-log-chain' with no argument.
%set library-chains 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% print debugging information [X] (compile time dependend)
% ----------------------------------------------------------------------
%set debug X
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% align memory on 8 byte boundary [0] (no effect on SunOS or Solaris)
% ----------------------------------------------------------------------
%set align-8-byte 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% only report allocations which ended up being wasted (i.e don't report
% allocations which were completely freed properly. ) [1]
% ----------------------------------------------------------------------
%set only-wasting-alloc 1

View File

@ -10,8 +10,8 @@ int dh_tests (void)
DO(dh_test()); DO(dh_test());
/* make up two keys */ /* make up two keys */
DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &usera)); DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera));
DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 96, &userb)); DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &userb));
/* make the shared secret */ /* make the shared secret */
x = 4096; x = 4096;
@ -50,7 +50,7 @@ int dh_tests (void)
dh_free (&userb); dh_free (&userb);
/* test encrypt_key */ /* test encrypt_key */
dh_make_key (&test_yarrow, find_prng ("yarrow"), 128, &usera); dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera);
for (x = 0; x < 16; x++) { for (x = 0; x < 16; x++) {
buf[0][x] = x; buf[0][x] = x;
} }

View File

@ -10,8 +10,8 @@ int ecc_tests (void)
DO(ecc_test ()); DO(ecc_test ());
/* make up two keys */ /* make up two keys */
DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &usera)); DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &usera));
DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 24, &userb)); DO(ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &userb));
/* make the shared secret */ /* make the shared secret */
x = 4096; x = 4096;
@ -53,7 +53,7 @@ int ecc_tests (void)
ecc_free (&userb); ecc_free (&userb);
/* test encrypt_key */ /* test encrypt_key */
ecc_make_key (&test_yarrow, find_prng ("yarrow"), 20, &usera); ecc_make_key (&test_yarrow, find_prng ("yarrow"), 65, &usera);
for (x = 0; x < 32; x++) { for (x = 0; x < 32; x++) {
buf[0][x] = x; buf[0][x] = x;
} }

View File

@ -3,10 +3,10 @@
int mac_test(void) int mac_test(void)
{ {
DO(hmac_test()); DO(hmac_test());
DO(pmac_test()); DO(pmac_test());
DO(omac_test()); DO(omac_test());
DO(eax_test()); DO(eax_test());
DO(ocb_test()); DO(ocb_test());
return 0; return 0;
} }

View File

@ -1,13 +1,25 @@
# make test harness, it is good. # make test harness, it is good.
CFLAGS += -Wall -W -Os -I../../ -I./ CFLAGS += -Wall -W -Os -I../../ -I./
# add -g3 for ccmalloc debugging
#CFLAGS += -g3
# if you're not debugging
CFLAGS += -fomit-frame-pointer
default: test default: test
OBJECTS=test.o cipher_hash_test.o mac_test.o modes_test.o \ OBJECTS=test.o cipher_hash_test.o mac_test.o modes_test.o \
pkcs_1_test.o store_test.o rsa_test.o ecc_test.o dsa_test.c dh_tests.o pkcs_1_test.o store_test.o rsa_test.o ecc_test.o dsa_test.c dh_tests.o
#uncomment this to get heap checking [e.g. memory leaks]. Note
#that you *MUST* build libtomcrypt.a with -g3 enabled [and make install it]
#
#
#CCMALLOC = -lccmalloc -ldl
test: $(OBJECTS) test: $(OBJECTS)
$(CC) $(OBJECTS) -ltomcrypt -o test $(CC) $(OBJECTS) -ltomcrypt $(CCMALLOC) -o test
clean: clean:
rm -f test *.o *.obj *.exe *~ rm -f test *.o *.obj *.exe *~

View File

@ -17,7 +17,7 @@ int pkcs_1_test(void)
} }
/* do many tests */ /* do many tests */
for (x = 0; x < 10000; x++) { for (x = 0; x < 100; x++) {
zeromem(buf, sizeof(buf)); zeromem(buf, sizeof(buf));
/* make a dummy message (of random length) */ /* make a dummy message (of random length) */

View File

@ -9,8 +9,9 @@ test_entry test_list[26] = {
{"cipher_hash_test", "b", "a", cipher_hash_test }, {"cipher_hash_test", "b", "a", cipher_hash_test },
{"modes_test", "c", "b", modes_test }, {"modes_test", "c", "b", modes_test },
{"mac_test", "d", "c", mac_test }, {"mac_test", "d", "c", mac_test },
{"pkcs_1_test", "e", "b", pkcs_1_test }, {"pkcs_1_test", "e", "b", pkcs_1_test },
{"rsa_test", "f", "e", rsa_test }, {"rsa_test", "f", "", rsa_test },
{"ecc_test", "g", "a", ecc_tests }, {"ecc_test", "g", "a", ecc_tests },
{"dsa_test", "h", "a", dsa_test }, {"dsa_test", "h", "a", dsa_test },
{"dh_test", "i", "a", dh_tests }, {"dh_test", "i", "a", dh_tests },
@ -153,12 +154,53 @@ void sort(void)
} }
} while (y == 1); } while (y == 1);
} }
#define STACKBLOCK 8
#define STACK_EST_USAGE 32768
unsigned char stack_mask[STACKBLOCK];
unsigned long stack_cur=0;
void stack_masker(void)
{
#ifdef STACK_TEST
volatile unsigned char M[STACK_EST_USAGE];
stack_cur = 0;
for (stack_cur = 0; stack_cur < STACK_EST_USAGE/STACKBLOCK; stack_cur++) {
memcpy(M+(stack_cur*STACKBLOCK), stack_mask, STACKBLOCK);
}
#endif
}
void stack_check(void)
{
#ifdef STACK_TEST
unsigned char M[STACK_EST_USAGE];
stack_cur = 0;
#ifdef STACK_DOWN
while (memcmp(M+(STACK_EST_USAGE-STACKBLOCK-stack_cur), stack_mask, STACKBLOCK) &&
#else
while (memcmp(M+stack_cur, stack_mask, STACKBLOCK) &&
#endif
stack_cur < (STACK_EST_USAGE - STACKBLOCK)) {
++stack_cur;
}
#endif
}
int main(void) int main(void)
{ {
int x;
/* setup stack checker */
srand(time(NULL));
for (x = 0; x < STACKBLOCK; x++) {
stack_mask[x] = rand() & 255;
}
stack_masker();
printf("Built with\n%s\n", crypt_build_settings); printf("Built with\n%s\n", crypt_build_settings);
srand(time(NULL));
sort(); sort();
register_algs(); register_algs();
@ -167,6 +209,22 @@ int main(void)
DO(yarrow_add_entropy("test", 4, &test_yarrow)); DO(yarrow_add_entropy("test", 4, &test_yarrow));
DO(yarrow_ready(&test_yarrow)); DO(yarrow_ready(&test_yarrow));
// output sizes
printf("Sizes of objects (in bytes)\n");
printf("\tsymmetric_key\t=\t%5d\n", sizeof(symmetric_key));
printf("\thash_state\t=\t%5d\n", sizeof(hash_state));
printf("\thmac_state\t=\t%5d\n", sizeof(hmac_state));
printf("\tomac_state\t=\t%5d\n", sizeof(omac_state));
printf("\tpmac_state\t=\t%5d\n", sizeof(pmac_state));
printf("\tocb_state\t=\t%5d\n", sizeof(ocb_state));
printf("\teax_state\t=\t%5d\n", sizeof(eax_state));
printf("\tmp_int\t\t=\t%5d\n", sizeof(mp_int));
printf("\trsa_key\t\t=\t%5d\n", sizeof(rsa_key));
printf("\tdsa_key\t\t=\t%5d\n", sizeof(dsa_key));
printf("\tdh_key\t\t=\t%5d\n", sizeof(dh_key));
printf("\tecc_key\t\t=\t%5d\n", sizeof(ecc_key));
printf("\n\n");
// do tests // do tests
for (current_test = 0; tests[current_test].name != NULL; current_test++) { for (current_test = 0; tests[current_test].name != NULL; current_test++) {
printf("[%-20s]: ", tests[current_test].name); fflush(stdout); printf("[%-20s]: ", tests[current_test].name); fflush(stdout);

View File

@ -3,6 +3,12 @@
#include "mycrypt.h" #include "mycrypt.h"
/* enable stack testing */
// #define STACK_TEST
/* stack testing, define this if stack usage goes downwards [e.g. x86] */
#define STACK_DOWN
typedef struct { typedef struct {
char *name, *prov, *req; char *name, *prov, *req;
int (*entry)(void); int (*entry)(void);
@ -10,10 +16,14 @@ typedef struct {
extern prng_state test_yarrow; extern prng_state test_yarrow;
void stack_masker(void);
void stack_check(void);
extern unsigned long stack_cur;
#define stack_chk(x) { stack_check(); if (stack_cur >= 1024) { fprintf(stderr, " Warning: Stack usage of %lu in %s, %s:%d\n", stack_cur, x, __FILE__, __LINE__); } }
void run_cmd(int res, int line, char *file, char *cmd); void run_cmd(int res, int line, char *file, char *cmd);
#define DO(x) run_cmd((x), __LINE__, __FILE__, #x) #define DO(x) { stack_masker(); run_cmd((x), __LINE__, __FILE__, #x); stack_chk(#x); }
/* TESTS */ /* TESTS */
int cipher_hash_test(void); int cipher_hash_test(void);

2
des.c
View File

@ -1377,7 +1377,7 @@ static void cookey(const ulong32 *raw1, ulong32 *keyout)
*cook++ |= (*raw1 & 0x0000003fL); *cook++ |= (*raw1 & 0x0000003fL);
} }
memcpy(keyout, dough, sizeof dough); XMEMCPY(keyout, dough, sizeof dough);
} }
#ifdef CLEAN_STACK #ifdef CLEAN_STACK

22
dh.c
View File

@ -12,6 +12,9 @@
#ifdef MDH #ifdef MDH
/* max export size we'll encounter (smaller than this but lets round up a bit */
#define DH_BUF_SIZE 1200
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */ /* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
static const struct { static const struct {
int size; int size;
@ -226,7 +229,7 @@ int dh_get_size(dh_key *key)
int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key) int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key)
{ {
unsigned char buf[512]; unsigned char *buf;
unsigned long x; unsigned long x;
mp_int p, g; mp_int p, g;
int err; int err;
@ -251,14 +254,21 @@ int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key)
} }
key->idx = x; key->idx = x;
/* allocate buffer */
buf = XMALLOC(keysize);
if (buf == NULL) {
return CRYPT_MEM;
}
/* make up random string */ /* make up random string */
if (prng_descriptor[wprng].read(buf, keysize, prng) != (unsigned long)keysize) { if (prng_descriptor[wprng].read(buf, keysize, prng) != (unsigned long)keysize) {
return CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto error2;
} }
/* init parameters */ /* init parameters */
if ((err = mp_init_multi(&g, &p, &key->x, &key->y, NULL)) != MP_OKAY) { if ((err = mp_init_multi(&g, &p, &key->x, &key->y, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err); goto error;
} }
if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&g, sets[key->idx].base, 64)) != MP_OKAY) { goto error; }
if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p, sets[key->idx].prime, 64)) != MP_OKAY) { goto error; }
@ -276,10 +286,14 @@ int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key)
goto done; goto done;
error: error:
err = mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
error2:
mp_clear_multi(&key->x, &key->y, NULL); mp_clear_multi(&key->x, &key->y, NULL);
done: done:
#ifdef CLEAN_STACK
zeromem(buf, keysize);
#endif
mp_clear_multi(&p, &g, NULL); mp_clear_multi(&p, &g, NULL);
zeromem(buf, sizeof(buf)); XFREE(buf);
return err; return err;
} }

126
dh_sys.c
View File

@ -13,7 +13,7 @@ int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
prng_state *prng, int wprng, int hash, prng_state *prng, int wprng, int hash,
dh_key *key) dh_key *key)
{ {
unsigned char pub_expt[768], dh_shared[768], skey[MAXBLOCKSIZE]; unsigned char *pub_expt, *dh_shared, *skey;
dh_key pubkey; dh_key pubkey;
unsigned long x, y, z, hashsize, pubkeysize; unsigned long x, y, z, hashsize, pubkeysize;
int err; int err;
@ -36,36 +36,54 @@ int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
return CRYPT_INVALID_HASH; return CRYPT_INVALID_HASH;
} }
/* make a random key and export the public copy */ /* allocate memory */
if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) { pub_expt = XMALLOC(DH_BUF_SIZE);
return err; dh_shared = XMALLOC(DH_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (pub_expt == NULL || dh_shared == NULL || skey == NULL) {
if (pub_expt != NULL) {
XFREE(pub_expt);
}
if (dh_shared != NULL) {
XFREE(dh_shared);
}
if (skey != NULL) {
XFREE(skey);
}
return CRYPT_MEM;
} }
pubkeysize = sizeof(pub_expt); /* make a random key and export the public copy */
if ((err = dh_make_key(prng, wprng, dh_get_size(key), &pubkey)) != CRYPT_OK) {
goto __ERR;
}
pubkeysize = DH_BUF_SIZE;
if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { if ((err = dh_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
dh_free(&pubkey); dh_free(&pubkey);
return err; goto __ERR;
} }
/* now check if the out buffer is big enough */ /* now check if the out buffer is big enough */
if (*len < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + keylen)) { if (*len < (1 + 4 + 4 + PACKET_SIZE + pubkeysize + keylen)) {
dh_free(&pubkey); dh_free(&pubkey);
return CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
} }
/* make random key */ /* make random key */
hashsize = hash_descriptor[hash].hashsize; hashsize = hash_descriptor[hash].hashsize;
x = (unsigned long)sizeof(dh_shared); x = DH_BUF_SIZE;
if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) { if ((err = dh_shared_secret(&pubkey, key, dh_shared, &x)) != CRYPT_OK) {
dh_free(&pubkey); dh_free(&pubkey);
return err; goto __ERR;
} }
dh_free(&pubkey); dh_free(&pubkey);
z = sizeof(skey); z = MAXBLOCKSIZE;
if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) { if ((err = hash_memory(hash, dh_shared, x, skey, &z)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* store header */ /* store header */
@ -93,21 +111,26 @@ int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
} }
*len = y; *len = y;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
/* clean up */ /* clean up */
zeromem(pub_expt, sizeof(pub_expt)); zeromem(pub_expt, DH_BUF_SIZE);
zeromem(dh_shared, sizeof(dh_shared)); zeromem(dh_shared, DH_BUF_SIZE);
zeromem(skey, sizeof(skey)); zeromem(skey, MAXBLOCKSIZE);
#endif #endif
XFREE(skey);
XFREE(dh_shared);
XFREE(pub_expt);
return CRYPT_OK; return err;
} }
int dh_decrypt_key(const unsigned char *in, unsigned long inlen, int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen, unsigned char *outkey, unsigned long *keylen,
dh_key *key) dh_key *key)
{ {
unsigned char shared_secret[768], skey[MAXBLOCKSIZE]; unsigned char *shared_secret, *skey;
unsigned long x, y, z,hashsize, keysize; unsigned long x, y, z,hashsize, keysize;
int hash, err; int hash, err;
dh_key pubkey; dh_key pubkey;
@ -122,23 +145,38 @@ int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
return CRYPT_PK_NOT_PRIVATE; return CRYPT_PK_NOT_PRIVATE;
} }
/* allocate ram */
shared_secret = XMALLOC(DH_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (shared_secret == NULL || skey == NULL) {
if (shared_secret != NULL) {
XFREE(shared_secret);
}
if (skey != NULL) {
XFREE(skey);
}
return CRYPT_MEM;
}
/* check if initial header should fit */ /* check if initial header should fit */
if (inlen < PACKET_SIZE+1+4+4) { if (inlen < PACKET_SIZE+1+4+4) {
return CRYPT_INVALID_PACKET; err = CRYPT_INVALID_PACKET;
goto __ERR;
} else { } else {
inlen -= PACKET_SIZE+1+4+4; inlen -= PACKET_SIZE+1+4+4;
} }
/* is header correct? */ /* is header correct? */
if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_ENC_KEY)) != CRYPT_OK) { if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DH, PACKET_SUB_ENC_KEY)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* now lets get the hash name */ /* now lets get the hash name */
y = PACKET_SIZE; y = PACKET_SIZE;
hash = find_hash_id(in[y++]); hash = find_hash_id(in[y++]);
if (hash == -1) { if (hash == -1) {
return CRYPT_INVALID_HASH; err = CRYPT_INVALID_HASH;
goto __ERR;
} }
/* common values */ /* common values */
@ -149,28 +187,29 @@ int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
/* now check if the imported key will fit */ /* now check if the imported key will fit */
if (inlen < x) { if (inlen < x) {
return CRYPT_INVALID_PACKET; err = CRYPT_INVALID_PACKET;
goto __ERR;
} else { } else {
inlen -= x; inlen -= x;
} }
y += 4; y += 4;
if ((err = dh_import(in+y, x, &pubkey)) != CRYPT_OK) { if ((err = dh_import(in+y, x, &pubkey)) != CRYPT_OK) {
return err; goto __ERR;
} }
y += x; y += x;
/* make shared key */ /* make shared key */
x = (unsigned long)sizeof(shared_secret); x = DH_BUF_SIZE;
if ((err = dh_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) { if ((err = dh_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) {
dh_free(&pubkey); dh_free(&pubkey);
return err; goto __ERR;
} }
dh_free(&pubkey); dh_free(&pubkey);
z = sizeof(skey); z = MAXBLOCKSIZE;
if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) { if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* load in the encrypted key */ /* load in the encrypted key */
@ -178,14 +217,15 @@ int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
/* will the outkey fit as part of the input */ /* will the outkey fit as part of the input */
if (inlen < keysize) { if (inlen < keysize) {
return CRYPT_INVALID_PACKET; err = CRYPT_INVALID_PACKET;
goto __ERR;
} else { } else {
inlen -= keysize; inlen -= keysize;
} }
if (keysize > *keylen) { if (keysize > *keylen) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
y += 4; y += 4;
@ -196,11 +236,15 @@ int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
} }
err = CRYPT_OK; err = CRYPT_OK;
done: __ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(shared_secret, sizeof(shared_secret)); zeromem(shared_secret, DH_BUF_SIZE);
zeromem(skey, sizeof(skey)); zeromem(skey, MAXBLOCKSIZE);
#endif #endif
XFREE(skey);
XFREE(shared_secret);
return err; return err;
} }
@ -227,7 +271,7 @@ int dh_sign_hash(const unsigned char *in, unsigned long inlen,
prng_state *prng, int wprng, dh_key *key) prng_state *prng, int wprng, dh_key *key)
{ {
mp_int a, b, k, m, g, p, p1, tmp; mp_int a, b, k, m, g, p, p1, tmp;
unsigned char buf[520]; unsigned char *buf;
unsigned long x, y; unsigned long x, y;
int err; int err;
@ -250,18 +294,23 @@ int dh_sign_hash(const unsigned char *in, unsigned long inlen,
return CRYPT_PK_INVALID_TYPE; return CRYPT_PK_INVALID_TYPE;
} }
/* allocate ram for buf */
buf = XMALLOC(520);
/* make up a random value k, /* make up a random value k,
* since the order of the group is prime * since the order of the group is prime
* we need not check if gcd(k, r) is 1 * we need not check if gcd(k, r) is 1
*/ */
if (prng_descriptor[wprng].read(buf, sets[key->idx].size, prng) != if (prng_descriptor[wprng].read(buf, sets[key->idx].size, prng) !=
(unsigned long)(sets[key->idx].size)) { (unsigned long)(sets[key->idx].size)) {
return CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto __ERR;
} }
/* init bignums */ /* init bignums */
if ((err = mp_init_multi(&a, &b, &k, &m, &p, &g, &p1, &tmp, NULL)) != MP_OKAY) { if ((err = mp_init_multi(&a, &b, &k, &m, &p, &g, &p1, &tmp, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
goto __ERR;
} }
/* load k and m */ /* load k and m */
@ -290,7 +339,7 @@ int dh_sign_hash(const unsigned char *in, unsigned long inlen,
/* check for overflow */ /* check for overflow */
if ((unsigned long)(PACKET_SIZE + 4 + 4 + mp_unsigned_bin_size(&a) + mp_unsigned_bin_size(&b)) > *outlen) { if ((unsigned long)(PACKET_SIZE + 4 + 4 + mp_unsigned_bin_size(&a) + mp_unsigned_bin_size(&b)) > *outlen) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
/* store header */ /* store header */
@ -310,7 +359,7 @@ int dh_sign_hash(const unsigned char *in, unsigned long inlen,
/* check if size too big */ /* check if size too big */
if (*outlen < y) { if (*outlen < y) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
/* store header */ /* store header */
@ -318,11 +367,14 @@ int dh_sign_hash(const unsigned char *in, unsigned long inlen,
*outlen = y; *outlen = y;
err = CRYPT_OK; err = CRYPT_OK;
goto done; goto __ERR;
error: error:
err = mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
done: __ERR:
mp_clear_multi(&tmp, &p1, &g, &p, &m, &k, &b, &a, NULL); mp_clear_multi(&tmp, &p1, &g, &p, &m, &k, &b, &a, NULL);
XFREE(buf);
return err; return err;
} }

Binary file not shown.

View File

@ -14,9 +14,9 @@
int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key) int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key)
{ {
mp_int tmp, tmp2; mp_int tmp, tmp2;
int err, res; int err, res;
unsigned char buf[512]; unsigned char *buf;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
@ -26,26 +26,33 @@ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size,
} }
/* check size */ /* check size */
if (group_size >= 1024 || group_size <= 15 || if (group_size >= MDSA_MAX_GROUP || group_size <= 15 ||
group_size >= modulus_size || (modulus_size - group_size) >= (int)sizeof(buf)) { group_size >= modulus_size || (modulus_size - group_size) >= MDSA_DELTA) {
return CRYPT_INVALID_ARG; return CRYPT_INVALID_ARG;
} }
/* allocate ram */
buf = XMALLOC(MDSA_DELTA);
if (buf == NULL) {
return CRYPT_MEM;
}
/* init mp_ints */ /* init mp_ints */
if ((err = mp_init_multi(&tmp, &tmp2, &key->g, &key->q, &key->p, &key->x, &key->y, NULL)) != MP_OKAY) { if ((err = mp_init_multi(&tmp, &tmp2, &key->g, &key->q, &key->p, &key->x, &key->y, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
goto __ERR;
} }
/* make our prime q */ /* make our prime q */
if ((err = rand_prime(&key->q, group_size*8, prng, wprng)) != CRYPT_OK) { goto error2; } if ((err = rand_prime(&key->q, group_size*8, prng, wprng)) != CRYPT_OK) { goto __ERR; }
/* double q */ /* double q */
if ((err = mp_mul_2(&key->q, &tmp)) != MP_OKAY) { goto error; } if ((err = mp_mul_2(&key->q, &tmp)) != MP_OKAY) { goto error; }
/* now make a random string and multply it against q */ /* now make a random string and multply it against q */
if (prng_descriptor[wprng].read(buf+1, modulus_size - group_size, prng) != (unsigned long)(modulus_size - group_size)) { if (prng_descriptor[wprng].read(buf+1, modulus_size - group_size, prng) != (unsigned long)(modulus_size - group_size)) {
err = CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto error2; goto __ERR;
} }
/* force magnitude */ /* force magnitude */
@ -60,7 +67,7 @@ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size,
/* now loop until p is prime */ /* now loop until p is prime */
for (;;) { for (;;) {
if ((err = is_prime(&key->p, &res)) != CRYPT_OK) { goto error2; } if ((err = is_prime(&key->p, &res)) != CRYPT_OK) { goto __ERR; }
if (res == MP_YES) break; if (res == MP_YES) break;
/* add 2q to p and 2 to tmp2 */ /* add 2q to p and 2 to tmp2 */
@ -85,7 +92,7 @@ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size,
do { do {
if (prng_descriptor[wprng].read(buf, group_size, prng) != (unsigned long)group_size) { if (prng_descriptor[wprng].read(buf, group_size, prng) != (unsigned long)group_size) {
err = CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto error2; goto __ERR;
} }
if ((err = mp_read_unsigned_bin(&key->x, buf, group_size)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&key->x, buf, group_size)) != MP_OKAY) { goto error; }
} while (mp_cmp_d(&key->x, 1) != MP_GT); } while (mp_cmp_d(&key->x, 1) != MP_GT);
@ -101,17 +108,21 @@ int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size,
if ((err = mp_shrink(&key->x)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->x)) != MP_OKAY) { goto error; }
if ((err = mp_shrink(&key->y)) != MP_OKAY) { goto error; } if ((err = mp_shrink(&key->y)) != MP_OKAY) { goto error; }
err = CRYPT_OK;
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, MDSA_DELTA);
#endif #endif
err = CRYPT_OK;
goto done; goto done;
error : err = mpi_to_ltc_error(err); error:
error2: mp_clear_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL); err = mpi_to_ltc_error(err);
done : mp_clear_multi(&tmp, &tmp2, NULL); __ERR:
return err; mp_clear_multi(&key->g, &key->q, &key->p, &key->x, &key->y, NULL);
done:
mp_clear_multi(&tmp, &tmp2, NULL);
XFREE(buf);
return err;
} }
#endif #endif

View File

@ -16,11 +16,10 @@ int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, dsa_key *key) prng_state *prng, int wprng, dsa_key *key)
{ {
mp_int k, kinv, tmp, r, s; mp_int k, kinv, tmp, r, s;
unsigned char buf[512]; unsigned char *buf;
int err, y; int err, y;
unsigned long len; unsigned long len;
_ARGCHK(in != NULL); _ARGCHK(in != NULL);
_ARGCHK(out != NULL); _ARGCHK(out != NULL);
@ -35,10 +34,15 @@ int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
} }
/* check group order size */ /* check group order size */
if (key->qord >= (int)sizeof(buf)) { if (key->qord >= MDSA_MAX_GROUP) {
return CRYPT_INVALID_ARG; return CRYPT_INVALID_ARG;
} }
buf = XMALLOC(MDSA_MAX_GROUP);
if (buf == NULL) {
return CRYPT_MEM;
}
/* Init our temps */ /* Init our temps */
if ((err = mp_init_multi(&k, &kinv, &r, &s, &tmp, NULL)) != MP_OKAY) { goto error; } if ((err = mp_init_multi(&k, &kinv, &r, &s, &tmp, NULL)) != MP_OKAY) { goto error; }
@ -48,7 +52,7 @@ retry:
/* gen random k */ /* gen random k */
if (prng_descriptor[wprng].read(buf, key->qord, prng) != (unsigned long)key->qord) { if (prng_descriptor[wprng].read(buf, key->qord, prng) != (unsigned long)key->qord) {
err = CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto done; goto __ERR;
} }
/* read k */ /* read k */
@ -83,7 +87,7 @@ retry:
/* first check that we have enough room */ /* first check that we have enough room */
if (*outlen < (unsigned long)(PACKET_SIZE + 4 + mp_unsigned_bin_size(&s) + mp_unsigned_bin_size(&r))) { if (*outlen < (unsigned long)(PACKET_SIZE + 4 + mp_unsigned_bin_size(&s) + mp_unsigned_bin_size(&r))) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
/* packet header */ /* packet header */
@ -112,13 +116,16 @@ retry:
*outlen = y; *outlen = y;
err = CRYPT_OK; err = CRYPT_OK;
goto done; goto __ERR;
error : err = mpi_to_ltc_error(err); error:
done : mp_clear_multi(&k, &kinv, &r, &s, &tmp, NULL); err = mpi_to_ltc_error(err);
__ERR:
mp_clear_multi(&k, &kinv, &r, &s, &tmp, NULL);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, MDSA_MAX_GROUP);
#endif #endif
XFREE(buf);
return err; return err;
} }

View File

@ -23,38 +23,58 @@ int eax_decrypt_verify_memory(int cipher,
unsigned char *tag, unsigned long taglen, unsigned char *tag, unsigned long taglen,
int *res) int *res)
{ {
int err; int err;
eax_state eax; eax_state *eax;
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf;
unsigned long buflen; unsigned long buflen;
_ARGCHK(res != NULL); _ARGCHK(res != NULL);
/* default to zero */ /* default to zero */
*res = 0; *res = 0;
if ((err = eax_init(&eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) { /* allocate ram */
return err; buf = XMALLOC(taglen);
eax = XMALLOC(sizeof(eax_state));
if (eax == NULL || buf == NULL) {
if (eax != NULL) {
XFREE(eax);
}
if (buf != NULL) {
XFREE(buf);
}
return CRYPT_MEM;
} }
if ((err = eax_decrypt(&eax, ct, pt, ctlen)) != CRYPT_OK) { if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
return err; goto __ERR;
}
if ((err = eax_decrypt(eax, ct, pt, ctlen)) != CRYPT_OK) {
goto __ERR;
} }
buflen = MIN(sizeof(buf), taglen); buflen = taglen;
if ((err = eax_done(&eax, buf, &buflen)) != CRYPT_OK) { if ((err = eax_done(eax, buf, &buflen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* compare tags */ /* compare tags */
if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) { if (buflen >= taglen && memcmp(buf, tag, taglen) == 0) {
*res = 1; *res = 1;
} }
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, taglen);
zeromem(eax, sizeof(eax_state));
#endif #endif
return CRYPT_OK;
XFREE(eax);
XFREE(buf);
return err;
} }
#endif #endif

View File

@ -17,17 +17,31 @@
int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen) int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)
{ {
int err; int err;
unsigned char headermac[MAXBLOCKSIZE], ctmac[MAXBLOCKSIZE]; unsigned char *headermac, *ctmac;
unsigned long x, len; unsigned long x, len;
_ARGCHK(eax != NULL); _ARGCHK(eax != NULL);
_ARGCHK(tag != NULL); _ARGCHK(tag != NULL);
_ARGCHK(taglen != NULL); _ARGCHK(taglen != NULL);
/* allocate ram */
headermac = XMALLOC(MAXBLOCKSIZE);
ctmac = XMALLOC(MAXBLOCKSIZE);
if (headermac == NULL || ctmac == NULL) {
if (headermac != NULL) {
XFREE(headermac);
}
if (ctmac != NULL) {
XFREE(ctmac);
}
return CRYPT_MEM;
}
/* finish ctomac */ /* finish ctomac */
len = sizeof(ctmac); len = MAXBLOCKSIZE;
if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) { if ((err = omac_done(&eax->ctomac, ctmac, &len)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* finish headeromac */ /* finish headeromac */
@ -35,7 +49,7 @@ int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)
/* note we specifically don't reset len so the two lens are minimal */ /* note we specifically don't reset len so the two lens are minimal */
if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) { if ((err = omac_done(&eax->headeromac, headermac, &len)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* compute N xor H xor C */ /* compute N xor H xor C */
@ -44,13 +58,18 @@ int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen)
} }
*taglen = x; *taglen = x;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(ctmac, sizeof(ctmac)); zeromem(ctmac, MAXBLOCKSIZE);
zeromem(headermac, sizeof(headermac)); zeromem(headermac, MAXBLOCKSIZE);
zeromem(eax, sizeof(*eax)); zeromem(eax, sizeof(*eax));
#endif #endif
return CRYPT_OK; XFREE(ctmac);
XFREE(headermac);
return err;
} }
#endif #endif

View File

@ -23,21 +23,31 @@ int eax_encrypt_authenticate_memory(int cipher,
unsigned char *tag, unsigned long *taglen) unsigned char *tag, unsigned long *taglen)
{ {
int err; int err;
eax_state eax; eax_state *eax;
if ((err = eax_init(&eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) { eax = XMALLOC(sizeof(eax_state));
return err;
if ((err = eax_init(eax, cipher, key, keylen, nonce, noncelen, header, headerlen)) != CRYPT_OK) {
goto __ERR;
} }
if ((err = eax_encrypt(&eax, pt, ct, ptlen)) != CRYPT_OK) { if ((err = eax_encrypt(eax, pt, ct, ptlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = eax_done(&eax, tag, taglen)) != CRYPT_OK) { if ((err = eax_done(eax, tag, taglen)) != CRYPT_OK) {
return err; goto __ERR;
} }
return CRYPT_OK; err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(eax, sizeof(eax_state));
#endif
XFREE(eax);
return err;
} }
#endif #endif

View File

@ -18,9 +18,9 @@ int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long
const unsigned char *nonce, unsigned long noncelen, const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen) const unsigned char *header, unsigned long headerlen)
{ {
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf;
int err, blklen; int err, blklen;
omac_state omac; omac_state *omac;
unsigned long len; unsigned long len;
@ -36,42 +36,56 @@ int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long
} }
blklen = cipher_descriptor[cipher].block_length; blklen = cipher_descriptor[cipher].block_length;
/* allocate ram */
buf = XMALLOC(MAXBLOCKSIZE);
omac = XMALLOC(sizeof(omac_state));
if (buf == NULL || omac == NULL) {
if (buf != NULL) {
XFREE(buf);
}
if (omac != NULL) {
XFREE(omac);
}
return CRYPT_MEM;
}
/* N = OMAC_0K(nonce) */ /* N = OMAC_0K(nonce) */
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE);
if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) { if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* omac the [0]_n */ /* omac the [0]_n */
if ((err = omac_process(&omac, buf, blklen)) != CRYPT_OK) { if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* omac the nonce */ /* omac the nonce */
if ((err = omac_process(&omac, nonce, noncelen)) != CRYPT_OK) { if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* store result */ /* store result */
len = sizeof(eax->N); len = sizeof(eax->N);
if ((err = omac_done(&omac, eax->N, &len)) != CRYPT_OK) { if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* H = OMAC_1K(header) */ /* H = OMAC_1K(header) */
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE);
buf[blklen - 1] = 1; buf[blklen - 1] = 1;
if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) { if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* omac the [1]_n */ /* omac the [1]_n */
if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) { if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* omac the header */ /* omac the header */
if (headerlen != 0) { if (headerlen != 0) {
if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) { if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
} }
@ -79,28 +93,34 @@ int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long
/* setup the CTR mode */ /* setup the CTR mode */
if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) { if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* use big-endian counter */ /* use big-endian counter */
eax->ctr.mode = 1; eax->ctr.mode = 1;
/* setup the OMAC for the ciphertext */ /* setup the OMAC for the ciphertext */
if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) { if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* omac [2]_n */ /* omac [2]_n */
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE);
buf[blklen-1] = 2; buf[blklen-1] = 2;
if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) { if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
return err; goto __ERR;
} }
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE);
zeromem(&omac, sizeof(omac)); zeromem(omac, sizeof(omac_state));
#endif #endif
return CRYPT_OK;
XFREE(omac);
XFREE(buf);
return err;
} }
#endif #endif

47
ecc.c
View File

@ -19,6 +19,9 @@
#ifdef MECC #ifdef MECC
/* size of our temp buffers for exported keys */
#define ECC_BUF_SIZE 160
/* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */ /* This holds the key settings. ***MUST*** be organized by size from smallest to largest. */
static const struct { static const struct {
int size; int size;
@ -368,10 +371,10 @@ done:
static int ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *modulus) static int ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *modulus)
{ {
ecc_point *tG, *M[8]; ecc_point *tG, *M[8];
int i, j, err; int i, j, err;
mp_int mu; mp_int mu;
mp_digit buf; mp_digit buf;
int first, bitbuf, bitcpy, bitcnt, mode, digidx; int first, bitbuf, bitcpy, bitcnt, mode, digidx;
/* init barrett reduction */ /* init barrett reduction */
if ((err = mp_init(&mu)) != MP_OKAY) { if ((err = mp_init(&mu)) != MP_OKAY) {
@ -595,10 +598,10 @@ void ecc_sizes(int *low, int *high)
int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key) int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
{ {
int x, err; int x, err;
ecc_point *base; ecc_point *base;
mp_int prime; mp_int prime;
unsigned char buf[128]; unsigned char *buf;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
@ -616,19 +619,29 @@ int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
} }
key->idx = x; key->idx = x;
/* allocate ram */
base = NULL;
buf = XMALLOC(128);
if (buf == NULL) {
return CRYPT_MEM;
}
/* make up random string */ /* make up random string */
if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) { if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
return CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto __ERR2;
} }
/* setup the key variables */ /* setup the key variables */
if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL)) != MP_OKAY) { if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL)) != MP_OKAY) {
return mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
goto __ERR;
} }
base = new_point(); base = new_point();
if (base == NULL) { if (base == NULL) {
mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL); mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL);
return CRYPT_MEM; err = CRYPT_MEM;
goto __ERR;
} }
/* read in the specs for this key */ /* read in the specs for this key */
@ -638,7 +651,7 @@ int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
if ((err = mp_read_unsigned_bin(&key->k, (unsigned char *)buf, keysize)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&key->k, (unsigned char *)buf, keysize)) != MP_OKAY) { goto error; }
/* make the public key */ /* make the public key */
if ((err = ecc_mulmod(&key->k, base, &key->pubkey, &prime)) != CRYPT_OK) { goto done; } if ((err = ecc_mulmod(&key->k, base, &key->pubkey, &prime)) != CRYPT_OK) { goto __ERR; }
key->type = PK_PRIVATE; key->type = PK_PRIVATE;
/* shrink key */ /* shrink key */
@ -648,15 +661,19 @@ int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
/* free up ram */ /* free up ram */
err = CRYPT_OK; err = CRYPT_OK;
goto done; goto __ERR;
error: error:
err = mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
done: __ERR:
del_point(base); del_point(base);
mp_clear(&prime); mp_clear(&prime);
__ERR2:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, 128);
#endif #endif
XFREE(buf);
return err; return err;
} }

157
ecc_sys.c
View File

@ -13,10 +13,10 @@ int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
prng_state *prng, int wprng, int hash, prng_state *prng, int wprng, int hash,
ecc_key *key) ecc_key *key)
{ {
unsigned char pub_expt[256], ecc_shared[256], skey[MAXBLOCKSIZE]; unsigned char *pub_expt, *ecc_shared, *skey;
ecc_key pubkey; ecc_key pubkey;
unsigned long x, y, z, hashsize, pubkeysize; unsigned long x, y, z, hashsize, pubkeysize;
int err; int err;
_ARGCHK(inkey != NULL); _ARGCHK(inkey != NULL);
_ARGCHK(out != NULL); _ARGCHK(out != NULL);
@ -41,29 +41,47 @@ int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
return err; return err;
} }
pubkeysize = (unsigned long)sizeof(pub_expt); pub_expt = XMALLOC(ECC_BUF_SIZE);
ecc_shared = XMALLOC(ECC_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
if (pub_expt != NULL) {
XFREE(pub_expt);
}
if (ecc_shared != NULL) {
XFREE(ecc_shared);
}
if (skey != NULL) {
XFREE(skey);
}
ecc_free(&pubkey);
return CRYPT_MEM;
}
pubkeysize = ECC_BUF_SIZE;
if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
ecc_free(&pubkey); ecc_free(&pubkey);
return err; goto __ERR;
} }
/* now check if the out buffer is big enough */ /* now check if the out buffer is big enough */
if (*len < (9 + PACKET_SIZE + pubkeysize + hash_descriptor[hash].hashsize)) { if (*len < (9 + PACKET_SIZE + pubkeysize + hash_descriptor[hash].hashsize)) {
ecc_free(&pubkey); ecc_free(&pubkey);
return CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
} }
/* make random key */ /* make random key */
hashsize = hash_descriptor[hash].hashsize; hashsize = hash_descriptor[hash].hashsize;
x = (unsigned long)sizeof(ecc_shared); x = ECC_BUF_SIZE;
if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) { if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
ecc_free(&pubkey); ecc_free(&pubkey);
return err; goto __ERR;
} }
ecc_free(&pubkey); ecc_free(&pubkey);
z = (unsigned long)sizeof(skey); z = MAXBLOCKSIZE;
if ((err = hash_memory(hash, ecc_shared, x, skey, &z)) != CRYPT_OK) { if ((err = hash_memory(hash, ecc_shared, x, skey, &z)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* store header */ /* store header */
@ -92,23 +110,30 @@ int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
} }
*len = y; *len = y;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
/* clean up */ /* clean up */
zeromem(pub_expt, sizeof(pub_expt)); zeromem(pub_expt, ECC_BUF_SIZE);
zeromem(ecc_shared, sizeof(ecc_shared)); zeromem(ecc_shared, ECC_BUF_SIZE);
zeromem(skey, sizeof(skey)); zeromem(skey, MAXBLOCKSIZE);
#endif #endif
return CRYPT_OK;
XFREE(skey);
XFREE(ecc_shared);
XFREE(pub_expt);
return err;
} }
int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen, unsigned char *outkey, unsigned long *keylen,
ecc_key *key) ecc_key *key)
{ {
unsigned char shared_secret[256], skey[MAXBLOCKSIZE]; unsigned char *shared_secret, *skey;
unsigned long x, y, z, hashsize, keysize; unsigned long x, y, z, hashsize, keysize;
int hash, err; int hash, err;
ecc_key pubkey; ecc_key pubkey;
_ARGCHK(in != NULL); _ARGCHK(in != NULL);
_ARGCHK(outkey != NULL); _ARGCHK(outkey != NULL);
@ -155,22 +180,37 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
} }
y += x; y += x;
/* allocate memory */
shared_secret = XMALLOC(ECC_BUF_SIZE);
skey = XMALLOC(MAXBLOCKSIZE);
if (shared_secret == NULL || skey == NULL) {
if (shared_secret != NULL) {
XFREE(shared_secret);
}
if (skey != NULL) {
XFREE(skey);
}
ecc_free(&pubkey);
return CRYPT_MEM;
}
/* make shared key */ /* make shared key */
x = (unsigned long)sizeof(shared_secret); x = ECC_BUF_SIZE;
if ((err = ecc_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) { if ((err = ecc_shared_secret(key, &pubkey, shared_secret, &x)) != CRYPT_OK) {
ecc_free(&pubkey); ecc_free(&pubkey);
return err; goto __ERR;
} }
ecc_free(&pubkey); ecc_free(&pubkey);
z = (unsigned long)sizeof(skey); z = MAXBLOCKSIZE;
if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) { if ((err = hash_memory(hash, shared_secret, x, skey, &z)) != CRYPT_OK) {
return err; goto __ERR;
} }
LOAD32L(keysize, in+y); LOAD32L(keysize, in+y);
if (inlen < keysize) { if (inlen < keysize) {
return CRYPT_INVALID_PACKET; err = CRYPT_INVALID_PACKET;
goto __ERR;
} else { } else {
inlen -= keysize; inlen -= keysize;
} }
@ -178,7 +218,7 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
if (*keylen < keysize) { if (*keylen < keysize) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
/* Decrypt the key */ /* Decrypt the key */
@ -189,11 +229,15 @@ int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
*keylen = keysize; *keylen = keysize;
err = CRYPT_OK; err = CRYPT_OK;
done: __ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(shared_secret, sizeof(shared_secret)); zeromem(shared_secret, ECC_BUF_SIZE);
zeromem(skey, sizeof(skey)); zeromem(skey, MAXBLOCKSIZE);
#endif #endif
XFREE(skey);
XFREE(shared_secret);
return err; return err;
} }
@ -201,11 +245,11 @@ int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, ecc_key *key) prng_state *prng, int wprng, ecc_key *key)
{ {
ecc_key pubkey; ecc_key pubkey;
mp_int b, p; mp_int b, p;
unsigned char epubkey[256], er[256]; unsigned char *epubkey, *er;
unsigned long x, y, pubkeysize, rsize; unsigned long x, y, pubkeysize, rsize;
int err; int err;
_ARGCHK(in != NULL); _ARGCHK(in != NULL);
_ARGCHK(out != NULL); _ARGCHK(out != NULL);
@ -231,17 +275,32 @@ int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
return err; return err;
} }
pubkeysize = (unsigned long)sizeof(epubkey); /* allocate ram */
epubkey = XMALLOC(ECC_BUF_SIZE);
er = XMALLOC(ECC_BUF_SIZE);
if (epubkey == NULL || er == NULL) {
if (epubkey != NULL) {
XFREE(epubkey);
}
if (er != NULL) {
XFREE(er);
}
ecc_free(&pubkey);
return CRYPT_MEM;
}
pubkeysize = ECC_BUF_SIZE;
if ((err = ecc_export(epubkey, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) { if ((err = ecc_export(epubkey, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
ecc_free(&pubkey); ecc_free(&pubkey);
return err; goto __ERR;
} }
/* get the hash and load it as a bignum into 'b' */ /* get the hash and load it as a bignum into 'b' */
/* init the bignums */ /* init the bignums */
if ((err = mp_init_multi(&b, &p, NULL)) != MP_OKAY) { if ((err = mp_init_multi(&b, &p, NULL)) != MP_OKAY) {
ecc_free(&pubkey); ecc_free(&pubkey);
return mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
goto __ERR;
} }
if ((err = mp_read_radix(&p, (char *)sets[key->idx].order, 64)) != MP_OKAY) { goto error; } if ((err = mp_read_radix(&p, (char *)sets[key->idx].order, 64)) != MP_OKAY) { goto error; }
if ((err = mp_read_unsigned_bin(&b, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; } if ((err = mp_read_unsigned_bin(&b, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
@ -253,7 +312,7 @@ int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
/* export it */ /* export it */
rsize = (unsigned long)mp_unsigned_bin_size(&b); rsize = (unsigned long)mp_unsigned_bin_size(&b);
if (rsize > (unsigned long)sizeof(er)) { if (rsize > ECC_BUF_SIZE) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto error; goto error;
} }
@ -262,7 +321,7 @@ int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
/* now lets check the outlen before we write */ /* now lets check the outlen before we write */
if (*outlen < (12 + rsize + pubkeysize)) { if (*outlen < (12 + rsize + pubkeysize)) {
err = CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto done; goto __ERR;
} }
/* lets output */ /* lets output */
@ -288,20 +347,24 @@ int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
/* store header */ /* store header */
packet_store_header(out, PACKET_SECT_ECC, PACKET_SUB_SIGNED); packet_store_header(out, PACKET_SECT_ECC, PACKET_SUB_SIGNED);
/* clear memory */
*outlen = y; *outlen = y;
/* all ok */
err = CRYPT_OK; err = CRYPT_OK;
goto done; goto __ERR;
error: error:
err = mpi_to_ltc_error(err); err = mpi_to_ltc_error(err);
done: __ERR:
mp_clear_multi(&b, &p, NULL); mp_clear_multi(&b, &p, NULL);
ecc_free(&pubkey); ecc_free(&pubkey);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(er, sizeof(er)); zeromem(er, ECC_BUF_SIZE);
zeromem(epubkey, sizeof(epubkey)); zeromem(epubkey, ECC_BUF_SIZE);
#endif #endif
XFREE(epubkey);
XFREE(er);
return err; return err;
} }
@ -322,11 +385,11 @@ int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long inlen, const unsigned char *hash, unsigned long inlen,
int *stat, ecc_key *key) int *stat, ecc_key *key)
{ {
ecc_point *mG; ecc_point *mG;
ecc_key pubkey; ecc_key pubkey;
mp_int b, p, m, mu; mp_int b, p, m, mu;
unsigned long x, y; unsigned long x, y;
int err; int err;
_ARGCHK(sig != NULL); _ARGCHK(sig != NULL);
_ARGCHK(hash != NULL); _ARGCHK(hash != NULL);

View File

@ -36,14 +36,16 @@ int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outle
hash_descriptor[hash].init(&md); hash_descriptor[hash].init(&md);
do { do {
x = fread(buf, 1, sizeof(buf), in); x = fread(buf, 1, sizeof(buf), in);
hash_descriptor[hash].process(&md, buf, x); if ((err = hash_descriptor[hash].process(&md, buf, x)) != CRYPT_OK) {
return err;
}
} while (x == sizeof(buf)); } while (x == sizeof(buf));
hash_descriptor[hash].done(&md, dst); err = hash_descriptor[hash].done(&md, dst);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, sizeof(buf));
#endif #endif
return CRYPT_OK; return err;
#endif #endif
} }

View File

@ -12,7 +12,7 @@
int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen) int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen)
{ {
hash_state md; hash_state *md;
int err; int err;
_ARGCHK(data != NULL); _ARGCHK(data != NULL);
@ -26,10 +26,23 @@ int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned
if (*outlen < hash_descriptor[hash].hashsize) { if (*outlen < hash_descriptor[hash].hashsize) {
return CRYPT_BUFFER_OVERFLOW; return CRYPT_BUFFER_OVERFLOW;
} }
*outlen = hash_descriptor[hash].hashsize;
hash_descriptor[hash].init(&md); md = XMALLOC(sizeof(hash_state));
hash_descriptor[hash].process(&md, data, len); if (md == NULL) {
hash_descriptor[hash].done(&md, dst); return CRYPT_MEM;
return CRYPT_OK; }
hash_descriptor[hash].init(md);
if ((err = hash_descriptor[hash].process(md, data, len)) != CRYPT_OK) {
goto __ERR;
}
err = hash_descriptor[hash].done(md, dst);
*outlen = hash_descriptor[hash].hashsize;
__ERR:
#ifdef CLEAN_STACK
zeromem(md, sizeof(hash_state));
#endif
XFREE(md);
return err;
} }

View File

@ -35,14 +35,14 @@
int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen) int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
{ {
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf, *isha;
unsigned char isha[MAXBLOCKSIZE];
unsigned long hashsize, i; unsigned long hashsize, i;
int hash, err; int hash, err;
_ARGCHK(hmac != NULL); _ARGCHK(hmac != NULL);
_ARGCHK(hashOut != NULL); _ARGCHK(hashOut != NULL);
/* test hash */
hash = hmac->hash; hash = hmac->hash;
if((err = hash_is_valid(hash)) != CRYPT_OK) { if((err = hash_is_valid(hash)) != CRYPT_OK) {
return err; return err;
@ -51,9 +51,22 @@ int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
/* get the hash message digest size */ /* get the hash message digest size */
hashsize = hash_descriptor[hash].hashsize; hashsize = hash_descriptor[hash].hashsize;
/* allocate buffers */
buf = XMALLOC(HMAC_BLOCKSIZE);
isha = XMALLOC(hashsize);
if (buf == NULL || isha == NULL) {
if (buf != NULL) {
XFREE(buf);
}
if (isha != NULL) {
XFREE(isha);
}
return CRYPT_MEM;
}
// Get the hash of the first HMAC vector plus the data // Get the hash of the first HMAC vector plus the data
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) { if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
return err; goto __ERR;
} }
// Create the second HMAC vector vector for step (3) // Create the second HMAC vector vector for step (3)
@ -63,9 +76,15 @@ int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
// Now calculate the "outer" hash for step (5), (6), and (7) // Now calculate the "outer" hash for step (5), (6), and (7)
hash_descriptor[hash].init(&hmac->md); hash_descriptor[hash].init(&hmac->md);
hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE); if ((err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE)) != CRYPT_OK) {
hash_descriptor[hash].process(&hmac->md, isha, hashsize); goto __ERR;
hash_descriptor[hash].done(&hmac->md, buf); }
if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
goto __ERR;
}
if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {
goto __ERR;
}
// copy to output // copy to output
for (i = 0; i < hashsize && i < *outlen; i++) { for (i = 0; i < hashsize && i < *outlen; i++) {
@ -73,12 +92,18 @@ int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
} }
*outlen = i; *outlen = i;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(isha, sizeof(buf)); zeromem(isha, hashsize);
zeromem(buf, sizeof(isha)); zeromem(buf, hashsize);
zeromem(hmac, sizeof(*hmac)); zeromem(hmac, sizeof(*hmac));
#endif #endif
return CRYPT_OK;
XFREE(isha);
XFREE(buf);
return err;
} }
#endif #endif

View File

@ -12,27 +12,8 @@
#include "mycrypt.h" #include "mycrypt.h"
/*
(1) append zeros to the end of K to create a B byte string
(e.g., if K is of length 20 bytes and B=64, then K will be
appended with 44 zero bytes 0x00)
(2) XOR (bitwise exclusive-OR) the B byte string computed in step
(1) with ipad (ipad = the byte 0x36 repeated B times)
(3) append the stream of data 'text' to the B byte string resulting
from step (2)
(4) apply H to the stream generated in step (3)
(5) XOR (bitwise exclusive-OR) the B byte string computed in
step (1) with opad (opad = the byte 0x5C repeated B times.)
(6) append the H result from step (4) to the B byte string
resulting from step (5)
(7) apply H to the stream generated in step (6) and output
the result
*/
#ifdef HMAC #ifdef HMAC
#define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
/* hmac_file added by Tom St Denis */ /* hmac_file added by Tom St Denis */
int hmac_file(int hash, const char *fname, int hmac_file(int hash, const char *fname,
const unsigned char *key, unsigned long keylen, const unsigned char *key, unsigned long keylen,

View File

@ -35,38 +35,44 @@
int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen) int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen)
{ {
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf;
unsigned long hashsize; unsigned long hashsize;
unsigned long i, z; unsigned long i, z;
int err; int err;
_ARGCHK(hmac != NULL); _ARGCHK(hmac != NULL);
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
/* valid hash? */
if ((err = hash_is_valid(hash)) != CRYPT_OK) { if ((err = hash_is_valid(hash)) != CRYPT_OK) {
return err; return err;
} }
hmac->hash = hash;
hashsize = hash_descriptor[hash].hashsize;
/* valid key length? */ /* valid key length? */
if (keylen == 0) { if (keylen == 0) {
return CRYPT_INVALID_KEYSIZE; return CRYPT_INVALID_KEYSIZE;
} }
hmac->hash = hash; /* allocate ram for buf */
buf = XMALLOC(HMAC_BLOCKSIZE);
if (buf == NULL) {
return CRYPT_MEM;
}
// (1) make sure we have a large enough key // (1) make sure we have a large enough key
hashsize = hash_descriptor[hash].hashsize;
if(keylen > HMAC_BLOCKSIZE) { if(keylen > HMAC_BLOCKSIZE) {
z = (unsigned long)sizeof(hmac->key); z = (unsigned long)sizeof(hmac->key);
if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) { if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
return err; goto __ERR;
} }
if(hashsize < HMAC_BLOCKSIZE) { if(hashsize < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize)); zeromem((hmac->key) + hashsize, (size_t)(HMAC_BLOCKSIZE - hashsize));
} }
keylen = hashsize; keylen = hashsize;
} else { } else {
memcpy(hmac->key, key, (size_t)keylen); XMEMCPY(hmac->key, key, (size_t)keylen);
if(keylen < HMAC_BLOCKSIZE) { if(keylen < HMAC_BLOCKSIZE) {
zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen)); zeromem((hmac->key) + keylen, (size_t)(HMAC_BLOCKSIZE - keylen));
} }
@ -79,9 +85,14 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
// Pre-pend that to the hash data // Pre-pend that to the hash data
hash_descriptor[hash].init(&hmac->md); hash_descriptor[hash].init(&hmac->md);
hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE); err = hash_descriptor[hash].process(&hmac->md, buf, HMAC_BLOCKSIZE);
__ERR:
return CRYPT_OK; #ifdef CLEAN_STACK
zeromem(buf, HMAC_BLOCKSIZE);
#endif
XFREE(buf);
return err;
} }
#endif #endif

View File

@ -12,55 +12,46 @@
#include "mycrypt.h" #include "mycrypt.h"
/*
(1) append zeros to the end of K to create a B byte string
(e.g., if K is of length 20 bytes and B=64, then K will be
appended with 44 zero bytes 0x00)
(2) XOR (bitwise exclusive-OR) the B byte string computed in step
(1) with ipad (ipad = the byte 0x36 repeated B times)
(3) append the stream of data 'text' to the B byte string resulting
from step (2)
(4) apply H to the stream generated in step (3)
(5) XOR (bitwise exclusive-OR) the B byte string computed in
step (1) with opad (opad = the byte 0x5C repeated B times.)
(6) append the H result from step (4) to the B byte string
resulting from step (5)
(7) apply H to the stream generated in step (6) and output
the result
*/
#ifdef HMAC #ifdef HMAC
#define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
int hmac_memory(int hash, const unsigned char *key, unsigned long keylen, int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
const unsigned char *data, unsigned long len, const unsigned char *data, unsigned long len,
unsigned char *dst, unsigned long *dstlen) unsigned char *dst, unsigned long *dstlen)
{ {
hmac_state hmac; hmac_state *hmac;
int err; int err;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
_ARGCHK(data != NULL); _ARGCHK(data != NULL);
_ARGCHK(dst != NULL); _ARGCHK(dst != NULL);
_ARGCHK(dstlen != NULL); _ARGCHK(dstlen != NULL);
if((err = hash_is_valid(hash)) != CRYPT_OK) { /* allocate ram for hmac state */
return err; hmac = XMALLOC(sizeof(hmac_state));
if (hmac == NULL) {
return CRYPT_MEM;
} }
if ((err = hmac_init(&hmac, hash, key, keylen)) != CRYPT_OK) { if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hmac_process(&hmac, data, len)) != CRYPT_OK) { if ((err = hmac_process(hmac, data, len)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hmac_done(&hmac, dst, dstlen)) != CRYPT_OK) { if ((err = hmac_done(hmac, dst, dstlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
return CRYPT_OK;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(hmac, sizeof(hmac_state));
#endif
XFREE(hmac);
return err;
} }
#endif #endif

View File

@ -12,27 +12,8 @@
#include "mycrypt.h" #include "mycrypt.h"
/*
(1) append zeros to the end of K to create a B byte string
(e.g., if K is of length 20 bytes and B=64, then K will be
appended with 44 zero bytes 0x00)
(2) XOR (bitwise exclusive-OR) the B byte string computed in step
(1) with ipad (ipad = the byte 0x36 repeated B times)
(3) append the stream of data 'text' to the B byte string resulting
from step (2)
(4) apply H to the stream generated in step (3)
(5) XOR (bitwise exclusive-OR) the B byte string computed in
step (1) with opad (opad = the byte 0x5C repeated B times.)
(6) append the H result from step (4) to the B byte string
resulting from step (5)
(7) apply H to the stream generated in step (6) and output
the result
*/
#ifdef HMAC #ifdef HMAC
#define HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len) int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len)
{ {
int err; int err;

View File

@ -27,7 +27,7 @@
#define MAX(x,y) ((x)>(y)?(x):(y)) #define MAX(x,y) ((x)>(y)?(x):(y))
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { "C" {
/* C++ compilers don't like assigning void * to mp_digit * */ /* C++ compilers don't like assigning void * to mp_digit * */
#define OPT_CAST(x) (x *) #define OPT_CAST(x) (x *)
@ -99,12 +99,16 @@ extern "C" {
#define XFREE free #define XFREE free
#define XREALLOC realloc #define XREALLOC realloc
#define XCALLOC calloc #define XCALLOC calloc
#define XMEMSET memset
#define XMEMCPY memcpy
#else #else
/* prototypes for our heap functions */ /* prototypes for our heap functions */
extern void *XMALLOC(size_t n); void *XMALLOC(size_t n);
extern void *REALLOC(void *p, size_t n); void *REALLOC(void *p, size_t n);
extern void *XCALLOC(size_t n, size_t s); void *XCALLOC(size_t n, size_t s);
extern void XFREE(void *p); void XFREE(void *p);
void *XMEMCPY(void *dest, const void *src, size_t n);
int XMEMCMP(const void *s1, const void *s2, size_t n);
#endif #endif
#endif #endif
@ -143,7 +147,7 @@ extern "C" {
typedef int mp_err; typedef int mp_err;
/* you'll have to tune these... */ /* you'll have to tune these... */
extern int KARATSUBA_MUL_CUTOFF, int KARATSUBA_MUL_CUTOFF,
KARATSUBA_SQR_CUTOFF, KARATSUBA_SQR_CUTOFF,
TOOM_MUL_CUTOFF, TOOM_MUL_CUTOFF,
TOOM_SQR_CUTOFF; TOOM_SQR_CUTOFF;
@ -546,7 +550,7 @@ int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y); int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
void bn_reverse(unsigned char *s, int len); void bn_reverse(unsigned char *s, int len);
extern const char *mp_s_rmap; const char *mp_s_rmap;
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -2,14 +2,9 @@
# #
# Tom St Denis # Tom St Denis
# Modified by Clay Culver # Modified by Clay Culver
#
# NOTE: This should later be replaced by autoconf/automake scripts, but for
# the time being this is actually pretty clean. The only ugly part is
# handling CFLAGS so that the x86 specific optimizations don't break
# a build. This is easy to remedy though, for those that have problems.
# The version # The version
VERSION=0.96 VERSION=0.97
# Compiler and Linker Names # Compiler and Linker Names
#CC=gcc #CC=gcc
@ -26,15 +21,14 @@ CFLAGS += -c -I./ -Wall -Wsign-compare -W -Wshadow
# optimize for SPEED # optimize for SPEED
#CFLAGS += -O3 -funroll-loops #CFLAGS += -O3 -funroll-loops
#add -fomit-frame-pointer. GCC v3.2 is buggy for certain platforms! #add -fomit-frame-pointer. hinders debugging!
CFLAGS += -fomit-frame-pointer CFLAGS += -fomit-frame-pointer
# optimize for SIZE # optimize for SIZE
CFLAGS += -Os CFLAGS += -Os
# compile for DEBUGING # compile for DEBUGING (required for ccmalloc checking!!!)
#CFLAGS += -g3 #CFLAGS += -g3
#ch1-01-3
#These flags control how the library gets built. #These flags control how the library gets built.
@ -147,8 +141,11 @@ default:library
#ciphers come in two flavours... enc+dec and enc #ciphers come in two flavours... enc+dec and enc
aes_enc.o: aes.c aes_tab.c aes_enc.o: aes.c aes_tab.c
$(CC) $(CFLAGS) -DENCRYPT_ONLY -c aes.c -o aes_enc.o $(CC) $(CFLAGS) -DENCRYPT_ONLY -c aes.c -o aes_enc.o
#These are the rules to make certain object files. #These are the rules to make certain object files.
aes.o: aes.c aes_tab.c
twofish.o: twofish.c twofish_tab.c
whirl.o: whirl.c whirltab.c
ecc.o: ecc.c ecc_sys.c ecc.o: ecc.c ecc_sys.c
dh.o: dh.c dh_sys.c dh.o: dh.c dh_sys.c
sha512.o: sha512.c sha384.c sha512.o: sha512.c sha384.c
@ -195,7 +192,7 @@ clean:
rm -f $(OBJECTS) $(TESTOBJECTS) $(HASHOBJECTS) $(CRYPTOBJECTS) $(SMALLOBJECTS) $(LEFTOVERS) $(LIBNAME) rm -f $(OBJECTS) $(TESTOBJECTS) $(HASHOBJECTS) $(CRYPTOBJECTS) $(SMALLOBJECTS) $(LEFTOVERS) $(LIBNAME)
rm -f $(TEST) $(HASH) $(COMPRESSED) $(PROFS) $(PROF) $(TVS) $(TV) rm -f $(TEST) $(HASH) $(COMPRESSED) $(PROFS) $(PROF) $(TVS) $(TV)
rm -f *.a *.dll *stackdump *.lib *.exe *.obj demos/*.obj demos/*.o *.bat *.txt *.il *.da demos/*.il demos/*.da *.dyn *.dpi \ rm -f *.a *.dll *stackdump *.lib *.exe *.obj demos/*.obj demos/*.o *.bat *.txt *.il *.da demos/*.il demos/*.da *.dyn *.dpi \
*.gcda *.gcno demos/*.gcno demos/*.gcda *~ doc/* *.gcda *.gcno demos/*.gcno demos/*.gcda *~ doc/*
cd demos/test ; make clean cd demos/test ; make clean
#This builds the crypt.pdf file. Note that the rm -f *.pdf has been removed #This builds the crypt.pdf file. Note that the rm -f *.pdf has been removed
@ -206,8 +203,8 @@ docs: crypt.tex
rm -f doc/crypt.pdf $(LEFTOVERS) rm -f doc/crypt.pdf $(LEFTOVERS)
echo "hello" > crypt.ind echo "hello" > crypt.ind
latex crypt > /dev/null latex crypt > /dev/null
makeindex crypt > /dev/null
latex crypt > /dev/null latex crypt > /dev/null
makeindex crypt.idx > /dev/null
latex crypt > /dev/null latex crypt > /dev/null
dvipdf crypt dvipdf crypt
mv -ivf crypt.pdf doc/crypt.pdf mv -ivf crypt.pdf doc/crypt.pdf
@ -217,9 +214,13 @@ docdvi: crypt.tex
echo hello > crypt.ind echo hello > crypt.ind
latex crypt > /dev/null latex crypt > /dev/null
latex crypt > /dev/null latex crypt > /dev/null
makeindex crypt makeindex.idx crypt
latex crypt > /dev/null latex crypt > /dev/null
#pretty build
pretty:
perl pretty.build
#beta #beta
beta: clean beta: clean
cd .. ; rm -rf crypt* libtomcrypt-$(VERSION)-beta ; mkdir libtomcrypt-$(VERSION)-beta ; \ cd .. ; rm -rf crypt* libtomcrypt-$(VERSION)-beta ; mkdir libtomcrypt-$(VERSION)-beta ; \
@ -231,5 +232,4 @@ zipup: clean docs
cd .. ; rm -rf crypt* libtomcrypt-$(VERSION) ; mkdir libtomcrypt-$(VERSION) ; \ cd .. ; rm -rf crypt* libtomcrypt-$(VERSION) ; mkdir libtomcrypt-$(VERSION) ; \
cp -R ./libtomcrypt/* ./libtomcrypt-$(VERSION)/ ; tar -c libtomcrypt-$(VERSION)/* > crypt-$(VERSION).tar ; \ cp -R ./libtomcrypt/* ./libtomcrypt-$(VERSION)/ ; tar -c libtomcrypt-$(VERSION)/* > crypt-$(VERSION).tar ; \
bzip2 -9vv crypt-$(VERSION).tar ; zip -9 -r crypt-$(VERSION).zip libtomcrypt-$(VERSION)/* ; \ bzip2 -9vv crypt-$(VERSION).tar ; zip -9 -r crypt-$(VERSION).zip libtomcrypt-$(VERSION)/* ; \
gpg -b -a crypt-$(VERSION).tar.bz2 ; \ gpg -b -a crypt-$(VERSION).tar.bz2 ; gpg -b -a crypt-$(VERSION).zip
gpg -b -a crypt-$(VERSION).zip

View File

@ -150,7 +150,7 @@ LEFTOVERS=*.dvi *.log *.aux *.toc *.idx *.ilg *.ind
COMPRESSED=crypt.tar.bz2 crypt.zip crypt.tar.gz COMPRESSED=crypt.tar.bz2 crypt.zip crypt.tar.gz
#Header files used by libtomcrypt. #Header files used by libtomcrypt.
HEADERS=tommath.h mycrypt_cfg.h mycrypt_gf.h mycrypt_kr.h \ HEADERS=ltc_tommath.h mycrypt_cfg.h \
mycrypt_misc.h mycrypt_prng.h mycrypt_cipher.h mycrypt_hash.h \ mycrypt_misc.h mycrypt_prng.h mycrypt_cipher.h mycrypt_hash.h \
mycrypt_macros.h mycrypt_pk.h mycrypt.h mycrypt_argchk.h mycrypt_custom.h mycrypt_macros.h mycrypt_pk.h mycrypt.h mycrypt_argchk.h mycrypt_custom.h

6
md2.c
View File

@ -111,7 +111,7 @@ int md2_process(hash_state *md, const unsigned char *buf, unsigned long len)
} }
while (len > 0) { while (len > 0) {
n = MIN(len, (16 - md->md2.curlen)); n = MIN(len, (16 - md->md2.curlen));
memcpy(md->md2.buf + md->md2.curlen, buf, (size_t)n); XMEMCPY(md->md2.buf + md->md2.curlen, buf, (size_t)n);
md->md2.curlen += n; md->md2.curlen += n;
buf += n; buf += n;
len -= n; len -= n;
@ -149,11 +149,11 @@ int md2_done(hash_state * md, unsigned char *hash)
md2_update_chksum(md); md2_update_chksum(md);
/* hash checksum */ /* hash checksum */
memcpy(md->md2.buf, md->md2.chksum, 16); XMEMCPY(md->md2.buf, md->md2.chksum, 16);
md2_compress(md); md2_compress(md);
/* output is lower 16 bytes of X */ /* output is lower 16 bytes of X */
memcpy(hash, md->md2.X, 16); XMEMCPY(hash, md->md2.X, 16);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(md, sizeof(hash_state)); zeromem(md, sizeof(hash_state));

View File

@ -1,46 +0,0 @@
/* test CFB/OFB/CBC modes */
#include "test.h"
int modes_test(void)
{
unsigned char pt[64], ct[64], tmp[64], key[16], iv[16];
int x, cipher_idx;
symmetric_CBC cbc;
/* make a random pt, key and iv */
yarrow_read(pt, 64, &test_yarrow);
yarrow_read(key, 16, &test_yarrow);
yarrow_read(iv, 16, &test_yarrow);
/* test CBC mode */
cipher_idx = find_cipher("aes");
if (cipher_idx == -1) {
printf("test requires AES");
return 1;
}
/* encode the block */
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
for (x = 0; x < 4; x++) {
DO(cbc_encrypt(pt+x*16, ct+x*16, &cbc));
}
/* decode the block */
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
for (x = 0; x < 4; x++) {
DO(cbc_decrypt(ct+x*16, tmp+x*16, &cbc));
}
if (memcmp(tmp, pt, 64) != 0) {
printf("CBC failed");
return 1;
}
/*
extern int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_CBC *cbc);
extern int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc);
extern int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc);
*/
}

12
mpi.c
View File

@ -408,7 +408,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* clear temp buf (the columns) */ /* clear temp buf (the columns) */
memset (W, 0, sizeof (mp_word) * digs); XMEMSET (W, 0, sizeof (mp_word) * digs);
/* calculate the columns */ /* calculate the columns */
pa = a->used; pa = a->used;
@ -538,7 +538,7 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
/* like the other comba method we compute the columns first */ /* like the other comba method we compute the columns first */
pa = a->used; pa = a->used;
pb = b->used; pb = b->used;
memset (W + digs, 0, (pa + pb + 1 - digs) * sizeof (mp_word)); XMEMSET (W + digs, 0, (pa + pb + 1 - digs) * sizeof (mp_word));
for (ix = 0; ix < pa; ix++) { for (ix = 0; ix < pa; ix++) {
{ {
register mp_digit tmpx, *tmpy; register mp_digit tmpx, *tmpy;
@ -654,8 +654,8 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
* the inner product can be doubled using n doublings instead of * the inner product can be doubled using n doublings instead of
* n**2 * n**2
*/ */
memset (W, 0, newused * sizeof (mp_word)); XMEMSET (W, 0, newused * sizeof (mp_word));
memset (W2, 0, newused * sizeof (mp_word)); XMEMSET (W2, 0, newused * sizeof (mp_word));
/* This computes the inner product. To simplify the inner N**2 loop /* This computes the inner product. To simplify the inner N**2 loop
* the multiplication by two is done afterwards in the N loop. * the multiplication by two is done afterwards in the N loop.
@ -1145,7 +1145,7 @@ mp_clear (mp_int * a)
/* only do anything if a hasn't been freed previously */ /* only do anything if a hasn't been freed previously */
if (a->dp != NULL) { if (a->dp != NULL) {
/* first zero the digits */ /* first zero the digits */
memset (a->dp, 0, sizeof (mp_digit) * a->used); XMEMSET (a->dp, 0, sizeof (mp_digit) * a->used);
/* free ram */ /* free ram */
XFREE(a->dp); XFREE(a->dp);
@ -7538,7 +7538,7 @@ mp_zero (mp_int * a)
{ {
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
a->used = 0; a->used = 0;
memset (a->dp, 0, sizeof (mp_digit) * a->alloc); XMEMSET (a->dp, 0, sizeof (mp_digit) * a->alloc);
} }
/* End: bn_mp_zero.c */ /* End: bn_mp_zero.c */

View File

@ -12,18 +12,18 @@
#include <mycrypt_custom.h> #include <mycrypt_custom.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { "C" {
#endif #endif
/* version */ /* version */
#define CRYPT 0x0096 #define CRYPT 0x0097
#define SCRYPT "0.96" #define SCRYPT "0.97"
/* max size of either a cipher/hash block or symmetric key [largest of the two] */ /* max size of either a cipher/hash block or symmetric key [largest of the two] */
#define MAXBLOCKSIZE 128 #define MAXBLOCKSIZE 64
/* descriptor table size */ /* descriptor table size */
#define TAB_SIZE 32 #define TAB_SIZE 32
/* error codes [will be expanded in future releases] */ /* error codes [will be expanded in future releases] */
enum { enum {

View File

@ -7,7 +7,7 @@
#include <signal.h> #include <signal.h>
/* this is the default LibTomCrypt macro */ /* this is the default LibTomCrypt macro */
extern void crypt_argchk(char *v, char *s, int d); void crypt_argchk(char *v, char *s, int d);
#define _ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } #define _ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); }
#elif ARGTYPE == 1 #elif ARGTYPE == 1

View File

@ -8,13 +8,17 @@
#define MYCRYPT_CFG_H #define MYCRYPT_CFG_H
/* you can change how memory allocation works ... */ /* you can change how memory allocation works ... */
extern void *XMALLOC(size_t n); void *XMALLOC(size_t n);
extern void *REALLOC(void *p, size_t n); void *REALLOC(void *p, size_t n);
extern void *XCALLOC(size_t n, size_t s); void *XCALLOC(size_t n, size_t s);
extern void XFREE(void *p); void XFREE(void *p);
/* change the clock function too */ /* change the clock function too */
extern clock_t XCLOCK(void); clock_t XCLOCK(void);
/* various other functions */
void *XMEMCPY(void *dest, const void *src, size_t n);
int XMEMCMP(const void *s1, const void *s2, size_t n);
/* ch1-01-1 */ /* ch1-01-1 */
/* type of argument checking, 0=default, 1=fatal and 2=none */ /* type of argument checking, 0=default, 1=fatal and 2=none */

View File

@ -179,7 +179,7 @@ typedef struct Symmetric_CTR {
} symmetric_CTR; } symmetric_CTR;
/* cipher descriptor table, last entry has "name == NULL" to mark the end of table */ /* cipher descriptor table, last entry has "name == NULL" to mark the end of table */
extern struct _cipher_descriptor { extern struct _cipher_descriptor {
char *name; char *name;
unsigned char ID; unsigned char ID;
int min_key_length, max_key_length, block_length, default_rounds; int min_key_length, max_key_length, block_length, default_rounds;
@ -191,65 +191,65 @@ extern struct _cipher_descriptor {
} cipher_descriptor[]; } cipher_descriptor[];
#ifdef BLOWFISH #ifdef BLOWFISH
extern int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int blowfish_test(void); int blowfish_test(void);
extern int blowfish_keysize(int *desired_keysize); int blowfish_keysize(int *desired_keysize);
extern const struct _cipher_descriptor blowfish_desc; extern const struct _cipher_descriptor blowfish_desc;
#endif #endif
#ifdef RC5 #ifdef RC5
extern int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int rc5_test(void); int rc5_test(void);
extern int rc5_keysize(int *desired_keysize); int rc5_keysize(int *desired_keysize);
extern const struct _cipher_descriptor rc5_desc; extern const struct _cipher_descriptor rc5_desc;
#endif #endif
#ifdef RC6 #ifdef RC6
extern int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int rc6_test(void); int rc6_test(void);
extern int rc6_keysize(int *desired_keysize); int rc6_keysize(int *desired_keysize);
extern const struct _cipher_descriptor rc6_desc; extern const struct _cipher_descriptor rc6_desc;
#endif #endif
#ifdef RC2 #ifdef RC2
extern int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int rc2_test(void); int rc2_test(void);
extern int rc2_keysize(int *desired_keysize); int rc2_keysize(int *desired_keysize);
extern const struct _cipher_descriptor rc2_desc; extern const struct _cipher_descriptor rc2_desc;
#endif #endif
#ifdef SAFERP #ifdef SAFERP
extern int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int saferp_test(void); int saferp_test(void);
extern int saferp_keysize(int *desired_keysize); int saferp_keysize(int *desired_keysize);
extern const struct _cipher_descriptor saferp_desc; extern const struct _cipher_descriptor saferp_desc;
#endif #endif
#ifdef SAFER #ifdef SAFER
extern int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int safer_k64_test(void); int safer_k64_test(void);
extern int safer_sk64_test(void); int safer_sk64_test(void);
extern int safer_sk128_test(void); int safer_sk128_test(void);
extern int safer_64_keysize(int *desired_keysize); int safer_64_keysize(int *desired_keysize);
extern int safer_128_keysize(int *desired_keysize); int safer_128_keysize(int *desired_keysize);
extern const struct _cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc; extern const struct _cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc;
#endif #endif
#ifdef RIJNDAEL #ifdef RIJNDAEL
@ -265,132 +265,132 @@ extern const struct _cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk
#define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt #define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt
#define aes_enc_keysize rijndael_enc_keysize #define aes_enc_keysize rijndael_enc_keysize
extern int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int rijndael_test(void); int rijndael_test(void);
extern int rijndael_keysize(int *desired_keysize); int rijndael_keysize(int *desired_keysize);
extern int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern int rijndael_enc_keysize(int *desired_keysize); int rijndael_enc_keysize(int *desired_keysize);
extern const struct _cipher_descriptor rijndael_desc, aes_desc; extern const struct _cipher_descriptor rijndael_desc, aes_desc;
extern const struct _cipher_descriptor rijndael_enc_desc, aes_enc_desc; extern const struct _cipher_descriptor rijndael_enc_desc, aes_enc_desc;
#endif #endif
#ifdef XTEA #ifdef XTEA
extern int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int xtea_test(void); int xtea_test(void);
extern int xtea_keysize(int *desired_keysize); int xtea_keysize(int *desired_keysize);
extern const struct _cipher_descriptor xtea_desc; extern const struct _cipher_descriptor xtea_desc;
#endif #endif
#ifdef TWOFISH #ifdef TWOFISH
extern int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int twofish_test(void); int twofish_test(void);
extern int twofish_keysize(int *desired_keysize); int twofish_keysize(int *desired_keysize);
extern const struct _cipher_descriptor twofish_desc; extern const struct _cipher_descriptor twofish_desc;
#endif #endif
#ifdef DES #ifdef DES
extern int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int des_test(void); int des_test(void);
extern int des_keysize(int *desired_keysize); int des_keysize(int *desired_keysize);
extern int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int des3_test(void); int des3_test(void);
extern int des3_keysize(int *desired_keysize); int des3_keysize(int *desired_keysize);
extern const struct _cipher_descriptor des_desc, des3_desc; extern const struct _cipher_descriptor des_desc, des3_desc;
#endif #endif
#ifdef CAST5 #ifdef CAST5
extern int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int cast5_test(void); int cast5_test(void);
extern int cast5_keysize(int *desired_keysize); int cast5_keysize(int *desired_keysize);
extern const struct _cipher_descriptor cast5_desc; extern const struct _cipher_descriptor cast5_desc;
#endif #endif
#ifdef NOEKEON #ifdef NOEKEON
extern int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int noekeon_test(void); int noekeon_test(void);
extern int noekeon_keysize(int *desired_keysize); int noekeon_keysize(int *desired_keysize);
extern const struct _cipher_descriptor noekeon_desc; extern const struct _cipher_descriptor noekeon_desc;
#endif #endif
#ifdef SKIPJACK #ifdef SKIPJACK
extern int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
extern void skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); void skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
extern void skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); void skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
extern int skipjack_test(void); int skipjack_test(void);
extern int skipjack_keysize(int *desired_keysize); int skipjack_keysize(int *desired_keysize);
extern const struct _cipher_descriptor skipjack_desc; extern const struct _cipher_descriptor skipjack_desc;
#endif #endif
#ifdef ECB #ifdef ECB
extern int ecb_start(int cipher, const unsigned char *key, int ecb_start(int cipher, const unsigned char *key,
int keylen, int num_rounds, symmetric_ECB *ecb); int keylen, int num_rounds, symmetric_ECB *ecb);
extern int ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_ECB *ecb); int ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_ECB *ecb);
extern int ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_ECB *ecb); int ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_ECB *ecb);
#endif #endif
#ifdef CFB #ifdef CFB
extern int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key, int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_CFB *cfb); int keylen, int num_rounds, symmetric_CFB *cfb);
extern int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb); int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb);
extern int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb); int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb);
extern int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb); int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb);
extern int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb); int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb);
#endif #endif
#ifdef OFB #ifdef OFB
extern int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_OFB *ofb); int keylen, int num_rounds, symmetric_OFB *ofb);
extern int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb); int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb);
extern int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb); int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb);
extern int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb); int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb);
extern int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb); int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb);
#endif #endif
#ifdef CBC #ifdef CBC
extern int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key, int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_CBC *cbc); int keylen, int num_rounds, symmetric_CBC *cbc);
extern int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc); int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc);
extern int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc); int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc);
extern int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc); int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc);
extern int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc); int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc);
#endif #endif
#ifdef CTR #ifdef CTR
extern int ctr_start(int cipher, const unsigned char *IV, const unsigned char *key, int ctr_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_CTR *ctr); int keylen, int num_rounds, symmetric_CTR *ctr);
extern int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr); int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr);
extern int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr); int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr);
extern int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr); int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr);
extern int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr); int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr);
#endif #endif
extern int find_cipher(const char *name); int find_cipher(const char *name);
extern int find_cipher_any(const char *name, int blocklen, int keylen); int find_cipher_any(const char *name, int blocklen, int keylen);
extern int find_cipher_id(unsigned char ID); int find_cipher_id(unsigned char ID);
extern int register_cipher(const struct _cipher_descriptor *cipher); int register_cipher(const struct _cipher_descriptor *cipher);
extern int unregister_cipher(const struct _cipher_descriptor *cipher); int unregister_cipher(const struct _cipher_descriptor *cipher);
extern int cipher_is_valid(int idx); int cipher_is_valid(int idx);

View File

@ -9,12 +9,16 @@
#error mycrypt_custom.h should be included before mycrypt.h #error mycrypt_custom.h should be included before mycrypt.h
#endif #endif
/* macros for various libc functions */ /* macros for various libc functions you can change for embedded targets */
#define XMALLOC malloc #define XMALLOC malloc
#define XREALLOC realloc #define XREALLOC realloc
#define XCALLOC calloc #define XCALLOC calloc
#define XFREE free #define XFREE free
#define XCLOCK clock
#define XMEMSET memset
#define XMEMCPY memcpy
#define XCLOCK clock
#define XCLOCKS_PER_SEC CLOCKS_PER_SEC #define XCLOCKS_PER_SEC CLOCKS_PER_SEC
/* Use small code where possible */ /* Use small code where possible */
@ -45,6 +49,10 @@
#define CAST5 #define CAST5
#define NOEKEON #define NOEKEON
#define SKIPJACK #define SKIPJACK
/* SAFER code isn't public domain. It appears to be free to use
* but has been disabled by default to avoid any such problems
*/
//#define SAFER
/* modes of operation */ /* modes of operation */
#define CFB #define CFB
@ -89,10 +97,19 @@
/* Public Key Neatoness */ /* Public Key Neatoness */
#define MRSA #define MRSA
#define RSA_TIMING // enable RSA side channel timing prevention /* enable RSA side channel timing prevention */
#define RSA_TIMING
/* Digital Signature Algorithm */
#define MDSA #define MDSA
/* Max diff between group and modulus size in bytes */
#define MDSA_DELTA 512
/* Max DSA group size in bytes (default allows 4k-bit groups) */
#define MDSA_MAX_GROUP 512
/* Diffie-Hellman */
#define MDH #define MDH
#define MECC /* Supported Key Sizes */
#define DH768 #define DH768
#define DH1024 #define DH1024
#define DH1280 #define DH1280
@ -102,15 +119,21 @@
#define DH2560 #define DH2560
#define DH3072 #define DH3072
#define DH4096 #define DH4096
/* ECC */
#define MECC
/* Supported Key Sizes */
#define ECC160 #define ECC160
#define ECC192 #define ECC192
#define ECC224 #define ECC224
#define ECC256 #define ECC256
#define ECC384 #define ECC384
#define ECC521 #define ECC521
/* Include the MPI functionality? (required by the PK algorithms) */
#define MPI #define MPI
/* PKCS #1 and 5 stuff */ /* PKCS #1 and #5 stuff */
#define PKCS_1 #define PKCS_1
#define PKCS_5 #define PKCS_5

View File

@ -111,7 +111,7 @@ typedef union Hash_state {
#endif #endif
} hash_state; } hash_state;
extern struct _hash_descriptor { extern struct _hash_descriptor {
char *name; char *name;
unsigned char ID; unsigned char ID;
unsigned long hashsize; /* digest output size in bytes */ unsigned long hashsize; /* digest output size in bytes */
@ -126,118 +126,118 @@ extern struct _hash_descriptor {
#ifdef WHIRLPOOL #ifdef WHIRLPOOL
extern void whirlpool_init(hash_state * md); void whirlpool_init(hash_state * md);
extern int whirlpool_process(hash_state * md, const unsigned char *buf, unsigned long len); int whirlpool_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int whirlpool_done(hash_state * md, unsigned char *hash); int whirlpool_done(hash_state * md, unsigned char *hash);
extern int whirlpool_test(void); int whirlpool_test(void);
extern const struct _hash_descriptor whirlpool_desc; extern const struct _hash_descriptor whirlpool_desc;
#endif #endif
#ifdef SHA512 #ifdef SHA512
extern void sha512_init(hash_state * md); void sha512_init(hash_state * md);
extern int sha512_process(hash_state * md, const unsigned char *buf, unsigned long len); int sha512_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int sha512_done(hash_state * md, unsigned char *hash); int sha512_done(hash_state * md, unsigned char *hash);
extern int sha512_test(void); int sha512_test(void);
extern const struct _hash_descriptor sha512_desc; extern const struct _hash_descriptor sha512_desc;
#endif #endif
#ifdef SHA384 #ifdef SHA384
#ifndef SHA512 #ifndef SHA512
#error SHA512 is required for SHA384 #error SHA512 is required for SHA384
#endif #endif
extern void sha384_init(hash_state * md); void sha384_init(hash_state * md);
#define sha384_process sha512_process #define sha384_process sha512_process
extern int sha384_done(hash_state * md, unsigned char *hash); int sha384_done(hash_state * md, unsigned char *hash);
extern int sha384_test(void); int sha384_test(void);
extern const struct _hash_descriptor sha384_desc; const struct _hash_descriptor sha384_desc;
#endif #endif
#ifdef SHA256 #ifdef SHA256
extern void sha256_init(hash_state * md); void sha256_init(hash_state * md);
extern int sha256_process(hash_state * md, const unsigned char *buf, unsigned long len); int sha256_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int sha256_done(hash_state * md, unsigned char *hash); int sha256_done(hash_state * md, unsigned char *hash);
extern int sha256_test(void); int sha256_test(void);
extern const struct _hash_descriptor sha256_desc; extern const struct _hash_descriptor sha256_desc;
#ifdef SHA224 #ifdef SHA224
#ifndef SHA256 #ifndef SHA256
#error SHA256 is required for SHA224 #error SHA256 is required for SHA224
#endif #endif
extern void sha224_init(hash_state * md); void sha224_init(hash_state * md);
#define sha224_process sha256_process #define sha224_process sha256_process
extern int sha224_done(hash_state * md, unsigned char *hash); int sha224_done(hash_state * md, unsigned char *hash);
extern int sha224_test(void); int sha224_test(void);
extern const struct _hash_descriptor sha224_desc; extern const struct _hash_descriptor sha224_desc;
#endif #endif
#endif #endif
#ifdef SHA1 #ifdef SHA1
extern void sha1_init(hash_state * md); void sha1_init(hash_state * md);
extern int sha1_process(hash_state * md, const unsigned char *buf, unsigned long len); int sha1_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int sha1_done(hash_state * md, unsigned char *hash); int sha1_done(hash_state * md, unsigned char *hash);
extern int sha1_test(void); int sha1_test(void);
extern const struct _hash_descriptor sha1_desc; extern const struct _hash_descriptor sha1_desc;
#endif #endif
#ifdef MD5 #ifdef MD5
extern void md5_init(hash_state * md); void md5_init(hash_state * md);
extern int md5_process(hash_state * md, const unsigned char *buf, unsigned long len); int md5_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int md5_done(hash_state * md, unsigned char *hash); int md5_done(hash_state * md, unsigned char *hash);
extern int md5_test(void); int md5_test(void);
extern const struct _hash_descriptor md5_desc; extern const struct _hash_descriptor md5_desc;
#endif #endif
#ifdef MD4 #ifdef MD4
extern void md4_init(hash_state * md); void md4_init(hash_state * md);
extern int md4_process(hash_state * md, const unsigned char *buf, unsigned long len); int md4_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int md4_done(hash_state * md, unsigned char *hash); int md4_done(hash_state * md, unsigned char *hash);
extern int md4_test(void); int md4_test(void);
extern const struct _hash_descriptor md4_desc; extern const struct _hash_descriptor md4_desc;
#endif #endif
#ifdef MD2 #ifdef MD2
extern void md2_init(hash_state * md); void md2_init(hash_state * md);
extern int md2_process(hash_state * md, const unsigned char *buf, unsigned long len); int md2_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int md2_done(hash_state * md, unsigned char *hash); int md2_done(hash_state * md, unsigned char *hash);
extern int md2_test(void); int md2_test(void);
extern const struct _hash_descriptor md2_desc; extern const struct _hash_descriptor md2_desc;
#endif #endif
#ifdef TIGER #ifdef TIGER
extern void tiger_init(hash_state * md); void tiger_init(hash_state * md);
extern int tiger_process(hash_state * md, const unsigned char *buf, unsigned long len); int tiger_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int tiger_done(hash_state * md, unsigned char *hash); int tiger_done(hash_state * md, unsigned char *hash);
extern int tiger_test(void); int tiger_test(void);
extern const struct _hash_descriptor tiger_desc; extern const struct _hash_descriptor tiger_desc;
#endif #endif
#ifdef RIPEMD128 #ifdef RIPEMD128
extern void rmd128_init(hash_state * md); void rmd128_init(hash_state * md);
extern int rmd128_process(hash_state * md, const unsigned char *buf, unsigned long len); int rmd128_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int rmd128_done(hash_state * md, unsigned char *hash); int rmd128_done(hash_state * md, unsigned char *hash);
extern int rmd128_test(void); int rmd128_test(void);
extern const struct _hash_descriptor rmd128_desc; extern const struct _hash_descriptor rmd128_desc;
#endif #endif
#ifdef RIPEMD160 #ifdef RIPEMD160
extern void rmd160_init(hash_state * md); void rmd160_init(hash_state * md);
extern int rmd160_process(hash_state * md, const unsigned char *buf, unsigned long len); int rmd160_process(hash_state * md, const unsigned char *buf, unsigned long len);
extern int rmd160_done(hash_state * md, unsigned char *hash); int rmd160_done(hash_state * md, unsigned char *hash);
extern int rmd160_test(void); int rmd160_test(void);
extern const struct _hash_descriptor rmd160_desc; extern const struct _hash_descriptor rmd160_desc;
#endif #endif
extern int find_hash(const char *name); int find_hash(const char *name);
extern int find_hash_id(unsigned char ID); int find_hash_id(unsigned char ID);
extern int find_hash_any(const char *name, int digestlen); int find_hash_any(const char *name, int digestlen);
extern int register_hash(const struct _hash_descriptor *hash); int register_hash(const struct _hash_descriptor *hash);
extern int unregister_hash(const struct _hash_descriptor *hash); int unregister_hash(const struct _hash_descriptor *hash);
extern int hash_is_valid(int idx); int hash_is_valid(int idx);
extern int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen); int hash_memory(int hash, const unsigned char *data, unsigned long len, unsigned char *dst, unsigned long *outlen);
extern int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen); int hash_filehandle(int hash, FILE *in, unsigned char *dst, unsigned long *outlen);
extern int hash_file(int hash, const char *fname, unsigned char *dst, unsigned long *outlen); int hash_file(int hash, const char *fname, unsigned char *dst, unsigned long *outlen);
/* a simple macro for making hash "process" functions */ /* a simple macro for making hash "process" functions */
#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \ #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
@ -279,14 +279,14 @@ typedef struct Hmac_state {
unsigned char key[MAXBLOCKSIZE]; unsigned char key[MAXBLOCKSIZE];
} hmac_state; } hmac_state;
extern int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen); int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen);
extern int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len); int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len);
extern int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen); int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen);
extern int hmac_test(void); int hmac_test(void);
extern int hmac_memory(int hash, const unsigned char *key, unsigned long keylen, int hmac_memory(int hash, const unsigned char *key, unsigned long keylen,
const unsigned char *data, unsigned long len, const unsigned char *data, unsigned long len,
unsigned char *dst, unsigned long *dstlen); unsigned char *dst, unsigned long *dstlen);
extern int hmac_file(int hash, const char *fname, const unsigned char *key, int hmac_file(int hash, const char *fname, const unsigned char *key,
unsigned long keylen, unsigned long keylen,
unsigned char *dst, unsigned long *dstlen); unsigned char *dst, unsigned long *dstlen);
#endif #endif
@ -303,15 +303,15 @@ typedef struct {
symmetric_key key; symmetric_key key;
} omac_state; } omac_state;
extern int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen); int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
extern int omac_process(omac_state *state, const unsigned char *buf, unsigned long len); int omac_process(omac_state *state, const unsigned char *buf, unsigned long len);
extern int omac_done(omac_state *state, unsigned char *out, unsigned long *outlen); int omac_done(omac_state *state, unsigned char *out, unsigned long *outlen);
extern int omac_memory(int cipher, const unsigned char *key, unsigned long keylen, int omac_memory(int cipher, const unsigned char *key, unsigned long keylen,
const unsigned char *msg, unsigned long msglen, const unsigned char *msg, unsigned long msglen,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
extern int omac_file(int cipher, const unsigned char *key, unsigned long keylen, int omac_file(int cipher, const unsigned char *key, unsigned long keylen,
const char *filename, unsigned char *out, unsigned long *outlen); const char *filename, unsigned char *out, unsigned long *outlen);
extern int omac_test(void); int omac_test(void);
#endif /* OMAC */ #endif /* OMAC */
#ifdef PMAC #ifdef PMAC
@ -330,22 +330,22 @@ typedef struct {
buflen; /* number of bytes in the buffer */ buflen; /* number of bytes in the buffer */
} pmac_state; } pmac_state;
extern int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen); int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen);
extern int pmac_process(pmac_state *state, const unsigned char *buf, unsigned long len); int pmac_process(pmac_state *state, const unsigned char *buf, unsigned long len);
extern int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen); int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen);
extern int pmac_memory(int cipher, const unsigned char *key, unsigned long keylen, int pmac_memory(int cipher, const unsigned char *key, unsigned long keylen,
const unsigned char *msg, unsigned long msglen, const unsigned char *msg, unsigned long msglen,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
extern int pmac_file(int cipher, const unsigned char *key, unsigned long keylen, int pmac_file(int cipher, const unsigned char *key, unsigned long keylen,
const char *filename, unsigned char *out, unsigned long *outlen); const char *filename, unsigned char *out, unsigned long *outlen);
extern int pmac_test(void); int pmac_test(void);
/* internal functions */ /* internal functions */
extern int pmac_ntz(unsigned long x); int pmac_ntz(unsigned long x);
extern void pmac_shift_xor(pmac_state *pmac); void pmac_shift_xor(pmac_state *pmac);
#endif /* PMAC */ #endif /* PMAC */
@ -361,16 +361,16 @@ typedef struct {
omac_state headeromac, ctomac; omac_state headeromac, ctomac;
} eax_state; } eax_state;
extern int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen, int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen, const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen); const unsigned char *header, unsigned long headerlen);
extern int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length); int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length);
extern int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length); int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length);
extern int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length); int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length);
extern int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen); int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen);
extern int eax_encrypt_authenticate_memory(int cipher, int eax_encrypt_authenticate_memory(int cipher,
const unsigned char *key, unsigned long keylen, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen, const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen, const unsigned char *header, unsigned long headerlen,
@ -378,7 +378,7 @@ extern int eax_encrypt_authenticate_memory(int cipher,
unsigned char *ct, unsigned char *ct,
unsigned char *tag, unsigned long *taglen); unsigned char *tag, unsigned long *taglen);
extern int eax_decrypt_verify_memory(int cipher, int eax_decrypt_verify_memory(int cipher,
const unsigned char *key, unsigned long keylen, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, unsigned long noncelen, const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen, const unsigned char *header, unsigned long headerlen,
@ -387,7 +387,7 @@ extern int eax_decrypt_verify_memory(int cipher,
unsigned char *tag, unsigned long taglen, unsigned char *tag, unsigned long taglen,
int *res); int *res);
extern int eax_test(void); int eax_test(void);
#endif /* EAX MODE */ #endif /* EAX MODE */
#ifdef OCB_MODE #ifdef OCB_MODE
@ -405,30 +405,30 @@ typedef struct {
block_len; /* length of block */ block_len; /* length of block */
} ocb_state; } ocb_state;
extern int ocb_init(ocb_state *ocb, int cipher, int ocb_init(ocb_state *ocb, int cipher,
const unsigned char *key, unsigned long keylen, const unsigned char *nonce); const unsigned char *key, unsigned long keylen, const unsigned char *nonce);
extern int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct); int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct);
extern int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt); int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt);
extern int ocb_done_encrypt(ocb_state *ocb, int ocb_done_encrypt(ocb_state *ocb,
const unsigned char *pt, unsigned long ptlen, const unsigned char *pt, unsigned long ptlen,
unsigned char *ct, unsigned char *ct,
unsigned char *tag, unsigned long *taglen); unsigned char *tag, unsigned long *taglen);
extern int ocb_done_decrypt(ocb_state *ocb, int ocb_done_decrypt(ocb_state *ocb,
const unsigned char *ct, unsigned long ctlen, const unsigned char *ct, unsigned long ctlen,
unsigned char *pt, unsigned char *pt,
const unsigned char *tag, unsigned long taglen, int *res); const unsigned char *tag, unsigned long taglen, int *res);
extern int ocb_encrypt_authenticate_memory(int cipher, int ocb_encrypt_authenticate_memory(int cipher,
const unsigned char *key, unsigned long keylen, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, const unsigned char *nonce,
const unsigned char *pt, unsigned long ptlen, const unsigned char *pt, unsigned long ptlen,
unsigned char *ct, unsigned char *ct,
unsigned char *tag, unsigned long *taglen); unsigned char *tag, unsigned long *taglen);
extern int ocb_decrypt_verify_memory(int cipher, int ocb_decrypt_verify_memory(int cipher,
const unsigned char *key, unsigned long keylen, const unsigned char *key, unsigned long keylen,
const unsigned char *nonce, const unsigned char *nonce,
const unsigned char *ct, unsigned long ctlen, const unsigned char *ct, unsigned long ctlen,
@ -436,12 +436,12 @@ extern int ocb_decrypt_verify_memory(int cipher,
const unsigned char *tag, unsigned long taglen, const unsigned char *tag, unsigned long taglen,
int *res); int *res);
extern int ocb_test(void); int ocb_test(void);
/* internal functions */ /* internal functions */
extern void ocb_shift_xor(ocb_state *ocb, unsigned char *Z); void ocb_shift_xor(ocb_state *ocb, unsigned char *Z);
extern int ocb_ntz(unsigned long x); int ocb_ntz(unsigned long x);
extern int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode); unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode);
#endif /* OCB_MODE */ #endif /* OCB_MODE */

View File

@ -1,81 +0,0 @@
#ifdef KR
#if !defined(MRSA) || !defined(MDH) || !defined(MECC)
#error "Keyring code requires all three public key algorithms."
#endif
#define MAXLEN 256
enum {
NON_KEY=0,
RSA_KEY,
DH_KEY,
ECC_KEY
};
typedef union {
rsa_key rsa;
dh_key dh;
ecc_key ecc;
} _pk_key;
typedef struct Pk_key {
int key_type, /* PUBLIC, PRIVATE, PRIVATE_OPTIMIZED */
system; /* RSA, ECC or DH ? */
unsigned char
name[MAXLEN], /* various info's about this key */
email[MAXLEN],
description[MAXLEN];
unsigned long ID; /* CRC32 of the name/email/description together */
_pk_key key;
struct Pk_key *next; /* linked list chain */
} pk_key;
extern int kr_init(pk_key **pk);
extern unsigned long kr_crc(const unsigned char *name, const unsigned char *email, const unsigned char *description);
extern pk_key *kr_find(pk_key *pk, unsigned long ID);
extern pk_key *kr_find_name(pk_key *pk, const char *name);
extern int kr_add(pk_key *pk, int key_type, int sys, const unsigned char *name,
const unsigned char *email, const unsigned char *description, const _pk_key *key);
extern int kr_del(pk_key **_pk, unsigned long ID);
extern int kr_clear(pk_key **pk);
extern int kr_make_key(pk_key *pk, prng_state *prng, int wprng,
int sys, int keysize, const unsigned char *name,
const unsigned char *email, const unsigned char *description);
extern int kr_export(pk_key *pk, unsigned long ID, int key_type, unsigned char *out, unsigned long *outlen);
extern int kr_import(pk_key *pk, const unsigned char *in, unsigned long inlen);
extern int kr_load(pk_key **pk, FILE *in, symmetric_CTR *ctr);
extern int kr_save(pk_key *pk, FILE *out, symmetric_CTR *ctr);
extern int kr_encrypt_key(pk_key *pk, unsigned long ID,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, int hash);
extern int kr_decrypt_key(pk_key *pk, const unsigned char *in,
unsigned char *out, unsigned long *outlen);
extern int kr_sign_hash(pk_key *pk, unsigned long ID,
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng);
extern int kr_verify_hash(pk_key *pk, const unsigned char *in,
const unsigned char *hash, unsigned long hashlen,
int *stat);
extern int kr_fingerprint(pk_key *pk, unsigned long ID, int hash,
unsigned char *out, unsigned long *outlen);
#endif

View File

@ -1,17 +1,17 @@
/* ---- BASE64 Routines ---- */ /* ---- BASE64 Routines ---- */
#ifdef BASE64 #ifdef BASE64
extern int base64_encode(const unsigned char *in, unsigned long len, int base64_encode(const unsigned char *in, unsigned long len,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
extern int base64_decode(const unsigned char *in, unsigned long len, int base64_decode(const unsigned char *in, unsigned long len,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
#endif #endif
/* ---- MEM routines ---- */ /* ---- MEM routines ---- */
extern void zeromem(void *dst, size_t len); void zeromem(void *dst, size_t len);
extern void burn_stack(unsigned long len); void burn_stack(unsigned long len);
extern const char *error_to_string(int err); const char *error_to_string(int err);
extern int mpi_to_ltc_error(int err); int mpi_to_ltc_error(int err);
extern const char *crypt_build_settings; extern const char *crypt_build_settings;

View File

@ -44,8 +44,8 @@
} \ } \
} }
extern int is_prime(mp_int *, int *); int is_prime(mp_int *, int *);
extern int rand_prime(mp_int *N, long len, prng_state *prng, int wprng); int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
#else #else
#ifdef MRSA #ifdef MRSA
@ -72,8 +72,8 @@ extern int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
/* ---- PACKET ---- */ /* ---- PACKET ---- */
#ifdef PACKET #ifdef PACKET
extern void packet_store_header(unsigned char *dst, int section, int subsection); void packet_store_header(unsigned char *dst, int section, int subsection);
extern int packet_valid_header(unsigned char *src, int section, int subsection); int packet_valid_header(unsigned char *src, int section, int subsection);
#endif #endif
@ -93,16 +93,16 @@ typedef struct Rsa_key {
mp_int e, d, N, qP, pQ, dP, dQ, p, q; mp_int e, d, N, qP, pQ, dP, dQ, p, q;
} rsa_key; } rsa_key;
extern int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
extern int rsa_exptmod(const unsigned char *in, unsigned long inlen, int rsa_exptmod(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, int which, unsigned char *out, unsigned long *outlen, int which,
prng_state *prng, int prng_idx, prng_state *prng, int prng_idx,
rsa_key *key); rsa_key *key);
#ifdef RSA_TIMING #ifdef RSA_TIMING
extern int tim_exptmod(prng_state *prng, int prng_idx, int tim_exptmod(prng_state *prng, int prng_idx,
mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m); mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
#else #else
@ -111,7 +111,7 @@ extern int tim_exptmod(prng_state *prng, int prng_idx,
#endif #endif
extern void rsa_free(rsa_key *key); void rsa_free(rsa_key *key);
int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen, int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
unsigned char *outkey, unsigned long *outlen, unsigned char *outkey, unsigned long *outlen,
@ -150,33 +150,33 @@ typedef struct Dh_key {
mp_int x, y; mp_int x, y;
} dh_key; } dh_key;
extern int dh_test(void); int dh_test(void);
extern void dh_sizes(int *low, int *high); void dh_sizes(int *low, int *high);
extern int dh_get_size(dh_key *key); int dh_get_size(dh_key *key);
extern int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
extern void dh_free(dh_key *key); void dh_free(dh_key *key);
extern int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
extern int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
extern int dh_shared_secret(dh_key *private_key, dh_key *public_key, int dh_shared_secret(dh_key *private_key, dh_key *public_key,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
extern int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen, int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
unsigned char *out, unsigned long *len, unsigned char *out, unsigned long *len,
prng_state *prng, int wprng, int hash, prng_state *prng, int wprng, int hash,
dh_key *key); dh_key *key);
extern int dh_decrypt_key(const unsigned char *in, unsigned long inlen, int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen, unsigned char *outkey, unsigned long *keylen,
dh_key *key); dh_key *key);
extern int dh_sign_hash(const unsigned char *in, unsigned long inlen, int dh_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, dh_key *key); prng_state *prng, int wprng, dh_key *key);
extern int dh_verify_hash(const unsigned char *sig, unsigned long siglen, int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long hashlen, const unsigned char *hash, unsigned long hashlen,
int *stat, dh_key *key); int *stat, dh_key *key);
@ -195,33 +195,33 @@ typedef struct {
mp_int k; mp_int k;
} ecc_key; } ecc_key;
extern int ecc_test(void); int ecc_test(void);
extern void ecc_sizes(int *low, int *high); void ecc_sizes(int *low, int *high);
extern int ecc_get_size(ecc_key *key); int ecc_get_size(ecc_key *key);
extern int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
extern void ecc_free(ecc_key *key); void ecc_free(ecc_key *key);
extern int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
extern int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
extern int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
unsigned char *out, unsigned long *outlen); unsigned char *out, unsigned long *outlen);
extern int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen, int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
unsigned char *out, unsigned long *len, unsigned char *out, unsigned long *len,
prng_state *prng, int wprng, int hash, prng_state *prng, int wprng, int hash,
ecc_key *key); ecc_key *key);
extern int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
unsigned char *outkey, unsigned long *keylen, unsigned char *outkey, unsigned long *keylen,
ecc_key *key); ecc_key *key);
extern int ecc_sign_hash(const unsigned char *in, unsigned long inlen, int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, ecc_key *key); prng_state *prng, int wprng, ecc_key *key);
extern int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long hashlen, const unsigned char *hash, unsigned long hashlen,
int *stat, ecc_key *key); int *stat, ecc_key *key);
#endif #endif
@ -233,21 +233,21 @@ typedef struct {
mp_int g, q, p, x, y; mp_int g, q, p, x, y;
} dsa_key; } dsa_key;
extern int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
extern void dsa_free(dsa_key *key); void dsa_free(dsa_key *key);
extern int dsa_sign_hash(const unsigned char *in, unsigned long inlen, int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
prng_state *prng, int wprng, dsa_key *key); prng_state *prng, int wprng, dsa_key *key);
extern int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
const unsigned char *hash, unsigned long inlen, const unsigned char *hash, unsigned long inlen,
int *stat, dsa_key *key); int *stat, dsa_key *key);
extern int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
extern int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
extern int dsa_verify_key(dsa_key *key, int *stat); int dsa_verify_key(dsa_key *key, int *stat);
#endif #endif

View File

@ -24,43 +24,43 @@ extern struct _prng_descriptor {
} prng_descriptor[]; } prng_descriptor[];
#ifdef YARROW #ifdef YARROW
extern int yarrow_start(prng_state *prng); int yarrow_start(prng_state *prng);
extern int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); int yarrow_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
extern int yarrow_ready(prng_state *prng); int yarrow_ready(prng_state *prng);
extern unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng); unsigned long yarrow_read(unsigned char *buf, unsigned long len, prng_state *prng);
extern const struct _prng_descriptor yarrow_desc; extern const struct _prng_descriptor yarrow_desc;
#endif #endif
#ifdef RC4 #ifdef RC4
extern int rc4_start(prng_state *prng); int rc4_start(prng_state *prng);
extern int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); int rc4_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
extern int rc4_ready(prng_state *prng); int rc4_ready(prng_state *prng);
extern unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng); unsigned long rc4_read(unsigned char *buf, unsigned long len, prng_state *prng);
extern const struct _prng_descriptor rc4_desc; extern const struct _prng_descriptor rc4_desc;
#endif #endif
#ifdef SPRNG #ifdef SPRNG
extern int sprng_start(prng_state *prng); int sprng_start(prng_state *prng);
extern int sprng_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng); int sprng_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng);
extern int sprng_ready(prng_state *prng); int sprng_ready(prng_state *prng);
extern unsigned long sprng_read(unsigned char *buf, unsigned long len, prng_state *prng); unsigned long sprng_read(unsigned char *buf, unsigned long len, prng_state *prng);
extern const struct _prng_descriptor sprng_desc; extern const struct _prng_descriptor sprng_desc;
#endif #endif
extern int find_prng(const char *name); int find_prng(const char *name);
extern int register_prng(const struct _prng_descriptor *prng); int register_prng(const struct _prng_descriptor *prng);
extern int unregister_prng(const struct _prng_descriptor *prng); int unregister_prng(const struct _prng_descriptor *prng);
extern int prng_is_valid(int idx); int prng_is_valid(int idx);
/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this /* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
* might not work on all platforms as planned * might not work on all platforms as planned
*/ */
/* ch2-02-1 */ /* ch2-02-1 */
extern unsigned long rng_get_bytes(unsigned char *buf, unsigned long rng_get_bytes(unsigned char *buf,
unsigned long len, unsigned long len,
void (*callback)(void)); void (*callback)(void));
/* ch2-02-1 */ /* ch2-02-1 */
extern int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)); int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));

View File

@ -23,8 +23,7 @@ int ocb_decrypt_verify_memory(int cipher,
int *res) int *res)
{ {
int err; int err;
ocb_state ocb; ocb_state *ocb;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
_ARGCHK(nonce != NULL); _ARGCHK(nonce != NULL);
@ -33,20 +32,34 @@ int ocb_decrypt_verify_memory(int cipher,
_ARGCHK(tag != NULL); _ARGCHK(tag != NULL);
_ARGCHK(res != NULL); _ARGCHK(res != NULL);
if ((err = ocb_init(&ocb, cipher, key, keylen, nonce)) != CRYPT_OK) { /* allocate memory */
return err; ocb = XMALLOC(sizeof(ocb_state));
if (ocb == NULL) {
return CRYPT_MEM;
} }
while (ctlen > (unsigned long)ocb.block_len) { if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
if ((err = ocb_decrypt(&ocb, ct, pt)) != CRYPT_OK) { goto __ERR;
return err; }
while (ctlen > (unsigned long)ocb->block_len) {
if ((err = ocb_decrypt(ocb, ct, pt)) != CRYPT_OK) {
goto __ERR;
} }
ctlen -= ocb.block_len; ctlen -= ocb->block_len;
pt += ocb.block_len; pt += ocb->block_len;
ct += ocb.block_len; ct += ocb->block_len;
} }
return ocb_done_decrypt(&ocb, ct, ctlen, pt, tag, taglen, res); err = ocb_done_decrypt(ocb, ct, ctlen, pt, tag, taglen, res);
__ERR:
#ifdef CLEAN_STACK
zeromem(ocb, sizeof(ocb_state));
#endif
XFREE(ocb);
return err;
} }
#endif #endif

View File

@ -20,7 +20,7 @@ int ocb_done_decrypt(ocb_state *ocb,
const unsigned char *tag, unsigned long taglen, int *res) const unsigned char *tag, unsigned long taglen, int *res)
{ {
int err; int err;
unsigned char tagbuf[MAXBLOCKSIZE]; unsigned char *tagbuf;
unsigned long tagbuflen; unsigned long tagbuflen;
_ARGCHK(ocb != NULL); _ARGCHK(ocb != NULL);
@ -29,22 +29,33 @@ int ocb_done_decrypt(ocb_state *ocb,
_ARGCHK(tag != NULL); _ARGCHK(tag != NULL);
_ARGCHK(res != NULL); _ARGCHK(res != NULL);
/* default to failed */
*res = 0; *res = 0;
tagbuflen = sizeof(tagbuf); /* allocate memory */
tagbuf = XMALLOC(MAXBLOCKSIZE);
if (tagbuf == NULL) {
return CRYPT_MEM;
}
tagbuflen = MAXBLOCKSIZE;
if ((err = __ocb_done(ocb, ct, ctlen, pt, tagbuf, &tagbuflen, 1)) != CRYPT_OK) { if ((err = __ocb_done(ocb, ct, ctlen, pt, tagbuf, &tagbuflen, 1)) != CRYPT_OK) {
return err; goto __ERR;
} }
if (taglen <= tagbuflen && memcmp(tagbuf, tag, taglen) == 0) { if (taglen <= tagbuflen && memcmp(tagbuf, tag, taglen) == 0) {
*res = 1; *res = 1;
} }
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(tagbuf, sizeof(tagbuf)); zeromem(tagbuf, MAXBLOCKSIZE);
#endif #endif
return CRYPT_OK; XFREE(tagbuf);
return err;
} }
#endif #endif

View File

@ -22,7 +22,7 @@ int ocb_encrypt_authenticate_memory(int cipher,
unsigned char *tag, unsigned long *taglen) unsigned char *tag, unsigned long *taglen)
{ {
int err; int err;
ocb_state ocb; ocb_state *ocb;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
_ARGCHK(nonce != NULL); _ARGCHK(nonce != NULL);
@ -31,20 +31,34 @@ int ocb_encrypt_authenticate_memory(int cipher,
_ARGCHK(tag != NULL); _ARGCHK(tag != NULL);
_ARGCHK(taglen != NULL); _ARGCHK(taglen != NULL);
if ((err = ocb_init(&ocb, cipher, key, keylen, nonce)) != CRYPT_OK) { /* allocate ram */
return err; ocb = XMALLOC(sizeof(ocb_state));
if (ocb == NULL) {
return CRYPT_MEM;
} }
while (ptlen > (unsigned long)ocb.block_len) { if ((err = ocb_init(ocb, cipher, key, keylen, nonce)) != CRYPT_OK) {
if ((err = ocb_encrypt(&ocb, pt, ct)) != CRYPT_OK) { goto __ERR;
return err; }
while (ptlen > (unsigned long)ocb->block_len) {
if ((err = ocb_encrypt(ocb, pt, ct)) != CRYPT_OK) {
goto __ERR;
} }
ptlen -= ocb.block_len; ptlen -= ocb->block_len;
pt += ocb.block_len; pt += ocb->block_len;
ct += ocb.block_len; ct += ocb->block_len;
} }
return ocb_done_encrypt(&ocb, pt, ptlen, ct, tag, taglen); err = ocb_done_encrypt(ocb, pt, ptlen, ct, tag, taglen);
__ERR:
#ifdef CLEAN_STACK
zeromem(ocb, sizeof(ocb_state));
#endif
XFREE(ocb);
return err;
} }
#endif #endif

View File

@ -73,7 +73,7 @@ int ocb_init(ocb_state *ocb, int cipher,
cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key); cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key);
/* find Ls[i] = L << i for i == 0..31 */ /* find Ls[i] = L << i for i == 0..31 */
memcpy(ocb->Ls[0], ocb->L, ocb->block_len); XMEMCPY(ocb->Ls[0], ocb->L, ocb->block_len);
for (x = 1; x < 32; x++) { for (x = 1; x < 32; x++) {
m = ocb->Ls[x-1][0] >> 7; m = ocb->Ls[x-1][0] >> 7;
for (y = 0; y < ocb->block_len-1; y++) { for (y = 0; y < ocb->block_len-1; y++) {
@ -104,7 +104,7 @@ int ocb_init(ocb_state *ocb, int cipher,
} }
/* set Li, checksum */ /* set Li, checksum */
zeromem(ocb->Li, ocb->block_len); zeromem(ocb->Li, ocb->block_len);
zeromem(ocb->checksum, ocb->block_len); zeromem(ocb->checksum, ocb->block_len);
/* set other params */ /* set other params */

View File

@ -21,7 +21,7 @@ int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb)
if ((unsigned long)ofb->blocklen > *len) { if ((unsigned long)ofb->blocklen > *len) {
return CRYPT_BUFFER_OVERFLOW; return CRYPT_BUFFER_OVERFLOW;
} }
memcpy(IV, ofb->IV, ofb->blocklen); XMEMCPY(IV, ofb->IV, ofb->blocklen);
*len = ofb->blocklen; *len = ofb->blocklen;
return CRYPT_OK; return CRYPT_OK;

View File

@ -59,7 +59,7 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
/* copy up as require */ /* copy up as require */
if (x == 0) { if (x == 0) {
memcpy(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0])); XMEMCPY(omac->Lu[1], omac->Lu[0], sizeof(omac->Lu[0]));
} }
} }

View File

@ -19,24 +19,38 @@ int omac_memory(int cipher,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
int err; int err;
omac_state omac; omac_state *omac;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
_ARGCHK(msg != NULL); _ARGCHK(msg != NULL);
_ARGCHK(out != NULL); _ARGCHK(out != NULL);
_ARGCHK(outlen != NULL); _ARGCHK(outlen != NULL);
if ((err = omac_init(&omac, cipher, key, keylen)) != CRYPT_OK) { /* allocate ram for omac state */
return err; omac = XMALLOC(sizeof(omac_state));
} if (omac == NULL) {
if ((err = omac_process(&omac, msg, msglen)) != CRYPT_OK) { return CRYPT_MEM;
return err;
}
if ((err = omac_done(&omac, out, outlen)) != CRYPT_OK) {
return err;
} }
return CRYPT_OK; /* omac process the message */
if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
goto __ERR;
}
if ((err = omac_process(omac, msg, msglen)) != CRYPT_OK) {
goto __ERR;
}
if ((err = omac_done(omac, out, outlen)) != CRYPT_OK) {
goto __ERR;
}
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(omac, sizeof(omac_state));
#endif
XFREE(omac);
return err;
} }
#endif #endif

View File

@ -40,7 +40,7 @@ int omac_process(omac_state *state, const unsigned char *buf, unsigned long len)
/* add bytes */ /* add bytes */
n = MIN(len, (unsigned long)(state->blklen - state->buflen)); n = MIN(len, (unsigned long)(state->blklen - state->buflen));
memcpy(state->block + state->buflen, buf, n); XMEMCPY(state->block + state->buflen, buf, n);
state->buflen += n; state->buflen += n;
len -= n; len -= n;
buf += n; buf += n;

View File

@ -20,8 +20,8 @@ int pkcs_1_mgf1(const unsigned char *seed, unsigned long seedlen,
{ {
unsigned long hLen, counter, x; unsigned long hLen, counter, x;
int err; int err;
hash_state md; hash_state *md;
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf;
_ARGCHK(seed != NULL); _ARGCHK(seed != NULL);
_ARGCHK(mask != NULL); _ARGCHK(mask != NULL);
@ -34,6 +34,19 @@ int pkcs_1_mgf1(const unsigned char *seed, unsigned long seedlen,
/* get hash output size */ /* get hash output size */
hLen = hash_descriptor[hash_idx].hashsize; hLen = hash_descriptor[hash_idx].hashsize;
/* allocate memory */
md = XMALLOC(sizeof(hash_state));
buf = XMALLOC(hLen);
if (md == NULL || buf == NULL) {
if (md != NULL) {
XFREE(md);
}
if (buf != NULL) {
XFREE(buf);
}
return CRYPT_MEM;
}
/* start counter */ /* start counter */
counter = 0; counter = 0;
@ -43,15 +56,15 @@ int pkcs_1_mgf1(const unsigned char *seed, unsigned long seedlen,
++counter; ++counter;
/* get hash of seed || counter */ /* get hash of seed || counter */
hash_descriptor[hash_idx].init(&md); hash_descriptor[hash_idx].init(md);
if ((err = hash_descriptor[hash_idx].process(&md, seed, seedlen)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].process(&md, buf, 4)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].done(&md, buf)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* store it */ /* store it */
@ -60,7 +73,17 @@ int pkcs_1_mgf1(const unsigned char *seed, unsigned long seedlen,
} }
} }
return CRYPT_OK; err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(buf, hLen);
zeromem(md, sizeof(hash_state));
#endif
XFREE(buf);
XFREE(md);
return err;
} }
#endif /* PKCS_1 */ #endif /* PKCS_1 */

View File

@ -20,7 +20,7 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
unsigned char *out, unsigned long *outlen, unsigned char *out, unsigned long *outlen,
int *res) int *res)
{ {
unsigned char DB[1024], seed[MAXBLOCKSIZE], mask[sizeof(DB)]; unsigned char *DB, *seed, *mask;
unsigned long hLen, x, y, modulus_len; unsigned long hLen, x, y, modulus_len;
int err; int err;
@ -39,9 +39,28 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
hLen = hash_descriptor[hash_idx].hashsize; hLen = hash_descriptor[hash_idx].hashsize;
modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
/* allocate ram for DB/mask/salt of size modulus_len */
DB = XMALLOC(modulus_len);
mask = XMALLOC(modulus_len);
seed = XMALLOC(modulus_len);
if (DB == NULL || mask == NULL || seed == NULL) {
if (DB != NULL) {
XFREE(DB);
}
if (mask != NULL) {
XFREE(mask);
}
if (seed != NULL) {
XFREE(seed);
}
return CRYPT_MEM;
}
/* test message size */ /* test message size */
if (modulus_len >= sizeof(DB) || msglen != modulus_len) { if (msglen != modulus_len) {
return CRYPT_PK_INVALID_SIZE; err = CRYPT_PK_INVALID_SIZE;
goto __ERR;
} }
/* ok so it's now in the form /* ok so it's now in the form
@ -54,7 +73,8 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
/* must have leading 0x00 byte */ /* must have leading 0x00 byte */
if (msg[0] != 0x00) { if (msg[0] != 0x00) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* now read the masked seed */ /* now read the masked seed */
@ -69,7 +89,7 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
/* compute MGF1 of maskedDB (hLen) */ /* compute MGF1 of maskedDB (hLen) */
if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* XOR against seed */ /* XOR against seed */
@ -79,7 +99,7 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
/* compute MGF1 of seed (k - hlen - 1) */ /* compute MGF1 of seed (k - hlen - 1) */
if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* xor against DB */ /* xor against DB */
@ -90,21 +110,22 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
/* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */ /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
/* compute lhash and store it in seed [reuse temps!] */ /* compute lhash and store it in seed [reuse temps!] */
x = sizeof(seed); x = modulus_len;
if (lparam != NULL) { if (lparam != NULL) {
if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) { if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
} else { } else {
/* can't pass hash_memory a NULL so use DB with zero length */ /* can't pass hash_memory a NULL so use DB with zero length */
if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) { if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
} }
/* compare the lhash'es */ /* compare the lhash'es */
if (memcmp(seed, DB, hLen) != 0) { if (memcmp(seed, DB, hLen) != 0) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* now zeroes before a 0x01 */ /* now zeroes before a 0x01 */
@ -114,12 +135,14 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
/* error out if wasn't 0x01 */ /* error out if wasn't 0x01 */
if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) { if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* rest is the message (and skip 0x01) */ /* rest is the message (and skip 0x01) */
if (msglen - ++x > *outlen) { if (msglen - ++x > *outlen) {
return CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
} }
/* copy message */ /* copy message */
@ -128,16 +151,22 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
out[y++] = DB[x++]; out[y++] = DB[x++];
} }
#ifdef CLEAN_STACK
zeromem(DB, sizeof(DB));
zeromem(seed, sizeof(seed));
zeromem(mask, sizeof(mask));
#endif
/* valid packet */ /* valid packet */
*res = 1; *res = 1;
return CRYPT_OK; err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(DB, modulus_len);
zeromem(seed, modulus_len);
zeromem(mask, modulus_len);
#endif
XFREE(seed);
XFREE(mask);
XFREE(DB);
return err;
} }
#endif /* PKCS_1 */ #endif /* PKCS_1 */

View File

@ -20,7 +20,7 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
int prng_idx, int hash_idx, int prng_idx, int hash_idx,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
unsigned char DB[1024], seed[MAXBLOCKSIZE], mask[sizeof(DB)]; unsigned char *DB, *seed, *mask;
unsigned long hLen, x, y, modulus_len; unsigned long hLen, x, y, modulus_len;
int err; int err;
@ -41,22 +41,41 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
hLen = hash_descriptor[hash_idx].hashsize; hLen = hash_descriptor[hash_idx].hashsize;
modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
/* allocate ram for DB/mask/salt of size modulus_len */
DB = XMALLOC(modulus_len);
mask = XMALLOC(modulus_len);
seed = XMALLOC(modulus_len);
if (DB == NULL || mask == NULL || seed == NULL) {
if (DB != NULL) {
XFREE(DB);
}
if (mask != NULL) {
XFREE(mask);
}
if (seed != NULL) {
XFREE(seed);
}
return CRYPT_MEM;
}
/* test message size */ /* test message size */
if (modulus_len >= sizeof(DB) || msglen > (modulus_len - 2*hLen - 2)) { if (msglen > (modulus_len - 2*hLen - 2)) {
return CRYPT_PK_INVALID_SIZE; err = CRYPT_PK_INVALID_SIZE;
goto __ERR;
} }
/* get lhash */ /* get lhash */
// DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes // DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes
x = sizeof(DB); x = modulus_len;
if (lparam != NULL) { if (lparam != NULL) {
if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) { if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
} else { } else {
/* can't pass hash_memory a NULL so use DB with zero length */ /* can't pass hash_memory a NULL so use DB with zero length */
if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) { if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
} }
@ -76,12 +95,13 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
/* now choose a random seed */ /* now choose a random seed */
if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) { if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) {
return CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto __ERR;
} }
/* compute MGF1 of seed (k - hlen - 1) */ /* compute MGF1 of seed (k - hlen - 1) */
if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* xor against DB */ /* xor against DB */
@ -91,7 +111,7 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
/* compute MGF1 of maskedDB (hLen) */ /* compute MGF1 of maskedDB (hLen) */
if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* XOR against seed */ /* XOR against seed */
@ -101,7 +121,8 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
/* create string of length modulus_len */ /* create string of length modulus_len */
if (*outlen < modulus_len) { if (*outlen < modulus_len) {
return CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
} }
/* start output which is 0x00 || maskedSeed || maskedDB */ /* start output which is 0x00 || maskedSeed || maskedDB */
@ -114,14 +135,20 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
out[x++] = DB[y]; out[x++] = DB[y];
} }
*outlen = x; *outlen = x;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(DB, sizeof(DB)); zeromem(DB, modulus_len);
zeromem(seed, sizeof(seed)); zeromem(seed, modulus_len);
zeromem(mask, sizeof(mask)); zeromem(mask, modulus_len);
#endif #endif
return CRYPT_OK; XFREE(seed);
XFREE(mask);
XFREE(DB);
return err;
} }
#endif /* PKCS_1 */ #endif /* PKCS_1 */

View File

@ -19,7 +19,7 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
unsigned long saltlen, int hash_idx, unsigned long saltlen, int hash_idx,
unsigned long modulus_bitlen, int *res) unsigned long modulus_bitlen, int *res)
{ {
unsigned char DB[1024], mask[sizeof(DB)], salt[sizeof(DB)], hash[sizeof(DB)]; unsigned char *DB, *mask, *salt, *hash;
unsigned long x, y, hLen, modulus_len; unsigned long x, y, hLen, modulus_len;
int err; int err;
hash_state md; hash_state md;
@ -38,15 +38,38 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
hLen = hash_descriptor[hash_idx].hashsize; hLen = hash_descriptor[hash_idx].hashsize;
modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
/* allocate ram for DB/mask/salt/hash of size modulus_len */
DB = XMALLOC(modulus_len);
mask = XMALLOC(modulus_len);
salt = XMALLOC(modulus_len);
hash = XMALLOC(modulus_len);
if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
if (DB != NULL) {
XFREE(DB);
}
if (mask != NULL) {
XFREE(mask);
}
if (salt != NULL) {
XFREE(salt);
}
if (hash != NULL) {
XFREE(hash);
}
return CRYPT_MEM;
}
/* check sizes */ /* check sizes */
if ((saltlen > sizeof(salt)) || (modulus_len > sizeof(DB)) || if ((saltlen > modulus_len) ||
(modulus_len < hLen + saltlen + 2) || (siglen != modulus_len)) { (modulus_len < hLen + saltlen + 2) || (siglen != modulus_len)) {
return CRYPT_INVALID_ARG; err = CRYPT_INVALID_ARG;
goto __ERR;
} }
/* ensure the 0xBC byte */ /* ensure the 0xBC byte */
if (sig[siglen-1] != 0xBC) { if (sig[siglen-1] != 0xBC) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* copy out the DB */ /* copy out the DB */
@ -61,12 +84,13 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
/* check the MSB */ /* check the MSB */
if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)))) != 0) { if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)))) != 0) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* generate mask of length modulus_len - hLen - 1 from hash */ /* generate mask of length modulus_len - hLen - 1 from hash */
if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* xor against DB */ /* xor against DB */
@ -82,28 +106,30 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
/* check for zeroes and 0x01 */ /* check for zeroes and 0x01 */
for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) { for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) {
if (DB[x] != 0x00) { if (DB[x] != 0x00) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
} }
if (DB[x++] != 0x01) { if (DB[x++] != 0x01) {
return CRYPT_OK; err = CRYPT_OK;
goto __ERR;
} }
/* M = (eight) 0x00 || msghash || salt, mask = H(M) */ /* M = (eight) 0x00 || msghash || salt, mask = H(M) */
hash_descriptor[hash_idx].init(&md); hash_descriptor[hash_idx].init(&md);
zeromem(mask, 8); zeromem(mask, 8);
if ((err = hash_descriptor[hash_idx].process(&md, mask, 8)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, mask, 8)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].process(&md, DB+x, saltlen)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, DB+x, saltlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* mask == hash means valid signature */ /* mask == hash means valid signature */
@ -111,14 +137,21 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
*res = 1; *res = 1;
} }
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(DB, sizeof(DB)); zeromem(DB, modulus_len);
zeromem(mask, sizeof(mask)); zeromem(mask, modulus_len);
zeromem(salt, sizeof(salt)); zeromem(salt, modulus_len);
zeromem(hash, sizeof(hash)); zeromem(hash, modulus_len);
#endif #endif
return CRYPT_OK; XFREE(hash);
XFREE(salt);
XFREE(mask);
XFREE(DB);
return err;
} }
#endif /* PKCS_1 */ #endif /* PKCS_1 */

View File

@ -20,7 +20,7 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
unsigned long modulus_bitlen, unsigned long modulus_bitlen,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
unsigned char DB[1024], mask[sizeof(DB)], salt[sizeof(DB)], hash[sizeof(DB)]; unsigned char *DB, *mask, *salt, *hash;
unsigned long x, y, hLen, modulus_len; unsigned long x, y, hLen, modulus_len;
int err; int err;
hash_state md; hash_state md;
@ -40,15 +40,39 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
hLen = hash_descriptor[hash_idx].hashsize; hLen = hash_descriptor[hash_idx].hashsize;
modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
/* allocate ram for DB/mask/salt/hash of size modulus_len */
DB = XMALLOC(modulus_len);
mask = XMALLOC(modulus_len);
salt = XMALLOC(modulus_len);
hash = XMALLOC(modulus_len);
if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
if (DB != NULL) {
XFREE(DB);
}
if (mask != NULL) {
XFREE(mask);
}
if (salt != NULL) {
XFREE(salt);
}
if (hash != NULL) {
XFREE(hash);
}
return CRYPT_MEM;
}
/* check sizes */ /* check sizes */
if ((saltlen > sizeof(salt)) || (modulus_len > sizeof(DB)) || (modulus_len < hLen + saltlen + 2)) { if ((saltlen > modulus_len) || (modulus_len < hLen + saltlen + 2)) {
return CRYPT_INVALID_ARG; err = CRYPT_INVALID_ARG;
goto __ERR;
} }
/* generate random salt */ /* generate random salt */
if (saltlen > 0) { if (saltlen > 0) {
if (prng_descriptor[prng_idx].read(salt, saltlen, prng) != saltlen) { if (prng_descriptor[prng_idx].read(salt, saltlen, prng) != saltlen) {
return CRYPT_ERROR_READPRNG; err = CRYPT_ERROR_READPRNG;
goto __ERR;
} }
} }
@ -56,16 +80,16 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
hash_descriptor[hash_idx].init(&md); hash_descriptor[hash_idx].init(&md);
zeromem(DB, 8); zeromem(DB, 8);
if ((err = hash_descriptor[hash_idx].process(&md, DB, 8)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, DB, 8)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].process(&md, salt, saltlen)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].process(&md, salt, saltlen)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hash_descriptor[hash_idx].done(&md, hash)) != CRYPT_OK) { if ((err = hash_descriptor[hash_idx].done(&md, hash)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */ /* generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
@ -79,7 +103,7 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
/* generate mask of length modulus_len - hLen - 1 from hash */ /* generate mask of length modulus_len - hLen - 1 from hash */
if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* xor against DB */ /* xor against DB */
@ -89,7 +113,8 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
/* output is DB || hash || 0xBC */ /* output is DB || hash || 0xBC */
if (*outlen < modulus_len) { if (*outlen < modulus_len) {
return CRYPT_BUFFER_OVERFLOW; err = CRYPT_BUFFER_OVERFLOW;
goto __ERR;
} }
/* DB */ /* DB */
@ -108,15 +133,21 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
/* store output size */ /* store output size */
*outlen = modulus_len; *outlen = modulus_len;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(DB, sizeof(DB)); zeromem(DB, modulus_len);
zeromem(mask, sizeof(mask)); zeromem(mask, modulus_len);
zeromem(salt, sizeof(salt)); zeromem(salt, modulus_len);
zeromem(hash, sizeof(hash)); zeromem(hash, modulus_len);
#endif #endif
return CRYPT_OK; XFREE(hash);
XFREE(salt);
XFREE(mask);
XFREE(DB);
return err;
} }
#endif /* PKCS_1 */ #endif /* PKCS_1 */

View File

@ -28,13 +28,13 @@ int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen,
/* default to failed */ /* default to failed */
*res = 0; *res = 0;
/* must be at least 12 bytes long */ modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
if (msglen < 12) {
/* must be at least modulus_bytelen bytes long */
if (msglen != modulus_bytelen) {
return CRYPT_INVALID_ARG; return CRYPT_INVALID_ARG;
} }
modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
/* should start with 0x00 0x02 */ /* should start with 0x00 0x02 */
if (msg[0] != 0x00 || msg[1] != 0x02) { if (msg[0] != 0x00 || msg[1] != 0x02) {
return CRYPT_OK; return CRYPT_OK;
@ -52,7 +52,7 @@ int pkcs_1_v15_es_decode(const unsigned char *msg, unsigned long msglen,
if (x + outlen > modulus_bytelen) { if (x + outlen > modulus_bytelen) {
return CRYPT_PK_INVALID_SIZE; return CRYPT_PK_INVALID_SIZE;
} }
memcpy(out, msg + x, outlen); XMEMCPY(out, msg + x, outlen);
*res = 1; *res = 1;
return CRYPT_OK; return CRYPT_OK;
} }

View File

@ -46,7 +46,7 @@ int pkcs_1_v15_es_encode(const unsigned char *msg, unsigned long msglen,
} }
x += y; x += y;
out[x++] = 0x00; out[x++] = 0x00;
memcpy(out+x, msg, msglen); XMEMCPY(out+x, msg, msglen);
*outlen = modulus_bytelen; *outlen = modulus_bytelen;
return CRYPT_OK; return CRYPT_OK;

View File

@ -20,8 +20,8 @@ int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
{ {
int err; int err;
unsigned long x; unsigned long x;
hash_state md; hash_state *md;
unsigned char buf[MAXBLOCKSIZE]; unsigned char *buf;
_ARGCHK(password != NULL); _ARGCHK(password != NULL);
_ARGCHK(salt != NULL); _ARGCHK(salt != NULL);
@ -33,17 +33,36 @@ int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
return err; return err;
} }
/* allocate memory */
md = XMALLOC(sizeof(hash_state));
buf = XMALLOC(MAXBLOCKSIZE);
if (md == NULL || buf == NULL) {
if (md != NULL) {
XFREE(md);
}
if (buf != NULL) {
XFREE(buf);
}
return CRYPT_MEM;
}
/* hash initial password + salt */ /* hash initial password + salt */
hash_descriptor[hash_idx].init(&md); hash_descriptor[hash_idx].init(md);
hash_descriptor[hash_idx].process(&md, password, password_len); if ((err = hash_descriptor[hash_idx].process(md, password, password_len)) != CRYPT_OK) {
hash_descriptor[hash_idx].process(&md, salt, 8); goto __ERR;
hash_descriptor[hash_idx].done(&md, buf); }
if ((err = hash_descriptor[hash_idx].process(md, salt, 8)) != CRYPT_OK) {
goto __ERR;
}
if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
goto __ERR;
}
while (--iteration_count) { while (--iteration_count) {
// code goes here. // code goes here.
x = sizeof(buf); x = MAXBLOCKSIZE;
if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) { if ((err = hash_memory(hash_idx, buf, hash_descriptor[hash_idx].hashsize, buf, &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
} }
@ -52,12 +71,17 @@ int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
out[x] = buf[x]; out[x] = buf[x];
} }
*outlen = x; *outlen = x;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE);
zeromem(md, sizeof(hash_state));
#endif #endif
return CRYPT_OK; XFREE(buf);
XFREE(md);
return err;
} }
#endif #endif

View File

@ -20,8 +20,8 @@ int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
{ {
int err, itts; int err, itts;
unsigned long stored, left, x, y, blkno; unsigned long stored, left, x, y, blkno;
unsigned char buf[2][MAXBLOCKSIZE]; unsigned char *buf[2];
hmac_state hmac; hmac_state *hmac;
_ARGCHK(password != NULL); _ARGCHK(password != NULL);
_ARGCHK(salt != NULL); _ARGCHK(salt != NULL);
@ -33,37 +33,51 @@ int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
return err; return err;
} }
buf[0] = XMALLOC(MAXBLOCKSIZE * 2);
hmac = XMALLOC(sizeof(hmac_state));
if (hmac == NULL || buf[0] == NULL) {
if (hmac != NULL) {
XFREE(hmac);
}
if (buf[0] != NULL) {
XFREE(buf[0]);
}
return CRYPT_MEM;
}
/* buf[1] points to the second block of MAXBLOCKSIZE bytes */
buf[1] = buf[0] + MAXBLOCKSIZE;
left = *outlen; left = *outlen;
blkno = 1; blkno = 1;
stored = 0; stored = 0;
while (left != 0) { while (left != 0) {
/* process block number blkno */ /* process block number blkno */
zeromem(buf, sizeof(buf)); zeromem(buf, MAXBLOCKSIZE*2);
/* store current block number and increment for next pass */ /* store current block number and increment for next pass */
STORE32H(blkno, buf[1]); STORE32H(blkno, buf[1]);
++blkno; ++blkno;
/* get PRF(P, S||int(blkno)) */ /* get PRF(P, S||int(blkno)) */
if ((err = hmac_init(&hmac, hash_idx, password, password_len)) != CRYPT_OK) { if ((err = hmac_init(hmac, hash_idx, password, password_len)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hmac_process(&hmac, salt, salt_len)) != CRYPT_OK) { if ((err = hmac_process(hmac, salt, salt_len)) != CRYPT_OK) {
return err; goto __ERR;
} }
if ((err = hmac_process(&hmac, buf[1], 4)) != CRYPT_OK) { if ((err = hmac_process(hmac, buf[1], 4)) != CRYPT_OK) {
return err; goto __ERR;
} }
x = sizeof(buf[0]); x = MAXBLOCKSIZE;
if ((err = hmac_done(&hmac, buf[0], &x)) != CRYPT_OK) { if ((err = hmac_done(hmac, buf[0], &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
/* now compute repeated and XOR it in buf[1] */ /* now compute repeated and XOR it in buf[1] */
memcpy(buf[1], buf[0], x); XMEMCPY(buf[1], buf[0], x);
for (itts = 2; itts < iteration_count; ++itts) { for (itts = 2; itts < iteration_count; ++itts) {
if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) { if ((err = hmac_memory(hash_idx, password, password_len, buf[0], x, buf[0], &x)) != CRYPT_OK) {
return err; goto __ERR;
} }
for (y = 0; y < x; y++) { for (y = 0; y < x; y++) {
buf[1][y] ^= buf[0][y]; buf[1][y] ^= buf[0][y];
@ -78,10 +92,17 @@ int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
} }
*outlen = stored; *outlen = stored;
err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf[0], MAXBLOCKSIZE*2);
zeromem(hmac, sizeof(hmac_state));
#endif #endif
return CRYPT_OK;
XFREE(hmac);
XFREE(buf[0]);
return err;
} }
#endif #endif

View File

@ -35,7 +35,7 @@ static const struct {
int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen) int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen)
{ {
int poly, x, y, m, err; int poly, x, y, m, err;
unsigned char L[MAXBLOCKSIZE]; unsigned char *L;
_ARGCHK(pmac != NULL); _ARGCHK(pmac != NULL);
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
@ -61,12 +61,18 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
return err; return err;
} }
/* allocate L */
L = XMALLOC(pmac->block_len);
if (L == NULL) {
return CRYPT_MEM;
}
/* find L = E[0] */ /* find L = E[0] */
zeromem(L, pmac->block_len); zeromem(L, pmac->block_len);
cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key); cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key);
/* find Ls[i] = L << i for i == 0..31 */ /* find Ls[i] = L << i for i == 0..31 */
memcpy(pmac->Ls[0], L, pmac->block_len); XMEMCPY(pmac->Ls[0], L, pmac->block_len);
for (x = 1; x < 32; x++) { for (x = 1; x < 32; x++) {
m = pmac->Ls[x-1][0] >> 7; m = pmac->Ls[x-1][0] >> 7;
for (y = 0; y < pmac->block_len-1; y++) { for (y = 0; y < pmac->block_len-1; y++) {
@ -105,9 +111,11 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
zeromem(pmac->checksum, sizeof(pmac->checksum)); zeromem(pmac->checksum, sizeof(pmac->checksum));
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(L, sizeof(L)); zeromem(L, pmac->block_len);
#endif #endif
XFREE(L);
return CRYPT_OK; return CRYPT_OK;
} }

View File

@ -20,25 +20,37 @@ int pmac_memory(int cipher,
unsigned char *out, unsigned long *outlen) unsigned char *out, unsigned long *outlen)
{ {
int err; int err;
pmac_state pmac; pmac_state *pmac;
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
_ARGCHK(msg != NULL); _ARGCHK(msg != NULL);
_ARGCHK(out != NULL); _ARGCHK(out != NULL);
_ARGCHK(outlen != NULL); _ARGCHK(outlen != NULL);
/* allocate ram for pmac state */
if ((err = pmac_init(&pmac, cipher, key, keylen)) != CRYPT_OK) { pmac = XMALLOC(sizeof(pmac_state));
return err; if (pmac == NULL) {
return CRYPT_MEM;
} }
if ((err = pmac_process(&pmac, msg, msglen)) != CRYPT_OK) {
return err; if ((err = pmac_init(pmac, cipher, key, keylen)) != CRYPT_OK) {
goto __ERR;
} }
if ((err = pmac_done(&pmac, out, outlen)) != CRYPT_OK) { if ((err = pmac_process(pmac, msg, msglen)) != CRYPT_OK) {
return err; goto __ERR;
}
if ((err = pmac_done(pmac, out, outlen)) != CRYPT_OK) {
goto __ERR;
} }
return CRYPT_OK; err = CRYPT_OK;
__ERR:
#ifdef CLEAN_STACK
zeromem(pmac, sizeof(pmac_state));
#endif
XFREE(pmac);
return err;
} }
#endif #endif

View File

@ -46,7 +46,7 @@ int pmac_process(pmac_state *state, const unsigned char *buf, unsigned long len)
/* add bytes */ /* add bytes */
n = MIN(len, (unsigned long)(state->block_len - state->buflen)); n = MIN(len, (unsigned long)(state->block_len - state->buflen));
memcpy(state->block + state->buflen, buf, n); XMEMCPY(state->block + state->buflen, buf, n);
state->buflen += n; state->buflen += n;
len -= n; len -= n;
buf += n; buf += n;

82
pretty.build Normal file
View File

@ -0,0 +1,82 @@
#!/bin/perl -w
#
# Cute little builder for perl
# Total waste of development time...
#
# This will build all the object files and then the archive .a file
# requires GCC, GNU make and a sense of humour.
#
# Tom St Denis
use strict;
my $count = 0;
my $starttime = time;
my $rate = 0;
print "Scanning for source files...\n";
foreach my $filename (glob "*.c") {
if (!($filename =~ "aes_tab.c")) {
if (!($filename =~ "twofish_tab.c")) {
if (!($filename =~ "whirltab.c")) {
if (!($filename =~ "sha224.c")) {
if (!($filename =~ "sha384.c")) {
if (!($filename =~ "dh_sys.c")) {
if (!($filename =~ "ecc_sys.c")) {
++$count;
}}}}}}}
}
print "Source files to build: $count\nBuilding...\n";
my $i = 0;
my $lines = 0;
my $filesbuilt = 0;
foreach my $filename (glob "*.c") {
if (!($filename =~ "aes_tab.c")) {
if (!($filename =~ "twofish_tab.c")) {
if (!($filename =~ "whirltab.c")) {
if (!($filename =~ "sha224.c")) {
if (!($filename =~ "sha384.c")) {
if (!($filename =~ "dh_sys.c")) {
if (!($filename =~ "ecc_sys.c")) {
printf("Building %3.2f%%, ", (++$i/$count)*100.0);
if ($i % 4 == 0) { print "/, "; }
if ($i % 4 == 1) { print "-, "; }
if ($i % 4 == 2) { print "\\, "; }
if ($i % 4 == 3) { print "|, "; }
if ($rate > 0) {
my $tleft = ($count - $i) / $rate;
my $tsec = $tleft%60;
my $tmin = ($tleft/60)%60;
my $thour = ($tleft/3600)%60;
printf("%2d:%02d:%02d left, ", $thour, $tmin, $tsec);
}
my $cnt = ($i/$count)*30.0;
my $x = 0;
print "[";
for (; $x < $cnt; $x++) { print "#"; }
for (; $x < 30; $x++) { print " "; }
print "]\r";
my $tmp = $filename;
$tmp =~ s/\.c/".o"/ge;
if (open(SRC, "<$tmp")) {
close SRC;
} else {
!system("make $tmp > /dev/null 2>/dev/null") or die "\nERROR: Failed to make $tmp!!!\n";
open( SRC, "<$filename" ) or die "Couldn't open $filename for reading: $!";
++$lines while (<SRC>);
close SRC or die "Error closing $filename after reading: $!";
++$filesbuilt;
}
# update timer
if (time != $starttime) {
my $delay = time - $starttime;
$rate = $i/$delay;
}
}}}}}}}
}
# finish building the library
printf("\nFinished building source (%d seconds, %3.2f files per second).\n", time - $starttime, $rate);
print "Compiled approximately $filesbuilt files and $lines lines of code.\n";
print "Doing final make (building archive...)\n";
!system("make > /dev/null 2>/dev/null") or die "\nERROR: Failed to perform last make command!!!\n";
print "done.\n";

2
rc4.c
View File

@ -56,7 +56,7 @@ int rc4_ready(prng_state *prng)
_ARGCHK(prng != NULL); _ARGCHK(prng != NULL);
/* extract the key */ /* extract the key */
memcpy(key, prng->rc4.buf, 256); XMEMCPY(key, prng->rc4.buf, 256);
keylen = prng->rc4.x; keylen = prng->rc4.x;
/* make RC4 perm and shuffle */ /* make RC4 perm and shuffle */

2
rc5.c
View File

@ -81,7 +81,7 @@ int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ke
/* setup the S array */ /* setup the S array */
t = (ulong32)(2 * (num_rounds + 1)); t = (ulong32)(2 * (num_rounds + 1));
memcpy(S, stab, t * sizeof(*S)); XMEMCPY(S, stab, t * sizeof(*S));
/* mix buffer */ /* mix buffer */
s = 3 * MAX(t, j); s = 3 * MAX(t, j);

2
rc6.c
View File

@ -71,7 +71,7 @@ int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ke
} }
/* setup the S array */ /* setup the S array */
memcpy(S, stab, 44 * sizeof(stab[0])); XMEMCPY(S, stab, 44 * sizeof(stab[0]));
/* mix buffer */ /* mix buffer */
s = 3 * MAX(44, j); s = 3 * MAX(44, j);

View File

@ -23,6 +23,7 @@ int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
{ {
unsigned long modulus_bitlen, modulus_bytelen, x; unsigned long modulus_bitlen, modulus_bytelen, x;
int err; int err;
unsigned char *tmp;
_ARGCHK(outkey != NULL); _ARGCHK(outkey != NULL);
_ARGCHK(keylen != NULL); _ARGCHK(keylen != NULL);
@ -43,15 +44,24 @@ int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
return CRYPT_INVALID_PACKET; return CRYPT_INVALID_PACKET;
} }
/* allocate ram */
tmp = XMALLOC(inlen);
if (tmp == NULL) {
return CRYPT_MEM;
}
/* rsa decode the packet */ /* rsa decode the packet */
x = *keylen; x = inlen;
if ((err = rsa_exptmod(in, inlen, outkey, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) { if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) {
XFREE(tmp);
return err; return err;
} }
/* now OAEP decode the packet */ /* now OAEP decode the packet */
return pkcs_1_oaep_decode(outkey, x, lparam, lparamlen, modulus_bitlen, hash_idx, err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
outkey, keylen, res); outkey, keylen, res);
XFREE(tmp);
return err;
} }
#endif /* MRSA */ #endif /* MRSA */

View File

@ -28,10 +28,12 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
_ARGCHK(outlen != NULL); _ARGCHK(outlen != NULL);
_ARGCHK(key != NULL); _ARGCHK(key != NULL);
/* valid prng? */
if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
return err; return err;
} }
/* is the key of the right type for the operation? */
if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) { if (which == PK_PRIVATE && (key->type != PK_PRIVATE && key->type != PK_PRIVATE_OPTIMIZED)) {
return CRYPT_PK_NOT_PRIVATE; return CRYPT_PK_NOT_PRIVATE;
} }

View File

@ -65,40 +65,43 @@ int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key)
goto error; goto error;
} }
if ((err = mp_set_int(&key->e, e)) != MP_OKAY) { goto error2; } /* key->e = e */ if ((err = mp_set_int(&key->e, e)) != MP_OKAY) { goto error2; } /* key->e = e */
if ((err = mp_invmod(&key->e, &tmp1, &key->d)) != MP_OKAY) { goto error2; } /* key->d = 1/e mod lcm(p-1,q-1) */ if ((err = mp_invmod(&key->e, &tmp1, &key->d)) != MP_OKAY) { goto error2; } /* key->d = 1/e mod lcm(p-1,q-1) */
if ((err = mp_mul(&p, &q, &key->N)) != MP_OKAY) { goto error2; } /* key->N = pq */ if ((err = mp_mul(&p, &q, &key->N)) != MP_OKAY) { goto error2; } /* key->N = pq */
/* optimize for CRT now */ /* optimize for CRT now */
/* find d mod q-1 and d mod p-1 */ /* find d mod q-1 and d mod p-1 */
if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error2; } /* tmp1 = q-1 */ if ((err = mp_sub_d(&p, 1, &tmp1)) != MP_OKAY) { goto error2; } /* tmp1 = q-1 */
if ((err = mp_sub_d(&q, 1, &tmp2)) != MP_OKAY) { goto error2; } /* tmp2 = p-1 */ if ((err = mp_sub_d(&q, 1, &tmp2)) != MP_OKAY) { goto error2; } /* tmp2 = p-1 */
if ((err = mp_mod(&key->d, &tmp1, &key->dP)) != MP_OKAY) { goto error2; } /* dP = d mod p-1 */ if ((err = mp_mod(&key->d, &tmp1, &key->dP)) != MP_OKAY) { goto error2; } /* dP = d mod p-1 */
if ((err = mp_mod(&key->d, &tmp2, &key->dQ)) != MP_OKAY) { goto error2; } /* dQ = d mod q-1 */ if ((err = mp_mod(&key->d, &tmp2, &key->dQ)) != MP_OKAY) { goto error2; } /* dQ = d mod q-1 */
if ((err = mp_invmod(&q, &p, &key->qP)) != MP_OKAY) { goto error2; } /* qP = 1/q mod p */ if ((err = mp_invmod(&q, &p, &key->qP)) != MP_OKAY) { goto error2; } /* qP = 1/q mod p */
if ((err = mp_mulmod(&key->qP, &q, &key->N, &key->qP)) != MP_OKAY) { goto error2; } /* qP = q * (1/q mod p) mod N */ if ((err = mp_mulmod(&key->qP, &q, &key->N, &key->qP)) != MP_OKAY) { goto error2; } /* qP = q * (1/q mod p) mod N */
if ((err = mp_invmod(&p, &q, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = 1/p mod q */ if ((err = mp_invmod(&p, &q, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = 1/p mod q */
if ((err = mp_mulmod(&key->pQ, &p, &key->N, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = p * (1/p mod q) mod N */ if ((err = mp_mulmod(&key->pQ, &p, &key->N, &key->pQ)) != MP_OKAY) { goto error2; } /* pQ = p * (1/p mod q) mod N */
if ((err = mp_copy(&p, &key->p)) != MP_OKAY) { goto error2; } if ((err = mp_copy(&p, &key->p)) != MP_OKAY) { goto error2; }
if ((err = mp_copy(&q, &key->q)) != MP_OKAY) { goto error2; } if ((err = mp_copy(&q, &key->q)) != MP_OKAY) { goto error2; }
/* shrink ram required */ /* shrink ram required */
if ((err = mp_shrink(&key->e)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->e)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->d)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->d)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->N)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->N)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->dQ)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->dQ)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->dP)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->dP)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->qP)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->qP)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->pQ)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->pQ)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->p)) != MP_OKAY) { goto error2; }
if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error2; } if ((err = mp_shrink(&key->q)) != MP_OKAY) { goto error2; }
err = CRYPT_OK; /* set key type (in this case it's CRT optimized) */
key->type = PK_PRIVATE_OPTIMIZED; key->type = PK_PRIVATE_OPTIMIZED;
/* return ok and free temps */
err = CRYPT_OK;
goto done; goto done;
error2: error2:
mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP, mp_clear_multi(&key->d, &key->e, &key->N, &key->dQ, &key->dP,

View File

@ -48,7 +48,7 @@ int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
} }
/* allocate temp buffer for decoded sig */ /* allocate temp buffer for decoded sig */
tmpbuf = XCALLOC(1, modulus_bytelen + 1); tmpbuf = XMALLOC(siglen);
if (tmpbuf == NULL) { if (tmpbuf == NULL) {
return CRYPT_MEM; return CRYPT_MEM;
} }

View File

@ -25,7 +25,7 @@ int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode) unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode)
{ {
unsigned char Z[MAXBLOCKSIZE], Y[MAXBLOCKSIZE], X[MAXBLOCKSIZE]; unsigned char *Z, *Y, *X;
int err, x; int err, x;
_ARGCHK(ocb != NULL); _ARGCHK(ocb != NULL);
@ -41,9 +41,26 @@ int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
return CRYPT_INVALID_ARG; return CRYPT_INVALID_ARG;
} }
/* allocate ram */
Z = XMALLOC(MAXBLOCKSIZE);
Y = XMALLOC(MAXBLOCKSIZE);
X = XMALLOC(MAXBLOCKSIZE);
if (X == NULL || Y == NULL || Z == NULL) {
if (X != NULL) {
XFREE(X);
}
if (Y != NULL) {
XFREE(Y);
}
if (Z != NULL) {
XFREE(Z);
}
return CRYPT_MEM;
}
/* compute X[m] = len(pt[m]) XOR Lr XOR Z[m] */ /* compute X[m] = len(pt[m]) XOR Lr XOR Z[m] */
ocb_shift_xor(ocb, X); ocb_shift_xor(ocb, X);
memcpy(Z, X, ocb->block_len); XMEMCPY(Z, X, ocb->block_len);
X[ocb->block_len-1] ^= (ptlen*8)&255; X[ocb->block_len-1] ^= (ptlen*8)&255;
X[ocb->block_len-2] ^= ((ptlen*8)>>8)&255; X[ocb->block_len-2] ^= ((ptlen*8)>>8)&255;
@ -90,11 +107,16 @@ int __ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
*taglen = x; *taglen = x;
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(X, sizeof(X)); zeromem(X, MAXBLOCKSIZE);
zeromem(Y, sizeof(Y)); zeromem(Y, MAXBLOCKSIZE);
zeromem(Z, sizeof(Z)); zeromem(Z, MAXBLOCKSIZE);
zeromem(ocb, sizeof(*ocb)); zeromem(ocb, sizeof(*ocb));
#endif #endif
XFREE(X);
XFREE(Y);
XFREE(Z);
return CRYPT_OK; return CRYPT_OK;
} }

View File

@ -50,7 +50,7 @@ int sha224_done(hash_state * md, unsigned char *hash)
int err; int err;
err = sha256_done(md, buf); err = sha256_done(md, buf);
memcpy(hash, buf, 28); XMEMCPY(hash, buf, 28);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, sizeof(buf));
#endif #endif

View File

@ -104,7 +104,7 @@ static void sha256_compress(hash_state * md, unsigned char *buf)
RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i); RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
} }
#else #else
#define RND(a,b,c,d,e,f,g,h,i,ki) \ #define RND(a,b,c,d,e,f,g,h,i,ki) \
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \

View File

@ -58,7 +58,7 @@ int sha384_done(hash_state * md, unsigned char *hash)
} }
sha512_done(md, buf); sha512_done(md, buf);
memcpy(hash, buf, 48); XMEMCPY(hash, buf, 48);
#ifdef CLEAN_STACK #ifdef CLEAN_STACK
zeromem(buf, sizeof(buf)); zeromem(buf, sizeof(buf));
#endif #endif

View File

@ -34,13 +34,13 @@ int tim_exptmod(prng_state *prng, int prng_idx,
} }
/* pick random r */ /* pick random r */
rtmp = XMALLOC(MAX_RSA_SIZE/8); rlen = mp_unsigned_bin_size(n);
rtmp = XMALLOC(rlen);
if (rtmp == NULL) { if (rtmp == NULL) {
return CRYPT_MEM; return CRYPT_MEM;
} }
/* read in random value "r" */
rlen = mp_unsigned_bin_size(n);
if (prng_descriptor[prng_idx].read(rtmp, rlen, prng) != rlen) { if (prng_descriptor[prng_idx].read(rtmp, rlen, prng) != rlen) {
XFREE(rtmp); XFREE(rtmp);
return CRYPT_ERROR_READPRNG; return CRYPT_ERROR_READPRNG;