forked from wolfSSL/wolfssl
Merge pull request #7296 from JacobBarthelmeh/autosar
initial AutoSAR shim layer
This commit is contained in:
15
configure.ac
15
configure.ac
@ -7932,6 +7932,19 @@ else
|
||||
fi
|
||||
|
||||
|
||||
# Support for autosar shim
|
||||
AC_ARG_ENABLE([autosar],
|
||||
[AS_HELP_STRING([--enable-autosar],[Enable AutoSAR support (default: disabled)])],
|
||||
[ ENABLED_AUTOSAR=$enableval ],
|
||||
[ ENABLED_AUTOSAR=no ]
|
||||
)
|
||||
|
||||
if test "$ENABLED_AUTOSAR" = "yes"
|
||||
then
|
||||
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_AUTOSAR"
|
||||
fi
|
||||
|
||||
|
||||
# Session Export
|
||||
AC_ARG_ENABLE([sessionexport],
|
||||
[AS_HELP_STRING([--enable-sessionexport],[Enable export and import of sessions (default: disabled)])],
|
||||
@ -9177,6 +9190,7 @@ AM_CONDITIONAL([BUILD_DTLS],[test "x$ENABLED_DTLS" = "xyes" || test "x$ENABLED_U
|
||||
AM_CONDITIONAL([BUILD_MAXQ10XX],[test "x$ENABLED_MAXQ10XX" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_ARIA],[test "x$ENABLED_ARIA" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_XILINX],[test "x$ENABLED_XILINX" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_AUTOSAR],[test "x$ENABLED_AUTOSAR" = "xyes"])
|
||||
|
||||
if test "$ENABLED_REPRODUCIBLE_BUILD" != "yes" &&
|
||||
(test "$ax_enable_debug" = "yes" ||
|
||||
@ -9668,6 +9682,7 @@ echo " * Dual alg cert support: $ENABLED_DUAL_ALG_CERTS"
|
||||
echo " * ERR Queues per Thread: $ENABLED_ERRORQUEUEPERTHREAD"
|
||||
echo " * rwlock: $ENABLED_RWLOCK"
|
||||
echo " * keylog export: $ENABLED_KEYLOG_EXPORT"
|
||||
echo " * AutoSAR : $ENABLED_AUTOSAR"
|
||||
echo ""
|
||||
echo "---"
|
||||
|
||||
|
@ -214,3 +214,6 @@ if BUILD_MAXQ10XX
|
||||
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/maxim/maxq10xx.c
|
||||
endif
|
||||
EXTRA_DIST += wolfcrypt/src/port/maxim/README.md
|
||||
|
||||
include wolfcrypt/src/port/autosar/include.am
|
||||
|
||||
|
45
wolfcrypt/src/port/autosar/README.md
Normal file
45
wolfcrypt/src/port/autosar/README.md
Normal file
@ -0,0 +1,45 @@
|
||||
## 1.0 Intro To Using wolfSSL with AutoSAR
|
||||
|
||||
This readme covers building and using wolfSSL for AutoSAR applications. Currently AES-CBC and RNG are supported. The version of AutoSAR used for reference was 4.4. Currently there is no asynchronous support.
|
||||
|
||||
|
||||
## 2.0 Building wolfSSL
|
||||
|
||||
### 2.1 wolfSSL Library
|
||||
To enable the use of AutoSAR with wolfSSL use the enable option --enable-autosar. In example “./configure --eanble-autosar”. If building without autotools then the macro WOLFSSL_AUTOSAR should be defined. This is usually defined in a user_settings.h file which gets included to the wolfSSL build when the macro WOLFSSL_USER_SETTINGS is defined.
|
||||
|
||||
|
||||
### 2.2 Key Redirection
|
||||
By default the next available key with the same key type desired is used. When specific keys are to be used then key input redirection is needed. This is done at compile time with setting specific macros. An example of key redirection would be as follows :
|
||||
|
||||
/* set redirection of primary and secondary */
|
||||
#define REDIRECTION_CONFIG 0x03
|
||||
|
||||
/* set primary key to keyId of 1 and element type CRYPTO_KE_CIPHER_KEY */
|
||||
#define REDIRECTION_IN1_KEYID 1
|
||||
#define REDIRECTION_IN1_KEYELMID 0x01
|
||||
|
||||
|
||||
/* set secondary key to keyId of 4 and element type CRYPTO_KE_CIPHER_IV */
|
||||
#define REDIRECTION_IN2_KEYID 4
|
||||
#define REDIRECTION_IN2_KEYELMID 0x05
|
||||
|
||||
|
||||
## 3.0 example
|
||||
|
||||
There is an example test case located at wolfcrypt/src/port/autsar/example.c. After compiling with autotools (./configure --enable-autsar && make) the example can be ran by running the command ./wolfcrypt/src/port/autsar/example.test
|
||||
|
||||
## 4.0 API Implemented
|
||||
|
||||
- Std_ReturnType Csm_Decrypt(uint32 jobId,
|
||||
Crypto_OperationModeType mode, const uint8* dataPtr, uint32 dataLength,
|
||||
uint8* resultPtr, uint32* resultLengthPtr);
|
||||
- Std_ReturnType Csm_Encrypt(uint32 jobId,
|
||||
Crypto_OperationModeType mode, const uint8* dataPtr, uint32 dataLength,
|
||||
uint8* resultPtr, uint32* resultLengthPtr);
|
||||
- Std_ReturnType Csm_KeyElementSet(uint32 keyId, uint32 keyElementId,
|
||||
const uint8* keyPtr, uint32 keyLength);
|
||||
- Std_ReturnType Csm_RandomGenerate( uint32 jobId, uint8* resultPtr,
|
||||
uint32* resultLengthPtr);
|
||||
|
||||
Along with the structures necessary for these API.
|
103
wolfcrypt/src/port/autosar/cryif.c
Normal file
103
wolfcrypt/src/port/autosar/cryif.c
Normal file
@ -0,0 +1,103 @@
|
||||
/* cryif.c
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
/* AutoSAR 4.4 */
|
||||
/* shim layer for use of wolfSSL crypto driver */
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/version.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Crypto.h>
|
||||
|
||||
#ifdef WOLFSSL_AUTOSAR
|
||||
#ifndef NO_WOLFSSL_AUTOSAR_CRYIF
|
||||
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
|
||||
/* initialization function */
|
||||
void CryIf_Init(const CryIf_ConfigType* in)
|
||||
{
|
||||
(void)in;
|
||||
Crypto_Init(NULL);
|
||||
}
|
||||
|
||||
|
||||
void CryIf_GetVersionInfo(Std_VersionInfoType* ver)
|
||||
{
|
||||
if (ver != NULL) {
|
||||
ver->vendorID = 0; /* no vendor or module ID */
|
||||
ver->moduleID = 0;
|
||||
ver->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
|
||||
ver->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
|
||||
ver->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success */
|
||||
Std_ReturnType CryIf_ProcessJob(uint32 id, Crypto_JobType* job)
|
||||
{
|
||||
WOLFSSL_ENTER("CryIf_ProcessJob");
|
||||
if (job == NULL) {
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
/* only handle synchronous jobs */
|
||||
if (job->jobPrimitiveInfo->processingType != CRYPTO_PROCESSING_SYNC) {
|
||||
WOLFSSL_MSG("Crypto Interface only supporting synchronous jobs");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
return Crypto_ProcessJob(id, job);
|
||||
}
|
||||
|
||||
|
||||
/* not implemented yet since async not supported */
|
||||
Std_ReturnType CryIf_CancelJob(uint32 id, Crypto_JobType* job)
|
||||
{
|
||||
(void)id;
|
||||
(void)job;
|
||||
WOLFSSL_STUB("CryIf_CancelJob");
|
||||
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
|
||||
/* return E_OK on success */
|
||||
Std_ReturnType CryIf_KeyElementSet(uint32 keyId, uint32 eId, const uint8* key,
|
||||
uint32 keySz)
|
||||
{
|
||||
if (key == NULL || keySz == 0) {
|
||||
/* report CRYIF_E_PARAM_POINTER to the DET */
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
return Crypto_KeyElementSet(keyId, eId, key, keySz);
|
||||
}
|
||||
#endif /* NO_WOLFSSL_AUTOSAR_CRYIF */
|
||||
#endif /* WOLFSSL_AUTOSAR */
|
||||
|
495
wolfcrypt/src/port/autosar/crypto.c
Normal file
495
wolfcrypt/src/port/autosar/crypto.c
Normal file
@ -0,0 +1,495 @@
|
||||
/* crypto.c
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Crypto.h>
|
||||
|
||||
#ifdef WOLFSSL_AUTOSAR
|
||||
#ifndef NO_WOLFSSL_AUTOSAR_CRYPTO
|
||||
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/aes.h>
|
||||
#include <wolfssl/wolfcrypt/random.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssl/wolfcrypt/misc.h>
|
||||
#else
|
||||
#define WOLFSSL_MISC_INCLUDED
|
||||
#include <wolfcrypt/src/misc.c>
|
||||
#endif
|
||||
|
||||
/* Low level crypto (software based driver) where wolfCrypt gets called */
|
||||
Std_ReturnType wolfSSL_Crypto_CBC(Crypto_JobType* job);
|
||||
Std_ReturnType wolfSSL_Crypto(Crypto_JobType* job);
|
||||
Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job);
|
||||
|
||||
|
||||
#ifndef MAX_KEYSTORE
|
||||
#define MAX_KEYSTORE 15
|
||||
#endif
|
||||
#ifndef MAX_JOBS
|
||||
#define MAX_JOBS 10
|
||||
#endif
|
||||
|
||||
static wolfSSL_Mutex crypto_mutex;
|
||||
struct Keys {
|
||||
uint32 keyLen;
|
||||
uint32 eId;
|
||||
|
||||
/* raw key */
|
||||
uint8 key[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE];
|
||||
} Keys;
|
||||
|
||||
|
||||
struct Jobs {
|
||||
uint32 jobId;
|
||||
uint8 inUse; /* is the key available for use */
|
||||
Aes aes;
|
||||
} Jobs;
|
||||
|
||||
static struct Jobs activeJobs[MAX_JOBS];
|
||||
static struct Keys keyStore[MAX_KEYSTORE];
|
||||
|
||||
|
||||
/* tries to get a key of type eId
|
||||
* returns 0 on success
|
||||
*/
|
||||
static int GetKey(Crypto_JobType* job, uint32 eId, uint8 **key, uint32 *keySz)
|
||||
{
|
||||
int i, ret = 0;
|
||||
|
||||
if (key == NULL || keySz == NULL || *key != NULL) {
|
||||
WOLFSSL_MSG("Bad parameter to GetKey");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (wc_LockMutex(&crypto_mutex) != 0) {
|
||||
WOLFSSL_MSG("Unable to lock crypto mutex");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef REDIRECTION_CONFIG
|
||||
/* keys should be set... */
|
||||
if (job->jobRedirectionInfoRef == NULL) {
|
||||
WOLFSSL_MSG("Issue with getting key redirection");
|
||||
wc_UnLockMutex(&crypto_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* @TODO sanity checks on setup... uint8 redirectionConfig; */
|
||||
switch (eid) {
|
||||
case job->jobRedirectionInfoRef->inputKeyElementId:
|
||||
if (job->jobRedirectionInfoRef->inputKeyId >= MAX_KEYSTORE) {
|
||||
WOLFSSL_MSG("Bogus input key ID redirection (too large)");
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
i = job->jobRedirectionInfoRef->inputKeyId;
|
||||
*key = keyStore[i].key;
|
||||
*keySz = keyStore[i].keyLen;
|
||||
}
|
||||
break;
|
||||
case job->jobRedirectionInfoRef->secondaryInputKeyElementId:
|
||||
if (job->jobRedirectionInfoRef->secondaryInputKeyId >= MAX_KEYSTORE) {
|
||||
WOLFSSL_MSG("Bogus input key ID redirection (too large)");
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
i = job->jobRedirectionInfoRef->secondaryInputKeyId;
|
||||
*key = keyStore[i].key;
|
||||
*keySz = keyStore[i].keyLen;
|
||||
}
|
||||
break;
|
||||
case job->jobRedirectionInfoRef->tertiaryInputKeyElementId:
|
||||
if (job->jobRedirectionInfoRef->tertiaryInputKeyId >= MAX_KEYSTORE) {
|
||||
WOLFSSL_MSG("Bogus input key ID redirection (too large)");
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
i = job->jobRedirectionInfoRef->tertiaryInputKeyId;
|
||||
*key = keyStore[i].key;
|
||||
*keySz = keyStore[i].keyLen;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
WOLFSSL_MSG("Unknown key element ID");
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
#else
|
||||
/* find first key of key element type */
|
||||
for (i = 0; i < MAX_KEYSTORE; i++) {
|
||||
if (keyStore[i].eId == eId && keyStore[i].keyLen ==
|
||||
job->jobPrimitiveInfo->primitiveInfo->algorithm.keyLength) {
|
||||
/* found matching key available, use it */
|
||||
*key = keyStore[i].key;
|
||||
*keySz = keyStore[i].keyLen;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (*key == NULL) {
|
||||
WOLFSSL_MSG("Unable to find an available key");
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if (wc_UnLockMutex(&crypto_mutex) != 0) {
|
||||
WOLFSSL_MSG("Unable to unlock crypto mutex");
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* returns a pointer to the Aes struct on success, NULL on failure */
|
||||
static Aes* GetAesStruct(Crypto_JobType* job)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_JOBS; i++) {
|
||||
if (activeJobs[i].inUse == 1 && activeJobs[i].jobId == job->jobId) {
|
||||
return &activeJobs[i].aes;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* returns a pointer to the Aes struct on success, NULL on failure */
|
||||
static Aes* NewAesStruct(Crypto_JobType* job)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_JOBS; i++) {
|
||||
if (activeJobs[i].inUse == 0) {
|
||||
int ret;
|
||||
|
||||
activeJobs[i].inUse = 1;
|
||||
activeJobs[i].jobId = job->jobId;
|
||||
ret = wc_AesInit(&activeJobs[i].aes, NULL, INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
WOLFSSL_MSG("Error initializing AES structure");
|
||||
return NULL;
|
||||
}
|
||||
return &activeJobs[i].aes;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* free's up the use of an AES structure */
|
||||
static void FreeAesStruct(Crypto_JobType* job) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_JOBS; i++) {
|
||||
if (activeJobs[i].inUse == 1 && activeJobs[i].jobId == job->jobId) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i >= MAX_JOBS) {
|
||||
WOLFSSL_MSG("Error finding AES structure");
|
||||
}
|
||||
else {
|
||||
wc_AesFree(&activeJobs[i].aes);
|
||||
activeJobs[i].inUse = 0;
|
||||
activeJobs[i].jobId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success */
|
||||
Std_ReturnType wolfSSL_Crypto_CBC(Crypto_JobType* job)
|
||||
{
|
||||
Std_ReturnType ret = E_OK;
|
||||
int encrypt;
|
||||
Aes* aes ;
|
||||
|
||||
encrypt = (job->jobPrimitiveInfo->primitiveInfo->service == CRYPTO_ENCRYPT)
|
||||
? AES_ENCRYPTION : AES_DECRYPTION;
|
||||
|
||||
/* check if key should be set */
|
||||
if ((job->jobPrimitiveInputOutput.mode & CRYPTO_OPERATIONMODE_START) != 0) {
|
||||
uint8 *key = NULL;
|
||||
uint8 *iv = NULL;
|
||||
uint32 keySz = 0;
|
||||
uint32 ivSz = 0;
|
||||
|
||||
if (GetKey(job, CRYPTO_KE_CIPHER_KEY, &key, &keySz) != 0) {
|
||||
WOLFSSL_MSG("Crypto error with getting a key");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (GetKey(job, CRYPTO_KE_CIPHER_IV, &iv, &ivSz) != 0) {
|
||||
WOLFSSL_MSG("Crypto error with getting an IV");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (iv != NULL && ivSz < AES_BLOCK_SIZE) {
|
||||
WOLFSSL_MSG("Error IV is too small");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
aes = NewAesStruct(job);
|
||||
if (aes == NULL) {
|
||||
WOLFSSL_MSG("Unable to get AES structure for use");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (wc_AesSetKey(aes, key, keySz, iv, encrypt) != 0) {
|
||||
WOLFSSL_MSG("Crypto error setting up AES key");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
ForceZero(key, keySz);
|
||||
}
|
||||
|
||||
if ((job->jobPrimitiveInputOutput.mode & CRYPTO_OPERATIONMODE_UPDATE)
|
||||
!= 0) {
|
||||
aes = GetAesStruct(job);
|
||||
if (aes == NULL) {
|
||||
WOLFSSL_MSG("Error finding AES structure");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (encrypt == AES_ENCRYPTION) {
|
||||
if (wc_AesCbcEncrypt(aes, job->jobPrimitiveInputOutput.outputPtr,
|
||||
job->jobPrimitiveInputOutput.inputPtr,
|
||||
job->jobPrimitiveInputOutput.inputLength) != 0) {
|
||||
WOLFSSL_MSG("AES-CBC encrypt error");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wc_AesCbcDecrypt(aes, job->jobPrimitiveInputOutput.outputPtr,
|
||||
job->jobPrimitiveInputOutput.inputPtr,
|
||||
job->jobPrimitiveInputOutput.inputLength) != 0) {
|
||||
WOLFSSL_MSG("AES-CBC decrypt error");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((job->jobPrimitiveInputOutput.mode & CRYPTO_OPERATIONMODE_FINISH)
|
||||
!= 0) {
|
||||
FreeAesStruct(job);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success and E_NOT_OK on failure */
|
||||
Std_ReturnType wolfSSL_Crypto(Crypto_JobType* job)
|
||||
{
|
||||
Std_ReturnType ret = E_OK;
|
||||
|
||||
WOLFSSL_ENTER("wolfSSL_Crypto");
|
||||
|
||||
/* switch on encryption type */
|
||||
switch (job->jobPrimitiveInfo->primitiveInfo->algorithm.mode) {
|
||||
case CRYPTO_ALGOMODE_CBC:
|
||||
ret = wolfSSL_Crypto_CBC(job);
|
||||
break;
|
||||
|
||||
case CRYPTO_ALGOMODE_NOT_SET:
|
||||
WOLFSSL_MSG("Encrypt algo mode not set!");
|
||||
ret = E_NOT_OK;
|
||||
break;
|
||||
|
||||
default:
|
||||
WOLFSSL_MSG("Unsupported encryption mode");
|
||||
ret = E_NOT_OK;
|
||||
break;
|
||||
}
|
||||
|
||||
WOLFSSL_LEAVE("wolfSSL_Crypto", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static WC_RNG rng;
|
||||
static wolfSSL_Mutex rngMutex;
|
||||
static volatile byte rngInit = 0;
|
||||
|
||||
/* returns E_OK on success */
|
||||
Std_ReturnType wolfSSL_Crypto_RNG(Crypto_JobType* job)
|
||||
{
|
||||
int ret;
|
||||
|
||||
uint8 *out = job->jobPrimitiveInputOutput.outputPtr;
|
||||
uint32 *outSz = job->jobPrimitiveInputOutput.outputLengthPtr;
|
||||
|
||||
if (outSz == NULL || out == NULL) {
|
||||
WOLFSSL_MSG("Bad parameter passed into wolfSSL_Crypto_RNG");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (rngInit == 1) {
|
||||
if (wc_LockMutex(&rngMutex) != 0) {
|
||||
WOLFSSL_MSG("Error locking RNG mutex");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
if (rngInit == 0) {
|
||||
if (wc_InitMutex(&rngMutex) != 0) {
|
||||
WOLFSSL_MSG("Error initializing RNG mutex");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (wc_LockMutex(&rngMutex) != 0) {
|
||||
WOLFSSL_MSG("Error locking RNG mutex");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
ret = wc_InitRng_ex(&rng, NULL, 0);
|
||||
if (ret != 0) {
|
||||
WOLFSSL_MSG("Error initializing RNG");
|
||||
wc_UnLockMutex(&rngMutex);
|
||||
return E_NOT_OK;
|
||||
}
|
||||
rngInit = 1;
|
||||
}
|
||||
|
||||
ret = wc_RNG_GenerateBlock(&rng, out, *outSz);
|
||||
if (ret != 0) {
|
||||
WOLFSSL_MSG("Unable to generate random values");
|
||||
ret = wc_FreeRng(&rng);
|
||||
if (ret != 0) {
|
||||
WOLFSSL_MSG("Error free'ing RNG");
|
||||
}
|
||||
rngInit = 0;
|
||||
wc_UnLockMutex(&rngMutex);
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
if (wc_UnLockMutex(&rngMutex) != 0) {
|
||||
WOLFSSL_MSG("Error unlocking RNG mutex");
|
||||
return E_NOT_OK;
|
||||
}
|
||||
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success and E_NOT_OK on failure */
|
||||
Std_ReturnType Crypto_ProcessJob(uint32 objectId, Crypto_JobType* job)
|
||||
{
|
||||
Std_ReturnType ret = E_OK;
|
||||
(void)objectId;
|
||||
|
||||
WOLFSSL_ENTER("Crypto_ProcessJob");
|
||||
if (job == NULL) {
|
||||
WOLFSSL_MSG("Bad parameter passed to Crypto_ProcessJob");
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
|
||||
/* only handle synchronous jobs */
|
||||
if (ret == E_OK &&
|
||||
job->jobPrimitiveInfo->processingType != CRYPTO_PROCESSING_SYNC) {
|
||||
WOLFSSL_MSG("Crypto only supporting synchronous jobs");
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
|
||||
if (ret == E_OK) {
|
||||
job->jobState = CRYPTO_JOBSTATE_ACTIVE;
|
||||
switch (job->jobPrimitiveInfo->primitiveInfo->service) {
|
||||
case CRYPTO_ENCRYPT:
|
||||
ret = wolfSSL_Crypto(job);
|
||||
break;
|
||||
|
||||
case CRYPTO_DECRYPT:
|
||||
ret = wolfSSL_Crypto(job);
|
||||
break;
|
||||
|
||||
case CRYPTO_RANDOMGENERATE:
|
||||
ret = wolfSSL_Crypto_RNG(job);
|
||||
break;
|
||||
|
||||
default:
|
||||
WOLFSSL_MSG("Unsuported Crypto service");
|
||||
ret = E_NOT_OK;
|
||||
break;
|
||||
}
|
||||
job->jobState = CRYPTO_JOBSTATE_IDLE;
|
||||
}
|
||||
|
||||
WOLFSSL_LEAVE("Crypto_ProcessJob", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* config currently not used, should always be null */
|
||||
void Crypto_Init(const Crypto_ConfigType* config)
|
||||
{
|
||||
if (wc_InitMutex(&crypto_mutex) != 0) {
|
||||
WOLFSSL_MSG("Issues setting up crypto mutex");
|
||||
}
|
||||
XMEMSET(&keyStore, 0, MAX_KEYSTORE * sizeof(Keys));
|
||||
XMEMSET(&activeJobs, 0, MAX_JOBS * sizeof(Jobs));
|
||||
(void)config;
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success and E_NOT_OK on failure */
|
||||
Std_ReturnType Crypto_KeyElementSet(uint32 keyId, uint32 eId, const uint8* key,
|
||||
uint32 keySz)
|
||||
{
|
||||
Std_ReturnType ret = E_OK;
|
||||
|
||||
if (key == NULL || keySz == 0 || keyId >= MAX_KEYSTORE) {
|
||||
WOLFSSL_MSG("Bad argument to Crypto_KeyElementSet");
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
|
||||
if (ret == E_OK && wc_LockMutex(&crypto_mutex) != 0) {
|
||||
WOLFSSL_MSG("Unable to lock crypto mutex");
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
|
||||
if (ret == E_OK) {
|
||||
if (keySz > sizeof(keyStore[keyId].key)) {
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
if (ret == E_OK) {
|
||||
WOLFSSL_MSG("Setting new key");
|
||||
keyStore[keyId].eId = eId;
|
||||
XMEMCPY(keyStore[keyId].key, key, keySz);
|
||||
keyStore[keyId].keyLen = keySz;
|
||||
}
|
||||
|
||||
if (wc_UnLockMutex(&crypto_mutex) != 0) {
|
||||
WOLFSSL_MSG("Unable to unlock crypto mutex");
|
||||
ret = E_NOT_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* NO_WOLFSSL_AUTOSAR_CRYPTO */
|
||||
#endif /* WOLFSSL_AUTOSAR */
|
||||
|
298
wolfcrypt/src/port/autosar/csm.c
Normal file
298
wolfcrypt/src/port/autosar/csm.c
Normal file
@ -0,0 +1,298 @@
|
||||
/* csm.c
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/version.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/CryIf.h>
|
||||
|
||||
#ifdef WOLFSSL_AUTOSAR
|
||||
#ifndef NO_WOLFSSL_AUTOSAR_CSM
|
||||
|
||||
|
||||
/* AutoSAR 4.4 */
|
||||
/* basic shim layer to plug in wolfSSL crypto */
|
||||
|
||||
#ifndef REDIRECTION_CONFIG
|
||||
Crypto_JobRedirectionInfoType redirect = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
#else
|
||||
Crypto_JobRedirectionInfoType redirect = {
|
||||
REDIRECTION_CONFIG,
|
||||
#ifdef REDIRECTION_IN1_KEYID
|
||||
REDIRECTION_IN1_KEYID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_IN1_KEYELMID
|
||||
REDIRECTION_IN1_KEYELMID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef REDIRECTION_IN2_KEYID
|
||||
REDIRECTION_IN2_KEYID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_IN2_KEYELMID
|
||||
REDIRECTION_IN2_KEYELMID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef REDIRECTION_IN3_KEYID
|
||||
REDIRECTION_IN3_KEYID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_IN3_KEYELMID
|
||||
REDIRECTION_IN3_KEYELMID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_OUT1_KEYID
|
||||
REDIRECTION_OUT1_KEYID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_OUT1_KEYELMID
|
||||
REDIRECTION_OUT1_KEYELMID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef REDIRECTION_OUT2_KEYID
|
||||
REDIRECTION_OUT2_KEYID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
|
||||
#ifdef REDIRECTION_OUT2_KEYELMID
|
||||
REDIRECTION_OUT2_KEYELMID,
|
||||
#else
|
||||
0,
|
||||
#endif
|
||||
};
|
||||
#endif
|
||||
|
||||
static byte CsmDevErrorDetect = 1; /* flag for development error detection */
|
||||
|
||||
enum {
|
||||
CSM_E_PARAM_POINTER,
|
||||
CSM_E_SMALL_BUFFER,
|
||||
CSM_E_UNINT,
|
||||
CSM_E_INIT_FAILED,
|
||||
CSM_E_PROCESSING_MODE
|
||||
};
|
||||
|
||||
|
||||
/* development error reporting */
|
||||
void ReportToDET(int err)
|
||||
{
|
||||
if (CsmDevErrorDetect == 1) {
|
||||
switch (err) {
|
||||
case CSM_E_PARAM_POINTER:
|
||||
WOLFSSL_MSG("AutoSAR CSM_E_PARAM_POINTER error");
|
||||
break;
|
||||
|
||||
case CSM_E_SMALL_BUFFER:
|
||||
WOLFSSL_MSG("AutoSAR CSM_E_SMALL_BUFFER error");
|
||||
break;
|
||||
|
||||
case CSM_E_UNINT:
|
||||
WOLFSSL_MSG("AutoSAR CSM_E_UNINT error");
|
||||
break;
|
||||
|
||||
case CSM_E_INIT_FAILED:
|
||||
WOLFSSL_MSG("AutoSAR CSM_E_INIT_FAILED error");
|
||||
break;
|
||||
|
||||
case CSM_E_PROCESSING_MODE:
|
||||
WOLFSSL_MSG("AutoSAR CSM_E_PROCESSING_MODE error");
|
||||
break;
|
||||
|
||||
default:
|
||||
WOLFSSL_MSG("AutoSAR Unknown error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Csm_Init(const Csm_ConfigType* config)
|
||||
{
|
||||
(void)config;
|
||||
CryIf_Init(NULL);
|
||||
}
|
||||
|
||||
|
||||
/* getter function for CSM version info */
|
||||
void Csm_GetVersionInfo(Std_VersionInfoType* version)
|
||||
{
|
||||
if (version != NULL) {
|
||||
version->vendorID = 0; /* no vendor or module ID */
|
||||
version->moduleID = 0;
|
||||
version->sw_major_version = (LIBWOLFSSL_VERSION_HEX >> 24) & 0xFFF;
|
||||
version->sw_minor_version = (LIBWOLFSSL_VERSION_HEX >> 12) & 0xFFF;
|
||||
version->sw_patch_version = (LIBWOLFSSL_VERSION_HEX) & 0xFFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* creates a new job type and passes it down to CryIf
|
||||
*
|
||||
* return E_OK on success
|
||||
*/
|
||||
static Std_ReturnType CreateAndRunJobType(uint32 id,
|
||||
Crypto_JobPrimitiveInfoType* jobInfo, Crypto_JobInfoType* jobInfoType,
|
||||
const uint8* data, uint32 dataSz, uint8* out, uint32* outSz,
|
||||
Crypto_OperationModeType mode)
|
||||
{
|
||||
WOLFSSL_JOBIO jobIO;
|
||||
WOLFSSL_JOBTYPE jobType;
|
||||
|
||||
XMEMSET(&jobIO, 0, sizeof(WOLFSSL_JOBIO));
|
||||
jobIO.inputPtr = data;
|
||||
jobIO.inputLength = dataSz;
|
||||
jobIO.outputPtr = out;
|
||||
jobIO.outputLengthPtr = outSz;
|
||||
jobIO.mode = mode;
|
||||
|
||||
jobType.jobId = id;
|
||||
jobType.jobState = CRYPTO_JOBSTATE_IDLE;
|
||||
jobType.jobPrimitiveInputOutput = jobIO;
|
||||
jobType.jobPrimitiveInfo = jobInfo;
|
||||
jobType.jobInfo = jobInfoType;
|
||||
jobType.jobRedirectionInfoRef = &redirect;
|
||||
|
||||
return CryIf_ProcessJob(id, &jobType);
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success */
|
||||
static Std_ReturnType Csm_CBC_Operation(uint32 id, Crypto_OperationModeType mode,
|
||||
const uint8* data, uint32 dataSz, uint8* out, uint32* outSz,
|
||||
Crypto_ServiceInfoType service)
|
||||
{
|
||||
Crypto_JobInfoType jobInfoType;
|
||||
Crypto_PrimitiveInfoType pInfo;
|
||||
Crypto_JobPrimitiveInfoType jobInfo;
|
||||
|
||||
Crypto_AlgorithmInfoType algorithm = {
|
||||
CRYPTO_ALGOFAM_AES,
|
||||
CRYPTO_ALGOFAM_NOT_SET,
|
||||
16, /* 128 bit key length */
|
||||
CRYPTO_ALGOMODE_CBC
|
||||
};
|
||||
|
||||
jobInfoType.jobId = id;
|
||||
jobInfoType.jobPriority = 0;
|
||||
|
||||
pInfo.resultLength = 16; /* 128 bit key length */
|
||||
pInfo.service = service;
|
||||
pInfo.algorithm = algorithm;
|
||||
|
||||
jobInfo.callbackId = 0;
|
||||
jobInfo.primitiveInfo = &pInfo;
|
||||
jobInfo.cryIfKeyId = 0;
|
||||
jobInfo.processingType = CRYPTO_PROCESSING_SYNC;
|
||||
jobInfo.callbackUpdateNotification = FALSE;
|
||||
|
||||
return CreateAndRunJobType(id, &jobInfo, &jobInfoType,
|
||||
data, dataSz, out, outSz, mode);
|
||||
}
|
||||
|
||||
|
||||
/* single shot encrypt
|
||||
* returns E_OK on success */
|
||||
Std_ReturnType Csm_Encrypt(uint32 id, Crypto_OperationModeType mode,
|
||||
const uint8* data, uint32 dataSz, uint8* out, uint32* outSz)
|
||||
{
|
||||
WOLFSSL_ENTER("Csm_Encrypt");
|
||||
return Csm_CBC_Operation(id, mode, data, dataSz, out, outSz,CRYPTO_ENCRYPT);
|
||||
}
|
||||
|
||||
|
||||
/* single shot decrypt
|
||||
* returns E_OK on success */
|
||||
Std_ReturnType Csm_Decrypt(uint32 id, Crypto_OperationModeType mode,
|
||||
const uint8* data, uint32 dataSz, uint8* out, uint32* outSz)
|
||||
{
|
||||
WOLFSSL_ENTER("Csm_Decrypt");
|
||||
return Csm_CBC_Operation(id, mode, data, dataSz, out, outSz,CRYPTO_DECRYPT);
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success */
|
||||
Std_ReturnType Csm_RandomGenerate(uint32 id, uint8* out, uint32* outSz)
|
||||
{
|
||||
Crypto_JobInfoType jobInfoType;
|
||||
Crypto_PrimitiveInfoType pInfo;
|
||||
Crypto_JobPrimitiveInfoType jobInfo;
|
||||
|
||||
Crypto_AlgorithmInfoType algorithm = {
|
||||
CRYPTO_ALGOFAM_DRBG,
|
||||
CRYPTO_ALGOFAM_NOT_SET,
|
||||
0, /* key length */
|
||||
CRYPTO_ALGOMODE_NOT_SET
|
||||
};
|
||||
|
||||
jobInfoType.jobId = id;
|
||||
jobInfoType.jobPriority = 0;
|
||||
|
||||
pInfo.resultLength = 0;
|
||||
pInfo.service = CRYPTO_RANDOMGENERATE;
|
||||
pInfo.algorithm = algorithm;
|
||||
|
||||
jobInfo.callbackId = 0;
|
||||
jobInfo.primitiveInfo = &pInfo;
|
||||
jobInfo.cryIfKeyId = 0;
|
||||
jobInfo.processingType = CRYPTO_PROCESSING_SYNC;
|
||||
jobInfo.callbackUpdateNotification = FALSE;
|
||||
|
||||
return CreateAndRunJobType(id, &jobInfo, &jobInfoType,
|
||||
NULL, 0, out, outSz, CRYPTO_OPERATIONMODE_SINGLECALL);
|
||||
}
|
||||
|
||||
|
||||
/* returns E_OK on success */
|
||||
Std_ReturnType Csm_KeyElementSet(uint32 keyId, uint32 eId,
|
||||
const uint8* key, uint32 keySz)
|
||||
{
|
||||
return CryIf_KeyElementSet(keyId, eId, key, keySz);
|
||||
}
|
||||
|
||||
#endif /* NO_WOLFSSL_AUTOSAR_CSM */
|
||||
#endif /* WOLFSSL_AUTOSAR */
|
||||
|
23
wolfcrypt/src/port/autosar/include.am
Normal file
23
wolfcrypt/src/port/autosar/include.am
Normal file
@ -0,0 +1,23 @@
|
||||
|
||||
EXTRA_DIST += wolfcrypt/src/port/autosar/csm.c \
|
||||
wolfcrypt/src/port/autosar/crypto.c \
|
||||
wolfcrypt/src/port/autosar/cryif.c \
|
||||
wolfcrypt/src/port/autosar/README.md \
|
||||
wolfcrypt/src/port/autosar/test.c
|
||||
|
||||
if BUILD_AUTOSAR
|
||||
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/autosar/csm.c
|
||||
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/autosar/crypto.c
|
||||
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/port/autosar/cryif.c
|
||||
|
||||
if BUILD_TESTS
|
||||
check_PROGRAMS += wolfcrypt/src/port/autosar/test.test
|
||||
noinst_PROGRAMS += wolfcrypt/src/port/autosar/test.test
|
||||
|
||||
wolfcrypt_src_port_autosar_test_test_SOURCES = \
|
||||
wolfcrypt/src/port/autosar/test.c
|
||||
wolfcrypt_src_port_autosar_test_test_LDADD = \
|
||||
src/libwolfssl.la $(LIB_STATIC_ADD)
|
||||
wolfcrypt_src_port_autosar_test_test_DEPENDENCIES = src/libwolfssl.la
|
||||
endif
|
||||
endif
|
430
wolfcrypt/src/port/autosar/test.c
Normal file
430
wolfcrypt/src/port/autosar/test.c
Normal file
@ -0,0 +1,430 @@
|
||||
/* test.c
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/Csm.h>
|
||||
#define BLOCK_SIZE 16
|
||||
|
||||
static int singleshot_test(void)
|
||||
{
|
||||
Std_ReturnType ret;
|
||||
|
||||
uint8 cipher[BLOCK_SIZE * 2];
|
||||
uint8 plain[BLOCK_SIZE * 2];
|
||||
|
||||
uint32 cipherSz = 0;
|
||||
uint32 plainSz = 0;
|
||||
const uint8 msg[] = { /* "Now is the time for all " w/o trailing 0 */
|
||||
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
|
||||
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20
|
||||
};
|
||||
const uint8 verify[] =
|
||||
{
|
||||
0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
|
||||
0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
|
||||
};
|
||||
const uint8 key[] = "0123456789abcdef ";
|
||||
const uint8 iv[] = "1234567890abcdef ";
|
||||
|
||||
XMEMSET(cipher, 0, BLOCK_SIZE);
|
||||
XMEMSET(plain, 0, BLOCK_SIZE);
|
||||
|
||||
/* set key that will be used for encryption */
|
||||
ret = Csm_KeyElementSet(0U, CRYPTO_KE_CIPHER_KEY, key, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(1U, CRYPTO_KE_CIPHER_IV, iv, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key IV");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* encrypt data using AES CBC */
|
||||
ret = Csm_Encrypt(1U, CRYPTO_OPERATIONMODE_SINGLECALL, msg, BLOCK_SIZE,
|
||||
cipher, &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (XMEMCMP(cipher, verify, BLOCK_SIZE) != 0) {
|
||||
printf("Error with cipher data\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set key that will be used for decryption */
|
||||
ret = Csm_KeyElementSet(0U, CRYPTO_KE_CIPHER_KEY, key, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(1U, CRYPTO_KE_CIPHER_IV, iv, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key IV");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* decrypt data using AES CBC */
|
||||
ret = Csm_Decrypt(2U, CRYPTO_OPERATIONMODE_SINGLECALL, cipher, BLOCK_SIZE,
|
||||
plain, &plainSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with decrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (XMEMCMP(msg, plain, BLOCK_SIZE) != 0) {
|
||||
printf("Error with plain data\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int update_test(void)
|
||||
{
|
||||
Std_ReturnType ret;
|
||||
|
||||
uint8 cipher[BLOCK_SIZE * 3];
|
||||
uint8 plain[BLOCK_SIZE * 3];
|
||||
|
||||
uint32 cipherSz = 0;
|
||||
uint32 plainSz = 0;
|
||||
const uint8 msg[] = { /* "Now is the time for all " w/o trailing 0 */
|
||||
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
|
||||
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20
|
||||
};
|
||||
const uint8 key[] = "0123456789abcdef ";
|
||||
const uint8 iv[] = "1234567890abcdef ";
|
||||
|
||||
XMEMSET(cipher, 0, BLOCK_SIZE);
|
||||
XMEMSET(plain, 0, BLOCK_SIZE);
|
||||
|
||||
/* set key that will be used for encryption */
|
||||
ret = Csm_KeyElementSet(0U, CRYPTO_KE_CIPHER_KEY, key, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(1U, CRYPTO_KE_CIPHER_IV, iv, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key IV");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* encrypt data using AES CBC */
|
||||
ret = Csm_Encrypt(1U,
|
||||
CRYPTO_OPERATIONMODE_START | CRYPTO_OPERATIONMODE_UPDATE,
|
||||
msg, BLOCK_SIZE, cipher, &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_Encrypt(1U, CRYPTO_OPERATIONMODE_UPDATE, msg, BLOCK_SIZE,
|
||||
cipher + BLOCK_SIZE, &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_Encrypt(1U,
|
||||
CRYPTO_OPERATIONMODE_UPDATE | CRYPTO_OPERATIONMODE_FINISH,
|
||||
msg, BLOCK_SIZE, cipher + (BLOCK_SIZE * 2), &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* set key that will be used for decryption */
|
||||
ret = Csm_KeyElementSet(0U, CRYPTO_KE_CIPHER_KEY, key, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(1U, CRYPTO_KE_CIPHER_IV, iv, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key IV");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* decrypt data using AES CBC */
|
||||
ret = Csm_Decrypt(2U,
|
||||
CRYPTO_OPERATIONMODE_START | CRYPTO_OPERATIONMODE_UPDATE,
|
||||
cipher, BLOCK_SIZE, plain, &plainSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with decrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_Decrypt(2U, CRYPTO_OPERATIONMODE_UPDATE, cipher + BLOCK_SIZE,
|
||||
BLOCK_SIZE, plain + BLOCK_SIZE, &plainSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with decrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_Decrypt(2U,
|
||||
CRYPTO_OPERATIONMODE_UPDATE | CRYPTO_OPERATIONMODE_FINISH,
|
||||
cipher + (BLOCK_SIZE * 2), BLOCK_SIZE,
|
||||
plain + (BLOCK_SIZE * 2), &plainSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with decrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (XMEMCMP(msg, plain, BLOCK_SIZE) != 0 ||
|
||||
XMEMCMP(msg, plain + BLOCK_SIZE, BLOCK_SIZE) != 0 ||
|
||||
XMEMCMP(msg, plain + (BLOCK_SIZE * 2), BLOCK_SIZE) != 0) {
|
||||
printf("Error with plain data\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int random_test(void)
|
||||
{
|
||||
Std_ReturnType ret;
|
||||
|
||||
int i;
|
||||
uint8 j;
|
||||
uint8 data[BLOCK_SIZE * 3];
|
||||
uint32 dataSz;
|
||||
XMEMSET(data, 0, BLOCK_SIZE * 3);
|
||||
|
||||
/* make three calls, filling up data buffer */
|
||||
for (i = 0; i < 3; i++) {
|
||||
dataSz = BLOCK_SIZE;
|
||||
ret = Csm_RandomGenerate(0U, data + (i * BLOCK_SIZE), &dataSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with getting random data block");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dataSz != BLOCK_SIZE) {
|
||||
printf("Did not get full block of random data");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* simple test that is not all 0's still after random generate */
|
||||
j = 0;
|
||||
dataSz = sizeof(data);
|
||||
for (i = 0; i < (int)dataSz; i++) {
|
||||
j |= data[i];
|
||||
}
|
||||
if (j == 0) {
|
||||
printf("call to random generate produced all 0's");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* fill full data buffer all at once */
|
||||
dataSz = sizeof(data);
|
||||
ret = Csm_RandomGenerate(0U, data, &dataSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with getting random data block");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dataSz != sizeof(data)) {
|
||||
printf("Did not get full block of random data");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#ifndef MAX_KEYSTORE
|
||||
/* default max key slots from crypto.c */
|
||||
#define MAX_KEYSTORE 15
|
||||
#endif
|
||||
static int key_test(void)
|
||||
{
|
||||
Std_ReturnType ret;
|
||||
|
||||
uint8 i;
|
||||
uint8 max = MAX_KEYSTORE;
|
||||
uint8 data[BLOCK_SIZE];
|
||||
uint32 dataSz;
|
||||
XMEMSET(data, 0, BLOCK_SIZE);
|
||||
|
||||
for (i = 0; i < max; i++) {
|
||||
dataSz = BLOCK_SIZE;
|
||||
ret = Csm_RandomGenerate(0U, data, &dataSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with getting random data block for key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dataSz != BLOCK_SIZE) {
|
||||
printf("Did not get full block of random data");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(i, CRYPTO_KE_CIPHER_KEY, data, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key id %d", i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* try creating one more key for fail case */
|
||||
ret = Csm_KeyElementSet(i, CRYPTO_KE_CIPHER_KEY, data, BLOCK_SIZE);
|
||||
if (ret == E_OK) {
|
||||
printf("Created more keys than should be possible");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef REDIRECTION_CONFIG
|
||||
static int redirect_test()
|
||||
{
|
||||
Std_ReturnType ret;
|
||||
|
||||
uint8 cipher[BLOCK_SIZE * 2];
|
||||
uint32 cipherSz = 0;
|
||||
const uint8 msg[] = { /* "Now is the time for all " w/o trailing 0 */
|
||||
0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
|
||||
0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20
|
||||
};
|
||||
const uint8 verify[] =
|
||||
{
|
||||
0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
|
||||
0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
|
||||
};
|
||||
const uint8 key[] = "0123456789abcdef "; /* align */
|
||||
const uint8 iv[] = "1234567890abcdef "; /* align */
|
||||
unsigned int i;
|
||||
|
||||
XMEMSET(cipher, 0, BLOCK_SIZE);
|
||||
|
||||
/* fill keystore with bad keys */
|
||||
for (i = 0; i < MAX_KEYSTORE; i++) {
|
||||
ret = Csm_KeyElementSet(i, CRYPTO_KE_CIPHER_KEY, verify, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* set specific key that will be used for encryption */
|
||||
ret = Csm_KeyElementSet(REDIRECTION_IN1_KEYID, REDIRECTION_IN1_KEYELMID,
|
||||
key, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = Csm_KeyElementSet(REDIRECTION_IN2_KEYID, REDIRECTION_IN2_KEYELMID,
|
||||
iv, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key IV");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* encrypt data using AES CBC */
|
||||
ret = Csm_Encrypt(0U, CRYPTO_OPERATIONMODE_SINGLECALL, msg, BLOCK_SIZE,
|
||||
cipher, &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (XMEMCMP(cipher, verify, BLOCK_SIZE) != 0) {
|
||||
printf("Error with cipher data ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* now set bad key to be used for encryption */
|
||||
ret = Csm_KeyElementSet(REDIRECTION_IN1_KEYID, REDIRECTION_IN1_KEYELMID,
|
||||
verify, BLOCK_SIZE);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with setting key ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* encrypt data using AES CBC */
|
||||
ret = Csm_Encrypt(0U, CRYPTO_OPERATIONMODE_SINGLECALL, msg, BLOCK_SIZE,
|
||||
cipher, &cipherSz);
|
||||
if (ret != E_OK) {
|
||||
printf("Issue with encrypting msg ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (XMEMCMP(cipher, verify, BLOCK_SIZE) == 0) {
|
||||
printf("Error with cipher data ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* REDIRECTION_CONFIG */
|
||||
|
||||
/* takes in test function test() and name of test
|
||||
* returns 1 if test failed and 0 if passed */
|
||||
static int run_test(int(test)(void), const char* name)
|
||||
{
|
||||
printf("%s", name);
|
||||
if (test() != 0) {
|
||||
printf("fail\n");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
printf("pass\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* AES block size */
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ret = 0;
|
||||
(void)argv;
|
||||
(void)argc;
|
||||
|
||||
wolfSSL_Debugging_ON();
|
||||
Csm_Init(NULL);
|
||||
|
||||
ret |= run_test(singleshot_test, "singleshot_test ... ");
|
||||
ret |= run_test(update_test, "update_test ... ");
|
||||
ret |= run_test(random_test, "random_test ... ");
|
||||
ret |= run_test(key_test, "key_test ... ");
|
||||
#ifdef REDIRECTION_CONFIG
|
||||
ret |= run_test(redirect_test, "redirect_test ... ");
|
||||
#endif /* REDIRECTION_CONFIG */
|
||||
return ret;
|
||||
}
|
@ -222,3 +222,11 @@ endif
|
||||
if BUILD_MAXQ10XX
|
||||
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/maxim/maxq10xx.h
|
||||
endif
|
||||
|
||||
if BUILD_AUTOSAR
|
||||
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/autosar/Csm.h
|
||||
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/autosar/CryIf.h
|
||||
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/autosar/Crypto.h
|
||||
nobase_include_HEADERS+= wolfssl/wolfcrypt/port/autosar/StandardTypes.h
|
||||
endif
|
||||
|
||||
|
49
wolfssl/wolfcrypt/port/autosar/CryIf.h
Normal file
49
wolfssl/wolfcrypt/port/autosar/CryIf.h
Normal file
@ -0,0 +1,49 @@
|
||||
/* CryIf.h
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_CRYIF_H
|
||||
#define WOLFSSL_CRYIF_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* implementation specific structure, for now not used */
|
||||
typedef struct CryIf_ConfigType {
|
||||
void* heap;
|
||||
} CryIf_ConfigType;
|
||||
|
||||
WOLFSSL_LOCAL void CryIf_Init(const CryIf_ConfigType* in);
|
||||
WOLFSSL_LOCAL void CryIf_GetVersionInfo(Std_VersionInfoType* ver);
|
||||
WOLFSSL_LOCAL Std_ReturnType CryIf_ProcessJob(uint32 id, Crypto_JobType* job);
|
||||
WOLFSSL_LOCAL Std_ReturnType CryIf_CancelJob(uint32 id, Crypto_JobType* job);
|
||||
WOLFSSL_LOCAL Std_ReturnType CryIf_KeyElementSet(uint32 keyId, uint32 eId,
|
||||
const uint8* key, uint32 keySz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* WOLFSSL_CRYIF_H */
|
||||
|
55
wolfssl/wolfcrypt/port/autosar/Crypto.h
Normal file
55
wolfssl/wolfcrypt/port/autosar/Crypto.h
Normal file
@ -0,0 +1,55 @@
|
||||
/* Crypto.h
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSL_CRYPTO_H
|
||||
#define WOLFSSL_CRYPTO_H
|
||||
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* key format */
|
||||
enum {
|
||||
CRYPTO_KE_FORMAT_BIN_OCTET = 0x01,
|
||||
CRYPTO_KE_FORMAT_BIN_RSA_PRIVATEKEY = 0x05,
|
||||
CRYPTO_KE_FORMAT_BIN_RSA_PUBLICKEY = 0x06
|
||||
};
|
||||
|
||||
/* implementation specific structure, for now not used */
|
||||
typedef struct Crypto_ConfigType {
|
||||
void* heap;
|
||||
} Crypto_ConfigType;
|
||||
|
||||
WOLFSSL_LOCAL Std_ReturnType Crypto_KeyElementSet(uint32 keyId, uint32 eId,
|
||||
const uint8* key, uint32 keySz);
|
||||
WOLFSSL_LOCAL void Crypto_Init(const Crypto_ConfigType* config);
|
||||
WOLFSSL_LOCAL Std_ReturnType Crypto_ProcessJob(uint32 objectId,
|
||||
Crypto_JobType* job);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* WOLFSSL_CRYPTO_H */
|
||||
|
295
wolfssl/wolfcrypt/port/autosar/Csm.h
Normal file
295
wolfssl/wolfcrypt/port/autosar/Csm.h
Normal file
@ -0,0 +1,295 @@
|
||||
/* csm.h
|
||||
*
|
||||
* Copyright (C) 2006-2024 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
/* specifications from AUTOSAR_SWS_CryptoServiceManager Release 4.4.0 */
|
||||
/* naming scheme from 4.4 specifications, needed for applications to use
|
||||
* standardized names when linking */
|
||||
|
||||
|
||||
#ifndef WOLFSSL_CSM_H
|
||||
#define WOLFSSL_CSM_H
|
||||
|
||||
#ifdef WOLFSSL_AUTOSAR
|
||||
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
#include <wolfssl/wolfcrypt/port/autosar/StandardTypes.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Error values */
|
||||
#define WOLFSSL_CSM_E_PARAM_POINTER 0x01
|
||||
#define WOLFSSL_CSM_E_SMALL_BUFFER 0x03
|
||||
#define WOLFSSL_CSM_E_PARAM_HANDLE 0x04
|
||||
#define WOLFSSL_CSM_E_UNINIT 0x05
|
||||
#define WOLFSSL_CSM_E_INIT_FAILED 0x07
|
||||
#define WOLFSSL_CSM_E_PROCESSING_MOD 0x08
|
||||
|
||||
|
||||
#define Crypto_JobType WOLFSSL_JOBTYPE
|
||||
#define Crypto_JobPrimitiveInputOutputType WOLFSSL_JOBIO
|
||||
#define Crypto_JobStateType WOLFSSL_JOBSTATE
|
||||
#define Crypto_VerifyResultType WOLFSSL_VERIFY
|
||||
#define Crypto_OperationModeType WOLFSSL_OMODE_TYPE
|
||||
|
||||
/* implementation specific structure, for now not used */
|
||||
typedef struct Csm_ConfigType {
|
||||
void* heap;
|
||||
} Csm_ConfigType;
|
||||
|
||||
typedef enum WOLFSSL_JOBSTATE {
|
||||
CRYPTO_JOBSTATE_IDLE = 0x00,
|
||||
CRYPTO_JOBSTATE_ACTIVE = 0x01
|
||||
} WOLFSSL_JOBSTATE;
|
||||
|
||||
typedef enum WOLFSSL_VERIFY {
|
||||
CRYPTO_E_VER_OK = 0x00,
|
||||
CRYPTO_E_VER_NOT_OK = 0x01
|
||||
} WOLFSSL_VERIFY;
|
||||
|
||||
/* operation modes <Rte_Csm_Type.h> */
|
||||
typedef enum WOLFSSL_OMODE_TYPE {
|
||||
CRYPTO_OPERATIONMODE_START = 0x01,
|
||||
CRYPTO_OPERATIONMODE_UPDATE = 0x02,
|
||||
CRYPTO_OPERATIONMODE_STREAMSTART = 0x03,
|
||||
CRYPTO_OPERATIONMODE_FINISH = 0x04,
|
||||
CRYPTO_OPERATIONMODE_SINGLECALL = 0x07
|
||||
} WOLFSSL_OMODE_TYPE;
|
||||
|
||||
|
||||
typedef enum Crypto_ServiceInfoType {
|
||||
CRYPTO_ENCRYPT = 0x03,
|
||||
CRYPTO_DECRYPT = 0x04,
|
||||
CRYPTO_RANDOMGENERATE = 0x0B,
|
||||
|
||||
#ifdef CSM_UNSUPPORTED_ALGS
|
||||
/* not yet supported */
|
||||
CRYPTO_HASH = 0x00,
|
||||
CRYPTO_MACGENERATE = 0x01,
|
||||
CRYPTO_MACVERIFY = 0x02,
|
||||
CRYPTO_AEADENCRYPT = 0x05,
|
||||
CRYPTO_AEADDECRYPT = 0x06,
|
||||
CRYPTO_SIGNATUREGENERATE = 0x07,
|
||||
CRYPTO_SIGNATUREVERIFY = 0x08,
|
||||
CRYPTO_RANDOMSEED = 0x0C,
|
||||
CRYPTO_KEYGENERATE= 0x0D,
|
||||
CRYPTO_KEYDERIVE = 0x0E,
|
||||
CRYPTO_KEYEXCHANGECALCPUBVAL = 0x0F,
|
||||
CRYPTO_KEYEXCHANGECALCSECRET = 0x10,
|
||||
CRYPTO_CERTIFICATEPARSE = 0x11,
|
||||
CRYPTO_CERTIFICATEVERIFY = 0x12,
|
||||
CRYPTO_KEYSETVALID = 0x13,
|
||||
#endif
|
||||
} Crypto_ServiceInfoType;
|
||||
|
||||
|
||||
typedef enum Crypto_AlgorithmModeType {
|
||||
CRYPTO_ALGOMODE_NOT_SET = 0x00,
|
||||
CRYPTO_ALGOMODE_CBC = 0x02,
|
||||
|
||||
#ifdef CSM_UNSUPPORTED_ALGS
|
||||
/* not yet supported */
|
||||
CRYPTO_ALGOMODE_ECB = 0x01,
|
||||
CRYPTO_ALGOMODE_CFB = 0x03,
|
||||
CRYPTO_ALGOMODE_OFB = 0x04,
|
||||
CRYPTO_ALGOMODE_CTR = 0x05,
|
||||
CRYPTO_ALGOMODE_GCM = 0x06,
|
||||
CRYPTO_ALGOMODE_XTS = 0x07,
|
||||
CRYPTO_ALGOMODE_RSAES_OAEP = 0x08,
|
||||
CRYPTO_ALGOMODE_RSAAES_PKCS1_V1_5 = 0x09,
|
||||
CRYPTO_ALGOMODE_RSAAES_PSS = 0x0A,
|
||||
CRYPTO_ALGOMODE_RSAASA_PKCS1_V1_5 = 0x0B,
|
||||
CRYPTO_ALGOMODE_8ROUNDS = 0x0C, /* ChaCha8 */
|
||||
CRYPTO_ALGOMODE_12ROUNDS = 0x0D, /* ChaCha12 */
|
||||
CRYPTO_ALGOMODE_20ROUNDS = 0x0E, /* ChaCha20 */
|
||||
CRYPTO_ALGOMODE_HMAC = 0x0F,
|
||||
CRYPTO_ALGOMODE_CMAC = 0x10,
|
||||
CRYPTO_ALGOMODE_GMAC = 0x11,
|
||||
#endif
|
||||
} Crypto_AlgorithmModeType;
|
||||
|
||||
typedef enum Crypto_AlgorithmFamilyType {
|
||||
CRYPTO_ALGOFAM_NOT_SET = 0x00,
|
||||
CRYPTO_ALGOFAM_SHA1 = 0x01,
|
||||
CRYPTO_ALGOFAM_SHA2_224 = 0x02,
|
||||
CRYPTO_ALGOFAM_SHA2_256 = 0x03,
|
||||
CRYPTO_ALGOFAM_SHA2_384 = 0x04,
|
||||
CRYPTO_ALGOFAM_SHA2_512 = 0x05,
|
||||
CRYPTO_ALGOFAM_SHA2_512_224 = 0x06,
|
||||
CRYPTO_ALGOFAM_SHA2_512_256 = 0x07,
|
||||
CRYPTO_ALGOFAM_SHA3_224 = 0x08,
|
||||
CRYPTO_ALGOFAM_SHA3_256 = 0x09,
|
||||
CRYPTO_ALGOFAM_SHA3_384 = 0x0A,
|
||||
CRYPTO_ALGOFAM_SHA3_512 = 0x0B,
|
||||
CRYPTO_ALGOFAM_SHAKE128 = 0x0C,
|
||||
CRYPTO_ALGOFAM_SHAKE256 = 0x0D,
|
||||
CRYPTO_ALGOFAM_RIPEMD160 = 0x0E,
|
||||
CRYPTO_ALGOFAM_BLAKE_1_256 = 0x0D,
|
||||
CRYPTO_ALGOFAM_BLAKE_1_512 = 0x10,
|
||||
CRYPTO_ALGOFAM_BLAKE_2s_256 = 0x11,
|
||||
CRYPTO_ALGOFAM_BLAKE_2s_512 = 0x12,
|
||||
CRYPTO_ALGOFAM_3DES = 0x13,
|
||||
CRYPTO_ALGOFAM_AES = 0x14,
|
||||
CRYPTO_ALGOFAM_CHACHA = 0x15,
|
||||
CRYPTO_ALGOFAM_RSA = 0x16,
|
||||
CRYPTO_ALGOFAM_ED25519 = 0x17,
|
||||
CRYPTO_ALGOFAM_BRAINPOOL = 0x18,
|
||||
CRYPTO_ALGOFAM_ECCNIST = 0x19,
|
||||
CRYPTO_ALGOFAM_RNG = 0x1B,
|
||||
CRYPTO_ALGOFAM_SIPHASH = 0x1C,
|
||||
CRYPTO_ALGOFAM_ECIES = 0x1D,
|
||||
CRYPTO_ALGOFAM_ECCANSI = 0x1E,
|
||||
CRYPTO_ALGOFAM_ECCSEC = 0x1F,
|
||||
CRYPTO_ALGOFAM_DRBG = 0x20,
|
||||
CRYPTO_ALGOFAM_FIPS186 = 0x21, /* random number gen according to FIPS 186 */
|
||||
CRYPTO_ALGOFAM_PADDING_PKCS7 = 0x22,
|
||||
CRYPTO_ALGOFAM_PADDING_ONEWITHZEROS = 0x23 /* fill with 0's but first bit
|
||||
* after data is 1 */
|
||||
} Crypto_AlgorithmFamilyType;
|
||||
|
||||
typedef enum Crypto_KeyID {
|
||||
/* Cipher/AEAD */
|
||||
CRYPTO_KE_CIPHER_KEY = 0x01,
|
||||
CRYPTO_KE_CIPHER_IV = 0x05,
|
||||
CRYPTO_KE_CIPHER_PROOF = 0x06,
|
||||
CRYPTO_KE_CIPHER_2NDKEY = 0x07
|
||||
} Crypto_KeyID;
|
||||
|
||||
|
||||
typedef enum Crypto_ProcessingType {
|
||||
CRYPTO_PROCESSING_ASYNC = 0x00,
|
||||
CRYPTO_PROCESSING_SYNC = 0x01
|
||||
} Crypto_ProcessingType;
|
||||
|
||||
|
||||
/* removed const on elements @TODO which is different than 8.2.8 in
|
||||
* AUTOSAR_SWS_CryptoServiceManager.pdf */
|
||||
typedef struct Crypto_JobInfoType {
|
||||
uint32 jobId;
|
||||
uint32 jobPriority;
|
||||
} Crypto_JobInfoType;
|
||||
|
||||
typedef struct Crypto_JobRedirectionInfoType {
|
||||
uint8 redirectionConfig;
|
||||
uint32 inputKeyId;
|
||||
uint32 inputKeyElementId;
|
||||
uint32 secondaryInputKeyId;
|
||||
uint32 secondaryInputKeyElementId;
|
||||
uint32 tertiaryInputKeyId;
|
||||
uint32 tertiaryInputKeyElementId;
|
||||
uint32 outputKeyId;
|
||||
uint32 outputKeyElementId;
|
||||
uint32 secondaryOutputKeyId;
|
||||
uint32 secondaryOutputKeyElementId;
|
||||
} Crypto_JobRedirectionInfoType;
|
||||
|
||||
|
||||
enum Crypto_InputOutputRedirectionConfigType {
|
||||
CRYPTO_REDIRECT_CONFIG_PRIMARY_INPUT = 0x01,
|
||||
CRYPTO_REDIRECT_CONFIG_SECONDARY_INPUT = 0x02,
|
||||
CRYPTO_REDIRECT_CONFIG_TERTIARY_INPUT = 0x04,
|
||||
CRYPTO_REDIRECT_CONFIG_PRIMARY_OUTPUT = 0x10,
|
||||
CRYPTO_REDIRECT_CONFIG_SECONDARY_OUTPUT = 0x20
|
||||
};
|
||||
|
||||
|
||||
typedef struct WOLFSSL_JOBIO {
|
||||
const uint8 *inputPtr;
|
||||
uint32 inputLength;
|
||||
const uint8 *secondaryInputPtr; /* secondary data for verify */
|
||||
uint32 secondaryInputLength;
|
||||
const uint8 *tertiaryInputPtr; /* third input data for verify */
|
||||
uint32 tertiaryInputLength;
|
||||
uint8 *outputPtr;
|
||||
uint32 *outputLengthPtr;
|
||||
uint8 *secondaryOutputPtr;
|
||||
uint32 *secondaryOutputLengthPtr;
|
||||
uint64 input64; /* input parameter */
|
||||
Crypto_VerifyResultType *verifyPtr;
|
||||
uint64 *output64Ptr;
|
||||
Crypto_OperationModeType mode;
|
||||
uint32 cryIfKeyId;
|
||||
uint32 targetCryIfKeyId;
|
||||
} WOLFSSL_JOBIO;
|
||||
|
||||
|
||||
typedef struct Crypto_AlgorithmInfoType {
|
||||
Crypto_AlgorithmFamilyType family;
|
||||
Crypto_AlgorithmFamilyType secondaryFamily; /* second algo type if needed */
|
||||
uint32 keyLength;
|
||||
Crypto_AlgorithmModeType mode; /* i.e. CBC / RSA OAEP */
|
||||
} Crypto_AlgorithmInfoType;
|
||||
|
||||
|
||||
/* removed const on all 3 elements which is slightly different than AutoSAR */
|
||||
typedef struct Crypto_PrimitiveInfoType {
|
||||
uint32 resultLength;
|
||||
Crypto_ServiceInfoType service;
|
||||
Crypto_AlgorithmInfoType algorithm;
|
||||
} Crypto_PrimitiveInfoType;
|
||||
|
||||
|
||||
typedef struct Crypto_JobPrimitiveInfoType {
|
||||
uint32 callbackId;
|
||||
const Crypto_PrimitiveInfoType *primitiveInfo;
|
||||
uint32 cryIfKeyId;
|
||||
Crypto_ProcessingType processingType;
|
||||
boolean callbackUpdateNotification;
|
||||
} Crypto_JobPrimitiveInfoType;
|
||||
|
||||
|
||||
typedef struct WOLFSSL_JOBTYPE {
|
||||
uint32 jobId;
|
||||
WOLFSSL_JOBSTATE jobState;
|
||||
WOLFSSL_JOBIO jobPrimitiveInputOutput;
|
||||
const Crypto_JobPrimitiveInfoType* jobPrimitiveInfo;
|
||||
const Crypto_JobInfoType* jobInfo;
|
||||
Crypto_JobRedirectionInfoType* jobRedirectionInfoRef;
|
||||
} WOLFSSL_JOBTYPE;
|
||||
|
||||
|
||||
WOLFSSL_API void Csm_Init(const Csm_ConfigType* config);
|
||||
|
||||
/* can be called before init, all else return WOLFSSL_CSM_E_UNINIT */
|
||||
WOLFSSL_API void Csm_GetVersionInfo(Std_VersionInfoType* version);
|
||||
|
||||
WOLFSSL_API Std_ReturnType Csm_Decrypt(uint32 jobId,
|
||||
Crypto_OperationModeType mode, const uint8* dataPtr, uint32 dataLength,
|
||||
uint8* resultPtr, uint32* resultLengthPtr);
|
||||
WOLFSSL_API Std_ReturnType Csm_Encrypt(uint32 jobId,
|
||||
Crypto_OperationModeType mode, const uint8* dataPtr, uint32 dataLength,
|
||||
uint8* resultPtr, uint32* resultLengthPtr);
|
||||
WOLFSSL_API Std_ReturnType Csm_KeyElementSet(uint32 keyId, uint32 keyElementId,
|
||||
const uint8* keyPtr, uint32 keyLength);
|
||||
WOLFSSL_API Std_ReturnType Csm_RandomGenerate( uint32 jobId, uint8* resultPtr,
|
||||
uint32* resultLengthPtr);
|
||||
WOLFSSL_LOCAL void ReportToDET(int err);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* WOLFSSL_AUTOSAR */
|
||||
#endif /* WOLFSSL_CSM_H */
|
||||
|
66
wolfssl/wolfcrypt/port/autosar/StandardTypes.h
Normal file
66
wolfssl/wolfcrypt/port/autosar/StandardTypes.h
Normal file
@ -0,0 +1,66 @@
|
||||
/* StandardTypes.h
|
||||
*
|
||||
* Copyright (C) 2006-2019 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
#ifdef WOLFSSL_AUTOSAR
|
||||
|
||||
#ifndef WOLFSSL_STANDARDTYPES_H
|
||||
#define WOLFSSL_STANDARDTYPES_H
|
||||
#include <wolfssl/wolfcrypt/types.h>
|
||||
|
||||
/* remap primitives */
|
||||
typedef byte uint8;
|
||||
typedef byte boolean;
|
||||
typedef word16 uint16;
|
||||
typedef word32 uint32;
|
||||
typedef word64 uint64;
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
/* return types */
|
||||
typedef enum Std_ReturnType {
|
||||
E_OK = 0x00,
|
||||
E_NOT_OK = 0x01,
|
||||
E_SMALL_BUFFER = 0x02,
|
||||
E_ENTROPY_EXHAUSTION = 0x03,
|
||||
E_KEY_READ_FAIL = 0x04,
|
||||
E_KEY_NOT_AVAILABLE = 0x05,
|
||||
E_KEY_NOT_VALID = 0x06,
|
||||
E_JOB_CANCELED = 0x07,
|
||||
E_KEY_EMPTY = 0x08
|
||||
} Std_ReturnType;
|
||||
|
||||
|
||||
typedef struct Std_VersionInfoType {
|
||||
uint16 vendorID;
|
||||
uint16 moduleID;
|
||||
uint8 sw_major_version;
|
||||
uint8 sw_minor_version;
|
||||
uint8 sw_patch_version;
|
||||
} Std_VersionInfoType;
|
||||
#endif /* WOLFSSL_AUTOSAR */
|
||||
|
||||
#endif /* WOLFSSL_STANDARDTYPES_H */
|
||||
|
Reference in New Issue
Block a user