You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
#include "common.h"
|
|
|
|
void ecdaa_rand(void *buffer, size_t buflen) {
|
|
getrandom(buffer, buflen, GRND_RANDOM);
|
|
}
|
|
|
|
char bin2hex(uint8_t byte) {
|
|
char word = byte & 0x0f;
|
|
char hex = 0;
|
|
if (byte >= 0 && byte <= 9) hex = word + '0';
|
|
else if (byte >= 10 && byte <= 15) hex = word - 10 + 'A';
|
|
return hex;
|
|
}
|
|
|
|
uint8_t hex2bin(char hex) {
|
|
uint8_t byte = 0;
|
|
if (hex >= '0' && hex <= '9') byte = hex - '0';
|
|
else if (hex >= 'a' && hex <= 'f') byte = hex - 'a' + 10;
|
|
else if (hex >= 'A' && hex <= 'F') byte = hex - 'A' + 10;
|
|
return byte;
|
|
}
|
|
|
|
void ecdaa_hextobin(const char *in_hex, uint8_t *out_bin, size_t outlen) {
|
|
for (size_t i = 0, j = 0; i < outlen; i++, j+=2) {
|
|
uint8_t val = hex2bin(in_hex[j]);
|
|
val += hex2bin(in_hex[j+1]) * 16;
|
|
out_bin[i] = (char) val;
|
|
}
|
|
}
|
|
|
|
void ecdaa_bintohex(const uint8_t *in_bin, size_t inlen, char *out_hex) {
|
|
for (size_t i = 0, j = 0; i < inlen; i++, j+=2) {
|
|
out_hex[j] = bin2hex(in_bin[i] % 0x0f);
|
|
out_hex[j+1] = bin2hex((in_bin[i] / 16) % 0x0f);
|
|
}
|
|
}
|
|
|
|
|