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