| 1 |
From 9f0a5a3b57be75450c7eaa12a4721fc796099517 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephan Mueller <[email protected]> |
| 3 |
Date: Thu, 10 Aug 2017 08:15:15 +0200 |
| 4 |
Subject: [PATCH v9 1/3] crypto: AF_ALG -- add sign/verify API |
| 5 |
|
| 6 |
Add the flags for handling signature generation and signature |
| 7 |
verification. |
| 8 |
|
| 9 |
The af_alg helper code as well as the algif_skcipher and algif_aead code |
| 10 |
must be changed from a boolean indicating the cipher operation to an |
| 11 |
integer because there are now 4 different cipher operations that are |
| 12 |
defined. Yet, the algif_aead and algif_skcipher code still only allows |
| 13 |
encryption and decryption cipher operations. |
| 14 |
|
| 15 |
Signed-off-by: Stephan Mueller <[email protected]> |
| 16 |
Signed-off-by: Tadeusz Struk <[email protected]> |
| 17 |
--- |
| 18 |
crypto/af_alg.c | 10 +++++----- |
| 19 |
crypto/algif_aead.c | 36 ++++++++++++++++++++++++------------ |
| 20 |
crypto/algif_skcipher.c | 26 +++++++++++++++++--------- |
| 21 |
include/crypto/if_alg.h | 4 ++-- |
| 22 |
include/uapi/linux/if_alg.h | 2 ++ |
| 23 |
5 files changed, 50 insertions(+), 28 deletions(-) |
| 24 |
|
| 25 |
diff --git a/crypto/af_alg.c b/crypto/af_alg.c |
| 26 |
index 337cf382718e..cbb348e15a17 100644 |
| 27 |
--- a/crypto/af_alg.c |
| 28 |
+++ b/crypto/af_alg.c |
| 29 |
@@ -857,7 +857,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 30 |
struct af_alg_tsgl *sgl; |
| 31 |
struct af_alg_control con = {}; |
| 32 |
long copied = 0; |
| 33 |
- bool enc = 0; |
| 34 |
+ int op = 0; |
| 35 |
bool init = 0; |
| 36 |
int err = 0; |
| 37 |
|
| 38 |
@@ -868,11 +868,11 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 39 |
|
| 40 |
init = 1; |
| 41 |
switch (con.op) { |
| 42 |
+ case ALG_OP_VERIFY: |
| 43 |
+ case ALG_OP_SIGN: |
| 44 |
case ALG_OP_ENCRYPT: |
| 45 |
- enc = 1; |
| 46 |
- break; |
| 47 |
case ALG_OP_DECRYPT: |
| 48 |
- enc = 0; |
| 49 |
+ op = con.op; |
| 50 |
break; |
| 51 |
default: |
| 52 |
return -EINVAL; |
| 53 |
@@ -889,7 +889,7 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 54 |
} |
| 55 |
|
| 56 |
if (init) { |
| 57 |
- ctx->enc = enc; |
| 58 |
+ ctx->op = op; |
| 59 |
if (con.iv) |
| 60 |
memcpy(ctx->iv, con.iv->iv, ivsize); |
| 61 |
|
| 62 |
diff --git a/crypto/algif_aead.c b/crypto/algif_aead.c |
| 63 |
index 516b38c3a169..77abc04cf942 100644 |
| 64 |
--- a/crypto/algif_aead.c |
| 65 |
+++ b/crypto/algif_aead.c |
| 66 |
@@ -60,7 +60,7 @@ static inline bool aead_sufficient_data(struct sock *sk) |
| 67 |
* The minimum amount of memory needed for an AEAD cipher is |
| 68 |
* the AAD and in case of decryption the tag. |
| 69 |
*/ |
| 70 |
- return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as); |
| 71 |
+ return ctx->used >= ctx->aead_assoclen + (ctx->op ? 0 : as); |
| 72 |
} |
| 73 |
|
| 74 |
static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
| 75 |
@@ -137,7 +137,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 76 |
* buffer provides the tag which is consumed resulting in only the |
| 77 |
* plaintext without a buffer for the tag returned to the caller. |
| 78 |
*/ |
| 79 |
- if (ctx->enc) |
| 80 |
+ if (ctx->op) |
| 81 |
outlen = used + as; |
| 82 |
else |
| 83 |
outlen = used - as; |
| 84 |
@@ -196,7 +196,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 85 |
/* Use the RX SGL as source (and destination) for crypto op. */ |
| 86 |
src = areq->first_rsgl.sgl.sg; |
| 87 |
|
| 88 |
- if (ctx->enc) { |
| 89 |
+ if (ctx->op == ALG_OP_ENCRYPT) { |
| 90 |
/* |
| 91 |
* Encryption operation - The in-place cipher operation is |
| 92 |
* achieved by the following operation: |
| 93 |
@@ -212,7 +212,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 94 |
if (err) |
| 95 |
goto free; |
| 96 |
af_alg_pull_tsgl(sk, processed, NULL, 0); |
| 97 |
- } else { |
| 98 |
+ } else if (ctx->op == ALG_OP_DECRYPT) { |
| 99 |
/* |
| 100 |
* Decryption operation - To achieve an in-place cipher |
| 101 |
* operation, the following SGL structure is used: |
| 102 |
@@ -258,6 +258,9 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 103 |
} else |
| 104 |
/* no RX SGL present (e.g. authentication only) */ |
| 105 |
src = areq->tsgl; |
| 106 |
+ } else { |
| 107 |
+ err = -EOPNOTSUPP; |
| 108 |
+ goto free; |
| 109 |
} |
| 110 |
|
| 111 |
/* Initialize the crypto operation */ |
| 112 |
@@ -272,19 +275,28 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 113 |
aead_request_set_callback(&areq->cra_u.aead_req, |
| 114 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 115 |
af_alg_async_cb, areq); |
| 116 |
- err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 117 |
- crypto_aead_decrypt(&areq->cra_u.aead_req); |
| 118 |
- } else { |
| 119 |
+ } else |
| 120 |
/* Synchronous operation */ |
| 121 |
aead_request_set_callback(&areq->cra_u.aead_req, |
| 122 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 123 |
af_alg_complete, &ctx->completion); |
| 124 |
- err = af_alg_wait_for_completion(ctx->enc ? |
| 125 |
- crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 126 |
- crypto_aead_decrypt(&areq->cra_u.aead_req), |
| 127 |
- &ctx->completion); |
| 128 |
+ |
| 129 |
+ switch (ctx->op) { |
| 130 |
+ case ALG_OP_ENCRYPT: |
| 131 |
+ err = crypto_aead_encrypt(&areq->cra_u.aead_req); |
| 132 |
+ break; |
| 133 |
+ case ALG_OP_DECRYPT: |
| 134 |
+ err = crypto_aead_decrypt(&areq->cra_u.aead_req); |
| 135 |
+ break; |
| 136 |
+ default: |
| 137 |
+ err = -EOPNOTSUPP; |
| 138 |
+ goto free; |
| 139 |
} |
| 140 |
|
| 141 |
+ /* Wait for synchronous operation completion */ |
| 142 |
+ if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) |
| 143 |
+ err = af_alg_wait_for_completion(err, &ctx->completion); |
| 144 |
+ |
| 145 |
/* AIO operation in progress */ |
| 146 |
if (err == -EINPROGRESS) { |
| 147 |
sock_hold(sk); |
| 148 |
@@ -552,7 +564,7 @@ static int aead_accept_parent_nokey(void *private, struct sock *sk) |
| 149 |
ctx->rcvused = 0; |
| 150 |
ctx->more = 0; |
| 151 |
ctx->merge = 0; |
| 152 |
- ctx->enc = 0; |
| 153 |
+ ctx->op = 0; |
| 154 |
ctx->aead_assoclen = 0; |
| 155 |
af_alg_init_completion(&ctx->completion); |
| 156 |
|
| 157 |
diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c |
| 158 |
index 8ae4170aaeb4..3bf761868689 100644 |
| 159 |
--- a/crypto/algif_skcipher.c |
| 160 |
+++ b/crypto/algif_skcipher.c |
| 161 |
@@ -121,22 +121,30 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
| 162 |
skcipher_request_set_callback(&areq->cra_u.skcipher_req, |
| 163 |
CRYPTO_TFM_REQ_MAY_SLEEP, |
| 164 |
af_alg_async_cb, areq); |
| 165 |
- err = ctx->enc ? |
| 166 |
- crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) : |
| 167 |
- crypto_skcipher_decrypt(&areq->cra_u.skcipher_req); |
| 168 |
- } else { |
| 169 |
+ } else |
| 170 |
/* Synchronous operation */ |
| 171 |
skcipher_request_set_callback(&areq->cra_u.skcipher_req, |
| 172 |
CRYPTO_TFM_REQ_MAY_SLEEP | |
| 173 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 174 |
af_alg_complete, |
| 175 |
&ctx->completion); |
| 176 |
- err = af_alg_wait_for_completion(ctx->enc ? |
| 177 |
- crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) : |
| 178 |
- crypto_skcipher_decrypt(&areq->cra_u.skcipher_req), |
| 179 |
- &ctx->completion); |
| 180 |
+ |
| 181 |
+ switch (ctx->op) { |
| 182 |
+ case ALG_OP_ENCRYPT: |
| 183 |
+ err = crypto_skcipher_encrypt(&areq->cra_u.skcipher_req); |
| 184 |
+ break; |
| 185 |
+ case ALG_OP_DECRYPT: |
| 186 |
+ err = crypto_skcipher_decrypt(&areq->cra_u.skcipher_req); |
| 187 |
+ break; |
| 188 |
+ default: |
| 189 |
+ err = -EOPNOTSUPP; |
| 190 |
+ goto free; |
| 191 |
} |
| 192 |
|
| 193 |
+ /* Wait for synchronous operation completion */ |
| 194 |
+ if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) |
| 195 |
+ err = af_alg_wait_for_completion(err, &ctx->completion); |
| 196 |
+ |
| 197 |
/* AIO operation in progress */ |
| 198 |
if (err == -EINPROGRESS) { |
| 199 |
sock_hold(sk); |
| 200 |
@@ -387,7 +395,7 @@ static int skcipher_accept_parent_nokey(void *private, struct sock *sk) |
| 201 |
ctx->rcvused = 0; |
| 202 |
ctx->more = 0; |
| 203 |
ctx->merge = 0; |
| 204 |
- ctx->enc = 0; |
| 205 |
+ ctx->op = 0; |
| 206 |
af_alg_init_completion(&ctx->completion); |
| 207 |
|
| 208 |
ask->private = ctx; |
| 209 |
diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h |
| 210 |
index 75ec9c662268..50a21488f3ba 100644 |
| 211 |
--- a/include/crypto/if_alg.h |
| 212 |
+++ b/include/crypto/if_alg.h |
| 213 |
@@ -142,7 +142,7 @@ struct af_alg_async_req { |
| 214 |
* @more: More data to be expected from user space? |
| 215 |
* @merge: Shall new data from user space be merged into existing |
| 216 |
* SG? |
| 217 |
- * @enc: Cryptographic operation to be performed when |
| 218 |
+ * @op: Cryptographic operation to be performed when |
| 219 |
* recvmsg is invoked. |
| 220 |
* @len: Length of memory allocated for this data structure. |
| 221 |
*/ |
| 222 |
@@ -159,7 +159,7 @@ struct af_alg_ctx { |
| 223 |
|
| 224 |
bool more; |
| 225 |
bool merge; |
| 226 |
- bool enc; |
| 227 |
+ int op; |
| 228 |
|
| 229 |
unsigned int len; |
| 230 |
}; |
| 231 |
diff --git a/include/uapi/linux/if_alg.h b/include/uapi/linux/if_alg.h |
| 232 |
index f2acd2fde1f3..d81dcca5bdd7 100644 |
| 233 |
--- a/include/uapi/linux/if_alg.h |
| 234 |
+++ b/include/uapi/linux/if_alg.h |
| 235 |
@@ -38,5 +38,7 @@ struct af_alg_iv { |
| 236 |
/* Operations */ |
| 237 |
#define ALG_OP_DECRYPT 0 |
| 238 |
#define ALG_OP_ENCRYPT 1 |
| 239 |
+#define ALG_OP_SIGN 2 |
| 240 |
+#define ALG_OP_VERIFY 3 |
| 241 |
|
| 242 |
#endif /* _LINUX_IF_ALG_H */ |
| 243 |
-- |
| 244 |
2.13.5 |
| 245 |
|