/// <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); } }
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))); } }
public void testGenerateKey() { /* foreach */ foreach (TestTpmPrivateKey.KeyTestData dataSet in keyTestData) { TpmPrivateKey key = net.named_data.jndn.security.tpm.TpmPrivateKey .generatePrivateKey(dataSet.keyParams); Blob publicKeyBits = key.derivePublicKey(); Blob data = new Blob(new int[] { 0x01, 0x02, 0x03, 0x04 }); // Sign and verify. Blob signature = key.sign(data.buf(), net.named_data.jndn.security.DigestAlgorithm.SHA256); bool result = net.named_data.jndn.security.VerificationHelpers.verifySignature(data, signature, new PublicKey(publicKeyBits)); Assert.AssertTrue(result); // Check that another generated private key is different. TpmPrivateKey key2 = net.named_data.jndn.security.tpm.TpmPrivateKey .generatePrivateKey(dataSet.keyParams); Assert.AssertTrue(!key.toPkcs8().equals(key2.toPkcs8())); } }