Skip to content

Commit

Permalink
More cbor update (#50)
Browse files Browse the repository at this point in the history
* Update packages used by COSE

* Push up the 1.2 build

Ready to drop a 1.2 version

* syntax error in travis

* Change the sdk version

* Remove unused line

* Change build matrix

* try a different 'nuget' for dotnet

* Stop trying to do a dotnet build for now

* Setup to release

Add more framewords to drop
Update bouncy castle
Move tests from nunit to mstest in the hope we can get multiple framework testing of cose.dll as some point.

* Ok - builds don't work

because of problems with nuget

* We are fialing builds on travis

let's try it on appveyor

* trigger build

* Need to move to right directory

* syntax err

* Try an older mono version

* Add examples pull

* Fix indenting

* Fixes for other directories around CBOR upgrade

* Additional work

* Add HssLms signature algorithm
* Add Countersign1 code
* Renaming of csproj files for JOSE
  • Loading branch information
jimsch authored Jun 28, 2020
1 parent 80636be commit f7561d1
Show file tree
Hide file tree
Showing 37 changed files with 4,311 additions and 2,555 deletions.
1 change: 1 addition & 0 deletions COSE/COSE.Std10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Copyright>Copyright @2016-9 Jim Schaad</Copyright>
<PackageTags>COSE CBOR</PackageTags>
<PackageLicenseExpression>EPL-2.0</PackageLicenseExpression>

<PackageReleaseNotes>
1.6.0
Expand Down
22 changes: 11 additions & 11 deletions COSE/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@ public bool Compare(OneKey key2)

if (_map[CoseKeyKeys.KeyType].Type == CBORType.TextString) {
switch (_map[CoseKeyKeys.KeyType].AsString()) {
case "HSS-LMS":
if (!CompareField(key2, CoseKeyParameterKeys.Lms_Public)) {
return false;
}
break;

default:
return true;
}
Expand Down Expand Up @@ -193,6 +187,13 @@ public bool Compare(OneKey key2)

break;

case GeneralValuesInt.KeyType_HssLms:
if (!CompareField(key2, CoseKeyParameterKeys.Lms_Public))
{
return false;
}
break;

default:
return true;
}
Expand Down Expand Up @@ -431,12 +432,7 @@ public OneKey PublicKey()
{
OneKey newKey = new OneKey();
if (_map[CoseKeyKeys.KeyType].Type == CBORType.TextString) {
if (_map[CoseKeyKeys.KeyType].AsString() == "HSS-LMS") {
newKey.Add(CoseKeyParameterKeys.Lms_Public, _map[CoseKeyParameterKeys.Lms_Public]);
}
else {
throw new CoseException("Key type unrecognized");
}
}
else {
switch ((GeneralValuesInt) _map[CoseKeyKeys.KeyType].AsInt16()) {
Expand All @@ -459,6 +455,10 @@ public OneKey PublicKey()
newKey.Add(CoseKeyParameterKeys.EC_X, _map[CoseKeyParameterKeys.EC_X]);
break;

case GeneralValuesInt.KeyType_HssLms:
newKey.Add(CoseKeyParameterKeys.Lms_Public, _map[CoseKeyParameterKeys.Lms_Public]);
break;

default:
throw new CoseException("Key type unrecognized");
}
Expand Down
8 changes: 5 additions & 3 deletions COSE/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public enum AlgorithmValuesInt

ECDH_ES_HKDF_256_AES_KW_128 = -29, ECDH_ES_HKDF_256_AES_KW_192 = -30, ECDH_ES_HKDF_256_AES_KW_256 = -31,
ECDH_SS_HKDF_256_AES_KW_128 = -32, ECDH_SS_HKDF_256_AES_KW_192 = -33, ECDH_SS_HKDF_256_AES_KW_256 = -34,

HSS_LMS = -46,
}

public class AlgorithmValues
Expand Down Expand Up @@ -160,7 +162,7 @@ public class AlgorithmValues
public static readonly CBORObject ECDH_SS_HKDF_256_AES_KW_192 = CBORObject.FromObject(AlgorithmValuesInt.ECDH_SS_HKDF_256_AES_KW_192);
public static readonly CBORObject ECDH_SS_HKDF_256_AES_KW_256 = CBORObject.FromObject(AlgorithmValuesInt.ECDH_SS_HKDF_256_AES_KW_256);

public static readonly CBORObject HSS_LMS_HASH = CBORObject.FromObject("HSS-LMS");
public static readonly CBORObject HSS_LMS_HASH = CBORObject.FromObject(AlgorithmValuesInt.HSS_LMS);
}

public class CoseKeyKeys
Expand Down Expand Up @@ -221,7 +223,7 @@ public class CoseKeyParameterKeys

public enum GeneralValuesInt
{
KeyType_OKP = 1, KeyType_EC2=2, KeyType_RSA=3, KeyType_Octet = 4,
KeyType_OKP = 1, KeyType_EC2=2, KeyType_RSA=3, KeyType_Octet = 4, KeyType_HssLms = 5,
P256=1, P384=2, P521=3, X25519=4, X448=5, Ed25519=6, Ed448=7
}

Expand All @@ -231,7 +233,7 @@ public class GeneralValues
public static readonly CBORObject KeyType_EC = CBORObject.FromObject(GeneralValuesInt.KeyType_EC2);
public static readonly CBORObject KeyType_RSA = CBORObject.FromObject(GeneralValuesInt.KeyType_RSA);
public static readonly CBORObject KeyType_Octet = CBORObject.FromObject(GeneralValuesInt.KeyType_Octet);
public static readonly CBORObject KeyType_HSS_LMS = CBORObject.FromObject("HSS-LMS");
public static readonly CBORObject KeyType_HSS_LMS = CBORObject.FromObject(GeneralValuesInt.KeyType_HssLms);
public static readonly CBORObject P256 = CBORObject.FromObject(GeneralValuesInt.P256);
public static readonly CBORObject P384 = CBORObject.FromObject(GeneralValuesInt.P384);
public static readonly CBORObject P521 = CBORObject.FromObject(GeneralValuesInt.P521);
Expand Down
46 changes: 24 additions & 22 deletions COSE/Sign0Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,6 @@ private byte[] _Sign(byte[] bytesToBeSigned)
digest2 = new Sha384Digest();
break;

case "HSS-LMS":
break;

default:
throw new CoseException("Unknown Algorithm Specified");
}
Expand Down Expand Up @@ -314,6 +311,9 @@ private byte[] _Sign(byte[] bytesToBeSigned)
case AlgorithmValuesInt.EdDSA:
break;

