1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #include "hitls_build.h" 17 #if defined(HITLS_CRYPTO_ENTROPY) && defined(HITLS_CRYPTO_ENTROPY_SYS) 18 19 #include <stdint.h> 20 #include <stddef.h> 21 #include "bsl_err_internal.h" 22 #include "crypt_errno.h" 23 #include "es_health_test.h" 24 ES_HealthTestRct(ES_HealthTest * state,uint64_t data)25int32_t ES_HealthTestRct(ES_HealthTest *state, uint64_t data) 26 { 27 if (data == state->lastData) { 28 state->rctCount++; 29 if (state->rctCount >= state->rctCutoff) { 30 BSL_ERR_PUSH_ERROR(CRYPT_ENTROPY_RCT_FAILURE); 31 return CRYPT_ENTROPY_RCT_FAILURE; 32 } 33 } else { 34 state->lastData = data; 35 state->rctCount = 1; 36 } 37 38 return CRYPT_SUCCESS; 39 } 40 ES_HealthTestApt(ES_HealthTest * state,uint64_t data)41int32_t ES_HealthTestApt(ES_HealthTest *state, uint64_t data) 42 { 43 if (state->aptBaseSet == 0) { // NIST SP800-90B section 4.4.2 step 1/2 44 state->aptBaseSet = 1; 45 state->aptBaseData = data; 46 state->aptCount = 1; 47 state->aptI = 1; 48 return CRYPT_SUCCESS; 49 } 50 51 if (state->aptBaseData == data) { 52 state->aptCount++; 53 if (state->aptCount >= state->aptCutOff) { 54 state->aptBaseSet = 0; // Restart an APT window next time. 55 BSL_ERR_PUSH_ERROR(CRYPT_ENTROPY_APT_FAILURE); 56 return CRYPT_ENTROPY_APT_FAILURE; 57 } 58 } 59 60 state->aptI++; 61 if (state->aptI >= state->aptWindowSize) { 62 state->aptBaseSet = 0; 63 } 64 return CRYPT_SUCCESS; 65 } 66 67 #endif