2010-03-12 Jb Evain <jbevain@novell.com>
[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 #if !NET_2_1 || MONOTOUCH
32
33 using System.Globalization;
34 using System.Runtime.InteropServices;
35
36 // References:
37 // a.   FIPS PUB 46-3: Data Encryption Standard
38 //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
39
40 namespace System.Security.Cryptography {
41
42 [ComVisible (true)]
43 public abstract class DES : SymmetricAlgorithm {
44
45         private const int keySizeByte = 8;
46
47         protected 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 algName) 
66         {
67                 return (DES) CryptoConfig.CreateFromName (algName);
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 (rgbKey == null)
98                         throw new CryptographicException (Locale.GetText ("Null Key"));
99                 if (rgbKey.Length != keySizeByte)
100                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
101
102                 // (fast) pre-check with "weak bytes"
103                 for (int i=0; i < rgbKey.Length; i++) {
104                         switch (rgbKey [i] | 0x11) {
105                                 case 0x11:
106                                 case 0x1F:
107                                 case 0xF1:
108                                 case 0xFF:
109                                         break;
110                                 default:
111                                         return false;
112                         }
113                 }
114
115                 // compare with known weak keys
116                 for (int i=0; i < (weakKeys.Length >> 3); i++) {
117                         int j = 0;
118                         for (; j < rgbKey.Length; j++) {
119                                 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
120                                         break;
121                         }
122                         if (j==8)
123                                 return true;
124                 }
125                 return false;
126         }
127
128         public static bool IsSemiWeakKey (byte[] rgbKey)
129         {
130                 if (rgbKey == null)
131                         throw new CryptographicException (Locale.GetText ("Null Key"));
132                 if (rgbKey.Length != keySizeByte)
133                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
134
135                 // (fast) pre-check with "weak bytes"
136                 for (int i=0; i < rgbKey.Length; i++) {
137                         switch (rgbKey [i] | 0x11) {
138                                 case 0x11:
139                                 case 0x1F:
140                                 case 0xF1:
141                                 case 0xFF:
142                                         break;
143                                 default:
144                                         return false;
145                         }
146                 }
147
148                 // compare with known weak keys
149                 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
150                         int j = 0;
151                         for (; j < rgbKey.Length; j++) {
152                                 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
153                                         break;
154                         }
155                         if (j==8)
156                                 return true;
157                 }
158                 return false;
159         }
160
161         public override byte[] Key {
162                 get {
163                         if (KeyValue == null) {
164                                 // GenerateKey is responsible to return a valid key
165                                 // e.g. no weak or semi-weak keys
166                                 GenerateKey ();
167                         }
168                         return (byte[]) KeyValue.Clone ();
169                 }
170                 set {
171                         if (value == null)
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"));
179
180                         KeyValue = (byte[]) value.Clone ();
181                 }
182         }
183 }
184
185 }
186
187 #endif
188