src/java.base/share/classes/com/sun/crypto/provider/OutputFeedback.java
changeset 51052 080776992b29
parent 47216 71c04702a3d5
equal deleted inserted replaced
51051:4e98b465d706 51052:080776992b29
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    25 
    25 
    26 package com.sun.crypto.provider;
    26 package com.sun.crypto.provider;
    27 
    27 
    28 import java.security.InvalidKeyException;
    28 import java.security.InvalidKeyException;
    29 import java.security.ProviderException;
    29 import java.security.ProviderException;
       
    30 import sun.security.util.ArrayUtil;
    30 
    31 
    31 /**
    32 /**
    32  * This class represents ciphers in output-feedback (OFB) mode.
    33  * This class represents ciphers in output-feedback (OFB) mode.
    33  *
    34  *
    34  * <p>This mode is implemented independently of a particular cipher.
    35  * <p>This mode is implemented independently of a particular cipher.
   146      * a multiple of the <code>numBytes</code>
   147      * a multiple of the <code>numBytes</code>
   147      * @return the length of the encrypted data
   148      * @return the length of the encrypted data
   148      */
   149      */
   149     int encrypt(byte[] plain, int plainOffset, int plainLen,
   150     int encrypt(byte[] plain, int plainOffset, int plainLen,
   150                 byte[] cipher, int cipherOffset) {
   151                 byte[] cipher, int cipherOffset) {
   151 
   152         ArrayUtil.blockSizeCheck(plainLen, numBytes);
   152         if ((plainLen % numBytes) != 0) {
   153         ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
   153             throw new ProviderException("Internal error in input buffering");
   154         ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
   154         }
   155 
   155         int nShift = blockSize - numBytes;
   156         int nShift = blockSize - numBytes;
   156         int loopCount = plainLen / numBytes;
   157         int loopCount = plainLen / numBytes;
   157 
   158 
   158         for (; loopCount > 0;
   159         for (; loopCount > 0;
   159              plainOffset += numBytes, cipherOffset += numBytes,
   160              plainOffset += numBytes, cipherOffset += numBytes,
   187      * @param cipherOffset the offset in <code>cipher</code>
   188      * @param cipherOffset the offset in <code>cipher</code>
   188      * @return the length of the encrypted data
   189      * @return the length of the encrypted data
   189      */
   190      */
   190     int encryptFinal(byte[] plain, int plainOffset, int plainLen,
   191     int encryptFinal(byte[] plain, int plainOffset, int plainLen,
   191                      byte[] cipher, int cipherOffset) {
   192                      byte[] cipher, int cipherOffset) {
       
   193         ArrayUtil.nullAndBoundsCheck(plain, plainOffset, plainLen);
       
   194         ArrayUtil.nullAndBoundsCheck(cipher, cipherOffset, plainLen);
       
   195 
   192         int oddBytes = plainLen % numBytes;
   196         int oddBytes = plainLen % numBytes;
   193         int len = encrypt(plain, plainOffset, (plainLen - oddBytes),
   197         int len = encrypt(plain, plainOffset, (plainLen - oddBytes),
   194                           cipher, cipherOffset);
   198                           cipher, cipherOffset);
   195         plainOffset += len;
   199         plainOffset += len;
   196         cipherOffset += len;
   200         cipherOffset += len;