예제 #1
0
//generate a coap token
void OCGenerateCoAPToken(OCCoAPToken * token)
{
    if (token)
    {
        token->tokenLength = MAX_TOKEN_LENGTH;
        OCFillRandomMem((uint8_t*)token->token, token->tokenLength);
    }
}
예제 #2
0
/**
 * Checks if DeviceID is generated during provisioning for the new device.
 * If DeviceID is NULL then generates the new DeviceID.
 * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
 *
 */
void CheckDeviceID()
{
    if(strcmp((char *)gDoxm->deviceID.id, "") == 0 )
    {
        OCFillRandomMem(gDoxm->deviceID.id, sizeof(gDoxm->deviceID.id));
        UpdatePersistentStorage(gDoxm);
    }
}
예제 #3
0
void setup() {
    Serial.begin(115200);
    Serial.println("Testing Random Number generator for Arduino");

    Serial.print("Testing OCSeedRandom ... ");
    if (OCSeedRandom() == 0) {
        Serial.println("[Success]");
    } else {
        Serial.println("[Fail]");
    }

    Serial.print("Testing OCGetRandomByte ... ");
    uint8_t value8 = OCGetRandomByte();
    if (value8 >= 0 && value8 < pow(2, 8)) {
        Serial.println("[Success]");
    } else {
        Serial.println("[Fail]");
    }

    Serial.print("Testing OCGetRandom ... ");
    uint32_t value32 = OCGetRandom();
    if (value32 >= 0 && value32 < pow(2, 32)) {
        Serial.println("[Success]");
    } else {
        Serial.println("[Fail]");
    }

    Serial.print("Testing OCFillRandomMem ... ");
    uint8_t array[ARR_SIZE] = {};
    OCFillRandomMem(array + 1, ARR_SIZE - 2);
    uint8_t overall = 0;
    uint8_t value82 = 0;
    for (int i = 1; i <= ARR_SIZE - 2; i++) {
        value82 = array[i];
        if (value82 >= 0 && value82 < pow(2, 8)) {
            overall++;
        }
    }
    if (overall == ARR_SIZE - 2 && array[0] == 0 && array[ARR_SIZE - 1] == 0) {
        Serial.println("[Success]");
    } else {
        Serial.println("[Fail]");
        Serial.print("overall:");Serial.println(overall);
        Serial.print("array[0]:");Serial.println(array[0]);
        Serial.print("array[ARR_SIZE - 1]:");Serial.println(array[ARR_SIZE - 1]);
    }
    Serial.println("========DONE TESTING=========");

}
예제 #4
0
OCRandomUuidResult OCGenerateUuid(uint8_t uuid[UUID_SIZE])
{
    if (!uuid)
    {
        return RAND_UUID_INVALID_PARAM;
    }
#if defined(__ANDROID__)
    char uuidString[UUID_STRING_SIZE];
    int8_t ret = OCGenerateUuidString(uuidString);

    if (ret < 0)
    {
        return ret;
    }

    uuid[ 0] = parseUuidPart(&uuidString[0]);
    uuid[ 1] = parseUuidPart(&uuidString[2]);
    uuid[ 2] = parseUuidPart(&uuidString[4]);
    uuid[ 3] = parseUuidPart(&uuidString[6]);

    uuid[ 4] = parseUuidPart(&uuidString[9]);
    uuid[ 5] = parseUuidPart(&uuidString[11]);

    uuid[ 6] = parseUuidPart(&uuidString[14]);
    uuid[ 7] = parseUuidPart(&uuidString[16]);

    uuid[ 8] = parseUuidPart(&uuidString[19]);
    uuid[ 9] = parseUuidPart(&uuidString[21]);

    uuid[10] = parseUuidPart(&uuidString[24]);
    uuid[11] = parseUuidPart(&uuidString[26]);
    uuid[12] = parseUuidPart(&uuidString[28]);
    uuid[13] = parseUuidPart(&uuidString[30]);
    uuid[14] = parseUuidPart(&uuidString[32]);
    uuid[15] = parseUuidPart(&uuidString[34]);

    return RAND_UUID_OK;
#elif defined(HAVE_UUID_UUID_H)
    // note: uuid_t is typedefed as unsigned char[16] on linux/apple
    uuid_generate(uuid);
    return RAND_UUID_OK;
#else
    // Fallback for all platforms is filling the array with random data
    OCFillRandomMem(uuid, UUID_SIZE);
    return RAND_UUID_OK;
#endif
}
예제 #5
0
uint32_t OCGetRandom()
{
    uint32_t result = 0;
    OCFillRandomMem((uint8_t*) &result, 4);
    return result;
}
예제 #6
0
void RMGenerateGatewayID(uint8_t *id, size_t idLen)
{
    OIC_LOG(DEBUG, TAG, "RMGenerateGatewayID IN");
    OCFillRandomMem(id, idLen);
    OIC_LOG(DEBUG, TAG, "RMGenerateGatewayID OUT");
}
예제 #7
0
int32_t GetDtlsPskForRandomPinOxm( CADtlsPskCredType_t type,
              const unsigned char *UNUSED1, size_t UNUSED2,
              unsigned char *result, size_t result_length)
{
    int32_t ret = -1;

    (void)UNUSED1;
    (void)UNUSED2;

    if (NULL == result || result_length < OWNER_PSK_LENGTH_128)
    {
        return ret;
    }

    switch (type)
    {
        case CA_DTLS_PSK_HINT:
        case CA_DTLS_PSK_IDENTITY:
            /**
             * The server will provide PSK hint to identify PSK according to RFC 4589 and RFC 4279.
             *
             * At this point, The server generate random hint and
             * provide it to client through server key exchange message.
             */
            OCFillRandomMem(result, result_length);
            ret = result_length;

            OIC_LOG(DEBUG, TAG, "PSK HINT : ");
            OIC_LOG_BUFFER(DEBUG, TAG, result, result_length);
            break;

        case CA_DTLS_PSK_KEY:
            {
                int dtlsRes = DeriveCryptoKeyFromPassword(
                                                          (const unsigned char *)g_PinOxmData.pinData,
                                                          OXM_RANDOM_PIN_SIZE,
                                                          g_PinOxmData.newDevice.id,
                                                          UUID_LENGTH, PBKDF_ITERATIONS,
                                                          OWNER_PSK_LENGTH_128, (uint8_t*)result);

                OIC_LOG_V(DEBUG, TAG, "DeriveCryptoKeyFromPassword Completed (%d)", dtlsRes);
                OIC_LOG_V(DEBUG, TAG, "PIN : %s", g_PinOxmData.pinData);
                OIC_LOG(DEBUG, TAG, "UUID : ");
                OIC_LOG_BUFFER(DEBUG, TAG, g_PinOxmData.newDevice.id, UUID_LENGTH);

                if(0 == dtlsRes)
                {
                    ret = OWNER_PSK_LENGTH_128;
                }
                else
                {
                    OIC_LOG_V(ERROR, TAG, "Failed to derive crypto key from PIN : result=%d", dtlsRes);
                    ret = -1;
                }
            }
            break;

        default:
            {
                OIC_LOG (ERROR, TAG, "Wrong value passed for CADtlsPskCredType_t.");
                ret = -1;
            }
            break;
    }

    return ret;
}