Switch to compiler-tester
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DES.cs
1 //
2 // System.Security.Cryptography.DES
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-2005 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 #if NET_2_0
41 [ComVisible (true)]
42 #endif
43 public abstract class DES : SymmetricAlgorithm {
44
45         private const int keySizeByte = 8;
46
47         public DES ()
48         {
49                 KeySizeValue = 64; 
50                 BlockSizeValue = 64; 
51                 FeedbackSizeValue = 8;
52
53                 LegalKeySizesValue = new KeySizes[1];
54                 LegalKeySizesValue[0] = new KeySizes(64, 64, 0);
55
56                 LegalBlockSizesValue = new KeySizes[1];
57                 LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
58         }
59
60         public static new DES Create () 
61         {
62                 return Create ("System.Security.Cryptography.DES");
63         }
64
65         public static new DES Create (string algo) 
66         {
67                 return (DES) CryptoConfig.CreateFromName (algo);
68         }
69
70
71         // Ek(Ek(m)) = m
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 },
77         };
78
79         // Ek1(Ek2(m)) = m
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
93         };\r
94
95         public static bool IsWeakKey (byte[] rgbKey) 
96         {
97 #if NET_2_0
98                 if (rgbKey == null)
99                         throw new CryptographicException (Locale.GetText ("Null Key"));
100 #endif
101                 if (rgbKey.Length != keySizeByte)
102                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
103
104                 // (fast) pre-check with "weak bytes"
105                 for (int i=0; i < rgbKey.Length; i++) {
106                         switch (rgbKey [i] | 0x11) {
107                                 case 0x11:
108                                 case 0x1F:
109                                 case 0xF1:
110                                 case 0xFF:
111                                         break;
112                                 default:
113                                         return false;
114                         }
115                 }
116
117                 // compare with known weak keys
118                 for (int i=0; i < (weakKeys.Length >> 3); i++) {
119                         int j = 0;
120                         for (; j < rgbKey.Length; j++) {
121                                 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
122                                         break;
123                         }
124                         if (j==8)
125                                 return true;
126                 }
127                 return false;
128         }
129
130         public static bool IsSemiWeakKey (byte[] rgbKey)
131         {
132 #if NET_2_0
133                 if (rgbKey == null)
134                         throw new CryptographicException (Locale.GetText ("Null Key"));
135 #endif
136                 if (rgbKey.Length != keySizeByte)
137                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
138
139                 // (fast) pre-check with "weak bytes"
140                 for (int i=0; i < rgbKey.Length; i++) {
141                         switch (rgbKey [i] | 0x11) {
142                                 case 0x11:
143                                 case 0x1F:
144                                 case 0xF1:
145                                 case 0xFF:
146                                         break;
147                                 default:
148                                         return false;
149                         }
150                 }
151
152                 // compare with known weak keys
153                 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
154                         int j = 0;
155                         for (; j < rgbKey.Length; j++) {
156                                 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
157                                         break;
158                         }
159                         if (j==8)
160                                 return true;
161                 }
162                 return false;
163         }
164
165         public override byte[] Key {
166                 get {
167                         if (KeyValue == null) {
168                                 // GenerateKey is responsible to return a valid key
169                                 // e.g. no weak or semi-weak keys
170                                 GenerateKey ();
171                         }
172                         return (byte[]) KeyValue.Clone ();
173                 }
174                 set {
175                         if (value == null)
176                                 throw new ArgumentNullException ("Key");
177                         if (value.Length != keySizeByte)
178                                 throw new ArgumentException (Locale.GetText ("Wrong Key Length"));
179                         if (IsWeakKey (value))
180                                 throw new CryptographicException (Locale.GetText ("Weak Key"));
181                         if (IsSemiWeakKey (value))
182                                 throw new CryptographicException (Locale.GetText ("Semi Weak Key"));
183
184                         KeyValue = (byte[]) value.Clone ();
185                 }
186         }
187 }
188
189 }