[bugfix]667855 - Fix handling of oracle raw data types sanely.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / CryptoTools.cs
1 //
2 // Mono.Security.Cryptography.CryptoTools
3 //      Shared class for common cryptographic functionalities
4 //
5 // Authors:
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004, 2008 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;
32 using System.Security.Cryptography;
33
34 namespace Mono.Security.Cryptography {
35
36 #if INSIDE_CORLIB
37         internal
38 #else
39         public
40 #endif
41         sealed class KeyBuilder {
42         
43                 static private RandomNumberGenerator rng;
44
45                 private KeyBuilder ()
46                 {
47                 }
48
49                 static RandomNumberGenerator Rng {
50                         get {
51 #if MOONLIGHT
52                                 if (rng == null)
53                                         rng = new RNGCryptoServiceProvider ();
54 #else
55                                 if (rng == null)
56                                         rng = RandomNumberGenerator.Create ();
57 #endif
58                                 return rng;
59                         }
60                 }
61         
62                 static public byte[] Key (int size) 
63                 {
64                         byte[] key = new byte [size];
65                         Rng.GetBytes (key);
66                         return key;
67                 }
68         
69                 static public byte[] IV (int size) 
70                 {
71                         byte[] iv = new byte [size];
72                         Rng.GetBytes (iv);
73                         return iv;
74                 }
75         }
76         
77         // Process an array as a sequence of blocks
78 #if INSIDE_CORLIB
79         internal
80 #else
81         public
82 #endif
83         class BlockProcessor {
84                 private ICryptoTransform transform;
85                 private byte[] block;
86                 private int blockSize;  // in bytes (not in bits)
87                 private int blockCount;
88         
89                 public BlockProcessor (ICryptoTransform transform) 
90                         : this (transform, transform.InputBlockSize) {} 
91         
92                 // some Transforms (like HashAlgorithm descendant) return 1 for
93                 // block size (which isn't their real internal block size)
94                 public BlockProcessor (ICryptoTransform transform, int blockSize)
95                 {
96                         this.transform = transform;
97                         this.blockSize = blockSize;
98                         block = new byte [blockSize];
99                 }
100         
101                 ~BlockProcessor () 
102                 {
103                         // zeroize our block (so we don't retain any information)
104                         Array.Clear (block, 0, blockSize);
105                 }
106         
107                 public void Initialize ()
108                 {
109                         Array.Clear (block, 0, blockSize);
110                         blockCount = 0;
111                 }
112         
113                 public void Core (byte[] rgb) 
114                 {
115                         Core (rgb, 0, rgb.Length);
116                 }
117         
118                 public void Core (byte[] rgb, int ib, int cb) 
119                 {
120                         // 1. fill the rest of the "block"
121                         int n = System.Math.Min (blockSize - blockCount, cb);
122                         Buffer.BlockCopy (rgb, ib, block, blockCount, n); 
123                         blockCount += n;
124         
125                         // 2. if block is full then transform it
126                         if (blockCount == blockSize) {
127                                 transform.TransformBlock (block, 0, blockSize, block, 0);
128         
129                                 // 3. transform any other full block in specified buffer
130                                 int b = (int) ((cb - n) / blockSize);
131                                 for (int i=0; i < b; i++) {
132                                         transform.TransformBlock (rgb, n + ib, blockSize, block, 0);
133                                         n += blockSize;
134                                 }
135         
136                                 // 4. if data is still present fill the "block" with the remainder
137                                 blockCount = cb - n;
138                                 if (blockCount > 0)
139                                         Buffer.BlockCopy (rgb, n + ib, block, 0, blockCount);
140                         }
141                 }
142         
143                 public byte[] Final () 
144                 {
145                         return transform.TransformFinalBlock (block, 0, blockCount);
146                 }
147         }
148 }