Exemplo n.º 1
0
        /// <summary>
        /// Loads a RelinKeys from an input stream overwriting the current RelinKeys.
        /// No checking of the validity of the RelinKeys data against encryption
        /// parameters is performed. This function should not be used unless the
        /// RelinKeys comes from a fully trusted source.
        /// </summary>
        /// <param name="stream">The stream to load the RelinKeys from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if valid RelinKeys could not be read
        /// from stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            try
            {
                // Read the ParmsId
                ParmsId parmsId = new ParmsId();
                parmsId.Load(stream);
                ParmsId = parmsId;

                using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true))
                {
                    // Read the decomposition bit count
                    int dbc = reader.ReadInt32();
                    DecompositionBitCount = dbc;

                    // Read the size
                    ulong size = reader.ReadUInt64();

                    // Clear current data and reserve new size
                    NativeMethods.RelinKeys_ClearDataAndReserve(NativePtr, size);

                    // Read all lists
                    for (ulong i = 0; i < size; i++)
                    {
                        // Read size of second list
                        ulong             keySize = reader.ReadUInt64();
                        List <Ciphertext> ciphers = new List <Ciphertext>((int)keySize);

                        // Load all ciphertexts
                        for (ulong j = 0; j < keySize; j++)
                        {
                            Ciphertext cipher = new Ciphertext();
                            cipher.UnsafeLoad(reader.BaseStream);
                            ciphers.Add(cipher);
                        }

                        IntPtr[] pointers = ciphers.Select(c =>
                        {
                            return(c.NativePtr);
                        }).ToArray();

                        NativeMethods.RelinKeys_AddKeyList(NativePtr, (ulong)pointers.LongLength, pointers);
                    }
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new ArgumentException("Stream ended unexpectedly", ex);
            }
            catch (IOException ex)
            {
                throw new ArgumentException("Error reading keys", ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads a PublicKey from an input stream overwriting the current PublicKey.
        /// No checking of the validity of the PublicKey data against encryption
        /// parameters is performed. This function should not be used unless the
        /// PublicKey comes from a fully trusted source.
        /// </summary>
        /// <param name="stream">The stream to load the PublicKey from</param>
        /// <exception cref="ArgumentNullException">if stream is null</exception>
        /// <exception cref="ArgumentException">if a valid PublicKey could not be read from stream</exception>
        public void UnsafeLoad(Stream stream)
        {
            if (null == stream)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            Data.UnsafeLoad(stream);
        }