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-2017 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__) || defined(__MINGW64__)
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/semaphore.h"
56 #include "MagickCore/random_.h"
57 #include "MagickCore/random-private.h"
58 #include "MagickCore/resource_.h"
59 #include "MagickCore/signature-private.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/thread_.h"
62 #include "MagickCore/thread-private.h"
63 #include "MagickCore/utility.h"
64 #include "MagickCore/utility-private.h"
68 #define PseudoRandomHash SHA256Hash
69 #define RandomEntropyLevel 9
70 #define RandomFilename "reservoir.xdm"
71 #define RandomFiletype "random"
72 #define RandomProtocolMajorVersion 1
73 #define RandomProtocolMinorVersion 0
114 External declarations.
116 #if defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
117 #include <crt_externs.h>
118 #define environ (*_NSGetEnviron())
121 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
130 *random_semaphore = (SemaphoreInfo *) NULL;
135 static MagickBooleanType
136 gather_true_random = MagickFalse;
139 Forward declarations.
142 *GenerateEntropicChaos(RandomInfo *);
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % A c q u i r e R a n d o m I n f o %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % AcquireRandomInfo() allocates the RandomInfo structure.
157 % The format of the AcquireRandomInfo method is:
159 % RandomInfo *AcquireRandomInfo(void)
163 MagickExport RandomInfo *AcquireRandomInfo(void)
176 random_info=(RandomInfo *) AcquireMagickMemory(sizeof(*random_info));
177 if (random_info == (RandomInfo *) NULL)
178 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
179 (void) ResetMagickMemory(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=1.0/(~0UL);
188 random_info->secret_key=secret_key;
189 random_info->protocol_major=RandomProtocolMajorVersion;
190 random_info->protocol_minor=RandomProtocolMinorVersion;
191 random_info->semaphore=AcquireSemaphoreInfo();
192 random_info->timestamp=(ssize_t) time(0);
193 random_info->signature=MagickCoreSignature;
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);
209 Seed random reservoir with entropic data.
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);
220 Seed pseudo random number generator.
222 if (random_info->secret_key == ~0UL)
224 key=GetRandomKey(random_info,sizeof(random_info->secret_key));
225 (void) CopyMagickMemory(random_info->seed,GetStringInfoDatum(key),
226 GetStringInfoLength(key));
227 key=DestroyStringInfo(key);
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) CopyMagickMemory(random_info->seed,GetStringInfoDatum(digest),
242 MagickMin(GetSignatureDigestsize(signature_info),
243 sizeof(*random_info->seed)));
244 signature_info=DestroySignatureInfo(signature_info);
246 random_info->seed[1]=0x50a7f451UL;
247 random_info->seed[2]=0x5365417eUL;
248 random_info->seed[3]=0xc3a4171aUL;
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 + D e s t r o y R a n d o m I n f o %
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 % DestroyRandomInfo() deallocates memory associated with the random
266 % The format of the DestroyRandomInfo method is:
268 % RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
270 % A description of each parameter follows:
272 % o random_info: the random info.
275 MagickExport RandomInfo *DestroyRandomInfo(RandomInfo *random_info)
277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
278 assert(random_info != (RandomInfo *) NULL);
279 assert(random_info->signature == MagickCoreSignature);
280 LockSemaphoreInfo(random_info->semaphore);
281 if (random_info->reservoir != (StringInfo *) NULL)
282 random_info->reservoir=DestroyStringInfo(random_info->reservoir);
283 if (random_info->nonce != (StringInfo *) NULL)
284 random_info->nonce=DestroyStringInfo(random_info->nonce);
285 if (random_info->signature_info != (SignatureInfo *) NULL)
286 random_info->signature_info=DestroySignatureInfo(
287 random_info->signature_info);
288 (void) ResetMagickMemory(random_info->seed,0,sizeof(*random_info->seed));
289 random_info->signature=(~MagickCoreSignature);
290 UnlockSemaphoreInfo(random_info->semaphore);
291 RelinquishSemaphoreInfo(&random_info->semaphore);
292 random_info=(RandomInfo *) RelinquishMagickMemory(random_info);
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 + G e n e r a t e E n t r o p i c C h a o s %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % GenerateEntropicChaos() generate entropic chaos used to initialize the
310 % The format of the GenerateEntropicChaos method is:
312 % StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
314 % A description of each parameter follows:
316 % o random_info: the random info.
320 #if !defined(MAGICKCORE_WINDOWS_SUPPORT)
321 static ssize_t ReadRandom(int file,unsigned char *source,size_t length)
323 register unsigned char
331 for (q=source; length != 0; length-=count)
333 count=(ssize_t) read(file,q,length);
348 static StringInfo *GenerateEntropicChaos(RandomInfo *random_info)
350 #define MaxEntropyExtent 64
367 Initialize random reservoir.
369 entropy=AcquireStringInfo(0);
370 LockSemaphoreInfo(random_info->semaphore);
371 chaos=AcquireStringInfo(sizeof(unsigned char *));
372 SetStringInfoDatum(chaos,(unsigned char *) &entropy);
373 ConcatenateStringInfo(entropy,chaos);
374 SetStringInfoDatum(chaos,(unsigned char *) entropy);
375 ConcatenateStringInfo(entropy,chaos);
376 pid=(ssize_t) getpid();
377 SetStringInfoLength(chaos,sizeof(pid));
378 SetStringInfoDatum(chaos,(unsigned char *) &pid);
379 ConcatenateStringInfo(entropy,chaos);
380 tid=GetMagickThreadId();
381 SetStringInfoLength(chaos,sizeof(tid));
382 SetStringInfoDatum(chaos,(unsigned char *) &tid);
383 ConcatenateStringInfo(entropy,chaos);
384 #if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
389 pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
390 SetStringInfoLength(chaos,sizeof(pages));
391 SetStringInfoDatum(chaos,(unsigned char *) &pages);
392 ConcatenateStringInfo(entropy,chaos);
395 #if defined(MAGICKCORE_HAVE_GETRUSAGE) && defined(RUSAGE_SELF)
400 if (getrusage(RUSAGE_SELF,&usage) == 0)
402 SetStringInfoLength(chaos,sizeof(usage));
403 SetStringInfoDatum(chaos,(unsigned char *) &usage);
407 seconds=time((time_t *) 0);
409 #if defined(MAGICKCORE_HAVE_GETTIMEOFDAY)
414 if (gettimeofday(&timer,(struct timezone *) NULL) == 0)
416 seconds=timer.tv_sec;
417 nanoseconds=1000UL*timer.tv_usec;
421 #if defined(MAGICKCORE_HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME_HR)
426 if (clock_gettime(CLOCK_REALTIME_HR,&timer) == 0)
428 seconds=timer.tv_sec;
429 nanoseconds=timer.tv_nsec;
433 SetStringInfoLength(chaos,sizeof(seconds));
434 SetStringInfoDatum(chaos,(unsigned char *) &seconds);
435 ConcatenateStringInfo(entropy,chaos);
436 SetStringInfoLength(chaos,sizeof(nanoseconds));
437 SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
438 ConcatenateStringInfo(entropy,chaos);
440 #if defined(MAGICKCORE_HAVE_CLOCK)
443 #if defined(MAGICKCORE_HAVE_TIMES)
448 (void) times(&timer);
449 nanoseconds=timer.tms_utime+timer.tms_stime;
452 SetStringInfoLength(chaos,sizeof(nanoseconds));
453 SetStringInfoDatum(chaos,(unsigned char *) &nanoseconds);
454 ConcatenateStringInfo(entropy,chaos);
455 #if defined(MAGICKCORE_HAVE_MKSTEMP)
458 path[MagickPathExtent];
463 (void) strcpy(path,"XXXXXX");
467 #if defined(MAGICKCORE_HAVE_FCHMOD)
468 (void) fchmod(file,0600);
471 setmode(file,O_BINARY);
475 (void) remove_utf8(path);
476 SetStringInfoLength(chaos,strlen(path));
477 SetStringInfoDatum(chaos,(unsigned char *) path);
478 ConcatenateStringInfo(entropy,chaos);
481 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
490 Not crytographically strong but better than nothing.
492 datum=NTElapsedTime()+NTUserTime();
493 SetStringInfoLength(chaos,sizeof(datum));
494 SetStringInfoDatum(chaos,(unsigned char *) &datum);
495 ConcatenateStringInfo(entropy,chaos);
496 if (QueryPerformanceCounter(&datum1) != 0)
498 SetStringInfoLength(chaos,sizeof(datum1));
499 SetStringInfoDatum(chaos,(unsigned char *) &datum1);
500 ConcatenateStringInfo(entropy,chaos);
503 Our best hope for true entropy.
505 SetStringInfoLength(chaos,MaxEntropyExtent);
506 (void) NTGatherRandomData(MaxEntropyExtent,GetStringInfoDatum(chaos));
507 ConcatenateStringInfo(entropy,chaos);
524 Not crytographically strong but better than nothing.
526 if (environ != (char **) NULL)
532 Squeeze some entropy from the sometimes unpredicatble environment.
534 for (i=0; environ[i] != (char *) NULL; i++)
536 SetStringInfoLength(chaos,strlen(environ[i]));
537 SetStringInfoDatum(chaos,(unsigned char *) environ[i]);
538 ConcatenateStringInfo(entropy,chaos);
541 filename=AcquireString("/dev/urandom");
542 device=StringToStringInfo(filename);
543 device=DestroyStringInfo(device);
544 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
545 filename=DestroyString(filename);
548 SetStringInfoLength(chaos,MaxEntropyExtent);
549 count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
551 SetStringInfoLength(chaos,(size_t) count);
552 ConcatenateStringInfo(entropy,chaos);
554 if (gather_true_random != MagickFalse)
557 Our best hope for true entropy.
559 filename=AcquireString("/dev/random");
560 device=StringToStringInfo(filename);
561 device=DestroyStringInfo(device);
562 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
563 filename=DestroyString(filename);
566 filename=AcquireString("/dev/srandom");
567 device=StringToStringInfo(filename);
568 device=DestroyStringInfo(device);
569 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
573 SetStringInfoLength(chaos,MaxEntropyExtent);
574 count=ReadRandom(file,GetStringInfoDatum(chaos),MaxEntropyExtent);
576 SetStringInfoLength(chaos,(size_t) count);
577 ConcatenateStringInfo(entropy,chaos);
582 chaos=DestroyStringInfo(chaos);
583 UnlockSemaphoreInfo(random_info->semaphore);
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
592 % G e t P s e u d o R a n d o m V a l u e %
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 % GetPseudoRandomValue() return a non-negative double-precision floating-point
599 % value uniformly distributed over the interval [0.0, 1.0) with a 2 to the
602 % The format of the GetPseudoRandomValue method is:
604 % double GetPseudoRandomValue(RandomInfo *randon_info)
606 % A description of each parameter follows:
608 % o random_info: the random info.
611 MagickExport double GetPseudoRandomValue(RandomInfo *random_info)
613 register unsigned long
619 seed=random_info->seed;
622 alpha=(unsigned long) (seed[1] ^ (seed[1] << 11));
626 seed[0]=(seed[0] ^ (seed[0] >> 19)) ^ (alpha ^ (alpha >> 8));
627 } while (seed[0] == ~0UL);
628 return(random_info->normalize*seed[0]);
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 + G e t R a n d o m I n f o N o r m a l i z e %
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % GetRandomInfoNormalize() returns the random normalize value.
644 % The format of the GetRandomInfoNormalize method is:
646 % double GetRandomInfoNormalize(const RandomInfo *random_info)
648 % A description of each parameter follows:
650 % o random_info: the random info.
653 MagickPrivate double GetRandomInfoNormalize(const RandomInfo *random_info)
655 assert(random_info != (const RandomInfo *) NULL);
656 return(random_info->normalize);
660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 + G e t R a n d o m I n f o S e e d %
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670 % GetRandomInfoSeed() returns the random seed.
672 % The format of the GetRandomInfoSeed method is:
674 % unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
676 % A description of each parameter follows:
678 % o random_info: the random info.
681 MagickPrivate unsigned long *GetRandomInfoSeed(RandomInfo *random_info)
683 assert(random_info != (RandomInfo *) NULL);
684 return(random_info->seed);
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 % G e t R a n d o m K e y %
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 % GetRandomKey() gets a random key from the reservoir.
700 % The format of the GetRandomKey method is:
702 % StringInfo *GetRandomKey(RandomInfo *random_info,const size_t length)
704 % A description of each parameter follows:
706 % o random_info: the random info.
708 % o length: the key length.
711 MagickExport StringInfo *GetRandomKey(RandomInfo *random_info,
717 assert(random_info != (RandomInfo *) NULL);
718 key=AcquireStringInfo(length);
719 SetRandomKey(random_info,length,GetStringInfoDatum(key));
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 % G e t R a n d o m S e c r e t K e y %
732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % GetRandomSecretKey() returns the random secet key.
736 % The format of the GetRandomSecretKey method is:
738 % unsigned long GetRandomSecretKey(const RandomInfo *random_info)
740 % A description of each parameter follows:
742 % o random_info: the random info.
744 MagickExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
746 return(random_info->secret_key);
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % G e t R a n d o m V a l u e %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % GetRandomValue() return a non-negative double-precision floating-point
761 % value uniformly distributed over the interval [0.0, 1.0) with a 2 to the
762 % 128th-1 period (not cryptographically strong).
764 % The format of the GetRandomValue method is:
766 % double GetRandomValue(void)
769 MagickExport double GetRandomValue(RandomInfo *random_info)
778 SetRandomKey(random_info,sizeof(key),(unsigned char *) &key);
779 } while (key == range);
780 return((double) key/range);
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 + R a n d o m C o m p o n e n t G e n e s i s %
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % RandomComponentGenesis() instantiates the random component.
796 % The format of the RandomComponentGenesis method is:
798 % MagickBooleanType RandomComponentGenesis(void)
801 MagickPrivate MagickBooleanType RandomComponentGenesis(void)
803 if (random_semaphore == (SemaphoreInfo *) NULL)
804 random_semaphore=AcquireSemaphoreInfo();
809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 + R a n d o m C o m p o n e n t T e r m i n u s %
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819 % RandomComponentTerminus() destroys the random component.
821 % The format of the RandomComponentTerminus method is:
823 % RandomComponentTerminus(void)
826 MagickPrivate void RandomComponentTerminus(void)
828 if (random_semaphore == (SemaphoreInfo *) NULL)
829 ActivateSemaphoreInfo(&random_semaphore);
830 RelinquishSemaphoreInfo(&random_semaphore);
834 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838 % S e t R a n d o m K e y %
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
844 % SetRandomKey() sets a random key from the reservoir.
846 % The format of the SetRandomKey method is:
848 % void SetRandomKey(RandomInfo *random_info,const size_t length,
849 % unsigned char *key)
851 % A description of each parameter follows:
853 % o random_info: the random info.
855 % o length: the key length.
861 static inline void IncrementRandomNonce(StringInfo *nonce)
869 datum=GetStringInfoDatum(nonce);
870 for (i=(ssize_t) (GetStringInfoLength(nonce)-1); i != 0; i--)
876 ThrowFatalException(RandomFatalError,"SequenceWrapError");
879 MagickExport void SetRandomKey(RandomInfo *random_info,const size_t length,
885 register unsigned char
894 assert(random_info != (RandomInfo *) NULL);
897 LockSemaphoreInfo(random_info->semaphore);
898 signature_info=random_info->signature_info;
899 datum=GetStringInfoDatum(random_info->reservoir);
901 for (p=key; (i != 0) && (random_info->i != 0); i--)
903 *p++=datum[random_info->i];
905 if (random_info->i == GetSignatureDigestsize(signature_info))
908 while (i >= GetSignatureDigestsize(signature_info))
910 InitializeSignature(signature_info);
911 UpdateSignature(signature_info,random_info->nonce);
912 FinalizeSignature(signature_info);
913 IncrementRandomNonce(random_info->nonce);
914 (void) CopyMagickMemory(p,GetStringInfoDatum(GetSignatureDigest(
915 signature_info)),GetSignatureDigestsize(signature_info));
916 p+=GetSignatureDigestsize(signature_info);
917 i-=GetSignatureDigestsize(signature_info);
921 InitializeSignature(signature_info);
922 UpdateSignature(signature_info,random_info->nonce);
923 FinalizeSignature(signature_info);
924 IncrementRandomNonce(random_info->nonce);
925 SetStringInfo(random_info->reservoir,GetSignatureDigest(signature_info));
927 datum=GetStringInfoDatum(random_info->reservoir);
931 UnlockSemaphoreInfo(random_info->semaphore);
935 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
939 % S e t R a n d o m S e c r e t K e y %
943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
945 % SetRandomSecretKey() sets the pseudo-random number generator secret key.
947 % The format of the SetRandomSecretKey method is:
949 % void SetRandomSecretKey(const unsigned long key)
951 % A description of each parameter follows:
953 % o key: the secret seed.
956 MagickExport void SetRandomSecretKey(const unsigned long key)
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
966 % S e t R a n d o m T r u e R a n d o m %
970 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % SetRandomTrueRandom() declares your intentions to use true random numbers.
973 % True random numbers are encouraged but may not always be practical because
974 % your application may block while entropy is gathered from your environment.
976 % The format of the SetRandomTrueRandom method is:
978 % void SetRandomTrueRandom(const MagickBooleanType true_random)
980 % A description of each parameter follows:
982 % o true_random: declare your intentions to use true-random number.
985 MagickExport void SetRandomTrueRandom(const MagickBooleanType true_random)
987 gather_true_random=true_random;