Beispiel #1
0
        /// <summary>
        /// Generate a new random decrypt key for AES based on the given params.
        /// </summary>
        ///
        /// <param name="params">The key params with the key size (in bits).</param>
        /// <returns>The new decrypt key.</returns>
        public static DecryptKey generateKey(AesKeyParams paras)
        {
            // Convert the key bit size to bytes.
            ByteBuffer key = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(paras.getKeySize() / 8);
            net.named_data.jndn.util.Common.getRandom().nextBytes(key.array());

            DecryptKey decryptKey = new DecryptKey(new Blob(key, false));
            return decryptKey;
        }
        public void testKeyGeneration()
        {
            AesKeyParams keyParams = new AesKeyParams(128);
            DecryptKey decryptKey = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(keyParams);
            EncryptKey encryptKey = net.named_data.jndn.encrypt.algo.AesAlgorithm.deriveEncryptKey(decryptKey
                    .getKeyBits());

            Blob plainBlob = new Blob(PLAINTEXT, false);

            // Encrypt/decrypt data in AES_CBC with auto-generated IV.
            EncryptParams encryptParams = new EncryptParams(
                    net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb, 16);
            Blob cipherBlob = net.named_data.jndn.encrypt.algo.AesAlgorithm.encrypt(encryptKey.getKeyBits(),
                    plainBlob, encryptParams);
            Blob receivedBlob = net.named_data.jndn.encrypt.algo.AesAlgorithm.decrypt(decryptKey.getKeyBits(),
                    cipherBlob, encryptParams);
            Assert.AssertTrue(receivedBlob.equals(plainBlob));
        }
        internal static void generateAesKeys(Blob[] encryptionKeyBlob,
				Blob[] decryptionKeyBlob)
        {
            AesKeyParams paras = new AesKeyParams();
            DecryptKey memberDecryptKey = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(paras);
            decryptionKeyBlob[0] = memberDecryptKey.getKeyBits();
            EncryptKey memberEncryptKey = net.named_data.jndn.encrypt.algo.AesAlgorithm
                    .deriveEncryptKey(decryptionKeyBlob[0]);
            encryptionKeyBlob[0] = memberEncryptKey.getKeyBits();
        }
        public void testDatabaseFunctions()
        {
            // Test construction.
            ProducerDb database = new Sqlite3ProducerDb(System.IO.Path.GetFullPath(databaseFilePath.Name));

            // Create member.
            AesKeyParams // Create member.
                    paras = new AesKeyParams(128);
            Blob keyBlob1 = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(paras).getKeyBits();
            Blob keyBlob2 = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(paras).getKeyBits();

            double point1 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150101T100000");
            double point2 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150102T100000");
            double point3 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150103T100000");
            double point4 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150104T100000");

            // Add keys into the database.
            database.addContentKey(point1, keyBlob1);
            database.addContentKey(point2, keyBlob1);
            database.addContentKey(point3, keyBlob2);

            // Throw an exception when adding a key to an existing time slot.
            try {
                database.addContentKey(point1, keyBlob1);
                Fail("addContentKey did not throw an exception");
            } catch (ProducerDb.Error ex) {
            } catch (Exception ex_0) {
                Fail("addContentKey did not throw an exception");
            }

            // Check has functions.
            AssertEquals(true, database.hasContentKey(point1));
            AssertEquals(true, database.hasContentKey(point2));
            AssertEquals(true, database.hasContentKey(point3));
            AssertEquals(false, database.hasContentKey(point4));

            // Get content keys.
            Blob keyResult = database.getContentKey(point1);
            AssertTrue(keyResult.equals(keyBlob1));

            keyResult = database.getContentKey(point3);
            AssertTrue(keyResult.equals(keyBlob2));

            // Throw exception when there is no such time slot in the database.
            try {
                database.getContentKey(point4);
                Fail("getContentKey did not throw an exception");
            } catch (ProducerDb.Error ex_1) {
            } catch (Exception ex_2) {
                Fail("getContentKey did not throw an exception");
            }

            // Delete content keys.
            AssertEquals(true, database.hasContentKey(point1));
            database.deleteContentKey(point1);
            AssertEquals(false, database.hasContentKey(point1));

            // Delete at a non-existing time slot.
            try {
                database.deleteContentKey(point4);
            } catch (Exception ex_3) {
                Fail("deleteContentKey threw an exception");
            }
        }
