File: obfuscated_string.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (212 lines) | stat: -rw-r--r-- 6,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/************************************************************************
 *
 * Copyright (C) 2022-2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight 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.
 *
 * Sight 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 Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

// cspell:ignore NOLINTNEXTLINE hicpp

#pragma once

#include <sight/core/config.hpp>

#include "core/crypto/secure_string.hpp"

#include <array>
#include <cstdint>
#include <iostream>

namespace sight::core::crypto
{

template<std::uint32_t S, std::uint32_t A = 16807UL, std::uint32_t C = 0UL, std::uint32_t M = (1UL << 31) - 1>
struct linear_generator
{
    static const std::uint32_t STATE = ((std::uint64_t) S * A + C) % M;
    static const std::uint32_t VALUE = STATE;
    using next = linear_generator<STATE>;
    struct split
    {
        // Leapfrog
        using gen1 = linear_generator<STATE, A* A, 0, M>;
        using gen2 = linear_generator<next::state, A* A, 0, M>;
    };
};

// Metafunction to get a particular index from generator
template<std::uint32_t S, std::size_t index>
struct generate
{
    static const std::uint8_t VALUE = generate<linear_generator<S>::STATE, index - 1>::VALUE;
};

template<std::uint32_t S>
struct generate<S, 0>
{
    static const std::uint8_t VALUE = static_cast<std::uint8_t>(linear_generator<S>::VALUE);
};

// List of indices
template<std::size_t ...>
struct st_list {};

// Concatenate
template<typename TL, typename TR>
struct concat;

template<std::size_t ... SL, std::size_t ... SR>
struct concat<st_list<SL ...>, st_list<SR ...> >
{
    using type = st_list<SL ..., SR ...>;
};

template<typename TL, typename TR>
using concat_t = typename concat<TL, TR>::type;

// Count from zero to n-1
template<std::size_t s>
struct count
{
    using type = concat_t<typename count<s - 1>::type, st_list<s - 1> >;
};

template<>
struct count<0>
{
    using type = st_list<>;
};

template<std::size_t s>
using count_t = typename count<s>::type;

// Get a scrambled character of a string
template<std::uint32_t seed, std::size_t index, std::size_t N>
constexpr std::uint8_t get_scrambled_char(const std::array<char, N>& _a)
{
    return static_cast<std::uint8_t>(_a[index]) + generate<seed, index>::VALUE;
}

// Get a ciphertext from a plaintext string
template<std::uint32_t seed, typename T>
struct cipher_helper;

template<std::uint32_t seed, std::size_t ... SL>
struct cipher_helper<seed, st_list<SL ...> >
{
    //------------------------------------------------------------------------------

    static constexpr std::array<std::uint8_t, sizeof...(SL)> get_array(const std::array<char, sizeof...(SL)>& _a)
    {
        return {{get_scrambled_char<seed, SL>(_a) ...}};
    }
};

//------------------------------------------------------------------------------

template<std::uint32_t seed, std::size_t N>
constexpr std::array<std::uint8_t, N> get_cipher_text(const std::array<char, N>& _a)
{
    return cipher_helper<seed, count_t<N> >::get_array(_a);
}

// Get a noise sequence from a seed and string length
template<std::uint32_t seed, typename T>
struct noise_helper;

template<std::uint32_t seed, std::size_t ... SL>
struct noise_helper<seed, st_list<SL ...> >
{
    //------------------------------------------------------------------------------

    static constexpr std::array<std::uint8_t, sizeof...(SL)> get_array()
    {
        return {{generate<seed, SL>::VALUE ...}};
    }
};

//------------------------------------------------------------------------------

template<std::uint32_t seed, std::size_t N>
constexpr std::array<std::uint8_t, N> get_key()
{
    return noise_helper<seed, count_t<N> >::get_array();
}

template<typename T, std::size_t N>
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
constexpr std::array<T, N> to_array(const char (& _a)[N])
{
    std::array<T, N> res {};
    for(std::size_t i = 0 ; i < N ; i++)
    {
        res[i] = _a[i];
    }

    return res;
}

// Scramble a string
template<std::uint32_t seed, std::size_t N>
class obfuscated_string
{
private:

    std::array<std::uint8_t, N> m_cipher_text;
    std::array<std::uint8_t, N> m_key;

public:

    explicit constexpr obfuscated_string(/*const char (& a)[N]*/ const std::array<char, N>& _a) :
        m_cipher_text(get_cipher_text<seed, N>(_a)),
        m_key(get_key<seed, N>())
    {
    }

    operator sight::core::crypto::secure_string() const
    {
        std::array<char, N> plain_text;
        for(std::size_t i = 0 ; i < N ; ++i)
        {
            const char temp = static_cast<char>(m_cipher_text[i] - m_key[i]);
            plain_text[i] = temp;
        }

        return sight::core::crypto::secure_string {plain_text.begin(), plain_text.end()};
    }
};

//------------------------------------------------------------------------------

template<std::uint32_t seed, std::size_t N>
std::ostream& operator<<(std::ostream& _s, const sight::core::crypto::obfuscated_string<seed, N>& _str)
{
    _s << static_cast<sight::core::crypto::secure_string>(_str);
    return _s;
}

#define RNG_SEED ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 \
                  + (__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 \
                  + (__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000) \
        + (__LINE__ * 100000)

#define OBFUSCATED_STR(STR) \
        sight::core::crypto::obfuscated_string<RNG_SEED, \
                                               sizeof(STR)> {sight::core::crypto::to_array<char, sizeof(STR)>(STR) \
        }

} // namespace sight::core::crypto