1 /* 2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.crypto.provider; 27 28 import javax.crypto.ShortBufferException; 29 30 /** 31 * This class implements padding as specified in the W3 XML ENC standard. 32 * 33 * @author Valerie Peng 34 * 35 * 36 * @see Padding 37 */ 38 final class ISO10126Padding implements Padding { 39 40 private int blockSize; 41 42 ISO10126Padding(int blockSize) { 43 this.blockSize = blockSize; 44 } 45 46 /** 47 * Adds the given number of padding bytes to the data input. 48 * The value of the padding bytes is determined 49 * by the specific padding mechanism that implements this 50 * interface. 51 * 52 * @param in the input buffer with the data to pad 53 * @param off the offset in <code>in</code> where the padding bytes 54 * are appended 55 * @param len the number of padding bytes to add 56 * 57 * @exception ShortBufferException if <code>in</code> is too small to hold 58 * the padding bytes 59 */ 60 public void padWithLen(byte[] in, int off, int len) 61 throws ShortBufferException 62 { 63 if (in == null) 64 return; 65 66 int idx = Math.addExact(off, len); 67 if (idx > in.length) { 68 throw new ShortBufferException("Buffer too small to hold padding"); 69 } 70 71 byte paddingOctet = (byte) (len & 0xff); 72 byte[] padding = new byte[len - 1]; 73 SunJCE.getRandom().nextBytes(padding); 74 System.arraycopy(padding, 0, in, off, len - 1); 75 in[idx - 1] = paddingOctet; 76 return; 77 } 78 79 /** 80 * Returns the index where the padding starts. 81 * 82 * <p>Given a buffer with padded data, this method returns the 83 * index where the padding starts. 84 * 85 * @param in the buffer with the padded data 86 * @param off the offset in <code>in</code> where the padded data starts 87 * @param len the length of the padded data 88 * 89 * @return the index where the padding starts, or -1 if the input is 90 * not properly padded 91 */ 92 public int unpad(byte[] in, int off, int len) { 93 if ((in == null) || 94 (len == 0)) { // this can happen if input is really a padded buffer 95 return 0; 96 } 97 98 int idx = Math.addExact(off, len); 99 byte lastByte = in[idx - 1]; 100 int padValue = (int)lastByte & 0x0ff; 101 if ((padValue < 0x01) 102 || (padValue > blockSize)) { 103 return -1; 104 } 105 106 int start = idx - padValue; 107 if (start < off) { 108 return -1; 109 } 110 111 return start; 112 } 113 114 /** 115 * Determines how long the padding will be for a given input length. 116 * 117 * @param len the length of the data to pad 118 * 119 * @return the length of the padding 120 */ 121 public int padLength(int len) { 122 int paddingOctet = blockSize - (len % blockSize); 123 return paddingOctet; 124 } 125 }