linux-stable/drivers/crypto
Brian Masney a680b1832c crypto: qcom-rng - ensure buffer for generate is completely filled
The generate function in struct rng_alg expects that the destination
buffer is completely filled if the function returns 0. qcom_rng_read()
can run into a situation where the buffer is partially filled with
randomness and the remaining part of the buffer is zeroed since
qcom_rng_generate() doesn't check the return value. This issue can
be reproduced by running the following from libkcapi:

    kcapi-rng -b 9000000 > OUTFILE

The generated OUTFILE will have three huge sections that contain all
zeros, and this is caused by the code where the test
'val & PRNG_STATUS_DATA_AVAIL' fails.

Let's fix this issue by ensuring that qcom_rng_read() always returns
with a full buffer if the function returns success. Let's also have
qcom_rng_generate() return the correct value.

Here's some statistics from the ent project
(https://www.fourmilab.ch/random/) that shows information about the
quality of the generated numbers:

    $ ent -c qcom-random-before
    Value Char Occurrences Fraction
      0           606748   0.067416
      1            33104   0.003678
      2            33001   0.003667
    ...
    253   �        32883   0.003654
    254   �        33035   0.003671
    255   �        33239   0.003693

    Total:       9000000   1.000000

    Entropy = 7.811590 bits per byte.

    Optimum compression would reduce the size
    of this 9000000 byte file by 2 percent.

    Chi square distribution for 9000000 samples is 9329962.81, and
    randomly would exceed this value less than 0.01 percent of the
    times.

    Arithmetic mean value of data bytes is 119.3731 (127.5 = random).
    Monte Carlo value for Pi is 3.197293333 (error 1.77 percent).
    Serial correlation coefficient is 0.159130 (totally uncorrelated =
    0.0).

Without this patch, the results of the chi-square test is 0.01%, and
the numbers are certainly not random according to ent's project page.
The results improve with this patch:

    $ ent -c qcom-random-after
    Value Char Occurrences Fraction
      0            35432   0.003937
      1            35127   0.003903
      2            35424   0.003936
    ...
    253   �        35201   0.003911
    254   �        34835   0.003871
    255   �        35368   0.003930

    Total:       9000000   1.000000

    Entropy = 7.999979 bits per byte.

    Optimum compression would reduce the size
    of this 9000000 byte file by 0 percent.

    Chi square distribution for 9000000 samples is 258.77, and randomly
    would exceed this value 42.24 percent of the times.

    Arithmetic mean value of data bytes is 127.5006 (127.5 = random).
    Monte Carlo value for Pi is 3.141277333 (error 0.01 percent).
    Serial correlation coefficient is 0.000468 (totally uncorrelated =
    0.0).

This change was tested on a Nexus 5 phone (msm8974 SoC).

Signed-off-by: Brian Masney <bmasney@redhat.com>
Fixes: ceec5f5b59 ("crypto: qcom-rng - Add Qcom prng driver")
Cc: stable@vger.kernel.org # 4.19+
Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Reviewed-by: Andrew Halaney <ahalaney@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2022-03-14 14:41:04 +12:00
..
allwinner crypto: sun8i-ce - Add support for the D1 variant 2021-11-26 16:25:16 +11:00
amcc crypto: amcc - fix incorrect kernel-doc comment syntax in files 2021-03-26 20:15:58 +11:00
amlogic crypto: amlogic - Fix the parameter of dma_unmap_sg() 2021-03-26 20:02:34 +11:00
axis crypto: sha - split sha.h into sha1.h and sha2.h 2020-11-20 14:45:33 +11:00
bcm crypto: bcm - Fix a whole host of kernel-doc misdemeanours 2021-03-26 20:02:35 +11:00
caam crypto: caam - save caam memory to support crypto engine retry mechanism. 2021-12-03 15:56:17 +11:00
cavium crypto: cavium - Use kcalloc() instead of kzalloc() 2021-12-17 16:59:47 +11:00
ccp crypto: ccp - remove unneeded semicolon 2021-12-31 18:10:55 +11:00
ccree crypto: ccree - remove redundant 'flush_workqueue()' calls 2021-11-20 15:06:38 +11:00
chelsio treewide: Replace open-coded flex arrays in unions 2021-10-18 12:28:53 -07:00
gemini crypto: sl3516 - Typo s/Stormlink/Storlink/ 2021-06-28 11:28:08 +08:00
hisilicon crypto: hisilicon - cleanup warning in qm_get_qos_value() 2021-12-31 18:10:56 +11:00
inside-secure crypto: inside-secure - Minor typo fix in the file safexcel.c 2021-03-26 20:02:35 +11:00
keembay crypto: keembay-ocs-ecc - Fix error return code in kmb_ocs_ecc_probe() 2021-11-20 15:02:08 +11:00
marvell crypto: octeontx2 - Avoid stack variable overflow 2022-01-31 11:22:53 +11:00
nx powerpc updates for 5.14 2021-07-02 12:54:34 -07:00
qat crypto: qat - fix definition of ring reset results 2021-12-31 18:10:56 +11:00
qce crypto: qce - fix uaf on qce_skcipher_register_one 2021-11-20 15:02:08 +11:00
rockchip crypto: rockchip - delete unneeded variable initialization 2021-04-09 17:45:13 +10:00
stm32 crypto: stm32/crc32 - Fix kernel BUG triggered in probe() 2021-12-31 18:10:55 +11:00
ux500 crypto: ux500 - Use platform_get_irq() to get the interrupt 2021-12-24 14:18:28 +11:00
virtio virtio: wrap config->reset calls 2022-01-14 18:50:52 -05:00
vmx crypto: vmx - fix incorrect kernel-doc comment syntax in files 2021-03-26 20:15:58 +11:00
xilinx crypto: algapi - Remove skbuff.h inclusion 2020-08-20 14:04:28 +10:00
atmel-aes-regs.h
atmel-aes.c crypto: atmel-aes - Reestablish the correct tfm context at dequeue 2021-11-20 15:02:07 +11:00
atmel-authenc.h crypto: sha - split sha.h into sha1.h and sha2.h 2020-11-20 14:45:33 +11:00
atmel-ecc.c crypto: atmel-ecc - Struct headers need to start with keyword 'struct' 2021-03-26 20:02:36 +11:00
atmel-i2c.c crypto: atmel - use the correct print format 2021-04-16 21:16:33 +10:00
atmel-i2c.h
atmel-sha-regs.h
atmel-sha.c crypto: atmel - use the correct print format 2021-04-16 21:16:33 +10:00
atmel-sha204a.c
atmel-tdes-regs.h
atmel-tdes.c crypto: atmel - Set OFB's blocksize to 1 2021-07-30 10:58:34 +08:00
exynos-rng.c
geode-aes.c crypto: geode - use DEFINE_SPINLOCK() for spinlock 2021-04-16 21:16:31 +10:00
geode-aes.h
hifn_795x.c crypto: drivers - use semicolons rather than commas to separate statements 2020-10-02 18:02:15 +10:00
img-hash.c crypto: img-hash - remove need for error return variable ret 2021-09-17 11:06:14 +08:00
ixp4xx_crypto.c ARM: SoC changes for 5.14 2021-07-10 09:22:44 -07:00
Kconfig s390/crypto: add SIMD implementation for ChaCha20 2021-12-06 14:42:24 +01:00
Makefile crypto: sl3516 - Add sl3516 crypto engine 2021-06-11 15:03:29 +08:00
mxs-dcp.c crypto: mxs-dcp - Use sg_mapping_iter to copy data 2021-07-16 15:49:34 +08:00
n2_asm.S
n2_core.c crypto: sha - split sha.h into sha1.h and sha2.h 2020-11-20 14:45:33 +11:00
n2_core.h
omap-aes-gcm.c
omap-aes.c crypto: omap-aes - Fix broken pm_runtime_and_get() usage 2021-12-24 14:18:28 +11:00
omap-aes.h crypto: omap-aes - permit asynchronous skcipher as fallback 2020-07-16 21:49:02 +10:00
omap-crypto.c crypto: omap - Avoid redundant copy when using truncated sg list 2021-08-21 15:44:53 +08:00
omap-crypto.h
omap-des.c crypto: omap - increase priority of DES/3DES 2021-12-24 14:18:22 +11:00
omap-sham.c crypto: omap - Fix inconsistent locking of device lists 2021-08-21 15:44:53 +08:00
padlock-aes.c crypto: algapi - Remove skbuff.h inclusion 2020-08-20 14:04:28 +10:00
padlock-sha.c crypto: sha - split sha.h into sha1.h and sha2.h 2020-11-20 14:45:33 +11:00
qcom-rng.c crypto: qcom-rng - ensure buffer for generate is completely filled 2022-03-14 14:41:04 +12:00
s5p-sss.c crypto: s5p-sss - Add error handling in s5p_aes_probe() 2021-10-29 21:04:03 +08:00
sa2ul.c crypto: sa2ul - Use bitfield helpers 2021-12-03 15:56:17 +11:00
sa2ul.h crypto: sa2ul - Add support for AM64 2021-04-22 17:31:30 +10:00
sahara.c crypto: sahara - Remove unused .id_table support 2021-01-03 08:41:34 +11:00
talitos.c crypto: talitos - Fix ctr(aes) on SEC1 2021-01-29 15:57:59 +11:00
talitos.h crypto: talitos - Work around SEC6 ERRATA (AES-CTR mode data size error) 2021-01-29 15:57:58 +11:00