1 /*
   2  * Copyright (c) 1997, 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 import java.util.Arrays;
  30 
  31 /**
  32  * This class implements padding as specified in the PKCS#5 standard.
  33  *
  34  * @author Gigi Ankeny
  35  *
  36  *
  37  * @see Padding
  38  */
  39 final class PKCS5Padding implements Padding {
  40 
  41     private int blockSize;
  42 
  43     PKCS5Padding(int blockSize) {
  44         this.blockSize = blockSize;
  45     }
  46 
  47     /**
  48      * Adds the given number of padding bytes to the data input.
  49      * The value of the padding bytes is determined
  50      * by the specific padding mechanism that implements this
  51      * interface.
  52      *
  53      * @param in the input buffer with the data to pad
  54      * @param off the offset in <code>in</code> where the padding bytes
  55      * are appended
  56      * @param len the number of padding bytes to add
  57      *
  58      * @exception ShortBufferException if <code>in</code> is too small to hold
  59      * the padding bytes
  60      */
  61     public void padWithLen(byte[] in, int off, int len)
  62         throws ShortBufferException
  63     {
  64         if (in == null)
  65             return;
  66 
  67         int idx = Math.addExact(off, len);
  68         if (idx > in.length) {
  69             throw new ShortBufferException("Buffer too small to hold padding");
  70         }
  71 
  72         byte paddingOctet = (byte) (len & 0xff);
  73         Arrays.fill(in, off, idx, paddingOctet);
  74         return;
  75     }
  76 
  77     /**
  78      * Returns the index where the padding starts.
  79      *
  80      * <p>Given a buffer with padded data, this method returns the
  81      * index where the padding starts.
  82      *
  83      * @param in the buffer with the padded data
  84      * @param off the offset in <code>in</code> where the padded data starts
  85      * @param len the length of the padded data
  86      *
  87      * @return the index where the padding starts, or -1 if the input is
  88      * not properly padded
  89      */
  90     public int unpad(byte[] in, int off, int len) {
  91         if ((in == null) ||
  92             (len == 0)) { // this can happen if input is really a padded buffer
  93             return 0;
  94         }
  95         int idx = Math.addExact(off, len);
  96         byte lastByte = in[idx - 1];
  97         int padValue = (int)lastByte & 0x0ff;
  98         if ((padValue < 0x01)
  99             || (padValue > blockSize)) {
 100             return -1;
 101         }
 102 
 103         int start = idx - padValue;
 104         if (start < off) {
 105             return -1;
 106         }
 107 
 108         for (int i = start; i < idx; i++) {
 109             if (in[i] != lastByte) {
 110                 return -1;
 111             }
 112         }
 113         return start;
 114     }
 115 
 116     /**
 117      * Determines how long the padding will be for a given input length.
 118      *
 119      * @param len the length of the data to pad
 120      *
 121      * @return the length of the padding
 122      */
 123     public int padLength(int len) {
 124         int paddingOctet = blockSize - (len % blockSize);
 125         return paddingOctet;
 126     }
 127 }