Beispiel #5
0
        /// <summary>
        /// Create the content key corresponding to the timeSlot. This first checks if
        /// the content key exists. For an existing content key, this returns the
        /// content key name directly. If the key does not exist, this creates one and
        /// encrypts it using the corresponding E-KEYs. The encrypted content keys are
        /// passed to the onEncryptedKeys callback.
        /// </summary>
        ///
        /// <param name="timeSlot_0">The time slot as milliseconds since Jan 1, 1970 UTC.</param>
        /// <param name="onEncryptedKeys_1">content key Data packets. If onEncryptedKeys is null, this does not use it. NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.</param>
        /// <param name="onError_2">better error handling the callback should catch and properly handle any exceptions.</param>
        /// <returns>The content key name.</returns>
        public Name createContentKey(double timeSlot_0,
				Producer.OnEncryptedKeys  onEncryptedKeys_1, net.named_data.jndn.encrypt.EncryptError.OnError  onError_2)
        {
            double hourSlot = getRoundedTimeSlot(timeSlot_0);

            // Create the content key name.
            Name contentKeyName = new Name(namespace_);
            contentKeyName.append(net.named_data.jndn.encrypt.algo.Encryptor.NAME_COMPONENT_C_KEY);
            contentKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(hourSlot));

            Blob contentKeyBits;

            // Check if we have created the content key before.
            if (database_.hasContentKey(timeSlot_0))
                // We have created the content key. Return its name directly.
                return contentKeyName;

            // We haven't created the content key. Create one and add it into the database.
            AesKeyParams aesParams = new AesKeyParams(128);
            contentKeyBits = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(aesParams).getKeyBits();
            database_.addContentKey(timeSlot_0, contentKeyBits);

            // Now we need to retrieve the E-KEYs for content key encryption.
            double timeCount = Math.Round(timeSlot_0,MidpointRounding.AwayFromZero);
            ILOG.J2CsMapping.Collections.Collections.Put(keyRequests_,timeCount,new Producer.KeyRequest (eKeyInfo_.Count));
            Producer.KeyRequest  keyRequest = (Producer.KeyRequest ) ILOG.J2CsMapping.Collections.Collections.Get(keyRequests_,timeCount);

            // Check if the current E-KEYs can cover the content key.
            Exclude timeRange = new Exclude();
            excludeAfter(timeRange,
                    new Name.Component(net.named_data.jndn.encrypt.Schedule.toIsoString(timeSlot_0)));
            new ILOG.J2CsMapping.Collections.IteratorAdapter(eKeyInfo_.GetEnumerator());
            for (IIterator i = new ILOG.J2CsMapping.Collections.IteratorAdapter(eKeyInfo_.GetEnumerator()); i.HasNext();) {
                // For each current E-KEY.
                DictionaryEntry entry = (DictionaryEntry) i.Next();
                Producer.KeyInfo  keyInfo = (Producer.KeyInfo ) ((DictionaryEntry) entry).Value;
                if (timeSlot_0 < keyInfo.beginTimeSlot
                        || timeSlot_0 >= keyInfo.endTimeSlot) {
                    // The current E-KEY cannot cover the content key, so retrieve one.
                    ILOG.J2CsMapping.Collections.Collections.Put(keyRequest.repeatAttempts,((DictionaryEntry) entry).Key,0);
                    sendKeyInterest(
                            new Interest((Name) ((DictionaryEntry) entry).Key).setExclude(
                                    timeRange).setChildSelector(1), timeSlot_0,
                            onEncryptedKeys_1, onError_2);
                } else {
                    // The current E-KEY can cover the content key.
                    // Encrypt the content key directly.
                    Name eKeyName = new Name((Name) ((DictionaryEntry) entry).Key);
                    eKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(keyInfo.beginTimeSlot));
                    eKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(keyInfo.endTimeSlot));
                    encryptContentKey(keyInfo.keyBits, eKeyName, timeSlot_0,
                            onEncryptedKeys_1, onError_2);
                }
            }

            return contentKeyName;
        }