Path: blob/master/test/jdk/javax/crypto/spec/DESKeySpec/CheckWeakKeys.java
41152 views
/*1* Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 634091926* @summary Incorrect list of keys reported as weak by DESKeySpec.isWeak()27* @author Brad R. Wetmore28*/29import javax.crypto.spec.DESKeySpec;3031public class CheckWeakKeys {3233/**34* Weak/semi-weak keys copied from FIPS 74.35*36* "...The first 6 keys have duals different than themselves, hence37* each is both a key and a dual giving 12 keys with duals. The last38* four keys equal their duals, and are called self-dual keys..."39*40* 1. E001E001F101F101 01E001E001F101F141* 2. FE1FFE1FFEOEFEOE 1FFE1FFEOEFEOEFE42* 3. E01FE01FF10EF10E 1FE01FEOOEF10EF143* 4. 01FE01FE01FE01FE FE01FE01FE01FE0144* 5. 011F011F010E010E 1F011F010E010E0145* 6. E0FEE0FEF1FEF1FE FEE0FEE0FEF1FEF146* 7. 0101010101010101 010101010101010147* 8. FEFEFEFEFEFEFEFE FEFEFEFEFEFEFEFE48* 9. E0E0E0E0F1F1F1F1 E0E0E0E0F1F1F1F149* 10. 1F1F1F1F0E0E0E0E 1F1F1F1F0E0E0E0E50*/51static private byte [][] weakKeys = {52{ (byte)0xE0, (byte)0x01, (byte)0xE0, (byte)0x01,53(byte)0xF1, (byte)0x01, (byte)0xF1, (byte)0x01 },5455{ (byte)0x01, (byte)0xE0, (byte)0x01, (byte)0xE0,56(byte)0x01, (byte)0xF1, (byte)0x01, (byte)0xF1 },5758{ (byte)0xFE, (byte)0x1F, (byte)0xFE, (byte)0x1F,59(byte)0xFE, (byte)0x0E, (byte)0xFE, (byte)0x0E },6061{ (byte)0x1F, (byte)0xFE, (byte)0x1F, (byte)0xFE,62(byte)0x0E, (byte)0xFE, (byte)0x0E, (byte)0xFE },6364{ (byte)0xE0, (byte)0x1F, (byte)0xE0, (byte)0x1F,65(byte)0xF1, (byte)0x0E, (byte)0xF1, (byte)0x0E },6667{ (byte)0x1F, (byte)0xE0, (byte)0x1F, (byte)0xE0,68(byte)0x0E, (byte)0xF1, (byte)0x0E, (byte)0xF1 },6970{ (byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE,71(byte)0x01, (byte)0xFE, (byte)0x01, (byte)0xFE },7273{ (byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01,74(byte)0xFE, (byte)0x01, (byte)0xFE, (byte)0x01 },7576{ (byte)0x01, (byte)0x1F, (byte)0x01, (byte)0x1F,77(byte)0x01, (byte)0x0E, (byte)0x01, (byte)0x0E },7879{ (byte)0x1F, (byte)0x01, (byte)0x1F, (byte)0x01,80(byte)0x0E, (byte)0x01, (byte)0x0E, (byte)0x01 },8182{ (byte)0xE0, (byte)0xFE, (byte)0xE0, (byte)0xFE,83(byte)0xF1, (byte)0xFE, (byte)0xF1, (byte)0xFE },8485{ (byte)0xFE, (byte)0xE0, (byte)0xFE, (byte)0xE0,86(byte)0xFE, (byte)0xF1, (byte)0xFE, (byte)0xF1 },8788{ (byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01,89(byte)0x01, (byte)0x01, (byte)0x01, (byte)0x01 },9091{ (byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE,92(byte)0xFE, (byte)0xFE, (byte)0xFE, (byte)0xFE },9394{ (byte)0xE0, (byte)0xE0, (byte)0xE0, (byte)0xE0,95(byte)0xF1, (byte)0xF1, (byte)0xF1, (byte)0xF1 },9697{ (byte)0x1F, (byte)0x1F, (byte)0x1F, (byte)0x1F,98(byte)0x0E, (byte)0x0E, (byte)0x0E, (byte)0x0E }99};100101public static void main(String[] args) throws Exception {102103boolean failed = false;104105for (int i = 0; i < weakKeys.length; i++) {106DESKeySpec desSpec = new DESKeySpec(weakKeys[i]);107if (!DESKeySpec.isWeak(weakKeys[i], 0)) {108failed = true;109System.out.println("Entry " + i + " should be weak");110}111}112113if (failed) {114throw new Exception("Failed test!!!");115}116117System.out.println("Passed test.");118}119}120121122