MagickCore  7.0.8
Convert, Edit, Or Compose Bitmap Images
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 % MagickCore Methods to Compute a Message Digest for an Image %
13 % %
14 % Software Design %
15 % Cristy %
16 % December 1992 %
17 % %
18 % %
19 % Copyright 1999-2019 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 "MagickCore/studio.h"
43 #include "MagickCore/cache.h"
44 #include "MagickCore/exception.h"
46 #include "MagickCore/property.h"
47 #include "MagickCore/image.h"
48 #include "MagickCore/memory_.h"
51 #include "MagickCore/quantum.h"
53 #include "MagickCore/signature.h"
55 #include "MagickCore/string_.h"
56 /*
57  Define declarations.
58 */
59 #define SignatureBlocksize 64
60 #define SignatureDigestsize 32
61 
62 /*
63  Typedef declarations.
64 */
66 {
67  unsigned int
69  blocksize;
70 
73  *message;
74 
75  unsigned int
77  low_order,
78  high_order;
79 
80  size_t
82 
85 
86  ssize_t
88 
89  size_t
91 };
92 
93 /*
94  Forward declarations.
95 */
96 static void
98 
99 /*
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % %
102 % %
103 % %
104 + A c q u i r e S i g n a t u r e I n f o %
105 % %
106 % %
107 % %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 %
110 % AcquireSignatureInfo() allocate the SignatureInfo structure.
111 %
112 % The format of the AcquireSignatureInfo method is:
113 %
114 % SignatureInfo *AcquireSignatureInfo(void)
115 %
116 */
118 {
120  *signature_info;
121 
122  unsigned long
123  lsb_first;
124 
125  signature_info=(SignatureInfo *) AcquireCriticalMemory(
126  sizeof(*signature_info));
127  (void) memset(signature_info,0,sizeof(*signature_info));
128  signature_info->digestsize=SignatureDigestsize;
129  signature_info->blocksize=SignatureBlocksize;
132  signature_info->accumulator=(unsigned int *) AcquireQuantumMemory(
133  SignatureBlocksize,sizeof(*signature_info->accumulator));
134  if (signature_info->accumulator == (unsigned int *) NULL)
135  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
136  (void) memset(signature_info->accumulator,0,SignatureBlocksize*
137  sizeof(*signature_info->accumulator));
138  lsb_first=1;
139  signature_info->lsb_first=(int) (*(char *) &lsb_first) == 1 ? MagickTrue :
140  MagickFalse;
141  signature_info->timestamp=(ssize_t) time((time_t *) NULL);
142  signature_info->signature=MagickCoreSignature;
143  InitializeSignature(signature_info);
144  return(signature_info);
145 }
146 
147 /*
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % %
150 % %
151 % %
152 + D e s t r o y S i g n a t u r e I n f o %
153 % %
154 % %
155 % %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %
158 % DestroySignatureInfo() zeros memory associated with the SignatureInfo
159 % structure.
160 %
161 % The format of the DestroySignatureInfo method is:
162 %
163 % SignatureInfo *DestroySignatureInfo(SignatureInfo *signature_info)
164 %
165 % A description of each parameter follows:
166 %
167 % o signature_info: the cipher signature_info.
168 %
169 */
171 {
172  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
173  assert(signature_info != (SignatureInfo *) NULL);
174  assert(signature_info->signature == MagickCoreSignature);
175  if (signature_info->accumulator != (unsigned int *) NULL)
176  signature_info->accumulator=(unsigned int *) RelinquishMagickMemory(
177  signature_info->accumulator);
178  if (signature_info->message != (StringInfo *) NULL)
179  signature_info->message=DestroyStringInfo(signature_info->message);
180  if (signature_info->digest != (StringInfo *) NULL)
181  signature_info->digest=DestroyStringInfo(signature_info->digest);
182  signature_info->signature=(~MagickCoreSignature);
183  signature_info=(SignatureInfo *) RelinquishMagickMemory(signature_info);
184  return(signature_info);
185 }
186 
187 /*
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % %
190 % %
191 % %
192 + F i n a l i z e S i g n a t u r e %
193 % %
194 % %
195 % %
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 %
198 % FinalizeSignature() finalizes the Signature message accumulator computation.
199 %
200 % The format of the FinalizeSignature method is:
201 %
202 % FinalizeSignature(SignatureInfo *signature_info)
203 %
204 % A description of each parameter follows:
205 %
206 % o signature_info: the address of a structure of type SignatureInfo.
207 %
208 */
210 {
211  register ssize_t
212  i;
213 
214  register unsigned char
215  *q;
216 
217  register unsigned int
218  *p;
219 
220  size_t
221  extent;
222 
223  unsigned char
224  *datum;
225 
226  unsigned int
227  high_order,
228  low_order;
229 
230  /*
231  Add padding and return the message accumulator.
232  */
233  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
234  assert(signature_info != (SignatureInfo *) NULL);
235  assert(signature_info->signature == MagickCoreSignature);
236  low_order=signature_info->low_order;
237  high_order=signature_info->high_order;
238  extent=((low_order >> 3) & 0x3f);
239  datum=GetStringInfoDatum(signature_info->message);
240  datum[extent++]=(unsigned char) 0x80;
241  if (extent <= (unsigned int) (GetStringInfoLength(signature_info->message)-8))
242  (void) memset(datum+extent,0,GetStringInfoLength(
243  signature_info->message)-8-extent);
244  else
245  {
246  (void) memset(datum+extent,0,GetStringInfoLength(
247  signature_info->message)-extent);
248  TransformSignature(signature_info);
249  (void) memset(datum,0,GetStringInfoLength(
250  signature_info->message)-8);
251  }
252  datum[56]=(unsigned char) (high_order >> 24);
253  datum[57]=(unsigned char) (high_order >> 16);
254  datum[58]=(unsigned char) (high_order >> 8);
255  datum[59]=(unsigned char) high_order;
256  datum[60]=(unsigned char) (low_order >> 24);
257  datum[61]=(unsigned char) (low_order >> 16);
258  datum[62]=(unsigned char) (low_order >> 8);
259  datum[63]=(unsigned char) low_order;
260  TransformSignature(signature_info);
261  p=signature_info->accumulator;
262  q=GetStringInfoDatum(signature_info->digest);
263  for (i=0; i < (SignatureDigestsize/4); i++)
264  {
265  *q++=(unsigned char) ((*p >> 24) & 0xff);
266  *q++=(unsigned char) ((*p >> 16) & 0xff);
267  *q++=(unsigned char) ((*p >> 8) & 0xff);
268  *q++=(unsigned char) (*p & 0xff);
269  p++;
270  }
271 }
272 
273 /*
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % %
276 % %
277 % %
278 + G e t S i g n a t u r e B l o c k s i z e %
279 % %
280 % %
281 % %
282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 %
284 % GetSignatureBlocksize() returns the Signature blocksize.
285 %
286 % The format of the GetSignatureBlocksize method is:
287 %
288 % unsigned int *GetSignatureBlocksize(const SignatureInfo *signature_info)
289 %
290 % A description of each parameter follows:
291 %
292 % o signature_info: the signature info.
293 %
294 */
296  const SignatureInfo *signature_info)
297 {
298  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
299  assert(signature_info != (SignatureInfo *) NULL);
300  assert(signature_info->signature == MagickCoreSignature);
301  return(signature_info->blocksize);
302 }
303 
304 /*
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % %
307 % %
308 % %
309 + G e t S i g n a t u r e D i g e s t %
310 % %
311 % %
312 % %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 %
315 % GetSignatureDigest() returns the signature digest.
316 %
317 % The format of the GetSignatureDigest method is:
318 %
319 % const StringInfo *GetSignatureDigest(const SignatureInfo *signature_info)
320 %
321 % A description of each parameter follows:
322 %
323 % o signature_info: the signature info.
324 %
325 */
327  const SignatureInfo *signature_info)
328 {
329  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
330  assert(signature_info != (SignatureInfo *) NULL);
331  assert(signature_info->signature == MagickCoreSignature);
332  return(signature_info->digest);
333 }
334 
335 /*
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 % %
338 % %
339 % %
340 + G e t S i g n a t u r e D i g e s t s i z e %
341 % %
342 % %
343 % %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 %
346 % GetSignatureDigestsize() returns the Signature digest size.
347 %
348 % The format of the GetSignatureDigestsize method is:
349 %
350 % unsigned int *GetSignatureDigestsize(const SignatureInfo *signature_info)
351 %
352 % A description of each parameter follows:
353 %
354 % o signature_info: the signature info.
355 %
356 */
358  const SignatureInfo *signature_info)
359 {
360  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
361  assert(signature_info != (SignatureInfo *) NULL);
362  assert(signature_info->signature == MagickCoreSignature);
363  return(signature_info->digestsize);
364 }
365 
366 /*
367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 % %
369 % %
370 % %
371 + I n i t i a l i z e S i g n a t u r e %
372 % %
373 % %
374 % %
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 %
377 % InitializeSignature() initializes the Signature accumulator.
378 %
379 % The format of the DestroySignatureInfo method is:
380 %
381 % void InitializeSignatureInfo(SignatureInfo *signature_info)
382 %
383 % A description of each parameter follows:
384 %
385 % o signature_info: the cipher signature_info.
386 %
387 */
389 {
390  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
391  assert(signature_info != (SignatureInfo *) NULL);
392  assert(signature_info->signature == MagickCoreSignature);
393  signature_info->accumulator[0]=0x6a09e667U;
394  signature_info->accumulator[1]=0xbb67ae85U;
395  signature_info->accumulator[2]=0x3c6ef372U;
396  signature_info->accumulator[3]=0xa54ff53aU;
397  signature_info->accumulator[4]=0x510e527fU;
398  signature_info->accumulator[5]=0x9b05688cU;
399  signature_info->accumulator[6]=0x1f83d9abU;
400  signature_info->accumulator[7]=0x5be0cd19U;
401  signature_info->low_order=0;
402  signature_info->high_order=0;
403  signature_info->extent=0;
404 }
405 
406 /*
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 % %
409 % %
410 % %
411 + S e t S i g n a t u r e D i g e s t %
412 % %
413 % %
414 % %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 %
417 % SetSignatureDigest() set the signature digest.
418 %
419 % The format of the SetSignatureDigest method is:
420 %
421 % SetSignatureDigest(SignatureInfo *signature_info,
422 % const StringInfo *digest)
423 %
424 % A description of each parameter follows:
425 %
426 % o signature_info: the signature info.
427 %
428 % o digest: the digest.
429 %
430 */
432  const StringInfo *digest)
433 {
434  /*
435  Set the signature accumulator.
436  */
437  assert(signature_info != (SignatureInfo *) NULL);
438  assert(signature_info->signature == MagickCoreSignature);
439  SetStringInfo(signature_info->digest,digest);
440 }
441 
442 /*
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % %
445 % %
446 % %
447 % S i g n a t u r e I m a g e %
448 % %
449 % %
450 % %
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 %
453 % SignatureImage() computes a message digest from an image pixel stream with
454 % an implementation of the NIST SHA-256 Message Digest algorithm. This
455 % signature uniquely identifies the image and is convenient for determining
456 % if an image has been modified or whether two images are identical.
457 %
458 % The format of the SignatureImage method is:
459 %
460 % MagickBooleanType SignatureImage(Image *image,ExceptionInfo *exception)
461 %
462 % A description of each parameter follows:
463 %
464 % o image: the image.
465 %
466 % o exception: return any errors or warnings in this structure.
467 %
468 */
470  ExceptionInfo *exception)
471 {
472  CacheView
473  *image_view;
474 
475  char
476  *hex_signature;
477 
478  double
479  pixel;
480 
481  register const Quantum
482  *p;
483 
485  *signature_info;
486 
487  ssize_t
488  y;
489 
490  StringInfo
491  *signature;
492 
493  unsigned char
494  *pixels;
495 
496  /*
497  Compute image digital signature.
498  */
499  assert(image != (Image *) NULL);
500  assert(image->signature == MagickCoreSignature);
501  if (image->debug != MagickFalse)
502  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
503  signature_info=AcquireSignatureInfo();
504  signature=AcquireStringInfo(GetPixelChannels(image)*image->columns*
505  sizeof(pixel));
506  image_view=AcquireVirtualCacheView(image,exception);
507  for (y=0; y < (ssize_t) image->rows; y++)
508  {
509  register ssize_t
510  x;
511 
512  register unsigned char
513  *q;
514 
515  p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
516  if (p == (const Quantum *) NULL)
517  break;
518  SetStringInfoLength(signature,GetPixelChannels(image)*image->columns*
519  sizeof(pixel));
520  pixels=GetStringInfoDatum(signature);
521  q=pixels;
522  for (x=0; x < (ssize_t) image->columns; x++)
523  {
524  register ssize_t
525  i;
526 
527  if (GetPixelReadMask(image,p) <= (QuantumRange/2))
528  {
529  p+=GetPixelChannels(image);
530  continue;
531  }
532  for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
533  {
534  register ssize_t
535  j;
536 
537  PixelChannel channel = GetPixelChannelChannel(image,i);
538  PixelTrait traits = GetPixelChannelTraits(image,channel);
539  if (traits == UndefinedPixelTrait)
540  continue;
541  pixel=QuantumScale*p[i];
542  if (signature_info->lsb_first == MagickFalse)
543  for (j=(ssize_t) sizeof(pixel)-1; j >= 0; j--)
544  *q++=(unsigned char) ((unsigned char *) &pixel)[j];
545  else
546  for (j=0; j < (ssize_t) sizeof(pixel); j++)
547  *q++=(unsigned char) ((unsigned char *) &pixel)[j];
548  }
549  p+=GetPixelChannels(image);
550  }
551  SetStringInfoLength(signature,(size_t) (q-pixels));
552  UpdateSignature(signature_info,signature);
553  }
554  image_view=DestroyCacheView(image_view);
555  FinalizeSignature(signature_info);
556  hex_signature=StringInfoToHexString(GetSignatureDigest(signature_info));
557  (void) DeleteImageProperty(image,"signature");
558  (void) SetImageProperty(image,"signature",hex_signature,exception);
559  /*
560  Free resources.
561  */
562  hex_signature=DestroyString(hex_signature);
563  signature=DestroyStringInfo(signature);
564  signature_info=DestroySignatureInfo(signature_info);
565  return(MagickTrue);
566 }
567 
568 /*
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % %
571 % %
572 % %
573 + T r a n s f o r m S i g n a t u r e %
574 % %
575 % %
576 % %
577 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 %
579 % TransformSignature() transforms the Signature message accumulator.
580 %
581 % The format of the TransformSignature method is:
582 %
583 % TransformSignature(SignatureInfo *signature_info)
584 %
585 % A description of each parameter follows:
586 %
587 % o signature_info: the address of a structure of type SignatureInfo.
588 %
589 */
590 static void TransformSignature(SignatureInfo *signature_info)
591 {
592 #define Ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
593 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
594 #define RotateRight(x,n) (Trunc32(((x) >> n) | ((x) << (32-n))))
595 #define Sigma0(x) (RotateRight(x,7) ^ RotateRight(x,18) ^ Trunc32((x) >> 3))
596 #define Sigma1(x) (RotateRight(x,17) ^ RotateRight(x,19) ^ Trunc32((x) >> 10))
597 #define Suma0(x) (RotateRight(x,2) ^ RotateRight(x,13) ^ RotateRight(x,22))
598 #define Suma1(x) (RotateRight(x,6) ^ RotateRight(x,11) ^ RotateRight(x,25))
599 #define Trunc32(x) ((unsigned int) ((x) & 0xffffffffU))
600 
601  register ssize_t
602  i;
603 
604  register unsigned char
605  *p;
606 
607  ssize_t
608  j;
609 
610  static const unsigned int
611  K[64] =
612  {
613  0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU,
614  0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U, 0xd807aa98U, 0x12835b01U,
615  0x243185beU, 0x550c7dc3U, 0x72be5d74U, 0x80deb1feU, 0x9bdc06a7U,
616  0xc19bf174U, 0xe49b69c1U, 0xefbe4786U, 0x0fc19dc6U, 0x240ca1ccU,
617  0x2de92c6fU, 0x4a7484aaU, 0x5cb0a9dcU, 0x76f988daU, 0x983e5152U,
618  0xa831c66dU, 0xb00327c8U, 0xbf597fc7U, 0xc6e00bf3U, 0xd5a79147U,
619  0x06ca6351U, 0x14292967U, 0x27b70a85U, 0x2e1b2138U, 0x4d2c6dfcU,
620  0x53380d13U, 0x650a7354U, 0x766a0abbU, 0x81c2c92eU, 0x92722c85U,
621  0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U,
622  0xd6990624U, 0xf40e3585U, 0x106aa070U, 0x19a4c116U, 0x1e376c08U,
623  0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU,
624  0x682e6ff3U, 0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U,
625  0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U
626  }; /* 32-bit fractional part of the cube root of the first 64 primes */
627 
628  unsigned int
629  A,
630  B,
631  C,
632  D,
633  E,
634  F,
635  G,
636  H,
637  shift,
638  T,
639  T1,
640  T2,
641  W[64];
642 
643  shift=32;
644  p=GetStringInfoDatum(signature_info->message);
645  if (signature_info->lsb_first == MagickFalse)
646  {
647 DisableMSCWarning(4127)
648  if (sizeof(unsigned int) <= 4)
650  for (i=0; i < 16; i++)
651  {
652  T=(*((unsigned int *) p));
653  p+=4;
654  W[i]=Trunc32(T);
655  }
656  else
657  for (i=0; i < 16; i+=2)
658  {
659  T=(*((unsigned int *) p));
660  p+=8;
661  W[i]=Trunc32(T >> shift);
662  W[i+1]=Trunc32(T);
663  }
664  }
665  else
666 DisableMSCWarning(4127)
667  if (sizeof(unsigned int) <= 4)
669  for (i=0; i < 16; i++)
670  {
671  T=(*((unsigned int *) p));
672  p+=4;
673  W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
674  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
675  }
676  else
677  for (i=0; i < 16; i+=2)
678  {
679  T=(*((unsigned int *) p));
680  p+=8;
681  W[i]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
682  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
683  T>>=shift;
684  W[i+1]=((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
685  ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
686  }
687  /*
688  Copy accumulator to registers.
689  */
690  A=signature_info->accumulator[0];
691  B=signature_info->accumulator[1];
692  C=signature_info->accumulator[2];
693  D=signature_info->accumulator[3];
694  E=signature_info->accumulator[4];
695  F=signature_info->accumulator[5];
696  G=signature_info->accumulator[6];
697  H=signature_info->accumulator[7];
698  for (i=16; i < 64; i++)
699  W[i]=Trunc32(Sigma1(W[i-2])+W[i-7]+Sigma0(W[i-15])+W[i-16]);
700  for (j=0; j < 64; j++)
701  {
702  T1=Trunc32(H+Suma1(E)+Ch(E,F,G)+K[j]+W[j]);
703  T2=Trunc32(Suma0(A)+Maj(A,B,C));
704  H=G;
705  G=F;
706  F=E;
707  E=Trunc32(D+T1);
708  D=C;
709  C=B;
710  B=A;
711  A=Trunc32(T1+T2);
712  }
713  /*
714  Add registers back to accumulator.
715  */
716  signature_info->accumulator[0]=Trunc32(signature_info->accumulator[0]+A);
717  signature_info->accumulator[1]=Trunc32(signature_info->accumulator[1]+B);
718  signature_info->accumulator[2]=Trunc32(signature_info->accumulator[2]+C);
719  signature_info->accumulator[3]=Trunc32(signature_info->accumulator[3]+D);
720  signature_info->accumulator[4]=Trunc32(signature_info->accumulator[4]+E);
721  signature_info->accumulator[5]=Trunc32(signature_info->accumulator[5]+F);
722  signature_info->accumulator[6]=Trunc32(signature_info->accumulator[6]+G);
723  signature_info->accumulator[7]=Trunc32(signature_info->accumulator[7]+H);
724  /*
725  Reset working registers.
726  */
727  A=0;
728  B=0;
729  C=0;
730  D=0;
731  E=0;
732  F=0;
733  G=0;
734  H=0;
735  T=0;
736  T1=0;
737  T2=0;
738  (void) memset(W,0,sizeof(W));
739 }
740 
741 /*
742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
743 % %
744 % %
745 % %
746 + U p d a t e S i g n a t u r e %
747 % %
748 % %
749 % %
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 %
752 % UpdateSignature() updates the Signature message accumulator.
753 %
754 % The format of the UpdateSignature method is:
755 %
756 % UpdateSignature(SignatureInfo *signature_info,const StringInfo *message)
757 %
758 % A description of each parameter follows:
759 %
760 % o signature_info: the address of a structure of type SignatureInfo.
761 %
762 % o message: the message.
763 %
764 */
766  const StringInfo *message)
767 {
768  register size_t
769  i;
770 
771  register unsigned char
772  *p;
773 
774  size_t
775  n;
776 
777  unsigned int
778  length;
779 
780  /*
781  Update the Signature accumulator.
782  */
783  assert(signature_info != (SignatureInfo *) NULL);
784  assert(signature_info->signature == MagickCoreSignature);
785  n=GetStringInfoLength(message);
786  length=Trunc32((unsigned int) (signature_info->low_order+(n << 3)));
787  if (length < signature_info->low_order)
788  signature_info->high_order++;
789  signature_info->low_order=length;
790  signature_info->high_order+=(unsigned int) n >> 29;
791  p=GetStringInfoDatum(message);
792  if (signature_info->extent != 0)
793  {
794  i=GetStringInfoLength(signature_info->message)-signature_info->extent;
795  if (i > n)
796  i=n;
797  (void) memcpy(GetStringInfoDatum(signature_info->message)+
798  signature_info->extent,p,i);
799  n-=i;
800  p+=i;
801  signature_info->extent+=i;
802  if (signature_info->extent != GetStringInfoLength(signature_info->message))
803  return;
804  TransformSignature(signature_info);
805  }
806  while (n >= GetStringInfoLength(signature_info->message))
807  {
808  SetStringInfoDatum(signature_info->message,p);
809  p+=GetStringInfoLength(signature_info->message);
810  n-=GetStringInfoLength(signature_info->message);
811  TransformSignature(signature_info);
812  }
813  (void) memcpy(GetStringInfoDatum(signature_info->message),p,n);
814  signature_info->extent=n;
815 }
size_t rows
Definition: image.h:172
MagickExport CacheView * DestroyCacheView(CacheView *cache_view)
Definition: cache-view.c:252
MagickPrivate void InitializeSignature(SignatureInfo *signature_info)
Definition: signature.c:388
#define Suma1(x)
#define DisableMSCWarning(nr)
Definition: studio.h:351
unsigned int high_order
Definition: signature.c:76
unsigned int low_order
Definition: signature.c:76
size_t extent
Definition: signature.c:81
static void TransformSignature(SignatureInfo *)
Definition: signature.c:590
#define ThrowFatalException(severity, tag)
#define Ch(x, y, z)
static PixelTrait GetPixelChannelTraits(const Image *magick_restrict image, const PixelChannel channel)
static void * AcquireCriticalMemory(const size_t size)
static Quantum GetPixelReadMask(const Image *magick_restrict image, const Quantum *magick_restrict pixel)
MagickExport MagickBooleanType SignatureImage(Image *image, ExceptionInfo *exception)
Definition: signature.c:469
MagickExport const Quantum * GetCacheViewVirtualPixels(const CacheView *cache_view, const ssize_t x, const ssize_t y, const size_t columns, const size_t rows, ExceptionInfo *exception)
Definition: cache-view.c:651
size_t signature
Definition: signature.c:90
#define SignatureBlocksize
Definition: signature.c:59
StringInfo * digest
Definition: signature.c:72
StringInfo * message
Definition: signature.c:72
unsigned int * accumulator
Definition: signature.c:76
MagickPrivate SignatureInfo * AcquireSignatureInfo(void)
Definition: signature.c:117
Definition: log.h:52
#define Sigma1(x)
Definition: image.h:151
MagickPrivate void FinalizeSignature(SignatureInfo *signature_info)
Definition: signature.c:209
#define MagickCoreSignature
#define SignatureDigestsize
Definition: signature.c:60
MagickExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition: string.c:1295
MagickBooleanType
Definition: magick-type.h:158
MagickExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition: string.c:850
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:533
#define RestoreMSCWarning
Definition: studio.h:352
MagickExport MagickBooleanType SetImageProperty(Image *image, const char *property, const char *value, ExceptionInfo *exception)
Definition: property.c:4114
MagickPrivate unsigned int GetSignatureDigestsize(const SignatureInfo *signature_info)
Definition: signature.c:357
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1398
size_t signature
Definition: image.h:354
#define QuantumScale
Definition: magick-type.h:115
size_t columns
Definition: image.h:172
PixelChannel
Definition: pixel.h:67
unsigned int blocksize
Definition: signature.c:68
MagickExport StringInfo * AcquireStringInfo(const size_t length)
Definition: string.c:187
static size_t GetPixelChannels(const Image *magick_restrict image)
unsigned int digestsize
Definition: signature.c:68
MagickPrivate unsigned int GetSignatureBlocksize(const SignatureInfo *signature_info)
Definition: signature.c:295
char filename[MagickPathExtent]
Definition: image.h:319
#define GetMagickModule()
Definition: log.h:28
MagickPrivate void UpdateSignature(SignatureInfo *signature_info, const StringInfo *message)
Definition: signature.c:765
static PixelChannel GetPixelChannelChannel(const Image *magick_restrict image, const ssize_t offset)
MagickExport CacheView * AcquireVirtualCacheView(const Image *image, ExceptionInfo *exception)
Definition: cache-view.c:149
MagickBooleanType lsb_first
Definition: signature.c:84
unsigned short Quantum
Definition: magick-type.h:82
ssize_t timestamp
Definition: signature.c:87
MagickExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition: string.c:1825
MagickExport char * DestroyString(char *string)
Definition: string.c:823
MagickExport MagickBooleanType DeleteImageProperty(Image *image, const char *property)
Definition: property.c:279
MagickPrivate const StringInfo * GetSignatureDigest(const SignatureInfo *signature_info)
Definition: signature.c:326
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1054
#define Trunc32(x)
#define MagickPrivate
#define MagickExport
#define Maj(x, y, z)
MagickPrivate void SetSignatureDigest(SignatureInfo *signature_info, const StringInfo *digest)
Definition: signature.c:431
MagickExport char * StringInfoToHexString(const StringInfo *string_info)
Definition: string.c:2013
#define Suma0(x)
PixelTrait
Definition: pixel.h:135
MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition: string.c:1324
#define Sigma0(x)
#define QuantumRange
Definition: magick-type.h:83
MagickExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
Definition: string.c:1792
MagickBooleanType debug
Definition: image.h:334
MagickExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition: string.c:1752
MagickPrivate SignatureInfo * DestroySignatureInfo(SignatureInfo *signature_info)
Definition: signature.c:170