< prev index next >

src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/NativeCipherWithJavaPadding.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 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


 143                 trailingBytes.put(dataFromUpdate,
 144                                   dataFromUpdate.length - newTBSize, newTBSize);
 145             } else {
 146                 trailingBytes.put(dataFromUpdate);
 147             }
 148             return result;
 149         }
 150 
 151         public int getBufferedLength() {
 152             if (trailingBytes != null) {
 153                 return trailingBytes.position();
 154             }
 155             return 0;
 156         }
 157 
 158         public int unpad(byte[] lastData, byte[] out, int outOfs)
 159                 throws BadPaddingException, IllegalBlockSizeException,
 160                 ShortBufferException {
 161             int tbSize = (trailingBytes == null? 0:trailingBytes.position());
 162             int dataLen = tbSize + lastData.length;
 163             // check total length
 164             if ((dataLen < 1) || (dataLen % blockSize != 0)) {



 165                 UcryptoProvider.debug("PKCS5Padding: unpad, buffered " + tbSize +
 166                                  " bytes, last block " + lastData.length + " bytes");
 167 
 168                 throw new IllegalBlockSizeException
 169                     ("Input length must be multiples of " + blockSize);
 170             }
 171 
 172             // check padding bytes
 173             if (lastData.length == 0) {
 174                 if (tbSize != 0) {
 175                     // work on 'trailingBytes' directly
 176                     lastData = Arrays.copyOf(trailingBytes.array(), tbSize);
 177                     trailingBytes.clear();
 178                     tbSize = 0;
 179                 } else {
 180                     throw new BadPaddingException("No pad bytes found!");
 181                 }
 182             }
 183             byte padValue = lastData[lastData.length - 1];
 184             if (padValue < 1 || padValue > blockSize) {


 385             int actualOut = this.engineDoFinal(in, inOfs, inLen, out, 0);
 386             // truncate off extra bytes
 387             if (actualOut != out.length) {
 388                 out = Arrays.copyOf(out, actualOut);
 389             }
 390         } catch (ShortBufferException sbe) {
 391             throw new UcryptoException("Internal Error", sbe);
 392         } finally {
 393             reset();
 394         }
 395         return out;
 396     }
 397 
 398     // see JCE spec
 399     @Override
 400     protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
 401                                              int outOfs)
 402         throws ShortBufferException, IllegalBlockSizeException,
 403                BadPaddingException {
 404         int estimatedOutLen = engineGetOutputSize(inLen);
 405 
 406         if (out.length - outOfs < estimatedOutLen) {
 407             throw new ShortBufferException("Actual: " + (out.length - outOfs) +
 408                 ". Estimated Out Length: " + estimatedOutLen);
 409         }
 410         try {
 411             if (nc.encrypt) {
 412                 int k = nc.engineUpdate(in, inOfs, inLen, out, outOfs);
 413                 lastBlockLen += inLen;
 414                 lastBlockLen &= (blockSize - 1);
 415                 byte[] padBytes = padding.getPaddingBytes(lastBlockLen);
 416                 k += nc.engineDoFinal(padBytes, 0, padBytes.length, out, (outOfs + k));
 417                 return k;
 418             } else {
 419                 byte[] tempOut = nc.engineDoFinal(in, inOfs, inLen);
 420                 int len = padding.unpad(tempOut, out, outOfs);
 421                 return len;
 422             }
 423         } finally {
 424             reset();
 425         }


   1 /*
   2  * Copyright (c) 2014, 2016, 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


 143                 trailingBytes.put(dataFromUpdate,
 144                                   dataFromUpdate.length - newTBSize, newTBSize);
 145             } else {
 146                 trailingBytes.put(dataFromUpdate);
 147             }
 148             return result;
 149         }
 150 
 151         public int getBufferedLength() {
 152             if (trailingBytes != null) {
 153                 return trailingBytes.position();
 154             }
 155             return 0;
 156         }
 157 
 158         public int unpad(byte[] lastData, byte[] out, int outOfs)
 159                 throws BadPaddingException, IllegalBlockSizeException,
 160                 ShortBufferException {
 161             int tbSize = (trailingBytes == null? 0:trailingBytes.position());
 162             int dataLen = tbSize + lastData.length;
 163 
 164             // Special handling to match SunJCE provider behavior
 165             if (dataLen <= 0) {
 166                 return 0;
 167             } else if (dataLen % blockSize != 0) {
 168                 UcryptoProvider.debug("PKCS5Padding: unpad, buffered " + tbSize +
 169                                  " bytes, last block " + lastData.length + " bytes");
 170 
 171                 throw new IllegalBlockSizeException
 172                     ("Input length must be multiples of " + blockSize);
 173             }
 174 
 175             // check padding bytes
 176             if (lastData.length == 0) {
 177                 if (tbSize != 0) {
 178                     // work on 'trailingBytes' directly
 179                     lastData = Arrays.copyOf(trailingBytes.array(), tbSize);
 180                     trailingBytes.clear();
 181                     tbSize = 0;
 182                 } else {
 183                     throw new BadPaddingException("No pad bytes found!");
 184                 }
 185             }
 186             byte padValue = lastData[lastData.length - 1];
 187             if (padValue < 1 || padValue > blockSize) {


 388             int actualOut = this.engineDoFinal(in, inOfs, inLen, out, 0);
 389             // truncate off extra bytes
 390             if (actualOut != out.length) {
 391                 out = Arrays.copyOf(out, actualOut);
 392             }
 393         } catch (ShortBufferException sbe) {
 394             throw new UcryptoException("Internal Error", sbe);
 395         } finally {
 396             reset();
 397         }
 398         return out;
 399     }
 400 
 401     // see JCE spec
 402     @Override
 403     protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
 404                                              int outOfs)
 405         throws ShortBufferException, IllegalBlockSizeException,
 406                BadPaddingException {
 407         int estimatedOutLen = engineGetOutputSize(inLen);

 408         if (out.length - outOfs < estimatedOutLen) {
 409             throw new ShortBufferException("Actual: " + (out.length - outOfs) +
 410                 ". Estimated Out Length: " + estimatedOutLen);
 411         }
 412         try {
 413             if (nc.encrypt) {
 414                 int k = nc.engineUpdate(in, inOfs, inLen, out, outOfs);
 415                 lastBlockLen += inLen;
 416                 lastBlockLen &= (blockSize - 1);
 417                 byte[] padBytes = padding.getPaddingBytes(lastBlockLen);
 418                 k += nc.engineDoFinal(padBytes, 0, padBytes.length, out, (outOfs + k));
 419                 return k;
 420             } else {
 421                 byte[] tempOut = nc.engineDoFinal(in, inOfs, inLen);
 422                 int len = padding.unpad(tempOut, out, outOfs);
 423                 return len;
 424             }
 425         } finally {
 426             reset();
 427         }


< prev index next >