| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "internal.h" |
| 22 |
#include "kcapi.h" |
| 23 |
|
| 24 |
int kcapi_akcipher_init(struct kcapi_handle **handle, const char *ciphername, |
| 25 |
uint32_t flags) |
| 26 |
{ |
| 27 |
return _kcapi_handle_init(handle, "akcipher", ciphername, flags); |
| 28 |
} |
| 29 |
|
| 30 |
void kcapi_akcipher_destroy(struct kcapi_handle *handle) |
| 31 |
{ |
| 32 |
_kcapi_handle_destroy(handle); |
| 33 |
} |
| 34 |
|
| 35 |
int kcapi_akcipher_setkey(struct kcapi_handle *handle, |
| 36 |
const uint8_t *key, uint32_t keylen) |
| 37 |
{ |
| 38 |
return _kcapi_common_setkey(handle, key, keylen); |
| 39 |
} |
| 40 |
|
| 41 |
int kcapi_akcipher_setpubkey(struct kcapi_handle *handle, |
| 42 |
const uint8_t *key, uint32_t keylen) |
| 43 |
{ |
| 44 |
struct kcapi_handle_tfm *tfm = handle->tfm; |
| 45 |
int ret = 0; |
| 46 |
|
| 47 |
ret = setsockopt(tfm->tfmfd, SOL_ALG, ALG_SET_PUBKEY, key, keylen); |
| 48 |
return (ret >= 0) ? ret : -errno; |
| 49 |
} |
| 50 |
|
| 51 |
IMPL_SYMVER(akcipher_encrypt, "1.3.1") |
| 52 |
ssize_t impl_akcipher_encrypt(struct kcapi_handle *handle, |
| 53 |
const uint8_t *in, size_t inlen, |
| 54 |
uint8_t *out, size_t outlen, int access) |
| 55 |
{ |
| 56 |
return _kcapi_cipher_crypt(handle, in, inlen, out, outlen, access, |
| 57 |
ALG_OP_ENCRYPT); |
| 58 |
} |
| 59 |
|
| 60 |
ORIG_SYMVER(akcipher_encrypt, "0.12.0") |
| 61 |
int32_t orig_akcipher_encrypt(struct kcapi_handle *handle, |
| 62 |
const uint8_t *in, uint32_t inlen, |
| 63 |
uint8_t *out, uint32_t outlen, int access) |
| 64 |
{ |
| 65 |
return (int32_t)_kcapi_cipher_crypt(handle, in, inlen, out, outlen, |
| 66 |
access, ALG_OP_ENCRYPT); |
| 67 |
} |
| 68 |
|
| 69 |
IMPL_SYMVER(akcipher_decrypt, "1.3.1") |
| 70 |
ssize_t impl_akcipher_decrypt(struct kcapi_handle *handle, |
| 71 |
const uint8_t *in, size_t inlen, |
| 72 |
uint8_t *out, size_t outlen, int access) |
| 73 |
{ |
| 74 |
return _kcapi_cipher_crypt(handle, in, inlen, out, outlen, access, |
| 75 |
ALG_OP_DECRYPT); |
| 76 |
} |
| 77 |
|
| 78 |
ORIG_SYMVER(akcipher_decrypt, "0.12.0") |
| 79 |
int32_t orig_akcipher_decrypt(struct kcapi_handle *handle, |
| 80 |
const uint8_t *in, uint32_t inlen, |
| 81 |
uint8_t *out, uint32_t outlen, int access) |
| 82 |
{ |
| 83 |
return (int32_t)_kcapi_cipher_crypt(handle, in, inlen, out, outlen, |
| 84 |
access, ALG_OP_DECRYPT); |
| 85 |
} |
| 86 |
|
| 87 |
IMPL_SYMVER(akcipher_sign, "1.3.1") |
| 88 |
ssize_t impl_akcipher_sign(struct kcapi_handle *handle, |
| 89 |
const uint8_t *in, size_t inlen, |
| 90 |
uint8_t *out, size_t outlen, int access) |
| 91 |
{ |
| 92 |
return _kcapi_cipher_crypt(handle, in, inlen, out, outlen, access, |
| 93 |
ALG_OP_SIGN); |
| 94 |
} |
| 95 |
|
| 96 |
ORIG_SYMVER(akcipher_sign, "0.12.0") |
| 97 |
int32_t orig_akcipher_sign(struct kcapi_handle *handle, |
| 98 |
const uint8_t *in, uint32_t inlen, |
| 99 |
uint8_t *out, uint32_t outlen, int access) |
| 100 |
{ |
| 101 |
return (int32_t)_kcapi_cipher_crypt(handle, in, inlen, out, outlen, |
| 102 |
access, ALG_OP_SIGN); |
| 103 |
} |
| 104 |
|
| 105 |
IMPL_SYMVER(akcipher_verify, "1.3.1") |
| 106 |
ssize_t impl_akcipher_verify(struct kcapi_handle *handle, |
| 107 |
const uint8_t *in, size_t inlen, |
| 108 |
uint8_t *out, size_t outlen, int access) |
| 109 |
{ |
| 110 |
return _kcapi_cipher_crypt(handle, in, inlen, out, outlen, access, |
| 111 |
ALG_OP_VERIFY); |
| 112 |
} |
| 113 |
|
| 114 |
ORIG_SYMVER(akcipher_verify, "0.12.0") |
| 115 |
int32_t orig_akcipher_verify(struct kcapi_handle *handle, |
| 116 |
const uint8_t *in, uint32_t inlen, |
| 117 |
uint8_t *out, uint32_t outlen, int access) |
| 118 |
{ |
| 119 |
return (int32_t)_kcapi_cipher_crypt(handle, in, inlen, out, outlen, |
| 120 |
access, ALG_OP_VERIFY); |
| 121 |
} |
| 122 |
|
| 123 |
IMPL_SYMVER(akcipher_stream_init_enc, "1.3.1") |
| 124 |
ssize_t impl_akcipher_stream_init_enc(struct kcapi_handle *handle, |
| 125 |
struct iovec *iov, size_t iovlen) |
| 126 |
{ |
| 127 |
return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_ENCRYPT, |
| 128 |
MSG_MORE); |
| 129 |
} |
| 130 |
|
| 131 |
ORIG_SYMVER(akcipher_stream_init_enc, "0.12.0") |
| 132 |
int32_t orig_akcipher_stream_init_enc(struct kcapi_handle *handle, |
| 133 |
struct iovec *iov, uint32_t iovlen) |
| 134 |
{ |
| 135 |
return (int32_t)_kcapi_common_send_meta(handle, iov, iovlen, |
| 136 |
ALG_OP_ENCRYPT, MSG_MORE); |
| 137 |
} |
| 138 |
|
| 139 |
IMPL_SYMVER(akcipher_stream_init_dec, "1.3.1") |
| 140 |
ssize_t impl_akcipher_stream_init_dec(struct kcapi_handle *handle, |
| 141 |
struct iovec *iov, size_t iovlen) |
| 142 |
{ |
| 143 |
return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_DECRYPT, |
| 144 |
MSG_MORE); |
| 145 |
} |
| 146 |
|
| 147 |
ORIG_SYMVER(akcipher_stream_init_dec, "0.12.0") |
| 148 |
int32_t orig_akcipher_stream_init_dec(struct kcapi_handle *handle, |
| 149 |
struct iovec *iov, uint32_t iovlen) |
| 150 |
{ |
| 151 |
return (int32_t)_kcapi_common_send_meta(handle, iov, iovlen, |
| 152 |
ALG_OP_DECRYPT, MSG_MORE); |
| 153 |
} |
| 154 |
|
| 155 |
IMPL_SYMVER(akcipher_stream_init_sgn, "1.3.1") |
| 156 |
ssize_t impl_akcipher_stream_init_sgn(struct kcapi_handle *handle, |
| 157 |
struct iovec *iov, size_t iovlen) |
| 158 |
{ |
| 159 |
return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_SIGN, |
| 160 |
MSG_MORE); |
| 161 |
} |
| 162 |
|
| 163 |
ORIG_SYMVER(akcipher_stream_init_sgn, "0.12.0") |
| 164 |
int32_t orig_akcipher_stream_init_sgn(struct kcapi_handle *handle, |
| 165 |
struct iovec *iov, uint32_t iovlen) |
| 166 |
{ |
| 167 |
return (int32_t)_kcapi_common_send_meta(handle, iov, iovlen, |
| 168 |
ALG_OP_SIGN, MSG_MORE); |
| 169 |
} |
| 170 |
|
| 171 |
IMPL_SYMVER(akcipher_stream_init_vfy, "1.3.1") |
| 172 |
ssize_t impl_akcipher_stream_init_vfy(struct kcapi_handle *handle, |
| 173 |
struct iovec *iov, size_t iovlen) |
| 174 |
{ |
| 175 |
return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_VERIFY, |
| 176 |
MSG_MORE); |
| 177 |
} |
| 178 |
|
| 179 |
ORIG_SYMVER(akcipher_stream_init_vfy, "0.12.0") |
| 180 |
int32_t orig_akcipher_stream_init_vfy(struct kcapi_handle *handle, |
| 181 |
struct iovec *iov, uint32_t iovlen) |
| 182 |
{ |
| 183 |
return (int32_t)_kcapi_common_send_meta(handle, iov, iovlen, |
| 184 |
ALG_OP_VERIFY, MSG_MORE); |
| 185 |
} |
| 186 |
|
| 187 |
IMPL_SYMVER(akcipher_stream_update, "1.3.1") |
| 188 |
ssize_t impl_akcipher_stream_update(struct kcapi_handle *handle, |
| 189 |
struct iovec *iov, size_t iovlen) |
| 190 |
{ |
| 191 |
if (handle->processed_sg < handle->flags.alg_max_pages) |
| 192 |
return _kcapi_common_vmsplice_iov(handle, iov, iovlen, |
| 193 |
SPLICE_F_MORE); |
| 194 |
else |
| 195 |
return _kcapi_common_send_data(handle, iov, iovlen, MSG_MORE); |
| 196 |
} |
| 197 |
|
| 198 |
ORIG_SYMVER(akcipher_stream_update, "0.12.0") |
| 199 |
int32_t orig_akcipher_stream_update(struct kcapi_handle *handle, |
| 200 |
struct iovec *iov, uint32_t iovlen) |
| 201 |
{ |
| 202 |
return (int32_t)impl_akcipher_stream_update(handle, iov, iovlen); |
| 203 |
} |
| 204 |
|
| 205 |
IMPL_SYMVER(akcipher_stream_update_last, "1.3.1") |
| 206 |
ssize_t impl_akcipher_stream_update_last(struct kcapi_handle *handle, |
| 207 |
struct iovec *iov, size_t iovlen) |
| 208 |
{ |
| 209 |
if (handle->processed_sg < handle->flags.alg_max_pages) |
| 210 |
return _kcapi_common_vmsplice_iov(handle, iov, iovlen, 0); |
| 211 |
else |
| 212 |
return _kcapi_common_send_data(handle, iov, iovlen, 0); |
| 213 |
} |
| 214 |
|
| 215 |
ORIG_SYMVER(akcipher_stream_update_last, "0.12.0") |
| 216 |
int32_t orig_akcipher_stream_update_last(struct kcapi_handle *handle, |
| 217 |
struct iovec *iov, uint32_t iovlen) |
| 218 |
{ |
| 219 |
return (int32_t)impl_akcipher_stream_update_last(handle, iov, iovlen); |
| 220 |
} |
| 221 |
|
| 222 |
IMPL_SYMVER(akcipher_stream_op, "1.3.1") |
| 223 |
ssize_t impl_akcipher_stream_op(struct kcapi_handle *handle, |
| 224 |
struct iovec *iov, size_t iovlen) |
| 225 |
{ |
| 226 |
if (!iov || !iovlen) { |
| 227 |
kcapi_dolog(KCAPI_LOG_ERR, |
| 228 |
"Asymmetric operation: No buffer for output data provided"); |
| 229 |
return -EINVAL; |
| 230 |
} |
| 231 |
return _kcapi_common_recv_data(handle, iov, iovlen); |
| 232 |
} |
| 233 |
|
| 234 |
ORIG_SYMVER(akcipher_stream_op, "0.12.0") |
| 235 |
int32_t orig_akcipher_stream_op(struct kcapi_handle *handle, |
| 236 |
struct iovec *iov, uint32_t iovlen) |
| 237 |
{ |
| 238 |
return (int32_t)impl_akcipher_stream_op(handle, iov, iovlen); |
| 239 |
} |
| 240 |
|
| 241 |
static ssize_t |
| 242 |
_kcapi_akcipher_crypt_aio(struct kcapi_handle *handle, struct iovec *iniov, |
| 243 |
struct iovec *outiov, size_t iovlen, int access, |
| 244 |
unsigned int enc) |
| 245 |
{ |
| 246 |
ssize_t processed = 0, ret; |
| 247 |
|
| 248 |
|
| 249 |
while (iovlen) { |
| 250 |
ret = _kcapi_cipher_crypt_aio(handle, iniov, outiov, 1, |
| 251 |
access, enc); |
| 252 |
if (ret < 0) |
| 253 |
return ret; |
| 254 |
|
| 255 |
processed += ret; |
| 256 |
|
| 257 |
iniov++; |
| 258 |
outiov++; |
| 259 |
iovlen--; |
| 260 |
} |
| 261 |
|
| 262 |
return processed; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static ssize_t |
| 269 |
_kcapi_akcipher_encrypt_aio_fallback(struct kcapi_handle *handle, |
| 270 |
struct iovec *iniov, struct iovec *outiov, |
| 271 |
size_t iovlen) |
| 272 |
{ |
| 273 |
size_t i; |
| 274 |
ssize_t ret = kcapi_akcipher_stream_init_enc(handle, NULL, 0); |
| 275 |
|
| 276 |
if (ret < 0) |
| 277 |
return ret; |
| 278 |
|
| 279 |
for (i = 0; i < iovlen; i++) { |
| 280 |
ssize_t rc = kcapi_akcipher_stream_update_last(handle, iniov, |
| 281 |
1); |
| 282 |
if (rc < 0) |
| 283 |
return rc; |
| 284 |
|
| 285 |
rc = kcapi_akcipher_stream_op(handle, outiov, 1); |
| 286 |
if (rc < 0) |
| 287 |
return rc; |
| 288 |
|
| 289 |
ret += rc; |
| 290 |
|
| 291 |
iniov++; |
| 292 |
outiov++; |
| 293 |
} |
| 294 |
|
| 295 |
return ret; |
| 296 |
} |
| 297 |
|
| 298 |
IMPL_SYMVER(akcipher_encrypt_aio, "1.3.1") |
| 299 |
ssize_t impl_akcipher_encrypt_aio(struct kcapi_handle *handle, |
| 300 |
struct iovec *iniov, struct iovec *outiov, |
| 301 |
size_t iovlen, int access) |
| 302 |
{ |
| 303 |
ssize_t ret; |
| 304 |
|
| 305 |
ret = _kcapi_akcipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 306 |
access, ALG_OP_ENCRYPT); |
| 307 |
if (ret != -EOPNOTSUPP) |
| 308 |
return ret; |
| 309 |
|
| 310 |
return _kcapi_akcipher_encrypt_aio_fallback(handle, iniov, outiov, |
| 311 |
iovlen); |
| 312 |
} |
| 313 |
|
| 314 |
ORIG_SYMVER(akcipher_encrypt_aio, "0.14.0") |
| 315 |
int32_t orig_akcipher_encrypt_aio(struct kcapi_handle *handle, |
| 316 |
struct iovec *iniov, struct iovec *outiov, |
| 317 |
uint32_t iovlen, int access) |
| 318 |
{ |
| 319 |
return (int32_t)impl_akcipher_encrypt_aio(handle, iniov, outiov, |
| 320 |
iovlen, access); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
static ssize_t |
| 327 |
_kcapi_akcipher_decrypt_aio_fallback(struct kcapi_handle *handle, |
| 328 |
struct iovec *iniov, struct iovec *outiov, |
| 329 |
size_t iovlen) |
| 330 |
{ |
| 331 |
uint32_t i; |
| 332 |
ssize_t ret = kcapi_akcipher_stream_init_dec(handle, NULL, 0); |
| 333 |
|
| 334 |
if (ret < 0) |
| 335 |
return ret; |
| 336 |
|
| 337 |
for (i = 0; i < iovlen; i++) { |
| 338 |
ssize_t rc = kcapi_akcipher_stream_update_last(handle, iniov, |
| 339 |
1); |
| 340 |
if (rc < 0) |
| 341 |
return rc; |
| 342 |
|
| 343 |
rc = kcapi_akcipher_stream_op(handle, outiov, 1); |
| 344 |
if (rc < 0) |
| 345 |
return rc; |
| 346 |
|
| 347 |
ret += rc; |
| 348 |
|
| 349 |
iniov++; |
| 350 |
outiov++; |
| 351 |
} |
| 352 |
|
| 353 |
return ret; |
| 354 |
} |
| 355 |
|
| 356 |
IMPL_SYMVER(akcipher_decrypt_aio, "1.3.1") |
| 357 |
ssize_t impl_akcipher_decrypt_aio(struct kcapi_handle *handle, |
| 358 |
struct iovec *iniov, struct iovec *outiov, |
| 359 |
size_t iovlen, int access) |
| 360 |
{ |
| 361 |
ssize_t ret; |
| 362 |
|
| 363 |
ret = _kcapi_akcipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 364 |
access, ALG_OP_DECRYPT); |
| 365 |
if (ret != -EOPNOTSUPP) |
| 366 |
return ret; |
| 367 |
|
| 368 |
return _kcapi_akcipher_decrypt_aio_fallback(handle, iniov, outiov, |
| 369 |
iovlen); |
| 370 |
} |
| 371 |
|
| 372 |
ORIG_SYMVER(akcipher_decrypt_aio, "0.14.0") |
| 373 |
int32_t orig_akcipher_decrypt_aio(struct kcapi_handle *handle, |
| 374 |
struct iovec *iniov, struct iovec *outiov, |
| 375 |
uint32_t iovlen, int access) |
| 376 |
{ |
| 377 |
return (int32_t)impl_akcipher_decrypt_aio(handle, iniov, outiov, |
| 378 |
iovlen, access); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static ssize_t |
| 385 |
_kcapi_akcipher_sign_aio_fallback(struct kcapi_handle *handle, |
| 386 |
struct iovec *iniov, struct iovec *outiov, |
| 387 |
size_t iovlen) |
| 388 |
{ |
| 389 |
size_t i; |
| 390 |
ssize_t ret = kcapi_akcipher_stream_init_sgn(handle, NULL, 0); |
| 391 |
|
| 392 |
if (ret < 0) |
| 393 |
return ret; |
| 394 |
|
| 395 |
for (i = 0; i < iovlen; i++) { |
| 396 |
ssize_t rc = kcapi_akcipher_stream_update_last(handle, iniov, |
| 397 |
1); |
| 398 |
if (rc < 0) |
| 399 |
return rc; |
| 400 |
|
| 401 |
rc = kcapi_akcipher_stream_op(handle, outiov, 1); |
| 402 |
if (rc < 0) |
| 403 |
return rc; |
| 404 |
|
| 405 |
ret += rc; |
| 406 |
|
| 407 |
iniov++; |
| 408 |
outiov++; |
| 409 |
} |
| 410 |
|
| 411 |
return ret; |
| 412 |
} |
| 413 |
|
| 414 |
IMPL_SYMVER(akcipher_sign_aio, "1.3.1") |
| 415 |
ssize_t impl_akcipher_sign_aio(struct kcapi_handle *handle, |
| 416 |
struct iovec *iniov, struct iovec *outiov, |
| 417 |
size_t iovlen, int access) |
| 418 |
{ |
| 419 |
ssize_t ret; |
| 420 |
|
| 421 |
ret = _kcapi_akcipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 422 |
access, ALG_OP_SIGN); |
| 423 |
if (ret != -EOPNOTSUPP) |
| 424 |
return ret; |
| 425 |
|
| 426 |
return _kcapi_akcipher_sign_aio_fallback(handle, iniov, outiov, iovlen); |
| 427 |
} |
| 428 |
|
| 429 |
ORIG_SYMVER(akcipher_sign_aio, "0.14.0") |
| 430 |
int32_t orig_akcipher_sign_aio(struct kcapi_handle *handle, |
| 431 |
struct iovec *iniov, struct iovec *outiov, |
| 432 |
uint32_t iovlen, int access) |
| 433 |
{ |
| 434 |
return (int32_t)impl_akcipher_sign_aio(handle, iniov, outiov, iovlen, |
| 435 |
access); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
static ssize_t |
| 442 |
_kcapi_akcipher_verify_aio_fallback(struct kcapi_handle *handle, |
| 443 |
struct iovec *iniov, struct iovec *outiov, |
| 444 |
size_t iovlen) |
| 445 |
{ |
| 446 |
size_t i; |
| 447 |
ssize_t ret = kcapi_akcipher_stream_init_vfy(handle, NULL, 0); |
| 448 |
|
| 449 |
if (ret < 0) |
| 450 |
return ret; |
| 451 |
|
| 452 |
for (i = 0; i < iovlen; i++) { |
| 453 |
ssize_t rc = kcapi_akcipher_stream_update_last(handle, iniov, |
| 454 |
1); |
| 455 |
if (rc < 0) |
| 456 |
return rc; |
| 457 |
|
| 458 |
rc = kcapi_akcipher_stream_op(handle, outiov, 1); |
| 459 |
if (rc < 0) |
| 460 |
return rc; |
| 461 |
|
| 462 |
ret += rc; |
| 463 |
|
| 464 |
iniov++; |
| 465 |
outiov++; |
| 466 |
} |
| 467 |
|
| 468 |
return ret; |
| 469 |
} |
| 470 |
|
| 471 |
IMPL_SYMVER(akcipher_verify_aio, "1.3.1") |
| 472 |
ssize_t impl_akcipher_verify_aio(struct kcapi_handle *handle, |
| 473 |
struct iovec *iniov, struct iovec *outiov, |
| 474 |
size_t iovlen, int access) |
| 475 |
{ |
| 476 |
ssize_t ret; |
| 477 |
|
| 478 |
ret = _kcapi_akcipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 479 |
access, ALG_OP_VERIFY); |
| 480 |
if (ret != -EOPNOTSUPP) |
| 481 |
return ret; |
| 482 |
|
| 483 |
return _kcapi_akcipher_verify_aio_fallback(handle, iniov, outiov, |
| 484 |
iovlen); |
| 485 |
} |
| 486 |
|
| 487 |
ORIG_SYMVER(akcipher_verify_aio, "0.14.0") |
| 488 |
int32_t orig_akcipher_verify_aio(struct kcapi_handle *handle, |
| 489 |
struct iovec *iniov, struct iovec *outiov, |
| 490 |
uint32_t iovlen, int access) |
| 491 |
{ |
| 492 |
return (int32_t)impl_akcipher_verify_aio(handle, iniov, outiov, |
| 493 |
iovlen, access); |
| 494 |
} |