( \
SHA256MAGIC_LEN /* magic */ \
+ sizeof(uint32_t) /* c->md_len */ \
+ + sizeof(uint32_t) /* c->num */ \
+ sizeof(uint32_t) * 8 /* c->h */ \
+ sizeof(uint32_t) * 2 /* c->Nl + c->Nh */ \
+ sizeof(uint32_t) * SHA_LBLOCK /* c->data */ \
- + sizeof(uint32_t) /* c->num */ \
)
static int SHA256_Serialize(SHA256_CTX *c, unsigned char *out,
/* md_len */
p = OPENSSL_store_u32_le(p, c->md_len);
+ /* num */
+ p = OPENSSL_store_u32_le(p, c->num);
+
/* h */
for (i = 0; i < sizeof(c->h) / sizeof(SHA_LONG); i++)
p = OPENSSL_store_u32_le(p, c->h[i]);
for (i = 0; i < SHA_LBLOCK; i++)
p = OPENSSL_store_u32_le(p, c->data[i]);
- /* num */
- p = OPENSSL_store_u32_le(p, c->num);
-
if (outlen != NULL)
*outlen = SHA256_SERIALIZATION_LEN;
return 1;
}
+/*
+ * This function only performs basic input sanity checks and is not
+ * built to handle malicious input data. Only trusted input should be
+ * fed to this function
+ */
static int SHA256_Deserialize(SHA256_CTX *c, const unsigned char *in,
size_t inlen)
{
return 0;
}
+ /* num check */
+ p = OPENSSL_load_u32_le(&val, p);
+ if (val >= sizeof(c->data))
+ return 0;
+ c->num = (unsigned int)val;
+
/* h */
for (i = 0; i < (sizeof(c->h) / sizeof(SHA_LONG)); i++) {
p = OPENSSL_load_u32_le(&val, p);
c->data[i] = (SHA_LONG)val;
}
- /* num */
- p = OPENSSL_load_u32_le(&val, p);
- c->num = (unsigned int)val;
-
return 1;
}
( \
SHA512MAGIC_LEN /* magic */ \
+ sizeof(uint32_t) /* c->md_len */ \
+ + sizeof(uint32_t) /* c->num */ \
+ sizeof(uint64_t) * 8 /* c->h */ \
+ sizeof(uint64_t) * 2 /* c->Nl + c->Nh */ \
+ SHA512_CBLOCK /* c->u.d/c->u.p */ \
- + sizeof(uint32_t) /* c->num */ \
)
static int SHA512_Serialize(SHA512_CTX *c, unsigned char *out,
/* md_len */
p = OPENSSL_store_u32_le(p, c->md_len);
+ /* num */
+ p = OPENSSL_store_u32_le(p, c->num);
+
/* h */
for (i = 0; i < sizeof(c->h) / sizeof(SHA_LONG64); i++)
p = OPENSSL_store_u64_le(p, c->h[i]);
memcpy(p, c->u.p, SHA512_CBLOCK);
p += SHA512_CBLOCK;
- /* num */
- p = OPENSSL_store_u32_le(p, c->num);
-
if (outlen != NULL)
*outlen = SHA512_SERIALIZATION_LEN;
return 1;
}
+/*
+ * This function only performs basic input sanity checks and is not
+ * built to handle malicious input data. Only trusted input should be
+ * fed to this function
+ */
static int SHA512_Deserialize(SHA512_CTX *c, const unsigned char *in,
size_t inlen)
{
if ((unsigned int)val32 != c->md_len)
return 0;
+ /* num check */
+ p = OPENSSL_load_u32_le(&val32, p);
+ if (val32 >= sizeof(c->u.d))
+ return 0;
+ c->num = (unsigned int)val32;
+
/* h */
for (i = 0; i < (sizeof(c->h) / sizeof(SHA_LONG64)); i++) {
p = OPENSSL_load_u64_le(&val, p);
memcpy(c->u.p, p, SHA512_CBLOCK);
p += SHA512_CBLOCK;
- /* num */
- p = OPENSSL_load_u32_le(&val32, p);
- c->num = (unsigned int)val32;
-
return 1;
}
KECCAKMAGIC_LEN /* magic string */ \
+ sizeof(uint64_t) /* impl-ID */ \
+ sizeof(uint64_t) /* c->md_size */ \
- + (sizeof(uint64_t) * 5 * 5) /* c->A */ \
+ (sizeof(uint64_t) * 4) /* c->block_size, c->bufsz, c->pad, c->xof_state */ \
+ + (sizeof(uint64_t) * 5 * 5) /* c->A */ \
+ (KECCAK1600_WIDTH / 8 - 32) /* c->buf */ \
)
p = OPENSSL_store_u64_le(p, impl_id);
p = OPENSSL_store_u64_le(p, c->md_size);
+ p = OPENSSL_store_u64_le(p, c->block_size);
+ p = OPENSSL_store_u64_le(p, c->bufsz);
+ p = OPENSSL_store_u64_le(p, c->pad);
+ p = OPENSSL_store_u64_le(p, c->xof_state);
+
/* A matrix */
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++)
p = OPENSSL_store_u64_le(p, c->A[i][j]);
}
- p = OPENSSL_store_u64_le(p, c->block_size);
- p = OPENSSL_store_u64_le(p, c->bufsz);
- p = OPENSSL_store_u64_le(p, c->pad);
- p = OPENSSL_store_u64_le(p, c->xof_state);
-
if (outlen != NULL)
*outlen = KECCAK_SERIALIZATION_LEN;
return 1;
}
+/*
+ * This function only performs basic input sanity checks and is not
+ * built to handle malicious input data. Only trusted input should be
+ * fed to this function
+ */
static int KECCAK_Deserialize(KECCAK1600_CTX *c, int impl_id,
const unsigned char *input, size_t len)
{
if (val != (uint64_t)c->md_size)
return 0;
+ /* check that block_size is congruent with the initialized value */
+ p = OPENSSL_load_u64_le(&val, p);
+ if (val != c->block_size)
+ return 0;
+ /* check that bufsz does not exceed block_size */
+ p = OPENSSL_load_u64_le(&val, p);
+ if (val > c->block_size)
+ return 0;
+ c->bufsz = (size_t)val;
+ p = OPENSSL_load_u64_le(&val, p);
+ if (val != c->pad)
+ return 0;
+ p = OPENSSL_load_u64_le(&val, p);
+ c->xof_state = (int)val;
+
/* A matrix */
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
}
}
- p = OPENSSL_load_u64_le(&val, p);
- c->block_size = (size_t)val;
- p = OPENSSL_load_u64_le(&val, p);
- c->bufsz = (size_t)val;
- p = OPENSSL_load_u64_le(&val, p);
- c->pad = (unsigned char)val;
- p = OPENSSL_load_u64_le(&val, p);
- c->xof_state = (int)val;
-
/* buf */
memcpy(c->buf, p, sizeof(c->buf));