Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | /* SHA512 module */ |
| 2 | |
| 3 | /* This module provides an interface to NIST's SHA-512 and SHA-384 Algorithms */ |
| 4 | |
| 5 | /* See below for information about the original code this module was |
| 6 | based upon. Additional work performed by: |
| 7 | |
| 8 | Andrew Kuchling (amk@amk.ca) |
| 9 | Greg Stein (gstein@lyra.org) |
| 10 | Trevor Perrin (trevp@trevp.net) |
| 11 | |
Gregory P. Smith | f805785 | 2007-09-09 20:25:00 +0000 | [diff] [blame] | 12 | Copyright (C) 2005 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 13 | Licensed to PSF under a Contributor Agreement. |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | /* SHA objects */ |
| 18 | |
| 19 | #include "Python.h" |
| 20 | #include "structmember.h" |
| 21 | |
| 22 | #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */ |
| 23 | |
| 24 | /* Endianness testing and definitions */ |
| 25 | #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\ |
| 26 | if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;} |
| 27 | |
| 28 | #define PCT_LITTLE_ENDIAN 1 |
| 29 | #define PCT_BIG_ENDIAN 0 |
| 30 | |
| 31 | /* Some useful types */ |
| 32 | |
| 33 | typedef unsigned char SHA_BYTE; |
| 34 | |
| 35 | #if SIZEOF_INT == 4 |
| 36 | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
| 37 | typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */ |
| 38 | #else |
| 39 | /* not defined. compilation will die. */ |
| 40 | #endif |
| 41 | |
| 42 | /* The SHA block size and message digest sizes, in bytes */ |
| 43 | |
| 44 | #define SHA_BLOCKSIZE 128 |
| 45 | #define SHA_DIGESTSIZE 64 |
| 46 | |
| 47 | /* The structure for storing SHA info */ |
| 48 | |
| 49 | typedef struct { |
| 50 | PyObject_HEAD |
| 51 | SHA_INT64 digest[8]; /* Message digest */ |
| 52 | SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ |
| 53 | SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ |
| 54 | int Endianness; |
| 55 | int local; /* unprocessed amount in data */ |
| 56 | int digestsize; |
| 57 | } SHAobject; |
| 58 | |
| 59 | /* When run on a little-endian CPU we need to perform byte reversal on an |
| 60 | array of longwords. */ |
| 61 | |
| 62 | static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness) |
| 63 | { |
| 64 | SHA_INT64 value; |
| 65 | |
| 66 | if ( Endianness == PCT_BIG_ENDIAN ) |
| 67 | return; |
| 68 | |
| 69 | byteCount /= sizeof(*buffer); |
| 70 | while (byteCount--) { |
| 71 | value = *buffer; |
| 72 | |
| 73 | ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff; |
| 74 | ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff; |
| 75 | ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff; |
| 76 | ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff; |
| 77 | ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff; |
| 78 | ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff; |
| 79 | ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff; |
| 80 | ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff; |
| 81 | |
| 82 | buffer++; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void SHAcopy(SHAobject *src, SHAobject *dest) |
| 87 | { |
| 88 | dest->Endianness = src->Endianness; |
| 89 | dest->local = src->local; |
| 90 | dest->digestsize = src->digestsize; |
| 91 | dest->count_lo = src->count_lo; |
| 92 | dest->count_hi = src->count_hi; |
| 93 | memcpy(dest->digest, src->digest, sizeof(src->digest)); |
| 94 | memcpy(dest->data, src->data, sizeof(src->data)); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* ------------------------------------------------------------------------ |
| 99 | * |
| 100 | * This code for the SHA-512 algorithm was noted as public domain. The |
| 101 | * original headers are pasted below. |
| 102 | * |
| 103 | * Several changes have been made to make it more compatible with the |
| 104 | * Python environment and desired interface. |
| 105 | * |
| 106 | */ |
| 107 | |
| 108 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 109 | * |
| 110 | * LibTomCrypt is a library that provides various cryptographic |
| 111 | * algorithms in a highly modular and flexible manner. |
| 112 | * |
| 113 | * The library is free for all purposes without any express |
| 114 | * gurantee it works. |
| 115 | * |
| 116 | * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org |
| 117 | */ |
| 118 | |
| 119 | |
| 120 | /* SHA512 by Tom St Denis */ |
| 121 | |
| 122 | /* Various logical functions */ |
| 123 | #define ROR64(x, y) \ |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 124 | ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \ |
| 125 | ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
| 127 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
| 128 | #define S(x, n) ROR64((x),(n)) |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 129 | #define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 130 | #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) |
| 131 | #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) |
| 132 | #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) |
| 133 | #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) |
| 134 | |
| 135 | |
| 136 | static void |
| 137 | sha512_transform(SHAobject *sha_info) |
| 138 | { |
| 139 | int i; |
| 140 | SHA_INT64 S[8], W[80], t0, t1; |
| 141 | |
| 142 | memcpy(W, sha_info->data, sizeof(sha_info->data)); |
| 143 | longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness); |
| 144 | |
| 145 | for (i = 16; i < 80; ++i) { |
| 146 | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; |
| 147 | } |
| 148 | for (i = 0; i < 8; ++i) { |
| 149 | S[i] = sha_info->digest[i]; |
| 150 | } |
| 151 | |
| 152 | /* Compress */ |
| 153 | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
| 154 | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
| 155 | t1 = Sigma0(a) + Maj(a, b, c); \ |
| 156 | d += t0; \ |
| 157 | h = t0 + t1; |
| 158 | |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 159 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22)); |
| 160 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd)); |
| 161 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f)); |
| 162 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc)); |
| 163 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538)); |
| 164 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019)); |
| 165 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b)); |
| 166 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118)); |
| 167 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242)); |
| 168 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe)); |
| 169 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c)); |
| 170 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2)); |
| 171 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f)); |
| 172 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1)); |
| 173 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235)); |
| 174 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694)); |
| 175 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2)); |
| 176 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3)); |
| 177 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5)); |
| 178 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65)); |
| 179 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275)); |
| 180 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483)); |
| 181 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4)); |
| 182 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5)); |
| 183 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab)); |
| 184 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210)); |
| 185 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f)); |
| 186 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4)); |
| 187 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2)); |
| 188 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725)); |
| 189 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f)); |
| 190 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70)); |
| 191 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc)); |
| 192 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926)); |
| 193 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed)); |
| 194 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df)); |
| 195 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de)); |
| 196 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8)); |
| 197 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6)); |
| 198 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b)); |
| 199 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364)); |
| 200 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001)); |
| 201 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791)); |
| 202 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30)); |
| 203 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218)); |
| 204 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910)); |
| 205 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a)); |
| 206 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8)); |
| 207 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8)); |
| 208 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53)); |
| 209 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99)); |
| 210 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8)); |
| 211 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63)); |
| 212 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb)); |
| 213 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373)); |
| 214 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3)); |
| 215 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc)); |
| 216 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60)); |
| 217 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72)); |
| 218 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec)); |
| 219 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28)); |
| 220 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9)); |
| 221 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915)); |
| 222 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b)); |
| 223 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c)); |
| 224 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207)); |
| 225 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e)); |
| 226 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178)); |
| 227 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba)); |
| 228 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6)); |
| 229 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae)); |
| 230 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b)); |
| 231 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84)); |
| 232 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493)); |
| 233 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc)); |
| 234 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c)); |
| 235 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6)); |
| 236 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a)); |
| 237 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec)); |
| 238 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817)); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 239 | |
| 240 | #undef RND |
| 241 | |
| 242 | /* feedback */ |
| 243 | for (i = 0; i < 8; i++) { |
| 244 | sha_info->digest[i] = sha_info->digest[i] + S[i]; |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | /* initialize the SHA digest */ |
| 252 | |
| 253 | static void |
| 254 | sha512_init(SHAobject *sha_info) |
| 255 | { |
| 256 | TestEndianness(sha_info->Endianness) |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 257 | sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908); |
| 258 | sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b); |
| 259 | sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b); |
| 260 | sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1); |
| 261 | sha_info->digest[4] = Py_ULL(0x510e527fade682d1); |
| 262 | sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f); |
| 263 | sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b); |
| 264 | sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 265 | sha_info->count_lo = 0L; |
| 266 | sha_info->count_hi = 0L; |
| 267 | sha_info->local = 0; |
| 268 | sha_info->digestsize = 64; |
| 269 | } |
| 270 | |
| 271 | static void |
| 272 | sha384_init(SHAobject *sha_info) |
| 273 | { |
| 274 | TestEndianness(sha_info->Endianness) |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 275 | sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8); |
| 276 | sha_info->digest[1] = Py_ULL(0x629a292a367cd507); |
| 277 | sha_info->digest[2] = Py_ULL(0x9159015a3070dd17); |
| 278 | sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939); |
| 279 | sha_info->digest[4] = Py_ULL(0x67332667ffc00b31); |
| 280 | sha_info->digest[5] = Py_ULL(0x8eb44a8768581511); |
| 281 | sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7); |
| 282 | sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 283 | sha_info->count_lo = 0L; |
| 284 | sha_info->count_hi = 0L; |
| 285 | sha_info->local = 0; |
| 286 | sha_info->digestsize = 48; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* update the SHA digest */ |
| 291 | |
| 292 | static void |
| 293 | sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, int count) |
| 294 | { |
| 295 | int i; |
| 296 | SHA_INT32 clo; |
| 297 | |
| 298 | clo = sha_info->count_lo + ((SHA_INT32) count << 3); |
| 299 | if (clo < sha_info->count_lo) { |
| 300 | ++sha_info->count_hi; |
| 301 | } |
| 302 | sha_info->count_lo = clo; |
| 303 | sha_info->count_hi += (SHA_INT32) count >> 29; |
| 304 | if (sha_info->local) { |
| 305 | i = SHA_BLOCKSIZE - sha_info->local; |
| 306 | if (i > count) { |
| 307 | i = count; |
| 308 | } |
| 309 | memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
| 310 | count -= i; |
| 311 | buffer += i; |
| 312 | sha_info->local += i; |
| 313 | if (sha_info->local == SHA_BLOCKSIZE) { |
| 314 | sha512_transform(sha_info); |
| 315 | } |
| 316 | else { |
| 317 | return; |
| 318 | } |
| 319 | } |
| 320 | while (count >= SHA_BLOCKSIZE) { |
| 321 | memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); |
| 322 | buffer += SHA_BLOCKSIZE; |
| 323 | count -= SHA_BLOCKSIZE; |
| 324 | sha512_transform(sha_info); |
| 325 | } |
| 326 | memcpy(sha_info->data, buffer, count); |
| 327 | sha_info->local = count; |
| 328 | } |
| 329 | |
| 330 | /* finish computing the SHA digest */ |
| 331 | |
| 332 | static void |
| 333 | sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info) |
| 334 | { |
| 335 | int count; |
| 336 | SHA_INT32 lo_bit_count, hi_bit_count; |
| 337 | |
| 338 | lo_bit_count = sha_info->count_lo; |
| 339 | hi_bit_count = sha_info->count_hi; |
| 340 | count = (int) ((lo_bit_count >> 3) & 0x7f); |
| 341 | ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
| 342 | if (count > SHA_BLOCKSIZE - 16) { |
| 343 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 344 | SHA_BLOCKSIZE - count); |
| 345 | sha512_transform(sha_info); |
| 346 | memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16); |
| 347 | } |
| 348 | else { |
| 349 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 350 | SHA_BLOCKSIZE - 16 - count); |
| 351 | } |
| 352 | |
| 353 | /* GJS: note that we add the hi/lo in big-endian. sha512_transform will |
| 354 | swap these values into host-order. */ |
| 355 | sha_info->data[112] = 0; |
| 356 | sha_info->data[113] = 0; |
| 357 | sha_info->data[114] = 0; |
| 358 | sha_info->data[115] = 0; |
| 359 | sha_info->data[116] = 0; |
| 360 | sha_info->data[117] = 0; |
| 361 | sha_info->data[118] = 0; |
| 362 | sha_info->data[119] = 0; |
| 363 | sha_info->data[120] = (hi_bit_count >> 24) & 0xff; |
| 364 | sha_info->data[121] = (hi_bit_count >> 16) & 0xff; |
| 365 | sha_info->data[122] = (hi_bit_count >> 8) & 0xff; |
| 366 | sha_info->data[123] = (hi_bit_count >> 0) & 0xff; |
| 367 | sha_info->data[124] = (lo_bit_count >> 24) & 0xff; |
| 368 | sha_info->data[125] = (lo_bit_count >> 16) & 0xff; |
| 369 | sha_info->data[126] = (lo_bit_count >> 8) & 0xff; |
| 370 | sha_info->data[127] = (lo_bit_count >> 0) & 0xff; |
| 371 | sha512_transform(sha_info); |
| 372 | digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff); |
| 373 | digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff); |
| 374 | digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff); |
| 375 | digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff); |
| 376 | digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
| 377 | digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
| 378 | digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
| 379 | digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff); |
| 380 | digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff); |
| 381 | digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff); |
| 382 | digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff); |
| 383 | digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff); |
| 384 | digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
| 385 | digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
| 386 | digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
| 387 | digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff); |
| 388 | digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff); |
| 389 | digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff); |
| 390 | digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff); |
| 391 | digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff); |
| 392 | digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
| 393 | digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
| 394 | digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
| 395 | digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff); |
| 396 | digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff); |
| 397 | digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff); |
| 398 | digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff); |
| 399 | digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff); |
| 400 | digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
| 401 | digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
| 402 | digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
| 403 | digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff); |
| 404 | digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff); |
| 405 | digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff); |
| 406 | digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff); |
| 407 | digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff); |
| 408 | digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
| 409 | digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
| 410 | digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
| 411 | digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff); |
| 412 | digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff); |
| 413 | digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff); |
| 414 | digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff); |
| 415 | digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff); |
| 416 | digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff); |
| 417 | digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff); |
| 418 | digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff); |
| 419 | digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff); |
| 420 | digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff); |
| 421 | digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff); |
| 422 | digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff); |
| 423 | digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff); |
| 424 | digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff); |
| 425 | digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff); |
| 426 | digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff); |
| 427 | digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff); |
| 428 | digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff); |
| 429 | digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff); |
| 430 | digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff); |
| 431 | digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff); |
| 432 | digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff); |
| 433 | digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff); |
| 434 | digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff); |
| 435 | digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * End of copied SHA code. |
| 440 | * |
| 441 | * ------------------------------------------------------------------------ |
| 442 | */ |
| 443 | |
| 444 | static PyTypeObject SHA384type; |
| 445 | static PyTypeObject SHA512type; |
| 446 | |
| 447 | |
| 448 | static SHAobject * |
| 449 | newSHA384object(void) |
| 450 | { |
| 451 | return (SHAobject *)PyObject_New(SHAobject, &SHA384type); |
| 452 | } |
| 453 | |
| 454 | static SHAobject * |
| 455 | newSHA512object(void) |
| 456 | { |
| 457 | return (SHAobject *)PyObject_New(SHAobject, &SHA512type); |
| 458 | } |
| 459 | |
| 460 | /* Internal methods for a hash object */ |
| 461 | |
| 462 | static void |
| 463 | SHA512_dealloc(PyObject *ptr) |
| 464 | { |
| 465 | PyObject_Del(ptr); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* External methods for a hash object */ |
| 470 | |
| 471 | PyDoc_STRVAR(SHA512_copy__doc__, "Return a copy of the hash object."); |
| 472 | |
| 473 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 474 | SHA512_copy(SHAobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 475 | { |
| 476 | SHAobject *newobj; |
| 477 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 478 | if (((PyObject*)self)->ob_type == &SHA512type) { |
| 479 | if ( (newobj = newSHA512object())==NULL) |
| 480 | return NULL; |
| 481 | } else { |
| 482 | if ( (newobj = newSHA384object())==NULL) |
| 483 | return NULL; |
| 484 | } |
| 485 | |
| 486 | SHAcopy(self, newobj); |
| 487 | return (PyObject *)newobj; |
| 488 | } |
| 489 | |
| 490 | PyDoc_STRVAR(SHA512_digest__doc__, |
| 491 | "Return the digest value as a string of binary data."); |
| 492 | |
| 493 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 494 | SHA512_digest(SHAobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 495 | { |
| 496 | unsigned char digest[SHA_DIGESTSIZE]; |
| 497 | SHAobject temp; |
| 498 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 499 | SHAcopy(self, &temp); |
| 500 | sha512_final(digest, &temp); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 501 | return PyString_FromStringAndSize((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | PyDoc_STRVAR(SHA512_hexdigest__doc__, |
| 505 | "Return the digest value as a string of hexadecimal digits."); |
| 506 | |
| 507 | static PyObject * |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 508 | SHA512_hexdigest(SHAobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 509 | { |
| 510 | unsigned char digest[SHA_DIGESTSIZE]; |
| 511 | SHAobject temp; |
| 512 | PyObject *retval; |
| 513 | char *hex_digest; |
| 514 | int i, j; |
| 515 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 516 | /* Get the raw (binary) digest value */ |
| 517 | SHAcopy(self, &temp); |
| 518 | sha512_final(digest, &temp); |
| 519 | |
| 520 | /* Create a new string */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 521 | retval = PyString_FromStringAndSize(NULL, self->digestsize * 2); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 522 | if (!retval) |
| 523 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 524 | hex_digest = PyString_AsString(retval); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 525 | if (!hex_digest) { |
| 526 | Py_DECREF(retval); |
| 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | /* Make hex version of the digest */ |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 531 | for (i=j=0; i<self->digestsize; i++) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 532 | char c; |
| 533 | c = (digest[i] >> 4) & 0xf; |
| 534 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 535 | hex_digest[j++] = c; |
| 536 | c = (digest[i] & 0xf); |
| 537 | c = (c>9) ? c+'a'-10 : c + '0'; |
| 538 | hex_digest[j++] = c; |
| 539 | } |
| 540 | return retval; |
| 541 | } |
| 542 | |
| 543 | PyDoc_STRVAR(SHA512_update__doc__, |
| 544 | "Update this hash object's state with the provided string."); |
| 545 | |
| 546 | static PyObject * |
| 547 | SHA512_update(SHAobject *self, PyObject *args) |
| 548 | { |
| 549 | unsigned char *cp; |
| 550 | int len; |
| 551 | |
| 552 | if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) |
| 553 | return NULL; |
| 554 | |
| 555 | sha512_update(self, cp, len); |
| 556 | |
| 557 | Py_INCREF(Py_None); |
| 558 | return Py_None; |
| 559 | } |
| 560 | |
| 561 | static PyMethodDef SHA_methods[] = { |
Georg Brandl | 96a8c39 | 2006-05-29 21:04:52 +0000 | [diff] [blame] | 562 | {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__}, |
| 563 | {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__}, |
| 564 | {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 565 | {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__}, |
| 566 | {NULL, NULL} /* sentinel */ |
| 567 | }; |
| 568 | |
| 569 | static PyObject * |
| 570 | SHA512_get_block_size(PyObject *self, void *closure) |
| 571 | { |
| 572 | return PyInt_FromLong(SHA_BLOCKSIZE); |
| 573 | } |
| 574 | |
| 575 | static PyObject * |
| 576 | SHA512_get_name(PyObject *self, void *closure) |
| 577 | { |
| 578 | if (((SHAobject *)self)->digestsize == 64) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 579 | return PyString_FromStringAndSize("SHA512", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 580 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 581 | return PyString_FromStringAndSize("SHA384", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static PyGetSetDef SHA_getseters[] = { |
| 585 | {"block_size", |
| 586 | (getter)SHA512_get_block_size, NULL, |
| 587 | NULL, |
| 588 | NULL}, |
| 589 | {"name", |
| 590 | (getter)SHA512_get_name, NULL, |
| 591 | NULL, |
| 592 | NULL}, |
| 593 | {NULL} /* Sentinel */ |
| 594 | }; |
| 595 | |
| 596 | static PyMemberDef SHA_members[] = { |
| 597 | {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, |
| 598 | /* the old md5 and sha modules support 'digest_size' as in PEP 247. |
| 599 | * the old sha module also supported 'digestsize'. ugh. */ |
| 600 | {"digestsize", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, |
| 601 | {NULL} /* Sentinel */ |
| 602 | }; |
| 603 | |
| 604 | static PyTypeObject SHA384type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 605 | PyVarObject_HEAD_INIT(NULL, 0) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 606 | "_sha512.sha384", /*tp_name*/ |
| 607 | sizeof(SHAobject), /*tp_size*/ |
| 608 | 0, /*tp_itemsize*/ |
| 609 | /* methods */ |
| 610 | SHA512_dealloc, /*tp_dealloc*/ |
| 611 | 0, /*tp_print*/ |
| 612 | 0, /*tp_getattr*/ |
| 613 | 0, /*tp_setattr*/ |
| 614 | 0, /*tp_compare*/ |
| 615 | 0, /*tp_repr*/ |
| 616 | 0, /*tp_as_number*/ |
| 617 | 0, /*tp_as_sequence*/ |
| 618 | 0, /*tp_as_mapping*/ |
| 619 | 0, /*tp_hash*/ |
| 620 | 0, /*tp_call*/ |
| 621 | 0, /*tp_str*/ |
| 622 | 0, /*tp_getattro*/ |
| 623 | 0, /*tp_setattro*/ |
| 624 | 0, /*tp_as_buffer*/ |
| 625 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 626 | 0, /*tp_doc*/ |
| 627 | 0, /*tp_traverse*/ |
| 628 | 0, /*tp_clear*/ |
| 629 | 0, /*tp_richcompare*/ |
| 630 | 0, /*tp_weaklistoffset*/ |
| 631 | 0, /*tp_iter*/ |
| 632 | 0, /*tp_iternext*/ |
| 633 | SHA_methods, /* tp_methods */ |
| 634 | SHA_members, /* tp_members */ |
| 635 | SHA_getseters, /* tp_getset */ |
| 636 | }; |
| 637 | |
| 638 | static PyTypeObject SHA512type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 639 | PyVarObject_HEAD_INIT(NULL, 0) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 640 | "_sha512.sha512", /*tp_name*/ |
| 641 | sizeof(SHAobject), /*tp_size*/ |
| 642 | 0, /*tp_itemsize*/ |
| 643 | /* methods */ |
| 644 | SHA512_dealloc, /*tp_dealloc*/ |
| 645 | 0, /*tp_print*/ |
| 646 | 0, /*tp_getattr*/ |
| 647 | 0, /*tp_setattr*/ |
| 648 | 0, /*tp_compare*/ |
| 649 | 0, /*tp_repr*/ |
| 650 | 0, /*tp_as_number*/ |
| 651 | 0, /*tp_as_sequence*/ |
| 652 | 0, /*tp_as_mapping*/ |
| 653 | 0, /*tp_hash*/ |
| 654 | 0, /*tp_call*/ |
| 655 | 0, /*tp_str*/ |
| 656 | 0, /*tp_getattro*/ |
| 657 | 0, /*tp_setattro*/ |
| 658 | 0, /*tp_as_buffer*/ |
| 659 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 660 | 0, /*tp_doc*/ |
| 661 | 0, /*tp_traverse*/ |
| 662 | 0, /*tp_clear*/ |
| 663 | 0, /*tp_richcompare*/ |
| 664 | 0, /*tp_weaklistoffset*/ |
| 665 | 0, /*tp_iter*/ |
| 666 | 0, /*tp_iternext*/ |
| 667 | SHA_methods, /* tp_methods */ |
| 668 | SHA_members, /* tp_members */ |
| 669 | SHA_getseters, /* tp_getset */ |
| 670 | }; |
| 671 | |
| 672 | |
| 673 | /* The single module-level function: new() */ |
| 674 | |
| 675 | PyDoc_STRVAR(SHA512_new__doc__, |
| 676 | "Return a new SHA-512 hash object; optionally initialized with a string."); |
| 677 | |
| 678 | static PyObject * |
| 679 | SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict) |
| 680 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 681 | static char *kwlist[] = {"string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 682 | SHAobject *new; |
| 683 | unsigned char *cp = NULL; |
| 684 | int len; |
| 685 | |
| 686 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, |
| 687 | &cp, &len)) { |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | if ((new = newSHA512object()) == NULL) |
| 692 | return NULL; |
| 693 | |
| 694 | sha512_init(new); |
| 695 | |
| 696 | if (PyErr_Occurred()) { |
| 697 | Py_DECREF(new); |
| 698 | return NULL; |
| 699 | } |
| 700 | if (cp) |
| 701 | sha512_update(new, cp, len); |
| 702 | |
| 703 | return (PyObject *)new; |
| 704 | } |
| 705 | |
| 706 | PyDoc_STRVAR(SHA384_new__doc__, |
| 707 | "Return a new SHA-384 hash object; optionally initialized with a string."); |
| 708 | |
| 709 | static PyObject * |
| 710 | SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict) |
| 711 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 712 | static char *kwlist[] = {"string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 713 | SHAobject *new; |
| 714 | unsigned char *cp = NULL; |
| 715 | int len; |
| 716 | |
| 717 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, |
| 718 | &cp, &len)) { |
| 719 | return NULL; |
| 720 | } |
| 721 | |
| 722 | if ((new = newSHA384object()) == NULL) |
| 723 | return NULL; |
| 724 | |
| 725 | sha384_init(new); |
| 726 | |
| 727 | if (PyErr_Occurred()) { |
| 728 | Py_DECREF(new); |
| 729 | return NULL; |
| 730 | } |
| 731 | if (cp) |
| 732 | sha512_update(new, cp, len); |
| 733 | |
| 734 | return (PyObject *)new; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /* List of functions exported by this module */ |
| 739 | |
| 740 | static struct PyMethodDef SHA_functions[] = { |
| 741 | {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__}, |
| 742 | {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__}, |
| 743 | {NULL, NULL} /* Sentinel */ |
| 744 | }; |
| 745 | |
| 746 | |
| 747 | /* Initialize this module. */ |
| 748 | |
| 749 | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
| 750 | |
| 751 | PyMODINIT_FUNC |
| 752 | init_sha512(void) |
| 753 | { |
| 754 | PyObject *m; |
| 755 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 756 | Py_TYPE(&SHA384type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 757 | if (PyType_Ready(&SHA384type) < 0) |
| 758 | return; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 759 | Py_TYPE(&SHA512type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 760 | if (PyType_Ready(&SHA512type) < 0) |
| 761 | return; |
| 762 | m = Py_InitModule("_sha512", SHA_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 763 | if (m == NULL) |
| 764 | return; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | #endif |