The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Crypt::Util - A lightweight Crypt/Digest convenience API

SYNOPSIS

        use Crypto::Util; # also has a Sub::Exporter to return functions wrapping a default instance

        my $util = Crypto::Util->new;

        $util->default_key("my secret");

        # MAC or cipher+digest based tamper resistent encapsulation
        # (uses Storable on $data if necessary)
        my $tamper_resistent_string = $util->tamper_proof( $data );

        my $verified = $util->thaw_tamper_proof( $untrusted_string, key => "another secret" );

        # If the encoding is unspecified, base32 is used
        # (hex if base32 is unavailable)
        my $encoded = $util->encode_string( $bytes );

        my $hash = $util->digest( $bytes, digest => "md5" );

        die "baaaad" unless $util->verify_hash(
                hash   => $hash,
                data   => $bytes,
                digest => "md5",
        );

DESCRIPTION

This module provides an easy, intuitive and forgiving API for wielding crypto-fu.

Priorities

Ease of use

This module is designed to have an easy API to allow easy but responsible use of the more low level Crypt:: and Digest:: modules on CPAN. Therefore, patches to improve ease-of-use are very welcome.

Pluggability

Dependency hell is avoided using a fallback mechanism that tries to choose an algorithm based on an overridable list.

For "simple" use install Crypt::Util and your favourite digest, cipher and cipher mode (CBC, CFB, etc).

To ensure predictable behavior the fallback behavior can be disabled as necessary.

Interoperability

To ensure that your hashes and strings are compatible with Crypt::Util deployments on other machines (where different Crypt/Digest modules are available, etc) you should use disable_fallback.

Then either set the default ciphers, or always explicitly state the cipher.

If you are only encrypting and decrypting with the same installation, and new cryptographic modules are not being installed, the hashes/ciphertexts should be compatible without disabling fallback.

EXPORTED API

NOTE: nothing is exported by default.

Crypt::Util also presents an optional exported api using Sub::Exporter.

Unlike typical exported APIs, there is no class level default instance shared by all the importers, but instead every importer gets its own instance.

For example:

    package A;
    use Crypt::Util qw/:all/;

    default_key("moose");
    my $ciphertext = encrypt_string($plain);


    package B;
    use Crypt::Util qw/:all/;

    default_key("elk");
    my $ciphertext = encrypt_string($plain);

In this example every importing package has its own implicit instance, and the default_key function will in fact not share the value.

You can get the instance using the exported_instance function, which is just the identity method.

The export tags supported are: crypt (encryption and tamper proofing related functions), digest (digest and MAC related functions), encoding (various encoding and decoding functions), and params which give you functions for handling default values.

METHODS

tamper_proof( [ $data ], %params )
thaw_tamper_proof( [ $string ], %params )
tamper_proof_string $string, %params
thaw_tamper_proof_string $string, %params

The tamper_proof method is in an intermittent state, in that the data parameter's API is not completely finalized.

It is safer to use tamper_proof_string; its API is expected to remain the same in future versions as well.

See "TODO" for more information about the data types that will be supported in the future.

When thawing, the verify_digest or verify_mac methods will be used, with fatal defaulting to on unless explicitly disabled in the parameters.

    This method accepts the following parameters:

    * encrypt

    By default this parameter is true, unless default_tamper_proof_unencrypted(), has been enabled.

    A true value implies that all the parameters which are available to encrypt_string() are also available. If a negative value is specified, MAC mode is used, and the additional parameters of mac_digest_string() may also be specified to this method.

    * data

    The data to encrypt. If this is a reference Storable will be used to serialize the data.

If the string is encrypted then all the parameters of encrypt_string and digest_string are also available.

If the string is not encrypted, then all the parameters of mac_digest_string are also available.

encrypt_string( [ $string ], %params )
decrypt_string( [ $string ], %params )

All of the parameters which may be supplied to process_key(), cipher_object and maybe_encode are also available to these methods.

The following parameters may be used:

  • string

    The string to be en/decrypted can either be supplied first, creating an odd number of arguments, or as a named parameter.

process_key( $key, %params )

The following arguments may be specified:

  • literal_key

    This disables mungung. See also default_use_literal_key.

  • key_size

    Can be used to force a key size, even if the cipher specifies another size.

    If not specified, the key size chosen will depend

  • cipher

    Used to determine the key size.

cipher_object( %params )

Available parameters are:

digest_string( [ $string ], %params )

Delegates to digest_object. All parameters which can be used by digest_object may also be used here.

The following arguments are available:

  • string

    The string to be digested can either be supplied first, creating an odd number of arguments, or as a named parameter.

verify_digest( %params )

Delegates to digest_object. All parameters which can be used by digest_object may also be used here.

The following parameters are accepted:

  • hash

    A string containing the hash to verify.

  • string

    The digested string.

  • fatal

    If true, errors will be fatal. The default is false, which means that failures will return undef.

In addition, the parameters which can be supplied to digest_string() may also be supplied to this method.

digest_object( %params )
  • digest

    The digest algorithm to use.

Returns an object using Digest.

encode_string( [ $string ], %params )
decode_string( [ $string ], %params )

The following parameters are accepted:

  • encoding

    The encoding may be a symbolic type (uri, printable) or a concrete type (none, hex, base64, base32).

mac_digest_string( [ $string ], %param )

Delegates to mac_object. All parameters which can be used by mac_object may also be used here.

  • string

verify_mac( %params )

Delegates to mac_object. All parameters which can be used by mac_object may also be used here.

The following additional arguments are allowed:

  • hash

    The MAC string to verify.

  • string

    The digested string.

  • fatal

    If true, errors will be fatal. The default is false, which means that failures will return undef.

