-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: When setup the HPKE context you either set it up for encryption or decryption. We should change our interfaces to reflect this and so guard the user from miss-usage. Modifications: Introduce sub-types to support either encryption or decryption Result: Guard users from incorrect setup and usage of context
- Loading branch information
1 parent
6aa8909
commit 6dd4040
Showing
15 changed files
with
204 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
codec-ohttp-hpke/src/main/java/io/netty/incubator/codec/hpke/CryptoDecryptContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.incubator.codec.hpke; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* {@link CryptoContext} that can be used for decryption. | ||
*/ | ||
public interface CryptoDecryptContext extends CryptoContext { | ||
|
||
/** | ||
* Authenticate and decrypt data. The {@link ByteBuf#readerIndex()} will be increased by the amount of | ||
* data read and {@link ByteBuf#writerIndex()} by the bytes written. | ||
* | ||
* @param aad the AAD buffer | ||
* @param ct the data to decrypt | ||
* @param out the buffer for writing into. | ||
* @throws CryptoException in case of an error. | ||
*/ | ||
void open(ByteBuf aad, ByteBuf ct, ByteBuf out) throws CryptoException; | ||
} |
35 changes: 35 additions & 0 deletions
35
codec-ohttp-hpke/src/main/java/io/netty/incubator/codec/hpke/CryptoEncryptContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.incubator.codec.hpke; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* {@link CryptoContext} that can be used for encryption. | ||
*/ | ||
public interface CryptoEncryptContext extends CryptoContext { | ||
|
||
/** | ||
* Authenticate and encrypt data. The {@link ByteBuf#readerIndex()} will be increased by the amount of | ||
* data read and {@link ByteBuf#writerIndex()} by the bytes written. | ||
* | ||
* @param aad the AAD buffer | ||
* @param pt the data to encrypt. | ||
* @param out the buffer for writing into | ||
* @throws CryptoException in case of an error. | ||
*/ | ||
void seal(ByteBuf aad, ByteBuf pt, ByteBuf out) throws CryptoException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ain/java/io/netty/incubator/codec/hpke/bouncycastle/BouncyCastleHPKERecipientContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.incubator.codec.hpke.bouncycastle; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.incubator.codec.hpke.CryptoException; | ||
import io.netty.incubator.codec.hpke.HPKERecipientContext; | ||
import org.bouncycastle.crypto.InvalidCipherTextException; | ||
import org.bouncycastle.crypto.hpke.HPKEContext; | ||
|
||
final class BouncyCastleHPKERecipientContext extends BouncyCastleHPKEContext implements HPKERecipientContext { | ||
private final BouncyCastleCryptoOperation open; | ||
|
||
BouncyCastleHPKERecipientContext(HPKEContext context) { | ||
super(context); | ||
open = new BouncyCastleCryptoOperation() { | ||
@Override | ||
protected byte[] execute(byte[] arg1, byte[] arg2, int offset2, int length2) throws InvalidCipherTextException { | ||
return context.open(arg1, arg2, offset2, length2); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void open(ByteBuf aad, ByteBuf ct, ByteBuf out) throws CryptoException { | ||
checkClosed(); | ||
open.execute(aad, ct, out); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...c/main/java/io/netty/incubator/codec/hpke/bouncycastle/BouncyCastleHPKESenderContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2023 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.netty.incubator.codec.hpke.bouncycastle; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.incubator.codec.hpke.CryptoException; | ||
import io.netty.incubator.codec.hpke.HPKESenderContext; | ||
import org.bouncycastle.crypto.InvalidCipherTextException; | ||
import org.bouncycastle.crypto.hpke.HPKEContextWithEncapsulation; | ||
|
||
final class BouncyCastleHPKESenderContext extends BouncyCastleHPKEContext implements HPKESenderContext { | ||
|
||
private final BouncyCastleCryptoOperation seal; | ||
public BouncyCastleHPKESenderContext(HPKEContextWithEncapsulation context) { | ||
super(context); | ||
this.seal = new BouncyCastleCryptoOperation() { | ||
@Override | ||
protected byte[] execute(byte[] arg1, byte[] arg2, int offset2, int length2) | ||
throws InvalidCipherTextException { | ||
return context.seal(arg1, arg2, offset2, length2); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public byte[] encapsulation() { | ||
return ((HPKEContextWithEncapsulation) context).getEncapsulation(); | ||
} | ||
|
||
@Override | ||
public void seal(ByteBuf aad, ByteBuf pt, ByteBuf out) throws CryptoException { | ||
checkClosed(); | ||
seal.execute(aad, pt, out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.