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 | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 12 | Copyright (C) 2005-2007 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" |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 21 | #include "hashlib.h" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 22 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 23 | /*[clinic input] |
| 24 | module _sha512 |
| 25 | class SHA512Type "SHAobject *" "&PyType_Type" |
| 26 | [clinic start generated code]*/ |
| 27 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/ |
| 28 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 29 | #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */ |
| 30 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 31 | /* Some useful types */ |
| 32 | |
| 33 | typedef unsigned char SHA_BYTE; |
| 34 | |
| 35 | #if SIZEOF_INT == 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
| 37 | typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | int local; /* unprocessed amount in data */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 55 | int digestsize; |
| 56 | } SHAobject; |
| 57 | |
| 58 | /* When run on a little-endian CPU we need to perform byte reversal on an |
| 59 | array of longwords. */ |
| 60 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 61 | #if PY_LITTLE_ENDIAN |
| 62 | static void longReverse(SHA_INT64 *buffer, int byteCount) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 63 | { |
| 64 | SHA_INT64 value; |
| 65 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 66 | byteCount /= sizeof(*buffer); |
| 67 | while (byteCount--) { |
| 68 | value = *buffer; |
| 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff; |
| 71 | ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff; |
| 72 | ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff; |
| 73 | ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff; |
| 74 | ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff; |
| 75 | ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff; |
| 76 | ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff; |
| 77 | ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff; |
| 78 | |
| 79 | buffer++; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 82 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 83 | |
| 84 | static void SHAcopy(SHAobject *src, SHAobject *dest) |
| 85 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 86 | dest->local = src->local; |
| 87 | dest->digestsize = src->digestsize; |
| 88 | dest->count_lo = src->count_lo; |
| 89 | dest->count_hi = src->count_hi; |
| 90 | memcpy(dest->digest, src->digest, sizeof(src->digest)); |
| 91 | memcpy(dest->data, src->data, sizeof(src->data)); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* ------------------------------------------------------------------------ |
| 96 | * |
| 97 | * This code for the SHA-512 algorithm was noted as public domain. The |
| 98 | * original headers are pasted below. |
| 99 | * |
| 100 | * Several changes have been made to make it more compatible with the |
| 101 | * Python environment and desired interface. |
| 102 | * |
| 103 | */ |
| 104 | |
| 105 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 106 | * |
| 107 | * LibTomCrypt is a library that provides various cryptographic |
| 108 | * algorithms in a highly modular and flexible manner. |
| 109 | * |
| 110 | * The library is free for all purposes without any express |
| 111 | * gurantee it works. |
| 112 | * |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 113 | * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 114 | */ |
| 115 | |
| 116 | |
| 117 | /* SHA512 by Tom St Denis */ |
| 118 | |
| 119 | /* Various logical functions */ |
| 120 | #define ROR64(x, y) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 121 | ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \ |
| 122 | ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 123 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 125 | #define S(x, n) ROR64((x),(n)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 126 | #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] | 127 | #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) |
| 128 | #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) |
| 129 | #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) |
| 130 | #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) |
| 131 | |
| 132 | |
| 133 | static void |
| 134 | sha512_transform(SHAobject *sha_info) |
| 135 | { |
| 136 | int i; |
| 137 | SHA_INT64 S[8], W[80], t0, t1; |
| 138 | |
| 139 | memcpy(W, sha_info->data, sizeof(sha_info->data)); |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 140 | #if PY_LITTLE_ENDIAN |
| 141 | longReverse(W, (int)sizeof(sha_info->data)); |
| 142 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 143 | |
| 144 | for (i = 16; i < 80; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 146 | } |
| 147 | for (i = 0; i < 8; ++i) { |
| 148 | S[i] = sha_info->digest[i]; |
| 149 | } |
| 150 | |
| 151 | /* Compress */ |
| 152 | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
| 153 | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
| 154 | t1 = Sigma0(a) + Maj(a, b, c); \ |
| 155 | d += t0; \ |
| 156 | h = t0 + t1; |
| 157 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 158 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22)); |
| 159 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd)); |
| 160 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f)); |
| 161 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc)); |
| 162 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538)); |
| 163 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019)); |
| 164 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b)); |
| 165 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118)); |
| 166 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242)); |
| 167 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe)); |
| 168 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c)); |
| 169 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2)); |
| 170 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f)); |
| 171 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1)); |
| 172 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235)); |
| 173 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694)); |
| 174 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2)); |
| 175 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3)); |
| 176 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5)); |
| 177 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65)); |
| 178 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275)); |
| 179 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483)); |
| 180 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4)); |
| 181 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5)); |
| 182 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab)); |
| 183 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210)); |
| 184 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f)); |
| 185 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4)); |
| 186 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2)); |
| 187 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725)); |
| 188 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f)); |
| 189 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70)); |
| 190 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc)); |
| 191 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926)); |
| 192 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed)); |
| 193 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df)); |
| 194 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de)); |
| 195 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8)); |
| 196 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6)); |
| 197 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b)); |
| 198 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364)); |
| 199 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001)); |
| 200 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791)); |
| 201 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30)); |
| 202 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218)); |
| 203 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910)); |
| 204 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a)); |
| 205 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8)); |
| 206 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8)); |
| 207 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53)); |
| 208 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99)); |
| 209 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8)); |
| 210 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63)); |
| 211 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb)); |
| 212 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373)); |
| 213 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3)); |
| 214 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc)); |
| 215 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60)); |
| 216 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72)); |
| 217 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec)); |
| 218 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28)); |
| 219 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9)); |
| 220 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915)); |
| 221 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b)); |
| 222 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c)); |
| 223 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207)); |
| 224 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e)); |
| 225 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178)); |
| 226 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba)); |
| 227 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6)); |
| 228 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae)); |
| 229 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b)); |
| 230 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84)); |
| 231 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493)); |
| 232 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc)); |
| 233 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c)); |
| 234 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6)); |
| 235 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a)); |
| 236 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec)); |
| 237 | 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] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | #undef RND |
| 240 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 241 | /* feedback */ |
| 242 | for (i = 0; i < 8; i++) { |
| 243 | sha_info->digest[i] = sha_info->digest[i] + S[i]; |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | /* initialize the SHA digest */ |
| 251 | |
| 252 | static void |
| 253 | sha512_init(SHAobject *sha_info) |
| 254 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 255 | sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908); |
| 256 | sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b); |
| 257 | sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b); |
| 258 | sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1); |
| 259 | sha_info->digest[4] = Py_ULL(0x510e527fade682d1); |
| 260 | sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f); |
| 261 | sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b); |
| 262 | sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 263 | sha_info->count_lo = 0L; |
| 264 | sha_info->count_hi = 0L; |
| 265 | sha_info->local = 0; |
| 266 | sha_info->digestsize = 64; |
| 267 | } |
| 268 | |
| 269 | static void |
| 270 | sha384_init(SHAobject *sha_info) |
| 271 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8); |
| 273 | sha_info->digest[1] = Py_ULL(0x629a292a367cd507); |
| 274 | sha_info->digest[2] = Py_ULL(0x9159015a3070dd17); |
| 275 | sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939); |
| 276 | sha_info->digest[4] = Py_ULL(0x67332667ffc00b31); |
| 277 | sha_info->digest[5] = Py_ULL(0x8eb44a8768581511); |
| 278 | sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7); |
| 279 | sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 280 | sha_info->count_lo = 0L; |
| 281 | sha_info->count_hi = 0L; |
| 282 | sha_info->local = 0; |
| 283 | sha_info->digestsize = 48; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /* update the SHA digest */ |
| 288 | |
| 289 | static void |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 290 | sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 291 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 292 | Py_ssize_t i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 293 | SHA_INT32 clo; |
| 294 | |
| 295 | clo = sha_info->count_lo + ((SHA_INT32) count << 3); |
| 296 | if (clo < sha_info->count_lo) { |
| 297 | ++sha_info->count_hi; |
| 298 | } |
| 299 | sha_info->count_lo = clo; |
| 300 | sha_info->count_hi += (SHA_INT32) count >> 29; |
| 301 | if (sha_info->local) { |
| 302 | i = SHA_BLOCKSIZE - sha_info->local; |
| 303 | if (i > count) { |
| 304 | i = count; |
| 305 | } |
| 306 | memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
| 307 | count -= i; |
| 308 | buffer += i; |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 309 | sha_info->local += (int)i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 310 | if (sha_info->local == SHA_BLOCKSIZE) { |
| 311 | sha512_transform(sha_info); |
| 312 | } |
| 313 | else { |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | while (count >= SHA_BLOCKSIZE) { |
| 318 | memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); |
| 319 | buffer += SHA_BLOCKSIZE; |
| 320 | count -= SHA_BLOCKSIZE; |
| 321 | sha512_transform(sha_info); |
| 322 | } |
| 323 | memcpy(sha_info->data, buffer, count); |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 324 | sha_info->local = (int)count; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* finish computing the SHA digest */ |
| 328 | |
| 329 | static void |
| 330 | sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info) |
| 331 | { |
| 332 | int count; |
| 333 | SHA_INT32 lo_bit_count, hi_bit_count; |
| 334 | |
| 335 | lo_bit_count = sha_info->count_lo; |
| 336 | hi_bit_count = sha_info->count_hi; |
| 337 | count = (int) ((lo_bit_count >> 3) & 0x7f); |
| 338 | ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
| 339 | if (count > SHA_BLOCKSIZE - 16) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 341 | SHA_BLOCKSIZE - count); |
| 342 | sha512_transform(sha_info); |
| 343 | memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 344 | } |
| 345 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 347 | SHA_BLOCKSIZE - 16 - count); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* GJS: note that we add the hi/lo in big-endian. sha512_transform will |
| 351 | swap these values into host-order. */ |
| 352 | sha_info->data[112] = 0; |
| 353 | sha_info->data[113] = 0; |
| 354 | sha_info->data[114] = 0; |
| 355 | sha_info->data[115] = 0; |
| 356 | sha_info->data[116] = 0; |
| 357 | sha_info->data[117] = 0; |
| 358 | sha_info->data[118] = 0; |
| 359 | sha_info->data[119] = 0; |
| 360 | sha_info->data[120] = (hi_bit_count >> 24) & 0xff; |
| 361 | sha_info->data[121] = (hi_bit_count >> 16) & 0xff; |
| 362 | sha_info->data[122] = (hi_bit_count >> 8) & 0xff; |
| 363 | sha_info->data[123] = (hi_bit_count >> 0) & 0xff; |
| 364 | sha_info->data[124] = (lo_bit_count >> 24) & 0xff; |
| 365 | sha_info->data[125] = (lo_bit_count >> 16) & 0xff; |
| 366 | sha_info->data[126] = (lo_bit_count >> 8) & 0xff; |
| 367 | sha_info->data[127] = (lo_bit_count >> 0) & 0xff; |
| 368 | sha512_transform(sha_info); |
| 369 | digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff); |
| 370 | digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff); |
| 371 | digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff); |
| 372 | digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff); |
| 373 | digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
| 374 | digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
| 375 | digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
| 376 | digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff); |
| 377 | digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff); |
| 378 | digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff); |
| 379 | digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff); |
| 380 | digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff); |
| 381 | digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
| 382 | digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
| 383 | digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
| 384 | digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff); |
| 385 | digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff); |
| 386 | digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff); |
| 387 | digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff); |
| 388 | digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff); |
| 389 | digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
| 390 | digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
| 391 | digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
| 392 | digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff); |
| 393 | digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff); |
| 394 | digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff); |
| 395 | digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff); |
| 396 | digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff); |
| 397 | digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
| 398 | digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
| 399 | digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
| 400 | digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff); |
| 401 | digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff); |
| 402 | digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff); |
| 403 | digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff); |
| 404 | digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff); |
| 405 | digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
| 406 | digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
| 407 | digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
| 408 | digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff); |
| 409 | digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff); |
| 410 | digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff); |
| 411 | digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff); |
| 412 | digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff); |
| 413 | digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff); |
| 414 | digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff); |
| 415 | digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff); |
| 416 | digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff); |
| 417 | digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff); |
| 418 | digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff); |
| 419 | digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff); |
| 420 | digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff); |
| 421 | digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff); |
| 422 | digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff); |
| 423 | digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff); |
| 424 | digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff); |
| 425 | digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff); |
| 426 | digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff); |
| 427 | digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff); |
| 428 | digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff); |
| 429 | digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff); |
| 430 | digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff); |
| 431 | digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff); |
| 432 | digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * End of copied SHA code. |
| 437 | * |
| 438 | * ------------------------------------------------------------------------ |
| 439 | */ |
| 440 | |
| 441 | static PyTypeObject SHA384type; |
| 442 | static PyTypeObject SHA512type; |
| 443 | |
| 444 | |
| 445 | static SHAobject * |
| 446 | newSHA384object(void) |
| 447 | { |
| 448 | return (SHAobject *)PyObject_New(SHAobject, &SHA384type); |
| 449 | } |
| 450 | |
| 451 | static SHAobject * |
| 452 | newSHA512object(void) |
| 453 | { |
| 454 | return (SHAobject *)PyObject_New(SHAobject, &SHA512type); |
| 455 | } |
| 456 | |
| 457 | /* Internal methods for a hash object */ |
| 458 | |
| 459 | static void |
| 460 | SHA512_dealloc(PyObject *ptr) |
| 461 | { |
| 462 | PyObject_Del(ptr); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | /* External methods for a hash object */ |
| 467 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 468 | /*[clinic input] |
| 469 | SHA512Type.copy |
| 470 | |
| 471 | Return a copy of the hash object. |
| 472 | [clinic start generated code]*/ |
| 473 | |
| 474 | PyDoc_STRVAR(SHA512Type_copy__doc__, |
| 475 | "copy($self, /)\n" |
| 476 | "--\n" |
| 477 | "\n" |
| 478 | "Return a copy of the hash object."); |
| 479 | |
| 480 | #define SHA512TYPE_COPY_METHODDEF \ |
| 481 | {"copy", (PyCFunction)SHA512Type_copy, METH_NOARGS, SHA512Type_copy__doc__}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 482 | |
| 483 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 484 | SHA512Type_copy_impl(SHAobject *self); |
| 485 | |
| 486 | static PyObject * |
| 487 | SHA512Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored)) |
| 488 | { |
| 489 | return SHA512Type_copy_impl(self); |
| 490 | } |
| 491 | |
| 492 | static PyObject * |
| 493 | SHA512Type_copy_impl(SHAobject *self) |
| 494 | /*[clinic end generated code: output=14f8e6ce9c61ece0 input=9f5f31e6c457776a]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 495 | { |
| 496 | SHAobject *newobj; |
| 497 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 498 | if (((PyObject*)self)->ob_type == &SHA512type) { |
| 499 | if ( (newobj = newSHA512object())==NULL) |
| 500 | return NULL; |
| 501 | } else { |
| 502 | if ( (newobj = newSHA384object())==NULL) |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | SHAcopy(self, newobj); |
| 507 | return (PyObject *)newobj; |
| 508 | } |
| 509 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 510 | /*[clinic input] |
| 511 | SHA512Type.digest |
| 512 | |
| 513 | Return the digest value as a string of binary data. |
| 514 | [clinic start generated code]*/ |
| 515 | |
| 516 | PyDoc_STRVAR(SHA512Type_digest__doc__, |
| 517 | "digest($self, /)\n" |
| 518 | "--\n" |
| 519 | "\n" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 520 | "Return the digest value as a string of binary data."); |
| 521 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 522 | #define SHA512TYPE_DIGEST_METHODDEF \ |
| 523 | {"digest", (PyCFunction)SHA512Type_digest, METH_NOARGS, SHA512Type_digest__doc__}, |
| 524 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 525 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 526 | SHA512Type_digest_impl(SHAobject *self); |
| 527 | |
| 528 | static PyObject * |
| 529 | SHA512Type_digest(SHAobject *self, PyObject *Py_UNUSED(ignored)) |
| 530 | { |
| 531 | return SHA512Type_digest_impl(self); |
| 532 | } |
| 533 | |
| 534 | static PyObject * |
| 535 | SHA512Type_digest_impl(SHAobject *self) |
| 536 | /*[clinic end generated code: output=be8de024b232977e input=60c2cede9e023018]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 537 | { |
| 538 | unsigned char digest[SHA_DIGESTSIZE]; |
| 539 | SHAobject temp; |
| 540 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 541 | SHAcopy(self, &temp); |
| 542 | sha512_final(digest, &temp); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 543 | return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 546 | /*[clinic input] |
| 547 | SHA512Type.hexdigest |
| 548 | |
| 549 | Return the digest value as a string of hexadecimal digits. |
| 550 | [clinic start generated code]*/ |
| 551 | |
| 552 | PyDoc_STRVAR(SHA512Type_hexdigest__doc__, |
| 553 | "hexdigest($self, /)\n" |
| 554 | "--\n" |
| 555 | "\n" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 556 | "Return the digest value as a string of hexadecimal digits."); |
| 557 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 558 | #define SHA512TYPE_HEXDIGEST_METHODDEF \ |
| 559 | {"hexdigest", (PyCFunction)SHA512Type_hexdigest, METH_NOARGS, SHA512Type_hexdigest__doc__}, |
| 560 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 561 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 562 | SHA512Type_hexdigest_impl(SHAobject *self); |
| 563 | |
| 564 | static PyObject * |
| 565 | SHA512Type_hexdigest(SHAobject *self, PyObject *Py_UNUSED(ignored)) |
| 566 | { |
| 567 | return SHA512Type_hexdigest_impl(self); |
| 568 | } |
| 569 | |
| 570 | static PyObject * |
| 571 | SHA512Type_hexdigest_impl(SHAobject *self) |
| 572 | /*[clinic end generated code: output=28a4ab2f9a1781b8 input=498b877b25cbe0a2]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 573 | { |
| 574 | unsigned char digest[SHA_DIGESTSIZE]; |
| 575 | SHAobject temp; |
| 576 | PyObject *retval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 577 | Py_UCS1 *hex_digest; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 578 | int i, j; |
| 579 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 580 | /* Get the raw (binary) digest value */ |
| 581 | SHAcopy(self, &temp); |
| 582 | sha512_final(digest, &temp); |
| 583 | |
| 584 | /* Create a new string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 585 | retval = PyUnicode_New(self->digestsize * 2, 127); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 586 | if (!retval) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 588 | hex_digest = PyUnicode_1BYTE_DATA(retval); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 589 | |
| 590 | /* Make hex version of the digest */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 591 | for (i=j=0; i<self->digestsize; i++) { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 592 | unsigned char c; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 593 | c = (digest[i] >> 4) & 0xf; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 594 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 595 | c = (digest[i] & 0xf); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 596 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 597 | } |
Christian Heimes | f402e92 | 2013-01-03 09:21:55 +0100 | [diff] [blame] | 598 | #ifdef Py_DEBUG |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 599 | assert(_PyUnicode_CheckConsistency(retval, 1)); |
Christian Heimes | f402e92 | 2013-01-03 09:21:55 +0100 | [diff] [blame] | 600 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 601 | return retval; |
| 602 | } |
| 603 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 604 | /*[clinic input] |
| 605 | SHA512Type.update |
| 606 | |
| 607 | obj: object |
| 608 | / |
| 609 | |
| 610 | Update this hash object's state with the provided string. |
| 611 | [clinic start generated code]*/ |
| 612 | |
| 613 | PyDoc_STRVAR(SHA512Type_update__doc__, |
| 614 | "update($self, obj, /)\n" |
| 615 | "--\n" |
| 616 | "\n" |
| 617 | "Update this hash object\'s state with the provided string."); |
| 618 | |
| 619 | #define SHA512TYPE_UPDATE_METHODDEF \ |
| 620 | {"update", (PyCFunction)SHA512Type_update, METH_O, SHA512Type_update__doc__}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 621 | |
| 622 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 623 | SHA512Type_update(SHAobject *self, PyObject *obj) |
| 624 | /*[clinic end generated code: output=6be574cdc3a9c52d input=ded2b46656566283]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 625 | { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 626 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 627 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 628 | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 629 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 630 | sha512_update(self, buf.buf, buf.len); |
| 631 | |
| 632 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 633 | Py_INCREF(Py_None); |
| 634 | return Py_None; |
| 635 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 636 | /*[clinic input] |
| 637 | dump buffer |
| 638 | [clinic start generated code]*/ |
| 639 | |
| 640 | #ifndef SHA512TYPE_COPY_METHODDEF |
| 641 | #define SHA512TYPE_COPY_METHODDEF |
| 642 | #endif /* !defined(SHA512TYPE_COPY_METHODDEF) */ |
| 643 | |
| 644 | #ifndef SHA512TYPE_DIGEST_METHODDEF |
| 645 | #define SHA512TYPE_DIGEST_METHODDEF |
| 646 | #endif /* !defined(SHA512TYPE_DIGEST_METHODDEF) */ |
| 647 | |
| 648 | #ifndef SHA512TYPE_HEXDIGEST_METHODDEF |
| 649 | #define SHA512TYPE_HEXDIGEST_METHODDEF |
| 650 | #endif /* !defined(SHA512TYPE_HEXDIGEST_METHODDEF) */ |
| 651 | |
| 652 | #ifndef SHA512TYPE_UPDATE_METHODDEF |
| 653 | #define SHA512TYPE_UPDATE_METHODDEF |
| 654 | #endif /* !defined(SHA512TYPE_UPDATE_METHODDEF) */ |
| 655 | /*[clinic end generated code: output=de713947d31130e9 input=524ce2e021e4eba6]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 656 | |
| 657 | static PyMethodDef SHA_methods[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 658 | SHA512TYPE_COPY_METHODDEF |
| 659 | SHA512TYPE_DIGEST_METHODDEF |
| 660 | SHA512TYPE_HEXDIGEST_METHODDEF |
| 661 | SHA512TYPE_UPDATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 663 | }; |
| 664 | |
| 665 | static PyObject * |
| 666 | SHA512_get_block_size(PyObject *self, void *closure) |
| 667 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 668 | return PyLong_FromLong(SHA_BLOCKSIZE); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static PyObject * |
| 672 | SHA512_get_name(PyObject *self, void *closure) |
| 673 | { |
| 674 | if (((SHAobject *)self)->digestsize == 64) |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 675 | return PyUnicode_FromStringAndSize("sha512", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 676 | else |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 677 | return PyUnicode_FromStringAndSize("sha384", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static PyGetSetDef SHA_getseters[] = { |
| 681 | {"block_size", |
| 682 | (getter)SHA512_get_block_size, NULL, |
| 683 | NULL, |
| 684 | NULL}, |
| 685 | {"name", |
| 686 | (getter)SHA512_get_name, NULL, |
| 687 | NULL, |
| 688 | NULL}, |
| 689 | {NULL} /* Sentinel */ |
| 690 | }; |
| 691 | |
| 692 | static PyMemberDef SHA_members[] = { |
| 693 | {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 694 | {NULL} /* Sentinel */ |
| 695 | }; |
| 696 | |
| 697 | static PyTypeObject SHA384type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 698 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | "_sha512.sha384", /*tp_name*/ |
| 700 | sizeof(SHAobject), /*tp_size*/ |
| 701 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 702 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | SHA512_dealloc, /*tp_dealloc*/ |
| 704 | 0, /*tp_print*/ |
| 705 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 706 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 707 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 708 | 0, /*tp_repr*/ |
| 709 | 0, /*tp_as_number*/ |
| 710 | 0, /*tp_as_sequence*/ |
| 711 | 0, /*tp_as_mapping*/ |
| 712 | 0, /*tp_hash*/ |
| 713 | 0, /*tp_call*/ |
| 714 | 0, /*tp_str*/ |
| 715 | 0, /*tp_getattro*/ |
| 716 | 0, /*tp_setattro*/ |
| 717 | 0, /*tp_as_buffer*/ |
| 718 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 719 | 0, /*tp_doc*/ |
| 720 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | 0, /*tp_clear*/ |
| 722 | 0, /*tp_richcompare*/ |
| 723 | 0, /*tp_weaklistoffset*/ |
| 724 | 0, /*tp_iter*/ |
| 725 | 0, /*tp_iternext*/ |
| 726 | SHA_methods, /* tp_methods */ |
| 727 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 728 | SHA_getseters, /* tp_getset */ |
| 729 | }; |
| 730 | |
| 731 | static PyTypeObject SHA512type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 732 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | "_sha512.sha512", /*tp_name*/ |
| 734 | sizeof(SHAobject), /*tp_size*/ |
| 735 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 736 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | SHA512_dealloc, /*tp_dealloc*/ |
| 738 | 0, /*tp_print*/ |
| 739 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 740 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 741 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 742 | 0, /*tp_repr*/ |
| 743 | 0, /*tp_as_number*/ |
| 744 | 0, /*tp_as_sequence*/ |
| 745 | 0, /*tp_as_mapping*/ |
| 746 | 0, /*tp_hash*/ |
| 747 | 0, /*tp_call*/ |
| 748 | 0, /*tp_str*/ |
| 749 | 0, /*tp_getattro*/ |
| 750 | 0, /*tp_setattro*/ |
| 751 | 0, /*tp_as_buffer*/ |
| 752 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 753 | 0, /*tp_doc*/ |
| 754 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | 0, /*tp_clear*/ |
| 756 | 0, /*tp_richcompare*/ |
| 757 | 0, /*tp_weaklistoffset*/ |
| 758 | 0, /*tp_iter*/ |
| 759 | 0, /*tp_iternext*/ |
| 760 | SHA_methods, /* tp_methods */ |
| 761 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 762 | SHA_getseters, /* tp_getset */ |
| 763 | }; |
| 764 | |
| 765 | |
| 766 | /* The single module-level function: new() */ |
| 767 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 768 | /*[clinic input] |
| 769 | _sha512.sha512 |
| 770 | |
| 771 | string: object(c_default="NULL") = b'' |
| 772 | |
| 773 | Return a new SHA-512 hash object; optionally initialized with a string. |
| 774 | [clinic start generated code]*/ |
| 775 | |
| 776 | PyDoc_STRVAR(_sha512_sha512__doc__, |
| 777 | "sha512($module, /, string=b\'\')\n" |
| 778 | "--\n" |
| 779 | "\n" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 780 | "Return a new SHA-512 hash object; optionally initialized with a string."); |
| 781 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 782 | #define _SHA512_SHA512_METHODDEF \ |
| 783 | {"sha512", (PyCFunction)_sha512_sha512, METH_VARARGS|METH_KEYWORDS, _sha512_sha512__doc__}, |
| 784 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 785 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 786 | _sha512_sha512_impl(PyModuleDef *module, PyObject *string); |
| 787 | |
| 788 | static PyObject * |
| 789 | _sha512_sha512(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 790 | { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 791 | PyObject *return_value = NULL; |
| 792 | static char *_keywords[] = {"string", NULL}; |
| 793 | PyObject *string = NULL; |
| 794 | |
| 795 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 796 | "|O:sha512", _keywords, |
| 797 | &string)) |
| 798 | goto exit; |
| 799 | return_value = _sha512_sha512_impl(module, string); |
| 800 | |
| 801 | exit: |
| 802 | return return_value; |
| 803 | } |
| 804 | |
| 805 | static PyObject * |
| 806 | _sha512_sha512_impl(PyModuleDef *module, PyObject *string) |
| 807 | /*[clinic end generated code: output=9e39b11115f36878 input=e69bad9ae9b6a308]*/ |
| 808 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 809 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 810 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 811 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 812 | if (string) |
| 813 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 814 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 815 | if ((new = newSHA512object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 816 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 817 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 818 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 819 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 820 | |
| 821 | sha512_init(new); |
| 822 | |
| 823 | if (PyErr_Occurred()) { |
| 824 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 825 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 826 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 827 | return NULL; |
| 828 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 829 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 830 | sha512_update(new, buf.buf, buf.len); |
| 831 | PyBuffer_Release(&buf); |
| 832 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 833 | |
| 834 | return (PyObject *)new; |
| 835 | } |
| 836 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 837 | /*[clinic input] |
| 838 | _sha512.sha384 |
| 839 | |
| 840 | string: object(c_default="NULL") = b'' |
| 841 | |
| 842 | Return a new SHA-384 hash object; optionally initialized with a string. |
| 843 | [clinic start generated code]*/ |
| 844 | |
| 845 | PyDoc_STRVAR(_sha512_sha384__doc__, |
| 846 | "sha384($module, /, string=b\'\')\n" |
| 847 | "--\n" |
| 848 | "\n" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 849 | "Return a new SHA-384 hash object; optionally initialized with a string."); |
| 850 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 851 | #define _SHA512_SHA384_METHODDEF \ |
| 852 | {"sha384", (PyCFunction)_sha512_sha384, METH_VARARGS|METH_KEYWORDS, _sha512_sha384__doc__}, |
| 853 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 854 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 855 | _sha512_sha384_impl(PyModuleDef *module, PyObject *string); |
| 856 | |
| 857 | static PyObject * |
| 858 | _sha512_sha384(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 859 | { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 860 | PyObject *return_value = NULL; |
| 861 | static char *_keywords[] = {"string", NULL}; |
| 862 | PyObject *string = NULL; |
| 863 | |
| 864 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 865 | "|O:sha384", _keywords, |
| 866 | &string)) |
| 867 | goto exit; |
| 868 | return_value = _sha512_sha384_impl(module, string); |
| 869 | |
| 870 | exit: |
| 871 | return return_value; |
| 872 | } |
| 873 | |
| 874 | static PyObject * |
| 875 | _sha512_sha384_impl(PyModuleDef *module, PyObject *string) |
| 876 | /*[clinic end generated code: output=397c6fba3525b93a input=c9327788d4ea4545]*/ |
| 877 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 878 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 879 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 880 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 881 | if (string) |
| 882 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 883 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 884 | if ((new = newSHA384object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 885 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 886 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 887 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 888 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 889 | |
| 890 | sha384_init(new); |
| 891 | |
| 892 | if (PyErr_Occurred()) { |
| 893 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 894 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 895 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 896 | return NULL; |
| 897 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 898 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 899 | sha512_update(new, buf.buf, buf.len); |
| 900 | PyBuffer_Release(&buf); |
| 901 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 902 | |
| 903 | return (PyObject *)new; |
| 904 | } |
| 905 | |
| 906 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 907 | /*[clinic input] |
| 908 | dump buffer |
| 909 | [clinic start generated code]*/ |
| 910 | |
| 911 | #ifndef _SHA512_SHA512_METHODDEF |
| 912 | #define _SHA512_SHA512_METHODDEF |
| 913 | #endif /* !defined(_SHA512_SHA512_METHODDEF) */ |
| 914 | |
| 915 | #ifndef _SHA512_SHA384_METHODDEF |
| 916 | #define _SHA512_SHA384_METHODDEF |
| 917 | #endif /* !defined(_SHA512_SHA384_METHODDEF) */ |
| 918 | /*[clinic end generated code: output=69d84aa9445b01d8 input=524ce2e021e4eba6]*/ |
| 919 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 920 | /* List of functions exported by this module */ |
| 921 | |
| 922 | static struct PyMethodDef SHA_functions[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 923 | _SHA512_SHA512_METHODDEF |
| 924 | _SHA512_SHA384_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | |
| 929 | /* Initialize this module. */ |
| 930 | |
| 931 | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
| 932 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 933 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 934 | static struct PyModuleDef _sha512module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | PyModuleDef_HEAD_INIT, |
| 936 | "_sha512", |
| 937 | NULL, |
| 938 | -1, |
| 939 | SHA_functions, |
| 940 | NULL, |
| 941 | NULL, |
| 942 | NULL, |
| 943 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 944 | }; |
| 945 | |
| 946 | PyMODINIT_FUNC |
| 947 | PyInit__sha512(void) |
| 948 | { |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 949 | PyObject *m; |
| 950 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 951 | Py_TYPE(&SHA384type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 952 | if (PyType_Ready(&SHA384type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 953 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 954 | Py_TYPE(&SHA512type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 955 | if (PyType_Ready(&SHA512type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 956 | return NULL; |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 957 | |
| 958 | m = PyModule_Create(&_sha512module); |
| 959 | if (m == NULL) |
| 960 | return NULL; |
| 961 | |
| 962 | Py_INCREF((PyObject *)&SHA384type); |
| 963 | PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type); |
| 964 | Py_INCREF((PyObject *)&SHA512type); |
| 965 | PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type); |
| 966 | return m; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | #endif |