| 1 |
From 0dc7f22e1d5dbf2a974c9400d42a63db095891ed Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephan Mueller <[email protected]> |
| 3 |
Date: Sun, 7 Jan 2018 11:49:09 +0100 |
| 4 |
Subject: [PATCH v10 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 | 31 ++++++++++++++++++++----------- |
| 20 |
crypto/algif_skcipher.c | 25 +++++++++++++++++-------- |
| 21 |
include/crypto/if_alg.h | 4 ++-- |
| 22 |
include/uapi/linux/if_alg.h | 2 ++ |
| 23 |
5 files changed, 46 insertions(+), 26 deletions(-) |
| 24 |
|
| 25 |
diff --git a/crypto/af_alg.c b/crypto/af_alg.c |
| 26 |
index 35d4dcea381f..bd3d6e147623 100644 |
| 27 |
--- a/crypto/af_alg.c |
| 28 |
+++ b/crypto/af_alg.c |
| 29 |
@@ -831,7 +831,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 |
@@ -842,11 +842,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 |
@@ -863,7 +863,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 d963c8cf8a55..f956091e6909 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 |
@@ -76,6 +76,19 @@ static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
| 76 |
return af_alg_sendmsg(sock, msg, size, ivsize); |
| 77 |
} |
| 78 |
|
| 79 |
+static inline int aead_cipher_op(struct af_alg_ctx *ctx, |
| 80 |
+ struct af_alg_async_req *areq) |
| 81 |
+{ |
| 82 |
+ switch (ctx->op) { |
| 83 |
+ case ALG_OP_ENCRYPT: |
| 84 |
+ return crypto_aead_encrypt(&areq->cra_u.aead_req); |
| 85 |
+ case ALG_OP_DECRYPT: |
| 86 |
+ return crypto_aead_decrypt(&areq->cra_u.aead_req); |
| 87 |
+ default: |
| 88 |
+ return -EOPNOTSUPP; |
| 89 |
+ } |
| 90 |
+} |
| 91 |
+ |
| 92 |
static int crypto_aead_copy_sgl(struct crypto_skcipher *null_tfm, |
| 93 |
struct scatterlist *src, |
| 94 |
struct scatterlist *dst, unsigned int len) |
| 95 |
@@ -143,7 +156,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 96 |
* buffer provides the tag which is consumed resulting in only the |
| 97 |
* plaintext without a buffer for the tag returned to the caller. |
| 98 |
*/ |
| 99 |
- if (ctx->enc) |
| 100 |
+ if (ctx->op) |
| 101 |
outlen = used + as; |
| 102 |
else |
| 103 |
outlen = used - as; |
| 104 |
@@ -217,7 +230,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 105 |
/* Use the RX SGL as source (and destination) for crypto op. */ |
| 106 |
rsgl_src = areq->first_rsgl.sgl.sg; |
| 107 |
|
| 108 |
- if (ctx->enc) { |
| 109 |
+ if (ctx->op == ALG_OP_ENCRYPT) { |
| 110 |
/* |
| 111 |
* Encryption operation - The in-place cipher operation is |
| 112 |
* achieved by the following operation: |
| 113 |
@@ -233,7 +246,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 114 |
if (err) |
| 115 |
goto free; |
| 116 |
af_alg_pull_tsgl(sk, processed, NULL, 0); |
| 117 |
- } else { |
| 118 |
+ } else if (ctx->op == ALG_OP_DECRYPT) { |
| 119 |
/* |
| 120 |
* Decryption operation - To achieve an in-place cipher |
| 121 |
* operation, the following SGL structure is used: |
| 122 |
@@ -298,8 +311,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 123 |
aead_request_set_callback(&areq->cra_u.aead_req, |
| 124 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 125 |
af_alg_async_cb, areq); |
| 126 |
- err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 127 |
- crypto_aead_decrypt(&areq->cra_u.aead_req); |
| 128 |
+ err = aead_cipher_op(ctx, areq); |
| 129 |
|
| 130 |
/* AIO operation in progress */ |
| 131 |
if (err == -EINPROGRESS || err == -EBUSY) |
| 132 |
@@ -311,10 +323,7 @@ static int _aead_recvmsg(struct socket *sock, struct msghdr *msg, |
| 133 |
aead_request_set_callback(&areq->cra_u.aead_req, |
| 134 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 135 |
crypto_req_done, &ctx->wait); |
| 136 |
- err = crypto_wait_req(ctx->enc ? |
| 137 |
- crypto_aead_encrypt(&areq->cra_u.aead_req) : |
| 138 |
- crypto_aead_decrypt(&areq->cra_u.aead_req), |
| 139 |
- &ctx->wait); |
| 140 |
+ err = crypto_wait_req(aead_cipher_op(ctx, areq), &ctx->wait); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
@@ -574,7 +583,7 @@ static int aead_accept_parent_nokey(void *private, struct sock *sk) |
| 145 |
atomic_set(&ctx->rcvused, 0); |
| 146 |
ctx->more = 0; |
| 147 |
ctx->merge = 0; |
| 148 |
- ctx->enc = 0; |
| 149 |
+ ctx->op = 0; |
| 150 |
ctx->aead_assoclen = 0; |
| 151 |
crypto_init_wait(&ctx->wait); |
| 152 |
|
| 153 |
diff --git a/crypto/algif_skcipher.c b/crypto/algif_skcipher.c |
| 154 |
index c5c47b680152..950b33946a27 100644 |
| 155 |
--- a/crypto/algif_skcipher.c |
| 156 |
+++ b/crypto/algif_skcipher.c |
| 157 |
@@ -57,6 +57,19 @@ static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, |
| 158 |
return af_alg_sendmsg(sock, msg, size, ivsize); |
| 159 |
} |
| 160 |
|
| 161 |
+static inline int skcipher_cipher_op(struct af_alg_ctx *ctx, |
| 162 |
+ struct af_alg_async_req *areq) |
| 163 |
+{ |
| 164 |
+ switch (ctx->op) { |
| 165 |
+ case ALG_OP_ENCRYPT: |
| 166 |
+ return crypto_skcipher_encrypt(&areq->cra_u.skcipher_req); |
| 167 |
+ case ALG_OP_DECRYPT: |
| 168 |
+ return crypto_skcipher_decrypt(&areq->cra_u.skcipher_req); |
| 169 |
+ default: |
| 170 |
+ return -EOPNOTSUPP; |
| 171 |
+ } |
| 172 |
+} |
| 173 |
+ |
| 174 |
static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
| 175 |
size_t ignored, int flags) |
| 176 |
{ |
| 177 |
@@ -132,9 +145,7 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
| 178 |
skcipher_request_set_callback(&areq->cra_u.skcipher_req, |
| 179 |
CRYPTO_TFM_REQ_MAY_SLEEP, |
| 180 |
af_alg_async_cb, areq); |
| 181 |
- err = ctx->enc ? |
| 182 |
- crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) : |
| 183 |
- crypto_skcipher_decrypt(&areq->cra_u.skcipher_req); |
| 184 |
+ err = skcipher_cipher_op(ctx, areq); |
| 185 |
|
| 186 |
/* AIO operation in progress */ |
| 187 |
if (err == -EINPROGRESS || err == -EBUSY) |
| 188 |
@@ -147,10 +158,8 @@ static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
| 189 |
CRYPTO_TFM_REQ_MAY_SLEEP | |
| 190 |
CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 191 |
crypto_req_done, &ctx->wait); |
| 192 |
- err = crypto_wait_req(ctx->enc ? |
| 193 |
- crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) : |
| 194 |
- crypto_skcipher_decrypt(&areq->cra_u.skcipher_req), |
| 195 |
- &ctx->wait); |
| 196 |
+ err = crypto_wait_req(skcipher_cipher_op(ctx, areq), |
| 197 |
+ &ctx->wait); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
@@ -393,7 +402,7 @@ static int skcipher_accept_parent_nokey(void *private, struct sock *sk) |
| 202 |
atomic_set(&ctx->rcvused, 0); |
| 203 |
ctx->more = 0; |
| 204 |
ctx->merge = 0; |
| 205 |
- ctx->enc = 0; |
| 206 |
+ ctx->op = 0; |
| 207 |
crypto_init_wait(&ctx->wait); |
| 208 |
|
| 209 |
ask->private = ctx; |
| 210 |
diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h |
| 211 |
index f38227a78eae..712a3d1cd466 100644 |
| 212 |
--- a/include/crypto/if_alg.h |
| 213 |
+++ b/include/crypto/if_alg.h |
| 214 |
@@ -138,7 +138,7 @@ struct af_alg_async_req { |
| 215 |
* @more: More data to be expected from user space? |
| 216 |
* @merge: Shall new data from user space be merged into existing |
| 217 |
* SG? |
| 218 |
- * @enc: Cryptographic operation to be performed when |
| 219 |
+ * @op: Cryptographic operation to be performed when |
| 220 |
* recvmsg is invoked. |
| 221 |
* @len: Length of memory allocated for this data structure. |
| 222 |
*/ |
| 223 |
@@ -155,7 +155,7 @@ struct af_alg_ctx { |
| 224 |
|
| 225 |
bool more; |
| 226 |
bool merge; |
| 227 |
- bool enc; |
| 228 |
+ int op; |
| 229 |
|
| 230 |
unsigned int len; |
| 231 |
}; |
| 232 |
diff --git a/include/uapi/linux/if_alg.h b/include/uapi/linux/if_alg.h |
| 233 |
index bc2bcdec377b..e61e9fc353ec 100644 |
| 234 |
--- a/include/uapi/linux/if_alg.h |
| 235 |
+++ b/include/uapi/linux/if_alg.h |
| 236 |
@@ -39,5 +39,7 @@ struct af_alg_iv { |
| 237 |
/* Operations */ |
| 238 |
#define ALG_OP_DECRYPT 0 |
| 239 |
#define ALG_OP_ENCRYPT 1 |
| 240 |
+#define ALG_OP_SIGN 2 |
| 241 |
+#define ALG_OP_VERIFY 3 |
| 242 |
|
| 243 |
#endif /* _LINUX_IF_ALG_H */ |
| 244 |
-- |
| 245 |
2.14.3 |
| 246 |
|