| 1 | /* Kernel crypto API AF_ALG Message Digest 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_md_init(struct kcapi_handle **handle, const char *ciphername, |
| 25 | uint32_t flags) |
| 26 | { |
| 27 | return _kcapi_handle_init(handle, "hash", ciphername, flags); |
| 28 | } |
| 29 | |
| 30 | void kcapi_md_destroy(struct kcapi_handle *handle) |
| 31 | { |
| 32 | _kcapi_handle_destroy(handle); |
| 33 | } |
| 34 | |
| 35 | int kcapi_md_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 | static inline ssize_t _kcapi_md_update(struct kcapi_handle *handle, |
| 42 | const uint8_t *buffer, size_t len) |
| 43 | { |
| 44 | ssize_t ret = 0; |
| 45 | |
| 46 | if (len > INT_MAX) |
| 47 | return -EMSGSIZE; |
| 48 | |
| 49 | /* zero buffer length cannot be handled via splice */ |
| 50 | if (len < (1<<15)) { |
| 51 | ret = _kcapi_common_accept(handle); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | ret = send(*_kcapi_get_opfd(handle), buffer, len, MSG_MORE); |
| 55 | } else { |
| 56 | ret = _kcapi_common_vmsplice_chunk(handle, buffer, len, |
| 57 | SPLICE_F_MORE); |
| 58 | } |
| 59 | |
| 60 | if (ret < 0) |
| 61 | return ret; |
| 62 | if ((uint32_t)ret < len) |
| 63 | return -EIO; |
| 64 | |
| 65 | handle->processed_sg += 1; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | ssize_t kcapi_md_update(struct kcapi_handle *handle, |
| 70 | const uint8_t *buffer, size_t len) |
| 71 | { |
| 72 | return _kcapi_md_update(handle, buffer, len); |
| 73 | } |
| 74 | |
| 75 | static ssize_t _kcapi_md_final(struct kcapi_handle *handle, |
| 76 | uint8_t *buffer, size_t len) |
| 77 | { |
| 78 | struct iovec iov; |
| 79 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 80 | |
| 81 | if (!buffer || !len) { |
| 82 | kcapi_dolog(KCAPI_LOG_ERR, |
| 83 | "Message digest: output buffer too small (seen %lu - required %u)", |
| 84 | (unsigned long)len, tfm->info.hash_digestsize); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | /* Work around zero-sized hashing bug in pre-4.9 kernels: */ |
| 89 | if (!handle->flags.ge_v4_9 && !handle->processed_sg) |
| 90 | _kcapi_md_update(handle, NULL, 0); |
| 91 | |
| 92 | iov.iov_base = (void*)(uintptr_t)buffer; |
| 93 | iov.iov_len = len; |
| 94 | return _kcapi_common_recv_data(handle, &iov, 1); |
| 95 | } |
| 96 | |
| 97 | IMPL_SYMVER(md_final, "1.3.1") |
| 98 | ssize_t impl_md_final(struct kcapi_handle *handle, |
| 99 | uint8_t *buffer, size_t len) |
| 100 | { |
| 101 | return _kcapi_md_final(handle, buffer, len); |
| 102 | } |
| 103 | |
| 104 | ORIG_SYMVER(md_final, "0.12.0") |
| 105 | int32_t orig_md_final(struct kcapi_handle *handle, |
| 106 | uint8_t *buffer, uint32_t len) |
| 107 | { |
| 108 | return (int32_t)_kcapi_md_final(handle, buffer, len); |
| 109 | } |
| 110 | |
| 111 | IMPL_SYMVER(md_digest, "1.3.1") |
| 112 | ssize_t impl_md_digest(struct kcapi_handle *handle, |
| 113 | const uint8_t *in, size_t inlen, |
| 114 | uint8_t *out, size_t outlen) |
| 115 | { |
| 116 | ssize_t ret = 0; |
| 117 | |
| 118 | ret = _kcapi_md_update(handle, in, inlen); |
| 119 | if (0 > ret) |
| 120 | return ret; |
| 121 | return _kcapi_md_final(handle, out, outlen); |
| 122 | } |
| 123 | |
| 124 | ORIG_SYMVER(md_digest, "0.12.0") |
| 125 | int32_t orig_md_digest(struct kcapi_handle *handle, |
| 126 | const uint8_t *in, uint32_t inlen, |
| 127 | uint8_t *out, uint32_t outlen) |
| 128 | { |
| 129 | return (int32_t)impl_md_digest(handle, in, inlen, out, outlen); |
| 130 | } |
| 131 | |
| 132 | uint32_t kcapi_md_digestsize(struct kcapi_handle *handle) |
| 133 | { |
| 134 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 135 | |
| 136 | return tfm->info.hash_digestsize; |
| 137 | } |
| 138 | |
| 139 | uint32_t kcapi_md_blocksize(struct kcapi_handle *handle) |
| 140 | { |
| 141 | struct kcapi_handle_tfm *tfm = handle->tfm; |
| 142 | |
| 143 | return tfm->info.blocksize; |
| 144 | } |
| 145 | |
| 146 | static inline ssize_t kcapi_md_conv_common(const char *name, |
| 147 | const uint8_t *in, size_t inlen, |
| 148 | uint8_t *out, size_t outlen) |
| 149 | { |
| 150 | struct kcapi_handle *handle; |
| 151 | ssize_t ret = _kcapi_handle_init(&handle, "hash", name, 0); |
| 152 | |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | ret = kcapi_md_digest(handle, in, inlen, out, outlen); |
| 157 | |
| 158 | _kcapi_handle_destroy(handle); |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | IMPL_SYMVER(md_sha1, "1.3.1") |
| 164 | ssize_t impl_md_sha1(const uint8_t *in, size_t inlen, |
| 165 | uint8_t *out, size_t outlen) |
| 166 | { |
| 167 | return kcapi_md_conv_common("sha1", in, inlen, out, outlen); |
| 168 | } |
| 169 | |
| 170 | ORIG_SYMVER(md_sha1, "1.0.0") |
| 171 | int32_t orig_md_sha1(const uint8_t *in, uint32_t inlen, |
| 172 | uint8_t *out, uint32_t outlen) |
| 173 | { |
| 174 | return (int32_t)kcapi_md_conv_common("sha1", in, inlen, out, outlen); |
| 175 | } |
| 176 | |
| 177 | IMPL_SYMVER(md_sha224, "1.3.1") |
| 178 | ssize_t impl_md_sha224(const uint8_t *in, size_t inlen, |
| 179 | uint8_t *out, size_t outlen) |
| 180 | { |
| 181 | return kcapi_md_conv_common("sha224", in, inlen, out, outlen); |
| 182 | } |
| 183 | |
| 184 | ORIG_SYMVER(md_sha224, "1.0.0") |
| 185 | int32_t orig_md_sha224(const uint8_t *in, uint32_t inlen, |
| 186 | uint8_t *out, uint32_t outlen) |
| 187 | { |
| 188 | return (int32_t)kcapi_md_conv_common("sha224", in, inlen, out, outlen); |
| 189 | } |
| 190 | |
| 191 | IMPL_SYMVER(md_sha256, "1.3.1") |
| 192 | ssize_t impl_md_sha256(const uint8_t *in, size_t inlen, |
| 193 | uint8_t *out, size_t outlen) |
| 194 | { |
| 195 | return kcapi_md_conv_common("sha256", in, inlen, out, outlen); |
| 196 | } |
| 197 | |
| 198 | ORIG_SYMVER(md_sha256, "1.0.0") |
| 199 | ssize_t orig_md_sha256(const uint8_t *in, uint32_t inlen, |
| 200 | uint8_t *out, uint32_t outlen) |
| 201 | { |
| 202 | return (int32_t)kcapi_md_conv_common("sha256", in, inlen, out, outlen); |
| 203 | } |
| 204 | |
| 205 | IMPL_SYMVER(md_sha384, "1.3.1") |
| 206 | ssize_t impl_md_sha384(const uint8_t *in, size_t inlen, |
| 207 | uint8_t *out, size_t outlen) |
| 208 | { |
| 209 | return kcapi_md_conv_common("sha384", in, inlen, out, outlen); |
| 210 | } |
| 211 | |
| 212 | ORIG_SYMVER(md_sha384, "1.0.0") |
| 213 | int32_t orig_md_sha384(const uint8_t *in, uint32_t inlen, |
| 214 | uint8_t *out, uint32_t outlen) |
| 215 | { |
| 216 | return (int32_t)kcapi_md_conv_common("sha384", in, inlen, out, outlen); |
| 217 | } |
| 218 | |
| 219 | IMPL_SYMVER(md_sha512, "1.3.1") |
| 220 | ssize_t impl_md_sha512(const uint8_t *in, size_t inlen, |
| 221 | uint8_t *out, size_t outlen) |
| 222 | { |
| 223 | return kcapi_md_conv_common("sha512", in, inlen, out, outlen); |
| 224 | } |
| 225 | |
| 226 | ORIG_SYMVER(md_sha512, "1.0.0") |
| 227 | int32_t orig_md_sha512(const uint8_t *in, uint32_t inlen, |
| 228 | uint8_t *out, uint32_t outlen) |
| 229 | { |
| 230 | return (int32_t)kcapi_md_conv_common("sha512", in, inlen, out, outlen); |
| 231 | } |
| 232 | |
| 233 | ssize_t kcapi_md_sm3(const uint8_t *in, size_t inlen, |
| 234 | uint8_t *out, size_t outlen) |
| 235 | { |
| 236 | return kcapi_md_conv_common("sm3", in, inlen, out, outlen); |
| 237 | } |
| 238 | |
| 239 | static inline ssize_t kcapi_md_mac_conv_common(const char *name, |
| 240 | const uint8_t *key, uint32_t keylen, |
| 241 | const uint8_t *in, size_t inlen, uint8_t *out, size_t outlen) |
| 242 | { |
| 243 | struct kcapi_handle *handle; |
| 244 | ssize_t ret = _kcapi_handle_init(&handle, "hash", name, 0); |
| 245 | |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | ret = kcapi_md_setkey(handle, key, keylen); |
| 250 | if (ret) |
| 251 | goto out; |
| 252 | |
| 253 | ret = kcapi_md_digest(handle, in, inlen, out, outlen); |
| 254 | |
| 255 | out: |
| 256 | _kcapi_handle_destroy(handle); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | IMPL_SYMVER(md_hmac_sha1, "1.3.1") |
| 261 | ssize_t impl_md_hmac_sha1(const uint8_t *key, uint32_t keylen, |
| 262 | const uint8_t *in, size_t inlen, |
| 263 | uint8_t *out, size_t outlen) |
| 264 | { |
| 265 | return kcapi_md_mac_conv_common("hmac(sha1)", key, keylen, in, inlen, |
| 266 | out, outlen); |
| 267 | } |
| 268 | |
| 269 | ORIG_SYMVER(md_hmac_sha1, "1.0.0") |
| 270 | int32_t orig_md_hmac_sha1(const uint8_t *key, uint32_t keylen, |
| 271 | const uint8_t *in, uint32_t inlen, |
| 272 | uint8_t *out, uint32_t outlen) |
| 273 | { |
| 274 | return (int32_t)kcapi_md_mac_conv_common("hmac(sha1)", key, keylen, |
| 275 | in, inlen, out, outlen); |
| 276 | } |
| 277 | |
| 278 | IMPL_SYMVER(md_hmac_sha224, "1.3.1") |
| 279 | ssize_t impl_md_hmac_sha224(const uint8_t *key, uint32_t keylen, |
| 280 | const uint8_t *in, size_t inlen, |
| 281 | uint8_t *out, size_t outlen) |
| 282 | { |
| 283 | return kcapi_md_mac_conv_common("hmac(sha224)", key, keylen, in, inlen, |
| 284 | out, outlen); |
| 285 | } |
| 286 | |
| 287 | ORIG_SYMVER(md_hmac_sha224, "1.0.0") |
| 288 | int32_t orig_md_hmac_sha224(const uint8_t *key, uint32_t keylen, |
| 289 | const uint8_t *in, uint32_t inlen, |
| 290 | uint8_t *out, uint32_t outlen) |
| 291 | { |
| 292 | return (int32_t)kcapi_md_mac_conv_common("hmac(sha224)", key, keylen, |
| 293 | in, inlen, out, outlen); |
| 294 | } |
| 295 | |
| 296 | IMPL_SYMVER(md_hmac_sha256, "1.3.1") |
| 297 | ssize_t impl_md_hmac_sha256(const uint8_t *key, uint32_t keylen, |
| 298 | const uint8_t *in, size_t inlen, |
| 299 | uint8_t *out, size_t outlen) |
| 300 | { |
| 301 | return kcapi_md_mac_conv_common("hmac(sha256)", key, keylen, in, inlen, |
| 302 | out, outlen); |
| 303 | } |
| 304 | |
| 305 | ORIG_SYMVER(md_hmac_sha256, "1.0.0") |
| 306 | int32_t orig_md_hmac_sha256(const uint8_t *key, uint32_t keylen, |
| 307 | const uint8_t *in, uint32_t inlen, |
| 308 | uint8_t *out, uint32_t outlen) |
| 309 | { |
| 310 | return (int32_t)kcapi_md_mac_conv_common("hmac(sha256)", key, keylen, |
| 311 | in, inlen, out, outlen); |
| 312 | } |
| 313 | |
| 314 | IMPL_SYMVER(md_hmac_sha384, "1.3.1") |
| 315 | ssize_t impl_md_hmac_sha384(const uint8_t *key, uint32_t keylen, |
| 316 | const uint8_t *in, size_t inlen, |
| 317 | uint8_t *out, size_t outlen) |
| 318 | { |
| 319 | return kcapi_md_mac_conv_common("hmac(sha384)", key, keylen, in, inlen, |
| 320 | out, outlen); |
| 321 | } |
| 322 | |
| 323 | ORIG_SYMVER(md_hmac_sha384, "1.0.0") |
| 324 | int32_t orig_md_hmac_sha384(const uint8_t *key, uint32_t keylen, |
| 325 | const uint8_t *in, uint32_t inlen, |
| 326 | uint8_t *out, uint32_t outlen) |
| 327 | { |
| 328 | return (int32_t)kcapi_md_mac_conv_common("hmac(sha384)", key, keylen, |
| 329 | in, inlen, out, outlen); |
| 330 | } |
| 331 | |
| 332 | IMPL_SYMVER(md_hmac_sha512, "1.3.1") |
| 333 | ssize_t impl_md_hmac_sha512(const uint8_t *key, uint32_t keylen, |
| 334 | const uint8_t *in, size_t inlen, |
| 335 | uint8_t *out, size_t outlen) |
| 336 | { |
| 337 | return kcapi_md_mac_conv_common("hmac(sha512)", key, keylen, in, inlen, |
| 338 | out, outlen); |
| 339 | } |
| 340 | |
| 341 | ORIG_SYMVER(md_hmac_sha512, "1.0.0") |
| 342 | int32_t orig_md_hmac_sha512(const uint8_t *key, uint32_t keylen, |
| 343 | const uint8_t *in, uint32_t inlen, |
| 344 | uint8_t *out, uint32_t outlen) |
| 345 | { |
| 346 | return (int32_t)kcapi_md_mac_conv_common("hmac(sha512)", key, keylen, |
| 347 | in, inlen, out, outlen); |
| 348 | } |
| 349 | |
| 350 | ssize_t kcapi_md_hmac_sm3(const uint8_t *key, uint32_t keylen, |
| 351 | const uint8_t *in, size_t inlen, |
| 352 | uint8_t *out, size_t outlen) |
| 353 | { |
| 354 | return kcapi_md_mac_conv_common("hmac(sm3)", key, keylen, in, inlen, |
| 355 | out, outlen); |
| 356 | } |