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 12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) 13 Licensed to PSF under a Contributor Agreement. 14 15 */ 16 17 /* SHA objects */ 18 19 #include "Python.h" 20 #include "structmember.h" 21 #include "hashlib.h" 22 #include "pystrhex.h" 23 24 /*[clinic input] 25 module _sha512 26 class SHA512Type "SHAobject *" "&PyType_Type" 27 [clinic start generated code]*/ 28 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/ 29 30 /* Some useful types */ 31 32 typedef unsigned char SHA_BYTE; 33 34 #if SIZEOF_INT == 4 35 typedef unsigned int SHA_INT32; /* 32-bit integer */ 36 typedef unsigned long long SHA_INT64; /* 64-bit integer */ 37 #else 38 /* not defined. compilation will die. */ 39 #endif 40 41 /* The SHA block size and message digest sizes, in bytes */ 42 43 #define SHA_BLOCKSIZE 128 44 #define SHA_DIGESTSIZE 64 45 46 /* The structure for storing SHA info */ 47 48 typedef struct { 49 PyObject_HEAD 50 SHA_INT64 digest[8]; /* Message digest */ 51 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ 52 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ 53 int local; /* unprocessed amount in data */ 54 int digestsize; 55 } SHAobject; 56 57 #include "clinic/sha512module.c.h" 58 59 /* When run on a little-endian CPU we need to perform byte reversal on an 60 array of longwords. */ 61 62 #if PY_LITTLE_ENDIAN 63 static void longReverse(SHA_INT64 *buffer, int byteCount) 64 { 65 SHA_INT64 value; 66 67 byteCount /= sizeof(*buffer); 68 while (byteCount--) { 69 value = *buffer; 70 71 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff; 72 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff; 73 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff; 74 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff; 75 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff; 76 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff; 77 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff; 78 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff; 79 80 buffer++; 81 } 82 } 83 #endif 84 85 static void SHAcopy(SHAobject *src, SHAobject *dest) 86 { 87 dest->local = src->local; 88 dest->digestsize = src->digestsize; 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)); 93 } 94 95 96 /* ------------------------------------------------------------------------ 97 * 98 * This code for the SHA-512 algorithm was noted as public domain. The 99 * original 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 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 107 * 108 * LibTomCrypt is a library that provides various cryptographic 109 * algorithms in a highly modular and flexible manner. 110 * 111 * The library is free for all purposes without any express 112 * guarantee it works. 113 * 114 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org 115 */ 116 117 118 /* SHA512 by Tom St Denis */ 119 120 /* Various logical functions */ 121 #define ROR64(x, y) \ 122 ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned long long)(y) & 63)) | \ 123 ((x)<<((unsigned long long)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL) 124 #define Ch(x,y,z) (z ^ (x & (y ^ z))) 125 #define Maj(x,y,z) (((x | y) & z) | (x & y)) 126 #define S(x, n) ROR64((x),(n)) 127 #define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned long long)n)) 128 #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) 129 #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) 130 #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) 131 #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) 132 133 134 static void 135 sha512_transform(SHAobject *sha_info) 136 { 137 int i; 138 SHA_INT64 S[8], W[80], t0, t1; 139 140 memcpy(W, sha_info->data, sizeof(sha_info->data)); 141 #if PY_LITTLE_ENDIAN 142 longReverse(W, (int)sizeof(sha_info->data)); 143 #endif 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 159 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL); 160 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL); 161 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL); 162 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL); 163 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL); 164 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL); 165 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL); 166 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL); 167 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL); 168 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL); 169 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL); 170 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL); 171 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL); 172 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL); 173 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL); 174 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL); 175 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL); 176 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL); 177 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL); 178 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL); 179 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL); 180 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL); 181 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL); 182 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL); 183 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL); 184 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL); 185 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL); 186 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL); 187 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL); 188 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL); 189 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL); 190 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL); 191 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL); 192 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL); 193 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL); 194 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL); 195 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL); 196 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL); 197 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL); 198 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL); 199 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL); 200 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL); 201 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL); 202 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL); 203 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL); 204 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL); 205 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL); 206 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL); 207 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL); 208 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL); 209 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL); 210 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL); 211 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL); 212 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL); 213 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL); 214 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL); 215 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL); 216 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL); 217 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL); 218 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL); 219 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL); 220 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL); 221 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL); 222 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL); 223 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL); 224 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL); 225 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL); 226 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL); 227 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL); 228 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL); 229 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL); 230 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL); 231 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL); 232 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL); 233 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL); 234 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL); 235 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL); 236 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL); 237 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL); 238 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL); 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 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908); 257 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b); 258 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b); 259 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1); 260 sha_info->digest[4] = Py_ULL(0x510e527fade682d1); 261 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f); 262 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b); 263 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179); 264 sha_info->count_lo = 0L; 265 sha_info->count_hi = 0L; 266 sha_info->local = 0; 267 sha_info->digestsize = 64; 268 } 269 270 static void 271 sha384_init(SHAobject *sha_info) 272 { 273 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8); 274 sha_info->digest[1] = Py_ULL(0x629a292a367cd507); 275 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17); 276 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939); 277 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31); 278 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511); 279 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7); 280 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4); 281 sha_info->count_lo = 0L; 282 sha_info->count_hi = 0L; 283 sha_info->local = 0; 284 sha_info->digestsize = 48; 285 } 286 287 288 /* update the SHA digest */ 289 290 static void 291 sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count) 292 { 293 Py_ssize_t i; 294 SHA_INT32 clo; 295 296 clo = sha_info->count_lo + ((SHA_INT32) count << 3); 297 if (clo < sha_info->count_lo) { 298 ++sha_info->count_hi; 299 } 300 sha_info->count_lo = clo; 301 sha_info->count_hi += (SHA_INT32) count >> 29; 302 if (sha_info->local) { 303 i = SHA_BLOCKSIZE - sha_info->local; 304 if (i > count) { 305 i = count; 306 } 307 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); 308 count -= i; 309 buffer += i; 310 sha_info->local += (int)i; 311 if (sha_info->local == SHA_BLOCKSIZE) { 312 sha512_transform(sha_info); 313 } 314 else { 315 return; 316 } 317 } 318 while (count >= SHA_BLOCKSIZE) { 319 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); 320 buffer += SHA_BLOCKSIZE; 321 count -= SHA_BLOCKSIZE; 322 sha512_transform(sha_info); 323 } 324 memcpy(sha_info->data, buffer, count); 325 sha_info->local = (int)count; 326 } 327 328 /* finish computing the SHA digest */ 329 330 static void 331 sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info) 332 { 333 int count; 334 SHA_INT32 lo_bit_count, hi_bit_count; 335 336 lo_bit_count = sha_info->count_lo; 337 hi_bit_count = sha_info->count_hi; 338 count = (int) ((lo_bit_count >> 3) & 0x7f); 339 ((SHA_BYTE *) sha_info->data)[count++] = 0x80; 340 if (count > SHA_BLOCKSIZE - 16) { 341 memset(((SHA_BYTE *) sha_info->data) + count, 0, 342 SHA_BLOCKSIZE - count); 343 sha512_transform(sha_info); 344 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16); 345 } 346 else { 347 memset(((SHA_BYTE *) sha_info->data) + count, 0, 348 SHA_BLOCKSIZE - 16 - count); 349 } 350 351 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will 352 swap these values into host-order. */ 353 sha_info->data[112] = 0; 354 sha_info->data[113] = 0; 355 sha_info->data[114] = 0; 356 sha_info->data[115] = 0; 357 sha_info->data[116] = 0; 358 sha_info->data[117] = 0; 359 sha_info->data[118] = 0; 360 sha_info->data[119] = 0; 361 sha_info->data[120] = (hi_bit_count >> 24) & 0xff; 362 sha_info->data[121] = (hi_bit_count >> 16) & 0xff; 363 sha_info->data[122] = (hi_bit_count >> 8) & 0xff; 364 sha_info->data[123] = (hi_bit_count >> 0) & 0xff; 365 sha_info->data[124] = (lo_bit_count >> 24) & 0xff; 366 sha_info->data[125] = (lo_bit_count >> 16) & 0xff; 367 sha_info->data[126] = (lo_bit_count >> 8) & 0xff; 368 sha_info->data[127] = (lo_bit_count >> 0) & 0xff; 369 sha512_transform(sha_info); 370 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff); 371 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff); 372 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff); 373 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff); 374 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); 375 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); 376 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); 377 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff); 378 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff); 379 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff); 380 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff); 381 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff); 382 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); 383 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); 384 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); 385 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff); 386 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff); 387 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff); 388 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff); 389 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff); 390 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); 391 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); 392 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); 393 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff); 394 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff); 395 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff); 396 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff); 397 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff); 398 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); 399 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); 400 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); 401 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff); 402 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff); 403 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff); 404 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff); 405 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff); 406 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); 407 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); 408 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); 409 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff); 410 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff); 411 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff); 412 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff); 413 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff); 414 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff); 415 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff); 416 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff); 417 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff); 418 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff); 419 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff); 420 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff); 421 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff); 422 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff); 423 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff); 424 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff); 425 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff); 426 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff); 427 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff); 428 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff); 429 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff); 430 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff); 431 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff); 432 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff); 433 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff); 434 } 435 436 /* 437 * End of copied SHA code. 438 * 439 * ------------------------------------------------------------------------ 440 */ 441 442 static PyTypeObject SHA384type; 443 static PyTypeObject SHA512type; 444 445 446 static SHAobject * 447 newSHA384object(void) 448 { 449 return (SHAobject *)PyObject_New(SHAobject, &SHA384type); 450 } 451 452 static SHAobject * 453 newSHA512object(void) 454 { 455 return (SHAobject *)PyObject_New(SHAobject, &SHA512type); 456 } 457 458 /* Internal methods for a hash object */ 459 460 static void 461 SHA512_dealloc(PyObject *ptr) 462 { 463 PyObject_Del(ptr); 464 } 465 466 467 /* External methods for a hash object */ 468 469 /*[clinic input] 470 SHA512Type.copy 471 472 Return a copy of the hash object. 473 [clinic start generated code]*/ 474 475 static PyObject * 476 SHA512Type_copy_impl(SHAobject *self) 477 /*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/ 478 { 479 SHAobject *newobj; 480 481 if (((PyObject*)self)->ob_type == &SHA512type) { 482 if ( (newobj = newSHA512object())==NULL) 483 return NULL; 484 } else { 485 if ( (newobj = newSHA384object())==NULL) 486 return NULL; 487 } 488 489 SHAcopy(self, newobj); 490 return (PyObject *)newobj; 491 } 492 493 /*[clinic input] 494 SHA512Type.digest 495 496 Return the digest value as a string of binary data. 497 [clinic start generated code]*/ 498 499 static PyObject * 500 SHA512Type_digest_impl(SHAobject *self) 501 /*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/ 502 { 503 unsigned char digest[SHA_DIGESTSIZE]; 504 SHAobject temp; 505 506 SHAcopy(self, &temp); 507 sha512_final(digest, &temp); 508 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); 509 } 510 511 /*[clinic input] 512 SHA512Type.hexdigest 513 514 Return the digest value as a string of hexadecimal digits. 515 [clinic start generated code]*/ 516 517 static PyObject * 518 SHA512Type_hexdigest_impl(SHAobject *self) 519 /*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/ 520 { 521 unsigned char digest[SHA_DIGESTSIZE]; 522 SHAobject temp; 523 524 /* Get the raw (binary) digest value */ 525 SHAcopy(self, &temp); 526 sha512_final(digest, &temp); 527 528 return _Py_strhex((const char *)digest, self->digestsize); 529 } 530 531 /*[clinic input] 532 SHA512Type.update 533 534 obj: object 535 / 536 537 Update this hash object's state with the provided string. 538 [clinic start generated code]*/ 539 540 static PyObject * 541 SHA512Type_update(SHAobject *self, PyObject *obj) 542 /*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/ 543 { 544 Py_buffer buf; 545 546 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); 547 548 sha512_update(self, buf.buf, buf.len); 549 550 PyBuffer_Release(&buf); 551 Py_INCREF(Py_None); 552 return Py_None; 553 } 554 /*[clinic input] 555 dump buffer 556 [clinic start generated code]*/ 557 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ 558 559 static PyMethodDef SHA_methods[] = { 560 SHA512TYPE_COPY_METHODDEF 561 SHA512TYPE_DIGEST_METHODDEF 562 SHA512TYPE_HEXDIGEST_METHODDEF 563 SHA512TYPE_UPDATE_METHODDEF 564 {NULL, NULL} /* sentinel */ 565 }; 566 567 static PyObject * 568 SHA512_get_block_size(PyObject *self, void *closure) 569 { 570 return PyLong_FromLong(SHA_BLOCKSIZE); 571 } 572 573 static PyObject * 574 SHA512_get_name(PyObject *self, void *closure) 575 { 576 if (((SHAobject *)self)->digestsize == 64) 577 return PyUnicode_FromStringAndSize("sha512", 6); 578 else 579 return PyUnicode_FromStringAndSize("sha384", 6); 580 } 581 582 static PyGetSetDef SHA_getseters[] = { 583 {"block_size", 584 (getter)SHA512_get_block_size, NULL, 585 NULL, 586 NULL}, 587 {"name", 588 (getter)SHA512_get_name, NULL, 589 NULL, 590 NULL}, 591 {NULL} /* Sentinel */ 592 }; 593 594 static PyMemberDef SHA_members[] = { 595 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, 596 {NULL} /* Sentinel */ 597 }; 598 599 static PyTypeObject SHA384type = { 600 PyVarObject_HEAD_INIT(NULL, 0) 601 "_sha512.sha384", /*tp_name*/ 602 sizeof(SHAobject), /*tp_size*/ 603 0, /*tp_itemsize*/ 604 /* methods */ 605 SHA512_dealloc, /*tp_dealloc*/ 606 0, /*tp_print*/ 607 0, /*tp_getattr*/ 608 0, /*tp_setattr*/ 609 0, /*tp_reserved*/ 610 0, /*tp_repr*/ 611 0, /*tp_as_number*/ 612 0, /*tp_as_sequence*/ 613 0, /*tp_as_mapping*/ 614 0, /*tp_hash*/ 615 0, /*tp_call*/ 616 0, /*tp_str*/ 617 0, /*tp_getattro*/ 618 0, /*tp_setattro*/ 619 0, /*tp_as_buffer*/ 620 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 621 0, /*tp_doc*/ 622 0, /*tp_traverse*/ 623 0, /*tp_clear*/ 624 0, /*tp_richcompare*/ 625 0, /*tp_weaklistoffset*/ 626 0, /*tp_iter*/ 627 0, /*tp_iternext*/ 628 SHA_methods, /* tp_methods */ 629 SHA_members, /* tp_members */ 630 SHA_getseters, /* tp_getset */ 631 }; 632 633 static PyTypeObject SHA512type = { 634 PyVarObject_HEAD_INIT(NULL, 0) 635 "_sha512.sha512", /*tp_name*/ 636 sizeof(SHAobject), /*tp_size*/ 637 0, /*tp_itemsize*/ 638 /* methods */ 639 SHA512_dealloc, /*tp_dealloc*/ 640 0, /*tp_print*/ 641 0, /*tp_getattr*/ 642 0, /*tp_setattr*/ 643 0, /*tp_reserved*/ 644 0, /*tp_repr*/ 645 0, /*tp_as_number*/ 646 0, /*tp_as_sequence*/ 647 0, /*tp_as_mapping*/ 648 0, /*tp_hash*/ 649 0, /*tp_call*/ 650 0, /*tp_str*/ 651 0, /*tp_getattro*/ 652 0, /*tp_setattro*/ 653 0, /*tp_as_buffer*/ 654 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 655 0, /*tp_doc*/ 656 0, /*tp_traverse*/ 657 0, /*tp_clear*/ 658 0, /*tp_richcompare*/ 659 0, /*tp_weaklistoffset*/ 660 0, /*tp_iter*/ 661 0, /*tp_iternext*/ 662 SHA_methods, /* tp_methods */ 663 SHA_members, /* tp_members */ 664 SHA_getseters, /* tp_getset */ 665 }; 666 667 668 /* The single module-level function: new() */ 669 670 /*[clinic input] 671 _sha512.sha512 672 673 string: object(c_default="NULL") = b'' 674 675 Return a new SHA-512 hash object; optionally initialized with a string. 676 [clinic start generated code]*/ 677 678 static PyObject * 679 _sha512_sha512_impl(PyObject *module, PyObject *string) 680 /*[clinic end generated code: output=8b865a2df73bd387 input=e69bad9ae9b6a308]*/ 681 { 682 SHAobject *new; 683 Py_buffer buf; 684 685 if (string) 686 GET_BUFFER_VIEW_OR_ERROUT(string, &buf); 687 688 if ((new = newSHA512object()) == NULL) { 689 if (string) 690 PyBuffer_Release(&buf); 691 return NULL; 692 } 693 694 sha512_init(new); 695 696 if (PyErr_Occurred()) { 697 Py_DECREF(new); 698 if (string) 699 PyBuffer_Release(&buf); 700 return NULL; 701 } 702 if (string) { 703 sha512_update(new, buf.buf, buf.len); 704 PyBuffer_Release(&buf); 705 } 706 707 return (PyObject *)new; 708 } 709 710 /*[clinic input] 711 _sha512.sha384 712 713 string: object(c_default="NULL") = b'' 714 715 Return a new SHA-384 hash object; optionally initialized with a string. 716 [clinic start generated code]*/ 717 718 static PyObject * 719 _sha512_sha384_impl(PyObject *module, PyObject *string) 720 /*[clinic end generated code: output=ae4b2e26decf81e8 input=c9327788d4ea4545]*/ 721 { 722 SHAobject *new; 723 Py_buffer buf; 724 725 if (string) 726 GET_BUFFER_VIEW_OR_ERROUT(string, &buf); 727 728 if ((new = newSHA384object()) == NULL) { 729 if (string) 730 PyBuffer_Release(&buf); 731 return NULL; 732 } 733 734 sha384_init(new); 735 736 if (PyErr_Occurred()) { 737 Py_DECREF(new); 738 if (string) 739 PyBuffer_Release(&buf); 740 return NULL; 741 } 742 if (string) { 743 sha512_update(new, buf.buf, buf.len); 744 PyBuffer_Release(&buf); 745 } 746 747 return (PyObject *)new; 748 } 749 750 751 /*[clinic input] 752 dump buffer 753 [clinic start generated code]*/ 754 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ 755 756 /* List of functions exported by this module */ 757 758 static struct PyMethodDef SHA_functions[] = { 759 _SHA512_SHA512_METHODDEF 760 _SHA512_SHA384_METHODDEF 761 {NULL, NULL} /* Sentinel */ 762 }; 763 764 765 /* Initialize this module. */ 766 767 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } 768 769 770 static struct PyModuleDef _sha512module = { 771 PyModuleDef_HEAD_INIT, 772 "_sha512", 773 NULL, 774 -1, 775 SHA_functions, 776 NULL, 777 NULL, 778 NULL, 779 NULL 780 }; 781 782 PyMODINIT_FUNC 783 PyInit__sha512(void) 784 { 785 PyObject *m; 786 787 Py_TYPE(&SHA384type) = &PyType_Type; 788 if (PyType_Ready(&SHA384type) < 0) 789 return NULL; 790 Py_TYPE(&SHA512type) = &PyType_Type; 791 if (PyType_Ready(&SHA512type) < 0) 792 return NULL; 793 794 m = PyModule_Create(&_sha512module); 795 if (m == NULL) 796 return NULL; 797 798 Py_INCREF((PyObject *)&SHA384type); 799 PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type); 800 Py_INCREF((PyObject *)&SHA512type); 801 PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type); 802 return m; 803 } 804