libkazv
Loading...
Searching...
No Matches
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
18namespace 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>
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
99 template<class RangeT1, class RangeT2>
100 static AES256CTRDesc fromRaw(RangeT1 key, RangeT2 iv)
101 {
102 if (key.size() < keySize || iv.size() < ivSize) {
103 // Not enough random, return an invalid one
104 return AES256CTRDesc(RawTag{}, DataT(), DataT());
105 }
106 return AES256CTRDesc(RawTag{},
107 DataT(key.begin(), key.begin() + keySize),
108 DataT(iv.begin(), iv.begin() + ivSize));
109 }
110
112
114
119 bool valid() const;
120
122 std::string key() const;
123
125 std::string iv() const;
126
127 template<class RangeT>
128 using ResultBase = std::pair<AES256CTRDesc, RangeT>;
129
131
151 Result process(DataT data) const &;
158 template<class RangeT,
159 class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
161 using ActualRangeT = std::decay_t<RangeT>;
162 auto [next, res] = process(DataT(begin(data), end(data)));
163 return { next, ActualRangeT(res.begin(), res.end()) };
164 }
165
172 Result process(DataT data) &&;
179 template<class RangeT,
180 class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
182 using ActualRangeT = std::decay_t<RangeT>;
183 auto [next, res] = std::move(*this).process(DataT(begin(data), end(data)));
184 return { next, ActualRangeT(res.begin(), res.end()) };
185 }
186
199 template<class RangeT,
200 class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
201 std::decay_t<RangeT> processInPlace(RangeT data) {
202 using ActualRangeT = std::decay_t<RangeT>;
203 auto res = processInPlace(DataT(begin(data), end(data)));
204 return ActualRangeT(res.begin(), res.end());
205 }
206
207 private:
208 struct Private;
209
210 std::unique_ptr<Private> m_d;
211 };
212}
Definition aes-256-ctr.hpp:27
ResultBase< std::decay_t< RangeT > > process(RangeT data) &&
Encrypts or decrypts data and derives the next cipher state.
Definition aes-256-ctr.hpp:181
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:201
static AES256CTRDesc fromRandom(RangeT random)
Generate a new AES-256-CTR cipher from random data.
Definition aes-256-ctr.hpp:80
static const int randomSize
Random size to generate a new cipher, provided in fromRandom().
Definition aes-256-ctr.hpp:40
static AES256CTRDesc fromRaw(RangeT1 key, RangeT2 iv)
Generate a new AES-256-CTR cipher from raw data.
Definition aes-256-ctr.hpp:100
Result process(DataT data) const &
Encrypts or decrypts data and derives the next cipher state.
Definition aes-256-ctr.cpp:104
static const int optimalBlockSize
The optimal block size for this cipher.
Definition aes-256-ctr.hpp:30
static const int ivSizeInit
The size used for initialization of the iv for this cipher.
Definition aes-256-ctr.hpp:38
std::pair< AES256CTRDesc, RangeT > ResultBase
Definition aes-256-ctr.hpp:128
DataT processInPlace(DataT data)
Encrypt or decrypt data and modify the state of the cipher in-place.
Definition aes-256-ctr.cpp:118
ResultBase< std::decay_t< RangeT > > process(RangeT data) const &
Encrypts or decrypts data and derives the next cipher state.
Definition aes-256-ctr.hpp:160
std::string key() const
Definition aes-256-ctr.cpp:94
static const int keySize
The key size for this cipher.
Definition aes-256-ctr.hpp:32
static const int ivSize
The iv size for this cipher.
Definition aes-256-ctr.hpp:34
bool valid() const
Definition aes-256-ctr.cpp:89
std::string iv() const
Definition aes-256-ctr.cpp:99
std::string DataT
Definition aes-256-ctr.hpp:41
ResultBase< DataT > Result
Definition aes-256-ctr.hpp:130
A RangeT is an ordered collection that can be iterated through.
Definition range-t.hpp:21
auto begin() const
The beginning iterator of this range.
#define KAZV_DECLARE_COPYABLE(typeName)
Definition copy-helper.hpp:10
Definition location.hpp:10
@ Private
Definition client-model.hpp:41