Skip to content

Commit

Permalink
Ed dsa (#74)
Browse files Browse the repository at this point in the history
Add an implementation of EdDSA that points to a different implementation.  That module should not be required to be loaded.
  • Loading branch information
jimsch authored Nov 3, 2018
1 parent 6c2f025 commit ff8e8d3
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# Mobile Tools for Java (J2ME)
.mtj.tmp/
*.gpg
*.asc

# Package Files #
*.jar
Expand Down
18 changes: 18 additions & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
</properties>
</project-shared-configuration>
14 changes: 14 additions & 0 deletions nbactions-ossrh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>CUSTOM-deploy</actionName>
<displayName>deploy</displayName>
<preAction>build-with-dependencies</preAction>
<goals>
<goal>deploy</goal>
</goals>
<activatedProfiles>
<activatedProfile>ossrh</activatedProfile>
</activatedProfiles>
</action>
</actions>
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.augustcellars.cose</groupId>
<artifactId>cose-java</artifactId>
<version>0.9.7</version>
<version>0.9.8-SNAPSHOT</version>

<name>com.augustcellars.cose:cose-java</name>
<description>A Java implementation that supports the COSE secure message specification.</description>
Expand Down Expand Up @@ -136,6 +136,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.i2p.crypto</groupId>
<artifactId>eddsa</artifactId>
<version>0.2.0</version>
<type>jar</type>
</dependency>
</dependencies>

<profiles>
Expand Down
111 changes: 72 additions & 39 deletions src/main/java/COSE/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ public TagValue(int tagIn, ArrayList<TagValue> listIn) {
// 1.2.840.10045.2.1
public static final byte[] oid_ecPublicKey = new byte[]{0x06, 0x07, 0x2a, (byte) 0x86, 0x48, (byte) 0xce, 0x3d, 0x2, 0x1};

// 1.3.101.110
public static final byte[] Oid_X25519 = new byte[]{0x6, 3, 0x2b, 101, 110};
// 1.3.101.111
public static final byte[] Oid_X448 = new byte[]{0x6, 3, 0x2b, 101, 111};
// 1.3.101.112
public static final byte[] Oid_Ed25519 = new byte[]{0x6, 0x3, 0x2b, 101, 112};
// 1.3.101.113
public static final byte[] Oid_Ed448 = new byte[]{0x6, 0x3, 0x2b, 101, 113};

private static final byte[] SequenceX = new byte[]{0x30};
private static final byte[] BitstringTag = new byte[]{0x3};
private static final byte[] OctetstringTag = new byte[]{0x4};
private static final byte[] SequenceTag = new byte[]{0x30};
private static final byte[] OctetStringTag = new byte[]{0x4};
private static final byte[] BitStringTag = new byte[]{0x3};

/**
* Encode a subject public key info structure from an OID and the data bytes
Expand All @@ -58,7 +66,7 @@ public TagValue(int tagIn, ArrayList<TagValue> listIn) {
* @param keyBytes - encoded key bytes
* @return - encoded SPKI
*/
public static byte[] EncodeSubjectPublicKeyInfo(byte[] oid, byte[] keyBytes) throws CoseException
public static byte[] EncodeSubjectPublicKeyInfo(byte[] algorithm, byte[] keyBytes) throws CoseException
{
// SPKI ::= SEQUENCE {
// algorithm SEQUENCE {
Expand All @@ -68,9 +76,9 @@ public static byte[] EncodeSubjectPublicKeyInfo(byte[] oid, byte[] keyBytes) thr
// subjectPublicKey BIT STRING CONTAINS key bytes
// }
try {
ArrayList<byte[]> xxx = new ArrayList<>();
xxx.add(AlgorithmIdentifier(oid_ecPublicKey, oid));
xxx.add(BitstringTag);
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
xxx.add(algorithm);
xxx.add(new byte[]{3});
xxx.add(ComputeLength(keyBytes.length+1));
xxx.add(new byte[]{0});
xxx.add(keyBytes);
Expand All @@ -84,6 +92,44 @@ public static byte[] EncodeSubjectPublicKeyInfo(byte[] oid, byte[] keyBytes) thr
}

/**
* Encode an EC Private key
* @param oid - curve to use
* @param keyBytes - bytes of the key
* @param spki - optional SPKI
* @return
* @throws CoseException
*/
public static byte[] EncodeEcPrivateKey(byte[] oid, byte[] keyBytes, byte[] spki) throws CoseException
{
// ECPrivateKey ::= SEQUENCE {
// version INTEGER {1}
// privateKey OCTET STRING
// parameters [0] OBJECT IDENTIFIER = named curve
// public key [1] BIT STRING OPTIONAL
// }
//

ArrayList<byte[]> xxx = new ArrayList<byte[]>();
xxx.add(new byte[]{2, 1, 1});
xxx.add(OctetStringTag);
xxx.add(ComputeLength(keyBytes.length));
xxx.add(keyBytes);
xxx.add(new byte[]{(byte)0xa0});
xxx.add(ComputeLength(oid.length));
xxx.add(oid);
if (spki != null) {
xxx.add(new byte[]{(byte)0xa1});
xxx.add(ComputeLength(spki.length+1));
xxx.add(new byte[]{0});
xxx.add(spki);
}

byte[] ecPrivateKey = Sequence(xxx);

return ecPrivateKey;
}

/*
* Decode an object which is supposed to be a SubjectPublicKeyInfo strucuture
* and check that the right set of fields are in the right place
*
Expand Down Expand Up @@ -174,15 +220,8 @@ public static TagValue DecodeCompound(int offset, byte[] encoding) throws CoseEx
* @return byte array of encoded bytes
* @throws CoseException
*/
public static byte[] EncodePKCS8(byte[] oid, byte[] keyBytes, byte[] spki) throws CoseException
public static byte[] EncodePKCS8(byte[] algorithm, byte[] keyBytes, byte[] spki) throws CoseException
{
// ECPrivateKey ::= SEQUENCE {
// version INTEGER {1}
// privateKey OCTET STRING
// parameters [0] OBJECT IDENTIFIER = named curve
// public key [1] BIT STRING OPTIONAL
// }
//
// PKCS#8 ::= SEQUENCE {
// version INTEGER {0}
// privateKeyALgorithm SEQUENCE {
Expand All @@ -195,29 +234,13 @@ public static byte[] EncodePKCS8(byte[] oid, byte[] keyBytes, byte[] spki) throw
// }

try {
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
xxx.add(new byte[]{2, 1, 1});
xxx.add(OctetstringTag);
xxx.add(ComputeLength(keyBytes.length));
xxx.add(keyBytes);
xxx.add(new byte[]{(byte)0xa0});
xxx.add(ComputeLength(oid.length));
xxx.add(oid);
if (spki != null) {
xxx.add(new byte[]{(byte)0xa1});
xxx.add(ComputeLength(spki.length+1));
xxx.add(new byte[]{0});
xxx.add(spki);
}

byte[] ecPrivateKey = Sequence(xxx);

xxx = new ArrayList<byte[]>();
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
xxx.add(new byte[]{2, 1, 0});
xxx.add(AlgorithmIdentifier(oid_ecPublicKey, oid));
xxx.add(OctetstringTag);
xxx.add(ComputeLength(ecPrivateKey.length));
xxx.add(ecPrivateKey);
xxx.add(algorithm);
xxx.add(OctetStringTag);
xxx.add(ComputeLength(keyBytes.length));
xxx.add(keyBytes);

return Sequence(xxx);
}
Expand Down Expand Up @@ -294,7 +317,6 @@ public static ArrayList<TagValue> DecodePKCS8(byte[] encodedData) throws CoseExc
return retValue;
}


public static byte[] EncodeSignature(byte[] r, byte[] s) throws CoseException {
ArrayList<byte[]> x = new ArrayList<byte[]>();
x.add(UnsignedInteger(r));
Expand All @@ -303,7 +325,16 @@ public static byte[] EncodeSignature(byte[] r, byte[] s) throws CoseException {
return Sequence(x);
}

private static byte[] AlgorithmIdentifier(byte[] oid, byte[] params) throws CoseException
public static byte[] EncodeOctetString(byte[] data) throws CoseException {
ArrayList<byte[]> x = new ArrayList<byte[]>();
x.add(OctetStringTag);
x.add(ComputeLength(data.length));
x.add(data);

return ToBytes(x);
}

public static byte[] AlgorithmIdentifier(byte[] oid, byte[] params) throws CoseException
{
ArrayList<byte[]> xxx = new ArrayList<byte[]>();
xxx.add(oid);
Expand All @@ -312,16 +343,18 @@ private static byte[] AlgorithmIdentifier(byte[] oid, byte[] params) throws Cose
}
return Sequence(xxx);
}

private static byte[] Sequence(ArrayList<byte[]> members) throws CoseException
{
byte[] y = ToBytes(members);
ArrayList<byte[]> x = new ArrayList<byte[]>();
x.add(SequenceX);
x.add(SequenceTag);
x.add(ComputeLength(y.length));
x.add(y);

return ToBytes(x);
}

private static byte[] UnsignedInteger(byte[] i) throws CoseException {
int pad = 0, offset = 0;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/COSE/AlgorithmID.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum AlgorithmID {
HKDF_HMAC_AES_256(-13, 256, 0),
ECDSA_384(-35, 0, 0),
ECDSA_512(-36, 0, 0),
EDDSA(-8, 0, 0),

ECDH_ES_HKDF_256(-25, 0, 0),
ECDH_ES_HKDF_512(-26, 0, 0),
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/COSE/KeyKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ public enum KeyKeys {
EC2_X(-2),
EC2_Y(-3),
EC2_D(-4),
OKP_Curve(-1),
OKP_X(-2),
OKP_D(-4),
;

private final CBORObject value;

public final static CBORObject KeyType_OKP = CBORObject.FromObject(1);
public final static CBORObject KeyType_EC2 = CBORObject.FromObject(2);
public final static CBORObject KeyType_Octet = CBORObject.FromObject(4);

public final static CBORObject EC2_P256 = CBORObject.FromObject(1);
public final static CBORObject EC2_P384 = CBORObject.FromObject(2);
public final static CBORObject EC2_P521 = CBORObject.FromObject(3);

public final static CBORObject OKP_X25519 = CBORObject.FromObject(4);
public final static CBORObject OKP_X448 = CBORObject.FromObject(5);
public final static CBORObject OKP_Ed25519 = CBORObject.FromObject(6);
public final static CBORObject OKP_Ed448 = CBORObject.FromObject(7);

KeyKeys(int val) {
this.value = CBORObject.FromObject(val);
}
Expand Down
Loading

0 comments on commit ff8e8d3

Please sign in to comment.