[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / TripleDESCryptoServiceProvider.cs
blob6c66f945e9bc3af0fcc2447fce9508ceafa5db1f
1 //
2 // TripleDESCryptoServiceProvider.cs: Default TripleDES implementation
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
31 using Mono.Security.Cryptography;
33 namespace System.Security.Cryptography {
35 // References:
36 // a. FIPS PUB 46-3: TripleDES
37 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
38 // b. ANSI X9.52
39 // not free :-(
40 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
42 // TripleDES is just DES-EDE
43 internal class TripleDESTransform : SymmetricTransform {
45 // for encryption
46 private DESTransform E1;
47 private DESTransform D2;
48 private DESTransform E3;
50 // for decryption
51 private DESTransform D1;
52 private DESTransform E2;
53 private DESTransform D3;
55 public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv)
57 if (key == null) {
58 key = GetStrongKey ();
60 // note: checking weak keys also checks valid key length
61 if (TripleDES.IsWeakKey (key)) {
62 string msg = Locale.GetText ("This is a known weak key.");
63 throw new CryptographicException (msg);
66 byte[] key1 = new byte [8];
67 byte[] key2 = new byte [8];
68 byte[] key3 = new byte [8];
69 DES des = DES.Create ();
70 Buffer.BlockCopy (key, 0, key1, 0, 8);
71 Buffer.BlockCopy (key, 8, key2, 0, 8);
72 if (key.Length == 16)
73 Buffer.BlockCopy (key, 0, key3, 0, 8);
74 else
75 Buffer.BlockCopy (key, 16, key3, 0, 8);
77 // note: some modes (like CFB) requires encryption when decrypting
78 if ((encryption) || (algo.Mode == CipherMode.CFB)) {
79 E1 = new DESTransform (des, true, key1, iv);
80 D2 = new DESTransform (des, false, key2, iv);
81 E3 = new DESTransform (des, true, key3, iv);
83 else {
84 D1 = new DESTransform (des, false, key3, iv);
85 E2 = new DESTransform (des, true, key2, iv);
86 D3 = new DESTransform (des, false, key1, iv);
90 // note: this method is garanteed to be called with a valid blocksize
91 // for both input and output
92 protected override void ECB (byte[] input, byte[] output)
94 DESTransform.Permutation (input, output, DESTransform.ipTab, false);
95 if (encrypt) {
96 E1.ProcessBlock (output, output);
97 D2.ProcessBlock (output, output);
98 E3.ProcessBlock (output, output);
100 else {
101 D1.ProcessBlock (output, output);
102 E2.ProcessBlock (output, output);
103 D3.ProcessBlock (output, output);
105 DESTransform.Permutation (output, output, DESTransform.fpTab, true);
108 static internal byte[] GetStrongKey ()
110 int size = DESTransform.BLOCK_BYTE_SIZE * 3;
111 byte[] key = KeyBuilder.Key (size);
112 while (TripleDES.IsWeakKey (key))
113 key = KeyBuilder.Key (size);
114 return key;