1 package org.bouncycastle.crypto.params;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class DESParameters
26 extends KeyParameter {
27 public DESParameters(
28 byte[] key) {
29 super(key);
30
31 if (isWeakKey(key, 0)) {
32 throw new IllegalArgumentException("attempt to create weak DES key");
33 }
34 }
35
36
37
38
39 static public final int DES_KEY_LENGTH = 8;
40
41
42
43
44 static private final int N_DES_WEAK_KEYS = 16;
45
46 static private byte[] DES_weak_keys = {
47
48 (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
49 (byte) 0x1f, (byte) 0x1f, (byte) 0x1f, (byte) 0x1f, (byte) 0x0e, (byte) 0x0e, (byte) 0x0e, (byte) 0x0e,
50 (byte) 0xe0, (byte) 0xe0, (byte) 0xe0, (byte) 0xe0, (byte) 0xf1, (byte) 0xf1, (byte) 0xf1, (byte) 0xf1,
51 (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe, (byte) 0xfe,
52
53
54 (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe,
55 (byte) 0x1f, (byte) 0xe0, (byte) 0x1f, (byte) 0xe0, (byte) 0x0e, (byte) 0xf1, (byte) 0x0e, (byte) 0xf1,
56 (byte) 0x01, (byte) 0xe0, (byte) 0x01, (byte) 0xe0, (byte) 0x01, (byte) 0xf1, (byte) 0x01, (byte) 0xf1,
57 (byte) 0x1f, (byte) 0xfe, (byte) 0x1f, (byte) 0xfe, (byte) 0x0e, (byte) 0xfe, (byte) 0x0e, (byte) 0xfe,
58 (byte) 0x01, (byte) 0x1f, (byte) 0x01, (byte) 0x1f, (byte) 0x01, (byte) 0x0e, (byte) 0x01, (byte) 0x0e,
59 (byte) 0xe0, (byte) 0xfe, (byte) 0xe0, (byte) 0xfe, (byte) 0xf1, (byte) 0xfe, (byte) 0xf1, (byte) 0xfe,
60 (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01, (byte) 0xfe, (byte) 0x01,
61 (byte) 0xe0, (byte) 0x1f, (byte) 0xe0, (byte) 0x1f, (byte) 0xf1, (byte) 0x0e, (byte) 0xf1, (byte) 0x0e,
62 (byte) 0xe0, (byte) 0x01, (byte) 0xe0, (byte) 0x01, (byte) 0xf1, (byte) 0x01, (byte) 0xf1, (byte) 0x01,
63 (byte) 0xfe, (byte) 0x1f, (byte) 0xfe, (byte) 0x1f, (byte) 0xfe, (byte) 0x0e, (byte) 0xfe, (byte) 0x0e,
64 (byte) 0x1f, (byte) 0x01, (byte) 0x1f, (byte) 0x01, (byte) 0x0e, (byte) 0x01, (byte) 0x0e, (byte) 0x01,
65 (byte) 0xfe, (byte) 0xe0, (byte) 0xfe, (byte) 0xe0, (byte) 0xfe, (byte) 0xf1, (byte) 0xfe, (byte) 0xf1
66 };
67
68
69
70
71
72
73
74
75
76
77
78
79 public static boolean isWeakKey(
80 byte[] key,
81 int offset) {
82 if (key.length - offset < DES_KEY_LENGTH) {
83 throw new IllegalArgumentException("key material too short.");
84 }
85
86 nextkey: for (int i = 0; i < N_DES_WEAK_KEYS; i++) {
87 for (int j = 0; j < DES_KEY_LENGTH; j++) {
88 if (key[j + offset] != DES_weak_keys[i * DES_KEY_LENGTH + j]) {
89 continue nextkey;
90 }
91 }
92
93 return true;
94 }
95 return false;
96 }
97
98
99
100
101
102
103
104 public static void setOddParity(
105 byte[] bytes) {
106 for (int i = 0; i < bytes.length; i++) {
107 int b = bytes[i];
108 bytes[i] = (byte) ((b & 0xfe) |
109 ((((b >> 1) ^
110 (b >> 2) ^
111 (b >> 3) ^
112 (b >> 4) ^
113 (b >> 5) ^
114 (b >> 6) ^
115 (b >> 7)) ^ 0x01) & 0x01));
116 }
117 }
118 }