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
00065
00066
00067
00068
00069
00070
00071 #include <asterisk/sha1.h>
00072
00073
00074 #define SHA1_ROTL(bits,word) \
00075 (((word) << (bits)) | ((word) >> (32-(bits))))
00076
00077
00078
00079
00080
00081 static uint32_t addTemp;
00082 #define SHA1AddLength(context, length) \
00083 (addTemp = (context)->Length_Low, \
00084 (context)->Corrupted = \
00085 (((context)->Length_Low += (length)) < addTemp) && \
00086 (++(context)->Length_High == 0) ? shaInputTooLong \
00087 : (context)->Corrupted )
00088
00089
00090 static void SHA1ProcessMessageBlock(SHA1Context * context);
00091 static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte);
00092 static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte);
00093
00094
00095
00096
00097
00098
00099
00100
00101 int SHA1Reset(SHA1Context *context)
00102 {
00103 if (!context) {
00104 return shaNull;
00105 }
00106
00107 context->Length_High = context->Length_Low = 0;
00108 context->Message_Block_Index = 0;
00109
00110
00111 context->Intermediate_Hash[0] = 0x67452301;
00112 context->Intermediate_Hash[1] = 0xEFCDAB89;
00113 context->Intermediate_Hash[2] = 0x98BADCFE;
00114 context->Intermediate_Hash[3] = 0x10325476;
00115 context->Intermediate_Hash[4] = 0xC3D2E1F0;
00116
00117 context->Computed = 0;
00118 context->Corrupted = shaSuccess;
00119
00120 return shaSuccess;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 int SHA1Input(SHA1Context *context,
00134 const uint8_t *message_array, unsigned length)
00135 {
00136 if (!context) {
00137 return shaNull;
00138 }
00139 if (!length) {
00140 return shaSuccess;
00141 }
00142 if (!message_array) {
00143 return shaNull;
00144 }
00145
00146 if (context->Computed) {
00147 context->Corrupted = shaStateError;
00148 return shaStateError;
00149 }
00150
00151 if (context->Corrupted) {
00152 return context->Corrupted;
00153 }
00154
00155 while (length--) {
00156 context->Message_Block[context->Message_Block_Index++] =
00157 *message_array;
00158
00159 if ((SHA1AddLength(context, 8) == shaSuccess) &&
00160 (context->Message_Block_Index == SHA1_Message_Block_Size))
00161 SHA1ProcessMessageBlock(context);
00162
00163 message_array++;
00164 }
00165
00166 return context->Corrupted;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 int SHA1FinalBits(SHA1Context * context, uint8_t message_bits,
00180 unsigned int length)
00181 {
00182 static uint8_t masks[8] = {
00183 0x00, 0x80,
00184 0xC0, 0xE0,
00185 0xF0, 0xF8,
00186 0xFC, 0xFE
00187 };
00188
00189 static uint8_t markbit[8] = {
00190 0x80, 0x40,
00191 0x20, 0x10,
00192 0x08, 0x04,
00193 0x02, 0x01
00194 };
00195
00196 if (!context)
00197 return shaNull;
00198 if (!length)
00199 return shaSuccess;
00200 if (context->Corrupted)
00201 return context->Corrupted;
00202 if (context->Computed)
00203 return context->Corrupted = shaStateError;
00204 if (length >= 8)
00205 return context->Corrupted = shaBadParam;
00206
00207 SHA1AddLength(context, length);
00208 SHA1Finalize(context,
00209 (uint8_t) ((message_bits & masks[length]) |
00210 markbit[length]));
00211
00212 return context->Corrupted;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 int SHA1Result(SHA1Context * context, uint8_t Message_Digest[SHA1HashSize])
00227 {
00228 int i;
00229
00230 if (!context) {
00231 return shaNull;
00232 }
00233 if (!Message_Digest) {
00234 return shaNull;
00235 }
00236 if (context->Corrupted) {
00237 return context->Corrupted;
00238 }
00239
00240 if (!context->Computed) {
00241 SHA1Finalize(context, 0x80);
00242 }
00243
00244 for (i = 0; i < SHA1HashSize; ++i) {
00245 Message_Digest[i] = (uint8_t) (context->Intermediate_Hash[i >> 2]
00246 >> (8 * (3 - (i & 0x03))));
00247 }
00248
00249 return shaSuccess;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 static void SHA1ProcessMessageBlock(SHA1Context *context)
00261 {
00262
00263 const uint32_t K[4] = {
00264 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
00265 };
00266 int t;
00267 uint32_t temp;
00268 uint32_t W[80];
00269 uint32_t A, B, C, D, E;
00270
00271
00272
00273
00274 for (t = 0; t < 16; t++) {
00275 W[t] = ((uint32_t) context->Message_Block[t * 4]) << 24;
00276 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 1]) << 16;
00277 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 2]) << 8;
00278 W[t] |= ((uint32_t) context->Message_Block[t * 4 + 3]);
00279 }
00280
00281 for (t = 16; t < 80; t++) {
00282 W[t] = SHA1_ROTL(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
00283 }
00284
00285 A = context->Intermediate_Hash[0];
00286 B = context->Intermediate_Hash[1];
00287 C = context->Intermediate_Hash[2];
00288 D = context->Intermediate_Hash[3];
00289 E = context->Intermediate_Hash[4];
00290
00291 for (t = 0; t < 20; t++) {
00292 temp = SHA1_ROTL(5, A) + SHA_Ch(B, C, D) + E + W[t] + K[0];
00293 E = D;
00294 D = C;
00295 C = SHA1_ROTL(30, B);
00296 B = A;
00297 A = temp;
00298 }
00299
00300 for (t = 20; t < 40; t++) {
00301 temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[1];
00302 E = D;
00303 D = C;
00304 C = SHA1_ROTL(30, B);
00305 B = A;
00306 A = temp;
00307 }
00308
00309 for (t = 40; t < 60; t++) {
00310 temp = SHA1_ROTL(5, A) + SHA_Maj(B, C, D) + E + W[t] + K[2];
00311 E = D;
00312 D = C;
00313 C = SHA1_ROTL(30, B);
00314 B = A;
00315 A = temp;
00316 }
00317
00318 for (t = 60; t < 80; t++) {
00319 temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[3];
00320 E = D;
00321 D = C;
00322 C = SHA1_ROTL(30, B);
00323 B = A;
00324 A = temp;
00325 }
00326
00327 context->Intermediate_Hash[0] += A;
00328 context->Intermediate_Hash[1] += B;
00329 context->Intermediate_Hash[2] += C;
00330 context->Intermediate_Hash[3] += D;
00331 context->Intermediate_Hash[4] += E;
00332
00333 context->Message_Block_Index = 0;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte)
00347 {
00348 int i;
00349 SHA1PadMessage(context, Pad_Byte);
00350
00351 for (i = 0; i < SHA1_Message_Block_Size; ++i) {
00352 context->Message_Block[i] = 0;
00353 }
00354 context->Length_High = 0;
00355 context->Length_Low = 0;
00356 context->Computed = 1;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte)
00375 {
00376
00377
00378
00379
00380
00381
00382 if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) {
00383 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
00384 while (context->Message_Block_Index < SHA1_Message_Block_Size) {
00385 context->Message_Block[context->Message_Block_Index++] = 0;
00386 }
00387
00388 SHA1ProcessMessageBlock(context);
00389 } else
00390 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
00391
00392 while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8)) {
00393 context->Message_Block[context->Message_Block_Index++] = 0;
00394 }
00395
00396
00397
00398
00399 context->Message_Block[56] = (uint8_t) (context->Length_High >> 24);
00400 context->Message_Block[57] = (uint8_t) (context->Length_High >> 16);
00401 context->Message_Block[58] = (uint8_t) (context->Length_High >> 8);
00402 context->Message_Block[59] = (uint8_t) (context->Length_High);
00403 context->Message_Block[60] = (uint8_t) (context->Length_Low >> 24);
00404 context->Message_Block[61] = (uint8_t) (context->Length_Low >> 16);
00405 context->Message_Block[62] = (uint8_t) (context->Length_Low >> 8);
00406 context->Message_Block[63] = (uint8_t) (context->Length_Low);
00407
00408 SHA1ProcessMessageBlock(context);
00409 }