我必须(在状态机上)制作 RSA 签名C
在 32 位板上。我的内存有限,所以我无法将小数存储在向量或类似的东西中。
最好的事情是如果我可以存储位并且可以轻松访问它们;什么存储方法最好?
我做了这个:
#if (CPU_TYPE == CPU_TYPE_32)
typedef uint32_t word;
#define word_length 32
typedef struct BigNumber {
word words[64];
} BigNumber;
#elif (CPU_TYPE == CPU_TYPE_16)
typedef uint16_t word;
#define word_length 16
typedef struct BigNumber {
word words[128];
} BigNumber;
#else
#error Unsupported CPU_TYPE
#endif
这看起来很难用。我怎样才能简化它?
您可以简单地使用 OpenSSL 的 BigNumber API。您可以找到完整的 APIhere https://www.openssl.org/docs/man1.0.2/crypto/bn.html.
并且,您可以使用此代码示例作为开始:
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/bn.h>
int main(int argc, char *argv[])
{
static const char num1[] = "18446744073709551616";
static const char num2[] = "36893488147419103232";
BIGNUM *bn1 = NULL;
BIGNUM *bn2 = NULL;
BN_CTX *ctx = BN_CTX_new();
BN_dec2bn(&bn1, num1); // convert the string to BIGNUM
BN_dec2bn(&bn2, num2);
BN_add(bn1, bn1, bn2); // bn1 = bn1 + bn2
char *result_str = BN_bn2dec(bn1); // convert the BIGNUM back to string
printf("%s + %s = %s\n", num1, num2, result_str);
OPENSSL_free(result_str);
BN_free(bn1);
BN_free(bn2);
BN_CTX_free(ctx);
return 0;
}
编译它:
#> gcc -Wall -Wextra -g -o sample sample.c -lcrypto
执行时你应该得到类似这样的结果:
18446744073709551616 + 36893488147419103232 = 55340232221128654848
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)