Skip to content

Commit

Permalink
Create initial high level APIs
Browse files Browse the repository at this point in the history
Related to #4 and #5
  • Loading branch information
jvz committed Apr 18, 2021
1 parent 3ab702b commit 889058c
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,21 @@
* SPDX-License-Identifier: ISC
*/

package dev.o1c.spi;
package dev.o1c;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface KeyFactory {
default @NotNull KeyPair generateKey() {
return generateKey(null);
}
public interface KeyManager {

@NotNull KeyPair generateKey(byte @Nullable [] id);
// todo: batch generate apis
@NotNull KeyPair generateKeyPair();

default @NotNull KeyPair parsePrivateKey(byte @NotNull [] keyData) {
return parsePrivateKey(null, keyData);
}
@NotNull SecretKey generateSecretKey();

@NotNull KeyPair parsePrivateKey(byte @Nullable [] id, byte @NotNull [] keyData);
@NotNull SecretKey parseSecretKey(byte @NotNull [] secretKey);

default @NotNull PublicKey parsePublicKey(byte @NotNull [] keyData) {
return parsePublicKey(null, keyData);
}
@NotNull PublicKey parsePublicKey(byte @NotNull [] publicKey);

@NotNull KeyPair parsePrivateKey(byte @NotNull [] privateKey);

@NotNull PublicKey parsePublicKey(byte @Nullable [] id, byte @NotNull [] keyData);
}
37 changes: 37 additions & 0 deletions src/main/java/dev/o1c/KeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* ISC License
*
* Copyright (c) 2021, Matt Sicker
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* SPDX-License-Identifier: ISC
*/

package dev.o1c;

import org.jetbrains.annotations.NotNull;

public interface KeyPair extends PublicKey {

byte @NotNull [] box(@NotNull PublicKey recipient, byte @NotNull [] message, byte @NotNull [] context);

byte @NotNull [] openBox(@NotNull PublicKey sender, byte @NotNull [] box, byte @NotNull [] context);

byte @NotNull [] sign(byte @NotNull [] message);

byte @NotNull [] sealedBox(@NotNull PublicKey recipient, byte @NotNull [] message, byte @NotNull [] context);

byte @NotNull [] openSealedBox(@NotNull PublicKey sender, byte @NotNull [] sealedBox, byte @NotNull [] context);

}
27 changes: 27 additions & 0 deletions src/main/java/dev/o1c/PublicKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* ISC License
*
* Copyright (c) 2021, Matt Sicker
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* SPDX-License-Identifier: ISC
*/

package dev.o1c;

import org.jetbrains.annotations.NotNull;

public interface PublicKey {
byte @NotNull [] openSignedMessage(byte @NotNull [] signedMessage);
}
31 changes: 31 additions & 0 deletions src/main/java/dev/o1c/SecretKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ISC License
*
* Copyright (c) 2021, Matt Sicker
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* SPDX-License-Identifier: ISC
*/

package dev.o1c;

import org.jetbrains.annotations.NotNull;

public interface SecretKey {

byte @NotNull [] box(byte @NotNull [] data, byte @NotNull [] context);

byte @NotNull [] openBox(byte @NotNull [] box, byte @NotNull [] context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,63 @@
* SPDX-License-Identifier: ISC
*/

package dev.o1c.impl.ristretto255;
package dev.o1c.impl;

import cafe.cryptography.curve25519.CompressedRistretto;
import cafe.cryptography.curve25519.Scalar;
import dev.o1c.KeyManager;
import dev.o1c.KeyPair;
import dev.o1c.PublicKey;
import dev.o1c.SecretKey;
import dev.o1c.impl.blake3.Blake3HashFactory;
import dev.o1c.impl.blake3.Blake3RandomBytesGenerator;
import dev.o1c.spi.Hash;
import dev.o1c.spi.InvalidKeyException;
import dev.o1c.spi.KeyFactory;
import dev.o1c.spi.PublicKey;
import dev.o1c.spi.KeyPair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

public class Ristretto255KeyFactory implements KeyFactory {
private final Hash expandHash = Blake3HashFactory.INSTANCE.newKeyDerivationFunction("expand_key");
public class DefaultKeyManager implements KeyManager {
private static final int KEY_LENGTH = 32;

@Override
public @NotNull KeyPair generateKey(byte @Nullable [] id) {
byte[] keyData = Blake3RandomBytesGenerator.getInstance().generateBytes(32);
return parsePrivateKey(id, keyData);
public @NotNull KeyPair generateKeyPair() {
return parsePrivateKey(Blake3RandomBytesGenerator.getInstance().generateBytes(KEY_LENGTH));
}

@Override
public @NotNull KeyPair parsePrivateKey(byte @Nullable [] id, byte @NotNull [] keyData) {
if (keyData.length != 32) {
throw new InvalidKeyException("Keys must be 32 bytes");
public @NotNull SecretKey generateSecretKey() {
return new DefaultSecretKey();
}

@Override
public @NotNull SecretKey parseSecretKey(byte @NotNull [] secretKey) {
return new DefaultSecretKey(secretKey);
}

@Override
public @NotNull PublicKey parsePublicKey(byte @NotNull [] publicKey) {
if (publicKey.length != KEY_LENGTH) {
throw new InvalidKeyException("Public key must be 32 bytes");
}
return new DefaultPublicKey(new CompressedRistretto(publicKey));
}

@Override
public @NotNull KeyPair parsePrivateKey(byte @NotNull [] privateKey) {
if (privateKey.length != KEY_LENGTH) {
throw new InvalidKeyException("Private key must be 32 bytes");
}
byte[] expandedKey = new byte[64];
expandHash.reset();
expandHash.update(keyData);
expandHash.doFinalize(expandedKey);
byte[] lower = Arrays.copyOf(expandedKey, 32);
byte[] upper = Arrays.copyOfRange(expandedKey, 32, 64);
Hash expandHash = Blake3HashFactory.INSTANCE.newHash(64);
expandHash.update(privateKey);
byte[] expandedKey = expandHash.doFinalize();
byte[] lower = Arrays.copyOf(expandedKey, KEY_LENGTH);
byte[] upper = Arrays.copyOfRange(expandedKey, KEY_LENGTH, 64);
lower[0] &= 248;
lower[31] &= 127;
lower[31] |= 64;
Scalar scalar = Scalar.fromBits(lower);
Hash challenge = Blake3HashFactory.INSTANCE.newKeyedHash(upper);
return new Ristretto255KeyPair(id, scalar, challenge);
}

@Override
public @NotNull PublicKey parsePublicKey(byte @Nullable [] id, byte @NotNull [] keyData) {
if (keyData.length != 32) {
throw new InvalidKeyException("Keys must be 32 bytes");
}
return new Ristretto255PublicKey(id, new CompressedRistretto(keyData.clone()));
return new DefaultKeyPair(scalar, challenge);
}
}
Loading

0 comments on commit 889058c

Please sign in to comment.