-#if MONO
- // PKCS #1 v.2.1, Section 4.2
- // OS2IP converts an octet string to a nonnegative integer.
- static byte[] OS2IP (byte[] x)
- {
- int i = 0;
- while ((x [i++] == 0x00) && (i < x.Length)) {
- // confuse compiler into reporting a warning with {}
- }
- i--;
- if (i > 0) {
- byte[] result = new byte [x.Length - i];
- Buffer.BlockCopy (x, i, result, 0, result.Length);
- return result;
- }
- else
- return x;
- }
-
- static byte[] I2OSP (int x, int size)
- {
- byte[] array = Mono.Security.BitConverterLE.GetBytes (x);
- Array.Reverse (array, 0, array.Length);
- return I2OSP (array, size);
- }
-
- static byte[] I2OSP (byte[] x, int size)
- {
- byte[] result = new byte [size];
- Buffer.BlockCopy (x, 0, result, (result.Length - x.Length), x.Length);
- return result;
- }
-
- // PKCS #1 v.2.1, Section 5.1.1
- static byte[] RSAEP (RSA rsa, byte[] m)
- {
- // c = m^e mod n
- return rsa.EncryptValue (m);
- }
-
- // PKCS #1 v.2.1, Section B.2.1
- static byte[] MGF1 (HashAlgorithm hash, byte[] mgfSeed, int maskLen)
- {
- // 1. If maskLen > 2^32 hLen, output "mask too long" and stop.
- // easy - this is impossible by using a int (31bits) as parameter ;-)
- // BUT with a signed int we do have to check for negative values!
- if (maskLen < 0)
- throw new OverflowException();
-
- int mgfSeedLength = mgfSeed.Length;
- int hLen = (hash.HashSize >> 3); // from bits to bytes
- int iterations = (maskLen / hLen);
- if (maskLen % hLen != 0)
- iterations++;
- // 2. Let T be the empty octet string.
- byte[] T = new byte [iterations * hLen];
-
- byte[] toBeHashed = new byte [mgfSeedLength + 4];
- int pos = 0;
- // 3. For counter from 0 to \ceil (maskLen / hLen) - 1, do the following:
- for (int counter = 0; counter < iterations; counter++) {
- // a. Convert counter to an octet string C of length 4 octets
- byte[] C = I2OSP (counter, 4);
-
- // b. Concatenate the hash of the seed mgfSeed and C to the octet string T:
- // T = T || Hash (mgfSeed || C)
- Buffer.BlockCopy (mgfSeed, 0, toBeHashed, 0, mgfSeedLength);
- Buffer.BlockCopy (C, 0, toBeHashed, mgfSeedLength, 4);
- byte[] output = hash.ComputeHash (toBeHashed);
- Buffer.BlockCopy (output, 0, T, pos, hLen);
- pos += hLen;
- }
-
- // 4. Output the leading maskLen octets of T as the octet string mask.
- byte[] mask = new byte [maskLen];
- Buffer.BlockCopy (T, 0, mask, 0, maskLen);
- return mask;
- }
-#endif
-