Skip to content

Instantly share code, notes, and snippets.

@jpopesculian
Last active June 28, 2019 09:02
Show Gist options
  • Save jpopesculian/5811e5cacba19e9128db8b03152e8ea5 to your computer and use it in GitHub Desktop.
Save jpopesculian/5811e5cacba19e9128db8b03152e8ea5 to your computer and use it in GitHub Desktop.
Failing SCONE ECKeyPair generator
FROM openjdk:8-alpine
COPY HelloEc.java .
RUN javac -g HelloEc.java
CMD ["java", "HelloEc"]
FROM sconecuratedimages/apps:8-jdk-alpine
COPY HelloEc.java .
RUN javac -g HelloEc.java
ENV SCONE_VERSION=1
ENV SCONE_LOG=7
ENV SCONE_MODE=hw
CMD ["java", "HelloEc"]
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECFieldFp;
import java.security.spec.EllipticCurve;
class HelloEc {
public static void main(String[] args)
throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
// secp256k1 specs
ECGenParameterSpec namedSecp256k1Spec = new ECGenParameterSpec("secp256k1");
EllipticCurve secp256k1Curve = new EllipticCurve(
new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 16)),
new BigInteger("0000000000000000000000000000000000000000000000000000000000000000", 16),
new BigInteger("0000000000000000000000000000000000000000000000000000000000000007", 16)
);
ECParameterSpec secp256k1ParameterSpec = new ECParameterSpec(
secp256k1Curve,
new ECPoint(
new BigInteger("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", 16),
new BigInteger("483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", 16)
),
new BigInteger("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16),
1
);
// secp256r1 specs
ECGenParameterSpec namedSecp256r1Spec = new ECGenParameterSpec("secp256r1");
EllipticCurve secp256r1Curve = new EllipticCurve(
new ECFieldFp(new BigInteger("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", 16)),
new BigInteger("ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", 16),
new BigInteger("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", 16)
);
ECParameterSpec secp256r1ParameterSpec = new ECParameterSpec(
secp256r1Curve,
new ECPoint(
new BigInteger("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", 16),
new BigInteger("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", 16)
),
new BigInteger("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 16),
1
);
// failing scenarios
runScenario(namedSecp256k1Spec, "named secp256k1 specifications");
runScenario(secp256k1ParameterSpec, "manually input secp256k1 specifications");
// passing scenarios
runScenario(namedSecp256r1Spec, "named secp256r1 specifications");
runScenario(secp256r1ParameterSpec, "manually input secp256r1 specifications");
}
private static void success(String message) {
int[] emojiCodepoints = {0x1f44d};
String emoji = new String(emojiCodepoints, 0, emojiCodepoints.length);
System.out.println(emoji + " " + message);
}
private static void failure(String message) {
int[] emojiCodepoints = {0x1f914};
String emoji = new String(emojiCodepoints, 0, emojiCodepoints.length);
System.out.println(emoji + " " + message);
}
private static void runScenario(AlgorithmParameterSpec spec, String specName) {
String scenarioName = "Key generation with " + specName;
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "SunEC");
kpg.initialize(spec);
KeyPair kp = kpg.generateKeyPair();
success(scenarioName + " successfully ran: " + kp.toString());
} catch (Exception e) {
failure(scenarioName + " fails!");
// throw e;
}
}
}
all: build run
scone: build-scone run-scone
build: HelloEc.java
docker build -f Dockerfile -t hello-ec .
run:
docker run -it --rm --device=/dev/isgx hello-ec
build-scone: HelloEc.java
docker build -f Dockerfile.scone -t hello-ec-scone .
run-scone:
docker run -it --rm --device=/dev/isgx hello-ec-scone
run-debug:
docker run -it --rm --device=/dev/isgx -e JAVA_TOOL_OPTIONS="-Xmx256m -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005" -p 5005:5005 hello-ec-scone
clean:
-docker rmi hello-ec
-docker rmi hello-ec-scone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment