Update of /cvsroot/pgsqlclient/pgsqlclient_10/PgSqlClient.Security.Tls/source/Cryptography
In directory sc8-pr-cvs1:/tmp/cvs-serv20307
Added Files:
ARC4Managed.cs RC4.cs
Log Message:
Added initial release of a simple TLS protocol implementation,
it uses tree files from the Mono project that are under the MIT X11
license.
--- NEW FILE: ARC4Managed.cs ---
//
// ARC4Managed.cs: Alleged RC4(tm) compatible symmetric stream cipher
// RC4 is a trademark of RSA Security
//
using System;
using System.Security.Cryptography;
namespace Mono.Security.Cryptography {
// References:
// a. Usenet 1994 - RC4 Algorithm revealed
// http://www.qrst.de/html/dsds/rc4.htm
public class ARC4Managed : RC4, ICryptoTransform {
private byte[] key;
private byte[] state;
private byte x;
private byte y;
private bool m_disposed;
public ARC4Managed () : base ()
{
state = new byte [256];
m_disposed = false;
}
~ARC4Managed ()
{
Dispose (true);
}
protected override void Dispose (bool disposing)
{
if (!m_disposed) {
x = 0;
y = 0;
if (key != null) {
Array.Clear (key, 0, key.Length);
key = null;
}
Array.Clear (state, 0, state.Length);
state = null;
GC.SuppressFinalize (this);
m_disposed = true;
}
}
public override byte[] Key {
get { return (byte[]) key.Clone (); }
set {
key = (byte[]) value.Clone ();
KeySetup (key);
}
}
public bool CanReuseTransform {
get { return false; }
}
public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgvIV)
{
Key = rgbKey;
return (ICryptoTransform) this;
}
public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgvIV)
{
Key = rgbKey;
return CreateEncryptor ();
}
public override void GenerateIV ()
{
// not used for a stream cipher
IV = new byte [0];
}
public override void GenerateKey ()
{
byte[] key = new byte [KeySizeValue >> 3];
RandomNumberGenerator rng = RandomNumberGenerator.Create ();
rng.GetBytes (key);
Key = key;
}
public bool CanTransformMultipleBlocks {
get { return true; }
}
public int InputBlockSize {
get { return 1; }
}
public int OutputBlockSize {
get { return 1; }
}
private void KeySetup (byte[] key)
{
byte index1 = 0;
byte index2 = 0;
for (int counter = 0; counter < 256; counter++)
state [counter] = (byte) counter;
x = 0;
y = 0;
for (int counter = 0; counter < 256; counter++) {
index2 = (byte) ((key [index1] + state [counter] + index2) % 256);
// swap byte
byte tmp = state [counter];
state [counter] = state [index2];
state [index2] = tmp;
index1 = (byte) ((index1 + 1) % key.Length);
}
}
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
byte xorIndex;
for (int counter = 0; counter < inputCount; counter ++) {
x = (byte) ((x + 1) % 256);
y = (byte) ((state [x] + y) % 256);
// swap byte
byte tmp = state [x];
state [x] = state [y];
state [y] = tmp;
xorIndex = (byte) (state [x] + (state [y]) % 256);
outputBuffer [outputOffset + counter] = (byte) (inputBuffer [inputOffset + counter] ^ state [xorIndex]);
}
return inputCount;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
byte[] output = new byte [inputCount];
TransformBlock (inputBuffer, inputOffset, inputCount, output, 0);
return output;
}
}
}
--- NEW FILE: RC4.cs ---
//
// RC4.cs: RC4(tm) symmetric stream cipher
// RC4 is a trademark of RSA Security
//
// Author:
// Sebastien Pouliot (spo...@mo...)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
using System;
using System.Security.Cryptography;
namespace Mono.Security.Cryptography {
public abstract class RC4 : SymmetricAlgorithm {
private static KeySizes[] s_legalBlockSizes = {
new KeySizes (64, 64, 0)
};
private static KeySizes[] s_legalKeySizes = {
new KeySizes (40, 2048, 8)
};
public RC4()
{
KeySizeValue = 128;
BlockSizeValue = 64;
FeedbackSizeValue = BlockSizeValue;
LegalBlockSizesValue = s_legalBlockSizes;
LegalKeySizesValue = s_legalKeySizes;
}
new static public RC4 Create()
{
return Create ("RC4");
}
new static public RC4 Create (string algName)
{
object o = CryptoConfig.CreateFromName (algName);
// in case machine.config isn't configured to use
// any RC4 implementation
if (o == null) {
o = new ARC4Managed ();
}
return (RC4) o;
}
}
}
|