| 1 | /* |
| 2 | * Copyright (c) 2015, Secure Endpoints Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include "krb5_locl.h"cxx . . $ `<file>/lib/krb5/krb5_locl.h`/ |
| 33 | |
| 34 | /* |
| 35 | * SP800-108 KDF |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * As described in SP800-108 5.1 (for HMAC) |
| 40 | * |
| 41 | * @param context Kerberos 5 context |
| 42 | * @param kdf_K1 Base key material. |
| 43 | * @param kdf_label A string that identifies the purpose for the derived key. |
| 44 | * @param kdf_context A binary string containing parties, nonce, etc. |
| 45 | * @param md Message digest function to use for PRF. |
| 46 | * @param kdf_K0 Derived key data. |
| 47 | * |
| 48 | * @return Return an error code for an failure or 0 on success. |
| 49 | * @ingroup krb5_crypto |
| 50 | */ |
| 51 | krb5_error_codecxx . . $ krb5_error_code# |
| 52 | _krb5_SP800_108_HMAC_KDFcxx . . $ _krb5_SP800_108_HMAC_KDF(97c7bac2ec0ec15f).(krb5_contextcxx . . $ krb5_context# context, |
| 53 | const krb5_datacxx . . $ krb5_data# *kdf_K1, |
| 54 | const krb5_datacxx . . $ krb5_data# *kdf_label, |
| 55 | const krb5_datacxx . . $ krb5_data# *kdf_context, |
| 56 | const EVP_MDcxx . . $ `include/hcrypto/evp.h:114:9`! *md, |
| 57 | krb5_datacxx . . $ krb5_data# *kdf_K0) |
| 58 | { |
| 59 | HMAC_CTXcxx . . $ HMAC_CTX# c; |
| 60 | unsigned char *p = kdf_K0->datacxx . . $ heim_base_data#data.; |
| 61 | size_t i, n, left = kdf_K0->lengthcxx . . $ heim_base_data#length.; |
| 62 | unsigned char hmac[EVP_MAX_MD_SIZEcxx . . $ `include/hcrypto/evp.h:145:9`!]; |
| 63 | unsigned int h = EVP_MD_sizecxx . . $ `include/hcrypto/evp.h:73:9`!(md); |
| 64 | const size_t L = kdf_K0->lengthcxx . . $ heim_base_data#length.; |
| 65 | |
| 66 | heim_assertcxx . . $ `include/heimbase.h:124:9`!(md != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, "SP800-108 KDF internal error"); |
| 67 | |
| 68 | HMAC_CTX_initcxx . . $ `include/hcrypto/hmac.h:42:9`!(&c); |
| 69 | |
| 70 | n = L / h; |
| 71 | |
| 72 | for (i = 0; i <= n; i++) { |
| 73 | unsigned char tmp[4]; |
| 74 | size_t len; |
| 75 | |
| 76 | HMAC_Init_excxx . . $ `include/hcrypto/hmac.h:45:9`!(&c, kdf_K1->datacxx . . $ heim_base_data#data., kdf_K1->lengthcxx . . $ heim_base_data#length., md, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 77 | |
| 78 | _krb5_put_intcxx . . $ _krb5_put_int(d4616eeeba662c18).(tmp, i + 1, 4); |
| 79 | HMAC_Updatecxx . . $ `include/hcrypto/hmac.h:46:9`!(&c, tmp, 4); |
| 80 | HMAC_Updatecxx . . $ `include/hcrypto/hmac.h:46:9`!(&c, kdf_label->datacxx . . $ heim_base_data#data., kdf_label->lengthcxx . . $ heim_base_data#length.); |
| 81 | HMAC_Updatecxx . . $ `include/hcrypto/hmac.h:46:9`!(&c, (unsigned char *)"", 1); |
| 82 | if (kdf_context) |
| 83 | HMAC_Updatecxx . . $ `include/hcrypto/hmac.h:46:9`!(&c, kdf_context->datacxx . . $ heim_base_data#data., kdf_context->lengthcxx . . $ heim_base_data#length.); |
| 84 | _krb5_put_intcxx . . $ _krb5_put_int(d4616eeeba662c18).(tmp, L * 8, 4); |
| 85 | HMAC_Updatecxx . . $ `include/hcrypto/hmac.h:46:9`!(&c, tmp, 4); |
| 86 | |
| 87 | HMAC_Finalcxx . . $ `include/hcrypto/hmac.h:47:9`!(&c, hmac, &h); |
| 88 | len = h > left ? left : h; |
| 89 | memcpycxx . . $ memcpy(cc817cb175f282d).(p, hmac, len); |
| 90 | p += len; |
| 91 | left -= len; |
| 92 | } |
| 93 | |
| 94 | HMAC_CTX_cleanupcxx . . $ `include/hcrypto/hmac.h:43:9`!(&c); |
| 95 | |
| 96 | return 0; |
| 97 | } |