| 1 | /* Kernel crypto API AF_ALG Symmetric Cipher API |
| 2 | * |
| 3 | * Copyright (C) 2016 - 2022, Stephan Mueller <[email protected]> |
| 4 | * |
| 5 | * License: see COPYING file in root directory |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 8 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 9 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 10 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 13 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 14 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 15 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 16 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 17 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 18 | * DAMAGE. |
| 19 | */ |
| 20 | |
| 21 | #include "internal.h" |
| 22 | #include "kcapi.h" |
| 23 | |
| 24 | int kcapi_cipher_init(struct kcapi_handle **handle, const char *ciphername, |
| 25 | uint32_t flags) |
| 26 | { |
| 27 | return _kcapi_handle_init(handle, "skcipher", ciphername, flags); |
| 28 | } |
| 29 | |
| 30 | void kcapi_cipher_destroy(struct kcapi_handle *handle) |
| 31 | { |
| 32 | _kcapi_handle_destroy(handle); |
| 33 | } |
| 34 | |
| 35 | int kcapi_cipher_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 | IMPL_SYMVER(cipher_encrypt, "1.3.1") |
| 42 | ssize_t impl_cipher_encrypt(struct kcapi_handle *handle, |
| 43 | const uint8_t *in, size_t inlen, |
| 44 | const uint8_t *iv, |
| 45 | uint8_t *out, size_t outlen, int access) |
| 46 | { |
| 47 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 48 | uint32_t bs = tfm->info.blocksize; |
| 49 | |
| 50 | /* require properly sized output data size */ |
| 51 | if (outlen < ((inlen + bs - 1) / bs * bs)) |
| 52 | kcapi_dolog(KCAPI_LOG_WARN, |
| 53 | "Symmetric Encryption: Ciphertext buffer (%lu) is not plaintext buffer (%lu) rounded up to multiple of block size %u", |
| 54 | (unsigned long) outlen, (unsigned long)inlen, bs); |
| 55 | |
| 56 | handle->cipher.iv = iv; |
| 57 | return _kcapi_cipher_crypt_chunk(handle, in, inlen, out, outlen, access, |
| 58 | ALG_OP_ENCRYPT); |
| 59 | } |
| 60 | |
| 61 | ORIG_SYMVER(cipher_encrypt, "0.12.0") |
| 62 | int32_t orig_cipher_encrypt(struct kcapi_handle *handle, |
| 63 | const uint8_t *in, uint32_t inlen, |
| 64 | const uint8_t *iv, |
| 65 | uint8_t *out, uint32_t outlen, int access) |
| 66 | { |
| 67 | return (int32_t)impl_cipher_encrypt(handle, in, inlen, iv, out, outlen, |
| 68 | access); |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Fallback function if AIO is not present, but caller requested AIO operation. |
| 73 | */ |
| 74 | static ssize_t |
| 75 | _kcapi_cipher_encrypt_aio_fallback(struct kcapi_handle *handle, |
| 76 | struct iovec *iniov, struct iovec *outiov, |
| 77 | size_t iovlen, const uint8_t *iv) |
| 78 | { |
| 79 | ssize_t rc = kcapi_cipher_stream_init_enc(handle, iv, iniov, iovlen); |
| 80 | |
| 81 | if (rc < 0) |
| 82 | return rc; |
| 83 | |
| 84 | return kcapi_cipher_stream_op(handle, outiov, iovlen); |
| 85 | } |
| 86 | |
| 87 | IMPL_SYMVER(cipher_encrypt_aio, "1.3.1") |
| 88 | ssize_t impl_cipher_encrypt_aio(struct kcapi_handle *handle, |
| 89 | struct iovec *iniov, struct iovec *outiov, |
| 90 | size_t iovlen, const uint8_t *iv, int access) |
| 91 | { |
| 92 | ssize_t ret; |
| 93 | |
| 94 | handle->cipher.iv = iv; |
| 95 | |
| 96 | ret = _kcapi_cipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 97 | access, ALG_OP_ENCRYPT); |
| 98 | if (ret != -EOPNOTSUPP) |
| 99 | return ret; |
| 100 | |
| 101 | return _kcapi_cipher_encrypt_aio_fallback(handle, iniov, outiov, |
| 102 | iovlen, iv); |
| 103 | } |
| 104 | |
| 105 | ORIG_SYMVER(cipher_encrypt_aio, "0.12.0") |
| 106 | int32_t orig_cipher_encrypt_aio(struct kcapi_handle *handle, |
| 107 | struct iovec *iniov, struct iovec *outiov, |
| 108 | uint32_t iovlen, const uint8_t *iv, |
| 109 | int access) |
| 110 | { |
| 111 | return (int32_t)impl_cipher_encrypt_aio(handle, iniov, outiov, iovlen, |
| 112 | iv, access); |
| 113 | } |
| 114 | |
| 115 | IMPL_SYMVER(cipher_decrypt, "1.3.1") |
| 116 | ssize_t impl_cipher_decrypt(struct kcapi_handle *handle, |
| 117 | const uint8_t *in, size_t inlen, |
| 118 | const uint8_t *iv, |
| 119 | uint8_t *out, size_t outlen, int access) |
| 120 | { |
| 121 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 122 | |
| 123 | /* require properly sized output data size */ |
| 124 | if (inlen % tfm->info.blocksize) |
| 125 | kcapi_dolog(KCAPI_LOG_WARN, |
| 126 | "Symmetric Decryption: Ciphertext buffer is not multiple of block size %u", |
| 127 | tfm->info.blocksize); |
| 128 | |
| 129 | if (outlen < inlen) |
| 130 | kcapi_dolog(KCAPI_LOG_WARN, |
| 131 | "Symmetric Decryption: Plaintext buffer (%lu) is smaller as ciphertext buffer (%lu)", |
| 132 | (unsigned long)outlen, (unsigned long)inlen); |
| 133 | |
| 134 | handle->cipher.iv = iv; |
| 135 | return _kcapi_cipher_crypt_chunk(handle, in, inlen, out, outlen, access, |
| 136 | ALG_OP_DECRYPT); |
| 137 | } |
| 138 | |
| 139 | ORIG_SYMVER(cipher_decrypt, "0.12.0") |
| 140 | int32_t orig_cipher_decrypt(struct kcapi_handle *handle, |
| 141 | const uint8_t *in, uint32_t inlen, |
| 142 | const uint8_t *iv, |
| 143 | uint8_t *out, uint32_t outlen, int access) |
| 144 | { |
| 145 | return (int32_t)impl_cipher_decrypt(handle, in, inlen, iv, out, outlen, |
| 146 | access); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Fallback function if AIO is not present, but caller requested AIO operation. |
| 151 | */ |
| 152 | static ssize_t |
| 153 | _kcapi_cipher_decrypt_aio_fallback(struct kcapi_handle *handle, |
| 154 | struct iovec *iniov, struct iovec *outiov, |
| 155 | size_t iovlen, const uint8_t *iv) |
| 156 | { |
| 157 | ssize_t rc = kcapi_cipher_stream_init_dec(handle, iv, iniov, iovlen); |
| 158 | |
| 159 | if (rc < 0) |
| 160 | return rc; |
| 161 | |
| 162 | return kcapi_cipher_stream_op(handle, outiov, iovlen); |
| 163 | } |
| 164 | |
| 165 | IMPL_SYMVER(cipher_decrypt_aio, "1.3.1") |
| 166 | ssize_t impl_cipher_decrypt_aio(struct kcapi_handle *handle, |
| 167 | struct iovec *iniov, struct iovec *outiov, |
| 168 | size_t iovlen, const uint8_t *iv, int access) |
| 169 | { |
| 170 | ssize_t ret; |
| 171 | |
| 172 | handle->cipher.iv = iv; |
| 173 | |
| 174 | ret = _kcapi_cipher_crypt_aio(handle, iniov, outiov, iovlen, |
| 175 | access, ALG_OP_DECRYPT); |
| 176 | if (ret != -EOPNOTSUPP) |
| 177 | return ret; |
| 178 | |
| 179 | return _kcapi_cipher_decrypt_aio_fallback(handle, iniov, outiov, |
| 180 | iovlen, iv); |
| 181 | } |
| 182 | |
| 183 | ORIG_SYMVER(cipher_decrypt_aio, "0.12.0") |
| 184 | int32_t orig_cipher_decrypt_aio(struct kcapi_handle *handle, |
| 185 | struct iovec *iniov, struct iovec *outiov, |
| 186 | uint32_t iovlen, const uint8_t *iv, int access) |
| 187 | { |
| 188 | return (int32_t)impl_cipher_decrypt_aio(handle, iniov, outiov, iovlen, |
| 189 | iv, access); |
| 190 | } |
| 191 | |
| 192 | IMPL_SYMVER(cipher_stream_init_enc, "1.3.1") |
| 193 | ssize_t impl_cipher_stream_init_enc(struct kcapi_handle *handle, |
| 194 | const uint8_t *iv, |
| 195 | struct iovec *iov, size_t iovlen) |
| 196 | { |
| 197 | handle->cipher.iv = iv; |
| 198 | return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_ENCRYPT, |
| 199 | MSG_MORE); |
| 200 | } |
| 201 | |
| 202 | ORIG_SYMVER(cipher_stream_init_enc, "0.12.0") |
| 203 | int32_t orig_cipher_stream_init_enc(struct kcapi_handle *handle, |
| 204 | const uint8_t *iv, |
| 205 | struct iovec *iov, uint32_t iovlen) |
| 206 | { |
| 207 | return (int32_t)impl_cipher_stream_init_enc(handle, iv, iov, iovlen); |
| 208 | } |
| 209 | |
| 210 | IMPL_SYMVER(cipher_stream_init_dec, "1.3.1") |
| 211 | ssize_t impl_cipher_stream_init_dec(struct kcapi_handle *handle, |
| 212 | const uint8_t *iv, |
| 213 | struct iovec *iov, size_t iovlen) |
| 214 | { |
| 215 | handle->cipher.iv = iv; |
| 216 | return _kcapi_common_send_meta(handle, iov, iovlen, ALG_OP_DECRYPT, |
| 217 | MSG_MORE); |
| 218 | } |
| 219 | |
| 220 | ORIG_SYMVER(cipher_stream_init_dec, "0.12.0") |
| 221 | int32_t orig_cipher_stream_init_dec(struct kcapi_handle *handle, |
| 222 | const uint8_t *iv, |
| 223 | struct iovec *iov, uint32_t iovlen) |
| 224 | { |
| 225 | return (int32_t)impl_cipher_stream_init_dec(handle, iv, iov, iovlen); |
| 226 | } |
| 227 | |
| 228 | IMPL_SYMVER(cipher_stream_update, "1.3.1") |
| 229 | ssize_t impl_cipher_stream_update(struct kcapi_handle *handle, |
| 230 | struct iovec *iov, size_t iovlen) |
| 231 | { |
| 232 | if (handle->processed_sg <= handle->flags.alg_max_pages) |
| 233 | return _kcapi_common_vmsplice_iov(handle, iov, iovlen, |
| 234 | SPLICE_F_MORE); |
| 235 | else |
| 236 | return _kcapi_common_send_data(handle, iov, iovlen, MSG_MORE); |
| 237 | } |
| 238 | |
| 239 | ORIG_SYMVER(cipher_stream_update, "0.12.0") |
| 240 | int32_t orig_cipher_stream_update(struct kcapi_handle *handle, |
| 241 | struct iovec *iov, uint32_t iovlen) |
| 242 | { |
| 243 | return (int32_t)impl_cipher_stream_update(handle, iov, iovlen); |
| 244 | } |
| 245 | |
| 246 | IMPL_SYMVER(cipher_stream_update_last, "1.3.1") |
| 247 | ssize_t impl_cipher_stream_update_last(struct kcapi_handle *handle, |
| 248 | struct iovec *iov, size_t iovlen) |
| 249 | { |
| 250 | if (handle->processed_sg <= handle->flags.alg_max_pages) |
| 251 | return _kcapi_common_vmsplice_iov(handle, iov, iovlen, 0); |
| 252 | else |
| 253 | return _kcapi_common_send_data(handle, iov, iovlen, 0); |
| 254 | } |
| 255 | |
| 256 | ORIG_SYMVER(cipher_stream_update_last, "1.2.0") |
| 257 | int32_t orig_cipher_stream_update_last(struct kcapi_handle *handle, |
| 258 | struct iovec *iov, uint32_t iovlen) |
| 259 | { |
| 260 | return (int32_t)impl_cipher_stream_update_last(handle, iov, iovlen); |
| 261 | } |
| 262 | |
| 263 | IMPL_SYMVER(cipher_stream_op, "1.3.1") |
| 264 | ssize_t impl_cipher_stream_op(struct kcapi_handle *handle, |
| 265 | struct iovec *iov, size_t iovlen) |
| 266 | { |
| 267 | if (!iov || !iovlen) { |
| 268 | kcapi_dolog(KCAPI_LOG_ERR, |
| 269 | "Symmetric operation: No buffer for output data provided"); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | return _kcapi_common_recv_data(handle, iov, iovlen); |
| 273 | } |
| 274 | |
| 275 | ORIG_SYMVER(cipher_stream_op, "0.12.0") |
| 276 | int32_t orig_cipher_stream_op(struct kcapi_handle *handle, |
| 277 | struct iovec *iov, uint32_t iovlen) |
| 278 | { |
| 279 | return (int32_t)impl_cipher_stream_op(handle, iov, iovlen); |
| 280 | } |
| 281 | |
| 282 | uint32_t kcapi_cipher_ivsize(struct kcapi_handle *handle) |
| 283 | { |
| 284 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 285 | |
| 286 | return tfm->info.ivsize; |
| 287 | } |
| 288 | |
| 289 | uint32_t kcapi_cipher_blocksize(struct kcapi_handle *handle) |
| 290 | { |
| 291 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 292 | |
| 293 | return tfm->info.blocksize; |
| 294 | } |
| 295 | |
| 296 | static inline ssize_t kcapi_cipher_conv_enc_common(const char *name, |
| 297 | const uint8_t *key, uint32_t keylen, |
| 298 | const uint8_t *in, size_t inlen, |
| 299 | const uint8_t *iv, |
| 300 | uint8_t *out, size_t outlen) |
| 301 | { |
| 302 | struct kcapi_handle *handle; |
| 303 | ssize_t ret = _kcapi_handle_init(&handle, "skcipher", name, 0); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | |
| 307 | ret = kcapi_cipher_setkey(handle, key, keylen); |
| 308 | if (ret) |
| 309 | goto out; |
| 310 | |
| 311 | ret = kcapi_cipher_encrypt(handle, in, inlen, iv, out, outlen, 0); |
| 312 | |
| 313 | out: |
| 314 | _kcapi_handle_destroy(handle); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static inline ssize_t kcapi_cipher_conv_dec_common(const char *name, |
| 319 | const uint8_t *key, uint32_t keylen, |
| 320 | const uint8_t *in, size_t inlen, |
| 321 | const uint8_t *iv, |
| 322 | uint8_t *out, size_t outlen) |
| 323 | { |
| 324 | struct kcapi_handle *handle; |
| 325 | ssize_t ret = _kcapi_handle_init(&handle, "skcipher", name, 0); |
| 326 | |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | |
| 330 | ret = kcapi_cipher_setkey(handle, key, keylen); |
| 331 | if (ret) |
| 332 | goto out; |
| 333 | |
| 334 | ret = kcapi_cipher_decrypt(handle, in, inlen, iv, out, outlen, 0); |
| 335 | |
| 336 | out: |
| 337 | _kcapi_handle_destroy(handle); |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | IMPL_SYMVER(cipher_enc_aes_cbc, "1.3.1") |
| 342 | ssize_t impl_cipher_enc_aes_cbc(const uint8_t *key, uint32_t keylen, |
| 343 | const uint8_t *in, size_t inlen, |
| 344 | const uint8_t *iv, |
| 345 | uint8_t *out, size_t outlen) |
| 346 | { |
| 347 | return kcapi_cipher_conv_enc_common("cbc(aes)", key, keylen, in, inlen, |
| 348 | iv, out, outlen); |
| 349 | } |
| 350 | |
| 351 | ORIG_SYMVER(cipher_enc_aes_cbc, "1.0.0") |
| 352 | int32_t orig_cipher_enc_aes_cbc(const uint8_t *key, uint32_t keylen, |
| 353 | const uint8_t *in, uint32_t inlen, |
| 354 | const uint8_t *iv, |
| 355 | uint8_t *out, uint32_t outlen) |
| 356 | { |
| 357 | return (int32_t)kcapi_cipher_conv_enc_common("cbc(aes)", key, keylen, |
| 358 | in, inlen, iv, |
| 359 | out, outlen); |
| 360 | } |
| 361 | |
| 362 | IMPL_SYMVER(cipher_enc_aes_ctr, "1.3.1") |
| 363 | ssize_t impl_cipher_enc_aes_ctr(const uint8_t *key, uint32_t keylen, |
| 364 | const uint8_t *in, size_t inlen, |
| 365 | const uint8_t *ctr, |
| 366 | uint8_t *out, size_t outlen) |
| 367 | { |
| 368 | return kcapi_cipher_conv_enc_common("ctr(aes)", key, keylen, in, inlen, |
| 369 | ctr, out, outlen); |
| 370 | } |
| 371 | |
| 372 | ORIG_SYMVER(cipher_enc_aes_ctr, "1.0.0") |
| 373 | int32_t orig_cipher_enc_aes_ctr(const uint8_t *key, uint32_t keylen, |
| 374 | const uint8_t *in, uint32_t inlen, |
| 375 | const uint8_t *ctr, |
| 376 | uint8_t *out, uint32_t outlen) |
| 377 | { |
| 378 | return (int32_t)kcapi_cipher_conv_enc_common("ctr(aes)", key, keylen, |
| 379 | in, inlen, ctr, |
| 380 | out, outlen); |
| 381 | } |
| 382 | |
| 383 | IMPL_SYMVER(cipher_dec_aes_cbc, "1.3.1") |
| 384 | ssize_t impl_cipher_dec_aes_cbc(const uint8_t *key, uint32_t keylen, |
| 385 | const uint8_t *in, size_t inlen, |
| 386 | const uint8_t *iv, |
| 387 | uint8_t *out, size_t outlen) |
| 388 | { |
| 389 | return kcapi_cipher_conv_dec_common("cbc(aes)", key, keylen, in, inlen, |
| 390 | iv, out, outlen); |
| 391 | } |
| 392 | |
| 393 | ORIG_SYMVER(cipher_dec_aes_cbc, "1.0.0") |
| 394 | int32_t orig_cipher_dec_aes_cbc(const uint8_t *key, uint32_t keylen, |
| 395 | const uint8_t *in, uint32_t inlen, |
| 396 | const uint8_t *iv, |
| 397 | uint8_t *out, uint32_t outlen) |
| 398 | { |
| 399 | return (int32_t)kcapi_cipher_conv_dec_common("cbc(aes)", key, keylen, |
| 400 | in, inlen, iv, |
| 401 | out, outlen); |
| 402 | } |
| 403 | |
| 404 | IMPL_SYMVER(cipher_dec_aes_ctr, "1.3.1") |
| 405 | ssize_t impl_cipher_dec_aes_ctr(const uint8_t *key, uint32_t keylen, |
| 406 | const uint8_t *in, size_t inlen, |
| 407 | const uint8_t *ctr, |
| 408 | uint8_t *out, size_t outlen) |
| 409 | { |
| 410 | return kcapi_cipher_conv_dec_common("ctr(aes)", key, keylen, in, inlen, |
| 411 | ctr, out, outlen); |
| 412 | } |
| 413 | |
| 414 | ORIG_SYMVER(cipher_dec_aes_ctr, "1.0.0") |
| 415 | int32_t orig_cipher_dec_aes_ctr(const uint8_t *key, uint32_t keylen, |
| 416 | const uint8_t *in, uint32_t inlen, |
| 417 | const uint8_t *ctr, |
| 418 | uint8_t *out, uint32_t outlen) |
| 419 | { |
| 420 | return (int32_t)kcapi_cipher_conv_dec_common("ctr(aes)", key, keylen, |
| 421 | in, inlen, ctr, |
| 422 | out, outlen); |
| 423 | } |
| 424 | |
| 425 | ssize_t kcapi_cipher_enc_sm4_cbc(const uint8_t *key, uint32_t keylen, |
| 426 | const uint8_t *in, size_t inlen, |
| 427 | const uint8_t *iv, |
| 428 | uint8_t *out, size_t outlen) |
| 429 | { |
| 430 | return kcapi_cipher_conv_enc_common("cbc(sm4)", key, keylen, in, inlen, |
| 431 | iv, out, outlen); |
| 432 | } |
| 433 | |
| 434 | ssize_t kcapi_cipher_enc_sm4_ctr(const uint8_t *key, uint32_t keylen, |
| 435 | const uint8_t *in, size_t inlen, |
| 436 | const uint8_t *ctr, |
| 437 | uint8_t *out, size_t outlen) |
| 438 | { |
| 439 | return kcapi_cipher_conv_enc_common("ctr(sm4)", key, keylen, in, inlen, |
| 440 | ctr, out, outlen); |
| 441 | } |
| 442 | |
| 443 | ssize_t kcapi_cipher_dec_sm4_cbc(const uint8_t *key, uint32_t keylen, |
| 444 | const uint8_t *in, size_t inlen, |
| 445 | const uint8_t *iv, |
| 446 | uint8_t *out, size_t outlen) |
| 447 | { |
| 448 | return kcapi_cipher_conv_dec_common("cbc(sm4)", key, keylen, in, inlen, |
| 449 | iv, out, outlen); |
| 450 | } |
| 451 | |
| 452 | ssize_t kcapi_cipher_dec_sm4_ctr(const uint8_t *key, uint32_t keylen, |
| 453 | const uint8_t *in, size_t inlen, |
| 454 | const uint8_t *ctr, |
| 455 | uint8_t *out, size_t outlen) |
| 456 | { |
| 457 | return kcapi_cipher_conv_dec_common("ctr(sm4)", key, keylen, in, inlen, |
| 458 | ctr, out, outlen); |
| 459 | } |