| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <openssl/bytestring.h> |
| 18 | #include <openssl/err.h> |
| 19 | #include <openssl/hkdf.h> |
| 20 | #include <openssl/kdf.h> |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "../internal.h" |
| 24 | #include "internal.h" |
| 25 |
| 26 |
| 27 | typedef struct { |
| 28 | int mode; |
| 29 | const EVP_MD *md; |
| 30 | uint8_t *key; |
| 31 | size_t key_len; |
| 32 | uint8_t *salt; |
| 33 | size_t salt_len; |
| 34 | CBB info; |
| 35 | } HKDF_PKEY_CTX; |
| 36 |
| 37 | static int pkey_hkdf_init(EVP_PKEY_CTX *ctx) { |
| 38 | HKDF_PKEY_CTX *hctx = |
| 39 | reinterpret_cast<HKDF_PKEY_CTX *>(OPENSSL_zalloc(sizeof(HKDF_PKEY_CTX))); |
| 40 | if (hctx == NULL) { |
| 41 | return 0; |
| 42 | } |
| 43 |
| 44 | if (!CBB_init(&hctx->info, 0)) { |
| 45 | OPENSSL_free(hctx); |
| 46 | return 0; |
| 47 | } |
| 48 |
| 49 | ctx->data = hctx; |
| 50 | return 1; |
| 51 | } |
| 52 |
| 53 | static int pkey_hkdf_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { |
| 54 | if (!pkey_hkdf_init(dst)) { |
| 55 | return 0; |
| 56 | } |
| 57 |
| 58 | HKDF_PKEY_CTX *hctx_dst = reinterpret_cast<HKDF_PKEY_CTX *>(dst->data); |
| 59 | const HKDF_PKEY_CTX *hctx_src = |
| 60 | reinterpret_cast<const HKDF_PKEY_CTX *>(src->data); |
| 61 | hctx_dst->mode = hctx_src->mode; |
| 62 | hctx_dst->md = hctx_src->md; |
| 63 |
| 64 | if (hctx_src->key_len != 0) { |
| 65 | hctx_dst->key = reinterpret_cast<uint8_t *>( |
| 66 | OPENSSL_memdup(hctx_src->key, hctx_src->key_len)); |
| 67 | if (hctx_dst->key == NULL) { |
| 68 | return 0; |
| 69 | } |
| 70 | hctx_dst->key_len = hctx_src->key_len; |
| 71 | } |
| 72 |
| 73 | if (hctx_src->salt_len != 0) { |
| 74 | hctx_dst->salt = reinterpret_cast<uint8_t *>( |
| 75 | OPENSSL_memdup(hctx_src->salt, hctx_src->salt_len)); |
| 76 | if (hctx_dst->salt == NULL) { |
| 77 | return 0; |
| 78 | } |
| 79 | hctx_dst->salt_len = hctx_src->salt_len; |
| 80 | } |
| 81 |
| 82 | if (!CBB_add_bytes(&hctx_dst->info, CBB_data(&hctx_src->info), |
| 83 | CBB_len(&hctx_src->info))) { |
| 84 | return 0; |
| 85 | } |
| 86 |
| 87 | return 1; |
| 88 | } |
| 89 |
| 90 | static void pkey_hkdf_cleanup(EVP_PKEY_CTX *ctx) { |
| 91 | HKDF_PKEY_CTX *hctx = reinterpret_cast<HKDF_PKEY_CTX *>(ctx->data); |
| 92 | if (hctx != NULL) { |
| 93 | OPENSSL_free(hctx->key); |
| 94 | OPENSSL_free(hctx->salt); |
| 95 | CBB_cleanup(&hctx->info); |
| 96 | OPENSSL_free(hctx); |
| 97 | ctx->data = NULL; |
| 98 | } |
| 99 | } |
| 100 |
| 101 | static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len) { |
| 102 | HKDF_PKEY_CTX *hctx = reinterpret_cast<HKDF_PKEY_CTX *>(ctx->data); |
| 103 | if (hctx->md == NULL) { |
| 104 | OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS); |
| 105 | return 0; |
| 106 | } |
| 107 | if (hctx->key_len == 0) { |
| 108 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); |
| 109 | return 0; |
| 110 | } |
| 111 |
| 112 | if (out == NULL) { |
| 113 | if (hctx->mode == EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) { |
| 114 | *out_len = EVP_MD_size(hctx->md); |
| 115 | } |
| 116 | // HKDF-Expand is variable-length and returns |*out_len| bytes. "Output" the |
| 117 | // input length by leaving it alone. |
| 118 | return 1; |
| 119 | } |
| 120 |
| 121 | switch (hctx->mode) { |
| 122 | case EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND: |
| 123 | return HKDF(out, *out_len, hctx->md, hctx->key, hctx->key_len, hctx->salt, |
| 124 | hctx->salt_len, CBB_data(&hctx->info), CBB_len(&hctx->info)); |
| 125 |
| 126 | case EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY: |
| 127 | if (*out_len < EVP_MD_size(hctx->md)) { |
| 128 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 129 | return 0; |
| 130 | } |
| 131 | return HKDF_extract(out, out_len, hctx->md, hctx->key, hctx->key_len, |
| 132 | hctx->salt, hctx->salt_len); |
| 133 |
| 134 | case EVP_PKEY_HKDEF_MODE_EXPAND_ONLY: |
| 135 | return HKDF_expand(out, *out_len, hctx->md, hctx->key, hctx->key_len, |
| 136 | CBB_data(&hctx->info), CBB_len(&hctx->info)); |
| 137 | } |
| 138 | OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); |
| 139 | return 0; |
| 140 | } |
| 141 |
| 142 | static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { |
| 143 | HKDF_PKEY_CTX *hctx = reinterpret_cast<HKDF_PKEY_CTX *>(ctx->data); |
| 144 | switch (type) { |
| 145 | case EVP_PKEY_CTRL_HKDF_MODE: |
| 146 | if (p1 != EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND && |
| 147 | p1 != EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY && |
| 148 | p1 != EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) { |
| 149 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION); |
| 150 | return 0; |
| 151 | } |
| 152 | hctx->mode = p1; |
| 153 | return 1; |
| 154 | case EVP_PKEY_CTRL_HKDF_MD: |
| 155 | hctx->md = reinterpret_cast<const EVP_MD *>(p2); |
| 156 | return 1; |
| 157 | case EVP_PKEY_CTRL_HKDF_KEY: { |
| 158 | const CBS *key = reinterpret_cast<const CBS *>(p2); |
| 159 | if (!CBS_stow(key, &hctx->key, &hctx->key_len)) { |
| 160 | return 0; |
| 161 | } |
| 162 | return 1; |
| 163 | } |
| 164 | case EVP_PKEY_CTRL_HKDF_SALT: { |
| 165 | const CBS *salt = reinterpret_cast<const CBS *>(p2); |
| 166 | if (!CBS_stow(salt, &hctx->salt, &hctx->salt_len)) { |
| 167 | return 0; |
| 168 | } |
| 169 | return 1; |
| 170 | } |
| 171 | case EVP_PKEY_CTRL_HKDF_INFO: { |
| 172 | const CBS *info = reinterpret_cast<const CBS *>(p2); |
| 173 | // |EVP_PKEY_CTX_add1_hkdf_info| appends to the info string, rather than |
| 174 | // replacing it. |
| 175 | if (!CBB_add_bytes(&hctx->info, CBS_data(info), CBS_len(info))) { |
| 176 | return 0; |
| 177 | } |
| 178 | return 1; |
| 179 | } |
| 180 | default: |
| 181 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 |
| 186 | const EVP_PKEY_METHOD hkdf_pkey_meth = { |
| 187 | /*pkey_id=*/EVP_PKEY_HKDF, |
| 188 | pkey_hkdf_init, |
| 189 | pkey_hkdf_copy, |
| 190 | pkey_hkdf_cleanup, |
| 191 | /*keygen=*/NULL, |
| 192 | /*sign=*/NULL, |
| 193 | /*sign_message=*/NULL, |
| 194 | /*verify=*/NULL, |
| 195 | /*verify_message=*/NULL, |
| 196 | /*verify_recover=*/NULL, |
| 197 | /*encrypt=*/NULL, |
| 198 | /*decrypt=*/NULL, |
| 199 | pkey_hkdf_derive, |
| 200 | /*paramgen=*/NULL, |
| 201 | pkey_hkdf_ctrl, |
| 202 | }; |
| 203 |
| 204 | int EVP_PKEY_CTX_hkdf_mode(EVP_PKEY_CTX *ctx, int mode) { |
| 205 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_HKDF, EVP_PKEY_OP_DERIVE, |
| 206 | EVP_PKEY_CTRL_HKDF_MODE, mode, NULL); |
| 207 | } |
| 208 |
| 209 | int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) { |
| 210 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_HKDF, EVP_PKEY_OP_DERIVE, |
| 211 | EVP_PKEY_CTRL_HKDF_MD, 0, (void *)md); |
| 212 | } |
| 213 |
| 214 | int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, const uint8_t *key, |
| 215 | size_t key_len) { |
| 216 | CBS cbs; |
| 217 | CBS_init(&cbs, key, key_len); |
| 218 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_HKDF, EVP_PKEY_OP_DERIVE, |
| 219 | EVP_PKEY_CTRL_HKDF_KEY, 0, &cbs); |
| 220 | } |
| 221 |
| 222 | int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, const uint8_t *salt, |
| 223 | size_t salt_len) { |
| 224 | CBS cbs; |
| 225 | CBS_init(&cbs, salt, salt_len); |
| 226 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_HKDF, EVP_PKEY_OP_DERIVE, |
| 227 | EVP_PKEY_CTRL_HKDF_SALT, 0, &cbs); |
| 228 | } |
| 229 |
| 230 | int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, const uint8_t *info, |
| 231 | size_t info_len) { |
| 232 | CBS cbs; |
| 233 | CBS_init(&cbs, info, info_len); |
| 234 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_HKDF, EVP_PKEY_OP_DERIVE, |
| 235 | EVP_PKEY_CTRL_HKDF_INFO, 0, &cbs); |
| 236 | } |
| 237 |