public void EncryptNoTag() { Encrypt0Message msg = new Encrypt0Message(false, true); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED); msg.SetContent(rgbContent); msg.Encrypt(rgbKey128); CBORObject cn = msg.EncodeToCBORObject(); Assert.AreEqual(cn.IsTagged, (false)); }
public void encryptNoEmitContent() { Encrypt0Message msg = new Encrypt0Message(true, false); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.UNPROTECTED); msg.SetContent(strContent); msg.Encrypt(rgbKey128); CBORObject cn = msg.EncodeToCBORObject(); Assert.IsTrue(cn[2].IsNull); }
public void TestRoundTrip4() { Encrypt0Message msg = new Encrypt0Message(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED); msg.SetContent(rgbContent); msg.Encrypt(rgbKey128); CBORObject rgbMsg = msg.EncodeToCBORObject(); msg = (Encrypt0Message)Message.DecodeFromCBOR(rgbMsg); byte[] contentNew = msg.Decrypt(rgbKey128); CollectionAssert.AreEqual(rgbContent, (contentNew)); }
private static byte[] DoCompression(Encrypt0Message msg) { // Start with 0abc deee // a, b, c and d are presence flags // eee is the length of the iv field. int cbSize = 1; // CBORObject partialIV = msg.FindAttribute(HeaderKeys.PartialIV); byte[] iv = new byte[0]; if (partialIV != null) { iv = partialIV.GetByteString(); } cbSize += iv.Length; byte head = (byte)iv.Length; // Context Hint/Group ID CBORObject gid = msg.FindAttribute(CBORObject.FromObject("gid")); if (gid != null) { cbSize += (1 + gid.GetByteString().Length); head |= 0x10; } CBORObject sig = msg.FindAttribute(HeaderKeys.CounterSignature); byte[] sigBytes = null; if (sig != null) { sigBytes = sig.EncodeToBytes(); cbSize += (1 + sigBytes.Length); head |= 0x20; } CBORObject kid = msg.FindAttribute(HeaderKeys.KeyId); if (kid != null) { cbSize += (0 + kid.GetByteString().Length); head |= 0x08; } // Additional items to flag byte[] encBody = new byte[cbSize]; encBody[0] = head; cbSize = 1; if (iv.Length > 0) { Array.Copy(iv, 0, encBody, cbSize, iv.Length); cbSize += iv.Length; } if (gid != null) { if (gid.GetByteString().Length > 255) { throw new Exception("GID too large"); } encBody[cbSize] = (byte)gid.GetByteString().Length; Array.Copy(gid.GetByteString(), 0, encBody, cbSize + 1, gid.GetByteString().Length); cbSize += gid.GetByteString().Length + 1; } if (sig != null) { if (sigBytes.Length > 255) { throw new Exception("SIG too large"); } encBody[cbSize] = (byte)sigBytes.Length; Array.Copy(sigBytes, 0, encBody, cbSize + 1, sig.GetByteString().Length); cbSize += sigBytes.Length + 1; } if (kid != null) { if (kid.GetByteString().Length > 255) { throw new Exception("KID too large"); } Array.Copy(kid.GetByteString(), 0, encBody, cbSize, kid.GetByteString().Length); } #if DEBUG { CBORObject xxx = msg.EncodeToCBORObject(); Console.WriteLine("Protected Attributes = " + BitConverter.ToString(xxx[0].GetByteString())); } #endif return(encBody); }
public byte[] CreateMessage2() { CBORObject msg = CBORObject.NewArray(); CBORObject obj; if (_fSymmetricSecret) { msg.Add(5); // Msg Type } else { msg.Add(2); } msg.Add(_SessionId[1]); // S_U msg.Add(_SessionId[0]); // S_V msg.Add(_Nonce[0]); // N_V msg.Add(_Keys[0].PublicKey().AsCBOR()); // E_V msg.Add(_algKeyAgree); // HKDF_V msg.Add(_algAEAD); // AEAD_V if (!_fSymmetricSecret) { msg.Add(_algSign); // SIG_V obj = CBORObject.NewArray(); obj.Add(AlgorithmValuesInt.ECDSA_256); obj.Add(AlgorithmValues.EdDSA); msg.Add(obj); // SIGs_V } if (_algKeyAgree.Equals(COSE.AlgorithmValues.ECDH_SS_HKDF_256)) { _MessageDigest = new Sha256Digest(); } byte[] data2 = msg.EncodeToBytes(); byte[] aad_2 = ConcatenateAndHash(new byte[2][] { _Messages[0], data2 }, _MessageDigest); // M00TODO - hash message[0] before passing it in. byte[][] useKeys = _DeriveKeys(_Keys, _SecretSalt, aad_2, _algAEAD); byte[] aeadKey = useKeys[0]; byte[] signResult = new byte[0]; if (!_fSymmetricSecret) { Sign1Message sign1 = new Sign1Message(false, false); sign1.SetContent(aad_2); sign1.AddAttribute(HeaderKeys.KeyId, _SigningKey[CoseKeyKeys.KeyIdentifier], Attributes.UNPROTECTED); sign1.AddAttribute(HeaderKeys.Algorithm, _algSign, Attributes.DO_NOT_SEND); sign1.Sign(_SigningKey); signResult = sign1.EncodeToBytes(); } Encrypt0Message enc0 = new Encrypt0Message(true); enc0.AddAttribute(HeaderKeys.Algorithm, _algAEAD, Attributes.DO_NOT_SEND); enc0.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(useKeys[1]), Attributes.DO_NOT_SEND); enc0.SetExternalData(aad_2); CBORObject encContent = CBORObject.NewArray(); encContent.Add(signResult); enc0.SetContent(encContent.EncodeToBytes()); enc0.Encrypt(aeadKey); msg.Add(enc0.EncodeToCBORObject()); // COSE_ENC_2 _Messages[1] = msg.EncodeToBytes(); _LastMessageAuthenticator = ConcatenateAndHash(new byte[][] { _Messages[0], _Messages[1] }, _MessageDigest); return(_Messages[1]); }