1 /**
2 * DESX
3 *
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.block.desx;
12
13 import botan.constants;
14 static if (BOTAN_HAS_DES):
15
16 import botan.block.des;
17 import botan.utils.xor_buf;
18 import botan.utils.mem_ops;
19
20 /**
21 * DESX
22 */
23 final class DESX : BlockCipherFixedParams!(8, 24), BlockCipher, SymmetricAlgorithm
24 {
25 public:
26 /*
27 * DESX Encryption
28 */
29 override void encryptN(const(ubyte)* input, ubyte* output, size_t blocks)
30 {
31 foreach (size_t i; 0 .. blocks)
32 {
33 xorBuf(output, cast(ubyte*)input, m_K1.ptr, BLOCK_SIZE);
34 m_des.encrypt(output);
35 xorBuf(output, cast(ubyte*)m_K2.ptr, BLOCK_SIZE);
36
37 input += BLOCK_SIZE;
38 output += BLOCK_SIZE;
39 }
40 }
41
42 /*
43 * DESX Decryption
44 */
45 override void decryptN(const(ubyte)* input, ubyte* output, size_t blocks)
46 {
47 foreach (size_t i; 0 .. blocks)
48 {
49 xorBuf(output, cast(ubyte*)input, m_K2.ptr, BLOCK_SIZE);
50 m_des.decrypt(output);
51 xorBuf(output, m_K1.ptr, BLOCK_SIZE);
52
53 input += BLOCK_SIZE;
54 output += BLOCK_SIZE;
55 }
56 }
57
58 override void clear()
59 {
60 m_des = new DES;
61 zap(m_K1);
62 zap(m_K2);
63 }
64
65 @property string name() const { return "DESX"; }
66 override @property size_t parallelism() const { return 1; }
67 override BlockCipher clone() const { return new DESX; }
68 override size_t blockSize() const { return super.blockSize(); }
69 override KeyLengthSpecification keySpec() const { return super.keySpec(); }
70
71 protected:
72 /*
73 * DESX Key Schedule
74 */
75 override void keySchedule(const(ubyte)* key, size_t)
76 {
77 m_K1[] = key[0 .. 8];
78 m_des.setKey(key + 8, 8);
79 m_K2[] = key[16 .. 24];
80 }
81
82 SecureVector!ubyte m_K1, m_K2;
83 Unique!DES m_des;
84 }