2012-12-29 18:40:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Codecrypt.
|
|
|
|
*
|
2017-06-26 12:28:54 +00:00
|
|
|
* Copyright (C) 2013-2017 Mirek Kratochvil <exa.exa@gmail.com>
|
2016-04-17 13:47:47 +00:00
|
|
|
*
|
2012-12-29 18:40:14 +00:00
|
|
|
* Codecrypt is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* Codecrypt is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Codecrypt. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-04-17 06:52:22 +00:00
|
|
|
#ifndef _ccr_generator_h_
|
|
|
|
#define _ccr_generator_h_
|
2012-12-29 18:40:14 +00:00
|
|
|
|
2014-04-05 09:36:50 +00:00
|
|
|
#include "chacha.h"
|
2012-12-29 18:40:14 +00:00
|
|
|
#include "prng.h"
|
|
|
|
|
2014-04-05 09:36:50 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
class ccr_rng : public prng
|
2012-12-29 18:40:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-09-10 15:29:23 +00:00
|
|
|
typedef uint64_t randmax_t;
|
|
|
|
|
2014-04-05 09:36:50 +00:00
|
|
|
chacha20 r;
|
2012-12-29 18:40:14 +00:00
|
|
|
|
2014-04-05 09:36:50 +00:00
|
|
|
ccr_rng() {
|
2014-03-19 08:30:59 +00:00
|
|
|
r.init ();
|
2012-12-29 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 09:36:50 +00:00
|
|
|
~ccr_rng() {
|
2012-12-29 18:40:14 +00:00
|
|
|
r.clear();
|
|
|
|
}
|
|
|
|
|
2017-06-26 12:28:54 +00:00
|
|
|
bool seed (uint bits, bool quick = true);
|
2012-12-29 18:40:14 +00:00
|
|
|
|
|
|
|
uint random (uint n) {
|
2017-09-10 15:29:23 +00:00
|
|
|
randmax_t i;
|
|
|
|
r.gen (sizeof (randmax_t), (byte*) &i);
|
2014-04-05 09:36:50 +00:00
|
|
|
return i % n;
|
2012-12-29 18:40:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|