Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | /* SHA256 module */ |
| 2 | |
| 3 | /* This module provides an interface to NIST's SHA-256 and SHA-224 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 _sha256 |
| 26 | class SHA256Type "SHAobject *" "&PyType_Type" |
| 27 | [clinic start generated code]*/ |
| 28 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 29 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 30 | /* Some useful types */ |
| 31 | |
| 32 | typedef unsigned char SHA_BYTE; |
| 33 | |
| 34 | #if SIZEOF_INT == 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 36 | #else |
| 37 | /* not defined. compilation will die. */ |
| 38 | #endif |
| 39 | |
| 40 | /* The SHA block size and message digest sizes, in bytes */ |
| 41 | |
| 42 | #define SHA_BLOCKSIZE 64 |
| 43 | #define SHA_DIGESTSIZE 32 |
| 44 | |
| 45 | /* The structure for storing SHA info */ |
| 46 | |
| 47 | typedef struct { |
| 48 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | SHA_INT32 digest[8]; /* Message digest */ |
| 50 | SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ |
| 51 | SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | int local; /* unprocessed amount in data */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 53 | int digestsize; |
| 54 | } SHAobject; |
| 55 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 56 | #include "clinic/sha256module.c.h" |
| 57 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 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_INT32 *buffer, int byteCount) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 63 | { |
| 64 | SHA_INT32 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 | value = ( ( value & 0xFF00FF00L ) >> 8 ) | \ |
| 70 | ( ( value & 0x00FF00FFL ) << 8 ); |
| 71 | *buffer++ = ( value << 16 ) | ( value >> 16 ); |
| 72 | } |
| 73 | } |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 74 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 75 | |
| 76 | static void SHAcopy(SHAobject *src, SHAobject *dest) |
| 77 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 78 | dest->local = src->local; |
| 79 | dest->digestsize = src->digestsize; |
| 80 | dest->count_lo = src->count_lo; |
| 81 | dest->count_hi = src->count_hi; |
| 82 | memcpy(dest->digest, src->digest, sizeof(src->digest)); |
| 83 | memcpy(dest->data, src->data, sizeof(src->data)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /* ------------------------------------------------------------------------ |
| 88 | * |
| 89 | * This code for the SHA-256 algorithm was noted as public domain. The |
| 90 | * original headers are pasted below. |
| 91 | * |
| 92 | * Several changes have been made to make it more compatible with the |
| 93 | * Python environment and desired interface. |
| 94 | * |
| 95 | */ |
| 96 | |
| 97 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 98 | * |
| 99 | * LibTomCrypt is a library that provides various cryptographic |
| 100 | * algorithms in a highly modular and flexible manner. |
| 101 | * |
| 102 | * The library is free for all purposes without any express |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 103 | * guarantee it works. |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 104 | * |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 105 | * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 106 | */ |
| 107 | |
| 108 | |
| 109 | /* SHA256 by Tom St Denis */ |
| 110 | |
| 111 | /* Various logical functions */ |
| 112 | #define ROR(x, y)\ |
| 113 | ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \ |
| 114 | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
| 115 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 117 | #define S(x, n) ROR((x),(n)) |
| 118 | #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) |
| 119 | #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) |
| 120 | #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) |
| 121 | #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) |
| 122 | #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) |
| 123 | |
| 124 | |
| 125 | static void |
| 126 | sha_transform(SHAobject *sha_info) |
| 127 | { |
| 128 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | SHA_INT32 S[8], W[64], t0, t1; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 130 | |
| 131 | memcpy(W, sha_info->data, sizeof(sha_info->data)); |
Christian Heimes | 743e0cd | 2012-10-17 23:52:17 +0200 | [diff] [blame] | 132 | #if PY_LITTLE_ENDIAN |
| 133 | longReverse(W, (int)sizeof(sha_info->data)); |
| 134 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 135 | |
| 136 | for (i = 16; i < 64; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | 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] | 138 | } |
| 139 | for (i = 0; i < 8; ++i) { |
| 140 | S[i] = sha_info->digest[i]; |
| 141 | } |
| 142 | |
| 143 | /* Compress */ |
| 144 | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
| 145 | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
| 146 | t1 = Sigma0(a) + Maj(a, b, c); \ |
| 147 | d += t0; \ |
| 148 | h = t0 + t1; |
| 149 | |
| 150 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); |
| 151 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); |
| 152 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); |
| 153 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); |
| 154 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); |
| 155 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); |
| 156 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); |
| 157 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); |
| 158 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); |
| 159 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); |
| 160 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); |
| 161 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); |
| 162 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); |
| 163 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); |
| 164 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); |
| 165 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); |
| 166 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); |
| 167 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); |
| 168 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); |
| 169 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); |
| 170 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); |
| 171 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); |
| 172 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); |
| 173 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); |
| 174 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); |
| 175 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); |
| 176 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); |
| 177 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); |
| 178 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); |
| 179 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); |
| 180 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); |
| 181 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); |
| 182 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); |
| 183 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); |
| 184 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); |
| 185 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); |
| 186 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); |
| 187 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); |
| 188 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); |
| 189 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); |
| 190 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); |
| 191 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); |
| 192 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); |
| 193 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); |
| 194 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); |
| 195 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); |
| 196 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); |
| 197 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); |
| 198 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); |
| 199 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); |
| 200 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); |
| 201 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); |
| 202 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); |
| 203 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); |
| 204 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); |
| 205 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); |
| 206 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); |
| 207 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); |
| 208 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); |
| 209 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); |
| 210 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); |
| 211 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); |
| 212 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); |
| 213 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); |
| 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | #undef RND |
| 216 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 217 | /* feedback */ |
| 218 | for (i = 0; i < 8; i++) { |
| 219 | sha_info->digest[i] = sha_info->digest[i] + S[i]; |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | /* initialize the SHA digest */ |
| 227 | |
| 228 | static void |
| 229 | sha_init(SHAobject *sha_info) |
| 230 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 231 | sha_info->digest[0] = 0x6A09E667L; |
| 232 | sha_info->digest[1] = 0xBB67AE85L; |
| 233 | sha_info->digest[2] = 0x3C6EF372L; |
| 234 | sha_info->digest[3] = 0xA54FF53AL; |
| 235 | sha_info->digest[4] = 0x510E527FL; |
| 236 | sha_info->digest[5] = 0x9B05688CL; |
| 237 | sha_info->digest[6] = 0x1F83D9ABL; |
| 238 | sha_info->digest[7] = 0x5BE0CD19L; |
| 239 | sha_info->count_lo = 0L; |
| 240 | sha_info->count_hi = 0L; |
| 241 | sha_info->local = 0; |
| 242 | sha_info->digestsize = 32; |
| 243 | } |
| 244 | |
| 245 | static void |
| 246 | sha224_init(SHAobject *sha_info) |
| 247 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 248 | sha_info->digest[0] = 0xc1059ed8L; |
| 249 | sha_info->digest[1] = 0x367cd507L; |
| 250 | sha_info->digest[2] = 0x3070dd17L; |
| 251 | sha_info->digest[3] = 0xf70e5939L; |
| 252 | sha_info->digest[4] = 0xffc00b31L; |
| 253 | sha_info->digest[5] = 0x68581511L; |
| 254 | sha_info->digest[6] = 0x64f98fa7L; |
| 255 | sha_info->digest[7] = 0xbefa4fa4L; |
| 256 | sha_info->count_lo = 0L; |
| 257 | sha_info->count_hi = 0L; |
| 258 | sha_info->local = 0; |
| 259 | sha_info->digestsize = 28; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* update the SHA digest */ |
| 264 | |
| 265 | static void |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 266 | sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 267 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 268 | Py_ssize_t i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 269 | SHA_INT32 clo; |
| 270 | |
| 271 | clo = sha_info->count_lo + ((SHA_INT32) count << 3); |
| 272 | if (clo < sha_info->count_lo) { |
| 273 | ++sha_info->count_hi; |
| 274 | } |
| 275 | sha_info->count_lo = clo; |
| 276 | sha_info->count_hi += (SHA_INT32) count >> 29; |
| 277 | if (sha_info->local) { |
| 278 | i = SHA_BLOCKSIZE - sha_info->local; |
| 279 | if (i > count) { |
| 280 | i = count; |
| 281 | } |
| 282 | memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
| 283 | count -= i; |
| 284 | buffer += i; |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 285 | sha_info->local += (int)i; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 286 | if (sha_info->local == SHA_BLOCKSIZE) { |
| 287 | sha_transform(sha_info); |
| 288 | } |
| 289 | else { |
| 290 | return; |
| 291 | } |
| 292 | } |
| 293 | while (count >= SHA_BLOCKSIZE) { |
| 294 | memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); |
| 295 | buffer += SHA_BLOCKSIZE; |
| 296 | count -= SHA_BLOCKSIZE; |
| 297 | sha_transform(sha_info); |
| 298 | } |
| 299 | memcpy(sha_info->data, buffer, count); |
Victor Stinner | 70792d2 | 2013-05-08 00:00:44 +0200 | [diff] [blame] | 300 | sha_info->local = (int)count; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* finish computing the SHA digest */ |
| 304 | |
| 305 | static void |
| 306 | sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info) |
| 307 | { |
| 308 | int count; |
| 309 | SHA_INT32 lo_bit_count, hi_bit_count; |
| 310 | |
| 311 | lo_bit_count = sha_info->count_lo; |
| 312 | hi_bit_count = sha_info->count_hi; |
| 313 | count = (int) ((lo_bit_count >> 3) & 0x3f); |
| 314 | ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
| 315 | if (count > SHA_BLOCKSIZE - 8) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 317 | SHA_BLOCKSIZE - count); |
| 318 | sha_transform(sha_info); |
| 319 | memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 320 | } |
| 321 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
| 323 | SHA_BLOCKSIZE - 8 - count); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* GJS: note that we add the hi/lo in big-endian. sha_transform will |
| 327 | swap these values into host-order. */ |
| 328 | sha_info->data[56] = (hi_bit_count >> 24) & 0xff; |
| 329 | sha_info->data[57] = (hi_bit_count >> 16) & 0xff; |
| 330 | sha_info->data[58] = (hi_bit_count >> 8) & 0xff; |
| 331 | sha_info->data[59] = (hi_bit_count >> 0) & 0xff; |
| 332 | sha_info->data[60] = (lo_bit_count >> 24) & 0xff; |
| 333 | sha_info->data[61] = (lo_bit_count >> 16) & 0xff; |
| 334 | sha_info->data[62] = (lo_bit_count >> 8) & 0xff; |
| 335 | sha_info->data[63] = (lo_bit_count >> 0) & 0xff; |
| 336 | sha_transform(sha_info); |
| 337 | digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
| 338 | digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
| 339 | digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
| 340 | digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff); |
| 341 | digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
| 342 | digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
| 343 | digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
| 344 | digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff); |
| 345 | digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
| 346 | digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
| 347 | digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
| 348 | digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff); |
| 349 | digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
| 350 | digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
| 351 | digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
| 352 | digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff); |
| 353 | digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
| 354 | digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
| 355 | digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
| 356 | digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff); |
| 357 | digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff); |
| 358 | digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff); |
| 359 | digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff); |
| 360 | digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff); |
| 361 | digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff); |
| 362 | digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff); |
| 363 | digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff); |
| 364 | digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff); |
| 365 | digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff); |
| 366 | digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff); |
| 367 | digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff); |
| 368 | digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * End of copied SHA code. |
| 373 | * |
| 374 | * ------------------------------------------------------------------------ |
| 375 | */ |
| 376 | |
| 377 | static PyTypeObject SHA224type; |
| 378 | static PyTypeObject SHA256type; |
| 379 | |
| 380 | |
| 381 | static SHAobject * |
| 382 | newSHA224object(void) |
| 383 | { |
| 384 | return (SHAobject *)PyObject_New(SHAobject, &SHA224type); |
| 385 | } |
| 386 | |
| 387 | static SHAobject * |
| 388 | newSHA256object(void) |
| 389 | { |
| 390 | return (SHAobject *)PyObject_New(SHAobject, &SHA256type); |
| 391 | } |
| 392 | |
| 393 | /* Internal methods for a hash object */ |
| 394 | |
| 395 | static void |
| 396 | SHA_dealloc(PyObject *ptr) |
| 397 | { |
| 398 | PyObject_Del(ptr); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /* External methods for a hash object */ |
| 403 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 404 | /*[clinic input] |
| 405 | SHA256Type.copy |
| 406 | |
| 407 | Return a copy of the hash object. |
| 408 | [clinic start generated code]*/ |
| 409 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 410 | static PyObject * |
| 411 | SHA256Type_copy_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 412 | /*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 413 | { |
| 414 | SHAobject *newobj; |
| 415 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 416 | if (Py_TYPE(self) == &SHA256type) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 417 | if ( (newobj = newSHA256object())==NULL) |
| 418 | return NULL; |
| 419 | } else { |
| 420 | if ( (newobj = newSHA224object())==NULL) |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | SHAcopy(self, newobj); |
| 425 | return (PyObject *)newobj; |
| 426 | } |
| 427 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 428 | /*[clinic input] |
| 429 | SHA256Type.digest |
| 430 | |
| 431 | Return the digest value as a string of binary data. |
| 432 | [clinic start generated code]*/ |
| 433 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 434 | static PyObject * |
| 435 | SHA256Type_digest_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 436 | /*[clinic end generated code: output=46616a5e909fbc3d input=1fb752e58954157d]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 437 | { |
| 438 | unsigned char digest[SHA_DIGESTSIZE]; |
| 439 | SHAobject temp; |
| 440 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 441 | SHAcopy(self, &temp); |
| 442 | sha_final(digest, &temp); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 443 | return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 446 | /*[clinic input] |
| 447 | SHA256Type.hexdigest |
| 448 | |
| 449 | Return the digest value as a string of hexadecimal digits. |
| 450 | [clinic start generated code]*/ |
| 451 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 452 | static PyObject * |
| 453 | SHA256Type_hexdigest_impl(SHAobject *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 454 | /*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 455 | { |
| 456 | unsigned char digest[SHA_DIGESTSIZE]; |
| 457 | SHAobject temp; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 458 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 459 | /* Get the raw (binary) digest value */ |
| 460 | SHAcopy(self, &temp); |
| 461 | sha_final(digest, &temp); |
| 462 | |
Gregory P. Smith | 8cb6569 | 2015-04-25 23:22:26 +0000 | [diff] [blame] | 463 | return _Py_strhex((const char *)digest, self->digestsize); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 466 | /*[clinic input] |
| 467 | SHA256Type.update |
| 468 | |
| 469 | obj: object |
| 470 | / |
| 471 | |
| 472 | Update this hash object's state with the provided string. |
| 473 | [clinic start generated code]*/ |
| 474 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 475 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 476 | SHA256Type_update(SHAobject *self, PyObject *obj) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 477 | /*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 478 | { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 479 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 480 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 481 | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 482 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 483 | sha_update(self, buf.buf, buf.len); |
| 484 | |
| 485 | PyBuffer_Release(&buf); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 486 | Py_RETURN_NONE; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static PyMethodDef SHA_methods[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 490 | SHA256TYPE_COPY_METHODDEF |
| 491 | SHA256TYPE_DIGEST_METHODDEF |
| 492 | SHA256TYPE_HEXDIGEST_METHODDEF |
| 493 | SHA256TYPE_UPDATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | static PyObject * |
| 498 | SHA256_get_block_size(PyObject *self, void *closure) |
| 499 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 500 | return PyLong_FromLong(SHA_BLOCKSIZE); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static PyObject * |
| 504 | SHA256_get_name(PyObject *self, void *closure) |
| 505 | { |
| 506 | if (((SHAobject *)self)->digestsize == 32) |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 507 | return PyUnicode_FromStringAndSize("sha256", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 508 | else |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 509 | return PyUnicode_FromStringAndSize("sha224", 6); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static PyGetSetDef SHA_getseters[] = { |
| 513 | {"block_size", |
| 514 | (getter)SHA256_get_block_size, NULL, |
| 515 | NULL, |
| 516 | NULL}, |
| 517 | {"name", |
| 518 | (getter)SHA256_get_name, NULL, |
| 519 | NULL, |
| 520 | NULL}, |
| 521 | {NULL} /* Sentinel */ |
| 522 | }; |
| 523 | |
| 524 | static PyMemberDef SHA_members[] = { |
| 525 | {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 526 | {NULL} /* Sentinel */ |
| 527 | }; |
| 528 | |
| 529 | static PyTypeObject SHA224type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 530 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | "_sha256.sha224", /*tp_name*/ |
| 532 | sizeof(SHAobject), /*tp_size*/ |
| 533 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 534 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | SHA_dealloc, /*tp_dealloc*/ |
| 536 | 0, /*tp_print*/ |
| 537 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 538 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 539 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 540 | 0, /*tp_repr*/ |
| 541 | 0, /*tp_as_number*/ |
| 542 | 0, /*tp_as_sequence*/ |
| 543 | 0, /*tp_as_mapping*/ |
| 544 | 0, /*tp_hash*/ |
| 545 | 0, /*tp_call*/ |
| 546 | 0, /*tp_str*/ |
| 547 | 0, /*tp_getattro*/ |
| 548 | 0, /*tp_setattro*/ |
| 549 | 0, /*tp_as_buffer*/ |
| 550 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 551 | 0, /*tp_doc*/ |
| 552 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | 0, /*tp_clear*/ |
| 554 | 0, /*tp_richcompare*/ |
| 555 | 0, /*tp_weaklistoffset*/ |
| 556 | 0, /*tp_iter*/ |
| 557 | 0, /*tp_iternext*/ |
| 558 | SHA_methods, /* tp_methods */ |
| 559 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 560 | SHA_getseters, /* tp_getset */ |
| 561 | }; |
| 562 | |
| 563 | static PyTypeObject SHA256type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 564 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | "_sha256.sha256", /*tp_name*/ |
| 566 | sizeof(SHAobject), /*tp_size*/ |
| 567 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 568 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | SHA_dealloc, /*tp_dealloc*/ |
| 570 | 0, /*tp_print*/ |
| 571 | 0, /*tp_getattr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 572 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 573 | 0, /*tp_reserved*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 574 | 0, /*tp_repr*/ |
| 575 | 0, /*tp_as_number*/ |
| 576 | 0, /*tp_as_sequence*/ |
| 577 | 0, /*tp_as_mapping*/ |
| 578 | 0, /*tp_hash*/ |
| 579 | 0, /*tp_call*/ |
| 580 | 0, /*tp_str*/ |
| 581 | 0, /*tp_getattro*/ |
| 582 | 0, /*tp_setattro*/ |
| 583 | 0, /*tp_as_buffer*/ |
| 584 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 585 | 0, /*tp_doc*/ |
| 586 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | 0, /*tp_clear*/ |
| 588 | 0, /*tp_richcompare*/ |
| 589 | 0, /*tp_weaklistoffset*/ |
| 590 | 0, /*tp_iter*/ |
| 591 | 0, /*tp_iternext*/ |
| 592 | SHA_methods, /* tp_methods */ |
| 593 | SHA_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 594 | SHA_getseters, /* tp_getset */ |
| 595 | }; |
| 596 | |
| 597 | |
| 598 | /* The single module-level function: new() */ |
| 599 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 600 | /*[clinic input] |
| 601 | _sha256.sha256 |
| 602 | |
| 603 | string: object(c_default="NULL") = b'' |
| 604 | |
| 605 | Return a new SHA-256 hash object; optionally initialized with a string. |
| 606 | [clinic start generated code]*/ |
| 607 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 608 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 609 | _sha256_sha256_impl(PyObject *module, PyObject *string) |
| 610 | /*[clinic end generated code: output=fa644436dcea5c31 input=09cce3fb855056b2]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 611 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 612 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 613 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 614 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 615 | if (string) |
| 616 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 617 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 618 | if ((new = newSHA256object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 619 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 620 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 621 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 622 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 623 | |
| 624 | sha_init(new); |
| 625 | |
| 626 | if (PyErr_Occurred()) { |
| 627 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 628 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 629 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 630 | return NULL; |
| 631 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 632 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 633 | sha_update(new, buf.buf, buf.len); |
| 634 | PyBuffer_Release(&buf); |
| 635 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 636 | |
| 637 | return (PyObject *)new; |
| 638 | } |
| 639 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 640 | /*[clinic input] |
| 641 | _sha256.sha224 |
| 642 | |
| 643 | string: object(c_default="NULL") = b'' |
| 644 | |
| 645 | Return a new SHA-224 hash object; optionally initialized with a string. |
| 646 | [clinic start generated code]*/ |
| 647 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 648 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 649 | _sha256_sha224_impl(PyObject *module, PyObject *string) |
| 650 | /*[clinic end generated code: output=21e3ba22c3404f93 input=27a04ba24c353a73]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 651 | { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 652 | SHAobject *new; |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 653 | Py_buffer buf; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 654 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 655 | if (string) |
| 656 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 657 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 658 | if ((new = newSHA224object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 659 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 660 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 661 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 662 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 663 | |
| 664 | sha224_init(new); |
| 665 | |
| 666 | if (PyErr_Occurred()) { |
| 667 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 668 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 669 | PyBuffer_Release(&buf); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 670 | return NULL; |
| 671 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 672 | if (string) { |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 673 | sha_update(new, buf.buf, buf.len); |
| 674 | PyBuffer_Release(&buf); |
| 675 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 676 | |
| 677 | return (PyObject *)new; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* List of functions exported by this module */ |
| 682 | |
| 683 | static struct PyMethodDef SHA_functions[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 684 | _SHA256_SHA256_METHODDEF |
| 685 | _SHA256_SHA224_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | |
| 690 | /* Initialize this module. */ |
| 691 | |
| 692 | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
| 693 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 694 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 695 | static struct PyModuleDef _sha256module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | PyModuleDef_HEAD_INIT, |
| 697 | "_sha256", |
| 698 | NULL, |
| 699 | -1, |
| 700 | SHA_functions, |
| 701 | NULL, |
| 702 | NULL, |
| 703 | NULL, |
| 704 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 705 | }; |
| 706 | |
| 707 | PyMODINIT_FUNC |
| 708 | PyInit__sha256(void) |
| 709 | { |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 710 | PyObject *m; |
| 711 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 712 | Py_TYPE(&SHA224type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 713 | if (PyType_Ready(&SHA224type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 714 | return NULL; |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 715 | Py_TYPE(&SHA256type) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 716 | if (PyType_Ready(&SHA256type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 717 | return NULL; |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 718 | |
| 719 | m = PyModule_Create(&_sha256module); |
| 720 | if (m == NULL) |
| 721 | return NULL; |
| 722 | |
| 723 | Py_INCREF((PyObject *)&SHA224type); |
| 724 | PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type); |
| 725 | Py_INCREF((PyObject *)&SHA256type); |
| 726 | PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type); |
| 727 | return m; |
| 728 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 729 | } |