[Mono-dev] crypto API
Lauren Del Giudice
lauren.bedoule at gmail.com
Sun Jan 7 05:01:38 EST 2007
Please find the attached unit test. It consists of 2 encryptions followed by
two decryptions (CBC, default padding). I couldn't check the result under
Mono since I didn't compile the latests release, so I let you open the bug
if any.
Lauren.
2007/1/6, Sebastien Pouliot <sebastien.pouliot at gmail.com>:
>
> Hello Lauren,
>
> On Sat, 2007-01-06 at 11:57 +0100, Lauren Del Giudice wrote:
> > Sebastien,
> >
> > Concerning the closed bug 80439, rev70491;
> > By reducing the input count, you will loose the last block for next
> > decryption. You first decryption will succeed, but the next one will
> > fail.
>
> Yes...
>
> > Test it and let me know if it succeeds:
>
> but unit tests (added in r70493) shows that, under MS, the next
> decryption fails.
>
> > Let say you decrypt 56 bytes (CBC, default padding), you will obtain
> > 48 bytes. Iterate and obtain again 48 bytes. The last 48 bytes will
> > depend on the buffered block from the first decryption.
>
> Please provide a (working on MS runtime / failing under Mono) test case,
> mine simply fails in this case, and attach it to a new bug report.
>
> > When you decrypt from 56 bytes and obtain 48 bytes, there is no
> > critical issue (well don't consider memory as a critical issue for a
> > moment), since you don't really throw the last decrypted block but
> > rather you have to buffer it.
> > This buffered decrypted block will be injected at the next decryption
> > process, in the output array as the first block.
> >
> > And also, watch out with overlapping input/output...
>
> That was an issue long ago (before SymmetricalTransform) but now data is
> copied. Again if you have a specific issue please provide a test case.
>
> > HTH.
> > Lauren.
> >
> >
> > _______________________________________________
> > Mono-devel-list mailing list
> > Mono-devel-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070107/d4f0ddb4/attachment.html
-------------- next part --------------
using System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace ConsoleApplication1
{
[TestFixture]
public class Program
{
/// <summary>
/// CBC, default padding.
/// c c d d
/// [48] ---> [48] ---> [48] ---> [40] ---> [40]
/// </summary>
[Test]
public void Bug80439_1()
{
DES des = DES.Create();
des.IV = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
des.Key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
byte[] input = new byte[48] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F};
byte[] result1 = new byte[48];
byte[] result2 = new byte[48];
// encrypts
ICryptoTransform encryptor = des.CreateEncryptor();
int len = encryptor.TransformBlock(input, 0, input.Length, result1, 0);
Assert.IsTrue(len == 48, "1st encryption len = " + len);
len = encryptor.TransformBlock(result1, 0, result1.Length, result2, 0);
Assert.IsTrue(len == 48, "2nd encryption len = " + len);
// decrypts
des.IV = new byte[] { 0x0F, 0x93, 0x8D, 0xED, 0xE3, 0x37, 0xA3, 0x09 };
byte[] result3 = new byte[40];
ICryptoTransform decryptor = des.CreateDecryptor();
byte[] result4 = new byte[40];
len = decryptor.TransformBlock(result2, 0, result2.Length, result3, 0);
Assert.IsTrue(len == 40, "1st decryption len = " + len);
for (int i = 0; i < 40; i++)
{
Assert.IsTrue(result3[i] == result1[i], "1st decryption: wrong output data starting at " + i);
}
len = decryptor.TransformBlock(result3, 0, len, result4, 0);
Assert.IsTrue(len == 40, "2nd decryption len = " + len);
for (int i = 0; i < 8; i++)
{
Assert.IsTrue(result4[i] == result1[40 + i], "2nd decryption: wrong output data starting at " + i);
}
for (int i = 8; i < 16; i++)
{
Assert.IsTrue(result4[i] == result2[i - 8 + 40], "2nd decryption: wrong output data starting at " + i);
}
for (int i = 16; i < 40; i++)
{
Assert.IsTrue(result4[i] == input[i - 16 + 8], "2nd decryption: wrong output data starting at " + i);
}
}
}
}
More information about the Mono-devel-list
mailing list