PageRenderTime 114ms CodeModel.GetById 71ms app.highlight 10ms RepoModel.GetById 29ms app.codeStats 0ms

/java-1.7.0-openjdk/mauve-2008-10-22/gnu/testlet/gnu/javax/crypto/cipher/TestOfBlowfish.java

#
Java | 148 lines | 106 code | 11 blank | 31 comment | 3 complexity | d7d4e84a0a23af646c526e67ef62f963 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1/* TestOfBlowfish.java -- 
  2   Copyright (C) 2006 Free Software Foundation, Inc.
  3This file is part of Mauve.
  4
  5Mauve is free software; you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation; either version 2, or (at your option)
  8any later version.
  9
 10Mauve is distributed in the hope that it will be useful, but
 11WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with Mauve; see the file COPYING.  If not, write to the
 17Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 1802110-1301 USA.
 19
 20*/
 21
 22// Tags: GNU-CRYPTO JDK1.4
 23// Uses: BaseCipherTestCase
 24
 25package gnu.testlet.gnu.javax.crypto.cipher;
 26
 27import gnu.java.security.util.Util;
 28import gnu.javax.crypto.cipher.Blowfish;
 29import gnu.javax.crypto.cipher.IBlockCipher;
 30import gnu.testlet.TestHarness;
 31import java.util.Arrays;
 32import java.util.HashMap;
 33
 34/**
 35 * Conformance test for the Blowfish cipher.
 36 */
 37public class TestOfBlowfish extends BaseCipherTestCase
 38{
 39  /**
 40   * Eric Young's Blowfish test vectors. See <a
 41   * href="http://www.counterpane.com/vectors.txt">http://www.counterpane.com/vectors.txt</a>.
 42   */
 43  static final String[][] TV = {
 44      // key bytes           clear bytes         cipher bytes
 45      { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
 46      { "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A" },
 47      { "3000000000000000", "1000000000000001", "7D856F9A613063F2" },
 48      { "1111111111111111", "1111111111111111", "2466DD878B963C9D" },
 49      { "0123456789ABCDEF", "1111111111111111", "61F9C3802281B096" },
 50      { "1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7" },
 51      { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
 52      { "FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D" },
 53      { "7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B" },
 54      { "0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0" },
 55      { "07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4" },
 56      { "3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB" },
 57      { "04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A" },
 58      { "0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918" },
 59      { "0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98" },
 60      { "43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5" },
 61      { "07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79" },
 62      { "04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3" },
 63      { "37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969" },
 64      { "1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B" },
 65      { "584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E" },
 66      { "025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD" },
 67      { "49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019" },
 68      { "4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3" },
 69      { "49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5" },
 70      { "018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778" },
 71      { "1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201" },
 72      { "0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2" },
 73      { "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE" },
 74      { "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D" },
 75      { "0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4" },
 76      { "FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC" },
 77      { "0123456789ABCDEF", "0000000000000000", "245946885754369A" },
 78      { "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A" } };
 79
 80  public void test(TestHarness harness)
 81  {
 82    harness.checkPoint("TestOfBlowfish");
 83    cipher = new Blowfish();
 84    HashMap attrib = new HashMap();
 85    attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(8));
 86    attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
 87    try
 88      {
 89        harness.check(validityTest(), "validityTest()");
 90        harness.check(cloneabilityTest(), "cloneabilityTest()");
 91        harness.check(vectorsTest(), "vectorsTest()");
 92      }
 93    catch (Exception x)
 94      {
 95        harness.debug(x);
 96        harness.fail("TestOfBlowfish");
 97      }
 98  }
 99
100  /** Test cloneability. */
101  protected boolean cloneabilityTest() throws Exception
102  {
103    int blockSize = cipher.defaultBlockSize();
104    int keySize = cipher.defaultKeySize();
105
106    byte[] pt = new byte[blockSize];
107    byte[] ct1 = new byte[blockSize];
108    byte[] ct2 = new byte[blockSize];
109    byte[] kb = new byte[keySize];
110    HashMap attributes = new HashMap();
111    attributes.put(IBlockCipher.KEY_MATERIAL, kb);
112
113    cipher.reset();
114    cipher.init(attributes);
115
116    cipher.encryptBlock(pt, 0, pt, 0);
117    IBlockCipher thomas = (IBlockCipher) cipher.clone();
118    thomas.init(attributes);
119    cipher.encryptBlock(pt, 0, ct1, 0);
120    thomas.encryptBlock(pt, 0, ct2, 0);
121
122    return Arrays.equals(ct1, ct2);
123  }
124
125  protected boolean vectorsTest() throws Exception
126  {
127    HashMap attrib = new HashMap();
128    byte[] kb, pt, ct1, ct2 = new byte[8], cpt = new byte[8];
129    for (int i = 0; i < TV.length; i++)
130      {
131        kb = Util.toBytesFromString(TV[i][0]);
132        pt = Util.toBytesFromString(TV[i][1]);
133        ct1 = Util.toBytesFromString(TV[i][2]);
134        attrib.put(IBlockCipher.KEY_MATERIAL, kb);
135        cipher.reset();
136        cipher.init(attrib);
137        cipher.encryptBlock(pt, 0, ct2, 0);
138        if (!Arrays.equals(ct1, ct2))
139          {
140            return false;
141          }
142        cipher.decryptBlock(ct2, 0, cpt, 0);
143        if (!Arrays.equals(pt, cpt))
144            return false;
145      }
146    return true;
147  }
148}