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 * Block cipher padders are expected to conform to this interface
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic interface BlockCipherPadding
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Initialise the padder.
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param random the source of randomness for the padding, if required.
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public void init(SecureRandom random)
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IllegalArgumentException;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the name of the algorithm the cipher implements.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the name of the algorithm the cipher implements.
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getPaddingName();
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * add the pad bytes to the passed in block, returning the
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * number of bytes added.
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <p>
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Note: this assumes that the last block of plain text is always
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * passed to it inside in. i.e. if inOff is zero, indicating the
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * entire block is to be overwritten with padding the value of in
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * should be the same as the last block of plain text. The reason
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * for this is that some modes such as "trailing bit compliment"
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * base the padding on the last byte of plain text.
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </p>
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int addPadding(byte[] in, int inOff);
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return the number of pad bytes present in the block.
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception InvalidCipherTextException if the padding is badly formed
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * or invalid.
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int padCount(byte[] in)
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws InvalidCipherTextException;
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
49