< prev index next >

src/java.base/share/classes/com/sun/crypto/provider/CounterMode.java

Print this page




 156     }
 157 
 158     /**
 159      * Increment the counter value.
 160      */
 161     private static void increment(byte[] b) {
 162         int n = b.length - 1;
 163         while ((n >= 0) && (++b[n] == 0)) {
 164             n--;
 165         }
 166     }
 167 
 168     /**
 169      * Do the actual encryption/decryption operation.
 170      * Essentially we XOR the input plaintext/ciphertext stream with a
 171      * keystream generated by encrypting the counter values. Counter values
 172      * are encrypted on demand.
 173      */
 174     private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 175 
 176         cryptBlockCheck(in, inOff, len);
 177         cryptBlockCheck(out, outOff, len);
 178         return implCrypt(in, inOff, len, out, outOff);
 179     }
 180 
 181     // Implementation of crpyt() method. Possibly replaced with a compiler intrinsic.
 182     @HotSpotIntrinsicCandidate
 183     private int implCrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 184         int result = len;
 185         while (len-- > 0) {
 186             if (used >= blockSize) {
 187                 embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);
 188                 increment(counter);
 189                 used = 0;
 190             }
 191             out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);
 192         }
 193         return result;
 194     }
 195 
 196     // Used to perform all checks required by the Java semantics
 197     // (i.e., null checks and bounds checks) on the input parameters to crypt().
 198     // Normally, the Java Runtime performs these checks, however, as crypt() is
 199     // possibly replaced with compiler intrinsic, the JDK performs the
 200     // required checks instead.
 201     // Does not check accesses to class-internal (private) arrays.
 202     private static void cryptBlockCheck(byte[] array, int offset, int len) {
 203         Objects.requireNonNull(array);
 204 
 205         if (offset < 0 || len < 0 || offset >= array.length) {
 206             throw new ArrayIndexOutOfBoundsException(offset);
 207         }
 208 
 209         int largestIndex = offset + len - 1;
 210         if (largestIndex < 0 || largestIndex >= array.length) {
 211             throw new ArrayIndexOutOfBoundsException(largestIndex);
 212         }
 213     }
 214 }


 156     }
 157 
 158     /**
 159      * Increment the counter value.
 160      */
 161     private static void increment(byte[] b) {
 162         int n = b.length - 1;
 163         while ((n >= 0) && (++b[n] == 0)) {
 164             n--;
 165         }
 166     }
 167 
 168     /**
 169      * Do the actual encryption/decryption operation.
 170      * Essentially we XOR the input plaintext/ciphertext stream with a
 171      * keystream generated by encrypting the counter values. Counter values
 172      * are encrypted on demand.
 173      */
 174     private int crypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 175 
 176       Objects.checkFromIndexSize(inOff, len, in.length);
 177       Objects.checkFromIndexSize(outOff, len, out.length);
 178       return implCrypt(in, inOff, len, out, outOff);
 179     }
 180 
 181     // Implementation of crpyt() method. Possibly replaced with a compiler intrinsic.
 182     @HotSpotIntrinsicCandidate
 183     private int implCrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
 184         int result = len;
 185         while (len-- > 0) {
 186             if (used >= blockSize) {
 187                 embeddedCipher.encryptBlock(counter, 0, encryptedCounter, 0);
 188                 increment(counter);
 189                 used = 0;
 190             }
 191             out[outOff++] = (byte)(in[inOff++] ^ encryptedCounter[used++]);
 192         }
 193         return result;
 194     }
 195 


















 196 }
< prev index next >