Skip to content

Commit 4d2fa8b

Browse files
committed
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
Pull crypto updates from Herbert Xu: "Here is the crypto update for 5.3: API: - Test shash interface directly in testmgr - cra_driver_name is now mandatory Algorithms: - Replace arc4 crypto_cipher with library helper - Implement 5 way interleave for ECB, CBC and CTR on arm64 - Add xxhash - Add continuous self-test on noise source to drbg - Update jitter RNG Drivers: - Add support for SHA204A random number generator - Add support for 7211 in iproc-rng200 - Fix fuzz test failures in inside-secure - Fix fuzz test failures in talitos - Fix fuzz test failures in qat" * 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (143 commits) crypto: stm32/hash - remove interruptible condition for dma crypto: stm32/hash - Fix hmac issue more than 256 bytes crypto: stm32/crc32 - rename driver file crypto: amcc - remove memset after dma_alloc_coherent crypto: ccp - Switch to SPDX license identifiers crypto: ccp - Validate the the error value used to index error messages crypto: doc - Fix formatting of new crypto engine content crypto: doc - Add parameter documentation crypto: arm64/aes-ce - implement 5 way interleave for ECB, CBC and CTR crypto: arm64/aes-ce - add 5 way interleave routines crypto: talitos - drop icv_ool crypto: talitos - fix hash on SEC1. crypto: talitos - move struct talitos_edesc into talitos.h lib/scatterlist: Fix mapping iterator when sg->offset is greater than PAGE_SIZE crypto/NX: Set receive window credits to max number of CRBs in RxFIFO crypto: asymmetric_keys - select CRYPTO_HASH where needed crypto: serpent - mark __serpent_setkey_sbox noinline crypto: testmgr - dynamically allocate crypto_shash crypto: testmgr - dynamically allocate testvec_config crypto: talitos - eliminate unneeded 'done' functions at build time ...
2 parents 8b68150 + f3880a2 commit 4d2fa8b

168 files changed

Lines changed: 4527 additions & 3798 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/crypto/api-samples.rst‎

Lines changed: 77 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,111 +4,89 @@ Code Examples
44
Code Example For Symmetric Key Cipher Operation
55
-----------------------------------------------
66

7-
::
8-
9-
10-
/* tie all data structures together */
11-
struct skcipher_def {
12-
struct scatterlist sg;
13-
struct crypto_skcipher *tfm;
14-
struct skcipher_request *req;
15-
struct crypto_wait wait;
16-
};
17-
18-
/* Perform cipher operation */
19-
static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
20-
int enc)
21-
{
22-
int rc;
23-
24-
if (enc)
25-
rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
26-
else
27-
rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);
28-
29-
if (rc)
30-
pr_info("skcipher encrypt returned with result %d\n", rc);
7+
This code encrypts some data with AES-256-XTS. For sake of example,
8+
all inputs are random bytes, the encryption is done in-place, and it's
9+
assumed the code is running in a context where it can sleep.
3110

32-
return rc;
33-
}
11+
::
3412

