Skip to content

Commit

Permalink
Crypto pt 1 working (#93)
Browse files Browse the repository at this point in the history
* Update starlark to tip & start import of test_RSA.star from pycryptodome using py2star

* Allow python like packages (Name/__init__.star)

* Start implementation of Pycryptodome using bouncycastle.

* More RSA migrations

* Actually integrate the test to make it import and fail.

* Add test types ported from bazel.

* Allow types.is_bytes() and types.is_bytearray() to work. Introduce test_types test

* continue

* Add tests for sets, allow chaining of described as.

* Get asn1 tests working

* Enable common Dual operations for bytes and bytearrays

* checkpoint

* checkpoint

* checkpoint

* checkpoint

* checkpoint

* Checkpoint

* More tests

* checkpoint

* - add export public keys test
- update random larky impl to java bc impl

* strxor tests pass!

* introduce concept of quick tests. padding test works. add rfind to bytelike

* number tests passing

* byte() or bytearray() => empty

* trying my best to really solve this asn1 thing, but now just to skip to the crypto implementations

* some of the asn tests are passing, introduced a bytearrayutil to help, some major refactoring is needed for the byte-like stuff

* Asn DerObject tests are all passing

Co-authored-by: Mahmoud Abdelkader <mabdelkader@gmail.com>
  • Loading branch information
aslepakurov and mahmoudimus authored Mar 31, 2021
1 parent 3f0fa6b commit bb60fba
Show file tree
Hide file tree
Showing 114 changed files with 13,520 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
Expand Down Expand Up @@ -91,11 +95,31 @@ public LarkyByteLike a2b_base64(String s, StarlarkThread thread) throws EvalExce
parameters = {
@Param(
name = "hexstr",
allowedTypes = {@ParamType(type = String.class)}
allowedTypes = {
@ParamType(type=LarkyByteLike.class),
@ParamType(type=String.class)
}
)
},
useStarlarkThread = true)
public LarkyByteLike a2b_hex(String hexstr, StarlarkThread thread) throws EvalException {
public LarkyByteLike a2b_hex(Object hexed, StarlarkThread thread) throws EvalException {
String hexstr = "";
if(hexed instanceof LarkyByteLike) {
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
LarkyByteLike byteLike = (LarkyByteLike) hexed;
hexstr = decoder.decode(ByteBuffer.wrap(byteLike.getBytes())).toString();
} catch (CharacterCodingException e) {
// we accept only ascii-only unicode strings or strings
throw Starlark.errorf(e.getMessage());
}
}
else if(hexed instanceof String) {
hexstr = (String) hexed;
}

int length = hexstr.length();
if (length % 2 != 0) {
throw new EvalException(ODD_LENGTH_STRING);
Expand Down Expand Up @@ -128,7 +152,7 @@ public LarkyByteLike a2b_hex(String hexstr, StarlarkThread thread) throws EvalEx
parameters = {
@Param(
name = "data",
allowedTypes = {@ParamType(type = LarkyByte.class)}
allowedTypes = {@ParamType(type = LarkyByteLike.class)}
),
@Param(
name = "newline",
Expand All @@ -138,7 +162,7 @@ public LarkyByteLike a2b_hex(String hexstr, StarlarkThread thread) throws EvalEx
)
},
useStarlarkThread = true)
public LarkyByteLike b2a_base64(LarkyByte data, Boolean newline, StarlarkThread thread) throws EvalException {
public LarkyByteLike b2a_base64(LarkyByteLike data, Boolean newline, StarlarkThread thread) throws EvalException {
byte[] encoded;
try {
encoded = Base64.getEncoder().encode(data.getBytes());
Expand Down Expand Up @@ -174,14 +198,14 @@ public LarkyByteLike b2a_base64(LarkyByte data, Boolean newline, StarlarkThread
parameters = {
@Param(
name = "data",
allowedTypes = {@ParamType(type = LarkyByte.class)}
allowedTypes = {@ParamType(type = LarkyByteLike.class)}
),
@Param(
name = "sep",
allowedTypes = {
@ParamType(type = NoneType.class),
@ParamType(type = String.class),
@ParamType(type = LarkyByte.class)
@ParamType(type = LarkyByteLike.class)
},
named = true,
defaultValue = "None"
Expand All @@ -193,7 +217,7 @@ public LarkyByteLike b2a_base64(LarkyByte data, Boolean newline, StarlarkThread
defaultValue = "1"
)
})
public String b2a_hex(LarkyByte binstr, Object sep, StarlarkInt bytes_per_sep) {
public String b2a_hex(LarkyByteLike binstr, Object sep, StarlarkInt bytes_per_sep) {
StringBuilder b = new StringBuilder(binstr.size() * 2);
byte[] bytes = binstr.getBytes();
for (int n : bytes) {
Expand Down Expand Up @@ -222,7 +246,7 @@ public String b2a_hex(LarkyByte binstr, Object sep, StarlarkInt bytes_per_sep) {
allowedTypes = {@ParamType(type = StarlarkInt.class)})
}
)
public StarlarkInt crc32(LarkyByte data, StarlarkInt value) {
public StarlarkInt crc32(LarkyByteLike data, StarlarkInt value) {
CRC32 crc32 = new CRC32();
if(value.toIntUnchecked() != 0) {
crc32.update(value.toIntUnchecked());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkFloat;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkValue;

import java.math.BigDecimal;
import java.math.RoundingMode;


@StarlarkBuiltin(
name = "c99math",
Expand Down Expand Up @@ -91,4 +96,66 @@ public StarlarkFloat pow(Object x, Object y) throws EvalException {
return StarlarkFloat.of(Math.pow(base, exp));
}

@StarlarkMethod(
name = "fabs",
doc = "Return the absolute value of x",
parameters = {
@Param(
name = "x",
doc = "Return the absolute value of x."
)
}
)
public StarlarkFloat fabs(Object x) {
double base = Double.parseDouble(String.valueOf(x));
return StarlarkFloat.of(Math.abs(base));
}

@StarlarkMethod(
name = "ceil",
doc = "Return the ceiling of x, the smallest integer greater than or equal to x. " +
"If x is not a float, delegates to x.__ceil__(), which should return an Integral value.",
parameters = {
@Param(
name = "x"
)
}
)
public StarlarkInt ceil(Object x) {
double base = Double.parseDouble(String.valueOf(x));
return StarlarkInt.of((int) Math.ceil(base));
}

@StarlarkMethod(
name = "log",
doc = "Return the ceiling of x, the smallest integer greater than or equal to x. " +
"If x is not a float, delegates to x.__ceil__(), which should return an Integral value.",
parameters = {
@Param(
name = "x"
),
@Param(
name = "base",
named = true,
defaultValue = "None"
)
}
)
public Object log(Object x, StarlarkValue base) throws EvalException {
BigDecimal decimal;
if(x instanceof StarlarkFloat) {
decimal = BigDecimal.valueOf(Math.log(
((StarlarkFloat) x).toDouble()
));
} else {
int nx = Starlark.toInt(x, "to int in Math.log()");
decimal = BigDecimal.valueOf(Math.log(nx));
}
if (base == Starlark.NONE) {
return StarlarkFloat.of(decimal.doubleValue());
}
BigDecimal b = BigDecimal.valueOf(Double.parseDouble(String.valueOf(base)));
return StarlarkFloat.of(decimal.divide(b, RoundingMode.HALF_EVEN).doubleValue());
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.verygood.security.larky.modules;

import com.google.common.primitives.Bytes;

import com.verygood.security.larky.modules.codecs.TextUtil;
import com.verygood.security.larky.modules.types.LarkyByte;

Expand All @@ -10,11 +8,15 @@
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkInt;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkThread;
import net.starlark.java.eval.StarlarkValue;

import java.util.stream.Collectors;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;


@StarlarkBuiltin(
Expand Down Expand Up @@ -57,15 +59,20 @@ public class CodecsModule implements StarlarkValue {
defaultValue = "'strict'",
named = true
)
}
},
useStarlarkThread = true
)
public StarlarkList<StarlarkInt> encode(String strToEncode, String encoding, String errors) throws EvalException {
TextUtil textUtil = new TextUtil(TextUtil.unescapeJavaString(strToEncode));
return StarlarkList.immutableCopyOf(
Bytes.asList(textUtil.copyBytes()).stream()
.map(Byte::toUnsignedInt)
.map(StarlarkInt::of)
.collect(Collectors.toList()));
public LarkyByte encode(String strToEncode, String encoding, String errors, StarlarkThread thread) throws EvalException {
CharsetEncoder encoder = Charset.forName(encoding)
.newEncoder()
.onMalformedInput(TextUtil.CodecHelper.convertCodingErrorAction(errors))
.onUnmappableCharacter(TextUtil.CodecHelper.convertCodingErrorAction(errors));
try {
ByteBuffer encoded = encoder.encode(CharBuffer.wrap(TextUtil.unescapeJavaString(strToEncode)));
return (LarkyByte) LarkyByte.builder(thread).setSequence(encoded).build();
} catch (CharacterCodingException e) {
throw Starlark.errorf(e.getMessage());
}
}

@StarlarkMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.verygood.security.larky.modules;

import com.verygood.security.larky.modules.crypto.CryptoCipherModule;
import com.verygood.security.larky.modules.crypto.CryptoHashModule;
import com.verygood.security.larky.modules.crypto.CryptoIOModule;
import com.verygood.security.larky.modules.crypto.CryptoMathModule;
import com.verygood.security.larky.modules.crypto.CryptoProtocolModule;
import com.verygood.security.larky.modules.crypto.CryptoPublicKeyModule;
import com.verygood.security.larky.modules.crypto.CryptoRandomModule;
import com.verygood.security.larky.modules.crypto.CryptoSignatureModule;
import com.verygood.security.larky.modules.crypto.CryptoUtilModule;

import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
Expand Down Expand Up @@ -46,10 +54,35 @@ public class CryptoModule implements StarlarkValue {

public static final CryptoModule INSTANCE = new CryptoModule();

@StarlarkMethod(name="Cipher", structField = true)
public CryptoCipherModule Cipher() { return CryptoCipherModule.INSTANCE; }

@StarlarkMethod(name="Hash", structField = true)
public CryptoHashModule Hash() {
return CryptoHashModule.INSTANCE;
}

@StarlarkMethod(name="IO", structField = true)
public CryptoIOModule IO() { return CryptoIOModule.INSTANCE; }

@StarlarkMethod(name="Math", structField = true)
public CryptoMathModule Math() { return CryptoMathModule.INSTANCE; }

@StarlarkMethod(name="Protocol", structField = true)
public CryptoProtocolModule Protocol() { return CryptoProtocolModule.INSTANCE; }

@StarlarkMethod(name="PublicKey", structField = true)
public CryptoPublicKeyModule PublicKey() { return CryptoPublicKeyModule.INSTANCE; }

@StarlarkMethod(name="Random", structField = true)
public CryptoRandomModule Random() {
return CryptoRandomModule.INSTANCE;
}

@StarlarkMethod(name="Signature", structField = true)
public CryptoSignatureModule Signature() { return CryptoSignatureModule.INSTANCE; }

@StarlarkMethod(name="Util", structField = true)
public CryptoUtilModule Util() { return CryptoUtilModule.INSTANCE; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public byte[] pack_single_data(char fmt, long val) {

default:
//do nothing
System.out.println("Invalid format specifier");
System.out.println("Invalid format specifier: " + fmt);
bx = null;
break;

Expand Down
Loading

0 comments on commit bb60fba

Please sign in to comment.