Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AsymmetricHybridCipherTest.java « test « provider « jcajce « pqc « bouncycastle « org « java « test « src « prov - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f66dc6bf62ee691fbe149ccc63aea4665ee7d74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.bouncycastle.pqc.jcajce.provider.test;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

/**
 * Base class for unit tests of {@link AsymmetricHybridCipher}s.
 */
public abstract class AsymmetricHybridCipherTest
    extends FlexiTest
{

    /**
     * the {@link KeyPairGenerator} to use for the test
     */
    protected KeyPairGenerator kpg;

    /**
     * the {@link AsymmetricHybridCipher} to use for the test
     */
    protected Cipher cipher;

    private KeyPair keyPair;

    private PublicKey pubKey;

    private PrivateKey privKey;

    private byte[] mBytes, cBytes, dBytes;

    protected final void performEnDecryptionTest(int numPassesKPG,
                                                 int numPassesEncDec, int plainTextSize,
                                                 AlgorithmParameterSpec params)
    {

        try
        {
            for (int j = 0; j < numPassesKPG; j++)
            {
                // generate key pair
                //kpg.initialize(params);
                keyPair = kpg.genKeyPair();
                pubKey = keyPair.getPublic();
                privKey = keyPair.getPrivate();

                for (int k = 1; k <= numPassesEncDec; k++)
                {
                    // initialize for encryption
                    cipher.init(Cipher.ENCRYPT_MODE, pubKey, params, sr);

                    // generate random message
                    int mLength = rand.nextInt(plainTextSize) + 1;
                    mBytes = new byte[mLength];
                    rand.nextBytes(mBytes);

                    // encrypt
                    cBytes = cipher.doFinal(mBytes);


                    // initialize for decryption
                    cipher.init(Cipher.DECRYPT_MODE, privKey, params);
                    // decrypt
                    dBytes = cipher.doFinal(cBytes);
                    // compare
                    assertEquals(
                        "Encryption/decryption test failed for message \""
                            + ByteUtils.toHexString(mBytes)
                            + "\":\n actual decrypted text: "
                            + ByteUtils.toHexString(dBytes)
                            + "\n expected plain text: "
                            + ByteUtils.toHexString(mBytes), mBytes,
                        dBytes);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            fail(e);
        }
    }

}