Skip to content

Commit 653c4c5

Browse files
Allow RSA key imports! (#96)
* Sort tests where lowercase comes first * move jacobi symbol to cryptomathmodule from numpopsutils and remove unused class in number.star * Add unittest runner for test_import_RSA.star. Now they run, and obviously fail. * Surefire v3 test for larky * second test working.. * Export private key * commit failing tests * Working test for pem decoding encoded with PKCS#1 * checkpoint, drew you now take over * working test_export_public key! * going for it * importing it * tests work! * tests are passing, 1 by 1 * more passing tests * more tests passing * end of all working tests, we are 22 away from full test suite coverage * Make it a test * Update tests suite run breakdown * fix codecs Co-authored-by: aslepakurov <a.slepakurov@gmail.com>
1 parent 9206e2b commit 653c4c5

File tree

26 files changed

+1459
-642
lines changed

26 files changed

+1459
-642
lines changed

larky/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3535
<maven.source.plugin.version>3.2.1</maven.source.plugin.version>
3636
<maven.compiler.plugin.version>3.6.2</maven.compiler.plugin.version>
37+
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
3738
<libstarlark.version>1.0-SNAPSHOT</libstarlark.version>
3839
<org.projectlombok.version>1.18.12</org.projectlombok.version>
3940
<common.io.version>2.8.0</common.io.version>
@@ -272,6 +273,16 @@
272273
</execution>
273274
</executions>
274275
</plugin>
276+
<plugin>
277+
<artifactId>maven-surefire-plugin</artifactId>
278+
<version>${surefire-plugin.version}</version>
279+
<configuration>
280+
<!-- <argLine>-->
281+
<!-- -Xbootclasspath/p:${settings.localRepository}/com/google/errorprone/javac/${google.errorprone.javac.version}/javac-${google.errorprone.javac.version}.jar-->
282+
<!-- </argLine>-->
283+
<trimStackTrace>false</trimStackTrace>
284+
</configuration>
285+
</plugin>
275286
</plugins>
276287
<extensions>
277288
<extension>

larky/src/main/java/com/verygood/security/larky/modules/CodecsModule.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.verygood.security.larky.modules.codecs.TextUtil;
44
import com.verygood.security.larky.modules.types.LarkyByte;
5+
import com.verygood.security.larky.modules.types.LarkyByteLike;
56

67
import net.starlark.java.annot.Param;
78
import net.starlark.java.annot.ParamType;
@@ -16,7 +17,9 @@
1617
import java.nio.CharBuffer;
1718
import java.nio.charset.CharacterCodingException;
1819
import java.nio.charset.Charset;
20+
import java.nio.charset.CharsetDecoder;
1921
import java.nio.charset.CharsetEncoder;
22+
import java.nio.charset.StandardCharsets;
2023

2124

2225
@StarlarkBuiltin(
@@ -26,6 +29,7 @@
2629
public class CodecsModule implements StarlarkValue {
2730

2831
public static final CodecsModule INSTANCE = new CodecsModule();
32+
private static final String UTF8 = StandardCharsets.UTF_8.toString().toLowerCase();
2933

3034
@StarlarkMethod(
3135
name = "encode",
@@ -88,7 +92,7 @@ public LarkyByte encode(String strToEncode, String encoding, String errors, Star
8892
@Param(
8993
name = "obj",
9094
allowedTypes = {
91-
@ParamType(type = LarkyByte.class),
95+
@ParamType(type = LarkyByteLike.class),
9296
}
9397
),
9498
@Param(
@@ -109,7 +113,20 @@ public LarkyByte encode(String strToEncode, String encoding, String errors, Star
109113
)
110114
}
111115
)
112-
public String decode(LarkyByte bytesToDecode, String encoding, String errors) {
116+
public String decode(LarkyByteLike bytesToDecode, String encoding, String errors) throws EvalException {
117+
if(CodecsModule.UTF8.equals(encoding.toLowerCase())) {
113118
return TextUtil.starlarkDecodeUtf8(bytesToDecode.getBytes());
119+
}
120+
CharsetDecoder decoder = Charset.forName(encoding)
121+
.newDecoder()
122+
.onMalformedInput(TextUtil.CodecHelper.convertCodingErrorAction(errors))
123+
.onUnmappableCharacter(TextUtil.CodecHelper.convertCodingErrorAction(errors));
124+
CharBuffer decoded;
125+
try {
126+
decoded = decoder.decode(ByteBuffer.wrap(bytesToDecode.getBytes()));
127+
} catch (CharacterCodingException e) {
128+
throw Starlark.errorf(e.getMessage());
129+
}
130+
return decoded.toString();
114131
}
115132
}

larky/src/main/java/com/verygood/security/larky/modules/CryptoModule.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1818

19-
import java.security.Provider;
2019
import java.security.Security;
2120

2221

@@ -45,11 +44,7 @@ public class CryptoModule implements StarlarkValue {
4544
*/
4645
Security.addProvider(new BouncyCastleProvider());
4746
/* uncomment the below line for a post-quantum provider */
48-
// Security.addProvider(new BouncyCastlePQCProvider());
49-
Provider[] providers = Security.getProviders();
50-
for (int i = 0; i != providers.length; i++) {
51-
System.out.println("Name: " + providers[i].getName() + " Version: " + providers[i].getVersion());
52-
}
47+
// Security.addProvider(new BouncyCastlePQCProvider());
5348
}
5449

5550
public static final CryptoModule INSTANCE = new CryptoModule();

larky/src/main/java/com/verygood/security/larky/modules/crypto/CryptoMathModule.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public class CryptoMathModule implements StarlarkValue {
2828
private static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
2929

3030
@StarlarkMethod(name = "jacobi_number",
31-
doc = "jacobi number",
31+
doc = "Computes Jacobi(p,n).\n" +
32+
" Assumes n positive, odd, n>=3.\n" +
33+
" Compute the jacobi symbol <code>(a/n)</code>, as described in:\n" +
34+
" <a href=\"http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf\">Digital signature standard (DSS). FIPS PUB 186-4, National Institute of Standards and\n" +
35+
" Technology (NIST), 2013.</a>, pp. 76-77",
3236
parameters = {
3337
@Param(name = "a", allowedTypes = {@ParamType(type = StarlarkInt.class)}),
3438
@Param(name = "n", allowedTypes = {@ParamType(type = StarlarkInt.class)}),

0 commit comments

Comments
 (0)