MagickCore 7.0.10
signature.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% SSSSS IIIII GGGG N N AAA TTTTT U U RRRR EEEEE %
6% SS I G NN N A A T U U R R E %
7% SSS I G GG N N N AAAAA T U U RRRR EEE %
8% SS I G G N NN A A T U U R R E %
9% SSSSS IIIII GGG N N A A T UUU R R EEEEE %
10% %
11% %
12% Methods to Compute a Message Digest for an Image %
13% %
14% Software Design %
15% Cristy %
16% December 1992 %
17% %
18% %
19% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/exception.h"
45#include "wizard/memory_.h"
46#include "wizard/signature.h"
47#include "wizard/string_.h"
48
49/*
50 Define declarations.
51*/
52#define Trunc32(x) ((x) & 0xffffffffUL)
53
54/*
55 Forward declarations.
56*/
57static void
59
60/*
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62% %
63% %
64% %
65+ F i n a l i z e S i g n a t u r e %
66% %
67% %
68% %
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%
71% FinalizeSignature() finalizes the SHA message digest computation.
72%
73% The format of the FinalizeSignature method is:
74%
75% FinalizeSignature(SignatureInfo *signature_info)
76%
77% A description of each parameter follows:
78%
79% o signature_info: The address of a structure of type SignatureInfo.
80%
81%
82*/
84{
85 ssize_t
86 count;
87
88 size_t
89 high_order,
90 low_order;
91
92 /*
93 Add padding and return the message digest.
94 */
95 assert(signature_info != (SignatureInfo *) NULL);
96 assert(signature_info->signature == WizardSignature);
97 low_order=signature_info->low_order;
98 high_order=signature_info->high_order;
99 count=(ssize_t) ((low_order >> 3) & 0x3f);
100 signature_info->message[count++]=(unsigned char) 0x80;
101 if (count <= (WizardSignatureSize-8))
102 (void) memset(signature_info->message+count,0,(size_t)
103 (WizardSignatureSize-8-count));
104 else
105 {
106 (void) memset(signature_info->message+count,0,(size_t)
107 (WizardSignatureSize-count));
108 TransformSignature(signature_info);
109 (void) memset(signature_info->message,0,WizardSignatureSize-8);
110 }
111 signature_info->message[56]=(unsigned char) (high_order >> 24);
112 signature_info->message[57]=(unsigned char) (high_order >> 16);
113 signature_info->message[58]=(unsigned char) (high_order >> 8);
114 signature_info->message[59]=(unsigned char) high_order;
115 signature_info->message[60]=(unsigned char) (low_order >> 24);
116 signature_info->message[61]=(unsigned char) (low_order >> 16);
117 signature_info->message[62]=(unsigned char) (low_order >> 8);
118 signature_info->message[63]=(unsigned char) low_order;
119 TransformSignature(signature_info);
120}
121
122/*
123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124% %
125% %
126% %
127+ G e t S i g n a t u r e I n f o %
128% %
129% %
130% %
131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132%
133% GetSignatureInfo() initializes the SHA message digest structure.
134%
135% The format of the GetSignatureInfo method is:
136%
137% GetSignatureInfo(SignatureInfo *signature_info)
138%
139% A description of each parameter follows:
140%
141% o signature_info: The address of a structure of type SignatureInfo.
142%
143%
144*/
146{
147 size_t
148 lsb_first;
149
150 assert(signature_info != (SignatureInfo *) NULL);
151 (void) memset(signature_info,0,sizeof(*signature_info));
152 signature_info->digest[0]=0x6a09e667UL;
153 signature_info->digest[1]=0xbb67ae85UL;
154 signature_info->digest[2]=0x3c6ef372UL;
155 signature_info->digest[3]=0xa54ff53aUL;
156 signature_info->digest[4]=0x510e527fUL;
157 signature_info->digest[5]=0x9b05688cUL;
158 signature_info->digest[6]=0x1f83d9abUL;
159 signature_info->digest[7]=0x5be0cd19UL;
160 lsb_first=1;
161 signature_info->lsb_first=(int)
162 (*(char *) &lsb_first) == 1 ? WizardTrue : WizardFalse;
163 signature_info->signature=WizardSignature;
164}
165
166/*
167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168% %
169% %
170% %
171+ T r a n s f o r m S i g n a t u r e %
172% %
173% %
174% %
175%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176%
177% TransformSignature() transforms the SHA message digest.
178%
179% The format of the TransformSignature method is:
180%
181% TransformSignature(SignatureInfo *signature_info)
182%
183% A description of each parameter follows:
184%
185% o signature_info: The address of a structure of type SignatureInfo.
186%
187%
188*/
189static void TransformSignature(SignatureInfo *signature_info)
190{
191#define Ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
192#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
193#define RotateRight(x,n) Trunc32((((x) >> (n)) | ((x) << (32-(n)))))
194#define Sigma0(x) (RotateRight(x,7) ^ RotateRight(x,18) ^ Trunc32((x) >> 3))
195#define Sigma1(x) (RotateRight(x,17) ^ RotateRight(x,19) ^ Trunc32((x) >> 10))
196#define Suma0(x) (RotateRight(x,2) ^ RotateRight(x,13) ^ RotateRight(x,22))
197#define Suma1(x) (RotateRight(x,6) ^ RotateRight(x,11) ^ RotateRight(x,25))
198
199 ssize_t
200 j;
201
202 ssize_t
203 i;
204
205 unsigned char
206 *p;
207
208 static size_t
209 K[64] =
210 {
211 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
212 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
213 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
214 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
215 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
216 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
217 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
218 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
219 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
220 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
221 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
222 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
223 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
224 }; /* 32-bit fractional part of the cube root of the first 64 primes */
225
226 size_t
227 A,
228 B,
229 C,
230 D,
231 E,
232 F,
233 G,
234 H,
235 shift,
236 T,
237 T1,
238 T2,
239 W[64];
240
241 shift=32;
242 p=signature_info->message;
243 if (signature_info->lsb_first == WizardFalse)
244 {
245 if (sizeof(size_t) <= 4)
246 for (i=0; i < 16; i++)
247 {
248 T=(*((size_t *) p));
249 p+=4;
250 W[i]=Trunc32(T);
251 }
252 else
253 for (i=0; i < 16; i+=2)
254 {
255 T=(*((size_t *) p));
256 p+=8;
257 W[i]=Trunc32(T >> shift);
258 W[i+1]=Trunc32(T);
259 }
260 }
261 else
262 if (sizeof(size_t) <= 4)
263 for (i=0; i < 16; i++)
264 {
265 T=(*((size_t *) p));
266 p+=4;
267 W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
268 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
269 }
270 else
271 for (i=0; i < 16; i+=2)
272 {
273 T=(*((size_t *) p));
274 p+=8;
275 W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
276 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
277 T>>=shift;
278 W[i+1]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
279 ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
280 }
281 /*
282 Copy digest to registers.
283 */
284 A=signature_info->digest[0];
285 B=signature_info->digest[1];
286 C=signature_info->digest[2];
287 D=signature_info->digest[3];
288 E=signature_info->digest[4];
289 F=signature_info->digest[5];
290 G=signature_info->digest[6];
291 H=signature_info->digest[7];
292 for (i=16; i < 64; i++)
293 W[i]=Trunc32(Sigma1(W[i-2])+W[i-7]+Sigma0(W[i-15])+W[i-16]);
294 for (j=0; j < 64; j++)
295 {
296 T1=Trunc32(H+Suma1(E)+Ch(E,F,G)+K[j]+W[j]);
297 T2=Trunc32(Suma0(A)+Maj(A,B,C));
298 H=G;
299 G=F;
300 F=E;
301 E=Trunc32(D+T1);
302 D=C;
303 C=B;
304 B=A;
305 A=Trunc32(T1+T2);
306 }
307 /*
308 Add registers back to digest.
309 */
310 signature_info->digest[0]=Trunc32(signature_info->digest[0]+A);
311 signature_info->digest[1]=Trunc32(signature_info->digest[1]+B);
312 signature_info->digest[2]=Trunc32(signature_info->digest[2]+C);
313 signature_info->digest[3]=Trunc32(signature_info->digest[3]+D);
314 signature_info->digest[4]=Trunc32(signature_info->digest[4]+E);
315 signature_info->digest[5]=Trunc32(signature_info->digest[5]+F);
316 signature_info->digest[6]=Trunc32(signature_info->digest[6]+G);
317 signature_info->digest[7]=Trunc32(signature_info->digest[7]+H);
318}
319
320/*
321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322% %
323% %
324% %
325+ U p d a t e S i g n a t u r e %
326% %
327% %
328% %
329%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330%
331% UpdateSignature() updates the SHA message digest.
332%
333% The format of the UpdateSignature method is:
334%
335% UpdateSignature(SignatureInfo *signature_info,
336% const unsigned char *message,const size_t length)
337%
338% A description of each parameter follows:
339%
340% o signature_info: The address of a structure of type SignatureInfo.
341%
342% o message: the message
343%
344% o length: The length of the message.
345%
346%
347*/
349 const unsigned char *message,const size_t length)
350{
351 ssize_t
352 i;
353
354 size_t
355 count,
356 n;
357
358 /*
359 Update the SHA digest.
360 */
361 assert(signature_info != (SignatureInfo *) NULL);
362 assert(signature_info->signature == WizardSignature);
363 n=(size_t) length;
364 count=Trunc32(signature_info->low_order+(n << 3));
365 if (count < signature_info->low_order)
366 signature_info->high_order++;
367 signature_info->low_order=count;
368 signature_info->high_order+=(n >> 29);
369 if (signature_info->offset != 0)
370 {
371 i=WizardSignatureSize-signature_info->offset;
372 if (i > (ssize_t) n)
373 i=(ssize_t) n;
374 (void) CopyWizardMemory(signature_info->message+signature_info->offset,
375 message,(size_t) i);
376 n-=i;
377 message+=i;
378 signature_info->offset+=i;
379 if (signature_info->offset != WizardSignatureSize)
380 return;
381 TransformSignature(signature_info);
382 }
383 while (n >= WizardSignatureSize)
384 {
385 (void) CopyWizardMemory(signature_info->message,message,
387 message+=WizardSignatureSize;
389 TransformSignature(signature_info);
390 }
391 (void) CopyWizardMemory(signature_info->message,message,(size_t) n);
392 signature_info->offset=(ssize_t) n;
393}
static unsigned int H(const unsigned int x, const unsigned int y, const unsigned int z)
Definition md5.c:424
static unsigned int F(const unsigned int x, const unsigned int y, const unsigned int z)
Definition md5.c:412
static unsigned int G(const unsigned int x, const unsigned int y, const unsigned int z)
Definition md5.c:418
WizardExport void * CopyWizardMemory(void *destination, const void *source, const size_t size)
Definition memory.c:700
#define WizardExport
#define WizardSignature
#define Trunc32(x)
Definition signature.c:52
#define Maj(x, y, z)
#define Suma1(x)
WizardExport void UpdateSignature(SignatureInfo *signature_info, const unsigned char *message, const size_t length)
Definition signature.c:348
#define Sigma0(x)
#define Sigma1(x)
WizardExport void GetSignatureInfo(SignatureInfo *signature_info)
Definition signature.c:145
#define Suma0(x)
WizardExport void FinalizeSignature(SignatureInfo *signature_info)
Definition signature.c:83
#define Ch(x, y, z)
static void TransformSignature(SignatureInfo *)
Definition signature.c:189
#define WizardSignatureSize
Definition signature.h:25
unsigned char message[WizardSignatureSize]
Definition signature.h:38
ssize_t offset
Definition signature.h:35
WizardBooleanType lsb_first
Definition signature.h:41
size_t signature
Definition signature.h:44
size_t low_order
Definition signature.h:31
size_t digest[8]
Definition signature.h:30
size_t high_order
Definition signature.h:32
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27