|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "ogs-crypt.h" |
|
|
|
|
|
|
|
|
|
|
|
void ogs_hmac_sha224_init(ogs_hmac_sha224_ctx *ctx, const uint8_t *key, |
|
|
uint32_t key_size) |
|
|
{ |
|
|
uint32_t fill; |
|
|
uint32_t num; |
|
|
|
|
|
uint8_t key_temp[OGS_SHA224_DIGEST_SIZE]; |
|
|
int i; |
|
|
|
|
|
if (key_size == OGS_SHA224_BLOCK_SIZE) { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = OGS_SHA224_BLOCK_SIZE; |
|
|
} else { |
|
|
if (key_size > OGS_SHA224_BLOCK_SIZE){ |
|
|
num = OGS_SHA224_DIGEST_SIZE; |
|
|
ogs_sha224(key, key_size, key_temp); |
|
|
} else { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = key_size; |
|
|
} |
|
|
fill = OGS_SHA224_BLOCK_SIZE - num; |
|
|
|
|
|
memset(ctx->block_ipad + num, 0x36, fill); |
|
|
memset(ctx->block_opad + num, 0x5c, fill); |
|
|
} |
|
|
|
|
|
for (i = 0; i < num; i++) { |
|
|
ctx->block_ipad[i] = key_temp[i] ^ 0x36; |
|
|
ctx->block_opad[i] = key_temp[i] ^ 0x5c; |
|
|
} |
|
|
|
|
|
ogs_sha224_init(&ctx->ctx_inside); |
|
|
ogs_sha224_update(&ctx->ctx_inside, ctx->block_ipad, OGS_SHA224_BLOCK_SIZE); |
|
|
|
|
|
ogs_sha224_init(&ctx->ctx_outside); |
|
|
ogs_sha224_update(&ctx->ctx_outside, ctx->block_opad, |
|
|
OGS_SHA224_BLOCK_SIZE); |
|
|
|
|
|
|
|
|
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, |
|
|
sizeof(ogs_sha224_ctx)); |
|
|
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, |
|
|
sizeof(ogs_sha224_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha224_reinit(ogs_hmac_sha224_ctx *ctx) |
|
|
{ |
|
|
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, |
|
|
sizeof(ogs_sha224_ctx)); |
|
|
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, |
|
|
sizeof(ogs_sha224_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha224_update(ogs_hmac_sha224_ctx *ctx, const uint8_t *message, |
|
|
uint32_t message_len) |
|
|
{ |
|
|
ogs_sha224_update(&ctx->ctx_inside, message, message_len); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha224_final(ogs_hmac_sha224_ctx *ctx, uint8_t *mac, |
|
|
uint32_t mac_size) |
|
|
{ |
|
|
uint8_t digest_inside[OGS_SHA224_DIGEST_SIZE]; |
|
|
uint8_t mac_temp[OGS_SHA224_DIGEST_SIZE]; |
|
|
|
|
|
ogs_sha224_final(&ctx->ctx_inside, digest_inside); |
|
|
ogs_sha224_update(&ctx->ctx_outside, digest_inside, OGS_SHA224_DIGEST_SIZE); |
|
|
ogs_sha224_final(&ctx->ctx_outside, mac_temp); |
|
|
memcpy(mac, mac_temp, mac_size); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha224(const uint8_t *key, uint32_t key_size, |
|
|
const uint8_t *message, uint32_t message_len, |
|
|
uint8_t *mac, uint32_t mac_size) |
|
|
{ |
|
|
ogs_hmac_sha224_ctx ctx; |
|
|
|
|
|
ogs_hmac_sha224_init(&ctx, key, key_size); |
|
|
ogs_hmac_sha224_update(&ctx, message, message_len); |
|
|
ogs_hmac_sha224_final(&ctx, mac, mac_size); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ogs_hmac_sha256_init(ogs_hmac_sha256_ctx *ctx, const uint8_t *key, |
|
|
uint32_t key_size) |
|
|
{ |
|
|
uint32_t fill; |
|
|
uint32_t num; |
|
|
|
|
|
uint8_t key_temp[OGS_SHA256_DIGEST_SIZE]; |
|
|
int i; |
|
|
|
|
|
if (key_size == OGS_SHA256_BLOCK_SIZE) { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = OGS_SHA256_BLOCK_SIZE; |
|
|
} else { |
|
|
if (key_size > OGS_SHA256_BLOCK_SIZE){ |
|
|
num = OGS_SHA256_DIGEST_SIZE; |
|
|
ogs_sha256(key, key_size, key_temp); |
|
|
} else { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = key_size; |
|
|
} |
|
|
fill = OGS_SHA256_BLOCK_SIZE - num; |
|
|
|
|
|
memset(ctx->block_ipad + num, 0x36, fill); |
|
|
memset(ctx->block_opad + num, 0x5c, fill); |
|
|
} |
|
|
|
|
|
for (i = 0; i < num; i++) { |
|
|
ctx->block_ipad[i] = key_temp[i] ^ 0x36; |
|
|
ctx->block_opad[i] = key_temp[i] ^ 0x5c; |
|
|
} |
|
|
|
|
|
ogs_sha256_init(&ctx->ctx_inside); |
|
|
ogs_sha256_update(&ctx->ctx_inside, ctx->block_ipad, OGS_SHA256_BLOCK_SIZE); |
|
|
|
|
|
ogs_sha256_init(&ctx->ctx_outside); |
|
|
ogs_sha256_update(&ctx->ctx_outside, ctx->block_opad, |
|
|
OGS_SHA256_BLOCK_SIZE); |
|
|
|
|
|
|
|
|
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, |
|
|
sizeof(ogs_sha256_ctx)); |
|
|
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, |
|
|
sizeof(ogs_sha256_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha256_reinit(ogs_hmac_sha256_ctx *ctx) |
|
|
{ |
|
|
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, |
|
|
sizeof(ogs_sha256_ctx)); |
|
|
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, |
|
|
sizeof(ogs_sha256_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha256_update(ogs_hmac_sha256_ctx *ctx, const uint8_t *message, |
|
|
uint32_t message_len) |
|
|
{ |
|
|
ogs_sha256_update(&ctx->ctx_inside, message, message_len); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha256_final(ogs_hmac_sha256_ctx *ctx, uint8_t *mac, |
|
|
uint32_t mac_size) |
|
|
{ |
|
|
uint8_t digest_inside[OGS_SHA256_DIGEST_SIZE]; |
|
|
uint8_t mac_temp[OGS_SHA256_DIGEST_SIZE]; |
|
|
|
|
|
ogs_sha256_final(&ctx->ctx_inside, digest_inside); |
|
|
ogs_sha256_update(&ctx->ctx_outside, digest_inside, OGS_SHA256_DIGEST_SIZE); |
|
|
ogs_sha256_final(&ctx->ctx_outside, mac_temp); |
|
|
memcpy(mac, mac_temp, mac_size); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha256(const uint8_t *key, uint32_t key_size, |
|
|
const uint8_t *message, uint32_t message_len, |
|
|
uint8_t *mac, uint32_t mac_size) |
|
|
{ |
|
|
ogs_hmac_sha256_ctx ctx; |
|
|
|
|
|
ogs_hmac_sha256_init(&ctx, key, key_size); |
|
|
ogs_hmac_sha256_update(&ctx, message, message_len); |
|
|
ogs_hmac_sha256_final(&ctx, mac, mac_size); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ogs_hmac_sha384_init(ogs_hmac_sha384_ctx *ctx, const uint8_t *key, |
|
|
uint32_t key_size) |
|
|
{ |
|
|
uint32_t fill; |
|
|
uint32_t num; |
|
|
|
|
|
uint8_t key_temp[OGS_SHA384_DIGEST_SIZE]; |
|
|
int i; |
|
|
|
|
|
if (key_size == OGS_SHA384_BLOCK_SIZE) { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = OGS_SHA384_BLOCK_SIZE; |
|
|
} else { |
|
|
if (key_size > OGS_SHA384_BLOCK_SIZE){ |
|
|
num = OGS_SHA384_DIGEST_SIZE; |
|
|
ogs_sha384(key, key_size, key_temp); |
|
|
} else { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = key_size; |
|
|
} |
|
|
fill = OGS_SHA384_BLOCK_SIZE - num; |
|
|
|
|
|
memset(ctx->block_ipad + num, 0x36, fill); |
|
|
memset(ctx->block_opad + num, 0x5c, fill); |
|
|
} |
|
|
|
|
|
for (i = 0; i < num; i++) { |
|
|
ctx->block_ipad[i] = key_temp[i] ^ 0x36; |
|
|
ctx->block_opad[i] = key_temp[i] ^ 0x5c; |
|
|
} |
|
|
|
|
|
ogs_sha384_init(&ctx->ctx_inside); |
|
|
ogs_sha384_update(&ctx->ctx_inside, ctx->block_ipad, OGS_SHA384_BLOCK_SIZE); |
|
|
|
|
|
ogs_sha384_init(&ctx->ctx_outside); |
|
|
ogs_sha384_update(&ctx->ctx_outside, ctx->block_opad, |
|
|
OGS_SHA384_BLOCK_SIZE); |
|
|
|
|
|
|
|
|
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, |
|
|
sizeof(ogs_sha384_ctx)); |
|
|
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, |
|
|
sizeof(ogs_sha384_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha384_reinit(ogs_hmac_sha384_ctx *ctx) |
|
|
{ |
|
|
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, |
|
|
sizeof(ogs_sha384_ctx)); |
|
|
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, |
|
|
sizeof(ogs_sha384_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha384_update(ogs_hmac_sha384_ctx *ctx, const uint8_t *message, |
|
|
uint32_t message_len) |
|
|
{ |
|
|
ogs_sha384_update(&ctx->ctx_inside, message, message_len); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha384_final(ogs_hmac_sha384_ctx *ctx, uint8_t *mac, |
|
|
uint32_t mac_size) |
|
|
{ |
|
|
uint8_t digest_inside[OGS_SHA384_DIGEST_SIZE]; |
|
|
uint8_t mac_temp[OGS_SHA384_DIGEST_SIZE]; |
|
|
|
|
|
ogs_sha384_final(&ctx->ctx_inside, digest_inside); |
|
|
ogs_sha384_update(&ctx->ctx_outside, digest_inside, OGS_SHA384_DIGEST_SIZE); |
|
|
ogs_sha384_final(&ctx->ctx_outside, mac_temp); |
|
|
memcpy(mac, mac_temp, mac_size); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha384(const uint8_t *key, uint32_t key_size, |
|
|
const uint8_t *message, uint32_t message_len, |
|
|
uint8_t *mac, uint32_t mac_size) |
|
|
{ |
|
|
ogs_hmac_sha384_ctx ctx; |
|
|
|
|
|
ogs_hmac_sha384_init(&ctx, key, key_size); |
|
|
ogs_hmac_sha384_update(&ctx, message, message_len); |
|
|
ogs_hmac_sha384_final(&ctx, mac, mac_size); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ogs_hmac_sha512_init(ogs_hmac_sha512_ctx *ctx, const uint8_t *key, |
|
|
uint32_t key_size) |
|
|
{ |
|
|
uint32_t fill; |
|
|
uint32_t num; |
|
|
|
|
|
uint8_t key_temp[OGS_SHA512_DIGEST_SIZE]; |
|
|
int i; |
|
|
|
|
|
if (key_size == OGS_SHA512_BLOCK_SIZE) { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = OGS_SHA512_BLOCK_SIZE; |
|
|
} else { |
|
|
if (key_size > OGS_SHA512_BLOCK_SIZE){ |
|
|
num = OGS_SHA512_DIGEST_SIZE; |
|
|
ogs_sha512(key, key_size, key_temp); |
|
|
} else { |
|
|
memcpy(key_temp, key, sizeof(key_temp)); |
|
|
num = key_size; |
|
|
} |
|
|
fill = OGS_SHA512_BLOCK_SIZE - num; |
|
|
|
|
|
memset(ctx->block_ipad + num, 0x36, fill); |
|
|
memset(ctx->block_opad + num, 0x5c, fill); |
|
|
} |
|
|
|
|
|
for (i = 0; i < num; i++) { |
|
|
ctx->block_ipad[i] = key_temp[i] ^ 0x36; |
|
|
ctx->block_opad[i] = key_temp[i] ^ 0x5c; |
|
|
} |
|
|
|
|
|
ogs_sha512_init(&ctx->ctx_inside); |
|
|
ogs_sha512_update(&ctx->ctx_inside, ctx->block_ipad, OGS_SHA512_BLOCK_SIZE); |
|
|
|
|
|
ogs_sha512_init(&ctx->ctx_outside); |
|
|
ogs_sha512_update(&ctx->ctx_outside, ctx->block_opad, |
|
|
OGS_SHA512_BLOCK_SIZE); |
|
|
|
|
|
|
|
|
memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside, |
|
|
sizeof(ogs_sha512_ctx)); |
|
|
memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside, |
|
|
sizeof(ogs_sha512_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha512_reinit(ogs_hmac_sha512_ctx *ctx) |
|
|
{ |
|
|
memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit, |
|
|
sizeof(ogs_sha512_ctx)); |
|
|
memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit, |
|
|
sizeof(ogs_sha512_ctx)); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha512_update(ogs_hmac_sha512_ctx *ctx, const uint8_t *message, |
|
|
uint32_t message_len) |
|
|
{ |
|
|
ogs_sha512_update(&ctx->ctx_inside, message, message_len); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha512_final(ogs_hmac_sha512_ctx *ctx, uint8_t *mac, |
|
|
uint32_t mac_size) |
|
|
{ |
|
|
uint8_t digest_inside[OGS_SHA512_DIGEST_SIZE]; |
|
|
uint8_t mac_temp[OGS_SHA512_DIGEST_SIZE]; |
|
|
|
|
|
ogs_sha512_final(&ctx->ctx_inside, digest_inside); |
|
|
ogs_sha512_update(&ctx->ctx_outside, digest_inside, OGS_SHA512_DIGEST_SIZE); |
|
|
ogs_sha512_final(&ctx->ctx_outside, mac_temp); |
|
|
memcpy(mac, mac_temp, mac_size); |
|
|
} |
|
|
|
|
|
void ogs_hmac_sha512(const uint8_t *key, uint32_t key_size, |
|
|
const uint8_t *message, uint32_t message_len, |
|
|
uint8_t *mac, uint32_t mac_size) |
|
|
{ |
|
|
ogs_hmac_sha512_ctx ctx; |
|
|
|
|
|
ogs_hmac_sha512_init(&ctx, key, key_size); |
|
|
ogs_hmac_sha512_update(&ctx, message, message_len); |
|
|
ogs_hmac_sha512_final(&ctx, mac, mac_size); |
|
|
} |
|
|
|