Exemplo n.º 1
0
 public I2PSessionConfig(I2PDestination dest, I2PMapping map, I2PDate date, I2PSigningPrivateKey privsignkey)
 {
     Destination       = dest;
     Map               = map != null ? map : new I2PMapping();
     Date              = date != null ? date : new I2PDate(DateTime.Now);
     PrivateSigningKey = privsignkey;
 }
Exemplo n.º 2
0
        public static byte[] DoSign(I2PSigningPrivateKey key, params BufLen[] bufs)
        {
            //Logging.LogDebug( "DoSign: " + key.Certificate.SignatureType.ToString() );

            switch (key.Certificate.SignatureType)
            {
            case I2PSigningKey.SigningKeyTypes.DSA_SHA1:
                return(DoSignDsaSha1(bufs, key));

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA256_P256:
                return(DoSignEcDsa(bufs, key, new Sha256Digest(), NistNamedCurves.GetByName("P-256"), 64));

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA384_P384:
                return(DoSignEcDsa(bufs, key, new Sha384Digest(), NistNamedCurves.GetByName("P-384"), 96));

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA512_P521:
                return(DoSignEcDsa(bufs, key, new Sha512Digest(), NistNamedCurves.GetByName("P-521"), 132));

            case I2PSigningKey.SigningKeyTypes.EdDSA_SHA512_Ed25519:
                return(DoSignEdDSASHA512Ed25519(bufs, key));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 3
0
 public I2PDestinationInfo(BufRef reader)
 {
     Certificate       = new I2PCertificate(reader);
     PrivateKey        = new I2PPrivateKey(reader, Certificate);
     PublicKey         = new I2PPublicKey(reader, Certificate);
     PrivateSigningKey = new I2PSigningPrivateKey(reader, Certificate);
     PublicSigningKey  = new I2PSigningPublicKey(reader, Certificate);
 }
Exemplo n.º 4
0
        public I2PDestinationInfo(string base64)
        {
            var reader = new BufRefLen(FreenetBase64.Decode(base64));

            Certificate       = new I2PCertificate(reader);
            PrivateKey        = new I2PPrivateKey(reader, Certificate);
            PublicKey         = new I2PPublicKey(reader, Certificate);
            PrivateSigningKey = new I2PSigningPrivateKey(reader, Certificate);
            PublicSigningKey  = new I2PSigningPublicKey(reader, Certificate);
        }
Exemplo n.º 5
0
 public I2PLeaseInfo(
     I2PPublicKey pubkey,
     I2PSigningPublicKey spubkey,
     I2PPrivateKey privkey,
     I2PSigningPrivateKey sprivkey)
 {
     PublicKey         = pubkey;
     PrivateKey        = privkey;
     PrivateSigningKey = sprivkey;
     PublicSigningKey  = spubkey;
 }
Exemplo n.º 6
0
        public I2PDestinationInfo(I2PSigningKey.SigningKeyTypes signkeytype)
        {
            Certificate = new I2PCertificate(signkeytype);
            var keys = I2PPrivateKey.GetNewKeyPair();

            PublicKey  = keys.PublicKey;
            PrivateKey = keys.PrivateKey;

            PrivateSigningKey = new I2PSigningPrivateKey(Certificate);
            PublicSigningKey  = new I2PSigningPublicKey(PrivateSigningKey);
        }
Exemplo n.º 7
0
 public I2PDestinationInfo(
     I2PCertificate cert,
     I2PPublicKey pubkey,
     I2PSigningPublicKey spubkey,
     I2PPrivateKey privkey,
     I2PSigningPrivateKey sprivkey)
 {
     Certificate       = cert;
     PublicKey         = pubkey;
     PrivateKey        = privkey;
     PrivateSigningKey = sprivkey;
     PublicSigningKey  = spubkey;
 }
Exemplo n.º 8
0
        public I2PSigningPublicKey(I2PSigningPrivateKey privkey)
            : base(privkey.Certificate)
        {
            switch (Certificate.SignatureType)
            {
            case SigningKeyTypes.DSA_SHA1:
                Key = new BufLen(I2PConstants.DsaG.ModPow(privkey.ToBigInteger(), I2PConstants.DsaP).ToByteArrayUnsigned());
                break;

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA256_P256:
            {
                var param  = NistNamedCurves.GetByName("P-256");
                var domain = new ECDomainParameters(param.Curve, param.G, param.N, param.H);

                var q           = domain.G.Multiply(privkey.ToBigInteger());
                var publicparam = new ECPublicKeyParameters(q, domain);
                Key = new BufLen(publicparam.Q.GetEncoded(), 1);
            }
            break;

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA384_P384:
            {
                var param  = NistNamedCurves.GetByName("P-384");
                var domain = new ECDomainParameters(param.Curve, param.G, param.N, param.H);

                var q           = domain.G.Multiply(privkey.ToBigInteger());
                var publicparam = new ECPublicKeyParameters(q, domain);
                Key = new BufLen(publicparam.Q.GetEncoded(), 1);
            }
            break;

            case I2PSigningKey.SigningKeyTypes.ECDSA_SHA512_P521:
            {
                var param  = NistNamedCurves.GetByName("P-521");
                var domain = new ECDomainParameters(param.Curve, param.G, param.N, param.H);

                var q           = domain.G.Multiply(privkey.ToBigInteger());
                var publicparam = new ECPublicKeyParameters(q, domain);
                Key = new BufLen(publicparam.Q.GetEncoded(), 1);
            }
            break;

            case SigningKeyTypes.EdDSA_SHA512_Ed25519:
                Key = new BufLen(Chaos.NaCl.Ed25519.PublicKeyFromSeed(privkey.Key.ToByteArray()));
                break;

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 9
0
        public static byte[] DoSignEcDsa(
            IEnumerable <BufLen> bufs,
            I2PSigningPrivateKey key,
            IDigest digest,
            X9ECParameters ecparam,
            int sigsize)
        {
            foreach (var buf in bufs)
            {
                digest.BlockUpdate(buf.BaseArray, buf.BaseArrayOffset, buf.Length);
            }
            var hash = new byte[digest.GetDigestSize()];

            digest.DoFinal(hash, 0);

            var param = new ECDomainParameters(ecparam.Curve, ecparam.G, ecparam.N, ecparam.H);
            var pk    = new ECPrivateKeyParameters(key.ToBigInteger(), param);

            var s = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner();

            s.Init(true, new ParametersWithRandom(pk));

            var sig    = s.GenerateSignature(hash);
            var result = new byte[sigsize];

            var b1 = sig[0].ToByteArrayUnsigned();
            var b2 = sig[1].ToByteArrayUnsigned();

            // https://geti2p.net/en/docs/spec/common-structures#type_Signature
            // When a signature is composed of two elements (for example values R,S),
            // it is serialized by padding each element to length/2 with leading zeros if necessary.
            // All types are Big Endian, except for EdDSA, which is stored and transmitted in a Little Endian format.

            // Pad msb. Big endian.
            Array.Copy(b1, 0, result, sigsize / 2 - b1.Length, b1.Length);
            Array.Copy(b2, 0, result, sigsize - b2.Length, b2.Length);

            DebugUtils.LogDebug("DoSignEcDsa: " + digest.ToString() + ": Used.");

            return(result);
        }
Exemplo n.º 10
0
        public static byte[] DoSignDsaSha1(IEnumerable <BufLen> bufs, I2PSigningPrivateKey key)
        {
            var sha = new Sha1Digest();

            foreach (var buf in bufs)
            {
                sha.BlockUpdate(buf.BaseArray, buf.BaseArrayOffset, buf.Length);
            }
            var hash = new byte[sha.GetDigestSize()];

            sha.DoFinal(hash, 0);

            var s = new Org.BouncyCastle.Crypto.Signers.DsaSigner();

            var dsaparams = new ParametersWithRandom(
                new DsaPrivateKeyParameters(
                    key.ToBigInteger(),
                    new DsaParameters(
                        I2PConstants.DsaP,
                        I2PConstants.DsaQ,
                        I2PConstants.DsaG)));

            s.Init(true, dsaparams);
            var sig    = s.GenerateSignature(hash);
            var result = new byte[40];

            var b1 = sig[0].ToByteArrayUnsigned();
            var b2 = sig[1].ToByteArrayUnsigned();

            // https://geti2p.net/en/docs/spec/common-structures#type_Signature
            // When a signature is composed of two elements (for example values R,S),
            // it is serialized by padding each element to length/2 with leading zeros if necessary.
            // All types are Big Endian, except for EdDSA, which is stored and transmitted in a Little Endian format.

            // Pad msb. Big endian.
            Array.Copy(b1, 0, result, 0 + 20 - b1.Length, b1.Length);
            Array.Copy(b2, 0, result, 20 + 20 - b2.Length, b2.Length);

            return(result);
        }
Exemplo n.º 11
0
        public static byte[] DoSignEcDsaSha256P256_old(IEnumerable <BufLen> bufs, I2PSigningPrivateKey key)
        {
            var sha = new Sha256Digest();

            foreach (var buf in bufs)
            {
                sha.BlockUpdate(buf.BaseArray, buf.BaseArrayOffset, buf.Length);
            }
            var hash = new byte[sha.GetDigestSize()];

            sha.DoFinal(hash, 0);

            var p     = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName("P-256");
            var param = new ECDomainParameters(p.Curve, p.G, p.N, p.H);
            var pk    = new ECPrivateKeyParameters(key.ToBigInteger(), param);

            var s = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner();

            s.Init(true, new ParametersWithRandom(pk));

            var sig    = s.GenerateSignature(hash);
            var result = new byte[64];

            var b1 = sig[0].ToByteArrayUnsigned();
            var b2 = sig[1].ToByteArrayUnsigned();

            // https://geti2p.net/en/docs/spec/common-structures#type_Signature
            // When a signature is composed of two elements (for example values R,S),
            // it is serialized by padding each element to length/2 with leading zeros if necessary.
            // All types are Big Endian, except for EdDSA, which is stored and transmitted in a Little Endian format.

            // Pad msb. Big endian.
            Array.Copy(b1, 0, result, 0 + 20 - b1.Length, b1.Length);
            Array.Copy(b2, 0, result, 20 + 20 - b2.Length, b2.Length);

            DebugUtils.LogDebug("DoSignEcDsaSha256P256: Used.");

            return(result);
        }
Exemplo n.º 12
0
 public static byte[] DoSignEdDSASHA512Ed25519(IEnumerable <BufLen> bufs, I2PSigningPrivateKey key)
 {
     return(Chaos.NaCl.Ed25519.Sign(bufs.SelectMany(b => b.ToByteArray()).ToArray(), key.ExpandedPrivateKey));
 }
Exemplo n.º 13
0
 public I2PDestinationInfo(BufRef reader)
 {
     Destination       = new I2PDestination(reader);
     PrivateKey        = new I2PPrivateKey(reader, Destination.Certificate);
     PrivateSigningKey = new I2PSigningPrivateKey(reader, Destination.Certificate);
 }