Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: doc - convert crypto API documentation to Sphinx

With the conversion of the kernel crypto API DocBook to Sphinx, the
monolithic document is broken up into individual documents. The
documentation is unchanged with the exception of a slight reordering to
keep the individual document parts self-contained.

Signed-off-by: Stephan Mueller <smueller@chronox.de>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Stephan Mueller and committed by
Jonathan Corbet
3b72c814 868c97a8

+1897
+68
Documentation/crypto/api-aead.rst
··· 1 + Authenticated Encryption With Associated Data (AEAD) Algorithm Definitions 2 + -------------------------------------------------------------------------- 3 + 4 + .. kernel-doc:: include/crypto/aead.h 5 + :doc: Authenticated Encryption With Associated Data (AEAD) Cipher API 6 + 7 + .. kernel-doc:: include/crypto/aead.h 8 + :functions: aead_request 9 + 10 + .. kernel-doc:: include/crypto/aead.h 11 + :functions: aead_alg 12 + 13 + Authenticated Encryption With Associated Data (AEAD) Cipher API 14 + --------------------------------------------------------------- 15 + 16 + .. kernel-doc:: include/crypto/aead.h 17 + :functions: crypto_alloc_aead 18 + 19 + .. kernel-doc:: include/crypto/aead.h 20 + :functions: crypto_free_aead 21 + 22 + .. kernel-doc:: include/crypto/aead.h 23 + :functions: crypto_aead_ivsize 24 + 25 + .. kernel-doc:: include/crypto/aead.h 26 + :functions: crypto_aead_authsize 27 + 28 + .. kernel-doc:: include/crypto/aead.h 29 + :functions: crypto_aead_blocksize 30 + 31 + .. kernel-doc:: include/crypto/aead.h 32 + :functions: crypto_aead_setkey 33 + 34 + .. kernel-doc:: include/crypto/aead.h 35 + :functions: crypto_aead_setauthsize 36 + 37 + .. kernel-doc:: include/crypto/aead.h 38 + :functions: crypto_aead_encrypt 39 + 40 + .. kernel-doc:: include/crypto/aead.h 41 + :functions: crypto_aead_decrypt 42 + 43 + Asynchronous AEAD Request Handle 44 + -------------------------------- 45 + 46 + .. kernel-doc:: include/crypto/aead.h 47 + :doc: Asynchronous AEAD Request Handle 48 + 49 + .. kernel-doc:: include/crypto/aead.h 50 + :functions: crypto_aead_reqsize 51 + 52 + .. kernel-doc:: include/crypto/aead.h 53 + :functions: aead_request_set_tfm 54 + 55 + .. kernel-doc:: include/crypto/aead.h 56 + :functions: aead_request_alloc 57 + 58 + .. kernel-doc:: include/crypto/aead.h 59 + :functions: aead_request_free 60 + 61 + .. kernel-doc:: include/crypto/aead.h 62 + :functions: aead_request_set_callback 63 + 64 + .. kernel-doc:: include/crypto/aead.h 65 + :functions: aead_request_set_crypt 66 + 67 + .. kernel-doc:: include/crypto/aead.h 68 + :functions: aead_request_set_ad
+56
Documentation/crypto/api-akcipher.rst
··· 1 + Asymmetric Cipher Algorithm Definitions 2 + --------------------------------------- 3 + 4 + .. kernel-doc:: include/crypto/akcipher.h 5 + :functions: akcipher_alg 6 + 7 + .. kernel-doc:: include/crypto/akcipher.h 8 + :functions: akcipher_request 9 + 10 + Asymmetric Cipher API 11 + --------------------- 12 + 13 + .. kernel-doc:: include/crypto/akcipher.h 14 + :doc: Generic Public Key API 15 + 16 + .. kernel-doc:: include/crypto/akcipher.h 17 + :functions: crypto_alloc_akcipher 18 + 19 + .. kernel-doc:: include/crypto/akcipher.h 20 + :functions: crypto_free_akcipher 21 + 22 + .. kernel-doc:: include/crypto/akcipher.h 23 + :functions: crypto_akcipher_set_pub_key 24 + 25 + .. kernel-doc:: include/crypto/akcipher.h 26 + :functions: crypto_akcipher_set_priv_key 27 + 28 + Asymmetric Cipher Request Handle 29 + -------------------------------- 30 + 31 + .. kernel-doc:: include/crypto/akcipher.h 32 + :functions: akcipher_request_alloc 33 + 34 + .. kernel-doc:: include/crypto/akcipher.h 35 + :functions: akcipher_request_free 36 + 37 + .. kernel-doc:: include/crypto/akcipher.h 38 + :functions: akcipher_request_set_callback 39 + 40 + .. kernel-doc:: include/crypto/akcipher.h 41 + :functions: akcipher_request_set_crypt 42 + 43 + .. kernel-doc:: include/crypto/akcipher.h 44 + :functions: crypto_akcipher_maxsize 45 + 46 + .. kernel-doc:: include/crypto/akcipher.h 47 + :functions: crypto_akcipher_encrypt 48 + 49 + .. kernel-doc:: include/crypto/akcipher.h 50 + :functions: crypto_akcipher_decrypt 51 + 52 + .. kernel-doc:: include/crypto/akcipher.h 53 + :functions: crypto_akcipher_sign 54 + 55 + .. kernel-doc:: include/crypto/akcipher.h 56 + :functions: crypto_akcipher_verify
+122
Documentation/crypto/api-digest.rst
··· 1 + Message Digest Algorithm Definitions 2 + ------------------------------------ 3 + 4 + .. kernel-doc:: include/crypto/hash.h 5 + :doc: Message Digest Algorithm Definitions 6 + 7 + .. kernel-doc:: include/crypto/hash.h 8 + :functions: hash_alg_common 9 + 10 + .. kernel-doc:: include/crypto/hash.h 11 + :functions: ahash_alg 12 + 13 + .. kernel-doc:: include/crypto/hash.h 14 + :functions: shash_alg 15 + 16 + Asynchronous Message Digest API 17 + ------------------------------- 18 + 19 + .. kernel-doc:: include/crypto/hash.h 20 + :doc: Asynchronous Message Digest API 21 + 22 + .. kernel-doc:: include/crypto/hash.h 23 + :functions: crypto_alloc_ahash 24 + 25 + .. kernel-doc:: include/crypto/hash.h 26 + :functions: crypto_free_ahash 27 + 28 + .. kernel-doc:: include/crypto/hash.h 29 + :functions: crypto_ahash_init 30 + 31 + .. kernel-doc:: include/crypto/hash.h 32 + :functions: crypto_ahash_digestsize 33 + 34 + .. kernel-doc:: include/crypto/hash.h 35 + :functions: crypto_ahash_reqtfm 36 + 37 + .. kernel-doc:: include/crypto/hash.h 38 + :functions: crypto_ahash_reqsize 39 + 40 + .. kernel-doc:: include/crypto/hash.h 41 + :functions: crypto_ahash_setkey 42 + 43 + .. kernel-doc:: include/crypto/hash.h 44 + :functions: crypto_ahash_finup 45 + 46 + .. kernel-doc:: include/crypto/hash.h 47 + :functions: crypto_ahash_final 48 + 49 + .. kernel-doc:: include/crypto/hash.h 50 + :functions: crypto_ahash_digest 51 + 52 + .. kernel-doc:: include/crypto/hash.h 53 + :functions: crypto_ahash_export 54 + 55 + .. kernel-doc:: include/crypto/hash.h 56 + :functions: crypto_ahash_import 57 + 58 + Asynchronous Hash Request Handle 59 + -------------------------------- 60 + 61 + .. kernel-doc:: include/crypto/hash.h 62 + :doc: Asynchronous Hash Request Handle 63 + 64 + .. kernel-doc:: include/crypto/hash.h 65 + :functions: ahash_request_set_tfm 66 + 67 + .. kernel-doc:: include/crypto/hash.h 68 + :functions: ahash_request_alloc 69 + 70 + .. kernel-doc:: include/crypto/hash.h 71 + :functions: ahash_request_free 72 + 73 + .. kernel-doc:: include/crypto/hash.h 74 + :functions: ahash_request_set_callback 75 + 76 + .. kernel-doc:: include/crypto/hash.h 77 + :functions: ahash_request_set_crypt 78 + 79 + Synchronous Message Digest API 80 + ------------------------------ 81 + 82 + .. kernel-doc:: include/crypto/hash.h 83 + :doc: Synchronous Message Digest API 84 + 85 + .. kernel-doc:: include/crypto/hash.h 86 + :functions: crypto_alloc_shash 87 + 88 + .. kernel-doc:: include/crypto/hash.h 89 + :functions: crypto_free_shash 90 + 91 + .. kernel-doc:: include/crypto/hash.h 92 + :functions: crypto_shash_blocksize 93 + 94 + .. kernel-doc:: include/crypto/hash.h 95 + :functions: crypto_shash_digestsize 96 + 97 + .. kernel-doc:: include/crypto/hash.h 98 + :functions: crypto_shash_descsize 99 + 100 + .. kernel-doc:: include/crypto/hash.h 101 + :functions: crypto_shash_setkey 102 + 103 + .. kernel-doc:: include/crypto/hash.h 104 + :functions: crypto_shash_digest 105 + 106 + .. kernel-doc:: include/crypto/hash.h 107 + :functions: crypto_shash_export 108 + 109 + .. kernel-doc:: include/crypto/hash.h 110 + :functions: crypto_shash_import 111 + 112 + .. kernel-doc:: include/crypto/hash.h 113 + :functions: crypto_shash_init 114 + 115 + .. kernel-doc:: include/crypto/hash.h 116 + :functions: crypto_shash_update 117 + 118 + .. kernel-doc:: include/crypto/hash.h 119 + :functions: crypto_shash_final 120 + 121 + .. kernel-doc:: include/crypto/hash.h 122 + :functions: crypto_shash_finup
+32
Documentation/crypto/api-rng.rst
··· 1 + Random Number Algorithm Definitions 2 + ----------------------------------- 3 + 4 + .. kernel-doc:: include/crypto/rng.h 5 + :functions: rng_alg 6 + 7 + Crypto API Random Number API 8 + ---------------------------- 9 + 10 + .. kernel-doc:: include/crypto/rng.h 11 + :doc: Random number generator API 12 + 13 + .. kernel-doc:: include/crypto/rng.h 14 + :functions: crypto_alloc_rng 15 + 16 + .. kernel-doc:: include/crypto/rng.h 17 + :functions: crypto_rng_alg 18 + 19 + .. kernel-doc:: include/crypto/rng.h 20 + :functions: crypto_free_rng 21 + 22 + .. kernel-doc:: include/crypto/rng.h 23 + :functions: crypto_rng_generate 24 + 25 + .. kernel-doc:: include/crypto/rng.h 26 + :functions: crypto_rng_get_bytes 27 + 28 + .. kernel-doc:: include/crypto/rng.h 29 + :functions: crypto_rng_reset 30 + 31 + .. kernel-doc:: include/crypto/rng.h 32 + :functions: crypto_rng_seedsize
+224
Documentation/crypto/api-samples.rst
··· 1 + Code Examples 2 + ============= 3 + 4 + Code Example For Symmetric Key Cipher Operation 5 + ----------------------------------------------- 6 + 7 + :: 8 + 9 + 10 + struct tcrypt_result { 11 + struct completion completion; 12 + int err; 13 + }; 14 + 15 + /* tie all data structures together */ 16 + struct skcipher_def { 17 + struct scatterlist sg; 18 + struct crypto_skcipher *tfm; 19 + struct skcipher_request *req; 20 + struct tcrypt_result result; 21 + }; 22 + 23 + /* Callback function */ 24 + static void test_skcipher_cb(struct crypto_async_request *req, int error) 25 + { 26 + struct tcrypt_result *result = req->data; 27 + 28 + if (error == -EINPROGRESS) 29 + return; 30 + result->err = error; 31 + complete(&result->completion); 32 + pr_info("Encryption finished successfully\n"); 33 + } 34 + 35 + /* Perform cipher operation */ 36 + static unsigned int test_skcipher_encdec(struct skcipher_def *sk, 37 + int enc) 38 + { 39 + int rc = 0; 40 + 41 + if (enc) 42 + rc = crypto_skcipher_encrypt(sk->req); 43 + else 44 + rc = crypto_skcipher_decrypt(sk->req); 45 + 46 + switch (rc) { 47 + case 0: 48 + break; 49 + case -EINPROGRESS: 50 + case -EBUSY: 51 + rc = wait_for_completion_interruptible( 52 + &sk->result.completion); 53 + if (!rc && !sk->result.err) { 54 + reinit_completion(&sk->result.completion); 55 + break; 56 + } 57 + default: 58 + pr_info("skcipher encrypt returned with %d result %d\n", 59 + rc, sk->result.err); 60 + break; 61 + } 62 + init_completion(&sk->result.completion); 63 + 64 + return rc; 65 + } 66 + 67 + /* Initialize and trigger cipher operation */ 68 + static int test_skcipher(void) 69 + { 70 + struct skcipher_def sk; 71 + struct crypto_skcipher *skcipher = NULL; 72 + struct skcipher_request *req = NULL; 73 + char *scratchpad = NULL; 74 + char *ivdata = NULL; 75 + unsigned char key[32]; 76 + int ret = -EFAULT; 77 + 78 + skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0); 79 + if (IS_ERR(skcipher)) { 80 + pr_info("could not allocate skcipher handle\n"); 81 + return PTR_ERR(skcipher); 82 + } 83 + 84 + req = skcipher_request_alloc(skcipher, GFP_KERNEL); 85 + if (!req) { 86 + pr_info("could not allocate skcipher request\n"); 87 + ret = -ENOMEM; 88 + goto out; 89 + } 90 + 91 + skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 92 + test_skcipher_cb, 93 + &sk.result); 94 + 95 + /* AES 256 with random key */ 96 + get_random_bytes(&key, 32); 97 + if (crypto_skcipher_setkey(skcipher, key, 32)) { 98 + pr_info("key could not be set\n"); 99 + ret = -EAGAIN; 100 + goto out; 101 + } 102 + 103 + /* IV will be random */ 104 + ivdata = kmalloc(16, GFP_KERNEL); 105 + if (!ivdata) { 106 + pr_info("could not allocate ivdata\n"); 107 + goto out; 108 + } 109 + get_random_bytes(ivdata, 16); 110 + 111 + /* Input data will be random */ 112 + scratchpad = kmalloc(16, GFP_KERNEL); 113 + if (!scratchpad) { 114 + pr_info("could not allocate scratchpad\n"); 115 + goto out; 116 + } 117 + get_random_bytes(scratchpad, 16); 118 + 119 + sk.tfm = skcipher; 120 + sk.req = req; 121 + 122 + /* We encrypt one block */ 123 + sg_init_one(&sk.sg, scratchpad, 16); 124 + skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata); 125 + init_completion(&sk.result.completion); 126 + 127 + /* encrypt data */ 128 + ret = test_skcipher_encdec(&sk, 1); 129 + if (ret) 130 + goto out; 131 + 132 + pr_info("Encryption triggered successfully\n"); 133 + 134 + out: 135 + if (skcipher) 136 + crypto_free_skcipher(skcipher); 137 + if (req) 138 + skcipher_request_free(req); 139 + if (ivdata) 140 + kfree(ivdata); 141 + if (scratchpad) 142 + kfree(scratchpad); 143 + return ret; 144 + } 145 + 146 + 147 + Code Example For Use of Operational State Memory With SHASH 148 + ----------------------------------------------------------- 149 + 150 + :: 151 + 152 + 153 + struct sdesc { 154 + struct shash_desc shash; 155 + char ctx[]; 156 + }; 157 + 158 + static struct sdescinit_sdesc(struct crypto_shash *alg) 159 + { 160 + struct sdescsdesc; 161 + int size; 162 + 163 + size = sizeof(struct shash_desc) + crypto_shash_descsize(alg); 164 + sdesc = kmalloc(size, GFP_KERNEL); 165 + if (!sdesc) 166 + return ERR_PTR(-ENOMEM); 167 + sdesc->shash.tfm = alg; 168 + sdesc->shash.flags = 0x0; 169 + return sdesc; 170 + } 171 + 172 + static int calc_hash(struct crypto_shashalg, 173 + const unsigned chardata, unsigned int datalen, 174 + unsigned chardigest) { 175 + struct sdescsdesc; 176 + int ret; 177 + 178 + sdesc = init_sdesc(alg); 179 + if (IS_ERR(sdesc)) { 180 + pr_info("trusted_key: can't alloc %s\n", hash_alg); 181 + return PTR_ERR(sdesc); 182 + } 183 + 184 + ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest); 185 + kfree(sdesc); 186 + return ret; 187 + } 188 + 189 + 190 + Code Example For Random Number Generator Usage 191 + ---------------------------------------------- 192 + 193 + :: 194 + 195 + 196 + static int get_random_numbers(u8 *buf, unsigned int len) 197 + { 198 + struct crypto_rngrng = NULL; 199 + chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */ 200 + int ret; 201 + 202 + if (!buf || !len) { 203 + pr_debug("No output buffer provided\n"); 204 + return -EINVAL; 205 + } 206 + 207 + rng = crypto_alloc_rng(drbg, 0, 0); 208 + if (IS_ERR(rng)) { 209 + pr_debug("could not allocate RNG handle for %s\n", drbg); 210 + return -PTR_ERR(rng); 211 + } 212 + 213 + ret = crypto_rng_get_bytes(rng, buf, len); 214 + if (ret < 0) 215 + pr_debug("generation of random numbers failed\n"); 216 + else if (ret == 0) 217 + pr_debug("RNG returned no data"); 218 + else 219 + pr_debug("RNG returned %d bytes of data\n", ret); 220 + 221 + out: 222 + crypto_free_rng(rng); 223 + return ret; 224 + }
+203
Documentation/crypto/api-skcipher.rst
··· 1 + Block Cipher Algorithm Definitions 2 + ---------------------------------- 3 + 4 + .. kernel-doc:: include/linux/crypto.h 5 + :doc: Block Cipher Algorithm Definitions 6 + 7 + .. kernel-doc:: include/linux/crypto.h 8 + :functions: crypto_alg 9 + 10 + .. kernel-doc:: include/linux/crypto.h 11 + :functions: ablkcipher_alg 12 + 13 + .. kernel-doc:: include/linux/crypto.h 14 + :functions: blkcipher_alg 15 + 16 + .. kernel-doc:: include/linux/crypto.h 17 + :functions: cipher_alg 18 + 19 + Symmetric Key Cipher API 20 + ------------------------ 21 + 22 + .. kernel-doc:: include/crypto/skcipher.h 23 + :doc: Symmetric Key Cipher API 24 + 25 + .. kernel-doc:: include/crypto/skcipher.h 26 + :functions: crypto_alloc_skcipher 27 + 28 + .. kernel-doc:: include/crypto/skcipher.h 29 + :functions: crypto_free_skcipher 30 + 31 + .. kernel-doc:: include/crypto/skcipher.h 32 + :functions: crypto_has_skcipher 33 + 34 + .. kernel-doc:: include/crypto/skcipher.h 35 + :functions: crypto_skcipher_ivsize 36 + 37 + .. kernel-doc:: include/crypto/skcipher.h 38 + :functions: crypto_skcipher_blocksize 39 + 40 + .. kernel-doc:: include/crypto/skcipher.h 41 + :functions: crypto_skcipher_setkey 42 + 43 + .. kernel-doc:: include/crypto/skcipher.h 44 + :functions: crypto_skcipher_reqtfm 45 + 46 + .. kernel-doc:: include/crypto/skcipher.h 47 + :functions: crypto_skcipher_encrypt 48 + 49 + .. kernel-doc:: include/crypto/skcipher.h 50 + :functions: crypto_skcipher_decrypt 51 + 52 + Symmetric Key Cipher Request Handle 53 + ----------------------------------- 54 + 55 + .. kernel-doc:: include/crypto/skcipher.h 56 + :doc: Symmetric Key Cipher Request Handle 57 + 58 + .. kernel-doc:: include/crypto/skcipher.h 59 + :functions: crypto_skcipher_reqsize 60 + 61 + .. kernel-doc:: include/crypto/skcipher.h 62 + :functions: skcipher_request_set_tfm 63 + 64 + .. kernel-doc:: include/crypto/skcipher.h 65 + :functions: skcipher_request_alloc 66 + 67 + .. kernel-doc:: include/crypto/skcipher.h 68 + :functions: skcipher_request_free 69 + 70 + .. kernel-doc:: include/crypto/skcipher.h 71 + :functions: skcipher_request_set_callback 72 + 73 + .. kernel-doc:: include/crypto/skcipher.h 74 + :functions: skcipher_request_set_crypt 75 + 76 + Single Block Cipher API 77 + ----------------------- 78 + 79 + .. kernel-doc:: include/linux/crypto.h 80 + :doc: Single Block Cipher API 81 + 82 + .. kernel-doc:: include/linux/crypto.h 83 + :functions: crypto_alloc_cipher 84 + 85 + .. kernel-doc:: include/linux/crypto.h 86 + :functions: crypto_free_cipher 87 + 88 + .. kernel-doc:: include/linux/crypto.h 89 + :functions: crypto_has_cipher 90 + 91 + .. kernel-doc:: include/linux/crypto.h 92 + :functions: crypto_cipher_blocksize 93 + 94 + .. kernel-doc:: include/linux/crypto.h 95 + :functions: crypto_cipher_setkey 96 + 97 + .. kernel-doc:: include/linux/crypto.h 98 + :functions: crypto_cipher_encrypt_one 99 + 100 + .. kernel-doc:: include/linux/crypto.h 101 + :functions: crypto_cipher_decrypt_one 102 + 103 + Asynchronous Block Cipher API - Deprecated 104 + ------------------------------------------ 105 + 106 + .. kernel-doc:: include/linux/crypto.h 107 + :doc: Asynchronous Block Cipher API 108 + 109 + .. kernel-doc:: include/linux/crypto.h 110 + :functions: crypto_alloc_ablkcipher 111 + 112 + .. kernel-doc:: include/linux/crypto.h 113 + :functions: crypto_free_ablkcipher 114 + 115 + .. kernel-doc:: include/linux/crypto.h 116 + :functions: crypto_has_ablkcipher 117 + 118 + .. kernel-doc:: include/linux/crypto.h 119 + :functions: crypto_ablkcipher_ivsize 120 + 121 + .. kernel-doc:: include/linux/crypto.h 122 + :functions: crypto_ablkcipher_blocksize 123 + 124 + .. kernel-doc:: include/linux/crypto.h 125 + :functions: crypto_ablkcipher_setkey 126 + 127 + .. kernel-doc:: include/linux/crypto.h 128 + :functions: crypto_ablkcipher_reqtfm 129 + 130 + .. kernel-doc:: include/linux/crypto.h 131 + :functions: crypto_ablkcipher_encrypt 132 + 133 + .. kernel-doc:: include/linux/crypto.h 134 + :functions: crypto_ablkcipher_decrypt 135 + 136 + Asynchronous Cipher Request Handle - Deprecated 137 + ----------------------------------------------- 138 + 139 + .. kernel-doc:: include/linux/crypto.h 140 + :doc: Asynchronous Cipher Request Handle 141 + 142 + .. kernel-doc:: include/linux/crypto.h 143 + :functions: crypto_ablkcipher_reqsize 144 + 145 + .. kernel-doc:: include/linux/crypto.h 146 + :functions: ablkcipher_request_set_tfm 147 + 148 + .. kernel-doc:: include/linux/crypto.h 149 + :functions: ablkcipher_request_alloc 150 + 151 + .. kernel-doc:: include/linux/crypto.h 152 + :functions: ablkcipher_request_free 153 + 154 + .. kernel-doc:: include/linux/crypto.h 155 + :functions: ablkcipher_request_set_callback 156 + 157 + .. kernel-doc:: include/linux/crypto.h 158 + :functions: ablkcipher_request_set_crypt 159 + 160 + Synchronous Block Cipher API - Deprecated 161 + ----------------------------------------- 162 + 163 + .. kernel-doc:: include/linux/crypto.h 164 + :doc: Synchronous Block Cipher API 165 + 166 + .. kernel-doc:: include/linux/crypto.h 167 + :functions: crypto_alloc_blkcipher 168 + 169 + .. kernel-doc:: include/linux/crypto.h 170 + :functions: crypto_free_blkcipher 171 + 172 + .. kernel-doc:: include/linux/crypto.h 173 + :functions: crypto_has_blkcipher 174 + 175 + .. kernel-doc:: include/linux/crypto.h 176 + :functions: crypto_blkcipher_name 177 + 178 + .. kernel-doc:: include/linux/crypto.h 179 + :functions: crypto_blkcipher_ivsize 180 + 181 + .. kernel-doc:: include/linux/crypto.h 182 + :functions: crypto_blkcipher_blocksize 183 + 184 + .. kernel-doc:: include/linux/crypto.h 185 + :functions: crypto_blkcipher_setkey 186 + 187 + .. kernel-doc:: include/linux/crypto.h 188 + :functions: crypto_blkcipher_encrypt 189 + 190 + .. kernel-doc:: include/linux/crypto.h 191 + :functions: crypto_blkcipher_encrypt_iv 192 + 193 + .. kernel-doc:: include/linux/crypto.h 194 + :functions: crypto_blkcipher_decrypt 195 + 196 + .. kernel-doc:: include/linux/crypto.h 197 + :functions: crypto_blkcipher_decrypt_iv 198 + 199 + .. kernel-doc:: include/linux/crypto.h 200 + :functions: crypto_blkcipher_set_iv 201 + 202 + .. kernel-doc:: include/linux/crypto.h 203 + :functions: crypto_blkcipher_get_iv
+24
Documentation/crypto/api.rst
··· 1 + Programming Interface 2 + ===================== 3 + 4 + Please note that the kernel crypto API contains the AEAD givcrypt API 5 + (crypto_aead_giv\* and aead_givcrypt\* function calls in 6 + include/crypto/aead.h). This API is obsolete and will be removed in the 7 + future. To obtain the functionality of an AEAD cipher with internal IV 8 + generation, use the IV generator as a regular cipher. For example, 9 + rfc4106(gcm(aes)) is the AEAD cipher with external IV generation and 10 + seqniv(rfc4106(gcm(aes))) implies that the kernel crypto API generates 11 + the IV. Different IV generators are available. 12 + 13 + .. class:: toc-title 14 + 15 + Table of contents 16 + 17 + .. toctree:: 18 + :maxdepth: 2 19 + 20 + api-skcipher 21 + api-aead 22 + api-digest 23 + api-rng 24 + api-akcipher
+435
Documentation/crypto/architecture.rst
··· 1 + Kernel Crypto API Architecture 2 + ============================== 3 + 4 + Cipher algorithm types 5 + ---------------------- 6 + 7 + The kernel crypto API provides different API calls for the following 8 + cipher types: 9 + 10 + - Symmetric ciphers 11 + 12 + - AEAD ciphers 13 + 14 + - Message digest, including keyed message digest 15 + 16 + - Random number generation 17 + 18 + - User space interface 19 + 20 + Ciphers And Templates 21 + --------------------- 22 + 23 + The kernel crypto API provides implementations of single block ciphers 24 + and message digests. In addition, the kernel crypto API provides 25 + numerous "templates" that can be used in conjunction with the single 26 + block ciphers and message digests. Templates include all types of block 27 + chaining mode, the HMAC mechanism, etc. 28 + 29 + Single block ciphers and message digests can either be directly used by 30 + a caller or invoked together with a template to form multi-block ciphers 31 + or keyed message digests. 32 + 33 + A single block cipher may even be called with multiple templates. 34 + However, templates cannot be used without a single cipher. 35 + 36 + See /proc/crypto and search for "name". For example: 37 + 38 + - aes 39 + 40 + - ecb(aes) 41 + 42 + - cmac(aes) 43 + 44 + - ccm(aes) 45 + 46 + - rfc4106(gcm(aes)) 47 + 48 + - sha1 49 + 50 + - hmac(sha1) 51 + 52 + - authenc(hmac(sha1),cbc(aes)) 53 + 54 + In these examples, "aes" and "sha1" are the ciphers and all others are 55 + the templates. 56 + 57 + Synchronous And Asynchronous Operation 58 + -------------------------------------- 59 + 60 + The kernel crypto API provides synchronous and asynchronous API 61 + operations. 62 + 63 + When using the synchronous API operation, the caller invokes a cipher 64 + operation which is performed synchronously by the kernel crypto API. 65 + That means, the caller waits until the cipher operation completes. 66 + Therefore, the kernel crypto API calls work like regular function calls. 67 + For synchronous operation, the set of API calls is small and 68 + conceptually similar to any other crypto library. 69 + 70 + Asynchronous operation is provided by the kernel crypto API which 71 + implies that the invocation of a cipher operation will complete almost 72 + instantly. That invocation triggers the cipher operation but it does not 73 + signal its completion. Before invoking a cipher operation, the caller 74 + must provide a callback function the kernel crypto API can invoke to 75 + signal the completion of the cipher operation. Furthermore, the caller 76 + must ensure it can handle such asynchronous events by applying 77 + appropriate locking around its data. The kernel crypto API does not 78 + perform any special serialization operation to protect the caller's data 79 + integrity. 80 + 81 + Crypto API Cipher References And Priority 82 + ----------------------------------------- 83 + 84 + A cipher is referenced by the caller with a string. That string has the 85 + following semantics: 86 + 87 + :: 88 + 89 + template(single block cipher) 90 + 91 + 92 + where "template" and "single block cipher" is the aforementioned 93 + template and single block cipher, respectively. If applicable, 94 + additional templates may enclose other templates, such as 95 + 96 + :: 97 + 98 + template1(template2(single block cipher))) 99 + 100 + 101 + The kernel crypto API may provide multiple implementations of a template 102 + or a single block cipher. For example, AES on newer Intel hardware has 103 + the following implementations: AES-NI, assembler implementation, or 104 + straight C. Now, when using the string "aes" with the kernel crypto API, 105 + which cipher implementation is used? The answer to that question is the 106 + priority number assigned to each cipher implementation by the kernel 107 + crypto API. When a caller uses the string to refer to a cipher during 108 + initialization of a cipher handle, the kernel crypto API looks up all 109 + implementations providing an implementation with that name and selects 110 + the implementation with the highest priority. 111 + 112 + Now, a caller may have the need to refer to a specific cipher 113 + implementation and thus does not want to rely on the priority-based 114 + selection. To accommodate this scenario, the kernel crypto API allows 115 + the cipher implementation to register a unique name in addition to 116 + common names. When using that unique name, a caller is therefore always 117 + sure to refer to the intended cipher implementation. 118 + 119 + The list of available ciphers is given in /proc/crypto. However, that 120 + list does not specify all possible permutations of templates and 121 + ciphers. Each block listed in /proc/crypto may contain the following 122 + information -- if one of the components listed as follows are not 123 + applicable to a cipher, it is not displayed: 124 + 125 + - name: the generic name of the cipher that is subject to the 126 + priority-based selection -- this name can be used by the cipher 127 + allocation API calls (all names listed above are examples for such 128 + generic names) 129 + 130 + - driver: the unique name of the cipher -- this name can be used by the 131 + cipher allocation API calls 132 + 133 + - module: the kernel module providing the cipher implementation (or 134 + "kernel" for statically linked ciphers) 135 + 136 + - priority: the priority value of the cipher implementation 137 + 138 + - refcnt: the reference count of the respective cipher (i.e. the number 139 + of current consumers of this cipher) 140 + 141 + - selftest: specification whether the self test for the cipher passed 142 + 143 + - type: 144 + 145 + - skcipher for symmetric key ciphers 146 + 147 + - cipher for single block ciphers that may be used with an 148 + additional template 149 + 150 + - shash for synchronous message digest 151 + 152 + - ahash for asynchronous message digest 153 + 154 + - aead for AEAD cipher type 155 + 156 + - compression for compression type transformations 157 + 158 + - rng for random number generator 159 + 160 + - givcipher for cipher with associated IV generator (see the geniv 161 + entry below for the specification of the IV generator type used by 162 + the cipher implementation) 163 + 164 + - blocksize: blocksize of cipher in bytes 165 + 166 + - keysize: key size in bytes 167 + 168 + - ivsize: IV size in bytes 169 + 170 + - seedsize: required size of seed data for random number generator 171 + 172 + - digestsize: output size of the message digest 173 + 174 + - geniv: IV generation type: 175 + 176 + - eseqiv for encrypted sequence number based IV generation 177 + 178 + - seqiv for sequence number based IV generation 179 + 180 + - chainiv for chain iv generation 181 + 182 + - <builtin> is a marker that the cipher implements IV generation and 183 + handling as it is specific to the given cipher 184 + 185 + Key Sizes 186 + --------- 187 + 188 + When allocating a cipher handle, the caller only specifies the cipher 189 + type. Symmetric ciphers, however, typically support multiple key sizes 190 + (e.g. AES-128 vs. AES-192 vs. AES-256). These key sizes are determined 191 + with the length of the provided key. Thus, the kernel crypto API does 192 + not provide a separate way to select the particular symmetric cipher key 193 + size. 194 + 195 + Cipher Allocation Type And Masks 196 + -------------------------------- 197 + 198 + The different cipher handle allocation functions allow the specification 199 + of a type and mask flag. Both parameters have the following meaning (and 200 + are therefore not covered in the subsequent sections). 201 + 202 + The type flag specifies the type of the cipher algorithm. The caller 203 + usually provides a 0 when the caller wants the default handling. 204 + Otherwise, the caller may provide the following selections which match 205 + the aforementioned cipher types: 206 + 207 + - CRYPTO_ALG_TYPE_CIPHER Single block cipher 208 + 209 + - CRYPTO_ALG_TYPE_COMPRESS Compression 210 + 211 + - CRYPTO_ALG_TYPE_AEAD Authenticated Encryption with Associated Data 212 + (MAC) 213 + 214 + - CRYPTO_ALG_TYPE_BLKCIPHER Synchronous multi-block cipher 215 + 216 + - CRYPTO_ALG_TYPE_ABLKCIPHER Asynchronous multi-block cipher 217 + 218 + - CRYPTO_ALG_TYPE_GIVCIPHER Asynchronous multi-block cipher packed 219 + together with an IV generator (see geniv field in the /proc/crypto 220 + listing for the known IV generators) 221 + 222 + - CRYPTO_ALG_TYPE_DIGEST Raw message digest 223 + 224 + - CRYPTO_ALG_TYPE_HASH Alias for CRYPTO_ALG_TYPE_DIGEST 225 + 226 + - CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash 227 + 228 + - CRYPTO_ALG_TYPE_AHASH Asynchronous multi-block hash 229 + 230 + - CRYPTO_ALG_TYPE_RNG Random Number Generation 231 + 232 + - CRYPTO_ALG_TYPE_AKCIPHER Asymmetric cipher 233 + 234 + - CRYPTO_ALG_TYPE_PCOMPRESS Enhanced version of 235 + CRYPTO_ALG_TYPE_COMPRESS allowing for segmented compression / 236 + decompression instead of performing the operation on one segment 237 + only. CRYPTO_ALG_TYPE_PCOMPRESS is intended to replace 238 + CRYPTO_ALG_TYPE_COMPRESS once existing consumers are converted. 239 + 240 + The mask flag restricts the type of cipher. The only allowed flag is 241 + CRYPTO_ALG_ASYNC to restrict the cipher lookup function to 242 + asynchronous ciphers. Usually, a caller provides a 0 for the mask flag. 243 + 244 + When the caller provides a mask and type specification, the caller 245 + limits the search the kernel crypto API can perform for a suitable 246 + cipher implementation for the given cipher name. That means, even when a 247 + caller uses a cipher name that exists during its initialization call, 248 + the kernel crypto API may not select it due to the used type and mask 249 + field. 250 + 251 + Internal Structure of Kernel Crypto API 252 + --------------------------------------- 253 + 254 + The kernel crypto API has an internal structure where a cipher 255 + implementation may use many layers and indirections. This section shall 256 + help to clarify how the kernel crypto API uses various components to 257 + implement the complete cipher. 258 + 259 + The following subsections explain the internal structure based on 260 + existing cipher implementations. The first section addresses the most 261 + complex scenario where all other scenarios form a logical subset. 262 + 263 + Generic AEAD Cipher Structure 264 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 265 + 266 + The following ASCII art decomposes the kernel crypto API layers when 267 + using the AEAD cipher with the automated IV generation. The shown 268 + example is used by the IPSEC layer. 269 + 270 + For other use cases of AEAD ciphers, the ASCII art applies as well, but 271 + the caller may not use the AEAD cipher with a separate IV generator. In 272 + this case, the caller must generate the IV. 273 + 274 + The depicted example decomposes the AEAD cipher of GCM(AES) based on the 275 + generic C implementations (gcm.c, aes-generic.c, ctr.c, ghash-generic.c, 276 + seqiv.c). The generic implementation serves as an example showing the 277 + complete logic of the kernel crypto API. 278 + 279 + It is possible that some streamlined cipher implementations (like 280 + AES-NI) provide implementations merging aspects which in the view of the 281 + kernel crypto API cannot be decomposed into layers any more. In case of 282 + the AES-NI implementation, the CTR mode, the GHASH implementation and 283 + the AES cipher are all merged into one cipher implementation registered 284 + with the kernel crypto API. In this case, the concept described by the 285 + following ASCII art applies too. However, the decomposition of GCM into 286 + the individual sub-components by the kernel crypto API is not done any 287 + more. 288 + 289 + Each block in the following ASCII art is an independent cipher instance 290 + obtained from the kernel crypto API. Each block is accessed by the 291 + caller or by other blocks using the API functions defined by the kernel 292 + crypto API for the cipher implementation type. 293 + 294 + The blocks below indicate the cipher type as well as the specific logic 295 + implemented in the cipher. 296 + 297 + The ASCII art picture also indicates the call structure, i.e. who calls 298 + which component. The arrows point to the invoked block where the caller 299 + uses the API applicable to the cipher type specified for the block. 300 + 301 + :: 302 + 303 + 304 + kernel crypto API | IPSEC Layer 305 + | 306 + +-----------+ | 307 + | | (1) 308 + | aead | <----------------------------------- esp_output 309 + | (seqiv) | ---+ 310 + +-----------+ | 311 + | (2) 312 + +-----------+ | 313 + | | <--+ (2) 314 + | aead | <----------------------------------- esp_input 315 + | (gcm) | ------------+ 316 + +-----------+ | 317 + | (3) | (5) 318 + v v 319 + +-----------+ +-----------+ 320 + | | | | 321 + | skcipher | | ahash | 322 + | (ctr) | ---+ | (ghash) | 323 + +-----------+ | +-----------+ 324 + | 325 + +-----------+ | (4) 326 + | | <--+ 327 + | cipher | 328 + | (aes) | 329 + +-----------+ 330 + 331 + 332 + 333 + The following call sequence is applicable when the IPSEC layer triggers 334 + an encryption operation with the esp_output function. During 335 + configuration, the administrator set up the use of rfc4106(gcm(aes)) as 336 + the cipher for ESP. The following call sequence is now depicted in the 337 + ASCII art above: 338 + 339 + 1. esp_output() invokes crypto_aead_encrypt() to trigger an 340 + encryption operation of the AEAD cipher with IV generator. 341 + 342 + In case of GCM, the SEQIV implementation is registered as GIVCIPHER 343 + in crypto_rfc4106_alloc(). 344 + 345 + The SEQIV performs its operation to generate an IV where the core 346 + function is seqiv_geniv(). 347 + 348 + 2. Now, SEQIV uses the AEAD API function calls to invoke the associated 349 + AEAD cipher. In our case, during the instantiation of SEQIV, the 350 + cipher handle for GCM is provided to SEQIV. This means that SEQIV 351 + invokes AEAD cipher operations with the GCM cipher handle. 352 + 353 + During instantiation of the GCM handle, the CTR(AES) and GHASH 354 + ciphers are instantiated. The cipher handles for CTR(AES) and GHASH 355 + are retained for later use. 356 + 357 + The GCM implementation is responsible to invoke the CTR mode AES and 358 + the GHASH cipher in the right manner to implement the GCM 359 + specification. 360 + 361 + 3. The GCM AEAD cipher type implementation now invokes the SKCIPHER API 362 + with the instantiated CTR(AES) cipher handle. 363 + 364 + During instantiation of the CTR(AES) cipher, the CIPHER type 365 + implementation of AES is instantiated. The cipher handle for AES is 366 + retained. 367 + 368 + That means that the SKCIPHER implementation of CTR(AES) only 369 + implements the CTR block chaining mode. After performing the block 370 + chaining operation, the CIPHER implementation of AES is invoked. 371 + 372 + 4. The SKCIPHER of CTR(AES) now invokes the CIPHER API with the AES 373 + cipher handle to encrypt one block. 374 + 375 + 5. The GCM AEAD implementation also invokes the GHASH cipher 376 + implementation via the AHASH API. 377 + 378 + When the IPSEC layer triggers the esp_input() function, the same call 379 + sequence is followed with the only difference that the operation starts 380 + with step (2). 381 + 382 + Generic Block Cipher Structure 383 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 384 + 385 + Generic block ciphers follow the same concept as depicted with the ASCII 386 + art picture above. 387 + 388 + For example, CBC(AES) is implemented with cbc.c, and aes-generic.c. The 389 + ASCII art picture above applies as well with the difference that only 390 + step (4) is used and the SKCIPHER block chaining mode is CBC. 391 + 392 + Generic Keyed Message Digest Structure 393 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 394 + 395 + Keyed message digest implementations again follow the same concept as 396 + depicted in the ASCII art picture above. 397 + 398 + For example, HMAC(SHA256) is implemented with hmac.c and 399 + sha256_generic.c. The following ASCII art illustrates the 400 + implementation: 401 + 402 + :: 403 + 404 + 405 + kernel crypto API | Caller 406 + | 407 + +-----------+ (1) | 408 + | | <------------------ some_function 409 + | ahash | 410 + | (hmac) | ---+ 411 + +-----------+ | 412 + | (2) 413 + +-----------+ | 414 + | | <--+ 415 + | shash | 416 + | (sha256) | 417 + +-----------+ 418 + 419 + 420 + 421 + The following call sequence is applicable when a caller triggers an HMAC 422 + operation: 423 + 424 + 1. The AHASH API functions are invoked by the caller. The HMAC 425 + implementation performs its operation as needed. 426 + 427 + During initialization of the HMAC cipher, the SHASH cipher type of 428 + SHA256 is instantiated. The cipher handle for the SHA256 instance is 429 + retained. 430 + 431 + At one time, the HMAC implementation requires a SHA256 operation 432 + where the SHA256 cipher handle is used. 433 + 434 + 2. The HMAC instance now invokes the SHASH API with the SHA256 cipher 435 + handle to calculate the message digest.
+247
Documentation/crypto/devel-algos.rst
··· 1 + Developing Cipher Algorithms 2 + ============================ 3 + 4 + Registering And Unregistering Transformation 5 + -------------------------------------------- 6 + 7 + There are three distinct types of registration functions in the Crypto 8 + API. One is used to register a generic cryptographic transformation, 9 + while the other two are specific to HASH transformations and 10 + COMPRESSion. We will discuss the latter two in a separate chapter, here 11 + we will only look at the generic ones. 12 + 13 + Before discussing the register functions, the data structure to be 14 + filled with each, struct crypto_alg, must be considered -- see below 15 + for a description of this data structure. 16 + 17 + The generic registration functions can be found in 18 + include/linux/crypto.h and their definition can be seen below. The 19 + former function registers a single transformation, while the latter 20 + works on an array of transformation descriptions. The latter is useful 21 + when registering transformations in bulk, for example when a driver 22 + implements multiple transformations. 23 + 24 + :: 25 + 26 + int crypto_register_alg(struct crypto_alg *alg); 27 + int crypto_register_algs(struct crypto_alg *algs, int count); 28 + 29 + 30 + The counterparts to those functions are listed below. 31 + 32 + :: 33 + 34 + int crypto_unregister_alg(struct crypto_alg *alg); 35 + int crypto_unregister_algs(struct crypto_alg *algs, int count); 36 + 37 + 38 + Notice that both registration and unregistration functions do return a 39 + value, so make sure to handle errors. A return code of zero implies 40 + success. Any return code < 0 implies an error. 41 + 42 + The bulk registration/unregistration functions register/unregister each 43 + transformation in the given array of length count. They handle errors as 44 + follows: 45 + 46 + - crypto_register_algs() succeeds if and only if it successfully 47 + registers all the given transformations. If an error occurs partway 48 + through, then it rolls back successful registrations before returning 49 + the error code. Note that if a driver needs to handle registration 50 + errors for individual transformations, then it will need to use the 51 + non-bulk function crypto_register_alg() instead. 52 + 53 + - crypto_unregister_algs() tries to unregister all the given 54 + transformations, continuing on error. It logs errors and always 55 + returns zero. 56 + 57 + Single-Block Symmetric Ciphers [CIPHER] 58 + --------------------------------------- 59 + 60 + Example of transformations: aes, arc4, ... 61 + 62 + This section describes the simplest of all transformation 63 + implementations, that being the CIPHER type used for symmetric ciphers. 64 + The CIPHER type is used for transformations which operate on exactly one 65 + block at a time and there are no dependencies between blocks at all. 66 + 67 + Registration specifics 68 + ~~~~~~~~~~~~~~~~~~~~~~ 69 + 70 + The registration of [CIPHER] algorithm is specific in that struct 71 + crypto_alg field .cra_type is empty. The .cra_u.cipher has to be 72 + filled in with proper callbacks to implement this transformation. 73 + 74 + See struct cipher_alg below. 75 + 76 + Cipher Definition With struct cipher_alg 77 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 78 + 79 + Struct cipher_alg defines a single block cipher. 80 + 81 + Here are schematics of how these functions are called when operated from 82 + other part of the kernel. Note that the .cia_setkey() call might happen 83 + before or after any of these schematics happen, but must not happen 84 + during any of these are in-flight. 85 + 86 + :: 87 + 88 + KEY ---. PLAINTEXT ---. 89 + v v 90 + .cia_setkey() -> .cia_encrypt() 91 + | 92 + '-----> CIPHERTEXT 93 + 94 + 95 + Please note that a pattern where .cia_setkey() is called multiple times 96 + is also valid: 97 + 98 + :: 99 + 100 + 101 + KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --. 102 + v v v v 103 + .cia_setkey() -> .cia_encrypt() -> .cia_setkey() -> .cia_encrypt() 104 + | | 105 + '---> CIPHERTEXT1 '---> CIPHERTEXT2 106 + 107 + 108 + Multi-Block Ciphers 109 + ------------------- 110 + 111 + Example of transformations: cbc(aes), ecb(arc4), ... 112 + 113 + This section describes the multi-block cipher transformation 114 + implementations. The multi-block ciphers are used for transformations 115 + which operate on scatterlists of data supplied to the transformation 116 + functions. They output the result into a scatterlist of data as well. 117 + 118 + Registration Specifics 119 + ~~~~~~~~~~~~~~~~~~~~~~ 120 + 121 + The registration of multi-block cipher algorithms is one of the most 122 + standard procedures throughout the crypto API. 123 + 124 + Note, if a cipher implementation requires a proper alignment of data, 125 + the caller should use the functions of crypto_skcipher_alignmask() to 126 + identify a memory alignment mask. The kernel crypto API is able to 127 + process requests that are unaligned. This implies, however, additional 128 + overhead as the kernel crypto API needs to perform the realignment of 129 + the data which may imply moving of data. 130 + 131 + Cipher Definition With struct blkcipher_alg and ablkcipher_alg 132 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 133 + 134 + Struct blkcipher_alg defines a synchronous block cipher whereas struct 135 + ablkcipher_alg defines an asynchronous block cipher. 136 + 137 + Please refer to the single block cipher description for schematics of 138 + the block cipher usage. 139 + 140 + Specifics Of Asynchronous Multi-Block Cipher 141 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 142 + 143 + There are a couple of specifics to the asynchronous interface. 144 + 145 + First of all, some of the drivers will want to use the Generic 146 + ScatterWalk in case the hardware needs to be fed separate chunks of the 147 + scatterlist which contains the plaintext and will contain the 148 + ciphertext. Please refer to the ScatterWalk interface offered by the 149 + Linux kernel scatter / gather list implementation. 150 + 151 + Hashing [HASH] 152 + -------------- 153 + 154 + Example of transformations: crc32, md5, sha1, sha256,... 155 + 156 + Registering And Unregistering The Transformation 157 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 158 + 159 + There are multiple ways to register a HASH transformation, depending on 160 + whether the transformation is synchronous [SHASH] or asynchronous 161 + [AHASH] and the amount of HASH transformations we are registering. You 162 + can find the prototypes defined in include/crypto/internal/hash.h: 163 + 164 + :: 165 + 166 + int crypto_register_ahash(struct ahash_alg *alg); 167 + 168 + int crypto_register_shash(struct shash_alg *alg); 169 + int crypto_register_shashes(struct shash_alg *algs, int count); 170 + 171 + 172 + The respective counterparts for unregistering the HASH transformation 173 + are as follows: 174 + 175 + :: 176 + 177 + int crypto_unregister_ahash(struct ahash_alg *alg); 178 + 179 + int crypto_unregister_shash(struct shash_alg *alg); 180 + int crypto_unregister_shashes(struct shash_alg *algs, int count); 181 + 182 + 183 + Cipher Definition With struct shash_alg and ahash_alg 184 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 185 + 186 + Here are schematics of how these functions are called when operated from 187 + other part of the kernel. Note that the .setkey() call might happen 188 + before or after any of these schematics happen, but must not happen 189 + during any of these are in-flight. Please note that calling .init() 190 + followed immediately by .finish() is also a perfectly valid 191 + transformation. 192 + 193 + :: 194 + 195 + I) DATA -----------. 196 + v 197 + .init() -> .update() -> .final() ! .update() might not be called 198 + ^ | | at all in this scenario. 199 + '----' '---> HASH 200 + 201 + II) DATA -----------.-----------. 202 + v v 203 + .init() -> .update() -> .finup() ! .update() may not be called 204 + ^ | | at all in this scenario. 205 + '----' '---> HASH 206 + 207 + III) DATA -----------. 208 + v 209 + .digest() ! The entire process is handled 210 + | by the .digest() call. 211 + '---------------> HASH 212 + 213 + 214 + Here is a schematic of how the .export()/.import() functions are called 215 + when used from another part of the kernel. 216 + 217 + :: 218 + 219 + KEY--. DATA--. 220 + v v ! .update() may not be called 221 + .setkey() -> .init() -> .update() -> .export() at all in this scenario. 222 + ^ | | 223 + '-----' '--> PARTIAL_HASH 224 + 225 + ----------- other transformations happen here ----------- 226 + 227 + PARTIAL_HASH--. DATA1--. 228 + v v 229 + .import -> .update() -> .final() ! .update() may not be called 230 + ^ | | at all in this scenario. 231 + '----' '--> HASH1 232 + 233 + PARTIAL_HASH--. DATA2-. 234 + v v 235 + .import -> .finup() 236 + | 237 + '---------------> HASH2 238 + 239 + 240 + Specifics Of Asynchronous HASH Transformation 241 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 242 + 243 + Some of the drivers will want to use the Generic ScatterWalk in case the 244 + implementation needs to be fed separate chunks of the scatterlist which 245 + contains the input data. The buffer containing the resulting hash will 246 + always be properly aligned to .cra_alignmask so there is no need to 247 + worry about this.
+24
Documentation/crypto/index.rst
··· 1 + ======================= 2 + Linux Kernel Crypto API 3 + ======================= 4 + 5 + :Author: Stephan Mueller 6 + :Author: Marek Vasut 7 + 8 + This documentation outlines the Linux kernel crypto API with its 9 + concepts, details about developing cipher implementations, employment of the API 10 + for cryptographic use cases, as well as programming examples. 11 + 12 + .. class:: toc-title 13 + 14 + Table of contents 15 + 16 + .. toctree:: 17 + :maxdepth: 2 18 + 19 + intro 20 + architecture 21 + devel-algos 22 + userspace-if 23 + api 24 + api-samples
+74
Documentation/crypto/intro.rst
··· 1 + Kernel Crypto API Interface Specification 2 + ========================================= 3 + 4 + Introduction 5 + ------------ 6 + 7 + The kernel crypto API offers a rich set of cryptographic ciphers as well 8 + as other data transformation mechanisms and methods to invoke these. 9 + This document contains a description of the API and provides example 10 + code. 11 + 12 + To understand and properly use the kernel crypto API a brief explanation 13 + of its structure is given. Based on the architecture, the API can be 14 + separated into different components. Following the architecture 15 + specification, hints to developers of ciphers are provided. Pointers to 16 + the API function call documentation are given at the end. 17 + 18 + The kernel crypto API refers to all algorithms as "transformations". 19 + Therefore, a cipher handle variable usually has the name "tfm". Besides 20 + cryptographic operations, the kernel crypto API also knows compression 21 + transformations and handles them the same way as ciphers. 22 + 23 + The kernel crypto API serves the following entity types: 24 + 25 + - consumers requesting cryptographic services 26 + 27 + - data transformation implementations (typically ciphers) that can be 28 + called by consumers using the kernel crypto API 29 + 30 + This specification is intended for consumers of the kernel crypto API as 31 + well as for developers implementing ciphers. This API specification, 32 + however, does not discuss all API calls available to data transformation 33 + implementations (i.e. implementations of ciphers and other 34 + transformations (such as CRC or even compression algorithms) that can 35 + register with the kernel crypto API). 36 + 37 + Note: The terms "transformation" and cipher algorithm are used 38 + interchangeably. 39 + 40 + Terminology 41 + ----------- 42 + 43 + The transformation implementation is an actual code or interface to 44 + hardware which implements a certain transformation with precisely 45 + defined behavior. 46 + 47 + The transformation object (TFM) is an instance of a transformation 48 + implementation. There can be multiple transformation objects associated 49 + with a single transformation implementation. Each of those 50 + transformation objects is held by a crypto API consumer or another 51 + transformation. Transformation object is allocated when a crypto API 52 + consumer requests a transformation implementation. The consumer is then 53 + provided with a structure, which contains a transformation object (TFM). 54 + 55 + The structure that contains transformation objects may also be referred 56 + to as a "cipher handle". Such a cipher handle is always subject to the 57 + following phases that are reflected in the API calls applicable to such 58 + a cipher handle: 59 + 60 + 1. Initialization of a cipher handle. 61 + 62 + 2. Execution of all intended cipher operations applicable for the handle 63 + where the cipher handle must be furnished to every API call. 64 + 65 + 3. Destruction of a cipher handle. 66 + 67 + When using the initialization API calls, a cipher handle is created and 68 + returned to the consumer. Therefore, please refer to all initialization 69 + API calls that refer to the data structure type a consumer is expected 70 + to receive and subsequently to use. The initialization API calls have 71 + all the same naming conventions of crypto_alloc\*. 72 + 73 + The transformation context is private data associated with the 74 + transformation object.
+387
Documentation/crypto/userspace-if.rst
··· 1 + User Space Interface 2 + ==================== 3 + 4 + Introduction 5 + ------------ 6 + 7 + The concepts of the kernel crypto API visible to kernel space is fully 8 + applicable to the user space interface as well. Therefore, the kernel 9 + crypto API high level discussion for the in-kernel use cases applies 10 + here as well. 11 + 12 + The major difference, however, is that user space can only act as a 13 + consumer and never as a provider of a transformation or cipher 14 + algorithm. 15 + 16 + The following covers the user space interface exported by the kernel 17 + crypto API. A working example of this description is libkcapi that can 18 + be obtained from [1]. That library can be used by user space 19 + applications that require cryptographic services from the kernel. 20 + 21 + Some details of the in-kernel kernel crypto API aspects do not apply to 22 + user space, however. This includes the difference between synchronous 23 + and asynchronous invocations. The user space API call is fully 24 + synchronous. 25 + 26 + [1] http://www.chronox.de/libkcapi.html 27 + 28 + User Space API General Remarks 29 + ------------------------------ 30 + 31 + The kernel crypto API is accessible from user space. Currently, the 32 + following ciphers are accessible: 33 + 34 + - Message digest including keyed message digest (HMAC, CMAC) 35 + 36 + - Symmetric ciphers 37 + 38 + - AEAD ciphers 39 + 40 + - Random Number Generators 41 + 42 + The interface is provided via socket type using the type AF_ALG. In 43 + addition, the setsockopt option type is SOL_ALG. In case the user space 44 + header files do not export these flags yet, use the following macros: 45 + 46 + :: 47 + 48 + #ifndef AF_ALG 49 + #define AF_ALG 38 50 + #endif 51 + #ifndef SOL_ALG 52 + #define SOL_ALG 279 53 + #endif 54 + 55 + 56 + A cipher is accessed with the same name as done for the in-kernel API 57 + calls. This includes the generic vs. unique naming schema for ciphers as 58 + well as the enforcement of priorities for generic names. 59 + 60 + To interact with the kernel crypto API, a socket must be created by the 61 + user space application. User space invokes the cipher operation with the 62 + send()/write() system call family. The result of the cipher operation is 63 + obtained with the read()/recv() system call family. 64 + 65 + The following API calls assume that the socket descriptor is already 66 + opened by the user space application and discusses only the kernel 67 + crypto API specific invocations. 68 + 69 + To initialize the socket interface, the following sequence has to be 70 + performed by the consumer: 71 + 72 + 1. Create a socket of type AF_ALG with the struct sockaddr_alg 73 + parameter specified below for the different cipher types. 74 + 75 + 2. Invoke bind with the socket descriptor 76 + 77 + 3. Invoke accept with the socket descriptor. The accept system call 78 + returns a new file descriptor that is to be used to interact with the 79 + particular cipher instance. When invoking send/write or recv/read 80 + system calls to send data to the kernel or obtain data from the 81 + kernel, the file descriptor returned by accept must be used. 82 + 83 + In-place Cipher operation 84 + ------------------------- 85 + 86 + Just like the in-kernel operation of the kernel crypto API, the user 87 + space interface allows the cipher operation in-place. That means that 88 + the input buffer used for the send/write system call and the output 89 + buffer used by the read/recv system call may be one and the same. This 90 + is of particular interest for symmetric cipher operations where a 91 + copying of the output data to its final destination can be avoided. 92 + 93 + If a consumer on the other hand wants to maintain the plaintext and the 94 + ciphertext in different memory locations, all a consumer needs to do is 95 + to provide different memory pointers for the encryption and decryption 96 + operation. 97 + 98 + Message Digest API 99 + ------------------ 100 + 101 + The message digest type to be used for the cipher operation is selected 102 + when invoking the bind syscall. bind requires the caller to provide a 103 + filled struct sockaddr data structure. This data structure must be 104 + filled as follows: 105 + 106 + :: 107 + 108 + struct sockaddr_alg sa = { 109 + .salg_family = AF_ALG, 110 + .salg_type = "hash", /* this selects the hash logic in the kernel */ 111 + .salg_name = "sha1" /* this is the cipher name */ 112 + }; 113 + 114 + 115 + The salg_type value "hash" applies to message digests and keyed message 116 + digests. Though, a keyed message digest is referenced by the appropriate 117 + salg_name. Please see below for the setsockopt interface that explains 118 + how the key can be set for a keyed message digest. 119 + 120 + Using the send() system call, the application provides the data that 121 + should be processed with the message digest. The send system call allows 122 + the following flags to be specified: 123 + 124 + - MSG_MORE: If this flag is set, the send system call acts like a 125 + message digest update function where the final hash is not yet 126 + calculated. If the flag is not set, the send system call calculates 127 + the final message digest immediately. 128 + 129 + With the recv() system call, the application can read the message digest 130 + from the kernel crypto API. If the buffer is too small for the message 131 + digest, the flag MSG_TRUNC is set by the kernel. 132 + 133 + In order to set a message digest key, the calling application must use 134 + the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC 135 + operation is performed without the initial HMAC state change caused by 136 + the key. 137 + 138 + Symmetric Cipher API 139 + -------------------- 140 + 141 + The operation is very similar to the message digest discussion. During 142 + initialization, the struct sockaddr data structure must be filled as 143 + follows: 144 + 145 + :: 146 + 147 + struct sockaddr_alg sa = { 148 + .salg_family = AF_ALG, 149 + .salg_type = "skcipher", /* this selects the symmetric cipher */ 150 + .salg_name = "cbc(aes)" /* this is the cipher name */ 151 + }; 152 + 153 + 154 + Before data can be sent to the kernel using the write/send system call 155 + family, the consumer must set the key. The key setting is described with 156 + the setsockopt invocation below. 157 + 158 + Using the sendmsg() system call, the application provides the data that 159 + should be processed for encryption or decryption. In addition, the IV is 160 + specified with the data structure provided by the sendmsg() system call. 161 + 162 + The sendmsg system call parameter of struct msghdr is embedded into the 163 + struct cmsghdr data structure. See recv(2) and cmsg(3) for more 164 + information on how the cmsghdr data structure is used together with the 165 + send/recv system call family. That cmsghdr data structure holds the 166 + following information specified with a separate header instances: 167 + 168 + - specification of the cipher operation type with one of these flags: 169 + 170 + - ALG_OP_ENCRYPT - encryption of data 171 + 172 + - ALG_OP_DECRYPT - decryption of data 173 + 174 + - specification of the IV information marked with the flag ALG_SET_IV 175 + 176 + The send system call family allows the following flag to be specified: 177 + 178 + - MSG_MORE: If this flag is set, the send system call acts like a 179 + cipher update function where more input data is expected with a 180 + subsequent invocation of the send system call. 181 + 182 + Note: The kernel reports -EINVAL for any unexpected data. The caller 183 + must make sure that all data matches the constraints given in 184 + /proc/crypto for the selected cipher. 185 + 186 + With the recv() system call, the application can read the result of the 187 + cipher operation from the kernel crypto API. The output buffer must be 188 + at least as large as to hold all blocks of the encrypted or decrypted 189 + data. If the output data size is smaller, only as many blocks are 190 + returned that fit into that output buffer size. 191 + 192 + AEAD Cipher API 193 + --------------- 194 + 195 + The operation is very similar to the symmetric cipher discussion. During 196 + initialization, the struct sockaddr data structure must be filled as 197 + follows: 198 + 199 + :: 200 + 201 + struct sockaddr_alg sa = { 202 + .salg_family = AF_ALG, 203 + .salg_type = "aead", /* this selects the symmetric cipher */ 204 + .salg_name = "gcm(aes)" /* this is the cipher name */ 205 + }; 206 + 207 + 208 + Before data can be sent to the kernel using the write/send system call 209 + family, the consumer must set the key. The key setting is described with 210 + the setsockopt invocation below. 211 + 212 + In addition, before data can be sent to the kernel using the write/send 213 + system call family, the consumer must set the authentication tag size. 214 + To set the authentication tag size, the caller must use the setsockopt 215 + invocation described below. 216 + 217 + Using the sendmsg() system call, the application provides the data that 218 + should be processed for encryption or decryption. In addition, the IV is 219 + specified with the data structure provided by the sendmsg() system call. 220 + 221 + The sendmsg system call parameter of struct msghdr is embedded into the 222 + struct cmsghdr data structure. See recv(2) and cmsg(3) for more 223 + information on how the cmsghdr data structure is used together with the 224 + send/recv system call family. That cmsghdr data structure holds the 225 + following information specified with a separate header instances: 226 + 227 + - specification of the cipher operation type with one of these flags: 228 + 229 + - ALG_OP_ENCRYPT - encryption of data 230 + 231 + - ALG_OP_DECRYPT - decryption of data 232 + 233 + - specification of the IV information marked with the flag ALG_SET_IV 234 + 235 + - specification of the associated authentication data (AAD) with the 236 + flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together 237 + with the plaintext / ciphertext. See below for the memory structure. 238 + 239 + The send system call family allows the following flag to be specified: 240 + 241 + - MSG_MORE: If this flag is set, the send system call acts like a 242 + cipher update function where more input data is expected with a 243 + subsequent invocation of the send system call. 244 + 245 + Note: The kernel reports -EINVAL for any unexpected data. The caller 246 + must make sure that all data matches the constraints given in 247 + /proc/crypto for the selected cipher. 248 + 249 + With the recv() system call, the application can read the result of the 250 + cipher operation from the kernel crypto API. The output buffer must be 251 + at least as large as defined with the memory structure below. If the 252 + output data size is smaller, the cipher operation is not performed. 253 + 254 + The authenticated decryption operation may indicate an integrity error. 255 + Such breach in integrity is marked with the -EBADMSG error code. 256 + 257 + AEAD Memory Structure 258 + ~~~~~~~~~~~~~~~~~~~~~ 259 + 260 + The AEAD cipher operates with the following information that is 261 + communicated between user and kernel space as one data stream: 262 + 263 + - plaintext or ciphertext 264 + 265 + - associated authentication data (AAD) 266 + 267 + - authentication tag 268 + 269 + The sizes of the AAD and the authentication tag are provided with the 270 + sendmsg and setsockopt calls (see there). As the kernel knows the size 271 + of the entire data stream, the kernel is now able to calculate the right 272 + offsets of the data components in the data stream. 273 + 274 + The user space caller must arrange the aforementioned information in the 275 + following order: 276 + 277 + - AEAD encryption input: AAD \|\| plaintext 278 + 279 + - AEAD decryption input: AAD \|\| ciphertext \|\| authentication tag 280 + 281 + The output buffer the user space caller provides must be at least as 282 + large to hold the following data: 283 + 284 + - AEAD encryption output: ciphertext \|\| authentication tag 285 + 286 + - AEAD decryption output: plaintext 287 + 288 + Random Number Generator API 289 + --------------------------- 290 + 291 + Again, the operation is very similar to the other APIs. During 292 + initialization, the struct sockaddr data structure must be filled as 293 + follows: 294 + 295 + :: 296 + 297 + struct sockaddr_alg sa = { 298 + .salg_family = AF_ALG, 299 + .salg_type = "rng", /* this selects the symmetric cipher */ 300 + .salg_name = "drbg_nopr_sha256" /* this is the cipher name */ 301 + }; 302 + 303 + 304 + Depending on the RNG type, the RNG must be seeded. The seed is provided 305 + using the setsockopt interface to set the key. For example, the 306 + ansi_cprng requires a seed. The DRBGs do not require a seed, but may be 307 + seeded. 308 + 309 + Using the read()/recvmsg() system calls, random numbers can be obtained. 310 + The kernel generates at most 128 bytes in one call. If user space 311 + requires more data, multiple calls to read()/recvmsg() must be made. 312 + 313 + WARNING: The user space caller may invoke the initially mentioned accept 314 + system call multiple times. In this case, the returned file descriptors 315 + have the same state. 316 + 317 + Zero-Copy Interface 318 + ------------------- 319 + 320 + In addition to the send/write/read/recv system call family, the AF_ALG 321 + interface can be accessed with the zero-copy interface of 322 + splice/vmsplice. As the name indicates, the kernel tries to avoid a copy 323 + operation into kernel space. 324 + 325 + The zero-copy operation requires data to be aligned at the page 326 + boundary. Non-aligned data can be used as well, but may require more 327 + operations of the kernel which would defeat the speed gains obtained 328 + from the zero-copy interface. 329 + 330 + The system-interent limit for the size of one zero-copy operation is 16 331 + pages. If more data is to be sent to AF_ALG, user space must slice the 332 + input into segments with a maximum size of 16 pages. 333 + 334 + Zero-copy can be used with the following code example (a complete 335 + working example is provided with libkcapi): 336 + 337 + :: 338 + 339 + int pipes[2]; 340 + 341 + pipe(pipes); 342 + /* input data in iov */ 343 + vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT); 344 + /* opfd is the file descriptor returned from accept() system call */ 345 + splice(pipes[0], NULL, opfd, NULL, ret, 0); 346 + read(opfd, out, outlen); 347 + 348 + 349 + Setsockopt Interface 350 + -------------------- 351 + 352 + In addition to the read/recv and send/write system call handling to send 353 + and retrieve data subject to the cipher operation, a consumer also needs 354 + to set the additional information for the cipher operation. This 355 + additional information is set using the setsockopt system call that must 356 + be invoked with the file descriptor of the open cipher (i.e. the file 357 + descriptor returned by the accept system call). 358 + 359 + Each setsockopt invocation must use the level SOL_ALG. 360 + 361 + The setsockopt interface allows setting the following data using the 362 + mentioned optname: 363 + 364 + - ALG_SET_KEY -- Setting the key. Key setting is applicable to: 365 + 366 + - the skcipher cipher type (symmetric ciphers) 367 + 368 + - the hash cipher type (keyed message digests) 369 + 370 + - the AEAD cipher type 371 + 372 + - the RNG cipher type to provide the seed 373 + 374 + - ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size for 375 + AEAD ciphers. For a encryption operation, the authentication tag of 376 + the given size will be generated. For a decryption operation, the 377 + provided ciphertext is assumed to contain an authentication tag of 378 + the given size (see section about AEAD memory layout below). 379 + 380 + User space API example 381 + ---------------------- 382 + 383 + Please see [1] for libkcapi which provides an easy-to-use wrapper around 384 + the aforementioned Netlink kernel interface. [1] also contains a test 385 + application that invokes all libkcapi API calls. 386 + 387 + [1] http://www.chronox.de/libkcapi.html
+1
Documentation/index.rst
··· 58 58 gpu/index 59 59 security/index 60 60 sound/index 61 + crypto/index 61 62 62 63 Korean translations 63 64 -------------------