Skip to content

Commit

Permalink
ECC (#250)
Browse files Browse the repository at this point in the history
* Fix DLTaggedObject in ASN1

* DSS, DSA, Primality

* DSS, DSA, Primality

* DSA/DSS updates. Shift too big :(

* Generate works!

* Signature working!

* Verification works.
Sometimes there is a modulus error
"No inverse value can be computed <some non-1 modulus>"

* JWS + ECC

* Add ECC to cryptography.hazmat.backends.pycryptodome

* Add more ECC and fix some ASN1

* More ECC + JWS

* Fix SHA384 hexdigest

* Jose ECDSA

* Remove extraneous print statements

* Delete DSA_old.star

* Delete DSS_old.star

Co-authored-by: aslepakurov <[email protected]>
  • Loading branch information
Josh and aslepakurov authored May 4, 2022
1 parent a6fc494 commit a203828
Show file tree
Hide file tree
Showing 20 changed files with 2,157 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.ASN1String;
import org.bouncycastle.asn1.ASN1TaggedObjectParser;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.DERUTF8String;
import org.bouncycastle.asn1.DLTaggedObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;

Expand Down Expand Up @@ -360,6 +362,19 @@ Object toStarlark() throws EvalException {
}
}

public static class LarkyDLTaggedObject extends LarkyASN1Encodable{

public LarkyDLTaggedObject(DLTaggedObject dlTaggedObject) {
super(dlTaggedObject);
}

@Override
Object toStarlark() {
return((DLTaggedObject)this.encodable).toString();
}
}


public static class LarkyASN1Sequence extends LarkyASN1Encodable
implements StarlarkIterable<LarkyASN1Encodable>, StarlarkIndexable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.bouncycastle.asn1.ASN1Null;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DLTaggedObject;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERUTF8String;
Expand Down Expand Up @@ -204,6 +205,9 @@ else if (obj instanceof DERBitString) {
} else if (obj instanceof DEROctetString) {
DEROctetString dbs = (DEROctetString) obj;
return new ASN1.LarkyOctetString(dbs);
} else if (obj instanceof DLTaggedObject){
DLTaggedObject dltObject = (DLTaggedObject) obj;
return new ASN1.LarkyDLTaggedObject(dltObject);
} else {
throw Starlark.errorf("Unknown type %s to convert to asASN1Encodable", Starlark.type(obj));
}
Expand Down
1 change: 1 addition & 0 deletions larky/src/main/resources/stdlib/sets.star
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def Set(iterable=None):
def difference(b):
return Set([e for e in self._values.keys() if e not in b._values])
self.difference = difference
self.__sub__ = difference

def _set_length():
return _length(self)
Expand Down
3 changes: 2 additions & 1 deletion larky/src/main/resources/vendor/Crypto/Hash/SHA384.star
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# SOFTWARE.
# ===================================================================

load("@stdlib//binascii", hexlify="hexlify")
load("@stdlib//larky", larky="larky")
load("@stdlib//jcrypto", _JCrypto="jcrypto")
load("@vendor//Crypto/Util/py3compat", tobytes="tobytes", bord="bord", tostr="tostr")
Expand Down Expand Up @@ -92,7 +93,7 @@ def SHA384Hash(data=None):
:rtype: string
"""

return "".join(["%02x" % bord(x) for x in self.digest()])
return tostr(hexlify(self.digest()))
self.hexdigest = hexdigest

def copy():
Expand Down
25 changes: 22 additions & 3 deletions larky/src/main/resources/vendor/Crypto/IO/PKCS8.star
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# POSSIBILITY OF SUCH DAMAGE.
# ===================================================================
load("@stdlib//larky", larky="larky")
load("@stdlib//binascii", unhexlify="unhexlify", hexlify="hexlify")
load("@vendor//Crypto/IO/_PBES", PBES1="PBES1", PBES2="PBES2", PbesError="PbesError")
load("@vendor//Crypto/Util/asn1", DerNull="DerNull", DerSequence="DerSequence", DerObjectId="DerObjectId", DerOctetString="DerOctetString")
load("@vendor//Crypto/Util/py3compat", bord="bord", tobytes="tobytes", _copy_bytes="copy_bytes")
Expand Down Expand Up @@ -187,6 +188,7 @@ def unwrap(p8_private_key, passphrase=None):
if len(pk_info) == 2 and not passphrase:
return Error("Not a valid clear PKCS#8 structure " +
"(maybe it is encrypted?)").unwrap()
pk_info._seq = _repack_oids(pk_info._seq)

#
# PrivateKeyInfo ::= SEQUENCE {
Expand Down Expand Up @@ -217,16 +219,33 @@ def unwrap(p8_private_key, passphrase=None):
if len(algo) == 1:
algo_params = None
else:
res = Ok(DerNull().decode).map(algo[1])
if res.is_ok:
if algo[1] == b'\x05\x00':
algo_params = None
else:
algo_params = algo[1]

# EncryptedData ::= OCTET STRING
private_key = DerOctetString().decode(pk_info[2]).payload
"""
if DerSequence._seq is returning a list containing strings, it has already been
parsed, and the pk_info[2] contains a string that starts with a '#', and is
followed by a hexlified string of the bytes that would be returned anyway.
"""
if type(pk_info[2]) == 'string':
private_key = bytes(unhexlify(''.join(pk_info[2].split('#'))), 'utf-8')
else:
private_key = DerOctetString().decode(pk_info[2]).payload

return (algo_oid, private_key, algo_params)

def _repack_oids(sequence):
for index, item in enumerate(sequence):
if type(item) == 'list':
der_seq = DerSequence()
for thing in item:
der_seq.append(DerObjectId(thing).encode())
item = der_seq.encode()
sequence[index] = item
return sequence

PKCS8 = larky.struct(
__name__='PKCS8',
Expand Down
2 changes: 1 addition & 1 deletion larky/src/main/resources/vendor/Crypto/Math/Numbers.star
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,4 @@ Numbers = larky.struct(
)
)

Integer = Numbers.Integer
Integer = Numbers.Integer
Loading

0 comments on commit a203828

Please sign in to comment.