| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * CRC vpmsum tester |
|---|
| 3 | 4 | * Copyright 2017 Daniel Axtens, IBM Corporation. |
|---|
| 4 | | - * |
|---|
| 5 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 7 | | - * published by the Free Software Foundation. |
|---|
| 8 | 5 | */ |
|---|
| 9 | 6 | |
|---|
| 10 | 7 | #include <linux/crc-t10dif.h> |
|---|
| .. | .. |
|---|
| 12 | 9 | #include <crypto/internal/hash.h> |
|---|
| 13 | 10 | #include <linux/init.h> |
|---|
| 14 | 11 | #include <linux/module.h> |
|---|
| 12 | +#include <linux/random.h> |
|---|
| 15 | 13 | #include <linux/string.h> |
|---|
| 16 | 14 | #include <linux/kernel.h> |
|---|
| 17 | 15 | #include <linux/cpufeature.h> |
|---|
| .. | .. |
|---|
| 25 | 23 | static int __init crc_test_init(void) |
|---|
| 26 | 24 | { |
|---|
| 27 | 25 | u16 crc16 = 0, verify16 = 0; |
|---|
| 28 | | - u32 crc32 = 0, verify32 = 0; |
|---|
| 29 | 26 | __le32 verify32le = 0; |
|---|
| 30 | 27 | unsigned char *data; |
|---|
| 28 | + u32 verify32 = 0; |
|---|
| 31 | 29 | unsigned long i; |
|---|
| 30 | + __le32 crc32; |
|---|
| 32 | 31 | int ret; |
|---|
| 33 | 32 | |
|---|
| 34 | 33 | struct crypto_shash *crct10dif_tfm; |
|---|
| .. | .. |
|---|
| 78 | 77 | |
|---|
| 79 | 78 | pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations); |
|---|
| 80 | 79 | for (i=0; i<iterations; i++) { |
|---|
| 81 | | - size_t len, offset; |
|---|
| 80 | + size_t offset = prandom_u32_max(16); |
|---|
| 81 | + size_t len = prandom_u32_max(MAX_CRC_LENGTH); |
|---|
| 82 | 82 | |
|---|
| 83 | | - get_random_bytes(data, MAX_CRC_LENGTH); |
|---|
| 84 | | - get_random_bytes(&len, sizeof(len)); |
|---|
| 85 | | - get_random_bytes(&offset, sizeof(offset)); |
|---|
| 86 | | - |
|---|
| 87 | | - len %= MAX_CRC_LENGTH; |
|---|
| 88 | | - offset &= 15; |
|---|
| 89 | 83 | if (len <= offset) |
|---|
| 90 | 84 | continue; |
|---|
| 85 | + prandom_bytes(data, len); |
|---|
| 91 | 86 | len -= offset; |
|---|
| 92 | 87 | |
|---|
| 93 | 88 | crypto_shash_update(crct10dif_shash, data+offset, len); |
|---|
| .. | .. |
|---|
| 105 | 100 | crypto_shash_final(crc32c_shash, (u8 *)(&crc32)); |
|---|
| 106 | 101 | verify32 = le32_to_cpu(verify32le); |
|---|
| 107 | 102 | verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len)); |
|---|
| 108 | | - if (crc32 != (u32)verify32le) { |
|---|
| 103 | + if (crc32 != verify32le) { |
|---|
| 109 | 104 | pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n", |
|---|
| 110 | 105 | crc32, verify32, len); |
|---|
| 111 | 106 | break; |
|---|
| 112 | 107 | } |
|---|
| 108 | + cond_resched(); |
|---|
| 113 | 109 | } |
|---|
| 114 | 110 | pr_info("crc-vpmsum_test done, completed %lu iterations\n", i); |
|---|
| 115 | 111 | } while (0); |
|---|