bring Mono Security to monotouch
[mcs.git] / class / corlib / System.Security.Cryptography / DES.cs
blob5c17493e6cf44f0289edbbb15ce6cf538e9f2253
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)
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.
31 #if !NET_2_1 || MONOTOUCH
33 using System.Globalization;
34 using System.Runtime.InteropServices;
36 // References:
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 {
42 #if NET_2_0
43 [ComVisible (true)]
44 #endif
45 public abstract class DES : SymmetricAlgorithm {
47 private const int keySizeByte = 8;
49 #if NET_2_0
50 protected DES ()
51 #else
52 public DES ()
53 #endif
55 KeySizeValue = 64;
56 BlockSizeValue = 64;
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);
77 // Ek(Ek(m)) = m
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 },
85 // Ek1(Ek2(m)) = m
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)
103 #if NET_2_0
104 if (rgbKey == null)
105 throw new CryptographicException (Locale.GetText ("Null Key"));
106 #endif
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) {
113 case 0x11:
114 case 0x1F:
115 case 0xF1:
116 case 0xFF:
117 break;
118 default:
119 return false;
123 // compare with known weak keys
124 for (int i=0; i < (weakKeys.Length >> 3); i++) {
125 int j = 0;
126 for (; j < rgbKey.Length; j++) {
127 if ((rgbKey [j] ^ weakKeys [i,j]) > 1)
128 break;
130 if (j==8)
131 return true;
133 return false;
136 public static bool IsSemiWeakKey (byte[] rgbKey)
138 #if NET_2_0
139 if (rgbKey == null)
140 throw new CryptographicException (Locale.GetText ("Null Key"));
141 #endif
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) {
148 case 0x11:
149 case 0x1F:
150 case 0xF1:
151 case 0xFF:
152 break;
153 default:
154 return false;
158 // compare with known weak keys
159 for (int i=0; i < (semiWeakKeys.Length >> 3); i++) {
160 int j = 0;
161 for (; j < rgbKey.Length; j++) {
162 if ((rgbKey [j] ^ semiWeakKeys [i,j]) > 1)
163 break;
165 if (j==8)
166 return true;
168 return false;
171 public override byte[] Key {
172 get {
173 if (KeyValue == null) {
174 // GenerateKey is responsible to return a valid key
175 // e.g. no weak or semi-weak keys
176 GenerateKey ();
178 return (byte[]) KeyValue.Clone ();
180 set {
181 if (value == null)
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 ();
197 #endif