Skip to content

Commit

Permalink
Broken partial impl of ..EC::Group.new 4-arg form
Browse files Browse the repository at this point in the history
See #308
  • Loading branch information
headius committed Jun 11, 2024
1 parent 23ff684 commit df905a1
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECFieldFp;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
Expand Down Expand Up @@ -740,16 +741,52 @@ public Group(Ruby runtime, RubyClass type) {
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;

if ( Arity.checkArgumentCount(runtime, args, 1, 4) == 1 ) {
IRubyObject arg = args[0];
switch ( Arity.checkArgumentCount(runtime, args, 1, 4) ) {
case 1: {
IRubyObject arg = args[0];

if ( arg instanceof Group ) {
this.curve_name = ((Group) arg).implCurveName(runtime);
return this;
if (arg instanceof Group) {
this.curve_name = ((Group) arg).implCurveName(runtime);
return this;
}

this.curve_name = arg.convertToString();
break;
}
case 4: {
if (args[0] instanceof RubySymbol) {
RubyString curveName = args[0].asString();
String curveID = curveName.toString();

BigInteger p = getBigInteger(context, args[1]);
BigInteger a = getBigInteger(context, args[2]);
BigInteger b = getBigInteger(context, args[3]);

EllipticCurve curve;

if (curveID.equals("GFp")) {
this.curve_name = curveName;
curve = new EllipticCurve(new ECFieldFp(p), a, b);

// unsure how to implement this... what to use for `m` in ECFieldF2m constructor?
// } else if (curveID.equals("GF2m")) {
// this.curve_name = curveName;
// curve = new EllipticCurve(new ECFieldF2m(1, p), a, b);
} else {
throw runtime.newArgumentError("unknown symbol, must be :GFp or :GF2m");
}

this.paramSpec = PKeyEC.getParamSpec(curveID, curve);
} else {
throw runtime.newArgumentError("unknown argument, must be :GFp or :GF2m");
}

this.curve_name = arg.convertToString();
break;
}
default:
throw context.runtime.newArgumentError("wrong number of arguments");
}

return this;
}

Expand Down

0 comments on commit df905a1

Please sign in to comment.