# FBB::ISymCryptStream(3bobcat)

## Symmetric en- and decryption
(libbobcat-dev_6.06.02)

## 2005-2024

## NAME

FBB::ISymCryptStream - Istream performing symmetric en/decryption

## SYNOPSIS

#include <bobcat/isymcryptstream>
Linking option: -lbobcat -lcrypto

## DESCRIPTION

FBB::ISymCryptStream objects can be used to encrypt or decrypt information that is available on separate std::istream streams.

The class ISymCryptStream is a class template, using a FBB::CryptType template non-type parameter. Objects of the class FBB::ISymCryptStream<FBB::ENCRYPT> encrypt the information they receive, objects of the class FBB::ISymCryptStream<FBB::DECRYPT> decrypt the information they receive.

All symmetric encryption methods defined by the OpenSSL library that can be selected by name may be used to en/decrypt information. To select a particular encryption method an identifier is passed to the constructor. E.g., "aes-256-gcm". For an overview of the currently supported cipher algorithms issue the command

        openssl list -cipher-algorithms

ISymCryptStream objects read the information to en/decrypt from std::istream objects, which are at construction-time specified as istream references or by filename. The characters that are thereupon extracted or read from ISymCryptStream objects are en/decrypted, and could, e.g., be written to some output stream.

## NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

## INHERITS FROM

FBB::ISymCryptStreambuf (private),
std::istream

## CONSTRUCTORS

- ISymCryptStream<CryptType>(std::istream &inStream, std::string const &cipherName, std::string const &key, std::string const &iv, size_t inBufSize = 100):
  This constructor defines a std::istream object encrypting or decrypting the characters which are read from inStream.

  - ISymCryptStream<FBB::ENCRYPT> objects perform encryption;
  ISymCryptStream<FBB::DECRYPT> objects perform decryption;

  - ISymCryptStream<CryptType> objects receive the characters to encrypt or decrypt from inStream;
  - The encryption method to use is specified by the cipherName parameter. E.g., "AES-256-GCM";
  - The symmetric key to use is specified by the key parameter;
  - The initialization vector is specified by the iv parameter;
  - The FBB::ISymCryptStreambuf internally used buffer will hold inBufSize characters. The default value is the smallest value that is used. When specifying a smaller bufSize value than the default value then the default value is used;

- ISymCryptStream<CryptType>(std::string const &inStreamName, std::string const &cipherName, std::string const &key, std::string const &iv, size_t inBufSize = 100):
  Same constructor as the previous one, but this constructor's first parameter specifies the name of the file containing the characters to encrypt or decrypt.

If the construction fails an exception is thrown, mentioning the openssl function that failed to complete (see also errorMsg below).

The move constructor is available, the copy constructor and assignment operators are not available,

## INHERITED MEMBERS

Since the class is publicly derived from std::istream, all std::istream members can be used.

## MEMBER FUNCTIONS

- static std::string errorMsg():
  If an openssl function fails an exception is thrown mentioning the name of the failing function. In those cases the function errorMsg can be called returning a std::string containing the openssl error code (returned by ERR_get_error) and its textual representation (returned by ERR_error_string). If the reported error code is zero, then in fact no error has occurred and the exception was spuriously reported;

- static size_t keyLength(std::string const &cipherName):
  returns the minimum key length required for cipher cipherName;

- static size_t ivLength(std::sting const &cipherName):
  returns the minimum length of the initialization vector that is required for cipher cipherName.

The latter two functions throw exceptions if cipherName does not contain the name of a supported cipher algorithm.

## EXAMPLE

    #include <iostream>
    #include <fstream>
    #include <string>

    #include <bobcat/isymcryptstream>
    #include <bobcat/isymcryptstreambuf>

    using namespace std;
    using namespace FBB;

    int main(int argc, char **argv)
    try
    {
        if (argc == 1)
        {
            cout << "arg[1]: e - encrypt, d - decrypt,\n"
                    "arg[2]: file to process, arg[3]: processed file\n";
            return 0;
        }

        string key = "0123456789abcdef0123456789abcdef";
        string iv = " 0123456789ab" "456";

        char cipherName[] =
            "AES-256-GCM"
            //"AES-256-CBC"
        ;

        ifstream in = Exception::factory<ifstream>(argv[2]);
        ofstream out{ argv[3] };

        ISymCryptStreambuf<ENCRYPT> encbuf{ in, cipherName, key, iv, 100 };

        if (*argv[1] == 'e')
        {
            ISymCryptStream<ENCRYPT> enc{ in, cipherName, key, iv, 100 };
                // comment out the previous line and uncomment the next
                // to use the constructor expecting a string as 1st arg:
    //      ISymCryptStream<ENCRYPT> enc{ argv[2], cipherName, key,
    //                                    iv, 100};

            out << enc.rdbuf();
        }
        else
        {
            ISymCryptStream<DECRYPT> decrypt{ in, cipherName, key, iv, 100 };
                // comment out the previous line and uncomment the next
                // to use the constructor expecting a string as 1st arg:
    //      ISymCryptStream<DECRYPT> decrypt{ argv[2], cipherName, key,
    //                                        iv, 100 };
            out << decrypt.rdbuf();
        }
    }
    catch (exception const &exc)
    {
        cerr << exc.what() << '\n';
    }

## FILES

bobcat/isymcryptstream - defines the class interface

## SEE ALSO

bobcat(7), isymcryptstreambuf(3bobcat), osymcryptstream(3bobcat), osymcryptstreambuf(3bobcat)

## BUGS

None Reported.

## BOBCAT PROJECT FILES

- https://fbb-git.gitlab.io/bobcat/: gitlab project page;
- bobcat_6.06.02-x.dsc: detached signature;
- bobcat_6.06.02-x.tar.gz: source archive;
- bobcat_6.06.02-x_i386.changes: change log;
- libbobcat1_6.06.02-x_*.deb: debian package containing the libraries;
- libbobcat1-dev_6.06.02-x_*.deb: debian package containing the libraries, headers and manual pages;

## BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

## COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

## AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).