aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/crypto.h
diff options
context:
space:
mode:
authorStephan Mueller <smueller@chronox.de>2014-11-11 23:29:36 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2014-11-13 09:31:42 -0500
commit58284f0d6c4a974d2d89446f3b3cbc51420432ea (patch)
tree13d3ea965b4794bdf61523b0f6174535c765f0fa /include/linux/crypto.h
parentfced7b02623e3ccace714f8adceed735698a9c8b (diff)
crypto: doc - BLKCIPHER API documentation
The API function calls exported by the kernel crypto API for synchronous block ciphers to be used by consumers are documented. Signed-off-by: Stephan Mueller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'include/linux/crypto.h')
-rw-r--r--include/linux/crypto.h188
1 files changed, 188 insertions, 0 deletions
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index 4704e71a31fe..c9ebe456dc9e 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -1565,6 +1565,36 @@ static inline void aead_request_set_assoc(struct aead_request *req,
1565 req->assoclen = assoclen; 1565 req->assoclen = assoclen;
1566} 1566}
1567 1567
1568/**
1569 * DOC: Synchronous Block Cipher API
1570 *
1571 * The synchronous block cipher API is used with the ciphers of type
1572 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1573 *
1574 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1575 * used in multiple calls and in parallel, this info should not be changeable
1576 * (unless a lock is used). This applies, for example, to the symmetric key.
1577 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1578 * structure for synchronous blkcipher api. So, its the only state info that can
1579 * be kept for synchronous calls without using a big lock across a tfm.
1580 *
1581 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1582 * consisting of a template (a block chaining mode) and a single block cipher
1583 * primitive (e.g. AES).
1584 *
1585 * The plaintext data buffer and the ciphertext data buffer are pointed to
1586 * by using scatter/gather lists. The cipher operation is performed
1587 * on all segments of the provided scatter/gather lists.
1588 *
1589 * The kernel crypto API supports a cipher operation "in-place" which means that
1590 * the caller may provide the same scatter/gather list for the plaintext and
1591 * cipher text. After the completion of the cipher operation, the plaintext
1592 * data is replaced with the ciphertext data in case of an encryption and vice
1593 * versa for a decryption. The caller must ensure that the scatter/gather lists
1594 * for the output data point to sufficiently large buffers, i.e. multiples of
1595 * the block size of the cipher.
1596 */
1597
1568static inline struct crypto_blkcipher *__crypto_blkcipher_cast( 1598static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1569 struct crypto_tfm *tfm) 1599 struct crypto_tfm *tfm)
1570{ 1600{
@@ -1578,6 +1608,20 @@ static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1578 return __crypto_blkcipher_cast(tfm); 1608 return __crypto_blkcipher_cast(tfm);
1579} 1609}
1580 1610
1611/**
1612 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1613 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1614 * blkcipher cipher
1615 * @type: specifies the type of the cipher
1616 * @mask: specifies the mask for the cipher
1617 *
1618 * Allocate a cipher handle for a block cipher. The returned struct
1619 * crypto_blkcipher is the cipher handle that is required for any subsequent
1620 * API invocation for that block cipher.
1621 *
1622 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1623 * of an error, PTR_ERR() returns the error code.
1624 */
1581static inline struct crypto_blkcipher *crypto_alloc_blkcipher( 1625static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1582 const char *alg_name, u32 type, u32 mask) 1626 const char *alg_name, u32 type, u32 mask)
1583{ 1627{
@@ -1594,11 +1638,25 @@ static inline struct crypto_tfm *crypto_blkcipher_tfm(
1594 return &tfm->base; 1638 return &tfm->base;
1595} 1639}
1596 1640
1641/**
1642 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1643 * @tfm: cipher handle to be freed
1644 */
1597static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) 1645static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1598{ 1646{
1599 crypto_free_tfm(crypto_blkcipher_tfm(tfm)); 1647 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1600} 1648}
1601 1649
1650/**
1651 * crypto_has_blkcipher() - Search for the availability of a block cipher
1652 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1653 * block cipher
1654 * @type: specifies the type of the cipher
1655 * @mask: specifies the mask for the cipher
1656 *
1657 * Return: true when the block cipher is known to the kernel crypto API; false
1658 * otherwise
1659 */
1602static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) 1660static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1603{ 1661{
1604 type &= ~CRYPTO_ALG_TYPE_MASK; 1662 type &= ~CRYPTO_ALG_TYPE_MASK;
@@ -1608,6 +1666,12 @@ static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1608 return crypto_has_alg(alg_name, type, mask); 1666 return crypto_has_alg(alg_name, type, mask);
1609} 1667}
1610 1668
1669/**
1670 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1671 * @tfm: cipher handle
1672 *
1673 * Return: The character string holding the name of the cipher
1674 */
1611static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) 1675static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1612{ 1676{
1613 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); 1677 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
@@ -1625,11 +1689,30 @@ static inline struct blkcipher_alg *crypto_blkcipher_alg(
1625 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; 1689 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1626} 1690}
1627 1691
1692/**
1693 * crypto_blkcipher_ivsize() - obtain IV size
1694 * @tfm: cipher handle
1695 *
1696 * The size of the IV for the block cipher referenced by the cipher handle is
1697 * returned. This IV size may be zero if the cipher does not need an IV.
1698 *
1699 * Return: IV size in bytes
1700 */
1628static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) 1701static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1629{ 1702{
1630 return crypto_blkcipher_alg(tfm)->ivsize; 1703 return crypto_blkcipher_alg(tfm)->ivsize;
1631} 1704}
1632 1705
1706/**
1707 * crypto_blkcipher_blocksize() - obtain block size of cipher
1708 * @tfm: cipher handle
1709 *
1710 * The block size for the block cipher referenced with the cipher handle is
1711 * returned. The caller may use that information to allocate appropriate
1712 * memory for the data returned by the encryption or decryption operation.
1713 *
1714 * Return: block size of cipher
1715 */
1633static inline unsigned int crypto_blkcipher_blocksize( 1716static inline unsigned int crypto_blkcipher_blocksize(
1634 struct crypto_blkcipher *tfm) 1717 struct crypto_blkcipher *tfm)
1635{ 1718{
@@ -1659,6 +1742,22 @@ static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1659 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); 1742 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1660} 1743}
1661 1744
1745/**
1746 * crypto_blkcipher_setkey() - set key for cipher
1747 * @tfm: cipher handle
1748 * @key: buffer holding the key
1749 * @keylen: length of the key in bytes
1750 *
1751 * The caller provided key is set for the block cipher referenced by the cipher
1752 * handle.
1753 *
1754 * Note, the key length determines the cipher type. Many block ciphers implement
1755 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1756 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1757 * is performed.
1758 *
1759 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1760 */
1662static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, 1761static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1663 const u8 *key, unsigned int keylen) 1762 const u8 *key, unsigned int keylen)
1664{ 1763{
@@ -1666,6 +1765,24 @@ static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1666 key, keylen); 1765 key, keylen);
1667} 1766}
1668 1767
1768/**
1769 * crypto_blkcipher_encrypt() - encrypt plaintext
1770 * @desc: reference to the block cipher handle with meta data
1771 * @dst: scatter/gather list that is filled by the cipher operation with the
1772 * ciphertext
1773 * @src: scatter/gather list that holds the plaintext
1774 * @nbytes: number of bytes of the plaintext to encrypt.
1775 *
1776 * Encrypt plaintext data using the IV set by the caller with a preceding
1777 * call of crypto_blkcipher_set_iv.
1778 *
1779 * The blkcipher_desc data structure must be filled by the caller and can
1780 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1781 * with the block cipher handle; desc.flags is filled with either
1782 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1783 *
1784 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1785 */
1669static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, 1786static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1670 struct scatterlist *dst, 1787 struct scatterlist *dst,
1671 struct scatterlist *src, 1788 struct scatterlist *src,
@@ -1675,6 +1792,25 @@ static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1675 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1792 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1676} 1793}
1677 1794
1795/**
1796 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1797 * @desc: reference to the block cipher handle with meta data
1798 * @dst: scatter/gather list that is filled by the cipher operation with the
1799 * ciphertext
1800 * @src: scatter/gather list that holds the plaintext
1801 * @nbytes: number of bytes of the plaintext to encrypt.
1802 *
1803 * Encrypt plaintext data with the use of an IV that is solely used for this
1804 * cipher operation. Any previously set IV is not used.
1805 *
1806 * The blkcipher_desc data structure must be filled by the caller and can
1807 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1808 * with the block cipher handle; desc.info is filled with the IV to be used for
1809 * the current operation; desc.flags is filled with either
1810 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1811 *
1812 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1813 */
1678static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, 1814static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1679 struct scatterlist *dst, 1815 struct scatterlist *dst,
1680 struct scatterlist *src, 1816 struct scatterlist *src,
@@ -1683,6 +1819,23 @@ static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1683 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1819 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1684} 1820}
1685 1821
1822/**
1823 * crypto_blkcipher_decrypt() - decrypt ciphertext
1824 * @desc: reference to the block cipher handle with meta data
1825 * @dst: scatter/gather list that is filled by the cipher operation with the
1826 * plaintext
1827 * @src: scatter/gather list that holds the ciphertext
1828 * @nbytes: number of bytes of the ciphertext to decrypt.
1829 *
1830 * Decrypt ciphertext data using the IV set by the caller with a preceding
1831 * call of crypto_blkcipher_set_iv.
1832 *
1833 * The blkcipher_desc data structure must be filled by the caller as documented
1834 * for the crypto_blkcipher_encrypt call above.
1835 *
1836 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1837 *
1838 */
1686static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, 1839static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1687 struct scatterlist *dst, 1840 struct scatterlist *dst,
1688 struct scatterlist *src, 1841 struct scatterlist *src,
@@ -1692,6 +1845,22 @@ static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1692 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1845 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1693} 1846}
1694 1847
1848/**
1849 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1850 * @desc: reference to the block cipher handle with meta data
1851 * @dst: scatter/gather list that is filled by the cipher operation with the
1852 * plaintext
1853 * @src: scatter/gather list that holds the ciphertext
1854 * @nbytes: number of bytes of the ciphertext to decrypt.
1855 *
1856 * Decrypt ciphertext data with the use of an IV that is solely used for this
1857 * cipher operation. Any previously set IV is not used.
1858 *
1859 * The blkcipher_desc data structure must be filled by the caller as documented
1860 * for the crypto_blkcipher_encrypt_iv call above.
1861 *
1862 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1863 */
1695static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, 1864static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1696 struct scatterlist *dst, 1865 struct scatterlist *dst,
1697 struct scatterlist *src, 1866 struct scatterlist *src,
@@ -1700,12 +1869,31 @@ static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1700 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1869 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1701} 1870}
1702 1871
1872/**
1873 * crypto_blkcipher_set_iv() - set IV for cipher
1874 * @tfm: cipher handle
1875 * @src: buffer holding the IV
1876 * @len: length of the IV in bytes
1877 *
1878 * The caller provided IV is set for the block cipher referenced by the cipher
1879 * handle.
1880 */
1703static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, 1881static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1704 const u8 *src, unsigned int len) 1882 const u8 *src, unsigned int len)
1705{ 1883{
1706 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); 1884 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1707} 1885}
1708 1886
1887/**
1888 * crypto_blkcipher_get_iv() - obtain IV from cipher
1889 * @tfm: cipher handle
1890 * @dst: buffer filled with the IV
1891 * @len: length of the buffer dst
1892 *
1893 * The caller can obtain the IV set for the block cipher referenced by the
1894 * cipher handle and store it into the user-provided buffer. If the buffer
1895 * has an insufficient space, the IV is truncated to fit the buffer.
1896 */
1709static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, 1897static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1710 u8 *dst, unsigned int len) 1898 u8 *dst, unsigned int len)
1711{ 1899{