Fri, 11 Oct 2002 03:14:01 +0000
[gaim-migrate @ 3753]
Yeah this will probably break a lot of shit knowing my luck. But hey, I really don't care what people thnk.
| 2086 | 1 | /* |
| 2 | * The contents of this file are subject to the Mozilla Public | |
| 3 | * License Version 1.1 (the "License"); you may not use this file | |
| 4 | * except in compliance with the License. You may obtain a copy of | |
| 5 | * the License at http://www.mozilla.org/MPL/ | |
| 6 | * | |
| 7 | * Software distributed under the License is distributed on an "AS | |
| 8 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
| 9 | * implied. See the License for the specific language governing | |
| 10 | * rights and limitations under the License. | |
| 11 | * | |
| 12 | * The Original Code is SHA 180-1 Reference Implementation (Compact version) | |
| 13 | * | |
| 14 | * The Initial Developer of the Original Code is Paul Kocher of | |
| 15 | * Cryptography Research. Portions created by Paul Kocher are | |
| 16 | * Copyright (C) 1995-9 by Cryptography Research, Inc. All | |
| 17 | * Rights Reserved. | |
| 18 | * | |
| 19 | * Contributor(s): | |
| 20 | * | |
| 21 | */ | |
| 22 | ||
| 3127 | 23 | #include "lib.h" |
| 2086 | 24 | |
| 3127 | 25 | static void shaHashBlock(j_SHA_CTX *ctx); |
| 2086 | 26 | |
| 3127 | 27 | void shaInit(j_SHA_CTX *ctx) { |
| 2086 | 28 | int i; |
| 29 | ||
| 30 | ctx->lenW = 0; | |
| 31 | ctx->sizeHi = ctx->sizeLo = 0; | |
| 32 | ||
| 33 | /* Initialize H with the magic constants (see FIPS180 for constants) | |
| 34 | */ | |
| 35 | ctx->H[0] = 0x67452301L; | |
| 36 | ctx->H[1] = 0xefcdab89L; | |
| 37 | ctx->H[2] = 0x98badcfeL; | |
| 38 | ctx->H[3] = 0x10325476L; | |
| 39 | ctx->H[4] = 0xc3d2e1f0L; | |
| 40 | ||
| 41 | for (i = 0; i < 80; i++) | |
| 42 | ctx->W[i] = 0; | |
| 43 | } | |
| 44 | ||
| 45 | ||
| 3127 | 46 | void shaUpdate(j_SHA_CTX *ctx, unsigned char *dataIn, int len) { |
| 2086 | 47 | int i; |
| 48 | ||
| 49 | /* Read the data into W and process blocks as they get full | |
| 50 | */ | |
| 51 | for (i = 0; i < len; i++) { | |
| 52 | ctx->W[ctx->lenW / 4] <<= 8; | |
| 53 | ctx->W[ctx->lenW / 4] |= (unsigned long)dataIn[i]; | |
| 54 | if ((++ctx->lenW) % 64 == 0) { | |
| 55 | shaHashBlock(ctx); | |
| 56 | ctx->lenW = 0; | |
| 57 | } | |
| 58 | ctx->sizeLo += 8; | |
| 59 | ctx->sizeHi += (ctx->sizeLo < 8); | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | ||
| 3127 | 64 | void shaFinal(j_SHA_CTX *ctx, unsigned char hashout[20]) { |
| 2086 | 65 | unsigned char pad0x80 = 0x80; |
| 66 | unsigned char pad0x00 = 0x00; | |
| 67 | unsigned char padlen[8]; | |
| 68 | int i; | |
| 69 | ||
| 70 | /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length | |
| 71 | */ | |
| 72 | padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255); | |
| 73 | padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255); | |
| 74 | padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255); | |
| 75 | padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255); | |
| 76 | padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255); | |
| 77 | padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255); | |
| 78 | padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255); | |
| 79 | padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255); | |
| 80 | shaUpdate(ctx, &pad0x80, 1); | |
| 81 | while (ctx->lenW != 56) | |
| 82 | shaUpdate(ctx, &pad0x00, 1); | |
| 83 | shaUpdate(ctx, padlen, 8); | |
| 84 | ||
| 85 | /* Output hash | |
| 86 | */ | |
| 87 | for (i = 0; i < 20; i++) { | |
| 88 | hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24); | |
| 89 | ctx->H[i / 4] <<= 8; | |
| 90 | } | |
| 91 | ||
| 92 | /* | |
| 93 | * Re-initialize the context (also zeroizes contents) | |
| 94 | */ | |
| 3127 | 95 | shaInit(ctx); |
| 2086 | 96 | } |
| 97 | ||
| 98 | ||
| 99 | void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) { | |
| 3127 | 100 | j_SHA_CTX ctx; |
| 2086 | 101 | |
| 102 | shaInit(&ctx); | |
| 103 | shaUpdate(&ctx, dataIn, len); | |
| 104 | shaFinal(&ctx, hashout); | |
| 105 | } | |
| 106 | ||
| 107 | ||
| 3127 | 108 | #define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL) |
| 2086 | 109 | |
| 3127 | 110 | static void shaHashBlock(j_SHA_CTX *ctx) { |
| 2086 | 111 | int t; |
| 112 | unsigned long A,B,C,D,E,TEMP; | |
| 113 | ||
| 114 | for (t = 16; t <= 79; t++) | |
| 115 | ctx->W[t] = | |
| 116 | SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1); | |
| 117 | ||
| 118 | A = ctx->H[0]; | |
| 119 | B = ctx->H[1]; | |
| 120 | C = ctx->H[2]; | |
| 121 | D = ctx->H[3]; | |
| 122 | E = ctx->H[4]; | |
| 123 | ||
| 124 | for (t = 0; t <= 19; t++) { | |
| 3127 | 125 | TEMP = (SHA_ROTL(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999L) & 0xffffffffL; |
| 2086 | 126 | E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; |
| 127 | } | |
| 128 | for (t = 20; t <= 39; t++) { | |
| 3127 | 129 | TEMP = (SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1L) & 0xffffffffL; |
| 2086 | 130 | E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; |
| 131 | } | |
| 132 | for (t = 40; t <= 59; t++) { | |
| 3127 | 133 | TEMP = (SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL) & 0xffffffffL; |
| 2086 | 134 | E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; |
| 135 | } | |
| 136 | for (t = 60; t <= 79; t++) { | |
| 3127 | 137 | TEMP = (SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6L) & 0xffffffffL; |
| 2086 | 138 | E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; |
| 139 | } | |
| 140 | ||
| 141 | ctx->H[0] += A; | |
| 142 | ctx->H[1] += B; | |
| 143 | ctx->H[2] += C; | |
| 144 | ctx->H[3] += D; | |
| 145 | ctx->H[4] += E; | |
| 146 | } | |
| 147 | ||
| 148 | /*---------------------------------------------------------------------------- | |
| 149 | * | |
| 150 | * This code added by Thomas "temas" Muldowney for Jabber compatability | |
| 151 | * | |
| 152 | *---------------------------------------------------------------------------*/ | |
| 153 | char *shahash(char *str) | |
| 154 | { | |
| 155 | static char final[41]; | |
| 156 | char *pos; | |
| 157 | unsigned char hashval[20]; | |
| 158 | int x; | |
| 159 | ||
| 160 | if(!str || strlen(str) == 0) | |
| 161 | return NULL; | |
| 162 | ||
| 163 | shaBlock((unsigned char *)str, strlen(str), hashval); | |
| 164 | ||
| 165 | pos = final; | |
| 166 | for(x=0;x<20;x++) | |
| 167 | { | |
| 168 | snprintf(pos, 3, "%02x", hashval[x]); | |
| 169 | pos += 2; | |
| 170 | } | |
| 171 | return (char *)final; | |
| 172 | } | |
| 173 | ||
| 174 | void shahash_r(const char* str, char hashbuf[41]) | |
| 175 | { | |
| 176 | int x; | |
| 177 | char *pos; | |
| 178 | unsigned char hashval[20]; | |
| 179 | ||
| 180 | if(!str || strlen(str) == 0) | |
| 181 | return; | |
| 182 | ||
| 183 | shaBlock((unsigned char *)str, strlen(str), hashval); | |
| 184 | ||
| 185 | pos = hashbuf; | |
| 186 | for(x=0;x<20;x++) | |
| 187 | { | |
| 188 | snprintf(pos, 3, "%02x", hashval[x]); | |
| 189 | pos += 2; | |
| 190 | } | |
| 191 | ||
| 192 | return; | |
| 193 | } |