RPCrypt IV

This commit is contained in:
Florian Märkl 2018-11-17 14:02:57 +01:00
commit 9a71e88819
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 102 additions and 11 deletions

View file

@ -28,17 +28,17 @@ extern "C" {
typedef enum
{
CHIAKI_ERR_SUCCESS = 0,
CHIAKI_ERR_PARSE_ADDR = 1,
CHIAKI_ERR_THREAD = 2,
CHIAKI_ERR_MEMORY = 3,
CHIAKI_ERR_NETWORK = 4,
CHIAKI_ERR_INVALID_DATA = 5,
CHIAKI_ERR_BUF_TOO_SMALL = 6
CHIAKI_ERR_UNKNOWN,
CHIAKI_ERR_PARSE_ADDR,
CHIAKI_ERR_THREAD,
CHIAKI_ERR_MEMORY,
CHIAKI_ERR_NETWORK,
CHIAKI_ERR_INVALID_DATA,
CHIAKI_ERR_BUF_TOO_SMALL
} ChiakiErrorCode;
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);
#ifdef __cplusplus
}
#endif

View file

@ -30,11 +30,16 @@ extern "C" {
typedef struct chiaki_rpcrypt_t
{
uint8_t bright[CHIAKI_KEY_BYTES];
uint8_t ambassador[CHIAKI_KEY_BYTES];
struct hmac_ctx_st *hmac_ctx;
} ChiakiRPCrypt;
CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning);
CHIAKI_EXPORT void chiaki_rpcrypt_init(ChiakiRPCrypt *rpcrypt, const uint8_t *nonce, const uint8_t *morning);
CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, uint8_t *iv, uint64_t counter);
#ifdef __cplusplus
}
#endif

View file

@ -17,6 +17,7 @@
#include <chiaki/common.h>
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
{
switch(code)
@ -36,4 +37,4 @@ CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
default:
return "Unknown";
}
}
}

View file

@ -17,6 +17,10 @@
#include <chiaki/rpcrypt.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <string.h>
CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning)
@ -42,4 +46,37 @@ CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *am
v ^= nonce[i];
bright[i] = v;
}
}
CHIAKI_EXPORT void chiaki_rpcrypt_init(ChiakiRPCrypt *rpcrypt, const uint8_t *nonce, const uint8_t *morning)
{
chiaki_rpcrypt_bright_ambassador(rpcrypt->bright, rpcrypt->ambassador, nonce, morning);
}
CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, uint8_t *iv, uint64_t counter)
{
uint8_t hmac_key[] = { 0xac, 0x07, 0x88, 0x83, 0xc8, 0x3a, 0x1f, 0xe8, 0x11, 0x46, 0x3a, 0xf3, 0x9e, 0xe3, 0xe3, 0x77 };
uint8_t buf[CHIAKI_KEY_BYTES + 8];
memcpy(buf, rpcrypt->ambassador, CHIAKI_KEY_BYTES);
buf[CHIAKI_KEY_BYTES + 0] = (uint8_t)((counter >> 0x38) & 0xff);
buf[CHIAKI_KEY_BYTES + 1] = (uint8_t)((counter >> 0x30) & 0xff);
buf[CHIAKI_KEY_BYTES + 2] = (uint8_t)((counter >> 0x28) & 0xff);
buf[CHIAKI_KEY_BYTES + 3] = (uint8_t)((counter >> 0x20) & 0xff);
buf[CHIAKI_KEY_BYTES + 4] = (uint8_t)((counter >> 0x18) & 0xff);
buf[CHIAKI_KEY_BYTES + 5] = (uint8_t)((counter >> 0x10) & 0xff);
buf[CHIAKI_KEY_BYTES + 6] = (uint8_t)((counter >> 0x08) & 0xff);
buf[CHIAKI_KEY_BYTES + 7] = (uint8_t)((counter >> 0x00) & 0xff);
uint8_t hmac[32];
unsigned int hmac_len = 0;
if(!HMAC(EVP_sha256(), hmac_key, CHIAKI_KEY_BYTES, buf, sizeof(buf), hmac, &hmac_len))
return CHIAKI_ERR_UNKNOWN;
if(hmac_len < CHIAKI_KEY_BYTES)
return CHIAKI_ERR_UNKNOWN;
memcpy(iv, hmac, CHIAKI_KEY_BYTES);
return CHIAKI_ERR_SUCCESS;
}