예제 #1
0
        public void testRsaDecryption()
        {
            TestTpmPrivateKey.KeyTestData dataSet = rsaKeyTestData;

            byte[]        pkcs8 = net.named_data.jndn.util.Common.base64Decode(dataSet.privateKeyPkcs8Unencrypted);
            TpmPrivateKey key   = new TpmPrivateKey();

            key.loadPkcs8(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs8));

            Blob plainText = new Blob(new int[] { 0x00, 0x01, 0x02, 0x03, 0x04,
                                                  0x05, 0x06, 0x07 });

            String cipherTextBase64 = "i2XNpZ2JbLa4JmBTdDrGmsd4/0C+p+BSCpW3MuPBNe5uChQ0eRO1dvjTnEqwSECY\n"
                                      + "38en9JZwcyb0It/TSFNXHlq+Z1ZpffnjIJxQR9HcgwvwQJh6WRH0vu38tvGkGuNv\n"
                                      + "60Rdn85hqSy1CikmXCeWXL9yCqeqcP21R94G/T3FuA+c1FtFko8KOzCwvrTXMO6n\n"
                                      + "5PNsqlLXabSGr+jz4EwOsSCgPkiDf9U6tXoSPRA2/YvqFQdaiUXIVlomESvaqqZ8\n"
                                      + "FxPs2BON0lobM8gT+xdzbRKofp+rNjNK+5uWyeOnXJwzCszh17cdJl2BH1dZwaVD\n"
                                      + "PmTiSdeDQXZ94U5boDQ4Aw==\n";

            byte[] cipherText = net.named_data.jndn.util.Common.base64Decode(cipherTextBase64);

            Blob decryptedText = key.decrypt(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(cipherText));

            Assert.AssertTrue(decryptedText.equals(plainText));
        }
예제 #2
0
        /// <summary>
        /// Decrypt the encryptedData using the keyBits according the encrypt params.
        /// </summary>
        ///
        /// <param name="keyBits">The key value (PKCS8-encoded private key).</param>
        /// <param name="encryptedData">The data to decrypt.</param>
        /// <param name="params">This decrypts according to params.getAlgorithmType().</param>
        /// <returns>The decrypted data.</returns>
        public static Blob decrypt(Blob keyBits, Blob encryptedData,
                                   EncryptParams paras)
        {
            TpmPrivateKey privateKey = new TpmPrivateKey();

            try {
                privateKey.loadPkcs8(keyBits.buf());
            } catch (TpmPrivateKey.Error ex) {
                throw new SecurityException("decrypt: Error in loadPkcs8: " + ex);
            }

            try {
                return(privateKey.decrypt(encryptedData.buf(),
                                          paras.getAlgorithmType()));
            } catch (TpmPrivateKey.Error ex_0) {
                throw new SecurityException("decrypt: Error in decrypt: " + ex_0);
            }
        }
예제 #3
0
        /// <summary>
        /// Derive a new encrypt key from the given decrypt key value.
        /// </summary>
        ///
        /// <param name="keyBits"></param>
        /// <returns>The new encrypt key (DER-encoded public key).</returns>
        public static EncryptKey deriveEncryptKey(Blob keyBits)
        {
            TpmPrivateKey privateKey = new TpmPrivateKey();

            try {
                privateKey.loadPkcs8(keyBits.buf());
            } catch (TpmPrivateKey.Error ex) {
                throw new SecurityException(
                          "deriveEncryptKey: Error in loadPkcs8: " + ex);
            }

            try {
                return(new EncryptKey(privateKey.derivePublicKey()));
            } catch (TpmPrivateKey.Error ex_0) {
                throw new SecurityException(
                          "deriveEncryptKey: Error in derivePublicKey: " + ex_0);
            }
        }
예제 #4
0
        public void testDerivePublicKey()
        {
            /* foreach */
            foreach (TestTpmPrivateKey.KeyTestData dataSet  in  keyTestData)
            {
                byte[] pkcs8 = net.named_data.jndn.util.Common
                               .base64Decode(dataSet.privateKeyPkcs8Unencrypted);
                TpmPrivateKey key = new TpmPrivateKey();
                try {
                    key.loadPkcs8(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs8));
                } catch (Exception ex) {
                    Assert.Fail("Unexpected exception: " + ex.Message);
                }

                // Derive the public key and compare.
                Blob   publicKeyBits = key.derivePublicKey();
                byte[] expected      = net.named_data.jndn.util.Common.base64Decode(dataSet.publicKeyEncoding);
                Assert.AssertTrue(publicKeyBits.equals(new Blob(expected)));
            }
        }
예제 #5
0
        public void testSaveLoad()
        {
            /* foreach */
            foreach (TestTpmPrivateKey.KeyTestData dataSet  in  keyTestData)
            {
                // Load the key in PKCS #1 format.
                byte[]        pkcs1 = net.named_data.jndn.util.Common.base64Decode(dataSet.privateKeyPkcs1);
                TpmPrivateKey key1  = new TpmPrivateKey();
                try {
                    key1.loadPkcs1(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs1));
                } catch (Exception ex) {
                    Assert.Fail("Unexpected exception: " + ex.Message);
                }

                // Save the key in PKCS #1 format.
                Blob savedPkcs1Key = null;
                try {
                    savedPkcs1Key = key1.toPkcs1();
                } catch (Exception ex_0) {
                    Assert.Fail("Unexpected exception: " + ex_0.Message);
                }
                Assert.AssertTrue(savedPkcs1Key.equals(new Blob(pkcs1)));

                // Load the key in unencrypted PKCS #8 format.
                byte[] pkcs8 = net.named_data.jndn.util.Common
                               .base64Decode(dataSet.privateKeyPkcs8Unencrypted);
                TpmPrivateKey key8 = new TpmPrivateKey();
                try {
                    key8.loadPkcs8(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs8));
                } catch (Exception ex_1) {
                    Assert.Fail("Unexpected exception: " + ex_1.Message);
                }

                // Save the key in unencrypted PKCS #8 format.
                Blob savedPkcs8Key = null;
                try {
                    savedPkcs8Key = key8.toPkcs8();
                } catch (Exception ex_2) {
                    Assert.Fail("Unexpected exception: " + ex_2.Message);
                }
                Assert.AssertTrue(savedPkcs8Key.equals(new Blob(pkcs8)));

                ByteBuffer password = new Blob("password").buf();

                // Load the key in encrypted PKCS #8 format.
                byte[] encryptedPkcs8 = net.named_data.jndn.util.Common
                                        .base64Decode(dataSet.privateKeyPkcs8);
                TpmPrivateKey encryptedKey8 = new TpmPrivateKey();
                try {
                    encryptedKey8.loadEncryptedPkcs8(
                        ILOG.J2CsMapping.NIO.ByteBuffer.wrap(encryptedPkcs8), password);
                } catch (Exception ex_3) {
                    Assert.Fail("Unexpected exception: " + ex_3.Message);
                }

                // Save the key in encrypted PKCS #8 format and resave as unencrypted.
                Blob savedEncryptedPkcs8Key = null;
                try {
                    savedEncryptedPkcs8Key = encryptedKey8
                                             .toEncryptedPkcs8(password);
                } catch (Exception ex_4) {
                    Assert.Fail("Unexpected exception: " + ex_4.Message);
                }
                key8 = new TpmPrivateKey();
                key8.loadEncryptedPkcs8(savedEncryptedPkcs8Key.buf(), password);
                Blob resavedPkcs8Key = key8.toPkcs8();
                Assert.AssertTrue(resavedPkcs8Key.equals(new Blob(pkcs8)));
            }
        }