case AlgorithmValuesInt.HSS_LMS:
break;

default:
throw new CoseException("Unknown Algorithm Specified");
}
Expand All @@ -322,12 +322,6 @@ private byte[] _Sign(byte[] bytesToBeSigned)

if (alg.Type == CBORType.TextString) {
switch (alg.AsString()) {
case "HSS-LMS":
HashSig sig = new HashSig(_keyToSign[CoseKeyParameterKeys.Lms_Private].AsString());
byte[] signBytes = sig.Sign(bytesToBeSigned);

_keyToSign.Replace(CoseKeyParameterKeys.Lms_Private, CBORObject.FromObject(sig.PrivateKey));
return signBytes;

default:
throw new CoseException("Unknown Algorithm Specified");
Expand Down Expand Up @@ -377,10 +371,10 @@ private byte[] _Sign(byte[] bytesToBeSigned)
ECDsaSigner ecdsa = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest()));
ecdsa.Init(true, param);

BigInteger[] sig = ecdsa.GenerateSignature(digestedMessage);
BigInteger[] sigLms = ecdsa.GenerateSignature(digestedMessage);

byte[] r = sig[0].ToByteArrayUnsigned();
byte[] s = sig[1].ToByteArrayUnsigned();
byte[] r = sigLms[0].ToByteArrayUnsigned();
byte[] s = sigLms[1].ToByteArrayUnsigned();

int cbR = (p.Curve.FieldSize + 7) / 8;

Expand Down Expand Up @@ -417,7 +411,14 @@ private byte[] _Sign(byte[] bytesToBeSigned)
}
#endif

default:
case AlgorithmValuesInt.HSS_LMS:
HashSig sig = new HashSig(_keyToSign[CoseKeyParameterKeys.Lms_Private].AsString());
byte[] signBytes = sig.Sign(bytesToBeSigned);

