'\" t .\" This documentation, which is part of the afnix writing .\" system distribution, is free and can be distributed as .\" long as this copyright notice is left intact. .\" .\" This documentation is distributed in the hope that it .\" will be useful, but without any warranty; without even .\" the implied warranty of merchantability and fitness .\" for a particular purpose. In no event, shall the .\" copyright holder be liable for any direct, incident .\" incidental or special damages arising in any way out .\" of the use of this documentation. .\" .\" Copyright (c) 1999-2023 Amaury Darsch .\" .TH sec 3 AFNIX "AFNIX Module" .SH NAME sec - standard security module .SH STANDARD SECURITY MODULE The Standard Security module is an original implementation of several standards and techniques used in the field of cryptography. The module provides the objects than enables message hashing, symetric and assymetric ciphers and digital signature computation. The implementation follows the recommendation from NIST and PKCS and the standard reference that it implements is always attached to the underlying object. .PP .B Hash objects .br Hashing is the ability to generate an almost unique representation from a string. Although, there is no guarantee that two different string will not produce the same result -- known as a collision -- the sophistication of the hashing function attempt to minimize such eventuality. The hashing process is not reversible. There are several hashing functions available in the public domain. To name a few, MD5 is the message digest 5, and SHA is the secure hash algorithm. The following table illustrates the size of the result with different hashing functions. .TS l l. Function Result size MD-2 128 bits MD-4 128 bits MD-5 128 bits SHA-1 160 bits SHA-224 224 bits SHA-256 256 bits SHA-384 384 bits SHA-512 512 bits .TE .sp .PP .I Hasher object .br The Hasher class is a text hashing computation class. The class computes a hash value from a literal object, a buffer or an input stream. Once computed, the hash value is stored as an array of bytes that can be retrieved one by one or at all in the form of a string representation. .PP .I Creating a hasher .br Several hasher objects are available in the module. For example, the Md5 object is the hasher object that implements the MD-5 algorithm. The constructor does not take any argument. .sp .nf # get a MD-5 hasher const md (afnix:sec:Md5) # check the object afnix:sec:hasher-p md # true .fi .sp The compute method computes the hash value. For example, the string "abc" returns the value "900150983CD24FB0D6963F7D28E17F72" which is 16 bytes long. .sp .nf const hval (md:compute "abc") .fi .sp .PP .I Creating a SHA hasher .br There are several SHA objects that produces results of different size as indicated in the next table. .TS l l l. Hasher Size Constructor SHA-1 160 bits Sha1 SHA-224 224 bits Sha224 SHA-256 256 bits Sha256 SHA-384 384 bits Sha384 SHA-512 512 bits Sha512 .TE .sp The compute method computes the hash value. For example, the string "abc" returns with SHA-1 the 20 bytes long value: "A9993E364706816ABA3E25717850C26C9CD0D89D" .PP .B Cipher key principles .br Cipher key management is an important concept in the ciphering land. In a simple mode, a key is used by a cipher to encode some data. Although the key can be any sequence of bytes, it is preferable to have the key built from a specific source such like a pass-phrase. A cipher key comes basically into two flavors: keys for symmetric ciphers and keys for asymmetric ciphers. A key for a symmetric cipher is easy to derive and generally follows a standard process which is independent of the cipher itself. A key for an asymmetric cipher is more complex to derive and is generally dependent on the cipher itself. .PP .I Key operations .br The basic operations associated with a key are the key identification by type and size. The key type is an item that identifies the key nature. The get-type method returns the key type as specified by the table below. .TS l l. Key Description KSYM Symmetric cipher key KRSA Asymmetric RSA cipher key KMAC Message authentication key KDSA Message signature key .TE .sp The message authentication key as represented by the KMAC symbol is similar to the symmetric key. The key type can be obtained with the get-type method. .sp .nf # get the key type const type (key:get-type) .fi .sp The key size is the canonical size as specified by the key or the cipher specification. The get-bits returns the key size in bits. The get-size returns the key size in bytes rounded to the nearest value. The table below describes the nature of the key size returned. .TS l l l. Key Type Description KSYM byte Byte array size KRSA bits Modulus size KMAC byte Byte array size KDSA bits Signature size .TE .sp .sp .nf const bits (key:get-bits) const size (key:get-size) .fi .sp .PP .I Key representation .br Unfortunately, it is not easy to represent a key, since the representation depends on the key's type. For example, a symmetric key can be formatted as a simple octet string. On the other hand, a RSA key has two components; namely the modulus and the exponent, which needs to be distinguished and therefore making the representation more difficult. Other cipher keys are even more complicated. For this reason, the representation model is a relaxed one. The format method can be called without argument to obtain an unique octet string representation if this representation is possible. If the key representation requires some parameters, the format method may accept one or several arguments to distinguish the key components. .TS l l l. Key Argument Description KSYM none Symmetric key octet string KRSA RSA-MODULUS RSA modulus octet string KRSA RSA-PUBLIC-EXPONENT RSA public exponent octet string KRSA RSA-SECRET-EXPONENT RSA secret exponent octet string KMAC none Message authentication key octet string KDSA DSA-P-PRIME DSA secret prime octet string KDSA DSA-Q-PRIME DSA secret prime octet string KDSA DSA-SECRET-KEY DSA secret key KDSA DSA-PUBLIC-KEY DSA public key KDSA DSA-PUBLIC-GENERATOR DSA public generator .TE .sp .sp .nf # get a simple key representation println (key:format) # get a rsa modulus key representation println (key:format afnix:sec:Key:RSA-MODULUS) .fi .sp There are other key representations. The natural one is the byte representation for a symmetric key, while a number based representation is generally more convenient with asymmetric keys. The get-byte method returns a key byte by index if possible. The get-relatif-key returns a key value by relatif number if possible. .PP .B Symmetric cipher key .br .PP .I Creating a symmetric cipher key .br The Key class can be used to create a cipher key suitable for a symmetric cipher. By default a 128 bits random key is generated, but the key can be also generated from an octet string. .sp .nf const key (afnix:sec:Key) assert true (afnix:sec:key-p key) .fi .sp The constructor also supports the use of an octet string representation of the key. .sp .nf # create an octet string key const key (afnix:sec:Key "0123456789ABCDEF") assert true (afnix:sec:key-p key) .fi .sp .PP .I Symmetric key functions .br The basic operation associated with a symmetric key is the byte extraction. The get-size method can be used to determine the byte key size. Once the key size has been obtained, the key byte can be accessed by index with the get-byte method. .sp .nf # create a 256 random symmetric key const key (afnix:sec:Key afnix:sec:Key:KSYM 256) # get the key size const size (key:get-size) # get the first byte const byte (key:get-byte 0) .fi .sp .PP .B Asymmetric cipher key .br An asymmetric cipher key can be generated for a particular asymmetric cipher, such like RSA. Generally, the key contains several components identified as the public and secret key components. These components are highly dependent on the cipher type. Under some circumstances, all components might not be available. .PP .I Creating an asymmetric cipher key .br The Key class can be used to create a specific asymmetric cipher key. Generally, the key is created by type and and bits size. .sp .nf # create a 1024 bits rsa key const key (afnix:sec:Key afnix:sec:Key:KRSA 1024) .fi .sp An asymmetric cipher key constructor is extremely dependent on the cipher type. For this reason, there is no constructor that can operate with a pass-phrase. .PP .I Asymmetric key functions .br The basic operation associated with a asymmetric key is the relatif based representation which is generally available for all key components. For example, in the case of the RSA cipher, the modulus, the public and secret exponents can be obtained in a relatif number based representation with the help of the get-relatif-key method. .sp .nf # create a 512 rsa key const key (afnix:sec:Key afnix:sec:Key:KRSA 512) # get the key modulus const kmod ( key:get-relatif-key afnix:sec:Key:RSA-MODULUS) # get the public exponent const pexp ( key:get-relatif-key afnix:sec:Key:RSA-PUBLIC-EXPONENT) # get the secret exponent const sexp ( key:get-relatif-key afnix:sec:Key:RSA-SECRET-EXPONENT) .fi .sp .PP .B Message authentication key .br .PP .I Creating a message authentication key .br The Key class can also be used to create a message authentication key suitable for a message authentication code generator or validator. By default a 128 bits random key is generated, but the key can be also generated from an octet string. .sp .nf const key (afnix:sec:Key afnix:sec:Key:KMAC) assert true (afnix:sec:key-p key) .fi .sp The constructor also supports the use of an octet string as a key representation. .sp .nf # create an octet string key const key ( afnix:sec:Key afnix:sec:Key:KMAC "0123456789ABCDEF") assert true (afnix:sec:key-p key) .fi .sp .PP .I Message authentication key functions .br The basic operation associated with a message authentication key is the byte extraction. The get-size method can be used to determine the byte key size. Once the key size has been obtained, the key byte can be accessed by index with the get-byte method. .sp .nf # create a 256 random message authentication key const key (afnix:sec:Key afnix:sec:Key:KMAC 256) # get the key size const size (key:get-size) # get the first byte const byte (key:get-byte 0) .fi .sp .PP .I Signature key functions .br The basic operation associated with a signature key is the relatif based representation which is generally available for all key components. For example, in the case of the DSA signer, the prime numbers, the public and secret components can be obtained in a relatif number based representation with the help of the get-relatif-key method. .sp .nf # create a 1024 dsa key const key (afnix:sec:Key afnix:sec:Key:KDSA) # get the key size const size (key:get-size) # get the secret component const sexp ( key:get-relatif-key afnix:sec:Key:DSA-SECRET-KEY) .fi .sp .PP .B Stream cipher .br A stream cipher is an object that encodes an input stream into an output stream. The data are read from the input stream, encoded and transmitted onto the output stream. There are basically two types of stream ciphers known as symmetric cipher and asymmetric cipher. .PP .I Symmetric cipher .br A symmetric cipher is a cipher that encodes and decode data with the same key. Normally, the key is kept secret, and the data are encoded by block. For this reason, symmetric cipher are also called block cipher. In normal mode, a symmetric cipher is created with key and the data are encoded from an input stream as long as they are available. The block size depends on the nature of the cipher. As of today, the recommended symmetric cipher is the Advanced Encryption Standard or AES, also known as Rijndael. .PP .I Asymmetric cipher .br An asymmetric cipher is a cipher that encodes and decode data with two keys. Normally, the data are encoded with a public key and decoded with a private key. In this model, anybody can encode a data stream, but only one person can read them. Obviously, the model can be reverse to operate in a kind of signature mode, where only one person can encode the data stream and anybody can read them. Asymmetric cipher are particularly useful when operating on unsecured channels. In this model, one end can send its public key as a mean for other people to crypt data that can only be read by the sender who is supposed to have the private key. As of today, the recommended asymmetric ciphers are RSA and DH. .PP .I Serial cipher .br A serial cipher is a cipher that encodes and decode data on a byte basis. Normally, the data are encoded and decoded with the same key, thus making the symmetric cipher key, the ideal candidate for a serial cipher key. Since the data is encoded on a byte basis, it can be used efficiently with a stream. However, the serial cipher does not define a block size and therefore require some mechanism to prevent a buffer overrun when reading bytes from a stream. For this reason, the serial cipher defines a default serial block size that can be used to buffer the stream data. A method is provided in the class to control the buffer size and is by default set to 4Kib bytes. .PP .I Cipher base class .br The Cipher base class is an abstract class that supports the symmetric, asymmetric and serial cipher models. A cipher object has a name and is bound to a key that is used by the cipher. The class provides some base methods that can be used to retrieve some information about the cipher. The get-name method returns the cipher name. The set-key and get-key methods are both used to set or retrieve the cipher key. The cipher operating mode can be found with the get-reverse method. If the get-reverse method returns true, the cipher is operating in decoding mode. Note that a set-reverse method also exists. .PP .B Block cipher .br A block cipher is an object that encodes an input stream with a symmetric cipher bound to a unique key. Since a block cipher is symmetric, the data can be coded and later decoded to their original form. The difference with the Cipher base class is that the BlockCipher class provides a get-block-size method which returns the cipher block size. .PP .I Block Cipher base .br The BlockCipher class is a base class for the block cipher engine. The class implements the stream method that reads from an input stream and write into an output stream. The BlockCipher class is an abstract class and cannot be instantiated by itself. The object is actually created by using a cipher algorithm class such like the Aes class. .sp .nf trans count (cipher:stream os is) .fi .sp The stream method returns the number of characters that have been encoded. Care should be taken that most of the stream cipher operates by block and therefore, will block until a complete block has been read from the input stream, unless the end of stream is read. The block cipher is always associated with a padding scheme. By default, the NIST 800-38A recommendation is associated with the block cipher, but can be changed with the set-padding-mode. .PP .I Creating a block cipher .br A BlockCipher object can be created with a cipher constructor. As of today, the Advanced Encryption Standard or AES is the recommended symmetric cipher. The Aes class creates a new block cipher that conforms to the AES standard. .sp .nf const cipher (afnix:sec:Aes) .fi .sp A block cipher can be created with a key and eventually a reverse flag. With one argument, the block cipher key is associated with the cipher. Such key can be created as indicated in the previous section. The reverse flag is used to determine if the cipher operate in encoding or decoding mode. By default, the cipher operates in coding mode. .sp .nf # create a 256 bits random key const key (afnix:sec:Key afnix:sec:KSYM 256) # create an aes block cipher const aes (afnix:sec:Aes key) .fi .sp .PP .I Block cipher information .br The BlockCipher class is derived from the Cipher class and contains several methods that provide information about the cipher. This include the cipher block size with the get-block-size method. .sp .nf println (aes:get-block-size) .fi .sp .PP .B Input cipher .br In the presence of a Cipher object, it is difficult to read an input stream and encode the character of a block basis. Furthermore, the existence of various method for block padding makes the coding operation even more difficult. For this reason, the InputCipher class provides the necessary method to code or decode an input stream in various mode of operations. .PP .I Input cipher mode .br The InputCipher class is an input stream that binds an input stream with a cipher. The class acts like an input stream, read the character from the bounded input stream and encode or decode them from the bended cipher. The InputCipher defines several modes of operations. In electronic codebook mode or ECB, the character are encoded in a block basis. In cipher block chaining mode, the block are encoded by doing an XOR operation with the previous block. Other modes are also available such like cipher feedback mode and output feedback mode. .PP .I Creating an input cipher .br By default an input cipher is created with a cipher object. Eventually, an input stream and/or the input mode can be specified at the object construction. .sp .nf # create a key const key (afnix:sec:Key "hello world") # create a direct cipher const aes (afnix:sec:Aes key) # create an input cipher const ic (afnix:sec:InputCipher aes) .fi .sp In this example, the input cipher is created in ECB mode. The input stream is later associated with the set-is method. .PP .I Input cipher operation .br The InputCipher class operates with one or several input streams. The set-is method sets the input stream. Read operation can be made with the help of the valid-p predicate. .sp .nf while (ic:valid-p) (os:write (ic:read)) .fi .sp Since the InputCipher operates like an input stream, the stream can be read as long as the valid-p predicate returns true. Note that the InputCipher manages automatically the padding operations with the mode associated with the block cipher. .PP .B Asymmetric cipher .br A public cipher is an object that encodes an input stream with a asymmetric cipher bound to a public and secret key. In theory, there is no difference between a block cipher and a public cipher. Furthermore, the interface provided by the engine is the same for both objects. .PP .I Public cipher .br A public cipher is an asymmetric stream cipher which operates with an asymmetric key. The main difference between a block cipher and a public cipher is the key nature as well as the encoded block size. With an asymmetric cipher, the size of the message to encode is generally not the same as the encoded block, because a message padding operation must occurs for each message block. .sp .nf trans count (cipher:stream os is) .fi .sp The stream method returns the number of characters that have been encoded. Like the block cipher, the stream method encodes an input stream or a buffer object. The number of encoded bytes is returned by the method. .PP .I Creating a public cipher .br A PublicCipher object can be created with a cipher constructor. The RSA asymmetric cipher is the typical example of public cipher. It is created by binding a RSA key to it. For security reasons, the key size must be large enough, typically with a size of at lease 1024 bits. .sp .nf const key (afnix:sec:Key afnix:sec:Key:KRSA 1024) const rsa (afnix:sec:Rsa key) .fi .sp A block cipher can be created with a key and eventually a reverse flag. Additional constructors are available to support various padding mode. Such padding mode depends on the cipher type. For example, the RSA cipher supports the ISO 18033-2 padding mode with a KDF1 or KDF2 object. Such constructor requires a hasher object as well. .sp .nf # create a 1024 bits rsa key const key (afnix:sec:Key afnix:sec:KRSA 1024) # create a SHA-1 hasher const ash (afnix:sec:Sha1) # create a rsa public cipher const rsa (afnix:sec:Rsa key ash "Demo") # set the padding mode rsa:set-padding-mode afnix:sec:Rsa:PAD-OAEP-K1 .fi .sp .PP .I Public cipher padding mode .br Like any cipher, a padding mode can be associated with the cipher. The set-padding-mode method can be used to set or change the padding mode. Depending on the padding mode type, additional objects might be needed at construction. .TS l l l. Cipher Padding mode Default RSA PKCS 1.5, PKCS 2.1, ISO/IEC 18033-2 PKCS 1.5 .TE .sp The default padding mode depends on the cipher type. For RSA, the default padding mode is set to PKCS 1.5 for compatibility reason. .PP .B Signature objects .br A digital signature is a unique representation, supposedly non forgeable, designed to authenticate a document, in whatever form it is represented. For example, a signature is used to sign a certificate which is used during the process of establish a secured connection over the Internet. A signature can also be used to sign a courrier or keys as it is in the Openssh protocol. Digital signatures come into several flavors eventually associated with the signed document. Sometimes, the signature acts as a container and permits to retrieve the document itself. Whatever the method, the principle remains the same. As of today technology, there are two standards used to sign document as indicated below. .TS l l. Standard Name DSS Digital Signature Standard RSA RSA based signature .TE .sp .PP .I Signer and signature objects .br The process of generating a signature is done with the help of a Signer object. A signer object is a generic object, similar in functionality to the hasher object. The result produced by a signer object is a Signature object which holds the generated signature. .PP .I Signature key .br The process of generating a signature often requires the use of a key. Such key can be generated with the help of the Key object. The nature of the key will depend on the target signature. The following table is a resume of the supported keys. .TS l l l. Standard Key Signer DSS KDSA Dsa .TE .sp In the case of DSS, a key can be generated automatically, although this process is time consuming. The default key size is 1024 bits. .sp .nf const key (afnix:sec:Key afnix:sec:Key:KDSA) assert 1024 (key:get-bits) .fi .sp .PP .I Creating a signer .br A Signer object is created with a particular signature object such like DSA. The Dsa object is a signer object that implements the Digital Signature Algorithm as specified by the Digital Signature Standard (DSS) in FIPS-PUB 186-3. .sp .nf # create a dsa signer const dsa (afnix:sec:Dsa key) assert true (afnix:sec:dsa-p dsa) .fi .sp .PP .I Creating a signature .br A signature is created with the help of the compute method. The Signature object is similar to the Hasher and operates with string or streams. .sp .nf # create a signature object const sgn (dsa:compute "afnix") assert true (afnix:sec:signature-p sgn) .fi .sp Once the signature is created, each data can be accessed directly with the associated component mapper. In the case of DSS, there are two components as show below. .sp .nf # get the DSS S component sgn:get-relatif-component afnix:sec:Signature:DSA-S-COMPONENT # get the DSS R component sgn:get-relatif-component afnix:sec:Signature:DSA-R-COMPONENT .fi .sp .SH STANDARD SECURITY REFERENCE .PP .B Hasher .br The Hasher class is a base class that is used to build a message hash. The hash result is stored in an array of bytes and can be retrieved byte by byte or as a formatted printable string. This class does not have a constructor. .PP .I Predicate .br .sp .RS hasher-p .RE .PP .I Inheritance .br .sp .RS Nameable .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the hasher object with its associated internal states. .RE .sp .RS .B hash-p -> Boolean (String) .br The hash-p predicate returns true if the string argument is potentially a hash value. It is not possible, with our current technology, to reverse a hash value to one or several representations, nor it is possible to assert that such value exists. .RE .sp .RS .B get-byte -> Byte (Integer) .br The get-byte method returns the hash byte value by index. The argument is the byte index which must be in the range of the hash result length. .RE .sp .RS .B format -> String (none) .br The format method return a string representation of the hash value. .RE .sp .RS .B compute -> String (Literal|Buffer|InputStream) .br The compute method computes the hash value from a string, a buffer or an input stream. The method returns a string representation of the result hash value. When the argument is a buffer object or an input stream, the characters are consumed from the object. .RE .sp .RS .B derive -> String (String) .br The derive method computes the hash value from an octet string which is converted before the hash computation. The method returns a string representation of the result hash value. .RE .sp .RS .B get-hash-length -> Integer (none) .br The get-hash-length method returns the hasher length in bytes. .RE .sp .RS .B get-result-length -> Integer (none) .br The get-result-length method returns the hasher result length in bytes. The result length is less or equal to the hasher length and is set at construction. .RE .PP .B Md2 .br The Md2 class is a hashing class that implements the MD-2 algorithm. .PP .I Predicate .br .sp .RS md2-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Md2 (none) .br The Md2 constructor creates a default hashing object that implements the MD-2 algorithm. .RE .sp .RS .B Md2 (Integer) .br The Md2 constructor creates a MD-2 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Md4 .br The Md4 class is a hashing class that implements the MD-4 algorithm. .PP .I Predicate .br .sp .RS md4-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Md4 (none) .br The Md4 constructor creates a default hashing object that implements the MD-4 algorithm. .RE .sp .RS .B Md4 (Integer) .br The Md4 constructor creates a MD-4 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Md5 .br The Md5 class is a hashing class that implements the MD-5 algorithm. .PP .I Predicate .br .sp .RS md5-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Md5 (none) .br The Md5 constructor creates a default hashing object that implements the MD-5 algorithm. .RE .sp .RS .B Md5 (Integer) .br The Md5 constructor creates a MD-5 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Sha1 .br The Sha1 class is a hashing class that implements the SHA-1 algorithm. .PP .I Predicate .br .sp .RS sha1-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Sha1 (none) .br The Sha1 constructor creates a default hashing object that implements the SHA-1 algorithm. .RE .sp .RS .B Sha1 (Integer) .br The Sha1 constructor creates a SHA-1 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Sha224 .br The Sha224 class is a hashing class that implements the SHA-224 algorithm. .PP .I Predicate .br .sp .RS sha224-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Sha224 (none) .br The Sha224 constructor creates a default hashing object that implements the SHA-224 algorithm. .RE .sp .RS .B Sha224 (Integer) .br The Sha224 constructor creates a SHA-224 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Sha256 .br The Sha256 class is a hashing class that implements the SHA-256 algorithm. .PP .I Predicate .br .sp .RS sha256-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Sha256 (none) .br The Sha256 constructor creates a default hashing object that implements the SHA-256 algorithm. .RE .sp .RS .B Sha256 (Integer) .br The Sha256 constructor creates a SHA-256 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Sha384 .br The Sha384 class is a hashing class that implements the SHA-384 algorithm. .PP .I Predicate .br .sp .RS sha384-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Sha384 (none) .br The Sha384 constructor creates a default hashing object that implements the SHA-384 algorithm. .RE .sp .RS .B Sha384 (Integer) .br The Sha384 constructor creates a SHA-384 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Sha512 .br The Sha512 class is a hashing class that implements the SHA-512 algorithm. .PP .I Predicate .br .sp .RS sha512-p .RE .PP .I Inheritance .br .sp .RS Hasher .RE .PP .I Constructors .br .sp .RS .B Sha512 (none) .br The Sha512 constructor creates a default hashing object that implements the SHA-512 algorithm. .RE .sp .RS .B Sha512 (Integer) .br The Sha512 constructor creates a SHA-512 hashing object with a result length. The argument is the hasher result length that must be less or equal to the hasher length. .RE .PP .B Key .br The Key class is an original class used to store a particular key or to generate one. A key is designed to operate with a variety of cipher that can be either symmetric or asymmetric. In the symmetric case, the key is generally an array of bytes. Asymmetric key are generally stored in the form of number list that can be computed or loaded by value. By default, a random 128 bit symmetric key is created. .PP .I Predicate .br .sp .RS key-p .RE .PP .I Inheritance .br .sp .RS Object .RE .PP .I Constructors .br .sp .RS .B Key (none) .br The Key constructor creates a default cipher key. The key is generated with random bytes and is 128 bits long. .RE .sp .RS .B Key (String) .br The Key constructor creates a symmetric key from an octet string. The octet string argument determines the size of the key. The octet string argument is compatible with the string obtained from the format method. .RE .sp .RS .B Key (Item) .br The Key constructor creates a key by type. If the key type is KSYM, a symmetric 128 bytes random key is generated. If the key type is KRSA, a 1024 bits RSA random key is generated. .RE .sp .RS .B Key (Item Integer|String|Vector) .br The Key constructor creates a key by type. The first argument is the key type to generate. The second argument is either the key size, the key octet string or the key byte values. In the first form, an integer argument specifies the key size in bytes or bits depending on the key nature. In the second form, a string is used as octet string to represent the key. In the third form, a vector of byte values can be used to load the key. .RE .PP .I Constants .br .sp .RS .B KSYM .br The KSYM constant indicates that the key is a symmetric key. .RE .sp .RS .B KRSA .br The KRSA constant indicates that the key is a asymmetric RSA key. .RE .sp .RS .B KMAC .br The KMAC constant indicates that the key is a message authentication (MAC) key. .RE .sp .RS .B RSA-MODULUS .br The RSA-MODULUS constant corresponds to the RSA modulus value. .RE .sp .RS .B RSA-PUBLIC-EXPONENT .br The RSA-PUBLIC-EXPONENT constant corresponds to the RSA public exponent value which is generally 65537. .RE .sp .RS .B RSA-SECRET-EXPONENT .br The RSA-SECRET-EXPONENT constant corresponds to the RSA secret exponent value. .RE .PP .I Methods .br .sp .RS .B get-byte -> Byte (Integer) .br The get-byte method returns a key byte value by index. The index must be in the key range or an exception is raised. This method is primarily used with symmetric key. .RE .sp .RS .B get-type -> Item (none) .br The get-type method returns the key type in the form of an item object. .RE .sp .RS .B get-bits -> Integer (none) .br The get-bits method returns the key size in bits. .RE .sp .RS .B get-size -> Integer (none) .br The get-size method returns the key size in bytes. .RE .sp .RS .B format -> String (none|Item) .br The format method returns a string representation of the key. In the first form, without argument, the key is returned as an octet string if possible. In the second form, the key value is returned as an octet string based on the key element to access. .RE .sp .RS .B get-relatif-key -> Relatif (Item) .br The get-relatif-key method returns a relatif representation of a key element. This method is well suited for asymmetric key. The key value is returned as a relatif based on the key element to access. .RE .PP .B Kdf .br The Kdf class is an abstract class used to model key derivation function. The class provides only a byte buffer which can be accessed by index. In the key derivation functions land, there are numerous standards, such like PKCS 2.1, IEEE P1363-2000, ISO/IEC 18033-2. All of these standards have sometimes conflicting definitions. .PP .I Predicate .br .sp .RS kdf-p .RE .PP .I Inheritance .br .sp .RS Nameable .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the internal state of the kdf object. .RE .sp .RS .B get-size -> Integer (none) .br The get-size method returns the kdf size in bytes. .RE .sp .RS .B get-byte -> Byte (Integer) .br The get-byte method returns a kdf byte value by index. The index must be in the key range or an exception is raised. .RE .sp .RS .B format -> String (none) .br The format method returns a string representation of the derived key. .RE .sp .RS .B derive -> String (String) .br The derive method returns a string representation of a derived key computed from the octet string argument. .RE .sp .RS .B compute -> String (String) .br The compute method returns a string representation of a derived key computed from the string argument. .RE .PP .B Hkdf .br The Hkdf class is an abstract class used to model key derivation function based on hash function. The class maintains a hasher object that is used to derive the key from an octet string. .PP .I Predicate .br .sp .RS hashed-kdf-p .RE .PP .I Inheritance .br .sp .RS Kdf .RE .PP .I Methods .br .sp .RS .B get-hasher -> none (none) .br The get-hasher method returns the hasher object associated with the key derivation function object. object. .RE .PP .B Kdf1 .br The Kdf1 class is a hashed key derivation function class that implements the KDF1 specification as defined by ISO/IEC 18033-2. The class is strictly equivalent to the mask generation function (MGF1) defined in PKCS 2.1. On the other hand, this implementation does not conform to the KDF1 specification of IEEE 1363-2000 which is somehow rather bizarre. The class operates in theory with any type of hasher object as long as the octet string is not too long. .PP .I Predicate .br .sp .RS kdf1-p .RE .PP .I Inheritance .br .sp .RS Hkdf .RE .PP .I Constructors .br .sp .RS .B Kdf1 (Hasher Integer) .br The Kdf1 constructor creates a KDF1 key derivation function object. The first argument is the hasher object to bind and the second argument is the kdf size. .RE .PP .B Kdf2 .br The Kdf2 class is a hashed key derivation function class that implements the KDF2 specification as defined by ISO/IEC 18033-2. The class is strictly equivalent to the key function derivation (KDF1) except that the internal counter runs from 1 to k instead of 0 to k-1. The class operates in theory with any type of hasher object as long as the octet string is not too long. .PP .I Predicate .br .sp .RS kdf2-p .RE .PP .I Inheritance .br .sp .RS Hkdf .RE .PP .I Constructors .br .sp .RS .B Kdf2 (Hasher Integer) .br The Kdf2 constructor creates a KDF2 key derivation function object. The first argument is the hasher object to bind and the second argument is the kdf size. .RE .PP .B Cipher .br The Cipher class is a base class that is used to implement a cipher. A cipher is used to encrypt or decrypt a message. There are basically two types of ciphers, namely symmetric cipher and asymmetric cipher. For the base class operation, only the cipher name and key is needed. A reverse flag controls whether or not an encryption operation must be reversed. A reset method can also be used to reset the internal cipher state. .PP .I Predicate .br .sp .RS cipher-p .RE .PP .I Inheritance .br .sp .RS Nameable .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the cipher internal state. .RE .sp .RS .B stream -> Integer (OutputStream InputStream) .br The stream method process an input stream and write into an output stream. The method returns the number of character processed. The first argument is the output stream used to write the coded characters. The second argument is the input stream used to read the characters. .RE .sp .RS .B set-key -> none (Key) .br The set-key method sets the cipher key. The first argument is the key to set. .RE .sp .RS .B get-key -> Key (none) .br The get-key method returns the cipher key. .RE .sp .RS .B set-reverse -> none (Boolean) .br The set-reverse method sets the cipher reverse flag. The first argument is the flag to set. If the flag is true, the cipher operates in reverse mode. If the flag is false, the cipher operates in direct mode. .RE .sp .RS .B get-reverse -> Boolean (none) .br The get-reverse method returns the cipher reverse flag. If the flag is true, the cipher operates in reverse mode. If the flag is false, the cipher operates in direct mode. .RE .PP .B BlockCipher .br The BlockCipher class is an abstract class that is used to implement a symmetric block cipher. By default the cipher operates in encryption mode. When the reverse flag is set, the decryption mode is activated. For a block cipher, a block size controls the cipher operations. The class also defines the constants that control the block padding with the associated methods. .PP .I Predicate .br .sp .RS block-cipher-p .RE .PP .I Inheritance .br .sp .RS Cipher .RE .PP .I Constants .br .sp .RS .B PAD-NONE .br The PAD-NONE constant indicates that the block should not be padded. .RE .sp .RS .B PAD-BIT-MODE .br The PAD-BIT constant indicates that the block should be padded in bit mode. .RE .sp .RS .B PAD-ANSI-X923 .br The PAD-ANSI-X923 constant indicates that the block should be padded according to ANSI X 923 standard. .RE .sp .RS .B PAD-NIST-800 .br The PAD-NIST-800 constant indicates that the block should be padded according to NIST 800-38A recommendations. This is the default mode. .RE .PP .I Methods .br .sp .RS .B get-block-size -> Integer (none) .br The get-block-size method returns the cipher block size. .RE .sp .RS .B set-padding-mode -> none (Item) .br The set-padding-mode method sets the cipher padding mode. .RE .sp .RS .B get-padding-mode -> Item (none) .br The get-padding-mode method returns the cipher padding mode. .RE .PP .B InputCipher .br The InputCipher class is an stream interface that can stream out an input stream from a cipher. In other word, an input stream is read and block are encoded as long as the input stream read characters. If the cipher is nil, the input cipher simply read the input stream and is therefore transparent. The class acts like an input stream, read the character from the bounded input stream and encode or decode them from the bounded cipher. The InputCipher defines several modes of operations. In electronic codebook mode or ECB, the character are encoded in a block basis. In cipher block chaining mode, the block are encoded by doing an XOR operation with the previous block. Other modes such like cipher feedback mode and output feedback mode are also defined. .PP .I Predicate .br .sp .RS input-cipher-p .RE .PP .I Inheritance .br .sp .RS Input .RE .PP .I Constructors .br .sp .RS .B InputCipher (Cipher) .br The InputCipher constructor creates an input cipher with a cipher object. The first argument is the cipher to used for processing. .RE .sp .RS .B InputCipher (Cipher Input) .br The InputCipher constructor creates an input cipher with a cipher object and an input stream. The first argument is the cipher to used for processing. The second argument is the input stream object used for the character reading. .RE .sp .RS .B InputCipher (Cipher InputStream Item) .br The InputCipher constructor creates an input cipher with a cipher object, an input stream and a mode. The first argument is the cipher to used for processing. The second argument is the input stream object used for the character reading. The third argument is the input cipher mode which can be either ECB, CBC, CFB or OFB. .RE .PP .I Constants .br .sp .RS .B ECB .br The ECB constant indicates that the input cipher is to operate in electronic codebook mode. This mode is the default mode. .RE .sp .RS .B CBC .br The CBC constant indicates that the input cipher is to operate in cipher chaining block mode. .RE .sp .RS .B CFB .br The CFB constant indicates that the input cipher is to operate in cipher feedback block mode. .RE .sp .RS .B OFB .br The OFB constant indicates that the input cipher is to operate in output feedback block mode. .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the input cipher object. .RE .sp .RS .B get-mode -> Item (none) .br The get-mode method returns the input cipher operating mode. .RE .sp .RS .B set-iv -> none (String|Buffer) .br The set-iv method sets the input cipher initial vector. In the first form, the initial vector is set from an octet string. In the second form, the initial vector is set from a buffer object. .RE .sp .RS .B get-iv -> String (none) .br The get-iv method returns the input cipher initial vector as an octet string. .RE .sp .RS .B set-is -> none (InputStream) .br The set-is method sets the input cipher input stream. This method can be used to chain multiple input streams in a unique coding session. .RE .PP .B Aes .br The Aes class is a block cipher class that implements the advanced encryption standard (AES), originally known as Rijndael. This is an original implementation that conforms to the standard FIPS PUB 197. It should be noted that the AES standard, unlike Rijndael, defines a fixed block size of 16 bytes (4 words) and 3 keys sizes (128, 192, 256). .PP .I Predicate .br .sp .RS aes-p .RE .PP .I Inheritance .br .sp .RS BlockCipher .RE .PP .I Constructors .br .sp .RS .B Aes (Key) .br The Aes constructor creates a direct cipher with a key. The first argument is the key used by the cipher. .RE .sp .RS .B Aes (Key Boolean) .br The Aes constructor creates a cipher with a key and a reverse flag. The first argument is the key used by the cipher. The second argument is the reverse flag. .RE .PP .B PublicCipher .br The PublicCipher class is an abstract class that is used to implement an asymmetric cipher. An asymmetric cipher or public key cipher is designed to operate with a public key and a secret key. Depending on the use model, the public key might be used to crypt the data, and the secret key to decrypt. The basic assumption around a public cipher is that the secret key cannot be derived from the public key. .PP .I Predicate .br .sp .RS public-cipher-p .RE .PP .I Inheritance .br .sp .RS Cipher .RE .PP .I Methods .br .sp .RS .B get-message-size -> Integer (none) .br The get-message-size method returns the cipher message size. .RE .sp .RS .B get-crypted-size -> Integer (none) .br The get-crypted-size method returns the cipher crypted block size. .RE .PP .B Rsa .br The Rsa class is a public cipher class that implements the RSA algorithm as described by PKCS 2.1, RFC 2437 and ISO 18033-2. The class implements also some padding mechanism described in PKCS 1.5, 2.1 and ISO 18033-2. The RSA algorithm is a public cryptographic cipher based on a secret and public keys. The class operates in crypting mode by default and uses the public key to do the encryption while the secret key is used in reverse (decryption) mode. By default, the PKCS 1.5 type 2 padding is used. The ISO RSA-REM1 padding with a key derivation function (KDF1) is equivalent to PKCS 2.1 padding with the mask generation function (MGF1). The ISO RSA-REM1 padding with KDF2 is not described in the PKCS 2.1. .PP .I Predicate .br .sp .RS rsa-p .RE .PP .I Inheritance .br .sp .RS PublicCipher .RE .PP .I Constructors .br .sp .RS .B Rsa (none) .br The Rsa constructor creates a default RSA public cipher by binding a 1024 bits random key. .RE .sp .RS .B Rsa (Key) .br The Rsa constructor creates a RSA public cipher by binding the key argument. .RE .sp .RS .B Rsa (Key Boolean) .br The Rsa constructor creates a RSA public cipher by binding the key argument and the reverse flag. The first argument is the key to bind. The second argument is the reverse flag to set. .RE .sp .RS .B Rsa (Key Hasher String) .br The Rsa constructor creates a RSA public cipher by binding the key argument and OAEP padding objects. The first argument is the key to bind. The second argument is hasher object to use with the OAEP padding mode. The third argument is an optional label to be used by the KDF object. .RE .PP .I Constants .br .sp .RS .B PAD-PKCS-11 .br The PAD-PKCS-11 constant indicates that the PKCS 1.5 type 1 block should be used to pad the message. .RE .sp .RS .B PAD-PKCS-12 .br The PAD-PKCS-12 constant indicates that the PKCS 1.5 type 3 block should be used to pad the message. .RE .sp .RS .B PAD-OAEP-K1 .br The PAD-OAEP-K1 constant indicates that the ISO/IEC 18033-2 OAEP with KDF1 should be used to pad the message. .RE .sp .RS .B PAD-OAEP-K2 .br The PAD-OAEP-K2 constant indicates that the ISO/IEC 18033-2 OAEP with KDF2 should be used to pad the message. .RE .PP .I Methods .br .sp .RS .B get-hasher -> Hasher (none) .br The get-hasher method returns the hasher object used by the OAEP padding mode. .RE .sp .RS .B set-hasher -> none (Hasher) .br The set-hasher method sets the hasher object used by the OAEP padding mode. .RE .sp .RS .B get-padding-mode -> Item (none) .br The get-padding-mode method returns the cipher padding mode. .RE .sp .RS .B set-padding-mode -> none (Item) .br The set-padding-mode method sets the cipher padding mode. .RE .sp .RS .B get-padding-label -> String (none) .br The get-padding-label method returns the cipher padding label. .RE .sp .RS .B set-padding-label -> none (String) .br The set-padding-mode method sets the cipher padding label. .RE .sp .RS .B get-padding-seed -> String (none) .br The get-padding-seed method returns the cipher padding seed. .RE .sp .RS .B set-padding-seed -> none (String) .br The set-padding-seed method sets the cipher padding seed. .RE .sp .RS .B pkcs-primitive -> Relatif (Integer|Relatif) .br The pkcs-primitive method compute a relatif value from a relatif argument by either crypting or decrypting the argument. seed. .RE .PP .B Signer .br The Signer class is a base class that is used to build a message signature. The signature result is stored in a special signature object which is algorithm dependent. .PP .I Predicate .br .sp .RS signer-p .RE .PP .I Inheritance .br .sp .RS Nameable .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the signer object with its associated internal states. .RE .sp .RS .B compute -> Signature (Literal|Buffer|InputStream) .br The compute method computes the signature from a string, a buffer or an input stream. The method returns a signature object. When the argument is a buffer object or an input stream, the characters are consumed from the object. .RE .sp .RS .B derive -> Signature (String) .br The derive method computes the signature from an octet string which is converted before the signature computation. The method returns a signature object. .RE .PP .B Signature .br The Signature class is a container class designed to store a message signature. The signature object is produced by a signing process, implemented in the form of a digital signature algorithm such like RSA or DSA. .PP .I Predicate .br .sp .RS signature-p .RE .PP .I Inheritance .br .sp .RS Object .RE .PP .I Constructors .br .sp .RS .B Signature (none) .br The Signature constructor creates an empty signature. .RE .PP .I Constants .br .sp .RS .B NIL .br The NIL constant indicates that the signature is a null signature. .RE .sp .RS .B DSA .br The DSA constant indicates that the signature is conforming to DSS. .RE .sp .RS .B DSA-S-COMPONENT .br The DSA-S-COMPONENT constant corresponds to the DSA S component value. .RE .sp .RS .B DSA-R-COMPONENT .br The DSA-R-COMPONENT constant corresponds to the DSA R component value. .RE .PP .I Methods .br .sp .RS .B reset -> none (none) .br The reset method reset the signature object to a null signature. .RE .sp .RS .B format -> String (Item) .br The format method returns a string representation of the signature component. The signature component is returned as an octet string based on the signature component to access. .RE .sp .RS .B get-relatif-component -> Relatif (Item) .br The get-relatif-component method returns a relatif representation of a signature component. .RE .PP .B Dsa .br The Dsa class is an original implementation of the Digital Signature Standard (DSS) as published in FIPS PUB 186-3. This class implements the Digital Signature Algorithm (DSA) with an approved key length of 1024, 2048 and 3072 bits with a 160, 224 and 256 bits hash function which is part of the SHA family. .PP .I Predicate .br .sp .RS dsa-p .RE .PP .I Inheritance .br .sp .RS Signer .RE .PP .I Constructors .br .sp .RS .B Dsa (none) .br The Dsa constructor creates a signer object with a default DSA key. .RE .sp .RS .B Dsa (Key) .br The Dsa constructor creates a signer object with a DSA key as its argument. .RE .sp .RS .B Dsa (Key Relatif) .br The Dsa constructor creates a signer object with a DSA key as its first argument and a fixed k argument as specified by DSS. .RE