-
Notifications
You must be signed in to change notification settings - Fork 3k
Add TRNG API test #7057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add TRNG API test #7057
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
669b618
Add TRNG API test
theamirocohen 4d1d1a0
Fix lisence and style
theamirocohen 6dadbd8
Repalce compression libs for license issues
theamirocohen abe2b00
Fix var declaration problem
theamirocohen 1466518
Fixing PR comments
theamirocohen 1f4405c
Reduce Debug prints
theamirocohen 225a3bf
Erase debug prints
theamirocohen 5256e96
Fix IAR issues
theamirocohen 5f7a640
Fix Compilation problems
theamirocohen 11f1aaf
Fix pithy library
theamirocohen 9b0b631
Fix messages
theamirocohen b2b14ca
Fix TRNG test to use reset from python script and not from code
780d9c6
Remove NVstore unused mechanism
theamirocohen 6d8dab8
Fix trng_len loop
theamirocohen 79e5111
Adding complete buffer print in case of failure.
e92f41a
Fix print error
theamirocohen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
""" | ||
Copyright (c) 2018 ARM Limited | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
""" | ||
This script is the host script for trng test sequence, it send the | ||
step signaling sequence and receive and transmit data to the device after | ||
reset if necesarry (default loading and storing mechanism while reseting the device | ||
is NVstore, in case NVstore isn't enabled we'll use current infrastructure, | ||
for more details see main.cpp file) | ||
""" | ||
|
||
import time | ||
from mbed_host_tests import BaseHostTest | ||
from mbed_host_tests.host_tests_runner.host_test_default import DefaultTestSelector | ||
from time import sleep | ||
|
||
DEFAULT_CYCLE_PERIOD = 1.0 | ||
MSG_VALUE_DUMMY = '0' | ||
MSG_TRNG_READY = 'ready' | ||
MSG_TRNG_BUFFER = 'buffer' | ||
MSG_TRNG_TEST_STEP1 = 'check_step1' | ||
MSG_TRNG_TEST_STEP2 = 'check_step2' | ||
MSG_KEY_SYNC = '__sync' | ||
MSG_KEY_RESET_COMPLETE = 'reset_complete' | ||
MSG_KEY_TEST_SUITE_ENDED = 'Test_suite_ended' | ||
MSG_KEY_EXIT = 'exit' | ||
|
||
class TRNGResetTest(BaseHostTest): | ||
"""Test for the TRNG API. | ||
""" | ||
|
||
def __init__(self): | ||
super(TRNGResetTest, self).__init__() | ||
self.did_reset = False | ||
self.ready = False | ||
self.suite_ended = False | ||
self.buffer = 0 | ||
self.test_steps_sequence = self.test_steps() | ||
# Advance the coroutine to it's first yield statement. | ||
self.test_steps_sequence.send(None) | ||
|
||
#define callback functions for msg handling | ||
def setup(self): | ||
self.register_callback(MSG_TRNG_READY, self.cb_device_ready) | ||
self.register_callback(MSG_TRNG_BUFFER, self.cb_trng_buffer) | ||
self.register_callback(MSG_KEY_TEST_SUITE_ENDED, self.cb_device_test_suit_ended) | ||
self.register_callback(MSG_KEY_RESET_COMPLETE, self.cb_reset_complete) | ||
|
||
#receive sent data from device before reset | ||
def cb_trng_buffer(self, key, value, timestamp): | ||
"""Acknowledge device rebooted correctly and feed the test execution | ||
""" | ||
self.buffer = value | ||
|
||
try: | ||
if self.test_steps_sequence.send(value): | ||
self.notify_complete(True) | ||
except (StopIteration, RuntimeError) as exc: | ||
self.notify_complete(False) | ||
|
||
def cb_device_ready(self, key, value, timestamp): | ||
"""Acknowledge device rebooted correctly and feed the test execution | ||
""" | ||
self.ready = True | ||
|
||
try: | ||
if self.test_steps_sequence.send(value): | ||
self.notify_complete(True) | ||
except (StopIteration, RuntimeError) as exc: | ||
self.notify_complete(False) | ||
|
||
def cb_reset_complete(self, key, value, timestamp): | ||
"""Acknowledge reset complete | ||
""" | ||
self.did_reset = True | ||
|
||
try: | ||
if self.test_steps_sequence.send(value): | ||
self.notify_complete(True) | ||
except (StopIteration, RuntimeError) as exc: | ||
self.notify_complete(False) | ||
|
||
def cb_device_test_suit_ended(self, key, value, timestamp): | ||
"""Acknowledge device finished a test step correctly and feed the test execution | ||
""" | ||
self.suite_ended = True | ||
|
||
try: | ||
if self.test_steps_sequence.send(value): | ||
self.notify_complete(True) | ||
except (StopIteration, RuntimeError) as exc: | ||
self.notify_complete(False) | ||
|
||
#define test steps and actions | ||
def test_steps(self): | ||
"""Test step 1 | ||
""" | ||
wait_for_communication = yield | ||
|
||
self.ready = False | ||
self.did_reset = False | ||
self.suite_ended = False | ||
self.send_kv(MSG_TRNG_TEST_STEP1, MSG_VALUE_DUMMY) | ||
wait_for_communication = yield | ||
if self.buffer == 0: | ||
raise RuntimeError('Phase 1: No buffer received.') | ||
|
||
self.reset() | ||
|
||
"""Test step 2 (After reset) | ||
""" | ||
wait_for_communication = yield | ||
if self.did_reset == False: | ||
raise RuntimeError('Phase 1: Platform did not reset as expected.') | ||
|
||
self.send_kv(MSG_KEY_SYNC, MSG_VALUE_DUMMY) | ||
|
||
wait_for_communication = yield | ||
|
||
if self.ready == False: | ||
raise RuntimeError('Phase 1: Platform not ready as expected.') | ||
|
||
self.send_kv(MSG_TRNG_TEST_STEP2, self.buffer) | ||
|
||
wait_for_communication = yield | ||
|
||
if self.suite_ended == False: | ||
raise RuntimeError('Test failed.') | ||
|
||
self.send_kv(MSG_KEY_EXIT, MSG_VALUE_DUMMY) | ||
|
||
# The sequence is correct -- test passed. | ||
yield |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
* Copyright (c) 2018 ARM Limited. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Licensed under the Apache License, Version 2.0 (the License); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "base64b.h" | ||
|
||
using namespace std; | ||
|
||
static char IntToBase64Char(uint8_t intVal) | ||
{ | ||
const char *base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
return base64Digits[intVal & 0x3F]; | ||
} | ||
|
||
#define BASE_64_PAD 0xFF | ||
static base64_result_e Base64CharToInt(char base64, uint8_t *intVal) | ||
{ | ||
if (NULL == intVal) { | ||
return BASE64_INVALID_PARAMETER; | ||
} | ||
|
||
if ((base64 >= 'A') && (base64 <= 'Z')) { | ||
*intVal = base64 - 'A' ; | ||
} else if ((base64 >= 'a') && (base64 <= 'z')) { | ||
*intVal = base64 - 'a' + 26; | ||
} else if ((base64 >= '0') && (base64 <= '9')) { | ||
*intVal = base64 - '0' + 52; | ||
} else if (base64 == '+') { | ||
*intVal = 62; | ||
} else if (base64 == '/') { | ||
*intVal = 63; | ||
} else if (base64 == '=') { | ||
*intVal = BASE_64_PAD; | ||
} else { | ||
return BASE64_ERROR; | ||
} | ||
|
||
return BASE64_SUCCESS; | ||
} | ||
|
||
base64_result_e trng_DecodeNBase64(const char *string, | ||
uint32_t stringMaxSize, | ||
void *buffer, | ||
uint32_t bufferSize, | ||
uint32_t *lengthWritten, | ||
uint32_t *charsProcessed) | ||
{ | ||
base64_result_e result = BASE64_SUCCESS; | ||
uint32_t bitOffset = 0; | ||
uint8_t *writePtr = (uint8_t *)buffer; | ||
uint8_t *bufferEnd = (uint8_t *)buffer + bufferSize; | ||
uint8_t tempVal = 0; | ||
uint32_t currPos = 0; | ||
uint32_t localBytesWritten = 0; | ||
uint32_t localCharsProcessed = 0; | ||
bool isEndOfString = false; | ||
|
||
if ((NULL == string) || (NULL == buffer) || (bufferSize == 0)) { | ||
return BASE64_INVALID_PARAMETER; | ||
} | ||
|
||
*writePtr = 0; | ||
while (( currPos < stringMaxSize ) && | ||
( string[currPos] != 0 ) && | ||
( writePtr < bufferEnd ) && | ||
( !isEndOfString )) { | ||
uint8_t val; | ||
|
||
if (string[currPos] == 0) { | ||
break; | ||
} | ||
|
||
result = Base64CharToInt(string[currPos++], &val); | ||
if (result != BASE64_SUCCESS) { | ||
break; | ||
} | ||
|
||
if (val != BASE_64_PAD) { | ||
if (bitOffset <= 2) { | ||
tempVal |= val << (2 - bitOffset); | ||
if (bitOffset == 2) { | ||
*writePtr++ = tempVal; | ||
tempVal = 0; | ||
} | ||
} else { | ||
*writePtr++ = (uint8_t)(tempVal | (val >> (bitOffset - 2))); | ||
tempVal = (uint8_t)(val << (10 - bitOffset)); | ||
} | ||
} else { // found BASE_64_PAD | ||
// At most two pad characters may occur at the end of the encoded stream | ||
if (bitOffset == 2) { | ||
isEndOfString = true; // The last padding byte has been processed. | ||
} else if (bitOffset != 4) { | ||
return BASE64_ERROR; // Incorrect padding | ||
} | ||
} | ||
|
||
bitOffset = (bitOffset + 6) & 0x7; | ||
if (bitOffset == 0) { | ||
localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); | ||
localCharsProcessed = currPos; | ||
} | ||
} | ||
if (charsProcessed == NULL) { | ||
localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); | ||
} else { | ||
*charsProcessed = localCharsProcessed; | ||
} | ||
if (lengthWritten != NULL) { | ||
*lengthWritten = localBytesWritten; | ||
} else if (bufferSize != localBytesWritten) { | ||
return BASE64_BUFFER_TOO_SMALL; | ||
} | ||
|
||
// Check if additional bytes should have been processed but buffer isn't sufficient. | ||
if (( result == BASE64_SUCCESS ) && | ||
( !isEndOfString ) && | ||
( currPos < stringMaxSize ) && | ||
( string[currPos] != 0 ) && | ||
( string[currPos] != '=' ) ) { | ||
return BASE64_BUFFER_TOO_SMALL; | ||
} | ||
|
||
if (result != BASE64_SUCCESS) { | ||
return result; | ||
} | ||
|
||
return BASE64_SUCCESS; | ||
} | ||
|
||
base64_result_e trng_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize) | ||
{ | ||
uint32_t bitOffset = 0; | ||
|
||
const uint8_t *readPtr = (const uint8_t *)buffer; | ||
const uint8_t *bufferEnd = (const uint8_t *)buffer + bufferSize; | ||
|
||
char *writePtr = string; | ||
char *stringEnd = string + stringSize - 1; | ||
|
||
if ((NULL == string) || (NULL == buffer) || (stringSize == 0)) { | ||
return BASE64_INVALID_PARAMETER; | ||
} | ||
|
||
stringSize--; | ||
while (readPtr < bufferEnd && writePtr < stringEnd) { | ||
uint8_t tempVal = 0; | ||
switch (bitOffset) { | ||
case 0: | ||
*writePtr++ = IntToBase64Char(*readPtr >> 2); // take upper 6 bits | ||
break; | ||
case 6: | ||
tempVal = *readPtr++ << 4; | ||
if (readPtr < bufferEnd) { | ||
tempVal |= *readPtr >> 4; | ||
} | ||
*writePtr++ = IntToBase64Char(tempVal); | ||
break; | ||
case 4: | ||
tempVal = *readPtr++ << 2; | ||
if (readPtr < bufferEnd) { | ||
tempVal |= *readPtr >> 6; | ||
} | ||
*writePtr++ = IntToBase64Char(tempVal); | ||
break; | ||
case 2: | ||
*writePtr++ = IntToBase64Char(*readPtr++); | ||
break; | ||
default: | ||
return BASE64_ERROR; // we should never reach this code. | ||
} | ||
bitOffset = (bitOffset + 6) & 0x7; | ||
} | ||
while (bitOffset > 0 && writePtr < stringEnd) { | ||
*writePtr++ = '='; | ||
bitOffset = (bitOffset + 6) & 0x7; | ||
} | ||
*writePtr = 0; | ||
|
||
if ((readPtr < bufferEnd) || (bitOffset != 0)) { | ||
return (BASE64_BUFFER_TOO_SMALL); | ||
} | ||
|
||
return (BASE64_SUCCESS); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2018 ARM Limited. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Licensed under the Apache License, Version 2.0 (the License); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an AS IS BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <string> | ||
|
||
typedef enum { | ||
BASE64_SUCCESS = 0, | ||
BASE64_INVALID_PARAMETER = 1, | ||
BASE64_BUFFER_TOO_SMALL = 2, | ||
BASE64_ERROR = 3, | ||
} base64_result_e; | ||
|
||
base64_result_e trng_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize); | ||
base64_result_e trng_DecodeNBase64(const char *string, uint32_t stringMaxSize, void *buffer, uint32_t bufferSize, | ||
uint32_t *lengthWritten, uint32_t *charsProcessed); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
license header missing in some files (please review), not only this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for that, all license header will be added.