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