2 // System.Security.Cryptography.DES.cs
5 // Sergey Chaban (serge@wildwestsoftware.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if !NET_2_1 || MONOTOUCH
33 using System.Globalization;
34 using System.Runtime.InteropServices;
37 // a. FIPS PUB 46-3: Data Encryption Standard
38 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
40 namespace System.Security.Cryptography {
43 public abstract class DES : SymmetricAlgorithm {
45 private const int keySizeByte = 8;
51 FeedbackSizeValue = 8;
53 LegalKeySizesValue = new KeySizes[1];
54 LegalKeySizesValue[0] = new KeySizes(64, 64, 0);
56 LegalBlockSizesValue = new KeySizes[1];
57 LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
60 public static new DES Create ()
62 return Create ("System.Security.Cryptography.DES");
65 public static new DES Create (string algName)
67 return (DES) CryptoConfig.CreateFromName (algName);
72 internal static readonly byte[,] weakKeys = {
73 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
74 { 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F },
75 { 0xE1, 0xE1, 0xE1, 0xE1, 0xF1, 0xF1, 0xF1, 0xF1 },
76 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
80 internal static readonly byte[,] semiWeakKeys = {
81 { 0x00, 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E }, // map to packed key 011F011F010E010E
\r
82 { 0x00, 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0 }, // map to packed key 01E001E001F101F1
\r
83 { 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE }, // map to packed key 01FE01FE01FE01FE
\r
84 { 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E, 0x00 }, // map to packed key 1F011F010E010E01
\r
85 { 0x1E, 0xE0, 0x1E, 0xE0, 0x0E, 0xF0, 0x0E, 0xF0 }, // map to packed key 1FE01FE00EF10EF1
\r
86 { 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, // map to packed key 1FFE1FFE0EFE0EFE
\r
87 { 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x00 }, // map to packed key E001E001F101F101
\r
88 { 0xE0, 0x1E, 0xE0, 0x1E, 0xF0, 0x0E, 0xF0, 0x0E }, // map to packed key E01FE01FF10EF10E
\r
89 { 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0, 0xFE }, // map to packed key E0FEE0FEF1FEF1FE
\r
90 { 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00 }, // map to packed key FE01FE01FE01FE01
\r
91 { 0xFE, 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E }, // map to packed key FE1FFE1FFE0EFE0E
\r
92 { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0 }, // map to packed key FEE0FEE0FEF1FEF1
\r
95 public static bool IsWeakKey (byte[] rgbKey)
98 throw new CryptographicException (Locale.GetText ("Null Key"));
99 if (rgbKey.Length != keySizeByte)
100 throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
102 // (fast) pre-check with "weak bytes"
103 for (int i=0; i < rgbKey.Length; i++) {
104 switch (rgbKey [i] | 0x11) {
115 // compare with known weak keys
116 for (int i=0; i < (weakKeys.Length >> 3); i++) {
118 for (; j < rgbKey.Length; j++) {
119 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
128 public static bool IsSemiWeakKey (byte[] rgbKey)
131 throw new CryptographicException (Locale.GetText ("Null Key"));
132 if (rgbKey.Length != keySizeByte)
133 throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
135 // (fast) pre-check with "weak bytes"
136 for (int i=0; i < rgbKey.Length; i++) {
137 switch (rgbKey [i] | 0x11) {
148 // compare with known weak keys
149 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
151 for (; j < rgbKey.Length; j++) {
152 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
161 public override byte[] Key {
163 if (KeyValue == null) {
164 // GenerateKey is responsible to return a valid key
165 // e.g. no weak or semi-weak keys
168 return (byte[]) KeyValue.Clone ();
172 throw new ArgumentNullException ("Key");
173 if (value.Length != keySizeByte)
174 throw new ArgumentException (Locale.GetText ("Wrong Key Length"));
175 if (IsWeakKey (value))
176 throw new CryptographicException (Locale.GetText ("Weak Key"));
177 if (IsSemiWeakKey (value))
178 throw new CryptographicException (Locale.GetText ("Semi Weak Key"));
180 KeyValue = (byte[]) value.Clone ();