2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
12 % MagickCore Methods to Generate Random Numbers %
19 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % https://www.imagemagick.org/script/license.php %
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. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 % The generation of random numbers is too important to be left to chance.
36 % -- Tom Christiansen <tchrist@mox.perl.com>
47 #if defined(__MINGW32__)
50 #include "MagickCore/studio.h"
51 #include "MagickCore/exception.h"
52 #include "MagickCore/exception-private.h"
53 #include "MagickCore/image-private.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/memory-private.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/random_.h"
58 #include "MagickCore/random-private.h"
59 #include "MagickCore/resource_.h"
60 #include "MagickCore/signature-private.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/thread_.h"
63 #include "MagickCore/thread-private.h"
64 #include "MagickCore/utility.h"
65 #include "MagickCore/utility-private.h"
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
115 External declarations.
117 #if defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
118 #include <crt_externs.h>
119 #define environ (*_NSGetEnviron())
122 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
131 *random_semaphore = (SemaphoreInfo *) NULL;
136 static MagickBooleanType
137 gather_true_random = MagickFalse;
140 Forward declarations.
143 *GenerateEntropicChaos(RandomInfo *);
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % A c q u i r e R a n d o m I n f o %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % AcquireRandomInfo() allocates the RandomInfo structure.
158 % The format of the AcquireRandomInfo method is:
160 % RandomInfo *AcquireRandomInfo(void)
164 MagickExport RandomInfo *AcquireRandomInfo(void)
177 random_info=(RandomInfo *) AcquireCriticalMemory(sizeof(*random_info));
178 (void) memset(random_info,0,sizeof(*random_info));
179 random_info->signature_info=AcquireSignatureInfo();
180 random_info->nonce=AcquireStringInfo(2*GetSignatureDigestsize(
181 random_info->signature_info));
182 ResetStringInfo(random_info->nonce);
183 random_info->reservoir=AcquireStringInfo(GetSignatureDigestsize(
184 random_info->signature_info));
185 ResetStringInfo(random_info->reservoir);
186 random_info->normalize=1.0/(~0UL);
187 random_info->secret_key=secret_key;
188 random_info->protocol_major=RandomProtocolMajorVersion;
189 random_info->protocol_minor=RandomProtocolMinorVersion;
190 random_info->semaphore=AcquireSemaphoreInfo();
191 random_info->timestamp=(ssize_t) time(0);
192 random_info->signature=MagickCoreSignature;
196 nonce=GenerateEntropicChaos(random_info);
197 if (nonce == (StringInfo *) NULL)
198 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
199 InitializeSignature(random_info->signature_info);
200 UpdateSignature(random_info->signature_info,nonce);
201 FinalizeSignature(random_info->signature_info);
202 SetStringInfoLength(nonce,(GetSignatureDigestsize(
203 random_info->signature_info)+1)/2);
204 SetStringInfo(nonce,GetSignatureDigest(random_info->signature_info));
205 SetStringInfo(random_info->nonce,nonce);
206 nonce=DestroyStringInfo(nonce);
208 Seed random reservoir with entropic data.
210 entropy=GenerateEntropicChaos(random_info);
211 if (entropy == (StringInfo *) NULL)
212 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
213 UpdateSignature(random_info->signature_info,entropy);
214 FinalizeSignature(random_info->signature_info);
215 SetStringInfo(random_info->reservoir,GetSignatureDigest(
216 random_info->signature_info));
217 entropy=DestroyStringInfo(entropy);
219 Seed pseudo random number generator.
221 if (random_info->secret_key == ~0UL)
223 key=GetRandomKey(random_info,sizeof(random_info->secret_key));
224 (void) memcpy(random_info->seed,GetStringInfoDatum(key),
225 GetStringInfoLength(key));
226 key=DestroyStringInfo(key);
233 signature_info=AcquireSignatureInfo();
234 key=AcquireStringInfo(sizeof(random_info->secret_key));
235 SetStringInfoDatum(key,(unsigned char *) &random_info->secret_key);
236 UpdateSignature(signature_info,key);
237 key=DestroyStringInfo(key);
238 FinalizeSignature(signature_info);
239 digest=GetSignatureDigest(signature_info);
240 (void) memcpy(random_info->seed,GetStringInfoDatum(digest),
241 MagickMin(GetSignatureDigestsize(signature_info),
242 sizeof(*random_info->seed)));
243 signature_info=DestroySignatureInfo(signature_info);
245 random_info->seed[1]=0x50a7f451UL;
246 random_info->seed[2]=0x5365417eUL;
247 random_info->seed[3]=0xc3a4171aUL;
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 + D e s t r o y R a n d o m I n f o %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % DestroyRandomInfo() deallocates memory associated with the random
265 % The format of the DestroyRandomInfo method is:
267 % RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
269 % A description of each parameter follows:
271 % o random_info: the random info.
274 MagickExport RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
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)
285 random_info->signature_info=DestroySignatureInfo(
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);
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 + G e n e r a t e E n t r o p i c C h a o s %
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % GenerateEntropicChaos() generate entropic chaos used to initialize the
309 % The format of the GenerateEntropicChaos method is:
311 % StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
313 % A description of each parameter follows:
315 % o random_info: the random info.
319 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
320 static ssize_t ReadRandom(int file,unsigned char *source,size_t length)
322 register unsigned char
330 for (q=source; length != 0; length-=count)
332 count=(ssize_t) read(file,q,length);
347 static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
349 #define MaxEntropyExtent 64
366 Initialize random reservoir.
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)
388 pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
389 SetStringInfoLength(chaos,sizeof(pages));
390 SetStringInfoDatum(chaos,(unsigned char *) &pages);
391 ConcatenateStringInfo(entropy,chaos);
394 #if defined(MAGICKCORE_HAVE_GETRUSAGE) && defined(RUSAGE_SELF)
399 if (getrusage(RUSAGE_SELF,&usage) == 0)
401 SetStringInfoLength(chaos,sizeof(usage));
402 SetStringInfoDatum(chaos,(unsigned char *) &usage);
406 seconds=time((time_t *) 0);
408 #if defined(MAGICKCORE_HAVE_GETTIMEOFDAY)
413 if (gettimeofday(&timer,(struct timezone *) NULL) == 0)
415 seconds=timer.tv_sec;
416 nanoseconds=1000UL*timer.tv_usec;
420 #if defined(MAGICKCORE_HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME_HR)
425 if (clock_gettime(CLOCK_REALTIME_HR,&timer) == 0)
427 seconds=timer.tv_sec;
428 nanoseconds=timer.tv_nsec;
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);
439 #if defined(MAGICKCORE_HAVE_CLOCK)
442 #if defined(MAGICKCORE_HAVE_TIMES)
447 (void) times(&timer);
448 nanoseconds=timer.tms_utime+timer.tms_stime;
451 SetStringInfoLength(chaos,sizeof(nanoseconds));
452 SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
453 ConcatenateStringInfo(entropy,chaos);
454 #if defined(MAGICKCORE_HAVE_MKSTEMP)
457 path[MagickPathExtent];
462 (void) strcpy(path,"XXXXXX");
466 #if defined(MAGICKCORE_HAVE_FCHMOD)
467 (void) fchmod(file,0600);
470 setmode(file,O_BINARY);
474 (void) remove_utf8(path);
475 SetStringInfoLength(chaos,strlen(path));
476 SetStringInfoDatum(chaos,(unsigned char *) path);
477 ConcatenateStringInfo(entropy,chaos);
480 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
489 Not crytographically strong but better than nothing.
491 datum=NTElapsedTime()+NTUserTime();
492 SetStringInfoLength(chaos,sizeof(datum));
493 SetStringInfoDatum(chaos,(unsigned char *) &datum);
494 ConcatenateStringInfo(entropy,chaos);
495 if (QueryPerformanceCounter(&datum1) != 0)
497 SetStringInfoLength(chaos,sizeof(datum1));
498 SetStringInfoDatum(chaos,(unsigned char *) &datum1);
499 ConcatenateStringInfo(entropy,chaos);
502 Our best hope for true entropy.
504 SetStringInfoLength(chaos,MaxEntropyExtent);
505 (void) NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
506 ConcatenateStringInfo(entropy,chaos);
523 Not crytographically strong but better than nothing.
525 if (environ != (char **) NULL)
531 Squeeze some entropy from the sometimes unpredicatble environment.
533 for (i=0; environ[i] != (char *) NULL; i++)
535 SetStringInfoLength(chaos,strlen(environ[i]));
536 SetStringInfoDatum(chaos,(unsigned char *) environ[i]);
537 ConcatenateStringInfo(entropy,chaos);
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);
547 SetStringInfoLength(chaos,MaxEntropyExtent);
548 count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
550 SetStringInfoLength(chaos,(size_t) count);
551 ConcatenateStringInfo(entropy,chaos);
553 if (gather_true_random != MagickFalse)
556 Our best hope for true entropy.
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);
565 filename=AcquireString("/dev/srandom");
566 device=StringToStringInfo(filename);
567 device=DestroyStringInfo(device);
568 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
572 SetStringInfoLength(chaos,MaxEntropyExtent);
573 count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
575 SetStringInfoLength(chaos,(size_t) count);
576 ConcatenateStringInfo(entropy,chaos);
581 chaos=DestroyStringInfo(chaos);
582 UnlockSemaphoreInfo(random_info->semaphore);
587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % G e t P s e u d o R a n d o m V a l u e %
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 % GetPseudoRandomValue() return a non-negative double-precision floating-point
598 % value uniformly distributed over the interval [0.0, 1.0) with a 2 to the
601 % The format of the GetPseudoRandomValue method is:
603 % double GetPseudoRandomValue(RandomInfo *randon_info)
605 % A description of each parameter follows:
607 % o random_info: the random info.
610 MagickExport double GetPseudoRandomValue(RandomInfo *random_info)
612 register unsigned long
618 seed=random_info->seed;
621 alpha=(unsigned long) (seed[1] ^ (seed[1] << 11));
625 seed[0]=(seed[0] ^ (seed[0] >> 19)) ^ (alpha ^ (alpha >> 8));
626 } while (seed[0] == ~0UL);
627 return(random_info->normalize*seed[0]);
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 + G e t R a n d o m I n f o N o r m a l i z e %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % GetRandomInfoNormalize() returns the random normalize value.
643 % The format of the GetRandomInfoNormalize method is:
645 % double GetRandomInfoNormalize(const RandomInfo *random_info)
647 % A description of each parameter follows:
649 % o random_info: the random info.
652 MagickPrivate double GetRandomInfoNormalize(const RandomInfo *random_info)
654 assert(random_info != (const RandomInfo *) NULL);
655 return(random_info->normalize);
659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 + G e t R a n d o m I n f o S e e d %
667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 % GetRandomInfoSeed() returns the random seed.
671 % The format of the GetRandomInfoSeed method is:
673 % unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
675 % A description of each parameter follows:
677 % o random_info: the random info.
680 MagickPrivate unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
682 assert(random_info != (RandomInfo *) NULL);
683 return(random_info->seed);
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691 % G e t R a n d o m K e y %
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 % GetRandomKey() gets a random key from the reservoir.
699 % The format of the GetRandomKey method is:
701 % StringInfo *GetRandomKey(RandomInfo *random_info,const size_t length)
703 % A description of each parameter follows:
705 % o random_info: the random info.
707 % o length: the key length.
710 MagickExport StringInfo *GetRandomKey(RandomInfo *random_info,
716 assert(random_info != (RandomInfo *) NULL);
717 key=AcquireStringInfo(length);
718 SetRandomKey(random_info,length,GetStringInfoDatum(key));
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % G e t R a n d o m S e c r e t K e y %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % GetRandomSecretKey() returns the random secet key.
735 % The format of the GetRandomSecretKey method is:
737 % unsigned long GetRandomSecretKey(const RandomInfo *random_info)
739 % A description of each parameter follows:
741 % o random_info: the random info.
743 MagickExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
745 return(random_info->secret_key);
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % G e t R a n d o m V a l u e %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % GetRandomValue() return a non-negative double-precision floating-point
760 % value uniformly distributed over the interval [0.0, 1.0) with a 2 to the
761 % 128th-1 period (not cryptographically strong).
763 % The format of the GetRandomValue method is:
765 % double GetRandomValue(void)
768 MagickExport double GetRandomValue(RandomInfo *random_info)
777 SetRandomKey(random_info,sizeof(key),(unsigned char *) &key);
778 } while (key == range);
779 return((double) key/range);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 + R a n d o m C o m p o n e n t G e n e s i s %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % RandomComponentGenesis() instantiates the random component.
795 % The format of the RandomComponentGenesis method is:
797 % MagickBooleanType RandomComponentGenesis(void)
800 MagickPrivate MagickBooleanType RandomComponentGenesis(void)
802 if (random_semaphore == (SemaphoreInfo *) NULL)
803 random_semaphore=AcquireSemaphoreInfo();
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 + R a n d o m C o m p o n e n t T e r m i n u s %
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % RandomComponentTerminus() destroys the random component.
820 % The format of the RandomComponentTerminus method is:
822 % RandomComponentTerminus(void)
825 MagickPrivate void RandomComponentTerminus(void)
827 if (random_semaphore == (SemaphoreInfo *) NULL)
828 ActivateSemaphoreInfo(&random_semaphore);
829 RelinquishSemaphoreInfo(&random_semaphore);
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % S e t R a n d o m K e y %
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843 % SetRandomKey() sets a random key from the reservoir.
845 % The format of the SetRandomKey method is:
847 % void SetRandomKey(RandomInfo *random_info,const size_t length,
848 % unsigned char *key)
850 % A description of each parameter follows:
852 % o random_info: the random info.
854 % o length: the key length.
860 static inline void IncrementRandomNonce(StringInfo *nonce)
868 datum=GetStringInfoDatum(nonce);
869 for (i=(ssize_t) (GetStringInfoLength(nonce)-1); i != 0; i--)
875 ThrowFatalException(RandomFatalError,"SequenceWrapError");
878 MagickExport void SetRandomKey(RandomInfo *random_info,const size_t length,
884 register unsigned char
893 assert(random_info != (RandomInfo *) NULL);
896 LockSemaphoreInfo(random_info->semaphore);
897 signature_info=random_info->signature_info;
898 datum=GetStringInfoDatum(random_info->reservoir);
900 for (p=key; (i != 0) && (random_info->i != 0); i--)
902 *p++=datum[random_info->i];
904 if (random_info->i == GetSignatureDigestsize(signature_info))
907 while (i >= GetSignatureDigestsize(signature_info))
909 InitializeSignature(signature_info);
910 UpdateSignature(signature_info,random_info->nonce);
911 FinalizeSignature(signature_info);
912 IncrementRandomNonce(random_info->nonce);
913 (void) memcpy(p,GetStringInfoDatum(GetSignatureDigest(
914 signature_info)),GetSignatureDigestsize(signature_info));
915 p+=GetSignatureDigestsize(signature_info);
916 i-=GetSignatureDigestsize(signature_info);
920 InitializeSignature(signature_info);
921 UpdateSignature(signature_info,random_info->nonce);
922 FinalizeSignature(signature_info);
923 IncrementRandomNonce(random_info->nonce);
924 SetStringInfo(random_info->reservoir,GetSignatureDigest(signature_info));
926 datum=GetStringInfoDatum(random_info->reservoir);
930 UnlockSemaphoreInfo(random_info->semaphore);
934 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938 % S e t R a n d o m S e c r e t K e y %
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % SetRandomSecretKey() sets the pseudo-random number generator secret key.
946 % The format of the SetRandomSecretKey method is:
948 % void SetRandomSecretKey(const unsigned long key)
950 % A description of each parameter follows:
952 % o key: the secret seed.
955 MagickExport void SetRandomSecretKey(const unsigned long key)
961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965 % S e t R a n d o m T r u e R a n d o m %
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
971 % SetRandomTrueRandom() declares your intentions to use true random numbers.
972 % True random numbers are encouraged but may not always be practical because
973 % your application may block while entropy is gathered from your environment.
975 % The format of the SetRandomTrueRandom method is:
977 % void SetRandomTrueRandom(const MagickBooleanType true_random)
979 % A description of each parameter follows:
981 % o true_random: declare your intentions to use true-random number.
984 MagickExport void SetRandomTrueRandom(const MagickBooleanType true_random)
986 gather_true_random=true_random;