35-
/* Initialize and trigger cipher operation */
3613
static int test_skcipher(void)
3714
{
38-
struct skcipher_def sk;
39-
struct crypto_skcipher *skcipher = NULL;
40-
struct skcipher_request *req = NULL;
41-
char *scratchpad = NULL;
42-
char *ivdata = NULL;
43-
unsigned char key[32];
44-
int ret = -EFAULT;
45-
46-
skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0);
47-
if (IS_ERR(skcipher)) {
48-
pr_info("could not allocate skcipher handle\n");
49-
return PTR_ERR(skcipher);
50-
}
51-
52-
req = skcipher_request_alloc(skcipher, GFP_KERNEL);
53-
if (!req) {
54-
pr_info("could not allocate skcipher request\n");
55-
ret = -ENOMEM;
56-
goto out;
57-
}
58-
59-
skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
60-
crypto_req_done,
61-
&sk.wait);
62-
63-
/* AES 256 with random key */
64-
get_random_bytes(&key, 32);
65-
if (crypto_skcipher_setkey(skcipher, key, 32)) {
66-
pr_info("key could not be set\n");
67-
ret = -EAGAIN;
68-
goto out;
69-
}
70-
71-
/* IV will be random */
72-
ivdata = kmalloc(16, GFP_KERNEL);
73-
if (!ivdata) {
74-
pr_info("could not allocate ivdata\n");
75-
goto out;
76-
}
77-
get_random_bytes(ivdata, 16);
78-
79-
/* Input data will be random */
80-
scratchpad = kmalloc(16, GFP_KERNEL);
81-
if (!scratchpad) {
82-
pr_info("could not allocate scratchpad\n");
83-
goto out;
84-
}
85-
get_random_bytes(scratchpad, 16);
86-
87-
sk.tfm = skcipher;
88-
sk.req = req;
89-
90-
/* We encrypt one block */
91-
sg_init_one(&sk.sg, scratchpad, 16);
92-
skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
93-
crypto_init_wait(&sk.wait);
94-
95-
/* encrypt data */
96-
ret = test_skcipher_encdec(&sk, 1);
97-
if (ret)
98-
goto out;
99-
100-
pr_info("Encryption triggered successfully\n");
101-
15+
struct crypto_skcipher *tfm = NULL;
16+
struct skcipher_request *req = NULL;
17+
u8 *data = NULL;
18+
const size_t datasize = 512; /* data size in bytes */
19+
struct scatterlist sg;
20+
DECLARE_CRYPTO_WAIT(wait);
21+
u8 iv[16]; /* AES-256-XTS takes a 16-byte IV */
22+
u8 key[64]; /* AES-256-XTS takes a 64-byte key */
23+
int err;
24+
25+
/*
26+
* Allocate a tfm (a transformation object) and set the key.
27+
*
28+
* In real-world use, a tfm and key are typically used for many
29+
* encryption/decryption operations. But in this example, we'll just do a
30+
* single encryption operation with it (which is not very efficient).
31+
*/
32+
33+
tfm = crypto_alloc_skcipher("xts(aes)", 0, 0);
34+
if (IS_ERR(tfm)) {
35+
pr_err("Error allocating xts(aes) handle: %ld\n", PTR_ERR(tfm));
36+
return PTR_ERR(tfm);
37+
}
38+
39+
get_random_bytes(key, sizeof(key));
40+
err = crypto_skcipher_setkey(tfm, key, sizeof(key));
41+
if (err) {
42+
pr_err("Error setting key: %d\n", err);
43+
goto out;
44+
}
45+
46+
/* Allocate a request object */
47+
req = skcipher_request_alloc(tfm, GFP_KERNEL);
48+
if (!req) {
49+
err = -ENOMEM;
50+
goto out;
51+
}
52+
53+
/* Prepare the input data */
54+
data = kmalloc(datasize, GFP_KERNEL);
55+
if (!data) {
56+
err = -ENOMEM;
57+
goto out;
58+
}
59+
get_random_bytes(data, datasize);
60+
61+
/* Initialize the IV */
62+
get_random_bytes(iv, sizeof(iv));
63+
64+
/*
65+
* Encrypt the data in-place.
66+
*
67+
* For simplicity, in this example we wait for the request to complete
68+
* before proceeding, even if the underlying implementation is asynchronous.
69+
*
70+
* To decrypt instead of encrypt, just change crypto_skcipher_encrypt() to
71+
* crypto_skcipher_decrypt().
72+
*/
73+
sg_init_one(&sg, data, datasize);
74+
skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
75+
CRYPTO_TFM_REQ_MAY_SLEEP,
76+
crypto_req_done, &wait);
77+
skcipher_request_set_crypt(req, &sg, &sg, datasize, iv);
78+
err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
79+
if (err) {
80+
pr_err("Error encrypting data: %d\n", err);
81+
goto out;
82+
}
83+
84+
pr_debug("Encryption was successful\n");
10285
out:
103-
if (skcipher)
104-
crypto_free_skcipher(skcipher);
105-
if (req)
86+
crypto_free_skcipher(tfm);
10687
skcipher_request_free(req);
107-
if (ivdata)
108-
kfree(ivdata);
109-
if (scratchpad)
110-
kfree(scratchpad);
111-
return ret;
88+
kfree(data);
89+
return err;
11290
}
11391

