Esempio n. 1
0
        public void nullKey()
        {
            Sign1Message msg = new Sign1Message();
            OneKey       key = null;

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.ECDSA_256, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            Assert.ThrowsException <NullReferenceException>(() =>
                                                            msg.Sign(key));
        }
Esempio n. 2
0
        public void noContent()
        {
            Sign1Message msg = new Sign1Message();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.ECDSA_256, Attributes.PROTECTED);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Sign(cnKeyPrivate));

            Assert.AreEqual(e.Message, ("No Content Specified"));
        }
Esempio n. 3
0
        public void noAlgorithm()
        {
            Sign1Message msg = new Sign1Message();

            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Sign(cnKeyPrivate));

            Assert.AreEqual(e.Message, ("No Algorithm Specified"));
        }
Esempio n. 4
0
        public void unsupportedAlgorithm()
        {
            Sign1Message msg = new Sign1Message();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Sign(cnKeyPrivate));

            Assert.AreEqual(e.Message, ("Unknown Algorithm Specified"));
        }
Esempio n. 5
0
        public void unknownAlgorithm()
        {
            Sign1Message msg = new Sign1Message();

            msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Sign(cnKeyPrivate));

            Assert.AreEqual(e.Message, ("Unknown Algorithm Specified"));
        }
Esempio n. 6
0
        public void publicKey()
        {
            Sign1Message msg = new Sign1Message();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.ECDSA_256, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Sign(cnKeyPublic));

            Assert.AreEqual(e.Message, ("Private key required to sign"));
        }
Esempio n. 7
0
        public void testRoundTrip()
        {
            Sign1Message msg = new Sign1Message();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.ECDSA_256, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            msg.Sign(cnKeyPrivate);
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (Sign1Message)Message.DecodeFromBytes(rgbMsg, Tags.Sign1);
            Boolean f = msg.Validate(cnKeyPublic);

            Assert.AreEqual(f, (true));
        }
Esempio n. 8
0
        public byte[] CreateMessage3()
        {
            CBORObject msg = CBORObject.NewArray();

            if (_fSymmetricSecret)
            {
                msg.Add(6);
            }
            else
            {
                msg.Add(3);
            }
            msg.Add(_SessionId[1]);

            byte[] aad_3 = ConcatenateAndHash(new byte[2][] { _LastMessageAuthenticator, msg.EncodeToBytes() }, _MessageDigest);

            byte[] signBody = new byte[0];
            if (!_fSymmetricSecret)
            {
                Sign1Message sign1 = new Sign1Message(false, false);
                sign1.SetContent(aad_3);
                sign1.AddAttribute(HeaderKeys.Algorithm, _algSign, Attributes.DO_NOT_SEND);
                sign1.AddAttribute(HeaderKeys.KeyId, _SigningKey[CoseKeyKeys.KeyIdentifier], Attributes.UNPROTECTED);
                sign1.Sign(_SigningKey);

                CBORObject obj = CBORObject.NewArray();
                obj.Add(sign1.EncodeToBytes());

                signBody = obj.EncodeToBytes();
            }

            byte[][] encKeys = _DeriveKeys(_Keys, _SecretSalt, aad_3, _algAEAD);

            Encrypt0Message enc = new Encrypt0Message(false);

            enc.SetContent(signBody);
            enc.SetExternalData(aad_3);
            enc.AddAttribute(HeaderKeys.Algorithm, _algAEAD, Attributes.DO_NOT_SEND);
            enc.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(encKeys[1]), Attributes.DO_NOT_SEND);
            enc.Encrypt(encKeys[0]);

            msg.Add(enc.EncodeToBytes());

            byte[] msgOut = msg.EncodeToBytes();

            _LastMessageAuthenticator = ConcatenateAndHash(new byte[2][] { _LastMessageAuthenticator, msgOut }, _MessageDigest);

            return(msgOut);
        }
Esempio n. 9
0
        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]);
        }