即使我们使用本地函数将currentMethod.bytes设置为生成随机数,RAND_bytes
也不会调用。设置RAND_set_rand_method(&cuurentMethod)
之后。
我在这里附加了已经尝试过的链接[https://github.com/openssl/openssl/blob/master/test/sm2_internal_test.c]。
int main()
{
unsigned char rand[16];
int ret;
RAND_METHOD *oldMethod,currentMethod,*temp;
oldMethod = RAND_get_rand_method();/*getting default method*/
currentMethod = *oldMethod;
currentMethod.bytes = local_function_rand;
if((ret = RAND_set_rand_method(¤tMethod))!= 1)
return 0;
/* Now we are printing both address of local_function_method_rand() and
temp->bytes , those address are same after getting. */
temp = RAND_get_rand_method();
/* after we are comparing with RAND_SSLeay() function , to find default or not*/
if((ret = RAND_bytes(rand,16)) != 1)
return 0;
return 1;
}
预期结果是我们的本地函数应该调用。另外,要调用RAND_bytes()
,是否需要在Linux系统中设置fips模式?
答案 0 :(得分:1)
清理并最小化测试程序并填写缺失的部分后:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vtas_kassenterminal, PID: 10504
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.vtas_kassenterminal-zh4y12ECK4PCd-N-FXMp6A==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.vtas_kassenterminal-zh4y12ECK4PCd-N-FXMp6A==/lib/arm, /system/lib, /system/vendor/lib]]] couldn't find "libpcsclite.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1657)
at com.example.vtas_kassenterminal.JacspcscLoader.<clinit>(JacspcscLoader.java:280)
at com.example.vtas_kassenterminal.nfcTest.onCreate(nfcTest.java:135)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
并运行它(使用OpenSSL 1.1.1):
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int local_function_rand(unsigned char *buf, int num) {
printf("in local_function_rand(); requested %d bytes\n", num);
memset(buf, 4, num); // RFC 1149.5 standard random number
return 1;
}
int main(void) {
unsigned char rand[16];
RAND_METHOD currentMethod = {.bytes = local_function_rand};
RAND_set_rand_method(¤tMethod);
if (RAND_bytes(rand, sizeof rand) != 1) {
return EXIT_FAILURE;
}
return 0;
}
它按预期工作; $ gcc -Wall -Wextra rand.c -lcrypto
$ ./a.out
in local_function_rand(); requested 16 bytes
正在调用用户提供的函数。如果您从代码中获得不同的结果,则可能是因为您未在问题中包含的位有问题。