11492

‎Documentation/crypto/api-skcipher.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Block Cipher Algorithm Definitions
55
:doc: Block Cipher Algorithm Definitions
66

77
.. kernel-doc:: include/linux/crypto.h
8-
:functions: crypto_alg ablkcipher_alg blkcipher_alg cipher_alg
8+
:functions: crypto_alg ablkcipher_alg blkcipher_alg cipher_alg compress_alg
99

1010
Symmetric Key Cipher API
1111
------------------------

‎Documentation/crypto/architecture.rst‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ the aforementioned cipher types:
208208
- CRYPTO_ALG_TYPE_KPP Key-agreement Protocol Primitive (KPP) such as
209209
an ECDH or DH implementation
210210

211-
- CRYPTO_ALG_TYPE_DIGEST Raw message digest
212-
213-
- CRYPTO_ALG_TYPE_HASH Alias for CRYPTO_ALG_TYPE_DIGEST
211+
- CRYPTO_ALG_TYPE_HASH Raw message digest
214212

215213
- CRYPTO_ALG_TYPE_SHASH Synchronous multi-block hash
216214

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,85 @@
1-
=============
2-
CRYPTO ENGINE
1+
.. SPDX-License-Identifier: GPL-2.0
2+
Crypto Engine
33
=============
44

55
Overview
66
--------
7-
The crypto engine API (CE), is a crypto queue manager.
7+
The crypto engine (CE) API is a crypto queue manager.
88

99
Requirement
1010
-----------
11-
You have to put at start of your tfm_ctx the struct crypto_engine_ctx::
11+
You must put, at the start of your transform context your_tfm_ctx, the structure
12+
crypto_engine:
13+
14+
::
1215

13-
struct your_tfm_ctx {
14-
struct crypto_engine_ctx enginectx;
15-
...
16-
};
16+
struct your_tfm_ctx {
17+
struct crypto_engine engine;
18+
...
19+
};
1720

18-
Why: Since CE manage only crypto_async_request, it cannot know the underlying
19-
request_type and so have access only on the TFM.
20-
So using container_of for accessing __ctx is impossible.
21-
Furthermore, the crypto engine cannot know the "struct your_tfm_ctx",
22-
so it must assume that crypto_engine_ctx is at start of it.
21+
The crypto engine only manages asynchronous requests in the form of
22+
crypto_async_request. It cannot know the underlying request type and thus only
23+
has access to the transform structure. It is not possible to access the context
24+
using container_of. In addition, the engine knows nothing about your
25+
structure "``struct your_tfm_ctx``". The engine assumes (requires) the placement
26+
of the known member ``struct crypto_engine`` at the beginning.
2327

