コード例 #1
0
        // 30 1d
        //   30 13
        //     06 0b 2a 86 48 86 f7 0d 01 09 10 03 06          ; 3DES wrap OID
        //     04 04
        //       00 00 00 01                                   ; Counter
        //                                                     ; a0 omitted (not present), PartyAInfo (Optional)
        //   a2 06                                             ; a2 is flag for supplemental info (keyLength)
        //     04 04
        //       00 00 00 c0                                   ; KeyLen (192 bits in this example)

        // 30 61
        //   30 13
        //     06 0b 2a 86 48 86 f7 0d 01 09 10 03 06
        //     04 04
        //       00 00 00 01
        //   a0 42
        //     04 40 ff e9 be b1 2d 30 aa a6 a6 2d 7f 6e eb 1c f3 bb 43 d4 44 53 4d 61 b4 4f f5 d6 67 5b f5 11 dd b5 d8 4a 55 24 64 fa 40 f8 3e cd 07 29 9c 18 04 b9 37 6d 4a c0 90 12 aa 0b 7a 51 a4 ea 96 ab ae f6
        //   a2 06
        //     04 04
        //       00 00 00 c0

        // 30 1D
        //   30 13
        //     06 0B 2A 86 48 86 F7 0D 01 09 10 03 06
        //     04 04
        //       00 00 00 01
        //   A2 06
        //     04 04
        //       00 00 01 00

        private BitString DerEncode(DerAns942Parameters derParams, BitString counter)
        {
            // Octet type || 4 bytes || value
            var counterOctet = AnsDerEncodingHelper.EncodeOctet(counter);

            // Concatenate OID and Counter as a sequence
            var algorithmIdentifier = AnsDerEncodingHelper.EncodeSequence(derParams.Oid.ConcatenateBits(counterOctet));

            // All optional information
            // a0-a3 || len || value
            var wrappedOtherInfo = new BitString(0);

            if (derParams.PartyUInfo.BitLength > 0)
            {
                wrappedOtherInfo = AnsDerEncodingHelper.EncodePartyUInfo(derParams.PartyUInfo);
            }
            if (derParams.PartyVInfo.BitLength > 0)
            {
                wrappedOtherInfo = BitString.ConcatenateBits(wrappedOtherInfo, AnsDerEncodingHelper.EncodePartyVInfo(derParams.PartyVInfo));
            }
            if (derParams.SuppPubInfo.BitLength > 0)
            {
                wrappedOtherInfo = BitString.ConcatenateBits(wrappedOtherInfo, AnsDerEncodingHelper.EncodeSuppPubInfo(derParams.SuppPubInfo));
            }
            if (derParams.SuppPrivInfo.BitLength > 0)
            {
                wrappedOtherInfo = BitString.ConcatenateBits(wrappedOtherInfo, AnsDerEncodingHelper.EncodeSuppPrivInfo(derParams.SuppPrivInfo));
            }

            // OuterWrapper (Sequence) || length || content
            var fullEncoding = AnsDerEncodingHelper.EncodeSequence(algorithmIdentifier.ConcatenateBits(wrappedOtherInfo));

            return(fullEncoding);
        }
コード例 #2
0
        public void ShouldKdfCorrectly()
        {
            var sha     = new NativeShaFactory().GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256));
            var subject = new AnsiX942Der(sha);

            var zz        = new BitString("0123456789ABCDEF");
            var otherInfo = new BitString(0);

            var param = new DerAns942Parameters
            {
                Zz           = zz,
                KeyLen       = 256,
                Oid          = AnsiX942OidHelper.GetOidFromEnum(AnsiX942Oids.TDES),
                PartyUInfo   = otherInfo,
                PartyVInfo   = otherInfo,
                SuppPubInfo  = otherInfo,
                SuppPrivInfo = otherInfo
            };

            var result = subject.DeriveKey(param);

            Assert.IsTrue(result.Success);

            Console.WriteLine(result.DerivedKey.ToHex());
            Assert.Pass();
        }
コード例 #3
0
        public void ShouldGenerateCorrectOtherInfo()
        {
            var sha     = new NativeShaFactory().GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256));
            var subject = new AnsiX942Der(sha);

            var expectedKey = new BitString("7088B27511516F85551F20B33BB09AF453DD0ECA4542C5F48D5263D3474FC0C4");
            var param       = new DerAns942Parameters
            {
                Zz           = new BitString("6B"),
                KeyLen       = 256,
                Oid          = AnsiX942OidHelper.GetOidFromEnum(AnsiX942Oids.TDES),
                PartyUInfo   = new BitString("299D468D60BC6A257E0B6523D691A3FC1602453B35F308C762FBBAC6069A88BC"),
                PartyVInfo   = new BitString("80D49BFE5BE01C7D56489AB017663C22B8CBB34C3174D1D71F00CB7505AC759A"),
                SuppPubInfo  = new BitString("3C21A5EA5988562C007986E0503D039E7231D9F152FE72A231A1FD98C59BCA6A"),
                SuppPrivInfo = new BitString("FD47477542989B51E4A0845DFABD6EEAA465F69B3D75349B2520051782C7F3FC")
            };

            var result = subject.DeriveKey(param);

            Assert.IsTrue(result.Success);
        }