1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define TLOG_TAG "hwrng_fake_srv" 18 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 #include <uapi/err.h> 24 25 #include <hwcrypto/hwrng_dev.h> 26 #include <trusty/time.h> 27 #include <trusty_log.h> 28 29 #pragma message "Compiling FAKE HWRNG provider" 30 hwrng_dev_init(void)31int hwrng_dev_init(void) { 32 TLOGE("Init FAKE!!!! HWRNG service provider\n"); 33 TLOGE("FAKE HWRNG service provider MUST be replaced with the REAL one\n"); 34 return NO_ERROR; 35 } 36 37 static size_t counter = 1; 38 39 __attribute__((no_sanitize("unsigned-integer-overflow"))) int trusty_rng_hw_rand(uint8_t * buf,size_t buf_len)40trusty_rng_hw_rand(uint8_t* buf, size_t buf_len) { 41 int64_t time; 42 trusty_gettime(0, &time); 43 44 time = time ^ (time >> 32); 45 time = time ^ (time >> 16); 46 const uint8_t mask = (time ^ (time >> 8)) & 0xff; 47 48 for (uint8_t* end = buf + buf_len; buf < end; ++buf) { 49 *buf = (counter++ ^ mask) & 0xff; 50 } 51 return NO_ERROR; 52 } 53