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