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 // (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System
.Security
.Cryptography
;
38 namespace Mono
.Security
.Cryptography
{
45 sealed class KeyBuilder
{
47 static private RandomNumberGenerator rng
;
53 static public byte[] Key (int size
)
56 rng
= RandomNumberGenerator
.Create ();
58 byte[] key
= new byte [size
];
63 static public byte[] IV (int size
)
66 rng
= RandomNumberGenerator
.Create ();
68 byte[] iv
= new byte [size
];
74 // Process an array as a sequence of blocks
80 class BlockProcessor
{
81 private ICryptoTransform transform
;
83 private int blockSize
; // in bytes (not in bits)
84 private int blockCount
;
86 public BlockProcessor (ICryptoTransform transform
)
87 : this (transform
, transform
.InputBlockSize
) {}
89 // some Transforms (like HashAlgorithm descendant) return 1 for
90 // block size (which isn't their real internal block size)
91 public BlockProcessor (ICryptoTransform transform
, int blockSize
)
93 this.transform
= transform
;
94 this.blockSize
= blockSize
;
95 block
= new byte [blockSize
];
100 // zeroize our block (so we don't retain any information)
101 Array
.Clear (block
, 0, blockSize
);
104 public void Initialize ()
106 Array
.Clear (block
, 0, blockSize
);
110 public void Core (byte[] rgb
)
112 Core (rgb
, 0, rgb
.Length
);
115 public void Core (byte[] rgb
, int ib
, int cb
)
117 // 1. fill the rest of the "block"
118 int n
= System
.Math
.Min (blockSize
- blockCount
, cb
);
119 Buffer
.BlockCopy (rgb
, ib
, block
, blockCount
, n
);
122 // 2. if block is full then transform it
123 if (blockCount
== blockSize
) {
124 transform
.TransformBlock (block
, 0, blockSize
, block
, 0);
126 // 3. transform any other full block in specified buffer
127 int b
= (int) ((cb
- n
) / blockSize
);
128 for (int i
=0; i
< b
; i
++) {
129 transform
.TransformBlock (rgb
, n
, blockSize
, block
, 0);
133 // 4. if data is still present fill the "block" with the remainder
136 Buffer
.BlockCopy (rgb
, n
, block
, 0, blockCount
);
140 public byte[] Final ()
142 return transform
.TransformFinalBlock (block
, 0, blockCount
);