leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_pre_shared_key.c Source File

arm_uc_pre_shared_key.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-client-common/arm_uc_config.h"
00020 #include "update-client-control-center/arm_uc_pre_shared_key.h"
00021 
00022 #if defined(ARM_UC_FEATURE_PSK_STORE_NVSTORE) && (ARM_UC_FEATURE_PSK_STORE_NVSTORE==1)
00023 
00024 
00025 #include "CloudClientStorage.h"
00026 
00027 #define DEFAULT_PSK_SIZE 128
00028 #define DEFAULT_PSK_SIZE_IN_BYTES (128/8)
00029 
00030 /* Pointer to the pre-shared-key. Module only supports 1 key at a time. */
00031 static uint16_t arm_uc_psk_size = DEFAULT_PSK_SIZE;
00032 static uint8_t pskbuffer[DEFAULT_PSK_SIZE_IN_BYTES] = { 0 };
00033 
00034 /**
00035  * @brief Set pointer to pre-shared-key with the given size.
00036  *
00037  * @param key Pointer to pre-shared-key.
00038  * @param bits Key size in bits.
00039  *
00040  * @return Error code.
00041  */
00042 arm_uc_error_t ARM_UC_PreSharedKey_SetSecret(const uint8_t *key, uint16_t bits)
00043 {
00044     // Do not support currently, you should provision to nvstore blob into image
00045     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00046 
00047     return result;
00048 }
00049 
00050 /**
00051  * @brief Get pointer to pre-shared-key with the given size.
00052  *
00053  * @param key Pointer-pointer to the shared key.
00054  * @param bits Key size in bits.
00055  *
00056  * @return Error code.
00057  */
00058 arm_uc_error_t ARM_UC_PreSharedKey_GetSecret(const uint8_t **key, uint16_t bits)
00059 {
00060     arm_uc_error_t retval = (arm_uc_error_t) { ERR_INVALID_PARAMETER };
00061 
00062     if (key && (bits == arm_uc_psk_size)) {
00063         size_t value_length = 0;
00064         memset(pskbuffer, 0, DEFAULT_PSK_SIZE_IN_BYTES);
00065 
00066         ccs_status_e ccs_status = get_config_parameter(UPDATE_PSK_SECRET,
00067                                                        pskbuffer,
00068                                                        arm_uc_psk_size,
00069                                                        &value_length);
00070         if (ccs_status == CCS_STATUS_KEY_DOESNT_EXIST) {
00071             retval.code = ARM_UC_DI_ERR_NOT_FOUND;
00072         }
00073 
00074         if (ccs_status == CCS_STATUS_SUCCESS) {
00075             retval.code = ERR_NONE;
00076 
00077             *key = pskbuffer;
00078 
00079         }
00080         /* set return value */
00081         retval = (arm_uc_error_t) { ERR_NONE };
00082 
00083     }
00084 
00085     return retval;
00086 }
00087 
00088 #elif defined(ARM_UC_FEATURE_PSK_STORE_RAW) && (ARM_UC_FEATURE_PSK_STORE_RAW==1)
00089 
00090 // Below is runtime memory-based volatile solution without NVSTORE
00091 /* Pointer to the pre-shared-key. Module only supports 1 key at a time. */
00092 static const uint8_t *arm_uc_psk_key = NULL;
00093 static uint16_t arm_uc_psk_size = 0;
00094 
00095 /**
00096  * @brief Set pointer to pre-shared-key with the given size.
00097  *
00098  * @param key Pointer to pre-shared-key.
00099  * @param bits Key size in bits.
00100  *
00101  * @return Error code.
00102  */
00103 arm_uc_error_t ARM_UC_PreSharedKey_SetSecret(const uint8_t *key, uint16_t bits)
00104 {
00105     arm_uc_psk_key = key;
00106     arm_uc_psk_size = bits;
00107 
00108     return (arm_uc_error_t) { ERR_NONE};
00109 }
00110 
00111 /**
00112  * @brief Get pointer to pre-shared-key with the given size.
00113  *
00114  * @param key Pointer-pointer to the shared key.
00115  * @param bits Key size in bits.
00116  *
00117  * @return Error code.
00118  */
00119 arm_uc_error_t ARM_UC_PreSharedKey_GetSecret(const uint8_t **key, uint16_t bits)
00120 {
00121     arm_uc_error_t retval = (arm_uc_error_t) { ERR_INVALID_PARAMETER };
00122 
00123     if (key && (bits == arm_uc_psk_size)) {
00124         /* set return value */
00125         retval = (arm_uc_error_t) { ERR_NONE };
00126 
00127         /* assign PSK pointer */
00128         *key = arm_uc_psk_key;
00129     }
00130 
00131     return retval;
00132 }
00133 
00134 #endif /* ARM_UC_FEATURE_PSK_STORE_NVSTORE/RAW */