1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2019 Google LLC
4 */
5
6 #include <errno.h>
7 #include <stdlib.h>
8
9 #define TST_NO_DEFAULT_MAIN
10 #include "tst_test.h"
11 #include "tst_af_alg.h"
12 #include "lapi/socket.h"
13
tst_alg_create(void)14 int tst_alg_create(void)
15 {
16 TEST(socket(AF_ALG, SOCK_SEQPACKET, 0));
17 if (TST_RET >= 0)
18 return TST_RET;
19 if (TST_ERR == EAFNOSUPPORT)
20 tst_brk(TCONF, "kernel doesn't support AF_ALG");
21 tst_brk(TBROK | TTERRNO, "unexpected error creating AF_ALG socket");
22 return -1;
23 }
24
tst_alg_bind_addr(int algfd,const struct sockaddr_alg * addr)25 void tst_alg_bind_addr(int algfd, const struct sockaddr_alg *addr)
26 {
27 TEST(bind(algfd, (const struct sockaddr *)addr, sizeof(*addr)));
28 if (TST_RET == 0)
29 return;
30 if (TST_ERR == ENOENT) {
31 tst_brk(TCONF, "kernel doesn't support %s algorithm '%s'",
32 addr->salg_type, addr->salg_name);
33 }
34 tst_brk(TBROK | TTERRNO,
35 "unexpected error binding AF_ALG socket to %s algorithm '%s'",
36 addr->salg_type, addr->salg_name);
37 }
38
init_sockaddr_alg(struct sockaddr_alg * addr,const char * algtype,const char * algname)39 static void init_sockaddr_alg(struct sockaddr_alg *addr,
40 const char *algtype, const char *algname)
41 {
42 memset(addr, 0, sizeof(*addr));
43
44 addr->salg_family = AF_ALG;
45
46 strncpy((char *)addr->salg_type, algtype, sizeof(addr->salg_type));
47 if (addr->salg_type[sizeof(addr->salg_type) - 1] != '\0')
48 tst_brk(TBROK, "algorithm type too long: '%s'", algtype);
49
50 strncpy((char *)addr->salg_name, algname, sizeof(addr->salg_name));
51 if (addr->salg_name[sizeof(addr->salg_name) - 1] != '\0')
52 tst_brk(TBROK, "algorithm name too long: '%s'", algname);
53 }
54
tst_alg_bind(int algfd,const char * algtype,const char * algname)55 void tst_alg_bind(int algfd, const char *algtype, const char *algname)
56 {
57 struct sockaddr_alg addr;
58
59 init_sockaddr_alg(&addr, algtype, algname);
60
61 tst_alg_bind_addr(algfd, &addr);
62 }
63
tst_have_alg(const char * algtype,const char * algname)64 bool tst_have_alg(const char *algtype, const char *algname)
65 {
66 int algfd;
67 struct sockaddr_alg addr;
68 bool have_alg = true;
69
70 algfd = tst_alg_create();
71
72 init_sockaddr_alg(&addr, algtype, algname);
73
74 TEST(bind(algfd, (const struct sockaddr *)&addr, sizeof(addr)));
75 if (TST_RET != 0) {
76 if (TST_ERR != ENOENT) {
77 tst_brk(TBROK | TTERRNO,
78 "unexpected error binding AF_ALG socket to %s algorithm '%s'",
79 algtype, algname);
80 }
81 have_alg = false;
82 }
83
84 close(algfd);
85 return have_alg;
86 }
87
tst_require_alg(const char * algtype,const char * algname)88 void tst_require_alg(const char *algtype, const char *algname)
89 {
90 int algfd = tst_alg_create();
91
92 tst_alg_bind(algfd, algtype, algname);
93
94 close(algfd);
95 }
96
tst_alg_setkey(int algfd,const uint8_t * key,unsigned int keylen)97 void tst_alg_setkey(int algfd, const uint8_t *key, unsigned int keylen)
98 {
99 uint8_t *keybuf = NULL;
100 unsigned int i;
101
102 if (key == NULL) {
103 /* generate a random key */
104 keybuf = SAFE_MALLOC(keylen);
105 for (i = 0; i < keylen; i++)
106 keybuf[i] = rand();
107 key = keybuf;
108 }
109 TEST(setsockopt(algfd, SOL_ALG, ALG_SET_KEY, key, keylen));
110 if (TST_RET != 0) {
111 tst_brk(TBROK | TTERRNO,
112 "unexpected error setting key (len=%u)", keylen);
113 }
114 free(keybuf);
115 }
116
tst_alg_accept(int algfd)117 int tst_alg_accept(int algfd)
118 {
119 TEST(accept(algfd, NULL, NULL));
120 if (TST_RET < 0) {
121 tst_brk(TBROK | TTERRNO,
122 "unexpected error accept()ing AF_ALG request socket");
123 }
124 return TST_RET;
125 }
126
tst_alg_setup(const char * algtype,const char * algname,const uint8_t * key,unsigned int keylen)127 int tst_alg_setup(const char *algtype, const char *algname,
128 const uint8_t *key, unsigned int keylen)
129 {
130 int algfd = tst_alg_create();
131
132 tst_alg_bind(algfd, algtype, algname);
133
134 if (keylen != 0)
135 tst_alg_setkey(algfd, key, keylen);
136
137 return algfd;
138 }
139
tst_alg_setup_reqfd(const char * algtype,const char * algname,const uint8_t * key,unsigned int keylen)140 int tst_alg_setup_reqfd(const char *algtype, const char *algname,
141 const uint8_t *key, unsigned int keylen)
142 {
143 int algfd = tst_alg_setup(algtype, algname, key, keylen);
144 int reqfd = tst_alg_accept(algfd);
145
146 close(algfd);
147 return reqfd;
148 }
149