2008-01-18 Jb Evain <jbevain@novell.com>
[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, 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 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 public byte[] Key (int size) 
50                 {
51                         if (rng == null)
52                                 rng = RandomNumberGenerator.Create ();
53
54                         byte[] key = new byte [size];
55                         rng.GetBytes (key);
56                         return key;
57                 }
58         
59                 static public byte[] IV (int size) 
60                 {
61                         if (rng == null)
62                                 rng = RandomNumberGenerator.Create ();
63
64                         byte[] iv = new byte [size];
65                         rng.GetBytes (iv);
66                         return iv;
67                 }
68         }
69         
70         // Process an array as a sequence of blocks
71 #if INSIDE_CORLIB
72         internal
73 #else
74         public
75 #endif
76         class BlockProcessor {
77                 private ICryptoTransform transform;
78                 private byte[] block;
79                 private int blockSize;  // in bytes (not in bits)
80                 private int blockCount;
81         
82                 public BlockProcessor (ICryptoTransform transform) 
83                         : this (transform, transform.InputBlockSize) {} 
84         
85                 // some Transforms (like HashAlgorithm descendant) return 1 for
86                 // block size (which isn't their real internal block size)
87                 public BlockProcessor (ICryptoTransform transform, int blockSize)
88                 {
89                         this.transform = transform;
90                         this.blockSize = blockSize;
91                         block = new byte [blockSize];
92                 }
93         
94                 ~BlockProcessor () 
95                 {
96                         // zeroize our block (so we don't retain any information)
97                         Array.Clear (block, 0, blockSize);
98                 }
99         
100                 public void Initialize ()
101                 {
102                         Array.Clear (block, 0, blockSize);
103                         blockCount = 0;
104                 }
105         
106                 public void Core (byte[] rgb) 
107                 {
108                         Core (rgb, 0, rgb.Length);
109                 }
110         
111                 public void Core (byte[] rgb, int ib, int cb) 
112                 {
113                         // 1. fill the rest of the "block"
114                         int n = System.Math.Min (blockSize - blockCount, cb);
115                         Buffer.BlockCopy (rgb, ib, block, blockCount, n); 
116                         blockCount += n;
117         
118                         // 2. if block is full then transform it
119                         if (blockCount == blockSize) {
120                                 transform.TransformBlock (block, 0, blockSize, block, 0);
121         
122                                 // 3. transform any other full block in specified buffer
123                                 int b = (int) ((cb - n) / blockSize);
124                                 for (int i=0; i < b; i++) {
125                                         transform.TransformBlock (rgb, n + ib, blockSize, block, 0);
126                                         n += blockSize;
127                                 }
128         
129                                 // 4. if data is still present fill the "block" with the remainder
130                                 blockCount = cb - n;
131                                 if (blockCount > 0)
132                                         Buffer.BlockCopy (rgb, n, block, 0, blockCount);
133                         }
134                 }
135         
136                 public byte[] Final () 
137                 {
138                         return transform.TransformFinalBlock (block, 0, blockCount);
139                 }
140         }
141 }