libkazv
aes-256-ctr.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <memory>
11 
12 
13 #include <copy-helper.hpp>
14 
15 #include "crypto-util.hpp"
16 
17 
18 namespace Kazv
19 {
20  namespace
21  {
22  using std::begin;
23  using std::end;
24  }
25 
27  {
28  public:
30  inline static const int optimalBlockSize{16};
32  inline static const int keySize{32};
34  inline static const int ivSize{16}; // AES block size
38  inline static const int ivSizeInit{8};
40  inline static const int randomSize{keySize + ivSizeInit};
41  using DataT = std::string;
42 
43  private:
44  struct RawTag {};
45 
54  AES256CTRDesc(RawTag, DataT key, DataT iv);
55 
56  public:
66  AES256CTRDesc(std::string key, std::string iv);
67 
79  template<class RangeT>
80  static AES256CTRDesc fromRandom(RangeT random) {
81  if (random.size() < randomSize) {
82  // Not enough random, return an invalid one
83  return AES256CTRDesc(RawTag{}, DataT(), DataT());
84  }
85  auto iv = DataT(ivSize, 0);
86  std::copy(random.begin() + keySize, random.begin() + keySize + ivSizeInit, iv.begin());
87  return AES256CTRDesc(RawTag{},
88  DataT(random.begin(), random.begin() + keySize),
89  iv);
90  }
91 
93 
95 
100  bool valid() const;
101 
103  std::string key() const;
104 
106  std::string iv() const;
107 
108  template<class RangeT>
109  using ResultBase = std::pair<AES256CTRDesc, RangeT>;
110 
112 
132  Result process(DataT data) const &;
139  template<class RangeT,
140  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
142  using ActualRangeT = std::decay_t<RangeT>;
143  auto [next, res] = process(DataT(begin(data), end(data)));
144  return { next, ActualRangeT(res.begin(), res.end()) };
145  }
146 
153  Result process(DataT data) &&;
160  template<class RangeT,
161  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
163  using ActualRangeT = std::decay_t<RangeT>;
164  auto [next, res] = std::move(*this).process(DataT(begin(data), end(data)));
165  return { next, ActualRangeT(res.begin(), res.end()) };
166  }
167 
173  DataT processInPlace(DataT data);
180  template<class RangeT,
181  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
182  std::decay_t<RangeT> processInPlace(RangeT data) {
183  using ActualRangeT = std::decay_t<RangeT>;
184  auto res = processInPlace(DataT(begin(data), end(data)));
185  return ActualRangeT(res.begin(), res.end());
186  }
187 
188  private:
189  struct Private;
190 
191  std::unique_ptr<Private> m_d;
192  };
193 }
Kazv::AES256CTRDesc::process
Result process(DataT data) const &
Encrypts or decrypts data and derives the next cipher state.
Definition: aes-256-ctr.cpp:104
Kazv::AES256CTRDesc::key
std::string key() const
Definition: aes-256-ctr.cpp:94
crypto-util.hpp
copy-helper.hpp
Kazv::AES256CTRDesc::DataT
std::string DataT
Definition: aes-256-ctr.hpp:41
Kazv::AES256CTRDesc
Definition: aes-256-ctr.hpp:26
Kazv::Private
@ Private
Definition: client-model.hpp:41
Kazv
Definition: location.hpp:10
Kazv::AES256CTRDesc::valid
bool valid() const
Definition: aes-256-ctr.cpp:89
Kazv::AES256CTRDesc::~AES256CTRDesc
~AES256CTRDesc()
Kazv::AES256CTRDesc::processInPlace
std::decay_t< RangeT > processInPlace(RangeT data)
Encrypt or decrypt data and modify the state of the cipher in-place.
Definition: aes-256-ctr.hpp:182
Kazv::AES256CTRDesc::ivSizeInit
static const int ivSizeInit
The size used for initialization of the iv for this cipher.
Definition: aes-256-ctr.hpp:38
KAZV_DECLARE_COPYABLE
#define KAZV_DECLARE_COPYABLE(typeName)
Definition: copy-helper.hpp:10
Kazv::AES256CTRDesc::process
ResultBase< std::decay_t< RangeT > > process(RangeT data) &&
Encrypts or decrypts data and derives the next cipher state.
Definition: aes-256-ctr.hpp:162
RangeT
A RangeT is an ordered collection that can be iterated through.
Definition: range-t.hpp:20
RangeT::begin
auto begin() const
The beginning iterator of this range.
Kazv::AES256CTRDesc::fromRandom
static AES256CTRDesc fromRandom(RangeT random)
Generate a new AES-256-CTR cipher from random data.
Definition: aes-256-ctr.hpp:80
Kazv::AES256CTRDesc::optimalBlockSize
static const int optimalBlockSize
The optimal block size for this cipher.
Definition: aes-256-ctr.hpp:30
Kazv::AES256CTRDesc::ResultBase
std::pair< AES256CTRDesc, RangeT > ResultBase
Definition: aes-256-ctr.hpp:109
Kazv::AES256CTRDesc::process
ResultBase< std::decay_t< RangeT > > process(RangeT data) const &
Encrypts or decrypts data and derives the next cipher state.
Definition: aes-256-ctr.hpp:141
Kazv::AES256CTRDesc::iv
std::string iv() const
Definition: aes-256-ctr.cpp:99
Kazv::AES256CTRDesc::processInPlace
DataT processInPlace(DataT data)
Encrypt or decrypt data and modify the state of the cipher in-place.
Definition: aes-256-ctr.cpp:118
Kazv::AES256CTRDesc::ivSize
static const int ivSize
The iv size for this cipher.
Definition: aes-256-ctr.hpp:34
libkazv-config.hpp
Kazv::AES256CTRDesc::Result
ResultBase< DataT > Result
Definition: aes-256-ctr.hpp:111
Kazv::AES256CTRDesc::keySize
static const int keySize
The key size for this cipher.
Definition: aes-256-ctr.hpp:32
Kazv::AES256CTRDesc::randomSize
static const int randomSize
Random size to generate a new cipher, provided in fromRandom().
Definition: aes-256-ctr.hpp:40