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