Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DES.cs
1 //
2 // System.Security.Cryptography.DES.cs
3 //
4 // Author:
5 //      Sergey Chaban (serge@wildwestsoftware.com)
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33
34 // References:
35 // a.   FIPS PUB 46-3: Data Encryption Standard
36 //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
37
38 namespace System.Security.Cryptography {
39
40 [ComVisible (true)]
41 public abstract class DES : SymmetricAlgorithm {
42
43         private const int keySizeByte = 8;
44
45         protected DES ()
46         {
47                 KeySizeValue = 64; 
48                 BlockSizeValue = 64; 
49                 FeedbackSizeValue = 8;
50
51                 LegalKeySizesValue = new KeySizes[1];
52                 LegalKeySizesValue[0] = new KeySizes(64, 64, 0);
53
54                 LegalBlockSizesValue = new KeySizes[1];
55                 LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
56         }
57
58         public static new DES Create () 
59         {
60                 return Create ("System.Security.Cryptography.DES");
61         }
62
63         public static new DES Create (string algName) 
64         {
65                 return (DES) CryptoConfig.CreateFromName (algName);
66         }
67
68
69         // Ek(Ek(m)) = m
70         internal static readonly byte[,] weakKeys = {
71                 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
72                 { 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F },
73                 { 0xE1, 0xE1, 0xE1, 0xE1, 0xF1, 0xF1, 0xF1, 0xF1 },
74                 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
75         };
76
77         // Ek1(Ek2(m)) = m
78         internal static readonly byte[,] semiWeakKeys = {
79                 { 0x00, 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E }, // map to packed key 011F011F010E010E
80                 { 0x00, 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0 }, // map to packed key 01E001E001F101F1
81                 { 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE }, // map to packed key 01FE01FE01FE01FE
82                 { 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E, 0x00 }, // map to packed key 1F011F010E010E01
83                 { 0x1E, 0xE0, 0x1E, 0xE0, 0x0E, 0xF0, 0x0E, 0xF0 }, // map to packed key 1FE01FE00EF10EF1
84                 { 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, // map to packed key 1FFE1FFE0EFE0EFE
85                 { 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x00 }, // map to packed key E001E001F101F101
86                 { 0xE0, 0x1E, 0xE0, 0x1E, 0xF0, 0x0E, 0xF0, 0x0E }, // map to packed key E01FE01FF10EF10E
87                 { 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0, 0xFE }, // map to packed key E0FEE0FEF1FEF1FE
88                 { 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00 }, // map to packed key FE01FE01FE01FE01
89                 { 0xFE, 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E }, // map to packed key FE1FFE1FFE0EFE0E
90                 { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0 }, // map to packed key FEE0FEE0FEF1FEF1
91         };
92
93         public static bool IsWeakKey (byte[] rgbKey) 
94         {
95                 if (rgbKey == null)
96                         throw new CryptographicException (Locale.GetText ("Null Key"));
97                 if (rgbKey.Length != keySizeByte)
98                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
99
100                 // (fast) pre-check with "weak bytes"
101                 for (int i=0; i < rgbKey.Length; i++) {
102                         switch (rgbKey [i] | 0x11) {
103                                 case 0x11:
104                                 case 0x1F:
105                                 case 0xF1:
106                                 case 0xFF:
107                                         break;
108                                 default:
109                                         return false;
110                         }
111                 }
112
113                 // compare with known weak keys
114                 for (int i=0; i < (weakKeys.Length >> 3); i++) {
115                         int j = 0;
116                         for (; j < rgbKey.Length; j++) {
117                                 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
118                                         break;
119                         }
120                         if (j==8)
121                                 return true;
122                 }
123                 return false;
124         }
125
126         public static bool IsSemiWeakKey (byte[] rgbKey)
127         {
128                 if (rgbKey == null)
129                         throw new CryptographicException (Locale.GetText ("Null Key"));
130                 if (rgbKey.Length != keySizeByte)
131                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
132
133                 // (fast) pre-check with "weak bytes"
134                 for (int i=0; i < rgbKey.Length; i++) {
135                         switch (rgbKey [i] | 0x11) {
136                                 case 0x11:
137                                 case 0x1F:
138                                 case 0xF1:
139                                 case 0xFF:
140                                         break;
141                                 default:
142                                         return false;
143                         }
144                 }
145
146                 // compare with known weak keys
147                 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
148                         int j = 0;
149                         for (; j < rgbKey.Length; j++) {
150                                 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
151                                         break;
152                         }
153                         if (j==8)
154                                 return true;
155                 }
156                 return false;
157         }
158
159         public override byte[] Key {
160                 get {
161                         if (KeyValue == null) {
162                                 // GenerateKey is responsible to return a valid key
163                                 // e.g. no weak or semi-weak keys
164                                 GenerateKey ();
165                         }
166                         return (byte[]) KeyValue.Clone ();
167                 }
168                 set {
169                         if (value == null)
170                                 throw new ArgumentNullException ("Key");
171                         if (value.Length != keySizeByte)
172                                 throw new ArgumentException (Locale.GetText ("Wrong Key Length"));
173                         if (IsWeakKey (value))
174                                 throw new CryptographicException (Locale.GetText ("Weak Key"));
175                         if (IsSemiWeakKey (value))
176                                 throw new CryptographicException (Locale.GetText ("Semi Weak Key"));
177
178                         KeyValue = (byte[]) value.Clone ();
179                 }
180         }
181 }
182
183 }
184