code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_HASH #include "securec.h" #include "bsl_sal.h" #include "list_base.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "hash_local.h" #include "bsl_hash.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define BSL_CSTL_HASH_OPTION3 3 #define BSL_CSTL_HASH_OPTION2 2 #define BSL_CSTL_HASH_OPTION1 1 struct BSL_HASH_TagNode { ListRawNode node; /**< Linked list node */ uintptr_t key; /**< Key or address for storing the key */ uintptr_t value; /**< value or address for storing the value */ }; typedef struct BSL_HASH_TagNode BSL_HASH_Node; struct BSL_HASH_Info { ListDupFreeFuncPair keyFunc; /**< key Copy and release function pair */ ListDupFreeFuncPair valueFunc; /**< value Copy and release function pair */ BSL_HASH_MatchFunc matchFunc; /**< matching function */ BSL_HASH_CodeCalcFunc hashFunc; /**< hash function */ uint32_t bucketSize; /**< hash table size */ uint32_t hashCount; /**< number of entries in the hash table */ RawList listArray[0]; /**< linked list control block array*/ }; /* murmurhash algorithm */ /* define constants */ #define HASH_VC1 0xCC9E2D51 #define HASH_VC2 0x1B873593 #define HASH_HC1 0xE6546B64 #define HASH_HC2 0x85EBCA6B #define HASH_HC3 0xC2B2AE35 #define HASH_HC4 5 #define CHAR_BIT 8 #define CHAR_FOR_PER_LOOP 4 #define HASH_V_ROTATE 15 #define HASH_H_ROTATE 13 #define SYS_BUS_WIDTH sizeof(uint32_t) #define HASH_SEED 0x3B9ACA07 /* large prime 1000000007. The seed can be random or specified. */ enum BSL_CstlByte { ONE_BYTE = 1, TWO_BYTE = 2, }; enum BSL_CstlShiftBit { SHIFT8 = 8, SHIFT13 = 13, SHIFT16 = 16, SHIFT24 = 24 }; static uint32_t BSL_HASH_Rotate(uint32_t v, uint32_t offset) { return ((v << offset) | (v >> (SYS_BUS_WIDTH * CHAR_BIT - offset))); } static uint32_t BSL_HASH_MixV(uint32_t v) { uint32_t res = v; res = res * HASH_VC1; res = BSL_HASH_Rotate(res, HASH_V_ROTATE); return res * HASH_VC2; } static uint32_t BSL_HASH_MixH(uint32_t h, uint32_t v) { uint32_t res = h; res ^= v; res = BSL_HASH_Rotate(res, HASH_H_ROTATE); return res * HASH_HC4 + HASH_HC1; } uint32_t BSL_HASH_CodeCalc(void *key, uint32_t keySize) { uint8_t *tmpKey = (uint8_t *)key; uint32_t i = 0; uint32_t v; uint32_t h = HASH_SEED; uint8_t c0, c1, c2, c3; uint32_t tmpLen = keySize - keySize % CHAR_FOR_PER_LOOP; while ((i + CHAR_FOR_PER_LOOP) <= tmpLen) { c0 = tmpKey[i++]; c1 = tmpKey[i++]; c2 = tmpKey[i++]; c3 = tmpKey[i++]; v = (uint32_t)c0 | ((uint32_t)c1 << SHIFT8) | ((uint32_t)c2 << SHIFT16) | ((uint32_t)c3 << SHIFT24); v = BSL_HASH_MixV(v); h = BSL_HASH_MixH(h, v); } v = 0; switch (keySize & BSL_CSTL_HASH_OPTION3) { case BSL_CSTL_HASH_OPTION3: v ^= ((uint32_t)tmpKey[i + TWO_BYTE] << SHIFT16); /* (keySize % 4) is equals 3, fallthrough, other branches are the same. */ /* fall-through */ case BSL_CSTL_HASH_OPTION2: v ^= ((uint32_t)tmpKey[i + ONE_BYTE] << SHIFT8); /* fall-through */ case BSL_CSTL_HASH_OPTION1: v ^= tmpKey[i]; v = BSL_HASH_MixV(v); h ^= v; break; default: break; } h ^= h >> SHIFT16; h *= HASH_HC2; h ^= h >> SHIFT13; h *= HASH_HC3; h ^= h >> SHIFT16; return h; } /* internal function definition */ static void BSL_HASH_HookRegister(BSL_HASH_Hash *hash, BSL_HASH_CodeCalcFunc hashFunc, BSL_HASH_MatchFunc matchFunc, ListDupFreeFuncPair *keyFunc, ListDupFreeFuncPair *valueFunc) { ListDupFreeFuncPair *hashKeyFunc = &hash->keyFunc; ListDupFreeFuncPair *hashValueFunc = &hash->valueFunc; hash->hashFunc = hashFunc == NULL ? BSL_HASH_CodeCalcInt : hashFunc; hash->matchFunc = matchFunc == NULL ? BSL_HASH_MatchInt : matchFunc; if (keyFunc == NULL) { hashKeyFunc->dupFunc = NULL; hashKeyFunc->freeFunc = NULL; } else { hashKeyFunc->dupFunc = keyFunc->dupFunc; hashKeyFunc->freeFunc = keyFunc->freeFunc; } if (valueFunc == NULL) { hashValueFunc->dupFunc = NULL; hashValueFunc->freeFunc = NULL; } else { hashValueFunc->dupFunc = valueFunc->dupFunc; hashValueFunc->freeFunc = valueFunc->freeFunc; } } static inline BSL_HASH_Iterator BSL_HASH_IterEndGet(const BSL_HASH_Hash *hash) { return (BSL_HASH_Iterator)(uintptr_t)(&hash->listArray[hash->bucketSize].head); } static BSL_HASH_Node *BSL_HASH_NodeCreate( const BSL_HASH_Hash *hash, uintptr_t key, uint32_t keySize, uintptr_t value, uint32_t valueSize) { uintptr_t tmpKey; uintptr_t tmpValue; BSL_HASH_Node *hashNode = NULL; void *tmpPtr = NULL; hashNode = (BSL_HASH_Node *)BSL_SAL_Malloc(sizeof(BSL_HASH_Node)); if (hashNode == NULL) { return NULL; } if (hash->keyFunc.dupFunc != NULL) { tmpPtr = hash->keyFunc.dupFunc((void *)key, keySize); tmpKey = (uintptr_t)tmpPtr; if (tmpKey == (uintptr_t)NULL) { BSL_SAL_FREE(hashNode); return NULL; } } else { tmpKey = key; } if (hash->valueFunc.dupFunc != NULL) { tmpPtr = hash->valueFunc.dupFunc((void *)value, valueSize); tmpValue = (uintptr_t)tmpPtr; if (tmpValue == (uintptr_t)NULL) { if (hash->keyFunc.freeFunc != NULL) { hash->keyFunc.freeFunc((void *)tmpKey); } BSL_SAL_FREE(hashNode); return NULL; } } else { tmpValue = value; } hashNode->key = tmpKey; hashNode->value = tmpValue; return hashNode; } static BSL_HASH_Node *BSL_HASH_FindNode(const RawList *list, uintptr_t key, BSL_HASH_MatchFunc matchFunc) { BSL_HASH_Node *hashNode = NULL; ListRawNode *rawListNode = NULL; for (rawListNode = ListRawFront(list); rawListNode != NULL; rawListNode = ListRawGetNext(list, rawListNode)) { hashNode = BSL_CONTAINER_OF(rawListNode, BSL_HASH_Node, node); if (matchFunc(hashNode->key, key)) { return hashNode; } } return NULL; } static BSL_HASH_Iterator BSL_HASH_Front(const BSL_HASH_Hash *hash) { uint32_t i = 0; const RawList *list = NULL; ListRawNode *rawListNode = NULL; while (i < hash->bucketSize) { list = &hash->listArray[i]; rawListNode = ListRawFront(list); if (rawListNode != NULL) { return BSL_CONTAINER_OF(rawListNode, BSL_HASH_Node, node); } i++; } return BSL_HASH_IterEndGet(hash); } static BSL_HASH_Iterator BSL_HASH_Next(const BSL_HASH_Hash *hash, BSL_HASH_Iterator hashNode) { uint32_t i; uint32_t hashCode; const RawList *list = NULL; ListRawNode *rawListNode = NULL; hashCode = hash->hashFunc(hashNode->key, hash->bucketSize); if (hashCode >= hash->bucketSize) { return BSL_HASH_IterEndGet(hash); } list = hash->listArray + hashCode; rawListNode = ListRawGetNext(list, &hashNode->node); if (rawListNode != NULL) { return BSL_CONTAINER_OF(rawListNode, BSL_HASH_Node, node); } for (i = hashCode + 1; i < hash->bucketSize; ++i) { list = &hash->listArray[i]; rawListNode = ListRawFront(list); if (rawListNode != NULL) { return BSL_CONTAINER_OF(rawListNode, BSL_HASH_Node, node); } } return BSL_HASH_IterEndGet(hash); } static void BSL_HASH_NodeFree(BSL_HASH_Hash *hash, BSL_HASH_Node *node) { ListFreeFunc keyFreeFunc = hash->keyFunc.freeFunc; ListFreeFunc valueFreeFunc = hash->valueFunc.freeFunc; if (keyFreeFunc != NULL) { keyFreeFunc((void *)node->key); } if (valueFreeFunc != NULL) { valueFreeFunc((void *)node->value); } BSL_SAL_FREE(node); } uint32_t BSL_HASH_CodeCalcInt(uintptr_t key, uint32_t bktSize) { return BSL_HASH_CodeCalc(&key, sizeof(key)) % bktSize; } bool BSL_HASH_MatchInt(uintptr_t key1, uintptr_t key2) { return key1 == key2; } uint32_t BSL_HASH_CodeCalcStr(uintptr_t key, uint32_t bktSize) { char *tmpKey = (char *)key; return BSL_HASH_CodeCalc(tmpKey, (uint32_t)strlen(tmpKey)) % bktSize; } bool BSL_HASH_MatchStr(uintptr_t key1, uintptr_t key2) { char *tkey1 = (char *)key1; char *tkey2 = (char *)key2; return strcmp(tkey1, tkey2) == 0; } BSL_HASH_Hash *BSL_HASH_Create(uint32_t bktSize, BSL_HASH_CodeCalcFunc hashFunc, BSL_HASH_MatchFunc matchFunc, ListDupFreeFuncPair *keyFunc, ListDupFreeFuncPair *valueFunc) { uint32_t i; uint32_t size; BSL_HASH_Hash *hash = NULL; RawList *listAddr = NULL; if (bktSize == 0U) { return NULL; } size = (bktSize + 1) * sizeof(RawList); if (IsMultiOverflow((bktSize + 1), sizeof(RawList)) || IsAddOverflow(size, sizeof(BSL_HASH_Hash))) { return NULL; } size += sizeof(BSL_HASH_Hash); hash = (BSL_HASH_Hash *)BSL_SAL_Malloc(size); if (hash == NULL) { return NULL; } (void)memset_s(hash, size, 0, size); hash->bucketSize = bktSize; BSL_HASH_HookRegister(hash, hashFunc, matchFunc, keyFunc, valueFunc); listAddr = hash->listArray; for (i = 0; i <= bktSize; ++i) { ListRawInit(listAddr + i, NULL); } return hash; } static int32_t BSL_HASH_InsertNode( BSL_HASH_Hash *hash, RawList *rawList, const BSL_CstlUserData *inputKey, const BSL_CstlUserData *inputValue) { BSL_HASH_Node *hashNode = BSL_HASH_NodeCreate( hash, inputKey->inputData, inputKey->dataSize, inputValue->inputData, inputValue->dataSize); if (hashNode == NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } ListRawPushBack(rawList, &hashNode->node); hash->hashCount++; return BSL_SUCCESS; } static int32_t BSL_HASH_UpdateNode(const BSL_HASH_Hash *hash, BSL_HASH_Node *node, uintptr_t value, uint32_t valueSize) { uintptr_t tmpValue; void *tmpPtr = NULL; if (hash->valueFunc.dupFunc != NULL) { tmpPtr = hash->valueFunc.dupFunc((void *)value, valueSize); tmpValue = (uintptr_t)tmpPtr; if (tmpValue == (uintptr_t)NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (hash->valueFunc.freeFunc != NULL) { hash->valueFunc.freeFunc((void *)node->value); } } else { tmpValue = value; } node->value = tmpValue; return BSL_SUCCESS; } static inline int32_t BSL_HASH_CodeCheck(const BSL_HASH_Hash *hash, uintptr_t key, uint32_t *hashCode) { if (hash == NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } *hashCode = hash->hashFunc(key, hash->bucketSize); if (*hashCode >= hash->bucketSize) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } return BSL_SUCCESS; } int32_t BSL_HASH_Insert(BSL_HASH_Hash *hash, uintptr_t key, uint32_t keySize, uintptr_t value, uint32_t valueSize) { int32_t ret; uint32_t hashCode; BSL_HASH_Node *hashNode = NULL; RawList *rawList = NULL; BSL_CstlUserData inputKey; BSL_CstlUserData inputValue; ret = BSL_HASH_CodeCheck(hash, key, &hashCode); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } rawList = &hash->listArray[hashCode]; hashNode = BSL_HASH_FindNode(rawList, key, hash->matchFunc); if (hashNode != NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } inputKey.inputData = key; inputKey.dataSize = keySize; inputValue.inputData = value; inputValue.dataSize = valueSize; return BSL_HASH_InsertNode(hash, rawList, &inputKey, &inputValue); } int32_t BSL_HASH_Put(BSL_HASH_Hash *hash, uintptr_t key, uint32_t keySize, uintptr_t value, uint32_t valueSize, BSL_HASH_UpdateNodeFunc updateNodeFunc) { int32_t ret; uint32_t hashCode; RawList *rawList = NULL; BSL_HASH_Node *hashNode = NULL; BSL_CstlUserData inputValue; BSL_CstlUserData inputKey; ret = BSL_HASH_CodeCheck(hash, key, &hashCode); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } rawList = &hash->listArray[hashCode]; hashNode = BSL_HASH_FindNode(rawList, key, hash->matchFunc); if (hashNode != NULL) { if (updateNodeFunc != NULL) { return updateNodeFunc(hash, hashNode, value, valueSize); } else { return BSL_HASH_UpdateNode(hash, hashNode, value, valueSize); } } inputKey.inputData = key; inputKey.dataSize = keySize; inputValue.inputData = value; inputValue.dataSize = valueSize; return BSL_HASH_InsertNode(hash, rawList, &inputKey, &inputValue); } int32_t BSL_HASH_At(const BSL_HASH_Hash *hash, uintptr_t key, uintptr_t *value) { BSL_HASH_Node *hashNode = BSL_HASH_Find(hash, key); if (hashNode == BSL_HASH_IterEndGet(hash)) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } *value = hashNode->value; return BSL_SUCCESS; } BSL_HASH_Iterator BSL_HASH_Find(const BSL_HASH_Hash *hash, uintptr_t key) { uint32_t hashCode; BSL_HASH_Node *hashNode = NULL; int32_t ret = BSL_HASH_CodeCheck(hash, key, &hashCode); if (ret != BSL_SUCCESS) { return hash == NULL ? NULL : BSL_HASH_IterEndGet(hash); } hashNode = BSL_HASH_FindNode(&hash->listArray[hashCode], key, hash->matchFunc); if (hashNode == NULL) { return BSL_HASH_IterEndGet(hash); } return hashNode; } bool BSL_HASH_Empty(const BSL_HASH_Hash *hash) { return (hash == NULL) || (hash->hashCount == 0U); } uint32_t BSL_HASH_Size(const BSL_HASH_Hash *hash) { return hash == NULL ? 0 : hash->hashCount; } BSL_HASH_Iterator BSL_HASH_Erase(BSL_HASH_Hash *hash, uintptr_t key) { uint32_t hashCode; BSL_HASH_Node *hashNode = NULL; BSL_HASH_Node *nextHashNode = NULL; BSL_HASH_MatchFunc matchFunc = NULL; BSL_HASH_CodeCalcFunc hashFunc = NULL; if (hash == NULL) { return NULL; } hashFunc = hash->hashFunc; hashCode = hashFunc(key, hash->bucketSize); if (hashCode >= hash->bucketSize) { return BSL_HASH_IterEndGet(hash); } matchFunc = hash->matchFunc; hashNode = BSL_HASH_FindNode(&hash->listArray[hashCode], key, matchFunc); if (hashNode == NULL) { return BSL_HASH_IterEndGet(hash); } nextHashNode = BSL_HASH_Next(hash, hashNode); ListRawRemove(&hash->listArray[hashCode], &hashNode->node); BSL_HASH_NodeFree(hash, hashNode); --hash->hashCount; return nextHashNode; } void BSL_HASH_Clear(BSL_HASH_Hash *hash) { uint32_t i; RawList *list = NULL; BSL_HASH_Node *hashNode = NULL; ListRawNode *rawListNode = NULL; if (hash == NULL) { return; } for (i = 0; i < hash->bucketSize; ++i) { list = &hash->listArray[i]; while (!ListRawEmpty(list)) { rawListNode = ListRawFront(list); hashNode = BSL_CONTAINER_OF(rawListNode, BSL_HASH_Node, node); ListRawRemove(list, rawListNode); BSL_HASH_NodeFree(hash, hashNode); } } hash->hashCount = 0; } void BSL_HASH_Destory(BSL_HASH_Hash *hash) { if (hash != NULL) { BSL_HASH_Clear(hash); BSL_SAL_Free(hash); } } BSL_HASH_Iterator BSL_HASH_IterBegin(const BSL_HASH_Hash *hash) { return hash == NULL ? NULL : BSL_HASH_Front(hash); } BSL_HASH_Iterator BSL_HASH_IterEnd(const BSL_HASH_Hash *hash) { return hash == NULL ? NULL : BSL_HASH_IterEndGet(hash); } BSL_HASH_Iterator BSL_HASH_IterNext(const BSL_HASH_Hash *hash, BSL_HASH_Iterator it) { return (hash == NULL || it == BSL_HASH_IterEnd(hash)) ? BSL_HASH_IterEnd(hash) : BSL_HASH_Next(hash, it); } uintptr_t BSL_HASH_HashIterKey(const BSL_HASH_Hash *hash, BSL_HASH_Iterator it) { return (it == NULL || it == BSL_HASH_IterEnd(hash)) ? 0 : it->key; } uintptr_t BSL_HASH_IterValue(const BSL_HASH_Hash *hash, BSL_HASH_Iterator it) { return (it == NULL || it == BSL_HASH_IterEnd(hash)) ? 0 : it->value; } #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_BSL_HASH */
2302_82127028/openHiTLS-examples
bsl/hash/src/bsl_hash.c
C
unknown
17,124
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_HASH #include <stdlib.h> #include <stdint.h> #include "list_base.h" #include "bsl_errno.h" #include "bsl_sal.h" #include "bsl_hash_list.h" #ifdef __cplusplus extern "C" { #endif typedef struct BslListNodeSt ListNode; int32_t BSL_ListInit(BSL_List *list, const ListDupFreeFuncPair *dataFunc) { int32_t ret = (int32_t)BSL_INTERNAL_EXCEPTION; if (list != NULL) { ret = ListRawInit(&list->rawList, NULL); if (dataFunc == NULL) { list->dataFunc.dupFunc = NULL; list->dataFunc.freeFunc = NULL; } else { list->dataFunc.dupFunc = dataFunc->dupFunc; list->dataFunc.freeFunc = dataFunc->freeFunc; } } return ret; } static int32_t ListRemoveNode(BSL_List *list, ListNode *node) { if (list->dataFunc.freeFunc != NULL) { (list->dataFunc.freeFunc((void *)(node->userdata))); } int32_t ret = ListRawRemove(&list->rawList, &node->rawNode); if (ret == BSL_SUCCESS) { BSL_SAL_FREE(node); } return ret; } int32_t BSL_ListClear(BSL_List *list) { int32_t ret = (int32_t)BSL_INTERNAL_EXCEPTION; ListNode *node = NULL; const RawList *rawList = NULL; const ListRawNode *head = NULL; if (list != NULL) { rawList = &list->rawList; head = &rawList->head; while (!ListRawEmpty(rawList)) { node = BSL_CONTAINER_OF(head->next, ListNode, rawNode); ret = ListRemoveNode(list, node); if (ret != BSL_SUCCESS) { break; } } } return ret; } int32_t BSL_ListDeinit(BSL_List *list) { int32_t ret = (int32_t)BSL_INTERNAL_EXCEPTION; if (list != NULL) { ret = BSL_ListClear(list); list->dataFunc.dupFunc = NULL; list->dataFunc.freeFunc = NULL; } return ret; } static int32_t ListWriteUserdata(const BSL_List *list, ListNode *node, uintptr_t userData, size_t userDataSize) { const void *copyBuff = NULL; if (list->dataFunc.dupFunc == NULL) { node->userdata = userData; return BSL_SUCCESS; } copyBuff = list->dataFunc.dupFunc((void *)userData, userDataSize); if (copyBuff == NULL) { return BSL_INTERNAL_EXCEPTION; } node->userdata = (uintptr_t)copyBuff; return BSL_SUCCESS; } static ListNode *NewNodeCreateByUserData(const BSL_List *list, uintptr_t userData, size_t userDataSize) { if (list == NULL) { return NULL; } ListNode *node = (ListNode *)BSL_SAL_Malloc(sizeof(ListNode)); if (node != NULL) { if (ListWriteUserdata(list, node, userData, userDataSize) != BSL_SUCCESS) { BSL_SAL_Free(node); node = NULL; } } return node; } static int32_t ListPush(BSL_List *list, uintptr_t userData, size_t userDataSize, bool isFront) { ListNode *node = NewNodeCreateByUserData(list, userData, userDataSize); if (node == NULL) { return BSL_INTERNAL_EXCEPTION; } int32_t ret = isFront ? ListRawPushFront(&list->rawList, &node->rawNode) : ListRawPushBack(&list->rawList, &node->rawNode); if (ret != BSL_SUCCESS) { if (list->dataFunc.freeFunc != NULL) { list->dataFunc.freeFunc((void *)(node->userdata)); } BSL_SAL_Free(node); } return ret; } int32_t BSL_ListPushFront(BSL_List *list, uintptr_t userData, size_t userDataSize) { return ListPush(list, userData, userDataSize, true); } int32_t BSL_ListPushBack(BSL_List *list, uintptr_t userData, size_t userDataSize) { return ListPush(list, userData, userDataSize, false); } int32_t BSL_ListInsert(BSL_List *list, const BSL_ListIterator it, uintptr_t userData, size_t userDataSize) { if (it == NULL) { return BSL_INTERNAL_EXCEPTION; } ListNode *node = NewNodeCreateByUserData(list, userData, userDataSize); if (node == NULL) { return BSL_INTERNAL_EXCEPTION; } int32_t ret = ListRawInsert(&it->rawNode, &node->rawNode); if (ret != BSL_SUCCESS) { if (list->dataFunc.freeFunc != NULL) { list->dataFunc.freeFunc((void *)(node->userdata)); } BSL_SAL_Free(node); } return ret; } bool BSL_ListIsEmpty(const BSL_List *list) { return list == NULL ? true : ListRawEmpty(&list->rawList); } int32_t BSL_ListPopFront(BSL_List *list) { if (BSL_ListIsEmpty(list) == true) { return BSL_INTERNAL_EXCEPTION; } ListNode *firstNode = BSL_CONTAINER_OF(list->rawList.head.next, ListNode, rawNode); return ListRemoveNode(list, firstNode); } int32_t BSL_ListPopBack(BSL_List *list) { if (BSL_ListIsEmpty(list) == true) { return BSL_INTERNAL_EXCEPTION; } ListNode *lastNode = BSL_CONTAINER_OF(list->rawList.head.prev, ListNode, rawNode); return ListRemoveNode(list, lastNode); } BSL_ListIterator BSL_ListIterErase(BSL_List *list, BSL_ListIterator it) { if (BSL_ListIsEmpty(list) || (it == NULL) || (it == (BSL_ListIterator)(&list->rawList.head))) { return NULL; } BSL_ListIterator retIt = BSL_CONTAINER_OF(it->rawNode.next, ListNode, rawNode); return ListRemoveNode(list, it) == BSL_SUCCESS ? retIt : NULL; } uintptr_t BSL_ListFront(const BSL_List *list) { if (BSL_ListIsEmpty(list) == true) { return 0; } const ListNode *node = BSL_CONTAINER_OF(list->rawList.head.next, ListNode, rawNode); return node->userdata; } uintptr_t BSL_ListBack(const BSL_List *list) { if (BSL_ListIsEmpty(list) == true) { return 0; } const ListNode *node = BSL_CONTAINER_OF(list->rawList.head.prev, ListNode, rawNode); return node->userdata; } BSL_ListIterator BSL_ListIterBegin(const BSL_List *list) { return list == NULL ? NULL : BSL_CONTAINER_OF(list->rawList.head.next, ListNode, rawNode); } BSL_ListIterator BSL_ListIterEnd(BSL_List *list) { return list == NULL ? NULL : (BSL_ListIterator)(&list->rawList.head); } size_t BSL_ListSize(const BSL_List *list) { return list == NULL ? 0 : ListRawSize(&list->rawList); } BSL_ListIterator BSL_ListIterPrev(const BSL_List *list, const BSL_ListIterator it) { return (BSL_ListIsEmpty(list) == true || it == NULL) ? NULL : BSL_CONTAINER_OF(it->rawNode.prev, ListNode, rawNode); } BSL_ListIterator BSL_ListIterNext(const BSL_List *list, const BSL_ListIterator it) { return (BSL_ListIsEmpty(list) == true || it == NULL) ? NULL : BSL_CONTAINER_OF(it->rawNode.next, ListNode, rawNode); } uintptr_t BSL_ListIterData(const BSL_ListIterator it) { return it == NULL ? 0 : it->userdata; } /* Linked list node search function. The type of the first parameter of iterCmpFunc is userdata of each iterator. */ BSL_ListIterator BSL_ListIterFind(BSL_List *list, ListKeyCmpFunc iterCmpFunc, uintptr_t data) { if (list == NULL || iterCmpFunc == NULL) { return NULL; } BSL_ListIterator headIt = (BSL_ListIterator)BSL_CONTAINER_OF(&list->rawList.head, ListNode, rawNode); BSL_ListIterator it = BSL_CONTAINER_OF(list->rawList.head.next, ListNode, rawNode); while (it != headIt) { if (iterCmpFunc(it->userdata, data) == 0) { return it; } it = BSL_CONTAINER_OF(it->rawNode.next, ListNode, rawNode); } return NULL; } #ifdef __cplusplus } #endif #endif /* HITLS_BSL_HASH */
2302_82127028/openHiTLS-examples
bsl/hash/src/bsl_hash_list.c
C
unknown
7,912
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_HASH #include "hash_local.h" #ifdef __cplusplus extern "C" { #endif bool IsMultiOverflow(uint32_t x, uint32_t y) { bool ret = false; if ((x > 0) && (y > 0)) { ret = ((SIZE_MAX / x) < y) ? true : false; } return ret; } bool IsAddOverflow(uint32_t x, uint32_t y) { return ((x + y) < x); } #ifdef __cplusplus } #endif #endif /* HITLS_BSL_HASH */
2302_82127028/openHiTLS-examples
bsl/hash/src/hash_local.c
C
unknown
965
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef HASH_LOCAL_H #define HASH_LOCAL_H #include "hitls_build.h" #ifdef HITLS_BSL_HASH #include <stdint.h> #include <stdbool.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ typedef struct { uintptr_t inputData; /* Actual data input by the user. */ uint32_t dataSize; /* Actual input size */ } BSL_CstlUserData; /* Check whether overflow occurs when two numbers are multiplied in the current system. */ bool IsMultiOverflow(uint32_t x, uint32_t y); /* Check whether the sum of the two numbers overflows in the current system. */ bool IsAddOverflow(uint32_t x, uint32_t y); #ifdef __cplusplus } #endif #endif /* HITLS_BSL_HASH */ #endif /* HASH_LOCAL_H */
2302_82127028/openHiTLS-examples
bsl/hash/src/hash_local.h
C
unknown
1,258
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_HASH #include <stdlib.h> #include "bsl_errno.h" #include "list_base.h" #ifdef __cplusplus extern "C" { #endif /* internal function definition */ static inline bool ListRawNodeInList(const ListRawNode *node) { return (node->next != NULL) && (node->prev != NULL) && ((const ListRawNode *)(node->next->prev) == node) && ((const ListRawNode *)(node->prev->next) == node); } static inline bool IsListRawEmptyCheck(const RawList *list) { return (&list->head)->next == &list->head; } static inline void ListRawAddAfterNode(ListRawNode *node, ListRawNode *where) { node->next = (where)->next; node->prev = (where); where->next = node; node->next->prev = node; } static inline void ListRawAddBeforeNode(ListRawNode *node, const ListRawNode *where) { ListRawAddAfterNode(node, where->prev); } static inline bool IsListRawFirstNode(const RawList *list, const ListRawNode *node) { return (const ListRawNode *)list->head.next == node; } static inline bool IsListRawLastNode(const RawList *list, const ListRawNode *node) { return (const ListRawNode *)list->head.prev == node; } /* Deleting the list node, internal function, input parameter validation is not required. */ static void ListRawRemoveNode(const RawList *list, ListRawNode *node) { node->prev->next = node->next; node->next->prev = node->prev; if (list->freeFunc != NULL) { list->freeFunc((void *)node); } } int32_t ListRawInit(RawList *list, ListFreeFunc freeFunc) { if (list == NULL) { return BSL_INTERNAL_EXCEPTION; } list->head.next = &list->head; list->head.prev = &list->head; list->freeFunc = freeFunc; return BSL_SUCCESS; } int32_t ListRawClear(RawList *list) { if (list == NULL) { return BSL_INTERNAL_EXCEPTION; } while (!IsListRawEmptyCheck(list)) { ListRawRemoveNode(list, (ListRawNode *)list->head.next); } return BSL_SUCCESS; } int32_t ListRawDeinit(RawList *list) { int32_t ret = (int32_t)BSL_INTERNAL_EXCEPTION; if (list != NULL) { ret = ListRawClear(list); list->freeFunc = NULL; } return ret; } bool ListRawEmpty(const RawList *list) { return list == NULL ? true : IsListRawEmptyCheck(list); } static inline size_t ListRawSizeInner(const RawList *list) { size_t size = 0; const ListRawNode *head = &list->head; for (const ListRawNode *node = head->next; node != head; node = node->next) { size++; } return size; } size_t ListRawSize(const RawList *list) { return (list == NULL || IsListRawEmptyCheck(list) == true) ? 0 : ListRawSizeInner(list); } int32_t ListRawPushFront(RawList *list, ListRawNode *node) { if (list == NULL || node == NULL) { return BSL_INTERNAL_EXCEPTION; } ListRawAddAfterNode(node, &(list->head)); return BSL_SUCCESS; } int32_t ListRawPushBack(RawList *list, ListRawNode *node) { if (list == NULL || node == NULL) { return BSL_INTERNAL_EXCEPTION; } ListRawAddBeforeNode(node, &(list->head)); return BSL_SUCCESS; } int32_t ListRawInsert(const ListRawNode *curNode, ListRawNode *newNode) { if ((curNode != NULL) && (newNode != NULL) && (ListRawNodeInList(curNode))) { ListRawAddBeforeNode(newNode, curNode); return BSL_SUCCESS; } return BSL_INTERNAL_EXCEPTION; } int32_t ListRawPopFront(RawList *list) { if (list == NULL || IsListRawEmptyCheck(list) == true) { return BSL_INTERNAL_EXCEPTION; } ListRawNode *firstNode = list->head.next; ListRawRemoveNode(list, firstNode); return BSL_SUCCESS; } int32_t ListRawPopBack(RawList *list) { if (list == NULL || IsListRawEmptyCheck(list) == true) { return BSL_INTERNAL_EXCEPTION; } ListRawNode *lastNode = list->head.prev; ListRawRemoveNode(list, lastNode); return BSL_SUCCESS; } static void ListRawRemoveInner(RawList *list, ListRawNode *node) { node->prev->next = node->next; node->next->prev = node->prev; if ((list != NULL) && !IsListRawEmptyCheck(list) && (list->freeFunc != NULL)) { list->freeFunc((void *)node); } } int32_t ListRawRemove(RawList *list, ListRawNode *node) { if (node == NULL || ListRawNodeInList(node) == false) { return BSL_INTERNAL_EXCEPTION; } ListRawRemoveInner(list, node); return BSL_SUCCESS; } ListRawNode *ListRawFront(const RawList *list) { return (list == NULL || IsListRawEmptyCheck(list) == true) ? NULL : list->head.next; } ListRawNode *ListRawBack(const RawList *list) { return (list == NULL || IsListRawEmptyCheck(list) == true) ? NULL : list->head.prev; } ListRawNode *ListRawGetPrev(const RawList *list, const ListRawNode *node) { return ((list == NULL) || (node == NULL) || (IsListRawEmptyCheck(list)) || (IsListRawFirstNode(list, node)) || (!ListRawNodeInList(node))) ? NULL : node->prev; } ListRawNode *ListRawGetNext(const RawList *list, const ListRawNode *node) { return ((list == NULL) || (node == NULL) || (IsListRawEmptyCheck(list)) || (IsListRawLastNode(list, node)) || (!ListRawNodeInList(node))) ? NULL : node->next; } /* Linked list node search function. The type of the first parameter of nodeMatchFunc must be (ListRawNode *) */ ListRawNode *ListRawFindNode(const RawList *list, ListMatchFunc nodeMatchFunc, uintptr_t data) { if (list == NULL || nodeMatchFunc == NULL) { return NULL; } const ListRawNode *head = (const ListRawNode *)(&list->head); ListRawNode *node = head->next; while ((const ListRawNode *)node != head) { if (nodeMatchFunc((void *)node, data)) { return node; } node = node->next; } return NULL; } #ifdef __cplusplus } #endif #endif /* HITLS_BSL_HASH */
2302_82127028/openHiTLS-examples
bsl/hash/src/list_base.c
C
unknown
6,424
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_BINLOG_ID_H #define BSL_BINLOG_ID_H #ifdef __cplusplus extern "C" { #endif enum BSL_BINLOG_ID { BINLOG_ID05001 = 5001, BINLOG_ID05002, BINLOG_ID05003, BINLOG_ID05004, BINLOG_ID05005, BINLOG_ID05006, BINLOG_ID05007, BINLOG_ID05008, BINLOG_ID05009, BINLOG_ID05010, BINLOG_ID05011, BINLOG_ID05012, BINLOG_ID05013, BINLOG_ID05014, BINLOG_ID05015, BINLOG_ID05016, BINLOG_ID05017, BINLOG_ID05018, BINLOG_ID05019, BINLOG_ID05020, BINLOG_ID05021, BINLOG_ID05022, BINLOG_ID05023, BINLOG_ID05024, BINLOG_ID05025, BINLOG_ID05026, BINLOG_ID05027, BINLOG_ID05028, BINLOG_ID05029, BINLOG_ID05030, BINLOG_ID05031, BINLOG_ID05032, BINLOG_ID05033, BINLOG_ID05034, BINLOG_ID05035, BINLOG_ID05036, BINLOG_ID05037, BINLOG_ID05038, BINLOG_ID05039, BINLOG_ID05040, BINLOG_ID05041, BINLOG_ID05042, BINLOG_ID05043, BINLOG_ID05044, BINLOG_ID05045, BINLOG_ID05046, BINLOG_ID05047, BINLOG_ID05048, BINLOG_ID05049, BINLOG_ID05050, BINLOG_ID05051, BINLOG_ID05052, BINLOG_ID05053, BINLOG_ID05054, BINLOG_ID05055, BINLOG_ID05056, BINLOG_ID05057, BINLOG_ID05058, BINLOG_ID05059, BINLOG_ID05060, BINLOG_ID05061, BINLOG_ID05062, BINLOG_ID05063, BINLOG_ID05064, BINLOG_ID05065, BINLOG_ID05066, BINLOG_ID05067, BINLOG_ID05068, BINLOG_ID05069, BINLOG_ID05070, BINLOG_ID05071, BINLOG_ID05072, BINLOG_ID05073, BINLOG_ID05074, BINLOG_ID05075, BINLOG_ID05076, BINLOG_ID05077, BINLOG_ID05078, BINLOG_ID05079, BINLOG_ID05080, BINLOG_ID05081, BINLOG_ID05082, BINLOG_ID05083, BINLOG_ID05084, BINLOG_ID05085, BINLOG_ID05086 }; #ifdef __cplusplus } #endif #endif
2302_82127028/openHiTLS-examples
bsl/include/bsl_binlog_id.h
C
unknown
2,148
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_BYTES_H #define BSL_BYTES_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /** * @brief convert uint8_t byte stream to uint16_t data * * @attention data cannot be empty * * @param data [IN] uint8_t byte stream * * @return uint16_t converted data */ static inline uint16_t BSL_ByteToUint16(const uint8_t *data) { /** Byte 0 is shifted by 8 bits to the left, and byte 1 remains unchanged. uint16_t is obtained after OR */ return ((uint16_t)data[0] << 8) | ((uint16_t)data[1]); } /** * @brief convert uint16_t data to uint8_t byte stream * * @attention data cannot be empty * * @param num [IN] data to be converted * @param data [OUT] converted data */ static inline void BSL_Uint16ToByte(uint16_t num, uint8_t *data) { /** convert to byte stream */ data[0] = (uint8_t)(num >> 8); // data is shifted rightwards by 8 bits and put in byte 0 data[1] = (uint8_t)(num & 0xffu); // data AND 0xffu, put in byte 1 return; } /** * @brief convert uint8_t byte stream to uint24_t data * * @attention data cannot be empty * * @param data [IN] uint8_t byte stream * * @return uint24_t, converted data */ static inline uint32_t BSL_ByteToUint24(const uint8_t *data) { /** Byte 0 is shifted left by 16 bits, byte 1 is shifted left by 8 bits, and byte 2 remains unchanged, uint24_t is obtained after the OR operation. */ return ((uint32_t)data[0] << 16) | ((uint32_t)data[1] << 8) | ((uint32_t)data[2]); } /** * @brief convert uint24_t data to uint8_t byte stream * * @attention data cannot be empty * * @param num [IN] data to be converted * @param data [OUT] converted data */ static inline void BSL_Uint24ToByte(uint32_t num, uint8_t *data) { /** convert to byte stream */ data[0] = (uint8_t)(num >> 16); // data is shifted rightwards by 16 bits and put in byte 0 data[1] = (uint8_t)(num >> 8); // data is shifted rightwards by 8 bits and placed in byte 1 data[2] = (uint8_t)(num & 0xffu); // data AND 0xffu, put in byte 2 return; } /** * @brief convert uint8_t byte stream to uint32_t data * * @attention data cannot be empty * * @param data [IN] uint8_t byte stream * * @return uint32_t, converted data */ static inline uint32_t BSL_ByteToUint32(const uint8_t *data) { /** Byte 0 is shifted leftward by 24 bits, byte 1 is shifted leftward by 16 bits, byte 2 is shifted leftward by 8 bits, and byte 3 remains unchanged, uint32_t is obtained after OR operation. */ return ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | ((uint32_t)data[2] << 8) | ((uint32_t)data[3]); } /** * @brief convert uint8_t byte stream to uint48_t data * * @attention data cannot be empty * * @param data [IN] uint8_t byte stream * * @return uint48_t, converted data */ static inline uint64_t BSL_ByteToUint48(const uint8_t *data) { /** Byte 0 is shifted leftward by 40 bits, byte 1 is shifted leftward by 32 bits, byte 2 is shifted leftward by 24 bits, byte 3 is shifted leftward by 16 bits, byte 4 is shifted leftward by 8 bits, and byte 5 remains unchanged, uint48_t is obtained after OR operation. */ return ((uint64_t)data[0] << 40) | ((uint64_t)data[1] << 32) | ((uint64_t)data[2] << 24) | ((uint64_t)data[3] << 16) | ((uint64_t)data[4] << 8) | ((uint64_t)data[5]); } /** * @brief convert uint48_t data to uint8_t byte stream * * @attention data cannot be empty * * @param num [IN] data to be converted * @param data [OUT] converted data */ static inline void BSL_Uint48ToByte(uint64_t num, uint8_t *data) { /** convert to byte stream */ data[0] = (uint8_t)(num >> 40); // data is shifted rightwards by 40 bits and put in byte 0 data[1] = (uint8_t)(num >> 32); // data is shifted rightwards by 32 bits and put in byte 1 data[2] = (uint8_t)(num >> 24); // data is shifted rightwards by 24 bits and put in byte 2 data[3] = (uint8_t)(num >> 16); // data is shifted rightwards by 16 bits and put in byte 3 data[4] = (uint8_t)(num >> 8); // data is shifted rightwards by 8 bits and put in byte 4 data[5] = (uint8_t)(num & 0xffu); // data AND 0xffu, put in byte 5 return; } /** * @brief convert uint8_t byte stream to uint64_t data * * @attention data cannot be empty * * @param data [IN] uint8_t byte stream * * @return uint32_t, converted data */ static inline uint64_t BSL_ByteToUint64(const uint8_t *data) { /** Byte 0 is shifted leftward by 56 bits, byte 1 is shifted leftward by 48 bits, byte 2 is shifted leftward by 40 bits, byte 3 is shifted leftward by 32 bits, byte 4 is shifted leftward by 24 bits, byte 5 is shifted leftward by 16 bits, byte 6 is shifted leftward by 8 bits, and byte 7 remains unchanged, uint64_t is obtained after OR operation. */ return ((uint64_t)data[0] << 56) | ((uint64_t)data[1] << 48) | ((uint64_t)data[2] << 40) | ((uint64_t)data[3] << 32) | ((uint64_t)data[4] << 24) | ((uint64_t)data[5] << 16) | ((uint64_t)data[6] << 8) | ((uint64_t)data[7]); } /** * @brief convert uint32_t data to uint8_t byte stream * * @attention data cannot be empty * * @param num [IN] data to be converted * @param data [OUT] converted data */ static inline void BSL_Uint32ToByte(uint32_t num, uint8_t *data) { /** convert to byte stream */ data[0] = (uint8_t)(num >> 24); // data is shifted rightwards by 24 bits and put in byte 0 data[1] = (uint8_t)(num >> 16); // data is shifted rightwards by 16 bits and put in byte 1 data[2] = (uint8_t)(num >> 8); // data is shifted rightwards by 8 bits and put in byte 2 data[3] = (uint8_t)(num & 0xffu); // data AND 0xffu, put in byte 3 return; } /** * @brief convert uint64_t data to uint8_t byte stream * * @attention data cannot be empty * * @param num [IN] data to be converted * @param data [OUT] converted data */ static inline void BSL_Uint64ToByte(uint64_t num, uint8_t *data) { /** convert to byte stream */ data[0] = (uint8_t)(num >> 56); // data is shifted rightwards by 56 bits and put in byte 0 data[1] = (uint8_t)(num >> 48); // data is shifted rightwards by 48 bits and put in byte 1 data[2] = (uint8_t)(num >> 40); // data is shifted rightwards by 40 bits and put in byte 2 data[3] = (uint8_t)(num >> 32); // data is shifted rightwards by 32 bits and put in byte 3 data[4] = (uint8_t)(num >> 24); // data is shifted rightwards by 24 bits and put in byte 4 data[5] = (uint8_t)(num >> 16); // data is shifted rightwards by 16 bits and put in byte 5 data[6] = (uint8_t)(num >> 8); // data is shifted rightwards by 8 bits and put in byte 6 data[7] = (uint8_t)(num & 0xffu); // data AND 0xffu, put in byte 7 return; } // if a's MSB is 0, output 0 // else if a' MSB is 1 output 0xffffffff static inline uint32_t Uint32ConstTimeMsb(uint32_t a) { // 31 == (4 * 8 - 1) return 0u - (a >> 31); } // if a is 0, output 0xffffffff, else output 0 static inline uint32_t Uint32ConstTimeIsZero(uint32_t a) { return Uint32ConstTimeMsb(~a & (a - 1)); } // if a == b, output 0xffffffff, else output 0 static inline uint32_t Uint32ConstTimeEqual(uint32_t a, uint32_t b) { return Uint32ConstTimeIsZero(a ^ b); } // if mask == 0xffffffff, return a, // else if mask == 0, return b static inline uint32_t Uint32ConstTimeSelect(uint32_t mask, uint32_t a, uint32_t b) { return ((mask) & a) | ((~mask) & b); } static inline uint32_t Uint8ConstTimeSelect(uint32_t mask, uint8_t a, uint8_t b) { return (((mask) & a) | ((~mask) & b)) & 0xff; } // if a < b, output 0xffffffff, else output 0 static inline uint32_t Uint32ConstTimeLt(uint32_t a, uint32_t b) { return Uint32ConstTimeMsb(a ^ ((a ^ b) | ((a - b) ^ a))); } // if a >= b, output 0xffffffff, else output 0 static inline uint32_t Uint32ConstTimeGe(uint32_t a, uint32_t b) { return ~Uint32ConstTimeLt(a, b); } // if a > b, output 0xffffffff, else output 0 static inline uint32_t Uint32ConstTimeGt(uint32_t a, uint32_t b) { return Uint32ConstTimeLt(b, a); } static inline uint32_t Uint32ConstTimeLe(uint32_t a, uint32_t b) { return Uint32ConstTimeGe(b, a); } // if a == b, return 0xffffffff, else return 0 static inline uint32_t ConstTimeMemcmp(uint8_t *a, uint8_t *b, uint32_t l) { uint8_t r = 0; for (uint32_t i = 0; i < l; i++) { r |= a[i] ^ b[i]; } return Uint32ConstTimeIsZero(r); } #ifdef __cplusplus } #endif /* __cplusplus */ #endif // BSL_BYTES_H
2302_82127028/openHiTLS-examples
bsl/include/bsl_bytes.h
C
unknown
9,102
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_MODULE_LIST_H #define BSL_MODULE_LIST_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /* * This structure is used to store the forward and backward pointers of nodes in the bidirectional linked list. * This linked list does not contain substantial data areas and is generally used to organize (concatenate) data nodes. */ typedef struct ListHeadSt { struct ListHeadSt *next, *prev; } ListHead; /** * @brief initialize the linked list when the linked list is reused * * @param head [IN] The address of the head node of the list */ #define LIST_INIT(head) (head)->next = (head)->prev = (head) /** * @brief Insert the 'item' node after the 'where' node. Before the change: where->A->B. After the change: where->item->A->B * * @param where [IN] The address where the item will be inserted after * @param item [IN] Address of the node(item) to be inserted */ #define LIST_ADD_AFTER(where, item) do { \ (item)->next = (where)->next; \ (item)->prev = (where); \ (where)->next = (item); \ (item)->next->prev = (item); \ } while (0) /** * @brief Insert the 'item' node before the 'where' node. * Before change: A->where->B. After change: A->item->where->B * * @param where [IN] The address where the item will be inserted before * @param item [IN] Address of the node to be inserted */ #define LIST_ADD_BEFORE(where, item) LIST_ADD_AFTER((where)->prev, (item)) /** * @brief Delete the node item. * * @param item [IN] The address of the item to be removed */ #define LIST_REMOVE(item) do { \ (item)->prev->next = (item)->next; \ (item)->next->prev = (item)->prev; \ } while (0) /** * @brief Check whether a list is empty * * @param head [IN] The address of the list to be checked. */ #define LIST_IS_EMPTY(head) ((head)->next == (head)) /** * @brief Travel through a list safety * * @param head [IN] Linked list to be traversed (The head of a list) * @param temp [IN] Point to the current node to safely delete the current node * @param item [IN] A temporary list node item for travelling the list */ #define LIST_FOR_EACH_ITEM_SAFE(item, temp, head) \ for ((item) = (head)->next, (temp) = (item)->next; (item) != (head); (item) = (temp), (temp) = (item)->next) /** * @brief Find the start address of the struct(large node) where the node is located * through a node (small node) in the linked list. * * @param item [IN] The address of a list item * @param type [IN] Type of the large node that contains the linked list node. * @param member [IN] Name of the list node in the structure * * Note: * Each struct variable forms a large node (including data and list nodes). * The large node is connected through the list(small node). * --------- --------- --------- -- ---- * | pre |<---| pre |<---| pre | |==>small node | * | next |--->| next |--->| next | | | * --------- --------- --------- -- | ===> Large node * | data1 | | data1 | | data1 | | * | data2 | | data2 | | data2 | | * --------- --------- --------- ---- * The reason why the list is not directly used as the big node is that * the list (ListHead type) has only the head and tail pointers and does not contain the data area. * In this way, the list can be used for mounting any data and is universal. */ #define LIST_ENTRY(item, type, member) \ ((type *)((uintptr_t)(char *)(item) - (uintptr_t)(&((type *)0)->member))) #ifdef __cplusplus } #endif #endif // BSL_MODULE_LIST_H
2302_82127028/openHiTLS-examples
bsl/include/bsl_module_list.h
C
unknown
4,268
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_UTIL_INTERNAL_H #define BSL_UTIL_INTERNAL_H #ifdef __cplusplus extern "C" { #endif // check if gcc/clang defined 128 bits int #ifdef __SIZEOF_INT128__ typedef __int128_t int128_t; typedef __uint128_t uint128_t; #endif #ifdef __cplusplus } #endif /* end __cplusplus */ #endif // BSL_UTIL_INTERNAL_H
2302_82127028/openHiTLS-examples
bsl/include/bsl_util_internal.h
C
unknown
860
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_INIT #include "bsl_err.h" #include "bsl_errno.h" #ifdef HITLS_BSL_OBJ #include "bsl_obj_internal.h" #endif // HITLS_BSL_OBJ int32_t BSL_GLOBAL_Init(void) { return BSL_ERR_Init(); } int32_t BSL_GLOBAL_DeInit(void) { BSL_ERR_RemoveErrStringBatch(); BSL_ERR_RemoveErrorStack(true); BSL_ERR_DeInit(); #if defined HITLS_BSL_OBJ && defined HITLS_BSL_HASH BSL_OBJ_FreeSignHashTable(); BSL_OBJ_FreeHashTable(); #endif // HITLS_BSL_OBJ && HITLS_BSL_HASH return BSL_SUCCESS; } #endif /* HITLS_BSL_INIT */
2302_82127028/openHiTLS-examples
bsl/init/bsl_init.c
C
unknown
1,113
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_LIST_INTERNAL_H #define BSL_LIST_INTERNAL_H #include "hitls_build.h" #ifdef HITLS_BSL_LIST #include "bsl_list.h" #ifdef __cplusplus extern "C" { #endif typedef int32_t (*QSORT_COMP_FN_TYPE)(const void *, const void *); /* Sort the list in ascending order of content */ int32_t BSL_ListSortInternal(BslList *pList, int32_t((*cmp)(const void *, const void *))); /** * @ingroup bsl_list * @brief To return the data of the node at the given index in the list, starting at 0. * * @param[IN] ulIndex The index in the list. * @param[IN] pstListNode The list node. * @param[IN] pstList The list. * * @retval void* The element which was found. [void *] * @retval void* If none found. [NULL] */ void *BSL_LIST_GetIndexNodeEx(uint32_t ulIndex, const BslListNode *pstListNode, const BslList *pstList); /** * @ingroup bsl_list * @brief This function searches a list based on the comparator function supplied (3rd param). * The second param is given to the comparator as its second param and each data item on the * list is given as its first param while searching. The comparator must return 0 to indicate a match. * * The Search callback function should return -2(SEC_INT_ERROR) if search should not be continued anymore. * * @param[IN] pList The list. * @param[IN] pSearchFor The element to be searched. * @param[IN] pSearcher The pointer to the comparison function of data. * @param[OUT] pstErr Error codes for internal error. [-2/0] * * @retval Void* The element which was found [Void*] * @retval Void* If none found [NULL] */ void *BSL_LIST_SearchEx(BslList *pList, const void *pSearchFor, BSL_LIST_PFUNC_CMP pSearcher); /** * @ingroup bsl_list * @brief This creates a new node before/after/At end /begin of the current node. If the list was already empty, * the node will be added as the only node.The current pointer is changed to point to the newly added node in the list. * If the current pointer is NULL then this operation fails. * * @param[IN] pList The list. * @param[IN] pData The element to be added. * @param[IN] enPosition Whether the element is to be added before/after the list * * @retval uint32_t, The error code * BSL_LIST_INVALID_LIST_CURRENT: If current pointer is NULL * BSL_LIST_DATA_NOT_AVAILABLE: If data pointer is NULL * BSL_MALLOC_FAIL: If failure to allocate memory for new node * BSL_SUCCESS: If successful */ uint32_t BSL_LIST_AddElementInt(BslList *pList, void *pData, BslListPosition enPosition); #define CURR_LIST_NODE(al) ((al)->curr) #define SET_CURR_LIST_NODE(al, listNode) ((al)->curr = (listNode)) #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_BSL_LIST */ #endif // BSL_LIST_INTERNAL_H
2302_82127028/openHiTLS-examples
bsl/list/include/bsl_list_internal.h
C
unknown
3,273
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_LIST #include "bsl_list_internal.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "securec.h" #include "bsl_list.h" #define MAX_LIST_ELEM_CNT_DEFAULT 10000000 /* this global var limits the maximum node number of a list */ static int32_t g_maxListCount = MAX_LIST_ELEM_CNT_DEFAULT; static uint32_t BslListAddAfterCurr(BslList *pList, void *pData) { BslListNode *newNode = NULL; /* check for missing argument */ if (pList == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } /* check if current is null */ if (pList->curr == NULL) { BSL_ERR_PUSH_ERROR(BSL_LIST_INVALID_LIST_CURRENT); return BSL_LIST_INVALID_LIST_CURRENT; } /* allocate memory for new node */ newNode = BSL_SAL_Calloc(1, sizeof(BslListNode)); if (newNode == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } newNode->data = pData; /* add new node after current */ newNode->next = pList->curr->next; newNode->prev = pList->curr; if ((pList->curr->next) != NULL) { pList->curr->next->prev = newNode; } else { pList->last = newNode; } pList->curr->next = newNode; pList->curr = newNode; pList->count++; return BSL_SUCCESS; } static uint32_t BslListInsertBeforeCurr(BslList *pList, void *pData) { BslListNode *pNewNode = NULL; /* check if current is null */ /* check for missing argument */ if (pList == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (pList->curr == NULL) { BSL_ERR_PUSH_ERROR(BSL_LIST_INVALID_LIST_CURRENT); return BSL_LIST_INVALID_LIST_CURRENT; } /* allocate memory for new node */ pNewNode = BSL_SAL_Calloc(1, sizeof(BslListNode)); if (pNewNode == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } pNewNode->data = pData; /* add new node before current */ pNewNode->next = pList->curr; pNewNode->prev = pList->curr->prev; if ((pList->curr->prev) != NULL) { pList->curr->prev->next = pNewNode; } else { pList->first = pNewNode; } pList->curr->prev = pNewNode; pList->curr = pNewNode; pList->count++; return BSL_SUCCESS; } uint32_t BSL_LIST_AddElementInt(BslList *pList, void *pData, BslListPosition enPosition) { BslListNode *pNewNode = NULL; if (pList->count >= g_maxListCount) { BSL_ERR_PUSH_ERROR(BSL_LIST_FULL); return BSL_LIST_FULL; } if (pList->curr == NULL) { if (pList->first == NULL) { /* allocate memory for new node */ pNewNode = BSL_SAL_Calloc(1, sizeof(BslListNode)); if (pNewNode == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } pNewNode->data = pData; /* set the new node as the first and last node of list */ pNewNode->next = NULL; pNewNode->prev = NULL; pList->first = pNewNode; pList->last = pNewNode; pList->curr = pNewNode; pList->count++; return BSL_SUCCESS; } else { if (enPosition == BSL_LIST_POS_AFTER) { pList->curr = pList->last; } else if (enPosition == BSL_LIST_POS_BEFORE) { pList->curr = pList->first; } } } if ((enPosition == BSL_LIST_POS_AFTER) || (enPosition == BSL_LIST_POS_END)) { if (enPosition == BSL_LIST_POS_END) { pList->curr = pList->last; } return BslListAddAfterCurr(pList, pData); } else { if (enPosition == BSL_LIST_POS_BEGIN) { pList->curr = pList->first; } return BslListInsertBeforeCurr(pList, pData); } } int32_t BSL_LIST_AddElement(BslList *pList, void *pData, BslListPosition enPosition) { /* check for missing argument */ if (pList == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } /* we are doing a range checking. the same thing is done in another way for clarity */ if (enPosition < BSL_LIST_POS_BEFORE || enPosition > BSL_LIST_POS_END) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (pData == NULL) { BSL_ERR_PUSH_ERROR(BSL_LIST_DATA_NOT_AVAILABLE); return BSL_LIST_DATA_NOT_AVAILABLE; } return (int32_t)BSL_LIST_AddElementInt(pList, pData, enPosition); } int32_t BSL_LIST_SetMaxElements(int32_t iMaxElements) { if (iMaxElements < 0xffff || iMaxElements > 0xfffffff) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } g_maxListCount = iMaxElements; return BSL_SUCCESS; } int32_t BSL_LIST_GetMaxElements(void) { return g_maxListCount; } void BSL_LIST_DeleteAll(BslList *pList, BSL_LIST_PFUNC_FREE pfFreeFunc) { BslListNode *pNode = NULL; BslListNode *pNext = NULL; /* check for missing argument */ if (pList == NULL) { return; } pNode = pList->first; /* delete each node one by one */ while (pNode != NULL) { pNext = pNode->next; if (pfFreeFunc == NULL) { BSL_SAL_FREE(pNode->data); } else { pfFreeFunc(pNode->data); pNode->data = NULL; } BSL_SAL_FREE(pNode); pNode = pNext; pList->count--; } pList->first = pList->last = pList->curr = NULL; } void BSL_LIST_DeleteCurrent(BslList *pList, BSL_LIST_PFUNC_FREE pfFreeFunc) { BslListNode *pNode = NULL; /* check for missing argument */ if (pList == NULL) { return; } if (pList->curr != NULL) { /* delete current and set prev and next appropriately */ if (pList->curr->next != NULL) { pList->curr->next->prev = pList->curr->prev; } else { pList->last = pList->curr->prev; } if (pList->curr->prev != NULL) { pList->curr->prev->next = pList->curr->next; } else { pList->first = pList->curr->next; } pNode = pList->curr; pList->curr = pList->curr->next; pList->count--; if (pfFreeFunc == NULL) { BSL_SAL_FREE(pNode->data); } else { pfFreeFunc(pNode->data); } BSL_SAL_FREE(pNode); } } void BSL_LIST_DetachCurrent(BslList *pList) { /* check for missing argument */ BslListNode *tmpCurr = NULL; if (pList == NULL) { return; } tmpCurr = pList->curr; // get the current node if (tmpCurr != NULL) { /* delete current and set prev and next appropriately */ if (tmpCurr->next != NULL) { // check for last node // current node is not the last node tmpCurr->next->prev = tmpCurr->prev; // update the current node and point it to the next node pList->curr = tmpCurr->next; } else { // current node is the last node pList->last = tmpCurr->prev; // update the current node and point it to the last node pList->curr = pList->last; } if (tmpCurr->prev != NULL) { // check for the first node // current node is not the first node tmpCurr->prev->next = tmpCurr->next; } else { // current node is the first node pList->first = tmpCurr->next; } // we have already updated the pList->curr pointer appropriately pList->count--; // now we must free the temp current node BSL_SAL_FREE(tmpCurr); } } /** * @ingroup bsl_list * @brief Searches for an element and as well return immediately after internal error * * @param pList [IN] List in which object is searched for. * @param pSearchFor [IN] Object to be searched for. * @param pSearcher [IN] Search Function to be used. * @param pstErr [OUT] Update the Internal Error if Any. If pstErr is not equal to NULL. If NULL this will be ignored. * @retval void * */ static void *BSL_ListSearchInt(BslList *pList, const void *pSearchFor, BSL_LIST_PFUNC_CMP pSearcher, int32_t *pstErr) { /* temporarily stores current node */ BslListNode *pstTempCurr = NULL; /* check for missing argument */ if (pList == NULL || pSearchFor == NULL) { return NULL; } pstTempCurr = pList->curr; /* parse all nodes one by one */ for ((pList)->curr = (pList)->first; (pList)->curr != NULL; (pList)->curr = (pList)->curr->next) { if (pSearcher == NULL) { /* if pSearcher is NULL, use memcmp */ if (memcmp(pList->curr->data, pSearchFor, (uint32_t)pList->dataSize) == 0) { return pList->curr->data; } } else { int32_t retVal = pSearcher(pList->curr->data, pSearchFor); if (retVal == SEC_INT_ERROR && pstErr != NULL) { *pstErr = SEC_INT_ERROR; return NULL; } if (retVal == 0) { return pList->curr->data; } } } /* no match found */ pList->curr = pstTempCurr; return NULL; } void *BSL_LIST_Search(BslList *pList, const void *pSearchFor, BSL_LIST_PFUNC_CMP pSearcher, int32_t *pstErr) { return BSL_ListSearchInt(pList, pSearchFor, pSearcher, pstErr); } void *BSL_LIST_SearchEx(BslList *pList, const void *pSearchFor, BSL_LIST_PFUNC_CMP pSearcher) { return BSL_ListSearchInt(pList, pSearchFor, pSearcher, NULL); } void *BSL_LIST_GetIndexNode(uint32_t ulIndex, BslList *pList) { if (pList == NULL) { return NULL; } if (ulIndex >= (uint32_t)pList->count) { return NULL; } if (BSL_LIST_GET_FIRST(pList) == NULL) { return NULL; } for (uint32_t ulIter = 0; ulIter < ulIndex; ulIter++) { if (BSL_LIST_GET_NEXT(pList) == NULL) { return NULL; } } return pList->curr->data; } BslList *BSL_LIST_Copy(BslList *pSrcList, BSL_LIST_PFUNC_DUP pFuncCpy, BSL_LIST_PFUNC_FREE pfFreeFunc) { void *pDstData = NULL; if (pSrcList == NULL) { return NULL; } /* we will first get the source data and if successful go ahead */ void *pSrcData = BSL_LIST_GET_FIRST(pSrcList); if (pSrcData == NULL) { return NULL; } BslList *pDstList = BSL_LIST_New(pSrcList->dataSize); if (pDstList == NULL) { return NULL; } for (int32_t i = 1; pSrcData != NULL && i <= BSL_LIST_COUNT(pSrcList); i++) { if (pFuncCpy != NULL) { pDstData = pFuncCpy(pSrcData); } else { uint32_t dataLen = (uint32_t)(pSrcList->dataSize); pDstData = BSL_SAL_Calloc(1, dataLen); /* we must do NULL check */ if (pDstData == NULL) { BSL_LIST_FREE(pDstList, pfFreeFunc); return NULL; } (void)memcpy_s(pDstData, dataLen, pSrcData, dataLen); } if (pDstData == NULL) { BSL_LIST_FREE(pDstList, pfFreeFunc); return NULL; } if (BSL_LIST_AddElement(pDstList, pDstData, BSL_LIST_POS_AFTER) != BSL_SUCCESS) { if (pfFreeFunc != NULL) { pfFreeFunc(pDstData); pDstData = NULL; } else { BSL_SAL_FREE(pDstData); } BSL_LIST_FREE(pDstList, pfFreeFunc); return NULL; } pSrcData = BSL_LIST_GET_NEXT(pSrcList); } return pDstList; } void BSL_LIST_DeleteAllAfterSort(BslList *pList) { BslListNode *pNode = NULL; BslListNode *pNext = NULL; /* check for missing argument */ if (pList == NULL) { return; } pNode = pList->first; /* delete each node one by one */ while (pNode != NULL) { pNext = pNode->next; BSL_SAL_FREE(pNode); pNode = pNext; pList->count--; } pList->first = pList->last = pList->curr = NULL; } BslList *BSL_LIST_Sort(BslList *pList, BSL_LIST_PFUNC_CMP pfCmp) { if (pfCmp == NULL) { return NULL; } int32_t iRet = BSL_ListSortInternal(pList, pfCmp); if (iRet != BSL_SUCCESS) { return NULL; } return pList; } void BSL_LIST_FreeWithoutData(BslList *pstList) { BslListNode *node = NULL; BslListNode *next = NULL; if (pstList != NULL) { node = pstList->first; while (node != NULL) { next = node->next; BSL_SAL_FREE(node); node = next; } BSL_SAL_FREE(pstList); } } void BSL_LIST_RevList(BslList *pstList) { struct BslListNode *pstTemp = NULL; if (pstList == NULL) { return; } pstList->curr = pstList->first; while (pstList->curr != NULL) { pstTemp = pstList->curr->next; pstList->curr->next = pstList->curr->prev; pstList->curr->prev = pstTemp; pstList->curr = pstTemp; } pstList->curr = pstList->first; pstList->first = pstList->last; pstList->last = pstList->curr; pstList->curr = pstList->first; return; } BslList *BSL_ListConcatToEmptyList(BslList *pDestList, const BslList *pSrcList) { pDestList->count = pSrcList->count; pDestList->first = pSrcList->first; pDestList->last = pSrcList->last; pDestList->curr = pDestList->first; return pDestList; } BslList *BSL_ListConcatToNonEmptyList(BslList *pDestList, const BslList *pSrcList) { if ((pDestList->count + pSrcList->count) > g_maxListCount) { return NULL; } pDestList->count += pSrcList->count; pSrcList->first->prev = pDestList->last; pDestList->last->next = pSrcList->first; pDestList->last = pSrcList->last; return pDestList; } BslList *BSL_LIST_Concat(BslList *pDestList, const BslList *pSrcList) { if (pDestList == NULL || pSrcList == NULL) { return NULL; } if (pSrcList->count == 0) { return pDestList; } if (pDestList->count == 0) { return BSL_ListConcatToEmptyList(pDestList, pSrcList); } return BSL_ListConcatToNonEmptyList(pDestList, pSrcList); } #endif /* HITLS_BSL_LIST */
2302_82127028/openHiTLS-examples
bsl/list/src/bsl_list.c
C
unknown
14,879
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_LIST #include "securec.h" #include "bsl_sal.h" #include "bsl_list.h" BslListNode *BSL_LIST_FirstNode(const BslList *list) { if (list == NULL) { return NULL; } return list->first; } void *BSL_LIST_GetData(const BslListNode *pstNode) { if (pstNode == NULL) { return NULL; } return pstNode->data; } BslListNode *BSL_LIST_GetNextNode(const BslList *pstList, const BslListNode *pstListNode) { if (pstList == NULL) { return NULL; } if (pstListNode != NULL) { return pstListNode->next; } return pstList->first; } void *BSL_LIST_GetIndexNodeEx(uint32_t ulIndex, const BslListNode *pstListNode, const BslList *pstList) { const BslListNode *pstTmpListNode = NULL; (void)pstListNode; if (pstList == NULL) { return NULL; } if (ulIndex >= (uint32_t)pstList->count) { return NULL; } if (pstList->first == NULL) { return NULL; } pstTmpListNode = pstList->first; for (uint32_t ulIter = 0; ulIter < ulIndex; ulIter++) { pstTmpListNode = pstTmpListNode->next; if (pstTmpListNode == NULL) { return NULL; } } return pstTmpListNode->data; } BslListNode *BSL_LIST_GetPrevNode(const BslListNode *pstListNode) { if (pstListNode == NULL) { return NULL; } return pstListNode->prev; } void BSL_LIST_DeleteNode(BslList *pstList, const BslListNode *pstListNode, BSL_LIST_PFUNC_FREE pfFreeFunc) { BslListNode *pstCurrentNode = NULL; if (pstList == NULL) { return; } pstCurrentNode = pstList->first; while (pstCurrentNode != NULL) { if (pstCurrentNode == pstListNode) { // found matching node, delete this node and adjust the list if ((pstCurrentNode->next) != NULL) { pstCurrentNode->next->prev = pstCurrentNode->prev; } else { pstList->last = pstCurrentNode->prev; } if ((pstCurrentNode->prev) != NULL) { pstCurrentNode->prev->next = pstCurrentNode->next; } else { pstList->first = pstCurrentNode->next; } if (pstCurrentNode == pstList->curr) { pstList->curr = pstList->curr->next; } pstList->count--; if (pfFreeFunc == NULL) { BSL_SAL_FREE(pstCurrentNode->data); } else { pfFreeFunc(pstCurrentNode->data); } BSL_SAL_FREE(pstCurrentNode); return; } pstCurrentNode = pstCurrentNode->next; } return; } void BSL_LIST_DetachNode(BslList *pstList, BslListNode **pstListNode) { if (pstList == NULL || pstListNode == NULL) { return; } BslListNode *pstCurrentNode = pstList->first; while (pstCurrentNode != NULL) { if (pstCurrentNode != *pstListNode) { pstCurrentNode = pstCurrentNode->next; continue; } // found matching node, delete this node and adjust the list if ((pstCurrentNode->next) != NULL) { pstCurrentNode->next->prev = pstCurrentNode->prev; if (*pstListNode == pstList->curr) { pstList->curr = pstCurrentNode->next; } *pstListNode = pstCurrentNode->next; // update the current node and point it to the next node } else { pstList->last = pstCurrentNode->prev; if (*pstListNode == pstList->curr) { pstList->curr = pstCurrentNode->prev; } *pstListNode = pstList->last; } if ((pstCurrentNode->prev) != NULL) { pstCurrentNode->prev->next = pstCurrentNode->next; } else { pstList->first = pstCurrentNode->next; } pstList->count--; BSL_SAL_FREE(pstCurrentNode); return; } return; } #endif /* HITLS_BSL_LIST */
2302_82127028/openHiTLS-examples
bsl/list/src/bsl_list_ex.c
C
unknown
4,554
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_LIST #include <stdlib.h> #include "bsl_list.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "bsl_list_internal.h" #define SEC_MAX_QSORT_SIZE (64 * 1024 * 1024) #define SEC_MIN_QSORT_SIZE 10000 static uint32_t g_maxQsortElem = 100000; BslList *BSL_LIST_New(int32_t dataSize) { BslList *pstList = NULL; if (dataSize < 0) { return NULL; } pstList = BSL_SAL_Calloc(1, sizeof(BslList)); if (pstList == NULL) { return NULL; } pstList->curr = NULL; pstList->last = NULL; pstList->first = NULL; pstList->dataSize = dataSize; pstList->count = 0; return pstList; } void *BSL_LIST_Prev(BslList *pstList) { if (pstList == NULL) { return NULL; } if (pstList->curr != NULL) { pstList->curr = pstList->curr->prev; } else { pstList->curr = pstList->last; } if (pstList->curr != NULL) { return (void *)&(pstList->curr->data); } return NULL; } void *BSL_LIST_Next(BslList *pstList) { if (pstList == NULL) { return NULL; } if (pstList->curr != NULL) { pstList->curr = pstList->curr->next; } else { pstList->curr = pstList->first; } if (pstList->curr != NULL) { return (void *)&(pstList->curr->data); } return NULL; } void *BSL_LIST_Last(BslList *pstList) { if (pstList == NULL) { return NULL; } pstList->curr = pstList->last; if (pstList->curr != NULL) { return (void *)&(pstList->curr->data); } return NULL; } void *BSL_LIST_First(BslList *pstList) { if (pstList == NULL) { return NULL; } pstList->curr = pstList->first; if (pstList->curr != NULL) { return (void *)&(pstList->curr->data); } return NULL; } void *BSL_LIST_Curr(const BslList *pstList) { if (pstList == NULL || pstList->curr == NULL) { return NULL; } return (void *)&(pstList->curr->data); } int32_t BSL_LIST_GetElmtIndex(const void *elmt, BslList *pstList) { int32_t idx = 0; void *tmpElmt = NULL; if (pstList == NULL) { return -1; // -1 means that the corresponding element is not found } BslListNode *tmp = (void *)pstList->curr; for ((pstList)->curr = (pstList)->first; (pstList)->curr != NULL; (pstList)->curr = (pstList)->curr->next) { tmpElmt = pstList->curr->data; if (tmpElmt == NULL) { break; } if (tmpElmt != elmt) { idx++; continue; } pstList->curr = tmp; return idx; } pstList->curr = tmp; return -1; // -1 means that the corresponding element is not found } int32_t BSL_ListSortInternal(BslList *pList, BSL_LIST_PFUNC_CMP cmp) { void **sortArray = NULL; void *elmt = NULL; int32_t i; /* Make sure pList is not NULL */ if (pList == NULL || g_maxQsortElem < (uint32_t)pList->count) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } /* Create array of elements so we can qsort the pList */ sortArray = BSL_SAL_Calloc((uint32_t)pList->count, sizeof(void *)); if (sortArray == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } /* Copy the elements from the pList into the sort array */ for (pList->curr = pList->first, i = 0; pList->curr; pList->curr = pList->curr->next, i++) { elmt = (void *)pList->curr->data; if (elmt == NULL || i >= pList->count) { break; } sortArray[i] = elmt; } /* sort encoded elements */ qsort(sortArray, (uint32_t)pList->count, sizeof(void *), cmp); for (pList->curr = pList->first, i = 0; pList->curr != NULL; pList->curr = pList->curr->next, i++) { pList->curr->data = sortArray[i]; } BSL_SAL_FREE(sortArray); /* Return the sorted pList */ return BSL_SUCCESS; } int32_t BSL_LIST_SetMaxQsortCount(uint32_t uiQsortSize) { if ((uiQsortSize > SEC_MAX_QSORT_SIZE) || (uiQsortSize < SEC_MIN_QSORT_SIZE)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } g_maxQsortElem = uiQsortSize; return BSL_SUCCESS; } uint32_t BSL_LIST_GetMaxQsortCount(void) { return g_maxQsortElem; } #endif /* HITLS_BSL_LIST */
2302_82127028/openHiTLS-examples
bsl/list/src/bsl_list_internal.c
C
unknown
4,905
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_LOG_INTERNAL_H #define BSL_LOG_INTERNAL_H #include <stdint.h> #include "hitls_build.h" #include "bsl_log.h" #ifdef __cplusplus extern "C" { #endif #ifdef HITLS_BSL_LOG #ifdef HITLS_BSL_LOG_NO_FORMAT_STRING #define LOG_STR(str) NULL #else #define LOG_STR(str) (str) #endif #define BSL_LOG_BUF_SIZE 1024U /** * @ingroup bsl_log * @brief four-parameter dotting log function * @attention A maximum of four parameters can be contained in the formatted string. * If the number of parameters is less than four, 0s or NULL must be added. * If the number of parameters exceeds four, multiple invoking is required. * Only the LOG_BINLOG_FIXLEN macro can be invoked. This macro cannot be redefined. * @param logId [IN] Log ID * @param logLevel [IN] Log level * @param logType [IN] String label * @param format [IN] Format string. Only literal strings are allowed. Variables are not allowed. * @param para1 [IN] Parameter 1 * @param para2 [IN] Parameter 2 * @param para3 [IN] Parameter 3 * @param para4 [IN] Parameter 4 */ void BSL_LOG_BinLogFixLen(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para1, void *para2, void *para3, void *para4); /** * @ingroup bsl_log * @brief one-parameter dotting log function * @attention The formatted character string contains only one parameter, which is %s. * For a pure character string, use LOG_BinLogFixLen * Only the LOG_BINLOG_VARLEN macro can be invoked. This macro cannot be redefined. * @param logId [IN] Log ID * @param logLevel [IN] Log level * @param logType [IN] String label * @param format [IN] Format string. Only literal strings are allowed. Variables are not allowed. * @param para [IN] Parameter */ void BSL_LOG_BinLogVarLen(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para); /** * @ingroup bsl_log * @brief four-parameter dotting log macro * @attention A maximum of four parameters can be contained in the formatted string. * If the number of parameters is less than four, 0s or NULL must be added. * If the number of parameters exceeds four, multiple invoking is required. * @param logId [IN] Log ID * @param logLevel [IN] Log level * @param logType [IN] String label * @param format [IN] Format string. Only literal strings are allowed. Variables are not allowed. * @param para1 [IN] Parameter 1 * @param para2 [IN] Parameter 2 * @param para3 [IN] Parameter 3 * @param para4 [IN] Parameter 4 */ #define BSL_LOG_BINLOG_FIXLEN(logId, logLevel, logType, format, para1, para2, para3, para4) \ BSL_LOG_BinLogFixLen(logId, logLevel, logType, \ (void *)(uintptr_t)(const void *)(LOG_STR(format)), (void *)(uintptr_t)(para1), (void *)(uintptr_t)(para2), \ (void *)(uintptr_t)(para3), (void *)(uintptr_t)(para4)) /** * @ingroup bsl_log * @brief one-parameter dotting log macro * @attention The formatted character string contains only one parameter, which is %s. * For a pure character string, use LOG_BinLogFixLen * @param logId [IN] Log ID * @param logLevel [IN] Log level * @param logType [IN] String label * @param format [IN] Format string. Only literal strings are allowed. Variables are not allowed. * @param para [IN] Parameter */ #define BSL_LOG_BINLOG_VARLEN(logId, logLevel, logType, format, para) \ BSL_LOG_BinLogVarLen(logId, logLevel, logType, \ (void *)(uintptr_t)(const void *)(LOG_STR(format)), (void *)(uintptr_t)(const void *)(para)) #else #define BSL_LOG_BINLOG_FIXLEN(logId, logLevel, logType, format, para1, para2, para3, para4) #define BSL_LOG_BINLOG_VARLEN(logId, logLevel, logType, format, para) #endif /* HITLS_BSL_LOG */ #ifdef __cplusplus } #endif #endif // BSL_LOG_INTERNAL_H
2302_82127028/openHiTLS-examples
bsl/log/include/bsl_log_internal.h
C
unknown
4,352
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_LOG #include <stdbool.h> #include "securec.h" #include "bsl_errno.h" #include "bsl_log.h" #include "bsl_log_internal.h" /* string of HiTLS version */ static char g_openHiTLSVersion[HITLS_VERSION_LEN] = OPENHITLS_VERSION_S; static uint64_t g_openHiTLSNumVersion = OPENHITLS_VERSION_I; int32_t BSL_LOG_GetVersion(char *version, uint32_t *versionLen) { if (version == NULL || versionLen == NULL) { return BSL_LOG_ERR_BAD_PARAM; } if (*versionLen < HITLS_VERSION_LEN) { return BSL_LOG_ERR_BAD_PARAM; } uint32_t len = (uint32_t)strlen(g_openHiTLSVersion); if (memcpy_s(version, *versionLen, g_openHiTLSVersion, len) != EOK) { return BSL_MEMCPY_FAIL; } *versionLen = len; return BSL_SUCCESS; } uint64_t BSL_LOG_GetVersionNum(void) { return g_openHiTLSNumVersion; } static BSL_LOG_BinLogFixLenFunc g_fixLenFunc = NULL; static BSL_LOG_BinLogVarLenFunc g_varLenFunc = NULL; static uint32_t g_binlogLevel = BSL_LOG_LEVEL_ERR; // error-level is enabled by default static uint32_t g_binlogType = BSL_LOG_BINLOG_TYPE_RUN; // type run is enabled by default, other types can be added. int32_t BSL_LOG_RegBinLogFunc(const BSL_LOG_BinLogFuncs *funcs) { bool invalid = funcs == NULL || funcs->fixLenFunc == NULL || funcs->varLenFunc == NULL; if (invalid) { return BSL_NULL_INPUT; } g_fixLenFunc = funcs->fixLenFunc; g_varLenFunc = funcs->varLenFunc; return BSL_SUCCESS; } int32_t BSL_LOG_SetBinLogLevel(uint32_t level) { if (level > BSL_LOG_LEVEL_DEBUG) { return BSL_LOG_ERR_BAD_PARAM; } g_binlogLevel = level; return BSL_SUCCESS; } uint32_t BSL_LOG_GetBinLogLevel(void) { return g_binlogLevel; } void BSL_LOG_BinLogFixLen(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para1, void *para2, void *para3, void *para4) { bool invalid = (logLevel > g_binlogLevel) || ((logType & g_binlogType) == 0) || (g_fixLenFunc == NULL); if (!invalid) { g_fixLenFunc(logId, logLevel, logType, format, para1, para2, para3, para4); } } void BSL_LOG_BinLogVarLen(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para) { bool invalid = (logLevel > g_binlogLevel) || ((logType & g_binlogType) == 0) || (g_varLenFunc == NULL); if (!invalid) { g_varLenFunc(logId, logLevel, logType, format, para); } } #endif /* HITLS_BSL_LOG */
2302_82127028/openHiTLS-examples
bsl/log/src/log.c
C
unknown
3,005
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_OBJ_INTERNAL_H #define BSL_OBJ_INTERNAL_H #include "hitls_build.h" #ifdef HITLS_BSL_OBJ #include <stdint.h> #include "bsl_obj.h" #ifdef __cplusplus extern "C" { #endif typedef enum { BSL_OID_GLOBAL, BSL_OID_HEAP } BslOidFlag; typedef struct { BslOidString strOid; const char *oidName; BslCid cid; } BslOidInfo; typedef struct { BslCid cid; int32_t min; int32_t max; const char *shortName; } BslAsn1DnInfo; BslCid BSL_OBJ_GetHashIdFromSignId(BslCid signAlg); BslCid BSL_OBJ_GetAsymAlgIdFromSignId(BslCid signAlg); const char *BSL_OBJ_GetOidNameFromOid(const BslOidString *oid); BslCid BSL_OBJ_GetSignIdFromHashAndAsymId(BslCid asymAlg, BslCid hashAlg); const BslAsn1DnInfo *BSL_OBJ_GetDnInfoFromCid(BslCid cid); const char *BSL_OBJ_GetOidNameFromCID(BslCid ulCID); const BslAsn1DnInfo *BSL_OBJ_GetDnInfoFromShortName(const char *shortName); void BSL_OBJ_FreeSignHashTable(void); void BSL_OBJ_FreeHashTable(void); #ifdef __cplusplus } #endif #endif #endif // BSL_OBJ_INTERNAL_H
2302_82127028/openHiTLS-examples
bsl/obj/include/bsl_obj_internal.h
C
unknown
1,585
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_OBJ #include <stddef.h> #include "securec.h" #include "bsl_obj.h" #include "bsl_sal.h" #include "bsl_obj_internal.h" #include "bsl_err_internal.h" #ifdef HITLS_BSL_HASH #include "bsl_hash.h" // Hash table for signature algorithm mappings BSL_HASH_Hash *g_signHashTable = NULL; // Read-write lock for thread-safe access to g_signHashTable static BSL_SAL_ThreadLockHandle g_signHashRwLock = NULL; // Once control for thread-safe initialization static uint32_t g_signHashInitOnce = BSL_SAL_ONCE_INIT; #define BSL_OBJ_SIGN_HASH_BKT_SIZE 64u #endif // HITLS_BSL_HASH typedef struct BslSignIdMap { BslCid signId; BslCid asymId; BslCid hashId; } BSL_SignIdMap; static BSL_SignIdMap g_signIdMap[] = { {BSL_CID_MD5WITHRSA, BSL_CID_RSA, BSL_CID_MD5}, {BSL_CID_SHA1WITHRSA, BSL_CID_RSA, BSL_CID_SHA1}, {BSL_CID_SHA224WITHRSAENCRYPTION, BSL_CID_RSA, BSL_CID_SHA224}, {BSL_CID_SHA256WITHRSAENCRYPTION, BSL_CID_RSA, BSL_CID_SHA256}, {BSL_CID_SHA384WITHRSAENCRYPTION, BSL_CID_RSA, BSL_CID_SHA384}, {BSL_CID_SHA512WITHRSAENCRYPTION, BSL_CID_RSA, BSL_CID_SHA512}, {BSL_CID_RSASSAPSS, BSL_CID_RSA, BSL_CID_UNKNOWN}, {BSL_CID_SM3WITHRSAENCRYPTION, BSL_CID_RSA, BSL_CID_SM3}, {BSL_CID_DSAWITHSHA1, BSL_CID_DSA, BSL_CID_SHA1}, {BSL_CID_DSAWITHSHA224, BSL_CID_DSA, BSL_CID_SHA224}, {BSL_CID_DSAWITHSHA256, BSL_CID_DSA, BSL_CID_SHA256}, {BSL_CID_DSAWITHSHA384, BSL_CID_DSA, BSL_CID_SHA384}, {BSL_CID_DSAWITHSHA512, BSL_CID_DSA, BSL_CID_SHA512}, {BSL_CID_ECDSAWITHSHA1, BSL_CID_ECDSA, BSL_CID_SHA1}, {BSL_CID_ECDSAWITHSHA224, BSL_CID_ECDSA, BSL_CID_SHA224}, {BSL_CID_ECDSAWITHSHA256, BSL_CID_ECDSA, BSL_CID_SHA256}, {BSL_CID_ECDSAWITHSHA384, BSL_CID_ECDSA, BSL_CID_SHA384}, {BSL_CID_ECDSAWITHSHA512, BSL_CID_ECDSA, BSL_CID_SHA512}, {BSL_CID_SM2DSAWITHSM3, BSL_CID_SM2DSA, BSL_CID_SM3}, {BSL_CID_SM2DSAWITHSHA1, BSL_CID_SM2DSA, BSL_CID_SHA1}, {BSL_CID_SM2DSAWITHSHA256, BSL_CID_SM2DSA, BSL_CID_SHA256}, {BSL_CID_ED25519, BSL_CID_ED25519, BSL_CID_SHA512}, }; #ifdef HITLS_BSL_HASH static void FreeBslSignIdMap(void *data) { BSL_SignIdMap *signIdMap = (BSL_SignIdMap *)data; BSL_SAL_Free(signIdMap); } static void *DupBslSignIdMap(void *data, size_t size) { if (data == NULL || size != sizeof(BSL_SignIdMap)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } BSL_SignIdMap *signIdMap = (BSL_SignIdMap *)data; BSL_SignIdMap *newSignIdMap = BSL_SAL_Malloc(sizeof(BSL_SignIdMap)); if (newSignIdMap == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } newSignIdMap->signId = signIdMap->signId; newSignIdMap->asymId = signIdMap->asymId; newSignIdMap->hashId = signIdMap->hashId; return (void *)newSignIdMap; } static void InitSignHashTableOnce(void) { int32_t ret = BSL_SAL_ThreadLockNew(&g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return; } ListDupFreeFuncPair valueFunc = {DupBslSignIdMap, FreeBslSignIdMap}; g_signHashTable = BSL_HASH_Create(BSL_OBJ_SIGN_HASH_BKT_SIZE, NULL, NULL, NULL, &valueFunc); if (g_signHashTable == NULL) { (void)BSL_SAL_ThreadLockFree(g_signHashRwLock); g_signHashRwLock = NULL; BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); } } #endif BslCid BSL_OBJ_GetHashIdFromSignId(BslCid signAlg) { if (signAlg == BSL_CID_UNKNOWN) { return BSL_CID_UNKNOWN; } // First, search in the static g_signIdMap table for (uint32_t iter = 0; iter < sizeof(g_signIdMap) / sizeof(BSL_SignIdMap); iter++) { if (signAlg == g_signIdMap[iter].signId) { return g_signIdMap[iter].hashId; } } #ifndef HITLS_BSL_HASH return BSL_CID_UNKNOWN; #else if (g_signHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_CID_UNKNOWN; } // Second, search in the dynamic hash table with read lock BSL_SignIdMap *signIdMap = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return BSL_CID_UNKNOWN; } ret = BSL_HASH_At(g_signHashTable, (uintptr_t)signAlg, (uintptr_t *)&signIdMap); BslCid result = (ret == BSL_SUCCESS && signIdMap != NULL) ? signIdMap->hashId : BSL_CID_UNKNOWN; (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_FIND_HASH_TABLE); } return result; #endif } BslCid BSL_OBJ_GetAsymAlgIdFromSignId(BslCid signAlg) { if (signAlg == BSL_CID_UNKNOWN) { return BSL_CID_UNKNOWN; } // First, search in the static g_signIdMap table for (uint32_t iter = 0; iter < sizeof(g_signIdMap) / sizeof(BSL_SignIdMap); iter++) { if (signAlg == g_signIdMap[iter].signId) { return g_signIdMap[iter].asymId; } } #ifndef HITLS_BSL_HASH return BSL_CID_UNKNOWN; #else if (g_signHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_CID_UNKNOWN; } // Second, search in the dynamic hash table with read lock BSL_SignIdMap *signIdMap = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return BSL_CID_UNKNOWN; } ret = BSL_HASH_At(g_signHashTable, (uintptr_t)signAlg, (uintptr_t *)&signIdMap); BslCid asymCid = (ret == BSL_SUCCESS && signIdMap != NULL) ? signIdMap->asymId : BSL_CID_UNKNOWN; (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_FIND_HASH_TABLE); } return asymCid; #endif } BslCid BSL_OBJ_GetSignIdFromHashAndAsymId(BslCid asymAlg, BslCid hashAlg) { if (asymAlg == BSL_CID_ED25519) { return BSL_CID_ED25519; } if (asymAlg == BSL_CID_UNKNOWN || hashAlg == BSL_CID_UNKNOWN) { return BSL_CID_UNKNOWN; } // First, search in the static g_signIdMap table for (uint32_t i = 0; i < sizeof(g_signIdMap) / sizeof(g_signIdMap[0]); i++) { if (g_signIdMap[i].asymId == asymAlg && g_signIdMap[i].hashId == hashAlg) { return g_signIdMap[i].signId; } } #ifndef HITLS_BSL_HASH return BSL_CID_UNKNOWN; #else if (g_signHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_CID_UNKNOWN; } // Second, search in the dynamic hash table with read lock BSL_SignIdMap *signIdMap = NULL; uint64_t asymAndHashKey = ((uint64_t)asymAlg << 32) | ((uint64_t)hashAlg & 0xFFFFFFFF); int32_t ret = BSL_SAL_ThreadReadLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return BSL_CID_UNKNOWN; } ret = BSL_HASH_At(g_signHashTable, (uintptr_t)asymAndHashKey, (uintptr_t *)&signIdMap); BslCid signCid = (ret == BSL_SUCCESS && signIdMap != NULL) ? signIdMap->signId : BSL_CID_UNKNOWN; (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_FIND_HASH_TABLE); } return signCid; #endif } #ifdef HITLS_BSL_HASH static bool IsSignIdInStaticTable(int32_t signId) { for (uint32_t iter = 0; iter < sizeof(g_signIdMap) / sizeof(BSL_SignIdMap); iter++) { if (signId == (int32_t)g_signIdMap[iter].signId) { return true; } } return false; } static int32_t IsSignIdInHashTable(int32_t signId) { BSL_SignIdMap *signIdMap = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BSL_HASH_At(g_signHashTable, (uintptr_t)signId, (uintptr_t *)&signIdMap); (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); if (ret != BSL_SUCCESS || signIdMap == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_FIND_HASH_TABLE); return BSL_OBJ_ERR_FIND_HASH_TABLE; } return BSL_SUCCESS; } // Inserts a new signature ID mapping into the hash table (using write lock) static int32_t InsertSignIdMapping(int32_t signId, int32_t asymId, int32_t hashId) { BSL_SignIdMap *signIdMap = NULL; BSL_SignIdMap newSignIdMap = {signId, asymId, hashId}; uint64_t asymAndHashKey; int32_t ret = BSL_SAL_ThreadWriteLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BSL_HASH_At(g_signHashTable, (uintptr_t)signId, (uintptr_t *)&signIdMap); if (ret == BSL_SUCCESS && signIdMap != NULL) { (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); return BSL_SUCCESS; } ret = BSL_HASH_Insert(g_signHashTable, (uintptr_t)signId, sizeof(BslCid), (uintptr_t)&newSignIdMap, sizeof(BSL_SignIdMap)); if (ret != BSL_SUCCESS) { (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_INSERT_HASH_TABLE); return BSL_OBJ_ERR_INSERT_HASH_TABLE; } asymAndHashKey = ((uint64_t)asymId << 32) | ((uint64_t)hashId & 0xFFFFFFFF); ret = BSL_HASH_Insert(g_signHashTable, (uintptr_t)asymAndHashKey, sizeof(uintptr_t), (uintptr_t)&newSignIdMap, sizeof(BSL_SignIdMap)); if (ret != BSL_SUCCESS) { BSL_HASH_Erase(g_signHashTable, (uintptr_t)signId); (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_INSERT_HASH_TABLE); return BSL_OBJ_ERR_INSERT_HASH_TABLE; } (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); return BSL_SUCCESS; } // Main function - now more concise int32_t BSL_OBJ_CreateSignId(int32_t signId, int32_t asymId, int32_t hashId) { // Parameter validation if (signId == BSL_CID_UNKNOWN || asymId == BSL_CID_UNKNOWN || hashId == BSL_CID_UNKNOWN) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (IsSignIdInStaticTable(signId)) { return BSL_SUCCESS; } int32_t ret = BSL_SAL_ThreadRunOnce(&g_signHashInitOnce, InitSignHashTableOnce); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (g_signHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_OBJ_INVALID_HASH_TABLE; } ret = IsSignIdInHashTable(signId); if (ret == BSL_SUCCESS) { return BSL_SUCCESS; } return InsertSignIdMapping(signId, asymId, hashId); } void BSL_OBJ_FreeSignHashTable(void) { if (g_signHashTable != NULL) { int32_t ret = BSL_SAL_ThreadWriteLock(g_signHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return; } BSL_HASH_Destory(g_signHashTable); g_signHashTable = NULL; (void)BSL_SAL_ThreadUnlock(g_signHashRwLock); if (g_signHashRwLock != NULL) { (void)BSL_SAL_ThreadLockFree(g_signHashRwLock); g_signHashRwLock = NULL; } g_signHashInitOnce = BSL_SAL_ONCE_INIT; } } #endif // HITLS_BSL_HASH #endif
2302_82127028/openHiTLS-examples
bsl/obj/src/bsl_cid_op.c
C
unknown
11,644
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_OBJ #include <stddef.h> #include <string.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_obj.h" #include "bsl_obj_internal.h" #include "bsl_err_internal.h" #ifdef HITLS_BSL_HASH #include "bsl_hash.h" #define BSL_OBJ_HASH_BKT_SIZE 256u BSL_HASH_Hash *g_oidHashTable = NULL; static BSL_SAL_ThreadLockHandle g_oidHashRwLock = NULL; static uint32_t g_oidHashInitOnce = BSL_SAL_ONCE_INIT; #endif // HITLS_BSL_HASH #define BSL_OBJ_ARCS_X_MAX 2 #define BSL_OBJ_ARCS_Y_MAX 40 #define BSL_OBJ_ARCS_MAX (BSL_OBJ_ARCS_X_MAX * BSL_OBJ_ARCS_Y_MAX + BSL_OBJ_ARCS_Y_MAX - 1) BslOidInfo g_oidTable[] = { {{9, "\140\206\110\1\145\3\4\1\1", BSL_OID_GLOBAL}, "aes-128-ecb", BSL_CID_AES128_ECB}, {{9, "\140\206\110\1\145\3\4\1\2", BSL_OID_GLOBAL}, "aes-128-cbc", BSL_CID_AES128_CBC}, {{9, "\140\206\110\1\145\3\4\1\3", BSL_OID_GLOBAL}, "aes-128-ofb", BSL_CID_AES128_OFB}, {{9, "\140\206\110\1\145\3\4\1\4", BSL_OID_GLOBAL}, "aes-128-cfb", BSL_CID_AES128_CFB}, {{9, "\140\206\110\1\145\3\4\1\25", BSL_OID_GLOBAL}, "aes-192-ecb", BSL_CID_AES192_ECB}, {{9, "\140\206\110\1\145\3\4\1\26", BSL_OID_GLOBAL}, "aes-192-cbc", BSL_CID_AES192_CBC}, {{9, "\140\206\110\1\145\3\4\1\27", BSL_OID_GLOBAL}, "aes-192-ofb", BSL_CID_AES192_OFB}, {{9, "\140\206\110\1\145\3\4\1\30", BSL_OID_GLOBAL}, "aes-192-cfb", BSL_CID_AES192_CFB}, {{9, "\140\206\110\1\145\3\4\1\51", BSL_OID_GLOBAL}, "aes-256-ecb", BSL_CID_AES256_ECB}, {{9, "\140\206\110\1\145\3\4\1\52", BSL_OID_GLOBAL}, "aes-256-cbc", BSL_CID_AES256_CBC}, {{9, "\140\206\110\1\145\3\4\1\53", BSL_OID_GLOBAL}, "aes-256-ofb", BSL_CID_AES256_OFB}, {{9, "\140\206\110\1\145\3\4\1\54", BSL_OID_GLOBAL}, "aes-256-cfb", BSL_CID_AES256_CFB}, {{9, "\52\206\110\206\367\15\1\1\1", BSL_OID_GLOBAL}, "RSAENCRYPTION", BSL_CID_RSA}, // rsa subkey {{7, "\52\206\110\316\70\4\1", BSL_OID_GLOBAL}, "DSAENCRYPTION", BSL_CID_DSA}, // dsa subkey {{8, "\52\206\110\206\367\15\2\5", BSL_OID_GLOBAL}, "MD5", BSL_CID_MD5}, {{5, "\53\16\3\2\32", BSL_OID_GLOBAL}, "SHA1", BSL_CID_SHA1}, {{9, "\140\206\110\1\145\3\4\2\4", BSL_OID_GLOBAL}, "SHA224", BSL_CID_SHA224}, {{9, "\140\206\110\1\145\3\4\2\1", BSL_OID_GLOBAL}, "SHA256", BSL_CID_SHA256}, {{9, "\140\206\110\1\145\3\4\2\2", BSL_OID_GLOBAL}, "SHA384", BSL_CID_SHA384}, {{9, "\140\206\110\1\145\3\4\2\3", BSL_OID_GLOBAL}, "SHA512", BSL_CID_SHA512}, {{8, "\53\6\1\5\5\10\1\1", BSL_OID_GLOBAL}, "HMAC-MD5", BSL_CID_HMAC_MD5}, {{8, "\52\206\110\206\367\15\2\7", BSL_OID_GLOBAL}, "HMAC-SHA1", BSL_CID_HMAC_SHA1}, {{8, "\52\206\110\206\367\15\2\10", BSL_OID_GLOBAL}, "HMAC-SHA224", BSL_CID_HMAC_SHA224}, {{8, "\52\206\110\206\367\15\2\11", BSL_OID_GLOBAL}, "HMAC-SHA256", BSL_CID_HMAC_SHA256}, {{8, "\52\206\110\206\367\15\2\12", BSL_OID_GLOBAL}, "HMAC-SHA384", BSL_CID_HMAC_SHA384}, {{8, "\52\206\110\206\367\15\2\13", BSL_OID_GLOBAL}, "HMAC-SHA512", BSL_CID_HMAC_SHA512}, {{9, "\52\206\110\206\367\15\1\1\4", BSL_OID_GLOBAL}, "MD5WITHRSA", BSL_CID_MD5WITHRSA}, {{9, "\52\206\110\206\367\15\1\1\5", BSL_OID_GLOBAL}, "SHA1WITHRSA", BSL_CID_SHA1WITHRSA}, {{7, "\52\206\110\316\70\4\3", BSL_OID_GLOBAL}, "DSAWITHSHA1", BSL_CID_DSAWITHSHA1}, {{7, "\52\206\110\316\75\4\1", BSL_OID_GLOBAL}, "ECDSAWITHSHA1", BSL_CID_ECDSAWITHSHA1}, {{8, "\52\206\110\316\75\4\3\1", BSL_OID_GLOBAL}, "ECDSAWITHSHA224", BSL_CID_ECDSAWITHSHA224}, {{8, "\52\206\110\316\75\4\3\2", BSL_OID_GLOBAL}, "ECDSAWITHSHA256", BSL_CID_ECDSAWITHSHA256}, {{8, "\52\206\110\316\75\4\3\3", BSL_OID_GLOBAL}, "ECDSAWITHSHA384", BSL_CID_ECDSAWITHSHA384}, {{8, "\52\206\110\316\75\4\3\4", BSL_OID_GLOBAL}, "ECDSAWITHSHA512", BSL_CID_ECDSAWITHSHA512}, {{9, "\52\206\110\206\367\15\1\1\13", BSL_OID_GLOBAL}, "SHA256WITHRSA", BSL_CID_SHA256WITHRSAENCRYPTION}, {{9, "\52\206\110\206\367\15\1\1\14", BSL_OID_GLOBAL}, "SHA384WITHRSA", BSL_CID_SHA384WITHRSAENCRYPTION}, {{9, "\52\206\110\206\367\15\1\1\15", BSL_OID_GLOBAL}, "SHA512WITHRSA", BSL_CID_SHA512WITHRSAENCRYPTION}, {{8, "\52\206\110\316\75\3\1\7", BSL_OID_GLOBAL}, "PRIME256V1", BSL_CID_PRIME256V1}, {{9, "\52\206\110\206\367\15\1\5\14", BSL_OID_GLOBAL}, "PBKDF2", BSL_CID_PBKDF2}, {{9, "\52\206\110\206\367\15\1\5\15", BSL_OID_GLOBAL}, "PBES2", BSL_CID_PBES2}, {{9, "\52\206\110\206\367\15\1\11\16", BSL_OID_GLOBAL}, "Requested Extensions", BSL_CID_EXTENSIONREQUEST}, {{3, "\125\4\4", BSL_OID_GLOBAL}, "SN", BSL_CID_AT_SURNAME}, {{3, "\125\4\52", BSL_OID_GLOBAL}, "GN", BSL_CID_AT_GIVENNAME}, {{3, "\125\4\53", BSL_OID_GLOBAL}, "initials", BSL_CID_AT_INITIALS}, {{3, "\125\4\54", BSL_OID_GLOBAL}, "generationQualifier", BSL_CID_AT_GENERATIONQUALIFIER}, {{3, "\125\4\3", BSL_OID_GLOBAL}, "CN", BSL_CID_AT_COMMONNAME}, {{3, "\125\4\7", BSL_OID_GLOBAL}, "L", BSL_CID_AT_LOCALITYNAME}, {{3, "\125\4\10", BSL_OID_GLOBAL}, "ST", BSL_CID_AT_STATEORPROVINCENAME}, {{3, "\125\4\12", BSL_OID_GLOBAL}, "O", BSL_CID_AT_ORGANIZATIONNAME}, {{3, "\125\4\13", BSL_OID_GLOBAL}, "OU", BSL_CID_AT_ORGANIZATIONALUNITNAME}, {{3, "\125\4\14", BSL_OID_GLOBAL}, "title", BSL_CID_AT_TITLE}, {{3, "\125\4\56", BSL_OID_GLOBAL}, "dnQualifier", BSL_CID_AT_DNQUALIFIER}, {{3, "\125\4\6", BSL_OID_GLOBAL}, "C", BSL_CID_AT_COUNTRYNAME}, {{3, "\125\4\5", BSL_OID_GLOBAL}, "serialNumber", BSL_CID_AT_SERIALNUMBER}, {{3, "\125\4\101", BSL_OID_GLOBAL}, "pseudonym", BSL_CID_AT_PSEUDONYM}, {{10, "\11\222\46\211\223\362\54\144\1\31", BSL_OID_GLOBAL}, "DC", BSL_CID_DOMAINCOMPONENT}, {{9, "\52\206\110\206\367\15\1\11\1", BSL_OID_GLOBAL}, "emailAddress", BSL_CID_EMAILADDRESS}, {{3, "\125\35\43", BSL_OID_GLOBAL}, "AuthorityKeyIdentifier", BSL_CID_CE_AUTHORITYKEYIDENTIFIER}, {{3, "\125\35\16", BSL_OID_GLOBAL}, "SubjectKeyIdentifier", BSL_CID_CE_SUBJECTKEYIDENTIFIER}, {{3, "\125\35\17", BSL_OID_GLOBAL}, "KeyUsage", BSL_CID_CE_KEYUSAGE}, {{3, "\125\35\21", BSL_OID_GLOBAL}, "SubjectAltName", BSL_CID_CE_SUBJECTALTNAME}, {{3, "\125\35\23", BSL_OID_GLOBAL}, "BasicConstraints", BSL_CID_CE_BASICCONSTRAINTS}, {{3, "\125\35\37", BSL_OID_GLOBAL}, "CRLDistributionPoints", BSL_CID_CE_CRLDISTRIBUTIONPOINTS}, {{3, "\125\35\45", BSL_OID_GLOBAL}, "ExtendedKeyUsage", BSL_CID_CE_EXTKEYUSAGE}, {{8, "\53\6\1\5\5\7\3\1", BSL_OID_GLOBAL}, "ServerAuth", BSL_CID_KP_SERVERAUTH}, {{8, "\53\6\1\5\5\7\3\2", BSL_OID_GLOBAL}, "ClientAuth", BSL_CID_KP_CLIENTAUTH}, {{8, "\53\6\1\5\5\7\3\3", BSL_OID_GLOBAL}, "CodeSigning", BSL_CID_KP_CODESIGNING}, {{8, "\53\6\1\5\5\7\3\4", BSL_OID_GLOBAL}, "EmailProtection", BSL_CID_KP_EMAILPROTECTION}, {{8, "\53\6\1\5\5\7\3\10", BSL_OID_GLOBAL}, "TimeStamping", BSL_CID_KP_TIMESTAMPING}, {{8, "\53\6\1\5\5\7\3\11", BSL_OID_GLOBAL}, "OSCPSigning", BSL_CID_KP_OCSPSIGNING}, {{3, "\125\35\56", BSL_OID_GLOBAL}, "FreshestCRL", BSL_CID_CE_FRESHESTCRL}, {{3, "\125\35\24", BSL_OID_GLOBAL}, "CrlNumber", BSL_CID_CE_CRLNUMBER}, {{3, "\125\35\34", BSL_OID_GLOBAL}, "IssuingDistributionPoint", BSL_CID_CE_ISSUINGDISTRIBUTIONPOINT}, {{3, "\125\35\33", BSL_OID_GLOBAL}, "DeltaCrlIndicator", BSL_CID_CE_DELTACRLINDICATOR}, {{3, "\125\35\25", BSL_OID_GLOBAL}, "CrlReason", BSL_CID_CE_CRLREASONS}, {{3, "\125\35\35", BSL_OID_GLOBAL}, "CertificateIssuer", BSL_CID_CE_CERTIFICATEISSUER}, {{3, "\125\35\30", BSL_OID_GLOBAL}, "InvalidityDate", BSL_CID_CE_INVALIDITYDATE}, {{11, "\52\206\110\206\367\15\1\14\12\1\1", BSL_OID_GLOBAL}, "keyBag", BSL_CID_KEYBAG}, {{11, "\52\206\110\206\367\15\1\14\12\1\2", BSL_OID_GLOBAL}, "pkcs8shroudedkeyBag", BSL_CID_PKCS8SHROUDEDKEYBAG}, {{11, "\52\206\110\206\367\15\1\14\12\1\3", BSL_OID_GLOBAL}, "certBag", BSL_CID_CERTBAG}, {{11, "\52\206\110\206\367\15\1\14\12\1\4", BSL_OID_GLOBAL}, "crlBag", BSL_CID_CRLBAG}, {{11, "\52\206\110\206\367\15\1\14\12\1\5", BSL_OID_GLOBAL}, "secretBag", BSL_CID_SECRETBAG}, {{11, "\52\206\110\206\367\15\1\14\12\1\6", BSL_OID_GLOBAL}, "safeContent", BSL_CID_SAFECONTENTSBAG}, {{10, "\52\206\110\206\367\15\1\11\26\1", BSL_OID_GLOBAL}, "x509Certificate", BSL_CID_X509CERTIFICATE}, {{9, "\52\206\110\206\367\15\1\11\24", BSL_OID_GLOBAL}, "friendlyName", BSL_CID_FRIENDLYNAME}, {{9, "\52\206\110\206\367\15\1\11\25", BSL_OID_GLOBAL}, "localKeyId", BSL_CID_LOCALKEYID}, {{9, "\52\206\110\206\367\15\1\7\1", BSL_OID_GLOBAL}, "data", BSL_CID_PKCS7_SIMPLEDATA}, {{9, "\52\206\110\206\367\15\1\7\6", BSL_OID_GLOBAL}, "encryptedData", BSL_CID_PKCS7_ENCRYPTEDDATA}, {{5, "\53\201\4\0\42", BSL_OID_GLOBAL}, "SECP384R1", BSL_CID_SECP384R1}, {{5, "\53\201\4\0\43", BSL_OID_GLOBAL}, "SECP521R1", BSL_CID_SECP521R1}, {{8, "\52\201\34\317\125\1\203\21", BSL_OID_GLOBAL}, "SM3", BSL_CID_SM3}, {{10, "\52\201\34\317\125\1\203\21\3\1", BSL_OID_GLOBAL}, "HMAC-SM3", BSL_CID_HMAC_SM3}, {{8, "\52\201\34\317\125\1\203\165", BSL_OID_GLOBAL}, "SM2DSAWITHSM3", BSL_CID_SM2DSAWITHSM3}, {{8, "\52\201\34\317\125\1\203\166", BSL_OID_GLOBAL}, "SM2DSAWITHSHA1", BSL_CID_SM2DSAWITHSHA1}, {{8, "\52\201\34\317\125\1\203\167", BSL_OID_GLOBAL}, "SM2DSAWITHSHA256", BSL_CID_SM2DSAWITHSHA256}, {{8, "\52\201\34\317\125\1\202\55", BSL_OID_GLOBAL}, "SM2PRIME256", BSL_CID_SM2PRIME256}, {{3, "\125\4\11", BSL_OID_GLOBAL}, "STREET", BSL_CID_AT_STREETADDRESS}, {{5, "\53\201\4\0\41", BSL_OID_GLOBAL}, "PRIME224", BSL_CID_NIST_PRIME224}, {{3, "\53\145\160", BSL_OID_GLOBAL}, "ED25519", BSL_CID_ED25519}, {{9, "\52\206\110\206\367\15\1\1\12", BSL_OID_GLOBAL}, "RSASSAPSS", BSL_CID_RSASSAPSS}, {{9, "\52\206\110\206\367\15\1\1\10", BSL_OID_GLOBAL}, "MGF1", BSL_CID_MGF1}, {{8, "\52\201\34\317\125\1\150\2", BSL_OID_GLOBAL}, "SM4-CBC", BSL_CID_SM4_CBC}, {{8, "\52\201\34\317\125\1\203\170", BSL_OID_GLOBAL}, "SM3WITHRSA", BSL_CID_SM3WITHRSAENCRYPTION}, {{9, "\140\206\110\1\145\3\4\3\2", BSL_OID_GLOBAL}, "DSAWITHSHA256", BSL_CID_DSAWITHSHA256}, {{9, "\140\206\110\1\145\3\4\3\1", BSL_OID_GLOBAL}, "DSAWITHSHA224", BSL_CID_DSAWITHSHA224}, {{9, "\140\206\110\1\145\3\4\3\3", BSL_OID_GLOBAL}, "DSAWITHSHA384", BSL_CID_DSAWITHSHA384}, {{9, "\140\206\110\1\145\3\4\3\4", BSL_OID_GLOBAL}, "DSAWITHSHA512", BSL_CID_DSAWITHSHA512}, {{9, "\52\206\110\206\367\15\1\1\16", BSL_OID_GLOBAL}, "SHA224WITHRSA", BSL_CID_SHA224WITHRSAENCRYPTION}, {{9, "\140\206\110\1\145\3\4\2\7", BSL_OID_GLOBAL}, "SHA3-224", BSL_CID_SHA3_224}, {{9, "\140\206\110\1\145\3\4\2\10", BSL_OID_GLOBAL}, "SHA3-256", BSL_CID_SHA3_256}, {{9, "\140\206\110\1\145\3\4\2\11", BSL_OID_GLOBAL}, "SHA3-384", BSL_CID_SHA3_384}, {{9, "\140\206\110\1\145\3\4\2\12", BSL_OID_GLOBAL}, "SHA3-512", BSL_CID_SHA3_512}, {{9, "\140\206\110\1\145\3\4\2\13", BSL_OID_GLOBAL}, "SHAKE128", BSL_CID_SHAKE128}, {{9, "\140\206\110\1\145\3\4\2\14", BSL_OID_GLOBAL}, "SHAKE256", BSL_CID_SHAKE256}, {{8, "\53\157\2\214\123\0\1\1", BSL_OID_GLOBAL}, "CID_AES128_XTS", BSL_CID_AES128_XTS}, {{8, "\53\157\2\214\123\0\1\2", BSL_OID_GLOBAL}, "CID_AES256_XTS", BSL_CID_AES256_XTS}, {{8, "\52\201\34\317\125\1\150\12", BSL_OID_GLOBAL}, "CID_SM4_XTS", BSL_CID_SM4_XTS}, {{8, "\52\201\34\317\125\1\150\7", BSL_OID_GLOBAL}, "CID_SM4_CTR", BSL_CID_SM4_CTR}, {{8, "\52\201\34\317\125\1\150\10", BSL_OID_GLOBAL}, "CID_SM4_GCM", BSL_CID_SM4_GCM}, {{8, "\52\201\34\317\125\1\150\4", BSL_OID_GLOBAL}, "CID_SM4_CFB", BSL_CID_SM4_CFB}, {{8, "\52\201\34\317\125\1\150\3", BSL_OID_GLOBAL}, "CID_SM4_OFB", BSL_CID_SM4_OFB}, {{9, "\53\44\3\3\2\10\1\1\7", BSL_OID_GLOBAL}, "BRAINPOOLP256R1", BSL_CID_ECC_BRAINPOOLP256R1}, {{9, "\53\44\3\3\2\10\1\1\13", BSL_OID_GLOBAL}, "BRAINPOOLP384R1", BSL_CID_ECC_BRAINPOOLP384R1}, {{9, "\53\44\3\3\2\10\1\1\15", BSL_OID_GLOBAL}, "BRAINPOOLP512R1", BSL_CID_ECC_BRAINPOOLP512R1}, {{7, "\52\206\110\316\75\2\1", BSL_OID_GLOBAL}, "EC-PUBLICKEY", BSL_CID_EC_PUBLICKEY}, // ecc subkey {{10, "\11\222\46\211\223\362\54\144\1\1", BSL_OID_GLOBAL}, "UID", BSL_CID_AT_USERID}, {{3, "\125\35\22", BSL_OID_GLOBAL}, "IssuerAlternativeName", BSL_CID_CE_ISSUERALTERNATIVENAME}, {{8, "\53\6\1\5\5\7\1\1", BSL_OID_GLOBAL}, "AuthorityInformationAccess", BSL_CID_CE_AUTHORITYINFORMATIONACCESS}, }; uint32_t g_tableSize = (uint32_t)sizeof(g_oidTable)/sizeof(g_oidTable[0]); #ifdef HITLS_BSL_HASH static void FreeBslOidInfo(void *data) { if (data == NULL) { return; } BslOidInfo *oidInfo = (BslOidInfo *)data; BSL_SAL_Free(oidInfo->strOid.octs); BSL_SAL_Free((char *)(uintptr_t)oidInfo->oidName); BSL_SAL_Free(oidInfo); } static void InitOidHashTableOnce(void) { int32_t ret = BSL_SAL_ThreadLockNew(&g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return; } ListDupFreeFuncPair valueFunc = {NULL, FreeBslOidInfo}; g_oidHashTable = BSL_HASH_Create(BSL_OBJ_HASH_BKT_SIZE, NULL, NULL, NULL, &valueFunc); if (g_oidHashTable == NULL) { (void)BSL_SAL_ThreadLockFree(g_oidHashRwLock); g_oidHashRwLock = NULL; BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); } } #endif // HITLS_BSL_HASH static int32_t GetOidIndex(int32_t inputCid) { int32_t left = 0; int32_t right = g_tableSize - 1; while (left <= right) { int32_t mid = (right - left) / 2 + left; int32_t cid = g_oidTable[mid].cid; if (cid == inputCid) { return mid; } else if (cid > inputCid) { right = mid - 1; } else { left = mid + 1; } } return -1; } BslCid BSL_OBJ_GetCID(const BslOidString *oidstr) { if (oidstr == NULL || oidstr->octs == NULL) { return BSL_CID_UNKNOWN; } /* First, search in the g_oidTable */ for (uint32_t i = 0; i < g_tableSize; i++) { if (g_oidTable[i].strOid.octetLen == oidstr->octetLen) { if (memcmp(g_oidTable[i].strOid.octs, oidstr->octs, oidstr->octetLen) == 0) { return g_oidTable[i].cid; } } } #ifndef HITLS_BSL_HASH return BSL_CID_UNKNOWN; #else if (g_oidHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_CID_UNKNOWN; } /* Second, search in the g_oidHashTable with read lock */ BslCid cid = BSL_CID_UNKNOWN; int32_t ret = BSL_SAL_ThreadReadLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return BSL_CID_UNKNOWN; } /* Since g_oidHashTable is keyed by cid, we need to iterate through all entries */ BSL_HASH_Iterator iter = BSL_HASH_IterBegin(g_oidHashTable); BSL_HASH_Iterator end = BSL_HASH_IterEnd(g_oidHashTable); while (iter != end) { BslOidInfo *oidInfo = (BslOidInfo *)BSL_HASH_IterValue(g_oidHashTable, iter); if (oidInfo != NULL && oidInfo->strOid.octetLen == oidstr->octetLen && memcmp(oidInfo->strOid.octs, oidstr->octs, oidstr->octetLen) == 0) { cid = oidInfo->cid; break; } iter = BSL_HASH_IterNext(g_oidHashTable, iter); } (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); return cid; #endif // HITLS_BSL_HASH } BslOidString *BSL_OBJ_GetOID(BslCid ulCID) { if (ulCID == BSL_CID_UNKNOWN) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } /* First, search in the g_oidTable */ int32_t index = GetOidIndex(ulCID); if (index != -1) { return &g_oidTable[index].strOid; } #ifndef HITLS_BSL_HASH return NULL; #else /* Initialize hash table if needed */ if (g_oidHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return NULL; } /* Second, search in the g_oidHashTable with read lock */ BslOidInfo *oidInfo = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } /* Since g_oidHashTable is keyed by cid, we can directly look up the entry */ ret = BSL_HASH_At(g_oidHashTable, (uintptr_t)ulCID, (uintptr_t *)&oidInfo); (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); BslOidString *oidString = (ret == BSL_SUCCESS && oidInfo != NULL) ? &oidInfo->strOid : NULL; if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_OBJ_ERR_FIND_HASH_TABLE); } return oidString; #endif // HITLS_BSL_HASH } const char *BSL_OBJ_GetOidNameFromOid(const BslOidString *oid) { if (oid == NULL || oid->octs == NULL) { return NULL; } /* First, search in the g_oidTable */ for (uint32_t i = 0; i < g_tableSize; i++) { if (g_oidTable[i].strOid.octetLen == oid->octetLen) { if (memcmp(g_oidTable[i].strOid.octs, oid->octs, oid->octetLen) == 0) { return g_oidTable[i].oidName; } } } #ifndef HITLS_BSL_HASH return NULL; #else if (g_oidHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return NULL; } /* Second, search in the g_oidHashTable with read lock */ const char *oidName = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } /* Since g_oidHashTable is keyed by cid, we need to iterate through all entries */ BSL_HASH_Iterator iter = BSL_HASH_IterBegin(g_oidHashTable); BSL_HASH_Iterator end = BSL_HASH_IterEnd(g_oidHashTable); while (iter != end) { BslOidInfo *oidInfo = (BslOidInfo *)BSL_HASH_IterValue(g_oidHashTable, iter); if (oidInfo != NULL && oidInfo->strOid.octetLen == oid->octetLen && memcmp(oidInfo->strOid.octs, oid->octs, oid->octetLen) == 0) { oidName = oidInfo->oidName; break; } iter = BSL_HASH_IterNext(g_oidHashTable, iter); } (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); return oidName; #endif // HITLS_BSL_HASH } #if defined(HITLS_PKI_X509) || defined(HITLS_PKI_INFO) /** * RFC 5280: A.1. Explicitly Tagged Module, 1988 Syntax * -- Upper Bounds */ static const BslAsn1DnInfo g_asn1DnTab[] = { {BSL_CID_AT_COMMONNAME, 1, 64, "CN"}, // ub-common-name INTEGER ::= 64 {BSL_CID_AT_SURNAME, 1, 40, "SN"}, // ub-surname-length INTEGER ::= 40 {BSL_CID_AT_SERIALNUMBER, 1, 64, "serialNumber"}, // ub-serial-number INTEGER ::= 64 {BSL_CID_AT_COUNTRYNAME, 2, 2, "C"}, // ub-country-name-alpha-length INTEGER ::= 2 {BSL_CID_AT_LOCALITYNAME, 1, 128, "L"}, // ub-locality-name INTEGER ::= 128 {BSL_CID_AT_STATEORPROVINCENAME, 1, 128, "ST"}, // ub-state-name INTEGER ::= 128 {BSL_CID_AT_STREETADDRESS, 1, -1, "street"}, // no limited {BSL_CID_AT_ORGANIZATIONNAME, 1, 64, "O"}, // ub-organization-name INTEGER ::= 64 {BSL_CID_AT_ORGANIZATIONALUNITNAME, 1, 64, "OU"}, // ub-organizational-unit-name INTEGER ::= 64 {BSL_CID_AT_TITLE, 1, 64, "title"}, // ub-title INTEGER ::= 64 {BSL_CID_AT_GIVENNAME, 1, 32768, "GN"}, // ub-name INTEGER ::= 32768 {BSL_CID_AT_INITIALS, 1, 32768, "initials"}, // ub-name INTEGER ::= 32768 {BSL_CID_AT_GENERATIONQUALIFIER, 1, 32768, "generationQualifier"}, // ub-name INTEGER ::= 32768 {BSL_CID_AT_DNQUALIFIER, 1, -1, "dnQualifier"}, // no limited {BSL_CID_AT_PSEUDONYM, 1, 128, "pseudonym"}, // ub-pseudonym INTEGER ::= 128 {BSL_CID_DOMAINCOMPONENT, 1, -1, "DC"}, // no limited {BSL_CID_AT_USERID, 1, 256, "UID"}, // RFC1274 }; #define BSL_DN_STR_CNT (sizeof(g_asn1DnTab) / sizeof(g_asn1DnTab[0])) const BslAsn1DnInfo *BSL_OBJ_GetDnInfoFromShortName(const char *shortName) { for (size_t i = 0; i < BSL_DN_STR_CNT; i++) { if (strcmp(g_asn1DnTab[i].shortName, shortName) == 0) { return &g_asn1DnTab[i]; } } return NULL; } const BslAsn1DnInfo *BSL_OBJ_GetDnInfoFromCid(BslCid cid) { for (size_t i = 0; i < sizeof(g_asn1DnTab) / sizeof(g_asn1DnTab[0]); i++) { if (cid == g_asn1DnTab[i].cid) { return &g_asn1DnTab[i]; } } return NULL; } #endif // HITLS_PKI_X509 || HITLS_PKI_INFO #if defined(HITLS_PKI_X509) || defined(HITLS_PKI_INFO) || defined(HITLS_CRYPTO_KEY_INFO) const char *BSL_OBJ_GetOidNameFromCID(BslCid ulCID) { if (ulCID >= BSL_CID_MAX) { /* check if ulCID is within range */ return NULL; } int32_t index = GetOidIndex(ulCID); if (index == -1) { return NULL; } return g_oidTable[index].oidName; } #endif // HITLS_PKI_X509 || HITLS_PKI_INFO || HITLS_CRYPTO_KEY_INFO #ifdef HITLS_BSL_HASH static int32_t BslOidStringCopy(const BslOidString *srcOidStr, BslOidString *oidString) { oidString->octs = BSL_SAL_Dump(srcOidStr->octs, srcOidStr->octetLen); if (oidString->octs == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } oidString->octetLen = srcOidStr->octetLen; oidString->flags = srcOidStr->flags; return BSL_SUCCESS; } static bool IsOidCidInStaticTable(int32_t cid) { for (uint32_t i = 0; i < g_tableSize; i++) { if ((int32_t)g_oidTable[i].cid == cid) { return true; } } return false; } static int32_t IsOidCidInHashTable(int32_t cid) { BslOidInfo *oidInfo = NULL; int32_t ret = BSL_SAL_ThreadReadLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BSL_HASH_At(g_oidHashTable, (uintptr_t)cid, (uintptr_t *)&oidInfo); (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); return ret; } static int32_t CreateOidInfo(const BslOidString *oid, const char *oidName, int32_t cid, BslOidInfo **newOidInfo) { BslOidInfo *oidInfo = (BslOidInfo *)BSL_SAL_Calloc(1, sizeof(BslOidInfo)); if (oidInfo == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } int32_t ret = BslOidStringCopy(oid, &oidInfo->strOid); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); BSL_SAL_Free(oidInfo); return ret; } oidInfo->oidName = BSL_SAL_Dump(oidName, (uint32_t)strlen(oidName) + 1); if (oidInfo->oidName == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); BSL_SAL_Free(oidInfo->strOid.octs); BSL_SAL_Free(oidInfo); return BSL_MALLOC_FAIL; } oidInfo->cid = cid; *newOidInfo = oidInfo; return BSL_SUCCESS; } // Insert OID info into hash table with write lock static int32_t InsertOidInfoToHashTable(int32_t cid, BslOidInfo *oidInfo) { int32_t ret = BSL_SAL_ThreadWriteLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } BslOidInfo *checkInfo = NULL; ret = BSL_HASH_At(g_oidHashTable, (uintptr_t)cid, (uintptr_t *)&checkInfo); if (ret == BSL_SUCCESS) { (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); return BSL_SUCCESS; } ret = BSL_HASH_Insert(g_oidHashTable, (uintptr_t)cid, sizeof(int32_t), (uintptr_t)oidInfo, sizeof(BslOidInfo)); (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); return ret; } // Main function for creating and registering OIDs int32_t BSL_OBJ_Create(char *octs, uint32_t octetLen, const char *oidName, int32_t cid) { if (octs == NULL || octetLen == 0 || oidName == NULL || cid == BSL_CID_UNKNOWN) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (IsOidCidInStaticTable(cid)) { return BSL_SUCCESS; } const BslOidString oid = { .octs = octs, .octetLen = octetLen, .flags = 6 }; int32_t ret = BSL_SAL_ThreadRunOnce(&g_oidHashInitOnce, InitOidHashTableOnce); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (g_oidHashTable == NULL) { BSL_ERR_PUSH_ERROR(BSL_OBJ_INVALID_HASH_TABLE); return BSL_OBJ_INVALID_HASH_TABLE; } ret = IsOidCidInHashTable(cid); if (ret == BSL_SUCCESS) { return BSL_SUCCESS; } BslOidInfo *oidInfo = NULL; ret = CreateOidInfo(&oid, oidName, cid, &oidInfo); if (ret != BSL_SUCCESS) { return ret; } ret = InsertOidInfoToHashTable(cid, oidInfo); if (ret != BSL_SUCCESS) { FreeBslOidInfo(oidInfo); return ret; } return BSL_SUCCESS; } void BSL_OBJ_FreeHashTable(void) { if (g_oidHashTable != NULL) { int32_t ret = BSL_SAL_ThreadWriteLock(g_oidHashRwLock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return; } BSL_HASH_Destory(g_oidHashTable); g_oidHashTable = NULL; (void)BSL_SAL_ThreadUnlock(g_oidHashRwLock); if (g_oidHashRwLock != NULL) { (void)BSL_SAL_ThreadLockFree(g_oidHashRwLock); g_oidHashRwLock = NULL; } g_oidHashInitOnce = BSL_SAL_ONCE_INIT; } } #endif // HITLS_BSL_HASH /* * Conversion Rules: * The first byte represents the first two nodes: X.Y, where X = first byte / 40, Y = first byte % 40. * Subsequent nodes use variable length encoding (Base 128), where the highest bit of each byte indicates * whether there are subsequent bytes (1 indicates continuation, 0 indicates end). * Detailed conversion process: * 1、Split the first two nodes, the first byte is decomposed into two numbers: first_node and second_node, * first_node = byte_value / 40, second_node = byte_value % 40. * 2、Decoding subsequent nodes, Each node may be composed of multiple bytes, with the most significant bit (MSB) * of each byte being the continuation flag and the remaining 7 bits being a part of the actual value, * Multiple 7-bit groups are combined in big endian order. */ char *BSL_OBJ_GetOidNumericString(const uint8_t *oid, uint32_t len) { if (oid == NULL || len < 1 || oid[0] > BSL_OBJ_ARCS_MAX) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } char buffer[256] = {0}; if (snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, "%d.%d", oid[0] / BSL_OBJ_ARCS_Y_MAX, oid[0] % BSL_OBJ_ARCS_Y_MAX) < 0) { return NULL; } uint64_t value = 0; uint32_t currentPos = strlen(buffer); for (uint32_t i = 1; i < len; i++) { if (value > (UINT64_MAX >> 7)) { /* Overflow check */ BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } if ((value == 0) && ((oid[i]) == 0x80)) { /* Any value must be encoded with the minimum number of bytes. No unnecessary or meaningless leading bytes are allowed */ BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } value = (value << 7) | (oid[i] & 0x7F); if (!(oid[i] & 0x80)) { char temp[20] = {0}; int32_t tempLen = snprintf_s(temp, sizeof(temp), sizeof(temp) - 1, ".%lu", value); if (tempLen < 0) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return NULL; } if (currentPos + tempLen >= sizeof(buffer)) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return NULL; } if (memcpy_s(buffer + currentPos, tempLen, temp, tempLen) != 0) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return NULL; } currentPos += tempLen; value = 0; } } if (value != 0) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } return BSL_SAL_Dump(buffer, sizeof(buffer)); } static void BslEncodeOidPart(uint64_t num, uint8_t *output, uint32_t *offset) { if (num < 0x80) { output[*offset] = num &0x7F; (*offset)++; } else { uint8_t temp[10]; // The data of uint64_t requires up to 10 bytes when encode in ASN1. int32_t i = 0; uint64_t t = num; while (t > 0) { temp[i] = (t & 0x7F) | 0x80; i++; t >>= 7; // Process 7 bits each time. } temp[0] &= 0x7F; for (int32_t j = i - 1; j >= 0; j--) { output[*offset] = temp[j]; (*offset)++; } } } static bool BslEncodeOidValueCheck(uint64_t *parts, uint32_t count) { // At least 2 pieces of data are required. if (count < 2 || parts[0] > BSL_OBJ_ARCS_X_MAX) { return false; } if (parts[1] >= BSL_OBJ_ARCS_Y_MAX) { return false; } return true; } #define MAX_OID_PARTS_LEN 128 uint8_t *BSL_OBJ_GetOidFromNumericString(const char *oid, uint32_t len, uint32_t *outLen) { if (len == 0 || oid == NULL || oid[0] == '.' || outLen == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } uint64_t parts[MAX_OID_PARTS_LEN]; uint32_t count = 0; parts[count] = 0; for (uint32_t i = 0; i < len; i++) { if (oid[i] > '9' || (oid[i] < '0' && oid[i] != '.')) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } if ((i < len - 1) && oid[i] == '.' && oid[i + 1] == '.') { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } if (oid[i] != '.') { // Convert decimal string to number. if (parts[count] > (UINT64_MAX - (oid[i] - '0')) / 10) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } parts[count] = parts[count] * 10 + (oid[i] - '0'); } else { count++; if (count >= MAX_OID_PARTS_LEN) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } parts[count] = 0; } } count++; if (BslEncodeOidValueCheck(parts, count) != true) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } uint32_t offset = 0; // The data of uint64_t requires up to 10 bytes when encode in ASN1. uint8_t *outBuf = BSL_SAL_Malloc(count * 10); if (outBuf == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } outBuf[0] = (uint8_t)(parts[0] * BSL_OBJ_ARCS_Y_MAX + parts[1]); offset++; for (uint32_t i = 2; i < count; i++) { BslEncodeOidPart(parts[i], outBuf, &offset); } *outLen = offset; return outBuf; } #endif
2302_82127028/openHiTLS-examples
bsl/obj/src/bsl_obj.c
C
unknown
30,815
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_PARAMS #include "bsl_errno.h" #include "securec.h" #include "bsl_err_internal.h" #include "bsl_params.h" #include "bsl_list.h" typedef struct { int32_t key; uint32_t type; void *value; uint32_t len; uint32_t allocLen; union { bool flag; int32_t i32; uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } num; int32_t flag; } BSL_PARAM_MAKER_DEF; #define PARAM_MAKER_MALLOCED_VALUE 1 struct BslParamMaker { uint32_t valueLen; BslList *params; }; static void PARAM_MAKER_DEF_Free(BSL_PARAM_MAKER_DEF *paramMakerDef) { if (paramMakerDef == NULL) { return; } if ((paramMakerDef->flag & PARAM_MAKER_MALLOCED_VALUE) != 0) { BSL_SAL_FREE(paramMakerDef->value); } BSL_SAL_Free(paramMakerDef); } BSL_ParamMaker *BSL_PARAM_MAKER_New(void) { BSL_ParamMaker *maker = BSL_SAL_Calloc(1, sizeof(BSL_ParamMaker)); if (maker == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } maker->params = BSL_LIST_New(sizeof(BSL_PARAM_MAKER_DEF)); if (maker->params == NULL) { BSL_SAL_Free(maker); BSL_ERR_PUSH_ERROR(BSL_LIST_MALLOC_FAIL); return NULL; } return maker; } static int32_t BSL_PARAM_MAKER_CheckNumberLen(uint32_t type, uint32_t len) { switch (type) { case BSL_PARAM_TYPE_UINT8: if (len < sizeof(uint8_t)) { return BSL_INVALID_ARG; } break; case BSL_PARAM_TYPE_UINT16: if (len < sizeof(uint16_t)) { return BSL_INVALID_ARG; } break; case BSL_PARAM_TYPE_UINT32: if (len < sizeof(uint32_t)) { return BSL_INVALID_ARG; } break; case BSL_PARAM_TYPE_INT32: if (len < sizeof(int32_t)) { return BSL_INVALID_ARG; } break; case BSL_PARAM_TYPE_BOOL: if (len < sizeof(bool)) { return BSL_INVALID_ARG; } break; default: return BSL_PARAMS_INVALID_TYPE; } return BSL_SUCCESS; } static int32_t BSL_PARAM_MAKER_CheckInput(const BSL_ParamMaker *maker, const void *value, uint32_t len) { if (maker == NULL || maker->params == NULL || (value == NULL && len != 0)) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (((uint32_t)maker->params->count + 1) > UINT32_MAX / sizeof(BSL_Param)) { return BSL_PARAMS_OUT_LIMIT; } return BSL_SUCCESS; } int32_t BSL_PARAM_MAKER_PushValue(BSL_ParamMaker *maker, int32_t key, uint32_t type, void *value, uint32_t len) { int32_t ret = BSL_PARAM_MAKER_CheckInput(maker, value, len); if (ret != BSL_SUCCESS) { return ret; } BSL_PARAM_MAKER_DEF *paramMakerDef = BSL_SAL_Calloc(1, sizeof(BSL_PARAM_MAKER_DEF)); if (paramMakerDef == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } paramMakerDef->key = key; paramMakerDef->type = type; paramMakerDef->len = len; switch (type) { case BSL_PARAM_TYPE_UINT8: case BSL_PARAM_TYPE_UINT16: case BSL_PARAM_TYPE_UINT32: case BSL_PARAM_TYPE_INT32: case BSL_PARAM_TYPE_BOOL: ret = BSL_PARAM_MAKER_CheckNumberLen(type, len); if (ret != BSL_SUCCESS) { goto exit; } (void)memcpy_s(&paramMakerDef->num, len, value, len); paramMakerDef->allocLen = len; break; case BSL_PARAM_TYPE_UINT32_PTR: case BSL_PARAM_TYPE_OCTETS_PTR: case BSL_PARAM_TYPE_FUNC_PTR: case BSL_PARAM_TYPE_CTX_PTR: paramMakerDef->value = value; paramMakerDef->allocLen = 0; break; case BSL_PARAM_TYPE_UTF8_STR: paramMakerDef->value = value; paramMakerDef->allocLen = len + 1; break; case BSL_PARAM_TYPE_OCTETS: paramMakerDef->value = value; paramMakerDef->allocLen = len; break; default: ret = BSL_PARAMS_INVALID_TYPE; goto exit; } maker->valueLen += paramMakerDef->allocLen; ret = BSL_LIST_AddElement(maker->params, paramMakerDef, BSL_LIST_POS_END); exit: if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); PARAM_MAKER_DEF_Free(paramMakerDef); } return ret; } int32_t BSL_PARAM_MAKER_DeepPushValue(BSL_ParamMaker *maker, int32_t key, uint32_t type, void *value, uint32_t len) { int32_t ret = BSL_PARAM_MAKER_CheckInput(maker, value, len); if (ret != BSL_SUCCESS) { return ret; } BSL_PARAM_MAKER_DEF *paramMakerDef = BSL_SAL_Calloc(1, sizeof(BSL_PARAM_MAKER_DEF)); if (paramMakerDef == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } paramMakerDef->key = key; paramMakerDef->type = type; paramMakerDef->len = len; uint32_t allocLen = 0; switch (type) { case BSL_PARAM_TYPE_UTF8_STR: allocLen = len + 1; break; case BSL_PARAM_TYPE_OCTETS: allocLen = len; break; default: ret = BSL_PARAMS_INVALID_TYPE; BSL_ERR_PUSH_ERROR(ret); goto exit; } paramMakerDef->value = BSL_SAL_Malloc(len); if (paramMakerDef->value == NULL) { ret = BSL_MALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto exit; } paramMakerDef->flag |= PARAM_MAKER_MALLOCED_VALUE; (void)memcpy_s(paramMakerDef->value, len, value, len); paramMakerDef->allocLen = allocLen; maker->valueLen += paramMakerDef->allocLen; ret = BSL_LIST_AddElement(maker->params, paramMakerDef, BSL_LIST_POS_END); exit: if (ret != BSL_SUCCESS) { PARAM_MAKER_DEF_Free(paramMakerDef); } return ret; } static int32_t BSL_PARAM_MAKER_NumberConvert(BSL_PARAM_MAKER_DEF *paramMakerDef, BSL_Param *params, int32_t i, uint8_t **valueIndex) { uint8_t *value = *valueIndex; (void)memcpy_s(value, paramMakerDef->len, &paramMakerDef->num, paramMakerDef->len); *valueIndex += paramMakerDef->allocLen; return BSL_PARAM_InitValue(&params[i], paramMakerDef->key, paramMakerDef->type, value, paramMakerDef->len); } static int32_t BSL_PARAM_MAKER_PointerConvert(BSL_PARAM_MAKER_DEF *paramMakerDef, BSL_Param *params, int32_t i) { void *value = paramMakerDef->value; return BSL_PARAM_InitValue(&params[i], paramMakerDef->key, paramMakerDef->type, value, paramMakerDef->len); } static int32_t BSL_PARAM_MAKER_StringConvert(BSL_PARAM_MAKER_DEF *paramMakerDef, BSL_Param *params, int32_t i, uint8_t **valueIndex) { uint8_t *value = *valueIndex; if (paramMakerDef->value != NULL) { (void)memcpy_s(value, paramMakerDef->len, paramMakerDef->value, paramMakerDef->len); } else { (void)memset_s(value, paramMakerDef->len, 0, paramMakerDef->len); } if (paramMakerDef->type == BSL_PARAM_TYPE_UTF8_STR) { ((char *)(value))[paramMakerDef->len] = '\0'; } *valueIndex += paramMakerDef->allocLen; return BSL_PARAM_InitValue(&params[i], paramMakerDef->key, paramMakerDef->type, value, paramMakerDef->len); } static int32_t BSL_PARAM_MAKER_CalculateSize(BSL_ParamMaker *maker, uint32_t *paramSize) { if (maker == NULL || maker->params == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BslList *list = maker->params; *paramSize = ((uint32_t)list->count + 1) * sizeof(BSL_Param); if (*paramSize > UINT32_MAX - maker->valueLen) { return BSL_PARAMS_OUT_LIMIT; } else { *paramSize += maker->valueLen; } return BSL_SUCCESS; } BSL_Param *BSL_PARAM_MAKER_ToParam(BSL_ParamMaker *maker) { uint32_t paramSize = 0; if (BSL_PARAM_MAKER_CalculateSize(maker, &paramSize) != BSL_SUCCESS) { return NULL; } BSL_Param *params = BSL_SAL_Calloc(1, paramSize); if (params == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } BslList *list = maker->params; uint8_t *valueIndex = (uint8_t *)(list->count + 1 + params); BSL_PARAM_MAKER_DEF **paramMakerDef = BSL_LIST_First(list); int i = 0; while (paramMakerDef != NULL) { int32_t ret; switch ((*paramMakerDef)->type) { case BSL_PARAM_TYPE_UINT8: case BSL_PARAM_TYPE_UINT16: case BSL_PARAM_TYPE_UINT32: case BSL_PARAM_TYPE_INT32: case BSL_PARAM_TYPE_BOOL: ret = BSL_PARAM_MAKER_NumberConvert(*paramMakerDef, params, i++, &valueIndex); break; case BSL_PARAM_TYPE_UINT32_PTR: case BSL_PARAM_TYPE_FUNC_PTR: case BSL_PARAM_TYPE_CTX_PTR: case BSL_PARAM_TYPE_OCTETS_PTR: ret = BSL_PARAM_MAKER_PointerConvert(*paramMakerDef, params, i++); break; case BSL_PARAM_TYPE_OCTETS: case BSL_PARAM_TYPE_UTF8_STR: default: ret = BSL_PARAM_MAKER_StringConvert(*paramMakerDef, params, i++, &valueIndex); break; } if (ret != BSL_SUCCESS) { BSL_SAL_Free(params); return NULL; } paramMakerDef = BSL_LIST_Next(list); } return params; } void BSL_PARAM_MAKER_Free(BSL_ParamMaker *maker) { if (maker == NULL) { return; } BSL_LIST_FREE(maker->params, (BSL_LIST_PFUNC_FREE)PARAM_MAKER_DEF_Free); BSL_SAL_Free(maker); maker = NULL; } #endif
2302_82127028/openHiTLS-examples
bsl/params/src/bsl_param_maker.c
C
unknown
10,255
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_PARAMS #include "bsl_errno.h" #include "securec.h" #include "bsl_err_internal.h" #include "bsl_params.h" #define BSL_PARAM_MAX_NUMBER 1000 int32_t BSL_PARAM_InitValue(BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t valueLen) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return BSL_PARAMS_INVALID_KEY; } if (param == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (type != BSL_PARAM_TYPE_FUNC_PTR && type != BSL_PARAM_TYPE_CTX_PTR) { /* Parameter validation: param cannot be NULL, if val is NULL, valueLen must be 0 */ if (val == NULL && valueLen != 0) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } } switch (type) { case BSL_PARAM_TYPE_UINT8: case BSL_PARAM_TYPE_UINT16: case BSL_PARAM_TYPE_UINT32: case BSL_PARAM_TYPE_UINT64: case BSL_PARAM_TYPE_OCTETS: case BSL_PARAM_TYPE_BOOL: case BSL_PARAM_TYPE_UINT32_PTR: case BSL_PARAM_TYPE_FUNC_PTR: case BSL_PARAM_TYPE_CTX_PTR: case BSL_PARAM_TYPE_INT32: case BSL_PARAM_TYPE_OCTETS_PTR: case BSL_PARAM_TYPE_UTF8_STR: param->value = val; param->valueLen = valueLen; param->valueType = type; param->key = key; param->useLen = 0; return BSL_SUCCESS; default: BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_TYPE); return BSL_PARAMS_INVALID_TYPE; } } static int32_t ParamCheck(BSL_Param *param, int32_t key, uint32_t type) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return BSL_PARAMS_INVALID_KEY; } if (param == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (param->key != key || param->valueType != type) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_MISMATCH); return BSL_PARAMS_MISMATCH; } return BSL_SUCCESS; } static int32_t SetOtherValues(BSL_Param *param, uint32_t type, void *val, uint32_t len) { if (param->valueLen != len || val == NULL || param->value == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } switch (type) { case BSL_PARAM_TYPE_UINT8: *(uint8_t *)param->value = *(uint8_t *)val; param->useLen = len; return BSL_SUCCESS; case BSL_PARAM_TYPE_UINT16: *(uint16_t *)param->value = *(uint16_t *)val; param->useLen = len; return BSL_SUCCESS; case BSL_PARAM_TYPE_UINT32: *(uint32_t *)param->value = *(uint32_t *)val; param->useLen = len; return BSL_SUCCESS; case BSL_PARAM_TYPE_UINT64: *(uint64_t *)param->value = *(uint64_t *)val; param->useLen = len; return BSL_SUCCESS; case BSL_PARAM_TYPE_BOOL: *(bool *)param->value = *(bool *)val; param->useLen = len; return BSL_SUCCESS; default: BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_TYPE); return BSL_PARAMS_INVALID_TYPE; } } int32_t BSL_PARAM_SetValue(BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t len) { int32_t ret = ParamCheck(param, key, type); if (ret != BSL_SUCCESS) { return ret; } switch (type) { case BSL_PARAM_TYPE_OCTETS_PTR: case BSL_PARAM_TYPE_FUNC_PTR: case BSL_PARAM_TYPE_CTX_PTR: param->value = val; param->useLen = len; return BSL_SUCCESS; default: return SetOtherValues(param, type, val, len); } } int32_t BSL_PARAM_GetPtrValue(const BSL_Param *param, int32_t key, uint32_t type, void **val, uint32_t *valueLen) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return BSL_PARAMS_INVALID_KEY; } if (param == NULL || val == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (type != BSL_PARAM_TYPE_FUNC_PTR && type != BSL_PARAM_TYPE_CTX_PTR) { if (valueLen == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } } if (param->key != key || param->valueType != type) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_MISMATCH); return BSL_PARAMS_MISMATCH; } switch (type) { case BSL_PARAM_TYPE_UINT32_PTR: case BSL_PARAM_TYPE_OCTETS_PTR: *val = param->value; *valueLen = param->valueLen; return BSL_SUCCESS; case BSL_PARAM_TYPE_FUNC_PTR: case BSL_PARAM_TYPE_CTX_PTR: *val = param->value; return BSL_SUCCESS; default: BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_TYPE); return BSL_PARAMS_INVALID_TYPE; } } int32_t BSL_PARAM_GetValue(const BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t *valueLen) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return BSL_PARAMS_INVALID_KEY; } if (param == NULL || val == NULL || valueLen == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (param->key != key || param->valueType != type) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_MISMATCH); return BSL_PARAMS_MISMATCH; } switch (type) { case BSL_PARAM_TYPE_UINT16: case BSL_PARAM_TYPE_UINT32: case BSL_PARAM_TYPE_UINT64: case BSL_PARAM_TYPE_OCTETS: case BSL_PARAM_TYPE_BOOL: case BSL_PARAM_TYPE_INT32: if (*valueLen < param->valueLen) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } (void)memcpy_s(val, param->valueLen, param->value, param->valueLen); *valueLen = param->valueLen; return BSL_SUCCESS; default: BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_TYPE); return BSL_PARAMS_INVALID_TYPE; } } const BSL_Param *BSL_PARAM_FindConstParam(const BSL_Param *param, int32_t key) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return NULL; } if (param == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } int32_t index = 0; while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) { if (param[index].key == key) { return &param[index]; } index++; } BSL_ERR_PUSH_ERROR(BSL_PARAMS_MISMATCH); return NULL; } BSL_Param *BSL_PARAM_FindParam(BSL_Param *param, int32_t key) { if (key == 0) { BSL_ERR_PUSH_ERROR(BSL_PARAMS_INVALID_KEY); return NULL; } if (param == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return NULL; } int32_t index = 0; while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) { if (param[index].key == key) { return &param[index]; } index++; } BSL_ERR_PUSH_ERROR(BSL_PARAMS_MISMATCH); return NULL; } void BSL_PARAM_Free(BSL_Param *param) { if (param != NULL) { BSL_SAL_Free(param); } } #endif
2302_82127028/openHiTLS-examples
bsl/params/src/bsl_params.c
C
unknown
7,879
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_PEM_INTERNAL_H #define BSL_PEM_INTERNAL_H #include "hitls_build.h" #ifdef HITLS_BSL_PEM #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define BSL_PEM_CERT_BEGIN_STR "-----BEGIN CERTIFICATE-----" #define BSL_PEM_CERT_END_STR "-----END CERTIFICATE-----" #define BSL_PEM_CRL_BEGIN_STR "-----BEGIN X509 CRL-----" #define BSL_PEM_CRL_END_STR "-----END X509 CRL-----" #define BSL_PEM_PUB_KEY_BEGIN_STR "-----BEGIN PUBLIC KEY-----" #define BSL_PEM_PUB_KEY_END_STR "-----END PUBLIC KEY-----" #define BSL_PEM_RSA_PUB_KEY_BEGIN_STR "-----BEGIN RSA PUBLIC KEY-----" #define BSL_PEM_RSA_PUB_KEY_END_STR "-----END RSA PUBLIC KEY-----" #define BSL_PEM_RSA_PRI_KEY_BEGIN_STR "-----BEGIN RSA PRIVATE KEY-----" #define BSL_PEM_RSA_PRI_KEY_END_STR "-----END RSA PRIVATE KEY-----" /** rfc5915 section 4 */ #define BSL_PEM_EC_PRI_KEY_BEGIN_STR "-----BEGIN EC PRIVATE KEY-----" #define BSL_PEM_EC_PRI_KEY_END_STR "-----END EC PRIVATE KEY-----" /** rfc5958 section 5 */ #define BSL_PEM_PRI_KEY_BEGIN_STR "-----BEGIN PRIVATE KEY-----" #define BSL_PEM_PRI_KEY_END_STR "-----END PRIVATE KEY-----" /** rfc5958 section 5 */ #define BSL_PEM_P8_PRI_KEY_BEGIN_STR "-----BEGIN ENCRYPTED PRIVATE KEY-----" #define BSL_PEM_P8_PRI_KEY_END_STR "-----END ENCRYPTED PRIVATE KEY-----" #define BSL_PEM_CERT_REQ_BEGIN_STR "-----BEGIN CERTIFICATE REQUEST-----" #define BSL_PEM_CERT_REQ_END_STR "-----END CERTIFICATE REQUEST-----" typedef struct { const char *head; const char *tail; } BSL_PEM_Symbol; int32_t BSL_PEM_EncodeAsn1ToPem(uint8_t *asn1Encode, uint32_t asn1Len, BSL_PEM_Symbol *symbol, char **encode, uint32_t *encodeLen); /* encode must end in '\0' */ int32_t BSL_PEM_DecodePemToAsn1(char **encode, uint32_t *encodeLen, BSL_PEM_Symbol *symbol, uint8_t **asn1Encode, uint32_t *asn1Len); /* encode must end in '\0' */ bool BSL_PEM_IsPemFormat(char *encode, uint32_t encodeLen); int32_t BSL_PEM_GetSymbolAndType(char *encode, uint32_t encodeLen, BSL_PEM_Symbol *symbol, char **type); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_BSL_PEM */ #endif /* BSL_PEM_INTERNAL_H */
2302_82127028/openHiTLS-examples
bsl/pem/include/bsl_pem_internal.h
C
unknown
2,656
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_PEM #include <stdint.h> #include <string.h> #include "securec.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "bsl_base64_internal.h" #include "bsl_base64.h" #include "bsl_pem_local.h" #include "bsl_pem_internal.h" #define PEM_LINE_LEN 64 int32_t BSL_PEM_GetPemRealEncode(char **encode, uint32_t *encodeLen, BSL_PEM_Symbol *symbol, char **realEncode, uint32_t *realLen) { uint32_t headLen = (uint32_t)strlen(symbol->head); uint32_t tailLen = (uint32_t)strlen(symbol->tail); if (*encodeLen < headLen + tailLen) { BSL_ERR_PUSH_ERROR(BSL_PEM_INVALID); return BSL_PEM_INVALID; } if (!BSL_PEM_IsPemFormat(*encode, *encodeLen)) { return BSL_PEM_INVALID; } char *begin = strstr(*encode, symbol->head); if (begin == NULL) { BSL_ERR_PUSH_ERROR(BSL_PEM_SYMBOL_NOT_FOUND); return BSL_PEM_SYMBOL_NOT_FOUND; } char *end = strstr(begin + headLen, symbol->tail); if (end == NULL) { BSL_ERR_PUSH_ERROR(BSL_PEM_SYMBOL_NOT_FOUND); return BSL_PEM_SYMBOL_NOT_FOUND; } *realEncode = begin + headLen; *realLen = end - *realEncode; *encodeLen -= (end - *encode + tailLen); *encode = end + tailLen; return BSL_SUCCESS; } // Obtain asn1 raw data int32_t BSL_PEM_GetAsn1Encode(const char *encode, const uint32_t encodeLen, uint8_t **asn1Encode, uint32_t *asn1Len) { uint32_t len = BSL_BASE64_DEC_ENOUGH_LEN(encodeLen); uint8_t *asn1 = BSL_SAL_Malloc(len); if (asn1 == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } int32_t ret = BSL_BASE64_Decode(encode, encodeLen, asn1, &len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_Free(asn1); return ret; } *asn1Encode = asn1; *asn1Len = len; return BSL_SUCCESS; } static void PemFormatBase64(char *src, uint32_t srcLen, char **des) { uint32_t len = srcLen; char *tmp = *des; while (len > PEM_LINE_LEN) { *tmp++ = '\n'; (void)memcpy_s(tmp, PEM_LINE_LEN, src, PEM_LINE_LEN); tmp += PEM_LINE_LEN; src += PEM_LINE_LEN; len -= PEM_LINE_LEN; } *tmp++ = '\n'; (void)memcpy_s(tmp, len, src, len); tmp += len; *tmp++ = '\n'; *des = tmp; } int32_t BSL_PEM_EncodeAsn1ToPem(uint8_t *asn1Encode, uint32_t asn1Len, BSL_PEM_Symbol *symbol, char **encode, uint32_t *encodeLen) { int32_t ret; uint32_t headLen = (uint32_t)strlen(symbol->head); uint32_t tailLen = (uint32_t)strlen(symbol->tail); uint32_t len = BSL_BASE64_ENC_ENOUGH_LEN(asn1Len); char *buff = BSL_SAL_Malloc(len); if (buff == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } char *tmp = buff; char *res = NULL; do { ret = BSL_BASE64_Encode(asn1Encode, asn1Len, tmp, &len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); break; } uint32_t line = (len + PEM_LINE_LEN - 1) / PEM_LINE_LEN; uint32_t sumLen = line + len + headLen + tailLen + 3; // 3: \n + \n +\0 res = BSL_SAL_Malloc(sumLen); if (res == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); ret = BSL_MALLOC_FAIL; break; } char *resTmp = res; (void)memcpy_s(resTmp, headLen, symbol->head, headLen); resTmp += headLen; PemFormatBase64(tmp, len, &resTmp); (void)memcpy_s(resTmp, tailLen, symbol->tail, tailLen); resTmp += tailLen; *resTmp++ = '\n'; *resTmp++ = '\0'; *encode = res; *encodeLen = sumLen - 1; BSL_SAL_FREE(buff); return BSL_SUCCESS; } while (0); BSL_SAL_FREE(buff); BSL_SAL_FREE(res); return ret; } int32_t BSL_PEM_DecodePemToAsn1(char **encode, uint32_t *encodeLen, BSL_PEM_Symbol *symbol, uint8_t **asn1Encode, uint32_t *asn1Len) { char *nextEncode = *encode; uint32_t nextEncodeLen = *encodeLen; char *realEncode = NULL; uint32_t realLen; int32_t ret = BSL_PEM_GetPemRealEncode(&nextEncode, &nextEncodeLen, symbol, &realEncode, &realLen); if (ret != BSL_SUCCESS) { return ret; } ret = BSL_PEM_GetAsn1Encode(realEncode, realLen, asn1Encode, asn1Len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *encode = nextEncode; *encodeLen = nextEncodeLen; return BSL_SUCCESS; } /** * reference rfc7468 * Textual encoding begins with a line comprising "-----BEGIN ", a label, and "-----", * and ends with a line comprising "-----END ", a label, and "-----". */ bool BSL_PEM_IsPemFormat(char *encode, uint32_t encodeLen) { if (encode == NULL || encodeLen < (BSL_PEM_BEGIN_STR_LEN + BSL_PEM_END_STR_LEN + 2 * BSL_PEM_SHORT_DASH_STR_LEN)) { return false; } // match "-----BEGIN" char *begin = strstr(encode, BSL_PEM_BEGIN_STR); if (begin == NULL) { return false; } char *tmp = (char *)encode + BSL_PEM_BEGIN_STR_LEN; // match "-----" begin = strstr(tmp, BSL_PEM_SHORT_DASH_STR); if (begin == NULL) { return false; } tmp = begin + BSL_PEM_SHORT_DASH_STR_LEN; // match "-----END" begin = strstr(tmp, BSL_PEM_END_STR); if (begin == NULL) { return false; } tmp = begin + BSL_PEM_END_STR_LEN; // match "-----" if (strstr(tmp, BSL_PEM_SHORT_DASH_STR) == NULL) { return false; } return true; } typedef struct { char *type; BSL_PEM_Symbol symbol; } PemHeaderInfo; static PemHeaderInfo g_pemHeaderInfo[] = { {"PRIKEY_RSA", {BSL_PEM_RSA_PRI_KEY_BEGIN_STR, BSL_PEM_RSA_PRI_KEY_END_STR}}, {"PRIKEY_ECC", {BSL_PEM_EC_PRI_KEY_BEGIN_STR, BSL_PEM_EC_PRI_KEY_END_STR}}, {"PRIKEY_PKCS8_UNENCRYPT", {BSL_PEM_PRI_KEY_BEGIN_STR, BSL_PEM_PRI_KEY_END_STR}}, {"PRIKEY_PKCS8_ENCRYPT", {BSL_PEM_P8_PRI_KEY_BEGIN_STR, BSL_PEM_P8_PRI_KEY_END_STR}}, {"PUBKEY_SUBKEY", {BSL_PEM_PUB_KEY_BEGIN_STR, BSL_PEM_PUB_KEY_END_STR}}, {"PUBKEY_RSA", {BSL_PEM_RSA_PUB_KEY_BEGIN_STR, BSL_PEM_RSA_PUB_KEY_END_STR}}, {"CERT", {BSL_PEM_CERT_BEGIN_STR, BSL_PEM_CERT_END_STR}}, {"CRL", {BSL_PEM_CRL_BEGIN_STR, BSL_PEM_CRL_END_STR}}, {"CSR", {BSL_PEM_CERT_REQ_BEGIN_STR, BSL_PEM_CERT_REQ_END_STR}}, }; int32_t BSL_PEM_GetSymbolAndType(char *encode, uint32_t encodeLen, BSL_PEM_Symbol *symbol, char **type) { if (symbol == NULL || type == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (!BSL_PEM_IsPemFormat(encode, encodeLen)) { BSL_ERR_PUSH_ERROR(BSL_PEM_INVALID); return BSL_PEM_INVALID; } for (uint32_t i = 0; i < sizeof(g_pemHeaderInfo) / sizeof(g_pemHeaderInfo[0]); i++) { char *beginMarker = strstr(encode, g_pemHeaderInfo[i].symbol.head); if (beginMarker != NULL) { char *endMarker = strstr(beginMarker + strlen(g_pemHeaderInfo[i].symbol.head), g_pemHeaderInfo[i].symbol.tail); if (endMarker != NULL) { symbol->head = g_pemHeaderInfo[i].symbol.head; symbol->tail = g_pemHeaderInfo[i].symbol.tail; *type = g_pemHeaderInfo[i].type; return BSL_SUCCESS; } } } BSL_ERR_PUSH_ERROR(BSL_PEM_SYMBOL_NOT_FOUND); return BSL_PEM_SYMBOL_NOT_FOUND; } #endif /* HITLS_BSL_PEM */
2302_82127028/openHiTLS-examples
bsl/pem/src/bsl_pem.c
C
unknown
8,044
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_PEM_LOCAL_H #define BSL_PEM_LOCAL_H #include "hitls_build.h" #ifdef HITLS_BSL_PEM #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define BSL_PEM_BEGIN_STR "-----BEGIN" #define BSL_PEM_BEGIN_STR_LEN 10 #define BSL_PEM_END_STR "-----END" #define BSL_PEM_END_STR_LEN 8 #define BSL_PEM_SHORT_DASH_STR "-----" #define BSL_PEM_SHORT_DASH_STR_LEN 5 #ifdef __cplusplus } #endif #endif /* HITLS_BSL_PEM */ #endif
2302_82127028/openHiTLS-examples
bsl/pem/src/bsl_pem_local.h
C
unknown
976
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef BSL_PRINT_H #define BSL_PRINT_H #include <stdint.h> #include <stdlib.h> #include <stdbool.h> #include "bsl_types.h" #include "bsl_sal.h" #include "bsl_uio.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_print * @brief Print asn1 data. * * @param layer [IN] Print layer. * @param uio [IN/OUT] Print uio context. * @param buff [IN] Print buffer. * @param buffLen [IN] Print buffer length. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_PRINT_Buff(uint32_t layer, BSL_UIO *uio, const void *buff, uint32_t buffLen); /** * @ingroup bsl_print * @brief Print the format string.. * * @param layer [IN] Print layer. * @param uio [IN/OUT] Print uio context. * @param fmt [IN] Print format. * @param ... [IN] Print data. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_PRINT_Fmt(uint32_t layer, BSL_UIO *uio, const char *fmt, ...); /** * @ingroup bsl_print * @brief Print Hex function. * * @param layer [IN] Print layer. * @param data [IN] Print data. * @param oneLine [IN] Print on one line. * @param dataLen [IN] Print data length * @param uio [IN/OUT] Print uio context. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_PRINT_Hex(uint32_t layer, bool oneLine, const uint8_t *data, uint32_t dataLen, BSL_UIO *uio); /** * @ingroup bsl_print * @brief Print time function. * * @param layer [IN] Print layer. * @param time [IN] Time. * @param uio [IN/OUT] Print uio context. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_PRINT_Time(uint32_t layer, const BSL_TIME *time, BSL_UIO *uio); /** * @ingroup bsl_print * @brief Print Number function. * * @param layer [IN] Print layer. * @param title [IN] Print title. * @param data [IN] Print data. * @param dataLen [IN] Print data length * @param uio [IN/OUT] Print uio context. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_PRINT_Number(uint32_t layer, const char *title, const uint8_t *data, uint32_t dataLen, BSL_UIO *uio); #ifdef __cplusplus } #endif #endif // BSL_PRINT_H
2302_82127028/openHiTLS-examples
bsl/print/include/bsl_print.h
C
unknown
2,830
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include <stdarg.h> #include <inttypes.h> #include <string.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "bsl_bytes.h" #include "bsl_print.h" #define BSL_PRINT_LEN 1024 #define BSL_PRINT_MAX_LAYER 10 #define BSL_PRINT_EACH_LAYER_INDENT 4 #define BSL_PRINT_MAX_INDENT ((BSL_PRINT_EACH_LAYER_INDENT) * (BSL_PRINT_MAX_LAYER)) #define BSL_PRINT_WIDTH 16 #define BSL_PRINT_MAX_WIDTH 20 // If the data length is less than or equal to 20, the data is printed in one line. #define BSL_ASN1_HEX_TO_COLON_HEX 3 // "XX:" Use 3 bytes. #define BSL_PRINT_HEX_LEN (BSL_PRINT_MAX_WIDTH * BSL_ASN1_HEX_TO_COLON_HEX + 1) #define BSL_PRINT_NEW_LINE "\n" static int32_t WriteBuff(BSL_UIO *uio, const void *buff, uint32_t buffLen) { uint32_t writeLen = 0; int32_t ret = BSL_UIO_Write(uio, buff, buffLen, &writeLen); return ret != BSL_SUCCESS ? ret : (writeLen != buffLen ? BSL_PRINT_ERR_BUF : BSL_SUCCESS); } static int32_t PrintBuff(uint32_t layer, BSL_UIO *uio, const void *buff, uint32_t buffLen) { int32_t ret; char *indent[BSL_PRINT_MAX_INDENT + 1] = {0}; (void)memset_s(indent, BSL_PRINT_MAX_INDENT, ' ', BSL_PRINT_MAX_INDENT); if (layer > 0) { ret = WriteBuff(uio, indent, layer * BSL_PRINT_EACH_LAYER_INDENT); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } if (buffLen == 0) { return BSL_SUCCESS; } ret = WriteBuff(uio, buff, buffLen); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t BSL_PRINT_Buff(uint32_t layer, BSL_UIO *uio, const void *buff, uint32_t buffLen) { if (layer > BSL_PRINT_MAX_LAYER || uio == NULL || (buffLen != 0 && buff == NULL)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } return PrintBuff(layer, uio, buff, buffLen); } static int32_t PrintHexOnOneLine(uint32_t layer, const uint8_t *data, uint32_t dataLen, BSL_UIO *uio) { char hexStr[BSL_PRINT_HEX_LEN] = {0}; uint32_t strIdx = 0; bool isEnd; bool needIndent = true; int32_t ret; for (uint32_t i = 0; i < dataLen; i++) { isEnd = (i + 1) == dataLen; if (sprintf_s(hexStr + strIdx * BSL_ASN1_HEX_TO_COLON_HEX, BSL_PRINT_HEX_LEN - strIdx * BSL_ASN1_HEX_TO_COLON_HEX, isEnd ? "%02x\n" : "%02x:", data[i]) == -1) { BSL_ERR_PUSH_ERROR(BSL_PRINT_ERR_BUF); return BSL_PRINT_ERR_BUF; } strIdx++; if (isEnd || strIdx == BSL_PRINT_MAX_WIDTH) { ret = PrintBuff(needIndent ? layer : 0, uio, hexStr, strIdx * BSL_ASN1_HEX_TO_COLON_HEX); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } needIndent = false; strIdx = 0; } } return BSL_SUCCESS; } int32_t BSL_PRINT_Hex(uint32_t layer, bool oneLine, const uint8_t *data, uint32_t dataLen, BSL_UIO *uio) { if (layer > BSL_PRINT_MAX_LAYER || data == NULL || dataLen == 0 || uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } if (oneLine) { return PrintHexOnOneLine(layer, data, dataLen, uio); } char hexStr[BSL_PRINT_HEX_LEN] = {0}; uint32_t lineLen = dataLen <= BSL_PRINT_MAX_WIDTH ? BSL_PRINT_MAX_WIDTH : BSL_PRINT_WIDTH; char *format = NULL; uint32_t strIdx = 0; bool isLineEnd; for (uint32_t i = 0; i < dataLen; i++) { isLineEnd = (i + 1) % lineLen == 0 || (i + 1) == dataLen; format = isLineEnd ? "%02x\n" : "%02x:"; if (sprintf_s(hexStr + strIdx * BSL_ASN1_HEX_TO_COLON_HEX, BSL_PRINT_HEX_LEN - strIdx * BSL_ASN1_HEX_TO_COLON_HEX, format, data[i]) == -1) { BSL_ERR_PUSH_ERROR(BSL_PRINT_ERR_BUF); return BSL_PRINT_ERR_BUF; } strIdx++; if (!isLineEnd) { continue; } if (PrintBuff(layer, uio, hexStr, strIdx * BSL_ASN1_HEX_TO_COLON_HEX) != 0) { BSL_ERR_PUSH_ERROR(BSL_PRINT_ERR_BUF); return BSL_PRINT_ERR_BUF; } strIdx = 0; } return BSL_SUCCESS; } int32_t BSL_PRINT_Fmt(uint32_t layer, BSL_UIO *uio, const char *fmt, ...) { if (layer > BSL_PRINT_MAX_LAYER || uio == NULL || fmt == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } va_list args; va_start(args, fmt); char buff[BSL_PRINT_LEN + 1] = {0}; if (vsprintf_s(buff, BSL_PRINT_LEN + 1, fmt, args) == -1) { va_end(args); BSL_ERR_PUSH_ERROR(BSL_PRINT_ERR_FMT); return BSL_PRINT_ERR_FMT; } int32_t ret = BSL_PRINT_Buff(layer, uio, buff, (uint32_t)strlen(buff)); va_end(args); return ret != 0 ? BSL_PRINT_ERR_FMT : BSL_SUCCESS; } // rfc822: https://www.w3.org/Protocols/rfc822/ static const char MONTH_STR[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; int32_t BSL_PRINT_Time(uint32_t layer, const BSL_TIME *time, BSL_UIO *uio) { if (time == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } return BSL_PRINT_Fmt(layer, uio, "%s %u %02u:%02u:%02u %u GMT\n", MONTH_STR[time->month - 1], time->day, time->hour, time->minute, time->second, time->year); } /** * Only positive numbers can be printed. */ int32_t BSL_PRINT_Number(uint32_t layer, const char *title, const uint8_t *data, uint32_t dataLen, BSL_UIO *uio) { if (data == NULL || dataLen == 0 || uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } uint32_t temp = layer; if (dataLen > (sizeof(uint64_t))) { if (title != NULL) { if (BSL_PRINT_Fmt(temp++, uio, "%s:\n", title) != 0) { BSL_ERR_PUSH_ERROR(BSL_PRINT_ERR_NUMBER); return BSL_PRINT_ERR_NUMBER; } } return BSL_PRINT_Hex(temp, false, data, dataLen, uio); } uint64_t num = 0; for (uint32_t i = 0; i < dataLen; i++) { num |= (uint64_t)data[i] << (8 * (dataLen - i - 1)); // 8: bits } if (title != NULL) { return BSL_PRINT_Fmt(layer, uio, "%s: %"PRIu64" (0x%"PRIX64")\n", title, num, num); } return BSL_PRINT_Fmt(layer, uio, "%"PRIu64" (0x%"PRIX64")\n", num, num); }
2302_82127028/openHiTLS-examples
bsl/print/src/bsl_print.c
C
unknown
6,911
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_ATOMIC_H #define SAL_ATOMIC_H #include <stdlib.h> #include "bsl_sal.h" #include "bsl_errno.h" /* The value of __STDC_VERSION__ is determined by the compilation option -std. The atomic API is provided only when -std=gnu11 is used. */ #if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__) #include <stdatomic.h> #define SAL_HAVE_C11_ATOMICS #endif #ifdef __cplusplus extern "C" { #endif // __cplusplus int BSL_SAL_AtomicAdd(int *val, int amount, int *ref, BSL_SAL_ThreadLockHandle lock); /* Atom operation mode 1, which uses the function provided by C11. Only the int type is considered. * ATOMIC_INT_LOCK_FREE: If the value is 1, the operation MAY BE lock-free operation. * ATOMIC_INT_LOCK_FREE: If the value is 2, it's the lock-free operation. * memory_order_relaxed only ensures the atomicity of the current operation * and does not consider the synchronization between threads. */ #if defined(SAL_HAVE_C11_ATOMICS) && defined(ATOMIC_INT_LOCK_FREE) && ATOMIC_INT_LOCK_FREE > 0 && !defined(HITLS_ATOMIC_THREAD_LOCK) #define SAL_USE_ATOMICS_LIB_FUNC typedef struct { atomic_int count; } BSL_SAL_RefCount; static inline int BSL_SAL_AtomicUpReferences(BSL_SAL_RefCount *references, int *ret) { *ret = atomic_fetch_add_explicit(&(references->count), 1, memory_order_relaxed) + 1; return BSL_SUCCESS; } static inline int BSL_SAL_AtomicDownReferences(BSL_SAL_RefCount *references, int *ret) { *ret = atomic_fetch_sub_explicit(&(references->count), 1, memory_order_relaxed) - 1; if (*ret == 0) { atomic_thread_fence(memory_order_acquire); } return BSL_SUCCESS; } /* Atom operation mode 2, using the function provided by the GCC. */ #elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) && __GCC_ATOMIC_INT_LOCK_FREE > 0 && !defined(HITLS_ATOMIC_THREAD_LOCK) #define SAL_USE_ATOMICS_LIB_FUNC typedef struct { int count; } BSL_SAL_RefCount; static inline int BSL_SAL_AtomicUpReferences(BSL_SAL_RefCount *references, int *ret) { *ret = __atomic_fetch_add(&(references->count), 1, __ATOMIC_RELAXED) + 1; return BSL_SUCCESS; } static inline int BSL_SAL_AtomicDownReferences(BSL_SAL_RefCount *references, int *ret) { *ret = __atomic_fetch_sub(&(references->count), 1, __ATOMIC_RELAXED) - 1; if (*ret == 0) { const int type = __ATOMIC_ACQUIRE; __atomic_thread_fence(type); } return BSL_SUCCESS; } // Atom operation mode 3, using read/write locks. #else typedef struct { int count; BSL_SAL_ThreadLockHandle lock; } BSL_SAL_RefCount; static inline int BSL_SAL_AtomicUpReferences(BSL_SAL_RefCount *references, int *ret) { return BSL_SAL_AtomicAdd(&(references->count), 1, ret, references->lock); } static inline int BSL_SAL_AtomicDownReferences(BSL_SAL_RefCount *references, int *ret) { return BSL_SAL_AtomicAdd(&(references->count), -1, ret, references->lock); } #endif #ifdef SAL_USE_ATOMICS_LIB_FUNC static inline int BSL_SAL_ReferencesInit(BSL_SAL_RefCount *references) { references->count = 1; return BSL_SUCCESS; } static inline void BSL_SAL_ReferencesFree(BSL_SAL_RefCount *references) { (void)references; return; } #else static inline int BSL_SAL_ReferencesInit(BSL_SAL_RefCount *references) { references->count = 1; return BSL_SAL_ThreadLockNew(&(references->lock)); } static inline void BSL_SAL_ReferencesFree(BSL_SAL_RefCount *references) { BSL_SAL_ThreadLockFree(references->lock); references->lock = NULL; return; } #endif #ifdef __cplusplus } #endif // __cplusplus #endif // SAL_ATOMIC_H
2302_82127028/openHiTLS-examples
bsl/sal/include/sal_atomic.h
C
unknown
4,109
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_FILE_H #define SAL_FILE_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_FILE #include <stdbool.h> #include <stdint.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_sal * @brief Reads the specified file into the buff * * Reads the specified file into the buff * * @attention None. * @param path [IN] specified file. * @param buff [OUT] return the read memory. * @param len [OUT] return the read memory len. * @retval if the operation is successful, BSL_SUCCESS is returned, for other errors, see bsl_error.h */ int32_t BSL_SAL_ReadFile(const char *path, uint8_t **buff, uint32_t *len); /** * @ingroup bsl_sal * @brief Writes the buff to the specified file * * Writes the buff to the specified file * * @attention None. * @param path [IN] specified file. * @param buff [IN] the write memory. * @param len [IN] the write memory len. * @retval if the operation is successful, BSL_SUCCESS is returned, for other errors, see bsl_error.h */ int32_t BSL_SAL_WriteFile(const char *path, const uint8_t *buff, uint32_t len); bool SAL_FileError(bsl_sal_file_handle stream); char *SAL_FGets(bsl_sal_file_handle stream, char *buf, int32_t readLen); bool SAL_FPuts(bsl_sal_file_handle stream, const char *buf); bool SAL_Flush(bsl_sal_file_handle stream); int32_t SAL_Feof(bsl_sal_file_handle stream); int32_t SAL_FSetAttr(bsl_sal_file_handle stream, int cmd, const void *arg); int32_t SAL_FGetAttr(bsl_sal_file_handle stream, void *arg); int32_t SAL_FileTell(bsl_sal_file_handle stream, long *pos); int32_t SAL_FileSeek(bsl_sal_file_handle stream, long offset, int32_t origin); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_BSL_SAL_FILE */ #endif // SAL_FILE_H
2302_82127028/openHiTLS-examples
bsl/sal/include/sal_file.h
C
unknown
2,289
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_NET_H #define SAL_NET_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_NET #include <stdint.h> #ifdef HITLS_BSL_SAL_LINUX #include <arpa/inet.h> #include <netinet/tcp.h> #endif #ifdef __cplusplus extern "C" { #endif int32_t SAL_Write(int32_t fd, const void *buf, uint32_t len, int32_t *err); int32_t SAL_Read(int32_t fd, void *buf, uint32_t len, int32_t *err); int32_t SAL_Sendto(int32_t sock, const void *buf, size_t len, int32_t flags, BSL_SAL_SockAddr address, int32_t addrLen, int32_t *err); int32_t SAL_RecvFrom(int32_t sock, void *buf, size_t len, int32_t flags, BSL_SAL_SockAddr address, int32_t *addrLen, int32_t *err); int32_t SAL_SockAddrNew(BSL_SAL_SockAddr *sockAddr); int32_t SAL_SockAddrGetFamily(const BSL_SAL_SockAddr sockAddr); void SAL_SockAddrFree(BSL_SAL_SockAddr sockAddr); uint32_t SAL_SockAddrSize(const BSL_SAL_SockAddr sockAddr); void SAL_SockAddrCopy(BSL_SAL_SockAddr dst, BSL_SAL_SockAddr src); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_BSL_SAL_NET */ #endif // SAL_NET_H
2302_82127028/openHiTLS-examples
bsl/sal/include/sal_net.h
C
unknown
1,627
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_TIME_H #define SAL_TIME_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_TIME #include <stddef.h> #include <stdint.h> #include <stdbool.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif #define BSL_TIME_CMP_ERROR 0U /* The comparison between two dates is incorrect. */ #define BSL_TIME_CMP_EQUAL 1U /* The two dates are the same. */ #define BSL_TIME_DATE_BEFORE 2U /* The first date is earlier than the second date */ #define BSL_TIME_DATE_AFTER 3U /* The first date is later than the second date. */ #define BSL_TIME_YEAR_START 1900U #define BSL_TIME_SYSTEM_EPOCH_YEAR 1970U #define BSL_TIME_DAY_PER_NONLEAP_YEAR 365U #define BSL_TIME_BIG_MONTH_DAY 31U #define BSL_TIME_SMALL_MONTH_DAY 30U #define BSL_TIME_LEAP_FEBRUARY_DAY 29U #define BSL_TIME_NOLEAP_FEBRUARY_DAY 28U #define BSL_MONTH_JAN 1U /* January */ #define BSL_MONTH_FEB 2U /* February */ #define BSL_MONTH_MAR 3U /* March */ #define BSL_MONTH_APR 4U /* April */ #define BSL_MONTH_MAY 5U /* May */ #define BSL_MONTH_JUN 6U /* June */ #define BSL_MONTH_JUL 7U /* July */ #define BSL_MONTH_AUG 8U /* August */ #define BSL_MONTH_SEM 9U /* September */ #define BSL_MONTH_OCT 10U /* October */ #define BSL_MONTH_NOV 11U /* November */ #define BSL_MONTH_DEC 12U /* December */ #define BSL_TIME_TICKS_PER_SECOND_DEFAULT 100U #define BSL_SECOND_TRANSFER_RATIO 1000U /* conversion ratio of microseconds -> milliseconds -> seconds */ #define BSL_UTCTIME_MAX 2005949145599L /* UTC time corresponding to December 31, 65535 23:59:59 */ bool BSL_IsLeapYear(uint32_t year); /** * @brief Obtain the date in string format. * @param dateTime [IN] Pointer to the date structure to be converted into a string. * @param timeStr [OUT] Pointer to the date string buffer. * @param len [IN] Date string buffer length, which must be greater than 26. * @return BSL_SUCCESS is successfully executed. * BSL_INTERNAL_EXCEPTION Execution Failure */ uint32_t BSL_DateToStrConvert(const BSL_TIME *dateTime, char *timeStr, size_t len); /** * @brief Add the time. * @param date [IN] * @param us [IN] * @return BSL_SUCCESS is successfully executed. * For other failures, see BSL_SAL_DateToUtcTimeConvert and BSL_SAL_UtcTimeToDateConvert. */ uint32_t BSL_DateTimeAddUs(BSL_TIME *dateR, const BSL_TIME *dateA, uint32_t us); /** * @brief Check whether the time format is correct. * @param dateTime [IN] Time to be checked * @return true The time format is correct. * false incorrect */ bool BSL_DateTimeCheck(const BSL_TIME *dateTime); void BSL_SysTimeFuncUnReg(void); #ifdef __cplusplus } #endif #endif /* HITLS_BSL_SAL_TIME */ #endif // SAL_TIME_H
2302_82127028/openHiTLS-examples
bsl/sal/include/sal_time.h
C
unknown
3,265
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_DL) #include <stdint.h> #include <stdlib.h> #include <string.h> #include <dlfcn.h> #include "bsl_errno.h" #include "bsl_err_internal.h" int32_t SAL_LoadLib(const char *fileName, void **handle) { void *tempHandle = dlopen(fileName, RTLD_NOW); if (tempHandle == NULL) { char *error = dlerror(); if (strstr(error, "No such file or directory") != NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_NOT_FOUND); return BSL_SAL_ERR_DL_NOT_FOUND; } BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_LOAD_FAIL); return BSL_SAL_ERR_DL_LOAD_FAIL; } *handle = tempHandle; return BSL_SUCCESS; } int32_t SAL_UnLoadLib(void *handle) { int32_t ret = dlclose(handle); if (ret != 0) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_UNLOAAD_FAIL); return BSL_SAL_ERR_DL_UNLOAAD_FAIL; } return BSL_SUCCESS; } int32_t SAL_GetFunc(void *handle, const char *funcName, void **func) { void *tempFunc = dlsym(handle, funcName); if (tempFunc == NULL) { char *error = dlerror(); if (strstr(error, "undefined symbol") != NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_NON_FUNCTION); return BSL_SAL_ERR_DL_NON_FUNCTION; } BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_LOOKUP_METHOD); return BSL_SAL_ERR_DL_LOOKUP_METHOD; } *func = tempFunc; return BSL_SUCCESS; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_sal_dl.c
C
unknown
2,017
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_FILE) #include <stdio.h> #include <stdlib.h> #include <termios.h> #include <unistd.h> #include <errno.h> #include "bsl_errno.h" #include "sal_file.h" #include "bsl_sal.h" int32_t SAL_FILE_FOpen(bsl_sal_file_handle *stream, const char *path, const char *mode) { bsl_sal_file_handle temp = NULL; if (path == NULL || path[0] == 0 || mode == NULL || stream == NULL) { return BSL_NULL_INPUT; } temp = (bsl_sal_file_handle)fopen(path, mode); if (temp == NULL) { return BSL_SAL_ERR_FILE_OPEN; } (*stream) = temp; return BSL_SUCCESS; } int32_t SAL_FILE_FRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len) { if (stream == NULL || buffer == NULL || len == NULL) { return BSL_NULL_INPUT; } *len = fread(buffer, size, num, stream); if (*len != num) { return feof(stream) != 0 ? BSL_SUCCESS : BSL_SAL_ERR_FILE_READ; } return BSL_SUCCESS; } int32_t SAL_FILE_FWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num) { if (stream == NULL || buffer == NULL) { /* size should not be more than 2^31 - 1 */ return BSL_NULL_INPUT; } size_t ret = fwrite(buffer, size, num, stream); return ret == num ? BSL_SUCCESS : BSL_SAL_ERR_FILE_WRITE; } void SAL_FILE_FClose(bsl_sal_file_handle stream) { if (stream == NULL) { return; } (void)fclose(stream); } int32_t SAL_FILE_FLength(const char *path, size_t *len) { int32_t ret; long tmp; bsl_sal_file_handle stream = NULL; if (path == NULL || len == NULL) { return BSL_NULL_INPUT; } ret = BSL_SAL_FileOpen(&stream, path, "rb"); if (ret != BSL_SUCCESS) { return BSL_SAL_ERR_FILE_LENGTH; } ret = fseek(stream, 0, SEEK_END); if (ret != BSL_SUCCESS) { BSL_SAL_FileClose(stream); return BSL_SAL_ERR_FILE_LENGTH; } tmp = ftell(stream); if (tmp < 0) { BSL_SAL_FileClose(stream); return BSL_SAL_ERR_FILE_LENGTH; } *len = (size_t)tmp; BSL_SAL_FileClose(stream); return BSL_SUCCESS; } bool SAL_FILE_FError(bsl_sal_file_handle stream) { if (stream == NULL) { return false; } return ferror(stream) != 0; } int32_t SAL_FILE_FTell(bsl_sal_file_handle stream, long *pos) { if (stream == NULL || pos == NULL) { return BSL_NULL_INPUT; } *pos = ftell(stream); return (*pos < 0) ? BSL_SAL_ERR_FILE_TELL : BSL_SUCCESS; } int32_t SAL_FILE_FSeek(bsl_sal_file_handle stream, long offset, int32_t origin) { if (stream == NULL) { return BSL_NULL_INPUT; } if (fseek(stream, offset, origin) == 0) { return BSL_SUCCESS; } return BSL_SAL_ERR_FILE_SEEK; } char *SAL_FILE_FGets(bsl_sal_file_handle stream, char *buf, int32_t readLen) { if (buf == NULL || readLen <= 0 || stream == NULL) { return NULL; } return fgets(buf, readLen, stream); } bool SAL_FILE_FPuts(bsl_sal_file_handle stream, const char *buf) { if (buf == NULL || stream == NULL) { return false; } return fputs(buf, stream) != EOF; } bool SAL_FILE_Flush(bsl_sal_file_handle stream) { if (stream == NULL) { return false; } return fflush(stream) != EOF; } int32_t SAL_FILE_Feof(bsl_sal_file_handle stream) { if (stream == NULL) { return BSL_NULL_INPUT; } if (feof(stream) != 0) { return BSL_SUCCESS; } return BSL_SAL_NOT_FILE_EOF; } int32_t SAL_FILE_FSetAttr(bsl_sal_file_handle stream, int cmd, const void *arg) { if (stream == NULL || arg == NULL) { return BSL_NULL_INPUT; } if (tcsetattr(fileno(stream), cmd, (const struct termios *)arg) != 0) { return BSL_SAL_ERR_FILE_SET_ATTR; } return BSL_SUCCESS; } int32_t SAL_FILE_FGetAttr(bsl_sal_file_handle stream, void *arg) { if (stream == NULL || arg == NULL) { return BSL_NULL_INPUT; } if (tcgetattr(fileno(stream), (struct termios *)arg) != 0) { return BSL_SAL_ERR_FILE_GET_ATTR; } return BSL_SUCCESS; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_sal_file.c
C
unknown
4,723
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include <pthread.h> #include <unistd.h> #include "hitls_build.h" #include "bsl_errno.h" #include "bsl_sal.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_LOCK) // Used for DEFAULT lock implementation typedef struct { pthread_rwlock_t rwlock; } BslOsalRWLock; int32_t SAL_RwLockNew(BSL_SAL_ThreadLockHandle *lock) { if (lock == NULL) { return BSL_SAL_ERR_BAD_PARAM; } BslOsalRWLock *newLock = (BslOsalRWLock *)BSL_SAL_Calloc(1, sizeof(BslOsalRWLock)); if (newLock == NULL) { return BSL_MALLOC_FAIL; } if (pthread_rwlock_init(&newLock->rwlock, (const pthread_rwlockattr_t *)NULL) != 0) { BSL_SAL_FREE(newLock); return BSL_SAL_ERR_UNKNOWN; } *lock = newLock; return BSL_SUCCESS; } int32_t SAL_RwReadLock(BSL_SAL_ThreadLockHandle rwLock) { BslOsalRWLock *lock = (BslOsalRWLock *)rwLock; if (lock == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (pthread_rwlock_rdlock(&lock->rwlock) != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } int32_t SAL_RwWriteLock(BSL_SAL_ThreadLockHandle rwLock) { BslOsalRWLock *lock = (BslOsalRWLock *)rwLock; if (lock == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (pthread_rwlock_wrlock(&lock->rwlock) != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } int32_t SAL_RwUnlock(BSL_SAL_ThreadLockHandle rwLock) { BslOsalRWLock *lock = (BslOsalRWLock *)rwLock; if (lock == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (pthread_rwlock_unlock(&lock->rwlock) != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } void SAL_RwLockFree(BSL_SAL_ThreadLockHandle rwLock) { BslOsalRWLock *lock = (BslOsalRWLock *)rwLock; if (lock != NULL) { (void)pthread_rwlock_destroy(&(lock->rwlock)); BSL_SAL_FREE(lock); } } #endif #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_THREAD) uint64_t SAL_GetPid(void) { // By default, gettid is not used to obtain the global tid corresponding to the thread // because other thread functions use the pthread library. // Use pthread_self to obtain the PID used by pthread_create in this process. // However, the pids of the parent and child processes may be the same. return (uint64_t)pthread_self(); } int32_t SAL_PthreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc) { if (onceControl == NULL || initFunc == NULL) { return BSL_SAL_ERR_BAD_PARAM; } pthread_once_t *tmpOnce = (pthread_once_t *)onceControl; if (pthread_once(tmpOnce, initFunc) != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } int32_t BSL_SAL_ThreadCreate(BSL_SAL_ThreadId *thread, void *(*startFunc)(void *), void *arg) { if (thread == NULL || startFunc == NULL) { return BSL_SAL_ERR_BAD_PARAM; } int32_t ret = pthread_create((pthread_t *)thread, NULL, startFunc, arg); if (ret != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } void BSL_SAL_ThreadClose(BSL_SAL_ThreadId thread) { if (thread == NULL) { return; } (void)pthread_join((pthread_t)(uintptr_t)thread, NULL); } int32_t BSL_SAL_CreateCondVar(BSL_SAL_CondVar *condVar) { if (condVar == NULL) { return BSL_SAL_ERR_BAD_PARAM; } pthread_cond_t *cond = (pthread_cond_t *)BSL_SAL_Malloc(sizeof(pthread_cond_t)); if (cond == NULL) { return BSL_MALLOC_FAIL; } if (pthread_cond_init(cond, NULL) != 0) { BSL_SAL_FREE(cond); return BSL_SAL_ERR_UNKNOWN; } *condVar = cond; return BSL_SUCCESS; } int32_t BSL_SAL_CondSignal(BSL_SAL_CondVar condVar) { if (condVar == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (pthread_cond_signal(condVar) != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } #define SAL_SECS_IN_NS 1000000000 // 1s = 1000000000ns #define SAL_SECS_IN_MS 1000 // 1s = 1000ms #define SAL_MS_IN_NS 1000000 // 1ms = 1000000ns int32_t BSL_SAL_CondTimedwaitMs(BSL_SAL_Mutex condMutex, BSL_SAL_CondVar condVar, int32_t timeout) { struct timespec stm = {0}; struct timespec etm = {0}; long int endNs; // nanosecond long int endSecs; // second if (condMutex == NULL || condVar == NULL) { return BSL_SAL_ERR_BAD_PARAM; } pthread_mutex_lock(condMutex); clock_gettime(CLOCK_REALTIME, &stm); endSecs = stm.tv_sec + timeout / SAL_SECS_IN_MS; endNs = stm.tv_nsec + (timeout % SAL_SECS_IN_MS) * SAL_MS_IN_NS; endSecs += endNs / SAL_SECS_IN_NS; endNs %= SAL_SECS_IN_NS; etm.tv_sec = endSecs; etm.tv_nsec = endNs; int32_t ret = pthread_cond_timedwait(condVar, condMutex, &etm); pthread_mutex_unlock(condMutex); if (ret != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } int32_t BSL_SAL_DeleteCondVar(BSL_SAL_CondVar condVar) { if (condVar == NULL) { return BSL_SAL_ERR_BAD_PARAM; } int32_t ret = pthread_cond_destroy((pthread_cond_t *)condVar); BSL_SAL_FREE(condVar); if (ret != 0) { return BSL_SAL_ERR_UNKNOWN; } return BSL_SUCCESS; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_sal_lockimpl.c
C
unknown
5,755
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_MEM) #include <stdint.h> #include <stdlib.h> void *SAL_MallocImpl(uint32_t size) { return malloc(size); } void SAL_FreeImpl(void *value) { if (value == NULL) { return; } free(value); } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_sal_mem.c
C
unknown
847
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_NET) #include <stdbool.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/un.h> #include <arpa/inet.h> #include <netdb.h> #include <errno.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_errno.h" #include "sal_net.h" typedef union { struct sockaddr addr; struct sockaddr_in6 addrIn6; struct sockaddr_in addrIn; struct sockaddr_un addrUn; } LinuxSockAddr; int32_t SAL_NET_Write(int32_t fd, const void *buf, uint32_t len, int32_t *err) { if (err == NULL) { return BSL_NULL_INPUT; } int32_t ret = (int32_t)write(fd, buf, len); if (ret < 0) { *err = errno; } return ret; } int32_t SAL_NET_Read(int32_t fd, void *buf, uint32_t len, int32_t *err) { if (err == NULL) { return BSL_NULL_INPUT; } int32_t ret = (int32_t)read(fd, buf, len); if (ret < 0) { *err = errno; } return ret; } int32_t SAL_NET_Sendto(int32_t sock, const void *buf, size_t len, int32_t flags, void *address, int32_t addrLen, int32_t *err) { if (err == NULL) { return BSL_NULL_INPUT; } int32_t ret = (int32_t)sendto(sock, buf, len, flags, (struct sockaddr *)address, (socklen_t)addrLen); if (ret <= 0) { *err = errno; } return ret; } int32_t SAL_NET_RecvFrom(int32_t sock, void *buf, size_t len, int32_t flags, void *address, int32_t *addrLen, int32_t *err) { if (err == NULL) { return BSL_NULL_INPUT; } int32_t ret = (int32_t)recvfrom(sock, buf, len, flags, (struct sockaddr *)address, (socklen_t *)addrLen); if (ret <= 0) { *err = errno; } return ret; } int32_t SAL_NET_SockAddrNew(BSL_SAL_SockAddr *sockAddr) { LinuxSockAddr *addr = (LinuxSockAddr *)BSL_SAL_Calloc(1, sizeof(LinuxSockAddr)); if (addr == NULL) { return BSL_MALLOC_FAIL; } *sockAddr = (BSL_SAL_SockAddr)addr; return BSL_SUCCESS; } void SAL_NET_SockAddrFree(BSL_SAL_SockAddr sockAddr) { BSL_SAL_Free(sockAddr); } int32_t SAL_NET_SockAddrGetFamily(const BSL_SAL_SockAddr sockAddr) { const LinuxSockAddr *addr = (const LinuxSockAddr *)sockAddr; if (addr == NULL) { return AF_UNSPEC; } return addr->addr.sa_family; } uint32_t SAL_NET_SockAddrSize(const BSL_SAL_SockAddr sockAddr) { const LinuxSockAddr *addr = (const LinuxSockAddr *)sockAddr; if (addr == NULL) { return 0; } switch (addr->addr.sa_family) { case AF_INET: return sizeof(addr->addrIn); case AF_INET6: return sizeof(addr->addrIn6); case AF_UNIX: return sizeof(addr->addrUn); default: break; } return sizeof(LinuxSockAddr); } void SAL_NET_SockAddrCopy(BSL_SAL_SockAddr dst, BSL_SAL_SockAddr src) { memcpy_s(dst, sizeof(LinuxSockAddr), src, sizeof(LinuxSockAddr)); } int32_t SAL_Socket(int32_t af, int32_t type, int32_t protocol) { return (int32_t)socket(af, type, protocol); } int32_t SAL_SockClose(int32_t sockId) { if (close((int32_t)(long)sockId) != 0) { return BSL_SAL_ERR_NET_SOCKCLOSE; } return BSL_SUCCESS; } int32_t SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len) { if (setsockopt((int32_t)sockId, level, name, (char *)(uintptr_t)val, (socklen_t)len) != 0) { return BSL_SAL_ERR_NET_SETSOCKOPT; } return BSL_SUCCESS; } int32_t SAL_GetSockopt(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len) { if (getsockopt((int32_t)sockId, level, name, val, (socklen_t *)len) != 0) { return BSL_SAL_ERR_NET_GETSOCKOPT; } return BSL_SUCCESS; } int32_t SAL_SockListen(int32_t sockId, int32_t backlog) { if (listen(sockId, backlog) != 0) { return BSL_SAL_ERR_NET_LISTEN; } return BSL_SUCCESS; } int32_t SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len) { if (bind(sockId, (struct sockaddr *)addr, (socklen_t)len) != 0) { return BSL_SAL_ERR_NET_BIND; } return BSL_SUCCESS; } int32_t SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len) { if (connect(sockId, (struct sockaddr *)addr, (socklen_t)len) != 0) { return BSL_SAL_ERR_NET_CONNECT; } return BSL_SUCCESS; } int32_t SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags) { return (int32_t)send(sockId, msg, len, flags); } int32_t SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags) { return (int32_t)recv(sockfd, (char *)buff, len, flags); } int32_t SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout) { return select(nfds, (fd_set *)readfds, (fd_set *)writefds, (fd_set *)exceptfds, (struct timeval *)timeout); } int32_t SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg) { if (ioctl(sockId, (unsigned long)cmd, arg) != 0) { return BSL_SAL_ERR_NET_IOCTL; } return BSL_SUCCESS; } int32_t SAL_SockGetLastSocketError(void) { return errno; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_sal_net.c
C
unknown
5,700
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_TIME) #include <unistd.h> #include <time.h> #include <sys/time.h> #include <sys/times.h> #include "bsl_sal.h" #include "sal_time.h" #include "bsl_errno.h" int64_t TIME_GetSysTime(void) { return (int64_t)time(NULL); } uint32_t TIME_DateToStrConvert(const BSL_TIME *dateTime, char *timeStr, size_t len) { struct tm timeStruct = {0}; timeStruct.tm_year = (int32_t)dateTime->year - (int32_t)BSL_TIME_YEAR_START; timeStruct.tm_mon = (int32_t)dateTime->month - 1; timeStruct.tm_mday = (int32_t)dateTime->day; timeStruct.tm_hour = (int32_t)dateTime->hour; timeStruct.tm_min = (int32_t)dateTime->minute; timeStruct.tm_sec = (int32_t)dateTime->second; if (asctime_r(&timeStruct, timeStr) != NULL) { return BSL_SUCCESS; } (void)len; return BSL_INTERNAL_EXCEPTION; } uint32_t TIME_SysTimeGet(BSL_TIME *sysTime) { struct timeval tv = {0}; int timeRet = gettimeofday(&tv, NULL); if (timeRet != 0) { return BSL_SAL_TIME_SYS_ERROR; } tzset(); int32_t ret = BSL_SAL_UtcTimeToDateConvert((int64_t)tv.tv_sec, sysTime); if (ret == BSL_SUCCESS) { /* milliseconds : non-thread safe */ sysTime->millSec = (uint16_t)(tv.tv_usec / 1000U); /* 1000 is multiple */ sysTime->microSec = (uint32_t)(tv.tv_usec % 1000U); /* 1000 is multiple */ } return ret; } uint32_t TIME_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime) { struct tm tempTime; time_t utcTimeTmp = (time_t)utcTime; if (gmtime_r(&utcTimeTmp, &tempTime) == NULL) { return BSL_SAL_ERR_BAD_PARAM; } sysTime->year = (uint16_t)((uint16_t)tempTime.tm_year + BSL_TIME_YEAR_START); /* 1900 is base year */ sysTime->month = (uint8_t)((uint8_t)tempTime.tm_mon + 1U); sysTime->day = (uint8_t)tempTime.tm_mday; sysTime->hour = (uint8_t)tempTime.tm_hour; sysTime->minute = (uint8_t)tempTime.tm_min; sysTime->second = (uint8_t)tempTime.tm_sec; sysTime->millSec = 0U; sysTime->microSec = 0U; return BSL_SUCCESS; } void SAL_Sleep(uint32_t time) { sleep(time); } long SAL_Tick(void) { struct tms buf = {0}; clock_t tickCount = times(&buf); return (long)tickCount; } long SAL_TicksPerSec(void) { return sysconf(_SC_CLK_TCK); } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/linux/linux_time_impl.c
C
unknown
2,905
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "sal_atomic.h" #include "bsl_errno.h" int BSL_SAL_AtomicAdd(int *val, int amount, int *ref, BSL_SAL_ThreadLockHandle lock) { if (val == NULL || ref == NULL) { return BSL_NULL_INPUT; } int32_t ret = BSL_SAL_ThreadWriteLock(lock); if (ret != 0) { return ret; } *val += amount; *ref = *val; return BSL_SAL_ThreadUnlock(lock); }
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_atomic.c
C
unknown
927
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #include "bsl_errno.h" #include "bsl_sal.h" #ifdef HITLS_BSL_SAL_NET #include "sal_netimpl.h" #endif #ifdef HITLS_BSL_SAL_TIME #include "sal_timeimpl.h" #endif #ifdef HITLS_BSL_SAL_FILE #include "sal_fileimpl.h" #endif #ifdef HITLS_BSL_SAL_DL #include "sal_dlimpl.h" #endif #include "sal_lockimpl.h" #include "sal_memimpl.h" /* The prefix of BSL_SAL_CB_FUNC_TYPE */ #define BSL_SAL_MEM_CB 0x0100 #define BSL_SAL_THREAD_CB 0x0200 #ifdef HITLS_BSL_SAL_NET #define BSL_SAL_NET_CB 0x0300 #endif #ifdef HITLS_BSL_SAL_TIME #define BSL_SAL_TIME_CB 0x0400 #endif #ifdef HITLS_BSL_SAL_FILE #define BSL_SAL_FILE_CB 0x0500 #endif #ifdef HITLS_BSL_SAL_DL #define BSL_SAL_DL_CB 0x0700 #endif int32_t BSL_SAL_CallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE funcType, void *funcCb) { uint32_t type = (uint32_t)funcType & 0xff00; switch (type) { case BSL_SAL_MEM_CB: return SAL_MemCallBack_Ctrl(funcType, funcCb); case BSL_SAL_THREAD_CB: return SAL_ThreadCallback_Ctrl(funcType, funcCb); #ifdef HITLS_BSL_SAL_NET case BSL_SAL_NET_CB: return SAL_NetCallback_Ctrl(funcType, funcCb); #endif #ifdef HITLS_BSL_SAL_TIME case BSL_SAL_TIME_CB: return SAL_TimeCallback_Ctrl(funcType, funcCb); #endif #ifdef HITLS_BSL_SAL_FILE case BSL_SAL_FILE_CB: return SAL_FileCallBack_Ctrl(funcType, funcCb); #endif #ifdef HITLS_BSL_SAL_DL case BSL_SAL_DL_CB: return SAL_DlCallback_Ctrl(funcType, funcCb); #endif default: return BSL_SAL_ERR_BAD_PARAM; } }
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_ctrl.c
C
unknown
2,159
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_DL) #include <stdio.h> #include <stdint.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "string.h" #include "sal_dlimpl.h" #include "bsl_log_internal.h" static BSL_SAL_DlCallback g_dlCallback = {0}; // Define macro for path reserve length #define BSL_SAL_PATH_RESERVE 10 #define BSL_SAL_PATH_MAX 4095 #define BSL_SAL_NAME_MAX 255 int32_t BSL_SAL_LibNameFormat(BSL_SAL_LibFmtCmd cmd, const char *fileName, char **name) { if (fileName == NULL || name == NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM); return BSL_SAL_ERR_BAD_PARAM; } int32_t ret = 0; char *tempName = NULL; uint32_t dlPathLen = strlen(fileName) + BSL_SAL_PATH_RESERVE; if (dlPathLen > BSL_SAL_NAME_MAX) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_PATH_EXCEED); return BSL_SAL_ERR_DL_PATH_EXCEED; } tempName = (char *)BSL_SAL_Calloc(1, dlPathLen); if (tempName == NULL) { return BSL_MALLOC_FAIL; } switch (cmd) { case BSL_SAL_LIB_FMT_SO: ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s.so", fileName); break; case BSL_SAL_LIB_FMT_LIBSO: ret = snprintf_s(tempName, dlPathLen, dlPathLen, "lib%s.so", fileName); break; case BSL_SAL_LIB_FMT_LIBDLL: ret = snprintf_s(tempName, dlPathLen, dlPathLen, "lib%s.dll", fileName); break; case BSL_SAL_LIB_FMT_DLL: ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s.dll", fileName); break; case BSL_SAL_LIB_FMT_OFF: ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s", fileName); break; default: // Default to the first(BSL_SAL_LIB_FMT_SO) conversion BSL_SAL_Free(tempName); BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM); return BSL_SAL_ERR_BAD_PARAM; } if (ret < 0) { BSL_SAL_Free(tempName); BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } *name = tempName; return BSL_SUCCESS; } int32_t BSL_SAL_LoadLib(const char *fileName, void **handle) { if (fileName == NULL || handle == NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM); return BSL_SAL_ERR_BAD_PARAM; } if (g_dlCallback.pfLoadLib != NULL && g_dlCallback.pfLoadLib != BSL_SAL_LoadLib) { return g_dlCallback.pfLoadLib(fileName, handle); } #ifdef HITLS_BSL_SAL_LINUX return SAL_LoadLib(fileName, handle); #else return BSL_SAL_DL_NO_REG_FUNC; #endif } int32_t BSL_SAL_UnLoadLib(void *handle) { if (handle == NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM); return BSL_SAL_ERR_BAD_PARAM; } if (g_dlCallback.pfUnLoadLib != NULL && g_dlCallback.pfUnLoadLib != BSL_SAL_UnLoadLib) { return g_dlCallback.pfUnLoadLib(handle); } #ifdef HITLS_BSL_SAL_LINUX return SAL_UnLoadLib(handle); #else return BSL_SAL_DL_NO_REG_FUNC; #endif } int32_t BSL_SAL_GetFuncAddress(void *handle, const char *funcName, void **func) { if (handle == NULL || func == NULL) { BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM); return BSL_SAL_ERR_BAD_PARAM; } if (g_dlCallback.pfGetFunc != NULL && g_dlCallback.pfGetFunc != BSL_SAL_GetFuncAddress) { return g_dlCallback.pfGetFunc(handle, funcName, func); } #ifdef HITLS_BSL_SAL_LINUX return SAL_GetFunc(handle, funcName, func); #else return BSL_SAL_DL_NO_REG_FUNC; #endif } int32_t SAL_DlCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_DL_SYM_CB_FUNC || type < BSL_SAL_DL_OPEN_CB_FUNC) { return BSL_SAL_ERR_BAD_PARAM; } uint32_t offset = (uint32_t)(type - BSL_SAL_DL_OPEN_CB_FUNC); ((void **)&g_dlCallback)[offset] = funcCb; return BSL_SUCCESS; } #endif /* HITLS_BSL_SAL_DL */
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_dl.c
C
unknown
4,480
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_DLIMPL_H #define SAL_DLIMPL_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_DL #include <stdint.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif typedef struct { BslSalLoadLib pfLoadLib; BslSalUnLoadLib pfUnLoadLib; BslSalGetFunc pfGetFunc; } BSL_SAL_DlCallback; int32_t SAL_DlCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #ifdef HITLS_BSL_SAL_LINUX /** * @brief Load a dynamic library * @param fileName Name of the library file to load * @param handle Pointer to store the handle of the loaded library * @return 0 on success, non-zero error code on failure */ int32_t SAL_LoadLib(const char *fileName, void **handle); /** * @brief Unload a previously loaded dynamic library * @param handle Handle of the library to unload * @return 0 on success, non-zero error code on failure */ int32_t SAL_UnLoadLib(void *handle); /** * @brief Get a function pointer from a loaded library * @param handle Handle of the loaded library * @param funcName Name of the function to retrieve * @param func Pointer to store the function pointer * @return 0 on success, non-zero error code on failure */ int32_t SAL_GetFunc(void *handle, const char *funcName, void **func); #endif #ifdef __cplusplus } #endif #endif /* HITLS_BSL_SAL_DL */ #endif // SAL_DLIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_dlimpl.h
C
unknown
1,857
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_FILE) #include "bsl_err_internal.h" #include "sal_fileimpl.h" static BSL_SAL_FileCallback g_fileCallBack = { 0 }; int32_t SAL_FileCallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_FILE_LENGTH_CB_FUNC || type < BSL_SAL_FILE_OPEN_CB_FUNC) { return BSL_SAL_FILE_NO_REG_FUNC; } uint32_t offet = (uint32_t)(type - BSL_SAL_FILE_OPEN_CB_FUNC); ((void **)&g_fileCallBack)[offet] = funcCb; return BSL_SUCCESS; } int32_t BSL_SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode) { if (g_fileCallBack.pfFileOpen != NULL && g_fileCallBack.pfFileOpen != BSL_SAL_FileOpen) { return g_fileCallBack.pfFileOpen(stream, path, mode); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FOpen(stream, path, mode); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t BSL_SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len) { if (g_fileCallBack.pfFileRead != NULL && g_fileCallBack.pfFileRead != BSL_SAL_FileRead) { return g_fileCallBack.pfFileRead(stream, buffer, size, num, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FRead(stream, buffer, size, num, len); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t BSL_SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num) { if (g_fileCallBack.pfFileWrite != NULL && g_fileCallBack.pfFileWrite != BSL_SAL_FileWrite) { return g_fileCallBack.pfFileWrite(stream, buffer, size, num); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FWrite(stream, buffer, size, num); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } void BSL_SAL_FileClose(bsl_sal_file_handle stream) { if (g_fileCallBack.pfFileClose != NULL && g_fileCallBack.pfFileClose != BSL_SAL_FileClose) { g_fileCallBack.pfFileClose(stream); return; } #ifdef HITLS_BSL_SAL_LINUX SAL_FILE_FClose(stream); #endif } int32_t BSL_SAL_FileLength(const char *path, size_t *len) { if (g_fileCallBack.pfFileLength != NULL && g_fileCallBack.pfFileLength != BSL_SAL_FileLength) { return g_fileCallBack.pfFileLength(path, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FLength(path, len); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } bool SAL_FileError(bsl_sal_file_handle stream) { if (g_fileCallBack.pfFileError != NULL && g_fileCallBack.pfFileError != SAL_FileError) { return g_fileCallBack.pfFileError(stream); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FError(stream); #else BSL_ERR_PUSH_ERROR(BSL_SAL_FILE_NO_REG_FUNC); return false; #endif } int32_t SAL_FileTell(bsl_sal_file_handle stream, long *pos) { if (g_fileCallBack.pfFileTell != NULL && g_fileCallBack.pfFileTell != SAL_FileTell) { return g_fileCallBack.pfFileTell(stream, pos); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FTell(stream, pos); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t SAL_FileSeek(bsl_sal_file_handle stream, long offset, int32_t origin) { if (g_fileCallBack.pfFileSeek != NULL && g_fileCallBack.pfFileSeek != SAL_FileSeek) { return g_fileCallBack.pfFileSeek(stream, offset, origin); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FSeek(stream, offset, origin); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } char *SAL_FGets(bsl_sal_file_handle stream, char *buf, int32_t readLen) { if (g_fileCallBack.pfFileGets != NULL && g_fileCallBack.pfFileGets != SAL_FGets) { return g_fileCallBack.pfFileGets(stream, buf, readLen); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FGets(stream, buf, readLen); #else BSL_ERR_PUSH_ERROR(BSL_SAL_FILE_NO_REG_FUNC); return NULL; #endif } bool SAL_FPuts(bsl_sal_file_handle stream, const char *buf) { if (g_fileCallBack.pfFilePuts != NULL && g_fileCallBack.pfFilePuts != SAL_FPuts) { return g_fileCallBack.pfFilePuts(stream, buf); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FPuts(stream, buf); #else BSL_ERR_PUSH_ERROR(BSL_SAL_FILE_NO_REG_FUNC); return false; #endif } bool SAL_Flush(bsl_sal_file_handle stream) { if (g_fileCallBack.pfFileFlush != NULL && g_fileCallBack.pfFileFlush != SAL_Flush) { return g_fileCallBack.pfFileFlush(stream); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_Flush(stream); #else BSL_ERR_PUSH_ERROR(BSL_SAL_FILE_NO_REG_FUNC); return false; #endif } int32_t SAL_Feof(bsl_sal_file_handle stream) { if (g_fileCallBack.pfFileEof != NULL && g_fileCallBack.pfFileEof != SAL_Feof) { return g_fileCallBack.pfFileEof(stream); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_Feof(stream); #else BSL_ERR_PUSH_ERROR(BSL_SAL_FILE_NO_REG_FUNC); return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t SAL_FSetAttr(bsl_sal_file_handle stream, int cmd, const void *arg) { if (g_fileCallBack.pfFileSetAttr != NULL && g_fileCallBack.pfFileSetAttr != SAL_FSetAttr) { return g_fileCallBack.pfFileSetAttr(stream, cmd, arg); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FSetAttr(stream, cmd, arg); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t SAL_FGetAttr(bsl_sal_file_handle stream, void *arg) { if (g_fileCallBack.pfFileGetAttr != NULL && g_fileCallBack.pfFileGetAttr != SAL_FGetAttr) { return g_fileCallBack.pfFileGetAttr(stream, arg); } #ifdef HITLS_BSL_SAL_LINUX return SAL_FILE_FGetAttr(stream, arg); #else return BSL_SAL_FILE_NO_REG_FUNC; #endif } int32_t BSL_SAL_ReadFile(const char *path, uint8_t **buff, uint32_t *len) { size_t readLen; size_t fileLen = 0; int32_t ret = BSL_SAL_FileLength(path, &fileLen); if (ret != BSL_SUCCESS) { return ret; } if (fileLen > UINT32_MAX - 1) { return BSL_SAL_ERR_FILE_LENGTH; } bsl_sal_file_handle stream = NULL; ret = BSL_SAL_FileOpen(&stream, path, "rb"); if (ret != BSL_SUCCESS) { return ret; } uint8_t *fileBuff = BSL_SAL_Malloc((uint32_t)fileLen + 1); if (fileBuff == NULL) { BSL_SAL_FileClose(stream); return BSL_MALLOC_FAIL; } ret = BSL_SAL_FileRead(stream, fileBuff, 1, fileLen, &readLen); BSL_SAL_FileClose(stream); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(fileBuff); return ret; } fileBuff[fileLen] = '\0'; *buff = fileBuff; *len = (uint32_t)fileLen; return ret; } int32_t BSL_SAL_WriteFile(const char *path, const uint8_t *buff, uint32_t len) { bsl_sal_file_handle stream = NULL; int32_t ret = BSL_SAL_FileOpen(&stream, path, "wb"); if (ret != BSL_SUCCESS) { return ret; } ret = BSL_SAL_FileWrite(stream, buff, 1, len); BSL_SAL_FileClose(stream); return ret; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_file.c
C
unknown
7,347
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_FILEIMPL_H #define SAL_FILEIMPL_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_FILE #include <stdint.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif typedef struct { BslSalFileOpen pfFileOpen; BslSalFileRead pfFileRead; BslSalFileWrite pfFileWrite; BslSalFileClose pfFileClose; BslSalFileLength pfFileLength; BslSalFileError pfFileError; BslSalFileTell pfFileTell; BslSalFileSeek pfFileSeek; BslSalFGets pfFileGets; BslSalFPuts pfFilePuts; BslSalFlush pfFileFlush; BslSalFeof pfFileEof; BslSalFSetAttr pfFileSetAttr; BslSalFGetAttr pfFileGetAttr; } BSL_SAL_FileCallback; int32_t SAL_FileCallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #ifdef HITLS_BSL_SAL_LINUX int32_t SAL_FILE_FOpen(bsl_sal_file_handle *stream, const char *path, const char *mode); int32_t SAL_FILE_FRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len); int32_t SAL_FILE_FWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num); void SAL_FILE_FClose(bsl_sal_file_handle stream); int32_t SAL_FILE_FLength(const char *path, size_t *len); bool SAL_FILE_FError(bsl_sal_file_handle stream); int32_t SAL_FILE_FTell(bsl_sal_file_handle stream, long *pos); int32_t SAL_FILE_FSeek(bsl_sal_file_handle stream, long offset, int32_t origin); char *SAL_FILE_FGets(bsl_sal_file_handle stream, char *buf, int32_t readLen); bool SAL_FILE_FPuts(bsl_sal_file_handle stream, const char *buf); bool SAL_FILE_Flush(bsl_sal_file_handle stream); int32_t SAL_FILE_Feof(bsl_sal_file_handle stream); int32_t SAL_FILE_FSetAttr(bsl_sal_file_handle stream, int cmd, const void *arg); int32_t SAL_FILE_FGetAttr(bsl_sal_file_handle stream, void *arg); #endif #ifdef __cplusplus } #endif #endif // HITLS_BSL_SAL_FILE #endif // SAL_FILEIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_fileimpl.h
C
unknown
2,378
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_LOCKIMPL_H #define SAL_LOCKIMPL_H #include <stdint.h> #include "hitls_build.h" #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct ThreadCallback { /** * @ingroup bsl_sal * @brief Create a thread lock. * * Create a thread lock. * * @param lock [IN/OUT] Lock handle * @retval #BSL_SUCCESS, created successfully. * @retval #BSL_MALLOC_FAIL, memory space is insufficient and thread lock space cannot be applied for. * @retval #BSL_SAL_ERR_UNKNOWN, thread lock initialization failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t (*pfThreadLockNew)(BSL_SAL_ThreadLockHandle *lock); /** * @ingroup bsl_sal * @brief Release the thread lock. * * Release the thread lock. Ensure that the lock can be released when other threads obtain the lock. * * @param lock [IN] Lock handle */ void (*pfThreadLockFree)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Lock the read operation. * * Lock the read operation. * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t (*pfThreadReadLock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Lock the write operation. * * Lock the write operation. * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t (*pfThreadWriteLock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Unlock * * Unlock * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t (*pfThreadUnlock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Obtain the thread ID. * * Obtain the thread ID. * * @retval Thread ID */ uint64_t (*pfThreadGetId)(void); } BSL_SAL_ThreadCallback; int32_t SAL_ThreadCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #ifdef HITLS_BSL_SAL_LINUX #ifdef HITLS_BSL_SAL_LOCK int32_t SAL_RwLockNew(BSL_SAL_ThreadLockHandle *lock); int32_t SAL_RwReadLock(BSL_SAL_ThreadLockHandle rwLock); int32_t SAL_RwWriteLock(BSL_SAL_ThreadLockHandle rwLock); int32_t SAL_RwUnlock(BSL_SAL_ThreadLockHandle rwLock); void SAL_RwLockFree(BSL_SAL_ThreadLockHandle rwLock); #endif #ifdef HITLS_BSL_SAL_THREAD int32_t SAL_PthreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc); uint64_t SAL_GetPid(void); #endif #endif #ifdef __cplusplus } #endif // __cplusplus #endif // SAL_LOCKIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_lockimpl.h
C
unknown
3,609
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include <stdlib.h> #include "securec.h" #include "hitls_build.h" #include "bsl_log_internal.h" #include "bsl_errno.h" #include "bsl_sal.h" #include "bsl_binlog_id.h" #include "sal_memimpl.h" static BSL_SAL_MemCallback g_memCallback = {0}; void *BSL_SAL_Malloc(uint32_t size) { // When size is 0, malloc of different systems may return NULL or non-NULL. Here, a definite result is required. // If the callback is registered, everything is determined by the callback. if (g_memCallback.pfMalloc != NULL && g_memCallback.pfMalloc != BSL_SAL_Malloc) { return g_memCallback.pfMalloc(size); } if (size == 0) { return NULL; } #if defined(HITLS_BSL_SAL_MEM) && defined(HITLS_BSL_SAL_LINUX) return SAL_MallocImpl(size); #else return NULL; #endif } void BSL_SAL_Free(void *value) { if (g_memCallback.pfFree == NULL || g_memCallback.pfFree == BSL_SAL_Free) { #if defined(HITLS_BSL_SAL_MEM) && defined(HITLS_BSL_SAL_LINUX) SAL_FreeImpl(value); #endif return; } g_memCallback.pfFree(value); } void *BSL_SAL_Calloc(uint32_t num, uint32_t size) { if (num == 0 || size == 0) { return BSL_SAL_Malloc(0); } if (num > UINT32_MAX / size) { // process the rewinding according to G.INT.02 in the HW C Coding Specifications V5.1 return NULL; } uint32_t blockSize = num * size; uint8_t *ptr = BSL_SAL_Malloc(blockSize); if (ptr == NULL) { return NULL; } // If the value is greater than SECUREC_MEM_MAX_LEN, segment processing is required. // This is because memset_s can process only the value which the size is SECUREC_MEM_MAX_LEN. uint32_t offset = 0; while (blockSize > SECUREC_MEM_MAX_LEN) { if (memset_s(&ptr[offset], SECUREC_MEM_MAX_LEN, 0, SECUREC_MEM_MAX_LEN) != EOK) { BSL_SAL_FREE(ptr); return NULL; } offset += SECUREC_MEM_MAX_LEN; blockSize -= SECUREC_MEM_MAX_LEN; } if (memset_s(&ptr[offset], blockSize, 0, blockSize) != EOK) { BSL_SAL_FREE(ptr); return NULL; } return ptr; } void *BSL_SAL_Realloc(void *addr, uint32_t newSize, uint32_t oldSize) { if (addr == NULL) { return BSL_SAL_Malloc(newSize); } uint32_t minSize = (oldSize > newSize) ? newSize : oldSize; void *ptr = BSL_SAL_Malloc(newSize); if (ptr == NULL) { return NULL; } if (memcpy_s(ptr, newSize, addr, minSize) != EOK) { BSL_SAL_FREE(ptr); } else { BSL_SAL_FREE(addr); } return ptr; } void *BSL_SAL_Dump(const void *src, uint32_t size) { if (src == NULL) { return NULL; } void *ptr = BSL_SAL_Malloc(size); if (ptr == NULL) { return NULL; } if (memcpy_s(ptr, size, src, size) != EOK) { BSL_SAL_FREE(ptr); return NULL; } return ptr; } int32_t SAL_MemCallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_MEM_FREE || type < BSL_SAL_MEM_MALLOC) { return BSL_SAL_ERR_BAD_PARAM; } uint32_t offset = (uint32_t)(type - BSL_SAL_MEM_MALLOC); ((void **)&g_memCallback)[offset] = funcCb; return BSL_SUCCESS; } #if !defined(__clang__) #pragma GCC push_options #pragma GCC optimize("O3") #endif #define CLEAN_THRESHOLD_SIZE 16UL static void CleanSensitiveDataLess16Byte(void *buf, uint32_t bufLen) { uint8_t *tmp = (uint8_t *)buf; switch (bufLen) { case 16: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 15: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 14: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 13: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 12: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 11: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 10: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 9: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 8: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 7: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 6: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 5: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 4: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 3: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 2: *(tmp++) = (uint8_t)0; /* FALLTHRU */ case 1: *(tmp) = (uint8_t)0; /* FALLTHRU */ default: break; } } static void CleanSensitiveData(void *buf, uint32_t bufLen) { uint8_t *tmp = (uint8_t *)buf; uint32_t boundOpt; if (((uintptr_t)buf & 0x3) == 0) { // buf & 0x3, used to determine whether 4-byte alignment // shift rightwards by 4 bits and then leftwards by 4 bits, which is used to calculate an integer multiple of 16 boundOpt = (bufLen >> 4) << 4; for (uint32_t i = 0; i < boundOpt; i += 16) { // Clear 16 pieces of memory each time. uint32_t *ctmp = (uint32_t *)(tmp + i); ctmp[0] = 0; ctmp[1] = 0; ctmp[2] = 0; ctmp[3] = 0; } } else { // shifted rightward by 2 bits and then left by 2 bits, used to calculate an integer multiple of 4. boundOpt = (bufLen >> 2) << 2; for (uint32_t i = 0; i < boundOpt; i += 4) { // Clear 4 pieces of memory each time. tmp[i] = 0; tmp[i + 1] = 0; tmp[i + 2] = 0; tmp[i + 3] = 0; } } for (uint32_t i = boundOpt; i < bufLen; ++i) { tmp[i] = 0; } } void BSL_SAL_CleanseData(void *ptr, uint32_t size) { if (ptr == NULL) { return; } if (size > CLEAN_THRESHOLD_SIZE) { CleanSensitiveData(ptr, size); } else { CleanSensitiveDataLess16Byte(ptr, size); } } void BSL_SAL_ClearFree(void *ptr, uint32_t size) { if (ptr == NULL) { return; } if (size != 0) { BSL_SAL_CleanseData(ptr, size); } BSL_SAL_FREE(ptr); } #if !defined(__clang__) #pragma GCC pop_options #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_mem.c
C
unknown
6,598
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_MEMIMPL_H #define SAL_MEMIMPL_H #include <stdint.h> #include "hitls_build.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_sal * * Registrable function structure for memory allocation/release. */ typedef struct MemCallback { /** * @ingroup bsl_sal * @brief Allocate a memory block. * * Allocate a memory block. * * @param size [IN] Size of the allocated memory. * @retval: Not NULL, The start address of the allocated memory when memory is allocated successfully. * @retval NULL, Memory allocation failure. */ void *(*pfMalloc)(uint32_t size); /** * @ingroup bsl_sal * @brief Reclaim a memory block allocated by pfMalloc. * * Reclaim a block of memory allocated by pfMalloc. * * @param addr [IN] Start address of the memory allocated by pfMalloc. */ void (*pfFree)(void *addr); } BSL_SAL_MemCallback; int32_t SAL_MemCallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #if defined(HITLS_BSL_SAL_MEM) && defined(HITLS_BSL_SAL_LINUX) void *SAL_MallocImpl(uint32_t size); void SAL_FreeImpl(void *value); #endif #ifdef __cplusplus } #endif #endif // SAL_MEMIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_memimpl.h
C
unknown
1,741
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_BSL_SAL_NET) #include <stdint.h> #include "bsl_sal.h" #include "bsl_errno.h" #include "sal_netimpl.h" static BSL_SAL_NetCallback g_netCallback = {0}; int32_t SAL_NetCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_NET_GETFAMILY_CB_FUNC || type < BSL_SAL_NET_WRITE_CB_FUNC) { return BSL_SAL_NET_NO_REG_FUNC; } uint32_t offset = (uint32_t)(type - BSL_SAL_NET_WRITE_CB_FUNC); ((void **)&g_netCallback)[offset] = funcCb; return BSL_SUCCESS; } int32_t SAL_Write(int32_t fd, const void *buf, uint32_t len, int32_t *err) { if (buf == NULL || len == 0 || err == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (g_netCallback.pfWrite != NULL && g_netCallback.pfWrite != SAL_Write) { return g_netCallback.pfWrite(fd, buf, len, err); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_Write(fd, buf, len, err); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t SAL_Read(int32_t fd, void *buf, uint32_t len, int32_t *err) { if (buf == NULL || len == 0 || err == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (g_netCallback.pfRead != NULL && g_netCallback.pfRead != SAL_Read) { return g_netCallback.pfRead(fd, buf, len, err); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_Read(fd, buf, len, err); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t SAL_Sendto(int32_t sock, const void *buf, size_t len, int32_t flags, BSL_SAL_SockAddr address, int32_t addrLen, int32_t *err) { if (buf == NULL || len == 0 || address == NULL || addrLen == 0 || err == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (g_netCallback.pfSendTo != NULL && g_netCallback.pfSendTo != SAL_Sendto) { return g_netCallback.pfSendTo(sock, buf, len, flags, address, addrLen, err); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_Sendto(sock, buf, len, flags, address, addrLen, err); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t SAL_RecvFrom(int32_t sock, void *buf, size_t len, int32_t flags, BSL_SAL_SockAddr address, int32_t *addrLen, int32_t *err) { if (buf == NULL || len == 0 || err == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (g_netCallback.pfRecvFrom != NULL && g_netCallback.pfRecvFrom != SAL_RecvFrom) { return g_netCallback.pfRecvFrom(sock, buf, len, flags, address, addrLen, err); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_RecvFrom(sock, buf, len, flags, address, addrLen, err); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t SAL_SockAddrNew(BSL_SAL_SockAddr *sockAddr) { if (g_netCallback.pfSockAddrNew != NULL && g_netCallback.pfSockAddrNew != SAL_SockAddrNew) { return g_netCallback.pfSockAddrNew(sockAddr); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_SockAddrNew(sockAddr); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } void SAL_SockAddrFree(BSL_SAL_SockAddr sockAddr) { if (g_netCallback.pfSockAddrFree != NULL && g_netCallback.pfSockAddrFree != SAL_SockAddrFree) { return g_netCallback.pfSockAddrFree(sockAddr); } #ifdef HITLS_BSL_SAL_LINUX SAL_NET_SockAddrFree(sockAddr); return; #endif } int32_t SAL_SockAddrGetFamily(const BSL_SAL_SockAddr sockAddr) { if (g_netCallback.pfSockAddrGetFamily != NULL && g_netCallback.pfSockAddrGetFamily != SAL_SockAddrGetFamily) { return g_netCallback.pfSockAddrGetFamily(sockAddr); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_SockAddrGetFamily(sockAddr); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } uint32_t SAL_SockAddrSize(const BSL_SAL_SockAddr sockAddr) { if (g_netCallback.pfSockAddrSize != NULL && g_netCallback.pfSockAddrSize != SAL_SockAddrSize) { return g_netCallback.pfSockAddrSize(sockAddr); } #ifdef HITLS_BSL_SAL_LINUX return SAL_NET_SockAddrSize(sockAddr); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } void SAL_SockAddrCopy(BSL_SAL_SockAddr dst, BSL_SAL_SockAddr src) { if (g_netCallback.pfSockAddrCopy != NULL && g_netCallback.pfSockAddrCopy != SAL_SockAddrCopy) { return g_netCallback.pfSockAddrCopy(src, dst); } #ifdef HITLS_BSL_SAL_LINUX SAL_NET_SockAddrCopy(src, dst); return; #endif } int32_t BSL_SAL_Socket(int32_t af, int32_t type, int32_t protocol) { if (g_netCallback.pfSocket != NULL && g_netCallback.pfSocket != BSL_SAL_Socket) { return g_netCallback.pfSocket(af, type, protocol); } #ifdef HITLS_BSL_SAL_LINUX return SAL_Socket(af, type, protocol); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockClose(int32_t sockId) { if (g_netCallback.pfSockClose != NULL && g_netCallback.pfSockClose != BSL_SAL_SockClose) { return g_netCallback.pfSockClose(sockId); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockClose(sockId); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len) { if (g_netCallback.pfSetSockopt != NULL && g_netCallback.pfSetSockopt != BSL_SAL_SetSockopt) { return g_netCallback.pfSetSockopt(sockId, level, name, val, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SetSockopt(sockId, level, name, val, len); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_GetSockopt(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len) { if (g_netCallback.pfGetSockopt != NULL && g_netCallback.pfGetSockopt != BSL_SAL_GetSockopt) { return g_netCallback.pfGetSockopt(sockId, level, name, val, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_GetSockopt(sockId, level, name, val, len); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockListen(int32_t sockId, int32_t backlog) { if (g_netCallback.pfSockListen != NULL && g_netCallback.pfSockListen != BSL_SAL_SockListen) { return g_netCallback.pfSockListen(sockId, backlog); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockListen(sockId, backlog); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len) { if (g_netCallback.pfSockBind != NULL && g_netCallback.pfSockBind != BSL_SAL_SockBind) { return g_netCallback.pfSockBind(sockId, addr, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockBind(sockId, addr, len); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len) { if (g_netCallback.pfSockConnect != NULL && g_netCallback.pfSockConnect != BSL_SAL_SockConnect) { return g_netCallback.pfSockConnect(sockId, addr, len); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockConnect(sockId, addr, len); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags) { if (g_netCallback.pfSockSend != NULL && g_netCallback.pfSockSend != BSL_SAL_SockSend) { return g_netCallback.pfSockSend(sockId, msg, len, flags); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockSend(sockId, msg, len, flags); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags) { if (g_netCallback.pfSockRecv != NULL && g_netCallback.pfSockRecv != BSL_SAL_SockRecv) { return g_netCallback.pfSockRecv(sockfd, buff, len, flags); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockRecv(sockfd, buff, len, flags); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout) { if (g_netCallback.pfSelect != NULL && g_netCallback.pfSelect != BSL_SAL_Select) { return g_netCallback.pfSelect(nfds, readfds, writefds, exceptfds, timeout); } #ifdef HITLS_BSL_SAL_LINUX return SAL_Select(nfds, readfds, writefds, exceptfds, timeout); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg) { if (g_netCallback.pfIoctlsocket != NULL && g_netCallback.pfIoctlsocket != BSL_SAL_Ioctlsocket) { return g_netCallback.pfIoctlsocket(sockId, cmd, arg); } #ifdef HITLS_BSL_SAL_LINUX return SAL_Ioctlsocket(sockId, cmd, arg); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } int32_t BSL_SAL_SockGetLastSocketError(void) { if (g_netCallback.pfSockGetLastSocketError != NULL && g_netCallback.pfSockGetLastSocketError != BSL_SAL_SockGetLastSocketError) { return g_netCallback.pfSockGetLastSocketError(); } #ifdef HITLS_BSL_SAL_LINUX return SAL_SockGetLastSocketError(); #else return BSL_SAL_NET_NO_REG_FUNC; #endif } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_net.c
C
unknown
9,298
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_NETIMPL_H #define SAL_NETIMPL_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_NET #include <stdint.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct { BslSalWrite pfWrite; BslSalRead pfRead; BslSalSocket pfSocket; BslSalSockClose pfSockClose; BslSalSetSockopt pfSetSockopt; BslSalGetSockopt pfGetSockopt; BslSalSockListen pfSockListen; BslSalSockBind pfSockBind; BslSalSockConnect pfSockConnect; BslSalSockSend pfSockSend; BslSalSockRecv pfSockRecv; BslSalSelect pfSelect; BslSalIoctlsocket pfIoctlsocket; BslSalSockGetLastSocketError pfSockGetLastSocketError; BslSalSockAddrNew pfSockAddrNew; BslSalSockAddrFree pfSockAddrFree; BslSalSockAddrSize pfSockAddrSize; BslSalSockAddrCopy pfSockAddrCopy; BslSalNetSendTo pfSendTo; BslSalNetRecvFrom pfRecvFrom; BslSalSockAddrGetFamily pfSockAddrGetFamily; } BSL_SAL_NetCallback; int32_t SAL_NetCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #ifdef HITLS_BSL_SAL_LINUX int32_t SAL_NET_Write(int32_t fd, const void *buf, uint32_t len, int32_t *err); int32_t SAL_NET_Read(int32_t fd, void *buf, uint32_t len, int32_t *err); int32_t SAL_NET_SockAddrNew(BSL_SAL_SockAddr *sockAddr); void SAL_NET_SockAddrFree(BSL_SAL_SockAddr sockAddr); int32_t SAL_NET_SockAddrGetFamily(const BSL_SAL_SockAddr sockAddr); uint32_t SAL_NET_SockAddrSize(const BSL_SAL_SockAddr sockAddr); void SAL_NET_SockAddrCopy(BSL_SAL_SockAddr dst, BSL_SAL_SockAddr src); int32_t SAL_NET_Sendto(int32_t sock, const void *buf, size_t len, int32_t flags, void *address, int32_t addrLen, int32_t *err); int32_t SAL_NET_RecvFrom(int32_t sock, void *buf, size_t len, int32_t flags, void *address, int32_t *addrLen, int32_t *err); int32_t SAL_Socket(int32_t af, int32_t type, int32_t protocol); int32_t SAL_SockClose(int32_t sockId); int32_t SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len); int32_t SAL_GetSockopt(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len); int32_t SAL_SockListen(int32_t sockId, int32_t backlog); int32_t SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); int32_t SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); int32_t SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags); int32_t SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags); int32_t SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout); int32_t SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg); int32_t SAL_SockGetLastSocketError(void); #endif #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_BSL_SAL_NET #endif // SAL_NETIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_netimpl.h
C
unknown
3,355
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_SAL_STR #include <stdlib.h> #include <stdint.h> #include <string.h> #include "securec.h" #include "bsl_errno.h" int32_t BSL_SAL_StrcaseCmp(const char *str1, const char *str2) { if (str1 == NULL || str2 == NULL) { return BSL_NULL_INPUT; } const char *tmpStr1 = str1; const char *tmpStr2 = str2; uint8_t t1 = 0; uint8_t t2 = 0; uint8_t sub = 'a' - 'A'; for (; (*tmpStr1 != '\0') && (*tmpStr2 != '\0'); tmpStr1++, tmpStr2++) { t1 = (uint8_t)*tmpStr1; t2 = (uint8_t)*tmpStr2; if (t1 >= 'A' && t1 <= 'Z') { t1 = t1 + sub; } if (t2 >= 'A' && t2 <= 'Z') { t2 = t2 + sub; } if (t1 != t2) { break; } } return (int32_t)(*tmpStr1) - (int32_t)(*tmpStr2); } void *BSL_SAL_Memchr(const char *str, int32_t character, size_t count) { if (str == NULL) { return NULL; } for (size_t i = 0; i < count; i++) { if ((int32_t)str[i] == character) { return (void *)(uintptr_t)(str + i); } } return NULL; } int32_t BSL_SAL_Atoi(const char *str) { int val = 0; if (str == NULL) { return 0; } if (sscanf_s(str, "%d", &val) != -1) { return (int32_t)val; } return 0; } uint32_t BSL_SAL_Strnlen(const char *string, uint32_t count) { uint32_t n; const char *pscTemp = string; if (pscTemp == NULL) { return 0; } for (n = 0; (n < count) && (*pscTemp != '\0'); n++) { pscTemp++; } return n; } #endif
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_string.c
C
unknown
2,149
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include <stddef.h> #include <pthread.h> #include "hitls_build.h" #include "bsl_binlog_id.h" #include "bsl_log_internal.h" #include "bsl_log.h" #include "bsl_errno.h" #include "sal_lockimpl.h" #include "bsl_sal.h" static BSL_SAL_ThreadCallback g_threadCallback = {0}; int32_t BSL_SAL_ThreadLockNew(BSL_SAL_ThreadLockHandle *lock) { if ((g_threadCallback.pfThreadLockNew != NULL) && (g_threadCallback.pfThreadLockNew != BSL_SAL_ThreadLockNew)) { return g_threadCallback.pfThreadLockNew(lock); } #if defined (HITLS_BSL_SAL_LOCK) && defined(HITLS_BSL_SAL_LINUX) return SAL_RwLockNew(lock); #else return BSL_SUCCESS; #endif } int32_t BSL_SAL_ThreadReadLock(BSL_SAL_ThreadLockHandle lock) { if ((g_threadCallback.pfThreadReadLock != NULL) && (g_threadCallback.pfThreadReadLock != BSL_SAL_ThreadReadLock)) { return g_threadCallback.pfThreadReadLock(lock); } #if defined (HITLS_BSL_SAL_LOCK) && defined(HITLS_BSL_SAL_LINUX) return SAL_RwReadLock(lock); #else return BSL_SUCCESS; #endif } int32_t BSL_SAL_ThreadWriteLock(BSL_SAL_ThreadLockHandle lock) { if ((g_threadCallback.pfThreadWriteLock != NULL) && (g_threadCallback.pfThreadWriteLock != BSL_SAL_ThreadWriteLock)) { return g_threadCallback.pfThreadWriteLock(lock); } #if defined (HITLS_BSL_SAL_LOCK) && defined(HITLS_BSL_SAL_LINUX) return SAL_RwWriteLock(lock); #else return BSL_SUCCESS; #endif } int32_t BSL_SAL_ThreadUnlock(BSL_SAL_ThreadLockHandle lock) { if ((g_threadCallback.pfThreadUnlock != NULL) && (g_threadCallback.pfThreadUnlock != BSL_SAL_ThreadUnlock)) { return g_threadCallback.pfThreadUnlock(lock); } #if defined (HITLS_BSL_SAL_LOCK) && defined(HITLS_BSL_SAL_LINUX) return SAL_RwUnlock(lock); #else return BSL_SUCCESS; #endif } void BSL_SAL_ThreadLockFree(BSL_SAL_ThreadLockHandle lock) { if ((g_threadCallback.pfThreadLockFree != NULL) && (g_threadCallback.pfThreadLockFree != BSL_SAL_ThreadLockFree)) { g_threadCallback.pfThreadLockFree(lock); return; } #if defined (HITLS_BSL_SAL_LOCK) && defined(HITLS_BSL_SAL_LINUX) SAL_RwLockFree(lock); #endif } uint64_t BSL_SAL_ThreadGetId(void) { if ((g_threadCallback.pfThreadGetId != NULL) && (g_threadCallback.pfThreadGetId != BSL_SAL_ThreadGetId)) { return g_threadCallback.pfThreadGetId(); } #if defined (HITLS_BSL_SAL_THREAD) && defined(HITLS_BSL_SAL_LINUX) return SAL_GetPid(); #else return BSL_SUCCESS; #endif } int32_t BSL_SAL_ThreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc) { if (onceControl == NULL || initFunc == NULL) { return BSL_SAL_ERR_BAD_PARAM; } #if defined (HITLS_BSL_SAL_THREAD) && defined(HITLS_BSL_SAL_LINUX) return SAL_PthreadRunOnce(onceControl, initFunc); #else if (*onceControl == 1) { return BSL_SUCCESS; } initFunc(); *onceControl = 1; return BSL_SUCCESS; #endif } int32_t SAL_ThreadCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_THREAD_GET_ID_CB_FUNC || type < BSL_SAL_THREAD_LOCK_NEW_CB_FUNC) { return BSL_SAL_ERR_BAD_PARAM; } uint32_t offset = (uint32_t)(type - BSL_SAL_THREAD_LOCK_NEW_CB_FUNC); ((void **)&g_threadCallback)[offset] = funcCb; return BSL_SUCCESS; }
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_threadlock.c
C
unknown
3,845
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_SAL_TIME #ifdef HITLS_BSL_ERR #include "bsl_err_internal.h" #endif #include "bsl_sal.h" #include "sal_timeimpl.h" #include "bsl_errno.h" #include "sal_time.h" static BSL_SAL_TimeCallback g_timeCallback = {0}; int32_t SAL_TimeCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb) { if (type > BSL_SAL_TIME_TICK_PER_SEC_CB_FUNC || type < BSL_SAL_TIME_GET_UTC_TIME_CB_FUNC) { return BSL_SAL_TIME_NO_REG_FUNC; } uint32_t offset = (uint32_t)(type - BSL_SAL_TIME_GET_UTC_TIME_CB_FUNC); ((void **)&g_timeCallback)[offset] = funcCb; return BSL_SUCCESS; } void BSL_SAL_SysTimeFuncReg(BslTimeFunc func) { if (func != NULL) { g_timeCallback.pfGetSysTime = func; } return; } void BSL_SysTimeFuncUnReg(void) { g_timeCallback.pfGetSysTime = NULL; return; } bool BSL_IsLeapYear(uint32_t year) { return ((((year % 4U) == 0U) && ((year % 100U) != 0U)) || ((year % 400U) == 0U)); } static int64_t BslMkTime64Get(const BSL_TIME *inputTime) { int64_t result; uint32_t i; int32_t unixYear; int32_t unixDay; int32_t extraDay = 0; int32_t year = inputTime->year; int32_t month = inputTime->month - 1; int32_t day = inputTime->day; int32_t hour = inputTime->hour; int32_t minute = inputTime->minute; int32_t second = inputTime->second; int32_t monthTable[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; for (i = BSL_TIME_SYSTEM_EPOCH_YEAR; (int32_t)i < year; i++) { if (BSL_IsLeapYear(i) == true) { extraDay++; } } unixYear = year - (int32_t)BSL_TIME_SYSTEM_EPOCH_YEAR; if (BSL_IsLeapYear((uint32_t)year) == true) { for (i = BSL_MONTH_FEB; i < BSL_MONTH_DEC; i++) { monthTable[i] = monthTable[i] + 1; } } unixDay = (unixYear * (int32_t)BSL_TIME_DAY_PER_NONLEAP_YEAR) + monthTable[month] + (day - 1) + extraDay; result = unixDay * (int64_t)86400; /* 86400 is the number of seconds in a day */ result = (hour * (int64_t)3600) + result; /* 3600 is the number of seconds in a hour */ result = (minute * (int64_t)60) + second + result; /* 60 is the number of seconds in a minute */ return result; } /** * @brief Convert the given date structure to the number of seconds since January 1,1970 * @param inputTime [IN] Pointer to the date to be converted. * @param utcTime [OUT] Pointer to the storage of the conversion result * @return BSL_SUCCESS successfully executed. * BSL_INTERNAL_EXCEPTION Execution Failure */ static uint32_t BslUtcTimeGet(const BSL_TIME *inputTime, int64_t *utcTime) { int64_t result; if (inputTime == NULL || utcTime == NULL) { return BSL_INTERNAL_EXCEPTION; } if (BSL_DateTimeCheck(inputTime) == false) { return BSL_INTERNAL_EXCEPTION; } result = BslMkTime64Get(inputTime); if (result < 0) { *utcTime = -1; return BSL_INTERNAL_EXCEPTION; } else { *utcTime = result; return BSL_SUCCESS; } } #define BSL_TIMESTR_MINLEN 26 uint32_t BSL_DateToStrConvert(const BSL_TIME *dateTime, char *timeStr, size_t len) { if (dateTime == NULL || timeStr == NULL || len < BSL_TIMESTR_MINLEN) { return BSL_INTERNAL_EXCEPTION; } if (BSL_DateTimeCheck(dateTime) != true) { return BSL_INTERNAL_EXCEPTION; } if (g_timeCallback.pfDateToStrConvert != NULL && g_timeCallback.pfDateToStrConvert != BSL_DateToStrConvert) { return g_timeCallback.pfDateToStrConvert(dateTime, timeStr, len); } #ifdef HITLS_BSL_SAL_LINUX return TIME_DateToStrConvert(dateTime, timeStr, len); #else return BSL_SAL_TIME_NO_REG_FUNC; #endif } int64_t BSL_SAL_CurrentSysTimeGet(void) { if (g_timeCallback.pfGetSysTime != NULL && g_timeCallback.pfGetSysTime != BSL_SAL_CurrentSysTimeGet) { return g_timeCallback.pfGetSysTime(); } #ifdef HITLS_BSL_SAL_LINUX return TIME_GetSysTime(); #else BSL_ERR_PUSH_ERROR(BSL_SAL_TIME_NO_REG_FUNC); return 0; #endif } static uint32_t BslDateTimeCmpCheck(const BSL_TIME *dateA, int64_t *utcTimeA, const BSL_TIME *dateB, int64_t *utcTimeB) { if ((dateA == NULL) || (dateB == NULL)) { return BSL_INTERNAL_EXCEPTION; } if (BslUtcTimeGet(dateA, utcTimeA) != BSL_SUCCESS) { return BSL_INTERNAL_EXCEPTION; } if (BslUtcTimeGet(dateB, utcTimeB) != BSL_SUCCESS) { return BSL_INTERNAL_EXCEPTION; } return BSL_SUCCESS; } int32_t BSL_SAL_DateTimeCompare(const BSL_TIME *dateA, const BSL_TIME *dateB, int64_t *diffSec) { int64_t utcTimeA = 0; int64_t utcTimeB = 0; int64_t dTimeDiff; uint32_t ret; if (BslDateTimeCmpCheck(dateA, &utcTimeA, dateB, &utcTimeB) == BSL_SUCCESS) { dTimeDiff = utcTimeA - utcTimeB; if (diffSec != NULL) { *diffSec = dTimeDiff; } if (dTimeDiff < 0) { ret = (uint32_t)BSL_TIME_DATE_BEFORE; } else if (dTimeDiff > 0) { ret = (uint32_t)BSL_TIME_DATE_AFTER; } else { ret = (uint32_t)BSL_TIME_CMP_EQUAL; } } else { ret = (uint32_t)BSL_TIME_CMP_ERROR; } return ret; } static uint32_t TimeCmp(uint32_t a, uint32_t b) { if (a > b) { return BSL_TIME_DATE_AFTER; } if (a < b) { return BSL_TIME_DATE_BEFORE; } return BSL_TIME_CMP_EQUAL; } int32_t BSL_SAL_DateTimeCompareByUs(const BSL_TIME *dateA, const BSL_TIME *dateB) { int64_t diffSec = 0; uint32_t ret; ret = BSL_SAL_DateTimeCompare(dateA, dateB, &diffSec); if (ret != BSL_TIME_CMP_EQUAL) { return ret; } ret = TimeCmp(dateA->millSec, dateB->millSec); if (ret != BSL_TIME_CMP_EQUAL) { return ret; } return TimeCmp(dateA->microSec, dateB->microSec); } uint32_t BSL_DateTimeAddUs(BSL_TIME *dateR, const BSL_TIME *dateA, uint32_t us) { uint32_t ret; int64_t utcTime = 0; /* Convert the date into seconds. */ ret = BSL_SAL_DateToUtcTimeConvert(dateA, &utcTime); if (ret != BSL_SUCCESS) { return ret; } /* Convert the increased time to seconds */ uint32_t microSec = us + dateA->microSec; uint32_t millSec = (microSec / BSL_SECOND_TRANSFER_RATIO) + dateA->millSec; microSec %= BSL_SECOND_TRANSFER_RATIO; uint32_t second = millSec / BSL_SECOND_TRANSFER_RATIO; millSec %= BSL_SECOND_TRANSFER_RATIO; /* Convert to the date after the number of seconds is added */ utcTime += (int64_t)second; ret = BSL_SAL_UtcTimeToDateConvert(utcTime, dateR); if (ret != BSL_SUCCESS) { return ret; } /* Complete milliseconds and microseconds. */ dateR->millSec = (uint16_t)millSec; dateR->microSec = microSec; return BSL_SUCCESS; } int32_t BSL_SAL_DateToUtcTimeConvert(const BSL_TIME *dateTime, int64_t *utcTime) { uint32_t ret = BSL_INTERNAL_EXCEPTION; if ((dateTime != NULL) && (utcTime != NULL)) { if (BSL_DateTimeCheck(dateTime) == true) { ret = BslUtcTimeGet(dateTime, utcTime); } } return ret; } static bool BslFebDayValidCheck(uint16_t year, uint8_t day) { bool ret; if ((BSL_IsLeapYear(year) == true) && (day <= BSL_TIME_LEAP_FEBRUARY_DAY)) { ret = true; } else if ((BSL_IsLeapYear(year) == false) && (day <= BSL_TIME_NOLEAP_FEBRUARY_DAY)) { ret = true; } else { ret = false; } return ret; } static bool BslDayValidCheck(uint16_t year, uint8_t month, uint8_t day) { bool ret = true; switch (month) { case BSL_MONTH_JAN: case BSL_MONTH_MAR: case BSL_MONTH_MAY: case BSL_MONTH_JUL: case BSL_MONTH_AUG: case BSL_MONTH_OCT: case BSL_MONTH_DEC: if (day > BSL_TIME_BIG_MONTH_DAY) { ret = false; } break; case BSL_MONTH_APR: case BSL_MONTH_JUN: case BSL_MONTH_SEM: case BSL_MONTH_NOV: if (day > BSL_TIME_SMALL_MONTH_DAY) { ret = false; } break; case BSL_MONTH_FEB: ret = BslFebDayValidCheck(year, day); break; default: ret = false; break; } return ret; } static bool BslYearMonthDayCheck(const BSL_TIME *dateTime) { if (dateTime->year < BSL_TIME_SYSTEM_EPOCH_YEAR) { return false; } else if ((dateTime->month < BSL_MONTH_JAN) || (dateTime->month > BSL_MONTH_DEC)) { return false; } else if (dateTime->day < BSL_MONTH_JAN) { return false; } else { return BslDayValidCheck(dateTime->year, dateTime->month, dateTime->day); } } static bool BslHourMinSecCheck(const BSL_TIME *dateTime) { bool ret; if (dateTime->hour > 23U) { ret = false; } else if (dateTime->minute > 59U) { ret = false; } else if (dateTime->second > 59U) { ret = false; } else if (dateTime->millSec > 999U) { ret = false; } else if (dateTime->microSec > 999U) { /* microseconds does not exceed the maximum value 1000 */ ret = false; } else { ret = true; } return ret; } bool BSL_DateTimeCheck(const BSL_TIME *dateTime) { bool ret = true; if ((BslYearMonthDayCheck(dateTime) == false) || (BslHourMinSecCheck(dateTime) == false)) { ret = false; } return ret; } int32_t BSL_SAL_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime) { if (sysTime == NULL || utcTime > BSL_UTCTIME_MAX) { return BSL_SAL_ERR_BAD_PARAM; } if (g_timeCallback.pfUtcTimeToDateConvert != NULL && g_timeCallback.pfUtcTimeToDateConvert != (BslSalUtcTimeToDateConvert)BSL_SAL_UtcTimeToDateConvert) { return g_timeCallback.pfUtcTimeToDateConvert(utcTime, sysTime); } #ifdef HITLS_BSL_SAL_LINUX return TIME_UtcTimeToDateConvert(utcTime, sysTime); #else return BSL_SAL_TIME_NO_REG_FUNC; #endif } int32_t BSL_SAL_SysTimeGet(BSL_TIME *sysTime) { if (sysTime == NULL) { return BSL_SAL_ERR_BAD_PARAM; } if (g_timeCallback.pfSysTimeGet != NULL && g_timeCallback.pfSysTimeGet != (BslSalSysTimeGet)BSL_SAL_SysTimeGet) { return g_timeCallback.pfSysTimeGet(sysTime); } #ifdef HITLS_BSL_SAL_LINUX return TIME_SysTimeGet(sysTime); #else return BSL_SAL_TIME_NO_REG_FUNC; #endif } void BSL_SAL_Sleep(uint32_t time) { if (g_timeCallback.pfSleep != NULL && g_timeCallback.pfSleep != BSL_SAL_Sleep) { g_timeCallback.pfSleep(time); return; } #ifdef HITLS_BSL_SAL_LINUX SAL_Sleep(time); #endif } long BSL_SAL_Tick(void) { if (g_timeCallback.pfTick != NULL && g_timeCallback.pfTick != BSL_SAL_Tick) { return g_timeCallback.pfTick(); } #ifdef HITLS_BSL_SAL_LINUX return SAL_Tick(); #else return BSL_SAL_TIME_NO_REG_FUNC; #endif } long BSL_SAL_TicksPerSec(void) { if (g_timeCallback.pfTicksPerSec != NULL && g_timeCallback.pfTicksPerSec != BSL_SAL_TicksPerSec) { return g_timeCallback.pfTicksPerSec(); } #ifdef HITLS_BSL_SAL_LINUX return SAL_TicksPerSec(); #else return BSL_SAL_TIME_NO_REG_FUNC; #endif } #endif /* HITLS_BSL_SAL_TIME */
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_time.c
C
unknown
11,851
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SAL_TIMEIMPL_H #define SAL_TIMEIMPL_H #include "hitls_build.h" #ifdef HITLS_BSL_SAL_TIME #include <stdint.h> #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct { BslSalGetSysTime pfGetSysTime; BslSalDateToStrConvert pfDateToStrConvert; BslSalSysTimeGet pfSysTimeGet; BslSalUtcTimeToDateConvert pfUtcTimeToDateConvert; BslSalSleep pfSleep; BslSalTick pfTick; BslSalTicksPerSec pfTicksPerSec; } BSL_SAL_TimeCallback; int32_t SAL_TimeCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb); #ifdef HITLS_BSL_SAL_LINUX int64_t TIME_GetSysTime(void); uint32_t TIME_DateToStrConvert(const BSL_TIME *dateTime, char *timeStr, size_t len); uint32_t TIME_SysTimeGet(BSL_TIME *sysTime); uint32_t TIME_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime); void SAL_Sleep(uint32_t time); long SAL_Tick(void); long SAL_TicksPerSec(void); #endif #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_BSL_SAL_TIME #endif // SAL_TIMEIMPL_H
2302_82127028/openHiTLS-examples
bsl/sal/src/sal_timeimpl.h
C
unknown
1,559
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef TLV_H #define TLV_H #include "hitls_build.h" #ifdef HITLS_BSL_TLV #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define TLV_HEADER_LENGTH (sizeof(uint32_t) + sizeof(uint32_t)) typedef struct { uint32_t type; uint32_t length; uint8_t *value; } BSL_Tlv; /** * @ingroup bsl_tlv * @brief Construct a TLV message based on the TLV structure. * * @param tlv [IN] TLV structure * @param buffer [OUT] Message memory * @param bufLen [IN] Memory length * @param usedLen [OUT] Message length * * @retval BSL_SUCCESS successfully created. * @retval BSL_TLV_ERR_BAD_PARAM Parameter incorrect * @retval BSL_MEMCPY_FAIL Memory Copy Failure */ int32_t BSL_TLV_Pack(const BSL_Tlv *tlv, uint8_t *buffer, uint32_t bufLen, uint32_t *usedLen); /** * @ingroup bsl_tlv * @brief Parse the TLV message of the specified type and generate the TLV structure. * * @param wantType [IN] TLV type * @param data [IN] TLV message memory * @param dataLen [IN] Message length * @param tlv [OUT] TLV Structure * @param readLen [OUT] Length of the parsed message * * @retval BSL_SUCCESS parsed successfully. * @retval BSL_TLV_ERR_BAD_PARAM Parameter incorrect * @retval BSL_MEMCPY_FAIL Memory Copy Failure * @retval BSL_TLV_ERR_NO_WANT_TYPE No TLV found */ int32_t BSL_TLV_Parse(uint32_t wantType, const uint8_t *data, uint32_t dataLen, BSL_Tlv *tlv, uint32_t *readLen); /** * @ingroup bsl_tlv * @brief Find the TLV of the specified type * and calculate the offset from the memory start address to the TLV data. * * @param wantType [IN] TLV type * @param data [IN] TLV message memory * @param dataLen [IN] Message length * @param offset [OUT] TLV data offset * @param length [OUT] Data length * * @retval BSL_SUCCESS succeeded. * @retval BSL_TLV_ERR_BAD_PARAM Parameter incorrect * @retval BSL_TLV_ERR_NO_WANT_TYPE No TLV found */ int32_t BSL_TLV_FindValuePos(uint32_t wantType, const uint8_t *data, uint32_t dataLen, uint32_t *offset, uint32_t *length); #ifdef __cplusplus } #endif #endif /* HITLS_BSL_TLV */ #endif // TLV_H
2302_82127028/openHiTLS-examples
bsl/tlv/include/tlv.h
C
unknown
2,674
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_TLV #include <stdint.h> #include "securec.h" #include "bsl_errno.h" #include "bsl_bytes.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "bsl_binlog_id.h" #include "tlv.h" int32_t BSL_TLV_Pack(const BSL_Tlv *tlv, uint8_t *buffer, uint32_t bufLen, uint32_t *usedLen) { uint8_t *curPos = buffer; if ((bufLen < TLV_HEADER_LENGTH) || (tlv->length > bufLen - TLV_HEADER_LENGTH)) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05013, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "TLV build error: bufLen = %u is not enough for tlv length = %u, tlv type = 0x%x.", bufLen, tlv->length, tlv->type, 0); BSL_ERR_PUSH_ERROR(BSL_TLV_ERR_BAD_PARAM); return BSL_TLV_ERR_BAD_PARAM; } /* Write the TLV type */ BSL_Uint32ToByte(tlv->type, curPos); curPos += sizeof(uint32_t); /* Write the TLV length */ BSL_Uint32ToByte(tlv->length, curPos); curPos += sizeof(uint32_t); /* Write TLV data */ if (memcpy_s(curPos, bufLen - TLV_HEADER_LENGTH, tlv->value, tlv->length) != EOK) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05014, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "TLV build error: write tlv value fail, bufLen = %u, tlv length = %u, tlv type = 0x%x.", bufLen, tlv->length, tlv->type, 0); BSL_ERR_PUSH_ERROR(BSL_MEMCPY_FAIL); return BSL_MEMCPY_FAIL; } *usedLen = TLV_HEADER_LENGTH + tlv->length; return BSL_SUCCESS; } static int32_t TLV_ParseHeader(const uint8_t *data, uint32_t dataLen, uint32_t *type, uint32_t *length) { const uint8_t *curPos = data; /* Parse the TLV type */ uint32_t tlvType = BSL_ByteToUint32(curPos); curPos += sizeof(uint32_t); /* Parse the TLV length */ uint32_t tlvLen = BSL_ByteToUint32(curPos); if (tlvLen > dataLen - TLV_HEADER_LENGTH) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05015, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Check TLV header error: dataLen = %u, tlv length = %u, tlv type = 0x%x.", dataLen, tlvLen, tlvType, 0); BSL_ERR_PUSH_ERROR(BSL_TLV_ERR_BAD_PARAM); return BSL_TLV_ERR_BAD_PARAM; } *type = tlvType; *length = tlvLen; return BSL_SUCCESS; } int32_t BSL_TLV_Parse(uint32_t wantType, const uint8_t *data, uint32_t dataLen, BSL_Tlv *tlv, uint32_t *readLen) { int32_t ret; const uint8_t *curPos = data; uint32_t remainLen = dataLen; uint32_t type; uint32_t length; while (remainLen >= TLV_HEADER_LENGTH) { /* Parse the TLV type and length */ ret = TLV_ParseHeader(curPos, remainLen, &type, &length); if (ret != BSL_SUCCESS) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05016, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Parse TLV error: tlv header illegal.", 0, 0, 0, 0); return ret; } remainLen -= (TLV_HEADER_LENGTH + length); /* The TLV type matches the expected type */ if (wantType == type) { /* Parse the TLV data */ if (memcpy_s(tlv->value, tlv->length, curPos + TLV_HEADER_LENGTH, length) != EOK) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05017, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Parse TLV error: write tlv value fail, bufLen = %u, tlv length = %u, tlv type = 0x%x.", tlv->length, length, type, 0); BSL_ERR_PUSH_ERROR(BSL_MEMCPY_FAIL); return BSL_MEMCPY_FAIL; } tlv->type = type; tlv->length = length; *readLen = dataLen - remainLen; return BSL_SUCCESS; } /* The TLV type does not match the expected type. Continue to parse the next TLV. */ curPos += (TLV_HEADER_LENGTH + length); } /* No matched TLV found */ BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05018, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Parse TLV error: no want type(0x%x), dataLen = %u.", wantType, dataLen, 0, 0); BSL_ERR_PUSH_ERROR(BSL_TLV_ERR_NO_WANT_TYPE); return BSL_TLV_ERR_NO_WANT_TYPE; } int32_t BSL_TLV_FindValuePos(uint32_t wantType, const uint8_t *data, uint32_t dataLen, uint32_t *offset, uint32_t *length) { int32_t ret; const uint8_t *curPos = data; uint32_t remainLen = dataLen; uint32_t type; while (remainLen > TLV_HEADER_LENGTH) { /* Parse the TLV type and length */ ret = TLV_ParseHeader(curPos, remainLen, &type, length); if (ret != BSL_SUCCESS) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05019, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Find TLV error: tlv header illegal.", 0, 0, 0, 0); return ret; } /* The TLV type matches the expected type */ if (wantType == type) { *offset = dataLen - remainLen + TLV_HEADER_LENGTH; return BSL_SUCCESS; } /* The TLV type does not match the expected type. Continue to parse the next TLV. */ curPos += (TLV_HEADER_LENGTH + *length); remainLen -= (TLV_HEADER_LENGTH + *length); } /* No matched TLV found */ BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05020, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Find TLV error: no want type(0x%x), dataLen = %u.", wantType, dataLen, 0, 0); BSL_ERR_PUSH_ERROR(BSL_TLV_ERR_NO_WANT_TYPE); return BSL_TLV_ERR_NO_WANT_TYPE; } #endif /* HITLS_BSL_TLV */
2302_82127028/openHiTLS-examples
bsl/tlv/src/tlv.c
C
unknown
5,973
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef UI_TYPE_H #define UI_TYPE_H #include "hitls_build.h" #ifdef HITLS_BSL_UI #include <stdint.h> #include "bsl_sal.h" #include "bsl_ui.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ struct UI_ControlMethod { int32_t (*uiOpen) (BSL_UI *ui); // Open the input and output streams int32_t (*uiWrite) (BSL_UI *ui, BSL_UI_DataPack *data); // Write callback int32_t (*uiRead) (BSL_UI *ui, BSL_UI_DataPack *data); // Read callback int32_t (*uiClose) (BSL_UI *ui); // Close the input and output streams. }; struct UI_Control { const BSL_UI_Method *method; BSL_SAL_ThreadLockHandle lock; void *in; void *out; void *exData; }; struct UI_ControlDataPack { uint32_t type; uint32_t flags; char *data; uint32_t dataLen; char *verifyData; }; #define BSL_UI_SUPPORT_ABILITY(cap, pos) (((cap) & (pos)) != 0) #define BSL_UI_READ_BUFF_MAX_LEN 1025 // 1024 + '\0' #ifdef __cplusplus } #endif /* __cpluscplus */ #endif /* HITLS_BSL_UI */ #endif /* UI_TYPE_H */
2302_82127028/openHiTLS-examples
bsl/ui/include/ui_type.h
C
unknown
1,571
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UI #include <stdio.h> #include "securec.h" #include "bsl_sal.h" #include "ui_type.h" #include "bsl_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "bsl_binlog_id.h" #include "bsl_ui.h" #define BSL_UI_PROMPT_PART_MAX_LEN 200 BSL_UI *BSL_UI_New(const BSL_UI_Method *method) { BSL_UI *ui = (BSL_UI *)BSL_SAL_Malloc(sizeof(BSL_UI)); if (ui == NULL) { return NULL; } (void)memset_s(ui, sizeof(BSL_UI), 0, sizeof(BSL_UI)); int32_t ret = BSL_SAL_ThreadLockNew(&(ui->lock)); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(ui); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05061, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui new: new thread lock error ret = %u.", (uint32_t)ret, 0, 0, 0); return NULL; } if (method == NULL) { ui->method = BSL_UI_GetOperMethod(NULL); } else { ui->method = method; } return ui; } void BSL_UI_Free(BSL_UI *ui) { if (ui == NULL) { return; } BSL_SAL_ThreadLockFree(ui->lock); BSL_SAL_FREE(ui); } BSL_UI_Method *BSL_UI_MethodNew(void) { BSL_UI_Method *method = (BSL_UI_Method *)BSL_SAL_Malloc(sizeof(BSL_UI_Method)); if (method == NULL) { return method; } (void)memset_s(method, sizeof(BSL_UI_Method), 0, sizeof(BSL_UI_Method)); return method; } void BSL_UI_MethodFree(BSL_UI_Method *method) { if (method == NULL) { return; } BSL_SAL_FREE(method); } int32_t BSL_UI_SetMethod(BSL_UI_Method *method, uint8_t type, void *func) { if (method == NULL || func == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (type) { case BSL_UIM_OPEN: method->uiOpen = func; break; case BSL_UIM_WRITE: method->uiWrite = func; break; case BSL_UIM_READ: method->uiRead = func; break; case BSL_UIM_CLOSE: method->uiClose = func; break; default: BSL_ERR_PUSH_ERROR(BSL_UI_METHOD_INVALID_TYPE); return BSL_UI_METHOD_INVALID_TYPE; } return BSL_SUCCESS; } int32_t BSL_UI_GetMethod(const BSL_UI_Method *method, uint8_t type, void **func) { if (method == NULL || func == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (type) { case BSL_UIM_OPEN: *func = method->uiOpen; break; case BSL_UIM_WRITE: *func = method->uiWrite; break; case BSL_UIM_READ: *func = method->uiRead; break; case BSL_UIM_CLOSE: *func = method->uiClose; break; default: BSL_ERR_PUSH_ERROR(BSL_UI_METHOD_INVALID_TYPE); return BSL_UI_METHOD_INVALID_TYPE; } return BSL_SUCCESS; } char *BSL_UI_ConstructPrompt(const char *objectDesc, const char *objectName) { if (objectDesc == NULL) { return NULL; } if (strlen(objectDesc) > BSL_UI_PROMPT_PART_MAX_LEN) { return NULL; } char *outString = NULL; char start[] = "Please input "; char middle[] = " for "; char end[] = ":"; uint32_t outLen = (uint32_t)strlen(start) + (uint32_t)strlen(objectDesc) + (uint32_t)strlen(end) + 1; if (objectName != NULL) { if (strlen(objectName) > BSL_UI_PROMPT_PART_MAX_LEN) { return NULL; } outLen += (uint32_t)strlen(middle) + (uint32_t)strlen(objectName); } outString = (char *)BSL_SAL_Malloc(outLen); if (outString == NULL) { return NULL; } (void)strcpy_s(outString, outLen, start); (void)strcat_s(outString, outLen, objectDesc); if (objectName != NULL) { (void)strcat_s(outString, outLen, middle); (void)strcat_s(outString, outLen, objectName); } (void)strcat_s(outString, outLen, end); return outString; } static int32_t BSL_UI_OperDataOnce(BSL_UI *ui, BSL_UI_DataPack *writeData, BSL_UI_DataPack *readData) { int32_t ret = ui->method->uiWrite(ui, writeData); if (ret != BSL_SUCCESS) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05082, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: write error:%u.", (uint32_t)ret, 0, 0, 0); return ret; } ret = ui->method->uiRead(ui, readData); if (ret != BSL_SUCCESS) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05084, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: read error:%u.", (uint32_t)ret, 0, 0, 0); return ret; } return BSL_SUCCESS; } static int32_t BSL_UI_OperVerifyData(BSL_UI *ui, const char *promptStr, BSL_UI_DataPack *firstReadData) { BSL_UI_DataPack writeData = {0}; BSL_UI_DataPack readData = {0}; char verifyRes[BSL_UI_READ_BUFF_MAX_LEN] = {0}; uint32_t verifyResLen = BSL_UI_READ_BUFF_MAX_LEN; char verifyPrompt[] = "Verify---"; char verifyFailPrompt[] = "Verify failed!\n"; uint32_t verifyLen = (uint32_t)strlen(promptStr) + (uint32_t)strlen(verifyPrompt) + 1; char *verifyStr = BSL_SAL_Malloc(verifyLen); if (verifyStr == NULL) { BSL_ERR_PUSH_ERROR(BSL_UI_MEM_ALLOC_FAIL); return BSL_UI_MEM_ALLOC_FAIL; } (void)memset_s(verifyStr, verifyLen, 0, verifyLen); (void)strcpy_s(verifyStr, verifyLen, verifyPrompt); (void)strcat_s(verifyStr, verifyLen, promptStr); writeData.data = verifyStr; writeData.dataLen = (uint32_t)strlen(verifyStr) + 1; readData.data = verifyRes; readData.dataLen = verifyResLen; int32_t ret = BSL_UI_OperDataOnce(ui, &writeData, &readData); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(verifyStr); BSL_ERR_PUSH_ERROR(ret); return ret; } if (readData.dataLen != firstReadData->dataLen || strcmp(verifyRes, firstReadData->data) != 0) { writeData.data = verifyFailPrompt; writeData.dataLen = (uint32_t)strlen(verifyFailPrompt) + 1; (void)ui->method->uiWrite(ui, &writeData); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05069, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: verify failed.", 0, 0, 0, 0); ret = BSL_UI_VERIFY_BUFF_FAILED; } BSL_SAL_FREE(verifyStr); (void)memset_s(verifyRes, sizeof(verifyRes), 0, sizeof(verifyRes)); BSL_ERR_PUSH_ERROR(ret); return ret; } static int32_t BSL_UI_OperInputData(BSL_UI *ui, BSL_UI_ReadPwdParam *param, BSL_UI_DataPack *readData, char **prompt, const BSL_UI_CheckDataCallBack checkDataCallBack, void *callBackData) { BSL_UI_DataPack writeData = {0}; char *promptStr = BSL_UI_ConstructPrompt(param->desc, param->name); if (promptStr == NULL) { BSL_ERR_PUSH_ERROR(BSL_UI_CONSTRUCT_PROMPT_ERROR); return BSL_UI_CONSTRUCT_PROMPT_ERROR; } writeData.data = promptStr; writeData.dataLen = (uint32_t)strlen(promptStr) + 1; int32_t ret = BSL_UI_OperDataOnce(ui, &writeData, readData); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(promptStr); BSL_ERR_PUSH_ERROR(ret); return ret; } if (checkDataCallBack != NULL) { ret = checkDataCallBack(ui, readData->data, readData->dataLen, callBackData); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(promptStr); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05086, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: callback check data failed:%u.", (uint32_t)ret, 0, 0, 0); BSL_ERR_PUSH_ERROR(ret); return ret; } } *prompt = promptStr; return BSL_SUCCESS; } int32_t BSL_UI_ReadPwdUtil(BSL_UI_ReadPwdParam *param, char *buff, uint32_t *buffLen, const BSL_UI_CheckDataCallBack checkDataCallBack, void *callBackData) { char result[BSL_UI_READ_BUFF_MAX_LEN] = {0}; char *promptStr = NULL; if (param == NULL || buff == NULL || buffLen == NULL || *buffLen == 0) { return BSL_NULL_INPUT; } BSL_UI *ui = BSL_UI_New(NULL); if (ui == NULL) { BSL_ERR_PUSH_ERROR(BSL_UI_CREATE_OBJECT_ERROR); return BSL_UI_CREATE_OBJECT_ERROR; } int32_t ret = ui->method->uiOpen(ui); if (ret != BSL_SUCCESS) { BSL_UI_Free(ui); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05083, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: open error:%u.", (uint32_t)ret, 0, 0, 0); BSL_ERR_PUSH_ERROR(ret); return ret; } do { BSL_UI_DataPack readData = {0, 0, result, BSL_UI_READ_BUFF_MAX_LEN, NULL}; ret = BSL_UI_OperInputData(ui, param, &readData, &promptStr, checkDataCallBack, callBackData); if (ret != BSL_SUCCESS) { break; } if (*buffLen < readData.dataLen) { ret = BSL_UI_OUTPUT_BUFF_TOO_SHORT; BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05066, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui pwd util: buff len is too short.", 0, 0, 0, 0); break; } if (param->verify) { ret = BSL_UI_OperVerifyData(ui, promptStr, &readData); if (ret != BSL_SUCCESS) { break; } } if (strcpy_s(buff, *buffLen, result) != EOK) { ret = BSL_UI_OUTPUT_BUFF_TOO_SHORT; break; } *buffLen = (uint32_t)strlen(buff) + 1; } while (0); ui->method->uiClose(ui); BSL_UI_Free(ui); BSL_SAL_FREE(promptStr); (void)memset_s(result, sizeof(result), 0, sizeof(result)); return ret; } static int32_t BSL_UI_DataReadProcess(BSL_UI_DataPack *data, void *parg, uint32_t larg) { if (parg == NULL || larg != sizeof(BSL_UI_CtrlRGetParam)) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05062, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "ui data process: read param larg error.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UI_INVALID_DATA_ARG); return BSL_UI_INVALID_DATA_ARG; } BSL_UI_CtrlRGetParam *param = (BSL_UI_CtrlRGetParam *)parg; data->type = BSL_UI_DATA_READ; data->flags = param->flags; data->data = param->buff; data->dataLen = param->buffLen; data->verifyData = param->verifyBuff; return BSL_SUCCESS; } static int32_t BSL_UI_DataWriteProcess(BSL_UI_DataPack *data, void *parg, uint32_t larg) { data->type = BSL_UI_DATA_WRITE; data->data = parg; data->dataLen = larg; return BSL_SUCCESS; } BSL_UI_DataPack *BSL_UI_DataPackNew(void) { BSL_UI_DataPack *data = (BSL_UI_DataPack *)BSL_SAL_Malloc(sizeof(BSL_UI_DataPack)); if (data == NULL) { return NULL; } (void)memset_s(data, sizeof(BSL_UI_DataPack), 0, sizeof(BSL_UI_DataPack)); return data; } void BSL_UI_DataPackFree(BSL_UI_DataPack *data) { if (data == NULL) { return; } BSL_SAL_FREE(data); } int32_t BSL_UI_DataCtrl(BSL_UI_DataPack *data, uint32_t type, void *parg, uint32_t larg) { int32_t ret = BSL_UI_INVALID_DATA_TYPE; if (data == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (type) { case BSL_UI_DATA_READ: ret = BSL_UI_DataReadProcess(data, parg, larg); break; case BSL_UI_DATA_WRITE: ret = BSL_UI_DataWriteProcess(data, parg, larg); break; default: break; } return ret; } int32_t BSL_UI_GetDataResult(BSL_UI_DataPack *data, char **result, uint32_t *resultLen) { if (data == NULL || result == NULL || resultLen == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (data->data == NULL) { BSL_ERR_PUSH_ERROR(BSL_UI_INVALID_DATA_RESULT); return BSL_UI_INVALID_DATA_RESULT; } *result = data->data; *resultLen = data->dataLen; return BSL_SUCCESS; } #endif /* HITLS_BSL_UI */
2302_82127028/openHiTLS-examples
bsl/ui/src/ui_core.c
C
unknown
12,382
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UI #include <stdio.h> #include <termios.h> #include "securec.h" #include "bsl_sal.h" #include "sal_file.h" #include "ui_type.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "bsl_ui.h" #define DEV_TTY "/dev/tty" int32_t UI_Open(BSL_UI *ui) { if (ui == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } (void)BSL_SAL_ThreadWriteLock(ui->lock); int32_t ret = BSL_SAL_FileOpen(&(ui->in), DEV_TTY, "r"); if (ret != BSL_SUCCESS) { (void)BSL_SAL_ThreadUnlock(ui->lock); return ret; } ret = BSL_SAL_FileOpen(&(ui->out), DEV_TTY, "w"); if (ret != BSL_SUCCESS) { BSL_SAL_FileClose(ui->in); (void)BSL_SAL_ThreadUnlock(ui->lock); return ret; } return BSL_SUCCESS; } static int32_t UI_CheckDataCommonParam(BSL_UI *ui, BSL_UI_DataPack *data) { if (ui == NULL || data == NULL || data->data == NULL || data->dataLen == 0) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } return BSL_SUCCESS; } static int32_t UI_CheckDataWriteParam(BSL_UI *ui, BSL_UI_DataPack *data) { int32_t ret = UI_CheckDataCommonParam(ui, data); if (ret != BSL_SUCCESS) { return ret; } if (ui->out == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } return BSL_SUCCESS; } int32_t UI_Write(BSL_UI *ui, BSL_UI_DataPack *data) { int32_t ret = UI_CheckDataWriteParam(ui, data); if (ret != BSL_SUCCESS) { return ret; } if (SAL_FPuts(ui->out, data->data)) { (void)SAL_Flush(ui->out); return BSL_SUCCESS; } BSL_ERR_PUSH_ERROR(BSL_UI_WRITE_ERROR); return BSL_UI_WRITE_ERROR; } static int32_t UI_ReadInternal(BSL_UI *ui, char *result, int32_t resultLen) { if (SAL_FGets(ui->in, result, resultLen) == NULL) { BSL_ERR_PUSH_ERROR(BSL_UI_FGETS_ERROR); return BSL_UI_FGETS_ERROR; } int32_t ret = SAL_Feof(ui->in); if (ret == BSL_SUCCESS || ret == BSL_SAL_FILE_NO_REG_FUNC) { // The input stream will be closed when Ctrl+D is pressed, or func is not regeister. BSL_ERR_PUSH_ERROR(BSL_UI_STDIN_END_ERROR); return BSL_UI_STDIN_END_ERROR; } if (SAL_FileError(ui->in)) { // Previous file operation error BSL_ERR_PUSH_ERROR(BSL_UI_OPERATION_ERROR); return BSL_UI_OPERATION_ERROR; } // 2 is before the last one if ((strlen(result) == (size_t)resultLen - 1) && (result[resultLen - 2] != '\n')) { BSL_ERR_PUSH_ERROR(BSL_UI_READ_BUFF_TOO_LONG); return BSL_UI_READ_BUFF_TOO_LONG; } return BSL_SUCCESS; } static int32_t UI_ReadSetFlag(BSL_UI *ui, uint32_t flags, struct termios *origTerm) { struct termios newTerm; if (!BSL_UI_SUPPORT_ABILITY(flags, BSL_UI_DATA_FLAG_ECHO)) { (void)memcpy_s(&newTerm, sizeof(newTerm), origTerm, sizeof(struct termios)); newTerm.c_lflag &= ~ECHO; return SAL_FSetAttr(ui->in, TCSANOW, (void *)&newTerm); } return BSL_SUCCESS; } static int32_t UI_ReadRecoverFlag(BSL_UI *ui, uint32_t flags, struct termios *origTerm) { BSL_UI_DataPack endData = {0}; char endStr[] = "\n"; if (!BSL_UI_SUPPORT_ABILITY(flags, BSL_UI_DATA_FLAG_ECHO)) { int32_t ret = SAL_FSetAttr(ui->in, TCSANOW, (void *)origTerm); if (ret != BSL_SUCCESS) { return ret; } endData.data = endStr; endData.dataLen = (uint32_t)strlen(endStr) + 1; ret = UI_Write(ui, &endData); if (ret != BSL_SUCCESS) { return ret; } } return BSL_SUCCESS; } static int32_t UI_CheckDataReadParam(BSL_UI *ui, BSL_UI_DataPack *data) { int32_t ret = UI_CheckDataCommonParam(ui, data); if (ret != BSL_SUCCESS) { return ret; } if (ui->in == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } return BSL_SUCCESS; } int32_t UI_Read(BSL_UI *ui, BSL_UI_DataPack *data) { struct termios origTerm; char result[BSL_UI_READ_BUFF_MAX_LEN + 1]; // real buff + '\n' + '\0' int32_t ret = UI_CheckDataReadParam(ui, data); if (ret != BSL_SUCCESS) { return ret; } ret = SAL_FGetAttr(ui->in, (void *)&origTerm); if (ret != BSL_SUCCESS) { return ret; } ret = UI_ReadSetFlag(ui, data->flags, &origTerm); if (ret != BSL_SUCCESS) { return ret; } do { ret = UI_ReadInternal(ui, result, (int32_t)sizeof(result)); if (ret != BSL_SUCCESS) { (void)UI_ReadRecoverFlag(ui, data->flags, &origTerm); break; } ret = UI_ReadRecoverFlag(ui, data->flags, &origTerm); if (ret != BSL_SUCCESS) { break; } char *pos = strchr(result, '\n'); if (pos != NULL) { *pos = '\0'; } if (strlen(result) == 0) { ret = BSL_UI_READ_LEN_TOO_SHORT; break; } if (data->dataLen < (strlen(result) + 1)) { ret = BSL_UI_OUTPUT_BUFF_TOO_SHORT; break; } if (data->verifyData != NULL && strcmp(data->verifyData, result) != 0) { ret = BSL_UI_VERIFY_BUFF_FAILED; break; } (void)strcpy_s(data->data, data->dataLen, result); data->dataLen = (uint32_t)strlen(result) + 1; } while (0); (void)memset_s(result, sizeof(result), 0, sizeof(result)); return ret; } int32_t UI_Close(BSL_UI *ui) { if (ui == NULL) { return BSL_SUCCESS; } if (ui->in != NULL) { BSL_SAL_FileClose(ui->in); } if (ui->out != NULL) { BSL_SAL_FileClose(ui->out); } (void)BSL_SAL_ThreadUnlock(ui->lock); return BSL_SUCCESS; } static BSL_UI_Method g_defaultUiMethod = { UI_Open, UI_Write, UI_Read, UI_Close }; const BSL_UI_Method *BSL_UI_GetOperMethod(const BSL_UI *ui) { if (ui == NULL) { return &g_defaultUiMethod; } return ui->method; } #endif /* HITLS_BSL_UI */
2302_82127028/openHiTLS-examples
bsl/ui/src/ui_default_impl.c
C
unknown
6,624
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef UIO_BASE_H #define UIO_BASE_H #include "hitls_build.h" #ifdef HITLS_BSL_UIO_PLT #include "bsl_uio.h" #ifdef __cplusplus extern "C" { #endif struct BSL_UIO_MethodStruct { int32_t uioType; BslUioWriteCb uioWrite; BslUioReadCb uioRead; BslUioCtrlCb uioCtrl; BslUioPutsCb uioPuts; BslUioGetsCb uioGets; BslUioCreateCb uioCreate; BslUioDestroyCb uioDestroy; }; /** * @ingroup bsl_uio * * @brief Get the fd of the UIO object * @param uio [IN] UIO object * @retval File Descriptor fd */ int32_t BSL_UIO_GetFd(BSL_UIO *uio); #ifdef __cplusplus } #endif #endif /* HITLS_BSL_UIO_PLT */ #endif // UIO_BASE_H
2302_82127028/openHiTLS-examples
bsl/uio/include/uio_base.h
C
unknown
1,201
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_PLT #include "securec.h" #include "bsl_sal.h" #include "bsl_binlog_id.h" #include "bsl_log_internal.h" #include "bsl_log.h" #include "bsl_err_internal.h" #include "bsl_errno.h" #include "bsl_uio.h" #include "uio_base.h" #include "uio_abstraction.h" BSL_UIO_Method *BSL_UIO_NewMethod(void) { BSL_UIO_Method *meth = (BSL_UIO_Method *)BSL_SAL_Calloc(1u, sizeof(BSL_UIO_Method)); if (meth == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05058, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "new method is NULL.", NULL, NULL, NULL, NULL); BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } return meth; } void BSL_UIO_FreeMethod(BSL_UIO_Method *meth) { BSL_SAL_FREE(meth); } int32_t BSL_UIO_SetMethodType(BSL_UIO_Method *meth, int32_t type) { if (meth == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05059, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "set method type is NULL.", NULL, NULL, NULL, NULL); return BSL_NULL_INPUT; } meth->uioType = type; return BSL_SUCCESS; } int32_t BSL_UIO_SetMethod(BSL_UIO_Method *meth, int32_t type, void *func) { if (meth == NULL || func == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05060, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "set method is NULL.", NULL, NULL, NULL, NULL); return BSL_NULL_INPUT; } switch (type) { case BSL_UIO_WRITE_CB: meth->uioWrite = func; break; case BSL_UIO_READ_CB: meth->uioRead = func; break; case BSL_UIO_CTRL_CB: meth->uioCtrl = func; break; case BSL_UIO_CREATE_CB: meth->uioCreate = func; break; case BSL_UIO_DESTROY_CB: meth->uioDestroy = func; break; case BSL_UIO_PUTS_CB: meth->uioPuts = func; break; case BSL_UIO_GETS_CB: meth->uioGets = func; break; default: BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05025, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "method type is wrong.", NULL, NULL, NULL, NULL); BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } return BSL_SUCCESS; } BSL_UIO *BSL_UIO_New(const BSL_UIO_Method *method) { int32_t ret = BSL_SUCCESS; if (method == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05021, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "method is NULL.", 0, 0, 0, 0); return NULL; } BSL_UIO *uio = (BSL_UIO *)BSL_SAL_Calloc(1, sizeof(struct UIO_ControlBlock)); if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05022, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio malloc fail.", 0, 0, 0, 0); return NULL; } (void)memcpy_s(&uio->method, sizeof(BSL_UIO_Method), method, sizeof(BSL_UIO_Method)); BSL_SAL_ReferencesInit(&(uio->references)); BSL_UIO_SetIsUnderlyingClosedByUio(uio, false); if (uio->method.uioCreate != NULL) { ret = uio->method.uioCreate(uio); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05023, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio create data fail.", 0, 0, 0, 0); BSL_SAL_FREE(uio); return NULL; } } return uio; } int32_t BSL_UIO_UpRef(BSL_UIO *uio) { if (uio == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05024, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio is NULL.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (uio->references.count == INT32_MAX) { BSL_ERR_PUSH_ERROR(BSL_UIO_REF_MAX); return BSL_UIO_REF_MAX; } int val = 0; BSL_SAL_AtomicUpReferences(&(uio->references), &val); return BSL_SUCCESS; } void BSL_UIO_Free(BSL_UIO *uio) { if (uio == NULL) { return; } int ret = 0; BSL_SAL_AtomicDownReferences(&(uio->references), &ret); if (ret > 0) { return; } if (uio->userData != NULL && uio->userDataFreeFunc != NULL) { (void)uio->userDataFreeFunc(uio->userData); uio->userData = NULL; } if (uio->method.uioDestroy != NULL) { (void)uio->method.uioDestroy(uio); } BSL_SAL_ReferencesFree(&(uio->references)); BSL_SAL_FREE(uio); return; } int32_t BSL_UIO_Write(BSL_UIO *uio, const void *data, uint32_t len, uint32_t *writeLen) { if (uio == NULL || uio->method.uioWrite == NULL || data == NULL || writeLen == NULL || len == 0) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05026, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio write: internal input error.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; // if the uio is null, the send size is zero, means no data send; } if (uio->init != 1) { BSL_ERR_PUSH_ERROR(BSL_UIO_UNINITIALIZED); return BSL_UIO_UNINITIALIZED; } int32_t ret = uio->method.uioWrite(uio, data, len, writeLen); if (ret == BSL_SUCCESS) { uio->writeNum += (int64_t)*writeLen; } return ret; } int32_t BSL_UIO_Puts(BSL_UIO *uio, const char *buf, uint32_t *writeLen) { if (uio == NULL || uio->method.uioPuts == NULL || writeLen == NULL || buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (uio->init != 1) { BSL_ERR_PUSH_ERROR(BSL_UIO_UNINITIALIZED); return BSL_UIO_UNINITIALIZED; } int32_t ret = uio->method.uioPuts(uio, buf, writeLen); if (ret == BSL_SUCCESS) { uio->writeNum += (int64_t)*writeLen; } return ret; } int32_t BSL_UIO_Gets(BSL_UIO *uio, char *buf, uint32_t *readLen) { if (uio == NULL || uio->method.uioGets == NULL || readLen == NULL || buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (uio->init != 1) { BSL_ERR_PUSH_ERROR(BSL_UIO_UNINITIALIZED); return BSL_UIO_UNINITIALIZED; } int32_t ret = uio->method.uioGets(uio, buf, readLen); if (ret == BSL_SUCCESS) { uio->readNum += (int64_t)*readLen; } return ret; } int32_t BSL_UIO_Read(BSL_UIO *uio, void *data, uint32_t len, uint32_t *readLen) { if (uio == NULL || uio->method.uioRead == NULL || data == NULL || len == 0 || readLen == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05027, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio read: internal input error.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (uio->init != 1) { BSL_ERR_PUSH_ERROR(BSL_UIO_UNINITIALIZED); return BSL_UIO_UNINITIALIZED; } int32_t ret = uio->method.uioRead(uio, data, len, readLen); if (ret == BSL_SUCCESS) { uio->readNum += (int64_t)*readLen; } return ret; } int32_t BSL_UIO_GetTransportType(const BSL_UIO *uio) { if (uio == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05028, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio is NULL.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } return uio->method.uioType; } bool BSL_UIO_GetUioChainTransportType(BSL_UIO *uio, const BSL_UIO_TransportType uioType) { if (uio == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05069, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "get uio type is NULL.", NULL, NULL, NULL, NULL); return false; } while (uio != NULL) { if (BSL_UIO_GetTransportType(uio) == (int32_t)uioType) { return true; } uio = BSL_UIO_Next(uio); } return false; } int32_t BSL_UIO_SetUserData(BSL_UIO *uio, void *data) { if (uio == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05029, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "uio is NULL.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uio->userData = data; return BSL_SUCCESS; } int32_t BSL_UIO_SetUserDataFreeFunc(BSL_UIO *uio, BSL_UIO_USERDATA_FREE_FUNC userDataFreeFunc) { if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uio->userDataFreeFunc = userDataFreeFunc; return BSL_SUCCESS; } void *BSL_UIO_GetUserData(const BSL_UIO *uio) { if (uio == NULL) { return NULL; } return uio->userData; } bool BSL_UIO_GetIsUnderlyingClosedByUio(const BSL_UIO *uio) { if (uio == NULL) { return false; // If the value is empty, the function will not release the value. } return uio->isUnderlyingClosedByUio; } void BSL_UIO_SetIsUnderlyingClosedByUio(BSL_UIO *uio, bool close) { if (uio == NULL) { return; } uio->isUnderlyingClosedByUio = close; } const BSL_UIO_Method *BSL_UIO_GetMethod(const BSL_UIO *uio) { if (uio == NULL) { return NULL; } return &uio->method; } static int32_t UIO_GetInit(BSL_UIO *uio, int32_t larg, bool *parg) { if (larg != (int32_t)sizeof(bool) || parg == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } *parg = uio->init; return BSL_SUCCESS; } static int32_t UIO_GetReadNum(BSL_UIO *uio, int32_t larg, int64_t *parg) { if (larg != (int32_t)sizeof(int64_t) || parg == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } *parg = uio->readNum; return BSL_SUCCESS; } static int32_t UIO_GetWriteNum(BSL_UIO *uio, int32_t larg, int64_t *parg) { if (larg != (int32_t)sizeof(int64_t) || parg == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } *parg = uio->writeNum; return BSL_SUCCESS; } int32_t BSL_UIO_Ctrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { if (uio == NULL || uio->method.uioCtrl == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (cmd) { case BSL_UIO_GET_INIT: return UIO_GetInit(uio, larg, parg); case BSL_UIO_GET_READ_NUM: return UIO_GetReadNum(uio, larg, parg); case BSL_UIO_GET_WRITE_NUM: return UIO_GetWriteNum(uio, larg, parg); default: return uio->method.uioCtrl(uio, cmd, larg, parg); } } void *BSL_UIO_GetCtx(const BSL_UIO *uio) { if (uio == NULL) { return NULL; } return uio->ctx; } void BSL_UIO_SetCtx(BSL_UIO *uio, void *ctx) { if (uio != NULL) { uio->ctx = ctx; } } int32_t BSL_UIO_GetFd(BSL_UIO *uio) { int32_t fd = -1; (void)BSL_UIO_Ctrl(uio, BSL_UIO_GET_FD, (int32_t)sizeof(fd), &fd); // Parameters are checked by each ctrl function. return fd; } void BSL_UIO_SetFD(BSL_UIO *uio, int fd) { bool invalid = (uio == NULL) || (fd < 0); if (invalid) { return; } BSL_UIO_Ctrl(uio, BSL_UIO_SET_FD, (int32_t)sizeof(fd), &fd); } int32_t BSL_UIO_SetFlags(BSL_UIO *uio, uint32_t flags) { if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint32_t validFlags = BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY | BSL_UIO_FLAGS_BASE64_NO_NEWLINE | BSL_UIO_FLAGS_BASE64_PEM; if ((flags & validFlags) == 0 || flags > validFlags) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } uio->flags |= flags; return BSL_SUCCESS; } int32_t BSL_UIO_ClearFlags(BSL_UIO *uio, uint32_t flags) { if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uio->flags &= ~flags; return BSL_SUCCESS; } uint32_t BSL_UIO_TestFlags(const BSL_UIO *uio, uint32_t flags, uint32_t *out) { if (uio == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } *out = uio->flags & flags; return BSL_SUCCESS; } void BSL_UIO_SetInit(BSL_UIO *uio, bool init) { if (uio != NULL) { uio->init = init; } } /** * @brief Checking for Fatal I/O Errors * * @param err [IN] error type * * @return true :No Fatal error * false:fatal errors */ bool UioIsNonFatalErr(int32_t err) { bool ret = true; /** @alias Check whether err is a fatal error and modify ret. */ switch (err) { #if defined(ENOTCONN) case ENOTCONN: #endif #ifdef EINTR case EINTR: #endif #ifdef EINPROGRESS case EINPROGRESS: #endif #ifdef EWOULDBLOCK #if !defined(WSAEWOULDBLOCK) || WSAEWOULDBLOCK != EWOULDBLOCK case EWOULDBLOCK: #endif #endif #ifdef EAGAIN #if EWOULDBLOCK != EAGAIN case EAGAIN: #endif #endif #ifdef EALREADY case EALREADY: #endif #ifdef EPROTO case EPROTO: #endif #ifdef EMSGSIZE case EMSGSIZE: #endif ret = true; break; default: ret = false; break; } return ret; } int32_t BSL_UIO_Append(BSL_UIO *uio, BSL_UIO *tail) { bool invalid = (uio == NULL) || (tail == NULL); if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BSL_UIO *t = uio; while (t->next != NULL) { t = t->next; } t->next = tail; tail->prev = t; return BSL_SUCCESS; } BSL_UIO *BSL_UIO_PopCurrent(BSL_UIO *uio) { if (uio == NULL) { return NULL; } BSL_UIO *ret = uio->next; if (uio->prev != NULL) { uio->prev->next = uio->next; } if (uio->next != NULL) { uio->next->prev = uio->prev; } uio->prev = NULL; uio->next = NULL; return ret; } void BSL_UIO_FreeChain(BSL_UIO *uio) { BSL_UIO *b = uio; while (b != NULL) { int ref = b->references.count; BSL_UIO *next = b->next; BSL_UIO_Free(b); if (ref > 1) { break; } b = next; } } BSL_UIO *BSL_UIO_Next(BSL_UIO *uio) { if (uio == NULL) { return NULL; } return uio->next; } #endif /* HITLS_BSL_UIO_PLT */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_abstraction.c
C
unknown
14,941
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef UIO_ABSTRACTION_H #define UIO_ABSTRACTION_H #include "hitls_build.h" #ifdef HITLS_BSL_UIO_PLT #include "bsl_uio.h" #include "uio_base.h" #include "sal_atomic.h" #ifdef __cplusplus extern "C" { #endif #define IP_ADDR_V4_LEN 4 #define IP_ADDR_V6_LEN 16 #define IP_ADDR_MAX_LEN IP_ADDR_V6_LEN #define SOCK_ADDR_V4_LEN (sizeof(struct sockaddr_in)) #define SOCK_ADDR_V6_LEN (sizeof(struct sockaddr_in6)) #define SOCK_ADDR_UNIX_LEN (sizeof(struct sockaddr_un)) #define DGRAM_SOCKADDR_MAX_LEN SOCK_ADDR_UNIX_LEN struct UIO_ControlBlock { struct BSL_UIO_MethodStruct method; uint32_t flags; // Read/write retry flag. For details, see BSL_UIO_FLAGS_* in bsl_uio.h bool init; // Initialization flag. 1 means it's initialized, and 0 means it's not initialized. int64_t writeNum; // count of write int64_t readNum; // count of read void *ctx; // Context uint32_t ctxLen; // Context length void *userData; // User data BSL_UIO_USERDATA_FREE_FUNC userDataFreeFunc; // Release User Data struct UIO_ControlBlock *prev; // Previous UIO object of the current UIO object in the UIO chain struct UIO_ControlBlock *next; // Next UIO object of the current UIO object in the UIO chain bool isUnderlyingClosedByUio; // Indicates whether related resources are released together with the UIO. BSL_SAL_RefCount references; // reference count }; typedef struct { uint8_t *data; uint64_t size; } BSL_UIO_CtrlGetInfoParam; /** * @brief Check whether a given error code is a fatal error. * * @param err [IN] Error code. * * @return true: A fatal error occurs. * false: No fatal error occurs. */ bool UioIsNonFatalErr(int32_t err); #ifdef __cplusplus } #endif #endif /* HITLS_BSL_UIO_PLT */ #endif // UIO_ABSTRACTION_H
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_abstraction.h
C
unknown
2,430
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_BUFFER #include "securec.h" #include "bsl_sal.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "bsl_uio.h" #include "uio_abstraction.h" // The write behavior must be the same. #define UIO_BUFFER_DEFAULT_SIZE 4096 #define DTLS_MIN_MTU 256 /* Minimum MTU setting size */ #define DTLS_MAX_MTU_OVERHEAD 48 /* Highest MTU overhead, IPv6 40 + UDP 8 */ typedef struct { uint32_t outSize; // This variable will make the write() logic consistent with the ossl. Reason: // 1) The handshake logic is complex. // 2) The behavior consistency problem of the handshake logic is difficult to locate. uint32_t outOff; uint32_t outLen; uint8_t *outBuf; } BufferCtx; static int32_t BufferCreate(BSL_UIO *uio) { if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BufferCtx *ctx = BSL_SAL_Calloc(1, sizeof(BufferCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } ctx->outSize = UIO_BUFFER_DEFAULT_SIZE; ctx->outBuf = (uint8_t *)BSL_SAL_Malloc(UIO_BUFFER_DEFAULT_SIZE); if (ctx->outBuf == NULL) { BSL_SAL_FREE(ctx); BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } BSL_UIO_SetCtx(uio, ctx); uio->init = 1; return BSL_SUCCESS; } static int32_t BufferDestroy(BSL_UIO *uio) { if (uio == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BufferCtx *ctx = BSL_UIO_GetCtx(uio); if (ctx != NULL) { BSL_SAL_FREE(ctx->outBuf); BSL_SAL_FREE(ctx); BSL_UIO_SetCtx(uio, NULL); } uio->flags = 0; uio->init = 0; return BSL_SUCCESS; } static int32_t BufferFlushInternal(BSL_UIO *uio) { BufferCtx *ctx = BSL_UIO_GetCtx(uio); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } while (ctx->outLen > 0) { uint32_t tmpWriteLen = 0; int32_t ret = BSL_UIO_Write(uio->next, &ctx->outBuf[ctx->outOff], ctx->outLen, &tmpWriteLen); if (ret != BSL_SUCCESS) { uio->flags = uio->next->flags; return ret; } if (tmpWriteLen == 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_BUSY); return BSL_UIO_IO_BUSY; } ctx->outOff += tmpWriteLen; ctx->outLen -= tmpWriteLen; } ctx->outOff = 0; ctx->outLen = 0; return BSL_SUCCESS; } static int32_t BufferFlush(BSL_UIO *uio, int32_t larg, void *parg) { bool invalid = (uio == NULL) || (uio->next == NULL) || (uio->ctx == NULL); if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BufferCtx *ctx = BSL_UIO_GetCtx(uio); if (ctx->outLen == 0) { // invoke the flush of the next UIO object return BSL_UIO_Ctrl(uio->next, BSL_UIO_FLUSH, larg, parg); } (void)BSL_UIO_ClearFlags(uio, (BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY)); int32_t ret = BufferFlushInternal(uio); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return BSL_UIO_Ctrl(uio->next, BSL_UIO_FLUSH, larg, parg); } static int32_t BufferReset(BSL_UIO *uio) { if (uio == NULL || uio->ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } BufferCtx *ctx = uio->ctx; ctx->outLen = 0; ctx->outOff = 0; if (uio->next == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } return BSL_UIO_Ctrl(uio->next, BSL_UIO_RESET, 0, NULL); } static int32_t BufferSetBufferSize(BSL_UIO *uio, int32_t larg, void *parg) { if (larg != (int32_t)sizeof(uint32_t) || parg == NULL || *(uint32_t *)parg < DTLS_MIN_MTU - DTLS_MAX_MTU_OVERHEAD) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } BufferCtx *ctx = BSL_UIO_GetCtx(uio); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint32_t len = *(uint32_t *)parg; BSL_SAL_FREE(ctx->outBuf); ctx->outBuf = (uint8_t *)BSL_SAL_Malloc(len); if (ctx->outBuf == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } ctx->outOff = 0; ctx->outLen = 0; ctx->outSize = len; return BSL_SUCCESS; } static int32_t BufferCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { switch (cmd) { case BSL_UIO_FLUSH: return BufferFlush(uio, larg, parg); case BSL_UIO_RESET: return BufferReset(uio); case BSL_UIO_SET_BUFFER_SIZE: return BufferSetBufferSize(uio, larg, parg); default: if (uio->next != NULL) { return BSL_UIO_Ctrl(uio->next, cmd, larg, parg); } break; } BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } // Add data to the remaining space. static int32_t TryCompleteBuffer(BufferCtx *ctx, const void *in, uint32_t remain, uint32_t *writeLen) { const uint32_t freeSpace = ctx->outSize - (ctx->outOff + ctx->outLen); if (freeSpace == 0) { return BSL_SUCCESS; } const uint32_t real = (freeSpace < remain) ? freeSpace : remain; if (memcpy_s(&ctx->outBuf[ctx->outOff + ctx->outLen], freeSpace, in, real) != EOK) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } ctx->outLen += real; *writeLen += real; return BSL_SUCCESS; } static int32_t BufferWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { bool invalid = (uio == NULL) || (buf == NULL) || (writeLen == NULL) || (uio->next == NULL); if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } *writeLen = 0; BufferCtx *ctx = BSL_UIO_GetCtx(uio); invalid = (ctx == NULL) || (ctx->outBuf == NULL); if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } (void)BSL_UIO_ClearFlags(uio, (BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY)); const uint8_t *in = buf; uint32_t remain = len; while (remain > 0) { const uint32_t freeSpace = ctx->outSize - (ctx->outOff + ctx->outLen); if (freeSpace >= remain) { // If the space is sufficient, cache the data. return TryCompleteBuffer(ctx, in, remain, writeLen); } // else: space is insufficient if (ctx->outLen > 0) { // buffer already has data, need to send the existing data first. int32_t ret = BufferFlushInternal(uio); // If next uio return busy, return success, upper layer will return busy if (ret == BSL_UIO_IO_BUSY) { return BSL_SUCCESS; } if (ret != BSL_SUCCESS) { return ret; } } ctx->outOff = 0; while (remain >= ctx->outSize) { uint32_t tmpWriteLen = 0; int32_t ret = BSL_UIO_Write(uio->next, in, remain, &tmpWriteLen); if (ret != BSL_SUCCESS || tmpWriteLen == 0) { uio->flags = uio->next->flags; return ret; } *writeLen += tmpWriteLen; in = &in[tmpWriteLen]; remain -= tmpWriteLen; } } return BSL_SUCCESS; } const BSL_UIO_Method *BSL_UIO_BufferMethod(void) { static const BSL_UIO_Method m = { BSL_UIO_BUFFER, BufferWrite, NULL, BufferCtrl, NULL, NULL, BufferCreate, BufferDestroy, }; return &m; } #endif /* HITLS_BSL_UIO_BUFFER */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_buffer.c
C
unknown
8,257
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_FILE #include <stdio.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "bsl_errno.h" #include "uio_base.h" #include "uio_abstraction.h" #include "sal_file.h" #include "bsl_uio.h" static int32_t FileWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } *writeLen = 0; if (len == 0) { return BSL_SUCCESS; } bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); int32_t ret = BSL_SAL_FileWrite(f, buf, 1, len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } *writeLen = len; return BSL_SUCCESS; } static int32_t FileRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } *readLen = 0; size_t rLen; bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); int32_t ret = BSL_SAL_FileRead(f, buf, 1, len, &rLen); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } *readLen = (uint32_t)rLen; return BSL_SUCCESS; } static int32_t FileDestroy(BSL_UIO *uio) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { // the closing of the file is specified by the user bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); if (f != NULL) { BSL_SAL_FileClose(f); BSL_UIO_SetCtx(uio, NULL); } } uio->init = false; return BSL_SUCCESS; } static int32_t FileOpen(BSL_UIO *uio, uint32_t flags, const char *filename) { if (filename == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); if (f != NULL) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { BSL_SAL_FileClose(f); BSL_UIO_SetCtx(uio, NULL); } else { return BSL_UIO_EXIST_CONTEXT_NOT_RELEASED; } } const char *mode = NULL; bsl_sal_file_handle fileHandle = NULL; if ((flags & BSL_UIO_FILE_APPEND) != 0) { mode = ((flags & BSL_UIO_FILE_READ) != 0) ? "a+" : "a"; } else if ((flags & BSL_UIO_FILE_READ) != 0) { mode = ((flags & BSL_UIO_FILE_WRITE) != 0) ? "r+" : "r"; } else if ((flags & BSL_UIO_FILE_WRITE) != 0) { mode = "w"; } else { BSL_ERR_PUSH_ERROR(BSL_UIO_FILE_OPEN_FAIL); return BSL_UIO_FILE_OPEN_FAIL; } if (BSL_SAL_FileOpen(&fileHandle, filename, mode) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_FILE_OPEN_FAIL); return BSL_UIO_FILE_OPEN_FAIL; } BSL_UIO_SetCtx(uio, (void *)fileHandle); uio->init = true; return BSL_SUCCESS; } static int32_t FilePending(BSL_UIO *uio, int32_t larg, uint64_t *ret) { if (ret == NULL || larg != sizeof(*ret)) { return BSL_INVALID_ARG; } bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); if (f == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } long current; // save the current if (SAL_FileTell(f, &current) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } if (SAL_FileSeek(f, 0, SEEK_END) != BSL_SUCCESS) { // move to the end BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } long max; // get the length if (SAL_FileTell(f, &max) != BSL_SUCCESS || max < current) { // error, including < 0, should restore the current (void)SAL_FileSeek(f, current, SEEK_SET); BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } *ret = (uint64_t)(max - current); // save the remaining length (void)SAL_FileSeek(f, current, SEEK_SET); // recover it return BSL_SUCCESS; } static int32_t FileWpending(int32_t larg, int64_t *ret) { if (larg != sizeof(int64_t) || ret == NULL) { return BSL_INVALID_ARG; } *ret = 0; // should return 0 if it's file UIO return BSL_SUCCESS; } static int32_t FileSetPtr(BSL_UIO *uio, int32_t isClosed, FILE *fp) { if (fp == NULL || (isClosed != 0 && isClosed != 1)) { return BSL_INVALID_ARG; } bsl_sal_file_handle file = BSL_UIO_GetCtx(uio); if (file != NULL) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { BSL_SAL_FileClose(file); BSL_UIO_SetCtx(uio, NULL); } else { return BSL_UIO_EXIST_CONTEXT_NOT_RELEASED; } } BSL_UIO_SetCtx(uio, fp); BSL_UIO_SetIsUnderlyingClosedByUio(uio, isClosed); uio->init = true; return BSL_SUCCESS; } static int32_t FileReset(BSL_UIO *uio) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } bsl_sal_file_handle *f = BSL_UIO_GetCtx(uio); if (SAL_FileSeek(f, 0, SEEK_SET) != 0) { BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION); return BSL_INTERNAL_EXCEPTION; } return BSL_SUCCESS; } static int32_t FileGetEof(BSL_UIO *uio, int32_t larg, bool *isEof) { if (larg != 1 || isEof == NULL) { return BSL_INVALID_ARG; } *isEof = false; if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } FILE *f = BSL_UIO_GetCtx(uio); if (feof(f) != 0) { *isEof = true; } return BSL_SUCCESS; } static int32_t FileFlush(BSL_UIO *uio) { FILE *file = BSL_UIO_GetCtx(uio); if (file == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (!SAL_Flush(file)) { return BSL_UIO_IO_EXCEPTION; } return BSL_SUCCESS; } static int32_t FileCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { if (larg < 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } switch (cmd) { case BSL_UIO_FILE_OPEN: return FileOpen(uio, (uint32_t)larg, parg); case BSL_UIO_PENDING: return FilePending(uio, larg, parg); case BSL_UIO_WPENDING: return FileWpending(larg, parg); case BSL_UIO_FILE_PTR: return FileSetPtr(uio, larg, parg); case BSL_UIO_RESET: return FileReset(uio); case BSL_UIO_FILE_GET_EOF: return FileGetEof(uio, larg, parg); case BSL_UIO_FLUSH: return FileFlush(uio); default: break; } BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } static int32_t FileGets(BSL_UIO *uio, char *buf, uint32_t *readLen) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); bsl_sal_file_handle f = BSL_UIO_GetCtx(uio); if (SAL_FGets(f, buf, (int32_t)*readLen) == NULL) { *readLen = 0; if (SAL_FileError(f) == false) { // read the end of the file successfully return BSL_SUCCESS; } (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_READ | BSL_UIO_FLAGS_SHOULD_RETRY); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } *readLen = (uint32_t)strlen(buf); return BSL_SUCCESS; } static int32_t FilePuts(BSL_UIO *uio, const char *buf, uint32_t *writeLen) { uint32_t len = 0; if (buf != NULL) { len = (uint32_t)strlen(buf); } return FileWrite(uio, buf, len, writeLen); } const BSL_UIO_Method *BSL_UIO_FileMethod(void) { static const BSL_UIO_Method METHOD = { BSL_UIO_FILE, FileWrite, FileRead, FileCtrl, FilePuts, FileGets, NULL, FileDestroy, }; return &METHOD; } #endif /* HITLS_BSL_UIO_FILE */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_file.c
C
unknown
8,548
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_MEM #include "securec.h" #include "bsl_buffer.h" #include "bsl_errno.h" #include "bsl_err_internal.h" #include "uio_base.h" #include "uio_abstraction.h" #include "bsl_uio.h" typedef struct { BSL_BufMem *buf; BSL_BufMem *tmpBuf; // only used in read-only mode size_t readIndex; int32_t eof; // Behavior when reading empty memory. If the value is not 0, retry will be set. } UIO_BufMem; static int32_t MemNewBuf(BSL_UIO *uio, int32_t len, void *buf) { if (buf == NULL || len < 0) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } BSL_BufMem *bm = ubm->buf; if (bm->data != NULL && (uio->flags & BSL_UIO_FLAGS_MEM_READ_ONLY) == 0) { /* If the UIO mode is not read-only, need to release the memory first. * Otherwise, the internal memory applied for the read/write mode will be overwritten, */ BSL_ERR_PUSH_ERROR(BSL_UIO_MEM_NOT_NULL); return BSL_UIO_MEM_NOT_NULL; } if (ubm->tmpBuf == NULL) { ubm->tmpBuf = BSL_BufMemNew(); if (ubm->tmpBuf == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } } ubm->readIndex = 0; ubm->eof = 0; // Read-only memory, and retry is not required. bm->length = (size_t)len; bm->max = (size_t)len; bm->data = (void *)buf; uio->flags = BSL_UIO_FLAGS_MEM_READ_ONLY; return BSL_SUCCESS; } static int32_t UioBufMemSync(UIO_BufMem *ubm) { if (ubm != NULL && ubm->readIndex != 0) { if (memmove_s(ubm->buf->data, ubm->buf->length, ubm->buf->data + ubm->readIndex, ubm->buf->length - ubm->readIndex) != EOK) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } ubm->buf->length -= ubm->readIndex; ubm->readIndex = 0; } return BSL_SUCCESS; } static int32_t MemWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if ((uio->flags & BSL_UIO_FLAGS_MEM_READ_ONLY) != 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_WRITE_NOT_ALLOWED); return BSL_UIO_WRITE_NOT_ALLOWED; } (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); *writeLen = 0; if (len == 0) { return BSL_SUCCESS; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } if (UioBufMemSync(ubm) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_MEMMOVE_FAIL); return BSL_MEMMOVE_FAIL; } const size_t origLen = ubm->buf->length; if (BSL_BufMemGrowClean(ubm->buf, origLen + len) == 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_MEM_GROW_FAIL); return BSL_UIO_MEM_GROW_FAIL; } // memory grow guarantee of success here (void)memcpy_s(ubm->buf->data + origLen, len, buf, len); *writeLen = len; return BSL_SUCCESS; } static int32_t MemRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen) { if (BSL_UIO_GetCtx(uio) == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); *readLen = 0; UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } size_t real = (size_t)len; if (real > ubm->buf->length - ubm->readIndex) { real = ubm->buf->length - ubm->readIndex; } if (buf != NULL && real > 0) { (void)memcpy_s(buf, len, ubm->buf->data + ubm->readIndex, real); ubm->readIndex += real; *readLen = (uint32_t)real; } if (*readLen > 0) { return BSL_SUCCESS; } /* when real equals 0, it is necessary to determine whether to retry based on eof */ if (ubm->eof != 0) { // retry if eof is not zero (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_READ | BSL_UIO_FLAGS_SHOULD_RETRY); } return BSL_SUCCESS; } static int32_t MemPending(BSL_UIO *uio, int32_t larg, uint64_t *ret) { if (ret == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (larg != sizeof(uint64_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } *ret = (uint64_t)(ubm->buf->length - ubm->readIndex); return BSL_SUCCESS; } static int32_t MemWpending(int32_t larg, uint64_t *ret) { if (ret == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (larg != sizeof(uint64_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } *ret = 0; // For the UIO of the mem type, return 0 return BSL_SUCCESS; } static int32_t MemGetInfo(BSL_UIO *uio, int32_t larg, BSL_UIO_CtrlGetInfoParam *param) { if (param == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (larg != sizeof(BSL_UIO_CtrlGetInfoParam)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } param->data = (uint8_t *)(&ubm->buf->data[ubm->readIndex]); param->size = ubm->buf->length - ubm->readIndex; return BSL_SUCCESS; } static int32_t MemGetPtr(BSL_UIO *uio, int32_t size, BSL_BufMem **ptr) { if (ptr == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != sizeof(BSL_BufMem *)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } if ((uio->flags & BSL_UIO_FLAGS_MEM_READ_ONLY) == 0) { if (UioBufMemSync(ubm) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_MEMMOVE_FAIL); return BSL_MEMMOVE_FAIL; } *ptr = ubm->buf; } else { /* when reset to read-only mode, can read from the beginning. * Temporary buf is not used to manage memory. */ ubm->tmpBuf->data = ubm->buf->data + ubm->readIndex; ubm->tmpBuf->length = ubm->buf->length - ubm->readIndex; ubm->tmpBuf->max = ubm->buf->max - ubm->readIndex; *ptr = ubm->tmpBuf; } return BSL_SUCCESS; } static int32_t MemSetEof(BSL_UIO *uio, int32_t larg, const int32_t *eof) { if (eof == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (larg != (int32_t)sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } ubm->eof = *eof; return BSL_SUCCESS; } static int32_t MemGetEof(BSL_UIO *uio, int32_t larg, int32_t *eof) { if (eof == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (larg != (int32_t)sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } *eof = ubm->eof; return BSL_SUCCESS; } static int32_t MemReset(BSL_UIO *uio) { UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL || ubm->buf->data == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if ((uio->flags & BSL_UIO_FLAGS_MEM_READ_ONLY) != 0) { // Read-only mode: The read index is reset and data can be read again ubm->readIndex = 0; } else { // Read/Write mode: Clear all data (void)memset_s(ubm->buf->data, ubm->buf->max, 0, ubm->buf->max); ubm->buf->length = 0; ubm->readIndex = 0; } return BSL_SUCCESS; } static int32_t MemFlush(int32_t larg, void *parg) { if (parg != NULL || larg != 0) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } return BSL_SUCCESS; } static int32_t MemCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { switch (cmd) { case BSL_UIO_PENDING: return MemPending(uio, larg, parg); case BSL_UIO_MEM_GET_INFO: return MemGetInfo(uio, larg, parg); case BSL_UIO_WPENDING: return MemWpending(larg, parg); case BSL_UIO_FLUSH: return MemFlush(larg, parg); case BSL_UIO_MEM_NEW_BUF: return MemNewBuf(uio, larg, parg); case BSL_UIO_MEM_GET_PTR: return MemGetPtr(uio, larg, parg); case BSL_UIO_MEM_SET_EOF: return MemSetEof(uio, larg, parg); case BSL_UIO_MEM_GET_EOF: return MemGetEof(uio, larg, parg); case BSL_UIO_RESET: return MemReset(uio); default: BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } } static int32_t MemPuts(BSL_UIO *uio, const char *buf, uint32_t *writeLen) { uint32_t len = 0; if (buf != NULL) { len = (uint32_t)strlen(buf); } return MemWrite(uio, buf, len, writeLen); } static int32_t MemGets(BSL_UIO *uio, char *buf, uint32_t *readLen) { uint32_t cnt = 0; int32_t ret; UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (ubm == NULL || ubm->buf == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (*readLen == 0) { return BSL_SUCCESS; } uint32_t len = (uint32_t)((*readLen - 1) >= (ubm->buf->length - ubm->readIndex) ? (ubm->buf->length - ubm->readIndex) : (*readLen - 1)); if (len == 0) { /* No data to read */ *buf = '\0'; *readLen = 0; return BSL_SUCCESS; } char *pre = ubm->buf->data + ubm->readIndex; /* len greater than 0 */ while (cnt < len) { cnt++; if (*pre++ == '\n') { break; } } ret = MemRead(uio, buf, cnt, readLen); if (ret == BSL_SUCCESS) { buf[cnt] = '\0'; } return ret; } static int32_t MemDestroy(BSL_UIO *uio) { UIO_BufMem *ubm = BSL_UIO_GetCtx(uio); if (BSL_UIO_GetIsUnderlyingClosedByUio(uio) && ubm != NULL) { if ((uio->flags & BSL_UIO_FLAGS_MEM_READ_ONLY) != 0) { ubm->buf->data = NULL; if (ubm->tmpBuf != NULL) { ubm->tmpBuf->data = NULL; BSL_BufMemFree(ubm->tmpBuf); } } BSL_BufMemFree(ubm->buf); BSL_SAL_FREE(ubm); } BSL_UIO_SetCtx(uio, NULL); uio->init = false; return BSL_SUCCESS; } static int32_t MemCreate(BSL_UIO *uio) { UIO_BufMem *ubm = (UIO_BufMem *)BSL_SAL_Calloc(1, sizeof(UIO_BufMem)); if (ubm == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } ubm->buf = BSL_BufMemNew(); if (ubm->buf == NULL) { BSL_SAL_FREE(ubm); BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } ubm->eof = -1; BSL_UIO_SetCtx(uio, ubm); BSL_UIO_SetIsUnderlyingClosedByUio(uio, true); // memory buffer is created here and will be closed here by default. uio->init = true; return BSL_SUCCESS; } const BSL_UIO_Method *BSL_UIO_MemMethod(void) { static const BSL_UIO_Method method = { BSL_UIO_MEM, MemWrite, MemRead, MemCtrl, MemPuts, MemGets, MemCreate, MemDestroy }; return &method; } #endif /* HITLS_BSL_UIO_MEM */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_mem.c
C
unknown
12,802
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_SCTP #include "securec.h" #include "bsl_sal.h" #include "bsl_binlog_id.h" #include "bsl_log_internal.h" #include "bsl_log.h" #include "bsl_err_internal.h" #include "bsl_errno.h" #include "bsl_uio.h" #include "sal_net.h" #include "uio_base.h" #include "uio_abstraction.h" #define SCTP_SHARE_AUTHKEY_ID_MAX 65535 typedef struct { bool peerAuthed; /* Whether auth is enabled at the peer end */ /* Whether authkey is added: If authkey is added but not active, success is returned when authkey is added again. */ bool isAddAuthkey; bool reserved[2]; /* Four-byte alignment is reserved. */ uint16_t sendAppStreamId; /* ID of the stream sent by the user-specified app. */ uint16_t prevShareKeyId; uint16_t shareKeyId; uint16_t reserved1; /* Four-byte alignment is reserved. */ } BslSctpData; typedef struct { BslSctpData data; int32_t fd; // Network socket uint32_t ipLen; uint8_t ip[IP_ADDR_MAX_LEN]; struct BSL_UIO_MethodStruct method; bool isAppMsg; // whether the message sent is the app message } SctpParameters; static int32_t BslSctpNew(BSL_UIO *uio) { SctpParameters *parameters = (SctpParameters *)BSL_SAL_Calloc(1u, sizeof(SctpParameters)); if (parameters == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05031, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: sctp param malloc fail.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } parameters->fd = -1; parameters->method.uioType = BSL_UIO_SCTP; uio->ctx = parameters; uio->ctxLen = sizeof(SctpParameters); // The default value of init is 0. Set the value of init to 1 after the fd is set. return BSL_SUCCESS; } static int32_t BslSctpDestroy(BSL_UIO *uio) { if (uio == NULL) { return BSL_SUCCESS; } SctpParameters *ctx = BSL_UIO_GetCtx(uio); uio->init = 0; if (ctx != NULL) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio) && ctx->fd != -1) { (void)BSL_SAL_SockClose(ctx->fd); } BSL_SAL_FREE(ctx); BSL_UIO_SetCtx(uio, NULL); } return BSL_SUCCESS; } static int32_t BslSctpWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { if (uio == NULL || uio->ctx == NULL || ((SctpParameters *)uio->ctx)->method.uioWrite == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05081, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Sctp write input error.", 0, 0, 0, 0); return BSL_INVALID_ARG; } *writeLen = 0; return ((SctpParameters *)uio->ctx)->method.uioWrite(uio, buf, len, writeLen); } static int32_t BslSctpRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen) { if (uio == NULL || uio->ctx == NULL || ((SctpParameters *)uio->ctx)->method.uioRead == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05082, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Sctp read input error.", 0, 0, 0, 0); return BSL_INVALID_ARG; } *readLen = 0; SctpParameters *parameters = (SctpParameters *)uio->ctx; if (!parameters->data.peerAuthed) { if (parameters->method.uioCtrl == NULL || parameters->method.uioCtrl(uio, BSL_UIO_SCTP_CHECK_PEER_AUTH, sizeof(parameters->data.peerAuthed), &parameters->data.peerAuthed) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05083, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Check peer auth failed.", 0, 0, 0, 0); return BSL_UIO_IO_EXCEPTION; } parameters->data.peerAuthed = true; } return parameters->method.uioRead(uio, buf, len, readLen); } static int32_t BslSctpAddAuthKey(BSL_UIO *uio, const uint8_t *parg, uint16_t larg) { SctpParameters *parameters = (SctpParameters *)BSL_UIO_GetCtx(uio); if (parg == NULL || larg != sizeof(BSL_UIO_SctpAuthKey)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05062, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "add auth key failed", 0, 0, 0, 0); return BSL_INVALID_ARG; } if (parameters->data.isAddAuthkey) { return BSL_SUCCESS; } uint16_t prevShareKeyId = parameters->data.shareKeyId; if (parameters->data.shareKeyId >= SCTP_SHARE_AUTHKEY_ID_MAX) { parameters->data.shareKeyId = 1; } else { parameters->data.shareKeyId++; } BSL_UIO_SctpAuthKey key = { 0 }; key.shareKeyId = parameters->data.shareKeyId; key.authKey = parg; key.authKeySize = larg; int32_t ret = parameters->method.uioCtrl(uio, BSL_UIO_SCTP_ADD_AUTH_SHARED_KEY, (int32_t)sizeof(key), &key); if (ret != BSL_SUCCESS) { parameters->data.shareKeyId = prevShareKeyId; BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05065, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "add auth key failed", 0, 0, 0, 0); return BSL_UIO_IO_EXCEPTION; } parameters->data.isAddAuthkey = true; parameters->data.prevShareKeyId = prevShareKeyId; return BSL_SUCCESS; } static int32_t BslSctpActiveAuthKey(BSL_UIO *uio) { SctpParameters *parameters = BSL_UIO_GetCtx(uio); if (parameters == NULL || parameters->method.uioCtrl == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint16_t shareKeyId = parameters->data.shareKeyId; int32_t ret = parameters->method.uioCtrl(uio, BSL_UIO_SCTP_ACTIVE_AUTH_SHARED_KEY, (int32_t)sizeof(shareKeyId), &shareKeyId); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05066, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "active auth key failed", 0, 0, 0, 0); return BSL_UIO_IO_EXCEPTION; } parameters->data.isAddAuthkey = false; return BSL_SUCCESS; } static int32_t BslSctpDelPreAuthKey(BSL_UIO *uio) { SctpParameters *parameters = BSL_UIO_GetCtx(uio); if (parameters == NULL || parameters->method.uioCtrl == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint16_t delShareKeyId = parameters->data.prevShareKeyId; int32_t ret = parameters->method.uioCtrl(uio, BSL_UIO_SCTP_DEL_PRE_AUTH_SHARED_KEY, (int32_t)sizeof(delShareKeyId), &delShareKeyId); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05067, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "del pre auth key failed", 0, 0, 0, 0); return BSL_UIO_IO_EXCEPTION; } return BSL_SUCCESS; } static int32_t BslSctpIsSndBuffEmpty(BSL_UIO *uio, void *parg, int32_t larg) { SctpParameters *parameters = BSL_UIO_GetCtx(uio); if (parameters == NULL || parameters->method.uioCtrl == NULL || parg == NULL || larg != sizeof(bool)) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint8_t isEmpty = 0; if (parameters->method.uioCtrl(uio, BSL_UIO_SCTP_SND_BUFF_IS_EMPTY, (int32_t)sizeof(uint8_t), &isEmpty) != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05068, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "get sctp status failed", 0, 0, 0, 0); return BSL_UIO_IO_EXCEPTION; } *(bool *)parg = (isEmpty > 0); return BSL_SUCCESS; } static int32_t BslSctpGetSendStreamId(const SctpParameters *parameters, void *parg, int32_t larg) { if (larg != (int32_t)sizeof(uint16_t) || parg == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05046, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Sctp input err.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } uint16_t *sendStreamId = (uint16_t *)parg; if (parameters->isAppMsg) { *sendStreamId = parameters->data.sendAppStreamId; } else { *sendStreamId = 0; } BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05047, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN, "Uio: User Get SCTP send StreamId [%hu].", *sendStreamId, 0, 0, 0); return BSL_SUCCESS; } int32_t BslSctpSetAppStreamId(SctpParameters *parameters, const void *parg, int32_t larg) { if (larg != (int32_t)sizeof(uint16_t) || parg == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05048, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Sctp input err.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } parameters->data.sendAppStreamId = *(const uint16_t *)parg; BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05055, BSL_LOG_LEVEL_DEBUG, BSL_LOG_BINLOG_TYPE_RUN, "Uio: User set SCTP AppStreamId [%hu].", parameters->data.sendAppStreamId, 0, 0, 0); return BSL_SUCCESS; } static int32_t BslSctpSetPeerIpAddr(SctpParameters *parameters, const uint8_t *addr, int32_t size) { if (addr == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05049, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: NULL error.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != IP_ADDR_V4_LEN && size != IP_ADDR_V6_LEN) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05050, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Set peer ip address input error.", 0, 0, 0, 0); return BSL_UIO_FAIL; } (void)memcpy_s(parameters->ip, sizeof(parameters->ip), addr, size); parameters->ipLen = (uint32_t)size; return BSL_SUCCESS; } static int32_t BslSctpGetPeerIpAddr(SctpParameters *parameters, void *parg, int32_t larg) { BSL_UIO_CtrlGetPeerIpAddrParam *para = (BSL_UIO_CtrlGetPeerIpAddrParam *)parg; if (parg == NULL || larg != (int32_t)sizeof(BSL_UIO_CtrlGetPeerIpAddrParam) || para->addr == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05051, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Get peer ip address input error.", 0, 0, 0, 0); return BSL_NULL_INPUT; } /* Check whether the IP address is set. */ if (parameters->ipLen == 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05052, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Ip address is already existed.", 0, 0, 0, 0); return BSL_UIO_FAIL; } if (para->size < parameters->ipLen) { BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05053, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Ip address length err.", 0, 0, 0, 0); return BSL_UIO_FAIL; } (void)memcpy_s(para->addr, para->size, parameters->ip, parameters->ipLen); para->size = parameters->ipLen; return BSL_SUCCESS; } static int32_t BslSctpSetFd(BSL_UIO *uio, void *parg, int32_t larg) { if (larg != (int32_t)sizeof(int32_t) || parg == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } int32_t *fd = (int32_t *)parg; SctpParameters *parameters = BSL_UIO_GetCtx(uio); if (parameters->fd != -1) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { (void)BSL_SAL_SockClose(parameters->fd); } } parameters->fd = *fd; uio->init = true; return BSL_SUCCESS; } static int32_t BslSctpGetFd(SctpParameters *parameters, void *parg, int32_t larg) { if (larg != (int32_t)sizeof(int32_t) || parg == NULL) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05054, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "get fd handle invalid parameter.", 0, 0, 0, 0); return BSL_INVALID_ARG; } *(int32_t *)parg = parameters->fd; return BSL_SUCCESS; } static int32_t BslSctpMaskAppMsg(SctpParameters *parameters, void *parg, int32_t larg) { if (parg == NULL || larg != sizeof(bool)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05030, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "mask app msg failed", 0, 0, 0, 0); return BSL_INVALID_ARG; } parameters->isAppMsg = *(bool *)parg; return BSL_SUCCESS; } static int32_t BslSctpSetCtxCb(SctpParameters *parameters, int32_t type, void *func) { if (parameters == NULL || func == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (type) { case BSL_UIO_WRITE_CB: parameters->method.uioWrite = func; break; case BSL_UIO_READ_CB: parameters->method.uioRead = func; break; case BSL_UIO_CTRL_CB: parameters->method.uioCtrl = func; break; default: BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } return BSL_SUCCESS; } int32_t BslSctpCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { if (uio->ctx == NULL) { return BSL_NULL_INPUT; } SctpParameters *parameters = BSL_UIO_GetCtx(uio); switch (cmd) { case BSL_UIO_SET_PEER_IP_ADDR: return BslSctpSetPeerIpAddr(parameters, parg, larg); case BSL_UIO_GET_PEER_IP_ADDR: return BslSctpGetPeerIpAddr(parameters, parg, larg); case BSL_UIO_SET_FD: return BslSctpSetFd(uio, parg, larg); case BSL_UIO_GET_FD: return BslSctpGetFd(parameters, parg, larg); case BSL_UIO_SCTP_GET_SEND_STREAM_ID: return BslSctpGetSendStreamId(parameters, parg, larg); case BSL_UIO_SCTP_SET_APP_STREAM_ID: return BslSctpSetAppStreamId(parameters, parg, larg); case BSL_UIO_SCTP_ADD_AUTH_SHARED_KEY: if (larg < 0 || larg > UINT16_MAX) { break; } return BslSctpAddAuthKey(uio, parg, larg); case BSL_UIO_SCTP_ACTIVE_AUTH_SHARED_KEY: return BslSctpActiveAuthKey(uio); case BSL_UIO_SCTP_DEL_PRE_AUTH_SHARED_KEY: return BslSctpDelPreAuthKey(uio); case BSL_UIO_SCTP_MASK_APP_MESSAGE: return BslSctpMaskAppMsg(parameters, parg, larg); case BSL_UIO_SCTP_SND_BUFF_IS_EMPTY: return BslSctpIsSndBuffEmpty(uio, parg, larg); case BSL_UIO_SCTP_SET_CALLBACK: return BslSctpSetCtxCb(parameters, larg, parg); case BSL_UIO_FLUSH: return BSL_SUCCESS; default: break; } BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05069, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "invalid args", 0, 0, 0, 0); return BSL_INVALID_ARG; } const BSL_UIO_Method *BSL_UIO_SctpMethod(void) { static const BSL_UIO_Method method = { BSL_UIO_SCTP, BslSctpWrite, BslSctpRead, BslSctpCtrl, NULL, NULL, BslSctpNew, BslSctpDestroy }; return &method; } #endif /* HITLS_BSL_UIO_SCTP */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_sctp.c
C
unknown
15,749
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_TCP #include "bsl_binlog_id.h" #include "bsl_err_internal.h" #include "bsl_log_internal.h" #include "bsl_log.h" #include "bsl_sal.h" #include "bsl_errno.h" #include "sal_net.h" #include "uio_base.h" #include "uio_abstraction.h" typedef struct { int32_t fd; } TcpPrameters; static int32_t TcpNew(BSL_UIO *uio) { if (uio->ctx != NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05056, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: ctx is already existed.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } TcpPrameters *parameters = (TcpPrameters *)BSL_SAL_Calloc(1u, sizeof(TcpPrameters)); if (parameters == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05057, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: tcp param malloc fail.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } parameters->fd = -1; uio->ctx = parameters; uio->ctxLen = sizeof(TcpPrameters); // Specifies whether to be closed by uio when setting fd. // The default value of init is 0. Set the value of init to 1 after the fd is set. return BSL_SUCCESS; } static int32_t TcpSocketDestroy(BSL_UIO *uio) { if (uio == NULL) { return BSL_SUCCESS; } uio->init = 0; TcpPrameters *ctx = BSL_UIO_GetCtx(uio); if (ctx != NULL) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio) && ctx->fd != -1) { (void)BSL_SAL_SockClose(ctx->fd); } BSL_SAL_FREE(ctx); BSL_UIO_SetCtx(uio, NULL); } return BSL_SUCCESS; } static int32_t TcpSocketWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { *writeLen = 0; int32_t err = 0; int32_t fd = BSL_UIO_GetFd(uio); if (fd < 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } int32_t ret = SAL_Write(fd, buf, len, &err); (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); if (ret > 0) { *writeLen = (uint32_t)ret; return BSL_SUCCESS; } // If the value of ret is less than or equal to 0, check errno first. if (UioIsNonFatalErr(err)) { // Indicates the errno for determining whether retry is allowed. (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_WRITE | BSL_UIO_FLAGS_SHOULD_RETRY); return BSL_SUCCESS; } BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } static int32_t TcpSocketRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen) { *readLen = 0; int32_t err = 0; (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); int32_t fd = BSL_UIO_GetFd(uio); if (fd < 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } int32_t ret = SAL_Read(fd, buf, len, &err); if (ret > 0) { // Success *readLen = (uint32_t)ret; return BSL_SUCCESS; } // If the value of ret is less than or equal to 0, check errno first. if (UioIsNonFatalErr(err)) { // Indicates the errno for determining whether retry is allowed. (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_READ | BSL_UIO_FLAGS_SHOULD_RETRY); return BSL_SUCCESS; } if (ret == 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EOF); return BSL_UIO_IO_EOF; } BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } static int32_t TcpSetFd(BSL_UIO *uio, int32_t size, const int32_t *fd) { bool invalid = (fd == NULL) || (uio == NULL); if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(*fd)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } TcpPrameters *ctx = BSL_UIO_GetCtx(uio); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (ctx->fd != -1) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { (void)BSL_SAL_SockClose(ctx->fd); } } ctx->fd = *fd; uio->init = 1; return BSL_SUCCESS; } static int32_t TcpGetFd(BSL_UIO *uio, int32_t size, int32_t *fd) { bool invalid = uio == NULL || fd == NULL; if (invalid) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(*fd)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } TcpPrameters *ctx = BSL_UIO_GetCtx(uio); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } *fd = ctx->fd; return BSL_SUCCESS; } static int32_t TcpSocketCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { switch (cmd) { case BSL_UIO_SET_FD: return TcpSetFd(uio, larg, parg); case BSL_UIO_GET_FD: return TcpGetFd(uio, larg, parg); case BSL_UIO_FLUSH: return BSL_SUCCESS; default: break; } BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } const BSL_UIO_Method *BSL_UIO_TcpMethod(void) { static const BSL_UIO_Method method = { BSL_UIO_TCP, TcpSocketWrite, TcpSocketRead, TcpSocketCtrl, NULL, NULL, TcpNew, TcpSocketDestroy }; return &method; } #endif /* HITLS_BSL_UIO_TCP */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_tcp.c
C
unknown
6,013
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_BSL_UIO_UDP #include "securec.h" #include "bsl_sal.h" #include "bsl_binlog_id.h" #include "bsl_log_internal.h" #include "bsl_log.h" #include "bsl_err_internal.h" #include "bsl_errno.h" #include "bsl_uio.h" #include "sal_net.h" #include "uio_base.h" #include "uio_abstraction.h" #define IPV4_WITH_UDP_HEADER_LEN 28 /* IPv4 protocol header 20 + UDP header 8 */ #define IPV6_WITH_UDP_HEADER_LEN 48 /* IPv6 protocol header 40 + UDP header 8 */ typedef struct { BSL_SAL_SockAddr peer; int32_t fd; // Network socket uint32_t connected; int32_t sysErrno; } UdpParameters; static int32_t UdpNew(BSL_UIO *uio) { if (uio->ctx != NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05056, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: ctx is already existed.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } UdpParameters *parameters = (UdpParameters *)BSL_SAL_Calloc(1u, sizeof(UdpParameters)); if (parameters == NULL) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05073, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: udp param malloc fail.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } int32_t ret = SAL_SockAddrNew(&(parameters->peer)); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_Free(parameters); return ret; } parameters->fd = -1; parameters->connected = 0; uio->ctx = parameters; uio->ctxLen = sizeof(UdpParameters); // Specifies whether to be closed by uio when setting fd. // The default value of init is 0. Set the value of init to 1 after the fd is set. return BSL_SUCCESS; } static int32_t UdpSocketDestroy(BSL_UIO *uio) { if (uio == NULL) { return BSL_SUCCESS; } UdpParameters *ctx = BSL_UIO_GetCtx(uio); if (ctx != NULL) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio) && ctx->fd != -1) { (void)BSL_SAL_SockClose(ctx->fd); } SAL_SockAddrFree(ctx->peer); BSL_SAL_Free(ctx); BSL_UIO_SetCtx(uio, NULL); } uio->init = false; return BSL_SUCCESS; } static int32_t UdpGetPeerIpAddr(UdpParameters *parameters, int32_t larg, uint8_t *parg) { uint32_t uniAddrSize = SAL_SockAddrSize(parameters->peer); if (parg == NULL || (uint32_t)larg < uniAddrSize) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05074, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: Get peer ip address input error.", 0, 0, 0, 0); return BSL_NULL_INPUT; } SAL_SockAddrCopy(parg, parameters->peer); return BSL_SUCCESS; } static int32_t UdpSetPeerIpAddr(UdpParameters *parameters, const uint8_t *addr, uint32_t size) { uint32_t uniAddrSize = SAL_SockAddrSize(parameters->peer); if (addr == NULL || uniAddrSize == 0 || size > uniAddrSize) { BSL_LOG_BINLOG_FIXLEN(BINLOG_ID05073, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Uio: NULL error.", 0, 0, 0, 0); BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } SAL_SockAddrCopy(parameters->peer, (BSL_SAL_SockAddr)(uintptr_t)addr); return BSL_SUCCESS; } static int32_t UdpSetFd(BSL_UIO *uio, int32_t size, const int32_t *fd) { if (fd == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(*fd)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UdpParameters *udpCtx = BSL_UIO_GetCtx(uio); // ctx is not NULL if (udpCtx->fd != -1) { if (BSL_UIO_GetIsUnderlyingClosedByUio(uio)) { (void)BSL_SAL_SockClose(udpCtx->fd); } } udpCtx->fd = *fd; uio->init = true; return BSL_SUCCESS; } static int32_t UdpGetMtuOverhead(UdpParameters *parameters, int32_t size, uint8_t *overhead) { if (overhead == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(uint8_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } switch (SAL_SockAddrGetFamily(parameters->peer)) { case SAL_IPV4: /* 20 for ipv4, 8 for udp */ *overhead = IPV4_WITH_UDP_HEADER_LEN; break; case SAL_IPV6: *overhead = IPV6_WITH_UDP_HEADER_LEN; break; default: *overhead = IPV4_WITH_UDP_HEADER_LEN; break; } return BSL_SUCCESS; } static int32_t UdpQueryMtu(UdpParameters *parameters, int32_t size, uint32_t *mtu) { if (mtu == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } int32_t socketVal = 0; int32_t socketOptLen = sizeof(socketVal); switch (SAL_SockAddrGetFamily(parameters->peer)) { case SAL_IPV4: if (BSL_SAL_GetSockopt(parameters->fd, SAL_PROTO_IP_LEVEL, SAL_MTU_OPTION, (void *)&socketVal, &socketOptLen) != BSL_SUCCESS || socketVal < IPV4_WITH_UDP_HEADER_LEN) { *mtu = 0; } else { *mtu = (uint32_t)socketVal - IPV4_WITH_UDP_HEADER_LEN; } break; case SAL_IPV6: if (BSL_SAL_GetSockopt(parameters->fd, SAL_PROTO_IPV6_LEVEL, SAL_IPV6_MTU_OPTION, (void *)&socketVal, &socketOptLen) != BSL_SUCCESS || socketVal < IPV6_WITH_UDP_HEADER_LEN) { *mtu = 0; } else { *mtu = (uint32_t)socketVal - IPV6_WITH_UDP_HEADER_LEN; } break; default: *mtu = 0; break; } return BSL_SUCCESS; } static int32_t UdpIsMtuExceeded(UdpParameters *parameters, int32_t size, bool *exceeded) { if (exceeded == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(bool)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } (void)parameters; #ifdef EMSGSIZE if (parameters->sysErrno == EMSGSIZE) { *exceeded = true; parameters->sysErrno = 0; } else { #endif *exceeded = false; #ifdef EMSGSIZE } #endif return BSL_SUCCESS; } static int32_t UdpGetFd(BSL_UIO *uio, int32_t size, int32_t *fd) { if (fd == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } if (size != (int32_t)sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); return BSL_INVALID_ARG; } UdpParameters *ctx = BSL_UIO_GetCtx(uio); // ctx is not NULL *fd = ctx->fd; return BSL_SUCCESS; } int32_t UdpSocketCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg) { UdpParameters *parameters = BSL_UIO_GetCtx(uio); if (parameters == NULL) { BSL_ERR_PUSH_ERROR(BSL_NULL_INPUT); return BSL_NULL_INPUT; } switch (cmd) { case BSL_UIO_SET_FD: return UdpSetFd(uio, larg, parg); case BSL_UIO_GET_FD: return UdpGetFd(uio, larg, parg); case BSL_UIO_SET_PEER_IP_ADDR: return UdpSetPeerIpAddr(parameters, parg, (uint32_t)larg); case BSL_UIO_GET_PEER_IP_ADDR: return UdpGetPeerIpAddr(parameters, larg, parg); case BSL_UIO_UDP_SET_CONNECTED: if (parg != NULL) { parameters->connected = 1; return UdpSetPeerIpAddr(parameters, parg, (uint32_t)larg); } else { parameters->connected = 0; return BSL_SUCCESS; } case BSL_UIO_UDP_GET_MTU_OVERHEAD: return UdpGetMtuOverhead(parameters, larg, parg); case BSL_UIO_UDP_QUERY_MTU: return UdpQueryMtu(parameters, larg, parg); case BSL_UIO_UDP_MTU_EXCEEDED: return UdpIsMtuExceeded(parameters, larg, parg); case BSL_UIO_FLUSH: return BSL_SUCCESS; default: break; } BSL_ERR_PUSH_ERROR(BSL_UIO_FAIL); return BSL_UIO_FAIL; } static int32_t UdpSocketWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen) { int32_t err = 0; int32_t sendBytes = 0; int32_t fd = BSL_UIO_GetFd(uio); UdpParameters *ctx = (UdpParameters *)BSL_UIO_GetCtx(uio); if (ctx == NULL || fd < 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } ctx->sysErrno = 0; uint32_t peerAddrSize = SAL_SockAddrSize(ctx->peer); if (ctx->connected == 1) { sendBytes = SAL_Write(fd, buf, len, &err); } else { sendBytes = SAL_Sendto(fd, buf, len, 0, ctx->peer, peerAddrSize, &err); } (void)BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_RWS | BSL_UIO_FLAGS_SHOULD_RETRY); if (sendBytes < 0) { /* None-fatal error */ if (UioIsNonFatalErr(err)) { (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_WRITE | BSL_UIO_FLAGS_SHOULD_RETRY); ctx->sysErrno = err; return BSL_SUCCESS; } /* Fatal error */ BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } *writeLen = (uint32_t)sendBytes; return BSL_SUCCESS; } static int32_t UdpSocketRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen) { *readLen = 0; int32_t err = 0; int32_t fd = BSL_UIO_GetFd(uio); UdpParameters *ctx = BSL_UIO_GetCtx(uio); if (ctx == NULL || fd < 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } int32_t addrlen = (int32_t)SAL_SockAddrSize(ctx->peer); int32_t ret = SAL_RecvFrom(fd, buf, len, 0, ctx->peer, &addrlen, &err); if (ret < 0) { if (UioIsNonFatalErr(err) == true) { (void)BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_READ | BSL_UIO_FLAGS_SHOULD_RETRY); return BSL_SUCCESS; } /* Fatal error */ BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } else if (ret == 0) { BSL_ERR_PUSH_ERROR(BSL_UIO_IO_EXCEPTION); return BSL_UIO_IO_EXCEPTION; } *readLen = (uint32_t)ret; return BSL_SUCCESS; } const BSL_UIO_Method *BSL_UIO_UdpMethod(void) { static const BSL_UIO_Method method = { BSL_UIO_UDP, UdpSocketWrite, UdpSocketRead, UdpSocketCtrl, NULL, NULL, UdpNew, UdpSocketDestroy }; return &method; } #endif /* HITLS_BSL_UIO_UDP */
2302_82127028/openHiTLS-examples
bsl/uio/src/uio_udp.c
C
unknown
11,217
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef DECODE_LOCAL_H #define DECODE_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CODECS #include "crypt_eal_implprovider.h" #include "crypt_eal_codecs.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define CRYPT_DECODER_STATE_UNTRIED 1 #define CRYPT_DECODER_STATE_TRING 2 #define CRYPT_DECODER_STATE_TRIED 3 #define CRYPT_DECODER_STATE_SUCCESS 4 #define MAX_CRYPT_DECODER_FORMAT_TYPE_STR_LEN 64 /** * @brief Decoder context structure */ typedef struct CRYPT_DECODER_Method { CRYPT_DECODER_IMPL_NewCtx newCtx; /* New context function */ CRYPT_DECODER_IMPL_SetParam setParam; /* Set parameter function */ CRYPT_DECODER_IMPL_GetParam getParam; /* Get parameter function */ CRYPT_DECODER_IMPL_Decode decode; /* Decode function */ CRYPT_DECODER_IMPL_FreeOutData freeOutData; /* Free output data function */ CRYPT_DECODER_IMPL_FreeCtx freeCtx; /* Free context function */ } CRYPT_DECODER_Method; struct CRYPT_DecoderCtx { /* To get the provider manager context when query */ CRYPT_EAL_ProvMgrCtx *providerMgrCtx; /* Provider manager context */ char *attrName; /* Attribute name */ const char *inFormat; /* Input data format */ const char *inType; /* Input data type */ const char *outFormat; /* Output data format */ const char *outType; /* Output data type */ void *decoderCtx; /* Decoder internal context */ CRYPT_DECODER_Method *method; /* Decoder method */ int32_t decoderState; /* Decoder state */ }; typedef struct { const char *format; /* Data format */ const char *type; /* Data type */ BSL_Param *data; /* Data */ } DataInfo; typedef struct CRYPT_DECODER_Node { DataInfo inData; /* Input data */ DataInfo outData; /* Output data */ CRYPT_DECODER_Ctx *decoderCtx; /* Decoder context */ } CRYPT_DECODER_Node; #define MAX_CRYPT_DECODE_FORMAT_TYPE_SIZE 128 struct CRYPT_DECODER_PoolCtx { CRYPT_EAL_LibCtx *libCtx; /* EAL library context */ const char *attrName; /* Attribute name */ const char *inputFormat; /* Input data format */ const char *inputType; /* Input data type */ int32_t inputKeyType; /* Input data key type */ BSL_Param *input; /* Input data */ const char *targetFormat; /* Target format */ const char *targetType; /* Target type */ int32_t targetKeyType; /* Target data key type */ BslList *decoders; /* The decoders pool of all provider, the entry is CRYPT_DECODER_Ctx */ BslList *decoderPath; /* The path of the decoder, the entry is CRYPT_DECODER_Node */ }; typedef struct { char *attrName; const char *inFormat; const char *inType; const char *outFormat; const char *outType; } DECODER_AttrInfo; int32_t CRYPT_DECODE_ParseDecoderAttr(const char *attrName, DECODER_AttrInfo *info); CRYPT_DECODER_Ctx *CRYPT_DECODE_NewDecoderCtxByMethod(const CRYPT_EAL_Func *funcs, CRYPT_EAL_ProvMgrCtx *mgrCtx, const char *attrName); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CODECS */ #endif /* DECODE_LOCAL_H */
2302_82127028/openHiTLS-examples
codecs/include/decode_local.h
C
unknown
4,097
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CODECS) && defined(HITLS_CRYPTO_PROVIDER) #include "securec.h" #include "bsl_sal.h" #include "bsl_list.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_eal_provider.h" #include "crypt_eal_implprovider.h" #include "crypt_provider.h" #include "crypt_eal_pkey.h" #include "crypt_eal_codecs.h" #include "bsl_types.h" #include "crypt_types.h" #include "crypt_utils.h" #include "decode_local.h" int32_t CRYPT_DECODE_ParseDecoderAttr(const char *attrName, DECODER_AttrInfo *info) { char *rest = NULL; info->inFormat = NULL; info->inType = NULL; info->outFormat = NULL; info->outType = NULL; info->attrName = (char *)BSL_SAL_Dump(attrName, (uint32_t)strlen(attrName) + 1); if (info->attrName == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } char *token = strtok_s(info->attrName, ",", &rest); while (token != NULL) { while (*token == ' ') { token++; } if (strstr(token, "inFormat=") == token) { info->inFormat = token + strlen("inFormat="); } else if (strstr(token, "inType=") == token) { info->inType = token + strlen("inType="); } else if (strstr(token, "outFormat=") == token) { info->outFormat = token + strlen("outFormat="); } else if (strstr(token, "outType=") == token) { info->outType = token + strlen("outType="); } token = strtok_s(NULL, ",", &rest); } return CRYPT_SUCCESS; } static int32_t SetDecoderMethod(CRYPT_DECODER_Ctx *ctx, const CRYPT_EAL_Func *funcs) { int32_t index = 0; CRYPT_DECODER_Method *method = BSL_SAL_Calloc(1, sizeof(CRYPT_DECODER_Method)); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } while (funcs[index].func != NULL) { switch (funcs[index].id) { case CRYPT_DECODER_IMPL_NEWCTX: method->newCtx = (CRYPT_DECODER_IMPL_NewCtx)funcs[index].func; break; case CRYPT_DECODER_IMPL_SETPARAM: method->setParam = (CRYPT_DECODER_IMPL_SetParam)funcs[index].func; break; case CRYPT_DECODER_IMPL_GETPARAM: method->getParam = (CRYPT_DECODER_IMPL_GetParam)funcs[index].func; break; case CRYPT_DECODER_IMPL_DECODE: method->decode = (CRYPT_DECODER_IMPL_Decode)funcs[index].func; break; case CRYPT_DECODER_IMPL_FREEOUTDATA: method->freeOutData = (CRYPT_DECODER_IMPL_FreeOutData)funcs[index].func; break; case CRYPT_DECODER_IMPL_FREECTX: method->freeCtx = (CRYPT_DECODER_IMPL_FreeCtx)funcs[index].func; break; default: BSL_SAL_Free(method); BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL); return CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL; } index++; } ctx->method = method; return CRYPT_SUCCESS; } CRYPT_DECODER_Ctx *CRYPT_DECODE_NewDecoderCtxByMethod(const CRYPT_EAL_Func *funcs, CRYPT_EAL_ProvMgrCtx *mgrCtx, const char *attrName) { void *provCtx = NULL; DECODER_AttrInfo attrInfo = {0}; CRYPT_DECODER_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(CRYPT_DECODER_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } int32_t ret = CRYPT_EAL_ProviderCtrl(mgrCtx, CRYPT_PROVIDER_GET_USER_CTX, &provCtx, sizeof(provCtx)); if (ret != CRYPT_SUCCESS) { goto ERR; } ret = SetDecoderMethod(ctx, funcs); if (ret != CRYPT_SUCCESS) { goto ERR; } if (ctx->method->newCtx == NULL || ctx->method->setParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); goto ERR; } ctx->decoderCtx = ctx->method->newCtx(provCtx); if (ctx->decoderCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } BSL_Param param[2] = {{CRYPT_PARAM_DECODE_PROVIDER_CTX, BSL_PARAM_TYPE_CTX_PTR, mgrCtx, 0, 0}, BSL_PARAM_END}; ret = ctx->method->setParam(ctx->decoderCtx, param); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (attrName != NULL) { ret = CRYPT_DECODE_ParseDecoderAttr(attrName, &attrInfo); if (ret != CRYPT_SUCCESS) { goto ERR; } } ctx->providerMgrCtx = mgrCtx; ctx->inFormat = attrInfo.inFormat; ctx->inType = attrInfo.inType; ctx->outFormat = attrInfo.outFormat; ctx->outType = attrInfo.outType; ctx->attrName = attrName != NULL ? attrInfo.attrName : NULL; ctx->decoderState = CRYPT_DECODER_STATE_UNTRIED; return ctx; ERR: CRYPT_DECODE_Free(ctx); return NULL; } CRYPT_DECODER_Ctx *CRYPT_DECODE_ProviderNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t keyType, const char *attrName) { const CRYPT_EAL_Func *funcsDecoder = NULL; CRYPT_EAL_ProvMgrCtx *mgrCtx = NULL; int32_t ret = CRYPT_EAL_ProviderGetFuncsAndMgrCtx(libCtx, CRYPT_EAL_OPERAID_DECODER, keyType, attrName, &funcsDecoder, &mgrCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } if (mgrCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } return CRYPT_DECODE_NewDecoderCtxByMethod(funcsDecoder, mgrCtx, attrName); } /* Free decoder context */ void CRYPT_DECODE_Free(CRYPT_DECODER_Ctx *ctx) { if (ctx == NULL) { return; } if (ctx->method != NULL && ctx->method->freeCtx != NULL) { ctx->method->freeCtx(ctx->decoderCtx); } BSL_SAL_Free(ctx->method); BSL_SAL_Free(ctx->attrName); BSL_SAL_Free(ctx); } /* Set decoder parameters */ int32_t CRYPT_DECODE_SetParam(CRYPT_DECODER_Ctx *ctx, const BSL_Param *param) { if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->method == NULL || ctx->method->setParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } return ctx->method->setParam(ctx->decoderCtx, param); } /* Get decoder parameters */ int32_t CRYPT_DECODE_GetParam(CRYPT_DECODER_Ctx *ctx, BSL_Param *param) { if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->method == NULL || ctx->method->getParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } return ctx->method->getParam(ctx->decoderCtx, param); } /* Execute decode operation */ int32_t CRYPT_DECODE_Decode(CRYPT_DECODER_Ctx *ctx, const BSL_Param *inParam, BSL_Param **outParam) { if (ctx == NULL || inParam == NULL || outParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->method == NULL || ctx->method->decode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } int32_t ret = ctx->method->decode(ctx->decoderCtx, inParam, outParam); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } void CRYPT_DECODE_FreeOutData(CRYPT_DECODER_Ctx *ctx, BSL_Param *outData) { if (ctx == NULL || outData == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return; } if (ctx->method == NULL || ctx->method->freeOutData == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return; } ctx->method->freeOutData(ctx->decoderCtx, outData); } #endif /* HITLS_CRYPTO_CODECS && HITLS_CRYPTO_PROVIDER */
2302_82127028/openHiTLS-examples
codecs/src/decode.c
C
unknown
8,345
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CODECS) && defined(HITLS_CRYPTO_PROVIDER) #include <stdint.h> #include <string.h> #include "securec.h" #include "crypt_eal_codecs.h" #include "crypt_eal_implprovider.h" #include "crypt_provider.h" #include "crypt_params_key.h" #include "crypt_types.h" #include "crypt_errno.h" #include "decode_local.h" #include "bsl_list.h" #include "bsl_errno.h" #include "bsl_err_internal.h" static CRYPT_DECODER_Node *CreateDecoderNode(const char *format, const char *type, const char *targetFormat, const char *targetType, const BSL_Param *input) { CRYPT_DECODER_Node *decoderNode = BSL_SAL_Calloc(1, sizeof(CRYPT_DECODER_Node)); if (decoderNode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } decoderNode->inData.format = format; decoderNode->inData.type = type; decoderNode->inData.data = (BSL_Param *)(uintptr_t)input; decoderNode->outData.format = targetFormat; decoderNode->outData.type = targetType; return decoderNode; } static void FreeDecoderNode(CRYPT_DECODER_Node *decoderNode) { if (decoderNode == NULL) { return; } CRYPT_DECODE_FreeOutData(decoderNode->decoderCtx, decoderNode->outData.data); BSL_SAL_Free(decoderNode); } CRYPT_DECODER_PoolCtx *CRYPT_DECODE_PoolNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType, const char *format, const char *type) { CRYPT_DECODER_PoolCtx *poolCtx = BSL_SAL_Calloc(1, sizeof(CRYPT_DECODER_PoolCtx)); if (poolCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } poolCtx->libCtx = libCtx; poolCtx->attrName = attrName; poolCtx->decoders = BSL_LIST_New(sizeof(CRYPT_DECODER_Ctx)); if (poolCtx->decoders == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(poolCtx); return NULL; } poolCtx->decoderPath = BSL_LIST_New(sizeof(CRYPT_DECODER_Node)); if (poolCtx->decoderPath == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } poolCtx->inputFormat = format; poolCtx->inputType = type; poolCtx->inputKeyType = keyType; poolCtx->targetFormat = NULL; poolCtx->targetType = NULL; return poolCtx; ERR: BSL_LIST_FREE(poolCtx->decoders, NULL); BSL_SAL_Free(poolCtx); return NULL; } void CRYPT_DECODE_PoolFreeCtx(CRYPT_DECODER_PoolCtx *poolCtx) { if (poolCtx == NULL) { return; } /* Free decoder path list and all decoder nodes */ if (poolCtx->decoderPath != NULL) { BSL_LIST_FREE(poolCtx->decoderPath, (BSL_LIST_PFUNC_FREE)FreeDecoderNode); } /* Free decoder list and all decoder contexts */ if (poolCtx->decoders != NULL) { BSL_LIST_FREE(poolCtx->decoders, (BSL_LIST_PFUNC_FREE)CRYPT_DECODE_Free); } BSL_SAL_Free(poolCtx); } static int32_t SetDecodeType(void *val, int32_t valLen, const char **targetValue) { if (valLen == 0 || valLen > MAX_CRYPT_DECODE_FORMAT_TYPE_SIZE) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *targetValue = val; return CRYPT_SUCCESS; } static int32_t SetFlagFreeOutData(CRYPT_DECODER_PoolCtx *poolCtx, void *val, int32_t valLen) { if (valLen != sizeof(bool)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (poolCtx->decoderPath == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_DECODER_Node *prevNode = BSL_LIST_GET_PREV(poolCtx->decoderPath); if (prevNode == NULL) { return CRYPT_SUCCESS; } bool isFreeOutData = *(bool *)val; if (!isFreeOutData) { prevNode->outData.data = NULL; } return CRYPT_SUCCESS; } int32_t CRYPT_DECODE_PoolCtrl(CRYPT_DECODER_PoolCtx *poolCtx, int32_t cmd, void *val, int32_t valLen) { if (poolCtx == NULL || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_DECODE_POOL_CMD_SET_TARGET_TYPE: return SetDecodeType(val, valLen, &poolCtx->targetType); case CRYPT_DECODE_POOL_CMD_SET_TARGET_FORMAT: return SetDecodeType(val, valLen, &poolCtx->targetFormat); case CRYPT_DECODE_POOL_CMD_SET_FLAG_FREE_OUT_DATA: return SetFlagFreeOutData(poolCtx, val, valLen); default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } } static int32_t CollectDecoder(CRYPT_DECODER_Ctx *decoderCtx, void *args) { int32_t ret; CRYPT_DECODER_PoolCtx *poolCtx = (CRYPT_DECODER_PoolCtx *)args; if (poolCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // TODO: Filter the decoder by input format and type According to poolCtx BSL_Param param[3] = { {CRYPT_PARAM_DECODE_LIB_CTX, BSL_PARAM_TYPE_CTX_PTR, poolCtx->libCtx, 0, 0}, {CRYPT_PARAM_DECODE_TARGET_ATTR_NAME, BSL_PARAM_TYPE_OCTETS_PTR, (void *)(uintptr_t)poolCtx->attrName, 0, 0}, BSL_PARAM_END }; ret = CRYPT_DECODE_SetParam(decoderCtx, param); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BSL_LIST_AddElement(poolCtx->decoders, decoderCtx, BSL_LIST_POS_END); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } static CRYPT_DECODER_Ctx* GetUsableDecoderFromPool(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODER_Node *currNode) { CRYPT_DECODER_Ctx *decoderCtx = NULL; const char *curFormat = currNode->inData.format; const char *curType = currNode->inData.type; CRYPT_DECODER_Ctx *node = BSL_LIST_GET_FIRST(poolCtx->decoders); while (node != NULL) { decoderCtx = node; if (decoderCtx == NULL || decoderCtx->decoderState != CRYPT_DECODER_STATE_UNTRIED) { node = BSL_LIST_GET_NEXT(poolCtx->decoders); continue; } /* Check if decoder matches the current node's input format and type */ if (curFormat != NULL && curType != NULL) { if ((decoderCtx->inFormat != NULL && BSL_SAL_StrcaseCmp(decoderCtx->inFormat, curFormat) == 0) && (decoderCtx->inType == NULL || BSL_SAL_StrcaseCmp(decoderCtx->inType, curType) == 0)) { break; } } else if (curFormat == NULL && curType != NULL) { if (decoderCtx->inType == NULL || BSL_SAL_StrcaseCmp(decoderCtx->inType, curType) == 0) { break; } } else if (curFormat != NULL && curType == NULL) { if (decoderCtx->inFormat != NULL && BSL_SAL_StrcaseCmp(decoderCtx->inFormat, curFormat) == 0) { break; } } else { break; } node = BSL_LIST_GET_NEXT(poolCtx->decoders); } if (node != NULL) { decoderCtx = node; decoderCtx->decoderState = CRYPT_DECODER_STATE_TRING; } return node != NULL ? decoderCtx : NULL; } static int32_t UpdateDecoderPath(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODER_Node *currNode) { /* Create new node */ CRYPT_DECODER_Node *newNode = CreateDecoderNode(currNode->outData.format, currNode->outData.type, poolCtx->targetFormat, poolCtx->targetType, currNode->outData.data); if (newNode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = BSL_LIST_AddElement(poolCtx->decoderPath, newNode, BSL_LIST_POS_END); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(newNode); BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } static int32_t TryDecodeWithDecoder(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODER_Node *currNode) { /* Convert password buffer to parameter if provided */ BSL_Param *decoderParam = NULL; int32_t ret = CRYPT_DECODE_Decode(currNode->decoderCtx, currNode->inData.data, &decoderParam); if (ret == CRYPT_SUCCESS) { /* Get output format and type from decoder */ BSL_Param outParam[3] = { {CRYPT_PARAM_DECODE_OUTPUT_FORMAT, BSL_PARAM_TYPE_OCTETS_PTR, NULL, 0, 0}, {CRYPT_PARAM_DECODE_OUTPUT_TYPE, BSL_PARAM_TYPE_OCTETS_PTR, NULL, 0, 0}, BSL_PARAM_END }; ret = CRYPT_DECODE_GetParam(currNode->decoderCtx, outParam); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } currNode->outData.data = decoderParam; currNode->outData.format = outParam[0].value; currNode->outData.type = outParam[1].value; currNode->decoderCtx->decoderState = CRYPT_DECODER_STATE_SUCCESS; ret = UpdateDecoderPath(poolCtx, currNode); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } else { /* Mark the node as tried */ currNode->decoderCtx->decoderState = CRYPT_DECODER_STATE_TRIED; return CRYPT_DECODE_RETRY; } } static void ResetLastNode(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODER_Node *currNode) { (void)currNode; CRYPT_DECODER_Node *prevNode = BSL_LIST_GET_PREV(poolCtx->decoderPath); /* Reset the out data of previous node if found */ if (prevNode != NULL) { CRYPT_DECODE_FreeOutData(prevNode->decoderCtx, prevNode->outData.data); prevNode->outData.data = NULL; prevNode->decoderCtx = NULL; prevNode->outData.format = poolCtx->targetFormat; prevNode->outData.type = poolCtx->targetType; (void)BSL_LIST_GET_NEXT(poolCtx->decoderPath); } else { (void)BSL_LIST_GET_FIRST(poolCtx->decoderPath); } BSL_LIST_DeleteCurrent(poolCtx->decoderPath, (BSL_LIST_PFUNC_FREE)FreeDecoderNode); (void)BSL_LIST_GET_LAST(poolCtx->decoderPath); } static int32_t BackToLastLayerDecodeNode(CRYPT_DECODER_PoolCtx *poolCtx, CRYPT_DECODER_Node *currNode) { if (poolCtx == NULL || currNode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ResetLastNode(poolCtx, currNode); /* Reset all decoders marked as tried to untried state */ CRYPT_DECODER_Ctx *decoderCtx = BSL_LIST_GET_FIRST(poolCtx->decoders); while (decoderCtx != NULL) { if (decoderCtx->decoderState == CRYPT_DECODER_STATE_TRIED) { decoderCtx->decoderState = CRYPT_DECODER_STATE_UNTRIED; } decoderCtx = BSL_LIST_GET_NEXT(poolCtx->decoders); } return CRYPT_SUCCESS; } static bool IsStrMatch(const char *source, const char *target) { if (source == NULL && target == NULL) { return true; } if (source == NULL || target == NULL) { return false; } return BSL_SAL_StrcaseCmp(source, target) == 0; } static int32_t DecodeWithKeyChain(CRYPT_DECODER_PoolCtx *poolCtx, BSL_Param **outParam) { int32_t ret; CRYPT_DECODER_Ctx *decoderCtx = NULL; CRYPT_DECODER_Node *currNode = BSL_LIST_GET_FIRST(poolCtx->decoderPath); while (!BSL_LIST_EMPTY(poolCtx->decoderPath)) { if (IsStrMatch(currNode->inData.format, poolCtx->targetFormat) && IsStrMatch(currNode->inData.type, poolCtx->targetType)) { *outParam = currNode->inData.data; return CRYPT_SUCCESS; } /* Get the usable decoder from the pool */ decoderCtx = GetUsableDecoderFromPool(poolCtx, currNode); /* If the decoder is found, try to decode */ if (decoderCtx != NULL) { currNode->decoderCtx = decoderCtx; ret = TryDecodeWithDecoder(poolCtx, currNode); if (ret == CRYPT_DECODE_RETRY) { continue; } } else { ret = BackToLastLayerDecodeNode(poolCtx, currNode); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } CRYPT_DECODER_Node **curNodePtr = (CRYPT_DECODER_Node **)BSL_LIST_Curr(poolCtx->decoderPath); currNode = curNodePtr == NULL ? NULL : *curNodePtr; } BSL_ERR_PUSH_ERROR(CRYPT_DECODE_ERR_NO_USABLE_DECODER); return CRYPT_DECODE_ERR_NO_USABLE_DECODER; } typedef int32_t (*CRYPT_DECODE_ProviderProcessCb)(CRYPT_DECODER_Ctx *decoderCtx, void *args); typedef struct { CRYPT_DECODE_ProviderProcessCb cb; void *args; } CRYPT_DECODE_ProviderProcessArgs; static int32_t ProcessEachProviderDecoder(CRYPT_EAL_ProvMgrCtx *ctx, void *args) { CRYPT_DECODE_ProviderProcessArgs *processArgs = (CRYPT_DECODE_ProviderProcessArgs *)args; CRYPT_DECODER_Ctx *decoderCtx = NULL; CRYPT_EAL_AlgInfo *algInfos = NULL; int32_t ret; if (ctx == NULL || args == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = CRYPT_EAL_ProviderQuery(ctx, CRYPT_EAL_OPERAID_DECODER, &algInfos); if (ret == CRYPT_NOT_SUPPORT) { return CRYPT_SUCCESS; } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } for (int32_t i = 0; algInfos != NULL && algInfos[i].algId != 0; i++) { decoderCtx = CRYPT_DECODE_NewDecoderCtxByMethod(algInfos[i].implFunc, ctx, algInfos[i].attr); if (decoderCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = processArgs->cb(decoderCtx, processArgs->args); if (ret != CRYPT_SUCCESS) { CRYPT_DECODE_Free(decoderCtx); BSL_ERR_PUSH_ERROR(ret); return ret; } } return CRYPT_SUCCESS; } int32_t CRYPT_DECODE_ProviderProcessAll(CRYPT_EAL_LibCtx *ctx, CRYPT_DECODE_ProviderProcessCb cb, void *args) { if (cb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_DECODE_ProviderProcessArgs processArgs = { .cb = cb, .args = args }; int32_t ret = CRYPT_EAL_ProviderProcessAll(ctx, ProcessEachProviderDecoder, &processArgs); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_SUCCESS; } int32_t CRYPT_DECODE_PoolDecode(CRYPT_DECODER_PoolCtx *poolCtx, const BSL_Param *inParam, BSL_Param **outParam) { if (poolCtx == NULL || inParam == NULL || outParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (*outParam != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = CRYPT_DECODE_ProviderProcessAll(poolCtx->libCtx, CollectDecoder, poolCtx); if (ret != CRYPT_SUCCESS) { return ret; } if (BSL_LIST_COUNT(poolCtx->decoders) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_DECODE_ERR_NO_DECODER); return CRYPT_DECODE_ERR_NO_DECODER; } CRYPT_DECODER_Node *initialNode = CreateDecoderNode(poolCtx->inputFormat, poolCtx->inputType, poolCtx->targetFormat, poolCtx->targetType, inParam); if (initialNode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = BSL_LIST_AddElement(poolCtx->decoderPath, initialNode, BSL_LIST_POS_END); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(initialNode); BSL_ERR_PUSH_ERROR(ret); return ret; } return DecodeWithKeyChain(poolCtx, outParam); } #endif /* HITLS_CRYPTO_CODECS && HITLS_CRYPTO_PROVIDER */
2302_82127028/openHiTLS-examples
codecs/src/decode_chain.c
C
unknown
16,054
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef HITLS_BUILD_H #define HITLS_BUILD_H #ifdef HITLS_TLS #include "hitls_config_layer_tls.h" #endif #ifdef HITLS_PKI #include "hitls_config_layer_pki.h" #endif #ifdef HITLS_CRYPTO #include "hitls_config_layer_crypto.h" #endif #include "hitls_config_layer_bsl.h" #ifndef HITLS_NO_CONFIG_CHECK #include "hitls_config_check.h" #endif #if defined(HITLS_CRYPTO_PROVIDER) && defined(HITLS_CONFIG_FILE) #include HITLS_CONFIG_FILE #endif #endif /* HITLS_BUILD_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_build.h
C
unknown
1,012
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /* Check the dependency of the configuration features. The check rules are as follows: * Non-deterministic feature dependency needs to be checked. * For example, feature a depends on feature b or c: * if feature a is defined, at least one of feature b and c must be defined. */ #ifndef HITLS_CONFIG_CHECK_H #define HITLS_CONFIG_CHECK_H #ifdef HITLS_TLS #if defined(HITLS_TLS_FEATURE_PROVIDER) && !defined(HITLS_CRYPTO_PROVIDER) #error "[HiTLS] The tls-provider must work with crypto-provider" #endif #if (defined(HITLS_TLS_FEATURE_PHA) || defined(HITLS_TLS_FEATURE_KEY_UPDATE)) && !defined(HITLS_TLS_PROTO_TLS13) #error "[HiTLS] Integrity check must work with TLS13" #endif #if defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_AES_128_GCM_SHA256 must work with sha256, gcm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_AES_256_GCM_SHA384 must work with sha384, gcm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256 must work with sha256, chacha20poly1305, \ chacha20" #endif #endif #if defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_AES_128_CCM_SHA256 must work with sha256, ccm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_AES_128_CCM_8_SHA256 must work with sha256, ccm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA256 must work with sha256, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, ecdh, ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, ecdh, ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, ecdh, \ ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes, ecdh, \ ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, ecdh, \ ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, ecdh, \ ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, rsa, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_ECDH) || !defined(HITLS_CRYPTO_ECDSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, ecdh, ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA256 must work with sha256, cbc, aes, dsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA256 must work with sha256, cbc, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_256_CCM must work with ccm, aes" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CCM must work with ccm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CCM must work with ccm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA384 must work with sha384, cbc, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_PSK_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, chacha20poly1305, \ chacha20" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_ECDH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_DH) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, dh" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CHACHA20POLY1305) || !defined(HITLS_CRYPTO_CHACHA20) || \ !defined(HITLS_CRYPTO_RSA) #error \ "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 must work with sha256, \ chacha20poly1305, chacha20, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CCM_SHA256 must work with sha256, ccm, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA256 must work with sha256, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA256 must work with sha256, cbc, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_GCM_SHA256) #if !defined(HITLS_CRYPTO_SHA256) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_GCM_SHA256 must work with sha256, gcm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_GCM_SHA384) #if !defined(HITLS_CRYPTO_SHA384) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_GCM_SHA384 must work with sha384, gcm, aes, dh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_128_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_128_CBC_SHA must work with sha1, cbc, aes, dh, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_256_CBC_SHA) #if !defined(HITLS_CRYPTO_SHA1) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_AES) || \ !defined(HITLS_CRYPTO_DH) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_256_CBC_SHA must work with sha1, cbc, aes, dh, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_ECDH) || \ !defined(HITLS_CRYPTO_ECDSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CCM must work with ccm, aes, ecdh, ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_ECDH) || \ !defined(HITLS_CRYPTO_ECDSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CCM must work with ccm, aes, ecdh, ecdsa" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CCM must work with ccm, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) || \ !defined(HITLS_CRYPTO_DH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CCM must work with ccm, aes, rsa, dh" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM must work with ccm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM_8) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM_8 must work with ccm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM must work with ccm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM_8) #if !defined(HITLS_CRYPTO_CCM) || !defined(HITLS_CRYPTO_AES) || !defined(HITLS_CRYPTO_RSA) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM_8 must work with ccm, aes, rsa" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_SM4_CBC_SM3) #if !defined(HITLS_CRYPTO_SM3) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_SM4) || \ !defined(HITLS_CRYPTO_SM2) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_SM4_CBC_SM3 must work with sm3, cbc, sm4, sm2, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECC_SM4_CBC_SM3) #if !defined(HITLS_CRYPTO_SM3) || !defined(HITLS_CRYPTO_CBC) || !defined(HITLS_CRYPTO_SM4) || \ !defined(HITLS_CRYPTO_SM2) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECC_SM4_CBC_SM3 must work with sm3, cbc, sm4, sm2" #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_SM4_GCM_SM3) #if !defined(HITLS_CRYPTO_SM3) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_SM4) || \ !defined(HITLS_CRYPTO_SM2) || !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECDHE_SM4_GCM_SM3 must work with sm3, gcm, sm4, sm2, ecdh" #endif #endif #if defined(HITLS_TLS_SUITE_ECC_SM4_GCM_SM3) #if !defined(HITLS_CRYPTO_SM3) || !defined(HITLS_CRYPTO_GCM) || !defined(HITLS_CRYPTO_SM4) || \ !defined(HITLS_CRYPTO_SM2) #error "[HiTLS] cipher suite HITLS_TLS_SUITE_ECC_SM4_GCM_SM3 must work with sm3, gcm, sm4, sm2" #endif #endif #if defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) || defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) || defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) || \ defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) #if (!defined(HITLS_TLS_SUITE_AUTH_RSA) && !defined(HITLS_TLS_SUITE_AUTH_ECDSA) && \ !defined(HITLS_TLS_SUITE_AUTH_PSK)) #error "[HiTLS] tls13 ciphersuite must work with suite_auth_rsa or suite_auth_ecdsa or suite_auth_psk" #endif #endif #endif /* HITLS_TLS */ #ifdef HITLS_CRYPTO #if defined(HITLS_CRYPTO_HMAC) && !defined(HITLS_CRYPTO_MD) #error "[HiTLS] The hmac must work with hash" #endif #if defined(HITLS_CRYPTO_DRBG_HASH) && !defined(HITLS_CRYPTO_MD) #error "[HiTLS] The drbg_hash must work with hash" #endif #if defined(HITLS_CRYPTO_DRBG_CTR) && !defined(HITLS_CRYPTO_AES) && !defined(HITLS_CRYPTO_SM4) #error "[HiTLS] AES or SM4 must be enabled for DRBG-CTR" #endif #if defined(HITLS_CRYPTO_ENTROPY) && !defined(HITLS_CRYPTO_DRBG) #error "[HiTLS] The entropy must work with at leaset one drbg algorithm." #endif #if defined(HITLS_CRYPTO_DRBG_GM) && !defined(HITLS_CRYPTO_DRBG_CTR) && !defined(HITLS_CRYPTO_DRBG_HASH) #error "[HiTLS]DRBG-HASH or DRBG-CTR must be enabled for DRBG-GM" #endif #if defined(HITLS_CRYPTO_ENTROPY_HARDWARE) && !defined(HITLS_CRYPTO_EALINIT) #error "[HiTLS] ealinit must be enabled when the hardware entropy source is enabled." #endif #if defined(HITLS_CRYPTO_ENTROPY) && defined(HITLS_CRYPTO_DRBG_CTR) && !defined(HITLS_CRYPTO_DRBG_GM) #if !defined(HITLS_CRYPTO_CMAC_AES) #error "[HiTLS] Configure the conditioning function. Currently, CRYPT_MAC_CMAC_AES is supported. \ others may be supported in the future." #endif #endif #if defined(HITLS_CRYPTO_BN) && !(defined(HITLS_THIRTY_TWO_BITS) || defined(HITLS_SIXTY_FOUR_BITS)) #error "[HiTLS] To use bn, the number of system bits must be specified first." #endif #if defined(HITLS_CRYPTO_HPKE) #if !defined(HITLS_CRYPTO_AES) && !defined(HITLS_CRYPTO_CHACHA20POLY1305) #error "[HiTLS] The hpke must work with aes or chacha20poly1305." #endif #if !defined(HITLS_CRYPTO_CHACHA20POLY1305) && defined(HITLS_CRYPTO_AES) && !defined(HITLS_CRYPTO_GCM) #error "[HiTLS] The hpke must work with aes-gcm." #endif #if !defined(HITLS_CRYPTO_CURVE_NISTP256) && !defined(HITLS_CRYPTO_CURVE_NISTP384) && \ !defined(HITLS_CRYPTO_CURVE_NISTP521) && !defined(HITLS_CRYPTO_X25519) #error "[HiTLS] The hpke must work with p256 or p384 or p521 or x25519." #endif #endif /* HITLS_CRYPTO_HPKE */ #if defined(HITLS_CRYPTO_RSA_BLINDING) && !(defined(HITLS_CRYPTO_BN_RAND)) #error "[HiTLS] The blind must work with bn_rand" #endif #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) #if !defined(HITLS_CRYPTO_RSA_EMSA_PSS) && !defined(HITLS_CRYPTO_RSA_EMSA_PKCSV15) #error "[HiTLS] The rsa_sign/rsa_verify must work with rsa_emsa_pss/rsa_emsa_pkcsv15" #endif #endif #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) #if !defined(HITLS_CRYPTO_RSAES_OAEP) && !defined(HITLS_CRYPTO_RSAES_PKCSV15) && \ !defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) && !defined(HITLS_CRYPTO_RSA_NO_PAD) #error "[HiTLS] The rsa_encrypt/rsa_decrypt must work with rsaes_oaep/rsaes_pkcsv15/rsaes_pkcsv15_tls/rsa_no_pad" #endif #endif #if defined(HITLS_CRYPTO_RSA_NO_PAD) || defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_RSAES_PKCSV15) || \ defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) #if !defined(HITLS_CRYPTO_RSA_ENCRYPT) && !defined(HITLS_CRYPTO_RSA_DECRYPT) #error "[HiTLS] The rsaes_oaep/rsaes_pkcsv15/rsaes_pkcsv15_tls/rsa_no_pad must work with rsa_encrypt/rsa_decrypt" #endif #endif #if defined(HITLS_CRYPTO_RSA_EMSA_PSS) || defined(HITLS_CRYPTO_RSA_EMSA_PKCSV15) #if !defined(HITLS_CRYPTO_RSA_SIGN) && !defined(HITLS_CRYPTO_RSA_VERIFY) #error "[HiTLS] The rsa_emsa_pss/rsa_emsa_pkcsv15 must work with rsa_sign/rsa_verify" #endif #endif #if defined(HITLS_CRYPTO_RSA_BLINDING) && !defined(HITLS_CRYPTO_RSA_SIGN) && !defined(HITLS_CRYPTO_RSA_DECRYPT) #error "[HiTLS] The rsa_blinding must work with rsa_sign or rsa_decrypt" #endif #if defined(HITLS_CRYPTO_RSA_ENCRYPT) && (defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_RSAES_PKCSV15)) #ifndef HITLS_CRYPTO_DRBG #error "[HiTLS] The rsa_encrypt+rsaes_oaep/rsa_pkcsv15 must work with a drbg algorithm." #endif #endif #if defined(HITLS_CRYPTO_RSA_SIGN) && defined(HITLS_CRYPTO_RSA_EMSA_PSS) && !defined(HITLS_CRYPTO_DRBG) #error "[HiTLS] The rsa_sign+rsa_emsa_pss must work with a drbg algorithm." #endif #if defined(HITLS_CRYPTO_RSA_GEN) && !(defined(HITLS_CRYPTO_BN_RAND) && defined(HITLS_CRYPTO_BN_PRIME)) #error "[HiTLS] The rsa_gen must work with bn_rand and bn_prime" #endif #if defined(HITLS_CRYPTO_ECDSA) #if !defined(HITLS_CRYPTO_CURVE_NISTP224) && !defined(HITLS_CRYPTO_CURVE_NISTP256) && \ !defined(HITLS_CRYPTO_CURVE_NISTP384) && !defined(HITLS_CRYPTO_CURVE_NISTP521) && \ !defined(HITLS_CRYPTO_CURVE_BP256R1) && !defined(HITLS_CRYPTO_CURVE_BP384R1) && \ !defined(HITLS_CRYPTO_CURVE_BP512R1) && !defined(HITLS_CRYPTO_CURVE_192WAPI) #error "[HiTLS] Nist curves or brainpool curves or 192Wapi curve must be enabled for ECDSA." #endif #endif #if defined(HITLS_CRYPTO_ECDH) #if !defined(HITLS_CRYPTO_CURVE_NISTP224) && !defined(HITLS_CRYPTO_CURVE_NISTP256) && \ !defined(HITLS_CRYPTO_CURVE_NISTP384) && !defined(HITLS_CRYPTO_CURVE_NISTP521) && \ !defined(HITLS_CRYPTO_CURVE_BP256R1) && !defined(HITLS_CRYPTO_CURVE_BP384R1) && \ !defined(HITLS_CRYPTO_CURVE_BP512R1) && !defined(HITLS_CRYPTO_CURVE_192WAPI) #error "[HiTLS] Nist curves or brainpool curves must be enabled for ECDH." #endif #endif #if defined(HITLS_CRYPTO_CMVP_INTEGRITY) && !defined(HITLS_CRYPTO_CMVP) #error "[HiTLS] Integrity check must work with CMVP" #endif #if (defined(HITLS_CRYPTO_SHA1_ARMV8) || \ defined(HITLS_CRYPTO_SHA256_ARMV8) || defined(HITLS_CRYPTO_SHA224_ARMV8) || defined(HITLS_CRYPTO_SHA2_ARMV8) || \ defined(HITLS_CRYPTO_SM4_X8664)) && !defined(HITLS_CRYPTO_EALINIT) #error "[HiTLS] ealinit must be enabled for sha1_armv8 or sha256_armv8 or sha224_armv8 or sm4_x8664." #endif #if defined(HITLS_CRYPTO_HYBRIDKEM) #if !defined(HITLS_CRYPTO_X25519) && !defined(HITLS_CRYPTO_ECDH) #error "[HiTLS] The hybrid must work with x25519 or ecdh." #endif #endif #if defined(HITLS_CRYPTO_HMAC) && !defined(HITLS_CRYPTO_MD) #error "[HiTLS] The hmac must work with hash." #endif #if defined(HITLS_CRYPTO_DRBG_HASH) && !defined(HITLS_CRYPTO_MD) #error "[HiTLS] The drbg_hash must work with hash." #endif #if defined(HITLS_CRYPTO_ENTROPY) && !defined(HITLS_CRYPTO_DRBG) #error "[HiTLS] The entropy must work with at leaset one drbg algorithm." #endif #if defined(HITLS_CRYPTO_PKEY) && !defined(HITLS_CRYPTO_MD) #error "[HiTLS] The pkey must work with hash." #endif #if defined(HITLS_CRYPTO_BN) && !(defined(HITLS_THIRTY_TWO_BITS) || defined(HITLS_SIXTY_FOUR_BITS)) #error "[HiTLS] To use bn, the number of system bits must be specified first." #endif #ifdef HITLS_CRYPTO_KEY_EPKI #if !defined(HITLS_CRYPTO_KEY_ENCODE) && !defined(HITLS_CRYPTO_KEY_DECODE) #error "[HiTLS] The key encrypt must work with key gen or key parse." #endif #if !defined(HITLS_CRYPTO_DRBG) #error "[HiTLS] The key encrypt must work with a drbg algorithm." #endif #if !defined(HITLS_CRYPTO_CIPHER) #error "[HiTLS] The key encrypt must work with a symmetric algorithm." #endif #endif #if defined(HITLS_CRYPTO_CODECSKEY) && (!defined(HITLS_CRYPTO_ECDSA) && !defined(HITLS_CRYPTO_SM2_SIGN) && \ !defined(HITLS_CRYPTO_SM2_CRYPT) && !defined(HITLS_CRYPTO_ED25519) && !defined(HITLS_CRYPTO_RSA_SIGN)) && \ !defined(HITLS_CRYPTO_RSA_VERIFY) #error "[HiTLS] The encode must work with ecdsa or sm2_sign or sm2_crypt or ed25519 or rsa_sign or rsa_verify." #endif #endif /* HITLS_CRYPTO */ #ifdef HITLS_PKI #if defined(HITLS_PKI_INFO) && !defined(HITLS_PKI_X509_CRT) #error "[HiTLS] The info must work with x509_crt_gen or x509_crt_parse." #endif #endif /* HITLS_PKI */ #if defined(HITLS_TLS_FEATURE_ETM) && !defined(HITLS_TLS_SUITE_CIPHER_CBC) #error "[HiTLS] The etm must work with cbc" #endif #endif /* HITLS_CONFIG_CHECK_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_config_check.h
C
unknown
38,606
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /* Derivation of configuration features. * The derivation type (rule) and sequence are as follows: * 1. Parent features derive child features. * 2. Derive the features of dependencies. * For example, if feature a depends on features b and c, you need to derive features b and c. * 3. Child features derive parent features. * The high-level interfaces of the crypto module is controlled by the parent feature macro, * if there is no parent feature, such interfaces will be unavailable. */ #ifndef HITLS_CONFIG_LAYER_BSL_H #define HITLS_CONFIG_LAYER_BSL_H /* BSL_INIT */ #if defined(HITLS_CRYPTO_EAL) && !defined(HITLS_BSL_INIT) #define HITLS_BSL_INIT #endif #if defined(HITLS_BSL_INIT) && !defined(HITLS_BSL_ERR) #define HITLS_BSL_ERR #endif #if defined(HITLS_BSL_UI) && !defined(HITLS_BSL_SAL_FILE) #define HITLS_BSL_SAL_FILE #endif #ifdef HITLS_BSL_CONF #ifndef HITLS_BSL_LIST #define HITLS_BSL_LIST #endif #ifndef HITLS_BSL_UIO_FILE #define HITLS_BSL_UIO_FILE #endif #endif /* BSL_UIO */ /* Derive the child-features of uio. */ #ifdef HITLS_BSL_UIO #ifndef HITLS_BSL_UIO_PLT #define HITLS_BSL_UIO_PLT #endif #ifndef HITLS_BSL_UIO_BUFFER #define HITLS_BSL_UIO_BUFFER #endif #ifndef HITLS_BSL_UIO_SCTP #define HITLS_BSL_UIO_SCTP #endif #ifndef HITLS_BSL_UIO_UDP #define HITLS_BSL_UIO_UDP #endif #ifndef HITLS_BSL_UIO_TCP #define HITLS_BSL_UIO_TCP #endif #ifndef HITLS_BSL_UIO_MEM #define HITLS_BSL_UIO_MEM #endif #ifndef HITLS_BSL_UIO_FILE #define HITLS_BSL_UIO_FILE #endif #endif #if defined(HITLS_BSL_UIO_FILE) && !defined(HITLS_BSL_SAL_FILE) #define HITLS_BSL_SAL_FILE #endif /* Derive the child-features of uio mem. */ #if defined(HITLS_BSL_UIO_MEM) #ifndef HITLS_BSL_SAL_MEM #define HITLS_BSL_SAL_MEM #endif #ifndef HITLS_BSL_BUFFER #define HITLS_BSL_BUFFER #endif #endif /* Derive the dependency features of uio_tcp and uio_sctp. */ #if defined(HITLS_BSL_UIO_TCP) || defined(HITLS_BSL_UIO_SCTP) #ifndef HITLS_BSL_SAL_NET #define HITLS_BSL_SAL_NET #endif #endif /* Derive parent feature from child features. */ #if defined(HITLS_BSL_UIO_BUFFER) || defined(HITLS_BSL_UIO_SCTP) || defined(HITLS_BSL_UIO_TCP) || \ defined(HITLS_BSL_UIO_MEM) || defined(HITLS_BSL_UIO_FILE) #ifndef HITLS_BSL_UIO_PLT #define HITLS_BSL_UIO_PLT #endif #endif #ifdef HITLS_BSL_PEM #ifndef HITLS_BSL_BASE64 #define HITLS_BSL_BASE64 #endif #endif #ifdef HITLS_BSL_ASN1 #ifndef HITLS_BSL_SAL_TIME #define HITLS_BSL_SAL_TIME #endif #endif #endif /* HITLS_CONFIG_LAYER_BSL_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_config_layer_bsl.h
C
unknown
3,285
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /* Derivation of configuration features. * The derivation type (rule) and sequence are as follows: * 1. Parent features derive child features. * 2. Derive the features of dependencies. * For example, if feature a depends on features b and c, you need to derive features b and c. * 3. Child features derive parent features. * The high-level interfaces of the crypto module is controlled by the parent feature macro, * if there is no parent feature, such interfaces will be unavailable. */ #ifndef HITLS_CONFIG_LAYER_CRYPTO_H #define HITLS_CONFIG_LAYER_CRYPTO_H #ifdef HITLS_CRYPTO_CODECS #ifndef HITLS_CRYPTO_PROVIDER #define HITLS_CRYPTO_PROVIDER #endif #endif #if defined(HITLS_CRYPTO_CODECSKEY) && defined(HITLS_CRYPTO_PROVIDER) #ifndef HITLS_CRYPTO_CODECS #define HITLS_CRYPTO_CODECS #endif #endif #ifdef HITLS_CRYPTO_CODECSKEY #ifndef HITLS_CRYPTO_KEY_ENCODE #define HITLS_CRYPTO_KEY_ENCODE #endif #ifndef HITLS_CRYPTO_KEY_DECODE #define HITLS_CRYPTO_KEY_DECODE #endif #ifndef HITLS_CRYPTO_KEY_EPKI #define HITLS_CRYPTO_KEY_EPKI #endif #ifndef HITLS_CRYPTO_KEY_INFO #define HITLS_CRYPTO_KEY_INFO #endif #endif #ifdef HITLS_CRYPTO_KEY_EPKI #ifndef HITLS_CRYPTO_PBKDF2 #define HITLS_CRYPTO_PBKDF2 #endif #endif #ifdef HITLS_CRYPTO_KEY_INFO #ifndef HITLS_BSL_PRINT #define HITLS_BSL_PRINT #endif #endif #if defined(HITLS_CRYPTO_KEY_ENCODE) || defined(HITLS_CRYPTO_KEY_DECODE) || defined(HITLS_CRYPTO_KEY_EPKI) || \ defined(HITLS_CRYPTO_KEY_INFO) #ifndef HITLS_CRYPTO_CODECSKEY #define HITLS_CRYPTO_CODECSKEY #endif #ifndef HITLS_BSL_ASN1 #define HITLS_BSL_ASN1 #endif #ifndef HITLS_BSL_OBJ #define HITLS_BSL_OBJ #endif #endif #ifdef HITLS_CRYPTO_PROVIDER #ifndef HITLS_BSL_PARAMS #define HITLS_BSL_PARAMS #endif #endif /* kdf */ #ifdef HITLS_CRYPTO_KDF #ifndef HITLS_CRYPTO_PBKDF2 #define HITLS_CRYPTO_PBKDF2 #endif #ifndef HITLS_CRYPTO_HKDF #define HITLS_CRYPTO_HKDF #endif #ifndef HITLS_CRYPTO_KDFTLS12 #define HITLS_CRYPTO_KDFTLS12 #endif #ifndef HITLS_CRYPTO_SCRYPT #define HITLS_CRYPTO_SCRYPT #endif #endif #ifdef HITLS_CRYPTO_HPKE #ifndef HITLS_CRYPTO_HKDF #define HITLS_CRYPTO_HKDF #endif #ifndef HITLS_BSL_PARAMS #define HITLS_BSL_PARAMS #endif #endif #ifdef HITLS_CRYPTO_SCRYPT #ifndef HITLS_CRYPTO_SHA256 #define HITLS_CRYPTO_SHA256 #endif #ifndef HITLS_CRYPTO_PBKDF2 #define HITLS_CRYPTO_PBKDF2 #endif #endif #if defined(HITLS_CRYPTO_PBKDF2) || defined(HITLS_CRYPTO_HKDF) || defined(HITLS_CRYPTO_KDFTLS12) || \ defined(HITLS_CRYPTO_SCRYPT) #ifndef HITLS_CRYPTO_KDF #define HITLS_CRYPTO_KDF #endif #ifndef HITLS_CRYPTO_HMAC #define HITLS_CRYPTO_HMAC #endif #ifndef HITLS_BSL_PARAMS #define HITLS_BSL_PARAMS #endif #endif /* DRBG */ #if defined(HITLS_CRYPTO_ENTROPY) && !defined(HITLS_BSL_LIST) #define HITLS_BSL_LIST #endif #if defined(HITLS_CRYPTO_ENTROPY) && !defined(HITLS_CRYPTO_ENTROPY_GETENTROPY) && \ !defined(HITLS_CRYPTO_ENTROPY_DEVRANDOM) && !defined(HITLS_CRYPTO_ENTROPY_SYS) && \ !defined(HITLS_CRYPTO_ENTROPY_HARDWARE) #define HITLS_CRYPTO_ENTROPY_DEVRANDOM #endif #ifdef HITLS_CRYPTO_DRBG #ifndef HITLS_CRYPTO_DRBG_HASH #define HITLS_CRYPTO_DRBG_HASH #endif #ifndef HITLS_CRYPTO_DRBG_HMAC #define HITLS_CRYPTO_DRBG_HMAC #endif #ifndef HITLS_CRYPTO_DRBG_CTR #define HITLS_CRYPTO_DRBG_CTR #endif #endif #if defined(HITLS_CRYPTO_DRBG_HMAC) && !defined(HITLS_CRYPTO_HMAC) #define HITLS_CRYPTO_HMAC #endif #if defined(HITLS_CRYPTO_DRBG_HASH) || defined(HITLS_CRYPTO_DRBG_HMAC) || defined(HITLS_CRYPTO_DRBG_CTR) #ifndef HITLS_CRYPTO_DRBG #define HITLS_CRYPTO_DRBG #endif #ifndef HITLS_BSL_PARAMS #define HITLS_BSL_PARAMS #endif #endif #if defined(HITLS_CRYPTO_DRBG_GM) #ifndef HITLS_BSL_SAL_TIME #define HITLS_BSL_SAL_TIME #endif #endif /* MAC */ #ifdef HITLS_CRYPTO_MAC #ifndef HITLS_CRYPTO_HMAC #define HITLS_CRYPTO_HMAC #endif #ifndef HITLS_CRYPTO_CMAC #define HITLS_CRYPTO_CMAC #endif #ifndef HITLS_CRYPTO_GMAC #define HITLS_CRYPTO_GMAC #endif #ifndef HITLS_CRYPTO_SIPHASH #define HITLS_CRYPTO_SIPHASH #endif #ifndef HITLS_CRYPTO_CBC_MAC #define HITLS_CRYPTO_CBC_MAC #endif #endif #if defined(HITLS_CRYPTO_CBC_MAC) && !defined(HITLS_CRYPTO_SM4) #define HITLS_CRYPTO_SM4 #endif #ifdef HITLS_CRYPTO_GMAC #ifndef HITLS_CRYPTO_EAL #define HITLS_CRYPTO_EAL #endif #ifndef HITLS_CRYPTO_AES #define HITLS_CRYPTO_AES #endif #ifndef HITLS_CRYPTO_GCM #define HITLS_CRYPTO_GCM #endif #endif #ifdef HITLS_CRYPTO_CMAC #ifndef HITLS_CRYPTO_CMAC_AES #define HITLS_CRYPTO_CMAC_AES #endif #ifndef HITLS_CRYPTO_CMAC_SM4 #define HITLS_CRYPTO_CMAC_SM4 #endif #endif #if defined(HITLS_CRYPTO_CMAC_AES) && !defined(HITLS_CRYPTO_AES) #define HITLS_CRYPTO_AES #endif #if defined(HITLS_CRYPTO_CMAC_SM4) && !defined(HITLS_CRYPTO_SM4) #define HITLS_CRYPTO_SM4 #endif #if defined(HITLS_CRYPTO_CMAC_AES) || defined(HITLS_CRYPTO_CMAC_SM4) #ifndef HITLS_CRYPTO_CMAC #define HITLS_CRYPTO_CMAC #endif #endif #if defined(HITLS_CRYPTO_HMAC) || defined(HITLS_CRYPTO_CMAC) || defined(HITLS_CRYPTO_GMAC) || \ defined(HITLS_CRYPTO_SIPHASH) || defined(HITLS_CRYPTO_CBC_MAC) #ifndef HITLS_CRYPTO_MAC #define HITLS_CRYPTO_MAC #endif #endif /* CIPHER */ #ifdef HITLS_CRYPTO_CIPHER #ifndef HITLS_CRYPTO_AES #define HITLS_CRYPTO_AES #endif #ifndef HITLS_CRYPTO_SM4 #define HITLS_CRYPTO_SM4 #endif #ifndef HITLS_CRYPTO_CHACHA20 #define HITLS_CRYPTO_CHACHA20 #endif #endif #if defined(HITLS_CRYPTO_CHACHA20) && !defined(HITLS_CRYPTO_CHACHA20POLY1305) #define HITLS_CRYPTO_CHACHA20POLY1305 #endif #if defined(HITLS_CRYPTO_AES) || defined(HITLS_CRYPTO_SM4) || defined(HITLS_CRYPTO_CHACHA20) #ifndef HITLS_CRYPTO_CIPHER #define HITLS_CRYPTO_CIPHER #endif #endif /* MODES */ #ifdef HITLS_CRYPTO_MODES #ifndef HITLS_CRYPTO_CTR #define HITLS_CRYPTO_CTR #endif #ifndef HITLS_CRYPTO_CBC #define HITLS_CRYPTO_CBC #endif #ifndef HITLS_CRYPTO_ECB #define HITLS_CRYPTO_ECB #endif #ifndef HITLS_CRYPTO_GCM #define HITLS_CRYPTO_GCM #endif #ifndef HITLS_CRYPTO_CCM #define HITLS_CRYPTO_CCM #endif #ifndef HITLS_CRYPTO_XTS #define HITLS_CRYPTO_XTS #endif #ifndef HITLS_CRYPTO_CFB #define HITLS_CRYPTO_CFB #endif #ifndef HITLS_CRYPTO_OFB #define HITLS_CRYPTO_OFB #endif #ifndef HITLS_CRYPTO_CHACHA20POLY1305 #define HITLS_CRYPTO_CHACHA20POLY1305 #endif #endif #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_CBC) || defined(HITLS_CRYPTO_ECB) || \ defined(HITLS_CRYPTO_GCM) || defined(HITLS_CRYPTO_CCM) || defined(HITLS_CRYPTO_XTS) || \ defined(HITLS_CRYPTO_CFB) || defined(HITLS_CRYPTO_OFB) || defined(HITLS_CRYPTO_CHACHA20POLY1305) #ifndef HITLS_CRYPTO_MODES #define HITLS_CRYPTO_MODES #endif #endif /* PKEY */ #ifdef HITLS_CRYPTO_PKEY #ifndef HITLS_CRYPTO_ECC #define HITLS_CRYPTO_ECC #endif #ifndef HITLS_CRYPTO_DSA #define HITLS_CRYPTO_DSA #endif #ifndef HITLS_CRYPTO_RSA #define HITLS_CRYPTO_RSA #endif #ifndef HITLS_CRYPTO_DH #define HITLS_CRYPTO_DH #endif #ifndef HITLS_CRYPTO_ECDSA #define HITLS_CRYPTO_ECDSA #endif #ifndef HITLS_CRYPTO_ECDH #define HITLS_CRYPTO_ECDH #endif #ifndef HITLS_CRYPTO_SM2 #define HITLS_CRYPTO_SM2 #endif #ifndef HITLS_CRYPTO_CURVE25519 #define HITLS_CRYPTO_CURVE25519 #endif #ifndef HITLS_CRYPTO_MLKEM #define HITLS_CRYPTO_MLKEM #endif #ifndef HITLS_CRYPTO_MLDSA #define HITLS_CRYPTO_MLDSA #endif #ifndef HITLS_CRYPTO_HYBRIDKEM #define HITLS_CRYPTO_HYBRIDKEM #endif #ifndef HITLS_CRYPTO_PAILLIER #define HITLS_CRYPTO_PAILLIER #endif #ifndef HITLS_CRYPTO_ELGAMAL #define HITLS_CRYPTO_ELGAMAL #endif #ifndef HITLS_CRYPTO_SLH_DSA #define HITLS_CRYPTO_SLH_DSA #endif #ifndef HITLS_CRYPTO_XMSS #define HITLS_CRYPTO_XMSS #endif #endif #ifdef HITLS_CRYPTO_RSA #ifndef HITLS_CRYPTO_RSA_SIGN #define HITLS_CRYPTO_RSA_SIGN #endif #ifndef HITLS_CRYPTO_RSA_VERIFY #define HITLS_CRYPTO_RSA_VERIFY #endif #ifndef HITLS_CRYPTO_RSA_ENCRYPT #define HITLS_CRYPTO_RSA_ENCRYPT #endif #ifndef HITLS_CRYPTO_RSA_DECRYPT #define HITLS_CRYPTO_RSA_DECRYPT #endif #ifndef HITLS_CRYPTO_RSA_BLINDING #define HITLS_CRYPTO_RSA_BLINDING #endif #ifndef HITLS_CRYPTO_RSA_GEN #define HITLS_CRYPTO_RSA_GEN #endif #ifndef HITLS_CRYPTO_RSA_PAD #define HITLS_CRYPTO_RSA_PAD #endif #ifndef HITLS_CRYPTO_RSA_BSSA #define HITLS_CRYPTO_RSA_BSSA #endif #ifndef HITLS_CRYPTO_RSA_CHECK #define HITLS_CRYPTO_RSA_CHECK #endif #endif #ifdef HITLS_CRYPTO_RSA_BSSA #ifndef HITLS_CRYPTO_RSA_EMSA_PSS #define HITLS_CRYPTO_RSA_EMSA_PSS #endif #ifndef HITLS_CRYPTO_RSA_BLINDING #define HITLS_CRYPTO_RSA_BLINDING #endif #endif #ifdef HITLS_CRYPTO_RSA_GEN #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #ifndef HITLS_CRYPTO_BN_PRIME #define HITLS_CRYPTO_BN_PRIME #endif #endif #ifdef HITLS_CRYPTO_RSA_BLINDING #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #endif #ifdef HITLS_CRYPTO_RSA_CHECK #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #endif #ifdef HITLS_CRYPTO_RSA_PAD #ifndef HITLS_CRYPTO_RSA_EMSA_PSS #define HITLS_CRYPTO_RSA_EMSA_PSS #endif #ifndef HITLS_CRYPTO_RSA_EMSA_PKCSV15 #define HITLS_CRYPTO_RSA_EMSA_PKCSV15 #endif #ifndef HITLS_CRYPTO_RSAES_OAEP #define HITLS_CRYPTO_RSAES_OAEP #endif #ifndef HITLS_CRYPTO_RSAES_PKCSV15 #define HITLS_CRYPTO_RSAES_PKCSV15 #endif #ifndef HITLS_CRYPTO_RSAES_PKCSV15_TLS #define HITLS_CRYPTO_RSAES_PKCSV15_TLS #endif #ifndef HITLS_CRYPTO_RSA_NO_PAD #define HITLS_CRYPTO_RSA_NO_PAD #endif #endif #if defined(HITLS_CRYPTO_RSA_EMSA_PSS) || defined(HITLS_CRYPTO_RSA_EMSA_PKCSV15) || \ defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_RSAES_PKCSV15) || \ defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSA_NO_PAD) #ifndef HITLS_CRYPTO_RSA_PAD #define HITLS_CRYPTO_RSA_PAD #endif #endif #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) || \ defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) || \ defined(HITLS_CRYPTO_RSA_BLINDING) || defined(HITLS_CRYPTO_RSA_PAD) || defined(HITLS_CRYPTO_RSA_GEN) #ifndef HITLS_CRYPTO_RSA #define HITLS_CRYPTO_RSA #endif // rsa common dependency #ifndef HITLS_CRYPTO_BN_BASIC #define HITLS_CRYPTO_BN_BASIC #endif #endif #ifdef HITLS_CRYPTO_CURVE25519 #ifndef HITLS_CRYPTO_X25519 #define HITLS_CRYPTO_X25519 #endif #ifndef HITLS_CRYPTO_ED25519 #define HITLS_CRYPTO_ED25519 #endif #ifndef HITLS_CRYPTO_X25519_CHECK #define HITLS_CRYPTO_X25519_CHECK #endif #ifndef HITLS_CRYPTO_ED25519_CHECK #define HITLS_CRYPTO_ED25519_CHECK #endif #endif #if defined(HITLS_CRYPTO_ED25519) && !defined(HITLS_CRYPTO_SHA512) #define HITLS_CRYPTO_SHA512 #endif #if defined(HITLS_CRYPTO_X25519) || defined(HITLS_CRYPTO_ED25519) #ifndef HITLS_CRYPTO_CURVE25519 #define HITLS_CRYPTO_CURVE25519 #endif #endif #ifdef HITLS_CRYPTO_SM2 #ifndef HITLS_CRYPTO_SM2_SIGN #define HITLS_CRYPTO_SM2_SIGN #endif #ifndef HITLS_CRYPTO_SM2_CRYPT #define HITLS_CRYPTO_SM2_CRYPT #endif #ifndef HITLS_CRYPTO_SM2_EXCH #define HITLS_CRYPTO_SM2_EXCH #endif #endif #if defined(HITLS_CRYPTO_SM2_SIGN) || defined(HITLS_CRYPTO_SM2_CRYPT) || defined(HITLS_CRYPTO_SM2_EXCH) #ifndef HITLS_CRYPTO_SM2 #define HITLS_CRYPTO_SM2 #endif #endif #ifdef HITLS_CRYPTO_SM2 #ifndef HITLS_CRYPTO_SM3 #define HITLS_CRYPTO_SM3 #endif #ifndef HITLS_CRYPTO_CURVE_SM2 #define HITLS_CRYPTO_CURVE_SM2 #endif #ifndef HITLS_CRYPTO_SM2_CHECK #define HITLS_CRYPTO_SM2_CHECK #endif #endif #ifdef HITLS_CRYPTO_XMSS #ifndef HITLS_CRYPTO_SLH_DSA #define HITLS_CRYPTO_SLH_DSA #endif #endif #ifdef HITLS_CRYPTO_SLH_DSA #ifndef HITLS_CRYPTO_SHA2 #define HITLS_CRYPTO_SHA2 #endif #ifndef HITLS_CRYPTO_SHA3 #define HITLS_CRYPTO_SHA3 #endif #ifndef HITLS_BSL_OBJ #define HITLS_BSL_OBJ #endif #ifndef HITLS_CRYPTO_EAL #define HITLS_CRYPTO_EAL #endif #ifndef HITLS_CRYPTO_HMAC #define HITLS_CRYPTO_HMAC #endif #ifndef HITLS_CRYPTO_SHA256 #define HITLS_CRYPTO_SHA256 #endif #ifndef HITLS_CRYPTO_SHA512 #define HITLS_CRYPTO_SHA512 #endif #ifndef HITLS_CRYPTO_SLH_DSA_CHECK #define HITLS_CRYPTO_SLH_DSA_CHECK #endif #endif #if defined(HITLS_CRYPTO_MLDSA) || defined(HITLS_CRYPTO_ELGAMAL) #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #ifndef HITLS_CRYPTO_BN_PRIME #define HITLS_CRYPTO_BN_PRIME #endif #endif #ifdef HITLS_CRYPTO_HYBRIDKEM #ifndef HITLS_CRYPTO_MLKEM #define HITLS_CRYPTO_MLKEM #endif #endif #ifdef HITLS_CRYPTO_MLKEM #ifndef HITLS_CRYPTO_SHA3 #define HITLS_CRYPTO_SHA3 #endif #ifndef HITLS_CRYPTO_KEM #define HITLS_CRYPTO_KEM #endif #ifndef HITLS_CRYPTO_MLKEM_CHECK #define HITLS_CRYPTO_MLKEM_CHECK #endif #endif #ifdef HITLS_CRYPTO_MLDSA #ifndef HITLS_CRYPTO_SHA3 #define HITLS_CRYPTO_SHA3 #endif #ifndef HITLS_BSL_OBJ #define HITLS_BSL_OBJ #endif #ifndef HITLS_CRYPTO_MLDSA_CHECK #define HITLS_CRYPTO_MLDSA_CHECK #endif #endif #ifdef HITLS_CRYPTO_ECC #ifndef HITLS_CRYPTO_CURVE_NISTP224 #define HITLS_CRYPTO_CURVE_NISTP224 #endif #ifndef HITLS_CRYPTO_CURVE_NISTP256 #define HITLS_CRYPTO_CURVE_NISTP256 #endif #ifndef HITLS_CRYPTO_CURVE_NISTP384 #define HITLS_CRYPTO_CURVE_NISTP384 #endif #ifndef HITLS_CRYPTO_CURVE_NISTP521 #define HITLS_CRYPTO_CURVE_NISTP521 #endif #ifndef HITLS_CRYPTO_CURVE_BP256R1 #define HITLS_CRYPTO_CURVE_BP256R1 #endif #ifndef HITLS_CRYPTO_CURVE_BP384R1 #define HITLS_CRYPTO_CURVE_BP384R1 #endif #ifndef HITLS_CRYPTO_CURVE_BP512R1 #define HITLS_CRYPTO_CURVE_BP512R1 #endif #ifndef HITLS_CRYPTO_CURVE_SM2 #define HITLS_CRYPTO_CURVE_SM2 #endif #ifndef HITLS_CRYPTO_ECC_CHECK #define HITLS_CRYPTO_ECC_CHECK #endif #endif #if defined(HITLS_CRYPTO_CURVE_NISTP224) || defined(HITLS_CRYPTO_CURVE_NISTP256) || \ defined(HITLS_CRYPTO_CURVE_NISTP384) || defined(HITLS_CRYPTO_CURVE_NISTP521) || \ defined(HITLS_CRYPTO_CURVE_BP256R1) || defined(HITLS_CRYPTO_CURVE_BP384R1) || \ defined(HITLS_CRYPTO_CURVE_BP512R1) || defined(HITLS_CRYPTO_CURVE_SM2) #ifndef HITLS_CRYPTO_ECC #define HITLS_CRYPTO_ECC #endif #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #endif #if defined(HITLS_CRYPTO_NIST_ECC_ACCELERATE) && !defined(HITLS_CRYPTO_ECC) #undef HITLS_CRYPTO_NIST_ECC_ACCELERATE // Avoid turning on unnecessary functions. #endif #if defined(HITLS_CRYPTO_NIST_ECC_ACCELERATE) && defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) #define HITLS_CRYPTO_NIST_USE_ACCEL #endif #ifdef HITLS_CRYPTO_DSA_GEN_PARA #ifndef HITLS_CRYPTO_DSA #define HITLS_CRYPTO_DSA #endif #endif #ifdef HITLS_CRYPTO_ECDH #ifndef HITLS_CRYPTO_ECDH_CHECK #define HITLS_CRYPTO_ECDH_CHECK #endif #endif #ifdef HITLS_CRYPTO_ECDSA #ifndef HITLS_CRYPTO_ECDSA_CHECK #define HITLS_CRYPTO_ECDSA_CHECK #endif #endif #ifdef HITLS_CRYPTO_DH #ifndef HITLS_CRYPTO_DH_CHECK #define HITLS_CRYPTO_DH_CHECK #endif #endif #ifdef HITLS_CRYPTO_DSA #ifndef HITLS_CRYPTO_DSA_CHECK #define HITLS_CRYPTO_DSA_CHECK #endif #endif #if defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_CURVE25519) || defined(HITLS_CRYPTO_RSA) || \ defined(HITLS_CRYPTO_DH) || defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_ECDH) || \ defined(HITLS_CRYPTO_SM2) || defined(HITLS_CRYPTO_PAILLIER)|| defined(HITLS_CRYPTO_ELGAMAL) || \ defined(HITLS_CRYPTO_MLDSA) || defined(HITLS_CRYPTO_MLKEM) || defined(HITLS_CRYPTO_HYBRIDKEM) || \ defined(HITLS_CRYPTO_SLH_DSA) || defined(HITLS_CRYPTO_XMSS) #ifndef HITLS_CRYPTO_PKEY #define HITLS_CRYPTO_PKEY #endif #ifndef HITLS_BSL_PARAMS #define HITLS_BSL_PARAMS #endif #endif /* bn */ #ifdef HITLS_CRYPTO_BN #ifndef HITLS_CRYPTO_BN_BASIC #define HITLS_CRYPTO_BN_BASIC #endif #ifndef HITLS_CRYPTO_BN_RAND #define HITLS_CRYPTO_BN_RAND #endif #ifndef HITLS_CRYPTO_EAL_BN #define HITLS_CRYPTO_EAL_BN #endif #ifndef HITLS_CRYPTO_BN_PRIME #define HITLS_CRYPTO_BN_PRIME #endif #ifndef HITLS_CRYPTO_BN_STR_CONV #define HITLS_CRYPTO_BN_STR_CONV #endif #ifndef HITLS_CRYPTO_BN_CB #define HITLS_CRYPTO_BN_CB #endif #ifndef HITLS_CRYPTO_BN_PRIME_RFC3526 #define HITLS_CRYPTO_BN_PRIME_RFC3526 #endif #endif #if defined(HITLS_CRYPTO_BN_PRIME) && !defined(HITLS_CRYPTO_BN_RAND) #define HITLS_CRYPTO_BN_RAND #endif #if defined(HITLS_CRYPTO_BN_RAND) || defined(HITLS_CRYPTO_EAL_BN) || defined(HITLS_CRYPTO_BN_PRIME) || \ defined(HITLS_CRYPTO_BN_STR_CONV) || defined(HITLS_CRYPTO_BN_CB) || defined(HITLS_CRYPTO_BN_PRIME_RFC3526) || \ defined(HITLS_CRYPTO_BN_BASIC) #ifndef HITLS_CRYPTO_BN #define HITLS_CRYPTO_BN #endif #endif /* MD */ #ifdef HITLS_CRYPTO_MD #ifndef HITLS_CRYPTO_MD5 #define HITLS_CRYPTO_MD5 #endif #ifndef HITLS_CRYPTO_SM3 #define HITLS_CRYPTO_SM3 #endif #ifndef HITLS_CRYPTO_SHA1 #define HITLS_CRYPTO_SHA1 #endif #ifndef HITLS_CRYPTO_SHA2 #define HITLS_CRYPTO_SHA2 #endif #ifndef HITLS_CRYPTO_SHA3 #define HITLS_CRYPTO_SHA3 #endif #endif #ifdef HITLS_CRYPTO_SHA2 #ifndef HITLS_CRYPTO_SHA224 #define HITLS_CRYPTO_SHA224 #endif #ifndef HITLS_CRYPTO_SHA256 #define HITLS_CRYPTO_SHA256 #endif #ifndef HITLS_CRYPTO_SHA384 #define HITLS_CRYPTO_SHA384 #endif #ifndef HITLS_CRYPTO_SHA512 #define HITLS_CRYPTO_SHA512 #endif #endif #if defined(HITLS_CRYPTO_SHA224) && !defined(HITLS_CRYPTO_SHA256) #define HITLS_CRYPTO_SHA256 #endif #if defined(HITLS_CRYPTO_SHA384) && !defined(HITLS_CRYPTO_SHA512) #define HITLS_CRYPTO_SHA512 #endif #if defined(HITLS_CRYPTO_SHA256) || defined(HITLS_CRYPTO_SHA512) #ifndef HITLS_CRYPTO_SHA2 #define HITLS_CRYPTO_SHA2 #endif #endif #if defined(HITLS_CRYPTO_MD5) || defined(HITLS_CRYPTO_SM3) || defined(HITLS_CRYPTO_SHA1) || \ defined(HITLS_CRYPTO_SHA2) || defined(HITLS_CRYPTO_SHA3) #ifndef HITLS_CRYPTO_MD #define HITLS_CRYPTO_MD #endif #endif /* Assembling Macros */ #if defined(HITLS_CRYPTO_AES_X8664) || defined(HITLS_CRYPTO_AES_ARMV8) #define HITLS_CRYPTO_AES_ASM #endif #if defined(HITLS_CRYPTO_CHACHA20_X8664) || defined(HITLS_CRYPTO_CHACHA20_ARMV8) #define HITLS_CRYPTO_CHACHA20_ASM #endif #if defined(HITLS_CRYPTO_SM4_X8664) || defined(HITLS_CRYPTO_SM4_ARMV8) #define HITLS_CRYPTO_SM4_ASM #endif #if defined(HITLS_CRYPTO_MODES_X8664) #define HITLS_CRYPTO_CHACHA20POLY1305_X8664 #define HITLS_CRYPTO_GCM_X8664 #endif #if defined(HITLS_CRYPTO_MODES_ARMV8) #define HITLS_CRYPTO_CHACHA20POLY1305_ARMV8 #define HITLS_CRYPTO_GCM_ARMV8 #endif #if defined(HITLS_CRYPTO_MODES_X8664) || defined(HITLS_CRYPTO_MODES_ARMV8) #define HITLS_CRYPTO_MODES_ASM #endif #if defined(HITLS_CRYPTO_CHACHA20POLY1305_X8664) || defined(HITLS_CRYPTO_CHACHA20POLY1305_ARMV8) #define HITLS_CRYPTO_CHACHA20POLY1305_ASM #endif #if defined(HITLS_CRYPTO_GCM_X8664) || defined(HITLS_CRYPTO_GCM_ARMV8) #define HITLS_CRYPTO_GCM_ASM #endif #if defined(HITLS_CRYPTO_MD5_X8664) #define HITLS_CRYPTO_MD5_ASM #endif #if defined(HITLS_CRYPTO_SHA1_X8664) || defined(HITLS_CRYPTO_SHA1_ARMV8) #define HITLS_CRYPTO_SHA1_ASM #endif #if defined(HITLS_CRYPTO_SHA224_X8664) || defined(HITLS_CRYPTO_SHA224_ARMV8) || \ defined(HITLS_CRYPTO_SHA256_X8664) || defined(HITLS_CRYPTO_SHA256_ARMV8) || \ defined(HITLS_CRYPTO_SHA384_X8664) || defined(HITLS_CRYPTO_SHA384_ARMV8) || \ defined(HITLS_CRYPTO_SHA512_X8664) || defined(HITLS_CRYPTO_SHA512_ARMV8) || \ defined(HITLS_CRYPTO_SHA2_X8664) || defined(HITLS_CRYPTO_SHA2_ARMV8) #define HITLS_CRYPTO_SHA2_ASM #endif #if defined(HITLS_CRYPTO_SM3_X8664) || defined(HITLS_CRYPTO_SM3_ARMV8) #define HITLS_CRYPTO_SM3_ASM #endif #if defined(HITLS_CRYPTO_BN_X8664) || defined(HITLS_CRYPTO_BN_ARMV8) #define HITLS_CRYPTO_BN_ASM #endif #if defined(HITLS_CRYPTO_ECC_X8664) #define HITLS_CRYPTO_CURVE_NISTP256_X8664 #define HITLS_CRYPTO_CURVE_SM2_X8664 #endif #if defined(HITLS_CRYPTO_ECC_ARMV8) #define HITLS_CRYPTO_CURVE_NISTP256_ARMV8 #define HITLS_CRYPTO_CURVE_SM2_ARMV8 #endif #if defined(HITLS_CRYPTO_ECC_X8664) || defined(HITLS_CRYPTO_ECC_ARMV8) #define HITLS_CRYPTO_ECC_ASM #endif #if defined(HITLS_CRYPTO_CURVE_NISTP256_X8664) || defined(HITLS_CRYPTO_CURVE_NISTP256_ARMV8) #define HITLS_CRYPTO_CURVE_NISTP256_ASM #endif #if defined(HITLS_CRYPTO_CURVE_NISTP384_X8664) || defined(HITLS_CRYPTO_CURVE_NISTP384_ARMV8) #define HITLS_CRYPTO_CURVE_NISTP384_ASM #endif #if defined(HITLS_CRYPTO_CURVE_SM2_X8664) || defined(HITLS_CRYPTO_CURVE_SM2_ARMV8) #define HITLS_CRYPTO_CURVE_SM2_ASM #endif #if (!defined(HITLS_SIXTY_FOUR_BITS)) #if (((defined(HITLS_CRYPTO_CURVE_NISTP224) || defined(HITLS_CRYPTO_CURVE_NISTP521)) && \ !defined(HITLS_CRYPTO_NIST_USE_ACCEL)) || \ defined(HITLS_CRYPTO_CURVE_NISTP384) || \ (defined(HITLS_CRYPTO_CURVE_NISTP256) && !defined(HITLS_CRYPTO_CURVE_NISTP256_ASM) && \ (!defined(HITLS_CRYPTO_NIST_ECC_ACCELERATE)) && (!defined(HITLS_CRYPTO_NIST_USE_ACCEL))) || \ (defined(HITLS_CRYPTO_CURVE_SM2) && !defined(HITLS_CRYPTO_CURVE_SM2_ASM))) #define HITLS_CRYPTO_CURVE_MONT_NIST #endif #endif #if defined(HITLS_CRYPTO_CURVE_BP256R1) || defined(HITLS_CRYPTO_CURVE_BP384R1) || \ defined(HITLS_CRYPTO_CURVE_BP512R1) #define HITLS_CRYPTO_CURVE_MONT_PRIME #endif #if defined(HITLS_CRYPTO_CURVE_MONT_PRIME) || defined(HITLS_CRYPTO_CURVE_MONT_NIST) #define HITLS_CRYPTO_CURVE_MONT #endif #if defined(HITLS_CRYPTO_ECDSA_CHECK) || defined(HITLS_CRYPTO_ECDH_CHECK) || defined(HITLS_CRYPTO_SM2_CHECK) #ifndef HITLS_CRYPTO_ECC_CHECK #define HITLS_CRYPTO_ECC_CHECK #endif #endif #endif /* HITLS_CONFIG_LAYER_CRYPTO_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_config_layer_crypto.h
C
unknown
24,120
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef HITLS_CONFIG_LAYER_PKI_H #define HITLS_CONFIG_LAYER_PKI_H #ifdef HITLS_PKI_PKCS12 #ifndef HITLS_PKI_PKCS12_GEN #define HITLS_PKI_PKCS12_GEN #endif #ifndef HITLS_PKI_PKCS12_PARSE #define HITLS_PKI_PKCS12_PARSE #endif #endif #ifdef HITLS_PKI_PKCS12_GEN #ifndef HITLS_PKI_X509_CRT_GEN #define HITLS_PKI_X509_CRT_GEN #endif #ifndef HITLS_PKI_X509_CRT_PARSE #define HITLS_PKI_X509_CRT_PARSE #endif #ifndef HITLS_CRYPTO_KEY_ENCODE #define HITLS_CRYPTO_KEY_ENCODE #endif #endif #ifdef HITLS_PKI_PKCS12_PARSE #ifndef HITLS_PKI_X509_CRT_PARSE #define HITLS_PKI_X509_CRT_PARSE #endif #ifndef HITLS_CRYPTO_KEY_DECODE #define HITLS_CRYPTO_KEY_DECODE #endif #endif #if defined(HITLS_PKI_PKCS12_GEN) || defined(HITLS_PKI_PKCS12_PARSE) #ifndef HITLS_PKI_PKCS12 #define HITLS_PKI_PKCS12 #endif #ifndef HITLS_CRYPTO_KEY_EPKI #define HITLS_CRYPTO_KEY_EPKI #endif #endif #ifdef HITLS_PKI_X509 #ifndef HITLS_PKI_X509_CRT #define HITLS_PKI_X509_CRT #endif #ifndef HITLS_PKI_X509_CSR #define HITLS_PKI_X509_CSR #endif #ifndef HITLS_PKI_X509_CRL #define HITLS_PKI_X509_CRL #endif #ifndef HITLS_PKI_X509_VFY #define HITLS_PKI_X509_VFY #endif #endif #ifdef HITLS_PKI_X509_VFY #ifndef HITLS_PKI_X509_CRT_PARSE #define HITLS_PKI_X509_CRT_PARSE #endif #ifndef HITLS_PKI_X509_CRL_PARSE #define HITLS_PKI_X509_CRL_PARSE #endif #endif #ifdef HITLS_PKI_X509_CRT #ifndef HITLS_PKI_X509_CRT_GEN #define HITLS_PKI_X509_CRT_GEN #endif #ifndef HITLS_PKI_X509_CRT_PARSE #define HITLS_PKI_X509_CRT_PARSE #endif #endif #if defined(HITLS_PKI_X509_CRT_GEN) || defined(HITLS_PKI_X509_CRT_PARSE) #ifndef HITLS_PKI_X509_CRT #define HITLS_PKI_X509_CRT #endif #endif #ifdef HITLS_PKI_X509_CSR #ifndef HITLS_PKI_X509_CSR_GEN #define HITLS_PKI_X509_CSR_GEN #endif #ifndef HITLS_PKI_X509_CSR_PARSE #define HITLS_PKI_X509_CSR_PARSE #endif #endif #if defined(HITLS_PKI_X509_CSR_GEN) || defined(HITLS_PKI_X509_CSR_PARSE) #ifndef HITLS_PKI_X509_CSR #define HITLS_PKI_X509_CSR #endif #endif #ifdef HITLS_PKI_X509_CRL #ifndef HITLS_PKI_X509_CRL_GEN #define HITLS_PKI_X509_CRL_GEN #endif #ifndef HITLS_PKI_X509_CRL_PARSE #define HITLS_PKI_X509_CRL_PARSE #endif #endif #if defined(HITLS_PKI_X509_CRL_GEN) || defined(HITLS_PKI_X509_CRL_PARSE) #ifndef HITLS_PKI_X509_CRL #define HITLS_PKI_X509_CRL #endif #endif #if defined(HITLS_PKI_X509_CRT) || defined(HITLS_PKI_X509_CSR) || defined(HITLS_PKI_X509_CRL) || \ defined(HITLS_PKI_X509_VFY) #ifndef HITLS_PKI_X509 #define HITLS_PKI_X509 #endif #endif #if defined(HITLS_PKI_X509_CRT_GEN) || defined(HITLS_PKI_X509_CSR_GEN) || defined(HITLS_PKI_X509_CRL_GEN) || \ defined(HITLS_PKI_PKCS12_GEN) #ifndef HITLS_CRYPTO_KEY_ENCODE #define HITLS_CRYPTO_KEY_ENCODE #endif #endif #if defined(HITLS_PKI_X509_CRT_PARSE) || defined(HITLS_PKI_X509_CSR_PARSE) || defined(HITLS_PKI_X509_CRL_PARSE) || \ defined(HITLS_PKI_PKCS12_PARSE) #ifndef HITLS_CRYPTO_KEY_DECODE #define HITLS_CRYPTO_KEY_DECODE #endif #endif #ifdef HITLS_PKI_INFO #ifndef HITLS_BSL_UIO_PLT #define HITLS_BSL_UIO_PLT #endif #endif // Common dependencies #ifndef HITLS_BSL_LIST #define HITLS_BSL_LIST #endif #ifndef HITLS_BSL_OBJ #define HITLS_BSL_OBJ #endif #ifndef HITLS_BSL_ASN1 #define HITLS_BSL_ASN1 #endif #endif /* HITLS_CONFIG_LAYER_PKI_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_config_layer_pki.h
C
unknown
4,233
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /* Derivation of configuration features. * The derivation type (rule) and sequence are as follows: * 1. Parent features derive child features. * 2. Derive the features of dependencies. * For example, if feature a depends on features b and c, you need to derive features b and c. * 3. Child features derive parent features. * The high-level interfaces of the crypto module is controlled by the parent feature macro, * if there is no parent feature, such interfaces will be unavailable. */ #ifndef HITLS_CONFIG_LAYER_TLS_H #define HITLS_CONFIG_LAYER_TLS_H // version #ifdef HITLS_TLS_PROTO_VERSION #ifndef HITLS_TLS_PROTO_TLS12 #define HITLS_TLS_PROTO_TLS12 #endif #ifndef HITLS_TLS_PROTO_TLS13 #define HITLS_TLS_PROTO_TLS13 #endif #ifndef HITLS_TLS_PROTO_TLCP11 #define HITLS_TLS_PROTO_TLCP11 #endif #ifndef HITLS_TLS_PROTO_DTLCP11 #define HITLS_TLS_PROTO_DTLCP11 #endif #ifndef HITLS_TLS_PROTO_DTLS12 #define HITLS_TLS_PROTO_DTLS12 #endif #endif #if defined(HITLS_TLS_PROTO_DTLCP11) #ifndef HITLS_TLS_PROTO_DTLS12 #define HITLS_TLS_PROTO_DTLS12 #endif #ifndef HITLS_TLS_PROTO_TLCP11 #define HITLS_TLS_PROTO_TLCP11 #endif #endif #if defined(HITLS_TLS_PROTO_TLS12) || defined(HITLS_TLS_PROTO_TLS13) || defined(HITLS_TLS_PROTO_TLCP11) #ifndef HITLS_TLS_PROTO_TLS #define HITLS_TLS_PROTO_TLS #endif #endif #if defined(HITLS_TLS_PROTO_TLS12) || defined(HITLS_TLS_PROTO_TLCP11) #ifndef HITLS_TLS_PROTO_TLS_BASIC #define HITLS_TLS_PROTO_TLS_BASIC #endif #endif #if defined(HITLS_TLS_PROTO_DTLS12) #ifndef HITLS_TLS_PROTO_DTLS #define HITLS_TLS_PROTO_DTLS #endif #endif #if defined(HITLS_TLS_PROTO_TLS12) && defined(HITLS_TLS_PROTO_TLS13) #ifndef HITLS_TLS_PROTO_ALL #define HITLS_TLS_PROTO_ALL #endif #endif // host #ifdef HITLS_TLS_HOST #ifndef HITLS_TLS_HOST_SERVER #define HITLS_TLS_HOST_SERVER #endif #ifndef HITLS_TLS_HOST_CLIENT #define HITLS_TLS_HOST_CLIENT #endif #endif #if defined(HITLS_TLS_HOST_SERVER) || defined(HITLS_TLS_HOST_CLIENT) #ifndef HITLS_TLS_HOST #define HITLS_TLS_HOST #endif #endif // callback #ifdef HITLS_TLS_CALLBACK #ifndef HITLS_TLS_FEATURE_PROVIDER #define HITLS_TLS_FEATURE_PROVIDER #endif #ifndef HITLS_TLS_CALLBACK_SAL #define HITLS_TLS_CALLBACK_SAL #endif #ifndef HITLS_TLS_CALLBACK_CERT #define HITLS_TLS_CALLBACK_CERT #endif #ifndef HITLS_TLS_CALLBACK_CRYPT #define HITLS_TLS_CALLBACK_CRYPT #endif #endif #if defined(HITLS_TLS_FEATURE_PROVIDER) #ifdef HITLS_TLS_CALLBACK_SAL #undef HITLS_TLS_CALLBACK_SAL #endif #ifdef HITLS_TLS_CALLBACK_CERT #undef HITLS_TLS_CALLBACK_CERT #endif #ifdef HITLS_TLS_CALLBACK_CRYPT #undef HITLS_TLS_CALLBACK_CRYPT #endif #endif #if defined(HITLS_TLS_CALLBACK_CERT) || defined(HITLS_TLS_CALLBACK_CRYPT) #ifndef HITLS_TLS_CALLBACK_SAL #define HITLS_TLS_CALLBACK_SAL #endif #endif #ifdef HITLS_TLS_FEATURE_PROVIDER #ifndef HITLS_BSL_HASH #define HITLS_BSL_HASH #endif #endif #if !defined(HITLS_TLS_FEATURE_PROVIDER) && !defined(HITLS_TLS_CALLBACK_SAL) #define HITLS_TLS_FEATURE_PROVIDER #endif // feature #ifdef HITLS_TLS_FEATURE #ifndef HITLS_TLS_FEATURE_RENEGOTIATION #define HITLS_TLS_FEATURE_RENEGOTIATION #endif #ifndef HITLS_TLS_FEATURE_ALPN #define HITLS_TLS_FEATURE_ALPN #endif #ifndef HITLS_TLS_FEATURE_SNI #define HITLS_TLS_FEATURE_SNI #endif #ifndef HITLS_TLS_FEATURE_PHA #define HITLS_TLS_FEATURE_PHA #endif #ifndef HITLS_TLS_FEATURE_PSK #define HITLS_TLS_FEATURE_PSK #endif #ifndef HITLS_TLS_FEATURE_SECURITY #define HITLS_TLS_FEATURE_SECURITY #endif #ifndef HITLS_TLS_FEATURE_INDICATOR #define HITLS_TLS_FEATURE_INDICATOR #endif #ifndef HITLS_TLS_FEATURE_SESSION #define HITLS_TLS_FEATURE_SESSION #endif #ifndef HITLS_TLS_FEATURE_KEY_UPDATE #define HITLS_TLS_FEATURE_KEY_UPDATE #endif #ifndef HITLS_TLS_FEATURE_FLIGHT #define HITLS_TLS_FEATURE_FLIGHT #endif #ifndef HITLS_TLS_FEATURE_CERT_MODE #define HITLS_TLS_FEATURE_CERT_MODE #endif #ifndef HITLS_TLS_FEATURE_MODE #define HITLS_TLS_FEATURE_MODE #endif #ifndef HITLS_TLS_FEATURE_KEM #define HITLS_TLS_FEATURE_KEM #endif #ifndef HITLS_TLS_FEATURE_CLIENT_HELLO_CB #define HITLS_TLS_FEATURE_CLIENT_HELLO_CB #endif #ifndef HITLS_TLS_FEATURE_CERT_CB #define HITLS_TLS_FEATURE_CERT_CB #endif #ifndef HITLS_TLS_FEATURE_MAX_SEND_FRAGMENT #define HITLS_TLS_FEATURE_MAX_SEND_FRAGMENT #endif #ifndef HITLS_TLS_FEATURE_REC_INBUFFER_SIZE #define HITLS_TLS_FEATURE_REC_INBUFFER_SIZE #endif #ifndef HITLS_TLS_FEATURE_CUSTOM_EXTENSION #define HITLS_TLS_FEATURE_CUSTOM_EXTENSION #endif #ifndef HITLS_TLS_FEATURE_CERTIFICATE_AUTHORITIES #define HITLS_TLS_FEATURE_CERTIFICATE_AUTHORITIES #endif #endif /* HITLS_TLS_FEATURE */ #ifdef HITLS_TLS_FEATURE_SESSION #ifndef HITLS_TLS_FEATURE_SESSION_TICKET #define HITLS_TLS_FEATURE_SESSION_TICKET #endif #ifndef HITLS_TLS_FEATURE_SESSION_ID #define HITLS_TLS_FEATURE_SESSION_ID #endif #endif #ifdef HITLS_TLS_FEATURE_MODE #ifndef HITLS_TLS_FEATURE_MODE_FALL_BACK_SCSV #define HITLS_TLS_FEATURE_MODE_FALL_BACK_SCSV #endif #ifndef HITLS_TLS_FEATURE_MODE_AUTO_RETRY #define HITLS_TLS_FEATURE_MODE_AUTO_RETRY #endif #ifndef HITLS_TLS_FEATURE_MODE_ACCEPT_MOVING_WRITE_BUFFER #define HITLS_TLS_FEATURE_MODE_ACCEPT_MOVING_WRITE_BUFFER #endif #ifndef HITLS_TLS_FEATURE_MODE_RELEASE_BUFFERS #define HITLS_TLS_FEATURE_MODE_RELEASE_BUFFERS #endif #endif #if defined(HITLS_TLS_FEATURE_MODE_FALL_BACK_SCSV) || defined(HITLS_TLS_FEATURE_MODE_AUTO_RETRY) || \ defined(HITLS_TLS_FEATURE_MODE_ACCEPT_MOVING_WRITE_BUFFER) || defined(HITLS_TLS_FEATURE_MODE_RELEASE_BUFFERS) #ifndef HITLS_TLS_FEATURE_MODE #define HITLS_TLS_FEATURE_MODE #endif #endif #if defined(HITLS_TLS_FEATURE_SESSION_TICKET) || defined(HITLS_TLS_FEATURE_SESSION_ID) #ifndef HITLS_TLS_FEATURE_SESSION #define HITLS_TLS_FEATURE_SESSION #endif #endif #ifdef HITLS_TLS_FEATURE_SECURITY #ifndef HITLS_TLS_CONFIG_CIPHER_SUITE #define HITLS_TLS_CONFIG_CIPHER_SUITE #endif #endif // proto #ifdef HITLS_TLS_PROTO #ifndef HITLS_BSL_TLV #define HITLS_BSL_TLV #endif #ifndef HITLS_BSL_SAL #define HITLS_BSL_SAL #endif #ifndef HITLS_CRYPTO_EAL #define HITLS_CRYPTO_EAL #endif #endif // suite_cipher #ifdef HITLS_TLS_SUITE_CIPHER #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #endif // KX #ifdef HITLS_TLS_SUITE_KX #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_KX_ECDH #define HITLS_TLS_SUITE_KX_ECDH #endif #ifndef HITLS_TLS_SUITE_KX_DH #define HITLS_TLS_SUITE_KX_DH #endif #ifndef HITLS_TLS_SUITE_KX_RSA #define HITLS_TLS_SUITE_KX_RSA #endif #endif // AUTH #ifdef HITLS_TLS_SUITE_AUTH #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_ECDSA #define HITLS_TLS_SUITE_AUTH_ECDSA #endif #ifndef HITLS_TLS_SUITE_AUTH_DSS #define HITLS_TLS_SUITE_AUTH_DSS #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #ifndef HITLS_TLS_SUITE_AUTH_SM2 #define HITLS_TLS_SUITE_AUTH_SM2 #endif #endif // MAINTAIN #ifdef HITLS_TLS_MAINTAIN #ifndef HITLS_TLS_MAINTAIN_KEYLOG #define HITLS_TLS_MAINTAIN_KEYLOG #endif #endif #ifdef HITLS_TLS_CONFIG #ifndef HITLS_TLS_CONFIG_MANUAL_DH #define HITLS_TLS_CONFIG_MANUAL_DH #endif #ifndef HITLS_TLS_CONFIG_CERT #define HITLS_TLS_CONFIG_CERT #endif #ifndef HITLS_TLS_CONFIG_KEY_USAGE #define HITLS_TLS_CONFIG_KEY_USAGE #endif #ifndef HITLS_TLS_CONFIG_INFO #define HITLS_TLS_CONFIG_INFO #endif #ifndef HITLS_TLS_CONFIG_STATE #define HITLS_TLS_CONFIG_STATE #endif #ifndef HITLS_TLS_CONFIG_RECORD_PADDING #define HITLS_TLS_CONFIG_RECORD_PADDING #endif #ifndef HITLS_TLS_CONFIG_USER_DATA #define HITLS_TLS_CONFIG_USER_DATA #endif #ifndef HITLS_TLS_CONFIG_CIPHER_SUITE #define HITLS_TLS_CONFIG_CIPHER_SUITE #endif #endif #ifdef HITLS_TLS_CONNECTION #ifndef HITLS_TLS_CONNECTION_INFO_NEGOTIATION #define HITLS_TLS_CONNECTION_INFO_NEGOTIATION #endif #endif #ifdef HITLS_TLS_CONNECTION_INFO_NEGOTIATION #ifndef HITLS_TLS_CONNECTION #define HITLS_TLS_CONNECTION #endif #endif #ifdef HITLS_TLS_CONFIG_CERT #ifndef HITLS_TLS_CONFIG_CERT_LOAD_FILE #define HITLS_TLS_CONFIG_CERT_LOAD_FILE #endif #ifndef HITLS_TLS_CONFIG_CERT_CALLBACK #define HITLS_TLS_CONFIG_CERT_CALLBACK #endif #ifndef HITLS_TLS_CONFIG_CERT_BUILD_CHAIN #define HITLS_TLS_CONFIG_CERT_BUILD_CHAIN #endif #endif #if defined(HITLS_TLS_PROTO_DTLS12) || defined(HITLS_TLS_PROTO_TLS13) #ifndef HITLS_TLS_EXTENSION_COOKIE #define HITLS_TLS_EXTENSION_COOKIE #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_RSA) #if !defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_GCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_256_GCM_SHA384 #endif #if !defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_8_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_ECDSA) #if !defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_GCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_256_GCM_SHA384 #endif #if !defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_8_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CCM #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CCM #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CCM #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CCM #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_PSK) #if !defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_GCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_256_GCM_SHA384 #endif #if !defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_8_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CCM_SHA256 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_GCM_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_RSA) #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CCM #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CCM #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CCM #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CCM #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_DSS) #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_GCM_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_PSK) #if !defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_GCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_256_GCM_SHA384 #endif #if !defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_SHA256 #endif #if !defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) && defined(HITLS_TLS_PROTO_TLS13) #define HITLS_TLS_SUITE_AES_128_CCM_8_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CCM #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CCM #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CCM #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CCM #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_RSA) && defined(HITLS_TLS_SUITE_AUTH_RSA) #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_RSA_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_RSA_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM #define HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM_8 #define HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM_8 #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM #define HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM_8 #define HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM_8 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_RSA) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_SM2) #ifndef HITLS_TLS_SUITE_ECDHE_SM4_GCM_SM3 #define HITLS_TLS_SUITE_ECDHE_SM4_GCM_SM3 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_RSA) #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_ECDSA) #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_ECDHE) && defined(HITLS_TLS_SUITE_AUTH_SM2) #ifndef HITLS_TLS_SUITE_ECDHE_SM4_CBC_SM3 #define HITLS_TLS_SUITE_ECDHE_SM4_CBC_SM3 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_RSA) #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA256 #define HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_DSS) #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA256 #define HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_DHE) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_RSA) && defined(HITLS_TLS_SUITE_AUTH_RSA) #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA256 #define HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_RSA) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_KX_DHE) #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_GCM_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_ECDHE) #ifndef HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_256_CBC_SHA #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_KX_DHE) #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA256 #define HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_128_GCM_SHA256 #define HITLS_TLS_SUITE_PSK_WITH_AES_128_GCM_SHA256 #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_256_GCM_SHA384 #define HITLS_TLS_SUITE_PSK_WITH_AES_256_GCM_SHA384 #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_256_CCM #define HITLS_TLS_SUITE_PSK_WITH_AES_256_CCM #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_CHACHA20_POLY1305_SHA256 #define HITLS_TLS_SUITE_PSK_WITH_CHACHA20_POLY1305_SHA256 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_AUTH_PSK) #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA #define HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA #define HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA256 #define HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA256 #endif #ifndef HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA384 #define HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA384 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_CBC) && defined(HITLS_TLS_SUITE_AUTH_SM2) #ifndef HITLS_TLS_SUITE_ECC_SM4_CBC_SM3 #define HITLS_TLS_SUITE_ECC_SM4_CBC_SM3 #endif #endif #if defined(HITLS_TLS_SUITE_CIPHER_AEAD) && defined(HITLS_TLS_SUITE_AUTH_SM2) #ifndef HITLS_TLS_SUITE_ECC_SM4_GCM_SM3 #define HITLS_TLS_SUITE_ECC_SM4_GCM_SM3 #endif #endif #if defined(HITLS_TLS_SUITE_AES_128_GCM_SHA256) || defined(HITLS_TLS_SUITE_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_CHACHA20_POLY1305_SHA256) || defined(HITLS_TLS_SUITE_AES_128_CCM_SHA256) || \ defined(HITLS_TLS_SUITE_AES_128_CCM_8_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA) || defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CBC_SHA256) || defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CBC_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_RSA #define HITLS_TLS_SUITE_KX_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM) || defined(HITLS_TLS_SUITE_RSA_WITH_AES_128_CCM_8) || \ defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM) || defined(HITLS_TLS_SUITE_RSA_WITH_AES_256_CCM_8) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_RSA #define HITLS_TLS_SUITE_KX_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CCM) || defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CCM) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_ECDSA #define HITLS_TLS_SUITE_AUTH_ECDSA #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_128_CCM) || defined(HITLS_TLS_SUITE_ECDHE_ECDSA_WITH_AES_256_CCM) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_ECDSA #define HITLS_TLS_SUITE_AUTH_ECDSA #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_GCM_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_DSS #define HITLS_TLS_SUITE_AUTH_DSS #endif #endif #if defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_DSS_WITH_AES_256_CBC_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_DSS #define HITLS_TLS_SUITE_AUTH_DSS #endif #endif #if defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_RSA_WITH_AES_256_CBC_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA) || defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_CBC_SHA256) || defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_RSA #define HITLS_TLS_SUITE_KX_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_PSK_WITH_AES_128_GCM_SHA256) || defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_PSK_WITH_AES_256_CCM) || defined(HITLS_TLS_SUITE_PSK_WITH_CHACHA20_POLY1305_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_GCM_SHA384) || defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_128_CCM) || \ defined(HITLS_TLS_SUITE_DHE_PSK_WITH_AES_256_CCM) || defined(HITLS_TLS_SUITE_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_RSA_PSK_WITH_AES_256_GCM_SHA384) || \ defined(HITLS_TLS_SUITE_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_RSA #define HITLS_TLS_SUITE_KX_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_RSA #define HITLS_TLS_SUITE_AUTH_RSA #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_CBC_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_CCM_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_ECDHE_PSK_WITH_AES_256_GCM_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_PSK #define HITLS_TLS_SUITE_AUTH_PSK #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA) || \ defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_CBC_SHA256) || \ defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_CBC_SHA256) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #endif #if defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_128_GCM_SHA256) || \ defined(HITLS_TLS_SUITE_DH_ANON_WITH_AES_256_GCM_SHA384) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_DHE #define HITLS_TLS_SUITE_KX_DHE #endif #endif #if defined(HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_128_CBC_SHA) || \ defined(HITLS_TLS_SUITE_ECDH_ANON_WITH_AES_256_CBC_SHA) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_SM4_CBC_SM3) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_SM2 #define HITLS_TLS_SUITE_AUTH_SM2 #endif #endif #if defined(HITLS_TLS_SUITE_ECC_SM4_CBC_SM3) #ifndef HITLS_TLS_SUITE_CIPHER_CBC #define HITLS_TLS_SUITE_CIPHER_CBC #endif #ifndef HITLS_TLS_SUITE_AUTH_SM2 #define HITLS_TLS_SUITE_AUTH_SM2 #endif #endif #if defined(HITLS_TLS_SUITE_ECDHE_SM4_GCM_SM3) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_KX_ECDHE #define HITLS_TLS_SUITE_KX_ECDHE #endif #ifndef HITLS_TLS_SUITE_AUTH_SM2 #define HITLS_TLS_SUITE_AUTH_SM2 #endif #endif #if defined(HITLS_TLS_SUITE_ECC_SM4_GCM_SM3) #ifndef HITLS_TLS_SUITE_CIPHER_AEAD #define HITLS_TLS_SUITE_CIPHER_AEAD #endif #ifndef HITLS_TLS_SUITE_AUTH_SM2 #define HITLS_TLS_SUITE_AUTH_SM2 #endif #endif #ifdef HITLS_TLS_SUITE_CIPHER_CBC #ifndef HITLS_TLS_CALLBACK_CRYPT_HMAC_PRIMITIVES #define HITLS_TLS_CALLBACK_CRYPT_HMAC_PRIMITIVES #endif #endif #ifdef HITLS_TLS_SUITE_CIPHER_CBC #ifndef HITLS_TLS_FEATURE_ETM #define HITLS_TLS_FEATURE_ETM #endif #endif #if defined(HITLS_TLS_SUITE_AUTH_ECDSA) || defined(HITLS_TLS_SUITE_AUTH_RSA) || defined(HITLS_TLS_SUITE_AUTH_DSS) || \ defined(HITLS_TLS_SUITE_AUTH_PSK) || defined(HITLS_TLS_SUITE_AUTH_SM2) #ifndef HITLS_TLS_SUITE_AUTH #define HITLS_TLS_SUITE_AUTH #endif #endif #endif /* HITLS_CONFIG_LAYER_TLS_H */
2302_82127028/openHiTLS-examples
config/macro_config/hitls_config_layer_tls.h
C
unknown
41,045
set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_PROCESSOR arm) set(TOOLCHAIN_PATH /usr/bin) set(TOOLCHAIN_PREFIX arm-none-eabi) set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gcc) set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-g++) set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-gcc) set(CMAKE_AR ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-ar) set(CMAKE_RANLIB ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-ranlib) set(CMAKE_STRIP ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-strip) set(CMAKE_OBJCOPY ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-objcopy) set(CMAKE_OBJDUMP ${TOOLCHAIN_PATH}/${TOOLCHAIN_PREFIX}-objdump) set(BUILD_SHARED_LIBS OFF) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
2302_82127028/openHiTLS-examples
config/toolchain/arm-none-eabi-gcc_toolchain.cmake
CMake
unknown
712
#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of the openHiTLS project. # # openHiTLS is licensed under the Mulan PSL v2. # You can use this software according to the terms and conditions of the Mulan PSL v2. # You may obtain a copy of Mulan PSL v2 at: # # http://license.coscl.org.cn/MulanPSL2 # # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. # See the Mulan PSL v2 for more details. """ Customize the openHiTLS build. Generate the modules.cmake file based on command line arguments and configuration files. Options usage and examples: 1 Enable the feature on demand and specify the implementation type of the feature, c or assembly. # Use 'enable' to specify the features to be constructed. # Compile C code if there is no other parameter. ./configure.py --enable all # Build all features of openHiTLS. ./configure.py --enable hitls_crypto # Build all features in the lib hitls_crypto. ./configure.py --enable md # Build all sub features of md. ./configure.py --enable sha2 sha3 hmac # Specifies to build certain features. # Use 'enable' to specify the features to be constructed. # Use 'asm_type' to specify the assembly type. # If there are features in enable list that supports assembly, compile its assembly implementation. ./configure.py --enable sm3 aes ... --asm_type armv8 # Use 'enable' to specify the features to be constructed. # Use 'asm_type' to specify the assembly type. # Use 'asm' to specify the assembly feature(s), which is(are) based on the enabled features. # Compile the assembly code of the features in the asm, and the C code of other features in the enable list. ./configure.py --enable sm3 aes ... --asm_type armv8 --asm sm3 2 Compile options: Add or delete compilation options based on the default compilation options (compile.json). ./configure.py --add_options "-O0 -g" --del_options "-O2 -D_FORTIFY_SOURCE=2" 3 Link options: Add or delete link options based on the default link options (compile.json). ./configure.py --add_link_flags "xxx xxx" --del_link_flags "xxx xxx" 4 Set the endian mode of the system. Set the endian mode of the system. The default value is little endian. ./configure.py --endian big 5 Specifies the system type. ./configure.py --system linux 6 Specifies the number of system bits. ./configure.py --bits 32 7 Generating modules.cmake ./configure.py -m 8 Specifies the directory where the compilation middleware is generated. The default directory is ./output. ./configure.py --build_dir build 9 Specifies the lib type. ./configure.py --lib_type static ./configure.py --lib_type static shared object 10 You can directly specify the compilation configuration files, omitting the above 1~9 command line parameters. For the file format, please refer to the compile_config.json and feature_config.json files generated after executing the above 1~9 commands. ./configure.py --feature_config path/to/xxx.json --compile_config path/to/xxx.json Note: Options for different functions can be combined. """ import sys sys.dont_write_bytecode = True import os import argparse import traceback import glob from script.methods import copy_file, save_json_file, trans2list from script.config_parser import (FeatureParser, CompileParser, FeatureConfigParser, CompileConfigParser, CompleteOptionParser) srcdir = os.path.dirname(os.path.realpath(sys.argv[0])) work_dir = os.path.abspath(os.getcwd()) def get_cfg_args(): parser = argparse.ArgumentParser(prog='openHiTLS', description='parser configure arguments') try: # Version/Release Build Configuration Parameters parser.add_argument('-m', '--module_cmake', action='store_true', help='generate moudules.cmake file') parser.add_argument('--build_dir', metavar='dir', type=str, default=os.path.join(srcdir, 'build'), help='compile temp directory') parser.add_argument('--output_dir', metavar='dir', type=str, default=os.path.join(srcdir, 'output'), help='compile output directory') parser.add_argument('--hkey', metavar='hkey', type=str, default="b8fc4931453af3285f0f", help='Key used by the HMAC.') # Configuration file parser.add_argument('--feature_config', metavar='file_path', type=str, default='', help='Configuration file of the compilation features.') parser.add_argument('--compile_config', metavar='file_path', type=str, default='', help='Configuration file of compilation parameters.') # Compilation Feature Configuration parser.add_argument('--enable', metavar='feature', nargs='+', default=[], help='enable some libs or features, such as --enable sha256 aes gcm_asm, default is "all"') parser.add_argument('--disable', metavar='feature', nargs='+', default=['uio_sctp'], help='disable some libs or features, such as --disable aes gcm_asm,\ default is disable "uio_sctp" ') parser.add_argument('--enable-sctp', action="store_true", help='enable sctp which is used in DTLS') parser.add_argument('--asm_type', type=str, help='Assembly Type, default is "no_asm".') parser.add_argument('--asm', metavar='feature', default=[], nargs='+', help='config asm, such as --asm sha2') # System Configuration parser.add_argument('--system', type=str, help='To enable feature "sal_xxx", should specify the system.') parser.add_argument('--endian', metavar='little|big', type=str, choices=['little', 'big'], help='Specify the platform endianness as little or big, default is "little".') parser.add_argument('--bits', metavar='32|64', type=int, choices=[32, 64], help='To enable feature "bn", should specify the number of OS bits, default is "64".') # Compiler Options, Link Options parser.add_argument('--lib_type', choices=['static', 'shared', 'object'], nargs='+', help='set lib type, such as --lib_type staic shared, default is "staic shared object"') parser.add_argument('--add_options', default='', type=str, help='add some compile options, such as --add_options="-O0 -g"') parser.add_argument('--del_options', default='', type=str, help='delete some compile options such as --del_options="-O2 -Werror"') parser.add_argument('--add_link_flags', default='', type=str, help='add some link flags such as --add_link_flags="-pie"') parser.add_argument('--del_link_flags', default='', type=str, help='delete some link flags such as --del_link_flags="-shared -Wl,-z,relro"') parser.add_argument('--no_config_check', action='store_true', help='disable the configuration check') parser.add_argument('--hitls_version', default='openHiTLS 0.2.0 15 May 2025', help='%(prog)s version str') parser.add_argument('--hitls_version_num', default=0x00200000, help='%(prog)s version num') parser.add_argument('--bundle_libs', action='store_true', help='Indicates that multiple libraries are bundled together. By default, it is not bound.\ It need to be used together with "-m"') # Compile the command apps. parser.add_argument('--executes', dest='executes', default=[], nargs='*', help='Enable hitls command apps') args = vars(parser.parse_args()) args['tmp_feature_config'] = os.path.join(args['build_dir'], 'feature_config.json') args['tmp_compile_config'] = os.path.join(args['build_dir'], 'compile_config.json') # disable uio_sctp by default if args['enable_sctp'] or args['module_cmake']: if 'uio_sctp' in args['disable']: args['disable'].remove('uio_sctp') except argparse.ArgumentError as e: parser.print_help() raise ValueError("Error: Failed to obtain parameters.") from e return argparse.Namespace(**args) class Configure: """Provides operations related to configuration and input parameter parsing: 1 Parse input parameters. 2 Read configuration files and input parameters. 3 Update the final configuration files in the build directory. """ config_json_file = 'config.json' feature_json_file = 'config/json/feature.json' complete_options_json_file = 'config/json/complete_options.json' default_compile_json_file = 'config/json/compile.json' def __init__(self, features: FeatureParser): self._features = features self._args = get_cfg_args() self._preprocess_args() @property def args(self): return self._args def _preprocess_args(self): if self._args.feature_config and not os.path.exists(self._args.feature_config): raise FileNotFoundError('File not found: %s' % self._args.feature_config) if self._args.compile_config and not os.path.exists(self._args.compile_config): raise FileNotFoundError('File not found: %s' % self._args.compile_config) if 'all' in self._args.enable: if len(self._args.enable) > 1: raise ValueError("Error: 'all' and other features cannot be set at the same time.") else: for fea in self._args.enable: if fea in self._features.libs or fea in self._features.feas_info: continue raise ValueError("unrecognized fea '%s'" % fea) if self._args.asm_type: if self._args.asm_type not in self._features.asm_types: raise ValueError("Unsupported asm_type: asm_type should be one of [%s]" % self._features.asm_types) else: if self._args.asm and not self._args.asm_type: raise ValueError("Error: 'asm_type' and 'asm' must be set at the same time.") # The value of 'asm' will be verified later. @staticmethod def _load_config(is_fea_cfg, src_file, dest_file): if os.path.exists(dest_file): if src_file != '': raise FileExistsError('{} already exists'.format(dest_file)) else: if src_file == '': # No custom configuration file is specified, create a default config file. cfg = FeatureConfigParser.default_cfg() if is_fea_cfg else CompileConfigParser.default_cfg() save_json_file(cfg, dest_file) else: copy_file(src_file, dest_file) def load_config_to_build(self): """Load the compilation feature and compilation option configuration files to the build directory: build/feature_config.json build/compile_config.json """ if not os.path.exists(self._args.build_dir): os.makedirs(self._args.build_dir) self._load_config(True, self._args.feature_config, self._args.tmp_feature_config) self._load_config(False, self._args.compile_config, self._args.tmp_compile_config) def update_feature_config(self, gen_cmake): """Update the feature configuration file in the build based on the input parameters.""" conf_custom_feature = FeatureConfigParser(self._features, self._args.tmp_feature_config) if self._args.executes: conf_custom_feature.enable_executes(self._args.executes) # If no feature is enabled before modules.cmake is generated, set enable to "all". if not conf_custom_feature.libs and not self._args.enable and gen_cmake: self._args.enable = ['all'] # Set parameters by referring to "FeatureConfigParser.key_value". conf_custom_feature.set_param('libType', self._args.lib_type) if self._args.bundle_libs: conf_custom_feature.set_param('bundleLibs', self._args.bundle_libs) conf_custom_feature.set_param('endian', self._args.endian) conf_custom_feature.set_param('system', self._args.system, False) conf_custom_feature.set_param('bits', self._args.bits, False) enable_feas, asm_feas = conf_custom_feature.get_enable_feas(self._args.enable, self._args.asm) asm_type = self._args.asm_type if self._args.asm_type else '' if not asm_type and conf_custom_feature.asm_type != 'no_asm': asm_type = conf_custom_feature.asm_type if asm_type: conf_custom_feature.set_asm_type(asm_type) conf_custom_feature.set_asm_features(enable_feas, asm_feas, asm_type) if enable_feas: conf_custom_feature.set_c_features(enable_feas) self._args.securec_lib = conf_custom_feature.securec_lib # update feature and resave file. conf_custom_feature.update_feature(self._args.enable, self._args.disable, gen_cmake) conf_custom_feature.save(self._args.tmp_feature_config) self._args.bundle_libs = conf_custom_feature.bundle_libs def update_compile_config(self, all_options: CompleteOptionParser): """Update the compilation configuration file in the build based on the input parameters.""" conf_custom_compile = CompileConfigParser(all_options, self._args.tmp_compile_config) if self._args.add_options: conf_custom_compile.change_options(self._args.add_options.strip().split(' '), True) if self._args.del_options: conf_custom_compile.change_options(self._args.del_options.strip().split(' '), False) if self._args.add_link_flags: conf_custom_compile.change_link_flags(self._args.add_link_flags.strip().split(' '), True) if self._args.del_link_flags: conf_custom_compile.change_link_flags(self._args.del_link_flags.strip().split(' '), False) conf_custom_compile.save(self._args.tmp_compile_config) class CMakeGenerator: """ Generating CMake Commands and Scripts Based on Configuration Files """ def __init__(self, args, features: FeatureParser, all_options: CompleteOptionParser): self._args = args self._cfg_feature = features self._cfg_compile = CompileParser(all_options, Configure.default_compile_json_file) self._cfg_custom_feature = FeatureConfigParser(features, args.tmp_feature_config) self._cfg_custom_feature.check_fea_opts() self._cfg_custom_compile = CompileConfigParser(all_options, args.tmp_compile_config) self._asm_type = self._cfg_custom_feature.asm_type self._platform = 'linux' self._approved_provider = False self._hmac = "sha256" @staticmethod def _add_if_exists(inc_dirs, path): if os.path.exists(path): inc_dirs.add(path) @staticmethod def _get_common_include(modules: list): """ modules: ['::','::']""" inc_dirs = set() top_modules = set(x.split('::')[0] for x in modules) top_modules.add('bsl/log') top_modules.add('bsl/err') for module in top_modules: CMakeGenerator._add_if_exists(inc_dirs, module + '/include') CMakeGenerator._add_if_exists(inc_dirs, 'include/' + module) CMakeGenerator._add_if_exists(inc_dirs, 'config/macro_config') CMakeGenerator._add_if_exists(inc_dirs, '../../../../Secure_C/include') CMakeGenerator._add_if_exists(inc_dirs, '../../../platform/Secure_C/include') return inc_dirs def _get_module_include(self, mod: str, dep_mods: list): inc_dirs = set() dep_mods.append(mod) for dep in dep_mods: top_dir, sub_dir = dep.split('::') path = "{}/{}/include".format(top_dir, sub_dir) if os.path.exists(path): inc_dirs.add(path) top_mod, sub_mod = dep.split('::') cfg_inc = self._cfg_feature.modules[top_mod][sub_mod].get('.include', []) for inc_dir in cfg_inc: if os.path.exists(inc_dir): inc_dirs.add(inc_dir) return inc_dirs @staticmethod def _expand_srcs(srcs): if not srcs: return [] ret = [] for x in srcs: ret += glob.glob(x, recursive=True) if len(ret) == 0: raise SystemError("The .c file does not exist in the {} directory.".format(srcs)) ret.sort() return ret @classmethod def _gen_cmd_cmake(cls, cmd: str, title, content_obj=None): if not content_obj: return '{}({})\n'.format(cmd, title) items = None if isinstance(content_obj, list) or isinstance(content_obj, set): items = content_obj elif isinstance(content_obj, dict): items = content_obj.values() elif isinstance(content_obj, str): items = [content_obj] else: raise ValueError('Unsupported type "%s"' % type(content_obj)) content = '' for item in items: content += ' {}\n'.format(item) if len(items) == 1: return '{}({} {})\n'.format(cmd, title, item) else: return '{}({}\n{})\n'.format(cmd, title, content) def _get_module_src_set(self, lib, top_mod, sub_mod, mod_obj): srcs = self._cfg_feature.get_mod_srcs(top_mod, sub_mod, mod_obj) return self._expand_srcs(srcs) def _gen_module_cmake(self, lib, mod, mod_obj, mods_cmake): top_mod, module_name = mod.split('::') inc_set = self._get_module_include(mod, mod_obj.get('deps', [])) src_list = self._get_module_src_set(lib, top_mod, module_name, mod_obj) tgt_name = module_name + '-objs' cmake = '\n# Add module {} \n'.format(module_name) cmake += self._gen_cmd_cmake('add_library', '{} OBJECT'.format(tgt_name)) cmake += self._gen_cmd_cmake('target_include_directories', '{} PRIVATE'.format(tgt_name), inc_set) cmake += self._gen_cmd_cmake('target_sources', '{} PRIVATE'.format(tgt_name), src_list) mods_cmake[tgt_name] = cmake def _gen_shared_lib_cmake(self, lib_name, tgt_obj_list, tgt_list, macros): tgt_name = lib_name + '-shared' properties = 'OUTPUT_NAME {}'.format(lib_name) cmake = '\n' cmake += self._gen_cmd_cmake('add_library', '{} SHARED'.format(tgt_name), tgt_obj_list) cmake += self._gen_cmd_cmake('target_link_options', '{} PRIVATE'.format(tgt_name), '${SHARED_LNK_FLAGS}') if os.path.exists('{}/platform/Secure_C/lib'.format(srcdir)): cmake += self._gen_cmd_cmake('target_link_directories', '{} PRIVATE'.format(tgt_name), '{}/platform/Secure_C/lib'.format(srcdir)) cmake += self._gen_cmd_cmake('set_target_properties', '{} PROPERTIES'.format(tgt_name), properties) cmake += 'install(TARGETS %s DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)\n' % tgt_name if (self._approved_provider): # Use the openssl command to generate an HMAC file. cmake += 'install(CODE "execute_process(COMMAND openssl dgst -hmac \\\"%s\\\" -%s -out lib%s.so.hmac lib%s.so)")\n' % (self._args.hkey, self._hmac, lib_name, lib_name) # Install the hmac file to the output directory. cmake += 'install(CODE "execute_process(COMMAND cp lib%s.so.hmac ${CMAKE_INSTALL_PREFIX}/lib/lib%s.so.hmac)")\n' % (lib_name, lib_name) if lib_name == 'hitls_bsl': for item in macros: if item == '-DHITLS_BSL_UIO' or item == '-DHITLS_BSL_UIO_SCTP': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_bsl-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake("target_link_libraries", "hitls_bsl-shared " + str(self._args.securec_lib)) if item == '-DHITLS_BSL_SAL_DL': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_bsl-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake("target_link_libraries", "hitls_bsl-shared dl " + str(self._args.securec_lib)) if lib_name == 'hitls_crypto': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_crypto-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake("target_link_libraries", "hitls_crypto-shared hitls_bsl-shared " + str(self._args.securec_lib)) if lib_name == 'hitls_tls': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_tls-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake("target_link_libraries", "hitls_tls-shared hitls_pki-shared hitls_crypto-shared hitls_bsl-shared " + str(self._args.securec_lib)) if lib_name == 'hitls_pki': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_pki-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake( "target_link_libraries", "hitls_pki-shared hitls_crypto-shared hitls_bsl-shared " + str(self._args.securec_lib)) if lib_name == 'hitls_auth': cmake += self._gen_cmd_cmake("target_link_directories", "hitls_auth-shared PRIVATE " + "${CMAKE_SOURCE_DIR}/platform/Secure_C/lib") cmake += self._gen_cmd_cmake( "target_link_libraries", "hitls_auth-shared hitls_crypto-shared hitls_bsl-shared " + str(self._args.securec_lib)) if self._approved_provider: cmake += self._gen_cmd_cmake("target_link_libraries", "hitls-shared m " + str(self._args.securec_lib)) tgt_list.append(tgt_name) return cmake def _gen_static_lib_cmake(self, lib_name, tgt_obj_list, tgt_list): tgt_name = lib_name + '-static' properties = 'OUTPUT_NAME {}'.format(lib_name) cmake = '\n' cmake += self._gen_cmd_cmake('add_library', '{} STATIC'.format(tgt_name), tgt_obj_list) cmake += self._gen_cmd_cmake('set_target_properties', '{} PROPERTIES'.format(tgt_name), properties) cmake += 'install(TARGETS %s DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)\n' % tgt_name tgt_list.append(tgt_name) return cmake def _gen_obejct_lib_cmake(self, lib_name, tgt_obj_list, tgt_list): tgt_name = lib_name + '-object' properties = 'OUTPUT_NAME lib{}.o'.format(lib_name) cmake = '\n' cmake += self._gen_cmd_cmake('add_executable', tgt_name, tgt_obj_list) cmake += self._gen_cmd_cmake('target_link_options', '{} PRIVATE'.format(tgt_name), '${SHARED_LNK_FLAGS}') cmake += self._gen_cmd_cmake('set_target_properties', '{} PROPERTIES'.format(tgt_name), properties) cmake += 'install(TARGETS %s DESTINATION ${CMAKE_INSTALL_PREFIX}/obj)\n' % tgt_name tgt_list.append(tgt_name) return cmake def _get_definitions(self): ret = '"${CMAKE_C_FLAGS} -DOPENHITLS_VERSION_S=\'\\"%s\\"\' -DOPENHITLS_VERSION_I=%lu %s' % ( self._args.hitls_version, self._args.hitls_version_num, '-D__FILENAME__=\'\\"$(notdir $(subst .o,,$@))\\"\'') if self._approved_provider: icv_key = '-DCMVP_INTEGRITYKEY=\'\\"%s\\"\'' % self._args.hkey ret += ' %s' % icv_key ret += '"' return ret def _gen_lib_cmake(self, lib_name, inc_dirs, lib_obj, macros): lang = self._cfg_feature.libs[lib_name].get('lang', 'C') cmake = 'project({} {})\n\n'.format(lib_name, lang) cmake += self._gen_cmd_cmake('set', 'CMAKE_ASM_NASM_OBJECT_FORMAT elf64') cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', '${CC_ALL_OPTIONS}') cmake += self._gen_cmd_cmake('set', 'CMAKE_ASM_FLAGS', '${CC_ALL_OPTIONS}') cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', self._get_definitions()) cmake += self._gen_cmd_cmake('include_directories', '', inc_dirs) for _, mod_cmake in lib_obj['mods_cmake'].items(): cmake += mod_cmake tgt_obj_list = list('$<TARGET_OBJECTS:{}>'.format(x) for x in lib_obj['mods_cmake'].keys()) tgt_list = [] lib_type = self._cfg_custom_feature.lib_type if 'shared' in lib_type: cmake += self._gen_shared_lib_cmake(lib_name, tgt_obj_list, tgt_list, macros) if 'static' in lib_type: cmake += self._gen_static_lib_cmake(lib_name, tgt_obj_list, tgt_list) if 'object' in lib_type: cmake += self._gen_obejct_lib_cmake(lib_name, tgt_obj_list, tgt_list) lib_obj['cmake'] = cmake lib_obj['targets'] = tgt_list def _gen_exe_cmake(self, exe_name, inc_dirs, exe_obj): lang = self._cfg_feature.executes[exe_name].get('lang', 'C') definitions = '"${CMAKE_C_FLAGS} -DHITLS_VERSION=\'\\"%s\\"\' %s"' % ( self._args.hitls_version, '-D__FILENAME__=\'\\"$(notdir $(subst .o,,$@))\\"\'') cmake = 'project({} {})\n\n'.format(exe_name, lang) cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', '${CC_ALL_OPTIONS}') cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', definitions) cmake += self._gen_cmd_cmake('include_directories', '', inc_dirs) for _, mod_cmake in exe_obj['mods_cmake'].items(): cmake += mod_cmake tgt_obj_list = list('$<TARGET_OBJECTS:{}>'.format(x) for x in exe_obj['mods_cmake'].keys()) cmake += self._gen_cmd_cmake('add_executable', exe_name, tgt_obj_list) lib_type = self._cfg_custom_feature.lib_type if 'shared' in lib_type: cmake += self._gen_cmd_cmake('add_dependencies', exe_name, 'hitls_pki-shared hitls_crypto-shared hitls_bsl-shared') elif 'static' in lib_type: cmake += self._gen_cmd_cmake('add_dependencies', exe_name, 'hitls_pki-static hitls_crypto-static hitls_bsl-static') common_link_dir = [ '${CMAKE_CURRENT_LIST_DIR}', # libhitls_* '${CMAKE_SOURCE_DIR}/platform/Secure_C/lib', ] common_link_lib = [ 'hitls_pki', 'hitls_crypto', 'hitls_bsl', 'dl', 'pthread', 'm', str(self._args.securec_lib) ] cmake += self._gen_cmd_cmake('list', 'APPEND HITLS_APP_LINK_DIRS', common_link_dir) cmake += self._gen_cmd_cmake('list', 'APPEND HITLS_APP_LINK_LIBS', common_link_lib) cmake += self._gen_cmd_cmake('target_link_directories', '%s PRIVATE' % exe_name, '${HITLS_APP_LINK_DIRS}') cmake += self._gen_cmd_cmake('target_link_libraries', exe_name, '${HITLS_APP_LINK_LIBS}') cmake += self._gen_cmd_cmake('target_link_options', '{} PRIVATE'.format(exe_name), '${EXE_LNK_FLAGS}') cmake += 'install(TARGETS %s DESTINATION ${CMAKE_INSTALL_PREFIX})\n' % exe_name exe_obj['cmake'] = cmake exe_obj['targets'] = [exe_name] def _gen_bundled_lib_cmake(self, lib_name, inc_dirs, projects, macros): lang = 'C ASM' if 'mpa' in projects.keys(): lang += 'ASM_NASM' cmake = 'project({} {})\n\n'.format(lib_name, lang) cmake += self._gen_cmd_cmake('set', 'CMAKE_ASM_NASM_OBJECT_FORMAT elf64') cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', '${CC_ALL_OPTIONS}') cmake += self._gen_cmd_cmake('set', 'CMAKE_ASM_FLAGS', '${CC_ALL_OPTIONS}') cmake += self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', self._get_definitions()) cmake += self._gen_cmd_cmake('include_directories', '', inc_dirs) tgt_obj_list = [] for _, lib_obj in projects.items(): tgt_obj_list.extend(list('$<TARGET_OBJECTS:{}>'.format(x) for x in lib_obj['mods_cmake'].keys())) for _, mod_cmake in lib_obj['mods_cmake'].items(): cmake += mod_cmake tgt_list = [] lib_type = self._cfg_custom_feature.lib_type if 'shared' in lib_type: cmake += self._gen_shared_lib_cmake(lib_name, tgt_obj_list, tgt_list, macros) if 'static' in lib_type: cmake += self._gen_static_lib_cmake(lib_name, tgt_obj_list, tgt_list) if 'object' in lib_type: cmake += self._gen_obejct_lib_cmake(lib_name, tgt_obj_list, tgt_list) return {lib_name: {'cmake': cmake, 'targets': tgt_list}} def _gen_common_compile_c_flags(self): return self._gen_cmd_cmake('set', 'CMAKE_C_FLAGS', self._get_definitions()) def _gen_projects_cmake(self, macros): lib_enable_modules, exe_enable_modules = self._cfg_custom_feature.get_enable_modules() projects = {} all_inc_dirs = set() for lib, lib_obj in lib_enable_modules.items(): projects[lib] = {} projects[lib]['mods_cmake'] = {} inc_dirs = self._get_common_include(lib_obj.keys()) for mod, mod_obj in lib_obj.items(): self._gen_module_cmake(lib, mod, mod_obj, projects[lib]['mods_cmake']) if self._args.bundle_libs: all_inc_dirs = all_inc_dirs.union(inc_dirs) continue self._gen_lib_cmake(lib, inc_dirs, projects[lib], macros) if self._args.bundle_libs: # update projects projects = self._gen_bundled_lib_cmake('hitls', all_inc_dirs, projects, macros) for exe, exe_obj in exe_enable_modules.items(): projects[exe] = {} projects[exe]['mods_cmake'] = {} inc_dirs = self._get_common_include(exe_obj.keys()) for mod, mod_obj in exe_obj.items(): self._gen_module_cmake(exe, mod, mod_obj, projects[exe]['mods_cmake']) self._gen_exe_cmake(exe, inc_dirs, projects[exe]) return projects def _gen_target_cmake(self, lib_tgts): cmake = 'add_custom_target(openHiTLS)\n' cmake += self._gen_cmd_cmake('add_dependencies', 'openHiTLS', lib_tgts) return cmake def _gen_set_param_cmake(self, macro_file): compile_flags, link_flags = self._cfg_compile.union_options(self._cfg_custom_compile) macros = self._cfg_custom_feature.get_fea_macros() macros.sort() if self._args.no_config_check: macros.append('-DHITLS_NO_CONFIG_CHECK') if '-DHITLS_CRYPTO_CMVP_ISO19790' in compile_flags: self._approved_provider = True self._hmac = "sha256" elif '-DHITLS_CRYPTO_CMVP_SM' in compile_flags: self._approved_provider = True self._hmac = "sm3" compile_flags.extend(macros) hitls_macros = list(filter(lambda x: '-DHITLS' in x, compile_flags)) with open(macro_file, "w") as f: f.write(" ".join(hitls_macros)) f.close() self._cc_all_options = compile_flags compile_flags_str = '"{}"'.format(" ".join(compile_flags)) shared_link_flags = '{}'.format(" ".join(link_flags['SHARED']) + " " + " ".join(link_flags['PUBLIC'])) exe_link_flags = '{}'.format(" ".join(link_flags['EXE']) + " " + " ".join(link_flags['PUBLIC'])) cmake = self._gen_cmd_cmake('set', 'CC_ALL_OPTIONS', compile_flags_str) + "\n" cmake += self._gen_cmd_cmake('set', 'SHARED_LNK_FLAGS', shared_link_flags) + "\n" cmake += self._gen_cmd_cmake('set', 'EXE_LNK_FLAGS', exe_link_flags) + "\n" return cmake, macros def out_cmake(self, cmake_path, macro_file): self._cfg_custom_feature.check_bn_config() set_param_cmake, macros = self._gen_set_param_cmake(macro_file) set_param_cmake += self._gen_common_compile_c_flags() projects = self._gen_projects_cmake(macros) lib_tgts = list(tgt for lib_obj in projects.values() for tgt in lib_obj['targets']) bottom_cmake = self._gen_target_cmake(lib_tgts) with open(cmake_path, "w") as f: f.write(set_param_cmake) for lib_obj in projects.values(): f.write(lib_obj['cmake']) f.write('\n\n') f.write(bottom_cmake) def main(): os.chdir(srcdir) # The Python version cannot be earlier than 3.5. if sys.version_info < (3, 5): print("your python version %d.%d should not be lower than 3.5" % tuple(sys.version_info[:2])) raise Exception("your python version %d.%d should not be lower than 3.5" % tuple(sys.version_info[:2])) conf_feature = FeatureParser(Configure.feature_json_file) complete_options = CompleteOptionParser(Configure.complete_options_json_file) cfg = Configure(conf_feature) cfg.load_config_to_build() cfg.update_feature_config(cfg.args.module_cmake) cfg.update_compile_config(complete_options) if cfg.args.module_cmake: tmp_cmake = os.path.join(cfg.args.build_dir, 'modules.cmake') macro_file = os.path.join(cfg.args.build_dir, 'macro.txt') if (os.path.exists(macro_file)): os.remove(macro_file) CMakeGenerator(cfg.args, conf_feature, complete_options).out_cmake(tmp_cmake, macro_file) if __name__ == '__main__': try: main() except SystemExit: exit(0) except: traceback.print_exc() exit(2)
2302_82127028/openHiTLS-examples
configure.py
Python
unknown
33,691
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_AES_H #define CRYPT_AES_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include <stdint.h> #ifdef __cplusplus extern "C" { #endif // __cplusplus #define CRYPT_AES_128 128 #define CRYPT_AES_192 192 #define CRYPT_AES_256 256 #define CRYPT_AES_MAX_ROUNDS 14 #define CRYPT_AES_MAX_KEYLEN (4 * (CRYPT_AES_MAX_ROUNDS + 1)) /** * @ingroup CRYPT_AES_Key * * aes key structure */ typedef struct { uint32_t key[CRYPT_AES_MAX_KEYLEN]; uint32_t rounds; } CRYPT_AES_Key; /** * @ingroup aes * @brief Set the AES encryption key. * * @param ctx [IN] AES handle * @param key [IN] Encryption key * @param len [IN] Key length. The value must be 16 bytes. */ int32_t CRYPT_AES_SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief Set the AES encryption key. * * @param ctx [IN] AES handle * @param key [IN] Encryption key * @param len [IN] Key length. The value must be 24 bytes. */ int32_t CRYPT_AES_SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief Set the AES encryption key. * * @param ctx [IN] AES handle * @param key [IN] Encryption key * @param len [IN] Key length. The value must be 32 bytes. */ int32_t CRYPT_AES_SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief Set the AES decryption key. * * @param ctx [IN] AES handle * @param key [IN] Decryption key * @param len [IN] Key length. The value must be 16 bytes. */ int32_t CRYPT_AES_SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief Set the AES decryption key. * * @param ctx [IN] AES handle * @param key [IN] Decryption key * @param len [IN] Key length. The value must be 24 bytes. */ int32_t CRYPT_AES_SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief Set the AES decryption key. * * @param ctx [IN] AES handle * @param key [IN] Decryption key * @param len [IN] Key length. The value must be 32 bytes. */ int32_t CRYPT_AES_SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len); /** * @ingroup aes * @brief AES encryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input plaintext data. The value must be 16 bytes. * @param out [OUT] Output ciphertext data. The length is 16 bytes. * @param len [IN] Block length. */ int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); /** * @ingroup aes * @brief AES decryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input ciphertext data. The value must be 16 bytes. * @param out [OUT] Output plaintext data. The length is 16 bytes. * @param len [IN] Block length. The length is 16. */ int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #ifdef HITLS_CRYPTO_CBC /** * @ingroup aes * @brief AES cbc encryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input plaintext data, 16 bytes. * @param out [OUT] Output ciphertext data. The length is 16 bytes. * @param len [IN] Block length. * @param iv [IN] Initialization vector. */ int32_t CRYPT_AES_CBC_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); /** * @ingroup aes * @brief AES cbc decryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input ciphertext data. The value is 16 bytes. * @param out [OUT] Output plaintext data. The length is 16 bytes. * @param len [IN] Block length. * @param iv [IN] Initialization vector. */ int32_t CRYPT_AES_CBC_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif /* HITLS_CRYPTO_CBC */ #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) /** * @ingroup aes * @brief AES ctr encryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input plaintext data, 16 bytes. * @param out [OUT] Output ciphertext data. The length is 16 bytes. * @param len [IN] Block length. * @param iv [IN] Initialization vector. */ int32_t CRYPT_AES_CTR_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif #ifdef HITLS_CRYPTO_ECB /** * @ingroup aes * @brief AES ecb encryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input plaintext data. The length is a multiple of 16 bytes. * @param out [OUT] Output ciphertext data. The length is a multiple of 16 bytes. * @param len [IN] Block length. */ int32_t CRYPT_AES_ECB_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); /** * @ingroup aes * @brief AES ecb decryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input ciphertext data. The value is 16 bytes. * @param out [OUT] Output plaintext data. The length is 16 bytes. * @param len [IN] Block length. */ int32_t CRYPT_AES_ECB_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_CFB /** * @brief Decryption in CFB mode * * @param ctx [IN] Mode handle * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Data length * @param iv [IN] Initial vector * @return Success response: CRYPT_SUCCESS * Returned upon failure: Other error codes. */ int32_t CRYPT_AES_CFB_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif #ifdef HITLS_CRYPTO_XTS /** * @ingroup aes * @brief AES xts encryption * * @param ctx [IN] AES key * @param in [IN] Input plaintext. * @param out [OUT] Output ciphertext. * @param len [IN] Input length. The length is guaraenteed to be greater than block-size. * @param tweak [IN/OUT] XTS tweak. */ int32_t CRYPT_AES_XTS_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, const uint8_t *tweak); /** * @ingroup aes * @brief AES xts decryption * * @param ctx [IN] AES handle, storing keys * @param in [IN] Input ciphertext data. The value is 16 bytes. * @param out [OUT] Output plaintext data. The length is 16 bytes. * @param len [IN] Block length. * @param t [IN/OUT] XTS tweak. */ int32_t CRYPT_AES_XTS_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, const uint8_t *t); #endif /** * @ingroup aes * @brief Delete the AES key information. * * @param ctx [IN] AES handle, storing keys * @return void */ void CRYPT_AES_Clean(CRYPT_AES_Key *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_AES #endif // CRYPT_AES_H
2302_82127028/openHiTLS-examples
crypto/aes/include/crypt_aes.h
C
unknown
7,250
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "crypt_arm.h" #include "crypt_aes_macro_armv8.s" .file "crypt_aes_armv8.S" .text .arch armv8-a+crypto KEY .req x0 IN .req x1 OUT .req x2 ROUNDS .req w6 RDK0 .req v17 RDK1 .req v18 .section .rodata .align 5 .g_cron: .long 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 .align 5 /* * In Return-oriented programming (ROP) and Jump-oriented programming (JOP), we explored features * that Arm introduced to the Arm architecture to mitigate against JOP-style and ROP-style attacks. * ... * Whether the combined or NOP-compatible instructions are set depends on the architecture * version that the code is built for. When building for Armv8.3-A, or later, the compiler will use * the combined operations. When building for Armv8.2-A, or earlier, it will use the NOP compatible * instructions. * * The paciasp and autiasp instructions are used for function pointer authentication. * The pointer authentication feature is added in armv8.3 and is supported only by AArch64. * The addition of pointer authentication features is described in Section A2.6.1 of * DDI0487H_a_a-profile_architecture_reference_manual.pdf. */ /* * int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len); */ .text .globl CRYPT_AES_Encrypt .type CRYPT_AES_Encrypt, %function .align 5 CRYPT_AES_Encrypt: .ecb_aesenc_start: AARCH64_PACIASP stp x29, x30, [sp, #-16]! add x29, sp, #0 ld1 {BLK0.16b}, [IN] AES_ENC_1_BLK KEY BLK0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b}, [OUT] eor x0, x0, x0 eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b ldp x29, x30, [sp], #16 AARCH64_AUTIASP ret .size CRYPT_AES_Encrypt, .-CRYPT_AES_Encrypt /* * int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len); */ .globl CRYPT_AES_Decrypt .type CRYPT_AES_Decrypt, %function .align 5 CRYPT_AES_Decrypt: .ecb_aesdec_start: AARCH64_PACIASP stp x29, x30, [sp, #-16]! add x29, sp, #0 ld1 {BLK0.16b}, [IN] AES_DEC_1_BLK KEY BLK0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b}, [OUT] eor x0, x0, x0 eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b ldp x29, x30, [sp], #16 AARCH64_AUTIASP ret .size CRYPT_AES_Decrypt, .-CRYPT_AES_Decrypt /* * void SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); * Generating extended keys. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetEncryptKey128 .type SetEncryptKey128, %function .align 5 SetEncryptKey128: .Lenc_key_128: AARCH64_PACIASP stp x29, x30, [sp, #-64]! add x29, sp, #0 stp x25, x26, [sp, #16] stp x23, x24, [sp, #32] stp x21, x22, [sp, #48] // Register push stack completed. adrp x23, .g_cron add x23, x23, :lo12:.g_cron // Round key start address. mov x24, x0 // Copy key string address. The address increases by 16 bytes. ld1 {v1.16b}, [x1] // Reads the 16-byte key of a user. mov w26, #10 // Number of encryption rounds, which is filled // with rounds in the structure. st1 {v1.4s}, [x0], #16 // Save the first key. eor v0.16b, v0.16b, v0.16b // Clear zeros in V0. mov w25, #10 // loop for 10 times. .Lenc_key_128_loop: ldr w21, [x23], #4 // Obtains the round constant. dup v1.4s, v1.s[3] // Repeated four times,The last word of v1 is changed to v1 (128 bits). ld1 {v2.4s}, [x24], #16 // Obtains the 4 words used for XOR. ext v1.16b, v1.16b, v1.16b, #1 // Byte loop. dup v3.4s, w21 // Repeat four times to change w21 to v3 (128 bits). aese v1.16b, v0.16b // Xor then shift then sbox (XOR operation with 0 is itself, // equivalent to omitting the XOR operation). subs w25, w25, #1 // Count of 10-round key extension. eor v1.16b, v1.16b, v3.16b // Round constant XOR. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (1). ext v2.16b, v0.16b, v2.16b, #12 // 4321->3210. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (2). ext v2.16b, v0.16b, v2.16b, #12 // 3210->2100. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (3). ext v2.16b, v0.16b, v2.16b, #12 // 2100->1000. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (4). st1 {v1.4s}, [x0], #16 // Stores the newly calculated 4-bytes key data into the key string. b.ne .Lenc_key_128_loop // Loop jump. str w26, [x0, #64] // Fill in the number of rounds. eor x24, x24, x24 // Clear sensitivity. eor x0, x0, x0 ldp x21, x22, [sp, #48] ldp x23, x24, [sp, #32] ldp x25, x26, [sp, #16] ldp x29, x30, [sp], #64 // Pop stack completed. AARCH64_AUTIASP ret .size SetEncryptKey128, .-SetEncryptKey128 /* * void SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); * Set a decryption key string. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetDecryptKey128 .type SetDecryptKey128, %function .align 5 SetDecryptKey128: AARCH64_PACIASP stp x29, x30, [sp, #-0x40]! add x29, sp, #0 stp x25, x28, [sp, #0x10] // Register push stack completed. stp d8, d9, [sp, #0x20] stp d10, d11, [sp, #0x30] mov x28, x0 bl .Lenc_key_128 ld1 {v0.4s}, [x28], #16 SETDECKEY_LDR_9_BLOCK x28 ld1 {v10.4s}, [x28] mov x25, #-16 SETDECKEY_INVMIX_9_BLOCK st1 {v0.4s}, [x28], x25 SETDECKEY_STR_9_BLOCK x28, x25 st1 {v10.4s}, [x28] eor x28, x28, x28 eor x0, x0, x0 ldp d10, d11, [sp, #0x30] ldp d8, d9, [sp, #0x20] ldp x25, x28, [sp, #0x10] ldp x29, x30, [sp], #0x40 // Stacking completed. AARCH64_AUTIASP ret .size SetDecryptKey128, .-SetDecryptKey128 /* * void SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); * Generating extended keys. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetEncryptKey192 .type SetEncryptKey192, %function .align 5 SetEncryptKey192: .Lenc_key_192: AARCH64_PACIASP stp x29, x30, [sp, #-64]! add x29, sp, #0 stp x25, x26, [sp, #16] stp x23, x24, [sp, #32] stp x21, x22, [sp, #48] // Register push stack completed. mov x24, x0 // Copy key string address. The address increases by 16 bytes. ld1 {v0.16b}, [x1], #16 // Obtain the first 128-bit key. mov w26, #12 // Number of encryption rounds. st1 {v0.4s}, [x0], #16 // Store the first 128-bit key. ld1 {v1.8b}, [x1] // Obtains the last 64-bit key. adrp x23, .g_cron add x23, x23, :lo12:.g_cron // Round key start address. st1 {v1.2s}, [x0], #8 // Store the last 64-bit key. eor v0.16b, v0.16b, v0.16b // Clear zeros in V0. mov w25, #8 // loop for 8 times. .Lenc_key_192_loop: dup v1.4s, v1.s[1] // Repeated four times,The last word of v1 is changed to v1 (128 bits). subs w25, w25, #1 // Count of 8-round key extensions. ext v1.16b, v1.16b, v1.16b, #1 // Byte cycle. ldr w22, [x23], #4 // Obtains the round constant. aese v1.16b, v0.16b // Shift and sbox (XOR operation with 0 is itself,equivalent to omitting the XOR operation). dup v2.4s, w22 // Repeat 4 times. W22 becomes v2(128bit). eor v1.16b, v1.16b, v2.16b // Round constant XOR. ld1 {v2.4s}, [x24], #16 // Obtains the 4 words used for XOR eor v1.16b, v1.16b, v2.16b // 4 XOR operation (1). ext v2.16b, v0.16b, v2.16b, #12 // 4321->3210. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (2). ext v2.16b, v0.16b, v2.16b, #12 // 3210->2100. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (3). ext v2.16b, v0.16b, v2.16b, #12 // 2100->1000. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (4). st1 {v1.4s}, [x0], #16 // Stores the newly calculated 4-word key data into the key string. ld1 {v2.2s}, [x24], #8 // Loads 6 words for the last 2 words of XOR. dup v1.2s, v1.s[3] // Repeated two times,The last word of v1 is changed to v1 (64bit). eor v1.8b, v1.8b, v2.8b // 2 XOR operation (1). ext v2.8b, v0.8b, v2.8b, #4 // 21->10. eor v1.8b, v1.8b, v2.8b // 2 XOR operation (2). st1 {v1.2s}, [x0], #8 // Stores the newly calculated 2-word key data into the key string. b.ne .Lenc_key_192_loop // Loop jump. str w26, [x0, #24] // Fill in the number of rounds. eor x24, x24, x24 // Clear sensitivity. eor x0, x0, x0 ldp x21, x22, [sp, #48] ldp x23, x24, [sp, #32] ldp x25, x26, [sp, #16] ldp x29, x30, [sp], #64 // Stacking completed. AARCH64_AUTIASP ret .size SetEncryptKey192, .-SetEncryptKey192 /* * void SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); * Set a decryption key string. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetDecryptKey192 .type SetDecryptKey192, %function .align 5 SetDecryptKey192: AARCH64_PACIASP stp x29, x30, [sp, #-0x50]! add x29, sp, #0 stp x25, x28, [sp, #0x10] // Register is stacked. stp d8, d9, [sp, #0x20] // Register is stacked. stp d10, d11, [sp, #0x30] // Register is stacked. stp d12, d13, [sp, #0x40] // Register is stacked. mov x28, x0 bl .Lenc_key_192 mov x25, #-16 ld1 {v0.4s}, [x28], #16 SETDECKEY_LDR_9_BLOCK x28 ld1 {v10.4s}, [x28], #16 ld1 {v11.4s}, [x28], #16 ld1 {v12.4s}, [x28] SETDECKEY_INVMIX_9_BLOCK aesimc v10.16b, v10.16b aesimc v11.16b, v11.16b st1 {v0.4s}, [x28], x25 SETDECKEY_STR_9_BLOCK x28, x25 st1 {v10.4s}, [x28], x25 st1 {v11.4s}, [x28], x25 st1 {v12.4s}, [x28] eor x28, x28, x28 eor x0, x0, x0 ldp d12, d13, [sp, #0x40] ldp d10, d11, [sp, #0x30] ldp d8, d9, [sp, #0x20] ldp x25, x28, [sp, #0x10] ldp x29, x30, [sp], #0x50 // Stacking completed. AARCH64_AUTIASP ret .size SetDecryptKey192, .-SetDecryptKey192 /* * void SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); * Generating extended keys. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetEncryptKey256 .type SetEncryptKey256, %function .align 5 SetEncryptKey256: .Lenc_key_256: AARCH64_PACIASP stp x29, x30, [sp, #-64]! add x29, sp, #0 stp x25, x26, [sp, #16] stp x23, x24, [sp, #32] stp x21, x22, [sp, #48] // Register is stacked. adrp x23, .g_cron add x23, x23, :lo12:.g_cron // Round key start address. ld1 {v0.16b}, [x1], #16 // Obtain the first 128-bit key. mov x24, x0 // Copy key string address. The address increases by 16 bytes. st1 {v0.4s}, [x0], #16 // Store the first 128-bit key. ld1 {v1.16b}, [x1] // Obtain the last 128-bit key. eor v0.16b, v0.16b, v0.16b // Clear zeros in V0. st1 {v1.4s}, [x0], #16 // Store the last 128-bit key. mov w26, #14 // Number of encryption rounds. mov w25, #6 // Loop for 7-1 times. .Lenc_key_256_loop: dup v1.4s, v1.s[3] // Repeated four times,The last word of v1 is changed to v1 (128 bits). ldr w22, [x23], #4 // Obtains the round constant. ext v1.16b, v1.16b, v1.16b, #1 // Byte cycle. aese v1.16b, v0.16b // XOR then shift then sbox (XOR operation with 0 is itself, // equivalent to omitting the XOR operation). dup v2.4s, w22 // Repeat 4 times. w22 becomes v2. eor v1.16b, v1.16b, v2.16b // Round constant XOR. ld1 {v2.4s}, [x24], #16 // Obtains the 4 words used for XOR. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (1). ext v2.16b, v0.16b, v2.16b, #12 // 4321->3210. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (2). ext v2.16b, v0.16b, v2.16b, #12 // 3210->2100. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (3). ext v2.16b, v0.16b, v2.16b, #12 // 2100->1000. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (4). st1 {v1.4s}, [x0], #16 // Stores the newly calculated 4-word key data into the key string. subs w25, w25, #1 // Count of 7-1-round key extensions. dup v1.4s, v1.s[3] // Repeated four times,The last word of v1 is changed to v1 (128 bits). ld1 {v2.4s}, [x24], #16 // Obtains the 4 words used for XOR. aese v1.16b, v0.16b // XOR then shift then sbox. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (1). ext v2.16b, v0.16b, v2.16b, #12 // 4321->3210. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (2). ext v2.16b, v0.16b, v2.16b, #12 // 3210->2100. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (3). ext v2.16b, v0.16b, v2.16b, #12 // 2100->1000. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (4). st1 {v1.4s}, [x0], #16 // Stores the newly calculated 4-word key data into the key string. b.ne .Lenc_key_256_loop // Loop jump. dup v1.4s, v1.s[3] // Repeated four times,The last word of v1 is changed to v1 (128 bits). ldr w22, [x23], #4 // Obtains the round constant. ext v1.16b, v1.16b, v1.16b, #1 // Byte cycle. aese v1.16b, v0.16b // XOR then shift then sbox. dup v2.4s, w22 // Repeat 4 times. w22 becomes v2(128bit). eor v1.16b, v1.16b, v2.16b // Round constant XOR. ld1 {v2.4s}, [x24], #16 // Obtains the 4 words used for XOR. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (1). ext v2.16b, v0.16b, v2.16b, #12 // 4321->3210. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (2). ext v2.16b, v0.16b, v2.16b, #12 // 3210->2100. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (3). ext v2.16b, v0.16b, v2.16b, #12 // 2100->1000. eor v1.16b, v1.16b, v2.16b // 4 XOR operation (4). st1 {v1.4s}, [x0], #16 // Stores the newly calculated 4-word key data into the key string. str w26, [x0] // Fill in the number of rounds. eor x24, x24, x24 // Clear sensitivity. eor x0, x0, x0 ldp x21, x22, [sp, #48] ldp x23, x24, [sp, #32] ldp x25, x26, [sp, #16] ldp x29, x30, [sp], #64 // Stacking completed. AARCH64_AUTIASP ret .size SetEncryptKey256, .-SetEncryptKey256 /* * void SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); * Set a decryption key string. * x0 => CRYPT_AES_Key *ctx; x1 => const uint8_t *key */ .globl SetDecryptKey256 .type SetDecryptKey256, %function .align 5 SetDecryptKey256: AARCH64_PACIASP stp x29, x30, [sp, #-0x60]! add x29, sp, #0 stp x25, x28, [sp, #0x10] stp d8, d9, [sp, #0x20] stp d10, d11, [sp, #0x30] stp d12, d13, [sp, #0x40] stp d14, d15, [sp, #0x50] mov x28, x0 bl .Lenc_key_256 mov x25, #-16 ld1 {v0.4s}, [x28], #16 SETDECKEY_LDR_9_BLOCK x28 ld1 {v10.4s}, [x28], #16 ld1 {v11.4s}, [x28], #16 ld1 {v12.4s}, [x28], #16 ld1 {v13.4s}, [x28], #16 ld1 {v14.4s}, [x28] SETDECKEY_INVMIX_9_BLOCK aesimc v10.16b, v10.16b aesimc v11.16b, v11.16b aesimc v12.16b, v12.16b aesimc v13.16b, v13.16b st1 {v0.4s}, [x28], x25 SETDECKEY_STR_9_BLOCK x28, x25 st1 {v10.4s}, [x28], x25 st1 {v11.4s}, [x28], x25 st1 {v12.4s}, [x28], x25 st1 {v13.4s}, [x28], x25 st1 {v14.4s}, [x28] eor x28, x28, x28 eor x0, x0, x0 ldp d14, d15, [sp, #0x50] ldp d12, d13, [sp, #0x40] ldp d10, d11, [sp, #0x30] ldp d8, d9, [sp, #0x20] ldp x25, x28, [sp, #0x10] ldp x29, x30, [sp], #0x60 // Stack has been popped. AARCH64_AUTIASP ret .size SetDecryptKey256, .-SetDecryptKey256 #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_armv8.S
Unix Assembly
unknown
17,570
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CBC) #include "crypt_arm.h" #include "crypt_aes_macro_armv8.s" .file "crypt_aes_cbc_armv8.S" .text .arch armv8-a+crypto KEY .req x0 IN .req x1 OUT .req x2 LEN .req x3 P_IV .req x4 KTMP .req x5 ROUNDS .req w6 BLK0 .req v0 BLK1 .req v1 BLK2 .req v2 BLK3 .req v3 BLK4 .req v4 BLK5 .req v5 BLK6 .req v6 BLK7 .req v7 KEY0_END .req v16 KEY0 .req v17 KEY1 .req v18 KEY2 .req v19 KEY3 .req v20 KEY4 .req v21 KEY5 .req v22 KEY6 .req v23 KEY7 .req v24 KEY8 .req v25 KEY9 .req v26 KEY10 .req v27 KEY11 .req v28 KEY12 .req v29 KEY13 .req v30 KEY14 .req v31 IVENC .req v1 IV0 .req v17 IV1 .req v18 IV2 .req v19 IV3 .req v20 IV4 .req v21 IV5 .req v22 IV6 .req v23 IV7 .req v24 IVT .req v25 RDK0 .req v26 RDK1 .req v27 RDK2 .req v28 /* * One round of encryption process. * block:input the plaintext. * key: One round key. */ .macro ROUND block, key aese \block, \key aesmc \block, \block .endm /* * Eight blocks of decryption. * block0_7:Input the ciphertext. * rdk0: Round key. * ktmp: Temporarily stores pointers to keys. */ .macro DEC8 rdk0s rdk0 blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 ktmp aesd \blk0, \rdk0 aesimc \blk0, \blk0 aesd \blk5, \rdk0 aesimc \blk5, \blk5 aesd \blk1, \rdk0 aesimc \blk1, \blk1 aesd \blk6, \rdk0 aesimc \blk6, \blk6 aesd \blk2, \rdk0 aesimc \blk2, \blk2 aesd \blk3, \rdk0 aesimc \blk3, \blk3 aesd \blk4, \rdk0 aesimc \blk4, \blk4 aesd \blk7, \rdk0 aesimc \blk7, \blk7 ld1 {\rdk0s}, [\ktmp], #16 .endm /** * Function description: AES encrypted assembly acceleration API in CBC mode. * int32_t CRYPT_AES_CBC_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); * Input register: * x0:Pointer to the input key structure * x1:points to the input data address * x2:points to the output data address * x3:Length of the input data, which must be a multiple of 16 * x4:Points to the CBC mode mask address * Change register:x5, x6, v0-v31 * Output register:x0 * Function/Macro Call: None */ .globl CRYPT_AES_CBC_Encrypt .type CRYPT_AES_CBC_Encrypt, %function CRYPT_AES_CBC_Encrypt: AARCH64_PACIASP ld1 {IVENC.16b}, [P_IV] // load IV ldr w6, [KEY, #240] // load rounds ld1 {BLK0.16b}, [IN], #16 // load in ld1 {KEY0.4s, KEY1.4s}, [KEY], #32 // load keys cmp w6, #12 ld1 {KEY2.4s, KEY3.4s}, [KEY], #32 ld1 {KEY4.4s, KEY5.4s}, [KEY], #32 ld1 {KEY6.4s, KEY7.4s}, [KEY], #32 ld1 {KEY8.4s, KEY9.4s}, [KEY], #32 eor IVENC.16b, IVENC.16b, BLK0.16b // iv + in b.lt .Laes_cbc_128_start ld1 {KEY10.4s, KEY11.4s}, [KEY], #32 b.eq .Laes_cbc_192_start ld1 {KEY12.4s, KEY13.4s}, [KEY], #32 .Laes_cbc_256_start: ld1 {KEY14.4s}, [KEY] ROUND IVENC.16b, KEY0.16b eor KEY0_END.16b, KEY0.16b, KEY14.16b // key0 + keyEnd b .Laes_cbc_256_round_loop .Laes_cbc_256_loop: ROUND IVENC.16b, KEY0.16b st1 {BLK0.16b}, [OUT], #16 .Laes_cbc_256_round_loop: ROUND IVENC.16b, KEY1.16b ROUND IVENC.16b, KEY2.16b subs LEN, LEN, #16 ROUND IVENC.16b, KEY3.16b ROUND IVENC.16b, KEY4.16b ROUND IVENC.16b, KEY5.16b ld1 {KEY0.16b}, [IN], #16 // load IN ROUND IVENC.16b, KEY6.16b ROUND IVENC.16b, KEY7.16b ROUND IVENC.16b, KEY8.16b ROUND IVENC.16b, KEY9.16b ROUND IVENC.16b, KEY10.16b ROUND IVENC.16b, KEY11.16b ROUND IVENC.16b, KEY12.16b aese IVENC.16b, KEY13.16b eor KEY0.16b, KEY0.16b, KEY0_END.16b // IN + KEY0 + KEYEND eor BLK0.16b, IVENC.16b, KEY14.16b b.gt .Laes_cbc_256_loop b .Lescbcenc_finish .Laes_cbc_128_start: ld1 {KEY10.4s}, [KEY] ROUND IVENC.16b, KEY0.16b eor KEY0_END.16b, KEY0.16b, KEY10.16b // key0 + keyEnd b .Laes_cbc_128_round_loop .Laes_cbc_128_loop: ROUND IVENC.16b, KEY0.16b st1 {BLK0.16b}, [OUT], #16 .Laes_cbc_128_round_loop: ROUND IVENC.16b, KEY1.16b ROUND IVENC.16b, KEY2.16b subs LEN, LEN, #16 ROUND IVENC.16b, KEY3.16b ROUND IVENC.16b, KEY4.16b ROUND IVENC.16b, KEY5.16b ld1 {KEY0.16b}, [IN], #16 // load IN ROUND IVENC.16b, KEY6.16b ROUND IVENC.16b, KEY7.16b ROUND IVENC.16b, KEY8.16b aese IVENC.16b, KEY9.16b eor KEY0.16b, KEY0.16b, KEY0_END.16b // IN + KEY0 + KEYEND eor BLK0.16b, IVENC.16b, KEY10.16b // enc OK b.gt .Laes_cbc_128_loop b .Lescbcenc_finish .Laes_cbc_192_start: ld1 {KEY12.4s}, [KEY] ROUND IVENC.16b, KEY0.16b eor KEY0_END.16b, KEY0.16b, KEY12.16b // key0 + keyEnd b .Laes_cbc_192_round_loop .Laes_cbc_192_loop: ROUND IVENC.16b, KEY0.16b st1 {BLK0.16b}, [OUT], #16 .Laes_cbc_192_round_loop: ROUND IVENC.16b, KEY1.16b ROUND IVENC.16b, KEY2.16b subs LEN, LEN, #16 ROUND IVENC.16b, KEY3.16b ROUND IVENC.16b, KEY4.16b ROUND IVENC.16b, KEY5.16b ld1 {KEY0.16b}, [IN], #16 // load IN ROUND IVENC.16b, KEY6.16b ROUND IVENC.16b, KEY7.16b ROUND IVENC.16b, KEY8.16b ROUND IVENC.16b, KEY9.16b ROUND IVENC.16b, KEY10.16b aese IVENC.16b, KEY11.16b eor KEY0.16b, KEY0.16b, KEY0_END.16b // IN + KEY0 + KEYEND eor BLK0.16b, IVENC.16b, KEY12.16b b.gt .Laes_cbc_192_loop .Lescbcenc_finish: st1 {BLK0.16b}, [OUT], #16 st1 {BLK0.16b}, [P_IV] mov x0, #0 AARCH64_AUTIASP ret .size CRYPT_AES_CBC_Encrypt, .-CRYPT_AES_CBC_Encrypt /** * Function description: AES decryption and assembly acceleration API in CBC mode. * int32_t CRYPT_AES_CBC_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); * Input register: * x0:pointer to the input key structure * x1:points to the input data address * x2:points to the output data address * x3:Length of the input data, which must be a multiple of 16 * x4:Points to the CBC mode mask address * Change register:x5, x6, v0-v31 * Output register:x0 * Function/Macro Call: AES_DEC_8_BLKS, AES_DEC_1_BLK, AES_DEC_2_BLKS, AES_DEC_3_BLKS, * AES_DEC_4_BLKS, AES_DEC_5_BLKS, AES_DEC_6_BLKS, AES_DEC_7_BLKS */ .globl CRYPT_AES_CBC_Decrypt .type CRYPT_AES_CBC_Decrypt, %function CRYPT_AES_CBC_Decrypt: AARCH64_PACIASP ld1 {IV0.16b}, [P_IV] .Lcbc_aesdec_start: cmp LEN, #64 b.ge .Lcbc_dec_above_equal_4_blks cmp LEN, #32 b.ge .Lcbc_dec_above_equal_2_blks cmp LEN, #0 b.eq .Lcbc_aesdec_finish b .Lcbc_dec_proc_1_blk .Lcbc_dec_above_equal_2_blks: cmp LEN, #48 b.lt .Lcbc_dec_proc_2_blks b .Lcbc_dec_proc_3_blks .Lcbc_dec_above_equal_4_blks: cmp LEN, #96 b.ge .Lcbc_dec_above_equal_6_blks cmp LEN, #80 b.lt .Lcbc_dec_proc_4_blks b .Lcbc_dec_proc_5_blks .Lcbc_dec_above_equal_6_blks: cmp LEN, #112 b.lt .Lcbc_dec_proc_6_blks cmp LEN, #128 b.lt .Lcbc_dec_proc_7_blks .align 4 .Lcbc_aesdec_8_blks_loop: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 mov KTMP, KEY ldr ROUNDS, [KEY, #240] ld1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [IN], #64 mov IV1.16b, BLK0.16b mov IV2.16b, BLK1.16b mov IV3.16b, BLK2.16b ld1 {RDK0.4s, RDK1.4s}, [KTMP], #32 mov IV4.16b, BLK3.16b mov IV5.16b, BLK4.16b mov IV6.16b, BLK5.16b mov IV7.16b, BLK6.16b mov IVT.16b, BLK7.16b DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP cmp ROUNDS, #12 b.lt .Ldec_8_blks_last DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP b.eq .Ldec_8_blks_last DEC8 RDK0.4s, RDK0.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP DEC8 RDK1.4s, RDK1.16b, BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b, BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b, KTMP .Ldec_8_blks_last: ld1 {RDK2.4s}, [KTMP] aesd BLK0.16b, RDK0.16b aesimc BLK0.16b, BLK0.16b aesd BLK1.16b, RDK0.16b aesimc BLK1.16b, BLK1.16b aesd BLK2.16b, RDK0.16b aesimc BLK2.16b, BLK2.16b eor IV0.16b, IV0.16b, RDK2.16b aesd BLK3.16b, RDK0.16b aesimc BLK3.16b, BLK3.16b eor IV1.16b, IV1.16b, RDK2.16b aesd BLK4.16b, RDK0.16b aesimc BLK4.16b, BLK4.16b eor IV2.16b, IV2.16b, RDK2.16b aesd BLK5.16b, RDK0.16b aesimc BLK5.16b, BLK5.16b eor IV3.16b, IV3.16b, RDK2.16b aesd BLK6.16b, RDK0.16b aesimc BLK6.16b, BLK6.16b eor IV4.16b, IV4.16b, RDK2.16b aesd BLK7.16b, RDK0.16b aesimc BLK7.16b, BLK7.16b eor IV5.16b, IV5.16b, RDK2.16b aesd BLK0.16b, RDK1.16b aesd BLK1.16b, RDK1.16b eor IV6.16b, IV6.16b, RDK2.16b aesd BLK2.16b, RDK1.16b aesd BLK3.16b, RDK1.16b eor IV7.16b, IV7.16b, RDK2.16b aesd BLK4.16b, RDK1.16b aesd BLK5.16b, RDK1.16b aesd BLK6.16b, RDK1.16b aesd BLK7.16b, RDK1.16b sub LEN, LEN, #128 eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b eor BLK3.16b, BLK3.16b, IV3.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 eor BLK4.16b, BLK4.16b, IV4.16b eor BLK5.16b, BLK5.16b, IV5.16b cmp LEN, #0 eor BLK6.16b, BLK6.16b, IV6.16b eor BLK7.16b, BLK7.16b, IV7.16b mov IV0.16b, IVT.16b st1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [OUT], #64 b.eq .Lcbc_aesdec_finish cmp LEN, #128 b.lt .Lcbc_aesdec_start b .Lcbc_aesdec_8_blks_loop .Lcbc_dec_proc_1_blk: ld1 {BLK0.16b}, [IN] AES_DEC_1_BLK KEY BLK0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN] ld1 {IV1.16b}, [IN], #16 AES_DEC_2_BLKS KEY BLK0.16b BLK1.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_3_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b}, [IN] ld1 {IV1.16b, IV2.16b}, [IN], #32 AES_DEC_3_BLKS KEY BLK0.16b BLK1.16b BLK2.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_4_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] ld1 {IV1.16b, IV2.16b, IV3.16b}, [IN], #48 AES_DEC_4_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b eor BLK3.16b, BLK3.16b, IV3.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_5_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] ld1 {IV1.16b, IV2.16b, IV3.16b, IV4.16b}, [IN], #64 ld1 {BLK4.16b}, [IN] AES_DEC_5_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b eor BLK3.16b, BLK3.16b, IV3.16b eor BLK4.16b, BLK4.16b, IV4.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_6_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] ld1 {IV1.16b, IV2.16b, IV3.16b, IV4.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b}, [IN] ld1 {IV5.16b}, [IN], #16 AES_DEC_6_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b eor BLK3.16b, BLK3.16b, IV3.16b eor BLK4.16b, BLK4.16b, IV4.16b eor BLK5.16b, BLK5.16b, IV5.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b}, [OUT] b .Lcbc_aesdec_finish .Lcbc_dec_proc_7_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] ld1 {IV1.16b, IV2.16b, IV3.16b, IV4.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b}, [IN] ld1 {IV5.16b, IV6.16b}, [IN], #32 AES_DEC_7_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b BLK6.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS eor BLK0.16b, BLK0.16b, IV0.16b eor BLK1.16b, BLK1.16b, IV1.16b eor BLK2.16b, BLK2.16b, IV2.16b eor BLK3.16b, BLK3.16b, IV3.16b eor BLK4.16b, BLK4.16b, IV4.16b eor BLK5.16b, BLK5.16b, IV5.16b eor BLK6.16b, BLK6.16b, IV6.16b ld1 {IV0.16b}, [IN] st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b}, [OUT] .Lcbc_aesdec_finish: st1 {IV0.16b}, [P_IV] mov x0, #0 eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b eor RDK2.16b, RDK2.16b, RDK2.16b AARCH64_AUTIASP ret .size CRYPT_AES_CBC_Decrypt, .-CRYPT_AES_CBC_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_cbc_armv8.S
Unix Assembly
unknown
15,260
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CBC) #include "crypt_aes_macro_x86_64.s" .file "crypt_aes_cbc_x86_64.S" .text .set ARG1, %rdi .set ARG2, %rsi .set ARG3, %rdx .set ARG4, %ecx .set ARG5, %r8 .set ARG6, %r9 .set RDK, %xmm3 .set KEY, %rdi .set KTMP, %r9 .set ROUNDS, %eax .set RET, %eax .set BLK0, %xmm1 .set BLK1, %xmm4 .set BLK2, %xmm5 .set BLK3, %xmm6 .set BLK4, %xmm10 .set BLK5, %xmm11 .set BLK6, %xmm12 .set BLK7, %xmm13 .set IV0, %xmm0 .set IV1, %xmm7 .set IV2, %xmm8 .set IV3, %xmm9 .set KEY1, %xmm4 .set KEY2, %xmm5 .set KEY3, %xmm6 .set KEY4, %xmm10 .set KEY5, %xmm11 .set KEY6, %xmm12 .set KEY7, %xmm13 .set KEY8, %xmm14 .set KEY9, %xmm15 .set KEY10, %xmm2 .set KEY11, %xmm7 .set KEY12, %xmm8 .set KEY13, %xmm9 .set KEYTEMP, %xmm3 /** * Function description:AES encrypted assembly acceleration API in CBC mode. * Function prototype:int32_t CRYPT_AES_CBC_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); * Input register: * rdi:pointer to the input key structure * rsi:points to the input data address * rdx:points to the output data address * rcx:Length of the input data, which must be a multiple of 16 * r8: Points to the CBC mode mask address * Change register:xmm0-xmm15 * Output register:eax * Function/Macro Call: None */ .globl CRYPT_AES_CBC_Encrypt .type CRYPT_AES_CBC_Encrypt, @function CRYPT_AES_CBC_Encrypt: .cfi_startproc .align 16 cmpl $16, ARG4 jb .Laescbcend_end movl 240(KEY), ROUNDS vmovdqu (ARG5), IV0 vmovdqu (KEY), KEY1 vmovdqu 16(KEY), KEY2 vmovdqu 32(KEY), KEY3 vmovdqu 48(KEY), KEY4 vmovdqu 64(KEY), KEY5 vmovdqu 80(KEY), KEY6 vmovdqu 96(KEY), KEY7 vmovdqu 112(KEY), KEY8 vmovdqu 128(KEY), KEY9 vmovdqu 144(KEY), KEY10 vmovdqu 160(KEY), KEY11 cmpl $12, ROUNDS jb .Laes_128_cbc_start je .Laes_192_cbc_start .align 16 .Laes_256_cbc_start: vmovdqu 176(KEY), KEY12 vmovdqu 192(KEY), KEY13 .Laes_256_cbc_loop: vpxor (ARG2), IV0, BLK0 vmovdqu 208(KEY), KEYTEMP vpxor BLK0, KEY1, BLK0 aesenc KEY2, BLK0 aesenc KEY3, BLK0 aesenc KEY4, BLK0 aesenc KEY5, BLK0 aesenc KEY6, BLK0 aesenc KEY7, BLK0 aesenc KEY8, BLK0 aesenc KEY9, BLK0 aesenc KEY10, BLK0 aesenc KEY11, BLK0 aesenc KEY12, BLK0 aesenc KEY13, BLK0 aesenc KEYTEMP, BLK0 vmovdqu 224(KEY), KEYTEMP aesenclast KEYTEMP, BLK0 leaq 16(ARG2), ARG2 vmovdqu BLK0, (ARG3) movdqa BLK0, IV0 leaq 16(ARG3), ARG3 subl $16, ARG4 cmpl $16, ARG4 jnb .Laes_256_cbc_loop // Special value processing vpxor KEY12, KEY12, KEY12 vpxor KEY13, KEY13, KEY13 vpxor KEYTEMP, KEYTEMP, KEYTEMP jmp .Laescbcenc_finish .align 16 .Laes_192_cbc_start: vmovdqu 176(KEY), KEY12 vmovdqu 192(KEY), KEY13 .Laes_192_cbc_loop: vpxor (ARG2), IV0, BLK0 vpxor BLK0, KEY1, BLK0 aesenc KEY2, BLK0 aesenc KEY3, BLK0 aesenc KEY4, BLK0 aesenc KEY5, BLK0 aesenc KEY6, BLK0 aesenc KEY7, BLK0 aesenc KEY8, BLK0 aesenc KEY9, BLK0 aesenc KEY10, BLK0 aesenc KEY11, BLK0 aesenc KEY12, BLK0 aesenclast KEY13, BLK0 leaq 16(ARG2), ARG2 vmovdqu BLK0, (ARG3) movdqa BLK0, IV0 leaq 16(ARG3), ARG3 subl $16 , ARG4 jnz .Laes_192_cbc_loop vpxor KEY12, KEY12, KEY12 vpxor KEY13, KEY13, KEY13 jmp .Laescbcenc_finish .align 16 .Laes_128_cbc_start: vpxor (ARG2), IV0, BLK0 vpxor BLK0, KEY1, BLK0 aesenc KEY2, BLK0 aesenc KEY3, BLK0 aesenc KEY4, BLK0 aesenc KEY5, BLK0 aesenc KEY6, BLK0 aesenc KEY7, BLK0 aesenc KEY8, BLK0 aesenc KEY9, BLK0 aesenc KEY10, BLK0 aesenclast KEY11, BLK0 leaq 16(ARG2), ARG2 vmovdqu BLK0, (ARG3) movdqa BLK0, IV0 leaq 16(ARG3), ARG3 subl $16, ARG4 jnz .Laes_128_cbc_start jmp .Laescbcenc_finish .Laescbcenc_finish: vmovdqu BLK0,(ARG5) vpxor KEY1, KEY1, KEY1 vpxor KEY2, KEY2, KEY2 vpxor KEY3, KEY3, KEY3 vpxor KEY4, KEY4, KEY4 vpxor KEY5, KEY5, KEY5 vpxor KEY6, KEY6, KEY6 vpxor KEY7, KEY7, KEY7 vpxor KEY8, KEY8, KEY8 vpxor KEY9, KEY9, KEY9 vpxor KEY10, KEY10, KEY10 vpxor KEY11, KEY11, KEY11 .Laescbcend_end: movl $0, RET ret .cfi_endproc .size CRYPT_AES_CBC_Encrypt, .-CRYPT_AES_CBC_Encrypt /** * Function description: Sets the AES decryption and assembly accelerated implementation interface in CBC mode * Function prototype:int32_t CRYPT_AES_CBC_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); * Input register: * rdi:pointer to the input key structure * rsi:points to the input data address. * rdx:points to the output data address. * rcx:Length of the input data, which must be a multiple of 16 * r8: Points to the CBC mode mask address * Change register:xmm0-xmm13 * Output register:eax * Function/Macro Call: None */ .globl CRYPT_AES_CBC_Decrypt .type CRYPT_AES_CBC_Decrypt, @function CRYPT_AES_CBC_Decrypt: .cfi_startproc .align 16 vmovdqu (ARG5), IV0 .Laes_cbc_dec_start: cmpl $64, ARG4 jae .Labove_equal_4_blks cmpl $32, ARG4 jae .Labove_equal_2_blks cmpl $0, ARG4 je .Laes_cbc_dec_finish jmp .Lproc_1_blk .Labove_equal_2_blks: cmpl $48, ARG4 jb .Lproc_2_blks jmp .Lproc_3_blks .Labove_equal_4_blks: cmpl $96, ARG4 jae .Labove_equal_6_blks cmpl $80, ARG4 jb .Lproc_4_blks jmp .Lproc_5_blks .Labove_equal_6_blks: cmpl $112, ARG4 jb .Lproc_6_blks cmpl $128, ARG4 jb .Lproc_7_blks .align 16 .Lproc_8_blks: .Laescbcdec_8_blks_loop: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 vmovdqu 32(ARG2), BLK2 movdqa BLK0, IV1 movdqa BLK1, IV2 movdqa BLK2, IV3 movq KEY, KTMP movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor BLK0, RDK, BLK0 vpxor BLK1, RDK, BLK1 vpxor BLK2, RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 vpxor 112(ARG2), RDK, BLK7 decl ROUNDS AES_DEC_8_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 BLK7 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vpxor BLK3, IV3, BLK3 vpxor 48(ARG2), BLK4, BLK4 vpxor 64(ARG2), BLK5, BLK5 vpxor 80(ARG2), BLK6, BLK6 vpxor 96(ARG2), BLK7, BLK7 vmovdqu 112(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) vmovdqu BLK7, 112(ARG3) subl $128, ARG4 leaq 128(ARG2), ARG2 leaq 128(ARG3), ARG3 cmpl $128, ARG4 jb .Laes_cbc_dec_start jmp .Laescbcdec_8_blks_loop .align 16 .Lproc_1_blk: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 decl ROUNDS AES_DEC_1_BLK KEY ROUNDS RDK BLK0 vpxor BLK0, IV0, BLK0 vmovdqu (ARG2), IV0 vmovdqu BLK0, (ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_2_blks: vmovdqu (ARG2), BLK0 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 vpxor BLK0, RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 decl ROUNDS AES_DEC_2_BLKS KEY ROUNDS RDK BLK0 BLK1 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vmovdqu 16(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_3_blks: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 movdqa BLK1, IV2 vpxor BLK0, RDK, BLK0 vpxor BLK1, RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 decl ROUNDS AES_DEC_3_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vmovdqu 32(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_4_blks: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 vmovdqu 32(ARG2), BLK2 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 movdqa BLK1, IV2 movdqa BLK2, IV3 vpxor BLK0, RDK, BLK0 vpxor BLK1, RDK, BLK1 vpxor BLK2, RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 decl ROUNDS AES_DEC_4_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vpxor BLK3, IV3, BLK3 vmovdqu 48(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_5_blks: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 vmovdqu 32(ARG2), BLK2 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 movdqa BLK1, IV2 movdqa BLK2, IV3 vpxor BLK0, RDK, BLK0 vpxor BLK1, RDK, BLK1 vpxor BLK2, RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 decl ROUNDS AES_DEC_5_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vpxor BLK3, IV3, BLK3 vpxor 48(ARG2), BLK4, BLK4 vmovdqu 64(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_6_blks: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 vmovdqu 32(ARG2), BLK2 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 movdqa BLK1, IV2 movdqa BLK2, IV3 vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 decl ROUNDS AES_DEC_6_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vpxor BLK3, IV3, BLK3 vpxor 48(ARG2), BLK4, BLK4 vpxor 64(ARG2), BLK5, BLK5 vmovdqu 80(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) jmp .Laes_cbc_dec_finish .align 16 .Lproc_7_blks: vmovdqu (ARG2), BLK0 vmovdqu 16(ARG2), BLK1 vmovdqu 32(ARG2), BLK2 movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movdqa BLK0, IV1 movdqa BLK1, IV2 movdqa BLK2, IV3 vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 decl ROUNDS AES_DEC_7_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 vpxor BLK0, IV0, BLK0 vpxor BLK1, IV1, BLK1 vpxor BLK2, IV2, BLK2 vpxor BLK3, IV3, BLK3 vpxor 48(ARG2), BLK4, BLK4 vpxor 64(ARG2), BLK5, BLK5 vpxor 80(ARG2), BLK6, BLK6 vmovdqu 96(ARG2), IV0 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) .align 16 .Laes_cbc_dec_finish: vmovdqu IV0, (ARG5) vpxor BLK0, BLK0, BLK0 vpxor BLK1, BLK1, BLK1 vpxor BLK2, BLK2, BLK2 vpxor BLK3, BLK3, BLK3 vpxor BLK4, BLK4, BLK4 vpxor BLK5, BLK5, BLK5 vpxor BLK6, BLK6, BLK6 vpxor BLK7, BLK7, BLK7 vpxor RDK, RDK, RDK movl $0, RET ret .cfi_endproc .size CRYPT_AES_CBC_Decrypt, .-CRYPT_AES_CBC_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_cbc_x86_64.S
Unix Assembly
unknown
12,757
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CFB) #include "crypt_arm.h" #include "crypt_aes_macro_armv8.s" .file "crypt_aes_cfb_armv8.S" .text .arch armv8-a+crypto .align 5 KEY .req x0 IN .req x1 OUT .req x2 LEN .req x3 IV .req x4 LTMP .req x12 IVC .req v19 CT1 .req v20 CT2 .req v21 CT3 .req v22 CT4 .req v23 CT5 .req v24 CT6 .req v25 CT7 .req v26 CT8 .req v27 BLK0 .req v0 BLK1 .req v1 BLK2 .req v2 BLK3 .req v3 BLK4 .req v4 BLK5 .req v5 BLK6 .req v6 BLK7 .req v7 RDK0 .req v17 RDK1 .req v18 ROUNDS .req w6 /* * int32_t CRYPT_AES_CFB_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); */ .globl CRYPT_AES_CFB_Decrypt .type CRYPT_AES_CFB_Decrypt, %function CRYPT_AES_CFB_Decrypt: AARCH64_PACIASP ld1 {IVC.16b}, [IV] // Load the IV mov LTMP, LEN .Lcfb_aesdec_start: cmp LTMP, #64 b.ge .Lcfb_dec_above_equal_4_blks cmp LTMP, #32 b.ge .Lcfb_dec_above_equal_2_blks cmp LTMP, #0 b.eq .Lcfb_len_zero b .Lcfb_dec_proc_1_blk .Lcfb_dec_above_equal_2_blks: cmp LTMP, #48 b.lt .Lcfb_dec_proc_2_blks b .Lcfb_dec_proc_3_blks .Lcfb_dec_above_equal_4_blks: cmp LTMP, #96 b.ge .Lcfb_dec_above_equal_6_blks cmp LTMP, #80 b.lt .Lcfb_dec_proc_4_blks b .Lcfb_dec_proc_5_blks .Lcfb_dec_above_equal_6_blks: cmp LTMP, #112 b.lt .Lcfb_dec_proc_6_blks cmp LTMP, #128 b.lt .Lcfb_dec_proc_7_blks .Lcfb_dec_proc_8_blks: /* When the length is greater than or equal to 128, eight blocks loop is used */ .Lcfb_aesdec_8_blks_loop: /* Compute 8 CBF Decryption */ ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [IN], #64 mov CT1.16b, IVC.16b // Prevent the IV or BLK from being changed mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b mov CT4.16b, BLK2.16b mov CT5.16b, BLK3.16b mov CT6.16b, BLK4.16b mov CT7.16b, BLK5.16b mov CT8.16b, BLK6.16b mov x14, KEY // Prevent the key from being changed AES_ENC_8_BLKS x14 CT1.16b CT2.16b CT3.16b CT4.16b CT5.16b \ CT6.16b CT7.16b CT8.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK7.16b // Prepares for the next loop or update eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b eor BLK3.16b, BLK3.16b, CT4.16b eor BLK4.16b, BLK4.16b, CT5.16b eor BLK5.16b, BLK5.16b, CT6.16b eor BLK6.16b, BLK6.16b, CT7.16b eor BLK7.16b, BLK7.16b, CT8.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [OUT], #64 sub LTMP, LTMP, #128 cmp LTMP, #0 b.eq .Lcfb_aesdec_finish cmp LTMP, #128 b.lt .Lcfb_aesdec_start b .Lcfb_aesdec_8_blks_loop .Lcfb_dec_proc_1_blk: ld1 {BLK0.16b}, [IN] mov CT1.16b, IVC.16b AES_ENC_1_BLK KEY CT1.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK0.16b eor BLK0.16b, CT1.16b, BLK0.16b st1 {BLK0.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b AES_ENC_2_BLKS KEY CT1.16b CT2.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK1.16b eor BLK0.16b, CT1.16b, BLK0.16b eor BLK1.16b, CT2.16b, BLK1.16b st1 {BLK0.16b, BLK1.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_3_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b AES_ENC_3_BLKS KEY CT1.16b CT2.16b CT3.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK2.16b eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_4_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b mov CT4.16b, BLK2.16b AES_ENC_4_BLKS KEY CT1.16b CT2.16b CT3.16b CT4.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK3.16b eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b eor BLK3.16b, BLK3.16b, CT4.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_5_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b mov CT4.16b, BLK2.16b mov CT5.16b, BLK3.16b AES_ENC_5_BLKS KEY CT1.16b CT2.16b CT3.16b CT4.16b CT5.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK4.16b eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b eor BLK3.16b, BLK3.16b, CT4.16b eor BLK4.16b, BLK4.16b, CT5.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_6_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b mov CT4.16b, BLK2.16b mov CT5.16b, BLK3.16b mov CT6.16b, BLK4.16b AES_ENC_6_BLKS KEY CT1.16b CT2.16b CT3.16b CT4.16b CT5.16b CT6.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK5.16b eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b eor BLK3.16b, BLK3.16b, CT4.16b eor BLK4.16b, BLK4.16b, CT5.16b eor BLK5.16b, BLK5.16b, CT6.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b}, [OUT] b .Lcfb_aesdec_finish .Lcfb_dec_proc_7_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b}, [IN] mov CT1.16b, IVC.16b mov CT2.16b, BLK0.16b mov CT3.16b, BLK1.16b mov CT4.16b, BLK2.16b mov CT5.16b, BLK3.16b mov CT6.16b, BLK4.16b mov CT7.16b, BLK5.16b AES_ENC_7_BLKS KEY CT1.16b CT2.16b CT3.16b CT4.16b CT5.16b CT6.16b CT7.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS mov IVC.16b, BLK6.16b eor BLK0.16b, BLK0.16b, CT1.16b eor BLK1.16b, BLK1.16b, CT2.16b eor BLK2.16b, BLK2.16b, CT3.16b eor BLK3.16b, BLK3.16b, CT4.16b eor BLK4.16b, BLK4.16b, CT5.16b eor BLK5.16b, BLK5.16b, CT6.16b eor BLK6.16b, BLK6.16b, CT7.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b}, [OUT] .Lcfb_aesdec_finish: st1 {IVC.16b}, [IV] .Lcfb_len_zero: mov x0, #0 eor CT1.16b, CT1.16b, CT1.16b eor CT2.16b, CT2.16b, CT2.16b eor CT3.16b, CT3.16b, CT3.16b eor CT4.16b, CT4.16b, CT4.16b eor CT5.16b, CT5.16b, CT5.16b eor CT6.16b, CT6.16b, CT6.16b eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b AARCH64_AUTIASP ret .size CRYPT_AES_CFB_Decrypt, .-CRYPT_AES_CFB_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_cfb_armv8.S
Unix Assembly
unknown
7,951
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CTR) #include "crypt_arm.h" #include "crypt_aes_macro_armv8.s" .file "crypt_aes_ctr_armv8.S" .text .arch armv8-a+crypto .align 5 KEY .req x0 IN .req x1 OUT .req x2 LEN .req x3 IV .req x4 LTMP .req x12 CTMP .req v27 BLK0 .req v0 BLK1 .req v1 BLK2 .req v2 BLK3 .req v3 BLK4 .req v4 BLK5 .req v5 BLK6 .req v6 BLK7 .req v7 CTR0 .req v19 CTR1 .req v20 CTR2 .req v21 CTR3 .req v22 CTR4 .req v23 CTR5 .req v24 CTR6 .req v25 CTR7 .req v26 RDK0 .req v17 RDK1 .req v18 ROUNDS .req w6 /* ctr + 1 */ .macro ADDCTR ctr #ifndef HITLS_BIG_ENDIAN add w11, w11, #1 rev w9, w11 mov \ctr, w9 #else rev w11, w11 add w11, w11, #1 rev w11, w11 mov \ctr, w11 #endif .endm /* * Vn - V0 ~ V31 * 8bytes - Vn.8B Vn.4H Vn.2S Vn.1D * 16bytes - Vn.16B Vn.8H Vn.4S Vn.2D */ /* * int32_t CRYPT_AES_CTR_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len, * uint8_t *iv); */ .globl CRYPT_AES_CTR_Encrypt .type CRYPT_AES_CTR_Encrypt, %function CRYPT_AES_CTR_Encrypt: AARCH64_PACIASP ld1 {CTR0.16b}, [IV] // Reads the IV. mov CTMP.16b, CTR0.16b mov w11, CTR0.s[3] #ifndef HITLS_BIG_ENDIAN rev w11, w11 #endif mov LTMP, LEN .Lctr_aesenc_start: cmp LTMP, #64 b.ge .Lctr_enc_above_equal_4_blks cmp LTMP, #32 b.ge .Lctr_enc_above_equal_2_blks cmp LTMP, #0 b.eq .Lctr_len_zero b .Lctr_enc_proc_1_blk .Lctr_enc_above_equal_2_blks: cmp LTMP, #48 b.lt .Lctr_enc_proc_2_blks b .Lctr_enc_proc_3_blks .Lctr_enc_above_equal_4_blks: cmp LTMP, #96 b.ge .Lctr_enc_above_equal_6_blks cmp LTMP, #80 b.lt .Lctr_enc_proc_4_blks b .Lctr_enc_proc_5_blks .Lctr_enc_above_equal_6_blks: cmp LTMP, #112 b.lt .Lctr_enc_proc_6_blks cmp LTMP, #128 b.lt .Lctr_enc_proc_7_blks .Lctr_enc_proc_8_blks: /* When the length is greater than or equal to 128, eight blocks loop is used. */ .Lctr_aesenc_8_blks_loop: /* Calculate eight CTRs. */ mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b mov CTR3.16b, CTMP.16b mov CTR4.16b, CTMP.16b mov CTR5.16b, CTMP.16b mov CTR6.16b, CTMP.16b mov CTR7.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] ADDCTR CTR3.s[3] ADDCTR CTR4.s[3] ADDCTR CTR5.s[3] ADDCTR CTR6.s[3] ADDCTR CTR7.s[3] mov x14, KEY // Prevent the key from being changed. AES_ENC_8_BLKS x14 CTR0.16b CTR1.16b CTR2.16b CTR3.16b CTR4.16b \ CTR5.16b CTR6.16b CTR7.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [IN], #64 eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b eor BLK3.16b, BLK3.16b, CTR3.16b eor BLK4.16b, BLK4.16b, CTR4.16b eor BLK5.16b, BLK5.16b, CTR5.16b eor BLK6.16b, BLK6.16b, CTR6.16b eor BLK7.16b, BLK7.16b, CTR7.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [OUT], #64 sub LTMP, LTMP, #128 cmp LTMP, #0 b.eq .Lctr_aesenc_finish ADDCTR CTMP.s[3] mov CTR0.16b, CTMP.16b cmp LTMP, #128 b.lt .Lctr_aesenc_start b .Lctr_aesenc_8_blks_loop .Lctr_enc_proc_1_blk: AES_ENC_1_BLK KEY CTR0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b}, [IN] eor BLK0.16b, CTR0.16b, BLK0.16b st1 {BLK0.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_2_blks: mov CTR1.16b, CTMP.16b ADDCTR CTR1.s[3] AES_ENC_2_BLKS KEY CTR0.16b CTR1.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b}, [IN] eor BLK0.16b, CTR0.16b, BLK0.16b eor BLK1.16b, CTR1.16b, BLK1.16b st1 {BLK0.16b, BLK1.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_3_blks: mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] AES_ENC_3_BLKS KEY CTR0.16b CTR1.16b CTR2.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b}, [IN] eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_4_blks: mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b mov CTR3.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] ADDCTR CTR3.s[3] AES_ENC_4_BLKS KEY CTR0.16b CTR1.16b CTR2.16b CTR3.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b eor BLK3.16b, BLK3.16b, CTR3.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_5_blks: mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b mov CTR3.16b, CTMP.16b mov CTR4.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] ADDCTR CTR3.s[3] ADDCTR CTR4.s[3] AES_ENC_5_BLKS KEY CTR0.16b CTR1.16b CTR2.16b CTR3.16b CTR4.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b}, [IN] eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b eor BLK3.16b, BLK3.16b, CTR3.16b eor BLK4.16b, BLK4.16b, CTR4.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_6_blks: mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b mov CTR3.16b, CTMP.16b mov CTR4.16b, CTMP.16b mov CTR5.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] ADDCTR CTR3.s[3] ADDCTR CTR4.s[3] ADDCTR CTR5.s[3] AES_ENC_6_BLKS KEY CTR0.16b CTR1.16b CTR2.16b CTR3.16b CTR4.16b \ CTR5.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b}, [IN] eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b eor BLK3.16b, BLK3.16b, CTR3.16b eor BLK4.16b, BLK4.16b, CTR4.16b eor BLK5.16b, BLK5.16b, CTR5.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b}, [OUT] b .Lctr_aesenc_finish .Lctr_enc_proc_7_blks: mov CTR1.16b, CTMP.16b mov CTR2.16b, CTMP.16b mov CTR3.16b, CTMP.16b mov CTR4.16b, CTMP.16b mov CTR5.16b, CTMP.16b mov CTR6.16b, CTMP.16b ADDCTR CTR1.s[3] ADDCTR CTR2.s[3] ADDCTR CTR3.s[3] ADDCTR CTR4.s[3] ADDCTR CTR5.s[3] ADDCTR CTR6.s[3] AES_ENC_7_BLKS KEY CTR0.16b CTR1.16b CTR2.16b CTR3.16b CTR4.16b \ CTR5.16b CTR6.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b}, [IN] eor BLK0.16b, BLK0.16b, CTR0.16b eor BLK1.16b, BLK1.16b, CTR1.16b eor BLK2.16b, BLK2.16b, CTR2.16b eor BLK3.16b, BLK3.16b, CTR3.16b eor BLK4.16b, BLK4.16b, CTR4.16b eor BLK5.16b, BLK5.16b, CTR5.16b eor BLK6.16b, BLK6.16b, CTR6.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b}, [OUT] .Lctr_aesenc_finish: ADDCTR CTMP.s[3] // Fill CTR0 for the next round. st1 {CTMP.16b}, [IV] .Lctr_len_zero: mov x0, #0 eor CTR0.16b, CTR0.16b, CTR0.16b eor CTR1.16b, CTR1.16b, CTR1.16b eor CTR2.16b, CTR2.16b, CTR2.16b eor CTR3.16b, CTR3.16b, CTR3.16b eor CTR4.16b, CTR4.16b, CTR4.16b eor CTR5.16b, CTR5.16b, CTR5.16b eor CTR6.16b, CTR6.16b, CTR6.16b eor CTR7.16b, CTR7.16b, CTR7.16b eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b AARCH64_AUTIASP ret .size CRYPT_AES_CTR_Encrypt, .-CRYPT_AES_CTR_Encrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_ctr_armv8.S
Unix Assembly
unknown
9,199
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CTR) .file "crypt_aes_ctr_x86_64.S" .text .set KEY, %rdi .set INPUT, %rsi .set OUTPUT, %rdx .set LEN, %ecx .set CTR_IV, %r8 .set RDK, %xmm0 .set RDK2, %xmm1 .set KTMP, %r13 .set ROUNDS, %eax .set RET, %eax .set IV0, %xmm2 .set IV1, %xmm3 .set IV2, %xmm4 .set IV3, %xmm5 .set IV4, %xmm6 .set IV5, %xmm7 .set IV6, %xmm8 .set IV7, %xmm9 .set BLK0, %xmm10 .set BLK1, %xmm11 .set BLK2, %xmm12 .set BLK3, %xmm13 .set BLK4, %xmm14 .set BLK5, %xmm15 /** * Macro description: Eight IVs are encrypted. * Input register: * Key: Round key. * block0-7: Encrypted IV. * Modify the register: block0-7. * Output register: * block0-7: IV after a round of encryption. */ .macro ONE_ENC key block0 block1 block2 block3 block4 block5 block6 block7 aesenc \key, \block0 aesenc \key, \block1 aesenc \key, \block2 aesenc \key, \block3 aesenc \key, \block4 aesenc \key, \block5 aesenc \key, \block6 aesenc \key, \block7 .endm /** * Macro description: Obtains a new ctr and XORs it with the round key. * input register: * ctr32:Initialization vector. * offset:Offset. * temp:32-bit CTR temporary register. * key32:32-bit round key. * addrOffset:push stack address offset. * addr:push stack address. * Modify the register: Temp. */ .macro XOR_KEY ctr32 offset temp key32 addrOffset addr leal \offset(\ctr32), \temp // XOR 32-bit ctr and key, push into the stack bswapl \temp xorl \key32, \temp movl \temp, \addrOffset+12(\addr) .endm /** * Macro description: Obtain the round key, encrypt the IV, obtain the next round of ctr, and XOR the round key. * Input register: * Key: pointer to the key. * Offset: round key offset. * Temp: Temporary register for the round key. * Ctr32: initialization vector. * Offset2: Ctr offset. * Temp2: 32-bit CTR temporary register. * Key32: 32-bit round key. * AddrOffset: Offest of entering the stack. * Addr: Address for entering the stack. * Modify register: Temp temp2 IV0-7. * Output register: * IV0-7: IV after a round of encryption. */ .macro ONE_ENC_XOR_KEY key offset temp ctr32 offset2 temp2 key32 addrOffset addr vmovdqu \offset(\key), \temp aesenc \temp, IV0 leal \offset2(\ctr32), \temp2 // XOR 32-bit ctr and key, push stack. aesenc \temp, IV1 bswapl \temp2 aesenc \temp, IV2 aesenc \temp, IV3 xorl \key32, \temp2 aesenc \temp, IV4 aesenc \temp, IV5 movl \temp2, \addrOffset+12(\addr) aesenc \temp, IV6 aesenc \temp, IV7 .endm /** * Macro description: Update the in and out pointer offsets and the remaining length of len. * Input register: * Input:pointer to the input memory. * Output:pointer to the output memory. * Len:remaining data length. * Offset:indicates the offset. * Modify the register: Input output len. * Output register: * Input output len */ .macro UPDATE_DATA input output len offset leaq \offset(\input), \input leaq \offset(\output), \output subl $\offset, \len .endm /** * Function description:Sets the AES encrypted assembly acceleration API, ctr mode. * Function prototype:int32_t CRYPT_AES_CTR_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, * uint32_t len, uint8_t *iv); * Input register: * rdi:Pointer to the input key structure. * rsi:Points to the 128-bit input data. * rdx:Points to the 128-bit output data. * rcx:Length of the data block, that is, 16 bytes. * r8: 16-byte initialization vector. * Change register:xmm1, xmm3, xmm4, xmm5, xmm6, xmm10, xmm11, xmm12, xmm13. * Output register:rdx, r8. */ .globl CRYPT_AES_CTR_Encrypt .type CRYPT_AES_CTR_Encrypt, @function CRYPT_AES_CTR_Encrypt: .cfi_startproc pushq %r12 pushq %r13 pushq %r14 pushq %r15 mov %rsp, %r12 subq $128, %rsp // Declare for 128-byte stack space. andq $-16, %rsp vmovdqu (KEY), RDK vpxor (CTR_IV), RDK, IV0 vmovdqa IV0, 0(%rsp) vmovdqa IV0, 16(%rsp) vmovdqa IV0, 32(%rsp) vmovdqa IV0, 48(%rsp) vmovdqa IV0, 64(%rsp) vmovdqa IV0, 80(%rsp) vmovdqa IV0, 96(%rsp) vmovdqa IV0, 112(%rsp) movl 12(CTR_IV), %r11d // Read 32-bit ctr. movl 12(KEY), %r9d // Read 32-bit key. bswap %r11d mov LEN, %r14d shr $4, %r14d and $7, %r14d cmp $1, %r14d je .Lctr_enc_proc_1_blk cmp $2, %r14d je .Lctr_enc_proc_2_blk cmp $3, %r14d je .Lctr_enc_proc_3_blk cmp $4, %r14d je .Lctr_enc_proc_4_blk cmp $5, %r14d je .Lctr_enc_proc_5_blk cmp $6, %r14d je .Lctr_enc_proc_6_blk cmp $7, %r14d je .Lctr_enc_proc_7_blk .Lctr_enc_proc_8_blk: cmp $0, LEN je .Lctr_aesenc_finish leal 0(%r11d), %r15d leal 1(%r11d), %r10d bswapl %r15d bswapl %r10d xorl %r9d, %r15d xorl %r9d, %r10d leal 2(%r11d), %r14d movl %r15d, 12(%rsp) bswapl %r14d movl %r10d, 16+12(%rsp) xorl %r9d, %r14d leal 3(%r11d), %r15d leal 4(%r11d), %r10d bswapl %r15d bswapl %r10d movl %r14d, 32+12(%rsp) xorl %r9d, %r15d xorl %r9d, %r10d movl %r15d, 48+12(%rsp) leal 5(%r11d), %r14d bswapl %r14d movl %r10d, 64+12(%rsp) xorl %r9d, %r14d leal 6(%r11d), %r15d leal 7(%r11d), %r10d movl %r14d, 80+12(%rsp) bswapl %r15d bswapl %r10d xorl %r9d, %r15d xorl %r9d, %r10d movl %r15d, 96+12(%rsp) movl %r10d, 112+12(%rsp) vmovdqa (%rsp), IV0 vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 vmovdqa 64(%rsp), IV4 vmovdqa 80(%rsp), IV5 vmovdqa 96(%rsp), IV6 vmovdqa 112(%rsp), IV7 .align 16 .Lctr_aesenc_8_blks_enc_loop: addl $8, %r11d // ctr+8 movl 240(KEY), ROUNDS ONE_ENC_XOR_KEY KEY, 16, RDK2, %r11d, 0, %r10d, %r9d, 0, %rsp // Round 1 encryption ONE_ENC_XOR_KEY KEY, 32, RDK2, %r11d, 1, %r10d, %r9d, 16, %rsp // Round 2 encryption ONE_ENC_XOR_KEY KEY, 48, RDK2, %r11d, 2, %r10d, %r9d, 32, %rsp // Round 3 encryption ONE_ENC_XOR_KEY KEY, 64, RDK2, %r11d, 3, %r10d, %r9d, 48, %rsp // Round 4 encryption ONE_ENC_XOR_KEY KEY, 80, RDK2, %r11d, 4, %r10d, %r9d, 64, %rsp // Round 5 encryption ONE_ENC_XOR_KEY KEY, 96, RDK2, %r11d, 5, %r10d, %r9d, 80, %rsp // Round 6 encryption ONE_ENC_XOR_KEY KEY, 112, RDK2, %r11d, 6, %r10d, %r9d, 96, %rsp // Round 7 encryption ONE_ENC_XOR_KEY KEY, 128, RDK2, %r11d, 7, %r10d, %r9d, 112, %rsp // Round 8 encryption vmovdqu 144(KEY), RDK // Round 9 key Load vmovdqu 160(KEY), RDK2 // Round 10 key Load cmp $12, ROUNDS jb .Lctr_aesenc_8_blks_enc_last ONE_ENC RDK, IV0, IV1, IV2, IV3, IV4, IV5, IV6, IV7 // Round 9 encryption vmovdqu 176(KEY), RDK // Round 11 key Load ONE_ENC RDK2, IV0, IV1, IV2, IV3, IV4, IV5, IV6, IV7 // Round 10 encryption vmovdqu 192(KEY), RDK2 // Round 12 key Load je .Lctr_aesenc_8_blks_enc_last ONE_ENC RDK, IV0, IV1, IV2, IV3, IV4, IV5, IV6, IV7 // Round 11 encryption vmovdqu 208(KEY), RDK // Round 13 key Load ONE_ENC RDK2, IV0, IV1, IV2, IV3, IV4, IV5, IV6, IV7 // Round 12 encryption vmovdqu 224(KEY), RDK2 // Round 14 key Load .align 16 .Lctr_aesenc_8_blks_enc_last: vpxor (INPUT), RDK2, BLK0 // Last round Key ^ Plaintext. vpxor 16(INPUT), RDK2, BLK1 vpxor 32(INPUT), RDK2, BLK2 vpxor 48(INPUT), RDK2, BLK3 ONE_ENC RDK, IV0, IV1, IV2, IV3, IV4, IV5, IV6, IV7 aesenclast BLK0, IV0 // Last round of encryption. aesenclast BLK1, IV1 aesenclast BLK2, IV2 aesenclast BLK3, IV3 aesenclast RDK2, IV4 aesenclast RDK2, IV5 aesenclast RDK2, IV6 aesenclast RDK2, IV7 vmovdqu IV0, (OUTPUT) // The first four ciphertexts are stored in out. vmovdqu IV1, 16(OUTPUT) vmovdqu IV2, 32(OUTPUT) vmovdqu IV3, 48(OUTPUT) vpxor 64(INPUT), IV4, BLK0 // Last Round Key ^ Plaintext. vpxor 80(INPUT), IV5, BLK1 vpxor 96(INPUT), IV6, BLK2 vpxor 112(INPUT), IV7, BLK3 vmovdqu BLK0, 64(OUTPUT) vmovdqu BLK1, 80(OUTPUT) vmovdqu BLK2, 96(OUTPUT) // The last four ciphertexts are stored in out. vmovdqu BLK3, 112(OUTPUT) vmovdqa (%rsp), IV0 // Reads the next round of ctr from the stack. vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 vmovdqa 64(%rsp), IV4 vmovdqa 80(%rsp), IV5 vmovdqa 96(%rsp), IV6 vmovdqa 112(%rsp), IV7 UPDATE_DATA INPUT, OUTPUT, LEN, 128 cmpl $0, LEN jbe .Lctr_aesenc_finish jmp .Lctr_aesenc_8_blks_enc_loop .Lctr_enc_proc_1_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS .align 16 .Laesenc_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 decl ROUNDS jnz .Laesenc_loop // Loop the loop until the ROUNDS is 0. leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 addl $1, %r11d // Update ctr32. vpxor (INPUT), IV0, BLK0 vmovdqu BLK0, (OUTPUT) // Ciphertext stored in out. UPDATE_DATA INPUT, OUTPUT, LEN, 16 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_2_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp vmovdqa 16(%rsp), IV1 .align 16 .Laesenc_2_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 decl ROUNDS jnz .Laesenc_2_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) addl $2, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 32 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_3_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp XOR_KEY %r11d, 2, %r10d, %r9d, 32, %rsp vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 .align 16 .Laesenc_3_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 aesenc RDK, IV2 decl ROUNDS jnz .Laesenc_3_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 aesenclast RDK, IV2 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vpxor 32(INPUT), IV2, BLK2 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) vmovdqu BLK2, 32(OUTPUT) addl $3, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 48 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_4_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp XOR_KEY %r11d, 2, %r10d, %r9d, 32, %rsp XOR_KEY %r11d, 3, %r10d, %r9d, 48, %rsp vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 .align 16 .Laesenc_4_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 aesenc RDK, IV2 aesenc RDK, IV3 decl ROUNDS jnz .Laesenc_4_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 aesenclast RDK, IV2 aesenclast RDK, IV3 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vpxor 32(INPUT), IV2, BLK2 vpxor 48(INPUT), IV3, BLK3 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) vmovdqu BLK2, 32(OUTPUT) vmovdqu BLK3, 48(OUTPUT) addl $4, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 64 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_5_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp XOR_KEY %r11d, 2, %r10d, %r9d, 32, %rsp XOR_KEY %r11d, 3, %r10d, %r9d, 48, %rsp XOR_KEY %r11d, 4, %r10d, %r9d, 64, %rsp vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 vmovdqa 64(%rsp), IV4 .align 16 .Laesenc_5_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 aesenc RDK, IV2 aesenc RDK, IV3 aesenc RDK, IV4 decl ROUNDS jnz .Laesenc_5_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 aesenclast RDK, IV2 aesenclast RDK, IV3 aesenclast RDK, IV4 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vpxor 32(INPUT), IV2, BLK2 vpxor 48(INPUT), IV3, BLK3 vpxor 64(INPUT), IV4, BLK4 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) vmovdqu BLK2, 32(OUTPUT) vmovdqu BLK3, 48(OUTPUT) vmovdqu BLK4, 64(OUTPUT) addl $5, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 80 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_6_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp XOR_KEY %r11d, 2, %r10d, %r9d, 32, %rsp XOR_KEY %r11d, 3, %r10d, %r9d, 48, %rsp XOR_KEY %r11d, 4, %r10d, %r9d, 64, %rsp XOR_KEY %r11d, 5, %r10d, %r9d, 80, %rsp vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 vmovdqa 64(%rsp), IV4 vmovdqa 80(%rsp), IV5 .align 16 .Laesenc_6_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 aesenc RDK, IV2 aesenc RDK, IV3 aesenc RDK, IV4 aesenc RDK, IV5 decl ROUNDS jnz .Laesenc_6_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 aesenclast RDK, IV2 aesenclast RDK, IV3 aesenclast RDK, IV4 aesenclast RDK, IV5 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vpxor 32(INPUT), IV2, BLK2 vpxor 48(INPUT), IV3, BLK3 vpxor 64(INPUT), IV4, BLK4 vpxor 80(INPUT), IV5, BLK5 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) vmovdqu BLK2, 32(OUTPUT) vmovdqu BLK3, 48(OUTPUT) vmovdqu BLK4, 64(OUTPUT) vmovdqu BLK5, 80(OUTPUT) addl $6, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 96 jmp .Lctr_enc_proc_8_blk .Lctr_enc_proc_7_blk: movl 240(KEY), ROUNDS movq KEY, KTMP decl ROUNDS XOR_KEY %r11d, 1, %r10d, %r9d, 16, %rsp XOR_KEY %r11d, 2, %r10d, %r9d, 32, %rsp XOR_KEY %r11d, 3, %r10d, %r9d, 48, %rsp XOR_KEY %r11d, 4, %r10d, %r9d, 64, %rsp XOR_KEY %r11d, 5, %r10d, %r9d, 80, %rsp XOR_KEY %r11d, 6, %r10d, %r9d, 96, %rsp vmovdqa 16(%rsp), IV1 vmovdqa 32(%rsp), IV2 vmovdqa 48(%rsp), IV3 vmovdqa 64(%rsp), IV4 vmovdqa 80(%rsp), IV5 vmovdqa 96(%rsp), IV6 .align 16 .Laesenc_7_blks_loop: leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenc RDK, IV0 aesenc RDK, IV1 aesenc RDK, IV2 aesenc RDK, IV3 aesenc RDK, IV4 aesenc RDK, IV5 aesenc RDK, IV6 decl ROUNDS jnz .Laesenc_7_blks_loop leaq 16(KTMP), KTMP vmovdqu (KTMP), RDK aesenclast RDK, IV0 aesenclast RDK, IV1 aesenclast RDK, IV2 aesenclast RDK, IV3 aesenclast RDK, IV4 aesenclast RDK, IV5 aesenclast RDK, IV6 vpxor (INPUT), IV0, BLK0 vpxor 16(INPUT), IV1, BLK1 vpxor 32(INPUT), IV2, BLK2 vpxor 48(INPUT), IV3, BLK3 vmovdqu BLK0, (OUTPUT) vmovdqu BLK1, 16(OUTPUT) vmovdqu BLK2, 32(OUTPUT) vmovdqu BLK3, 48(OUTPUT) vpxor 64(INPUT), IV4, BLK0 vpxor 80(INPUT), IV5, BLK1 vpxor 96(INPUT), IV6, BLK2 vmovdqu BLK0, 64(OUTPUT) vmovdqu BLK1, 80(OUTPUT) vmovdqu BLK2, 96(OUTPUT) addl $7, %r11d UPDATE_DATA INPUT, OUTPUT, LEN, 112 jmp .Lctr_enc_proc_8_blk .Lctr_aesenc_finish: bswap %r11d movl %r11d, 12(CTR_IV) vpxor IV0, IV0, IV0 vpxor IV1, IV1, IV1 vpxor IV2, IV2, IV2 vpxor IV3, IV3, IV3 vpxor IV4, IV4, IV4 vpxor IV5, IV5, IV5 vpxor IV6, IV6, IV6 vpxor IV7, IV7, IV7 vpxor RDK, RDK, RDK vmovdqa IV0, 0(%rsp) vmovdqa IV0, 16(%rsp) vmovdqa IV0, 32(%rsp) vmovdqa IV0, 48(%rsp) vmovdqa IV0, 64(%rsp) vmovdqa IV0, 80(%rsp) vmovdqa IV0, 96(%rsp) vmovdqa IV0, 112(%rsp) movq %r12, %rsp popq %r15 popq %r14 popq %r13 popq %r12 movl $0, RET ret .cfi_endproc .size CRYPT_AES_CTR_Encrypt, .-CRYPT_AES_CTR_Encrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_ctr_x86_64.S
Unix Assembly
unknown
18,679
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_ECB) #include "crypt_arm.h" #include "crypt_aes_macro_armv8.s" .file "crypt_aes_ecb_armv8.S" .text .arch armv8-a+crypto KEY .req x0 IN .req x1 OUT .req x2 LEN .req x3 KTMP .req x4 LTMP .req x9 ROUNDS .req w6 BLK0 .req v0 BLK1 .req v1 BLK2 .req v2 BLK3 .req v3 BLK4 .req v4 BLK5 .req v5 BLK6 .req v6 BLK7 .req v7 RDK0 .req v17 RDK1 .req v18 /* * Vn - V0 ~ V31 * 8bytes - Vn.8B Vn.4H Vn.2S Vn.1D * 16bytes - Vn.16B Vn.8H Vn.4S Vn.2D * * In Return-oriented programming (ROP) and Jump-oriented programming (JOP), we explored features * that Arm introduced to the Arm architecture to mitigate against JOP-style and ROP-style attacks. * ... * Whether the combined or NOP-compatible instructions are generated depends on the architecture * version that the code is built for. When building for Armv8.3-A, or later, the compiler will use * the combined operations. When building for Armv8.2-A, or earlier, it will use the NOP compatible * instructions. * (https://developer.arm.com/documentation/102433/0100/Applying-these-techniques-to-real-code?lang=en) * * The paciasp and autiasp instructions are used for function pointer authentication. The pointer * authentication feature is added in armv8.3 and is supported only by AArch64. * The addition of pointer authentication features is described in Section A2.6.1 of * DDI0487H_a_a-profile_architecture_reference_manual.pdf. */ /** * Function description: Sets the AES encryption assembly acceleration interface in ECB mode. * int32_t CRYPT_AES_ECB_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Points to the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: x4, x6, x9, v0-v7, v17, v18. * Output register: x0. * Function/Macro Call: AES_ENC_8_BLKS, AES_ENC_1_BLK, AES_ENC_2_BLKS, AES_ENC_4_BLKS, * AES_ENC_5_BLKS, AES_ENC_6_BLKS, AES_ENC_7_BLKS. */ .globl CRYPT_AES_ECB_Encrypt .type CRYPT_AES_ECB_Encrypt, %function CRYPT_AES_ECB_Encrypt: AARCH64_PACIASP mov LTMP, LEN .Lecb_aesenc_start: cmp LTMP, #64 b.ge .Lecb_enc_above_equal_4_blks cmp LTMP, #32 b.ge .Lecb_enc_above_equal_2_blks cmp LTMP, #0 b.eq .Lecb_aesenc_finish b .Lecb_enc_proc_1_blk .Lecb_enc_above_equal_2_blks: cmp LTMP, #48 b.lt .Lecb_enc_proc_2_blks b .Lecb_enc_proc_3_blks .Lecb_enc_above_equal_4_blks: cmp LTMP, #96 b.ge .Lecb_enc_above_equal_6_blks cmp LTMP, #80 b.lt .Lecb_enc_proc_4_blks b .Lecb_enc_proc_5_blks .Lecb_enc_above_equal_6_blks: cmp LTMP, #112 b.lt .Lecb_enc_proc_6_blks cmp LTMP, #128 b.lt .Lecb_enc_proc_7_blks .Lecb_enc_proc_8_blks: .Lecb_aesenc_8_blks_loop: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [IN], #64 mov KTMP, KEY AES_ENC_8_BLKS KTMP BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b \ BLK5.16b BLK6.16b BLK7.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [OUT], #64 sub LTMP, LTMP, #128 cmp LTMP, #128 b.lt .Lecb_aesenc_start b .Lecb_aesenc_8_blks_loop .Lecb_enc_proc_1_blk: ld1 {BLK0.16b}, [IN] AES_ENC_1_BLK KEY BLK0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN] AES_ENC_2_BLKS KEY BLK0.16b BLK1.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_3_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b}, [IN] AES_ENC_3_BLKS KEY BLK0.16b BLK1.16b BLK2.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_4_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] AES_ENC_4_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_5_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b}, [IN] AES_ENC_5_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_6_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b}, [IN] AES_ENC_6_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b}, [OUT] b .Lecb_aesenc_finish .Lecb_enc_proc_7_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b}, [IN] AES_ENC_7_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b BLK6.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b}, [OUT] .Lecb_aesenc_finish: mov x0, #0 eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b AARCH64_AUTIASP ret .size CRYPT_AES_ECB_Encrypt, .-CRYPT_AES_ECB_Encrypt /** * Function description: Sets the AES decryption and assembly acceleration API in ECB mode. * int32_t CRYPT_AES_ECB_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, * uint8_t *out, * uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Points to the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: x4, x6, x9, v0-v7, v17, v18 * Output register: x0 * Function/Macro Call: AES_DEC_8_BLKS, AES_DEC_1_BLK, AES_DEC_2_BLKS, AES_DEC_4_BLKS, * AES_DEC_5_BLKS, AES_DEC_6_BLKS, AES_DEC_7_BLKS. */ .globl CRYPT_AES_ECB_Decrypt .type CRYPT_AES_ECB_Decrypt, %function CRYPT_AES_ECB_Decrypt: AARCH64_PACIASP mov LTMP, LEN .Lecb_aesdec_start: cmp LTMP, #64 b.ge .Lecb_dec_above_equal_4_blks cmp LTMP, #32 b.ge .Lecb_dec_above_equal_2_blks cmp LTMP, #0 b.eq .Lecb_aesdec_finish b .Lecb_dec_proc_1_blk .Lecb_dec_above_equal_2_blks: cmp LTMP, #48 b.lt .Lecb_dec_proc_2_blks b .Lecb_dec_proc_3_blks .Lecb_dec_above_equal_4_blks: cmp LTMP, #96 b.ge .Lecb_dec_above_equal_6_blks cmp LTMP, #80 b.lt .Lecb_dec_proc_4_blks b .Lecb_dec_proc_5_blks .Lecb_dec_above_equal_6_blks: cmp LTMP, #112 b.lt .Lecb_dec_proc_6_blks cmp LTMP, #128 b.lt .Lecb_dec_proc_7_blks .Lecb_dec_proc_8_blks: .Lecb_aesdec_8_blks_loop: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [IN], #64 mov KTMP, KEY AES_DEC_8_BLKS KTMP BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b \ BLK5.16b BLK6.16b BLK7.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b, BLK7.16b}, [OUT], #64 sub LTMP, LTMP, #128 cmp LTMP, #128 b.lt .Lecb_aesdec_start b .Lecb_aesdec_8_blks_loop .Lecb_dec_proc_1_blk: ld1 {BLK0.16b}, [IN] AES_DEC_1_BLK KEY BLK0.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN] AES_DEC_2_BLKS KEY BLK0.16b BLK1.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_3_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b}, [IN] AES_DEC_3_BLKS KEY BLK0.16b BLK1.16b BLK2.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_4_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN] AES_DEC_4_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_5_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b}, [IN] AES_DEC_5_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_6_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b}, [IN] AES_DEC_6_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b}, [OUT] b .Lecb_aesdec_finish .Lecb_dec_proc_7_blks: ld1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [IN], #64 ld1 {BLK4.16b, BLK5.16b, BLK6.16b}, [IN] AES_DEC_7_BLKS KEY BLK0.16b BLK1.16b BLK2.16b BLK3.16b BLK4.16b BLK5.16b BLK6.16b RDK0.4s RDK1.4s RDK0.16b RDK1.16b ROUNDS st1 {BLK0.16b, BLK1.16b, BLK2.16b, BLK3.16b}, [OUT], #64 st1 {BLK4.16b, BLK5.16b, BLK6.16b}, [OUT] .Lecb_aesdec_finish: mov x0, #0 eor RDK0.16b, RDK0.16b, RDK0.16b eor RDK1.16b, RDK1.16b, RDK1.16b AARCH64_AUTIASP ret .size CRYPT_AES_ECB_Decrypt, .-CRYPT_AES_ECB_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_ecb_armv8.S
Unix Assembly
unknown
10,519
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_ECB) #include "crypt_aes_macro_x86_64.s" .file "crypt_aes_ecb_x86_64.S" .text .set ARG1, %rdi .set ARG2, %rsi .set ARG3, %rdx .set ARG4, %ecx .set ARG5, %r8 .set ARG6, %r9 .set RDK, %xmm3 .set KEY, %rdi .set KTMP, %r9 .set ROUNDS, %eax .set RET, %eax .set BLK0, %xmm1 .set BLK1, %xmm4 .set BLK2, %xmm5 .set BLK3, %xmm6 .set BLK4, %xmm10 .set BLK5, %xmm11 .set BLK6, %xmm12 .set BLK7, %xmm13 .set BLK8, %xmm0 .set BLK9, %xmm2 .set BLK10, %xmm7 .set BLK11, %xmm8 .set BLK12, %xmm9 .set BLK13, %xmm14 /** * Function description: Sets the AES encryption assembly acceleration API in ECB mode. * Function prototype: int32_t CRYPT_AES_ECB_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Points to the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: xmm1,xmm3,xmm4,xmm5,xmm6,xmm10,xmm11,xmm12,xmm13. * Output register: eax. * Function/Macro Call: None. */ .globl CRYPT_AES_ECB_Encrypt .type CRYPT_AES_ECB_Encrypt, @function CRYPT_AES_ECB_Encrypt: .cfi_startproc .align 16 .Lecb_aesenc_start: cmpl $64, ARG4 jae .Lecb_enc_above_equal_4_blks cmpl $32, ARG4 jae .Lecb_enc_above_equal_2_blks cmpl $0, ARG4 je .Lecb_aesdec_finish jmp .Lecb_enc_proc_1_blk .Lecb_enc_above_equal_2_blks: cmpl $48, ARG4 jb .Lecb_enc_proc_2_blks jmp .Lecb_enc_proc_3_blks .Lecb_enc_above_equal_4_blks: cmpl $96, ARG4 jae .Lecb_enc_above_equal_6_blks cmpl $80, ARG4 jb .Lecb_enc_proc_4_blks jmp .Lecb_enc_proc_5_blks .Lecb_enc_above_equal_6_blks: cmpl $112, ARG4 jb .Lecb_enc_proc_6_blks cmpl $128, ARG4 jb .Lecb_enc_proc_7_blks cmpl $256, ARG4 jbe .Lecb_enc_proc_8_blks .align 16 .ecb_enc_proc_14_blks: .Lecb_aesenc_14_blks_loop: movq KEY, KTMP vmovdqu (KEY), RDK movl 240(KEY), ROUNDS vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 vpxor 112(ARG2), RDK, BLK7 vpxor 128(ARG2), RDK, BLK8 vpxor 144(ARG2), RDK, BLK9 vpxor 160(ARG2), RDK, BLK10 vpxor 176(ARG2), RDK, BLK11 vpxor 192(ARG2), RDK, BLK12 vpxor 208(ARG2), RDK, BLK13 decl ROUNDS AES_ENC_14_BLKS ARG2 KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 BLK7 BLK8 BLK9 BLK10 BLK11 BLK12 BLK13 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) vmovdqu BLK7, 112(ARG3) vmovdqu BLK8, 128(ARG3) vmovdqu BLK9, 144(ARG3) vmovdqu BLK10, 160(ARG3) vmovdqu BLK11, 176(ARG3) vmovdqu BLK12, 192(ARG3) vmovdqu BLK13, 208(ARG3) leaq 224(ARG2), ARG2 leaq 224(ARG3), ARG3 subl $224, ARG4 cmpl $224, ARG4 jb .Lecb_aesenc_start jmp .Lecb_aesenc_14_blks_loop .align 16 .Lecb_enc_proc_8_blks: .Lecb_aesenc_8_blks_loop: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK movq KEY, KTMP vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 vpxor 112(ARG2), RDK, BLK7 decl ROUNDS AES_ENC_8_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 BLK7 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) vmovdqu BLK7, 112(ARG3) leaq 128(ARG2), ARG2 leaq 128(ARG3), ARG3 subl $128, ARG4 cmpl $128, ARG4 jb .Lecb_aesenc_start jmp .Lecb_aesenc_8_blks_loop .align 16 .Lecb_enc_proc_1_blk: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 decl ROUNDS AES_ENC_1_BLK KEY ROUNDS RDK BLK0 vmovdqu BLK0, (ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_2_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 decl ROUNDS AES_ENC_2_BLKS KEY ROUNDS RDK BLK0 BLK1 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_3_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 decl ROUNDS AES_ENC_3_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_4_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 decl ROUNDS AES_ENC_4_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_5_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 decl ROUNDS AES_ENC_5_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_6_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 decl ROUNDS AES_ENC_6_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) jmp .Lecb_aesenc_finish .align 16 .Lecb_enc_proc_7_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 decl ROUNDS AES_ENC_7_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) .align 16 .Lecb_aesenc_finish: vpxor RDK, RDK, RDK movl $0, RET ret .cfi_endproc .size CRYPT_AES_ECB_Encrypt, .-CRYPT_AES_ECB_Encrypt /** * Function description: Sets the AES decryption and assembly acceleration API in ECB mode. * Function prototype: int32_t CRYPT_AES_ECB_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Indicates the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: xmm1,xmm3,xmm4,xmm5,xmm6,xmm10,xmm11,xmm12,xmm13. * Output register: eax. * Function/Macro Call: None. */ .globl CRYPT_AES_ECB_Decrypt .type CRYPT_AES_ECB_Decrypt, @function CRYPT_AES_ECB_Decrypt: .cfi_startproc .align 16 .ecb_aesdec_start: cmpl $64, ARG4 jae .ecb_dec_above_equal_4_blks cmpl $32, ARG4 jae .ecb_dec_above_equal_2_blks cmpl $0, ARG4 je .Lecb_aesdec_finish jmp .ecb_dec_proc_1_blk .ecb_dec_above_equal_2_blks: cmpl $48, ARG4 jb .ecb_dec_proc_2_blks jmp .ecb_dec_proc_3_blks .ecb_dec_above_equal_4_blks: cmpl $96, ARG4 jae .ecb_dec_above_equal_6_blks cmpl $80, ARG4 jb .ecb_dec_proc_4_blks jmp .ecb_dec_proc_5_blks .ecb_dec_above_equal_6_blks: cmpl $112, ARG4 jb .ecb_dec_proc_6_blks cmpl $128, ARG4 jb .ecb_dec_proc_7_blks cmpl $256, ARG4 jbe .ecb_dec_proc_8_blks .align 16 .ecb_dec_proc_14_blks: .ecb_aesdec_14_blks_loop: movq KEY, KTMP movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 vpxor 112(ARG2), RDK, BLK7 vpxor 128(ARG2), RDK, BLK8 vpxor 144(ARG2), RDK, BLK9 vpxor 160(ARG2), RDK, BLK10 vpxor 176(ARG2), RDK, BLK11 vpxor 192(ARG2), RDK, BLK12 vpxor 208(ARG2), RDK, BLK13 decl ROUNDS AES_DEC_14_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 BLK7 BLK8 BLK9 BLK10 BLK11 BLK12 BLK13 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) vmovdqu BLK7, 112(ARG3) vmovdqu BLK8, 128(ARG3) vmovdqu BLK9, 144(ARG3) vmovdqu BLK10, 160(ARG3) vmovdqu BLK11, 176(ARG3) vmovdqu BLK12, 192(ARG3) vmovdqu BLK13, 208(ARG3) leaq 224(ARG2), ARG2 leaq 224(ARG3), ARG3 subl $224, ARG4 cmpl $224, ARG4 jb .ecb_aesdec_start jmp .ecb_aesdec_14_blks_loop .align 16 .ecb_dec_proc_8_blks: .aesecbdec_8_blks_loop: movq KEY, KTMP movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 vpxor 112(ARG2), RDK, BLK7 decl ROUNDS AES_DEC_8_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 BLK7 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) vmovdqu BLK7, 112(ARG3) leaq 128(ARG2), ARG2 leaq 128(ARG3), ARG3 subl $128, ARG4 cmpl $128, ARG4 jb .ecb_aesdec_start jmp .aesecbdec_8_blks_loop .align 16 .ecb_dec_proc_1_blk: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 decl ROUNDS AES_DEC_1_BLK KEY ROUNDS RDK BLK0 vmovdqu BLK0, (ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_2_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 decl ROUNDS AES_DEC_2_BLKS KEY ROUNDS RDK BLK0 BLK1 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_3_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 decl ROUNDS AES_DEC_3_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_4_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 decl ROUNDS AES_DEC_4_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_5_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 decl ROUNDS AES_DEC_5_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_6_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 decl ROUNDS AES_DEC_6_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) jmp .Lecb_aesdec_finish .align 16 .ecb_dec_proc_7_blks: movl 240(KEY), ROUNDS vmovdqu (KEY), RDK vpxor (ARG2), RDK, BLK0 vpxor 16(ARG2), RDK, BLK1 vpxor 32(ARG2), RDK, BLK2 vpxor 48(ARG2), RDK, BLK3 vpxor 64(ARG2), RDK, BLK4 vpxor 80(ARG2), RDK, BLK5 vpxor 96(ARG2), RDK, BLK6 decl ROUNDS AES_DEC_7_BLKS KEY ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 BLK5 BLK6 vmovdqu BLK0, (ARG3) vmovdqu BLK1, 16(ARG3) vmovdqu BLK2, 32(ARG3) vmovdqu BLK3, 48(ARG3) vmovdqu BLK4, 64(ARG3) vmovdqu BLK5, 80(ARG3) vmovdqu BLK6, 96(ARG3) .align 16 .Lecb_aesdec_finish: vpxor BLK0, BLK0, BLK0 vpxor BLK1, BLK1, BLK1 vpxor BLK2, BLK2, BLK2 vpxor BLK3, BLK3, BLK3 vpxor BLK4, BLK4, BLK4 vpxor BLK5, BLK5, BLK5 vpxor BLK6, BLK6, BLK6 vpxor BLK7, BLK7, BLK7 vpxor BLK8, BLK8, BLK8 vpxor BLK9, BLK9, BLK9 vpxor BLK10, BLK10, BLK10 vpxor BLK11, BLK11, BLK11 vpxor BLK12, BLK12, BLK12 vpxor BLK13, BLK13, BLK13 vpxor RDK, RDK, RDK movl $0, RET ret .cfi_endproc .size CRYPT_AES_ECB_Decrypt, .-CRYPT_AES_ECB_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_ecb_x86_64.S
Unix Assembly
unknown
14,681
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES .file "crypt_aes_macro_armv8.s" .text .arch armv8-a+crypto BLK0 .req v0 /* * AES_ENC_1_BLKS */ .macro AES_ENC_1_BLK key blk rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc: aese \blk,\rdk0 aesmc \blk,\blk subs \rounds,\rounds,#2 ld1 {\rdk0s},[\key],#16 aese \blk,\rdk1 aesmc \blk,\blk ld1 {\rdk1s},[\key],#16 b.gt .Loop_enc aese \blk,\rdk0 aesmc \blk,\blk ld1 {\rdk0s},[\key] aese \blk,\rdk1 eor \blk,\blk,\rdk0 .endm /* * AES_DEC_1_BLKS */ .macro AES_DEC_1_BLK key blk rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec: aesd \blk,\rdk0 aesimc \blk,\blk subs \rounds,\rounds,#2 ld1 {\rdk0s},[\key],#16 aesd \blk,\rdk1 aesimc \blk,\blk ld1 {\rdk1s},[\key],#16 b.gt .Loop_dec aesd \blk,\rdk0 aesimc \blk,\blk ld1 {\rdk0s},[\key] aesd \blk,\rdk1 eor \blk,\blk,\rdk0 .endm .macro SETDECKEY_LDR_9_BLOCK PTR ld1 {v1.4s}, [\PTR], #16 ld1 {v2.4s}, [\PTR], #16 ld1 {v3.4s}, [\PTR], #16 ld1 {v4.4s}, [\PTR], #16 ld1 {v5.4s}, [\PTR], #16 ld1 {v6.4s}, [\PTR], #16 ld1 {v7.4s}, [\PTR], #16 ld1 {v8.4s}, [\PTR], #16 ld1 {v9.4s}, [\PTR], #16 .endm .macro SETDECKEY_INVMIX_9_BLOCK aesimc v1.16b, v1.16b aesimc v2.16b, v2.16b aesimc v3.16b, v3.16b aesimc v4.16b, v4.16b aesimc v5.16b, v5.16b aesimc v6.16b, v6.16b aesimc v7.16b, v7.16b aesimc v8.16b, v8.16b aesimc v9.16b, v9.16b .endm .macro SETDECKEY_STR_9_BLOCK PTR OFFSETREG st1 {v1.4s}, [\PTR], \OFFSETREG st1 {v2.4s}, [\PTR], \OFFSETREG st1 {v3.4s}, [\PTR], \OFFSETREG st1 {v4.4s}, [\PTR], \OFFSETREG st1 {v5.4s}, [\PTR], \OFFSETREG st1 {v6.4s}, [\PTR], \OFFSETREG st1 {v7.4s}, [\PTR], \OFFSETREG st1 {v8.4s}, [\PTR], \OFFSETREG st1 {v9.4s}, [\PTR], \OFFSETREG .endm /* * AES_ENC_2_BLKS */ .macro AES_ENC_2_BLKS key blk0 blk1 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_2_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_2_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 .endm /* * AES_ENC_3_BLKS */ .macro AES_ENC_3_BLKS key blk0 blk1 blk2 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .align 3 .Loop_enc_3_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk2,\rdk1 aesmc \blk2,\blk2 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_3_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 .endm /* * AES_ENC_4_BLKS */ .macro AES_ENC_4_BLKS key blk0 blk1 blk2 blk3 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_4_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk2,\rdk1 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk3,\rdk1 aesmc \blk3,\blk3 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_4_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 aese \blk3,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 .endm /* * AES_ENC_5_BLKS */ .macro AES_ENC_5_BLKS key blk0 blk1 blk2 blk3 blk4 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_5_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 ld1 {\rdk0s},[\key],#16 subs \rounds,\rounds,#2 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk1 aesmc \blk2,\blk2 aese \blk3,\rdk1 aesmc \blk3,\blk3 aese \blk4,\rdk1 aesmc \blk4,\blk4 ld1 {\rdk1s},[\key],#16 b.gt .Loop_enc_5_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 aese \blk3,\rdk1 aese \blk4,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 .endm /* * AES_ENC_6_BLKS */ .macro AES_ENC_6_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_6_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk2,\rdk1 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk3,\rdk1 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk4,\rdk1 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 aese \blk5,\rdk1 aesmc \blk5,\blk5 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_6_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 aese \blk3,\rdk1 aese \blk4,\rdk1 aese \blk5,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 .endm /* * AES_ENC_7_BLKS */ .macro AES_ENC_7_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 blk6 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_7_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk2,\rdk1 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk3,\rdk1 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk4,\rdk1 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 aese \blk5,\rdk1 aesmc \blk5,\blk5 aese \blk6,\rdk0 aesmc \blk6,\blk6 aese \blk6,\rdk1 aesmc \blk6,\blk6 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_7_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 aese \blk6,\rdk0 aesmc \blk6,\blk6 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 aese \blk3,\rdk1 aese \blk4,\rdk1 aese \blk5,\rdk1 aese \blk6,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 eor \blk6,\blk6,\rdk0 .endm /* * AES_ENC_8_BLKS */ .macro AES_ENC_8_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_enc_8_blks: aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk0,\rdk1 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk1,\rdk1 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk2,\rdk1 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk3,\rdk1 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk4,\rdk1 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 aese \blk5,\rdk1 aesmc \blk5,\blk5 aese \blk6,\rdk0 aesmc \blk6,\blk6 aese \blk6,\rdk1 aesmc \blk6,\blk6 aese \blk7,\rdk0 aesmc \blk7,\blk7 aese \blk7,\rdk1 aesmc \blk7,\blk7 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_enc_8_blks aese \blk0,\rdk0 aesmc \blk0,\blk0 aese \blk1,\rdk0 aesmc \blk1,\blk1 aese \blk2,\rdk0 aesmc \blk2,\blk2 aese \blk3,\rdk0 aesmc \blk3,\blk3 aese \blk4,\rdk0 aesmc \blk4,\blk4 aese \blk5,\rdk0 aesmc \blk5,\blk5 aese \blk6,\rdk0 aesmc \blk6,\blk6 aese \blk7,\rdk0 aesmc \blk7,\blk7 ld1 {\rdk0s},[\key] aese \blk0,\rdk1 aese \blk1,\rdk1 aese \blk2,\rdk1 aese \blk3,\rdk1 aese \blk4,\rdk1 aese \blk5,\rdk1 aese \blk6,\rdk1 aese \blk7,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 eor \blk6,\blk6,\rdk0 eor \blk7,\blk7,\rdk0 .endm /* * AES_DEC_2_BLKS */ .macro AES_DEC_2_BLKS key blk0 blk1 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec_2_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_2_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 .endm /* * AES_DEC_3_BLKS */ .macro AES_DEC_3_BLKS key blk0 blk1 blk2 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .align 3 .Loop_dec_3_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk2,\rdk1 aesimc \blk2,\blk2 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_3_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 .endm /* * AES_DEC_4_BLKS */ .macro AES_DEC_4_BLKS key blk0 blk1 blk2 blk3 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec_4_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk2,\rdk1 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk3,\rdk1 aesimc \blk3,\blk3 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_4_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 aesd \blk3,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 .endm /* * AES_DEC_5_BLKS */ .macro AES_DEC_5_BLKS key blk0 blk1 blk2 blk3 blk4 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec_5_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk2,\rdk1 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk3,\rdk1 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk4,\rdk1 aesimc \blk4,\blk4 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_5_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 aesd \blk3,\rdk1 aesd \blk4,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 .endm /* * AES_DEC_6_BLKS */ .macro AES_DEC_6_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec_6_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk2,\rdk1 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk3,\rdk1 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk4,\rdk1 aesimc \blk4,\blk4 aesd \blk5,\rdk0 aesimc \blk5,\blk5 aesd \blk5,\rdk1 aesimc \blk5,\blk5 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_6_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk5,\rdk0 aesimc \blk5,\blk5 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 aesd \blk3,\rdk1 aesd \blk4,\rdk1 aesd \blk5,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 .endm /* * AES_DEC_7_BLKS */ .macro AES_DEC_7_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 blk6 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .Loop_dec_7_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk2,\rdk1 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk3,\rdk1 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk4,\rdk1 aesimc \blk4,\blk4 aesd \blk5,\rdk0 aesimc \blk5,\blk5 aesd \blk5,\rdk1 aesimc \blk5,\blk5 aesd \blk6,\rdk0 aesimc \blk6,\blk6 aesd \blk6,\rdk1 aesimc \blk6,\blk6 ld1 {\rdk0s,\rdk1s},[\key],#32 subs \rounds,\rounds,#2 b.gt .Loop_dec_7_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk5,\rdk0 aesimc \blk5,\blk5 aesd \blk6,\rdk0 aesimc \blk6,\blk6 ld1 {\rdk0s},[\key] aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 aesd \blk3,\rdk1 aesd \blk4,\rdk1 aesd \blk5,\rdk1 aesd \blk6,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 eor \blk6,\blk6,\rdk0 .endm /* * AES_DEC_8_BLKS */ .macro AES_DEC_8_BLKS key blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 rdk0s rdk1s rdk0 rdk1 rounds ldr \rounds,[\key,#240] ld1 {\rdk0s,\rdk1s},[\key],#32 sub \rounds,\rounds,#2 .align 5 .Loop_dec_8_blks: aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk5,\rdk0 aesimc \blk5,\blk5 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk6,\rdk0 aesimc \blk6,\blk6 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk7,\rdk0 aesimc \blk7,\blk7 aesd \blk0,\rdk1 aesimc \blk0,\blk0 aesd \blk5,\rdk1 aesimc \blk5,\blk5 aesd \blk1,\rdk1 aesimc \blk1,\blk1 aesd \blk6,\rdk1 aesimc \blk6,\blk6 aesd \blk2,\rdk1 aesimc \blk2,\blk2 aesd \blk3,\rdk1 aesimc \blk3,\blk3 aesd \blk4,\rdk1 aesimc \blk4,\blk4 aesd \blk7,\rdk1 ld1 {\rdk0s, \rdk1s},[\key],#32 aesimc \blk7,\blk7 subs \rounds,\rounds,#2 b.gt .Loop_dec_8_blks aesd \blk0,\rdk0 aesimc \blk0,\blk0 aesd \blk1,\rdk0 aesimc \blk1,\blk1 aesd \blk2,\rdk0 aesimc \blk2,\blk2 aesd \blk3,\rdk0 aesimc \blk3,\blk3 aesd \blk4,\rdk0 aesimc \blk4,\blk4 aesd \blk5,\rdk0 aesimc \blk5,\blk5 aesd \blk6,\rdk0 aesimc \blk6,\blk6 aesd \blk7,\rdk0 ld1 {\rdk0s},[\key] aesimc \blk7,\blk7 aesd \blk0,\rdk1 aesd \blk1,\rdk1 aesd \blk2,\rdk1 aesd \blk3,\rdk1 aesd \blk4,\rdk1 aesd \blk5,\rdk1 aesd \blk6,\rdk1 aesd \blk7,\rdk1 eor \blk0,\blk0,\rdk0 eor \blk1,\blk1,\rdk0 eor \blk2,\blk2,\rdk0 eor \blk3,\blk3,\rdk0 eor \blk4,\blk4,\rdk0 eor \blk5,\blk5,\rdk0 eor \blk6,\blk6,\rdk0 eor \blk7,\blk7,\rdk0 .endm #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_macro_armv8.s
Unix Assembly
unknown
19,831
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES .file "crypt_aes_macro_x86_64.s" /* AES_ENC_1_BLK */ .macro AES_ENC_1_BLK key round rdk blk .align 16 .Laesenc_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk decl \round jnz .Laesenc_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk .endm /* AES_ENC_2_BLKS */ .macro AES_ENC_2_BLKS key round rdk blk0 blk1 .align 16 .Laesenc_2_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 decl \round jnz .Laesenc_2_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 .endm /* AES_ENC_3_BLKS */ .macro AES_ENC_3_BLKS key round rdk blk0 blk1 blk2 .align 16 .Laesenc_3_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 decl \round jnz .Laesenc_3_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 .endm /* AES_ENC_4_BLKS */ .macro AES_ENC_4_BLKS key round rdk blk0 blk1 blk2 blk3 .align 16 .Laesenc_4_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 decl \round jnz .Laesenc_4_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 .endm /* AES_ENC_5_BLKS */ .macro AES_ENC_5_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 .align 16 .Laesenc_5_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 aesenc \rdk, \blk4 decl \round jnz .Laesenc_5_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 aesenclast \rdk, \blk4 .endm /* AES_ENC_6_BLKS */ .macro AES_ENC_6_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 .align 16 .Laesenc_6_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 aesenc \rdk, \blk4 aesenc \rdk, \blk5 decl \round jnz .Laesenc_6_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 aesenclast \rdk, \blk4 aesenclast \rdk, \blk5 .endm /* AES_ENC_7_BLKS */ .macro AES_ENC_7_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 .align 16 .Laesenc_7_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 aesenc \rdk, \blk4 aesenc \rdk, \blk5 aesenc \rdk, \blk6 decl \round jnz .Laesenc_7_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 aesenclast \rdk, \blk4 aesenclast \rdk, \blk5 aesenclast \rdk, \blk6 .endm /* AES_ENC_8_BLKS */ .macro AES_ENC_8_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 .align 16 .Laesenc_8_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 aesenc \rdk, \blk4 aesenc \rdk, \blk5 aesenc \rdk, \blk6 aesenc \rdk, \blk7 decl \round jnz .Laesenc_8_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 aesenclast \rdk, \blk4 aesenclast \rdk, \blk5 aesenclast \rdk, \blk6 aesenclast \rdk, \blk7 .endm /* AES_ENC_14_BLKS */ .macro AES_ENC_14_BLKS ARG2 key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 blk8 blk9 blk10 blk11 blk12 blk13 .align 16 .Laesenc_14_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesenc \rdk, \blk0 aesenc \rdk, \blk1 aesenc \rdk, \blk2 aesenc \rdk, \blk3 aesenc \rdk, \blk4 aesenc \rdk, \blk5 aesenc \rdk, \blk6 aesenc \rdk, \blk7 aesenc \rdk, \blk8 aesenc \rdk, \blk9 aesenc \rdk, \blk10 aesenc \rdk, \blk11 aesenc \rdk, \blk12 aesenc \rdk, \blk13 decl \round jnz .Laesenc_14_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesenclast \rdk, \blk0 aesenclast \rdk, \blk1 aesenclast \rdk, \blk2 aesenclast \rdk, \blk3 aesenclast \rdk, \blk4 aesenclast \rdk, \blk5 aesenclast \rdk, \blk6 aesenclast \rdk, \blk7 aesenclast \rdk, \blk8 aesenclast \rdk, \blk9 aesenclast \rdk, \blk10 aesenclast \rdk, \blk11 aesenclast \rdk, \blk12 aesenclast \rdk, \blk13 .endm /* AES_DEC_1_BLK */ .macro AES_DEC_1_BLK key round rdk blk .align 16 .Laesdec_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk decl \round jnz .Laesdec_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk .endm /* AES_DEC_2_BLKS */ .macro AES_DEC_2_BLKS key round rdk blk0 blk1 .align 32 .Laesdec_2_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 decl \round jnz .Laesdec_2_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 .endm /* AES_DEC_3_BLKS */ .macro AES_DEC_3_BLKS key round rdk blk0 blk1 blk2 .align 16 .Laesdec_3_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 decl \round jnz .Laesdec_3_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 .endm /* AES_DEC_4_BLKS */ .macro AES_DEC_4_BLKS key round rdk blk0 blk1 blk2 blk3 .align 16 .Laesdec_4_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 decl \round jnz .Laesdec_4_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 .endm /* AES_DEC_5_BLKS */ .macro AES_DEC_5_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 .align 16 .Laesdec_5_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 aesdec \rdk, \blk4 decl \round jnz .Laesdec_5_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 aesdeclast \rdk, \blk4 .endm /* AES_DEC_6_BLKS */ .macro AES_DEC_6_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 .align 16 .Laesdec_6_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 aesdec \rdk, \blk4 aesdec \rdk, \blk5 decl \round jnz .Laesdec_6_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 aesdeclast \rdk, \blk4 aesdeclast \rdk, \blk5 .endm /* AES_DEC_7_BLKS */ .macro AES_DEC_7_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 .align 16 .Laesdec_7_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 aesdec \rdk, \blk4 aesdec \rdk, \blk5 aesdec \rdk, \blk6 decl \round jnz .Laesdec_7_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 aesdeclast \rdk, \blk4 aesdeclast \rdk, \blk5 aesdeclast \rdk, \blk6 .endm /* AES_DEC_8_BLKS */ .macro AES_DEC_8_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 .align 16 .Laesdec_8_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 aesdec \rdk, \blk4 aesdec \rdk, \blk5 aesdec \rdk, \blk6 aesdec \rdk, \blk7 decl \round jnz .Laesdec_8_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 aesdeclast \rdk, \blk4 aesdeclast \rdk, \blk5 aesdeclast \rdk, \blk6 aesdeclast \rdk, \blk7 .endm /* AES_DEC_14_BLKS */ .macro AES_DEC_14_BLKS key round rdk blk0 blk1 blk2 blk3 blk4 blk5 blk6 blk7 blk8 blk9 blk10 blk11 blk12 blk13 .align 16 .Laesdec_14_blks_loop: leaq 16(\key), \key movdqu (\key), \rdk aesdec \rdk, \blk0 aesdec \rdk, \blk1 aesdec \rdk, \blk2 aesdec \rdk, \blk3 aesdec \rdk, \blk4 aesdec \rdk, \blk5 aesdec \rdk, \blk6 aesdec \rdk, \blk7 aesdec \rdk, \blk8 aesdec \rdk, \blk9 aesdec \rdk, \blk10 aesdec \rdk, \blk11 aesdec \rdk, \blk12 aesdec \rdk, \blk13 decl \round jnz .Laesdec_14_blks_loop leaq 16(\key), \key movdqu (\key), \rdk aesdeclast \rdk, \blk0 aesdeclast \rdk, \blk1 aesdeclast \rdk, \blk2 aesdeclast \rdk, \blk3 aesdeclast \rdk, \blk4 aesdeclast \rdk, \blk5 aesdeclast \rdk, \blk6 aesdeclast \rdk, \blk7 aesdeclast \rdk, \blk8 aesdeclast \rdk, \blk9 aesdeclast \rdk, \blk10 aesdeclast \rdk, \blk11 aesdeclast \rdk, \blk12 aesdeclast \rdk, \blk13 .endm #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_macro_x86_64.s
Unix Assembly
unknown
10,609
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "crypt_aes_macro_x86_64.s" .file "crypt_aes_x86_64.S" .text .set ARG1, %rdi .set ARG2, %rsi .set ARG3, %rdx .set ARG4, %rcx .set ARG5, %r8 .set ARG6, %r9 .set RET, %eax .set XM0, %xmm0 .set XM1, %xmm1 .set XM2, %xmm2 .set XM3, %xmm3 .set XM4, %xmm4 .set XM5, %xmm5 /** * aes128 macros for key extension processing. */ .macro KEY_EXPANSION_HELPER_128 xm0 xm1 xm2 vpermilps $0xff, \xm1, \xm1 vpslldq $4, \xm0, \xm2 vpxor \xm2, \xm0, \xm0 vpslldq $4, \xm2, \xm2 vpxor \xm2, \xm0, \xm0 vpslldq $4, \xm2, \xm2 vpxor \xm2, \xm0, \xm0 vpxor \xm1, \xm0, \xm0 .endm /** * aes192 macros for key extension processing. */ .macro KEY_EXPANSION_HELPER_192 xm1 xm3 vpslldq $4, \xm1, \xm3 vpxor \xm3, \xm1, \xm1 vpslldq $4, \xm3, \xm3 vpxor \xm3, \xm1, \xm1 vpslldq $4, \xm3, \xm3 vpxor \xm3, \xm1, \xm1 .endm /** * Function description: Sets the AES encryption key. Key length: 128 bits. * Function prototype: void SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register:xmm0-xmm2. * Output register:None. * Function/Macro Call: None. */ .globl SetEncryptKey128 .type SetEncryptKey128, @function SetEncryptKey128: .cfi_startproc movl $10, 240(%rdi) movdqu (ARG2), XM0 movdqu XM0, (ARG1) aeskeygenassist $0x01, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 16(ARG1) aeskeygenassist $0x02, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 32(ARG1) aeskeygenassist $0x04, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 48(ARG1) aeskeygenassist $0x08, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 64(ARG1) aeskeygenassist $0x10, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 80(ARG1) aeskeygenassist $0x20, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 96(ARG1) aeskeygenassist $0x40, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 112(ARG1) aeskeygenassist $0x80, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 128(ARG1) aeskeygenassist $0x1b, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 144(ARG1) aeskeygenassist $0x36, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0, 160(ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 ret .cfi_endproc .size SetEncryptKey128, .-SetEncryptKey128 /** * Function description: Sets the AES decryption key. Key length: 128 bits. * Function prototype: void SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register:xmm0-xmm3. * Output register: None. * Function/Macro Call: None. */ .globl SetDecryptKey128 .type SetDecryptKey128, @function SetDecryptKey128: .cfi_startproc movl $10, 240(%rdi) movdqu (ARG2), XM0 movdqu XM0, 160(ARG1) aeskeygenassist $0x01, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 144(ARG1) aeskeygenassist $0x02, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 128(ARG1) aeskeygenassist $0x04, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 112(ARG1) aeskeygenassist $0x08, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 96(ARG1) aeskeygenassist $0x10, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 80(ARG1) aeskeygenassist $0x20, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 64(ARG1) aeskeygenassist $0x40, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 48(ARG1) aeskeygenassist $0x80, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 32(ARG1) aeskeygenassist $0x1b, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 aesimc XM0, XM3 movdqu XM3, 16(ARG1) aeskeygenassist $0x36, XM0, XM1 KEY_EXPANSION_HELPER_128 XM0, XM1, XM2 movdqu XM0,(ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 vpxor XM3, XM3, XM3 ret .cfi_endproc .size SetDecryptKey128, .-SetDecryptKey128 /** * Function description: Sets the AES encryption key. Key length: 192 bits. * Function prototype: void SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register: xmm0-xmm4. * Output register: None. * Function/Macro Call: None. */ .globl SetEncryptKey192 .type SetEncryptKey192, @function SetEncryptKey192: .cfi_startproc movl $12, 240(ARG1) movdqu (ARG2), XM0 movdqu 8(ARG2), XM1 movdqu XM0,(ARG1) vpxor XM4, XM4, XM4 vshufps $0x40, XM0, XM4, XM2 aeskeygenassist $0x01, XM1, XM0 vshufps $0xf0, XM0, XM4, XM0 vpslldq $0x04, XM2, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM0, XM0 vshufps $0xee, XM0, XM1, XM0 movdqu XM0, 16(ARG1) movdqu XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 movdqu XM2, 32(ARG1) vshufps $0x4e, XM2, XM0, XM1 aeskeygenassist $0x02, XM2, XM0 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM0 vpxor XM1, XM0, XM0 movdqu XM0, 48(ARG1) vshufps $0x4e, XM0, XM2, XM1 vpslldq $8, XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 aeskeygenassist $0x04, XM2, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM2, XM2 movdqu XM2, 64(ARG1) vshufps $0x4e, XM2, XM0, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM0 vpxor XM1, XM0, XM0 movdqu XM0, 80(ARG1) vshufps $0x4e, XM0, XM2, XM1 aeskeygenassist $0x08, XM0, XM2 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM2 vpxor XM1, XM2, XM2 movdqu XM2, 96(ARG1) vshufps $0x4e, XM2, XM0, XM1 vpslldq $8, XM1, XM0 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpermilps $0xff, XM2, XM3 vpxor XM3, XM0, XM0 aeskeygenassist $0x10, XM0, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM0, XM0 movdqu XM0, 112(ARG1) vshufps $0x4e, XM0, XM2, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM2 vpxor XM1, XM2, XM2 movdqu XM2, 128(ARG1) vshufps $0x4e, XM2, XM0, XM1 aeskeygenassist $0x20, XM2, XM0 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM0 vpxor XM1, XM0, XM0 movdqu XM0, 144(ARG1) vshufps $0x4e, XM0, XM2, XM1 vpslldq $8, XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 aeskeygenassist $0x40, XM2, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM2, XM2 movdqu XM2, 160(ARG1) vshufps $0x4e, XM2, XM0, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM0 vpxor XM1, XM0, XM0 movdqu XM0, 176(ARG1) vshufps $0x4e, XM0, XM2, XM1 aeskeygenassist $0x80, XM0, XM2 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM2 vpxor XM1, XM2, XM2 movdqu XM2, 192(ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 vpxor XM3, XM3, XM3 vpxor XM4, XM4, XM4 ret .cfi_endproc .size SetEncryptKey192, .-SetEncryptKey192 /** * Function description: Sets the AES decryption key. Key length: 192 bits. * Function prototype: void SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register: xmm0-xmm5 * Output register: None. * Function/Macro Call: None. */ .globl SetDecryptKey192 .type SetDecryptKey192, @function SetDecryptKey192: .cfi_startproc movl $12, 240(ARG1) movdqu (ARG2), XM0 movdqu 8(ARG2), XM1 movdqu XM0, 192(ARG1) vpxor XM4, XM4, XM4 vshufps $0x40, XM0, XM4, XM2 aeskeygenassist $0x01, XM1, XM0 vshufps $0xf0, XM0, XM4, XM0 vpslldq $0x04, XM2, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM0, XM0 vshufps $0xee, XM0, XM1, XM0 aesimc XM0, XM5 movdqu XM5, 176(ARG1) movdqu XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 aesimc XM2, XM5 movdqu XM5, 160(ARG1) vshufps $0x4e, XM2, XM0, XM1 aeskeygenassist $0x02, XM2, XM0 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM0 vpxor XM1, XM0, XM0 aesimc XM0, XM5 movdqu XM5, 144(ARG1) vshufps $0x4e, XM0, XM2, XM1 vpslldq $8, XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 aeskeygenassist $0x04, XM2, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM2, XM2 aesimc XM2, XM5 movdqu XM5, 128(ARG1) vshufps $0x4e, XM2, XM0, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM0 vpxor XM1, XM0, XM0 aesimc XM0, XM5 movdqu XM5,112(ARG1) vshufps $0x4e, XM0, XM2, XM1 aeskeygenassist $0x08, XM0, XM2 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM2 vpxor XM1, XM2, XM2 aesimc XM2, XM5 movdqu XM5, 96(ARG1) vshufps $0x4e, XM2, XM0, XM1 vpslldq $8, XM1, XM0 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpermilps $0xff, XM2, XM3 vpxor XM3, XM0, XM0 aeskeygenassist $0x10, XM0, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM0, XM0 aesimc XM0, XM5 movdqu XM5, 80(ARG1) vshufps $0x4e, XM0, XM2, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM2 vpxor XM1, XM2, XM2 aesimc XM2, XM5 movdqu XM5, 64(ARG1) vshufps $0x4e, XM2, XM0, XM1 aeskeygenassist $0x20, XM2, XM0 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM0, XM0 vpxor XM1, XM0, XM0 aesimc XM0, XM5 movdqu XM5, 48(ARG1) vshufps $0x4e, XM0, XM2, XM1 vpslldq $8, XM1, XM2 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpermilps $0xff, XM0, XM3 vpxor XM3, XM2, XM2 aeskeygenassist $0x40, XM2, XM3 vpermilps $0xff, XM3, XM3 vpsrldq $8, XM1, XM4 vpslldq $12, XM4, XM4 vpxor XM4, XM1, XM1 vpxor XM3, XM1, XM1 vshufps $0xee, XM1, XM2, XM2 aesimc XM2, XM5 movdqu XM5, 32(ARG1) vshufps $0x4e, XM2, XM0, XM1 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM0 vpxor XM1, XM0, XM0 aesimc XM0, XM5 movdqu XM5, 16(ARG1) vshufps $0x4e, XM0, XM2, XM1 aeskeygenassist $0x80, XM0, XM2 KEY_EXPANSION_HELPER_192 XM1, XM3 vpermilps $0xff, XM2, XM2 vpxor XM1, XM2, XM2 movdqu XM2,(ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 vpxor XM3, XM3, XM3 vpxor XM4, XM4, XM4 vpxor XM5, XM5, XM5 ret .cfi_endproc .size SetDecryptKey192, .-SetDecryptKey192 /** * Function description: Sets the AES encryption key. Key length: 192 bits. * Function prototype: void SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register: xmm0-xmm3. * Output register: None. * Function/Macro Call: None. */ .globl SetEncryptKey256 .type SetEncryptKey256, @function SetEncryptKey256: .cfi_startproc movl $14, 240(ARG1) movdqu (ARG2), XM0 movdqu 16(ARG2), XM1 movdqu XM0, (ARG1) movdqu XM1, 16(ARG1) aeskeygenassist $0x01, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, 32(ARG1) aeskeygenassist $0x01, XM2, XM0 vpermilps $0xAA, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 movdqu XM0, 48(ARG1) /*2*/ aeskeygenassist $0x02, XM0, XM1 vpermilps $0xff, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 movdqu XM1, 64(ARG1) aeskeygenassist $0x02, XM1, XM2 vpermilps $0xAA, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, 80(ARG1) /*3*/ aeskeygenassist $0x04, XM2, XM0 vpermilps $0xff, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 movdqu XM0, 96(ARG1) aeskeygenassist $0x04, XM0, XM1 vpermilps $0xAA, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 movdqu XM1, 112(ARG1) /*4*/ aeskeygenassist $0x08, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, 128(ARG1) aeskeygenassist $0x08, XM2, XM0 vpermilps $0xAA, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 movdqu XM0, 144(ARG1) /*5*/ aeskeygenassist $0x10, XM0, XM1 vpermilps $0xff, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 movdqu XM1, 160(ARG1) aeskeygenassist $0x10, XM1, XM2 vpermilps $0xAA, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, 176(ARG1) /*6*/ aeskeygenassist $0x20, XM2, XM0 vpermilps $0xff, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 movdqu XM0, 192(ARG1) aeskeygenassist $0x20, XM0, XM1 vpermilps $0xAA, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 movdqu XM1, 208(ARG1) /*7*/ aeskeygenassist $0x40, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, 224(ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 vpxor XM3, XM3, XM3 ret .cfi_endproc .size SetEncryptKey256, .-SetEncryptKey256 /** * Function description: Sets the AES encryption key. Key length: 192 bits. * Function prototype: void SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); * Input register: * x0:Pointer to the output key structure. * x1:Pointer to the input key. * Change register: xmm0-xmm4. * Output register: None. * Function/Macro Call: None. */ .globl SetDecryptKey256 .type SetDecryptKey256, @function SetDecryptKey256: .cfi_startproc movl $14, 240(ARG1) movdqu (ARG2), XM0 movdqu 16(ARG2), XM1 movdqu XM0, 224(ARG1) aesimc XM1, XM4 movdqu XM4, 208(ARG1) aeskeygenassist $0x01, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 aesimc XM2, XM4 movdqu XM4, 192(ARG1) aeskeygenassist $0x01, XM2, XM0 vpermilps $0xAA, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 aesimc XM0, XM4 movdqu XM4, 176(ARG1) /*2*/ aeskeygenassist $0x02, XM0, XM1 vpermilps $0xff, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 aesimc XM1, XM4 movdqu XM4, 160(ARG1) aeskeygenassist $0x02, XM1, XM2 vpermilps $0xAA, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 aesimc XM2, XM4 movdqu XM4, 144(ARG1) /*3*/ aeskeygenassist $0x04, XM2, XM0 vpermilps $0xff, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 aesimc XM0, XM4 movdqu XM4, 128(ARG1) aeskeygenassist $0x04, XM0, XM1 vpermilps $0xAA, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 aesimc XM1, XM4 movdqu XM4, 112(ARG1) /*4*/ aeskeygenassist $0x08, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 aesimc XM2, XM4 movdqu XM4, 96(ARG1) aeskeygenassist $0x08, XM2, XM0 vpermilps $0xAA, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 aesimc XM0, XM4 movdqu XM4, 80(ARG1) /*5*/ aeskeygenassist $0x10, XM0, XM1 vpermilps $0xff, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 aesimc XM1, XM4 movdqu XM4, 64(ARG1) aeskeygenassist $0x10, XM1, XM2 vpermilps $0xAA, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 aesimc XM2, XM4 movdqu XM4, 48(ARG1) /*6*/ aeskeygenassist $0x20, XM2, XM0 vpermilps $0xff, XM0, XM0 vpslldq $4, XM1, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpslldq $4, XM3, XM3 vpxor XM3, XM1, XM1 vpxor XM1, XM0, XM0 aesimc XM0, XM4 movdqu XM4, 32(ARG1) aeskeygenassist $0x20, XM0, XM1 vpermilps $0xAA, XM1, XM1 vpslldq $4, XM2, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpslldq $4, XM3, XM3 vpxor XM3, XM2, XM2 vpxor XM2, XM1, XM1 aesimc XM1, XM4 movdqu XM4, 16(ARG1) /*7*/ aeskeygenassist $0x40, XM1, XM2 vpermilps $0xff, XM2, XM2 vpslldq $4, XM0, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpslldq $4, XM3, XM3 vpxor XM3, XM0, XM0 vpxor XM0, XM2, XM2 movdqu XM2, (ARG1) vpxor XM0, XM0, XM0 vpxor XM1, XM1, XM1 vpxor XM2, XM2, XM2 vpxor XM3, XM3, XM3 vpxor XM4, XM4, XM4 ret .cfi_endproc .size SetDecryptKey256, .-SetDecryptKey256 /** * Function description: This API is used to set the AES encryption assembly acceleration. * Function prototype: int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0:Pointer to the input key structure. * x1:Points to the 128-bit input data. * x2:Points to the 128-bit output data. * x3:Indicates the length of a data block, that is, 16 bytes. * Change register: xmm0-xmm1. * Output register: eax. * Function/Macro Call: None. */ .globl CRYPT_AES_Encrypt .type CRYPT_AES_Encrypt, @function CRYPT_AES_Encrypt: .cfi_startproc .set ROUNDS,%eax movdqu (ARG2), XM0 movl 240(ARG1),ROUNDS vpxor (ARG1), XM0, XM0 movdqu 16(ARG1), XM1 aesenc XM1, XM0 movdqu 32(ARG1), XM1 aesenc XM1, XM0 movdqu 48(ARG1), XM1 aesenc XM1, XM0 movdqu 64(ARG1), XM1 aesenc XM1, XM0 movdqu 80(ARG1), XM1 aesenc XM1, XM0 movdqu 96(ARG1), XM1 aesenc XM1, XM0 movdqu 112(ARG1), XM1 aesenc XM1, XM0 movdqu 128(ARG1), XM1 aesenc XM1, XM0 movdqu 144(ARG1), XM1 aesenc XM1, XM0 cmpl $10,ROUNDS je .Laesenc_128 movdqu 160(ARG1), XM1 aesenc XM1, XM0 movdqu 176(ARG1), XM1 aesenc XM1, XM0 cmpl $12,ROUNDS je .Laesenc_192 movdqu 192(ARG1), XM1 aesenc XM1, XM0 movdqu 208(ARG1), XM1 aesenc XM1, XM0 cmpl $14,ROUNDS je .Laesenc_256 .Laesenc_128: movdqu 160(ARG1), XM1 aesenclast XM1, XM0 jmp .Laesenc_end .Laesenc_192: movdqu 192(ARG1), XM1 aesenclast XM1, XM0 jmp .Laesenc_end .Laesenc_256: movdqu 224(ARG1), XM1 aesenclast XM1, XM0 .Laesenc_end: vpxor XM1, XM1, XM1 movdqu XM0,(ARG3) vpxor XM0, XM0, XM0 movl $0,RET ret .cfi_endproc .size CRYPT_AES_Encrypt, .-CRYPT_AES_Encrypt /** * Function description: AES decryption and assembly acceleration API. * Function prototype: int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0:Pointer to the input key structure. * x1:Points to the 128-bit input data. * x2:Points to the 128-bit output data. * x3:Indicates the length of a data block, that is, 16 bytes. * Change register: xmm0-xmm1. * Output register: eax. * Function/Macro Call: None. */ .globl CRYPT_AES_Decrypt .type CRYPT_AES_Decrypt, @function CRYPT_AES_Decrypt: .cfi_startproc .set ROUNDS,%eax movdqu (ARG2), XM0 movl 240(ARG1),ROUNDS vpxor (ARG1), XM0, XM0 movdqu 16(ARG1), XM1 aesdec XM1, XM0 movdqu 32(ARG1), XM1 aesdec XM1, XM0 movdqu 48(ARG1), XM1 aesdec XM1, XM0 movdqu 64(ARG1), XM1 aesdec XM1, XM0 movdqu 80(ARG1), XM1 aesdec XM1, XM0 movdqu 96(ARG1), XM1 aesdec XM1, XM0 movdqu 112(ARG1), XM1 aesdec XM1, XM0 movdqu 128(ARG1), XM1 aesdec XM1, XM0 movdqu 144(ARG1), XM1 aesdec XM1, XM0 cmpl $10,ROUNDS je .aesdec_128 movdqu 160(ARG1), XM1 aesdec XM1, XM0 movdqu 176(ARG1), XM1 aesdec XM1, XM0 cmpl $12,ROUNDS je .aesdec_192 movdqu 192(ARG1), XM1 aesdec XM1, XM0 movdqu 208(ARG1), XM1 aesdec XM1, XM0 cmpl $14,ROUNDS je .aesdec_256 .aesdec_128: movdqu 160(ARG1), XM1 aesdeclast XM1, XM0 jmp .aesdec_end .aesdec_192: movdqu 192(ARG1), XM1 aesdeclast XM1, XM0 jmp .aesdec_end .aesdec_256: movdqu 224(ARG1), XM1 aesdeclast XM1, XM0 .aesdec_end: vpxor XM1, XM1, XM1 movdqu XM0,(ARG3) vpxor XM0, XM0, XM0 movl $0,RET ret .cfi_endproc .size CRYPT_AES_Decrypt, .-CRYPT_AES_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_x86_64.S
Motorola 68K Assembly
unknown
25,400
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_XTS) #include "crypt_aes_macro_armv8.s" #include "crypt_arm.h" .file "crypt_aes_xts_armv8.S" .text .arch armv8-a+crypto KEY .req x0 IN .req x1 OUT .req x2 LEN .req x3 TWEAK .req x4 TMPOUT .req x17 WP .req w11 WC .req w12 KTMP .req x5 LTMP .req x6 TAILNUM .req x8 POS .req x16 ROUNDS .req w7 XROUNDS .req x7 TROUNDS .req w15 WTMP0 .req w9 WTMP1 .req w10 WTMP2 .req w11 WTMP3 .req w12 XTMP1 .req x10 XTMP2 .req x11 TWX0 .req x13 TWX1 .req x14 TWW1 .req w14 BLK0 .req v0 BLK1 .req v1 BLK2 .req v2 BLK3 .req v3 BLK4 .req v4 IN0 .req v5 IN1 .req v6 IN2 .req v7 IN3 .req v30 IN4 .req v31 TWK0 .req v8 TWK1 .req v9 TWK2 .req v10 TWK3 .req v11 TWK4 .req v12 TWKD00 .req d8 TWKD10 .req d9 TWKD20 .req d10 TWKD30 .req d11 TWKD40 .req d12 #define TWKD01 v8.d[1] #define TWKD11 v9.d[1] #define TWKD21 v10.d[1] #define TWKD31 v11.d[1] #define TWKD41 v12.d[1] RDK0 .req v16 RDK1 .req v17 RDK2 .req v18 RDK3 .req v19 RDK4 .req v20 RDK5 .req v21 RDK6 .req v22 RDK7 .req v23 RDK8 .req v24 TMP0 .req v25 TMP1 .req v26 TMP2 .req v27 TMP3 .req v28 TMP4 .req v29 #define MOV_REG_TO_VEC(SRC0, SRC1, DES0, DES1) \ fmov DES0,SRC0 ; \ fmov DES1,SRC1 ; \ .macro NextTweak twkl, twkh, twkd0, twkd1 asr XTMP2,\twkh,#63 extr \twkh,\twkh,\twkl,#63 and WTMP1,WTMP0,WTMP2 eor \twkl,XTMP1,\twkl,lsl#1 fmov \twkd0,\twkl // must set lower bits of 'q' register first.1 fmov \twkd1,\twkh // Set lower bits using 'd' register will clear higer bits. .endm .macro AesCrypt1x en, mc, d0, rk aes\en \d0\().16b, \rk\().16b aes\mc \d0\().16b, \d0\().16b .endm .macro AesEncrypt1x d0, rk AesCrypt1x e, mc, \d0, \rk .endm .macro AesDecrypt1x d0, rk AesCrypt1x d, imc, \d0, \rk .endm /** * int32_t CRYPT_AES_XTS_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, const uint8_t *tweak); */ .globl CRYPT_AES_XTS_Encrypt .type CRYPT_AES_XTS_Encrypt, %function .align 4 CRYPT_AES_XTS_Encrypt: AARCH64_PACIASP stp x29, x30, [sp,#-80]! add x29, sp, #0 stp d8, d9, [sp,#16] stp d10, d11, [sp,#32] stp d12, d13, [sp,#48] stp d14, d15, [sp,#64] ld1 {TWK0.16b}, [TWEAK] and TAILNUM, LEN, #0xF // get tail num, LEN % 16 and LTMP, LEN, #-16 mov WTMP0,0x87 ldr ROUNDS,[KEY,#240] fmov TWX0,TWKD00 fmov TWX1,TWKD01 sub ROUNDS,ROUNDS,#6 // perload last 7 rounds key add KTMP,KEY,XROUNDS,lsl#4 ld1 {RDK2.4s,RDK3.4s},[KTMP],#32 ld1 {RDK4.4s,RDK5.4s},[KTMP],#32 ld1 {RDK6.4s,RDK7.4s},[KTMP],#32 ld1 {RDK8.4s},[KTMP] .Lxts_aesenc_start: cmp LTMP, #80 b.ge .Lxts_enc_proc_5_blks cmp LTMP, #48 b.ge .Lxts_enc_proc_3_blks cmp LTMP, #32 b.eq .Lxts_enc_proc_2_blks cmp LTMP, #16 b.eq .Lxts_enc_proc_1blk .Lxtx_tail_blk: fmov TWX0,TWKD00 // reset already computed tweak fmov TWX1,TWKD01 cbz TAILNUM,.Lxts_aesenc_finish // prepare encrypt tail block sub TMPOUT,OUT,#16 .Lxtx_tail_blk_loop: subs TAILNUM,TAILNUM,1 ldrb WC,[TMPOUT,TAILNUM] ldrb WP,[IN,TAILNUM] strb WC,[OUT,TAILNUM] strb WP,[TMPOUT,TAILNUM] b.gt .Lxtx_tail_blk_loop ld1 {BLK0.16b}, [TMPOUT] mov LTMP,#16 mov OUT,TMPOUT b .Lxts_enc_proc_1blk_loaded cbz LTMP,.Lxts_aesenc_finish .Lxts_enc_proc_1blk: ld1 {BLK0.16b},[IN],#16 .Lxts_enc_proc_1blk_loaded: eor BLK0.16b,BLK0.16b,TWK0.16b mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 .Lxts_rounds_1blks: AesEncrypt1x BLK0,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesEncrypt1x BLK0,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_rounds_1blks AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK0,RDK1 // last 7 rounds AesEncrypt1x BLK0,RDK2 AesEncrypt1x BLK0,RDK3 AesEncrypt1x BLK0,RDK4 AesEncrypt1x BLK0,RDK5 AesEncrypt1x BLK0,RDK6 aese BLK0.16b,RDK7.16b // final round eor BLK0.16b,BLK0.16b,RDK8.16b eor BLK0.16b,BLK0.16b,TWK0.16b st1 {BLK0.16b}, [OUT], #16 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#16 b.hs .Lxts_aesenc_start .Lxts_enc_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN], #32 mov KTMP, KEY NextTweak TWX0,TWX1,TWKD10,TWKD11 ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 eor BLK0.16b, BLK0.16b, TWK0.16b eor BLK1.16b, BLK1.16b, TWK1.16b .Lxts_rounds_2blks: AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_rounds_2blks AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 // last 7 rounds AesEncrypt1x BLK0,RDK2 AesEncrypt1x BLK1,RDK2 AesEncrypt1x BLK0,RDK3 AesEncrypt1x BLK1,RDK3 AesEncrypt1x BLK0,RDK4 AesEncrypt1x BLK1,RDK4 AesEncrypt1x BLK0,RDK5 AesEncrypt1x BLK1,RDK5 AesEncrypt1x BLK0,RDK6 AesEncrypt1x BLK1,RDK6 eor TWK0.16b,TWK0.16b,RDK8.16b eor TWK1.16b,TWK1.16b,RDK8.16b aese BLK0.16b,RDK7.16b // final round aese BLK1.16b,RDK7.16b eor BLK0.16b,BLK0.16b,TWK0.16b eor BLK1.16b,BLK1.16b,TWK1.16b st1 {BLK0.16b, BLK1.16b}, [OUT], #32 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#32 b.hs .Lxts_aesenc_start .Lxts_enc_proc_3_blks: ld1 {BLK0.16b}, [IN], #16 // first block NextTweak TWX0,TWX1,TWKD10,TWKD11 eor BLK0.16b,BLK0.16b,TWK0.16b ld1 {BLK1.16b}, [IN], #16 // second block NextTweak TWX0,TWX1,TWKD20,TWKD21 eor BLK1.16b,BLK1.16b,TWK1.16b ld1 {BLK2.16b}, [IN], #16 // third block eor BLK2.16b,BLK2.16b,TWK2.16b mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 .Lxts_rounds_3blks: AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 AesEncrypt1x BLK2,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 AesEncrypt1x BLK2,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_rounds_3blks AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 AesEncrypt1x BLK2,RDK0 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 AesEncrypt1x BLK2,RDK1 // last 7 rounds AesEncrypt1x BLK0,RDK2 AesEncrypt1x BLK1,RDK2 AesEncrypt1x BLK2,RDK2 AesEncrypt1x BLK0,RDK3 AesEncrypt1x BLK1,RDK3 AesEncrypt1x BLK2,RDK3 AesEncrypt1x BLK0,RDK4 AesEncrypt1x BLK1,RDK4 AesEncrypt1x BLK2,RDK4 AesEncrypt1x BLK0,RDK5 AesEncrypt1x BLK1,RDK5 AesEncrypt1x BLK2,RDK5 AesEncrypt1x BLK0,RDK6 AesEncrypt1x BLK1,RDK6 AesEncrypt1x BLK2,RDK6 eor TWK0.16b,TWK0.16b,RDK8.16b eor TWK1.16b,TWK1.16b,RDK8.16b eor TWK2.16b,TWK2.16b,RDK8.16b aese BLK0.16b,RDK7.16b aese BLK1.16b,RDK7.16b aese BLK2.16b,RDK7.16b eor BLK0.16b,BLK0.16b,TWK0.16b eor BLK1.16b,BLK1.16b,TWK1.16b eor BLK2.16b,BLK2.16b,TWK2.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT], #48 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#48 b.hs .Lxts_aesenc_start .align 4 .Lxts_enc_proc_5_blks: ld1 {BLK0.16b}, [IN], #16 // first block NextTweak TWX0,TWX1,TWKD10,TWKD11 eor BLK0.16b,BLK0.16b,TWK0.16b ld1 {BLK1.16b}, [IN], #16 // second block NextTweak TWX0,TWX1,TWKD20,TWKD21 eor BLK1.16b,BLK1.16b,TWK1.16b sub LTMP,LTMP,#32 ld1 {BLK2.16b}, [IN], #16 // third block NextTweak TWX0,TWX1,TWKD30,TWKD31 eor BLK2.16b,BLK2.16b,TWK2.16b ld1 {BLK3.16b}, [IN], #16 // fourth block NextTweak TWX0,TWX1,TWKD40,TWKD41 eor BLK3.16b,BLK3.16b,TWK3.16b sub LTMP,LTMP,#32 ld1 {BLK4.16b}, [IN], #16 // fifth block eor BLK4.16b, BLK4.16b, TWK4.16b sub LTMP,LTMP,#16 mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 .align 4 .Lxts_rounds_5blks: AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 AesEncrypt1x BLK2,RDK0 AesEncrypt1x BLK3,RDK0 AesEncrypt1x BLK4,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 AesEncrypt1x BLK2,RDK1 AesEncrypt1x BLK3,RDK1 AesEncrypt1x BLK4,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_rounds_5blks AesEncrypt1x BLK0,RDK0 AesEncrypt1x BLK1,RDK0 AesEncrypt1x BLK2,RDK0 AesEncrypt1x BLK3,RDK0 AesEncrypt1x BLK4,RDK0 subs LTMP,LTMP,#80 AesEncrypt1x BLK0,RDK1 AesEncrypt1x BLK1,RDK1 AesEncrypt1x BLK2,RDK1 AesEncrypt1x BLK3,RDK1 AesEncrypt1x BLK4,RDK1 // last 7 rounds AesEncrypt1x BLK0,RDK2 AesEncrypt1x BLK1,RDK2 AesEncrypt1x BLK2,RDK2 AesEncrypt1x BLK3,RDK2 AesEncrypt1x BLK4,RDK2 csel POS,xzr,LTMP,gt // AesEncrypt1x BLK0,RDK3 AesEncrypt1x BLK1,RDK3 AesEncrypt1x BLK2,RDK3 AesEncrypt1x BLK3,RDK3 AesEncrypt1x BLK4,RDK3 add IN,IN,POS AesEncrypt1x BLK0,RDK4 AesEncrypt1x BLK1,RDK4 AesEncrypt1x BLK2,RDK4 AesEncrypt1x BLK3,RDK4 AesEncrypt1x BLK4,RDK4 AesEncrypt1x BLK0,RDK5 AesEncrypt1x BLK1,RDK5 AesEncrypt1x BLK2,RDK5 AesEncrypt1x BLK3,RDK5 AesEncrypt1x BLK4,RDK5 AesEncrypt1x BLK0,RDK6 AesEncrypt1x BLK1,RDK6 AesEncrypt1x BLK2,RDK6 AesEncrypt1x BLK3,RDK6 AesEncrypt1x BLK4,RDK6 eor TMP0.16b,TWK0.16b,RDK8.16b aese BLK0.16b,RDK7.16b // final round NextTweak TWX0,TWX1,TWKD00,TWKD01 // perform operations of next 5blks in advance eor TMP1.16b,TWK1.16b,RDK8.16b ld1 {IN0.16b}, [IN], #16 aese BLK1.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD10,TWKD11 eor TMP2.16b,TWK2.16b,RDK8.16b ld1 {IN1.16b}, [IN], #16 aese BLK2.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD20,TWKD21 eor TMP3.16b,TWK3.16b,RDK8.16b ld1 {IN2.16b}, [IN], #16 aese BLK3.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD30,TWKD31 eor TMP4.16b,TWK4.16b,RDK8.16b ld1 {IN3.16b}, [IN], #16 aese BLK4.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD40,TWKD41 ld1 {IN4.16b}, [IN], #16 mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 eor TMP0.16b,TMP0.16b,BLK0.16b eor BLK0.16b,IN0.16b,TWK0.16b // blk0 = in0 ^ twk0 eor TMP1.16b,TMP1.16b,BLK1.16b eor BLK1.16b,IN1.16b,TWK1.16b st1 {TMP0.16b}, [OUT], #16 eor TMP2.16b,TMP2.16b,BLK2.16b eor BLK2.16b,IN2.16b,TWK2.16b eor TMP3.16b,TMP3.16b,BLK3.16b eor BLK3.16b,IN3.16b,TWK3.16b st1 {TMP1.16b}, [OUT], #16 eor TMP4.16b,TMP4.16b,BLK4.16b eor BLK4.16b,IN4.16b,TWK4.16b st1 {TMP2.16b}, [OUT], #16 sub TROUNDS,ROUNDS,#2 st1 {TMP3.16b,TMP4.16b}, [OUT], #32 b.hs .Lxts_rounds_5blks add LTMP,LTMP,#80 // add 5 blocks length back if LTMP < 0 cbz LTMP,.Lxtx_tail_blk cmp LTMP, #16 b.eq .Lxts_pre_last_1blks cmp LTMP,#32 b.eq .Lxts_pre_last_2blks cmp LTMP,#48 b.eq .Lxts_pre_last_3blks cmp LTMP,#64 b.eq .Lxts_pre_last_4blks .Lxts_pre_last_1blks: eor IN0.16b,IN0.16b,IN4.16b //in0 = in0 ^ in41 eor BLK0.16b,BLK0.16b,IN0.16b // blk0 = in0 ^ twk0 ^ in0 ^ in4 fmov TWX0,TWKD00 // reset already computed tweak fmov TWX1,TWKD01 b .Lxts_rounds_1blks .Lxts_pre_last_2blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK0.16b,BLK0.16b,IN3.16b // in3 -> blk0 eor BLK1.16b,BLK1.16b,IN4.16b // in4 -> blk1 fmov TWX0,TWKD10 // reset already computed tweak fmov TWX1,TWKD11 b .Lxts_rounds_2blks .Lxts_pre_last_3blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK2.16b,BLK2.16b,IN2.16b eor BLK0.16b,BLK0.16b,IN2.16b // in2 -> blk0 eor BLK1.16b,BLK1.16b,IN3.16b // in3 -> blk1 eor BLK2.16b,BLK2.16b,IN4.16b // in4 -> blk2 fmov TWX0,TWKD20 // reset already computed tweak fmov TWX1,TWKD21 b .Lxts_rounds_3blks .Lxts_pre_last_4blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK2.16b,BLK2.16b,IN2.16b eor BLK3.16b,BLK3.16b,IN3.16b sub IN,IN,#16 // have loaded 4blks, using 3blks to process, so step back 1blk here eor BLK0.16b,BLK0.16b,IN1.16b // in1 -> blk0 eor BLK1.16b,BLK1.16b,IN2.16b // in2 -> blk1 eor BLK2.16b,BLK2.16b,IN3.16b // in3 -> blk2 eor BLK3.16b,BLK3.16b,IN4.16b // in4 -> blk3 fmov TWX0,TWKD20 // reset already computed tweak fmov TWX1,TWKD21 b .Lxts_rounds_3blks .Lxts_aesenc_finish: MOV_REG_TO_VEC(TWX0,TWX1,TWKD00,TWKD01) st1 {TWK0.16b}, [TWEAK] mov x0, #0 // return value ? no need ldp d14, d15, [sp,#64] ldp d12, d13, [sp, #48] ldp d10, d11, [sp, #32] ldp d8, d9, [sp, #16] ldp x29, x30, [sp], #80 AARCH64_AUTIASP ret .size CRYPT_AES_XTS_Encrypt, .-CRYPT_AES_XTS_Encrypt /** * int32_t CRYPT_AES_XTS_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len, const uint8_t *t); */ .globl CRYPT_AES_XTS_Decrypt .type CRYPT_AES_XTS_Decrypt, %function .align 4 CRYPT_AES_XTS_Decrypt: AARCH64_PACIASP stp x29, x30, [sp,#-80]! add x29, sp, #0 stp d8, d9, [sp,#16] stp d10, d11, [sp,#32] stp d12, d13, [sp,#48] stp d14, d15, [sp,#64] ld1 {TWK0.16b}, [TWEAK] and LTMP, LEN, #-16 ands TAILNUM, LEN, #0xF // get tail num, LEN % 16 sub XTMP1,LTMP,#16 // preserve last and tail block csel LTMP,XTMP1,LTMP,ne // if tailnum != 0, len -= 16 mov WTMP0,0x87 ldr ROUNDS,[KEY,#240] fmov TWX0,TWKD00 fmov TWX1,TWKD01 sub ROUNDS,ROUNDS,#6 // perload last 7 rounds key add KTMP,KEY,XROUNDS,lsl#4 ld1 {RDK2.4s,RDK3.4s},[KTMP],#32 ld1 {RDK4.4s,RDK5.4s},[KTMP],#32 ld1 {RDK6.4s,RDK7.4s},[KTMP],#32 ld1 {RDK8.4s},[KTMP] .Lxts_aesdec_start: cmp LTMP, #80 b.ge .Lxts_dec_proc_5_blks cmp LTMP, #48 b.ge .Lxts_dec_proc_3_blks cmp LTMP, #32 b.eq .Lxts_dec_proc_2_blks cmp LTMP, #16 b.eq .Lxts_dec_proc_1blk cmp LTMP, #0 b.eq .Lxts_dec_last_secondblk .Lxtx_dec_tail_blk: fmov TWX0,TWKD00 // reset already computed tweak fmov TWX1,TWKD01 cbz TAILNUM,.Lxts_aesdec_finish // prepare encrypt tail block sub TMPOUT,OUT,#16 .Lxtx_dec_tail_blk_loop: subs TAILNUM,TAILNUM,1 ldrb WC,[TMPOUT,TAILNUM] ldrb WP,[IN,TAILNUM] strb WC,[OUT,TAILNUM] strb WP,[TMPOUT,TAILNUM] b.gt .Lxtx_dec_tail_blk_loop ld1 {BLK0.16b}, [TMPOUT] mov OUT,TMPOUT mov TWK0.16b,TWK2.16b // load pre-tweak back b .Lxts_dec_proc_1blk_loaded cbz LTMP,.Lxts_aesdec_finish .Lxts_dec_last_secondblk: cbz TAILNUM,.Lxts_aesdec_finish mov TWK2.16b,TWK0.16b // save last second tweak NextTweak TWX0,TWX1,TWKD00,TWKD01 .Lxts_dec_proc_1blk: ld1 {BLK0.16b}, [IN],#16 .Lxts_dec_proc_1blk_loaded: mov KTMP, KEY eor BLK0.16b,BLK0.16b,TWK0.16b ld1 {RDK0.4s},[KTMP],#16 sub TROUNDS,ROUNDS,#2 ld1 {RDK1.4s},[KTMP],#16 .Lxts_dec_rounds_1blks: AesDecrypt1x BLK0,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesDecrypt1x BLK0,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_dec_rounds_1blks AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK0,RDK1 // last 7 rounds AesDecrypt1x BLK0,RDK2 AesDecrypt1x BLK0,RDK3 AesDecrypt1x BLK0,RDK4 AesDecrypt1x BLK0,RDK5 AesDecrypt1x BLK0,RDK6 aesd BLK0.16b,RDK7.16b // final round eor BLK0.16b,BLK0.16b,RDK8.16b eor BLK0.16b,BLK0.16b,TWK0.16b st1 {BLK0.16b}, [OUT], #16 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#16 b.lt .Lxtx_dec_tail_blk b.hs .Lxts_aesdec_start .Lxts_dec_proc_2_blks: ld1 {BLK0.16b, BLK1.16b}, [IN], #32 mov KTMP, KEY NextTweak TWX0,TWX1,TWKD10,TWKD11 ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 eor BLK0.16b, BLK0.16b, TWK0.16b eor BLK1.16b, BLK1.16b, TWK1.16b .Lxts_dec_rounds_2blks: AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_dec_rounds_2blks AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 // last 7 rounds AesDecrypt1x BLK0,RDK2 AesDecrypt1x BLK1,RDK2 AesDecrypt1x BLK0,RDK3 AesDecrypt1x BLK1,RDK3 AesDecrypt1x BLK0,RDK4 AesDecrypt1x BLK1,RDK4 AesDecrypt1x BLK0,RDK5 AesDecrypt1x BLK1,RDK5 AesDecrypt1x BLK0,RDK6 AesDecrypt1x BLK1,RDK6 eor TWK0.16b,TWK0.16b,RDK8.16b eor TWK1.16b,TWK1.16b,RDK8.16b aesd BLK0.16b,RDK7.16b // final round aesd BLK1.16b,RDK7.16b eor BLK0.16b,BLK0.16b,TWK0.16b eor BLK1.16b,BLK1.16b,TWK1.16b st1 {BLK0.16b, BLK1.16b}, [OUT], #32 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#32 b.hs .Lxts_aesdec_start .Lxts_dec_proc_3_blks: ld1 {BLK0.16b}, [IN], #16 // first block NextTweak TWX0,TWX1,TWKD10,TWKD11 eor BLK0.16b,BLK0.16b,TWK0.16b ld1 {BLK1.16b}, [IN], #16 // second block NextTweak TWX0,TWX1,TWKD20,TWKD21 eor BLK1.16b,BLK1.16b,TWK1.16b ld1 {BLK2.16b}, [IN], #16 // third block eor BLK2.16b,BLK2.16b,TWK2.16b mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 .Lxts_dec_rounds_3blks: AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 AesDecrypt1x BLK2,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 AesDecrypt1x BLK2,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_dec_rounds_3blks AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 AesDecrypt1x BLK2,RDK0 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 AesDecrypt1x BLK2,RDK1 // last 7 rounds AesDecrypt1x BLK0,RDK2 AesDecrypt1x BLK1,RDK2 AesDecrypt1x BLK2,RDK2 AesDecrypt1x BLK0,RDK3 AesDecrypt1x BLK1,RDK3 AesDecrypt1x BLK2,RDK3 AesDecrypt1x BLK0,RDK4 AesDecrypt1x BLK1,RDK4 AesDecrypt1x BLK2,RDK4 AesDecrypt1x BLK0,RDK5 AesDecrypt1x BLK1,RDK5 AesDecrypt1x BLK2,RDK5 AesDecrypt1x BLK0,RDK6 AesDecrypt1x BLK1,RDK6 AesDecrypt1x BLK2,RDK6 eor TWK0.16b,TWK0.16b,RDK8.16b eor TWK1.16b,TWK1.16b,RDK8.16b eor TWK2.16b,TWK2.16b,RDK8.16b aesd BLK0.16b,RDK7.16b aesd BLK1.16b,RDK7.16b aesd BLK2.16b,RDK7.16b eor BLK0.16b,BLK0.16b,TWK0.16b eor BLK1.16b,BLK1.16b,TWK1.16b eor BLK2.16b,BLK2.16b,TWK2.16b st1 {BLK0.16b, BLK1.16b, BLK2.16b}, [OUT], #48 NextTweak TWX0,TWX1,TWKD00,TWKD01 subs LTMP,LTMP,#48 b.hs .Lxts_aesdec_start .align 4 .Lxts_dec_proc_5_blks: ld1 {BLK0.16b}, [IN], #16 // first block NextTweak TWX0,TWX1,TWKD10,TWKD11 eor BLK0.16b,BLK0.16b,TWK0.16b ld1 {BLK1.16b}, [IN], #16 // second block NextTweak TWX0,TWX1,TWKD20,TWKD21 eor BLK1.16b,BLK1.16b,TWK1.16b sub LTMP,LTMP,#32 ld1 {BLK2.16b}, [IN], #16 // third block NextTweak TWX0,TWX1,TWKD30,TWKD31 eor BLK2.16b,BLK2.16b,TWK2.16b ld1 {BLK3.16b}, [IN], #16 // fourth block NextTweak TWX0,TWX1,TWKD40,TWKD41 eor BLK3.16b,BLK3.16b,TWK3.16b sub LTMP,LTMP,#32 ld1 {BLK4.16b}, [IN], #16 // fifth block eor BLK4.16b, BLK4.16b, TWK4.16b sub LTMP,LTMP,#16 mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 sub TROUNDS,ROUNDS,#2 .align 4 .Lxts_dec_rounds_5blks: AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 AesDecrypt1x BLK2,RDK0 AesDecrypt1x BLK3,RDK0 AesDecrypt1x BLK4,RDK0 ld1 {RDK0.4s},[KTMP],#16 subs TROUNDS,TROUNDS,#2 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 AesDecrypt1x BLK2,RDK1 AesDecrypt1x BLK3,RDK1 AesDecrypt1x BLK4,RDK1 ld1 {RDK1.4s},[KTMP],#16 b.gt .Lxts_dec_rounds_5blks AesDecrypt1x BLK0,RDK0 AesDecrypt1x BLK1,RDK0 AesDecrypt1x BLK2,RDK0 AesDecrypt1x BLK3,RDK0 AesDecrypt1x BLK4,RDK0 subs LTMP,LTMP,#80 AesDecrypt1x BLK0,RDK1 AesDecrypt1x BLK1,RDK1 AesDecrypt1x BLK2,RDK1 AesDecrypt1x BLK3,RDK1 AesDecrypt1x BLK4,RDK1 // last 7 rounds AesDecrypt1x BLK0,RDK2 AesDecrypt1x BLK1,RDK2 AesDecrypt1x BLK2,RDK2 AesDecrypt1x BLK3,RDK2 AesDecrypt1x BLK4,RDK2 csel POS,xzr,LTMP,gt // AesDecrypt1x BLK0,RDK3 AesDecrypt1x BLK1,RDK3 AesDecrypt1x BLK2,RDK3 AesDecrypt1x BLK3,RDK3 AesDecrypt1x BLK4,RDK3 add IN,IN,POS AesDecrypt1x BLK0,RDK4 AesDecrypt1x BLK1,RDK4 AesDecrypt1x BLK2,RDK4 AesDecrypt1x BLK3,RDK4 AesDecrypt1x BLK4,RDK4 AesDecrypt1x BLK0,RDK5 AesDecrypt1x BLK1,RDK5 AesDecrypt1x BLK2,RDK5 AesDecrypt1x BLK3,RDK5 AesDecrypt1x BLK4,RDK5 AesDecrypt1x BLK0,RDK6 AesDecrypt1x BLK1,RDK6 AesDecrypt1x BLK2,RDK6 AesDecrypt1x BLK3,RDK6 AesDecrypt1x BLK4,RDK6 eor TMP0.16b,TWK0.16b,RDK8.16b aesd BLK0.16b,RDK7.16b // final round NextTweak TWX0,TWX1,TWKD00,TWKD01 // perform operations of next 5blks in advance eor TMP1.16b,TWK1.16b,RDK8.16b ld1 {IN0.16b}, [IN], #16 aesd BLK1.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD10,TWKD11 eor TMP2.16b,TWK2.16b,RDK8.16b ld1 {IN1.16b}, [IN], #16 aesd BLK2.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD20,TWKD21 eor TMP3.16b,TWK3.16b,RDK8.16b ld1 {IN2.16b}, [IN], #16 aesd BLK3.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD30,TWKD31 eor TMP4.16b,TWK4.16b,RDK8.16b ld1 {IN3.16b}, [IN], #16 aesd BLK4.16b,RDK7.16b NextTweak TWX0,TWX1,TWKD40,TWKD41 ld1 {IN4.16b}, [IN], #16 mov KTMP, KEY ld1 {RDK0.4s,RDK1.4s},[KTMP],#32 eor TMP0.16b,TMP0.16b,BLK0.16b eor BLK0.16b,IN0.16b,TWK0.16b // blk0 = in0 ^ twk0 eor TMP1.16b,TMP1.16b,BLK1.16b eor BLK1.16b,IN1.16b,TWK1.16b st1 {TMP0.16b}, [OUT], #16 eor TMP2.16b,TMP2.16b,BLK2.16b eor BLK2.16b,IN2.16b,TWK2.16b eor TMP3.16b,TMP3.16b,BLK3.16b eor BLK3.16b,IN3.16b,TWK3.16b st1 {TMP1.16b}, [OUT], #16 eor TMP4.16b,TMP4.16b,BLK4.16b eor BLK4.16b,IN4.16b,TWK4.16b st1 {TMP2.16b}, [OUT], #16 sub TROUNDS,ROUNDS,#2 st1 {TMP3.16b,TMP4.16b}, [OUT], #32 b.hs .Lxts_dec_rounds_5blks add LTMP,LTMP,#80 // add 5 blocks length back if LTMP < 0 cbz LTMP, .Lxts_dec_pre_last_secondblks cmp LTMP, #16 b.eq .Lxts_dec_pre_last_1blks cmp LTMP,#32 b.eq .Lxts_dec_pre_last_2blks cmp LTMP,#48 b.eq .Lxts_dec_pre_last_3blks cmp LTMP,#64 b.eq .Lxts_dec_pre_last_4blks .Lxts_dec_pre_last_secondblks: fmov TWX0,TWKD10 // reset already computed tweak fmov TWX1,TWKD11 mov TWK2.16b, TWK0.16b //save the last second tweak mov TWK0.16b, TWK1.16b // use the last tweak b .Lxts_dec_proc_1blk .Lxts_dec_pre_last_1blks: eor IN0.16b,IN0.16b,IN4.16b //in0 = in0 ^ in41 eor BLK0.16b,BLK0.16b,IN0.16b // blk0 = in0 ^ twk0 ^ in0 ^ in4 fmov TWX0,TWKD00 // reset already computed tweak fmov TWX1,TWKD01 b .Lxts_dec_rounds_1blks .Lxts_dec_pre_last_2blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK0.16b,BLK0.16b,IN3.16b // in3 -> blk0 eor BLK1.16b,BLK1.16b,IN4.16b // in4 -> blk1 fmov TWX0,TWKD10 // reset already computed tweak fmov TWX1,TWKD11 b .Lxts_dec_rounds_2blks .Lxts_dec_pre_last_3blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK2.16b,BLK2.16b,IN2.16b eor BLK0.16b,BLK0.16b,IN2.16b // in2 -> blk0 eor BLK1.16b,BLK1.16b,IN3.16b // in3 -> blk1 eor BLK2.16b,BLK2.16b,IN4.16b // in4 -> blk2 fmov TWX0,TWKD20 // reset already computed tweak fmov TWX1,TWKD21 b .Lxts_dec_rounds_3blks .Lxts_dec_pre_last_4blks: eor BLK0.16b,BLK0.16b,IN0.16b eor BLK1.16b,BLK1.16b,IN1.16b eor BLK2.16b,BLK2.16b,IN2.16b eor BLK3.16b,BLK3.16b,IN3.16b sub IN,IN,#16 // have loaded 4blks, using 3blks to process, so step back 1blk here eor BLK0.16b,BLK0.16b,IN1.16b // in1 -> blk0 eor BLK1.16b,BLK1.16b,IN2.16b // in2 -> blk1 eor BLK2.16b,BLK2.16b,IN3.16b // in3 -> blk2 eor BLK3.16b,BLK3.16b,IN4.16b // in4 -> blk3 fmov TWX0,TWKD20 // reset already computed tweak fmov TWX1,TWKD21 b .Lxts_dec_rounds_3blks .Lxts_aesdec_finish: MOV_REG_TO_VEC(TWX0,TWX1,TWKD00,TWKD01) st1 {TWK0.16b}, [TWEAK] mov x0, #0 ldp d14, d15, [sp,#64] ldp d12, d13, [sp, #48] ldp d10, d11, [sp, #32] ldp d8, d9, [sp, #16] ldp x29, x30, [sp], #80 AARCH64_AUTIASP ret .size CRYPT_AES_XTS_Decrypt, .-CRYPT_AES_XTS_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_xts_armv8.S
Unix Assembly
unknown
25,458
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_XTS) #include "crypt_aes_macro_x86_64.s" .file "crypt_aes_xts_x86_64.S" .set KEY, %rdi .set IN, %rsi .set OUT, %rdx .set LEN, %ecx .set TWEAK, %r8 .set KTMP, %r9 .set LTMP, %r15d .set TAILNUM,%r14d .set TMPOUT,%r13 .set TMPIN,%r9 .set ROUNDS, %eax .set RET, %eax .set TROUNDS, %r10 .set ROUNDSQ,%rax .set KEYEND,%r9 .set WTMP0, %ecx .set WTMP1, %r10d .set WTMP2, %r11d .set XTMP0, %rcx .set XTMP1, %r10 .set XTMP2, %r11 .set TWX0, %r13 .set TWX1, %r14 .set BLK0, %xmm8 .set BLK1, %xmm9 .set BLK2, %xmm10 .set BLK3, %xmm11 .set BLK4, %xmm12 .set BLK5, %xmm13 .set BLK6, %xmm14 .set TWEAK0, %xmm0 .set TWEAK1, %xmm1 .set TWEAK2, %xmm2 .set TWEAK3, %xmm3 .set TWEAK4, %xmm4 .set TWEAK5, %xmm5 .set TWEAK6, %xmm6 .set RDK, %xmm15 .set RDK1, %xmm7 .set TMPX, %xmm7 .set GFP, %xmm6 .set TWKTMP, %xmm14 .macro NextTweakCore gfp, twkin, twktmp, tmp vmovdqa \twktmp,\tmp vpaddd \twktmp,\twktmp,\twktmp // doubleword << 1 vpsrad $31,\tmp,\tmp // ASR doubleword vpaddq \twkin,\twkin,\twkin // quadword << 1 vpand \gfp,\tmp,\tmp // and 0x10000000000000087 vpxor \tmp,\twkin,\twkin .endm .macro NextTweak gfp, twkin, twkout, twktmp, tmp NextTweakCore \gfp,\twkin,\twktmp,\tmp vmovdqa \twkin,\twkout .endm .macro SAVE_STACK push %rbx push %rbp push %rsp push %r12 push %r13 push %r14 push %r15 .endm .macro LOAD_STACK pop %r15 pop %r14 pop %r13 pop %r12 pop %rsp pop %rbp pop %rbx .endm .data .align 64 // modulus of Galois Field x^128+x^7+x^2+x+1 => 0x87(0b10000111) .Lgfp128: .long 0x87,0,1,0 .text /** * Function description: Sets the AES encryption assembly acceleration API in XTS mode. * Function prototype: int32_t CRYPT_AES_XTS_Encrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Points to the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: xmm1,xmm3,xmm4,xmm5,xmm6,xmm10,xmm11,xmm12,xmm13. * Output register: eax. * Function/Macro Call: None. */ .align 32 .globl CRYPT_AES_XTS_Encrypt .type CRYPT_AES_XTS_Encrypt, @function CRYPT_AES_XTS_Encrypt: .cfi_startproc pushq %rbx pushq %rbp pushq %r12 pushq %r13 pushq %r14 pushq %r15 sub $96,%rsp mov %rsp,%rbp and $-16,%rsp // 16 bytes align movl LEN, LTMP movl LEN, TAILNUM andl $-16,LTMP andl $0xf,TAILNUM // LEN % 16 movl 240(KEY), ROUNDS vmovdqa .Lgfp128(%rip),GFP vmovdqu (TWEAK), TWEAK0 shl $4,ROUNDS // roundkey size: rounds*16, except for the last one lea 16(KEY, ROUNDSQ),KEYEND // step to the end of roundkeys .Lxts_aesenc_start: cmpl $64, LTMP jae .Lxts_enc_above_equal_4_blks cmpl $32, LTMP jae .Lxts_enc_above_equal_2_blks cmpl $0, LTMP je .Lxts_aesenc_finish jmp .Lxts_enc_proc_1_blk .Lxts_enc_above_equal_2_blks: cmpl $48, LTMP jb .Lxts_enc_proc_2_blks jmp .Lxts_enc_proc_3_blks .Lxts_enc_above_equal_4_blks: cmpl $96, LTMP jae .Lxts_enc_proc_6_blks_pre cmpl $80, LTMP jb .Lxts_enc_proc_4_blks jmp .Lxts_enc_proc_5_blks .align 16 .Lxts_enc_proc_1_blk: vmovdqu (IN),BLK0 .Lxts_enc_proc_1blk_loaded: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK vpxor RDK,BLK0,BLK0 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 AES_ENC_1_BLK KTMP ROUNDS RDK BLK0 vpxor TWEAK0, BLK0, BLK0 vmovdqu BLK0, (OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 16(IN),IN subl $16,LTMP lea 16(OUT),OUT je .Lxts_aesenc_finish .align 16 .Lxts_enc_proc_2_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 AES_ENC_2_BLKS KTMP ROUNDS RDK BLK0 BLK1 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 32(IN),IN subl $32,LTMP lea 32(OUT),OUT je .Lxts_aesenc_finish .align 16 .Lxts_enc_proc_3_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 AES_ENC_3_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 48(IN),IN subl $48,LTMP lea 48(OUT),OUT je .Lxts_aesenc_finish .align 16 .Lxts_enc_proc_4_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX vpxor 48(IN), RDK, BLK3 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 AES_ENC_4_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 64(IN),IN subl $64,LTMP lea 64(OUT),OUT je .Lxts_aesenc_finish .align 16 .Lxts_enc_proc_5_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX vpxor 48(IN), RDK, BLK3 NextTweak GFP, TWEAK5, TWEAK4, TWKTMP, TMPX vpxor 64(IN), RDK, BLK4 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vpxor TWEAK4, BLK4, BLK4 AES_ENC_5_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vpxor TWEAK4, BLK4, BLK4 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) vmovdqu BLK4, 64(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 80(IN),IN subl $80,LTMP lea 80(OUT),OUT je .Lxts_aesenc_finish .align 16 .Lxts_enc_proc_6_blks_pre: vpshufd $0x5f,TWEAK0,TWKTMP // save higher doubleword of tweak vmovdqa TWEAK0,TWEAK5 // copy first tweak NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK4, TWKTMP, TMPX NextTweakCore GFP, TWEAK5, TWKTMP, TMPX .Lxts_enc_proc_6_blks: vmovdqu (KEY), RDK vmovdqu (IN),BLK0 vpxor TWEAK0,BLK0,BLK0 // blk0 ^= tweak0 vpxor RDK,BLK0,BLK0 // blk0 = blk0 ^ tweak0 ^ rk0, prepared for the loop round vmovdqu -16(KEYEND),RDK1 // load last round key vmovdqu 16(IN),BLK1 vpxor RDK1,TWEAK0,TWEAK0 aesenc 16(KEY),BLK0 // first round: rk1 vmovdqa TWEAK0,(%rsp) vpxor TWEAK1,BLK1,BLK1 vpxor RDK,BLK1,BLK1 vmovdqu 32(IN),BLK2 vpxor RDK1,TWEAK1,TWEAK1 aesenc 16(KEY),BLK1 vmovdqa TWEAK1,16(%rsp) vpxor TWEAK2,BLK2,BLK2 vpxor RDK,BLK2,BLK2 vmovdqu 48(IN),BLK3 vpxor RDK1,TWEAK2,TWEAK2 aesenc 16(KEY),BLK2 vmovdqa TWEAK2,32(%rsp) vpxor TWEAK3,BLK3,BLK3 vpxor RDK,BLK3,BLK3 vmovdqu 64(IN),BLK4 vpxor RDK1,TWEAK3,TWEAK3 aesenc 16(KEY),BLK3 vmovdqa TWEAK3,48(%rsp) vpxor TWEAK4,BLK4,BLK4 vpxor RDK,BLK4,BLK4 vmovdqu 80(IN),BLK5 vpxor RDK1,TWEAK4,TWEAK4 aesenc 16(KEY),BLK4 vmovdqa TWEAK4,64(%rsp) vpxor TWEAK5,BLK5,BLK5 vpxor RDK,BLK5,BLK5 vpxor RDK1,TWEAK5,TWEAK5 aesenc 16(KEY),BLK5 vmovdqa TWEAK5,80(%rsp) mov $(7*16),TROUNDS // loop 7 rounds sub ROUNDSQ,TROUNDS .align 16 .Lxts_6_blks_loop: vmovdqu -96(KEYEND,TROUNDS),RDK // left 5+1 block to interval aesenc RDK, BLK0 aesenc RDK, BLK1 aesenc RDK, BLK2 add $16,TROUNDS aesenc RDK, BLK3 aesenc RDK, BLK4 aesenc RDK, BLK5 jnz .Lxts_6_blks_loop vpxor 80(%rsp),RDK1,TWEAK5 // tweak5 = tweak5^lastroundkey^lastroundkey vmovdqu -96(KEYEND,TROUNDS),RDK vpshufd $0x5f,TWEAK5,TWKTMP // use new tweak-tmp vmovdqa TWKTMP,TMPX // pre-calculate next round tweak0~tweak5 aesenc RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenc RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenc RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesenc RDK, BLK3 vmovdqa TWEAK5,TWEAK0 aesenc RDK, BLK4 aesenc RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesenc RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenc RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenc RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesenc RDK, BLK3 vmovdqa TWEAK5,TWEAK1 aesenc RDK, BLK4 aesenc RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesenc RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenc RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenc RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesenc RDK, BLK3 vmovdqa TWEAK5,TWEAK2 aesenc RDK, BLK4 aesenc RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesenc RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenc RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenc RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesenc RDK, BLK3 vmovdqa TWEAK5,TWEAK3 aesenc RDK, BLK4 aesenc RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesenc RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenc RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenc RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 aesenc RDK, BLK3 vmovdqa TWEAK5,TWEAK4 aesenc RDK, BLK4 aesenc RDK, BLK5 vmovdqa TWKTMP,TMPX aesenclast (%rsp), BLK0 aesenclast 16(%rsp), BLK1 // already do the tweak^lastround, so here just aesenclast vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesenclast 32(%rsp), BLK2 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesenclast 48(%rsp), BLK3 vpxor TMPX,TWEAK5,TWEAK5 aesenclast 64(%rsp), BLK4 aesenclast 80(%rsp), BLK5 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) vmovdqu BLK4, 64(OUT) vmovdqu BLK5, 80(OUT) leaq 96(IN), IN leaq 96(OUT), OUT sub $96, LTMP cmp $96, LTMP jb .Lxts_aesenc_start jmp .Lxts_enc_proc_6_blks .align 16 .Lxts_aesenc_finish: cmp $0,TAILNUM je .Lxts_ret .Lxts_tail_proc: mov OUT,TMPOUT mov IN,TMPIN .Lxts_tail_loop: sub $1,TAILNUM movzb -16(TMPOUT),%r10d movzb (TMPIN),%r11d mov %r10b,(TMPOUT) lea 1(TMPIN),TMPIN mov %r11b,-16(TMPOUT) lea 1(TMPOUT),TMPOUT ja .Lxts_tail_loop sub $16,OUT // step 1 block back to save the last stealing block encryption add $16,LTMP vmovdqu (OUT),BLK0 jmp .Lxts_enc_proc_1blk_loaded .Lxts_ret: vmovdqu TWEAK0, (TWEAK) vpxor BLK0, BLK0, BLK0 vpxor BLK1, BLK1, BLK1 vpxor BLK2, BLK2, BLK2 vpxor BLK3, BLK3, BLK3 vpxor BLK4, BLK4, BLK4 vpxor BLK5, BLK5, BLK5 vpxor BLK6, BLK6, BLK6 vpxor RDK, RDK, RDK movl $0, RET mov %rbp,%rsp add $96,%rsp popq %r15 popq %r14 popq %r13 popq %r12 popq %rbp popq %rbx ret .cfi_endproc .size CRYPT_AES_XTS_Encrypt, .-CRYPT_AES_XTS_Encrypt /** * Function description: Sets the AES decryption and assembly acceleration API in XTS mode. * Function prototype: int32_t CRYPT_AES_XTS_Decrypt(const CRYPT_AES_Key *ctx, * const uint8_t *in, uint8_t *out, uint32_t len); * Input register: * x0: Pointer to the input key structure. * x1: Points to the 128-bit input data. * x2: Indicates the 128-bit output data. * x3: Indicates the length of a data block, that is, 16 bytes. * Change register: xmm1,xmm3,xmm4,xmm5,xmm6,xmm10,xmm11,xmm12,xmm13. * Output register: eax. * Function/Macro Call: None. */ .align 32 .globl CRYPT_AES_XTS_Decrypt .type CRYPT_AES_XTS_Decrypt, @function CRYPT_AES_XTS_Decrypt: .cfi_startproc pushq %rbx pushq %rbp pushq %r12 pushq %r13 pushq %r14 pushq %r15 sub $96,%rsp mov %rsp,%rbp and $-16,%rsp // 16 bytes align movl LEN, LTMP movl LEN, TAILNUM andl $-16,LTMP movl LTMP,WTMP2 sub $16,WTMP2 // preserve last and tail block andl $0xf,TAILNUM // LEN % 16 cmovg WTMP2,LTMP movl 240(KEY), ROUNDS vmovdqa .Lgfp128(%rip),GFP vmovdqu (TWEAK), TWEAK0 shl $4,ROUNDS // roundkey size: rounds*16, except for the last one lea 16(KEY, ROUNDSQ),KEYEND // step to the end of roundkeys .Lxts_aesdec_start: cmpl $64, LTMP jae .Lxts_dec_above_equal_4_blks cmpl $32, LTMP jae .Lxts_dec_above_equal_2_blks cmpl $0, LTMP je .Lxts_dec_last_2blks jmp .Lxts_dec_proc_1_blk .Lxts_dec_above_equal_2_blks: cmpl $48, LTMP jb .Lxts_dec_proc_2_blks jmp .Lxts_dec_proc_3_blks .Lxts_dec_above_equal_4_blks: cmpl $96, LTMP jae .Lxts_dec_proc_6_blks_pre cmpl $80, LTMP jb .Lxts_dec_proc_4_blks jmp .Lxts_dec_proc_5_blks .align 16 .Lxts_dec_tail_proc: cmp $0,TAILNUM je .Lxts_aesdec_finish vmovdqa TWEAK1,TWEAK0 // restore back tweak0 mov OUT,TMPOUT mov IN,TMPIN .Lxts_dec_tail_loop: sub $1,TAILNUM movzb -16(TMPOUT),%r10d movzb (TMPIN),%r11d mov %r10b,(TMPOUT) lea 1(TMPIN),TMPIN mov %r11b,-16(TMPOUT) lea 1(TMPOUT),TMPOUT ja .Lxts_dec_tail_loop sub $16,OUT // step 1 block back to save the last stealing block encryption add $16,LTMP vmovdqu (OUT),BLK0 jmp .Lxts_dec_proc_1blk_loaded .align 16 .Lxts_dec_last_2blks: cmp $0,TAILNUM je .Lxts_aesdec_finish vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK1 // tail block use tweak0, last block use tweak1 NextTweakCore GFP, TWEAK0, TWKTMP, TMPX .Lxts_dec_proc_1_blk: vmovdqu (IN),BLK0 .Lxts_dec_proc_1blk_loaded: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK vpxor RDK,BLK0,BLK0 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 AES_DEC_1_BLK KTMP ROUNDS RDK BLK0 vpxor TWEAK0, BLK0, BLK0 vmovdqu BLK0, (OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 16(IN),IN subl $16,LTMP lea 16(OUT),OUT jl .Lxts_dec_tail_proc jmp .Lxts_aesdec_start .align 16 .Lxts_dec_proc_2_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 AES_DEC_2_BLKS KTMP ROUNDS RDK BLK0 BLK1 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 32(IN),IN subl $32,LTMP lea 32(OUT),OUT jge .Lxts_aesdec_start .align 16 .Lxts_dec_proc_3_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 AES_DEC_3_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 48(IN),IN subl $48,LTMP lea 48(OUT),OUT jge .Lxts_aesdec_start .align 16 .Lxts_dec_proc_4_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX vpxor 48(IN), RDK, BLK3 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 AES_DEC_4_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 64(IN),IN subl $64,LTMP lea 64(OUT),OUT jge .Lxts_aesdec_start .align 16 .Lxts_dec_proc_5_blks: mov KEY,KTMP vpshufd $0x5f,TWEAK0,TWKTMP vmovdqa TWEAK0,TWEAK5 movl 240(KTMP), ROUNDS vmovdqu (KTMP), RDK NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX vpxor (IN), RDK, BLK0 vpxor 16(IN), RDK, BLK1 NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX vpxor 32(IN), RDK, BLK2 NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX vpxor 48(IN), RDK, BLK3 NextTweak GFP, TWEAK5, TWEAK4, TWKTMP, TMPX vpxor 64(IN), RDK, BLK4 decl ROUNDS vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vpxor TWEAK4, BLK4, BLK4 AES_DEC_5_BLKS KTMP ROUNDS RDK BLK0 BLK1 BLK2 BLK3 BLK4 vpxor TWEAK0, BLK0, BLK0 vpxor TWEAK1, BLK1, BLK1 vpxor TWEAK2, BLK2, BLK2 vpxor TWEAK3, BLK3, BLK3 vpxor TWEAK4, BLK4, BLK4 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) vmovdqu BLK4, 64(OUT) NextTweak GFP, TWEAK5, TWEAK0, TWKTMP, TMPX lea 80(IN),IN subl $80,LTMP lea 80(OUT),OUT jge .Lxts_aesdec_start .align 32 .Lxts_dec_proc_6_blks_pre: vpshufd $0x5f,TWEAK0,TWKTMP // save higher doubleword of tweak vmovdqa TWEAK0,TWEAK5 // copy first tweak NextTweak GFP, TWEAK5, TWEAK1, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK2, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK3, TWKTMP, TMPX NextTweak GFP, TWEAK5, TWEAK4, TWKTMP, TMPX NextTweakCore GFP, TWEAK5, TWKTMP, TMPX .align 32 .Lxts_dec_proc_6_blks: vmovdqu (KEY), RDK vmovdqu (IN),BLK0 vpxor TWEAK0,BLK0,BLK0 // blk0 ^= tweak0 vpxor RDK,BLK0,BLK0 // blk0 = blk0 ^ tweak0 ^ rk0, prepared for the loop round vmovdqu -16(KEYEND),RDK1 // load last round key vmovdqu 16(IN),BLK1 vpxor RDK1,TWEAK0,TWEAK0 aesdec 16(KEY),BLK0 // first round: rk1 vmovdqa TWEAK0,(%rsp) vpxor TWEAK1,BLK1,BLK1 vpxor RDK,BLK1,BLK1 vmovdqu 32(IN),BLK2 vpxor RDK1,TWEAK1,TWEAK1 aesdec 16(KEY),BLK1 vmovdqa TWEAK1,16(%rsp) vpxor TWEAK2,BLK2,BLK2 vpxor RDK,BLK2,BLK2 vmovdqu 48(IN),BLK3 vpxor RDK1,TWEAK2,TWEAK2 aesdec 16(KEY),BLK2 vmovdqa TWEAK2,32(%rsp) vpxor TWEAK3,BLK3,BLK3 vpxor RDK,BLK3,BLK3 vmovdqu 64(IN),BLK4 vpxor RDK1,TWEAK3,TWEAK3 aesdec 16(KEY),BLK3 vmovdqa TWEAK3,48(%rsp) vpxor TWEAK4,BLK4,BLK4 vpxor RDK,BLK4,BLK4 vmovdqu 80(IN),BLK5 vpxor RDK1,TWEAK4,TWEAK4 aesdec 16(KEY),BLK4 vmovdqa TWEAK4,64(%rsp) vpxor TWEAK5,BLK5,BLK5 vpxor RDK,BLK5,BLK5 vpxor RDK1,TWEAK5,TWEAK5 aesdec 16(KEY),BLK5 vmovdqa TWEAK5,80(%rsp) mov $(7*16),TROUNDS // loop 7 rounds sub ROUNDSQ,TROUNDS .align 32 .Lxts_dec_6blks_loop: vmovdqu -96(KEYEND,TROUNDS),RDK // left 5+1 block to interval aesdec RDK, BLK0 aesdec RDK, BLK1 aesdec RDK, BLK2 add $16,TROUNDS aesdec RDK, BLK3 aesdec RDK, BLK4 aesdec RDK, BLK5 jnz .Lxts_dec_6blks_loop vpxor 80(%rsp),RDK1,TWEAK5 // tweak5 = tweak5^lastroundkey^lastroundkey vmovdqu -96(KEYEND,TROUNDS),RDK vpshufd $0x5f,TWEAK5,TWKTMP // use new tweak-tmp vmovdqa TWKTMP,TMPX // pre-calculate next round tweak0~tweak5 aesdec RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdec RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdec RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesdec RDK, BLK3 vmovdqa TWEAK5,TWEAK0 aesdec RDK, BLK4 aesdec RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesdec RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdec RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdec RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesdec RDK, BLK3 vmovdqa TWEAK5,TWEAK1 aesdec RDK, BLK4 aesdec RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesdec RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdec RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdec RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesdec RDK, BLK3 vmovdqa TWEAK5,TWEAK2 aesdec RDK, BLK4 aesdec RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesdec RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdec RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdec RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 add $16,TROUNDS aesdec RDK, BLK3 vmovdqa TWEAK5,TWEAK3 aesdec RDK, BLK4 aesdec RDK, BLK5 vmovdqu -96(KEYEND,TROUNDS),RDK vmovdqa TWKTMP,TMPX aesdec RDK, BLK0 vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdec RDK, BLK1 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdec RDK, BLK2 vpxor TMPX,TWEAK5,TWEAK5 aesdec RDK, BLK3 vmovdqa TWEAK5,TWEAK4 aesdec RDK, BLK4 aesdec RDK, BLK5 vmovdqa TWKTMP,TMPX aesdeclast (%rsp), BLK0 aesdeclast 16(%rsp), BLK1 // already do the tweak^lastround, so here just aesdeclast vpaddd TWKTMP,TWKTMP,TWKTMP vpsrad $31,TMPX,TMPX aesdeclast 32(%rsp), BLK2 vpaddq TWEAK5,TWEAK5,TWEAK5 vpand GFP,TMPX,TMPX aesdeclast 48(%rsp), BLK3 vpxor TMPX,TWEAK5,TWEAK5 aesdeclast 64(%rsp), BLK4 aesdeclast 80(%rsp), BLK5 vmovdqu BLK0, (OUT) vmovdqu BLK1, 16(OUT) vmovdqu BLK2, 32(OUT) vmovdqu BLK3, 48(OUT) vmovdqu BLK4, 64(OUT) vmovdqu BLK5, 80(OUT) leaq 96(IN), IN leaq 96(OUT), OUT sub $96, LTMP cmp $96, LTMP jb .Lxts_aesdec_start jmp .Lxts_dec_proc_6_blks .align 16 .Lxts_aesdec_finish: vmovdqu TWEAK0, (TWEAK) vpxor BLK0, BLK0, BLK0 vpxor BLK1, BLK1, BLK1 vpxor BLK2, BLK2, BLK2 vpxor BLK3, BLK3, BLK3 vpxor BLK4, BLK4, BLK4 vpxor BLK5, BLK5, BLK5 vpxor BLK6, BLK6, BLK6 vpxor RDK, RDK, RDK movl $0, RET mov %rbp,%rsp add $96,%rsp popq %r15 popq %r14 popq %r13 popq %r12 popq %rbp popq %rbx ret .cfi_endproc .size CRYPT_AES_XTS_Decrypt, .-CRYPT_AES_XTS_Decrypt #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/asm/crypt_aes_xts_x86_64.S
Unix Assembly
unknown
25,529
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "bsl_sal.h" #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES #include "crypt_aes_tbox.h" #else #include "crypt_aes_sbox.h" #endif #include "crypt_aes.h" void SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 10; // 10 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_128, key, true); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_128, key); #endif } void SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 12; // 12 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_192, key, true); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_192, key); #endif } void SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 14; // 14 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_256, key, true); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_256, key); #endif } void SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 10; // 10 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_128, key, false); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_128, key); #endif } void SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 12; // 12 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_192, key, false); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_192, key); #endif } void SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key) { ctx->rounds = 14; // 14 rounds #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES SetAesKeyExpansionTbox(ctx, CRYPT_AES_256, key, false); #else SetAesKeyExpansionSbox(ctx, CRYPT_AES_256, key); #endif } int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES CRYPT_AES_EncryptTbox(ctx, in, out, len); #else CRYPT_AES_EncryptSbox(ctx, in, out, len); #endif return CRYPT_SUCCESS; } int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES CRYPT_AES_DecryptTbox(ctx, in, out, len); #else CRYPT_AES_DecryptSbox(ctx, in, out, len); #endif return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_AES */
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes.c
C
unknown
3,030
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_AES_LOCAL_H #define CRYPT_AES_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "crypt_aes.h" void SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); void SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); void SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); void SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key); void SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key); void SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key); #endif // HITLS_CRYPTO_AES #endif // CRYPT_AES_LOCAL_H
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_local.h
C
unknown
1,111
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && !defined(HITLS_CRYPTO_AES_PRECALC_TABLES) #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "crypt_aes.h" #include "crypt_aes_sbox.h" #define BYTE_BITS 8 static const uint8_t AES_S[256] = { 0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U, 0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U, 0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U, 0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U, 0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU, 0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U, 0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU, 0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U, 0x09U, 0x83U, 0x2cU, 0x1aU, 0x1bU, 0x6eU, 0x5aU, 0xa0U, 0x52U, 0x3bU, 0xd6U, 0xb3U, 0x29U, 0xe3U, 0x2fU, 0x84U, 0x53U, 0xd1U, 0x00U, 0xedU, 0x20U, 0xfcU, 0xb1U, 0x5bU, 0x6aU, 0xcbU, 0xbeU, 0x39U, 0x4aU, 0x4cU, 0x58U, 0xcfU, 0xd0U, 0xefU, 0xaaU, 0xfbU, 0x43U, 0x4dU, 0x33U, 0x85U, 0x45U, 0xf9U, 0x02U, 0x7fU, 0x50U, 0x3cU, 0x9fU, 0xa8U, 0x51U, 0xa3U, 0x40U, 0x8fU, 0x92U, 0x9dU, 0x38U, 0xf5U, 0xbcU, 0xb6U, 0xdaU, 0x21U, 0x10U, 0xffU, 0xf3U, 0xd2U, 0xcdU, 0x0cU, 0x13U, 0xecU, 0x5fU, 0x97U, 0x44U, 0x17U, 0xc4U, 0xa7U, 0x7eU, 0x3dU, 0x64U, 0x5dU, 0x19U, 0x73U, 0x60U, 0x81U, 0x4fU, 0xdcU, 0x22U, 0x2aU, 0x90U, 0x88U, 0x46U, 0xeeU, 0xb8U, 0x14U, 0xdeU, 0x5eU, 0x0bU, 0xdbU, 0xe0U, 0x32U, 0x3aU, 0x0aU, 0x49U, 0x06U, 0x24U, 0x5cU, 0xc2U, 0xd3U, 0xacU, 0x62U, 0x91U, 0x95U, 0xe4U, 0x79U, 0xe7U, 0xc8U, 0x37U, 0x6dU, 0x8dU, 0xd5U, 0x4eU, 0xa9U, 0x6cU, 0x56U, 0xf4U, 0xeaU, 0x65U, 0x7aU, 0xaeU, 0x08U, 0xbaU, 0x78U, 0x25U, 0x2eU, 0x1cU, 0xa6U, 0xb4U, 0xc6U, 0xe8U, 0xddU, 0x74U, 0x1fU, 0x4bU, 0xbdU, 0x8bU, 0x8aU, 0x70U, 0x3eU, 0xb5U, 0x66U, 0x48U, 0x03U, 0xf6U, 0x0eU, 0x61U, 0x35U, 0x57U, 0xb9U, 0x86U, 0xc1U, 0x1dU, 0x9eU, 0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U, 0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU, 0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U, 0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U }; #define SEARCH_SBOX(t) \ ((AES_S[((t) >> 24)] << 24) | (AES_S[((t) >> 16) & 0xFF] << 16) | (AES_S[((t) >> 8) & 0xFF] << 8) | \ (AES_S[((t) >> 0) & 0xFF] << 0)) #define SEARCH_INVSBOX(t) \ ((InvSubSbox(((t) >> 24)) << 24) | (InvSubSbox(((t) >> 16) & 0xFF) << 16) | (InvSubSbox(((t) >> 8) & 0xFF) << 8) | \ (InvSubSbox(((t) >> 0) & 0xFF) << 0)) void SetAesKeyExpansionSbox(CRYPT_AES_Key *ctx, uint32_t keyLenBits, const uint8_t *key) { uint32_t *ekey = ctx->key; uint32_t keyLenByte = keyLenBits / (sizeof(uint32_t) * BYTE_BITS); uint32_t i = 0; for (i = 0; i < keyLenByte; ++i) { ekey[i] = GET_UINT32_BE(key, i * sizeof(uint32_t)); } for (; i < 4 * (ctx->rounds + 1); ++i) { if ((i % keyLenByte) == 0) { ekey[i] = ekey[i - keyLenByte] ^ SEARCH_SBOX(ROTL32(ekey[i - 1], BYTE_BITS)) ^ RoundConstArray(i / keyLenByte - 1); } else if (keyLenByte > 6 && (i % keyLenByte) == 4) { ekey[i] = ekey[i - keyLenByte] ^ SEARCH_SBOX(ekey[i - 1]); } else { ekey[i] = ekey[i - keyLenByte] ^ ekey[i - 1]; } } } static void AesAddRoundKey(uint32_t *state, const uint32_t *round, int nr) { for (int i = 0; i < 4; ++i) { state[i] ^= round[4 * nr + i]; } } static void AesSubBytes(uint32_t *state) { for (int i = 0; i < 4; ++i) { state[i] = SEARCH_SBOX(state[i]); } } static void AesShiftRows(uint32_t *state) { uint32_t s[4] = {0}; for (int32_t i = 0; i < 4; ++i) { s[i] = state[i]; } state[0] = (s[0] & 0xFF000000) | (s[1] & 0xFF0000) | (s[2] & 0xFF00) | (s[3] & 0xFF); state[1] = (s[1] & 0xFF000000) | (s[2] & 0xFF0000) | (s[3] & 0xFF00) | (s[0] & 0xFF); state[2] = (s[2] & 0xFF000000) | (s[3] & 0xFF0000) | (s[0] & 0xFF00) | (s[1] & 0xFF); state[3] = (s[3] & 0xFF000000) | (s[0] & 0xFF0000) | (s[1] & 0xFF00) | (s[2] & 0xFF); } static uint8_t AesXtime(uint8_t x) { return ((x << 1) ^ (((x >> 7) & 1) * 0x1b)); } static uint8_t AesXtimes(uint8_t x, int ts) { uint8_t tmpX = x; int tmpTs = ts; while (tmpTs-- > 0) { tmpX = AesXtime(tmpX); } return tmpX; } static uint8_t AesMul(uint8_t x, uint8_t y) { return ((((y >> 0) & 1) * AesXtimes(x, 0)) ^ (((y >> 1) & 1) * AesXtimes(x, 1)) ^ (((y >> 2) & 1) * AesXtimes(x, 2)) ^ (((y >> 3) & 1) * AesXtimes(x, 3)) ^ (((y >> 4) & 1) * AesXtimes(x, 4)) ^ (((y >> 5) & 1) * AesXtimes(x, 5)) ^ (((y >> 6) & 1) * AesXtimes(x, 6)) ^ (((y >> 7) & 1) * AesXtimes(x, 7))); } static void AesMixColumns(uint32_t *state, bool isMixColumns) { uint8_t ts[16] = {0}; for (int32_t i = 0; i < 4; ++i) { PUT_UINT32_BE(state[i], ts, 4 * i); } uint8_t aesY[16] = {2, 3, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 3, 1, 1, 2}; uint8_t aesInvY[16] = {0x0e, 0x0b, 0x0d, 0x09, 0x09, 0x0e, 0x0b, 0x0d, 0x0d, 0x09, 0x0e, 0x0b, 0x0b, 0x0d, 0x09, 0x0e}; uint8_t s[4]; uint8_t *y = isMixColumns == true ? aesY : aesInvY; for (int i = 0; i < 4; ++i) { for (int r = 0; r < 4; ++r) { s[r] = 0; for (int j = 0; j < 4; ++j) { s[r] = s[r] ^ AesMul(ts[i * 4 + j], y[r * 4 + j]); } } for (int r = 0; r < 4; ++r) { ts[i * 4 + r] = s[r]; } } for (int32_t i = 0; i < 4; ++i) { state[i] = GET_UINT32_BE(ts, 4 * i); } } // addRound + 9/11/13 * (sub + shiftRow + mix + addRound) + (sub + shiftRow + addRound) void CRYPT_AES_EncryptSbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { (void)len; uint32_t s[4] = {0}; for (int32_t i = 0; i < 4; ++i) { s[i] = GET_UINT32_BE(in, 4 * i); } uint32_t nr = 0; AesAddRoundKey(s, ctx->key, nr); for (nr = 1; nr < ctx->rounds; ++nr) { AesSubBytes(s); AesShiftRows(s); AesMixColumns(s, true); AesAddRoundKey(s, ctx->key, nr); } AesSubBytes(s); AesShiftRows(s); AesAddRoundKey(s, ctx->key, nr); for (int32_t i = 0; i < 4; ++i) { PUT_UINT32_BE(s[i], out, 4 * i); } } static void InvShiftRows(uint32_t *state) { uint32_t s[4] = {0}; for (int32_t i = 0; i < 4; ++i) { s[i] = state[i]; } state[0] = (s[0] & 0xFF000000) | (s[3] & 0xFF0000) | (s[2] & 0xFF00) | (s[1] & 0xFF); state[1] = (s[1] & 0xFF000000) | (s[0] & 0xFF0000) | (s[3] & 0xFF00) | (s[2] & 0xFF); state[2] = (s[2] & 0xFF000000) | (s[1] & 0xFF0000) | (s[0] & 0xFF00) | (s[3] & 0xFF); state[3] = (s[3] & 0xFF000000) | (s[2] & 0xFF0000) | (s[1] & 0xFF00) | (s[0] & 0xFF); } static void InvSubBytes(uint32_t *state) { for (int i = 0; i < 4; ++i) { state[i] = SEARCH_INVSBOX(state[i]); } } // (addRound + InvShiftRow + InvSub) + 9/11/13 * (addRound + invMix + InvShiftRow + InvSub) + addRound void CRYPT_AES_DecryptSbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { (void)len; uint32_t s[4] = {0}; for (int32_t i = 0; i < 4; ++i) { s[i] = GET_UINT32_BE(in, 4 * i); } uint32_t nr = ctx->rounds; AesAddRoundKey(s, ctx->key, nr); InvShiftRows(s); InvSubBytes(s); for (nr = ctx->rounds - 1; nr > 0; --nr) { AesAddRoundKey(s, ctx->key, nr); AesMixColumns(s, false); InvShiftRows(s); InvSubBytes(s); } AesAddRoundKey(s, ctx->key, nr); for (int32_t i = 0; i < 4; ++i) { PUT_UINT32_BE(s[i], out, 4 * i); } BSL_SAL_CleanseData(&s, 4 * sizeof(uint32_t)); } #endif /* HITLS_CRYPTO_AES && !HITLS_CRYPTO_AES_PRECALC_TABLES */
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_sbox.c
C
unknown
8,560
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_AES_SBOX_H #define CRYPT_AES_SBOX_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && !defined(HITLS_CRYPTO_AES_PRECALC_TABLES) #include "crypt_aes.h" uint32_t RoundConstArray(int val); uint8_t InvSubSbox(uint8_t val); void SetAesKeyExpansionSbox(CRYPT_AES_Key *ctx, uint32_t keyLenBits, const uint8_t *key); void CRYPT_AES_EncryptSbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); void CRYPT_AES_DecryptSbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif /* HITLS_CRYPTO_AES && !HITLS_CRYPTO_AES_PRECALC_TABLES */ #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_sbox.h
C
unknown
1,189
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_aes_local.h" #include "bsl_sal.h" int32_t CRYPT_AES_SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 16) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetEncryptKey128(ctx, key); return CRYPT_SUCCESS; } int32_t CRYPT_AES_SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 24) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetEncryptKey192(ctx, key); return CRYPT_SUCCESS; } int32_t CRYPT_AES_SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 32) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetEncryptKey256(ctx, key); return CRYPT_SUCCESS; } int32_t CRYPT_AES_SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 16) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetDecryptKey128(ctx, key); return CRYPT_SUCCESS; } int32_t CRYPT_AES_SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 24) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetDecryptKey192(ctx, key); return CRYPT_SUCCESS; } int32_t CRYPT_AES_SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 32) { BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN); return CRYPT_AES_ERR_KEYLEN; } SetDecryptKey256(ctx, key); return CRYPT_SUCCESS; } void CRYPT_AES_Clean(CRYPT_AES_Key *ctx) { if (ctx == NULL) { return; } BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_AES_Key)); } #endif /* HITLS_CRYPTO_AES */
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_setkey.c
C
unknown
3,263
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_AES #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "crypt_aes.h" #include "crypt_aes_tbox.h" static const uint8_t INV_S[256] = { 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U, 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU, 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U, 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU, 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU, 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU, 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U, 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U, 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U, 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U, 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU, 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U, 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU, 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U, 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U, 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU, 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU, 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U, 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U, 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU, 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U, 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU, 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U, 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U, 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U, 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU, 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU, 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU, 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U, 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U, 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U, 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU, }; static const uint32_t RCON[] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, }; #ifndef HITLS_CRYPTO_AES_PRECALC_TABLES uint32_t RoundConstArray(uint8_t val) { return RCON[val]; } uint8_t InvSubSbox(uint8_t val) { return INV_S[val]; } #endif #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES #define TESEARCH(t0, t1, t2, t3) \ (((uint32_t)TE2[((t0) >> 24)] & 0xff000000) ^ ((uint32_t)TE3[((t1) >> 16) & 0xff] & 0x00ff0000) ^ \ ((uint32_t)TE0[((t2) >> 8) & 0xff] & 0x0000ff00) ^ ((uint32_t)TE1[(t3) & 0xff] & 0x000000ff)) #define INVSSEARCH(t0, t1, t2, t3) \ (((uint32_t)INV_S[((t0) >> 24)] << 24) ^ ((uint32_t)INV_S[((t3) >> 16) & 0xff] << 16) ^ \ ((uint32_t)INV_S[((t2) >> 8) & 0xff] << 8) ^ ((uint32_t)INV_S[(t1) & 0xff])) static const uint32_t TE0[256] = { 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, }; static const uint32_t TE1[256] = { 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, }; static const uint32_t TE2[256] = { 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, }; static const uint32_t TE3[256] = { 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, }; static const uint32_t TD0[256] = { 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, }; static const uint32_t TD1[256] = { 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, }; static const uint32_t TD2[256] = { 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, }; static const uint32_t TD3[256] = { 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, }; static void SetDecryptKeyTbox(CRYPT_AES_Key *ctx) { uint32_t i, j; uint32_t *dkey = ctx->key; for (i = 1; i < ctx->rounds; i++) { dkey += 4; // 4: 16 bytes are calculated each time. for (j = 0; j < 4; j++) { // for 0 to 4 , operation for 16 / sizeof(uint32_t) cycles dkey[j] = TD0[TE1[(dkey[j] >> 24)] & 0xff] ^ // dkey j >> 24 TD1[TE1[(dkey[j] >> 16) & 0xff] & 0xff] ^ // dkey j >> 16 TD2[TE1[(dkey[j] >> 8) & 0xff] & 0xff] ^ // dkey j >> 8 TD3[TE1[(dkey[j] >> 0) & 0xff] & 0xff]; // dkey j >> 0 } } } static inline uint32_t AES_G(uint32_t w, uint32_t rcon) { uint32_t ret = 0; /* Query the table and perform shift. */ ret ^= (uint32_t)TE2[(w >> 16) & 0xff] & 0xff000000; // 16, row/column conversion relationship in the T table ret ^= (uint32_t)TE3[(w >> 8) & 0xff] & 0x00ff0000; // 8, row/column conversion relationship in the T table ret ^= (uint32_t)TE0[(w) & 0xff] & 0x0000ff00; // 0, row/column conversion relationship in the T table ret ^= (uint32_t)TE1[(w >> 24)] & 0x000000ff; // 24, row/column conversion relationship in the T table ret ^= rcon; return ret; } void SetEncryptKey128Tbox(CRYPT_AES_Key *ctx, const uint8_t *key) { uint32_t *ekey = ctx->key; ekey[0] = GET_UINT32_BE(key, 0); // ekey 0: Four bytes starting from index key 0 ekey[1] = GET_UINT32_BE(key, 4); // ekey 1: Four bytes starting from index key 4 ekey[2] = GET_UINT32_BE(key, 8); // ekey 2: Four bytes starting from index key 8 ekey[3] = GET_UINT32_BE(key, 12); // ekey 3: Four bytes starting from index key 12 // 128bit Key length required 11 * 4 = 44. Number of expansion rounds: 10 -> 10 * 4 + 4 = 44 uint32_t times; for (times = 0; times < 9; times++) { // 9 times ekey[4] = AES_G(ekey[3], RCON[times]) ^ ekey[0]; // ekey 4 = (ekey 3 xor rcon) ^ ekey 0 ekey[5] = ekey[4] ^ ekey[1]; // ekey 5 = ekey 4 ^ ekey 1 ekey[6] = ekey[5] ^ ekey[2]; // ekey 6 = ekey 5 ^ ekey 2 ekey[7] = ekey[6] ^ ekey[3]; // ekey 7 = ekey 6 ^ ekey 3 ekey += 4; // add 4 } // the last times ekey[4] = AES_G(ekey[3], RCON[times]) ^ ekey[0]; // ekey 4 = (ekey 3 xor rcon) ^ ekey 0 ekey[5] = ekey[4] ^ ekey[1]; // ekey 5 = ekey 4 ^ ekey 1 ekey[6] = ekey[5] ^ ekey[2]; // ekey 6 = ekey 5 ^ ekey 2 ekey[7] = ekey[6] ^ ekey[3]; // ekey 7 = ekey 6 ^ ekey 3 } void SetEncryptKey192Tbox(CRYPT_AES_Key *ctx, const uint8_t *key) { uint32_t *ekey = ctx->key; ekey[0] = GET_UINT32_BE(key, 0); // ekey 0: Four bytes starting from index key 0 ekey[1] = GET_UINT32_BE(key, 4); // ekey 1: Four bytes starting from index key 4 ekey[2] = GET_UINT32_BE(key, 8); // ekey 2: Four bytes starting from index key 8 ekey[3] = GET_UINT32_BE(key, 12); // ekey 3: Four bytes starting from index key 12 ekey[4] = GET_UINT32_BE(key, 16); // ekey 4: Four bytes starting from index key 16 ekey[5] = GET_UINT32_BE(key, 20); // ekey 5: Four bytes starting from index key 20 uint32_t times; for (times = 0; times < 7; times++) { // 7 times ekey[6] = AES_G(ekey[5], RCON[times]) ^ ekey[0]; // ekey 6 = AES_G(ekey 5 xor rcon) ^ ekey 0 ekey[7] = ekey[6] ^ ekey[1]; // ekey 7 = ekey 6 ^ ekey 1 ekey[8] = ekey[7] ^ ekey[2]; // ekey 8 = ekey 7 ^ ekey 2 ekey[9] = ekey[8] ^ ekey[3]; // ekey 9 = ekey 8 ^ ekey 3 ekey[10] = ekey[9] ^ ekey[4]; // ekey 10 = ekey 9 ^ ekey 4 ekey[11] = ekey[10] ^ ekey[5]; // ekey 11 = ekey 10 ^ ekey 5 ekey += 6; // add 6 } // the last times ekey[6] = AES_G(ekey[5], RCON[times]) ^ ekey[0]; // ekey 6 = AES_G(ekey 5 xor rcon) ^ ekey 0 ekey[7] = ekey[6] ^ ekey[1]; // ekey 7 = ekey 6 ^ ekey 1 ekey[8] = ekey[7] ^ ekey[2]; // ekey 8 = ekey 7 ^ ekey 2 ekey[9] = ekey[8] ^ ekey[3]; // ekey 9 = ekey 8 ^ ekey 3 } void SetEncryptKey256Tbox(CRYPT_AES_Key *ctx, const uint8_t *key) { uint32_t *ekey = ctx->key; ekey[0] = GET_UINT32_BE(key, 0); // ekey 0: Four bytes starting from index key 0 ekey[1] = GET_UINT32_BE(key, 4); // ekey 1: Four bytes starting from index key 4 ekey[2] = GET_UINT32_BE(key, 8); // ekey 2: Four bytes starting from index key 8 ekey[3] = GET_UINT32_BE(key, 12); // ekey 3: Four bytes starting from index key 12 ekey[4] = GET_UINT32_BE(key, 16); // ekey 4: Four bytes starting from index key 16 ekey[5] = GET_UINT32_BE(key, 20); // ekey 5: Four bytes starting from index key 20 ekey[6] = GET_UINT32_BE(key, 24); // ekey 6: Four bytes starting from index key 24 ekey[7] = GET_UINT32_BE(key, 28); // ekey 7: Four bytes starting from index key 28 /* The key length must be 15 * 4 = 60. The number of extension rounds is 7 -> 7 * 8 + 8 - 4 = 60 */ uint32_t times; uint32_t tmp; for (times = 0; times < 6; times++) { // 6 times ekey[8] = AES_G(ekey[7], RCON[times]) ^ ekey[0]; // ekey 8 = AES_G(ekey 7 xor rcon) ^ ekey 0 ekey[9] = ekey[8] ^ ekey[1]; // ekey 9 = ekey 8 ^ ekey 1 ekey[10] = ekey[9] ^ ekey[2]; // ekey 10 = ekey 9 ^ ekey 2 ekey[11] = ekey[10] ^ ekey[3]; // ekey 11 = ekey 10 ^ ekey 3 /* Shift operation to compensate for G operation */ tmp = (ekey[11] >> 8) | (ekey[11] << 24); // tmp is ekey 11 >> 8 | ekey 11 << 24 ekey[12] = AES_G(tmp, 0) ^ ekey[4]; // ekey 12 = AES_G(tme, 0) ^ ekey 4 ekey[13] = ekey[12] ^ ekey[5]; // ekey 13 = ekey 12 ^ ekey 5 ekey[14] = ekey[13] ^ ekey[6]; // ekey 14 = ekey 13 ^ ekey 6 ekey[15] = ekey[14] ^ ekey[7]; // ekey 15 = ekey 14 ^ ekey 7 ekey += 8; // add 8 } // the last times ekey[8] = AES_G(ekey[7], RCON[times]) ^ ekey[0]; // ekey 8 = AES_G(ekey 7 xor rcon) ^ ekey 0 ekey[9] = ekey[8] ^ ekey[1]; // ekey 9 = ekey 8 ^ ekey 1 ekey[10] = ekey[9] ^ ekey[2]; // ekey 10 = ekey 9 ^ ekey 2 ekey[11] = ekey[10] ^ ekey[3]; // ekey 11 = ekey 10 ^ ekey 3 } void SetAesKeyExpansionTbox(CRYPT_AES_Key *ctx, uint32_t keyLenBits, const uint8_t *key, bool isEncrypt) { switch (keyLenBits) { case CRYPT_AES_128: SetEncryptKey128Tbox(ctx, key); break; case CRYPT_AES_192: SetEncryptKey192Tbox(ctx, key); break; case CRYPT_AES_256: SetEncryptKey256Tbox(ctx, key); break; default: return; } if (!isEncrypt) { SetDecryptKeyTbox(ctx); } } #define AES_ROUND_INIT(in, r, enc) \ do { \ r##0 = GET_UINT32_BE(in, 0) ^ enc##key[0]; \ r##1 = GET_UINT32_BE(in, 4) ^ enc##key[1]; \ r##2 = GET_UINT32_BE(in, 8) ^ enc##key[2]; \ r##3 = GET_UINT32_BE(in, 12) ^ enc##key[3]; \ } while (0) #define AES_ENC_ROUND(in, r, i, ekey) \ do { \ r##0 = TE0[(in##0 >> 24)] ^ TE1[(in##1 >> 16) & 0xff] ^ TE2[(in##2 >> 8) & 0xff] ^ TE3[(in##3) & 0xff] \ ^ (ekey)[((i) << 2) + 0]; \ r##1 = TE0[(in##1 >> 24)] ^ TE1[(in##2 >> 16) & 0xff] ^ TE2[(in##3 >> 8) & 0xff] ^ TE3[(in##0) & 0xff] \ ^ (ekey)[((i) << 2) + 1]; \ r##2 = TE0[(in##2 >> 24)] ^ TE1[(in##3 >> 16) & 0xff] ^ TE2[(in##0 >> 8) & 0xff] ^ TE3[(in##1) & 0xff] \ ^ (ekey)[((i) << 2) + 2]; \ r##3 = TE0[(in##3 >> 24)] ^ TE1[(in##0 >> 16) & 0xff] ^ TE2[(in##1 >> 8) & 0xff] ^ TE3[(in##2) & 0xff] \ ^ (ekey)[((i) << 2) + 3]; \ } while (0) void CRYPT_AES_EncryptTbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { (void)len; const uint32_t *ekey = ctx->key; uint32_t c0, c1, c2, c3, t0, t1, t2, t3; AES_ROUND_INIT(in, c, e); AES_ENC_ROUND(c, t, 1, ekey); AES_ENC_ROUND(t, c, 2, ekey); AES_ENC_ROUND(c, t, 3, ekey); AES_ENC_ROUND(t, c, 4, ekey); AES_ENC_ROUND(c, t, 5, ekey); AES_ENC_ROUND(t, c, 6, ekey); AES_ENC_ROUND(c, t, 7, ekey); AES_ENC_ROUND(t, c, 8, ekey); AES_ENC_ROUND(c, t, 9, ekey); if (ctx->rounds > 10) { // AES192/AES256 Performs 10th and 11th rounds of calculation. AES_ENC_ROUND(t, c, 10, ekey); AES_ENC_ROUND(c, t, 11, ekey); } if (ctx->rounds > 12) { // AES256 Performs 12th and 13th rounds of calculation. AES_ENC_ROUND(t, c, 12, ekey); AES_ENC_ROUND(c, t, 13, ekey); } /* In the last round, the column confusion is not performed. Instead, the shift is performed and the s-box is searched. */ // Do the position of the last ekey calculation, which is the ekey that has been used 4*rounds. ekey += ctx->rounds * 4; c0 = TESEARCH(t0, t1, t2, t3) ^ ekey[0]; // operation ekey 0 c1 = TESEARCH(t1, t2, t3, t0) ^ ekey[1]; // operation ekey 1 c2 = TESEARCH(t2, t3, t0, t1) ^ ekey[2]; // operation ekey 2 c3 = TESEARCH(t3, t0, t1, t2) ^ ekey[3]; // operation ekey 3 PUT_UINT32_BE(c0, out, 0); // c0 is converted into four bytes which index is 0 in the out. PUT_UINT32_BE(c1, out, 4); // c1 is converted into four bytes which index is 4 in the out. PUT_UINT32_BE(c2, out, 8); // c2 is converted into four bytes which index is 8 in the out PUT_UINT32_BE(c3, out, 12); // c3 is converted into four bytes which index is 12 in the out } #define AES_DEC_ROUND(in, r, i, dkey) \ do { \ r##0 = TD0[(in##0 >> 24)] ^ TD1[(in##3 >> 16) & 0xff] ^ TD2[(in##2 >> 8) & 0xff] ^ TD3[(in##1) & 0xff] \ ^ (dkey)[((i) << 2) + 0]; \ r##1 = TD0[(in##1 >> 24)] ^ TD1[(in##0 >> 16) & 0xff] ^ TD2[(in##3 >> 8) & 0xff] ^ TD3[(in##2) & 0xff] \ ^ (dkey)[((i) << 2) + 1]; \ r##2 = TD0[(in##2 >> 24)] ^ TD1[(in##1 >> 16) & 0xff] ^ TD2[(in##0 >> 8) & 0xff] ^ TD3[(in##3) & 0xff] \ ^ (dkey)[((i) << 2) + 2]; \ r##3 = TD0[(in##3 >> 24)] ^ TD1[(in##2 >> 16) & 0xff] ^ TD2[(in##1 >> 8) & 0xff] ^ TD3[(in##0) & 0xff] \ ^ (dkey)[((i) << 2) + 3]; \ } while (0) void CRYPT_AES_DecryptTbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { (void)len; const uint32_t *dkey = ctx->key + ctx->rounds * 4; // 4 bytes uint32_t p0, p1, p2, p3, t0, t1, t2, t3; // Initialize p0. The dkey starts from the end of the key. AES_ROUND_INIT(in, p, d); dkey = ctx->key; if (ctx->rounds > 12) { // AES256 Performs 12th and 13th rounds of calculation. AES_DEC_ROUND(p, t, 13, dkey); AES_DEC_ROUND(t, p, 12, dkey); } if (ctx->rounds > 10) { // AES192 Performs 10th and 11th rounds of calculation. AES_DEC_ROUND(p, t, 11, dkey); AES_DEC_ROUND(t, p, 10, dkey); } AES_DEC_ROUND(p, t, 9, dkey); AES_DEC_ROUND(t, p, 8, dkey); AES_DEC_ROUND(p, t, 7, dkey); AES_DEC_ROUND(t, p, 6, dkey); AES_DEC_ROUND(p, t, 5, dkey); AES_DEC_ROUND(t, p, 4, dkey); AES_DEC_ROUND(p, t, 3, dkey); AES_DEC_ROUND(t, p, 2, dkey); AES_DEC_ROUND(p, t, 1, dkey); /* In the last round, the column confusion is not performed. Instead, the shift is directly performed and the inverse s-box is searched. */ p0 = INVSSEARCH(t0, t1, t2, t3) ^ dkey[0]; // dkey 0 p1 = INVSSEARCH(t1, t2, t3, t0) ^ dkey[1]; // dkey 1 p2 = INVSSEARCH(t2, t3, t0, t1) ^ dkey[2]; // dkey 2 p3 = INVSSEARCH(t3, t0, t1, t2) ^ dkey[3]; // dkey 3 PUT_UINT32_BE(p0, out, 0); // 4 bytes starting from the index 0 of the in PUT_UINT32_BE(p1, out, 4); // 4 bytes starting from the index 4 of the in PUT_UINT32_BE(p2, out, 8); // 4 bytes starting from the index 8 of the in PUT_UINT32_BE(p3, out, 12); // 4 bytes starting from the index 12 of the in BSL_SAL_CleanseData(&p0, sizeof(uint32_t)); BSL_SAL_CleanseData(&p1, sizeof(uint32_t)); BSL_SAL_CleanseData(&p2, sizeof(uint32_t)); BSL_SAL_CleanseData(&p3, sizeof(uint32_t)); } #endif /* HITLS_CRYPTO_AES_PRECALC_TABLES */ #endif /* HITLS_CRYPTO_AES */
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_tbox.c
C
unknown
44,548
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_AES_TBOX_H #define CRYPT_AES_TBOX_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_AES_PRECALC_TABLES) #include "crypt_aes.h" void SetAesKeyExpansionTbox(CRYPT_AES_Key *ctx, uint32_t keyLenBits, const uint8_t *key, bool isEncrypt); void CRYPT_AES_EncryptTbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); void CRYPT_AES_DecryptTbox(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif /* HITLS_CRYPTO_AES_PRECALC_TABLES */ #endif
2302_82127028/openHiTLS-examples
crypto/aes/src/crypt_aes_tbox.h
C
unknown
1,107
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_BN_H #define CRYPT_BN_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_BN #include <stdint.h> #include <stdlib.h> #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif #if defined(HITLS_SIXTY_FOUR_BITS) #define BN_UINT uint64_t #define BN_MASK (0xffffffffffffffffL) #define BN_DEC_VAL (10000000000000000000ULL) #define BN_DEC_LEN 19 #define BN_UNIT_BITS 64 #elif defined(HITLS_THIRTY_TWO_BITS) #define BN_UINT uint32_t #define BN_MASK (0xffffffffL) #define BN_DEC_VAL (1000000000L) #define BN_DEC_LEN 9 #define BN_UNIT_BITS 32 #else #error BN_UINT MUST be defined first. #endif #define BN_MAX_BITS (1u << 29) /* @note: BN_BigNum bits limitation 2^29 bits */ #define BN_BITS_TO_BYTES(n) (((n) + 7) >> 3) /* @note: Calcute bytes form bits, bytes = (bits + 7) >> 3 */ #define BN_BYTES_TO_BITS(n) ((n) << 3) /* bits = bytes * 8 = bytes << 3 */ #define BN_UINT_BITS ((uint32_t)sizeof(BN_UINT) << 3) #define BITS_TO_BN_UNIT(bits) (((bits) + BN_UINT_BITS - 1) / BN_UINT_BITS) /* Flag of BigNum. If a new number is added, the value increases by 0x01 0x02 0x04... */ typedef enum { CRYPT_BN_FLAG_OPTIMIZER = 0x01, /**< Flag of BigNum, indicating the BigNum obtained from the optimizer */ CRYPT_BN_FLAG_STATIC = 0x02, /**< Flag of BigNum, indicating the BN memory management belongs to the user. */ CRYPT_BN_FLAG_CONSTTIME = 0x04, /**< Flag of BigNum, indicating the constant time execution. */ } CRYPT_BN_FLAG; typedef struct BigNum { bool sign; /* *< bignum sign: negtive(true) or not(false) */ uint32_t size; /* *< bignum size (count of BN_UINT) */ uint32_t room; /* *< bignum max size (count of BN_UINT) */ uint32_t flag; /* *< bignum flag */ BN_UINT *data; /* *< bignum data chunk(most significant limb at the largest) */ } BN_BigNum; typedef struct BnMont BN_Mont; typedef struct BnOptimizer BN_Optimizer; typedef struct BnCbCtx BN_CbCtx; typedef int32_t (*BN_CallBack)(BN_CbCtx *, int32_t, int32_t); /* If a is 0, all Fs are returned. If a is not 0, 0 is returned. */ static inline BN_UINT BN_IsZeroUintConsttime(BN_UINT a) { BN_UINT t = ~a & (a - 1); // The most significant bit of t is 1 only when a == 0. // Shifting 3 bits to the left is equivalent to multiplying 8, convert the number of bytes into the number of bits. return (BN_UINT)0 - (t >> (((uint32_t)sizeof(BN_UINT) << 3) - 1)); } #ifdef HITLS_CRYPTO_EAL_BN /* Check whether the BN entered externally is valid. */ bool BnVaild(const BN_BigNum *a); #endif /** * @ingroup bn * @brief BigNum creation * * @param bits [IN] Number of bits * * @retval not-NULL Success * @retval NULL fail */ BN_BigNum *BN_Create(uint32_t bits); /** * @ingroup bn * @brief BigNum Destruction * * @param a [IN] BigNum * * @retval none */ void BN_Destroy(BN_BigNum *a); /** * @ingroup bn * @brief BN initialization * @attention This interface is used to create the BN structure between modules. The BN does not manage the memory of the external BN structure and internal data space. the interface only the fixed attributes such as data, room, and flag. The size attribute is defined by the caller. * * @param bn [IN/OUT] BN, which is created by users and is not managed by the BN. * @param data [IN] BN data, the memory is allocated by the user and is not managed by the BN. * @param number [IN] number of BN that need to be initialized. * * @retval void */ void BN_Init(BN_BigNum *bn, BN_UINT *data, uint32_t room, int32_t number); #ifdef HITLS_CRYPTO_BN_CB /** * @ingroup bn * @brief BigNum callback creation * * @param none * * @retval not-NULL Success * @retval NULL fail */ BN_CbCtx *BN_CbCtxCreate(void); /** * @ingroup bn * @brief BigNum callback configuration * * @param gencb [out] Callback * @param callBack [in] Callback API * @param arg [in] Callback parameters * * @retval none */ void BN_CbCtxSet(BN_CbCtx *gencb, BN_CallBack callBack, void *arg); /** * @ingroup bn * @brief Invoke the callback. * * @param callBack [out] Callback * @param process [in] Parameter * @param target [in] Parameter * @retval CRYPT_SUCCESS succeeded * @retval other determined by the callback function */ int32_t BN_CbCtxCall(BN_CbCtx *callBack, int32_t process, int32_t target); /** * @ingroup bn * @brief Obtain the arg parameter in the callback. * * @param callBack [in] Callback * @retval void* NULL or callback parameter. */ void *BN_CbCtxGetArg(BN_CbCtx *callBack); /** * @ingroup bn * @brief Callback release * * @param cb [in] Callback * * @retval none */ void BN_CbCtxDestroy(BN_CbCtx *cb); #endif /** * @ingroup bn * @brief Set the symbol. * * @param a [IN] BigNum * @param sign [IN] symbol. The value true indicates a negative number and the value false indicates a positive number. * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_NO_NEGATOR_ZERO 0 cannot be set to a negative sign. */ int32_t BN_SetSign(BN_BigNum *a, bool sign); /** * @ingroup bn * @brief Set the flag. * * @param a [IN] BigNum * @param flag [IN] flag, for example, BN_MARK_CONSTTIME indicates that the constant interface is used. * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_FLAG_INVALID Invalid BigNum flag. */ int32_t BN_SetFlag(BN_BigNum *a, uint32_t flag); /** * @ingroup bn * @brief BigNum copy * * @param r [OUT] BigNum * @param a [IN] BigNum * * @retval CRYPT_SUCCESS succeeded. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Copy(BN_BigNum *r, const BN_BigNum *a); /** * @ingroup bn * @brief Generate a BigNum with the same content. * * @param a [IN] BigNum * * @retval Not NULL Success * @retval NULL failure */ BN_BigNum *BN_Dup(const BN_BigNum *a); /** * @ingroup bn * @brief Check whether the value of a BigNum is 0. * * @attention The input parameter cannot be null. * @param a [IN] BigNum * * @retval true. The value of a BigNum is 0. * @retval false. The value of a BigNum is not 0. * @retval other: indicates that the input parameter is abnormal. * */ bool BN_IsZero(const BN_BigNum *a); /** * @ingroup bn * @brief Check whether the value of a BigNum is 1. * * @attention The input parameter cannot be null. * @param a [IN] BigNum * * @retval true. The value of a BigNum is 1. * @retval false. The value of a BigNum is not 1. * @retval other: indicates that the input parameter is abnormal. * */ bool BN_IsOne(const BN_BigNum *a); /** * @ingroup bn * @brief Check whether a BigNum is a negative number. * * @attention The input parameter cannot be null. * @param a [IN] BigNum * * @retval true. The value of a BigNum is a negative number. * @retval false. The value of a BigNum is not a negative number. * */ bool BN_IsNegative(const BN_BigNum *a); /** * @ingroup bn * @brief Check whether the value of a BigNum is an odd number. * * @attention The input parameter cannot be null. * @param a [IN] BigNum * * @retval true. The value of a BigNum is an odd number. * @retval false. The value of a BigNum is not an odd number. * @retval other: indicates that the input parameter is abnormal. * */ bool BN_IsOdd(const BN_BigNum *a); /** * @ingroup bn * @brief Check whether the flag of a BigNum meets the expected flag. * * @param a [IN] BigNum * @param flag [IN] Flag. For example, BN_MARK_CONSTTIME indicates that the constant interface is used. * * @retval true, invalid null pointer * @retval false, 0 cannot be set to a negative number. * @retval other: indicates that the input parameter is abnormal. */ bool BN_IsFlag(const BN_BigNum *a, uint32_t flag); /** * @ingroup bn * @brief Set the value of a BigNum to 0. * * @param a [IN] BigNum * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval other: indicates that the input parameter is abnormal. */ int32_t BN_Zeroize(BN_BigNum *a); /** * @ingroup bn * @brief Compare whether the value of BigNum a is the target limb w. * * @attention The input parameter cannot be null. * @param a [IN] BigNum * @param w [IN] Limb * * @retval true: equal * @retval false, not equal * @retval other: indicates that the input parameter is abnormal. */ bool BN_IsLimb(const BN_BigNum *a, const BN_UINT w); /** * @ingroup bn * @brief Set a limb to the BigNum. * * @param a [IN] BigNum * @param w [IN] Limb * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_SetLimb(BN_BigNum *r, BN_UINT w); /** * @ingroup bn * @brief Obtain the limb from the BigNum. * * @param a [IN] BigNum * * @retval 0 Get 0 * @retval BN_MASK Obtain the mask. * @retval others The limb is obtained successfully. */ BN_UINT BN_GetLimb(const BN_BigNum *a); /** * @ingroup bn * @brief Obtain the value of the bit corresponding to a BigNum. The value is 1 or 0. * * @attention The input parameter of a BigNum cannot be null. * @param a [IN] BigNum * @param n [IN] Number of bits * * @retval true. The corresponding bit is 1. * @retval false. The corresponding bit is 0. * */ bool BN_GetBit(const BN_BigNum *a, uint32_t n); /** * @ingroup bn * @brief Set the bit corresponding to the BigNum to 1. * * @param a [IN] BigNum * @param n [IN] Number of bits * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_SPACE_NOT_ENOUGH The space is insufficient. */ int32_t BN_SetBit(BN_BigNum *a, uint32_t n); /** * @ingroup bn * @brief Clear the bit corresponding to the BigNum to 0. * * @param a [IN] BigNum * @param n [IN] Number of bits * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_SPACE_NOT_ENOUGH The space is insufficient. */ int32_t BN_ClrBit(BN_BigNum *a, uint32_t n); /** * @ingroup bn * @brief Truncate a BigNum from the corresponding bit. * * @param a [IN] BigNum * @param n [IN] Number of bits * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_SPACE_NOT_ENOUGH The space is insufficient. */ int32_t BN_MaskBit(BN_BigNum *a, uint32_t n); /** * @ingroup bn * @brief Obtain the valid bit length of a BigNum. * * @attention The input parameter of a BigNum cannot be null. * @param a [IN] BigNum * * @retval uint32_t, valid bit length */ uint32_t BN_Bits(const BN_BigNum *a); /** * @ingroup bn * @brief Obtain the valid byte length of a BigNum. * * @attention The large input parameter cannot be a null pointer. * @param a [IN] BigNum * * @retval uint32_t, valid byte length of a BigNum */ uint32_t BN_Bytes(const BN_BigNum *a); /** * @ingroup bn * @brief BigNum Calculate the greatest common divisor * @par Description: gcd(a, b) (a, b!=0) * * @param r [OUT] greatest common divisor * @param a [IN] BigNum * @param b [IN] BigNum * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_GCD_NO_ZERO The greatest common divisor cannot be 0. */ int32_t BN_Gcd(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum modulo inverse * * @param r [OUT] Result * @param x [IN] BigNum * @param m [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_NO_INVERSE Cannot calculate the module inverse. */ int32_t BN_ModInv(BN_BigNum *r, const BN_BigNum *x, const BN_BigNum *m, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum comparison * * @attention The input parameter of a BigNum cannot be null. * @param a [IN] BigNum * @param b [IN] BigNum * * @retval 0,a == b * @retval 1,a > b * @retval -1,a < b */ int32_t BN_Cmp(const BN_BigNum *a, const BN_BigNum *b); /** * @ingroup bn * @brief BigNum Addition * * @param r [OUT] and * @param a [IN] Addendum * @param b [IN] Addendum * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Add(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b); /** * @ingroup bn * @brief BigNum plus limb * * @param r [OUT] and * @param a [IN] Addendum * @param w [IN] Addendum * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_AddLimb(BN_BigNum *r, const BN_BigNum *a, BN_UINT w); /** * @ingroup bn * @brief subtraction of large numbers * * @param r [OUT] difference * @param a [IN] minuend * @param b [IN] subtrahend * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Sub(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b); /** * @ingroup bn * @brief BigNum minus limb * * @param r [OUT] difference * @param a [IN] minuend * @param w [IN] subtrahend * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_SubLimb(BN_BigNum *r, const BN_BigNum *a, BN_UINT w); /** * @ingroup bn * @brief BigNum Multiplication * * @param r [OUT] product * @param a [IN] multiplier * @param b [IN] multiplier * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. */ int32_t BN_Mul(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_Optimizer *opt); /** * @ingroup bn * @brief Multiplication of BigNum by Limb * * @param r [OUT] product * @param a [IN] multiplicand * @param w [IN] multiplier (limb) * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_MulLimb(BN_BigNum *r, const BN_BigNum *a, const BN_UINT w); /** * @ingroup bn * @brief BigNum square. r must not be a. * * @param r [OUT] product * @param a [IN] multiplier * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. */ int32_t BN_Sqr(BN_BigNum *r, const BN_BigNum *a, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Division * * @param q [OUT] quotient * @param r [OUT] remainder * @param x [IN] dividend * @param y [IN] divisor * @param opt [IN] optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_INVALID_ARG The addresses of q, r are identical, or both of them are null. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO divisor cannot be 0. */ int32_t BN_Div(BN_BigNum *q, BN_BigNum *r, const BN_BigNum *x, const BN_BigNum *y, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum divided by limb * * @param q [OUT] quotient * @param r [OUT] remainder * @param x [IN] dividend * @param y [IN] Divisor (limb) * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_ERR_DIVISOR_ZERO divisor cannot be 0. */ int32_t BN_DivLimb(BN_BigNum *q, BN_UINT *r, const BN_BigNum *x, const BN_UINT y); /** * @ingroup bn * @brief BigNum Modular addition * @par Description: r = (a + b) mod (mod) * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param b [IN] BigNum * @param mod [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_ModAdd(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_BigNum *mod, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Modular subtraction * @par Description: r = (a - b) mod (mod) * * @param r [OUT] Modulo result * @param a [IN] minuend * @param b [IN] subtrahend * @param mod [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_ModSub(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_BigNum *mod, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Modular multiplication * @par Description: r = (a * b) mod (mod) * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param b [IN] BigNum * @param mod [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_ModMul(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_BigNum *mod, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Modular squared * @par Description: r = (a ^ 2) mod (mod) * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param mod [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_ModSqr( BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *mod, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Modular power * @par Description: r = (a ^ e) mod (mod) * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param mod [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. * @retval CRYPT_BN_ERR_EXP_NO_NEGATIVE exponent cannot be a negative number */ int32_t BN_ModExp(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *e, const BN_BigNum *m, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum modulo * @par Description: r = a mod m * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param m [IN] mod * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_Mod(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *m, BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum modulo limb * @par Description: r = a mod m * * @param r [OUT] Modulus result * @param a [IN] BigNum * @param m [IN] Modulus (limb) * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_ERR_DIVISOR_ZERO module cannot be 0. */ int32_t BN_ModLimb(BN_UINT *r, const BN_BigNum *a, const BN_UINT m); #ifdef HITLS_CRYPTO_BN_PRIME /** * @ingroup bn * @brief generate BN prime * * @param r [OUT] Generate a prime number. * @param e [OUT] A helper prime to reduce the number of Miller-Rabin primes check. * @param bits [IN] Length of the generated prime number * @param half [IN] Whether to generate a prime number greater than the maximum value of this prime number by 1/2: * Yes: True, No: false * @param opt [IN] Optimizer * @param cb [IN] BigNum callback * @retval CRYPT_SUCCESS The prime number is successfully generated. * @retval CRYPT_NULL_INPUT Invalid null pointer. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_STACK_FULL The optimizer stack is full. * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_NOR_GEN_PRIME Failed to generate prime numbers. * @retval CRYPT_NO_REGIST_RAND No random number is registered. * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. */ int32_t BN_GenPrime(BN_BigNum *r, BN_BigNum *e, uint32_t bits, bool half, BN_Optimizer *opt, BN_CbCtx *cb); /** * @ingroup bn * @brief check prime number * * @param bn [IN] Prime number to be checked * @param checkTimes [IN] the user can set the check times of miller-rabin testing. * if checkTimes == 0, it will use the default detection times of miller-rabin. * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS The check result is a prime number. * @retval CRYPT_BN_NOR_CHECK_PRIME The check result is a non-prime number. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_OPTIMIZER_STACK_FULL The optimizer stack is full. * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_NO_REGIST_RAND No random number is registered. * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. */ int32_t BN_PrimeCheck(const BN_BigNum *bn, uint32_t checkTimes, BN_Optimizer *opt, BN_CbCtx *cb); #endif // HITLS_CRYPTO_BN_PRIME #ifdef HITLS_CRYPTO_BN_RAND #define BN_RAND_TOP_NOBIT 0 /* Not set bits */ #define BN_RAND_TOP_ONEBIT 1 /* Set the most significant bit to 1. */ #define BN_RAND_TOP_TWOBIT 2 /* Set the highest two bits to 1 */ #define BN_RAND_BOTTOM_NOBIT 0 /* Not set bits */ #define BN_RAND_BOTTOM_ONEBIT 1 /* Set the least significant bit to 1. */ #define BN_RAND_BOTTOM_TWOBIT 2 /* Set the least significant two bits to 1. */ /** * @ingroup bn * @brief generate random BigNum * * @param r [OUT] Generate a random number. * @param bits [IN] Length of the generated prime number * @param top [IN] Generating the flag indicating whether to set the most significant bit of a random number * @param bottom [IN] Generate the flag indicating whether to set the least significant bit of the random number. * * @retval CRYPT_SUCCESS A random number is generated successfully. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_ERR_RAND_TOP_BOTTOM The top or bottom is invalid during random number generation. * @retval CRYPT_NO_REGIST_RAND No random number is registered. * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. * @retval CRYPT_BN_ERR_RAND_BITS_NOT_ENOUGH The bit is too small during random number generation. */ int32_t BN_Rand(BN_BigNum *r, uint32_t bits, uint32_t top, uint32_t bottom); /** * @ingroup bn * @brief generate random BigNum * * @param libCtx [IN] provider libCtx * @param r [OUT] Generate a random number. * @param bits [IN] Length of the generated prime number * @param top [IN] Generating the flag indicating whether to set the most significant bit of a random number * @param bottom [IN] Generate the flag indicating whether to set the least significant bit of the random number. * * @retval CRYPT_SUCCESS A random number is generated successfully. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_ERR_RAND_TOP_BOTTOM The top or bottom is invalid during random number generation. * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. * @retval CRYPT_BN_ERR_RAND_BITS_NOT_ENOUGH The bit is too small during random number generation. */ int32_t BN_RandEx(void *libCtx, BN_BigNum *r, uint32_t bits, uint32_t top, uint32_t bottom); /** * @ingroup bn * @brief generate random BigNum * * @param r [OUT] Generate a random number. * @param p [IN] Compare data so that the generated r < p * * @retval CRYPT_SUCCESS A random number is successfully generated. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_NO_REGIST_RAND No random number is registered. * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. * @retval CRYPT_BN_ERR_RAND_ZERO Generate a random number smaller than 0. * @retval CRYPT_BN_ERR_RAND_NEGATE Generate a negative random number. */ int32_t BN_RandRange(BN_BigNum *r, const BN_BigNum *p); /** * @ingroup bn * @brief generate random BigNum * * @param libCtx [IN] provider libCtx * @param r [OUT] Generate a random number. * @param p [IN] Compare data so that the generated r < p * * @retval CRYPT_SUCCESS A random number is successfully generated. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_RAND_GEN_FAIL Failed to generate a random number. * @retval CRYPT_BN_ERR_RAND_ZERO Generate a random number smaller than 0. * @retval CRYPT_BN_ERR_RAND_NEGATE Generate a negative random number. */ int32_t BN_RandRangeEx(void *libCtx, BN_BigNum *r, const BN_BigNum *p); #endif /** * @ingroup bn * @brief Binary to BigNum * * @param r [OUT] BigNum * @param bin [IN] Data stream to be converted * @param binLen [IN] Data stream length * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Bin2Bn(BN_BigNum *r, const uint8_t *bin, uint32_t binLen); /** * @ingroup bn * @brief Convert BigNum to a big-endian binary * * @param a [IN] BigNum * @param bin [IN/OUT] Data stream to be converted -- The input pointer cannot be null. * @param binLen [IN/OUT] Data stream length -- When input, binLen is also the length of the bin buffer. * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL An error occurred during the copy. */ int32_t BN_Bn2Bin(const BN_BigNum *a, uint8_t *bin, uint32_t *binLen); /** * @ingroup bn * @brief fix size of BigNum * * @param a [IN] BigNum * * @retval void */ void BN_FixSize(BN_BigNum *a); /** * @ingroup bn * @brief * * @param a [IN/OUT] BigNum * @param words [IN] the bn room that the caller wanted. * * @retval CRYPT_SUCCESS * @retval others, see crypt_errno.h */ int32_t BN_Extend(BN_BigNum *a, uint32_t words); /** * @ingroup bn * @brief Convert BigNum to binary to obtain big-endian data with the length of binLen. * The most significant bits are filled with 0. * * @param a [IN] BigNum * @param bin [OUT] Data stream to be converted -- The input pointer cannot be null. * @param binLen [IN] Data stream length -- When input, binLen is also the length of the bin buffer. * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_BUFF_LEN_NOT_ENOUGH The space is insufficient. */ int32_t BN_Bn2BinFixZero(const BN_BigNum *a, uint8_t *bin, uint32_t binLen); #ifdef HITLS_CRYPTO_BN_STR_CONV /** * @ingroup bn * @brief Hexadecimal to a BigNum * * @param r [OUT] BigNum * @param r [IN] Data stream to be converted * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_CONVERT_INPUT_INVALID Invalid string */ int32_t BN_Hex2Bn(BN_BigNum **r, const char *str); /** * @ingroup bn * @brief Convert BigNum to hexadecimal number * * @param a [IN] BigNum * @param char [OUT] Converts a hexadecimal string. * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ char *BN_Bn2Hex(const BN_BigNum *a); /** * @ingroup bn * @brief Decimal to BigNum * * @param r [OUT] BigNum * @param str [IN] A decimal string to be converted * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_CONVERT_INPUT_INVALID Invalid string */ int32_t BN_Dec2Bn(BN_BigNum **r, const char *str); /** * @ingroup bn * @brief Convert BigNum to decimal number * * @param r [IN] BigNum * * @retval A decimal string after conversion or push error. */ char *BN_Bn2Dec(const BN_BigNum *a); #endif #if defined(HITLS_CRYPTO_CURVE_SM2_ASM) || \ ((defined(HITLS_CRYPTO_CURVE_NISTP521) || defined(HITLS_CRYPTO_CURVE_NISTP384_ASM)) && \ defined(HITLS_CRYPTO_NIST_USE_ACCEL)) /** * @ingroup bn * @brief Converting a 64-bit unsigned number array to a BigNum * * @param r [OUT] BigNum * @param array [IN] Array to be converted * @param len [IN] Number of elements in the array * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_U64Array2Bn(BN_BigNum *r, const uint64_t *array, uint32_t len); /** * @ingroup bn * @brief BigNum to 64-bit unsigned number array * * @param a [IN] BigNum * @param array [IN/OUT] Array for storing results -- The input pointer cannot be null. * @param len [IN/OUT] Length of the written array -- Number of writable elements when input * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A copy error occurs. */ int32_t BN_Bn2U64Array(const BN_BigNum *a, uint64_t *array, uint32_t *len); #endif /** * @ingroup bn * @brief BigNum optimizer creation * * @param None * * @retval Not NULL Success * @retval NULL failure */ BN_Optimizer *BN_OptimizerCreate(void); /** * @ingroup bn * @brief Destroy the BigNum optimizer. * * @param opt [IN] BigNum optimizer * * @retval none */ void BN_OptimizerDestroy(BN_Optimizer *opt); /** * @ingroup bn * @brief set library context * * @param libCtx [IN] Library context * @param opt [OUT] BigNum optimizer * * @retval none */ void BN_OptimizerSetLibCtx(void *libCtx, BN_Optimizer *opt); /** * @ingroup bn * @brief get library context * * @param opt [In] BigNum optimizer * * @retval library context */ void *BN_OptimizerGetLibCtx(BN_Optimizer *opt); /** * @ingroup bn * @brief BigNum Montgomery context creation and setting * * @param m [IN] Modulus m, which must be positive and odd * * @retval Not NULL Success * @retval NULL failure */ BN_Mont *BN_MontCreate(const BN_BigNum *m); /** * @ingroup bn * @brief BigNum Montgomery modular exponentiation. * Whether to use the constant API depends on the property of the BigNum. * * @param r [OUT] Modular exponentiation result * @param a [IN] base * @param e [IN] Index * @param mont [IN] Montgomery context * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS calculated successfully. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_MONT_BASE_TOO_MAX Montgomery modulus exponentiation base is too large * @retval CRYPT_BN_OPTIMIZER_STACK_FULL The optimizer stack is full. * @retval CRYPT_BN_ERR_EXP_NO_NEGATE exponent cannot be a negative number */ int32_t BN_MontExp(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *e, BN_Mont *mont, BN_Optimizer *opt); /** * @ingroup bn * @brief Constant time BigNum Montgomery modular exponentiation * * @param r [OUT] Modular exponentiation result * @param a [IN] base * @param e [IN] exponent * @param mont [IN] Montgomery context * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS calculated successfully. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_BN_OPTIMIZER_GET_FAIL Failed to apply for space from the optimizer. * @retval CRYPT_BN_MONT_BASE_TOO_MAX Montgomery Modular exponentiation base is too large * @retval CRYPT_BN_OPTIMIZER_STACK_FULL The optimizer stack is full. * @retval CRYPT_BN_ERR_EXP_NO_NEGATE exponent cannot be a negative number */ int32_t BN_MontExpConsttime(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *e, BN_Mont *mont, BN_Optimizer *opt); /** * @ingroup mont * @brief BigNum Montgomery Context Destruction * * @param mont [IN] BigNum Montgomery context * * @retval none */ void BN_MontDestroy(BN_Mont *mont); /** * @ingroup bn * @brief shift a BigNum to the right * * @param r [OUT] Shift result * @param a [IN] Source data * @param n [IN] Shift bit num * * @retval CRYPT_SUCCESS succeeded. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL The security function returns an error. */ int32_t BN_Rshift(BN_BigNum *r, const BN_BigNum *a, uint32_t n); /** * @ingroup bn * @brief shift a BigNum to the left * * @param r [OUT] Shift result * @param a [IN] Source data * @param n [IN] Shift bit num * * @retval CRYPT_SUCCESS succeeded. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Lshift(BN_BigNum *r, const BN_BigNum *a, uint32_t n); #ifdef HITLS_CRYPTO_DSA int32_t BN_MontExpMul(BN_BigNum *r, const BN_BigNum *a1, const BN_BigNum *e1, const BN_BigNum *a2, const BN_BigNum *e2, BN_Mont *mont, BN_Optimizer *opt); #endif #ifdef HITLS_CRYPTO_ECC /** * @ingroup bn * @brief Mould opening root * @par Description: r^2 = a mod p; p-1=q*2^s. * In the current implementation s=1 will take a special branch, and the calculation speed is faster. * The fast calculation branch with s=2 is not implemented currently. * Currently, the s corresponding to the mod p of the EC nist224, 256, 384, and 521 is 96, 1, 1, and 1 respectively * The branch with s=2 is not used. * The root number is provided for the EC. * @param r [OUT] Modular root result * @param a [IN] Source data, 0 <= a <= p-1 * @param p [IN] module, odd prime number * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS calculated successfully. * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_BN_ERR_SQRT_PARA The input parameter is incorrect. * @retval CRYPT_BN_ERR_LEGENDE_DATA: * Failed to find the specific number of the Legendre sign (z|p) of z to p equal to -1 when calculating the square root. * @retval CRYPT_BN_ERR_NO_SQUARE_ROOT The square root cannot be found. */ int32_t BN_ModSqrt(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *p, BN_Optimizer *opt); #endif #if defined(HITLS_CRYPTO_CURVE_SM2_ASM) || (defined(HITLS_CRYPTO_CURVE_NISTP256_ASM) && \ defined(HITLS_CRYPTO_NIST_USE_ACCEL)) /** * @ingroup bn * @brief BigNum to BN_UINT array * * @param src [IN] BigNum * @param dst [OUT] BN_UINT array for receiving the conversion result * @param size [IN] Length of the dst buffer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL The security function returns an error. */ int32_t BN_BN2Array(const BN_BigNum *src, BN_UINT *dst, uint32_t size); /** * @ingroup bn * @brief BN_UINT array to BigNum * * @param dst [OUT] BigNum * @param src [IN] BN_UINT array to be converted * @param size [IN] Length of the src buffer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Array2BN(BN_BigNum *dst, const BN_UINT *src, const uint32_t size); #endif #ifdef HITLS_CRYPTO_ECC /** * @ingroup bn * @brief Copy with the mask. When the mask is set to (0), r = a; when the mask is set to (-1), r = b. * * @attention Data r, a, and b must have the same room. * * @param r [OUT] Output result * @param a [IN] Source data * @param b [IN] Source data * @param mask [IN] Mask data * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_CopyWithMask(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_UINT mask); /** * @ingroup bn * @brief Calculate r = (a - b) % mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance-sensitive. * The user must ensure that a < mod, b < mod * In addition, a->room and b->room are not less than mod->size. * All data are non-negative * The mod information cannot be 0. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param b [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_ModSubQuick(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_BigNum *mod, const BN_Optimizer *opt); /** * @ingroup bn * @brief Calculate r = (a + b) % mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance-sensitive. * The user must ensure that a < mod, b < mod * In addition, a->room and b->room are not less than mod->size. * All data are non-negative * The mod information cannot be 0. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param b [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_ModAddQuick(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_BigNum *mod, const BN_Optimizer *opt); /** * @ingroup bn * @brief Calculate r = (a * b) % mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance sensitive. * The user must ensure that a < mod, b < mod * In addition, a->room and b->room are not less than mod->size. * All data are non-negative * The mod information can only be the parameter p of the curve of nistP224, nistP256, nistP384, and nistP521. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param b [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For other errors, see crypt_errno.h. */ int32_t BN_ModNistEccMul(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, void *mod, BN_Optimizer *opt); /** * @ingroup bn * @brief Calculate r = (a ^ 2) % mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance sensitive. * The user must guarantee a < mod * In addition, a->room are not less than mod->size. * All data are non-negative * The mod information can only be the parameter p of the curve of nistP224, nistP256, nistP384, and nistP521. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_ModNistEccSqr(BN_BigNum *r, const BN_BigNum *a, void *mod, BN_Optimizer *opt); #endif #ifdef HITLS_CRYPTO_CURVE_SM2 /** * @ingroup ecc * @brief sm2 curve: calculate r = (a*b)% mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance sensitive. * The user must guarantee a < mod、b < mod * In addition, a->room and b->room are not less than mod->size. * All data are non-negative * The mod information can only be the parameter p of the curve of sm2. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param b [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_ModSm2EccMul(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, void *data, BN_Optimizer *opt); /** * @ingroup ecc * @brief sm2 curve: calculate r = (a ^ 2) % mod * * @attention This API is invoked in the area where ECC point computing is intensive and is performance sensitive. * The user must guarantee a < mod * In addition, a->room are not less than mod->size. * All data are non-negative * The mod information can only be the parameter p of the curve of sm2. * Otherwise, the interface may not be functional. * * @param r [OUT] Output result * @param a [IN] Source data * @param mod [IN] Modular data * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS succeeded. * @retval For details about other errors, see crypt_errno.h. */ int32_t BN_ModSm2EccSqr(BN_BigNum *r, const BN_BigNum *a, void *data, BN_Optimizer *opt); #endif #ifdef HITLS_CRYPTO_BN_PRIME_RFC3526 /** * @ingroup bn * @brief Return the corresponding length of modulo exponent of the BigNum. * * @param r [OUT] Output result * @param len [IN] Length * * @retval Not NULL Success * @retval NULL failure */ BN_BigNum *BN_GetRfc3526Prime(BN_BigNum *r, uint32_t len); #endif /** * @ingroup bn * @brief Return the number of security bits provided by a specific algorithm and specific key size. * * @param [OUT] Output the result. * @param pubLen [IN] Size of the public key * @param prvLen [IN] Size of the private key. * * @retval Number of security bits */ int32_t BN_SecBits(int32_t pubLen, int32_t prvLen); #if defined(HITLS_CRYPTO_RSA) /** * @ingroup bn * @brief Montgomery modulus calculation process, need a < m, b < m, All is positive numbers, The large number optimizer must be enabled before this function is used. * * @param r [OUT] Output results * @param a [IN] Input data * @param b [IN] Input data * @param mont [IN] Montgomery context * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ int32_t MontMulCore(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_Mont *mont, BN_Optimizer *opt); #endif // HITLS_CRYPTO_RSA #if defined(HITLS_CRYPTO_BN_PRIME) /** * @ingroup bn * @brief Montgomery modulus calculation process, need a < m, unlimited symbols. * * @param r [OUT] Output results * @param a [IN] Input data * @param mont [IN] Montgomery context * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ int32_t MontSqrCore(BN_BigNum *r, const BN_BigNum *a, BN_Mont *mont, BN_Optimizer *opt); #endif // HITLS_CRYPTO_BN_PRIME /** * @ingroup bn * @brief Enabling the big data optimizer * * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ int32_t OptimizerStart(BN_Optimizer *opt); /** * @ingroup bn * @brief Disabling the Large Number Optimizer * * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ void OptimizerEnd(BN_Optimizer *opt); /** * @ingroup bn * @brief Get Bn from the large number optimizer. * * @param opt [IN] Large number optimizer * @param room [IN] Length of the big number. * * @retval BN_BigNum if success * @retval NULL if failed */ BN_BigNum *OptimizerGetBn(BN_Optimizer *opt, uint32_t room); #if defined(HITLS_CRYPTO_PAILLIER) || defined(HITLS_CRYPTO_RSA_CHECK) /** * @ingroup bn * @brief BigNum Calculate the least common multiple * @par Description: lcm(a, b) (a, b!=0) * * @param r [OUT] least common multiple * @param a [IN] BigNum * @param b [IN] BigNum * @param opt [IN] Optimizer * * @retval CRYPT_SUCCESS * @retval CRYPT_NULL_INPUT Invalid null pointer * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure */ int32_t BN_Lcm(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, BN_Optimizer *opt); #endif // HITLS_CRYPTO_PAILLIER || HITLS_CRYPTO_RSA_CHECK /** * @ingroup bn * @brief Enabling the big data optimizer * * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ int32_t OptimizerStart(BN_Optimizer *opt); /** * @ingroup bn * @brief Disabling the Large Number Optimizer * * @param opt [IN] Large number optimizer * * @retval CRYPT_SUCCESS * @retval For details about other errors, see crypt_errno.h. */ void OptimizerEnd(BN_Optimizer *opt); /** * @ingroup bn * @brief Get Bn from the large number optimizer. * * @param opt [IN] Large number optimizer * @param room [IN] Length of the big number. * * @retval BN_BigNum if success * @retval NULL if failed */ BN_BigNum *OptimizerGetBn(BN_Optimizer *opt, uint32_t room); #ifdef HITLS_CRYPTO_CURVE_MONT /** * a, b is mont form. * r = a * b */ int32_t BN_EcPrimeMontMul(BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, void *data, BN_Optimizer *opt); /** * a is mont form. * r = a ^ 2 */ int32_t BN_EcPrimeMontSqr(BN_BigNum *r, const BN_BigNum *a, void *mont, BN_Optimizer *opt); /** * r = Reduce(r * RR) */ int32_t BnMontEnc(BN_BigNum *r, BN_Mont *mont, BN_Optimizer *opt, bool consttime); /** * r = Reduce(r) */ void BnMontDec(BN_BigNum *r, BN_Mont *mont); /** * This interface is a constant time. * if mask = BN_MASK. swap a and b. * if mask = 0, a and b remain as they are. */ int32_t BN_SwapWithMask(BN_BigNum *a, BN_BigNum *b, BN_UINT mask); #endif // HITLS_CRYPTO_CURVE_MONT #ifdef __cplusplus } #endif #endif /* HITLS_CRYPTO_BN */ #endif
2302_82127028/openHiTLS-examples
crypto/bn/include/crypt_bn.h
C
unknown
48,680
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_BN #include <stdint.h> #include "bn_bincal.h" #ifndef HITLS_SIXTY_FOUR_BITS #error Bn binical x8664 optimizer must open BN-64. #endif // r = a + b, len = n, return carry BN_UINT BinAdd(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, uint32_t n) { if (n == 0) { return 0; } BN_UINT ret = 0; BN_UINT times = n >> 2; BN_UINT rem = n & 3; asm volatile( ".align 3 \n" " mov %0, #1 \n" " adcs %0, xzr, %0 \n" // clear C flags " mov %0, #0 \n" " cbz %1, 3f \n" "4: add x4, %3, %0 \n" " add x5, %4, %0 \n" " add x6, %5, %0 \n" " ldp x7, x8, [x5] \n" " ldp x9, x10, [x5,#16] \n" " ldp x11, x12, [x6] \n" " ldp x13, x14, [x6,#16] \n" " adcs x7, x7, x11 \n" " adcs x8, x8, x12 \n" " adcs x9, x9, x13 \n" " adcs x10, x10, x14 \n" " stp x7, x8, [x4] \n" " stp x9, x10, [x4, #16] \n" " sub %1, %1, #0x1 \n" " add %0, %0, #0x20 \n" " cbnz %1, 4b \n" "3: cbz %2, 2f \n" // times <= 0, jump to single cycle "1: ldr x7, [%4, %0] \n" " ldr x8, [%5, %0] \n" " adcs x7, x7, x8 \n" " str x7, [%3, %0] \n" " sub %2, %2, #0x1 \n" " add %0, %0, #0x8 \n" " cbnz %2, 1b \n" "2: mov %0, #0 \n" " adcs %0, xzr, %0 \n" :"+&r" (ret), "+r"(times), "+r"(rem) :"r"(r), "r"(a), "r"(b) :"x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "cc", "memory"); return ret & 1; } // r = a - b, len = n, return carry BN_UINT BinSub(BN_UINT *r, const BN_UINT *a, const BN_UINT *b, uint32_t n) { if (n == 0) { return 0; } BN_UINT ret = 0; BN_UINT rem = n & 3; BN_UINT times = n >> 2; asm volatile( ".align 3 \n" " mov %0, #1 \n" " sbcs %0, %0, xzr \n" // clear C flags " mov %0, #0 \n" " cbz %1, 2f \n" "4: add x4, %3, %0 \n" " add x5, %4, %0 \n" " add x6, %5, %0 \n" " ldp x7, x8, [x5] \n" " ldp x9, x10, [x5,#16] \n" " ldp x11, x12, [x6] \n" " ldp x13, x14, [x6,#16] \n" " sbcs x7, x7, x11 \n" " sbcs x8, x8, x12 \n" " sbcs x9, x9, x13 \n" " sbcs x10, x10, x14 \n" " stp x7, x8, [x4] \n" " stp x9, x10, [x4, #16] \n" " sub %1, %1, #0x1 \n" " add %0, %0, #0x20 \n" " cbnz %1, 4b \n" "2: cbz %2, 3f \n" // times <= 0, jump to single cycle "1: ldr x7, [%4, %0] \n" " ldr x8, [%5, %0] \n" " sbcs x7, x7, x8 \n" " str x7, [%3, %0] \n" " sub %2, %2, #0x1 \n" " add %0, %0, #0x8 \n" " cbnz %2, 1b \n" "3: mov %0,#0 \n" " sbcs %0,xzr,%0 \n" :"+&r" (ret), "+r"(times), "+r"(rem) :"r"(r), "r"(a), "r"(b) :"x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "cc", "memory"); return ret & 1; } // r = r - a * m, return the carry; BN_UINT BinSubMul(BN_UINT *r, const BN_UINT *a, BN_UINT aSize, BN_UINT m) { BN_UINT borrow = 0; BN_UINT i = 0; asm volatile( ".align 3 \n" "2: ldr x4, [%3, %1] \n" // x4 = r[i] " ldr x5, [%4, %1] \n" // x5 = r[i] " mul x7, x5, %5 \n" // x7 = al " umulh x6, x5, %5 \n" // x6 = ah " adds x7, %0, x7 \n" // x7 = borrow + al " adcs %0, x6, xzr \n" // borrow = ah + H(borrow + al) " cmp x7, x4 \n" // if r[i] > borrow + al, dont needs carry " beq 1f \n" " adc %0, %0, xzr \n" "1: sub x4, x4, x7 \n" " str x4, [%3, %1] \n" " sub %2, %2, #0x1 \n" " add %1, %1, #0x8 \n" " cbnz %2, 2b \n" :"+&r" (borrow), "+r"(i), "+r"(aSize) :"r"(r), "r"(a), "r"(m) :"x4", "x5", "x6", "x7", "cc", "memory"); return borrow; } /* Obtains the number of 0s in the first x most significant bits of data. */ uint32_t GetZeroBitsUint(BN_UINT x) { BN_UINT count; asm ("clz %0, %1" : "=r" (count) : "r" (x)); return (uint32_t)count; } #endif /* HITLS_CRYPTO_BN */
2302_82127028/openHiTLS-examples
crypto/bn/src/armv8_bn_bincal.c
C
unknown
7,049
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /* * Description: Big number Montgomery modular multiplication in armv8 implementation, MontMul_Asm * Ref: Montgomery Multiplication * Process: To cal A * B mod n, we can convert to mont form, and cal A*B*R^(-1). * Detail: * intput:A = (An-1,...,A1,A0)b, B = (Bn-1,...,B1,B0)b, n, n' * output:A*B*R^(-1) * tmp = (tn,tn-1,...,t1,t0)b, initialize to 0 * for i: 0 -> (n-1) * ui = (t0 + Ai*B0)m' mod b * t = (t + Ai*B + ui * m) / b * if t >= m * t -= m * return t; * * Deal process: * i. size % 8 == 0 & a == b --> Sqr8x --> complete multiplication * --> size == 8, goto single reduce step * --> size >= 8, goto loop reduce process * ii. size % 4 == 0 --> Mul4x * --> size == 4, goto single step * --> size >= 4, goto loop process * iii. Ordinary --> Mul1x * --> size == 2, goto single step * --> size >= 2, goto loop process * */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_BN #include "crypt_arm.h" .arch armv8-a+crypto .file "bn_mont_armv8.S" .text .global MontMul_Asm .type MontMul_Asm, %function .align 5 MontMul_Asm: AARCH64_PACIASP tst x5, #7 b.eq MontSqr8 tst x5, #3 b.eq MontMul4 stp x29, x30, [sp, #-64]! mov x29, sp stp x23, x24, [sp, #16] stp x21, x22, [sp, #32] stp x19, x20, [sp, #48] sub x21, x5 , #2 // j = size-2 cbnz x21,.LMul1xBegin // if size == 2, goto single step ldp x15, x16, [x1] // a[0], a[1] ldp x19, x20, [x2] // b[0], b[1] ldp x9, x10, [x3] // n[0], n[1] mul x23 , x15 , x19 // x23 = lo(a[0] * b[0]) umulh x24 , x15 , x19 // x24 = hi(a[0] * b[0]) mul x6, x16 , x19 // x6 = lo(a[1] * b[0]) umulh x7, x16 , x19 // x7 = hi(a[1] * b[0]) mul x11, x23 , x4 // x11 = lo(t[0] * k0) umulh x19, x9, x11 // x19 = hi(n[0] * t[0]*k0) mul x12, x10, x11 // x12 = lo(n[1] * t[0]*k0) umulh x13, x10, x11 // x13 = hi(n[1] * t[0]*k0) // we knowns a*b + n'n = 0 (mod R) // so lo(a[0] * b[0]) + lo(n[0] * t[0]*k0) = 0 (mod R) // if lo(a[0] * b[0]) > 0, then 'lo(a[0] * b[0]) + lo(n[0] * t[0]*k0)' would overflow, // else if lo(a[0] * b[0]) == 0, then lo(n[0] * t[0]*k0) == 0 cmp x23, #1 adc x19, x19, xzr adds x23 , x6, x24 // x23 = lo(a[1] * b[0]) + hi(a[0] * b[0]) adc x24 , x7, xzr // x24 = hi(a[1] * b[0]) + CF adds x8, x12, x19 // x8 = lo(n[1] * t[0]*k0) + hi(n[0] * t[0]*k0) adc x19, x13, xzr // x19 = hi(n[1] * t[0]*k0) + CF adds x21, x8, x23 // x21 = lo(n[1] * t[0]*k0) + hi(n[0] * t[0]*k0) adcs x22, x19, x24 adc x23, xzr, xzr // x23 = CF mul x14 , x15 , x20 // a[0] * b[1] umulh x15 , x15 , x20 mul x6, x16 , x20 // a[1] * b[1] umulh x7, x16 , x20 adds x14 , x14 , x21 adc x15 , x15 , xzr mul x11, x14 , x4 // t[0] * k0 umulh x20, x9, x11 mul x12, x10, x11 // n[1] * t[0]*k0 umulh x13, x10, x11 cmp x14 , #1 // Check whether the low location is carried. adc x20, x20, xzr adds x14 , x6, x15 adc x15 , x7, xzr adds x21, x12, x20 adcs x20, x13, x23 adc x23, xzr, xzr adds x14 , x14 , x22 adc x15 , x15 , xzr adds x21, x21, x14 adcs x20, x20, x15 adc x23, x23, xzr // x23 += CF subs x12 , x21, x9 sbcs x13, x20, x10 sbcs x23, x23, xzr // update CF csel x10, x21, x12, lo csel x11, x20, x13, lo stp x10, x11, [x0] b .LMul1xEnd .LMul1xBegin: mov x24, x5 // the outermost pointers of our loop lsl x5 , x5 , #3 sub x22, sp , x5 // The space size needs to be applied for. and x22, x22, #-16 // For 4-byte alignment mov sp , x22 // Apply for Space. mov x6, x5 mov x23, xzr .LMul1xInitstack: sub x6, x6, #8 str xzr, [x22], #8 cbnz x6, .LMul1xInitstack mov x22, sp .LMul1xLoopProces: sub x24, x24, #1 sub x21, x5 , #16 // j = size-2 // Begin mulx ldr x17, [x2], #8 // b[i] ldp x15, x16, [x1], #16 // a[0], a[1] ldp x9, x10, [x3], #16 // n[0], n[1] ldr x19, [x22] // The sp val is 0 during initialization. mul x14 , x15 , x17 // a[0] * b[i] umulh x15 , x15 , x17 mul x6, x16 , x17 // a[1] * b[i] umulh x7, x16 , x17 adds x14 , x14 , x19 adc x15 , x15 , xzr mul x11, x14 , x4 umulh x9, x9, x11 mul x12, x10, x11 // n[1] * t[0]*k0 cmp x14, #1 umulh x13, x10, x11 .LMul1xPrepare: sub x21, x21, #8 // index -= 1 ldr x16, [x1], #8 // a[i] ldr x10, [x3], #8 // n[i] ldr x19, [x22, #8] // t[j] adc x9, x9, xzr adds x14 , x6, x15 adc x15 , x7, xzr adds x8, x12, x9 adc x9, x13, xzr mul x6, x16, x17 // a[j] * b[i] adds x14, x14 , x19 umulh x7, x16 , x17 adc x15, x15 , xzr mul x12, x10, x11 // n[j] * t[0]*k0 adds x8, x8, x14 umulh x13, x10, x11 str x8, [x22], #8 // t[j-1] cbnz x21, .LMul1xPrepare .LMul1xReduce: ldr x19, [x22, #8] adc x9, x9, xzr adds x14 , x6, x15 adc x15 , x7, xzr adds x8, x12, x9 adcs x9, x13, x23 adc x23, xzr, xzr adds x14 , x14 , x19 adc x15 , x15 , xzr adds x8, x8, x14 adcs x9, x9, x15 adc x23, x23, xzr // x23 += CF, carry of the most significant bit. stp x8, x9, [x22], #8 mov x22, sp sub x1 , x1 , x5 subs x3 , x3 , x5 // x3 = &n[0] cbnz x24, .LMul1xLoopProces mov x1, x0 mov x21, x5 // get index .LMul1xSubMod: ldr x19, [x22], #8 ldr x10, [x3], #8 sub x21, x21, #8 // j-- sbcs x16, x19, x10 // t[j] - n[j] str x16, [x1], #8 // r[j] = t[j] - n[j] cbnz x21,.LMul1xSubMod sbcs x23, x23, xzr // x23 -= CF mov x22, sp .LMul1xCopy: ldr x19, [x22], #8 ldr x16, [x0] sub x5, x5, #8 // size-- csel x10, x19, x16, lo str x10, [x0], #8 cbnz x5 , .LMul1xCopy .LMul1xEnd: ldp x23, x24, [x29, #16] mov sp , x29 ldp x21, x22, [x29, #32] ldp x19, x20, [x29, #48] ldr x29, [sp], #64 AARCH64_AUTIASP ret .size MontMul_Asm, .-MontMul_Asm .type MontSqr8, %function MontSqr8: AARCH64_PACIASP cmp x1, x2 b.ne MontMul4 stp x29, x30, [sp, #-128]! // sp = sp - 128(Modify the SP and then save the SP.), [sp] = x29, [sp + 8] = x30, // !Indicates modification sp mov x29, sp // x29 = sp, The sp here has been reduced by 128. stp x27, x28, [sp, #16] stp x25, x26, [sp, #32] stp x23, x24, [sp, #48] stp x21, x22, [sp, #64] stp x19, x20, [sp, #80] stp x0 , x3 , [sp, #96] // offload r and n, Push the pointers of r and n into the stack. str x4 , [sp, #112] // store n0 lsl x5, x5, #3 // x5 = x5 * 8, Converts size to bytes. sub x2, sp, x5, lsl#1 // x2 = sp - 2*x5*8, x5 = size, x2 points to the start address of a 2*size memory. *8 is to convert to bytes mov sp, x2 // Alloca, Apply for Space. mov x19, x5 // The lowest eight data blocks do not need to be cleared. eor v0.16b,v0.16b,v0.16b eor v1.16b,v1.16b,v1.16b .LSqr8xStackInit: sub x19, x19, #8*8 // Offset 64, cyclic increment. st1 {v0.2d, v1.2d}, [x2], #32 st1 {v0.2d, v1.2d}, [x2], #32 st1 {v0.2d, v1.2d}, [x2], #32 st1 {v0.2d, v1.2d}, [x2], #32 cbnz x19, .LSqr8xStackInit // When x19 = 0, the loop exits. mov x2 , sp // After clear to zero, assign sp back to x2. ldp x27, x28, [x2] ldp x25, x26, [x2] ldp x23, x24, [x2] ldp x21, x22, [x2] add x3 , x1 , x5 // x3 = x1 + bytes(size * 8) ldp x14 , x15 , [x1], #16 // x14 = a[0], x15 = a[1] ldp x16 , x17 , [x1], #16 // x16 = a[2], x17 = a[3] ldp x6, x7, [x1], #16 // x6 = a[4], x7 = a[5] ldp x8, x9, [x1], #16 // x8 = a[6], x9 = a[7] .LSqr8xLoopMul: mul x10, x14, x15 // a[0] * a[1~4] mul x11, x14, x16 // keep cache hit ratio of x6 mul x12, x14, x17 mul x13, x14, x6 adds x28, x28, x10 // x27~x22 = t[0~7], x28 = t[1] = lo(a[0]*a[1]), adds is used to set CF to 0. adcs x25, x25, x11 // x10~x17 Used to save subsequent calculation results mul x10, x14 , x7 // lo(a[0] * a[5~7]), keep cache hit ratio of x14, the same below mul x11, x14 , x8 adcs x26, x26, x12 adcs x23, x23, x13 // t[4] = lo(a[0] * a[4]) adcs x24, x24, x10 // x24~x22 = t[5~7] mul x12, x14 , x9 // lo(a[0] * a[7]) stp x27, x28, [x2], #8*2 // t[0] = a[0]^2, Because the square term is not calculated temporarily, // so t[0] = 0, t[1] = a[0] * a[1] + carry adcs x21, x21, x11 adcs x22, x22, x12 // t[7] += lo(a[0] * a[7]), Carrying has to be given t[8] adc x27, xzr, xzr // x27 = CF ( Set by t[7] += lo(a[0] * a[7]) ), umulh x13, x14 , x15 // hi(a[0] * a[1~4]), Use x17 to keep the cache hit umulh x10, x14 , x16 umulh x11, x14 , x17 umulh x12, x14 , x6 // In the new round, the first calculation does not need to be carried, but the CF bit needs to be modified. adds x25, x25, x13 // t[2] += hi(a[0] * a[1]) adcs x26, x26, x10 adcs x23, x23, x11 adcs x24, x24, x12 // t[5] += hi(a[0] * a[4]) umulh x13, x14 , x7 // hi(a[0] * a[5~7]) umulh x10, x14 , x8 umulh x11, x14 , x9 //----- lo(a[1] * a[2~4]) ------ adcs x21, x21, x13 // t[6] += hi(a[0] * a[5]) adcs x22, x22, x10 // t[7] += hi(a[0] * a[6]) adc x27, x27, x11 // t[8] += hi(a[0] * a[7]) mul x12, x15, x16 // lo(a[1] * a[2]) mul x13, x15, x17 mul x10, x15, x6 //----- lo(a[1] * a[5~7]) ------ adds x26, x26, x12 // t[3] += lo(a[1] * a[2]), The first calculation of this round // does not take into account the previous carry, and the CF is not modified in line 118. adcs x23, x23, x13 // t[4] += lo(a[1] * a[3]) adcs x24, x24, x10 // t[5] += lo(a[1] * a[4]) mul x11, x15 , x7 mul x12, x15 , x8 mul x13, x15 , x9 //----- hi(a[1] * a[2~5]) ------ adcs x21, x21, x11 // t[6] += lo(a[1] * a[5]) adcs x22, x22, x12 // t[7] += lo(a[1] * a[6]) adcs x27, x27, x13 // t[8] += lo(a[1] * a[7]) umulh x10, x15, x16 // hi(a[1] * a[2]) umulh x11, x15, x17 umulh x12, x15, x6 umulh x13, x15, x7 stp x25, x26, [x2], #8*2 // t[2] and t[3] are calculated and stored in the memory. // x25 and x22 are used to store t[10] and t[11]. adc x28, xzr, xzr // t[9] = CF ( Set by t[8] += lo(a[1] * a[7]) ) //In the new round, the first calculation does not need to be carried, but the CF bit needs to be modified. //----- hi(a[1] * a[6~7]) ------ adds x23, x23, x10 // t[4] += hi(a[1] * a[2]) adcs x24, x24, x11 // t[5] += hi(a[1] * a[3]) adcs x21, x21, x12 // t[6] += hi(a[1] * a[4]) umulh x10, x15 , x8 // hi(a[1] * a[6]) umulh x11, x15 , x9 // hi(a[1] * a[7]) //----- lo(a[2] * a[3~7]) ------ adcs x22, x22, x13 // t[7] += hi(a[1] * a[5]) adcs x27, x27, x10 // t[8] += hi(a[1] * a[6]) adc x28, x28, x11 // t[9] += hi(a[1] * a[7]), Here, only the carry of the previous round mul x12, x16, x17 // lo(a[2] * a[3]) mul x13, x16, x6 mul x10, x16, x7 // of calculation is retained before x20 calculation. Add x15 to the carry. mul x11, x16 , x8 adds x24, x24, x12 // t[5] += lo(a[2] * a[3]), For the first calculation of this round, // the previous carry is not considered. mul x12, x16 , x9 adcs x21, x21, x13 // t[6] += lo(a[2] * a[4]) //----- hi(a[2] * a[3~7]) ------ adcs x22, x22, x10 // t[7] += lo(a[2] * a[5]) umulh x13, x16, x17 // hi(a[2] * a[3]) umulh x10, x16, x6 adcs x27, x27, x11 // t[8] += lo(a[2] * a[6]) adcs x28, x28, x12 // t[9] += lo(a[2] * a[7]) umulh x11, x16, x7 umulh x12, x16, x8 stp x23, x24, [x2], #8*2 // After t[4] and t[5] are calculated, they are stored in the memory. // x23 and x24 are used to store t[12] and t[13]. adc x25, xzr, xzr // t[10] = CF ( Set by t[9] += lo(a[2] * a[7]) ) // In the new round, the first calculation does not need to be carried, but the CF bit needs to be modified. adds x21, x21, x13 // t[6] += hi(a[2] * a[3]) adcs x22, x22, x10 // t[7] += hi(a[2] * a[4]) umulh x13, x16, x9 //----- lo(a[3] * a[4~7]) ------ adcs x27, x27, x11 // t[8] += hi(a[2] * a[5]) adcs x28, x28, x12 // t[9] += hi(a[2] * a[6]) adc x25, x25, x13 // t[10] += hi(a[2] * a[7]) mul x10, x17, x6 mul x11, x17, x7 mul x12, x17, x8 mul x13, x17, x9 //----- hi(a[3] * a[4~7]) ------ adds x22, x22, x10 // t[7] += lo(a[3] * a[4]) adcs x27, x27, x11 // t[8] += lo(a[3] * a[5]) adcs x28, x28, x12 // t[9] += lo(a[3] * a[6]) adcs x25, x25, x13 // t[10] += lo(a[3] * a[7]) umulh x10, x17, x6 umulh x11, x17, x7 umulh x12, x17, x8 umulh x13, x17, x9 stp x21, x22, [x2], #8*2 // t[6] and t[7] are calculated and stored in the memory. // x21 and x26 are used to store t[14] and t[15]. adc x26, xzr, xzr // t[11] = CF ( Set by t[10] += lo(a[3] * a[7]) ) // In the new round, the first calculation does not need to be carried, but the CF bit needs to be modified. adds x27, x27, x10 // t[8] += hi(a[3] * a[4]) //----- lo(a[4] * a[5~7]) ------ adcs x28, x28, x11 // t[9] += hi(a[3] * a[5]) adcs x25, x25, x12 // t[10] += hi(a[3] * a[6]) adc x26, x26, x13 // t[11] += hi(a[3] * a[7]) mul x10, x6, x7 mul x11, x6, x8 mul x12, x6, x9 //----- hi(a[4] * a[5~7]) ------ adds x28, x28, x10 // t[9] += lo(a[4] * a[5]) adcs x25, x25, x11 // t[10] += lo(a[4] * a[6]) adcs x26, x26, x12 // t[11] += lo(a[4] * a[7]) umulh x13, x6, x7 umulh x10, x6, x8 umulh x11, x6, x9 //----- lo(a[5] * a[6~7]) ------ mul x12, x7, x8 // This is actually a new round, but only t[0-7] can be calculated in each cycle, // and t[8-15] retains the intermediate calculation result. adc x23, xzr, xzr // t[12] = CF( Set by t[11] += lo(a[4] * a[7]) ) adds x25, x25, x13 // t[10] += hi(a[4] * a[5]) adcs x26, x26, x10 // t[11] += hi(a[4] * a[6]) mul x13, x7, x9 //----- hi(a[5] * a[6~7]) ------ adc x23, x23, x11 // t[12] += hi(a[4] * a[7]) umulh x10, x7, x8 umulh x11, x7, x9 adds x26, x26, x12 // t[11] += lo(a[5] * a[6]) //----- lo(a[6] * a[7]) ------ adcs x23, x23, x13 // t[12] += lo(a[5] * a[7]) mul x12, x8, x9 //----- hi(a[6] * a[7]) ------ adc x24, xzr, xzr // t[13] = CF ( Set by t[12] += lo(a[5] * a[7]) ), umulh x13, x8, x9 // This operation is required when a new umulh is added. adds x23, x23, x10 // t[12] += hi(a[5] * a[6]) adc x24, x24, x11 // t[13] += hi(a[5] * a[7]) sub x19, x3, x1 // x3 = &a[size], x1 = &a[8], x19 = (size - 8) * 8 adds x24, x24, x12 // t[13] += lo(a[6] * a[7]) adc x21, xzr, xzr // t[14] = CF ( set by t[13] += lo(a[6] * a[7]) ) add x21, x21, x13 // t[14] += hi(a[6] * a[7]), There must be no carry in the last step. cbz x19, .LSqr8xLoopMulEnd mov x0, x1 // x0 = &a[8] mov x22, xzr //######################################## //# a[0~7] * a[8~15] # //######################################## .LSqr8xHighMulBegian: mov x19, #-8*8 // Loop range. x0 can retrieve a[0–7] based on this offset. ldp x14 , x15 , [x2, #8*0] // x14 = t[8] , x15 = t[9] adds x27, x27, x14 // t[8](t[8] reserved in the previous round of calculation) + = t[8] // (t[8] taken from memory, initially 0) adcs x28, x28, x15 // t[9] += t[9], be the same as the above ldp x16 , x17 , [x2, #8*2] // x16 = t[10], x17 = t[11] ldp x14 , x15 , [x1], #16 // x14 = a[8], x15 = a[9] adcs x25, x25, x16 adcs x26, x26, x17 ldp x6, x7, [x2, #8*4] // x6 = t[12], x7 = t[13] ldp x16 , x17 , [x1], #16 // x16 = a[10], x17 = a[11] adcs x23, x23, x6 adcs x24, x24, x7 ldp x8, x9, [x2, #8*6] // x8 = t[14], x9 = t[15] ldp x6, x7, [x1], #16 // x6 = a[12], x7 = a[13] adcs x21, x21, x8 adcs x22, x22, x9 // t[15] = t[15] + CF, Because a[7]*a[7] is not calculated previously, t[15]=0 ldp x8, x9, [x1], #16 // x8 = a[14], x9 = a[15] .LSqr8xHighMulProces: ldr x4 , [x0, x19] // x4 = [x0 + x19] = [x0 - 56] = [&a[8] - 56] = a[8 - 7] = a[1] //-----lo(a[0] * a[8~11])----- adc x20, xzr, xzr // x20 += CF, Save the carry of t[15]. The same operation is performed below. add x19, x19, #8 // x19 += 8, Loop step size mul x10, x4 , x14 // x4 = a[0], x14 = a[8], x10 = lo(a[0] * a[8]) mul x11, x4 , x15 // x11 = lo(a[0] * a[9]) mul x12, x4 , x16 // x12 = lo(a[0] * a[10]) mul x13, x4 , x17 // x13 = lo(a[0] * a[11]) //-----lo(a[0] * a[12~15])----- adds x27, x27, x10 // CF does not need to be added for the first calculation, // t[8] += lo(a[0] * a[8]) adcs x28, x28, x11 // t[9] += lo(a[0] * a[9]) adcs x25, x25, x12 // t[10] += lo(a[0] * a[10]) adcs x26, x26, x13 // t[11] += lo(a[0] * a[11]) mul x10, x4 , x6 mul x11, x4 , x7 mul x12, x4 , x8 mul x13, x4 , x9 //-----hi(a[0] * a[8~11])----- adcs x23, x23, x10 // t[12] += lo(a[0] * a[12]) adcs x24, x24, x11 // t[13] += lo(a[0] * a[13]) adcs x21, x21, x12 // t[14] += lo(a[0] * a[14]) adcs x22, x22, x13 // t[15] += lo(a[0] * a[15]) umulh x10, x4 , x14 umulh x11, x4 , x15 umulh x12, x4 , x16 umulh x13, x4 , x17 adc x20, x20, xzr // x20 += CF, Save the carry of t[15] str x27, [x2], #8 // [x2] = t[8], x2 += 8, x27~x22 = t[9~16], // Update the mapping relationship to facilitate cycling. // x27~x26 always correspond to t[m~m+7], and x19 is always the LSB of the window //-----hi(a[0] * a[12~15])----- adds x27, x28, x10 // t[9] += hi(a[0] * a[8]), The last calculation was to calculate t[15], // so carry cannot be added to t[9], so adds is used adcs x28, x25, x11 // t[10] += hi(a[0] * a[9]) adcs x25, x26, x12 // t[11] += hi(a[0] * a[10]) adcs x26, x23, x13 // t[12] += hi(a[0] * a[11]) umulh x10, x4 , x6 umulh x11, x4 , x7 umulh x12, x4 , x8 umulh x13, x4 , x9 // x13 = hi(a[0] * a[15]) adcs x23, x24, x10 // t[13] += hi(a[0] * a[12]) adcs x24, x21, x11 // t[14] += hi(a[0] * a[13]) adcs x21, x22, x12 // t[15] += hi(a[0] * a[14]) adcs x22, x20, x13 // t[16] = hi(a[0] * a[15]) + CF cbnz x19, .LSqr8xHighMulProces // When exiting the loop, x0 = &a[8], x2 = &t[16] sub x16, x1, x3 // x3 = x1 + x5 * 8(Converted to bytes), When x1 = x3, the loop ends. cbnz x16, .LSqr8xHighMulBegian // x0 is the outer loop, x1 is the inner loop, and the inner loop ends. // In this case, x2 = &a[size], out-of-bounds position. mov x1, x0 // Outer Loop Increment, x1 = &a[16] ldp x14 , x15 , [x1], #16 // x14 = a[8] , x15 = a[9] ldp x16 , x17 , [x1], #16 // x16 = a[10], x17 = a[11] ldp x6, x7, [x1], #16 // x6 = a[12], x7 = a[13] ldp x8, x9, [x1], #16 // x8 = a[14], x9 = a[15] sub x10, x3 , x1 // Check whether the outer loop ends, x3 = &a[size], x10 = (size - 16)*8 cbz x10, .LSqr8xLoopMul sub x11, x2 , x10 // x2 = &t[24], x11 = &t[16] stp x27, x28, [x2 , #8*0] // t[24] = x27, t[25] = x28 ldp x27, x28, [x11, #8*0] // x27 = t[16], x28 = t[17] stp x25, x26, [x2 , #8*2] // t[26] = x25, t[27] = x26 ldp x25, x26, [x11, #8*2] // x25 = t[18], x26 = t[19] stp x23, x24, [x2 , #8*4] // t[28] = x23, t[29] = x24 ldp x23, x24, [x11, #8*4] // x23 = t[20], x24 = t[21] stp x21, x22, [x2 , #8*6] // t[30] = x21, t[31] = x22 ldp x21, x22, [x11, #8*6] // x21 = t[22], x22 = t[23] mov x2 , x11 // x2 = &t[16] b .LSqr8xLoopMul .align 4 .LSqr8xLoopMulEnd: //===== Calculate the squared term ===== //----- sp = &t[0] , x2 = &t[24]----- sub x10, x3, x5 // x10 = a[0] stp x27, x28, [x2, #8*0] // t[24] = x27, t[25] = x28 stp x25, x26, [x2, #8*2] // When this step is performed, the calculation results reserved for x27–x26 // are not pushed to the stack. stp x23, x24, [x2, #8*4] stp x21, x22, [x2, #8*6] ldp x11, x12, [sp, #8*1] // x11 = t[1], x12 = t[2] ldp x15, x17, [x10], #16 // x15 = a[0], x17 = a[1] ldp x7, x9, [x10], #16 // x7 = a[2], x9 = a[3] mov x1, x10 ldp x13, x10, [sp, #8*3] // x13 = t[3], x10 = t[4] mul x27, x15, x15 // x27 = lo(a[0] * a[0]) umulh x15, x15, x15 // x15 = hi(a[0] * a[0]) mov x2 , sp // x2 = sp = &t[0] mul x16, x17, x17 // x16 = lo(a[1] * a[1]) adds x28, x15, x11, lsl#1 // x28 = x15 + (x11 * 2) = hi(a[0] * a[0]) + 2 * t[1] umulh x17, x17, x17 // x17 = hi(a[1] * a[1]) extr x11, x12, x11, #63 // Lower 63 bits of x11 = x16 | most significant bit of x15 // Cyclic right shift by 63 bits to obtain the lower bit, // which is equivalent to cyclic left shift by 1 bit to obtain the upper bit. // The purpose is to *2. // x11 = 2*t[2](Ignore the overflowed part) + carry of (2*t[1]) mov x19, x5 // x19 = size*8 .LSqr8xDealSquare: adcs x25, x16 , x11 // x25 = lo(a[1] * a[1]) + 2*t[2] extr x12, x13, x12, #63 // x12 = 2*t[3](Ignore the overflowed part) + carry of (2*t[2]) adcs x26, x17 , x12 // x26 = hi(a[1] * a[1]) + 2*t[3] sub x19, x19, #8*4 // x19 = (size - 8)*8 stp x27, x28, [x2], #16 // t[0~3]Re-push stack stp x25, x26, [x2], #16 mul x6, x7, x7 // x6 = lo(a[2] * a[2]) umulh x7, x7, x7 // x7 = hi(a[2] * a[2]) mul x8, x9, x9 // x6 = lo(a[3] * a[3]) umulh x9, x9, x9 // x7 = hi(a[3] * a[3]) ldp x11, x12, [x2, #8] // x11 = t[5], x12 = t[6] extr x13, x10, x13, #63 // x13 = 2*t[4](Ignore the overflowed part) + carry of(2*t[3]) extr x10, x11, x10, #63 // x10 = 2*t[5](Ignore the overflowed part) + carry of(2*t[4]) adcs x23, x6, x13 // x23 = lo(a[2] * a[2]) + 2*t[4] adcs x24, x7, x10 // x24 = hi(a[2] * a[2]) + 2*t[5] cbz x19, .LSqr8xReduceStart ldp x13, x10, [x2, #24] // x13 = t[7], x10 = t[8] extr x11, x12, x11, #63 // x11 = 2*t[6](Ignore the overflowed part) + carry of(2*t[5]) extr x12, x13, x12, #63 // x12 = 2*t[7](Ignore the overflowed part) + carry of(2*t[6]) adcs x21, x8, x11 // x21 = lo(a[3] * a[3]) + 2*t[6] adcs x22, x9, x12 // x22 = hi(a[3] * a[3]) + 2*t[7] stp x23, x24, [x2], #16 // t[4~7]re-push stack stp x21, x22, [x2], #16 ldp x15, x17, [x1], #8*2 // x15 = a[4], x17 = a[5], x1 += 16 = &a[6] ldp x11, x12, [x2, #8] // x11 = t[9], x12 = t[10] mul x14 , x15 , x15 // x14 = lo(a[4] * a[4]) umulh x15 , x15 , x15 // x15 = hi(a[4] * a[4]) mul x16 , x17 , x17 // x16 = lo(a[5] * a[5]) umulh x17 , x17 , x17 // x17 = hi(a[5] * a[5]) extr x13, x10, x13, #63 // x13 = 2*t[8](Ignore the overflowed part) + carry of(2*t[7]) adcs x27, x14 , x13 // x27 = lo(a[4] * a[4]) + 2*t[8] extr x10, x11, x10, #63 // x10 = 2*t[9](Ignore the overflowed part) + carry of(2*t[8]) adcs x28, x15 , x10 // x28 = hi(a[4] * a[4]) + 2*t[9] extr x11, x12, x11, #63 // x11 = 2*t[10](Ignore the overflowed part) + carry of(2*t[9]) ldp x13, x10, [x2, #8*3] // Line 438 has obtained t[9] and t[10], x13 = &t[11], x10 = &t[12] ldp x7, x9, [x1], #8*2 // x7 = a[6], x9 = a[7], x1 += 16 = &a[8] b .LSqr8xDealSquare .LSqr8xReduceStart: extr x11, x12, x11, #63 // x11 = 2*t[2*size-2](Ignore the overflowed part) + carry of (2*t[2*size-3]) adcs x21, x8, x11 // x21 = lo(a[size-1] * a[size-1]) + 2*t[2*size-2] extr x12, xzr, x12, #63 // x12 = 2*t[2*size-1](Ignore the overflowed part) + carry of (2*t[2*size-2]) adc x22, x9, x12 // x22 = hi(a[size-1] * a[size-1]) + 2*t[2*size-1] ldp x1, x4, [x29, #104] // Pop n and k0 out of the stack, x1 = &n[0], x4 = k0 stp x23, x24, [x2] // t[2*size-4 ~ 2*size-1]re-push stack stp x21, x22, [x2,#8*2] cmp x5, #64 // if size == 8, we can goto Single step reduce b.ne .LSqr8xReduceLoop ldp x14 , x15 , [x1], #16 // x14~x9 = n[0~7] ldp x16 , x17 , [x1], #16 ldp x6, x7, [x1], #16 ldp x8, x9, [x1], #16 ldp x27, x28, [sp] // x14~x9 = t[0~7] ldp x25, x26, [sp,#8*2] ldp x23, x24, [sp,#8*4] ldp x21, x22, [sp,#8*6] mov x19, #8 mov x2 , sp // if size == 8, goto single reduce step .LSqr8xSingleReduce: mul x20, x4, x27 sub x19, x19, #1 //----- lo(n[1~7] * lo(t[0]*k0)) ----- mul x11, x15 , x20 mul x12, x16 , x20 mul x13, x17 , x20 mul x10, x6, x20 cmp x27, #1 adcs x27, x28, x11 adcs x28, x25, x12 adcs x25, x26, x13 adcs x26, x23, x10 mul x11, x7, x20 mul x12, x8, x20 mul x13, x9, x20 //----- hi(n[0~7] * lo(t[0]*k0)) ----- adcs x23, x24, x11 adcs x24, x21, x12 adcs x21, x22, x13 adc x22, xzr, xzr // x22 += CF umulh x10, x14 , x20 umulh x11, x15 , x20 umulh x12, x16 , x20 umulh x13, x17 , x20 adds x27, x27, x10 adcs x28, x28, x11 adcs x25, x25, x12 adcs x26, x26, x13 umulh x10, x6, x20 umulh x11, x7, x20 umulh x12, x8, x20 umulh x13, x9, x20 adcs x23, x23, x10 adcs x24, x24, x11 adcs x21, x21, x12 adc x22, x22, x13 cbnz x19, .LSqr8xSingleReduce // Need cycle 8 times ldp x10, x11, [x2, #64] // x10 = t[8], x11 = t[9] ldp x12, x13, [x2, #80] adds x27, x27, x10 adcs x28, x28, x11 ldp x10, x11, [x2, #96] adcs x25, x25, x12 adcs x26, x26, x13 adcs x23, x23, x10 ldp x12, x13, [x2, #112] adcs x24, x24, x11 adcs x21, x21, x12 adcs x22, x22, x13 adc x20, xzr, xzr ldr x0, [x29, #96] // r Pop-Stack // t - mod subs x14, x27, x14 sbcs x15, x28, x15 sbcs x16, x25, x16 sbcs x17, x26, x17 sbcs x6, x23, x6 sbcs x7, x24, x7 sbcs x8, x21, x8 sbcs x9, x22, x9 sbcs x20, x20, xzr // determine whether there is a borrowing // according to CF choose our result csel x14 , x27, x14 , lo csel x15 , x28, x15 , lo csel x16 , x25, x16 , lo csel x17 , x26, x17 , lo stp x14 , x15 , [x0, #8*0] csel x6, x23, x6, lo csel x7, x24, x7, lo stp x16 , x17 , [x0, #8*2] csel x8, x21, x8, lo csel x9, x22, x9, lo stp x6, x7, [x0, #8*4] stp x8, x9, [x0, #8*6] b .LMontSqr8xEnd .LSqr8xReduceLoop: add x3, x1, x5 // x3 = &n[size] mov x30, xzr ldp x14 , x15 , [x1], #16 // x14~x9 = n[0~7] ldp x16 , x17 , [x1], #16 ldp x6, x7, [x1], #16 ldp x8, x9, [x1], #16 ldp x27, x28, [sp] // x27 = t[0], x28 = t[1] ldp x25, x26, [sp,#8*2] // x25~x22 = t[2~7] ldp x23, x24, [sp,#8*4] ldp x21, x22, [sp,#8*6] mov x19, #8 mov x2 , sp .LSqr8xReduceProcess: mul x20, x4, x27 // x20 = lo(k0 * t[0]) sub x19, x19, #1 //----- lo(n[1~7] * lo(t[0]*k0)) ----- mul x11, x15, x20 // x11 = n[1] * lo(t[0]*k0) mul x12, x16, x20 // x12 = n[2] * lo(t[0]*k0) mul x13, x17, x20 // x13 = n[3] * lo(t[0]*k0) mul x10, x6, x20 // x10 = n[4] * lo(t[0]*k0) str x20, [x2], #8 // Push lo(t[0]*k0) on the stack., x2 += 8 cmp x27, #1 // Check whether the low location is carried. adcs x27, x28, x11 // x27 = t[1] + lo(n[1] * lo(t[0]*k0)) adcs x28, x25, x12 // x28 = t[2] + lo(n[2] * lo(t[0]*k0)) adcs x25, x26, x13 // x25 = t[3] + lo(n[3] * lo(t[0]*k0)) adcs x26, x23, x10 // x26 = t[4] + lo(n[4] * lo(t[0]*k0)) mul x11, x7, x20 mul x12, x8, x20 mul x13, x9, x20 //----- hi(n[0~7] * lo(t[0]*k0)) ----- adcs x23, x24, x11 // x23 = t[5] + lo(n[5] * lo(t[0]*k0)) adcs x24, x21, x12 // x24 = t[6] + lo(n[6] * lo(t[0]*k0)) adcs x21, x22, x13 // x21 = t[7] + lo(n[7] * lo(t[0]*k0)) adc x22, xzr, xzr // x22 += CF umulh x10, x14 , x20 umulh x11, x15 , x20 umulh x12, x16 , x20 umulh x13, x17 , x20 adds x27, x27, x10 // x27 += hi(n[0] * lo(t[0]*k0)) adcs x28, x28, x11 // x28 += hi(n[1] * lo(t[0]*k0)) adcs x25, x25, x12 // x25 += hi(n[2] * lo(t[0]*k0)) adcs x26, x26, x13 // x26 += hi(n[3] * lo(t[0]*k0)) umulh x10, x6, x20 umulh x11, x7, x20 umulh x12, x8, x20 umulh x13, x9, x20 adcs x23, x23, x10 // x23 += hi(n[4] * lo(t[0]*k0)) adcs x24, x24, x11 // x24 += hi(n[5] * lo(t[0]*k0)) adcs x21, x21, x12 // x21 += hi(n[6] * lo(t[0]*k0)) adc x22, x22, x13 // x22 += hi(n[7] * lo(t[0]*k0)) cbnz x19, .LSqr8xReduceProcess // Cycle 8 times, and at the end of the cycle, x2 += 8*8 ldp x10, x11, [x2, #8*0] // x10 = t[8], x11 = t[9] ldp x12, x13, [x2, #8*2] mov x0, x2 adds x27, x27, x10 adcs x28, x28, x11 ldp x10, x11, [x2,#8*4] adcs x25, x25, x12 adcs x26, x26, x13 ldp x12, x13, [x2,#8*6] adcs x23, x23, x10 adcs x24, x24, x11 adcs x21, x21, x12 adcs x22, x22, x13 ldr x4 , [x2, #-8*8] // x4 = t[0] ldp x14 , x15 , [x1], #16 // x14~x9 = &n[8]~&n[15] ldp x16 , x17 , [x1], #16 ldp x6, x7, [x1], #16 ldp x8, x9, [x1], #16 mov x19, #-8*8 .LSqr8xReduce: adc x20, xzr, xzr // x20 = CF add x19, x19, #8 mul x10, x14 , x4 mul x11, x15 , x4 mul x12, x16 , x4 mul x13, x17 , x4 adds x27, x27, x10 adcs x28, x28, x11 adcs x25, x25, x12 adcs x26, x26, x13 mul x10, x6, x4 mul x11, x7, x4 mul x12, x8, x4 mul x13, x9, x4 adcs x23, x23, x10 adcs x24, x24, x11 adcs x21, x21, x12 adcs x22, x22, x13 umulh x10, x14 , x4 umulh x11, x15 , x4 umulh x12, x16 , x4 umulh x13, x17 , x4 adc x20, x20, xzr str x27, [x2], #8 // x27 = t[8], x2 += 8 adds x27, x28, x10 // x27 = t[1] + lo(n[1] * lo(t[0]*k0)) adcs x28, x25, x11 // x28 = t[2] + lo(n[2] * lo(t[0]*k0)) adcs x25, x26, x12 // x25 = t[3] + lo(n[3] * lo(t[0]*k0)) adcs x26, x23, x13 // x26 = t[4] + lo(n[4] * lo(t[0]*k0)) umulh x10, x6, x4 umulh x11, x7, x4 umulh x12, x8, x4 umulh x13, x9, x4 // x0 = &t[8] ldr x4 , [x0, x19] adcs x23, x24, x10 adcs x24, x21, x11 adcs x21, x22, x12 adcs x22, x20, x13 cbnz x19, .LSqr8xReduce ldp x14 , x15 , [x2, #8*0] ldp x16 , x17 , [x2, #8*2] sub x19, x3, x1 // x19 = (size-16)*8 ldp x6, x7, [x2, #8*4] ldp x8, x9, [x2, #8*6] cbz x19, .LSqr8xReduceBreak ldr x4 , [x0, #-8*8] adds x27, x27, x14 adcs x28, x28, x15 adcs x25, x25, x16 adcs x26, x26, x17 adcs x23, x23, x6 adcs x24, x24, x7 adcs x21, x21, x8 adcs x22, x22, x9 ldp x14 , x15 , [x1], #16 ldp x16 , x17 , [x1], #16 ldp x6, x7, [x1], #16 ldp x8, x9, [x1], #16 mov x19, #-8*8 b .LSqr8xReduce .align 4 .LSqr8xReduceBreak: sub x12, x3, x5 // x12 = n, reassign to n ldr x4 , [x29, #112] // k0 pop-stack cmp x30, #1 // Check whether the low location is carried. adcs x10, x27, x14 adcs x11, x28, x15 stp x10, x11, [x2] , #16 ldp x27 ,x28, [x0 , #8*0] ldp x14 , x15, [x12], #16 // x12 = &n[0] (Line 638 assigns a value) adcs x25, x25, x16 adcs x26, x26, x17 adcs x23, x23, x6 adcs x24, x24, x7 adcs x21, x21, x8 adcs x22, x22, x9 adc x30, xzr, xzr ldp x16, x17, [x12], #16 ldp x6, x7, [x12], #16 ldp x8, x9, [x12], #16 stp x25, x26, [x2], #16 ldp x25, x26, [x0, #8*2] stp x23, x24, [x2], #16 ldp x23, x24, [x0, #8*4] stp x21, x22, [x2], #16 ldp x21, x22, [x0, #8*6] sub x20, x2, x29 // Check whether the loop ends mov x1, x12 mov x2, x0 // sliding window mov x19, #8 cbnz x20, .LSqr8xReduceProcess // Final step ldr x0 , [x29, #96] // r Pop-Stack add x2 , x2 , #8*8 subs x10, x27, x14 sbcs x11, x28, x15 sub x19, x5 , #8*8 mov x3 , x0 // backup x0 .LSqr8xSubMod: ldp x14 , x15, [x1], #16 sbcs x12, x25, x16 sbcs x13, x26, x17 ldp x16 , x17 , [x1], #16 stp x10, x11, [x0], #16 stp x12, x13, [x0], #16 sbcs x10, x23, x6 sbcs x11, x24, x7 ldp x6, x7, [x1], #16 sbcs x12, x21, x8 sbcs x13, x22, x9 ldp x8, x9, [x1], #16 stp x10, x11, [x0], #16 stp x12, x13, [x0], #16 ldp x27, x28, [x2], #16 ldp x25, x26, [x2], #16 ldp x23, x24, [x2], #16 ldp x21, x22, [x2], #16 sub x19, x19, #8*8 sbcs x10, x27, x14 sbcs x11, x28, x15 cbnz x19, .LSqr8xSubMod mov x2 , sp add x1 , sp , x5 sbcs x12, x25, x16 sbcs x13, x26, x17 stp x12, x13, [x0, #8*2] stp x10, x11, [x0, #8*0] sbcs x10, x23, x6 sbcs x11, x24, x7 stp x10, x11, [x0, #8*4] sbcs x12, x21, x8 sbcs x13, x22, x9 stp x12, x13, [x0, #8*6] sbcs xzr, x30, xzr // Determine whether there is a borrowing .LSqr8xCopy: ldp x14, x15, [x3, #8*0] ldp x16, x17, [x3, #8*2] ldp x6, x7, [x3, #8*4] ldp x8, x9, [x3, #8*6] ldp x27, x28, [x1], #16 ldp x25, x26, [x1], #16 ldp x23, x24, [x1], #16 ldp x21, x22, [x1], #16 sub x5, x5, #8*8 csel x10, x27, x14, lo // Condition selection instruction, lo = less than, // equivalent to x14 = (conf==lo) ? x27 : x14 csel x11, x28, x15, lo csel x12, x25, x16 , lo csel x13, x26, x17, lo csel x14, x23, x6, lo csel x15, x24, x7, lo csel x16, x21, x8, lo csel x17, x22, x9, lo stp x10, x11, [x3], #16 stp x12, x13, [x3], #16 stp x14, x15, [x3], #16 stp x16, x17, [x3], #16 cbnz x5, .LSqr8xCopy .LMontSqr8xEnd: ldr x30, [x29, #8] // Pop-Stack ldp x27, x28, [x29, #16] mov sp , x29 ldp x25, x26, [x29, #32] mov x0 , #1 ldp x23, x24, [x29, #48] ldp x21, x22, [x29, #64] ldp x19, x20, [x29, #80] // x19 = [x29 + 80], x20 = [x29 + 80 + 8], // ldp reads two 8-byte memory blocks at a time. ldr x29, [sp], #128 // x29 = [sp], sp = sp + 128,ldr reads only an 8-byte block of memory AARCH64_AUTIASP ret .size MontSqr8, .-MontSqr8 .type MontMul4, %function MontMul4: AARCH64_PACIASP stp x29, x30, [sp, #-128]! mov x29, sp stp x27, x28, [sp, #16] stp x25, x26, [sp, #32] stp x23, x24, [sp, #48] stp x21, x22, [sp, #64] stp x19, x20, [sp, #80] mov x27, xzr mov x28, xzr mov x25, xzr mov x26, xzr mov x30, xzr lsl x5 , x5 , #3 sub x22, sp , x5 sub sp , x22, #8*4 // The space of size + 4 is applied for mov x22, sp sub x6, x5, #32 cbnz x6, .LMul4xProcesStart ldp x14 , x15 , [x1, #8*0] ldp x16 , x17 , [x1, #8*2] // x14~x17 = a[0~3] ldp x10, x11, [x3] // x10~x13 = n[0~3] ldp x12, x13, [x3, #8*2] mov x1 , xzr mov x20, #4 // if size == 4, goto single step .LMul4xSingleStep: sub x20, x20, #0x1 ldr x24, [x2], #8 // b[i] //----- lo(a[0~3] * b[0]) ----- mul x6, x14 , x24 mul x7, x15 , x24 mul x8, x16 , x24 mul x9, x17 , x24 //----- hi(a[0~3] * b[0]) ----- adds x27, x27, x6 umulh x6, x14 , x24 adcs x28, x28, x7 umulh x7, x15 , x24 adcs x25, x25, x8 umulh x8, x16 , x24 adcs x26, x26, x9 umulh x9, x17 , x24 mul x21, x27, x4 adc x23, xzr, xzr //----- lo(n[0~3] * t[0]*k0) ----- adds x28, x28, x6 adcs x25, x25, x7 mul x7, x11, x21 adcs x26, x26, x8 mul x8, x12, x21 adc x23, x23, x9 mul x9, x13, x21 cmp x27, #1 adcs x27, x28, x7 //----- hi(n[0~3] *t[0]*k0) ----- umulh x6, x10, x21 umulh x7, x11, x21 adcs x28, x25, x8 umulh x8, x12, x21 adcs x25, x26, x9 umulh x9, x13, x21 adcs x26, x23, x1 adc x1 , xzr, xzr adds x27, x27, x6 adcs x28, x28, x7 adcs x25, x25, x8 adcs x26, x26, x9 adc x1 , x1 , xzr cbnz x20, .LMul4xSingleStep subs x14 , x27, x10 sbcs x15 , x28, x11 sbcs x16 , x25, x12 sbcs x17 , x26, x13 sbcs xzr, x1 , xzr // update CF, Determine whether to borrow csel x14 , x27, x14 , lo csel x15 , x28, x15 , lo csel x16 , x25, x16 , lo csel x17 , x26, x17 , lo stp x14 , x15 , [x0, #8*0] stp x16 , x17 , [x0, #8*2] b .LMontMul4xEnd .LMul4xProcesStart: add x6, x5, #32 eor v0.16b,v0.16b,v0.16b eor v1.16b,v1.16b,v1.16b .LMul4xInitstack: sub x6, x6, #32 st1 {v0.2d, v1.2d}, [x22], #32 cbnz x6, .LMul4xInitstack mov x22, sp add x6, x2 , x5 // x6 = &b[size] adds x19, x1 , x5 // x19 = &a[size] stp x0 , x6, [x29, #96] // r and b[size] push stack mov x0 , xzr mov x20, #0 .LMul4xLoopProces: ldr x24, [x2] // x24 = b[0] ldp x14 , x15 , [x1], #16 ldp x16 , x17 , [x1], #16 // x14~x17 = a[0~3] ldp x10 , x11 , [x3], #16 ldp x12 , x13 , [x3], #16 // x10~x13 = n[0~3] .LMul4xPrepare: //----- lo(a[0~3] * b[0]) ----- adc x0 , x0 , xzr // x0 += CF add x20, x20, #8 and x20, x20, #31 // x20 &= 0xff. The lower eight bits are used. // When x28 = 32, the instruction becomes 0. mul x6, x14 , x24 mul x7, x15 , x24 mul x8, x16 , x24 mul x9, x17 , x24 //----- hi(a[0~3] * b[0]) ----- adds x27, x27, x6 // t[0] += lo(a[0] * b[0]) adcs x28, x28, x7 // t[1] += lo(a[1] * b[0]) adcs x25, x25, x8 // t[2] += lo(a[2] * b[0]) adcs x26, x26, x9 // t[3] += lo(a[3] * b[0]) umulh x6, x14 , x24 // x6 = hi(a[0] * b[0]) umulh x7, x15 , x24 umulh x8, x16 , x24 umulh x9, x17 , x24 mul x21, x27, x4 // x21 = t[0] * k0, t[0]*k0 needs to be recalculated in each round. // t[0] is different in each round. adc x23, xzr, xzr // t[4] += CF(set by t[3] += lo(a[3] * b[0]) ) ldr x24, [x2, x20] // b[i] str x21, [x22], #8 // t[0] * k0 push stack, x22 += 8 //----- lo(n[0~3] * t[0]*k0) ----- adds x28, x28, x6 // t[1] += hi(a[0] * b[0]) adcs x25, x25, x7 // t[2] += hi(a[1] * b[0]) adcs x26, x26, x8 // t[3] += hi(a[2] * b[0]) adc x23, x23, x9 // t[4] += hi(a[3] * b[0]) mul x7, x11, x21 // x7 = lo(n[1] * t[0]*k0) mul x8, x12, x21 // x8 = lo(n[2] * t[0]*k0) mul x9, x13, x21 // x9 = lo(n[3] * t[0]*k0) cmp x27, #1 adcs x27, x28, x7 // (t[0]) = x27 = t[1] + lo(n[1] * t[0]*k0), // Perform S/R operations, r=2^64, shift right 64 bits. //----- hi(n[0~3] *t[0]*k0) ----- adcs x28, x25, x8 // x28 = t[2] + lo(n[2] * t[0]*k0) adcs x25, x26, x9 // x25 = t[3] + lo(n[3] * t[0]*k0) adcs x26, x23, x0 // x26 = t[4] + 0 + CF adc x0 , xzr, xzr // x0 = CF umulh x6, x10, x21 // x6 = hi(n[0] * t[0]*k0) umulh x7, x11, x21 // x7 = hi(n[1] * t[0]*k0) umulh x8, x12, x21 // x8 = hi(n[2] * t[0]*k0) umulh x9, x13, x21 // x9 = hi(n[3] * t[0]*k0) adds x27, x27, x6 // x27 = t[1] + hi(n[0] * t[0]*k0) adcs x28, x28, x7 // x28 = t[2] + hi(n[1] * t[0]*k0) adcs x25, x25, x8 // x25 = t[3] + hi(n[2] * t[0]*k0) adcs x26, x26, x9 // x26 = t[4] + hi(n[3] * t[0]*k0) cbnz x20, .LMul4xPrepare // Four t[0] * k0s are stacked in each loop. adc x0 , x0 , xzr ldp x6, x7, [x22, #8*4] // load A (cal before) ldp x8, x9, [x22, #8*6] adds x27, x27, x6 adcs x28, x28, x7 adcs x25, x25, x8 adcs x26, x26, x9 ldr x21, [sp] // x21 = t[0] * k0 .LMul4xReduceBegin: ldp x14 , x15 , [x1], #16 ldp x16 , x17 , [x1], #16 // x14~x17 = a[4~7] ldp x10 , x11 , [x3], #16 ldp x12 , x13 , [x3], #16 // n[4~7] .LMul4xReduceProces: adc x0 , x0 , xzr add x20, x20, #8 and x20, x20, #31 //----- lo(a[4~7] * b[i]) ----- mul x6, x14 , x24 mul x7, x15 , x24 mul x8, x16 , x24 mul x9, x17 , x24 //----- hi(a[4~7] * b[i]) ----- adds x27, x27, x6 // x27 += lo(a[4~7] * b[i]) adcs x28, x28, x7 adcs x25, x25, x8 adcs x26, x26, x9 adc x23, xzr, xzr umulh x6, x14 , x24 umulh x7, x15 , x24 umulh x8, x16 , x24 umulh x9, x17 , x24 ldr x24, [x2, x20] // b[i] //----- lo(n[4~7] * t[0]*k0) ----- adds x28, x28, x6 adcs x25, x25, x7 adcs x26, x26, x8 adc x23, x23, x9 mul x6, x10, x21 mul x7, x11, x21 mul x8, x12, x21 mul x9, x13, x21 //----- hi(n[4~7] * t[0]*k0) ----- adds x27, x27, x6 adcs x28, x28, x7 adcs x25, x25, x8 adcs x26, x26, x9 adcs x23, x23, x0 umulh x6, x10, x21 umulh x7, x11, x21 umulh x8, x12, x21 umulh x9, x13, x21 ldr x21, [sp, x20] // t[0]*k0 adc x0 , xzr, xzr // x0 = CF, record carry str x27, [x22], #8 // s[i] the calculation is complete, write the result, x22 += 8 adds x27, x28, x6 adcs x28, x25, x7 adcs x25, x26, x8 adcs x26, x23, x9 cbnz x20, .LMul4xReduceProces sub x6, x19, x1 // x6 = &a[size] - &a[i] // (The value of x1 increases cyclically.) Check whether the loop ends adc x0 , x0 , xzr cbz x6, .LMul4xLoopExitCheck ldp x6, x7, [x22, #8*4] // t[4~7] ldp x8, x9, [x22, #8*6] adds x27, x27, x6 adcs x28, x28, x7 adcs x25, x25, x8 adcs x26, x26, x9 b .LMul4xReduceBegin .LMul4xLoopExitCheck: ldr x9, [x29, #104] // b[size] Pop-Stack add x2 , x2 , #8*4 // b subscript is offset once by 4 each time, &b[4], &b[8]...... sub x9 , x9, x2 // Indicates whether the outer loop ends. adds x27, x27, x30 adcs x28, x28, xzr stp x27, x28, [x22, #8*0] // x27, x20 After the calculation is complete, Push-stack storage ldp x27, x28, [sp , #8*4] // t[0], t[1] adcs x25, x25, xzr adcs x26, x26, xzr stp x25, x26, [x22, #8*2] // x25, x22 After the calculation is complete, Push-stack storage ldp x25, x26, [sp , #8*6] // t[2], t[3] adc x30, x0 , xzr sub x3 , x3 , x5 // x1 = &n[0] cbz x9, .LMul4xEnd sub x1 , x1 , x5 // x1 = &a[0] mov x22, sp mov x0 , xzr b .LMul4xLoopProces .LMul4xEnd: ldp x10, x11, [x3], #16 ldp x12, x13, [x3], #16 ldr x0, [x29, #96] // r[0] Pop-Stack mov x19, x0 // backup, x19 = &r[0] subs x6, x27, x10 // t[0] - n[0], modify CF sbcs x7, x28, x11 // t[1] - n[1] - CF add x22, sp , #8*8 // x22 = &S[8] sub x20, x5 , #8*4 // x20 = (size - 4)*8 // t - n, x22 = &t[8], x3 = &n[0] .LMul4xSubMod: sbcs x8, x25, x12 sbcs x9, x26, x13 ldp x10, x11, [x3], #16 ldp x12, x13, [x3], #16 ldp x27, x28, [x22], #16 ldp x25, x26, [x22], #16 sub x20, x20, #8*4 stp x6, x7, [x0], 16 stp x8, x9, [x0], 16 sbcs x6, x27, x10 sbcs x7, x28, x11 cbnz x20, .LMul4xSubMod sbcs x8, x25, x12 sbcs x9, x26, x13 sbcs xzr, x30, xzr // CF = x30 - CF, x30 recorded the previous carry add x1 , sp , #8*4 // The size of the SP space is size + 4., x1 = sp + 4 stp x6, x7, [x0] stp x8, x9, [x0, #8*2] .LMul4xCopy: ldp x14 , x15 , [x19] // x14~x17 = r[0~3] ldp x16 , x17 , [x19, #8*2] ldp x27, x28, [x1], #16 // x27~22 = S[4~7] ldp x25, x26, [x1], #16 sub x5, x5, #8*4 csel x6, x27, x14, lo // x6 = (CF == 1) ? x27 : x14 csel x7, x28, x15, lo csel x8, x25, x16, lo csel x9, x26, x17, lo stp x6, x7, [x19], #16 stp x8, x9, [x19], #16 cbnz x5, .LMul4xCopy .LMontMul4xEnd: ldr x30, [x29, #8] // Value Pop-Stack in x30 (address of next instruction) ldp x27, x28, [x29, #16] ldp x25, x26, [x29, #32] ldp x23, x24, [x29, #48] ldp x21, x22, [x29, #64] ldp x19, x20, [x29, #80] mov sp , x29 ldr x29, [sp], #128 AARCH64_AUTIASP ret .size MontMul4, .-MontMul4 #endif
2302_82127028/openHiTLS-examples
crypto/bn/src/asm/bn_mont_armv8.S
Unix Assembly
unknown
51,548