_keyToSign.Replace(CoseKeyParameterKeys.Lms_Private, CBORObject.FromObject(sig.PrivateKey));
return signBytes;

default:
throw new CoseException("Unknown Algorithm Specified");
}
}
Expand Down Expand Up @@ -447,9 +448,6 @@ public bool Validate(OneKey signerKey)
digest2 = new Sha384Digest();
break;

case "HSS-LMS":
break;

default:
throw new CoseException("Unknown signature algorithm");
}
Expand Down Expand Up @@ -477,6 +475,9 @@ public bool Validate(OneKey signerKey)
case AlgorithmValuesInt.EdDSA:
break;

case AlgorithmValuesInt.HSS_LMS:
break;

default:
throw new CoseException("Unknown signature algorithm");
}
Expand All @@ -485,11 +486,6 @@ public bool Validate(OneKey signerKey)

if (alg.Type == CBORType.TextString) {
switch (alg.AsString()) {
case "HSS-LMS":
return HashSig.Validate(bytesToBeSigned,
signerKey[CoseKeyParameterKeys.Lms_Public].GetByteString(),
_rgbSignature);

default:
throw new CoseException("Unknown Algorithm");
}
Expand Down Expand Up @@ -554,7 +550,13 @@ public bool Validate(OneKey signerKey)
}
#endif

default:
case AlgorithmValuesInt.HSS_LMS:
return HashSig.Validate(bytesToBeSigned,
signerKey[CoseKeyParameterKeys.Lms_Public].GetByteString(),
_rgbSignature);


default:
throw new CoseException("Unknown Algorithm");
}
}
Expand Down
4 changes: 4 additions & 0 deletions COSE/SignMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public override CBORObject Encode()
}
}

if (CounterSigner1 != null) {
AddAttribute(HeaderKeys.CounterSignature0, CounterSigner1.EncodeToCBORObject(rgbProtected, rgbContent), UNPROTECTED);
}

if ((UnprotectedMap == null) || (UnprotectedMap.Count == 0)) obj.Add(CBORObject.NewMap());
else obj.Add(UnprotectedMap); // Add unprotected attributes

Expand Down
38 changes: 21 additions & 17 deletions COSE/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ protected byte[] toBeSigned(byte[] rgbContent, byte[] bodyAttributes)
cborProtected = CBORObject.FromObject(rgb);
}

if (rgbContent == null) {
rgbContent = new byte[0];
}

CBORObject signObj = CBORObject.NewArray();
signObj.Add(context);
signObj.Add(bodyAttributes);
Expand Down Expand Up @@ -293,9 +297,6 @@ public static byte[] Sign(byte[] toBeSigned, CBORObject alg, OneKey keyToSign)
digest2 = new Sha384Digest();
break;

case "HSS-LMS":
break;

default:
throw new Exception("Unknown signature algorithm");
}
Expand Down Expand Up @@ -323,6 +324,9 @@ public static byte[] Sign(byte[] toBeSigned, CBORObject alg, OneKey keyToSign)
case AlgorithmValuesInt.EdDSA:
break;

case AlgorithmValuesInt.HSS_LMS:
break;

default:
throw new CoseException("Unknown signature algorithm");
}
Expand All @@ -333,12 +337,6 @@ public static byte[] Sign(byte[] toBeSigned, CBORObject alg, OneKey keyToSign)

if (alg.Type == CBORType.TextString) {
switch (alg.AsString()) {
case "HSS-LMS":
HashSig sig = new HashSig(keyToSign[CoseKeyParameterKeys.Lms_Private].AsString());
byte[] signBytes = sig.Sign(toBeSigned);
keyToSign.Replace(CoseKeyParameterKeys.Lms_Private, CBORObject.FromObject(sig.PrivateKey));
return signBytes;

default:
throw new CoseException("Unknown Algorithm");
}
Expand Down Expand Up @@ -410,6 +408,12 @@ public static byte[] Sign(byte[] toBeSigned, CBORObject alg, OneKey keyToSign)
return eddsa.GenerateSignature();
}

