Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto rewrite #36

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,29 @@
*
*/

package net.daporkchop.lib.crypto.cipher.stream;
package net.daporkchop.lib.crypto;

import lombok.AllArgsConstructor;
import io.netty.buffer.ByteBuf;
import lombok.NonNull;
import org.bouncycastle.crypto.StreamCipher;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.locks.Lock;
import net.daporkchop.lib.unsafe.util.exception.AlreadyReleasedException;

/**
* Representation of a block cipher.
* <p>
* Block ciphers are symmetric ciphers which process data in fixed-size blocks. Plaintext and ciphertext are always
* exactly the same length, and data is only accepted in a size multiple of the block size.
*
* @author DaPorkchop_
*/
@AllArgsConstructor
public class StreamCipherOutput extends OutputStream {
@NonNull
private final Lock writeLock;

@NonNull
private final StreamCipher cipher;

@NonNull
private final OutputStream stream;

public interface BlockCipher extends SymmetricCipher {
@Override
public void write(int b) throws IOException {
this.stream.write(this.cipher.returnByte((byte) b) & 0xFF);
}
void process(@NonNull ByteBuf src, @NonNull ByteBuf dst);

@Override
public void flush() throws IOException {
this.stream.flush();
}
/**
* @return the size of the blocks used by this cipher, in bytes
*/
int blockSize();

@Override
public void close() throws IOException {
this.flush();
this.writeLock.unlock();
}
BlockCipher retain() throws AlreadyReleasedException;
}
70 changes: 70 additions & 0 deletions crypto/src/main/java/net/daporkchop/lib/crypto/Cipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Adapted from The MIT License (MIT)
*
* Copyright (c) 2018-2020 DaPorkchop_
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* Any persons and/or organizations using this software must include the above copyright notice and this permission notice,
* provide sufficient credit to the original authors of the project (IE: DaPorkchop_), as well as provide a link to the original project.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

package net.daporkchop.lib.crypto;

import io.netty.buffer.ByteBuf;
import lombok.NonNull;
import net.daporkchop.lib.common.misc.refcount.RefCounted;
import net.daporkchop.lib.crypto.key.Key;
import net.daporkchop.lib.unsafe.util.exception.AlreadyReleasedException;

/**
* Base representation of a cipher.
* <p>
* A cipher consumes plain-/ciphertext and en-/decrypts it.
* <p>
* Ciphers are stateful and not thread-safe. They are initialized in encryption or decryption mode with a key, and
* may buffer unprocessed data internally.
*
* @author DaPorkchop_
*/
public interface Cipher extends RefCounted {
/**
* Resets this cipher to its initial state, erasing any stored keys/buffers.
* <p>
* A cipher may be reset at any time. Once reset, a cipher cannot be used again until initialized.
*/
void reset();

void init(boolean encrypt, @NonNull Key key);

/**
* Reads data from the source buffer, processes it, and writes it to the destination buffer.
* <p>
* Implementations may apply any restrictions they like to the size/content of the source data, however passing an
* empty source buffer is generally expected to do nothing.
* <p>
* Using the same buffer for both parameters will result in undefined behavior.
*
* @param src the {@link ByteBuf} containing the source data
* @param dst the {@link ByteBuf} that processed data will be written to
*/
void process(@NonNull ByteBuf src, @NonNull ByteBuf dst);

@Override
int refCnt();

@Override
Cipher retain() throws AlreadyReleasedException;

@Override
boolean release() throws AlreadyReleasedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*
*/

package net.daporkchop.lib.crypto;

/**
* Sorry about this, bouncycastle are a bunch of assholes and decided to make a lot of DSA-related things package-private
* @author DaPorkchop_
*/
package net.daporkchop.lib.crypto.sig.ec.hackery;
public interface CryptProvider {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@

package net.daporkchop.lib.crypto;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Security;
import io.netty.buffer.ByteBuf;
import lombok.NonNull;
import net.daporkchop.lib.crypto.key.Key;
import net.daporkchop.lib.unsafe.util.exception.AlreadyReleasedException;

/**
* This class isn't really needed any more, but I'm keeping it around because why not :P
* Representation of a symmetric cipher.
* <p>
* Symmetric ciphers use the same key for encryption and decryption. Plaintext and ciphertext are generally exactly
* the same size, although some padding may be applied at the end during encryption.
*
* @author DaPorkchop_
*/
public class BouncyCastleInit {
static {
Security.addProvider(new BouncyCastleProvider());
}

public static void loadClass() {
//if this method has been called, the class is already loaded!
}
public interface SymmetricCipher extends Cipher {
@Override
SymmetricCipher retain() throws AlreadyReleasedException;
}
Loading