|
| 1 | +/**************************************************************************** |
| 2 | + * apps/crypto/mbedtls/source/pkcs5_alt.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + ****************************************************************************/ |
| 21 | + |
| 22 | +/**************************************************************************** |
| 23 | + * Included Files |
| 24 | + ****************************************************************************/ |
| 25 | + |
| 26 | +#include "mbedtls/pkcs5.h" |
| 27 | +#include "mbedtls/md.h" |
| 28 | +#include "mbedtls/error.h" |
| 29 | + |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include "dev_alt.h" |
| 33 | + |
| 34 | +/**************************************************************************** |
| 35 | + * Private Functions |
| 36 | + ****************************************************************************/ |
| 37 | + |
| 38 | +/**************************************************************************** |
| 39 | + * Name: pkcs5_alt_mac_from_md |
| 40 | + * |
| 41 | + * Description: |
| 42 | + * Map an mbedtls digest type to the matching cryptodev PBKDF2 MAC |
| 43 | + * algorithm. Only SHA1 and SHA256 are backed by the hardware engine; |
| 44 | + * every other digest is reported as unsupported. |
| 45 | + * |
| 46 | + ****************************************************************************/ |
| 47 | + |
| 48 | +static int pkcs5_alt_mac_from_md(mbedtls_md_type_t md_alg, FAR uint32_t *mac) |
| 49 | +{ |
| 50 | + switch (md_alg) |
| 51 | + { |
| 52 | + case MBEDTLS_MD_SHA1: |
| 53 | + *mac = CRYPTO_PBKDF2_HMAC_SHA1; |
| 54 | + return 0; |
| 55 | + case MBEDTLS_MD_SHA256: |
| 56 | + *mac = CRYPTO_PBKDF2_HMAC_SHA256; |
| 57 | + return 0; |
| 58 | + default: |
| 59 | + return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/**************************************************************************** |
| 64 | + * Public Functions |
| 65 | + ****************************************************************************/ |
| 66 | + |
| 67 | +int mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_type_t md_alg, |
| 68 | + FAR const unsigned char *password, |
| 69 | + size_t plen, |
| 70 | + FAR const unsigned char *salt, size_t slen, |
| 71 | + unsigned int iteration_count, |
| 72 | + uint32_t key_length, |
| 73 | + FAR unsigned char *output) |
| 74 | +{ |
| 75 | + uint8_t keybuf[HMAC_MAX_BLOCK_LEN]; |
| 76 | + cryptodev_context_t ctx; |
| 77 | + uint32_t mac; |
| 78 | + int ret; |
| 79 | + |
| 80 | + ret = pkcs5_alt_mac_from_md(md_alg, &mac); |
| 81 | + if (ret != 0) |
| 82 | + { |
| 83 | + return ret; |
| 84 | + } |
| 85 | + |
| 86 | + /* The HMAC key (password) is loaded into a fixed CSS key slot, so it must |
| 87 | + * fit the largest key the driver can accept. Longer passwords are not |
| 88 | + * supported by this hardware path. |
| 89 | + */ |
| 90 | + |
| 91 | + if (plen > sizeof(keybuf)) |
| 92 | + { |
| 93 | + return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
| 94 | + } |
| 95 | + |
| 96 | + ret = cryptodev_init(&ctx); |
| 97 | + if (ret != 0) |
| 98 | + { |
| 99 | + return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
| 100 | + } |
| 101 | + |
| 102 | + /* Copy the (possibly const) password into a writable buffer: the kernel |
| 103 | + * HMAC session setup XORs the key in place (ipad/opad). |
| 104 | + */ |
| 105 | + |
| 106 | + memcpy(keybuf, password, plen); |
| 107 | + |
| 108 | + ctx.session.cipher = 0; |
| 109 | + ctx.session.mac = mac; |
| 110 | + ctx.session.mackey = (caddr_t)keybuf; |
| 111 | + ctx.session.mackeylen = plen; |
| 112 | + |
| 113 | + ret = cryptodev_get_session(&ctx); |
| 114 | + if (ret != 0) |
| 115 | + { |
| 116 | + ret = MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE; |
| 117 | + goto errout; |
| 118 | + } |
| 119 | + |
| 120 | + ctx.crypt.ses = ctx.session.ses; |
| 121 | + ctx.crypt.op = COP_ENCRYPT; |
| 122 | + ctx.crypt.src = (caddr_t)salt; |
| 123 | + ctx.crypt.len = slen; |
| 124 | + ctx.crypt.mac = (caddr_t)output; |
| 125 | + ctx.crypt.olen = key_length; |
| 126 | + ctx.crypt.iterations = iteration_count; |
| 127 | + |
| 128 | + ret = cryptodev_crypt(&ctx); |
| 129 | + if (ret != 0) |
| 130 | + { |
| 131 | + ret = MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
| 132 | + } |
| 133 | + |
| 134 | + cryptodev_free_session(&ctx); |
| 135 | + |
| 136 | +errout: |
| 137 | + memset(keybuf, 0, sizeof(keybuf)); |
| 138 | + cryptodev_free(&ctx); |
| 139 | + return ret; |
| 140 | +} |
| 141 | + |
| 142 | +#if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 143 | +int mbedtls_pkcs5_pbkdf2_hmac(FAR mbedtls_md_context_t *ctx, |
| 144 | + FAR const unsigned char *password, |
| 145 | + size_t plen, |
| 146 | + FAR const unsigned char *salt, size_t slen, |
| 147 | + unsigned int iteration_count, |
| 148 | + uint32_t key_length, |
| 149 | + FAR unsigned char *output) |
| 150 | +{ |
| 151 | + mbedtls_md_type_t md_alg; |
| 152 | + |
| 153 | + if (ctx == NULL || ctx->MBEDTLS_PRIVATE(md_info) == NULL) |
| 154 | + { |
| 155 | + return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA; |
| 156 | + } |
| 157 | + |
| 158 | + md_alg = mbedtls_md_get_type(ctx->MBEDTLS_PRIVATE(md_info)); |
| 159 | + return mbedtls_pkcs5_pbkdf2_hmac_ext(md_alg, password, plen, salt, slen, |
| 160 | + iteration_count, key_length, output); |
| 161 | +} |
| 162 | +#endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
0 commit comments