|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "PSASpake2p.h" |
| 19 | + |
| 20 | +#include "CHIPCryptoPALPSA.h" |
| 21 | + |
| 22 | +#include <lib/support/CodeUtils.h> |
| 23 | + |
| 24 | +#include <psa/crypto.h> |
| 25 | + |
| 26 | +namespace chip { |
| 27 | +namespace Crypto { |
| 28 | + |
| 29 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::Init(const uint8_t * context, size_t context_len) |
| 30 | +{ |
| 31 | + Clear(); |
| 32 | + |
| 33 | + VerifyOrReturnError(context_len <= sizeof(mContext), CHIP_ERROR_BUFFER_TOO_SMALL); |
| 34 | + |
| 35 | + memcpy(mContext, context, context_len); |
| 36 | + mContextLen = context_len; |
| 37 | + |
| 38 | + return CHIP_NO_ERROR; |
| 39 | +} |
| 40 | + |
| 41 | +void PSASpake2p_P256_SHA256_HKDF_HMAC::Clear() |
| 42 | +{ |
| 43 | + IgnoreUnusedVariable(psa_pake_abort(&mOperation)); |
| 44 | + mOperation = psa_pake_operation_init(); |
| 45 | + |
| 46 | + IgnoreUnusedVariable(psa_destroy_key(mKey)); |
| 47 | + mKey = PSA_KEY_ID_NULL; |
| 48 | +} |
| 49 | + |
| 50 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::BeginVerifier(const uint8_t * my_identity, size_t my_identity_len, |
| 51 | + const uint8_t * peer_identity, size_t peer_identity_len, |
| 52 | + const uint8_t * w0in, size_t w0in_len, const uint8_t * Lin, |
| 53 | + size_t Lin_len) |
| 54 | +{ |
| 55 | + VerifyOrReturnError(w0in_len <= kSpake2p_WS_Length, CHIP_ERROR_INVALID_ARGUMENT); |
| 56 | + VerifyOrReturnError(Lin_len == kP256_Point_Length, CHIP_ERROR_INVALID_ARGUMENT); |
| 57 | + |
| 58 | + uint8_t password[kSpake2p_WS_Length + kP256_Point_Length]; |
| 59 | + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 60 | + psa_pake_cipher_suite_t cp = PSA_PAKE_CIPHER_SUITE_INIT; |
| 61 | + |
| 62 | + psa_pake_cs_set_algorithm(&cp, PSA_ALG_SPAKE2P_MATTER); |
| 63 | + psa_pake_cs_set_primitive(&cp, PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256)); |
| 64 | + |
| 65 | + memcpy(password + 0, w0in, w0in_len); |
| 66 | + memcpy(password + w0in_len, Lin, Lin_len); |
| 67 | + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 68 | + psa_set_key_algorithm(&attributes, PSA_ALG_SPAKE2P_MATTER); |
| 69 | + psa_set_key_type(&attributes, PSA_KEY_TYPE_SPAKE2P_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)); |
| 70 | + |
| 71 | + psa_status_t status = psa_import_key(&attributes, password, w0in_len + Lin_len, &mKey); |
| 72 | + |
| 73 | + psa_reset_key_attributes(&attributes); |
| 74 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 75 | + |
| 76 | + status = psa_pake_setup(&mOperation, mKey, &cp); |
| 77 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 78 | + |
| 79 | + mRole = PSA_PAKE_ROLE_SERVER; |
| 80 | + status = psa_pake_set_role(&mOperation, PSA_PAKE_ROLE_SERVER); |
| 81 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 82 | + |
| 83 | + status = psa_pake_set_peer(&mOperation, peer_identity, peer_identity_len); |
| 84 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 85 | + |
| 86 | + status = psa_pake_set_user(&mOperation, my_identity, my_identity_len); |
| 87 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 88 | + |
| 89 | + status = psa_pake_set_context(&mOperation, mContext, mContextLen); |
| 90 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 91 | + |
| 92 | + return CHIP_NO_ERROR; |
| 93 | +} |
| 94 | + |
| 95 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::BeginProver(const uint8_t * my_identity, size_t my_identity_len, |
| 96 | + const uint8_t * peer_identity, size_t peer_identity_len, |
| 97 | + const uint8_t * w0in, size_t w0in_len, const uint8_t * w1in, |
| 98 | + size_t w1in_len) |
| 99 | +{ |
| 100 | + VerifyOrReturnError(w0in_len <= kSpake2p_WS_Length, CHIP_ERROR_INVALID_ARGUMENT); |
| 101 | + VerifyOrReturnError(w1in_len <= kSpake2p_WS_Length, CHIP_ERROR_INVALID_ARGUMENT); |
| 102 | + |
| 103 | + uint8_t password[kSpake2p_WS_Length * 2]; |
| 104 | + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 105 | + psa_pake_cipher_suite_t cp = PSA_PAKE_CIPHER_SUITE_INIT; |
| 106 | + |
| 107 | + psa_pake_cs_set_algorithm(&cp, PSA_ALG_SPAKE2P_MATTER); |
| 108 | + psa_pake_cs_set_primitive(&cp, PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256)); |
| 109 | + |
| 110 | + memcpy(password + 0, w0in, w0in_len); |
| 111 | + memcpy(password + w0in_len, w1in, w1in_len); |
| 112 | + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 113 | + psa_set_key_algorithm(&attributes, PSA_ALG_SPAKE2P_MATTER); |
| 114 | + psa_set_key_type(&attributes, PSA_KEY_TYPE_SPAKE2P_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
| 115 | + |
| 116 | + psa_status_t status = psa_import_key(&attributes, password, w0in_len + w1in_len, &mKey); |
| 117 | + |
| 118 | + psa_reset_key_attributes(&attributes); |
| 119 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 120 | + |
| 121 | + status = psa_pake_setup(&mOperation, mKey, &cp); |
| 122 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 123 | + |
| 124 | + mRole = PSA_PAKE_ROLE_CLIENT; |
| 125 | + status = psa_pake_set_role(&mOperation, PSA_PAKE_ROLE_CLIENT); |
| 126 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 127 | + |
| 128 | + status = psa_pake_set_user(&mOperation, my_identity, my_identity_len); |
| 129 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 130 | + |
| 131 | + status = psa_pake_set_peer(&mOperation, peer_identity, peer_identity_len); |
| 132 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 133 | + |
| 134 | + status = psa_pake_set_context(&mOperation, mContext, mContextLen); |
| 135 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 136 | + |
| 137 | + return CHIP_NO_ERROR; |
| 138 | +} |
| 139 | + |
| 140 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::ComputeRoundOne(const uint8_t * pab, size_t pab_len, uint8_t * out, size_t * out_len) |
| 141 | +{ |
| 142 | + VerifyOrReturnError(out_len != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 143 | + |
| 144 | + psa_status_t status; |
| 145 | + |
| 146 | + if (mRole == PSA_PAKE_ROLE_SERVER) |
| 147 | + { |
| 148 | + status = psa_pake_input(&mOperation, PSA_PAKE_STEP_KEY_SHARE, pab, pab_len); |
| 149 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 150 | + } |
| 151 | + |
| 152 | + status = psa_pake_output(&mOperation, PSA_PAKE_STEP_KEY_SHARE, out, *out_len, out_len); |
| 153 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 154 | + |
| 155 | + return CHIP_NO_ERROR; |
| 156 | +} |
| 157 | + |
| 158 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::ComputeRoundTwo(const uint8_t * in, size_t in_len, uint8_t * out, size_t * out_len) |
| 159 | +{ |
| 160 | + VerifyOrReturnError(out_len != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 161 | + |
| 162 | + psa_status_t status; |
| 163 | + |
| 164 | + if (mRole == PSA_PAKE_ROLE_CLIENT) |
| 165 | + { |
| 166 | + status = psa_pake_input(&mOperation, PSA_PAKE_STEP_KEY_SHARE, in, in_len); |
| 167 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 168 | + } |
| 169 | + |
| 170 | + status = psa_pake_output(&mOperation, PSA_PAKE_STEP_CONFIRM, out, *out_len, out_len); |
| 171 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 172 | + |
| 173 | + return CHIP_NO_ERROR; |
| 174 | +} |
| 175 | + |
| 176 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::KeyConfirm(const uint8_t * in, size_t in_len) |
| 177 | +{ |
| 178 | + psa_status_t status = psa_pake_input(&mOperation, PSA_PAKE_STEP_CONFIRM, in, in_len); |
| 179 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 180 | + |
| 181 | + return CHIP_NO_ERROR; |
| 182 | +} |
| 183 | + |
| 184 | +CHIP_ERROR PSASpake2p_P256_SHA256_HKDF_HMAC::GetKeys(SessionKeystore & keystore, HkdfKeyHandle & key) |
| 185 | +{ |
| 186 | + auto & keyId = key.AsMutable<psa_key_id_t>(); |
| 187 | + |
| 188 | + psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 189 | + |
| 190 | + psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE); |
| 191 | + psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
| 192 | + psa_set_key_algorithm(&attributes, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 193 | + |
| 194 | + psa_status_t status = psa_pake_get_shared_key(&(mOperation), &attributes, &keyId); |
| 195 | + VerifyOrReturnError(status == PSA_SUCCESS, CHIP_ERROR_INTERNAL); |
| 196 | + |
| 197 | + return CHIP_NO_ERROR; |
| 198 | +} |
| 199 | + |
| 200 | +} // namespace Crypto |
| 201 | +} // namespace chip |
0 commit comments