case AlgorithmValuesInt.HSS_LMS:
HashSig sigHash = new HashSig(keyToSign[CoseKeyParameterKeys.Lms_Private].AsString());
byte[] signBytes = sigHash.Sign(toBeSigned);
keyToSign.Replace(CoseKeyParameterKeys.Lms_Private, CBORObject.FromObject(sigHash.PrivateKey));
return signBytes;

default:
throw new CoseException("Unknown Algorithm");
}
Expand Down Expand Up @@ -442,9 +446,6 @@ public static bool Validate(byte[] content, CBORObject alg, OneKey signKey, byte

if (alg.Type == CBORType.TextString) {
switch (alg.AsString()) {
case "HSS-LMS":
break;

default:
throw new Exception("Unknown signature algorithm");
}
Expand Down Expand Up @@ -472,6 +473,9 @@ public static bool Validate(byte[] content, CBORObject alg, OneKey signKey, byte
case AlgorithmValuesInt.EdDSA:
break;

case AlgorithmValuesInt.HSS_LMS:
break;

default:
throw new CoseException("Unknown signature algorith");
}
Expand All @@ -482,11 +486,6 @@ public static bool Validate(byte[] content, CBORObject alg, OneKey signKey, byte

if (alg.Type == CBORType.TextString) {
switch (alg.AsString()) {
case "HSS-LMS":
return HashSig.Validate(content,
signKey[CoseKeyParameterKeys.Lms_Public].GetByteString(),
rgbSignature);

default:
throw new CoseException("Unknown Algorithm");
}
Expand Down Expand Up @@ -550,6 +549,11 @@ public static bool Validate(byte[] content, CBORObject alg, OneKey signKey, byte
}
#endif

case AlgorithmValuesInt.HSS_LMS:
return HashSig.Validate(content,
signKey[CoseKeyParameterKeys.Lms_Public].GetByteString(),
rgbSignature);

default:
throw new CoseException("Unknown Algorithm");
}
Expand Down
3 changes: 2 additions & 1 deletion COSE/Tests/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Com.AugustCellars.COSE.Tests
{
[TestClass]
public class MessageTest
{
byte[] rgbKey128 = { (byte)'a', (byte)'b', (byte)'c', 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
Expand Down Expand Up @@ -92,7 +93,7 @@ public void testSetContent_String()
Message instance = new Encrypt0Message();
instance.SetContent(strData);
byte[] result = instance.GetContent();
Assert.AreEqual(result, (rgbData));
CollectionAssert.AreEqual(result, rgbData);
}
}
}
10 changes: 8 additions & 2 deletions COSE/Tests/Regression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public void ProcessDirectory()
#if !CHACHA20
if (di.Name == "chacha-poly-examples") continue;
#endif
if (di.Name == "X25519-tests") continue;
if (di.Name == ".git") continue;
if (di.Name == "anima") continue;
if (di.Name == "x509") continue;
ProcessDirectory(Path.Combine(directory.FullName, di.Name));
}
}
Expand Down Expand Up @@ -819,6 +820,11 @@ public void SetAttributes(Attributes msg, CBORObject cnAttributes, int which, Bo
cnValue = null;
break;

case "compressed":
cnKey = null;
cnValue = null;
break;

default:
throw new Exception("Attribute " + attr.AsString() + " is not part of SetAttributes");
}
Expand Down Expand Up @@ -998,7 +1004,7 @@ public byte[] HexStringToByteArray(String hex)

static CBORObject AlgorithmMap(CBORObject old)
{
if (old.Type == CBORType.Number) {
if (old.Type == CBORType.Integer) {
return old;
}

Expand Down
14 changes: 14 additions & 0 deletions JOSE/AlgorithmValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using PeterO.Cbor;

namespace Com.AugustCellars.JOSE
{
public class AlgorithmValues
{
public static readonly CBORObject AES_GCM_128 = CBORObject.FromObject("A128GCM");
public static readonly CBORObject AES_GCM_192 = CBORObject.FromObject("A128GCM");

public static readonly CBORObject ECDSA_256 = CBORObject.FromObject("ES256");

public static readonly CBORObject ECDH_ES_HKDF_256_AES_KW_128 = CBORObject.FromObject("ECDH-ES+A128KW");
}
}
Loading

0 comments on commit f7561d1

Please sign in to comment.