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
{
45 public abstract class DES
: SymmetricAlgorithm
{
47 private const int keySizeByte
= 8;
57 FeedbackSizeValue
= 8;
59 LegalKeySizesValue
= new KeySizes
[1];
60 LegalKeySizesValue
[0] = new KeySizes(64, 64, 0);
62 LegalBlockSizesValue
= new KeySizes
[1];
63 LegalBlockSizesValue
[0] = new KeySizes(64, 64, 0);
66 public static new DES
Create ()
68 return Create ("System.Security.Cryptography.DES");
71 public static new DES
Create (string algName
)
73 return (DES
) CryptoConfig
.CreateFromName (algName
);
78 internal static readonly byte[,] weakKeys
= {
79 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }
,
80 { 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F }
,
81 { 0xE1, 0xE1, 0xE1, 0xE1, 0xF1, 0xF1, 0xF1, 0xF1 }
,
82 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }
,
86 internal static readonly byte[,] semiWeakKeys
= {
87 { 0x00, 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E }
, // map to packed key 011F011F010E010E
88 { 0x00, 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0 }
, // map to packed key 01E001E001F101F1
89 { 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE }
, // map to packed key 01FE01FE01FE01FE
90 { 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E, 0x00 }
, // map to packed key 1F011F010E010E01
91 { 0x1E, 0xE0, 0x1E, 0xE0, 0x0E, 0xF0, 0x0E, 0xF0 }
, // map to packed key 1FE01FE00EF10EF1
92 { 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }
, // map to packed key 1FFE1FFE0EFE0EFE
93 { 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x00 }
, // map to packed key E001E001F101F101
94 { 0xE0, 0x1E, 0xE0, 0x1E, 0xF0, 0x0E, 0xF0, 0x0E }
, // map to packed key E01FE01FF10EF10E
95 { 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0, 0xFE }
, // map to packed key E0FEE0FEF1FEF1FE
96 { 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00 }
, // map to packed key FE01FE01FE01FE01
97 { 0xFE, 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E }
, // map to packed key FE1FFE1FFE0EFE0E
98 { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0 }
, // map to packed key FEE0FEE0FEF1FEF1
101 public static bool IsWeakKey (byte[] rgbKey
)
105 throw new CryptographicException (Locale
.GetText ("Null Key"));
107 if (rgbKey
.Length
!= keySizeByte
)
108 throw new CryptographicException (Locale
.GetText ("Wrong Key Length"));
110 // (fast) pre-check with "weak bytes"
111 for (int i
=0; i
< rgbKey
.Length
; i
++) {
112 switch (rgbKey
[i
] | 0x11) {
123 // compare with known weak keys
124 for (int i
=0; i
< (weakKeys
.Length
>> 3); i
++) {
126 for (; j
< rgbKey
.Length
; j
++) {
127 if ((rgbKey
[j
] ^ weakKeys
[i
,j
]) > 1)
136 public static bool IsSemiWeakKey (byte[] rgbKey
)
140 throw new CryptographicException (Locale
.GetText ("Null Key"));
142 if (rgbKey
.Length
!= keySizeByte
)
143 throw new CryptographicException (Locale
.GetText ("Wrong Key Length"));
145 // (fast) pre-check with "weak bytes"
146 for (int i
=0; i
< rgbKey
.Length
; i
++) {
147 switch (rgbKey
[i
] | 0x11) {
158 // compare with known weak keys
159 for (int i
=0; i
< (semiWeakKeys
.Length
>> 3); i
++) {
161 for (; j
< rgbKey
.Length
; j
++) {
162 if ((rgbKey
[j
] ^ semiWeakKeys
[i
,j
]) > 1)
171 public override byte[] Key
{
173 if (KeyValue
== null) {
174 // GenerateKey is responsible to return a valid key
175 // e.g. no weak or semi-weak keys
178 return (byte[]) KeyValue
.Clone ();
182 throw new ArgumentNullException ("Key");
183 if (value.Length
!= keySizeByte
)
184 throw new ArgumentException (Locale
.GetText ("Wrong Key Length"));
185 if (IsWeakKey (value))
186 throw new CryptographicException (Locale
.GetText ("Weak Key"));
187 if (IsSemiWeakKey (value))
188 throw new CryptographicException (Locale
.GetText ("Semi Weak Key"));
190 KeyValue
= (byte[]) value.Clone ();