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