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

SlotTwoTest.java « test « provider « jce « bouncycastle « org « java « test « src « prov - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7a2a880eb457780fc8a1397a9dc70e77e7183aad (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
package org.bouncycastle.jce.provider.test;

import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.test.SimpleTest;

public class SlotTwoTest 
    extends SimpleTest
{
    byte[] plainData = "abcdefghijklmnopqrstuvwxyz".getBytes();

    public String getName()
    {
        return "SlotTwo";
    }

    public void performTest() 
        throws Exception
    {
        Security.removeProvider("BC");
        Security.insertProviderAt(new BouncyCastleProvider(), 2);

        KeyGenerator keyGen = KeyGenerator.getInstance("DESede", "BC");
        
        keyGen.init(new SecureRandom());

        Key key = keyGen.generateKey();

        testDesEde(key, "ECB", "PKCS7Padding");
        testDesEde(key, "CBC", "PKCS7Padding");
        testDesEde(key, "CTR", "NoPadding");
        testDesEde(key, "CTR", "PKCS7Padding");
        testDesEde(key, "OFB", "PKCS7Padding");
        testDesEde(key, "CFB", "PKCS7Padding");
        
        Security.removeProvider("BC");
        Security.addProvider(new BouncyCastleProvider());
    }

    private void testDesEde(
        Key key, 
        String mode, 
        String padding) 
        throws Exception
    {
        Cipher encrypt = Cipher.getInstance("DESede/" + mode + "/" + padding, "BC");
        Cipher decrypt = Cipher.getInstance("DESede/" + mode + "/" + padding);
        
        if (!decrypt.getProvider().getName().equals("BC"))
        {
            fail("BC provider not returned for DESede/" + mode + "/" + padding + " got " + decrypt.getProvider().getName());
        }

        encrypt.init(Cipher.ENCRYPT_MODE, key);

        byte[] encryptedBytes = encrypt.doFinal(plainData);
        byte[] ivBytes = encrypt.getIV();
        
        if (ivBytes != null)
        {
            IvParameterSpec ivp = new IvParameterSpec(ivBytes);
    
            decrypt.init(Cipher.DECRYPT_MODE, key, ivp);
        }
        else
        {
            decrypt.init(Cipher.DECRYPT_MODE, key);
        }

        byte[] plainBytes = decrypt.doFinal(encryptedBytes, 0, encryptedBytes.length);
        
        if (!areEqual(plainData, plainBytes))
        {
            fail("decryption test failed.");
        }
    }
    
    public static void main(
        String[]    args)
    {
        runTest(new SlotTwoTest());
    }
}