~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/jce/provider/test/MacTest.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.jce.provider.test;
 
2
 
 
3
import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
4
import org.bouncycastle.util.encoders.Hex;
 
5
import org.bouncycastle.util.test.SimpleTest;
 
6
 
 
7
import javax.crypto.Mac;
 
8
import javax.crypto.SecretKey;
 
9
import javax.crypto.spec.IvParameterSpec;
 
10
import javax.crypto.spec.SecretKeySpec;
 
11
import java.security.Security;
 
12
 
 
13
/**
 
14
 * MAC tester - vectors from 
 
15
 * <a href=http://www.itl.nist.gov/fipspubs/fip81.htm>FIP 81</a> and 
 
16
 * <a href=http://www.itl.nist.gov/fipspubs/fip113.htm>FIP 113</a>.
 
17
 */
 
18
public class MacTest
 
19
    extends SimpleTest
 
20
{
 
21
    static byte[]   keyBytes = Hex.decode("0123456789abcdef");
 
22
    static byte[]   ivBytes = Hex.decode("1234567890abcdef");
 
23
 
 
24
    static byte[]   input = Hex.decode("37363534333231204e6f77206973207468652074696d6520666f7220");
 
25
 
 
26
    static byte[]   output1 = Hex.decode("f1d30f68");
 
27
    static byte[]   output2 = Hex.decode("58d2e77e");
 
28
    static byte[]   output3 = Hex.decode("cd647403");
 
29
 
 
30
    static byte[]   keyBytesISO9797 = Hex.decode("7CA110454A1A6E570131D9619DC1376E");
 
31
    
 
32
    static byte[]   inputISO9797 = "Hello World !!!!".getBytes(); 
 
33
    
 
34
    static byte[]   outputISO9797 = Hex.decode("F09B856213BAB83B");
 
35
    
 
36
    static byte[]   inputDesEDE64 = "Hello World !!!!".getBytes(); 
 
37
    
 
38
    static byte[]   outputDesEDE64 = Hex.decode("862304d33af01096");
 
39
    
 
40
    public MacTest()
 
41
    {
 
42
    }
 
43
 
 
44
    private void aliasTest(SecretKey key, String primary, String[] aliases)
 
45
        throws Exception
 
46
    {
 
47
        Mac mac = Mac.getInstance(primary, "BC");
 
48
 
 
49
        //
 
50
        // standard DAC - zero IV
 
51
        //
 
52
        mac.init(key);
 
53
 
 
54
        mac.update(input, 0, input.length);
 
55
 
 
56
        byte[] ref = mac.doFinal();
 
57
 
 
58
        for (int i = 0; i != aliases.length; i++)
 
59
        {
 
60
            mac = Mac.getInstance(aliases[i], "BC");
 
61
 
 
62
            mac.init(key);
 
63
 
 
64
            mac.update(input, 0, input.length);
 
65
 
 
66
            byte[] out = mac.doFinal();
 
67
            if (!areEqual(out, ref))
 
68
            {
 
69
                fail("Failed - expected " + new String(Hex.encode(ref)) + " got " + new String(Hex.encode(out)));
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
    public void performTest()
 
75
        throws Exception
 
76
    {
 
77
        SecretKey           key = new SecretKeySpec(keyBytes, "DES");
 
78
        byte[]              out;
 
79
        Mac                 mac;
 
80
 
 
81
        mac = Mac.getInstance("DESMac", "BC");
 
82
 
 
83
        //
 
84
        // standard DAC - zero IV
 
85
        //
 
86
        mac.init(key);
 
87
 
 
88
        mac.update(input, 0, input.length);
 
89
 
 
90
        out = mac.doFinal();
 
91
 
 
92
        if (!areEqual(out, output1))
 
93
        {
 
94
            fail("Failed - expected " + new String(Hex.encode(output1)) + " got " + new String(Hex.encode(out)));
 
95
        }
 
96
        
 
97
        //
 
98
        // mac with IV.
 
99
        //
 
100
        mac.init(key, new IvParameterSpec(ivBytes));
 
101
 
 
102
        mac.update(input, 0, input.length);
 
103
 
 
104
        out = mac.doFinal();
 
105
 
 
106
        if (!areEqual(out, output2))
 
107
        {
 
108
            fail("Failed - expected " + new String(Hex.encode(output2)) + " got " + new String(Hex.encode(out)));
 
109
        }
 
110
        
 
111
        //
 
112
        // CFB mac with IV - 8 bit CFB mode
 
113
        //
 
114
        mac = Mac.getInstance("DESMac/CFB8", "BC");
 
115
 
 
116
        mac.init(key, new IvParameterSpec(ivBytes));
 
117
 
 
118
        mac.update(input, 0, input.length);
 
119
 
 
120
        out = mac.doFinal();
 
121
 
 
122
        if (!areEqual(out, output3))
 
123
        {
 
124
            fail("Failed - expected " + new String(Hex.encode(output3)) + " got " + new String(Hex.encode(out)));
 
125
        }
 
126
        
 
127
        //
 
128
        // ISO9797 algorithm 3 using DESEDE
 
129
        //
 
130
        key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
 
131
        
 
132
        mac = Mac.getInstance("ISO9797ALG3", "BC");
 
133
 
 
134
        mac.init(key);
 
135
 
 
136
        mac.update(inputISO9797, 0, inputISO9797.length);
 
137
 
 
138
        out = mac.doFinal();
 
139
 
 
140
        if (!areEqual(out, outputISO9797))
 
141
        {
 
142
            fail("Failed - expected " + new String(Hex.encode(outputISO9797)) + " got " + new String(Hex.encode(out)));
 
143
        }
 
144
        
 
145
        //
 
146
        // 64bit DESede Mac
 
147
        //
 
148
        key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
 
149
        
 
150
        mac = Mac.getInstance("DESEDE64", "BC");
 
151
 
 
152
        mac.init(key);
 
153
 
 
154
        mac.update(inputDesEDE64, 0, inputDesEDE64.length);
 
155
 
 
156
        out = mac.doFinal();
 
157
 
 
158
        if (!areEqual(out, outputDesEDE64))
 
159
        {
 
160
            fail("Failed - expected " + new String(Hex.encode(outputDesEDE64)) + " got " + new String(Hex.encode(out)));
 
161
        }
 
162
 
 
163
        aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"), "DESedeMac64withISO7816-4Padding",
 
164
            new String[] { "DESEDE64WITHISO7816-4PADDING", "DESEDEISO9797ALG1MACWITHISO7816-4PADDING", "DESEDEISO9797ALG1WITHISO7816-4PADDING" });
 
165
 
 
166
        aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"), "ISO9797ALG3WITHISO7816-4PADDING",
 
167
            new String[] { "ISO9797ALG3MACWITHISO7816-4PADDING" });
 
168
    }
 
169
 
 
170
    public String getName()
 
171
    {
 
172
        return "Mac";
 
173
    }
 
174
 
 
175
    public static void main(
 
176
        String[]    args)
 
177
    {
 
178
        Security.addProvider(new BouncyCastleProvider());
 
179
 
 
180
        runTest(new MacTest());
 
181
    }
 
182
}