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

« Back to documentation index

Show/hide line numbers pal_plat_entropy_sotp.c Source File

pal_plat_entropy_sotp.c

00001 /*******************************************************************************
00002 * Copyright 2018 ARM Ltd.
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 *******************************************************************************/
00016 
00017 #if !defined(MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT) 
00018 #include "pal.h"
00019 #include "sotp.h"
00020 #include "pal_plat_entropy.h"
00021 
00022 #define SOTP_ENTROPY_BUFF_SIZE (PAL_PLAT_MAX_ENTROPY_SIZE % 4 == 0 ? PAL_PLAT_MAX_ENTROPY_SIZE / 4 : PAL_PLAT_MAX_ENTROPY_SIZE / 4 + 1)
00023 
00024 //Error Translation from SOTP module to PAL
00025 PAL_PRIVATE palStatus_t pal_osSotpErrorTranslation(sotp_result_e err)
00026 {
00027     palStatus_t ret;
00028     switch(err)
00029     {
00030         case SOTP_SUCCESS:
00031             ret = PAL_SUCCESS;
00032             break;
00033         case SOTP_BAD_VALUE:
00034             ret = PAL_ERR_INVALID_ARGUMENT ;
00035             break;
00036         case SOTP_BUFF_TOO_SMALL:
00037             ret = PAL_ERR_BUFFER_TOO_SMALL ;
00038             break;
00039 
00040         case SOTP_BUFF_NOT_ALIGNED:
00041             ret = PAL_ERR_RTOS_BUFFER_NOT_ALIGNED ;
00042             break;
00043         case SOTP_NOT_FOUND:
00044             ret = PAL_ERR_ITEM_NOT_EXIST ;
00045             break;
00046 
00047         case SOTP_READ_ERROR:
00048         case SOTP_DATA_CORRUPT:
00049         case SOTP_OS_ERROR:
00050         default:
00051             ret = PAL_ERR_GENERIC_FAILURE ;
00052             break;
00053     }
00054     return ret;
00055 }
00056 
00057 // Merge pal_plat_osEntropyInject and pal_plat_mbedtls_nv_seed_write
00058 palStatus_t pal_plat_osEntropyInject(const uint8_t *entropyBuf, size_t bufSizeBytes)
00059 {
00060     sotp_result_e sotp_result;
00061     uint16_t len; // Not used, just placeholder
00062 
00063     sotp_result = sotp_get_item_size(SOTP_TYPE_RANDOM_SEED, &len);
00064     if (sotp_result == SOTP_SUCCESS)
00065     {
00066         return PAL_ERR_ENTROPY_EXISTS;
00067     }
00068     else
00069     {
00070         if (bufSizeBytes > PAL_PLAT_MAX_ENTROPY_SIZE)
00071         {
00072             return PAL_ERR_ENTROPY_TOO_LARGE;
00073         }
00074 
00075         // copy to an aligned buffer and store in SOTP
00076         uint32_t sotpBuf[SOTP_ENTROPY_BUFF_SIZE];
00077         memcpy(sotpBuf, entropyBuf, bufSizeBytes);
00078         return pal_osSotpErrorTranslation(sotp_set(SOTP_TYPE_RANDOM_SEED, bufSizeBytes, sotpBuf));
00079     }
00080     
00081 }
00082 
00083 
00084 #endif
00085