mac_object
  • mac

    The MAC algorithm to use. Currently only hmac is supported.

maybe_encode
maybe_decode

This method has no external API but is documented for the sake of its shared options.

It is delegated to by the various encryption and digest method.

  • encode

    Expects a bool.

  • encoding

    Expects an algorithm name (symbolic (e.g. uri, alphanumeric), or concrete (e.g. base64, hex)).

If encode is explicitly supplied it will always determine whether or not the string will be encoded. Otherwise, if encoding is explicitly supplied then the string will always be encoded using the specified algorithm. If neither is supplied default_encode will be checked to determine whether or not to encode, and default_encoding or fallback_encoding will be used to determine the algorithm to use (see "HANDLING OF DEFAULT VALUES").

encode_string_alphanumerical( $string )
decode_string_alphanumerical( $string )
encode_string_uri( $string )
decode_string_uri( $string )
encode_string_printable( $string )
decode_string_printable( $string )

The above methods encode based on a fallback list (see "HANDLING OF DEFAULT VALUES").

The variations denote types of formats: alphanumerical is letters and numbers only (case insensitive), uri is safe for inclusions in URIs (without further escaping), and printable contains no control characters or whitespace.

encode_string_hex( $string )
decode_string_hex( $string )

Big endian hexadecimal (H* pack format).

encode_string_uri_escape( $string )
decode_string_uri_escape( $string )

URI::Escape based encoding.

encode_string_base64( $string )
decode_string_base64( $string )
encode_string_base64_wrapped( $string )

Requires MIME::Base64.

The wrapped variant will introduce line breaks as per the MIME::Base64 default>.

encode_string_uri_base64
decode_string_uri_base64

Requires MIME::Base64.

Implements the Base64 for URIs. See http://en.wikipedia.org/wiki/Base64#URL_Applications.

encode_string_base32( $string )
decode_string_base32( $string )

Requires MIME::Base32.

(note- unlike MIME::Base32 this is case insensitive).

HANDLING OF DEFAULT VALUES

disable_fallback()

When true only the first item from the fallback list will be tried, and if it can't be loaded there will be a fatal error.

Enable this to ensure portability.

For every parameter, there are several methods, where PARAMETER is replaced with the parameter name:

  • default_PARAMETER()

    This accessor is available for the user to override the default value.

    If set to undef, then fallback_PARAMETER will be consulted instead.

    ALL the default values are set to undef unless changed by the user.

  • fallback_PARAMETER()

    Iterates the fallback_PARAMETER_list, choosing the first value that is usable (it's provider is available).

    If disable_fallback is set to a true value, then only the first value in the fallback list will be tried.

  • fallback_PARAMETER_list()

    An ordered list of values to try and use as fallbacks.

    fallback_PARAMETER iterates this list and chooses the first one that works.

Available parameters are as follows:

  • cipher

    The fallback list is Rijndael, Serpent, Twofish, RC6, Blowfish and RC5.

    Crypt::Rijndael is the AES winner, the next three are AES finalists, and the last two are well known and widely used.

  • mode

    The mode in which to use the cipher.

    The fallback list is CFB, CBC, Ctr, and OFB.

  • digest

    The fallback list is SHA-1, SHA-256, RIPEMD160, Whirlpool, MD5, and Haval256.

  • encoding

    The fallback list is hex (effectively no fallback).

  • alphanumerical_encoding

    The fallback list is base32 and hex.

    MIME::Base32 is required for base32 encoding.

  • uri_encoding

    The fallback list is uri_base64.

  • printable_encoding

    The fallback list is base64

Defaults with no fallbacks

The following parameters have a default_ method, as described in the previous section, but the fallback_ methods are not applicable.

  • encode

    Whether or not to encode by default (applies to digests and encryptions).

  • key

    The key to use. Useful for when you are repeatedly encrypting.

  • use_literal_key

    Whether or not to not hash the key by default. See process_key.

  • tamper_proof_unencrypted

    Whether or not tamper resistent strings are by default unencrypted (just MAC).

Subclassing

You may safely subclass and override default_PARAMETER and fallback_PARAMETER_list to provide values from configurations.

Overriding the fallback_PARAMETER method is also "allowed" but not reccomended.

TODO

Features which are currently missing but are scheduled for 0.02 or 0.03:

  • Crypt::SaltedHash support

  • CMAC, EMAC (maybe, the modules are not OO and require refactoring) message authentication modes.

  • OCB encryption mode (with implicit authentication)

    Bruce Schneier Fact Database http://geekz.co.uk/lovesraymond/archive/bruce-schneier-facts.

  • Entropy fetching (get N weak/strong bytes, etc) from e.g. OpenSSL bindings, /dev/*random, and EGD.

  • Additional data formats (streams/iterators, filehandles, generalized storable data/string handling for all methods, not just tamper_proof).

    Streams should also be able to used via a simple push api.

SEE ALSO

Digest, Crypt::CBC, Crypt::CFB, http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation.

VERSION CONTROL

This module is maintained using Darcs. You can get the latest version from http://nothingmuch.woobling.org/Crypt-Util/, and use darcs send to commit changes.

AUTHORS

Yuval Kogman, <nothingmuch@woobling.org> Ann Barcomb

COPYRIGHT & LICENSE

Copyright 2006 by Yuval Kogman <nothingmuch@woobling.org>, Ann Barcomb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

6 POD Errors

The following errors were encountered while parsing the POD:

Around line 1003:

You can't have =items (as at line 1007) unless the first thing after the =over is an =item

Around line 1292:

You forgot a '=back' before '=head1'

Around line 1352:

Expected '=item *'

Around line 1361:

Expected '=item *'

Around line 1410:

=back without =over

Around line 1448:

You forgot a '=back' before '=head1'