Skip to content

Commit

Permalink
Merge pull request #7057 from theamirocohen/trng_test
Browse files Browse the repository at this point in the history
Add TRNG API test
  • Loading branch information
0xc0170 committed Sep 26, 2018
2 parents 4957dd5 + e92f41a commit a0a9b54
Show file tree
Hide file tree
Showing 6 changed files with 1,479 additions and 0 deletions.
145 changes: 145 additions & 0 deletions TESTS/host_tests/trng_reset.py
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
197 changes: 197 additions & 0 deletions TESTS/mbed_hal/trng/base64b/base64b.cpp
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);
}
32 changes: 32 additions & 0 deletions TESTS/mbed_hal/trng/base64b/base64b.h
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);


Loading

0 comments on commit a0a9b54

Please sign in to comment.