aboutsummaryrefslogtreecommitdiff
path: root/source/tanya/crypto/symmetric.d
blob: b6c268a163e5aa6cce30bc3c4d02383f6a523348 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Interfaces for implementing secret key algorithms.
 *
 * Copyright: Eugene Wissner 2016.
 * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
 *                  Mozilla Public License, v. 2.0).
 * Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
 */
module tanya.crypto.symmetric;

/**
 * Implemented by secret key algorithms.
 */
interface SymmetricCipher
{
    /**
     * Returns: Key length.
     */
    @property inout(uint) keyLength() inout const pure nothrow @safe @nogc;

    /**
     * Returns: Minimum key length.
     */
    @property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc;

    /**
     * Returns: Maximum key length.
     */
    @property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc;

    /// Cipher direction.
    protected enum Direction : ushort
    {
        encryption,
        decryption,
    }

    /**
     * Params:
     *     key = Key.
     */
    @property void key(ubyte[] key) pure nothrow @safe @nogc
    in
    {
        assert(key.length >= minKeyLength);
        assert(key.length <= maxKeyLength);
    }
}

/**
 * Implemented by block ciphers.
 */
interface BlockCipher : SymmetricCipher
{
    /**
     * Returns: Block size.
     */
    @property inout(uint) blockSize() inout const pure nothrow @safe @nogc;

    /**
     * Encrypts a block.
     *
     * Params:
     *    plain  = Plain text, input.
     *    cipher = Cipher text, output.
     */
    void encrypt(in ubyte[] plain, ubyte[] cipher)
    in
    {
        assert(plain.length == blockSize);
        assert(cipher.length == blockSize);
    }

    /**
     * Decrypts a block.
     *
     * Params:
     *    cipher = Cipher text, input.
     *    plain  = Plain text, output.
     */
    void decrypt(in ubyte[] cipher, ubyte[] plain)
    in
    {
        assert(plain.length == blockSize);
        assert(cipher.length == blockSize);
    }
}

/**
 * Mixed in by algorithms with fixed block size.
 *
 * Params:
 *     N = Block size.
 */
mixin template FixedBlockSize(uint N)
    if (N != 0)
{
    private enum uint blockSize_ = N;

    /**
     * Returns: Fixed block size.
     */
    final @property inout(uint) blockSize() inout const pure nothrow @safe @nogc
    {
        return blockSize_;
    }
}

/**
 * Mixed in by symmetric algorithms.
 * If $(D_PARAM Min) equals $(D_PARAM Max) fixed key length is assumed.
 *
 * Params:
 *     Min = Minimum key length.
 *     Max = Maximum key length.
 */
mixin template KeyLength(uint Min, uint Max = Min)
    if (Min != 0 && Max != 0)
{
    static if (Min == Max)
    {
        private enum uint keyLength_ = Min;

        /**
         * Returns: Key length.
         */
        final @property inout(uint) keyLength() inout const pure nothrow @safe @nogc
        {
            return keyLength_;
        }

        /**
         * Returns: Minimum key length.
         */
        final @property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc
        {
            return keyLength_;
        }

        /**
         * Returns: Maximum key length.
         */
        final @property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc
        {
            return keyLength_;
        }
    }
    else static if (Min < Max)
    {
        private enum uint minKeyLength_ = Min;
        private enum uint maxKeyLength_ = Max;

        /**
         * Returns: Minimum key length.
         */
        final @property inout(uint) minKeyLength() inout const pure nothrow @safe @nogc
        {
            return minKeyLength_;
        }

        /**
         * Returns: Maximum key length.
         */
        final @property inout(uint) maxKeyLength() inout const pure nothrow @safe @nogc
        {
            return maxKeyLength_;
        }
    }
    else
    {
        static assert(false, "Max should be larger or equal to Min");
    }
}