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