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 | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 22 | #include "pystrhex.h" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 23 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 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 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 30 | #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */ |
| 31 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 32 | /* Some useful types */ |
| 33 | |
| 34 | typedef unsigned char SHA_BYTE; |
| 35 | |
| 36 | #if SIZEOF_INT == 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
| 38 | typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 39 | #else |
| 40 | /* not defined. compilation will die. */ |
| 41 | #endif |
| 42 | |
| 43 | /* The SHA block size and message digest sizes, in bytes */ |
| 44 | |
| 45 | #define SHA_BLOCKSIZE 128 |
| 46 | #define SHA_DIGESTSIZE 64 |
| 47 | |
| 48 | /* The structure for storing SHA info */ |
| 49 | |
| 50 | typedef struct { |
| 51 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | SHA_INT64 digest[8]; /* Message digest */ |
| 53 | SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ |
| 54 | SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | int local; /* unprocessed amount in data */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 56 | int digestsize; |
| 57 | } SHAobject; |
| 58 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 59 | #include "clinic/sha512module.c.h" |
| 60 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 61 | /* When run on a little-endian CPU we need to perform byte reversal on an |
| 62 | array of longwords. */ |
| 63 | |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 64 | #if PY_LITTLE_ENDIAN |
| 65 | static void longReverse(SHA_INT64 *buffer, int byteCount) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 66 | { |
| 67 | SHA_INT64 value; |
| 68 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 69 | byteCount /= sizeof(*buffer); |
| 70 | while (byteCount--) { |
| 71 | value = *buffer; |
| 72 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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++; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 85 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 86 | |
| 87 | static void SHAcopy(SHAobject *src, SHAobject *dest) |
| 88 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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 | * |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 116 | * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 117 | */ |
| 118 | |
| 119 | |
| 120 | /* SHA512 by Tom St Denis */ |
| 121 | |
| 122 | /* Various logical functions */ |
| 123 | #define ROR64(x, y) \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | #define S(x, n) ROR64((x),(n)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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)); |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 143 | #if PY_LITTLE_ENDIAN |
| 144 | longReverse(W, (int)sizeof(sha_info->data)); |
| 145 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 146 | |
| 147 | for (i = 16; i < 80; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | 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] | 149 | } |
| 150 | for (i = 0; i < 8; ++i) { |
| 151 | S[i] = sha_info->digest[i]; |
| 152 | } |
| 153 | |
| 154 | /* Compress */ |
| 155 | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
| 156 | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
| 157 | t1 = Sigma0(a) + Maj(a, b, c); \ |
| 158 | d += t0; \ |
| 159 | h = t0 + t1; |
| 160 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 161 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22)); |
| 162 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd)); |
| 163 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f)); |
| 164 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc)); |
| 165 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538)); |
| 166 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019)); |
| 167 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b)); |
| 168 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118)); |
| 169 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242)); |
| 170 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe)); |
| 171 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c)); |
| 172 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2)); |
| 173 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f)); |
| 174 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1)); |
| 175 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235)); |
| 176 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694)); |
| 177 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2)); |
| 178 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3)); |
| 179 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5)); |
| 180 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65)); |
| 181 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275)); |
| 182 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483)); |
| 183 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4)); |
| 184 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5)); |
| 185 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab)); |
| 186 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210)); |
| 187 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f)); |
| 188 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4)); |
| 189 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2)); |
| 190 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725)); |
| 191 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f)); |
| 192 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70)); |
| 193 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc)); |
| 194 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926)); |
| 195 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed)); |
| 196 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df)); |
| 197 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de)); |
| 198 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8)); |
| 199 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6)); |
| 200 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b)); |
| 201 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364)); |
| 202 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001)); |
| 203 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791)); |
| 204 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30)); |
| 205 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218)); |
| 206 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910)); |
| 207 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a)); |
| 208 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8)); |
| 209 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8)); |
| 210 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53)); |
| 211 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99)); |
| 212 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8)); |
| 213 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63)); |
| 214 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb)); |
| 215 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373)); |
| 216 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3)); |
| 217 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc)); |
| 218 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60)); |
| 219 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72)); |
| 220 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec)); |
| 221 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28)); |
| 222 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9)); |
| 223 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915)); |
| 224 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b)); |
| 225 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c)); |
| 226 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207)); |
| 227 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e)); |
| 228 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178)); |
| 229 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba)); |
| 230 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6)); |
| 231 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae)); |
| 232 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b)); |
| 233 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84)); |
| 234 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493)); |
| 235 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc)); |
| 236 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c)); |
| 237 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6)); |
| 238 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a)); |
| 239 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec)); |
| 240 | 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] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | #undef RND |
| 243 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 244 | /* feedback */ |
| 245 | for (i = 0; i < 8; i++) { |
| 246 | sha_info->digest[i] = sha_info->digest[i] + S[i]; |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
| 253 | /* initialize the SHA digest */ |
| 254 | |
| 255 | static void |
| 256 | sha512_init(SHAobject *sha_info) |
| 257 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 258 | sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908); |
| 259 | sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b); |
| 260 | sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b); |
| 261 | sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1); |
| 262 | sha_info->digest[4] = Py_ULL(0x510e527fade682d1); |
| 263 | sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f); |
| 264 | sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b); |
| 265 | sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 266 | sha_info->count_lo = 0L; |
| 267 | sha_info->count_hi = 0L; |
| 268 | sha_info->local = 0; |
| 269 | sha_info->digestsize = 64; |
| 270 | } |
| 271 | |
| 272 | static void |
| 273 | sha384_init(SHAobject *sha_info) |
| 274 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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 |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 293 | 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] | 294 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 295 | Py_ssize_t i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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; |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 312 | sha_info->local += (int)i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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); |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 327 | sha_info->local = (int)count; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 347 | } |
| 348 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 350 | SHA_BLOCKSIZE - 16 - count); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 471 | /*[clinic input] |
| 472 | SHA512Type.copy |
| 473 | |
| 474 | Return a copy of the hash object. |
| 475 | [clinic start generated code]*/ |
| 476 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 477 | static PyObject * |
| 478 | SHA512Type_copy_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 479 | /*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 480 | { |
| 481 | SHAobject *newobj; |
| 482 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 483 | if (((PyObject*)self)->ob_type == &SHA512type) { |
| 484 | if ( (newobj = newSHA512object())==NULL) |
| 485 | return NULL; |
| 486 | } else { |
| 487 | if ( (newobj = newSHA384object())==NULL) |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | SHAcopy(self, newobj); |
| 492 | return (PyObject *)newobj; |
| 493 | } |
| 494 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 495 | /*[clinic input] |
| 496 | SHA512Type.digest |
| 497 | |
| 498 | Return the digest value as a string of binary data. |
| 499 | [clinic start generated code]*/ |
| 500 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 501 | static PyObject * |
| 502 | SHA512Type_digest_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 503 | /*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 504 | { |
| 505 | unsigned char digest[SHA_DIGESTSIZE]; |
| 506 | SHAobject temp; |
| 507 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 508 | SHAcopy(self, &temp); |
| 509 | sha512_final(digest, &temp); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 510 | return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 513 | /*[clinic input] |
| 514 | SHA512Type.hexdigest |
| 515 | |
| 516 | Return the digest value as a string of hexadecimal digits. |
| 517 | [clinic start generated code]*/ |
| 518 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 519 | static PyObject * |
| 520 | SHA512Type_hexdigest_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 521 | /*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 522 | { |
| 523 | unsigned char digest[SHA_DIGESTSIZE]; |
| 524 | SHAobject temp; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 525 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 526 | /* Get the raw (binary) digest value */ |
| 527 | SHAcopy(self, &temp); |
| 528 | sha512_final(digest, &temp); |
| 529 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 530 | return _Py_strhex((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 533 | /*[clinic input] |
| 534 | SHA512Type.update |
| 535 | |
| 536 | obj: object |
| 537 | / |
| 538 | |
| 539 | Update this hash object's state with the provided string. |
| 540 | [clinic start generated code]*/ |
| 541 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 542 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 543 | SHA512Type_update(SHAobject *self, PyObject *obj) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 544 | /*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 545 | { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 546 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 547 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 548 | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 549 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 550 | sha512_update(self, buf.buf, buf.len); |
| 551 | |
| 552 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 553 | Py_INCREF(Py_None); |
| 554 | return Py_None; |
| 555 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 556 | /*[clinic input] |
| 557 | dump buffer |
| 558 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 559 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 560 | |
| 561 | static PyMethodDef SHA_methods[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 562 | SHA512TYPE_COPY_METHODDEF |
| 563 | SHA512TYPE_DIGEST_METHODDEF |
| 564 | SHA512TYPE_HEXDIGEST_METHODDEF |
| 565 | SHA512TYPE_UPDATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | static PyObject * |
| 570 | SHA512_get_block_size(PyObject *self, void *closure) |
| 571 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 572 | return PyLong_FromLong(SHA_BLOCKSIZE); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | static PyObject * |
| 576 | SHA512_get_name(PyObject *self, void *closure) |
| 577 | { |
| 578 | if (((SHAobject *)self)->digestsize == 64) |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 579 | return PyUnicode_FromStringAndSize("sha512", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 580 | else |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 581 | return PyUnicode_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}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 598 | {NULL} /* Sentinel */ |
| 599 | }; |
| 600 | |
| 601 | static PyTypeObject SHA384type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 602 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | "_sha512.sha384", /*tp_name*/ |
| 604 | sizeof(SHAobject), /*tp_size*/ |
| 605 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 606 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | SHA512_dealloc, /*tp_dealloc*/ |
| 608 | 0, /*tp_print*/ |
| 609 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 610 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 611 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 612 | 0, /*tp_repr*/ |
| 613 | 0, /*tp_as_number*/ |
| 614 | 0, /*tp_as_sequence*/ |
| 615 | 0, /*tp_as_mapping*/ |
| 616 | 0, /*tp_hash*/ |
| 617 | 0, /*tp_call*/ |
| 618 | 0, /*tp_str*/ |
| 619 | 0, /*tp_getattro*/ |
| 620 | 0, /*tp_setattro*/ |
| 621 | 0, /*tp_as_buffer*/ |
| 622 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 623 | 0, /*tp_doc*/ |
| 624 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | 0, /*tp_clear*/ |
| 626 | 0, /*tp_richcompare*/ |
| 627 | 0, /*tp_weaklistoffset*/ |
| 628 | 0, /*tp_iter*/ |
| 629 | 0, /*tp_iternext*/ |
| 630 | SHA_methods, /* tp_methods */ |
| 631 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 632 | SHA_getseters, /* tp_getset */ |
| 633 | }; |
| 634 | |
| 635 | static PyTypeObject SHA512type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 636 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | "_sha512.sha512", /*tp_name*/ |
| 638 | sizeof(SHAobject), /*tp_size*/ |
| 639 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 640 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | SHA512_dealloc, /*tp_dealloc*/ |
| 642 | 0, /*tp_print*/ |
| 643 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 644 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 645 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 646 | 0, /*tp_repr*/ |
| 647 | 0, /*tp_as_number*/ |
| 648 | 0, /*tp_as_sequence*/ |
| 649 | 0, /*tp_as_mapping*/ |
| 650 | 0, /*tp_hash*/ |
| 651 | 0, /*tp_call*/ |
| 652 | 0, /*tp_str*/ |
| 653 | 0, /*tp_getattro*/ |
| 654 | 0, /*tp_setattro*/ |
| 655 | 0, /*tp_as_buffer*/ |
| 656 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 657 | 0, /*tp_doc*/ |
| 658 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | 0, /*tp_clear*/ |
| 660 | 0, /*tp_richcompare*/ |
| 661 | 0, /*tp_weaklistoffset*/ |
| 662 | 0, /*tp_iter*/ |
| 663 | 0, /*tp_iternext*/ |
| 664 | SHA_methods, /* tp_methods */ |
| 665 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 666 | SHA_getseters, /* tp_getset */ |
| 667 | }; |
| 668 | |
| 669 | |
| 670 | /* The single module-level function: new() */ |
| 671 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 672 | /*[clinic input] |
| 673 | _sha512.sha512 |
| 674 | |
| 675 | string: object(c_default="NULL") = b'' |
| 676 | |
| 677 | Return a new SHA-512 hash object; optionally initialized with a string. |
| 678 | [clinic start generated code]*/ |
| 679 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 680 | static PyObject * |
| 681 | _sha512_sha512_impl(PyModuleDef *module, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 682 | /*[clinic end generated code: output=da13bc0a94da6de3 input=e69bad9ae9b6a308]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 683 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 684 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 685 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 686 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 687 | if (string) |
| 688 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 689 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 690 | if ((new = newSHA512object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 691 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 692 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 693 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 694 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 695 | |
| 696 | sha512_init(new); |
| 697 | |
| 698 | if (PyErr_Occurred()) { |
| 699 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 700 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 701 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 702 | return NULL; |
| 703 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 704 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 705 | sha512_update(new, buf.buf, buf.len); |
| 706 | PyBuffer_Release(&buf); |
| 707 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 708 | |
| 709 | return (PyObject *)new; |
| 710 | } |
| 711 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 712 | /*[clinic input] |
| 713 | _sha512.sha384 |
| 714 | |
| 715 | string: object(c_default="NULL") = b'' |
| 716 | |
| 717 | Return a new SHA-384 hash object; optionally initialized with a string. |
| 718 | [clinic start generated code]*/ |
| 719 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 720 | static PyObject * |
| 721 | _sha512_sha384_impl(PyModuleDef *module, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 722 | /*[clinic end generated code: output=ac731aea5509174d input=c9327788d4ea4545]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 723 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 724 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 725 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 726 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 727 | if (string) |
| 728 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 729 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 730 | if ((new = newSHA384object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 731 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 732 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 733 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 734 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 735 | |
| 736 | sha384_init(new); |
| 737 | |
| 738 | if (PyErr_Occurred()) { |
| 739 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 740 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 741 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 742 | return NULL; |
| 743 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 744 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 745 | sha512_update(new, buf.buf, buf.len); |
| 746 | PyBuffer_Release(&buf); |
| 747 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 748 | |
| 749 | return (PyObject *)new; |
| 750 | } |
| 751 | |
| 752 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 753 | /*[clinic input] |
| 754 | dump buffer |
| 755 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 756 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 757 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 758 | /* List of functions exported by this module */ |
| 759 | |
| 760 | static struct PyMethodDef SHA_functions[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 761 | _SHA512_SHA512_METHODDEF |
| 762 | _SHA512_SHA384_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | |
| 767 | /* Initialize this module. */ |
| 768 | |
| 769 | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
| 770 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 771 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 772 | static struct PyModuleDef _sha512module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | PyModuleDef_HEAD_INIT, |
| 774 | "_sha512", |
| 775 | NULL, |
| 776 | -1, |
| 777 | SHA_functions, |
| 778 | NULL, |
| 779 | NULL, |
| 780 | NULL, |
| 781 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | PyMODINIT_FUNC |
| 785 | PyInit__sha512(void) |
| 786 | { |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 787 | PyObject *m; |
| 788 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 789 | Py_TYPE(&SHA384type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 790 | if (PyType_Ready(&SHA384type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 791 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 792 | Py_TYPE(&SHA512type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 793 | if (PyType_Ready(&SHA512type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 794 | return NULL; |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 795 | |
| 796 | m = PyModule_Create(&_sha512module); |
| 797 | if (m == NULL) |
| 798 | return NULL; |
| 799 | |
| 800 | Py_INCREF((PyObject *)&SHA384type); |
| 801 | PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type); |
| 802 | Py_INCREF((PyObject *)&SHA512type); |
| 803 | PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type); |
| 804 | return m; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | #endif |