1 /**
2  * @license
3  * Copyright 2016 Google Inc. All rights reserved.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.security.wycheproof;
18 
19 import java.nio.ByteBuffer;
20 import java.security.Provider;
21 import java.security.Security;
22 
23 /** Test utilities */
24 public class TestUtil {
25 
bytesToHex(byte[] bytes)26   public static String bytesToHex(byte[] bytes) {
27     // bytesToHex is used to convert output from Cipher.
28     // cipher.update can return null, which is equivalent to returning
29     // no plaitext rsp. ciphertext.
30     if (bytes == null) {
31       return "";
32     }
33     String chars = "0123456789abcdef";
34     StringBuilder result = new StringBuilder(2 * bytes.length);
35     for (byte b : bytes) {
36       // convert to unsigned
37       int val = b & 0xff;
38       result.append(chars.charAt(val / 16));
39       result.append(chars.charAt(val % 16));
40     }
41     return result.toString();
42   }
43 
44   /**
45    * Returns a hexadecimal representation of the bytes written to ByteBuffer (i.e. all the bytes
46    * before position()).
47    */
byteBufferToHex(ByteBuffer buffer)48   public static String byteBufferToHex(ByteBuffer buffer) {
49     ByteBuffer tmp = buffer.duplicate();
50     tmp.flip();
51     byte[] bytes = new byte[tmp.remaining()];
52     tmp.get(bytes);
53     return bytesToHex(bytes);
54   }
55 
hexToBytes(String hex)56   public static byte[] hexToBytes(String hex) throws IllegalArgumentException {
57     if (hex.length() % 2 != 0) {
58       throw new IllegalArgumentException("Expected a string of even length");
59     }
60     int size = hex.length() / 2;
61     byte[] result = new byte[size];
62     for (int i = 0; i < size; i++) {
63       int hi = Character.digit(hex.charAt(2 * i), 16);
64       int lo = Character.digit(hex.charAt(2 * i + 1), 16);
65       if ((hi == -1) || (lo == -1)) {
66         throw new IllegalArgumentException("input is not hexadecimal");
67       }
68       result[i] = (byte) (16 * hi + lo);
69     }
70     return result;
71   }
72 
installOnlyThisProvider(Provider provider)73   public static void installOnlyThisProvider(Provider provider) {
74     for (Provider p : Security.getProviders()) {
75       Security.removeProvider(p.getName());
76     }
77     Security.insertProviderAt(provider, 1);
78   }
79 
installOnlyOpenJDKProviders()80   public static void installOnlyOpenJDKProviders() throws Exception {
81     for (Provider p : Security.getProviders()) {
82       Security.removeProvider(p.getName());
83     }
84     installOpenJDKProvider("com.sun.net.ssl.internal.ssl.Provider");
85     installOpenJDKProvider("com.sun.crypto.provider.SunJCE");
86     installOpenJDKProvider("com.sun.security.sasl.Provider");
87     installOpenJDKProvider("org.jcp.xml.dsig.internal.dom.XMLDSigRI");
88     installOpenJDKProvider("sun.security.ec.SunEC");
89     installOpenJDKProvider("sun.security.jgss.SunProvider");
90     installOpenJDKProvider("sun.security.provider.Sun");
91     installOpenJDKProvider("sun.security.rsa.SunRsaSign");
92     installOpenJDKProvider("sun.security.smartcardio.SunPCSC");
93   }
94 
installOpenJDKProvider(String className)95   private static void installOpenJDKProvider(String className) throws Exception {
96     Provider provider = (Provider) Class.forName(className).getConstructor().newInstance();
97     Security.insertProviderAt(provider, 1);
98   }
99 }
100