Honeycomb  0.1
Component-Model Framework
Chacha.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 
41 class Chacha : public RandomGen
42 {
43 public:
45  typedef ByteArray<32> Key;
47  typedef ByteArray<8> Iv;
50 
52  struct State
53  {
54  static const szt coreSize = 16;
55 
56  State() : resIdx(coreSize) {}
57 
63  };
64 
65  Chacha() { setSeed(); }
66  ~Chacha() {}
67 
69  void setSeed();
70 
72  void setSeed(const Seed& seed);
73 
75  virtual uint64 next();
76 
78  void setKey(const Key& key);
80  void setIv(const Iv& iv);
81 
83  void encrypt(ByteBufConst msg, ByteBuf cipher);
85  void decrypt(ByteBufConst cipher, ByteBuf msg);
86 
88  void setState(const State& state) { _state = state; }
90  State& getState() { return _state; }
91 
92 private:
94  void step();
95 
96  State _state;
97 
98  static const byte _sigma[];
99 };
100 
101 }
void encrypt(ByteBufConst msg, ByteBuf cipher)
Encrypt a message. Result is stored in cipher.
Definition: Chacha.cpp:152
A contiguous region of referenced (not owned by object) memory.
Definition: Buffer.h:17
void setState(const State &state)
Set the state of the generator.
Definition: Chacha.h:88
Random number generator interface.
Definition: Gen.h:11
uint8 byte
An unsigned 8-bit integer.
Definition: Bytes.h:12
uint32 resIdx
Definition: Chacha.h:62
unsigned int uint32
Definition: Core.h:16
void setKey(const Key &key)
Init generator with a cryptographic key.
Definition: Chacha.cpp:124
static const szt coreSize
Definition: Chacha.h:54
unsigned long long uint64
Definition: Core.h:22
State & getState()
Get the current state of the generator. Pass result into setState() to restore a state.
Definition: Chacha.h:90
State()
Definition: Chacha.h:56
void setSeed()
Set the seed using a default method that gathers entropy from the device.
Definition: Chacha.cpp:24
uint32 core[coreSize]
512-bit state
Definition: Chacha.h:59
~Chacha()
Definition: Chacha.h:66
ChaCha8, a cryptographically secure pseudo random number generator.
Definition: Chacha.h:41
ByteArray< 32 > Key
256-bit Cryptographic Key
Definition: Chacha.h:45
ByteArray< 8 > Iv
64-bit Cryptographic IV
Definition: Chacha.h:47
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
void setIv(const Iv &iv)
Set initialization vector into generator. Call for every message between encrypt/decrypt calls to pro...
Definition: Chacha.cpp:142
Chacha()
Definition: Chacha.h:65
ByteArray< 40 > Seed
320-bit Seed (Key+IV)
Definition: Chacha.h:49
Generator State.
Definition: Chacha.h:52
virtual uint64 next()
Generate random number between 0 and 2^64-1 inclusive.
Definition: Chacha.cpp:113
Global Honeycomb namespace.
void decrypt(ByteBufConst cipher, ByteBuf msg)
Decrypt a cipher. Result is stored in msg.
Definition: Chacha.cpp:295
uint32 res[coreSize]
Cached results from step()
Definition: Chacha.h:61