Sha256: 59153d6798e4ebdd1ac390fc6c8dc27bc69d4b13888c6abb11014ffad4a80f7e

Contents?: true

Size: 1.47 KB

Versions: 10

Compression:

Stored size: 1.47 KB

Contents

module PacketGen
  module Header

    # Mixin for cryptographic classes
    # @api private
    # @author Sylvain Daubert
    module Crypto

      # Cryptographic error
      class Error < PacketGen::Error; end

      # Register cryptographic modes
      # @param [OpenSSL::Cipher] conf
      # @param [OpenSSL::HMAC] intg
      # @return [void]
      def set_crypto(conf, intg)
        @conf, @intg = conf, intg
        if conf.authenticated?
          # #auth_tag_len only supported from ruby 2.4.0
          @conf.auth_tag_len = @trunc if @conf.respond_to? :auth_tag_len
        end
      end

      # Get confidentiality mode name
      # @return [String]
      def confidentiality_mode
        mode = @conf.name.match(/-([^-]*)$/)[1]
        raise Error, 'unknown cipher mode' if mode.nil?
        mode.downcase
      end

      # Say if crypto modes permit authentication
      # @return [Boolean]
      def authenticated?
        @conf.authenticated? or !!@intg
      end

      def authenticate!
        @conf.final
        if @intg
          @intg.update @esn.to_s if @esn
          @intg.digest[0, @icv_length] == @icv
        else
          true
        end
      rescue OpenSSL::Cipher::CipherError
        false
      end

      def encipher(data)
        enciphered_data = @conf.update(data)
        @intg.update(enciphered_data) if @intg
        enciphered_data
      end

      def decipher(data)
        @intg.update(data) if @intg
        @conf.update(data)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
packetgen-2.4.0 lib/packetgen/header/crypto.rb
packetgen-2.3.0 lib/packetgen/header/crypto.rb
packetgen-2.2.0 lib/packetgen/header/crypto.rb
packetgen-2.1.4 lib/packetgen/header/crypto.rb
packetgen-2.1.3 lib/packetgen/header/crypto.rb
packetgen-2.1.2 lib/packetgen/header/crypto.rb
packetgen-2.1.1 lib/packetgen/header/crypto.rb
packetgen-2.1.0 lib/packetgen/header/crypto.rb
packetgen-2.0.1 lib/packetgen/header/crypto.rb
packetgen-2.0.0 lib/packetgen/header/crypto.rb