1 /*
2 * Copyright (c) 2002, 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
139 }
140 }
141 public static final class AES256_GCM_NoPadding extends OidImpl {
142 public AES256_GCM_NoPadding() {
143 super(32, "GCM", "NOPADDING");
144 }
145 }
146
147 // utility method used by AESCipher and AESWrapCipher
148 static final void checkKeySize(Key key, int fixedKeySize)
149 throws InvalidKeyException {
150 if (fixedKeySize != -1) {
151 if (key == null) {
152 throw new InvalidKeyException("The key must not be null");
153 }
154 byte[] value = key.getEncoded();
155 if (value == null) {
156 throw new InvalidKeyException("Key encoding must not be null");
157 } else if (value.length != fixedKeySize) {
158 throw new InvalidKeyException("The key must be " +
159 fixedKeySize*8 + " bits");
160 }
161 }
162 }
163
164 /*
165 * internal CipherCore object which does the real work.
166 */
167 private CipherCore core = null;
168
169 /*
170 * needed to support AES oids which associates a fixed key size
171 * to the cipher object.
172 */
173 private final int fixedKeySize; // in bytes, -1 if no restriction
174
175 /*
176 * needed to enforce ISE thrown when updateAAD is called after update for GCM mode.
177 */
178 private boolean updateCalled;
179
492 outputOffset);
493 updateCalled = false;
494 return outLen;
495 }
496
497 /**
498 * Returns the key size of the given key object.
499 *
500 * @param key the key object.
501 *
502 * @return the key size of the given key object.
503 *
504 * @exception InvalidKeyException if <code>key</code> is invalid.
505 */
506 protected int engineGetKeySize(Key key) throws InvalidKeyException {
507 byte[] encoded = key.getEncoded();
508 if (!AESCrypt.isKeySizeValid(encoded.length)) {
509 throw new InvalidKeyException("Invalid AES key length: " +
510 encoded.length + " bytes");
511 }
512 return encoded.length * 8;
513 }
514
515 /**
516 * Wrap a key.
517 *
518 * @param key the key to be wrapped.
519 *
520 * @return the wrapped key.
521 *
522 * @exception IllegalBlockSizeException if this cipher is a block
523 * cipher, no padding has been requested, and the length of the
524 * encoding of the key to be wrapped is not a
525 * multiple of the block size.
526 *
527 * @exception InvalidKeyException if it is impossible or unsafe to
528 * wrap the key with this cipher (e.g., a hardware protected key is
529 * being passed to a software only cipher).
530 */
531 protected byte[] engineWrap(Key key)
532 throws IllegalBlockSizeException, InvalidKeyException {
611 *
612 * @param src the buffer containing the AAD
613 *
614 * @throws IllegalStateException if this cipher is in a wrong state
615 * (e.g., has not been initialized), does not accept AAD, or if
616 * operating in either GCM or CCM mode and one of the {@code update}
617 * methods has already been called for the active
618 * encryption/decryption operation
619 * @throws UnsupportedOperationException if this method
620 * has not been overridden by an implementation
621 *
622 * @since 1.8
623 */
624 @Override
625 protected void engineUpdateAAD(ByteBuffer src) {
626 if (core.getMode() == CipherCore.GCM_MODE && updateCalled) {
627 throw new IllegalStateException("AAD must be supplied before encryption/decryption starts");
628 }
629 if (src != null) {
630 int aadLen = src.limit() - src.position();
631 if (aadLen != 0) {
632 if (src.hasArray()) {
633 int aadOfs = src.arrayOffset() + src.position();
634 core.updateAAD(src.array(), aadOfs, aadLen);
635 src.position(src.limit());
636 } else {
637 byte[] aad = new byte[aadLen];
638 src.get(aad);
639 core.updateAAD(aad, 0, aadLen);
640 }
641 }
642 }
643 }
644 }
645
|
1 /*
2 * Copyright (c) 2002, 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
139 }
140 }
141 public static final class AES256_GCM_NoPadding extends OidImpl {
142 public AES256_GCM_NoPadding() {
143 super(32, "GCM", "NOPADDING");
144 }
145 }
146
147 // utility method used by AESCipher and AESWrapCipher
148 static final void checkKeySize(Key key, int fixedKeySize)
149 throws InvalidKeyException {
150 if (fixedKeySize != -1) {
151 if (key == null) {
152 throw new InvalidKeyException("The key must not be null");
153 }
154 byte[] value = key.getEncoded();
155 if (value == null) {
156 throw new InvalidKeyException("Key encoding must not be null");
157 } else if (value.length != fixedKeySize) {
158 throw new InvalidKeyException("The key must be " +
159 fixedKeySize + " bytes");
160 }
161 }
162 }
163
164 /*
165 * internal CipherCore object which does the real work.
166 */
167 private CipherCore core = null;
168
169 /*
170 * needed to support AES oids which associates a fixed key size
171 * to the cipher object.
172 */
173 private final int fixedKeySize; // in bytes, -1 if no restriction
174
175 /*
176 * needed to enforce ISE thrown when updateAAD is called after update for GCM mode.
177 */
178 private boolean updateCalled;
179
492 outputOffset);
493 updateCalled = false;
494 return outLen;
495 }
496
497 /**
498 * Returns the key size of the given key object.
499 *
500 * @param key the key object.
501 *
502 * @return the key size of the given key object.
503 *
504 * @exception InvalidKeyException if <code>key</code> is invalid.
505 */
506 protected int engineGetKeySize(Key key) throws InvalidKeyException {
507 byte[] encoded = key.getEncoded();
508 if (!AESCrypt.isKeySizeValid(encoded.length)) {
509 throw new InvalidKeyException("Invalid AES key length: " +
510 encoded.length + " bytes");
511 }
512 return Math.multiplyExact(encoded.length, 8);
513 }
514
515 /**
516 * Wrap a key.
517 *
518 * @param key the key to be wrapped.
519 *
520 * @return the wrapped key.
521 *
522 * @exception IllegalBlockSizeException if this cipher is a block
523 * cipher, no padding has been requested, and the length of the
524 * encoding of the key to be wrapped is not a
525 * multiple of the block size.
526 *
527 * @exception InvalidKeyException if it is impossible or unsafe to
528 * wrap the key with this cipher (e.g., a hardware protected key is
529 * being passed to a software only cipher).
530 */
531 protected byte[] engineWrap(Key key)
532 throws IllegalBlockSizeException, InvalidKeyException {
611 *
612 * @param src the buffer containing the AAD
613 *
614 * @throws IllegalStateException if this cipher is in a wrong state
615 * (e.g., has not been initialized), does not accept AAD, or if
616 * operating in either GCM or CCM mode and one of the {@code update}
617 * methods has already been called for the active
618 * encryption/decryption operation
619 * @throws UnsupportedOperationException if this method
620 * has not been overridden by an implementation
621 *
622 * @since 1.8
623 */
624 @Override
625 protected void engineUpdateAAD(ByteBuffer src) {
626 if (core.getMode() == CipherCore.GCM_MODE && updateCalled) {
627 throw new IllegalStateException("AAD must be supplied before encryption/decryption starts");
628 }
629 if (src != null) {
630 int aadLen = src.limit() - src.position();
631 if (aadLen > 0) {
632 if (src.hasArray()) {
633 int aadOfs = Math.addExact(src.arrayOffset(), src.position());
634 core.updateAAD(src.array(), aadOfs, aadLen);
635 src.position(src.limit());
636 } else {
637 byte[] aad = new byte[aadLen];
638 src.get(aad);
639 core.updateAAD(aad, 0, aadLen);
640 }
641 }
642 }
643 }
644 }
645
|