PageRenderTime 31ms CodeModel.GetById 24ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gnu-crypto/source/gnu/testlet/gnu/crypto/cipher/TestOfBlowfish.java

https://github.com/clibrepo/04f6ea02286af71632b72d73554979a61da424cf681886674b21ecd595715c12
Java | 180 lines | 99 code | 17 blank | 64 comment | 3 complexity | d79088d887593f3c0b355ebbc8f7b9e0 MD5 | raw file
  1package gnu.testlet.gnu.crypto.cipher;
  2
  3// ----------------------------------------------------------------------------
  4// $Id: TestOfBlowfish.java,v 1.3 2003/09/27 00:04:20 raif Exp $
  5//
  6// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  7//
  8// This file is part of GNU Crypto.
  9//
 10// GNU Crypto is free software; you can redistribute it and/or modify
 11// it under the terms of the GNU General Public License as published by
 12// the Free Software Foundation; either version 2, or (at your option)
 13// any later version.
 14//
 15// GNU Crypto is distributed in the hope that it will be useful, but
 16// WITHOUT ANY WARRANTY; without even the implied warranty of
 17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18// General Public License for more details.
 19//
 20// You should have received a copy of the GNU General Public License
 21// along with this program; see the file COPYING.  If not, write to the
 22//
 23//    Free Software Foundation Inc.,
 24//    59 Temple Place - Suite 330,
 25//    Boston, MA 02111-1307
 26//    USA
 27//
 28// Linking this library statically or dynamically with other modules is
 29// making a combined work based on this library.  Thus, the terms and
 30// conditions of the GNU General Public License cover the whole
 31// combination.
 32//
 33// As a special exception, the copyright holders of this library give
 34// you permission to link this library with independent modules to
 35// produce an executable, regardless of the license terms of these
 36// independent modules, and to copy and distribute the resulting
 37// executable under terms of your choice, provided that you also meet,
 38// for each linked independent module, the terms and conditions of the
 39// license of that module.  An independent module is a module which is
 40// not derived from or based on this library.  If you modify this
 41// library, you may extend this exception to your version of the
 42// library, but you are not obligated to do so.  If you do not wish to
 43// do so, delete this exception statement from your version.
 44// ----------------------------------------------------------------------------
 45
 46// Tags: GNU-CRYPTO
 47// Uses: BaseCipherTestCase
 48
 49import gnu.crypto.cipher.Blowfish;
 50import gnu.crypto.cipher.IBlockCipher;
 51import gnu.crypto.util.Util;
 52import gnu.testlet.TestHarness;
 53import java.util.Arrays;
 54import java.util.HashMap;
 55
 56/**
 57 * <p>Conformance test for the Blowfish cipher.</p>
 58 *
 59 * @version $Revision: 1.3 $
 60 */
 61public class TestOfBlowfish extends BaseCipherTestCase {
 62
 63   // Constants and variables.
 64   // -----------------------------------------------------------------------
 65
 66   /**
 67    * Eric Young's Blowfish test vectors. See <a
 68    * href="http://www.counterpane.com/vectors.txt">http://www.counterpane.com/vectors.txt</a>.
 69    */
 70   static final String[][] TV = {
 71      // key bytes           clear bytes         cipher bytes
 72      { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
 73      { "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A" },
 74      { "3000000000000000", "1000000000000001", "7D856F9A613063F2" },
 75      { "1111111111111111", "1111111111111111", "2466DD878B963C9D" },
 76      { "0123456789ABCDEF", "1111111111111111", "61F9C3802281B096" },
 77      { "1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7" },
 78      { "0000000000000000", "0000000000000000", "4EF997456198DD78" },
 79      { "FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D" },
 80      { "7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B" },
 81      { "0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0" },
 82      { "07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4" },
 83      { "3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB" },
 84      { "04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A" },
 85      { "0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918" },
 86      { "0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98" },
 87      { "43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5" },
 88      { "07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79" },
 89      { "04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3" },
 90      { "37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969" },
 91      { "1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B" },
 92      { "584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E" },
 93      { "025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD" },
 94      { "49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019" },
 95      { "4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3" },
 96      { "49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5" },
 97      { "018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778" },
 98      { "1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201" },
 99      { "0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2" },
100      { "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE" },
101      { "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D" },
102      { "0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4" },
103      { "FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC" },
104      { "0123456789ABCDEF", "0000000000000000", "245946885754369A" },
105      { "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A" }
106   };
107
108   // Constructors.
109   // -----------------------------------------------------------------------
110
111   // default 0-arguments constructor
112
113   // Class methods.
114   // -----------------------------------------------------------------------
115
116   // Instance methods.
117   // -----------------------------------------------------------------------
118
119   public void test(TestHarness harness) {
120      harness.checkPoint("TestOfBlowfish");
121      cipher = new Blowfish();
122      HashMap attrib = new HashMap();
123      attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(8));
124      attrib.put(IBlockCipher.KEY_MATERIAL, new byte[16]);
125      try {
126         harness.check(validityTest(), "validityTest()");
127         harness.check(cloneabilityTest(), "cloneabilityTest()");
128         harness.check(vectorsTest(), "vectorsTest()");
129      } catch (Exception x) {
130         harness.debug(x);
131         harness.fail("TestOfBlowfish");
132      }
133   }
134
135   /** Test cloneability. */
136   protected boolean cloneabilityTest() throws Exception {
137      int blockSize = cipher.defaultBlockSize();
138      int keySize = cipher.defaultKeySize();
139
140      byte[] pt = new byte[blockSize];
141      byte[] ct1 = new byte[blockSize];
142      byte[] ct2 = new byte[blockSize];
143      byte[] kb = new byte[keySize];
144      HashMap attributes = new HashMap();
145      attributes.put(IBlockCipher.KEY_MATERIAL, kb);
146
147      cipher.reset();
148      cipher.init(attributes);
149
150      cipher.encryptBlock(pt, 0, pt, 0);
151      IBlockCipher thomas = (IBlockCipher) cipher.clone();
152      thomas.init(attributes);
153      cipher.encryptBlock(pt, 0, ct1, 0);
154      thomas.encryptBlock(pt, 0, ct2, 0);
155
156      return Arrays.equals(ct1, ct2);
157   }
158
159   protected boolean vectorsTest() throws Exception {
160      HashMap attrib = new HashMap();
161      byte[] kb, pt, ct1, ct2 = new byte[8], cpt = new byte[8];
162      for (int i = 0; i < TV.length; i++) {
163         kb = Util.toBytesFromString(TV[i][0]);
164         pt = Util.toBytesFromString(TV[i][1]);
165         ct1 = Util.toBytesFromString(TV[i][2]);
166         attrib.put(IBlockCipher.KEY_MATERIAL, kb);
167         cipher.reset();
168         cipher.init(attrib);
169         cipher.encryptBlock(pt, 0, ct2, 0);
170         if (!Arrays.equals(ct1, ct2)) {
171            return false;
172         }
173         cipher.decryptBlock(ct2, 0, cpt, 0);
174         if (!Arrays.equals(pt, cpt)) {
175            return false;
176         }
177      }
178      return true;
179   }
180}