00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 #include "asterisk/sha1.h"
00065
00066
00067
00068
00069 #define SHA1CircularShift(bits,word) \
00070 (((word) << (bits)) | ((word) >> (32-(bits))))
00071
00072
00073 void SHA1PadMessage(SHA1Context *);
00074 void SHA1ProcessMessageBlock(SHA1Context *);
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 int SHA1Reset(SHA1Context *context)
00092 {
00093 if (!context) {
00094 return shaNull;
00095 }
00096
00097 context->Length_Low = 0;
00098 context->Length_High = 0;
00099 context->Message_Block_Index = 0;
00100
00101 context->Intermediate_Hash[0] = 0x67452301;
00102 context->Intermediate_Hash[1] = 0xEFCDAB89;
00103 context->Intermediate_Hash[2] = 0x98BADCFE;
00104 context->Intermediate_Hash[3] = 0x10325476;
00105 context->Intermediate_Hash[4] = 0xC3D2E1F0;
00106
00107 context->Computed = 0;
00108 context->Corrupted = 0;
00109
00110 return shaSuccess;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 int SHA1Result( SHA1Context *context,
00133 uint8_t Message_Digest[SHA1HashSize])
00134 {
00135 int i;
00136
00137 if (!context || !Message_Digest) {
00138 return shaNull;
00139 }
00140
00141 if (context->Corrupted) {
00142 return context->Corrupted;
00143 }
00144
00145 if (!context->Computed) {
00146 SHA1PadMessage(context);
00147 for (i = 0; i < 64; ++i) {
00148
00149 context->Message_Block[i] = 0;
00150 }
00151 context->Length_Low = 0;
00152 context->Length_High = 0;
00153 context->Computed = 1;
00154 }
00155
00156 for (i = 0; i < SHA1HashSize; ++i) {
00157 Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
00158 }
00159
00160 return shaSuccess;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
00184 {
00185 if (!length) {
00186 return shaSuccess;
00187 }
00188
00189 if (!context || !message_array) {
00190 return shaNull;
00191 }
00192
00193 if (context->Computed) {
00194 context->Corrupted = shaStateError;
00195 return shaStateError;
00196 }
00197
00198 if (context->Corrupted) {
00199 return context->Corrupted;
00200 }
00201
00202 while (length-- && !context->Corrupted) {
00203 context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
00204
00205 context->Length_Low += 8;
00206 if (context->Length_Low == 0) {
00207 context->Length_High++;
00208 if (context->Length_High == 0) {
00209
00210 context->Corrupted = 1;
00211 }
00212 }
00213
00214 if (context->Message_Block_Index == 64) {
00215 SHA1ProcessMessageBlock(context);
00216 }
00217
00218 message_array++;
00219 }
00220
00221 return shaSuccess;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 void SHA1ProcessMessageBlock(SHA1Context *context)
00245 {
00246 const uint32_t K[] = {
00247 0x5A827999,
00248 0x6ED9EBA1,
00249 0x8F1BBCDC,
00250 0xCA62C1D6
00251 };
00252 int t;
00253 uint32_t temp;
00254 uint32_t W[80];
00255 uint32_t A, B, C, D, E;
00256
00257
00258
00259
00260 for (t = 0; t < 16; t++) {
00261 W[t] = context->Message_Block[t * 4] << 24;
00262 W[t] |= context->Message_Block[t * 4 + 1] << 16;
00263 W[t] |= context->Message_Block[t * 4 + 2] << 8;
00264 W[t] |= context->Message_Block[t * 4 + 3];
00265 }
00266
00267 for (t = 16; t < 80; t++) {
00268 W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
00269 }
00270
00271 A = context->Intermediate_Hash[0];
00272 B = context->Intermediate_Hash[1];
00273 C = context->Intermediate_Hash[2];
00274 D = context->Intermediate_Hash[3];
00275 E = context->Intermediate_Hash[4];
00276
00277 for (t = 0; t < 20; t++) {
00278 temp = SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
00279 E = D;
00280 D = C;
00281 C = SHA1CircularShift(30,B);
00282 B = A;
00283 A = temp;
00284 }
00285
00286 for (t = 20; t < 40; t++) {
00287 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
00288 E = D;
00289 D = C;
00290 C = SHA1CircularShift(30,B);
00291 B = A;
00292 A = temp;
00293 }
00294
00295 for (t = 40; t < 60; t++) {
00296 temp = SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
00297 E = D;
00298 D = C;
00299 C = SHA1CircularShift(30,B);
00300 B = A;
00301 A = temp;
00302 }
00303
00304 for (t = 60; t < 80; t++) {
00305 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
00306 E = D;
00307 D = C;
00308 C = SHA1CircularShift(30,B);
00309 B = A;
00310 A = temp;
00311 }
00312
00313 context->Intermediate_Hash[0] += A;
00314 context->Intermediate_Hash[1] += B;
00315 context->Intermediate_Hash[2] += C;
00316 context->Intermediate_Hash[3] += D;
00317 context->Intermediate_Hash[4] += E;
00318
00319 context->Message_Block_Index = 0;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 void SHA1PadMessage(SHA1Context *context)
00347 {
00348
00349
00350
00351
00352
00353
00354 if (context->Message_Block_Index > 55) {
00355 context->Message_Block[context->Message_Block_Index++] = 0x80;
00356 while (context->Message_Block_Index < 64) {
00357 context->Message_Block[context->Message_Block_Index++] = 0;
00358 }
00359
00360 SHA1ProcessMessageBlock(context);
00361
00362 while (context->Message_Block_Index < 56) {
00363 context->Message_Block[context->Message_Block_Index++] = 0;
00364 }
00365 } else {
00366 context->Message_Block[context->Message_Block_Index++] = 0x80;
00367 while (context->Message_Block_Index < 56) {
00368 context->Message_Block[context->Message_Block_Index++] = 0;
00369 }
00370 }
00371
00372
00373
00374
00375 context->Message_Block[56] = context->Length_High >> 24;
00376 context->Message_Block[57] = context->Length_High >> 16;
00377 context->Message_Block[58] = context->Length_High >> 8;
00378 context->Message_Block[59] = context->Length_High;
00379 context->Message_Block[60] = context->Length_Low >> 24;
00380 context->Message_Block[61] = context->Length_Low >> 16;
00381 context->Message_Block[62] = context->Length_Low >> 8;
00382 context->Message_Block[63] = context->Length_Low;
00383
00384 SHA1ProcessMessageBlock(context);
00385 }