MagickCore  6.9.13-17
Convert, Edit, Or Compose Bitmap Images
random.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % RRRR AAA N N DDDD OOO M M %
6 % R R A A NN N D D O O MM MM %
7 % RRRR AAAAA N N N D D O O M M M %
8 % R R A A N NN D D O O M M %
9 % R R A A N N DDDD OOO M M %
10 % %
11 % %
12 % MagickCore Methods to Generate Random Numbers %
13 % %
14 % Software Design %
15 % Cristy %
16 % December 2001 %
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 % The generation of random numbers is too important to be left to chance.
36 % -- Tom Christiansen <tchrist@mox.perl.com>
37 %
38 %
39 */
40 ␌
41 /*
42  Include declarations.
43 */
44 #if defined(__VMS)
45 #include <time.h>
46 #endif
47 #if defined(__MINGW32__)
48 #include <sys/time.h>
49 #endif
50 #include "magick/studio.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/image-private.h"
54 #include "magick/memory_.h"
55 #include "magick/memory-private.h"
56 #include "magick/random_.h"
57 #include "magick/resource_.h"
58 #include "magick/semaphore.h"
59 #include "magick/signature-private.h"
60 #include "magick/string_.h"
61 #include "magick/thread_.h"
62 #include "magick/thread-private.h"
63 #include "magick/timer-private.h"
64 #include "magick/utility-private.h"
65 #if defined(MAGICKCORE_HAVE_GETENTROPY)
66 #include <sys/random.h>
67 #endif
68 /*
69  Define declarations.
70 */
71 #define PseudoRandomHash SHA256Hash
72 #define RandomEntropyLevel 9
73 #define RandomFilename "reservoir.xdm"
74 #define RandomFiletype "random"
75 #define RandomProtocolMajorVersion 1
76 #define RandomProtocolMinorVersion 0
77 ␌
78 /*
79  Typedef declarations.
80 */
82 {
84  *signature_info;
85 
87  *nonce,
88  *reservoir;
89 
90  size_t
91  i;
92 
93  MagickSizeType
94  seed[4];
95 
96  double
97  normalize;
98 
99  unsigned long
100  secret_key;
101 
102  unsigned short
103  protocol_major,
104  protocol_minor;
105 
107  *semaphore;
108 
109  time_t
110  timestamp;
111 
112  size_t
113  signature;
114 };
115 ␌
116 /*
117  External declarations.
118 */
119 #if defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
120 #include <crt_externs.h>
121 #define environ (*_NSGetEnviron())
122 #endif
123 
124 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
125 extern char
126  **environ;
127 #endif
128 ␌
129 /*
130  Global declarations.
131 */
132 static SemaphoreInfo
133  *random_semaphore = (SemaphoreInfo *) NULL;
134 
135 static unsigned long
136  secret_key = ~0UL;
137 
138 static MagickBooleanType
139  gather_true_random = MagickFalse;
140 ␌
141 /*
142  Forward declarations.
143 */
144 static StringInfo
145  *GenerateEntropicChaos(RandomInfo *);
146 ␌
147 /*
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % %
150 % %
151 % %
152 % A c q u i r e R a n d o m I n f o %
153 % %
154 % %
155 % %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %
158 % AcquireRandomInfo() allocates the RandomInfo structure.
159 %
160 % The format of the AcquireRandomInfo method is:
161 %
162 % RandomInfo *AcquireRandomInfo(void)
163 %
164 */
165 MagickExport RandomInfo *AcquireRandomInfo(void)
166 {
167  const StringInfo
168  *digest;
169 
170  RandomInfo
171  *random_info;
172 
173  StringInfo
174  *entropy,
175  *key,
176  *nonce;
177 
178  random_info=(RandomInfo *) AcquireCriticalMemory(sizeof(*random_info));
179  (void) memset(random_info,0,sizeof(*random_info));
180  random_info->signature_info=AcquireSignatureInfo();
181  random_info->nonce=AcquireStringInfo(2*GetSignatureDigestsize(
182  random_info->signature_info));
183  ResetStringInfo(random_info->nonce);
184  random_info->reservoir=AcquireStringInfo(GetSignatureDigestsize(
185  random_info->signature_info));
186  ResetStringInfo(random_info->reservoir);
187  random_info->normalize=(double) (1.0/(MagickULLConstant(~0) >> 11));
188  random_info->seed[0]=MagickULLConstant(0x76e15d3efefdcbbf);
189  random_info->seed[1]=MagickULLConstant(0xc5004e441c522fb3);
190  random_info->seed[2]=MagickULLConstant(0x77710069854ee241);
191  random_info->seed[3]=MagickULLConstant(0x39109bb02acbe635);
192  random_info->secret_key=secret_key;
193  random_info->protocol_major=RandomProtocolMajorVersion;
194  random_info->protocol_minor=RandomProtocolMinorVersion;
195  random_info->semaphore=AllocateSemaphoreInfo();
196  random_info->timestamp=GetMagickTime();
197  random_info->signature=MagickCoreSignature;
198  /*
199  Seed random nonce.
200  */
201  nonce=GenerateEntropicChaos(random_info);
202  if (nonce == (StringInfo *) NULL)
203  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
204  InitializeSignature(random_info->signature_info);
205  UpdateSignature(random_info->signature_info,nonce);
206  FinalizeSignature(random_info->signature_info);
207  SetStringInfoLength(nonce,(GetSignatureDigestsize(
208  random_info->signature_info)+1)/2);
209  SetStringInfo(nonce,GetSignatureDigest(random_info->signature_info));
210  SetStringInfo(random_info->nonce,nonce);
211  nonce=DestroyStringInfo(nonce);
212  /*
213  Seed random reservoir with entropic data.
214  */
215  entropy=GenerateEntropicChaos(random_info);
216  if (entropy == (StringInfo *) NULL)
217  ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
218  UpdateSignature(random_info->signature_info,entropy);
219  FinalizeSignature(random_info->signature_info);
220  SetStringInfo(random_info->reservoir,GetSignatureDigest(
221  random_info->signature_info));
222  entropy=DestroyStringInfo(entropy);
223  /*
224  Seed pseudo random number generator.
225  */
226  if (random_info->secret_key == ~0UL)
227  {
228  key=GetRandomKey(random_info,sizeof(random_info->seed));
229  (void) memcpy(random_info->seed,GetStringInfoDatum(key),
230  sizeof(random_info->seed));
231  key=DestroyStringInfo(key);
232  }
233  else
234  {
236  *signature_info;
237 
238  signature_info=AcquireSignatureInfo();
239  key=AcquireStringInfo(sizeof(random_info->secret_key));
240  SetStringInfoDatum(key,(unsigned char *) &random_info->secret_key);
241  UpdateSignature(signature_info,key);
242  key=DestroyStringInfo(key);
243  FinalizeSignature(signature_info);
244  digest=GetSignatureDigest(signature_info);
245  (void) memcpy(random_info->seed,GetStringInfoDatum(digest),
246  MagickMin((size_t) GetSignatureDigestsize(signature_info),
247  sizeof(random_info->seed)));
248  signature_info=DestroySignatureInfo(signature_info);
249  }
250  return(random_info);
251 }
252 ␌
253 /*
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % %
256 % %
257 % %
258 + D e s t r o y R a n d o m I n f o %
259 % %
260 % %
261 % %
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 %
264 % DestroyRandomInfo() deallocates memory associated with the random
265 % reservoir.
266 %
267 % The format of the DestroyRandomInfo method is:
268 %
269 % RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
270 %
271 % A description of each parameter follows:
272 %
273 % o random_info: the random info.
274 %
275 */
276 MagickExport RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
277 {
278  assert(random_info != (RandomInfo *) NULL);
279  assert(random_info->signature == MagickCoreSignature);
280  if (IsEventLogging() != MagickFalse)
281  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
282  LockSemaphoreInfo(random_info->semaphore);
283  if (random_info->reservoir != (StringInfo *) NULL)
284  random_info->reservoir=DestroyStringInfo(random_info->reservoir);
285  if (random_info->nonce != (StringInfo *) NULL)
286  random_info->nonce=DestroyStringInfo(random_info->nonce);
287  if (random_info->signature_info != (SignatureInfo *) NULL)
288  random_info->signature_info=DestroySignatureInfo(
289  random_info->signature_info);
290  (void) memset(random_info->seed,0,sizeof(random_info->seed));
291  random_info->signature=(~MagickCoreSignature);
292  UnlockSemaphoreInfo(random_info->semaphore);
293  DestroySemaphoreInfo(&random_info->semaphore);
294  random_info=(RandomInfo *) RelinquishMagickMemory(random_info);
295  return(random_info);
296 }
297 ␌
298 /*
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % %
301 % %
302 % %
303 + G e n e r a t e E n t r o p i c C h a o s %
304 % %
305 % %
306 % %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 %
309 % GenerateEntropicChaos() generate entropic chaos used to initialize the
310 % random reservoir.
311 %
312 % The format of the GenerateEntropicChaos method is:
313 %
314 % StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
315 %
316 % A description of each parameter follows:
317 %
318 % o random_info: the random info.
319 %
320 */
321 
322 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
323 static ssize_t ReadRandom(int file,unsigned char *source,size_t length)
324 {
325  unsigned char
326  *q;
327 
328  ssize_t
329  offset,
330  count;
331 
332  offset=0;
333  for (q=source; length != 0; length-=count)
334  {
335  count=(ssize_t) read(file,q,length);
336  if (count <= 0)
337  {
338  count=0;
339  if (errno == EINTR)
340  continue;
341  return(-1);
342  }
343  q+=count;
344  offset+=count;
345  }
346  return(offset);
347 }
348 #endif
349 
350 static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
351 {
352 #define MaxEntropyExtent 64 /* max permitted: 256 */
353 
354  MagickThreadType
355  tid;
356 
357  StringInfo
358  *chaos,
359  *entropy;
360 
361  size_t
362  nanoseconds,
363  seconds;
364 
365  ssize_t
366  pid;
367 
368  /*
369  Initialize random reservoir.
370  */
371  entropy=AcquireStringInfo(0);
372  LockSemaphoreInfo(random_info->semaphore);
373 #if defined(MAGICKCORE_HAVE_GETENTROPY)
374  {
375  int
376  status;
377 
378  SetStringInfoLength(entropy,MaxEntropyExtent);
379  status=getentropy(GetStringInfoDatum(entropy),MaxEntropyExtent);
380  if (status == 0)
381  {
382  UnlockSemaphoreInfo(random_info->semaphore);
383  return(entropy);
384  }
385  }
386 #endif
387  chaos=AcquireStringInfo(sizeof(unsigned char *));
388  SetStringInfoDatum(chaos,(unsigned char *) &entropy);
389  ConcatenateStringInfo(entropy,chaos);
390  SetStringInfoDatum(chaos,(unsigned char *) entropy);
391  ConcatenateStringInfo(entropy,chaos);
392  pid=(ssize_t) getpid();
393  SetStringInfoLength(chaos,sizeof(pid));
394  SetStringInfoDatum(chaos,(unsigned char *) &pid);
395  ConcatenateStringInfo(entropy,chaos);
396  tid=GetMagickThreadId();
397  SetStringInfoLength(chaos,sizeof(tid));
398  SetStringInfoDatum(chaos,(unsigned char *) &tid);
399  ConcatenateStringInfo(entropy,chaos);
400 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
401  {
402  ssize_t
403  pages;
404 
405  pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
406  SetStringInfoLength(chaos,sizeof(pages));
407  SetStringInfoDatum(chaos,(unsigned char *) &pages);
408  ConcatenateStringInfo(entropy,chaos);
409  }
410 #endif
411 #if defined(MAGICKCORE_HAVE_GETRUSAGE) && defined(RUSAGE_SELF)
412  {
413  struct rusage
414  usage;
415 
416  if (getrusage(RUSAGE_SELF,&usage) == 0)
417  {
418  SetStringInfoLength(chaos,sizeof(usage));
419  SetStringInfoDatum(chaos,(unsigned char *) &usage);
420  }
421  }
422 #endif
423  seconds=time((time_t *) 0);
424  nanoseconds=0;
425 #if defined(MAGICKCORE_HAVE_GETTIMEOFDAY)
426  {
427  struct timeval
428  timer;
429 
430  if (gettimeofday(&timer,(struct timezone *) NULL) == 0)
431  {
432  seconds=(size_t) timer.tv_sec;
433  nanoseconds=(size_t) (1000UL*timer.tv_usec);
434  }
435  }
436 #endif
437 #if defined(MAGICKCORE_HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME_HR)
438  {
439  struct timespec
440  timer;
441 
442  if (clock_gettime(CLOCK_REALTIME_HR,&timer) == 0)
443  {
444  seconds=timer.tv_sec;
445  nanoseconds=timer.tv_nsec;
446  }
447  }
448 #endif
449  SetStringInfoLength(chaos,sizeof(seconds));
450  SetStringInfoDatum(chaos,(unsigned char *) &seconds);
451  ConcatenateStringInfo(entropy,chaos);
452  SetStringInfoLength(chaos,sizeof(nanoseconds));
453  SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
454  ConcatenateStringInfo(entropy,chaos);
455  nanoseconds=0;
456 #if defined(MAGICKCORE_HAVE_CLOCK)
457  nanoseconds=clock();
458 #endif
459 #if defined(MAGICKCORE_HAVE_TIMES)
460  {
461  struct tms
462  timer;
463 
464  (void) times(&timer);
465  nanoseconds=timer.tms_utime+timer.tms_stime;
466  }
467 #endif
468  SetStringInfoLength(chaos,sizeof(nanoseconds));
469  SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
470  ConcatenateStringInfo(entropy,chaos);
471 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
472  {
473  double
474  seconds;
475 
476  LARGE_INTEGER
477  nanoseconds;
478 
479  /*
480  Not cryptographically strong but better than nothing.
481  */
482  seconds=NTElapsedTime()+NTUserTime();
483  SetStringInfoLength(chaos,sizeof(seconds));
484  SetStringInfoDatum(chaos,(unsigned char *) &seconds);
485  ConcatenateStringInfo(entropy,chaos);
486  if (QueryPerformanceCounter(&nanoseconds) != 0)
487  {
488  SetStringInfoLength(chaos,sizeof(nanoseconds));
489  SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
490  ConcatenateStringInfo(entropy,chaos);
491  }
492  /*
493  Our best hope for true entropy.
494  */
495  SetStringInfoLength(chaos,MaxEntropyExtent);
496  (void) NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
497  ConcatenateStringInfo(entropy,chaos);
498  }
499 #else
500  {
501  char
502  *filename;
503 
504  int
505  file;
506 
507  ssize_t
508  count;
509 
510  StringInfo
511  *device;
512 
513  /*
514  Not cryptographically strong but better than nothing.
515  */
516  if (environ != (char **) NULL)
517  {
518  ssize_t
519  i;
520 
521  /*
522  Squeeze some entropy from the sometimes unpredictable environment.
523  */
524  for (i=0; environ[i] != (char *) NULL; i++)
525  {
526  SetStringInfoLength(chaos,strlen(environ[i]));
527  SetStringInfoDatum(chaos,(unsigned char *) environ[i]);
528  ConcatenateStringInfo(entropy,chaos);
529  }
530  }
531  filename=AcquireString("/dev/urandom");
532  device=StringToStringInfo(filename);
533  device=DestroyStringInfo(device);
534  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
535  filename=DestroyString(filename);
536  if (file != -1)
537  {
538  SetStringInfoLength(chaos,MaxEntropyExtent);
539  count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
540  (void) close(file);
541  SetStringInfoLength(chaos,(size_t) count);
542  ConcatenateStringInfo(entropy,chaos);
543  }
544  if (gather_true_random != MagickFalse)
545  {
546  /*
547  Our best hope for true entropy.
548  */
549  filename=AcquireString("/dev/random");
550  device=StringToStringInfo(filename);
551  device=DestroyStringInfo(device);
552  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
553  filename=DestroyString(filename);
554  if (file == -1)
555  {
556  filename=AcquireString("/dev/srandom");
557  device=StringToStringInfo(filename);
558  device=DestroyStringInfo(device);
559  file=open_utf8(filename,O_RDONLY | O_BINARY,0);
560  }
561  if (file != -1)
562  {
563  SetStringInfoLength(chaos,MaxEntropyExtent);
564  count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
565  (void) close(file);
566  SetStringInfoLength(chaos,(size_t) count);
567  ConcatenateStringInfo(entropy,chaos);
568  }
569  }
570  }
571 #endif
572  chaos=DestroyStringInfo(chaos);
573  UnlockSemaphoreInfo(random_info->semaphore);
574  return(entropy);
575 }
576 ␌
577 /*
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % %
580 % %
581 % %
582 % G e t P s e u d o R a n d o m V a l u e %
583 % %
584 % %
585 % %
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 %
588 % GetPseudoRandomValue() is a Xoshiro generator that returns a non-negative
589 % double-precision floating-point value uniformly distributed over the
590 % interval [0.0, 1.0) with a 2 to the 256th-1 period.
591 %
592 % The format of the GetPseudoRandomValue method is:
593 %
594 % double GetPseudoRandomValue(RandomInfo *randon_info)
595 %
596 % A description of each parameter follows:
597 %
598 % o random_info: the random info.
599 %
600 */
601 MagickExport double GetPseudoRandomValue(
602  RandomInfo *magick_restrict random_info)
603 {
604 #define RandomROTL(x,k) (((x) << (k)) | ((x) >> (64-(k))))
605 
606  const MagickSizeType
607  alpha = (random_info->seed[1] << 17),
608  value = (random_info->seed[0]+random_info->seed[3]);
609 
610  random_info->seed[2]^=random_info->seed[0];
611  random_info->seed[3]^=random_info->seed[1];
612  random_info->seed[1]^=random_info->seed[2];
613  random_info->seed[0]^=random_info->seed[3];
614  random_info->seed[2]^=alpha;
615  random_info->seed[3]=RandomROTL(random_info->seed[3],45);
616  return((double) ((value >> 11)*random_info->normalize));
617 }
618 ␌
619 /*
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621 % %
622 % %
623 % %
624 + G e t R a n d o m I n f o N o r m a l i z e %
625 % %
626 % %
627 % %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 %
630 % GetRandomInfoNormalize() returns the random normalize value.
631 %
632 % The format of the GetRandomInfoNormalize method is:
633 %
634 % double GetRandomInfoNormalize(const RandomInfo *random_info)
635 %
636 % A description of each parameter follows:
637 %
638 % o random_info: the random info.
639 %
640 */
641 MagickPrivate double GetRandomInfoNormalize(const RandomInfo *random_info)
642 {
643  assert(random_info != (const RandomInfo *) NULL);
644  return(random_info->normalize);
645 }
646 ␌
647 /*
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % %
650 % %
651 % %
652 + G e t R a n d o m I n f o S e e d %
653 % %
654 % %
655 % %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 %
658 % GetRandomInfoSeed() returns the random seed.
659 %
660 % The format of the GetRandomInfoSeed method is:
661 %
662 % unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
663 %
664 % A description of each parameter follows:
665 %
666 % o random_info: the random info.
667 %
668 */
669 MagickPrivate unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
670 {
671  assert(random_info != (RandomInfo *) NULL);
672  return((unsigned long *) random_info->seed);
673 }
674 ␌
675 /*
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % %
678 % %
679 % %
680 % G e t R a n d o m K e y %
681 % %
682 % %
683 % %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 %
686 % GetRandomKey() gets a random key from the reservoir.
687 %
688 % The format of the GetRandomKey method is:
689 %
690 % StringInfo *GetRandomKey(RandomInfo *random_info,const size_t length)
691 %
692 % A description of each parameter follows:
693 %
694 % o random_info: the random info.
695 %
696 % o length: the key length.
697 %
698 */
699 MagickExport StringInfo *GetRandomKey(RandomInfo *random_info,
700  const size_t length)
701 {
702  StringInfo
703  *key;
704 
705  assert(random_info != (RandomInfo *) NULL);
706  key=AcquireStringInfo(length);
707  SetRandomKey(random_info,length,GetStringInfoDatum(key));
708  return(key);
709 }
710 ␌
711 /*
712 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % %
714 % %
715 % %
716 % G e t R a n d o m S e c r e t K e y %
717 % %
718 % %
719 % %
720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 %
722 % GetRandomSecretKey() returns the random secret key.
723 %
724 % The format of the GetRandomSecretKey method is:
725 %
726 % unsigned long GetRandomSecretKey(const RandomInfo *random_info)
727 %
728 % A description of each parameter follows:
729 %
730 % o random_info: the random info.
731 */
732 MagickExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
733 {
734  return(random_info->secret_key);
735 }
736 ␌
737 /*
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 % %
740 % %
741 % %
742 % G e t R a n d o m V a l u e %
743 % %
744 % %
745 % %
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
747 %
748 % GetRandomValue() return a non-negative double-precision floating-point
749 % value uniformly distributed over the interval [0.0, 1.0) with a 2 to the
750 % 128th-1 period (not cryptographically strong).
751 %
752 % The format of the GetRandomValue method is:
753 %
754 % double GetRandomValue(void)
755 %
756 */
757 MagickExport double GetRandomValue(RandomInfo *random_info)
758 {
759  unsigned long
760  key,
761  range;
762 
763  range=(~0UL);
764  do
765  {
766  SetRandomKey(random_info,sizeof(key),(unsigned char *) &key);
767  } while (key == range);
768  return((double) key/range);
769 }
770 ␌
771 /*
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % %
774 % %
775 % %
776 + R a n d o m C o m p o n e n t G e n e s i s %
777 % %
778 % %
779 % %
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 %
782 % RandomComponentGenesis() instantiates the random component.
783 %
784 % The format of the RandomComponentGenesis method is:
785 %
786 % MagickBooleanType RandomComponentGenesis(void)
787 %
788 */
789 MagickExport MagickBooleanType RandomComponentGenesis(void)
790 {
791  if (random_semaphore == (SemaphoreInfo *) NULL)
792  random_semaphore=AllocateSemaphoreInfo();
793  return(MagickTrue);
794 }
795 ␌
796 /*
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % %
799 % %
800 % %
801 + R a n d o m C o m p o n e n t T e r m i n u s %
802 % %
803 % %
804 % %
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806 %
807 % RandomComponentTerminus() destroys the random component.
808 %
809 % The format of the RandomComponentTerminus method is:
810 %
811 % RandomComponentTerminus(void)
812 %
813 */
814 MagickExport void RandomComponentTerminus(void)
815 {
816  if (random_semaphore == (SemaphoreInfo *) NULL)
817  ActivateSemaphoreInfo(&random_semaphore);
818  DestroySemaphoreInfo(&random_semaphore);
819 }
820 ␌
821 /*
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % %
824 % %
825 % %
826 % S e t R a n d o m K e y %
827 % %
828 % %
829 % %
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
831 %
832 % SetRandomKey() sets a random key from the reservoir.
833 %
834 % The format of the SetRandomKey method is:
835 %
836 % void SetRandomKey(RandomInfo *random_info,const size_t length,
837 % unsigned char *key)
838 %
839 % A description of each parameter follows:
840 %
841 % o random_info: the random info.
842 %
843 % o length: the key length.
844 %
845 % o key: the key.
846 %
847 */
848 
849 static inline void IncrementRandomNonce(StringInfo *nonce)
850 {
851  ssize_t
852  i;
853 
854  unsigned char
855  *datum;
856 
857  datum=GetStringInfoDatum(nonce);
858  for (i=(ssize_t) (GetStringInfoLength(nonce)-1); i != 0; i--)
859  {
860  datum[i]++;
861  if (datum[i] != 0)
862  return;
863  }
864  ThrowFatalException(RandomFatalError,"SequenceWrapError");
865 }
866 
867 MagickExport void SetRandomKey(RandomInfo *random_info,const size_t length,
868  unsigned char *key)
869 {
870  size_t
871  i;
872 
873  unsigned char
874  *p;
875 
877  *signature_info;
878 
879  unsigned char
880  *datum;
881 
882  assert(random_info != (RandomInfo *) NULL);
883  if (length == 0)
884  return;
885  LockSemaphoreInfo(random_info->semaphore);
886  signature_info=random_info->signature_info;
887  datum=GetStringInfoDatum(random_info->reservoir);
888  i=length;
889  for (p=key; (i != 0) && (random_info->i != 0); i--)
890  {
891  *p++=datum[random_info->i];
892  random_info->i++;
893  if (random_info->i == GetSignatureDigestsize(signature_info))
894  random_info->i=0;
895  }
896  while (i >= GetSignatureDigestsize(signature_info))
897  {
898  InitializeSignature(signature_info);
899  UpdateSignature(signature_info,random_info->nonce);
900  FinalizeSignature(signature_info);
901  IncrementRandomNonce(random_info->nonce);
902  (void) memcpy(p,GetStringInfoDatum(GetSignatureDigest(
903  signature_info)),GetSignatureDigestsize(signature_info));
904  p+=GetSignatureDigestsize(signature_info);
905  i-=GetSignatureDigestsize(signature_info);
906  }
907  if (i != 0)
908  {
909  InitializeSignature(signature_info);
910  UpdateSignature(signature_info,random_info->nonce);
911  FinalizeSignature(signature_info);
912  IncrementRandomNonce(random_info->nonce);
913  SetStringInfo(random_info->reservoir,GetSignatureDigest(signature_info));
914  random_info->i=i;
915  datum=GetStringInfoDatum(random_info->reservoir);
916  while (i-- != 0)
917  p[i]=datum[i];
918  }
919  UnlockSemaphoreInfo(random_info->semaphore);
920 }
921 ␌
922 /*
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % %
925 % %
926 % %
927 % S e t R a n d o m S e c r e t K e y %
928 % %
929 % %
930 % %
931 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932 %
933 % SetRandomSecretKey() sets the pseudo-random number generator secret key.
934 %
935 % The format of the SetRandomSecretKey method is:
936 %
937 % void SetRandomSecretKey(const unsigned long key)
938 %
939 % A description of each parameter follows:
940 %
941 % o key: the secret key.
942 %
943 */
944 
945 MagickExport void SeedPseudoRandomGenerator(const unsigned long seed)
946 {
947  SetRandomSecretKey(seed);
948 }
949 
950 MagickExport void SetRandomSecretKey(const unsigned long key)
951 {
952  secret_key=key;
953 }
954 ␌
955 /*
956 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 % %
958 % %
959 % %
960 % S e t R a n d o m T r u e R a n d o m %
961 % %
962 % %
963 % %
964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965 %
966 % SetRandomTrueRandom() declares your intentions to use true random numbers.
967 % True random numbers are encouraged but may not always be practical because
968 % your application may block while entropy is gathered from your environment.
969 %
970 % The format of the SetRandomTrueRandom method is:
971 %
972 % void SetRandomTrueRandom(const MagickBooleanType true_random)
973 %
974 % A description of each parameter follows:
975 %
976 % o true_random: declare your intentions to use true-random number.
977 %
978 */
979 MagickExport void SetRandomTrueRandom(const MagickBooleanType true_random)
980 {
981  gather_true_random=true_random;
982 }