Skip to content

Commit

Permalink
6205692: (spec) javax.crypto.MacSpi.engineUpdate(ByteBuffer input): N…
Browse files Browse the repository at this point in the history
…PE should be specified

Reviewed-by: valeriep
  • Loading branch information
driverkt authored and Bradford Wetmore committed Aug 30, 2022
1 parent 622be79 commit 6335150
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 15 deletions.
25 changes: 13 additions & 12 deletions src/java.base/share/classes/javax/crypto/Mac.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private String getProviderName() {
*
* @param key the key.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
*/
public final void init(Key key) throws InvalidKeyException {
Expand Down Expand Up @@ -451,9 +451,9 @@ public final void init(Key key) throws InvalidKeyException {
* @param key the key.
* @param params the algorithm parameters.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
* @exception InvalidAlgorithmParameterException if the given algorithm
* @throws InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this MAC.
*/
public final void init(Key key, AlgorithmParameterSpec params)
Expand All @@ -476,7 +476,7 @@ public final void init(Key key, AlgorithmParameterSpec params)
*
* @param input the input byte to be processed.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte input) throws IllegalStateException {
Expand All @@ -492,7 +492,7 @@ public final void update(byte input) throws IllegalStateException {
*
* @param input the array of bytes to be processed.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte[] input) throws IllegalStateException {
Expand All @@ -513,7 +513,7 @@ public final void update(byte[] input) throws IllegalStateException {
* @param offset the offset in {@code input} where the input starts.
* @param len the number of bytes to process.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void update(byte[] input, int offset, int len)
Expand All @@ -538,8 +538,9 @@ public final void update(byte[] input, int offset, int len)
*
* @param input the ByteBuffer
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
* @throws IllegalArgumentException if {@code input} is null
* @since 1.5
*/
public final void update(ByteBuffer input) {
Expand Down Expand Up @@ -569,7 +570,7 @@ public final void update(ByteBuffer input) {
*
* @return the MAC result.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final byte[] doFinal() throws IllegalStateException {
Expand Down Expand Up @@ -603,9 +604,9 @@ public final byte[] doFinal() throws IllegalStateException {
* @param outOffset the offset in {@code output} where the MAC is
* stored
*
* @exception ShortBufferException if the given output buffer is too small
* @throws ShortBufferException if the given output buffer is too small
* to hold the result
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final void doFinal(byte[] output, int outOffset)
Expand Down Expand Up @@ -641,7 +642,7 @@ public final void doFinal(byte[] output, int outOffset)
* @param input data in bytes
* @return the MAC result.
*
* @exception IllegalStateException if this {@code Mac} has not been
* @throws IllegalStateException if this {@code Mac} has not been
* initialized.
*/
public final byte[] doFinal(byte[] input) throws IllegalStateException
Expand Down Expand Up @@ -678,7 +679,7 @@ public final void reset() {
*
* @return a clone if the provider implementation is cloneable.
*
* @exception CloneNotSupportedException if this is called on a
* @throws CloneNotSupportedException if this is called on a
* delegate that does not support {@code Cloneable}.
*/
public final Object clone() throws CloneNotSupportedException {
Expand Down
9 changes: 6 additions & 3 deletions src/java.base/share/classes/javax/crypto/MacSpi.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public MacSpi() {}
* @param key the (secret) key.
* @param params the algorithm parameters.
*
* @exception InvalidKeyException if the given key is inappropriate for
* @throws InvalidKeyException if the given key is inappropriate for
* initializing this MAC.
* @exception InvalidAlgorithmParameterException if the given algorithm
* @throws InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this MAC.
*/
protected abstract void engineInit(Key key,
Expand Down Expand Up @@ -101,6 +101,9 @@ protected abstract void engineInit(Key key,
* process ByteBuffers more efficiently than byte arrays.
*
* @param input the ByteBuffer
*
* @throws NullPointerException if {@code input} is null
*
* @since 1.5
*/
protected void engineUpdate(ByteBuffer input) {
Expand Down Expand Up @@ -145,7 +148,7 @@ protected void engineUpdate(ByteBuffer input) {
*
* @return a clone if the implementation is cloneable.
*
* @exception CloneNotSupportedException if this is called
* @throws CloneNotSupportedException if this is called
* on an implementation that does not support {@code Cloneable}.
*/
public Object clone() throws CloneNotSupportedException {
Expand Down
98 changes: 98 additions & 0 deletions test/jdk/com/sun/crypto/provider/Mac/Test6205692.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @library /test/lib
* @bug 6205692
* @summary verify MacSpi NPE on engineUpdate(ByteBuffer)
*/

import jdk.test.lib.Utils;

import javax.crypto.MacSpi;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

public class Test6205692 {

public boolean execute() throws Exception {

ByteBuffer byteBuffer = null;

MyMacSpi myMacSpi = new MyMacSpi();

Utils.runAndCheckException(() -> myMacSpi.engineUpdate(byteBuffer),
NullPointerException.class);

return true;
}

public static void main(String[] args) throws Exception {
Test6205692 test = new Test6205692();

if (test.execute()) {
System.out.println(test.getClass().getName() + ": passed!");
}
}

private static class MyMacSpi extends MacSpi {

/*
* This is the important part; the rest is blank mandatory overrides
*/
public void engineUpdate(ByteBuffer input) {
super.engineUpdate(input);
}

@Override
protected int engineGetMacLength() {
return 0;
}

@Override
protected void engineInit(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException {
}

@Override
protected void engineUpdate(byte input) {
}

@Override
protected void engineUpdate(byte[] input, int offset, int len) {
}

@Override
protected byte[] engineDoFinal() {
return new byte[0];
}

@Override
protected void engineReset() {
}
}
}

1 comment on commit 6335150

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.