2428
Order of operations
2529
-------------------
26-
You have to obtain a struct crypto_engine via crypto_engine_alloc_init().
27-
And start it via crypto_engine_start().
28-
29-
Before transferring any request, you have to fill the enginectx.
30-
- prepare_request: (taking a function pointer) If you need to do some processing before doing the request
31-
- unprepare_request: (taking a function pointer) Undoing what's done in prepare_request
32-
- do_one_request: (taking a function pointer) Do encryption for current request
33-
34-
Note: that those three functions get the crypto_async_request associated with the received request.
35-
So your need to get the original request via container_of(areq, struct yourrequesttype_request, base);
36-
37-
When your driver receive a crypto_request, you have to transfer it to
38-
the cryptoengine via one of:
39-
- crypto_transfer_ablkcipher_request_to_engine()
40-
- crypto_transfer_aead_request_to_engine()
41-
- crypto_transfer_akcipher_request_to_engine()
42-
- crypto_transfer_hash_request_to_engine()
43-
- crypto_transfer_skcipher_request_to_engine()
44-
45-
At the end of the request process, a call to one of the following function is needed:
46-
- crypto_finalize_ablkcipher_request
47-
- crypto_finalize_aead_request
48-
- crypto_finalize_akcipher_request
49-
- crypto_finalize_hash_request
50-
- crypto_finalize_skcipher_request
30+
You are required to obtain a struct crypto_engine via ``crypto_engine_alloc_init()``.
31+
Start it via ``crypto_engine_start()``. When finished with your work, shut down the
32+
engine using ``crypto_engine_stop()`` and destroy the engine with
33+
``crypto_engine_exit()``.
34+
35+
Before transferring any request, you have to fill the context enginectx by
36+
providing functions for the following:
37+
38+
* ``prepare_crypt_hardware``: Called once before any prepare functions are
39+
called.
40+
41+
* ``unprepare_crypt_hardware``: Called once after all unprepare functions have
42+
been called.
43+
44+
* ``prepare_cipher_request``/``prepare_hash_request``: Called before each
45+
corresponding request is performed. If some processing or other preparatory
46+
work is required, do it here.
47+
48+
* ``unprepare_cipher_request``/``unprepare_hash_request``: Called after each
49+
request is handled. Clean up / undo what was done in the prepare function.
50+
51+
* ``cipher_one_request``/``hash_one_request``: Handle the current request by
52+
performing the operation.
53+
54+
Note that these functions access the crypto_async_request structure
55+
associated with the received request. You are able to retrieve the original
56+
request by using:
57+
58+
::
59+
60+
container_of(areq, struct yourrequesttype_request, base);
61+
62+
When your driver receives a crypto_request, you must to transfer it to
63+
the crypto engine via one of:
64+
65+
* crypto_transfer_ablkcipher_request_to_engine()
66+
67+
* crypto_transfer_aead_request_to_engine()
68+
69+
* crypto_transfer_akcipher_request_to_engine()
70+
71+
* crypto_transfer_hash_request_to_engine()
72+
73+
* crypto_transfer_skcipher_request_to_engine()
74+
75+
At the end of the request process, a call to one of the following functions is needed:
76+
77+
* crypto_finalize_ablkcipher_request()
78+
79+
* crypto_finalize_aead_request()
80+
81+
* crypto_finalize_akcipher_request()
82+
83+
* crypto_finalize_hash_request()
84+
85+
* crypto_finalize_skcipher_request()

‎Documentation/devicetree/bindings/crypto/atmel-crypto.txt‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,3 @@ sha@f8034000 {
6666
dmas = <&dma1 2 17>;
6767
dma-names = "tx";
6868
};
69-
70-
* Eliptic Curve Cryptography (I2C)
71-
72-
Required properties:
73-
- compatible : must be "atmel,atecc508a".
74-
- reg: I2C bus address of the device.
75-
- clock-frequency: must be present in the i2c controller node.
76-
77-
Example:
78-
atecc508a@c0 {
79-
compatible = "atmel,atecc508a";
80-
reg = <0xC0>;
81-
};

‎Documentation/devicetree/bindings/rng/brcm,iproc-rng200.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ HWRNG support for the iproc-rng200 driver
22

33
Required properties:
44
- compatible : Must be one of:
5+
"brcm,bcm7211-rng200"
56
"brcm,bcm7278-rng200"
67
"brcm,iproc-rng200"
78
- reg : base address and size of control register block

‎Documentation/devicetree/bindings/trivial-devices.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ properties:
5252
- at,24c08
5353
# i2c trusted platform module (TPM)
5454
- atmel,at97sc3204t
55+
# i2c h/w symmetric crypto module
56+
- atmel,atsha204a
57+
# i2c h/w elliptic curve crypto module
58+
- atmel,atecc508a
5559
# CM32181: Ambient Light Sensor
5660
- capella,cm32181
5761
# CM3232: Ambient Light Sensor

‎MAINTAINERS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,6 +4257,7 @@ F: crypto/
42574257
F: drivers/crypto/
42584258
F: include/crypto/
42594259
F: include/linux/crypto*
4260+
F: lib/crypto/
42604261

42614262
CRYPTOGRAPHIC RANDOM NUMBER GENERATOR
42624263
M: Neil Horman <nhorman@tuxdriver.com>

0 commit comments

Comments
 (0)