00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00038 static uint32 fips186hinit[5] = { 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0, 0x67452301 };
00039
00040
00041 const randomGenerator fips186prng = { "FIPS 186", sizeof(fips186Param), (const randomGeneratorSetup) fips186Setup, (const randomGeneratorSeed) fips186Seed, (const randomGeneratorNext) fips186Next, (const randomGeneratorCleanup) fips186Cleanup };
00042
00043
00046
00047 static int fips186init(register sha1Param* p)
00048
00049 {
00050 mp32copy(5, p->h, fips186hinit);
00051 return 0;
00052 }
00053
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
00069
00070 if (pthread_mutex_init(&fp->lock, (pthread_mutexattr_t *) 0))
00071 return -1;
00072
00073
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
00099 if (pthread_mutex_lock(&fp->lock))
00100 return -1;
00101
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
00117 if (pthread_mutex_unlock(&fp->lock))
00118 return -1;
00119
00120 # endif
00121 # endif
00122 #endif
00123 return 0;
00124 }
00125 return -1;
00126 }
00127
00128
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
00143 if (pthread_mutex_lock(&fp->lock))
00144 return -1;
00145
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
00157 mp32copy(FIPS186_STATE_SIZE, fp->param.data, fp->state);
00158
00159 sha1Process(&fp->param);
00160
00161 (void) mp32addx(FIPS186_STATE_SIZE, fp->state, 5, fp->param.h);
00162 (void) mp32addw(FIPS186_STATE_SIZE, fp->state, 1);
00163
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
00183 if (pthread_mutex_unlock(&fp->lock))
00184 return -1;
00185
00186 # endif
00187 # endif
00188 #endif
00189 return 0;
00190 }
00191 return -1;
00192 }
00193
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
00209 if (pthread_mutex_destroy(&fp->lock))
00210 return -1;
00211
00212 # endif
00213 # endif
00214 #endif
00215 return 0;
00216 }
00217 return -1;
00218 }