Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 1 | /* SHA module */ |
| 2 | |
| 3 | /* This module provides an interface to NIST's Secure Hash Algorithm */ |
| 4 | |
| 5 | /* See below for information about the original code this module was |
| 6 | based upon. Additional work performed by: |
| 7 | |
Andrew M. Kuchling | a4e9a33 | 2000-08-01 01:26:02 +0000 | [diff] [blame] | 8 | Andrew Kuchling (amk1@bigfoot.com) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 9 | Greg Stein (gstein@lyra.org) |
| 10 | */ |
| 11 | |
| 12 | /* SHA objects */ |
| 13 | |
| 14 | #include "Python.h" |
| 15 | |
| 16 | |
| 17 | /* Endianness testing and definitions */ |
| 18 | #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\ |
| 19 | if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;} |
| 20 | |
| 21 | #define PCT_LITTLE_ENDIAN 1 |
| 22 | #define PCT_BIG_ENDIAN 0 |
| 23 | |
| 24 | /* Some useful types */ |
| 25 | |
| 26 | typedef unsigned char SHA_BYTE; |
| 27 | |
| 28 | #if SIZEOF_INT == 4 |
| 29 | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
| 30 | #else |
| 31 | /* not defined. compilation will die. */ |
| 32 | #endif |
| 33 | |
| 34 | /* The SHA block size and message digest sizes, in bytes */ |
| 35 | |
| 36 | #define SHA_BLOCKSIZE 64 |
| 37 | #define SHA_DIGESTSIZE 20 |
| 38 | |
| 39 | /* The structure for storing SHS info */ |
| 40 | |
| 41 | typedef struct { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 42 | PyObject_HEAD |
| 43 | SHA_INT32 digest[5]; /* Message digest */ |
| 44 | SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ |
| 45 | SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ |
| 46 | int Endianness; |
| 47 | int local; /* unprocessed amount in data */ |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 48 | } SHAobject; |
| 49 | |
| 50 | /* When run on a little-endian CPU we need to perform byte reversal on an |
| 51 | array of longwords. */ |
| 52 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 53 | static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 54 | { |
| 55 | SHA_INT32 value; |
| 56 | |
| 57 | if ( Endianness == PCT_BIG_ENDIAN ) |
| 58 | return; |
| 59 | |
| 60 | byteCount /= sizeof(*buffer); |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 61 | while (byteCount--) { |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 62 | value = *buffer; |
| 63 | value = ( ( value & 0xFF00FF00L ) >> 8 ) | \ |
| 64 | ( ( value & 0x00FF00FFL ) << 8 ); |
| 65 | *buffer++ = ( value << 16 ) | ( value >> 16 ); |
| 66 | } |
| 67 | } |
| 68 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 69 | static void SHAcopy(SHAobject *src, SHAobject *dest) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 70 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 71 | dest->Endianness = src->Endianness; |
| 72 | dest->local = src->local; |
| 73 | dest->count_lo = src->count_lo; |
| 74 | dest->count_hi = src->count_hi; |
| 75 | memcpy(dest->digest, src->digest, sizeof(src->digest)); |
| 76 | memcpy(dest->data, src->data, sizeof(src->data)); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | /* ------------------------------------------------------------------------ |
| 81 | * |
| 82 | * This code for the SHA algorithm was noted as public domain. The original |
| 83 | * headers are pasted below. |
| 84 | * |
| 85 | * Several changes have been made to make it more compatible with the |
| 86 | * Python environment and desired interface. |
| 87 | * |
| 88 | */ |
| 89 | |
| 90 | /* NIST Secure Hash Algorithm */ |
| 91 | /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */ |
| 92 | /* from Peter C. Gutmann's implementation as found in */ |
| 93 | /* Applied Cryptography by Bruce Schneier */ |
| 94 | /* Further modifications to include the "UNRAVEL" stuff, below */ |
| 95 | |
| 96 | /* This code is in the public domain */ |
| 97 | |
| 98 | /* UNRAVEL should be fastest & biggest */ |
| 99 | /* UNROLL_LOOPS should be just as big, but slightly slower */ |
| 100 | /* both undefined should be smallest and slowest */ |
| 101 | |
| 102 | #define UNRAVEL |
| 103 | /* #define UNROLL_LOOPS */ |
| 104 | |
| 105 | /* The SHA f()-functions. The f1 and f3 functions can be optimized to |
| 106 | save one boolean operation each - thanks to Rich Schroeppel, |
| 107 | rcs@cs.arizona.edu for discovering this */ |
| 108 | |
| 109 | /*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */ |
| 110 | #define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */ |
| 111 | #define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */ |
| 112 | /*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */ |
| 113 | #define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */ |
| 114 | #define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */ |
| 115 | |
| 116 | /* SHA constants */ |
| 117 | |
| 118 | #define CONST1 0x5a827999L /* Rounds 0-19 */ |
| 119 | #define CONST2 0x6ed9eba1L /* Rounds 20-39 */ |
| 120 | #define CONST3 0x8f1bbcdcL /* Rounds 40-59 */ |
| 121 | #define CONST4 0xca62c1d6L /* Rounds 60-79 */ |
| 122 | |
| 123 | /* 32-bit rotate */ |
| 124 | |
| 125 | #define R32(x,n) ((x << n) | (x >> (32 - n))) |
| 126 | |
| 127 | /* the generic case, for when the overall rotation is not unraveled */ |
| 128 | |
| 129 | #define FG(n) \ |
| 130 | T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \ |
| 131 | E = D; D = C; C = R32(B,30); B = A; A = T |
| 132 | |
| 133 | /* specific cases, for when the overall rotation is unraveled */ |
| 134 | |
| 135 | #define FA(n) \ |
| 136 | T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30) |
| 137 | |
| 138 | #define FB(n) \ |
| 139 | E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30) |
| 140 | |
| 141 | #define FC(n) \ |
| 142 | D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30) |
| 143 | |
| 144 | #define FD(n) \ |
| 145 | C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30) |
| 146 | |
| 147 | #define FE(n) \ |
| 148 | B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30) |
| 149 | |
| 150 | #define FT(n) \ |
| 151 | A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30) |
| 152 | |
| 153 | /* do SHA transformation */ |
| 154 | |
| 155 | static void |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 156 | sha_transform(SHAobject *sha_info) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 157 | { |
| 158 | int i; |
| 159 | SHA_INT32 T, A, B, C, D, E, W[80], *WP; |
| 160 | |
| 161 | memcpy(W, sha_info->data, sizeof(sha_info->data)); |
Guido van Rossum | ff1ccbf | 1999-04-10 15:48:23 +0000 | [diff] [blame] | 162 | longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 163 | |
| 164 | for (i = 16; i < 80; ++i) { |
| 165 | W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16]; |
| 166 | |
| 167 | /* extra rotation fix */ |
| 168 | W[i] = R32(W[i], 1); |
| 169 | } |
| 170 | A = sha_info->digest[0]; |
| 171 | B = sha_info->digest[1]; |
| 172 | C = sha_info->digest[2]; |
| 173 | D = sha_info->digest[3]; |
| 174 | E = sha_info->digest[4]; |
| 175 | WP = W; |
| 176 | #ifdef UNRAVEL |
| 177 | FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); |
| 178 | FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); |
| 179 | FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); |
| 180 | FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); |
| 181 | FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); |
| 182 | FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); |
| 183 | FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); |
| 184 | FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); |
| 185 | sha_info->digest[0] += E; |
| 186 | sha_info->digest[1] += T; |
| 187 | sha_info->digest[2] += A; |
| 188 | sha_info->digest[3] += B; |
| 189 | sha_info->digest[4] += C; |
| 190 | #else /* !UNRAVEL */ |
| 191 | #ifdef UNROLL_LOOPS |
| 192 | FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); |
| 193 | FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); |
| 194 | FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); |
| 195 | FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); |
| 196 | FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); |
| 197 | FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); |
| 198 | FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); |
| 199 | FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); |
| 200 | #else /* !UNROLL_LOOPS */ |
| 201 | for (i = 0; i < 20; ++i) { FG(1); } |
| 202 | for (i = 20; i < 40; ++i) { FG(2); } |
| 203 | for (i = 40; i < 60; ++i) { FG(3); } |
| 204 | for (i = 60; i < 80; ++i) { FG(4); } |
| 205 | #endif /* !UNROLL_LOOPS */ |
| 206 | sha_info->digest[0] += A; |
| 207 | sha_info->digest[1] += B; |
| 208 | sha_info->digest[2] += C; |
| 209 | sha_info->digest[3] += D; |
| 210 | sha_info->digest[4] += E; |
| 211 | #endif /* !UNRAVEL */ |
| 212 | } |
| 213 | |
| 214 | /* initialize the SHA digest */ |
| 215 | |
| 216 | static void |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 217 | sha_init(SHAobject *sha_info) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 218 | { |
| 219 | TestEndianness(sha_info->Endianness) |
| 220 | |
| 221 | sha_info->digest[0] = 0x67452301L; |
| 222 | sha_info->digest[1] = 0xefcdab89L; |
| 223 | sha_info->digest[2] = 0x98badcfeL; |
| 224 | sha_info->digest[3] = 0x10325476L; |
| 225 | sha_info->digest[4] = 0xc3d2e1f0L; |
| 226 | sha_info->count_lo = 0L; |
| 227 | sha_info->count_hi = 0L; |
| 228 | sha_info->local = 0; |
| 229 | } |
| 230 | |
| 231 | /* update the SHA digest */ |
| 232 | |
| 233 | static void |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 234 | sha_update(SHAobject *sha_info, SHA_BYTE *buffer, int count) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 235 | { |
| 236 | int i; |
| 237 | SHA_INT32 clo; |
| 238 | |
| 239 | clo = sha_info->count_lo + ((SHA_INT32) count << 3); |
| 240 | if (clo < sha_info->count_lo) { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 241 | ++sha_info->count_hi; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 242 | } |
| 243 | sha_info->count_lo = clo; |
| 244 | sha_info->count_hi += (SHA_INT32) count >> 29; |
| 245 | if (sha_info->local) { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 246 | i = SHA_BLOCKSIZE - sha_info->local; |
| 247 | if (i > count) { |
| 248 | i = count; |
| 249 | } |
| 250 | memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
| 251 | count -= i; |
| 252 | buffer += i; |
| 253 | sha_info->local += i; |
| 254 | if (sha_info->local == SHA_BLOCKSIZE) { |
| 255 | sha_transform(sha_info); |
| 256 | } |
| 257 | else { |
| 258 | return; |
| 259 | } |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 260 | } |
| 261 | while (count >= SHA_BLOCKSIZE) { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 262 | memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); |
| 263 | buffer += SHA_BLOCKSIZE; |
| 264 | count -= SHA_BLOCKSIZE; |
| 265 | sha_transform(sha_info); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 266 | } |
| 267 | memcpy(sha_info->data, buffer, count); |
| 268 | sha_info->local = count; |
| 269 | } |
| 270 | |
| 271 | /* finish computing the SHA digest */ |
| 272 | |
| 273 | static void |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 274 | sha_final(unsigned char digest[20], SHAobject *sha_info) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 275 | { |
| 276 | int count; |
| 277 | SHA_INT32 lo_bit_count, hi_bit_count; |
| 278 | |
| 279 | lo_bit_count = sha_info->count_lo; |
| 280 | hi_bit_count = sha_info->count_hi; |
| 281 | count = (int) ((lo_bit_count >> 3) & 0x3f); |
| 282 | ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 283 | if (count > SHA_BLOCKSIZE - 8) { |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 284 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 285 | SHA_BLOCKSIZE - count); |
| 286 | sha_transform(sha_info); |
| 287 | memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8); |
| 288 | } |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 289 | else { |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 290 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 291 | SHA_BLOCKSIZE - 8 - count); |
| 292 | } |
| 293 | |
| 294 | /* GJS: note that we add the hi/lo in big-endian. sha_transform will |
| 295 | swap these values into host-order. */ |
| 296 | sha_info->data[56] = (hi_bit_count >> 24) & 0xff; |
| 297 | sha_info->data[57] = (hi_bit_count >> 16) & 0xff; |
| 298 | sha_info->data[58] = (hi_bit_count >> 8) & 0xff; |
| 299 | sha_info->data[59] = (hi_bit_count >> 0) & 0xff; |
| 300 | sha_info->data[60] = (lo_bit_count >> 24) & 0xff; |
| 301 | sha_info->data[61] = (lo_bit_count >> 16) & 0xff; |
| 302 | sha_info->data[62] = (lo_bit_count >> 8) & 0xff; |
| 303 | sha_info->data[63] = (lo_bit_count >> 0) & 0xff; |
| 304 | sha_transform(sha_info); |
| 305 | digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
| 306 | digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
| 307 | digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
| 308 | digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff); |
| 309 | digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
| 310 | digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
| 311 | digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
| 312 | digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff); |
| 313 | digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
| 314 | digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
| 315 | digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
| 316 | digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff); |
| 317 | digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
| 318 | digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
| 319 | digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
| 320 | digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff); |
| 321 | digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
| 322 | digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
| 323 | digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
| 324 | digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff); |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * End of copied SHA code. |
| 329 | * |
| 330 | * ------------------------------------------------------------------------ |
| 331 | */ |
| 332 | |
| 333 | staticforward PyTypeObject SHAtype; |
| 334 | |
| 335 | |
| 336 | static SHAobject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 337 | newSHAobject(void) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 338 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 339 | return (SHAobject *)PyObject_New(SHAobject, &SHAtype); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* Internal methods for a hashing object */ |
| 343 | |
| 344 | static void |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 345 | SHA_dealloc(PyObject *ptr) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 346 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 347 | PyObject_Del(ptr); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | /* External methods for a hashing object */ |
| 352 | |
| 353 | static char SHA_copy__doc__[] = |
| 354 | "Return a copy of the hashing object."; |
| 355 | |
| 356 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 357 | SHA_copy(SHAobject *self, PyObject *args) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 358 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 359 | SHAobject *newobj; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 360 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 361 | if (!PyArg_ParseTuple(args, ":copy")) { |
| 362 | return NULL; |
| 363 | } |
| 364 | if ( (newobj = newSHAobject())==NULL) |
| 365 | return NULL; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 366 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 367 | SHAcopy(self, newobj); |
| 368 | return (PyObject *)newobj; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static char SHA_digest__doc__[] = |
| 372 | "Return the digest value as a string of binary data."; |
| 373 | |
| 374 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 375 | SHA_digest(SHAobject *self, PyObject *args) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 376 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 377 | unsigned char digest[SHA_DIGESTSIZE]; |
| 378 | SHAobject temp; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 379 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 380 | if (!PyArg_ParseTuple(args, ":digest")) |
| 381 | return NULL; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 382 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 383 | SHAcopy(self, &temp); |
| 384 | sha_final(digest, &temp); |
| 385 | return PyString_FromStringAndSize((const char *)digest, sizeof(digest)); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static char SHA_hexdigest__doc__[] = |
| 389 | "Return the digest value as a string of hexadecimal digits."; |
| 390 | |
| 391 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 392 | SHA_hexdigest(SHAobject *self, PyObject *args) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 393 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 394 | unsigned char digest[SHA_DIGESTSIZE]; |
| 395 | SHAobject temp; |
| 396 | PyObject *retval; |
| 397 | char *hex_digest; |
| 398 | int i, j; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 399 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 400 | if (!PyArg_ParseTuple(args, ":hexdigest")) |
| 401 | return NULL; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 402 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 403 | /* Get the raw (binary) digest value */ |
| 404 | SHAcopy(self, &temp); |
| 405 | sha_final(digest, &temp); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 406 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 407 | /* Create a new string */ |
| 408 | retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2); |
Barry Warsaw | 57b808d | 2000-08-15 06:03:35 +0000 | [diff] [blame] | 409 | if (!retval) |
| 410 | return NULL; |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 411 | hex_digest = PyString_AsString(retval); |
Barry Warsaw | 57b808d | 2000-08-15 06:03:35 +0000 | [diff] [blame] | 412 | if (!hex_digest) { |
| 413 | Py_DECREF(retval); |
| 414 | return NULL; |
| 415 | } |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 416 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 417 | /* Make hex version of the digest */ |
| 418 | for(i=j=0; i<sizeof(digest); i++) { |
| 419 | char c; |
Barry Warsaw | 57b808d | 2000-08-15 06:03:35 +0000 | [diff] [blame] | 420 | c = (digest[i] >> 4) & 0xf; |
| 421 | c = (c>9) ? c+'a'-10 : c + '0'; |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 422 | hex_digest[j++] = c; |
Barry Warsaw | 57b808d | 2000-08-15 06:03:35 +0000 | [diff] [blame] | 423 | c = (digest[i] & 0xf); |
| 424 | c = (c>9) ? c+'a'-10 : c + '0'; |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 425 | hex_digest[j++] = c; |
| 426 | } |
| 427 | return retval; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static char SHA_update__doc__[] = |
| 431 | "Update this hashing object's state with the provided string."; |
| 432 | |
| 433 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 434 | SHA_update(SHAobject *self, PyObject *args) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 435 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 436 | unsigned char *cp; |
| 437 | int len; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 438 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 439 | if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) |
| 440 | return NULL; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 441 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 442 | sha_update(self, cp, len); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 443 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 444 | Py_INCREF(Py_None); |
| 445 | return Py_None; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static PyMethodDef SHA_methods[] = { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 449 | {"copy", (PyCFunction)SHA_copy, METH_VARARGS, SHA_copy__doc__}, |
| 450 | {"digest", (PyCFunction)SHA_digest, METH_VARARGS, SHA_digest__doc__}, |
| 451 | {"hexdigest", (PyCFunction)SHA_hexdigest, METH_VARARGS, SHA_hexdigest__doc__}, |
| 452 | {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__}, |
| 453 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 457 | SHA_getattr(PyObject *self, char *name) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 458 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 459 | if (strcmp(name, "blocksize")==0) |
| 460 | return PyInt_FromLong(1); |
| 461 | if (strcmp(name, "digestsize")==0) |
| 462 | return PyInt_FromLong(20); |
| 463 | |
| 464 | return Py_FindMethod(SHA_methods, self, name); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static PyTypeObject SHAtype = { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 468 | PyObject_HEAD_INIT(NULL) |
| 469 | 0, /*ob_size*/ |
| 470 | "SHA", /*tp_name*/ |
| 471 | sizeof(SHAobject), /*tp_size*/ |
| 472 | 0, /*tp_itemsize*/ |
| 473 | /* methods */ |
| 474 | SHA_dealloc, /*tp_dealloc*/ |
| 475 | 0, /*tp_print*/ |
| 476 | SHA_getattr, /*tp_getattr*/ |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | |
| 480 | /* The single module-level function: new() */ |
| 481 | |
| 482 | static char SHA_new__doc__[] = |
| 483 | "Return a new SHA hashing object. An optional string " |
| 484 | "argument may be provided; if present, this string will be " |
| 485 | " automatically hashed."; |
| 486 | |
| 487 | static PyObject * |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 488 | SHA_new(PyObject *self, PyObject *args, PyObject *kwdict) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 489 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 490 | static char *kwlist[] = {"string", NULL}; |
| 491 | SHAobject *new; |
| 492 | unsigned char *cp = NULL; |
| 493 | int len; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 494 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 495 | if ((new = newSHAobject()) == NULL) |
| 496 | return NULL; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 497 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 498 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, |
| 499 | &cp, &len)) { |
| 500 | Py_DECREF(new); |
| 501 | return NULL; |
| 502 | } |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 503 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 504 | sha_init(new); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 505 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 506 | if (PyErr_Occurred()) { |
| 507 | Py_DECREF(new); |
| 508 | return NULL; |
| 509 | } |
| 510 | if (cp) |
| 511 | sha_update(new, cp, len); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 512 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 513 | return (PyObject *)new; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | /* List of functions exported by this module */ |
| 518 | |
| 519 | static struct PyMethodDef SHA_functions[] = { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 520 | {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__}, |
| 521 | {"sha", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__}, |
| 522 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | |
| 526 | /* Initialize this module. */ |
| 527 | |
| 528 | #define insint(n,v) { PyObject *o=PyInt_FromLong(v); \ |
| 529 | if (o!=NULL) PyDict_SetItemString(d,n,o); \ |
| 530 | Py_XDECREF(o); } |
| 531 | |
| 532 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 533 | initsha(void) |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 534 | { |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 535 | PyObject *d, *m; |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 536 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 537 | SHAtype.ob_type = &PyType_Type; |
| 538 | m = Py_InitModule("sha", SHA_functions); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 539 | |
Fred Drake | 2c4a3dc | 2000-07-08 06:41:03 +0000 | [diff] [blame] | 540 | /* Add some symbolic constants to the module */ |
| 541 | d = PyModule_GetDict(m); |
| 542 | insint("blocksize", 1); /* For future use, in case some hash |
| 543 | functions require an integral number of |
| 544 | blocks */ |
| 545 | insint("digestsize", 20); |
Guido van Rossum | 29d2acc | 1999-03-24 19:03:59 +0000 | [diff] [blame] | 546 | } |