Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

beecrypt/fips186.c

Go to the documentation of this file.
00001 
00007 /*
00008  * Copyright (c) 1998, 1999, 2000 Virtual Unlimited B.V.
00009  *
00010  * Author: Bob Deblier <bob@virtualunlimited.com>
00011  *
00012  * This library is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Lesser General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2.1 of the License, or (at your option) any later version.
00016  *
00017  * This library is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Lesser General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser General Public
00023  * License along with this library; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  */
00027 
00028 #include "system.h"
00029 #include "beecrypt.h"
00030 #include "fips186.h"
00031 #include "mp32opt.h"
00032 #include "mp32.h"
00033 #include "debug.h"
00034 
00037 /*@observer@*/ /*@unchecked@*/
00038 static uint32 fips186hinit[5] = { 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0x67452301 };
00039 
00040 /*@-sizeoftype@*/
00041 const randomGenerator fips186prng = { "FIPS 186", sizeof(fips186Param), (const randomGeneratorSetup) fips186Setup, (const randomGeneratorSeed) fips186Seed, (const randomGeneratorNext) fips186Next, (const randomGeneratorCleanup) fips186Cleanup };
00042 /*@=sizeoftype@*/
00043 
00046 /*@-boundswrite@*/
00047 static int fips186init(register sha1Param* p)
00048         /*@modifies p @*/
00049 {
00050         mp32copy(5, p->h, fips186hinit);
00051         return 0;
00052 }
00053 /*@=boundswrite@*/
00054 
00055 int fips186Setup(fips186Param* fp)
00056 {
00057         if (fp)
00058         {
00059                 #ifdef _REENTRANT
00060                 # if WIN32
00061                 if (!(fp->lock = CreateMutex(NULL, FALSE, NULL)))
00062                         return -1;
00063                 # else
00064                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00065                 if (mutex_init(&fp->lock, USYNC_THREAD, (void *) 0))
00066                         return -1;
00067                 #  elif HAVE_PTHREAD_H
00068                 /*@-nullpass@*/
00069                 /*@-moduncon@*/
00070                 if (pthread_mutex_init(&fp->lock, (pthread_mutexattr_t *) 0))
00071                         return -1;
00072                 /*@=moduncon@*/
00073                 /*@=nullpass@*/
00074                 #  endif
00075                 # endif
00076                 #endif
00077 
00078                 fp->digestsize = 0;
00079 
00080                 return entropyGatherNext(fp->state, FIPS186_STATE_SIZE);
00081         }
00082         return -1;
00083 }
00084 
00085 int fips186Seed(fips186Param* fp, const uint32* data, int size)
00086 {
00087         if (fp)
00088         {
00089                 #ifdef _REENTRANT
00090                 # if WIN32
00091                 if (WaitForSingleObject(fp->lock, INFINITE) != WAIT_OBJECT_0)
00092                         return -1;
00093                 # else
00094                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00095                 if (mutex_lock(&fp->lock))
00096                         return -1;
00097                 #  elif HAVE_PTHREAD_H
00098                 /*@-moduncon@*/
00099                 if (pthread_mutex_lock(&fp->lock))
00100                         return -1;
00101                 /*@=moduncon@*/
00102                 #  endif
00103                 # endif
00104                 #endif
00105                 if (data)
00106                         (void) mp32addx(FIPS186_STATE_SIZE, fp->state, size, data);
00107                 #ifdef _REENTRANT
00108                 # if WIN32
00109                 if (!ReleaseMutex(fp->lock))
00110                         return -1;
00111                 # else
00112                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00113                 if (mutex_unlock(&fp->lock))
00114                         return -1;
00115                 #  elif HAVE_PTHREAD_H
00116                 /*@-moduncon@*/
00117                 if (pthread_mutex_unlock(&fp->lock))
00118                         return -1;
00119                 /*@=moduncon@*/
00120                 #  endif
00121                 # endif
00122                 #endif
00123                 return 0;
00124         }
00125         return -1;
00126 }
00127 
00128 /*@-boundswrite@*/
00129 int fips186Next(fips186Param* fp, uint32* data, int size)
00130 {
00131         if (fp)
00132         {
00133                 #ifdef _REENTRANT
00134                 # if WIN32
00135                 if (WaitForSingleObject(fp->lock, INFINITE) != WAIT_OBJECT_0)
00136                         return -1;
00137                 # else
00138                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00139                 if (mutex_lock(&fp->lock))
00140                         return -1;
00141                 #  elif HAVE_PTHREAD_H
00142                 /*@-moduncon@*/
00143                 if (pthread_mutex_lock(&fp->lock))
00144                         return -1;
00145                 /*@=moduncon@*/
00146                 #  endif
00147                 # endif
00148                 #endif
00149                 while (size > 0)
00150                 {
00151                         register uint32 copy;
00152 
00153                         if (fp->digestsize == 0)
00154                         {
00155                                 (void) fips186init(&fp->param);
00156                                 /* copy the 512 bits of state data into the sha1Param */
00157                                 mp32copy(FIPS186_STATE_SIZE, fp->param.data, fp->state);
00158                                 /* process the data */
00159                                 sha1Process(&fp->param);
00160                                 /* set state to state + digest + 1 mod 2^512 */
00161                                 (void) mp32addx(FIPS186_STATE_SIZE, fp->state, 5, fp->param.h);
00162                                 (void) mp32addw(FIPS186_STATE_SIZE, fp->state, 1);
00163                                 /* we now have 5 words of pseudo-random data */
00164                                 fp->digestsize = 5;
00165                         }
00166 
00167                         copy = (size > fp->digestsize) ? fp->digestsize : size;
00168                         mp32copy(copy, data, fp->param.h + 5 - fp->digestsize);
00169                         fp->digestsize -= copy;
00170                         size -= copy;
00171                         data += copy;
00172                 }
00173                 #ifdef _REENTRANT
00174                 # if WIN32
00175                 if (!ReleaseMutex(fp->lock))
00176                         return -1;
00177                 # else
00178                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00179                 if (mutex_unlock(&fp->lock))
00180                         return -1;
00181                 #  elif HAVE_PTHREAD_H
00182                 /*@-moduncon@*/
00183                 if (pthread_mutex_unlock(&fp->lock))
00184                         return -1;
00185                 /*@=moduncon@*/
00186                 #  endif
00187                 # endif
00188                 #endif
00189                 return 0;
00190         }
00191         return -1;
00192 }
00193 /*@=boundswrite@*/
00194 
00195 int fips186Cleanup(fips186Param* fp)
00196 {
00197         if (fp)
00198         {
00199                 #ifdef _REENTRANT
00200                 # if WIN32
00201                 if (!CloseHandle(fp->lock))
00202                         return -1;
00203                 # else
00204                 #  if HAVE_THREAD_H && HAVE_SYNCH_H
00205                 if (mutex_destroy(&fp->lock))
00206                         return -1;
00207                 #  elif HAVE_PTHREAD_H
00208                 /*@-moduncon@*/
00209                 if (pthread_mutex_destroy(&fp->lock))
00210                         return -1;
00211                 /*@=moduncon@*/
00212                 #  endif
00213                 # endif
00214                 #endif
00215                 return 0;
00216         }
00217         return -1;
00218 }

Generated on Tue Sep 17 15:59:44 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002