1 | n/a | /* SHA512 module */ |
---|
2 | n/a | |
---|
3 | n/a | /* This module provides an interface to NIST's SHA-512 and SHA-384 Algorithms */ |
---|
4 | n/a | |
---|
5 | n/a | /* See below for information about the original code this module was |
---|
6 | n/a | based upon. Additional work performed by: |
---|
7 | n/a | |
---|
8 | n/a | Andrew Kuchling (amk@amk.ca) |
---|
9 | n/a | Greg Stein (gstein@lyra.org) |
---|
10 | n/a | Trevor Perrin (trevp@trevp.net) |
---|
11 | n/a | |
---|
12 | n/a | Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) |
---|
13 | n/a | Licensed to PSF under a Contributor Agreement. |
---|
14 | n/a | |
---|
15 | n/a | */ |
---|
16 | n/a | |
---|
17 | n/a | /* SHA objects */ |
---|
18 | n/a | |
---|
19 | n/a | #include "Python.h" |
---|
20 | n/a | #include "structmember.h" |
---|
21 | n/a | #include "hashlib.h" |
---|
22 | n/a | #include "pystrhex.h" |
---|
23 | n/a | |
---|
24 | n/a | /*[clinic input] |
---|
25 | n/a | module _sha512 |
---|
26 | n/a | class SHA512Type "SHAobject *" "&PyType_Type" |
---|
27 | n/a | [clinic start generated code]*/ |
---|
28 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/ |
---|
29 | n/a | |
---|
30 | n/a | /* Some useful types */ |
---|
31 | n/a | |
---|
32 | n/a | typedef unsigned char SHA_BYTE; |
---|
33 | n/a | |
---|
34 | n/a | #if SIZEOF_INT == 4 |
---|
35 | n/a | typedef unsigned int SHA_INT32; /* 32-bit integer */ |
---|
36 | n/a | typedef unsigned long long SHA_INT64; /* 64-bit integer */ |
---|
37 | n/a | #else |
---|
38 | n/a | /* not defined. compilation will die. */ |
---|
39 | n/a | #endif |
---|
40 | n/a | |
---|
41 | n/a | /* The SHA block size and message digest sizes, in bytes */ |
---|
42 | n/a | |
---|
43 | n/a | #define SHA_BLOCKSIZE 128 |
---|
44 | n/a | #define SHA_DIGESTSIZE 64 |
---|
45 | n/a | |
---|
46 | n/a | /* The structure for storing SHA info */ |
---|
47 | n/a | |
---|
48 | n/a | typedef struct { |
---|
49 | n/a | PyObject_HEAD |
---|
50 | n/a | SHA_INT64 digest[8]; /* Message digest */ |
---|
51 | n/a | SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ |
---|
52 | n/a | SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ |
---|
53 | n/a | int local; /* unprocessed amount in data */ |
---|
54 | n/a | int digestsize; |
---|
55 | n/a | } SHAobject; |
---|
56 | n/a | |
---|
57 | n/a | #include "clinic/sha512module.c.h" |
---|
58 | n/a | |
---|
59 | n/a | /* When run on a little-endian CPU we need to perform byte reversal on an |
---|
60 | n/a | array of longwords. */ |
---|
61 | n/a | |
---|
62 | n/a | #if PY_LITTLE_ENDIAN |
---|
63 | n/a | static void longReverse(SHA_INT64 *buffer, int byteCount) |
---|
64 | n/a | { |
---|
65 | n/a | SHA_INT64 value; |
---|
66 | n/a | |
---|
67 | n/a | byteCount /= sizeof(*buffer); |
---|
68 | n/a | while (byteCount--) { |
---|
69 | n/a | value = *buffer; |
---|
70 | n/a | |
---|
71 | n/a | ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff; |
---|
72 | n/a | ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff; |
---|
73 | n/a | ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff; |
---|
74 | n/a | ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff; |
---|
75 | n/a | ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff; |
---|
76 | n/a | ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff; |
---|
77 | n/a | ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff; |
---|
78 | n/a | ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff; |
---|
79 | n/a | |
---|
80 | n/a | buffer++; |
---|
81 | n/a | } |
---|
82 | n/a | } |
---|
83 | n/a | #endif |
---|
84 | n/a | |
---|
85 | n/a | static void SHAcopy(SHAobject *src, SHAobject *dest) |
---|
86 | n/a | { |
---|
87 | n/a | dest->local = src->local; |
---|
88 | n/a | dest->digestsize = src->digestsize; |
---|
89 | n/a | dest->count_lo = src->count_lo; |
---|
90 | n/a | dest->count_hi = src->count_hi; |
---|
91 | n/a | memcpy(dest->digest, src->digest, sizeof(src->digest)); |
---|
92 | n/a | memcpy(dest->data, src->data, sizeof(src->data)); |
---|
93 | n/a | } |
---|
94 | n/a | |
---|
95 | n/a | |
---|
96 | n/a | /* ------------------------------------------------------------------------ |
---|
97 | n/a | * |
---|
98 | n/a | * This code for the SHA-512 algorithm was noted as public domain. The |
---|
99 | n/a | * original headers are pasted below. |
---|
100 | n/a | * |
---|
101 | n/a | * Several changes have been made to make it more compatible with the |
---|
102 | n/a | * Python environment and desired interface. |
---|
103 | n/a | * |
---|
104 | n/a | */ |
---|
105 | n/a | |
---|
106 | n/a | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
---|
107 | n/a | * |
---|
108 | n/a | * LibTomCrypt is a library that provides various cryptographic |
---|
109 | n/a | * algorithms in a highly modular and flexible manner. |
---|
110 | n/a | * |
---|
111 | n/a | * The library is free for all purposes without any express |
---|
112 | n/a | * guarantee it works. |
---|
113 | n/a | * |
---|
114 | n/a | * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org |
---|
115 | n/a | */ |
---|
116 | n/a | |
---|
117 | n/a | |
---|
118 | n/a | /* SHA512 by Tom St Denis */ |
---|
119 | n/a | |
---|
120 | n/a | /* Various logical functions */ |
---|
121 | n/a | #define ROR64(x, y) \ |
---|
122 | n/a | ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned long long)(y) & 63)) | \ |
---|
123 | n/a | ((x)<<((unsigned long long)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL) |
---|
124 | n/a | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
---|
125 | n/a | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
---|
126 | n/a | #define S(x, n) ROR64((x),(n)) |
---|
127 | n/a | #define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned long long)n)) |
---|
128 | n/a | #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39)) |
---|
129 | n/a | #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41)) |
---|
130 | n/a | #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7)) |
---|
131 | n/a | #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6)) |
---|
132 | n/a | |
---|
133 | n/a | |
---|
134 | n/a | static void |
---|
135 | n/a | sha512_transform(SHAobject *sha_info) |
---|
136 | n/a | { |
---|
137 | n/a | int i; |
---|
138 | n/a | SHA_INT64 S[8], W[80], t0, t1; |
---|
139 | n/a | |
---|
140 | n/a | memcpy(W, sha_info->data, sizeof(sha_info->data)); |
---|
141 | n/a | #if PY_LITTLE_ENDIAN |
---|
142 | n/a | longReverse(W, (int)sizeof(sha_info->data)); |
---|
143 | n/a | #endif |
---|
144 | n/a | |
---|
145 | n/a | for (i = 16; i < 80; ++i) { |
---|
146 | n/a | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; |
---|
147 | n/a | } |
---|
148 | n/a | for (i = 0; i < 8; ++i) { |
---|
149 | n/a | S[i] = sha_info->digest[i]; |
---|
150 | n/a | } |
---|
151 | n/a | |
---|
152 | n/a | /* Compress */ |
---|
153 | n/a | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
---|
154 | n/a | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
---|
155 | n/a | t1 = Sigma0(a) + Maj(a, b, c); \ |
---|
156 | n/a | d += t0; \ |
---|
157 | n/a | h = t0 + t1; |
---|
158 | n/a | |
---|
159 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL); |
---|
160 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL); |
---|
161 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL); |
---|
162 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL); |
---|
163 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL); |
---|
164 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL); |
---|
165 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL); |
---|
166 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL); |
---|
167 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL); |
---|
168 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL); |
---|
169 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL); |
---|
170 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL); |
---|
171 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL); |
---|
172 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL); |
---|
173 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL); |
---|
174 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL); |
---|
175 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL); |
---|
176 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL); |
---|
177 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL); |
---|
178 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL); |
---|
179 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL); |
---|
180 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL); |
---|
181 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL); |
---|
182 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL); |
---|
183 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL); |
---|
184 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL); |
---|
185 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL); |
---|
186 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL); |
---|
187 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL); |
---|
188 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL); |
---|
189 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL); |
---|
190 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL); |
---|
191 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL); |
---|
192 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL); |
---|
193 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL); |
---|
194 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL); |
---|
195 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL); |
---|
196 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL); |
---|
197 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL); |
---|
198 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL); |
---|
199 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL); |
---|
200 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL); |
---|
201 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL); |
---|
202 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL); |
---|
203 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL); |
---|
204 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL); |
---|
205 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL); |
---|
206 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL); |
---|
207 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL); |
---|
208 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL); |
---|
209 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL); |
---|
210 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL); |
---|
211 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL); |
---|
212 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL); |
---|
213 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL); |
---|
214 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL); |
---|
215 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL); |
---|
216 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL); |
---|
217 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL); |
---|
218 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL); |
---|
219 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL); |
---|
220 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL); |
---|
221 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL); |
---|
222 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL); |
---|
223 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL); |
---|
224 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL); |
---|
225 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL); |
---|
226 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL); |
---|
227 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL); |
---|
228 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL); |
---|
229 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL); |
---|
230 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL); |
---|
231 | n/a | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL); |
---|
232 | n/a | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL); |
---|
233 | n/a | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL); |
---|
234 | n/a | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL); |
---|
235 | n/a | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL); |
---|
236 | n/a | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL); |
---|
237 | n/a | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL); |
---|
238 | n/a | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL); |
---|
239 | n/a | |
---|
240 | n/a | #undef RND |
---|
241 | n/a | |
---|
242 | n/a | /* feedback */ |
---|
243 | n/a | for (i = 0; i < 8; i++) { |
---|
244 | n/a | sha_info->digest[i] = sha_info->digest[i] + S[i]; |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | } |
---|
248 | n/a | |
---|
249 | n/a | |
---|
250 | n/a | |
---|
251 | n/a | /* initialize the SHA digest */ |
---|
252 | n/a | |
---|
253 | n/a | static void |
---|
254 | n/a | sha512_init(SHAobject *sha_info) |
---|
255 | n/a | { |
---|
256 | n/a | sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908); |
---|
257 | n/a | sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b); |
---|
258 | n/a | sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b); |
---|
259 | n/a | sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1); |
---|
260 | n/a | sha_info->digest[4] = Py_ULL(0x510e527fade682d1); |
---|
261 | n/a | sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f); |
---|
262 | n/a | sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b); |
---|
263 | n/a | sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179); |
---|
264 | n/a | sha_info->count_lo = 0L; |
---|
265 | n/a | sha_info->count_hi = 0L; |
---|
266 | n/a | sha_info->local = 0; |
---|
267 | n/a | sha_info->digestsize = 64; |
---|
268 | n/a | } |
---|
269 | n/a | |
---|
270 | n/a | static void |
---|
271 | n/a | sha384_init(SHAobject *sha_info) |
---|
272 | n/a | { |
---|
273 | n/a | sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8); |
---|
274 | n/a | sha_info->digest[1] = Py_ULL(0x629a292a367cd507); |
---|
275 | n/a | sha_info->digest[2] = Py_ULL(0x9159015a3070dd17); |
---|
276 | n/a | sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939); |
---|
277 | n/a | sha_info->digest[4] = Py_ULL(0x67332667ffc00b31); |
---|
278 | n/a | sha_info->digest[5] = Py_ULL(0x8eb44a8768581511); |
---|
279 | n/a | sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7); |
---|
280 | n/a | sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4); |
---|
281 | n/a | sha_info->count_lo = 0L; |
---|
282 | n/a | sha_info->count_hi = 0L; |
---|
283 | n/a | sha_info->local = 0; |
---|
284 | n/a | sha_info->digestsize = 48; |
---|
285 | n/a | } |
---|
286 | n/a | |
---|
287 | n/a | |
---|
288 | n/a | /* update the SHA digest */ |
---|
289 | n/a | |
---|
290 | n/a | static void |
---|
291 | n/a | sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count) |
---|
292 | n/a | { |
---|
293 | n/a | Py_ssize_t i; |
---|
294 | n/a | SHA_INT32 clo; |
---|
295 | n/a | |
---|
296 | n/a | clo = sha_info->count_lo + ((SHA_INT32) count << 3); |
---|
297 | n/a | if (clo < sha_info->count_lo) { |
---|
298 | n/a | ++sha_info->count_hi; |
---|
299 | n/a | } |
---|
300 | n/a | sha_info->count_lo = clo; |
---|
301 | n/a | sha_info->count_hi += (SHA_INT32) count >> 29; |
---|
302 | n/a | if (sha_info->local) { |
---|
303 | n/a | i = SHA_BLOCKSIZE - sha_info->local; |
---|
304 | n/a | if (i > count) { |
---|
305 | n/a | i = count; |
---|
306 | n/a | } |
---|
307 | n/a | memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i); |
---|
308 | n/a | count -= i; |
---|
309 | n/a | buffer += i; |
---|
310 | n/a | sha_info->local += (int)i; |
---|
311 | n/a | if (sha_info->local == SHA_BLOCKSIZE) { |
---|
312 | n/a | sha512_transform(sha_info); |
---|
313 | n/a | } |
---|
314 | n/a | else { |
---|
315 | n/a | return; |
---|
316 | n/a | } |
---|
317 | n/a | } |
---|
318 | n/a | while (count >= SHA_BLOCKSIZE) { |
---|
319 | n/a | memcpy(sha_info->data, buffer, SHA_BLOCKSIZE); |
---|
320 | n/a | buffer += SHA_BLOCKSIZE; |
---|
321 | n/a | count -= SHA_BLOCKSIZE; |
---|
322 | n/a | sha512_transform(sha_info); |
---|
323 | n/a | } |
---|
324 | n/a | memcpy(sha_info->data, buffer, count); |
---|
325 | n/a | sha_info->local = (int)count; |
---|
326 | n/a | } |
---|
327 | n/a | |
---|
328 | n/a | /* finish computing the SHA digest */ |
---|
329 | n/a | |
---|
330 | n/a | static void |
---|
331 | n/a | sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info) |
---|
332 | n/a | { |
---|
333 | n/a | int count; |
---|
334 | n/a | SHA_INT32 lo_bit_count, hi_bit_count; |
---|
335 | n/a | |
---|
336 | n/a | lo_bit_count = sha_info->count_lo; |
---|
337 | n/a | hi_bit_count = sha_info->count_hi; |
---|
338 | n/a | count = (int) ((lo_bit_count >> 3) & 0x7f); |
---|
339 | n/a | ((SHA_BYTE *) sha_info->data)[count++] = 0x80; |
---|
340 | n/a | if (count > SHA_BLOCKSIZE - 16) { |
---|
341 | n/a | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
---|
342 | n/a | SHA_BLOCKSIZE - count); |
---|
343 | n/a | sha512_transform(sha_info); |
---|
344 | n/a | memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16); |
---|
345 | n/a | } |
---|
346 | n/a | else { |
---|
347 | n/a | memset(((SHA_BYTE *) sha_info->data) + count, 0, |
---|
348 | n/a | SHA_BLOCKSIZE - 16 - count); |
---|
349 | n/a | } |
---|
350 | n/a | |
---|
351 | n/a | /* GJS: note that we add the hi/lo in big-endian. sha512_transform will |
---|
352 | n/a | swap these values into host-order. */ |
---|
353 | n/a | sha_info->data[112] = 0; |
---|
354 | n/a | sha_info->data[113] = 0; |
---|
355 | n/a | sha_info->data[114] = 0; |
---|
356 | n/a | sha_info->data[115] = 0; |
---|
357 | n/a | sha_info->data[116] = 0; |
---|
358 | n/a | sha_info->data[117] = 0; |
---|
359 | n/a | sha_info->data[118] = 0; |
---|
360 | n/a | sha_info->data[119] = 0; |
---|
361 | n/a | sha_info->data[120] = (hi_bit_count >> 24) & 0xff; |
---|
362 | n/a | sha_info->data[121] = (hi_bit_count >> 16) & 0xff; |
---|
363 | n/a | sha_info->data[122] = (hi_bit_count >> 8) & 0xff; |
---|
364 | n/a | sha_info->data[123] = (hi_bit_count >> 0) & 0xff; |
---|
365 | n/a | sha_info->data[124] = (lo_bit_count >> 24) & 0xff; |
---|
366 | n/a | sha_info->data[125] = (lo_bit_count >> 16) & 0xff; |
---|
367 | n/a | sha_info->data[126] = (lo_bit_count >> 8) & 0xff; |
---|
368 | n/a | sha_info->data[127] = (lo_bit_count >> 0) & 0xff; |
---|
369 | n/a | sha512_transform(sha_info); |
---|
370 | n/a | digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff); |
---|
371 | n/a | digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff); |
---|
372 | n/a | digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff); |
---|
373 | n/a | digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff); |
---|
374 | n/a | digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff); |
---|
375 | n/a | digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff); |
---|
376 | n/a | digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff); |
---|
377 | n/a | digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff); |
---|
378 | n/a | digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff); |
---|
379 | n/a | digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff); |
---|
380 | n/a | digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff); |
---|
381 | n/a | digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff); |
---|
382 | n/a | digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff); |
---|
383 | n/a | digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff); |
---|
384 | n/a | digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff); |
---|
385 | n/a | digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff); |
---|
386 | n/a | digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff); |
---|
387 | n/a | digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff); |
---|
388 | n/a | digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff); |
---|
389 | n/a | digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff); |
---|
390 | n/a | digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff); |
---|
391 | n/a | digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff); |
---|
392 | n/a | digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff); |
---|
393 | n/a | digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff); |
---|
394 | n/a | digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff); |
---|
395 | n/a | digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff); |
---|
396 | n/a | digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff); |
---|
397 | n/a | digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff); |
---|
398 | n/a | digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff); |
---|
399 | n/a | digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff); |
---|
400 | n/a | digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff); |
---|
401 | n/a | digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff); |
---|
402 | n/a | digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff); |
---|
403 | n/a | digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff); |
---|
404 | n/a | digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff); |
---|
405 | n/a | digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff); |
---|
406 | n/a | digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff); |
---|
407 | n/a | digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff); |
---|
408 | n/a | digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff); |
---|
409 | n/a | digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff); |
---|
410 | n/a | digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff); |
---|
411 | n/a | digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff); |
---|
412 | n/a | digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff); |
---|
413 | n/a | digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff); |
---|
414 | n/a | digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff); |
---|
415 | n/a | digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff); |
---|
416 | n/a | digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff); |
---|
417 | n/a | digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff); |
---|
418 | n/a | digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff); |
---|
419 | n/a | digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff); |
---|
420 | n/a | digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff); |
---|
421 | n/a | digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff); |
---|
422 | n/a | digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff); |
---|
423 | n/a | digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff); |
---|
424 | n/a | digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff); |
---|
425 | n/a | digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff); |
---|
426 | n/a | digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff); |
---|
427 | n/a | digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff); |
---|
428 | n/a | digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff); |
---|
429 | n/a | digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff); |
---|
430 | n/a | digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff); |
---|
431 | n/a | digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff); |
---|
432 | n/a | digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff); |
---|
433 | n/a | digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff); |
---|
434 | n/a | } |
---|
435 | n/a | |
---|
436 | n/a | /* |
---|
437 | n/a | * End of copied SHA code. |
---|
438 | n/a | * |
---|
439 | n/a | * ------------------------------------------------------------------------ |
---|
440 | n/a | */ |
---|
441 | n/a | |
---|
442 | n/a | static PyTypeObject SHA384type; |
---|
443 | n/a | static PyTypeObject SHA512type; |
---|
444 | n/a | |
---|
445 | n/a | |
---|
446 | n/a | static SHAobject * |
---|
447 | n/a | newSHA384object(void) |
---|
448 | n/a | { |
---|
449 | n/a | return (SHAobject *)PyObject_New(SHAobject, &SHA384type); |
---|
450 | n/a | } |
---|
451 | n/a | |
---|
452 | n/a | static SHAobject * |
---|
453 | n/a | newSHA512object(void) |
---|
454 | n/a | { |
---|
455 | n/a | return (SHAobject *)PyObject_New(SHAobject, &SHA512type); |
---|
456 | n/a | } |
---|
457 | n/a | |
---|
458 | n/a | /* Internal methods for a hash object */ |
---|
459 | n/a | |
---|
460 | n/a | static void |
---|
461 | n/a | SHA512_dealloc(PyObject *ptr) |
---|
462 | n/a | { |
---|
463 | n/a | PyObject_Del(ptr); |
---|
464 | n/a | } |
---|
465 | n/a | |
---|
466 | n/a | |
---|
467 | n/a | /* External methods for a hash object */ |
---|
468 | n/a | |
---|
469 | n/a | /*[clinic input] |
---|
470 | n/a | SHA512Type.copy |
---|
471 | n/a | |
---|
472 | n/a | Return a copy of the hash object. |
---|
473 | n/a | [clinic start generated code]*/ |
---|
474 | n/a | |
---|
475 | n/a | static PyObject * |
---|
476 | n/a | SHA512Type_copy_impl(SHAobject *self) |
---|
477 | n/a | /*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/ |
---|
478 | n/a | { |
---|
479 | n/a | SHAobject *newobj; |
---|
480 | n/a | |
---|
481 | n/a | if (((PyObject*)self)->ob_type == &SHA512type) { |
---|
482 | n/a | if ( (newobj = newSHA512object())==NULL) |
---|
483 | n/a | return NULL; |
---|
484 | n/a | } else { |
---|
485 | n/a | if ( (newobj = newSHA384object())==NULL) |
---|
486 | n/a | return NULL; |
---|
487 | n/a | } |
---|
488 | n/a | |
---|
489 | n/a | SHAcopy(self, newobj); |
---|
490 | n/a | return (PyObject *)newobj; |
---|
491 | n/a | } |
---|
492 | n/a | |
---|
493 | n/a | /*[clinic input] |
---|
494 | n/a | SHA512Type.digest |
---|
495 | n/a | |
---|
496 | n/a | Return the digest value as a string of binary data. |
---|
497 | n/a | [clinic start generated code]*/ |
---|
498 | n/a | |
---|
499 | n/a | static PyObject * |
---|
500 | n/a | SHA512Type_digest_impl(SHAobject *self) |
---|
501 | n/a | /*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/ |
---|
502 | n/a | { |
---|
503 | n/a | unsigned char digest[SHA_DIGESTSIZE]; |
---|
504 | n/a | SHAobject temp; |
---|
505 | n/a | |
---|
506 | n/a | SHAcopy(self, &temp); |
---|
507 | n/a | sha512_final(digest, &temp); |
---|
508 | n/a | return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); |
---|
509 | n/a | } |
---|
510 | n/a | |
---|
511 | n/a | /*[clinic input] |
---|
512 | n/a | SHA512Type.hexdigest |
---|
513 | n/a | |
---|
514 | n/a | Return the digest value as a string of hexadecimal digits. |
---|
515 | n/a | [clinic start generated code]*/ |
---|
516 | n/a | |
---|
517 | n/a | static PyObject * |
---|
518 | n/a | SHA512Type_hexdigest_impl(SHAobject *self) |
---|
519 | n/a | /*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/ |
---|
520 | n/a | { |
---|
521 | n/a | unsigned char digest[SHA_DIGESTSIZE]; |
---|
522 | n/a | SHAobject temp; |
---|
523 | n/a | |
---|
524 | n/a | /* Get the raw (binary) digest value */ |
---|
525 | n/a | SHAcopy(self, &temp); |
---|
526 | n/a | sha512_final(digest, &temp); |
---|
527 | n/a | |
---|
528 | n/a | return _Py_strhex((const char *)digest, self->digestsize); |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | /*[clinic input] |
---|
532 | n/a | SHA512Type.update |
---|
533 | n/a | |
---|
534 | n/a | obj: object |
---|
535 | n/a | / |
---|
536 | n/a | |
---|
537 | n/a | Update this hash object's state with the provided string. |
---|
538 | n/a | [clinic start generated code]*/ |
---|
539 | n/a | |
---|
540 | n/a | static PyObject * |
---|
541 | n/a | SHA512Type_update(SHAobject *self, PyObject *obj) |
---|
542 | n/a | /*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/ |
---|
543 | n/a | { |
---|
544 | n/a | Py_buffer buf; |
---|
545 | n/a | |
---|
546 | n/a | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
---|
547 | n/a | |
---|
548 | n/a | sha512_update(self, buf.buf, buf.len); |
---|
549 | n/a | |
---|
550 | n/a | PyBuffer_Release(&buf); |
---|
551 | n/a | Py_RETURN_NONE; |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | static PyMethodDef SHA_methods[] = { |
---|
555 | n/a | SHA512TYPE_COPY_METHODDEF |
---|
556 | n/a | SHA512TYPE_DIGEST_METHODDEF |
---|
557 | n/a | SHA512TYPE_HEXDIGEST_METHODDEF |
---|
558 | n/a | SHA512TYPE_UPDATE_METHODDEF |
---|
559 | n/a | {NULL, NULL} /* sentinel */ |
---|
560 | n/a | }; |
---|
561 | n/a | |
---|
562 | n/a | static PyObject * |
---|
563 | n/a | SHA512_get_block_size(PyObject *self, void *closure) |
---|
564 | n/a | { |
---|
565 | n/a | return PyLong_FromLong(SHA_BLOCKSIZE); |
---|
566 | n/a | } |
---|
567 | n/a | |
---|
568 | n/a | static PyObject * |
---|
569 | n/a | SHA512_get_name(PyObject *self, void *closure) |
---|
570 | n/a | { |
---|
571 | n/a | if (((SHAobject *)self)->digestsize == 64) |
---|
572 | n/a | return PyUnicode_FromStringAndSize("sha512", 6); |
---|
573 | n/a | else |
---|
574 | n/a | return PyUnicode_FromStringAndSize("sha384", 6); |
---|
575 | n/a | } |
---|
576 | n/a | |
---|
577 | n/a | static PyGetSetDef SHA_getseters[] = { |
---|
578 | n/a | {"block_size", |
---|
579 | n/a | (getter)SHA512_get_block_size, NULL, |
---|
580 | n/a | NULL, |
---|
581 | n/a | NULL}, |
---|
582 | n/a | {"name", |
---|
583 | n/a | (getter)SHA512_get_name, NULL, |
---|
584 | n/a | NULL, |
---|
585 | n/a | NULL}, |
---|
586 | n/a | {NULL} /* Sentinel */ |
---|
587 | n/a | }; |
---|
588 | n/a | |
---|
589 | n/a | static PyMemberDef SHA_members[] = { |
---|
590 | n/a | {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL}, |
---|
591 | n/a | {NULL} /* Sentinel */ |
---|
592 | n/a | }; |
---|
593 | n/a | |
---|
594 | n/a | static PyTypeObject SHA384type = { |
---|
595 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
596 | n/a | "_sha512.sha384", /*tp_name*/ |
---|
597 | n/a | sizeof(SHAobject), /*tp_size*/ |
---|
598 | n/a | 0, /*tp_itemsize*/ |
---|
599 | n/a | /* methods */ |
---|
600 | n/a | SHA512_dealloc, /*tp_dealloc*/ |
---|
601 | n/a | 0, /*tp_print*/ |
---|
602 | n/a | 0, /*tp_getattr*/ |
---|
603 | n/a | 0, /*tp_setattr*/ |
---|
604 | n/a | 0, /*tp_reserved*/ |
---|
605 | n/a | 0, /*tp_repr*/ |
---|
606 | n/a | 0, /*tp_as_number*/ |
---|
607 | n/a | 0, /*tp_as_sequence*/ |
---|
608 | n/a | 0, /*tp_as_mapping*/ |
---|
609 | n/a | 0, /*tp_hash*/ |
---|
610 | n/a | 0, /*tp_call*/ |
---|
611 | n/a | 0, /*tp_str*/ |
---|
612 | n/a | 0, /*tp_getattro*/ |
---|
613 | n/a | 0, /*tp_setattro*/ |
---|
614 | n/a | 0, /*tp_as_buffer*/ |
---|
615 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
616 | n/a | 0, /*tp_doc*/ |
---|
617 | n/a | 0, /*tp_traverse*/ |
---|
618 | n/a | 0, /*tp_clear*/ |
---|
619 | n/a | 0, /*tp_richcompare*/ |
---|
620 | n/a | 0, /*tp_weaklistoffset*/ |
---|
621 | n/a | 0, /*tp_iter*/ |
---|
622 | n/a | 0, /*tp_iternext*/ |
---|
623 | n/a | SHA_methods, /* tp_methods */ |
---|
624 | n/a | SHA_members, /* tp_members */ |
---|
625 | n/a | SHA_getseters, /* tp_getset */ |
---|
626 | n/a | }; |
---|
627 | n/a | |
---|
628 | n/a | static PyTypeObject SHA512type = { |
---|
629 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
630 | n/a | "_sha512.sha512", /*tp_name*/ |
---|
631 | n/a | sizeof(SHAobject), /*tp_size*/ |
---|
632 | n/a | 0, /*tp_itemsize*/ |
---|
633 | n/a | /* methods */ |
---|
634 | n/a | SHA512_dealloc, /*tp_dealloc*/ |
---|
635 | n/a | 0, /*tp_print*/ |
---|
636 | n/a | 0, /*tp_getattr*/ |
---|
637 | n/a | 0, /*tp_setattr*/ |
---|
638 | n/a | 0, /*tp_reserved*/ |
---|
639 | n/a | 0, /*tp_repr*/ |
---|
640 | n/a | 0, /*tp_as_number*/ |
---|
641 | n/a | 0, /*tp_as_sequence*/ |
---|
642 | n/a | 0, /*tp_as_mapping*/ |
---|
643 | n/a | 0, /*tp_hash*/ |
---|
644 | n/a | 0, /*tp_call*/ |
---|
645 | n/a | 0, /*tp_str*/ |
---|
646 | n/a | 0, /*tp_getattro*/ |
---|
647 | n/a | 0, /*tp_setattro*/ |
---|
648 | n/a | 0, /*tp_as_buffer*/ |
---|
649 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
650 | n/a | 0, /*tp_doc*/ |
---|
651 | n/a | 0, /*tp_traverse*/ |
---|
652 | n/a | 0, /*tp_clear*/ |
---|
653 | n/a | 0, /*tp_richcompare*/ |
---|
654 | n/a | 0, /*tp_weaklistoffset*/ |
---|
655 | n/a | 0, /*tp_iter*/ |
---|
656 | n/a | 0, /*tp_iternext*/ |
---|
657 | n/a | SHA_methods, /* tp_methods */ |
---|
658 | n/a | SHA_members, /* tp_members */ |
---|
659 | n/a | SHA_getseters, /* tp_getset */ |
---|
660 | n/a | }; |
---|
661 | n/a | |
---|
662 | n/a | |
---|
663 | n/a | /* The single module-level function: new() */ |
---|
664 | n/a | |
---|
665 | n/a | /*[clinic input] |
---|
666 | n/a | _sha512.sha512 |
---|
667 | n/a | |
---|
668 | n/a | string: object(c_default="NULL") = b'' |
---|
669 | n/a | |
---|
670 | n/a | Return a new SHA-512 hash object; optionally initialized with a string. |
---|
671 | n/a | [clinic start generated code]*/ |
---|
672 | n/a | |
---|
673 | n/a | static PyObject * |
---|
674 | n/a | _sha512_sha512_impl(PyObject *module, PyObject *string) |
---|
675 | n/a | /*[clinic end generated code: output=8b865a2df73bd387 input=e69bad9ae9b6a308]*/ |
---|
676 | n/a | { |
---|
677 | n/a | SHAobject *new; |
---|
678 | n/a | Py_buffer buf; |
---|
679 | n/a | |
---|
680 | n/a | if (string) |
---|
681 | n/a | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
---|
682 | n/a | |
---|
683 | n/a | if ((new = newSHA512object()) == NULL) { |
---|
684 | n/a | if (string) |
---|
685 | n/a | PyBuffer_Release(&buf); |
---|
686 | n/a | return NULL; |
---|
687 | n/a | } |
---|
688 | n/a | |
---|
689 | n/a | sha512_init(new); |
---|
690 | n/a | |
---|
691 | n/a | if (PyErr_Occurred()) { |
---|
692 | n/a | Py_DECREF(new); |
---|
693 | n/a | if (string) |
---|
694 | n/a | PyBuffer_Release(&buf); |
---|
695 | n/a | return NULL; |
---|
696 | n/a | } |
---|
697 | n/a | if (string) { |
---|
698 | n/a | sha512_update(new, buf.buf, buf.len); |
---|
699 | n/a | PyBuffer_Release(&buf); |
---|
700 | n/a | } |
---|
701 | n/a | |
---|
702 | n/a | return (PyObject *)new; |
---|
703 | n/a | } |
---|
704 | n/a | |
---|
705 | n/a | /*[clinic input] |
---|
706 | n/a | _sha512.sha384 |
---|
707 | n/a | |
---|
708 | n/a | string: object(c_default="NULL") = b'' |
---|
709 | n/a | |
---|
710 | n/a | Return a new SHA-384 hash object; optionally initialized with a string. |
---|
711 | n/a | [clinic start generated code]*/ |
---|
712 | n/a | |
---|
713 | n/a | static PyObject * |
---|
714 | n/a | _sha512_sha384_impl(PyObject *module, PyObject *string) |
---|
715 | n/a | /*[clinic end generated code: output=ae4b2e26decf81e8 input=c9327788d4ea4545]*/ |
---|
716 | n/a | { |
---|
717 | n/a | SHAobject *new; |
---|
718 | n/a | Py_buffer buf; |
---|
719 | n/a | |
---|
720 | n/a | if (string) |
---|
721 | n/a | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
---|
722 | n/a | |
---|
723 | n/a | if ((new = newSHA384object()) == NULL) { |
---|
724 | n/a | if (string) |
---|
725 | n/a | PyBuffer_Release(&buf); |
---|
726 | n/a | return NULL; |
---|
727 | n/a | } |
---|
728 | n/a | |
---|
729 | n/a | sha384_init(new); |
---|
730 | n/a | |
---|
731 | n/a | if (PyErr_Occurred()) { |
---|
732 | n/a | Py_DECREF(new); |
---|
733 | n/a | if (string) |
---|
734 | n/a | PyBuffer_Release(&buf); |
---|
735 | n/a | return NULL; |
---|
736 | n/a | } |
---|
737 | n/a | if (string) { |
---|
738 | n/a | sha512_update(new, buf.buf, buf.len); |
---|
739 | n/a | PyBuffer_Release(&buf); |
---|
740 | n/a | } |
---|
741 | n/a | |
---|
742 | n/a | return (PyObject *)new; |
---|
743 | n/a | } |
---|
744 | n/a | |
---|
745 | n/a | |
---|
746 | n/a | /* List of functions exported by this module */ |
---|
747 | n/a | |
---|
748 | n/a | static struct PyMethodDef SHA_functions[] = { |
---|
749 | n/a | _SHA512_SHA512_METHODDEF |
---|
750 | n/a | _SHA512_SHA384_METHODDEF |
---|
751 | n/a | {NULL, NULL} /* Sentinel */ |
---|
752 | n/a | }; |
---|
753 | n/a | |
---|
754 | n/a | |
---|
755 | n/a | /* Initialize this module. */ |
---|
756 | n/a | |
---|
757 | n/a | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
---|
758 | n/a | |
---|
759 | n/a | |
---|
760 | n/a | static struct PyModuleDef _sha512module = { |
---|
761 | n/a | PyModuleDef_HEAD_INIT, |
---|
762 | n/a | "_sha512", |
---|
763 | n/a | NULL, |
---|
764 | n/a | -1, |
---|
765 | n/a | SHA_functions, |
---|
766 | n/a | NULL, |
---|
767 | n/a | NULL, |
---|
768 | n/a | NULL, |
---|
769 | n/a | NULL |
---|
770 | n/a | }; |
---|
771 | n/a | |
---|
772 | n/a | PyMODINIT_FUNC |
---|
773 | n/a | PyInit__sha512(void) |
---|
774 | n/a | { |
---|
775 | n/a | PyObject *m; |
---|
776 | n/a | |
---|
777 | n/a | Py_TYPE(&SHA384type) = &PyType_Type; |
---|
778 | n/a | if (PyType_Ready(&SHA384type) < 0) |
---|
779 | n/a | return NULL; |
---|
780 | n/a | Py_TYPE(&SHA512type) = &PyType_Type; |
---|
781 | n/a | if (PyType_Ready(&SHA512type) < 0) |
---|
782 | n/a | return NULL; |
---|
783 | n/a | |
---|
784 | n/a | m = PyModule_Create(&_sha512module); |
---|
785 | n/a | if (m == NULL) |
---|
786 | n/a | return NULL; |
---|
787 | n/a | |
---|
788 | n/a | Py_INCREF((PyObject *)&SHA384type); |
---|
789 | n/a | PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type); |
---|
790 | n/a | Py_INCREF((PyObject *)&SHA512type); |
---|
791 | n/a | PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type); |
---|
792 | n/a | return m; |
---|
793 | n/a | } |
---|