gnu-crypto-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GNU Crypto] Odd behavior from PKCS7


From: Raif S. Naffah
Subject: Re: [GNU Crypto] Odd behavior from PKCS7
Date: Wed, 26 Nov 2003 20:02:30 +1100
User-agent: KMail/1.5

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

hello Ryan,

On Wed, 26 Nov 2003 06:05 am, Ryan Senior wrote:
> ...
> I'm using GNU-Crypto 1.1.0 ...
>
> My problem comes about when I attempt to pad remaining bytes in a
> block.  For whatever reason, PKCS7 is returning a byte array of
> length 252 instead of 5...
>
> Attached is the class that's having problems.  It's specifically the
> encryptStream() class.
>
> The output it gives is:
>
> Current Block Size 8
> Bytes available 3
> leftOver length is 3
> paddedTemp length is 252
> inData length is 8
> java.lang.ArrayIndexOutOfBoundsException
>         at java.lang.System.arraycopy(Native Method)
>         at GCISS_BE.encryptStream(GCISS_BE.java:99)
>
> Everything looked normal until the paddedTemp length is 252.
>
> Also, another question I have is that when I don't call pad.reset(),
> I get a java.lang.IllegalStateException however the pad has NOT been
> initialized yet.  I didn't think calling pad.reset() would hurt
> anything, so I went ahead and did that and it fixed my problem,
> however I was still wondering why I would get that if I just
> instantiated the PKCS7 class...

the problem was (and it has been fixed long time ago in the CVS tree) 
that we were not resetting the algorithm after running the selfTest().

basically whenever you getInstance() of an algorithm from its Factory 
class (incl. padding algorithms) we run the selfTest() method on that 
instance before we return it to you.  so, the following line in your 
class:

   IPad pad = PadFactory.getInstance("PKCS7");

is internally invoking the selfTest() on the pkcs7 algorithm instance 
you will get.  because we were forgetting to reset() the instance, it 
was returned (a) initialised with specific parameters, and (b) in the 
wrong state.  if you build the library from CVS you dont need any more 
this line (the reset() invocation):

   // pad.reset();
   pad.init(mode.currentBlockSize());

also because we already do the selfTest() you neither need the following 
line (invocation of selfTest()):

   // pad.selfTest();
   System.out.println("Current Block Size "+mode.currentBlockSize());

the figure of 252 you are getting is the result of the stale 
initialisation of the padding algorithm instance (from its builtin 
selfTest()).


last point;  you are doing --replace pt with paddedTemp:

   System.arraycopy(pt, 0, inData, leftOver.length, inData.length);

instead you should do:

   System.arraycopy(pt, 0, inData,leftOver.length, pt.length);

this is because paddedTemp is all you need to append to the left over 
input;  but you would have found that if you got past the resetting 
problem.


so to summarise:

* get and build a copy from the CVS and comment out, in your class, the 
reset() and selfTest() invocations as mentioned above, or

* keep using the 1.1 release but (a) leave the reset() before the 
initialisation, and (b) comment out the selfTest().



cheers;
rsn
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Que du magnifique

iD8DBQE/xGwu+e1AKnsTRiERAyGhAKDVFlQm3TenZUQcBcrrDgZq7r8WnwCfXhe0
lRQP9aljQ6CkIp6QRHIKIQc=
=lLfp
-----END PGP SIGNATURE-----





reply via email to

[Prev in Thread] Current Thread [Next in Thread]