[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: [RFC] Support for /dev/urandom in kdelibs
From:       Michael Buesch <mbuesch () freenet ! de>
Date:       2004-12-30 10:14:01
Message-ID: 200412301114.06917.mbuesch () freenet ! de
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


Hi Brad,

That's pointing into the right direction.

I designed a first class API to provide random data through QIODevice.
I'm not completely sure if QIODevice is the right choice. Maybe
QFile is even better. So currently the class is derived from QIODevice
but it's likely to change to QFile.

The goal of this is to provide a well known standard interface for
random data. Also note that I'm saying "random data" as a generic
term and not "random numbers" or "random strings" here. The long term
goal is to get the class working with QDataStream, so that it is
absolutely generic. It won't be easy to get it working as far as I can
see, yet. But maybe it's possible and worth the effort.

Example pseudo code for the class usage:

KEntropySource es(KEntropySource::source_auto);
es.open();
MyDataType t0;
/* The user has defined the >> operator for this data-type somewhere. */
int t1;
QDataStream stream(es);
stream >> t0;
stream >> t1;
es.close();


What do you think?
Is this usable, required?
What can be done better?


Quoting Brad Hards <bradh@frogmouth.net>:
> On Sun, 26 Dec 2004 23:49 pm, Michael Buesch wrote:
> > Is there a possibility to get support for a cryptographically strong
> > randomizer into the kdelibs for KDE-4?
> Do you really need it in KDE? If not, I'm hoping to provide QCA
> (as a dependency) for KDE-4, which has support for random
> numbers. At the moment, the API looks like the following. As
> someone else pointed out, there are reasons for wanting to
> have multiple sources of random numbers. It also occurs to me
> that we may not have a source of /dev/urandom type numbers
> on every platform that KDE supports.
[SNIP]

-- 
Regards Michael Buesch  [ http://www.tuxsoft.de.vu ]



["kentropysource.h" (text/x-chdr)]

/***************************************************************************
 *                                                                         *
 *   copyright (C) 2004 by Michael Buesch                                  *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

#ifndef KENTROPYSOURCE_H_
#define KENTROPYSOURCE_H_

#include <qiodevice.h>

class KEntropySource : public QIODevice
{
public:
	/** Entropy source types. */
	enum Source
	{
		/** No Entropy Source selected. */
		source_none,
		/** Automatically probe for available sources
		  * and select the best.
		  */
		source_auto,
		/** Automatically probe for available sources
		  * and select the securest.
		  * WARNING: Only use this if you know what you
		  * are doing. source_auto is as secure as this in
		  * most cases. This mode can run out of entropy
		  * _very_ fast and will block. Remember: It is
		  * entremely annoying to block the User Interface.
		  */
		source_autoSecure,
		/** /dev/urandom as Entropy Source. If the device
		  * is not available, open() will fail.
		  * This source will not block.
		  */
		source_urandom,
		/** /dev/random as Entropy Source. If the device is
		  * not available, open() will fail. WARNING: Operations
		  * may block if no entropy is available.
		  */
		source_random,
		/** Use the EGD (Entropy Gathering Daemon) as source.
		  * If the daemon is not available, open() will fail.
		  */
		source_egd,
		/** Use the C stdlib randomizer function.
		  * open() will not fail with this source.
		  * WARNING: This source maybe too insecure for your task.
		  */
		source_stdlib,
	};

public:
	/** Constructs an instance with no Entropy Source selected.
	  * Use setSource() to select an Entropy Source.
	  */
	KEntropySource();
	/** Constructs an instance with the given Entropy Source selected.
	  */
	KEntropySource(Source s);
	/** Destructs the instance. */
	virtual ~KEntropySource();

	/** Open the selected Entropy Source.
	  * (See setSource() for selecting a source.)
	  * @return Returns true on success and false on failure.
	  */
	bool open();
	/** Close the selected Entropy Source. */
	virtual void close();
	/** Set the Entropy Source.
	  * @return Returns true on success and false on failure.
	  */
	bool setSource(Source s);
	/** Get the current Entropy Source.
	  * @return Returns the currently used Entropy Source.
	  */
	Source getSource() const;

	/** Read a new random char from the Entropy Pool.
	  * @return Returns the byte/character read, or -1
	  *         on failure.
	  */
	virtual int getch();
	/** Read at most maxlen bytes from the Entropy Pool
	  * into data.
	  * @return Returns the number of bytes actually read.
	  */
	virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
	/** This always fills p with maxlen random chars.
	  * You generally don't want to use this function.
	  */
	virtual Q_LONG readLine(char *p, Q_ULONG maxlen);

protected:
	/** Do not use this. This function has no effect.
	  * @internal
	  */
	virtual void flush();
	/** Do not use this. This function has no effect.
	  * @internal
	  */
	virtual Offset size() const;
	/** Do not use this. This function has no effect.
	  * @internal
	  */
	virtual bool atEnd() const;

	/** Do not use this.
	  * This function will always return zero.
	  * @internal
	  */
	virtual Offset at() const;
	/** Do not use this function.
	  * @return This function will always return an
	  * empty QByteArray.
	  * @internal
	  */
	virtual QByteArray readAll();
	/** Do not use this.
	  * This function will always fail.
	  * @internal
	  */
	virtual bool at(Offset pos);
	/** Do not use this.
	  * As you don't have write access to the Entropy
	  * Pool, this function will always fail.
	  * @internal
	  */
	virtual int putch(int ch);
	/** Do not use this.
	  * As you don't have write access to the Entropy
	  * Pool, this function will always fail.
	  * @internal
	  */
	virtual int ungetch(int ch);
	/** Do not use this.
	  * As you don't have write access to the Entropy
	  * Pool, this function will always fail.
	  * @internal
	  */
	virtual Q_LONG writeBlock(const char *data, Q_ULONG len);

protected:
	/** Open the entropy pool with the selected mode.
	  * Only IO_ReadOnly is allowed.
	  * @internal
	  */
	virtual bool open(int m);
	/** Probe for an Entropy Source.
	  * @param secure If true, prefer very secure sources.
	  */
	Source probe(bool secure);

protected:
	/** Current Entropy Source.
	  * @internal
	  */
	Source source;
};

#endif // KENTROPYSOURCE_H_

[Attachment #6 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic