0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport javax.crypto.ShortBufferException;
0N/A
0N/A/**
0N/A * This class implements padding as specified in the PKCS#5 standard.
0N/A *
0N/A * @author Gigi Ankeny
0N/A *
0N/A *
0N/A * @see Padding
0N/A */
0N/Afinal class PKCS5Padding implements Padding {
0N/A
0N/A private int blockSize;
0N/A
0N/A PKCS5Padding(int blockSize) {
0N/A this.blockSize = blockSize;
0N/A }
0N/A
0N/A /**
0N/A * Adds the given number of padding bytes to the data input.
0N/A * The value of the padding bytes is determined
0N/A * by the specific padding mechanism that implements this
0N/A * interface.
0N/A *
0N/A * @param in the input buffer with the data to pad
0N/A * @param off the offset in <code>in</code> where the padding bytes
0N/A * are appended
0N/A * @param len the number of padding bytes to add
0N/A *
0N/A * @exception ShortBufferException if <code>in</code> is too small to hold
0N/A * the padding bytes
0N/A */
0N/A public void padWithLen(byte[] in, int off, int len)
0N/A throws ShortBufferException
0N/A {
0N/A if (in == null)
0N/A return;
0N/A
0N/A if ((off + len) > in.length) {
0N/A throw new ShortBufferException("Buffer too small to hold padding");
0N/A }
0N/A
0N/A byte paddingOctet = (byte) (len & 0xff);
0N/A for (int i = 0; i < len; i++) {
0N/A in[i + off] = paddingOctet;
0N/A }
0N/A return;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index where the padding starts.
0N/A *
0N/A * <p>Given a buffer with padded data, this method returns the
0N/A * index where the padding starts.
0N/A *
0N/A * @param in the buffer with the padded data
0N/A * @param off the offset in <code>in</code> where the padded data starts
0N/A * @param len the length of the padded data
0N/A *
0N/A * @return the index where the padding starts, or -1 if the input is
0N/A * not properly padded
0N/A */
0N/A public int unpad(byte[] in, int off, int len) {
0N/A if ((in == null) ||
0N/A (len == 0)) { // this can happen if input is really a padded buffer
0N/A return 0;
0N/A }
0N/A
0N/A byte lastByte = in[off + len - 1];
0N/A int padValue = (int)lastByte & 0x0ff;
0N/A if ((padValue < 0x01)
0N/A || (padValue > blockSize)) {
0N/A return -1;
0N/A }
0N/A
0N/A int start = off + len - ((int)lastByte & 0x0ff);
0N/A if (start < off) {
0N/A return -1;
0N/A }
0N/A
0N/A for (int i = 0; i < ((int)lastByte & 0x0ff); i++) {
0N/A if (in[start+i] != lastByte) {
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A return start;
0N/A }
0N/A
0N/A /**
0N/A * Determines how long the padding will be for a given input length.
0N/A *
0N/A * @param len the length of the data to pad
0N/A *
0N/A * @return the length of the padding
0N/A */
0N/A public int padLength(int len) {
0N/A int paddingOctet = blockSize - (len % blockSize);
0N/A return paddingOctet;
0N/A }
0N/A}