mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Add WIN32 rand
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(GmSSL)
|
||||
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
|
||||
if (WIN32)
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
else()
|
||||
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||
endif()
|
||||
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
|
||||
|
||||
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
|
||||
include_directories(include)
|
||||
|
||||
#enable_language(ASM)
|
||||
@@ -39,7 +34,6 @@ set(src
|
||||
src/sha256.c
|
||||
src/sha512.c
|
||||
src/chacha20.c
|
||||
src/rand.c
|
||||
src/hash_drbg.c
|
||||
src/block_cipher.c
|
||||
src/digest.c
|
||||
@@ -84,6 +78,9 @@ set(src
|
||||
|
||||
if (WIN32)
|
||||
list(APPEND src src/u_time.c)
|
||||
list(APPEND src src/rand_win.c)
|
||||
else()
|
||||
list(APPEND src src/rand.c)
|
||||
endif()
|
||||
|
||||
set(broken_crypto_src
|
||||
|
||||
47
src/rand_win.c
Normal file
47
src/rand_win.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <wincrypt.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
int rand_bytes(uint8_t *buf, size_t len)
|
||||
{
|
||||
HCRYPTPROV hCryptProv = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (!buf) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len > INT_MAX) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (CryptAcquireContextA(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT|CRYPT_SILENT) != TRUE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (CryptGenRandom(hCryptProv, (DWORD)len, buf) != TRUE) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 1
|
||||
end:
|
||||
if (CryptReleaseContext(hCryptProv, 0) != TRUE) {
|
||||
error_print();
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user