blob: b31101d9c8ff3f57538c29e49cf535f9bbf257fc [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 2010 David Sugar, Tycho Softworks.
2//
3// This file is part of GNU uCommon C++.
4//
5// GNU uCommon C++ is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published
7// by the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GNU uCommon C++ is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This library holds basic cryptographic functions and secure socket support
20 * for use with GNU uCommon C++. This library might be used in conjunction
21 * with openssl, gnutls, etc. If no secure socket library is available, then
22 * a stub library may be used with very basic cryptographic support.
23 * @file ucommon/secure.h
24 */
25
26/**
27 * Example of SSL socket code.
28 * @example ssl.cpp
29 */
30
31/**
32 * Example of cryptographic digest code.
33 * @example digest.cpp
34 */
35
36/**
37 * Example of cipher code.
38 * @example cipher.cpp
39 */
40
41#ifndef _UCOMMON_SECURE_H_
42#define _UCOMMON_SECURE_H_
43
44#ifndef _UCOMMON_CONFIG_H_
45#include <ucommon/platform.h>
46#endif
47
48#ifndef _UCOMMON_UCOMMON_H_
49#include <ucommon/ucommon.h>
50#endif
51
52#define MAX_CIPHER_KEYSIZE 512
53#define MAX_DIGEST_HASHSIZE 512
54
55NAMESPACE_UCOMMON
56
57/**
58 * Common secure socket support. This offers common routines needed for
59 * secure/ssl socket support code.
60 * @author David Sugar <dyfet@gnutelephony.org>
61 */
62class __SHARED secure
63{
64public:
65 /**
66 * Different error states of the security context.
67 */
68 typedef enum {OK=0, INVALID, MISSING_CERTIFICATE, MISSING_PRIVATEKEY, INVALID_CERTIFICATE, INVALID_AUTHORITY, INVALID_PEERNAME, INVALID_CIPHER} error_t;
69
70protected:
71 /**
72 * Last error flagged for this context.
73 */
74 error_t error;
75
76 inline secure() {error = OK;};
77
78public:
79 /**
80 * This is derived in different back-end libraries, and will be used to
81 * clear certificate credentials.
82 */
83 virtual ~secure();
84
85 /**
86 * Convenience type to represent a security context.
87 */
88 typedef secure *client_t;
89
90 typedef secure *server_t;
91
92 /**
93 * Convenience type to represent a secure socket session.
94 */
95 typedef void *session_t;
96
97 /**
98 * Convenience type to represent a secure socket buf i/o stream.
99 */
100 typedef void *bufio_t;
101
102 /**
103 * Initialize secure stack for first use, and report if SSL support is
104 * compiled in.
105 * @return true if ssl support is available, false if not.
106 */
107 static bool init(void);
108
109 /**
110 * Initialize secure stack with fips support. If fips support is not
111 * successfully enabled, the secure stack is also not initialized. Hence
112 * init() can be used for non-fips certified operation if fips fails.
113 * @return true if fips support enabled and stack initialized.
114 */
115 static bool fips(void);
116
117 /**
118 * Copy system certificates to a local path.
119 * @param path to copy to.
120 * @return 0 or error number on failure.
121 */
122 static int oscerts(const char *path);
123
124 /**
125 * Get path to system certificates.
126 * @return path to system certificates.
127 */
128 static const char *oscerts(void);
129
130 /**
131 * Verify a certificate chain through your certificate authority.
132 * This uses the ca loaded as an optional argument for client and
133 * server. Optionally the hostname of the connection can also be
134 * verified by pulling the peer certificate.
135 * @param session that is connected.
136 * @param peername that we expect.
137 * @return secure error level or secure::OK if none.
138 */
139 static error_t verify(session_t session, const char *peername = NULL);
140
141 /**
142 * Create a sever context. The certificate file used will be based on
143 * the init() method name. This may often be /etc/ssl/certs/initname.pem.
144 * Similarly, a matching private key certificate will also be loaded. An
145 * optional certificate authority document can be used when we are
146 * establishing a service which ssl clients have their own certificates.
147 * @param authority path to use or NULL if none.
148 * @return a security context that is cast from derived library.
149 */
150 static server_t server(const char *keyfile = NULL, const char *authority = NULL);
151
152 /**
153 * Create an anonymous client context with an optional authority to
154 * validate.
155 * @param authority path to use or NULL if none.
156 * @return a basic client security context.
157 */
158 static client_t client(const char *authority = NULL);
159
160 /**
161 * Create a peer user client context. This assumes a user certificate
162 * in ~/.ssl/certs and the user private key in ~/.ssl/private. The
163 * path to an authority is also sent.
164 * @param authority path to use.
165 */
166 static client_t user(const char *authority);
167
168 /**
169 * Assign a non-default cipher to the context.
170 * @param context to set cipher for.
171 * @param ciphers to set.
172 */
173 static void cipher(secure *context, const char *ciphers);
174
175 /**
176 * Determine if the current security context is valid.
177 * @return true if valid, -1 if not.
178 */
179 inline bool is_valid(void) const
180 {return error == OK;};
181
182 /**
183 * Get last error code associated with the security context.
184 * @return last error code or 0/OK if none.
185 */
186 inline error_t err(void)
187 {return error;};
188
189 /**
190 * Create 36 character traditional version 1 uuid.
191 * @param string to write uuid into, must be 37 bytes or more.
192 */
193 static void uuid(char *string);
194
195 static String uuid(void);
196
197 template <typename T>
198 inline static void erase(T *object)
199 {memset(object, 0, sizeof(object)); delete object;}
200
201 inline operator bool()
202 {return is_valid();}
203
204 inline bool operator!()
205 {return !is_valid();}
206
207};
208
209/**
210 * Secure socket buffer. This is used to create ssl socket connections
211 * for both clients and servers. The use depends in part on the type of
212 * context created and passed at construction time. If no context is
213 * passed (NULL), then this reverts to TCPBuffer behavior.
214 * @author David Sugar <dyfet@gnutelephony.org>
215 */
216class __SHARED SSLBuffer : public TCPBuffer
217{
218protected:
219 secure::session_t ssl;
220 secure::bufio_t bio;
221 bool server;
222 bool verify;
223
224public:
225 SSLBuffer(secure::client_t context);
226 SSLBuffer(const TCPServer *server, secure::server_t context, size_t size = 536);
227 ~SSLBuffer();
228
229 /**
230 * Connect a ssl client session to a specific host uri. If the socket
231 * was already connected, it is automatically closed first.
232 * @param host we are connecting to.
233 * @param service to connect to.
234 * @param size of buffer and tcp fragments.
235 */
236 void open(const char *host, const char *service, size_t size = 536);
237
238 void close(void);
239
240 void release(void);
241
242 size_t _push(const char *address, size_t size);
243
244 size_t _pull(char *address, size_t size);
245
246 bool _flush(void);
247
248 bool _pending(void);
249
250 inline bool is_secure(void)
251 {return bio != NULL;};
252};
253
254/**
255 * A generic data ciphering class. This is used to construct cryptographic
256 * ciphers to encode and decode data as needed. The cipher type is specified
257 * by the key object. This class can be used to send output streaming to
258 * memory or in a fixed size buffer. If the latter is used, a push() method
259 * is called through a virtual when the buffer is full. Since block ciphers
260 * are used, buffers should be aligned to the block size.
261 * @author David Sugar <dyfet@gnutelephony.org>
262 */
263class __SHARED Cipher
264{
265public:
266 typedef enum {ENCRYPT = 1, DECRYPT = 0} mode_t;
267
268 /**
269 * Cipher key formed by hash algorithm. This can generate both a
270 * key and iv table based on the algorithms used and required. Normally
271 * it is used from a pass-phrase, though any block of data may be
272 * supplied.
273 * @author David Sugar <dyfet@gnutelephony.org>
274 */
275 class __SHARED Key
276 {
277 protected:
278 friend class Cipher;
279
280 union {
281 const void *algotype;
282 int algoid;
283 };
284
285 union {
286 const void *hashtype;
287 int hashid;
288 };
289
290 int modeid;
291
292 // assume 512 bit cipher keys possible...
293 unsigned char keybuf[MAX_CIPHER_KEYSIZE / 8], ivbuf[MAX_CIPHER_KEYSIZE / 8];
294
295 // generated keysize
296 size_t keysize, blksize;
297
298 Key(const char *cipher);
299 Key();
300
301 void set(const char *cipher);
302
303 void set(const char *cipher, const char *digest);
304
305 void assign(const char *key, size_t size, const unsigned char *salt, unsigned rounds);
306
307 public:
308 Key(const char *cipher, const char *digest, const char *text, size_t size = 0, const unsigned char *salt = NULL, unsigned rounds = 1);
309
310 Key(const char *cipher, const char *digest);
311
312 ~Key();
313
314 void assign(const char *key, size_t size = 0);
315
316 void clear(void);
317
318 inline size_t size(void)
319 {return keysize;};
320
321 inline size_t iosize(void)
322 {return blksize;};
323
324 inline operator bool()
325 {return keysize > 0;};
326
327 inline bool operator!()
328 {return keysize == 0;};
329
330 inline Key& operator=(const char *pass)
331 {assign(pass); return *this;};
332
333 static void options(const unsigned char *salt = NULL, unsigned rounds = 1);
334 };
335
336 typedef Key *key_t;
337
338private:
339 Key keys;
340 size_t bufsize, bufpos;
341 mode_t bufmode;
342 unsigned char *bufaddr;
343 void *context;
344
345protected:
346 virtual void push(unsigned char *address, size_t size);
347
348 void release(void);
349
350public:
351 Cipher();
352
353 Cipher(key_t key, mode_t mode, unsigned char *address = NULL, size_t size = 0);
354
355 virtual ~Cipher();
356
357 void set(unsigned char *address, size_t size = 0);
358
359 void set(key_t key, mode_t mode, unsigned char *address, size_t size = 0);
360
361 /**
362 * Push a final cipher block. This is used to push the final buffer into
363 * the push method for any remaining data.
364 */
365 size_t flush(void);
366
367 /**
368 * Process cipher data. This requires the size to be a multiple of the
369 * cipher block size. If an unaligned sized block of data is used, it
370 * will be ignored and the size returned will be 0.
371 * @param data to process.
372 * @param size of data to process.
373 * @return size of processed output, should be same as size or 0 if error.
374 */
375 size_t put(const unsigned char *data, size_t size);
376
377 /**
378 * This essentially encrypts a single string and pads with NULL bytes
379 * as needed.
380 * @param string to encrypt.
381 * @return total encrypted size.
382 */
383 size_t puts(const char *string);
384
385 /**
386 * This is used to process any data unaligned to the blocksize at the end
387 * of a cipher session. On an encryption, it will add padding or an
388 * entire padding block with the number of bytes to strip. On decryption
389 * it will remove padding at the end. The pkcs5 method of padding with
390 * removal count is used. This also sets the address buffer to NULL
391 * to prevent further puts until reset.
392 * @param address of data to add before final pad.
393 * @param size of data to add before final pad.
394 * @return actual bytes encrypted or decrypted.
395 */
396 size_t pad(const unsigned char *address, size_t size);
397
398 /**
399 * Process encrypted data in-place. This assumes no need to set the
400 * address buffer.
401 * @param address of data to process.
402 * @param size of data to process.
403 * @param flag if to pad data.
404 * @return bytes processed and written back to buffer.
405 */
406 size_t process(unsigned char *address, size_t size, bool flag = false);
407
408 inline size_t size(void)
409 {return bufsize;};
410
411 inline size_t pos(void)
412 {return bufpos;};
413
414 inline size_t align(void)
415 {return keys.iosize();};
416
417 /**
418 * Check if a specific cipher is supported.
419 * @param name of cipher to check.
420 * @return true if supported, false if not.
421 */
422 static bool has(const char *name);
423};
424
425/**
426 * A cryptographic digest class. This class can support md5 digests, sha1,
427 * sha256, etc, depending on what the underlying library supports. The
428 * hash class accumulates the hash in the object.
429 * @author David Sugar <dyfet@gnutelephony.org>
430 */
431class __SHARED Digest
432{
433private:
434 void *context;
435
436 union {
437 const void *hashtype;
438 int hashid;
439 };
440
441 unsigned bufsize;
442 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
443 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
444
445protected:
446 void release(void);
447
448public:
449 Digest(const char *type);
450
451 Digest();
452
453 ~Digest();
454
455 inline bool puts(const char *str)
456 {return put(str, strlen(str));};
457
458 inline Digest &operator<<(const char *str)
459 {puts(str); return *this;}
460
461 inline Digest &operator<<(int16_t value)
462 {int16_t v = htons(value); put(&v, 2); return *this;}
463
464 inline Digest &operator<<(int32_t value)
465 {int32_t v = htonl(value); put(&v, 4); return *this;}
466
467 inline Digest &operator<<(const PrintProtocol& p)
468 {const char *cp = p._print(); if(cp) puts(cp); return *this;}
469
470 bool put(const void *memory, size_t size);
471
472 inline unsigned size() const
473 {return bufsize;};
474
475 const unsigned char *get(void);
476
477 const char *c_str(void);
478
479 inline String str(void)
480 {return String(c_str());};
481
482 inline operator String()
483 {return String(c_str());};
484
485 void set(const char *id);
486
487 inline void operator=(const char *id)
488 {set(id);};
489
490 inline bool operator *=(const char *text)
491 {return puts(text);};
492
493 inline bool operator +=(const char *text)
494 {return puts(text);};
495
496 inline const char *operator*()
497 {return c_str();};
498
499 inline bool operator!() const
500 {return !bufsize && context == NULL;};
501
502 inline operator bool() const
503 {return bufsize > 0 || context != NULL;};
504
505 /**
506 * Finalize and recycle current digest to start a new
507 * digest.
508 * @param binary digest used rather than text if true.
509 */
510 void recycle(bool binary = false);
511
512 /**
513 * Reset and restart digest object.
514 */
515 void reset(void);
516
517 /**
518 * Test to see if a specific digest type is supported.
519 * @param name of digest we want to check.
520 * @return true if supported, false if not.
521 */
522 static bool has(const char *name);
523
524 static void uuid(char *string, const char *name, const unsigned char *ns = NULL);
525
526 static String uuid(const char *name, const unsigned char *ns = NULL);
527};
528
529/**
530 * A cryptographic message authentication code class. This class can support
531 * md5 digests, sha1, sha256, etc, depending on what the underlying library
532 * supports.
533 * @author David Sugar <dyfet@gnutelephony.org>
534 */
535class __SHARED HMAC
536{
537private:
538 void *context;
539
540 union {
541 const void *hmactype;
542 int hmacid;
543 };
544
545 unsigned bufsize;
546 unsigned char buffer[MAX_DIGEST_HASHSIZE / 8];
547 char textbuf[MAX_DIGEST_HASHSIZE / 8 + 1];
548
549protected:
550 void release(void);
551
552public:
553 HMAC(const char *digest, const char *key, size_t keylen = 0);
554
555 HMAC();
556
557 ~HMAC();
558
559 inline bool puts(const char *str)
560 {return put(str, strlen(str));};
561
562 inline HMAC &operator<<(const char *str)
563 {puts(str); return *this;}
564
565 inline HMAC &operator<<(int16_t value)
566 {int16_t v = htons(value); put(&v, 2); return *this;}
567
568 inline HMAC &operator<<(int32_t value)
569 {int32_t v = htonl(value); put(&v, 4); return *this;}
570
571 inline HMAC &operator<<(const PrintProtocol& p)
572 {const char *cp = p._print(); if(cp) puts(cp); return *this;}
573
574 bool put(const void *memory, size_t size);
575
576 inline unsigned size() const
577 {return bufsize;};
578
579 const unsigned char *get(void);
580
581 const char *c_str(void);
582
583 inline String str(void)
584 {return String(c_str());};
585
586 inline operator String()
587 {return String(c_str());};
588
589 void set(const char *digest, const char *key, size_t len);
590
591 inline bool operator *=(const char *text)
592 {return puts(text);};
593
594 inline bool operator +=(const char *text)
595 {return puts(text);};
596
597 inline const char *operator*()
598 {return c_str();};
599
600 inline bool operator!() const
601 {return !bufsize && context == NULL;};
602
603 inline operator bool() const
604 {return bufsize > 0 || context != NULL;};
605
606 /**
607 * Test to see if a specific digest type is supported.
608 * @param name of digest we want to check.
609 * @return true if supported, false if not.
610 */
611 static bool has(const char *name);
612};
613
614/**
615 * Cryptographically relevant random numbers. This is used both to gather
616 * entropy pools and pseudo-random values.
617 * @author David Sugar <dyfet@gnutelephony.org>
618 */
619class __SHARED Random
620{
621public:
622 /**
623 * Push entropic seed.
624 * @param buffer of random data to push.
625 * @param size of buffer.
626 * @return true if successful.
627 */
628 static bool seed(const unsigned char *buffer, size_t size);
629
630 /**
631 * Re-seed pseudo-random generation and entropy pools.
632 */
633 static void seed(void);
634
635 /**
636 * Get high-entropy random data. This is often used to
637 * initialize keys. This operation may block if there is
638 * insufficient entropy immediately available.
639 * @param memory buffer to fill.
640 * @param size of buffer.
641 * @return number of bytes filled.
642 */
643 static size_t key(unsigned char *memory, size_t size);
644
645 /**
646 * Fill memory with pseudo-random values. This is used
647 * as the basis for all get and real operations and does
648 * not depend on seed entropy.
649 * @param memory buffer to fill.
650 * @param size of buffer to fill.
651 * @return number of bytes set.
652 */
653 static size_t fill(unsigned char *memory, size_t size);
654
655 /**
656 * Get a pseudo-random integer, range 0 - 32767.
657 * @return random integer.
658 */
659 static int get(void);
660
661 /**
662 * Get a pseudo-random integer in a preset range.
663 * @param min value of random integer.
664 * @param max value of random integer.
665 * @return random value from min to max.
666 */
667 static int get(int min, int max);
668
669 /**
670 * Get a pseudo-random floating point value.
671 * @return psudo-random value 0 to 1.
672 */
673 static double real(void);
674
675 /**
676 * Get a pseudo-random floating point value in a preset range.
677 * @param min value of random floating point number.
678 * @param max value of random floating point number.
679 * @return random value from min to max.
680 */
681 static double real(double min, double max);
682
683 /**
684 * Determine if we have sufficient entropy to return random
685 * values.
686 * @return true if sufficient entropy.
687 */
688 static bool status(void);
689
690 /**
691 * Create 36 character random uuid string.
692 * @param string to write uuid into, must be 37 bytes or more.
693 */
694 static void uuid(char *string);
695
696 static String uuid(void);
697};
698
699/**
700 * Convenience type for secure socket.
701 */
702typedef SSLBuffer ssl_t;
703
704/**
705 * Convenience type for generic digests.
706 */
707typedef Digest digest_t;
708
709/**
710 * Convenience type for generic digests.
711 */
712typedef HMAC hmac_t;
713
714/**
715 * Convenience type for generic ciphers.
716 */
717typedef Cipher cipher_t;
718
719/**
720 * Convenience type for generic cipher key.
721 */
722typedef Cipher::Key skey_t;
723
724inline void zerofill(void *addr, size_t size)
725{
726 ::memset(addr, 0, size);
727}
728
729#if defined(OLD_STDCPP) || defined(NEW_STDCPP)
730
731/**
732 * Secure socket using std::iostream. This class is similar to SSLBuffer
733 * but uses the libstdc++ library to stream i/o. Being based on tcpstream,
734 * it also inherits the character protocol. Like SSLBuffer, if no context
735 * is given or the handshake fails, then the stream defaults to insecure TCP
736 * connection behavior.
737 * @author David Sugar <dyfet@gnutelephony.org>
738 */
739class __SHARED sstream : public tcpstream
740{
741protected:
742 secure::session_t ssl;
743 secure::bufio_t bio;
744 bool server;
745 bool verify;
746
747private:
748 // kill copy constructor
749 sstream(const sstream&);
750
751public:
752 sstream(secure::client_t context);
753 sstream(const TCPServer *server, secure::server_t context, size_t size = 536);
754 ~sstream();
755
756 void open(const char *host, const char *service, size_t size = 536);
757
758 void close(void);
759
760 int sync();
761
762 void release(void);
763
764 ssize_t _write(const char *address, size_t size);
765
766 ssize_t _read(char *address, size_t size);
767
768 bool _wait(void);
769
770 inline void flush(void)
771 {sync();}
772
773 inline bool is_secure(void)
774 {return bio != NULL;}
775};
776
777/**
778 * A template to create a string array that automatically erases.
779 * This is a mini string/stringbuf class that supports a subset of
780 * functionality but does not require a complex supporting object. Like
781 * stringbuf, this can be used to create local string variables. When
782 * the object falls out of scope it's memory is reset.
783 * @author David Sugar <dyfet@gnutelephony.org>
784 */
785template<size_t S>
786class keystring
787{
788private:
789 char buffer[S];
790
791 /**
792 * Disable copy constructor.
793 */
794 inline keystring(const keystring& copy) {};
795
796public:
797 /**
798 * Create a new character buffer with an empty string.
799 */
800 inline keystring()
801 {buffer[0] = 0;}
802
803 /**
804 * Create a character buffer with assigned text. If the text is
805 * larger than the size of the object, it is truncated.
806 * @param text to assign.
807 */
808 inline keystring(const char *text)
809 {String::set(buffer, S, text);}
810
811 /**
812 * Clear memory when destroyed.
813 */
814 inline ~keystring()
815 {memset(buffer, 0, S);}
816
817 /**
818 * Clear current key memory.
819 */
820 inline void clear(void)
821 {memset(buffer, 0, S);}
822
823 /**
824 * Assign null terminated text to the object.
825 * @param text to assign.
826 */
827 inline void operator=(const char *text)
828 {String::set(buffer, S, text);}
829
830 /**
831 * Concatenate text into the object. If the text is larger than the
832 * size of the object, then it is truncated.
833 * @param text to append.
834 */
835 inline void operator+=(const char *text)
836 {String::add(buffer, S, text);}
837
838 /**
839 * Test if data is contained in the object.
840 * @return true if there is text.
841 */
842 inline operator bool() const
843 {return buffer[0];}
844
845 /**
846 * Test if the object is empty.
847 * @return true if the object is empty.
848 */
849 inline bool operator!() const
850 {return buffer[0] == 0;}
851
852 /**
853 * Get text by casting reference.
854 * @return pointer to text in object.
855 */
856 inline operator char *()
857 {return buffer;}
858
859 /**
860 * Get text by object pointer reference.
861 * @return pointer to text in object.
862 */
863 inline char *operator*()
864 {return buffer;}
865
866 /**
867 * Array operator to get a character from the object.
868 * @param offset of character in string buffer.
869 * @return character at offset.
870 */
871 inline char& operator[](size_t offset) const
872 {return buffer[offset];}
873
874 /**
875 * Get a pointer to an offset in the object by expression operator.
876 * @param offset of character in string buffer.
877 * @return pointer to offset in object.
878 */
879 inline char *operator()(size_t offset)
880 {return buffer + offset;}
881
882 /**
883 * Get allocated size of the object.
884 * @return allocated size.
885 */
886 inline size_t size(void) const
887 {return S;}
888
889 /**
890 * Get current length of string.
891 * @return length of string.
892 */
893 inline size_t len(void) const
894 {return strlen(buffer);}
895};
896
897/**
898 * A template to create a random generated key of specified size. The
899 * key memory is cleared when the object is destroyed.
900 * @author David Sugar <dyfet@gnutelephony.org>
901 */
902template<size_t S>
903class keyrandom
904{
905private:
906 unsigned char buffer[S];
907
908 /**
909 * Disable copy constructor.
910 */
911 inline keyrandom(const keyrandom& copy) {};
912
913public:
914 /**
915 * Create a new character buffer with an empty string.
916 */
917 inline keyrandom()
918 {Random::key(buffer, S);}
919
920 /**
921 * Clear memory when destroyed.
922 */
923 inline ~keyrandom()
924 {memset(buffer, 0, S);}
925
926 /**
927 * Update with new random key.
928 */
929 inline void update(void)
930 {Random::key(buffer, S);}
931
932 /**
933 * Clear current key memory.
934 */
935 inline void clear(void)
936 {memset(buffer, 0, S);}
937
938 /**
939 * Get text by casting reference.
940 * @return pointer to text in object.
941 */
942 inline operator unsigned char *()
943 {return buffer;}
944
945 /**
946 * Get text by object pointer reference.
947 * @return pointer to text in object.
948 */
949 inline unsigned char *operator*()
950 {return buffer;}
951
952 /**
953 * Get allocated size of the object.
954 * @return allocated size.
955 */
956 inline size_t size(void) const
957 {return S;}
958};
959
960
961#endif
962
963END_NAMESPACE
964
965#endif