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