summaryrefslogtreecommitdiffstats
path: root/src/include/ipxe/des.h
blob: 755a90ea0e37dcfbe1af305ee82a44be71f9bf28 (plain) (blame)
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
#ifndef _IPXE_DES_H
#define _IPXE_DES_H

/** @file
 *
 * DES algorithm
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/crypto.h>

/** A DES 32-bit dword value
 *
 * DES views data as 64-bit big-endian values, typically handled as a
 * most-significant "left" half and a least-significant "right" half.
 */
union des_dword {
	/** Raw bytes */
	uint8_t byte[4];
	/** 32-bit big-endian dword */
	uint32_t dword;
};

/** A DES 64-bit block */
union des_block {
	/** Raw bytes */
	uint8_t byte[8];
	/** 32-bit big-endian dwords */
	uint32_t dword[2];
	/** Named left and right halves */
	struct {
		/** Left (most significant) half */
		union des_dword left;
		/** Right (least significant) half */
		union des_dword right;
	};
	/** Named "C" and "D" halves */
	struct {
		/** "C" (most significant) half */
		union des_dword c;
		/** "D" (least significant) half */
		union des_dword d;
	};
};

/** DES blocksize */
#define DES_BLOCKSIZE sizeof ( union des_block )

/** A DES round key
 *
 * A DES round key is a 48-bit value, consumed as 8 groups of 6 bits.
 * We store these as 8 separate bytes, for simplicity of consumption.
 */
union des_round_key {
	/** Raw bytes */
	uint8_t byte[8];
	/** 32-bit big-endian dwords */
	uint32_t dword[2];
	/** 6-bit step key byte
	 *
	 * There are 8 steps within a DES round (one step per S-box).
	 * Each step requires six bits of the round key.
	 *
	 * As an optimisation, we store the least significant of the 6
	 * bits in the sign bit of a signed 8-bit value, and the
	 * remaining 5 bits in the least significant 5 bits of the
	 * 8-bit value.  See the comments in des_sbox() for further
	 * details.
	 */
	int8_t step[8];
};

/** Number of DES rounds */
#define DES_ROUNDS 16

/** DES context */
struct des_context {
	/** Round keys */
	union des_round_key rkey[DES_ROUNDS];
};

/** DES context size */
#define DES_CTX_SIZE sizeof ( struct des_context )

extern struct cipher_algorithm des_algorithm;
extern struct cipher_algorithm des_ecb_algorithm;
extern struct cipher_algorithm des_cbc_algorithm;

#endif /* _IPXE_DES_H */