2005-01-06 Sebastien Pouliot <sebastien@ximian.com>
[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
33 // References:
34 // a.   FIPS PUB 46-3: Data Encryption Standard
35 //      http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
36
37 namespace System.Security.Cryptography {
38
39 public abstract class DES : SymmetricAlgorithm {
40
41         private const int keySizeByte = 8;
42
43         public DES ()
44         {
45                 KeySizeValue = 64; 
46                 BlockSizeValue = 64; 
47                 FeedbackSizeValue = 8;
48
49                 LegalKeySizesValue = new KeySizes[1];
50                 LegalKeySizesValue[0] = new KeySizes(64, 64, 0);
51
52                 LegalBlockSizesValue = new KeySizes[1];
53                 LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
54         }
55
56         public static new DES Create () 
57         {
58                 return Create ("System.Security.Cryptography.DES");
59         }
60
61         public static new DES Create (string algo) 
62         {
63                 return (DES) CryptoConfig.CreateFromName (algo);
64         }
65
66
67         // Ek(Ek(m)) = m
68         internal static readonly byte[,] weakKeys = {
69                 { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
70                 { 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F },
71                 { 0xE1, 0xE1, 0xE1, 0xE1, 0xF1, 0xF1, 0xF1, 0xF1 },
72                 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
73         };
74
75         // Ek1(Ek2(m)) = m
76         internal static readonly byte[,] semiWeakKeys = {
77                 { 0x00, 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E }, // map to packed key 011F011F010E010E\r
78                 { 0x00, 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0 }, // map to packed key 01E001E001F101F1\r
79                 { 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE }, // map to packed key 01FE01FE01FE01FE\r
80                 { 0x1E, 0x00, 0x1E, 0x00, 0x0E, 0x00, 0x0E, 0x00 }, // map to packed key 1F011F010E010E01\r
81                 { 0x1E, 0xE0, 0x1E, 0xE0, 0x0E, 0xF0, 0x0E, 0xF0 }, // map to packed key 1FE01FE00EF10EF1\r
82                 { 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE }, // map to packed key 1FFE1FFE0EFE0EFE\r
83                 { 0xE0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF0, 0x00 }, // map to packed key E001E001F101F101\r
84                 { 0xE0, 0x1E, 0xE0, 0x1E, 0xF0, 0x0E, 0xF0, 0x0E }, // map to packed key E01FE01FF10EF10E\r
85                 { 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0, 0xFE }, // map to packed key E0FEE0FEF1FEF1FE\r
86                 { 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00 }, // map to packed key FE01FE01FE01FE01\r
87                 { 0xFE, 0x1E, 0xFE, 0x1E, 0xFE, 0x0E, 0xFE, 0x0E }, // map to packed key FE1FFE1FFE0EFE0E\r
88                 { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF0, 0xFE, 0xF0 }, // map to packed key FEE0FEE0FEF1FEF1\r
89         };\r
90
91         public static bool IsWeakKey (byte[] rgbKey) 
92         {
93                 if (rgbKey.Length != keySizeByte)
94                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
95
96                 // (fast) pre-check with "weak bytes"
97                 for (int i=0; i < rgbKey.Length; i++) {
98                         switch (rgbKey [i] | 0x11) {
99                                 case 0x11:
100                                 case 0x1F:
101                                 case 0xF1:
102                                 case 0xFF:
103                                         break;
104                                 default:
105                                         return false;
106                         }
107                 }
108
109                 // compare with known weak keys
110                 for (int i=0; i < (weakKeys.Length >> 3); i++) {
111                         int j = 0;
112                         for (; j < rgbKey.Length; j++) {
113                                 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
114                                         break;
115                         }
116                         if (j==8)
117                                 return true;
118                 }
119                 return false;
120         }
121
122         public static bool IsSemiWeakKey (byte[] rgbKey)
123         {
124                 if (rgbKey.Length != keySizeByte)
125                         throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
126
127                 // (fast) pre-check with "weak bytes"
128                 for (int i=0; i < rgbKey.Length; i++) {
129                         switch (rgbKey [i] | 0x11) {
130                                 case 0x11:
131                                 case 0x1F:
132                                 case 0xF1:
133                                 case 0xFF:
134                                         break;
135                                 default:
136                                         return false;
137                         }
138                 }
139
140                 // compare with known weak keys
141                 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
142                         int j = 0;
143                         for (; j < rgbKey.Length; j++) {
144                                 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
145                                         break;
146                         }
147                         if (j==8)
148                                 return true;
149                 }
150                 return false;
151         }
152
153         public override byte[] Key {
154                 get {
155                         if (KeyValue == null) {
156                                 // GenerateKey is responsible to return a valid key
157                                 // e.g. no weak or semi-weak keys
158                                 GenerateKey ();
159                         }
160                         return (byte[]) KeyValue.Clone ();
161                 }
162                 set {
163                         if (value == null)
164                                 throw new ArgumentNullException ("Key");
165                         if (value.Length != keySizeByte)
166                                 throw new ArgumentException (Locale.GetText ("Wrong Key Length"));
167                         if (IsWeakKey (value))
168                                 throw new CryptographicException (Locale.GetText ("Weak Key"));
169                         if (IsSemiWeakKey (value))
170                                 throw new CryptographicException (Locale.GetText ("Semi Weak Key"));
171
172                         KeyValue = (byte[]) value.Clone ();
173                 }
174         }
175 }
176
177 }