kernel-test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 ARM Limited.
  4. */
  5. #define _GNU_SOURCE
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/socket.h>
  15. #include <linux/kernel.h>
  16. #include <linux/if_alg.h>
  17. #define DATA_SIZE (16 * 4096)
  18. static int base, sock;
  19. static int digest_len;
  20. static char *ref;
  21. static char *digest;
  22. static char *alg_name;
  23. static struct iovec data_iov;
  24. static int zerocopy[2];
  25. static int sigs;
  26. static int iter;
  27. static void handle_exit_signal(int sig, siginfo_t *info, void *context)
  28. {
  29. printf("Terminated by signal %d, iterations=%d, signals=%d\n",
  30. sig, iter, sigs);
  31. exit(0);
  32. }
  33. static void handle_kick_signal(int sig, siginfo_t *info, void *context)
  34. {
  35. sigs++;
  36. }
  37. static char *drivers[] = {
  38. "sha1-ce",
  39. "sha224-arm64",
  40. "sha224-arm64-neon",
  41. "sha224-ce",
  42. "sha256-arm64",
  43. "sha256-arm64-neon",
  44. "sha256-ce",
  45. "sha384-ce",
  46. "sha512-ce",
  47. "sha3-224-ce",
  48. "sha3-256-ce",
  49. "sha3-384-ce",
  50. "sha3-512-ce",
  51. "sm3-ce",
  52. "sm3-neon",
  53. };
  54. static bool create_socket(void)
  55. {
  56. FILE *proc;
  57. struct sockaddr_alg addr;
  58. char buf[1024];
  59. char *c, *driver_name;
  60. bool is_shash, match;
  61. int ret, i;
  62. ret = socket(AF_ALG, SOCK_SEQPACKET, 0);
  63. if (ret < 0) {
  64. if (errno == EAFNOSUPPORT) {
  65. printf("AF_ALG not supported\n");
  66. return false;
  67. }
  68. printf("Failed to create AF_ALG socket: %s (%d)\n",
  69. strerror(errno), errno);
  70. return false;
  71. }
  72. base = ret;
  73. memset(&addr, 0, sizeof(addr));
  74. addr.salg_family = AF_ALG;
  75. strncpy((char *)addr.salg_type, "hash", sizeof(addr.salg_type));
  76. proc = fopen("/proc/crypto", "r");
  77. if (!proc) {
  78. printf("Unable to open /proc/crypto\n");
  79. return false;
  80. }
  81. driver_name = NULL;
  82. is_shash = false;
  83. match = false;
  84. /* Look through /proc/crypto for a driver with kernel mode FP usage */
  85. while (!match) {
  86. c = fgets(buf, sizeof(buf), proc);
  87. if (!c) {
  88. if (feof(proc)) {
  89. printf("Nothing found in /proc/crypto\n");
  90. return false;
  91. }
  92. continue;
  93. }
  94. /* Algorithm descriptions are separated by a blank line */
  95. if (*c == '\n') {
  96. if (is_shash && driver_name) {
  97. for (i = 0; i < ARRAY_SIZE(drivers); i++) {
  98. if (strcmp(drivers[i],
  99. driver_name) == 0) {
  100. match = true;
  101. }
  102. }
  103. }
  104. if (!match) {
  105. digest_len = 0;
  106. free(driver_name);
  107. driver_name = NULL;
  108. free(alg_name);
  109. alg_name = NULL;
  110. is_shash = false;
  111. }
  112. continue;
  113. }
  114. /* Remove trailing newline */
  115. c = strchr(buf, '\n');
  116. if (c)
  117. *c = '\0';
  118. /* Find the field/value separator and start of the value */
  119. c = strchr(buf, ':');
  120. if (!c)
  121. continue;
  122. c += 2;
  123. if (strncmp(buf, "digestsize", strlen("digestsize")) == 0)
  124. sscanf(c, "%d", &digest_len);
  125. if (strncmp(buf, "name", strlen("name")) == 0)
  126. alg_name = strdup(c);
  127. if (strncmp(buf, "driver", strlen("driver")) == 0)
  128. driver_name = strdup(c);
  129. if (strncmp(buf, "type", strlen("type")) == 0)
  130. if (strncmp(c, "shash", strlen("shash")) == 0)
  131. is_shash = true;
  132. }
  133. strncpy((char *)addr.salg_name, alg_name,
  134. sizeof(addr.salg_name) - 1);
  135. ret = bind(base, (struct sockaddr *)&addr, sizeof(addr));
  136. if (ret < 0) {
  137. printf("Failed to bind %s: %s (%d)\n",
  138. addr.salg_name, strerror(errno), errno);
  139. return false;
  140. }
  141. ret = accept(base, NULL, 0);
  142. if (ret < 0) {
  143. printf("Failed to accept %s: %s (%d)\n",
  144. addr.salg_name, strerror(errno), errno);
  145. return false;
  146. }
  147. sock = ret;
  148. ret = pipe(zerocopy);
  149. if (ret != 0) {
  150. printf("Failed to create zerocopy pipe: %s (%d)\n",
  151. strerror(errno), errno);
  152. return false;
  153. }
  154. ref = malloc(digest_len);
  155. if (!ref) {
  156. printf("Failed to allocate %d byte reference\n", digest_len);
  157. return false;
  158. }
  159. digest = malloc(digest_len);
  160. if (!digest) {
  161. printf("Failed to allocate %d byte digest\n", digest_len);
  162. return false;
  163. }
  164. return true;
  165. }
  166. static bool compute_digest(void *buf)
  167. {
  168. struct iovec iov;
  169. int ret, wrote;
  170. iov = data_iov;
  171. while (iov.iov_len) {
  172. ret = vmsplice(zerocopy[1], &iov, 1, SPLICE_F_GIFT);
  173. if (ret < 0) {
  174. printf("Failed to send buffer: %s (%d)\n",
  175. strerror(errno), errno);
  176. return false;
  177. }
  178. wrote = ret;
  179. ret = splice(zerocopy[0], NULL, sock, NULL, wrote, 0);
  180. if (ret < 0) {
  181. printf("Failed to splice buffer: %s (%d)\n",
  182. strerror(errno), errno);
  183. } else if (ret != wrote) {
  184. printf("Short splice: %d < %d\n", ret, wrote);
  185. }
  186. iov.iov_len -= wrote;
  187. iov.iov_base += wrote;
  188. }
  189. reread:
  190. ret = recv(sock, buf, digest_len, 0);
  191. if (ret == 0) {
  192. printf("No digest returned\n");
  193. return false;
  194. }
  195. if (ret != digest_len) {
  196. if (errno == -EAGAIN)
  197. goto reread;
  198. printf("Failed to get digest: %s (%d)\n",
  199. strerror(errno), errno);
  200. return false;
  201. }
  202. return true;
  203. }
  204. int main(void)
  205. {
  206. char *data;
  207. struct sigaction sa;
  208. int ret;
  209. /* Ensure we have unbuffered output */
  210. setvbuf(stdout, NULL, _IOLBF, 0);
  211. /* The parent will communicate with us via signals */
  212. memset(&sa, 0, sizeof(sa));
  213. sa.sa_sigaction = handle_exit_signal;
  214. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  215. sigemptyset(&sa.sa_mask);
  216. ret = sigaction(SIGTERM, &sa, NULL);
  217. if (ret < 0)
  218. printf("Failed to install SIGTERM handler: %s (%d)\n",
  219. strerror(errno), errno);
  220. sa.sa_sigaction = handle_kick_signal;
  221. ret = sigaction(SIGUSR1, &sa, NULL);
  222. if (ret < 0)
  223. printf("Failed to install SIGUSR1 handler: %s (%d)\n",
  224. strerror(errno), errno);
  225. ret = sigaction(SIGUSR2, &sa, NULL);
  226. if (ret < 0)
  227. printf("Failed to install SIGUSR2 handler: %s (%d)\n",
  228. strerror(errno), errno);
  229. data = malloc(DATA_SIZE);
  230. if (!data) {
  231. printf("Failed to allocate data buffer\n");
  232. return EXIT_FAILURE;
  233. }
  234. memset(data, 0, DATA_SIZE);
  235. data_iov.iov_base = data;
  236. data_iov.iov_len = DATA_SIZE;
  237. /*
  238. * If we can't create a socket assume it's a lack of system
  239. * support and fall back to a basic FPSIMD test for the
  240. * benefit of fp-stress.
  241. */
  242. if (!create_socket()) {
  243. execl("./fpsimd-test", "./fpsimd-test", NULL);
  244. printf("Failed to fall back to fspimd-test: %d (%s)\n",
  245. errno, strerror(errno));
  246. return EXIT_FAILURE;
  247. }
  248. /*
  249. * Compute a reference digest we hope is repeatable, we do
  250. * this at runtime partly to make it easier to play with
  251. * parameters.
  252. */
  253. if (!compute_digest(ref)) {
  254. printf("Failed to compute reference digest\n");
  255. return EXIT_FAILURE;
  256. }
  257. printf("AF_ALG using %s\n", alg_name);
  258. while (true) {
  259. if (!compute_digest(digest)) {
  260. printf("Failed to compute digest, iter=%d\n", iter);
  261. return EXIT_FAILURE;
  262. }
  263. if (memcmp(ref, digest, digest_len) != 0) {
  264. printf("Digest mismatch, iter=%d\n", iter);
  265. return EXIT_FAILURE;
  266. }
  267. iter++;
  268. }
  269. return EXIT_FAILURE;
  270. }