2 // Mono.Security.Cryptography.CryptoTools
3 // Shared class for common cryptographic functionalities
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004, 2008 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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Security.Cryptography;
34 namespace Mono.Security.Cryptography {
41 sealed class KeyBuilder {
43 static private RandomNumberGenerator rng;
49 static RandomNumberGenerator Rng {
52 rng = RandomNumberGenerator.Create ();
57 static public byte[] Key (int size)
59 byte[] key = new byte [size];
64 static public byte[] IV (int size)
66 byte[] iv = new byte [size];
72 // Process an array as a sequence of blocks
78 class BlockProcessor {
79 private ICryptoTransform transform;
81 private int blockSize; // in bytes (not in bits)
82 private int blockCount;
84 public BlockProcessor (ICryptoTransform transform)
85 : this (transform, transform.InputBlockSize) {}
87 // some Transforms (like HashAlgorithm descendant) return 1 for
88 // block size (which isn't their real internal block size)
89 public BlockProcessor (ICryptoTransform transform, int blockSize)
91 if (transform == null)
92 throw new ArgumentNullException ("transform");
94 throw new ArgumentOutOfRangeException ("blockSize");
95 this.transform = transform;
96 this.blockSize = blockSize;
97 block = new byte [blockSize];
102 // zeroize our block (so we don't retain any information)
103 Array.Clear (block, 0, blockSize);
106 public void Initialize ()
108 Array.Clear (block, 0, blockSize);
112 public void Core (byte[] rgb)
114 Core (rgb, 0, rgb.Length);
117 public void Core (byte[] rgb, int ib, int cb)
119 // 1. fill the rest of the "block"
120 int n = System.Math.Min (blockSize - blockCount, cb);
121 Buffer.BlockCopy (rgb, ib, block, blockCount, n);
124 // 2. if block is full then transform it
125 if (blockCount == blockSize) {
126 transform.TransformBlock (block, 0, blockSize, block, 0);
128 // 3. transform any other full block in specified buffer
129 int b = (int) ((cb - n) / blockSize);
130 for (int i=0; i < b; i++) {
131 transform.TransformBlock (rgb, n + ib, blockSize, block, 0);
135 // 4. if data is still present fill the "block" with the remainder
138 Buffer.BlockCopy (rgb, n + ib, block, 0, blockCount);
142 public byte[] Final ()
144 return transform.TransformFinalBlock (block, 0, blockCount);