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
36  inline static const int randomSize{keySize + ivSize};
37  using DataT = std::string;
38 
39  private:
40  struct RawTag {};
41 
50  AES256CTRDesc(RawTag, DataT key, DataT iv);
51 
52  public:
62  AES256CTRDesc(std::string key, std::string iv);
63 
75  template<class RangeT>
76  static AES256CTRDesc fromRandom(RangeT random) {
77  if (random.size() < randomSize) {
78  // Not enough random, return an invalid one
79  return AES256CTRDesc(RawTag{}, DataT(), DataT());
80  }
81  return AES256CTRDesc(RawTag{},
82  DataT(random.begin(), random.begin() + keySize),
83  DataT(random.begin() + keySize, random.begin() + keySize + ivSize));
84  }
85 
87 
89 
94  bool valid() const;
95 
97  std::string key() const;
98 
100  std::string iv() const;
101 
102  template<class RangeT>
103  using ResultBase = std::pair<AES256CTRDesc, RangeT>;
104 
106 
126  Result process(DataT data) const &;
133  template<class RangeT,
134  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
136  using ActualRangeT = std::decay_t<RangeT>;
137  auto [next, res] = process(DataT(begin(data), end(data)));
138  return { next, ActualRangeT(res.begin(), res.end()) };
139  }
140 
147  Result process(DataT data) &&;
154  template<class RangeT,
155  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
157  using ActualRangeT = std::decay_t<RangeT>;
158  auto [next, res] = std::move(*this).process(DataT(begin(data), end(data)));
159  return { next, ActualRangeT(res.begin(), res.end()) };
160  }
161 
167  DataT processInPlace(DataT data);
174  template<class RangeT,
175  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
176  std::decay_t<RangeT> processInPlace(RangeT data) {
177  using ActualRangeT = std::decay_t<RangeT>;
178  auto res = processInPlace(DataT(begin(data), end(data)));
179  return ActualRangeT(res.begin(), res.end());
180  }
181 
182  private:
183  struct Private;
184 
185  std::unique_ptr<Private> m_d;
186  };
187 }
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:37
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:176
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:156
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:76
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:103
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:135
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:105
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:36