1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.crypto.paddings;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.SecureRandom;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.crypto.InvalidCipherTextException;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * A padder that adds PKCS7/PKCS5 padding to a block.
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class PKCS7Padding
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements BlockCipherPadding
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Initialise the padder.
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param random - a SecureRandom if available.
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public void init(SecureRandom random)
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IllegalArgumentException
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // nothing to do.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the name of the algorithm the padder implements.
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the name of the algorithm the padder implements.
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getPaddingName()
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return "PKCS7";
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * add the pad bytes to the passed in block, returning the
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * number of bytes added.
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int addPadding(
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]  in,
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int     inOff)
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte code = (byte)(in.length - inOff);
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        while (inOff < in.length)
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            in[inOff] = code;
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            inOff++;
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return code;
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return the number of pad bytes present in the block.
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int padCount(byte[] in)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws InvalidCipherTextException
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int count = in[in.length - 1] & 0xff;
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
61c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (count > in.length || count == 0)
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new InvalidCipherTextException("pad block corrupted");
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 1; i <= count; i++)
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            if (in[in.length - i] != count)
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                throw new InvalidCipherTextException("pad block corrupted");
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return count;
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
77