void cryptonite_skein512_init(struct skein512_ctx *ctx, uint32_t hashlen) { uint64_t buf[8]; memset(ctx, 0, sizeof(*ctx)); SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_CFG)); memset(buf, '\0', sizeof(buf)); buf[0] = cpu_to_le64((SKEIN_VERSION << 32) | SKEIN_IDSTRING); buf[1] = cpu_to_le64(hashlen); buf[2] = 0; /* tree info, not implemented */ skein512_do_chunk(ctx, buf, 4*8); SET_TYPE(ctx, FLAG_FIRST | FLAG_TYPE(TYPE_MSG)); }
void skein512_finalize(struct skein512_ctx *ctx, uint8_t *out) { uint32_t outsize; uint64_t *p = (uint64_t *) out; uint64_t x[8]; int i, j, n; ctx->t1 |= FLAG_FINAL; /* if buf is not complete pad with 0 bytes */ if (ctx->bufindex < 64) memset(ctx->buf + ctx->bufindex, '\0', 64 - ctx->bufindex); skein512_do_chunk(ctx, (uint64_t *) ctx->buf, ctx->bufindex); memset(ctx->buf, '\0', 64); /* make sure we have a 8 bit rounded value */ outsize = ctx->hashlen; /* backup h[0--7] */ for (j = 0; j < 8; j++) x[j] = ctx->h[j]; /* threefish in counter mode, 0 for 1st 64 bytes, 1 for 2nd 64 bytes, .. */ for (i = 0; i*64 < outsize; i++) { uint64_t w[8]; *((uint64_t *) ctx->buf) = cpu_to_le64(i); SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_OUT)); skein512_do_chunk(ctx, (uint64_t *) ctx->buf, sizeof(uint64_t)); n = outsize - i * 64; if (n >= 64) n = 64; cpu_to_le64_array(w, ctx->h, 8); memcpy(out + i*64, w, n); /* restore h[0--7] */ for (j = 0; j < 8; j++) ctx->h[j] = x[j]; } }
void skein256_finalize(struct skein256_ctx *ctx, uint8_t *out) { uint32_t outsize; uint64_t *p = (uint64_t *) out; uint64_t x[4]; int i, j, n; ctx->t1 |= FLAG_FINAL; /* if buf is not complete pad with 0 bytes */ if (ctx->bufindex < 32) memset(ctx->buf + ctx->bufindex, '\0', 32 - ctx->bufindex); skein256_do_chunk(ctx, (uint64_t *) ctx->buf, ctx->bufindex); memset(ctx->buf, '\0', 32); /* make sure we have a 8 bit rounded value */ outsize = (ctx->hashlen + 7) >> 3; /* backup h[0--4] */ for (j = 0; j < 4; j++) x[j] = ctx->h[j]; /* threefish in counter mode, 0 for 1st 64 bytes, 1 for 2nd 64 bytes, .. */ for (i = 0; i*32 < outsize; i++) { *((uint64_t *) ctx->buf) = cpu_to_le64(i); SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_OUT)); skein256_do_chunk(ctx, (uint64_t *) ctx->buf, sizeof(uint64_t)); n = outsize - i * 32; if (n >= 32) n = 32; /* FIXME should be little endian array copy ? */ memcpy(out + i*32, ctx->h, n); /* restore h[0--4] */ for (j = 0; j < 4; j++) ctx->h[j] = x[j]; } }
static rc_t decode_encoded(struct decoded *y, const struct encoded *x) { unsigned i; unsigned type = x->flags & 0x3; rc_t rc; const uint8_t *src; uint8_t *hsrc; unsigned elem_bits; unsigned n = 0; memset(y, 0, sizeof(*y)); y->data_count = x->data_count; y->size_type = (x->flags >> 2) & 3; y->diff = alloc_raw_nbuf(y->data_count); if (y->diff == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); if (type) { if ((type & 1) != 0) { rc = zlib_decompress(y->diff->data.u8, 8 * y->data_count, &n, x->u.zipped.data, x->u.zipped.data_size ); if (rc) return rc; if ((type & 2) != 0) y->diff->min = x->u.packed.min; } else { y->diff->min = x->u.packed.min; memcpy(y->diff->data.u8, x->u.packed.data, n = x->u.packed.data_size); } elem_bits = n * 8 / x->data_count; if (elem_bits * x->data_count / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->diff, elem_bits); if (type == 1 && 4 - y->diff->var != y->size_type) { #if _DEBUGGING fprintf(stderr, "decode_encoded: var = %i, size_type = %u\n", (int)y->diff->var, (unsigned)y->size_type); #endif return RC(rcXF, rcFunction, rcExecuting, rcRange, rcExcessive); } y->diff->used = x->data_count; return 0; } y->type = malloc(x->u.izipped.segments); if (y->type == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); if (x->u.izipped.outliers) { hsrc = NULL; if (FLAG_TYPE(x->u.izipped) == DATA_ZIPPED) { hsrc = malloc(x->u.izipped.segments); if (hsrc == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); rc = zlib_decompress(hsrc, x->u.izipped.segments, &i, x->u.izipped.type, x->u.izipped.type_size); if (rc) { free(hsrc); return rc; } src = hsrc; } else src = x->u.izipped.type; decode_types(y->type, x->u.izipped.segments, src); if (hsrc) free(hsrc); for (n = i = 0; i != x->u.izipped.segments; ++i) { if (y->type[i]) ++n; } y->lines = x->u.izipped.segments - n; y->outliers = n; } else { memset(y->type, 0, x->u.izipped.segments); y->lines = x->u.izipped.segments; y->outliers = 0; } y->diff->min = x->u.izipped.min_diff; if (FLAG_DIFF(x->u.izipped) == DATA_CONSTANT) { y->diff->used = x->u.izipped.diff_size; memset(y->diff->data.raw, 0, nbuf_size(y->diff)); } else { if (FLAG_DIFF(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->diff->data.u8, y->diff->size * 8, &n, x->u.izipped.diff, x->u.izipped.diff_size); if (rc) return rc; } else { n = x->u.izipped.diff_size; memcpy(y->diff->data.u8, x->u.izipped.diff, n); } elem_bits = (n * 8) / y->diff->size; if (elem_bits * y->diff->size / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->diff, elem_bits); y->diff->used = n >> (4 - y->diff->var); } y->length = alloc_nbuf(y->lines + y->outliers, 2); if (y->length == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); y->length->min = x->u.izipped.min_length; if (FLAG_LENGTH(x->u.izipped) == DATA_CONSTANT) { y->length->used = y->lines + y->outliers; memset(y->length->data.raw, 0, nbuf_size(y->length)); } else { if (FLAG_LENGTH(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->length->data.u8, y->length->size * 4, &n, x->u.izipped.length, x->u.izipped.length_size); if (rc) return rc; } else { n = x->u.izipped.length_size; memcpy(y->length->data.u8, x->u.izipped.length, n); } elem_bits = (n * 8) / (y->lines + y->outliers); if (elem_bits * (y->lines + y->outliers) / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->length, elem_bits); y->length->used = n >> (4 - y->length->var); } y->dy = alloc_nbuf(y->lines, 1); if (y->dy == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); y->dy->min = x->u.izipped.min_dy; if (FLAG_DY(x->u.izipped) == DATA_CONSTANT) { y->dy->used = y->lines; memset(y->dy->data.raw, 0, nbuf_size(y->dy)); } else { if (FLAG_DY(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->dy->data.u8, y->dy->size * 8, &n, x->u.izipped.dy, x->u.izipped.dy_size); if (rc) return rc; } else { n = x->u.izipped.dy_size; memcpy(y->dy->data.u8, x->u.izipped.dy, n); } elem_bits = (n * 8) / y->lines; if (elem_bits * y->lines / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->dy, elem_bits); y->dy->used = n >> (4 - y->dy->var); } y->dx = alloc_nbuf(y->lines, 1); if (y->dx == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); y->dx->min = x->u.izipped.min_dx; if (FLAG_DX(x->u.izipped) == DATA_CONSTANT) { y->dx->used = y->lines; memset(y->dx->data.raw, 0, nbuf_size(y->dx)); } else { if (FLAG_DX(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->dx->data.u8, y->dx->size * 8, &n, x->u.izipped.dx, x->u.izipped.dx_size); if (rc) return rc; } else { n = x->u.izipped.dx_size; memcpy(y->dx->data.u8, x->u.izipped.dx, n); } elem_bits = (n * 8) / y->lines; if (elem_bits * y->lines / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->dx, elem_bits); y->dx->used = n >> (4 - y->dx->var); } y->a = alloc_nbuf(y->lines, 1); if (y->a == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); y->a->min = x->u.izipped.min_a; if (FLAG_A(x->u.izipped) == DATA_CONSTANT) { y->a->used = y->lines; memset(y->a->data.raw, 0, nbuf_size(y->a)); } else { if (FLAG_A(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->a->data.u8, y->a->size * 8, &n, x->u.izipped.a, x->u.izipped.a_size); if (rc) return rc; } else { n = x->u.izipped.a_size; memcpy(y->a->data.u8, x->u.izipped.a, n); } elem_bits = (n * 8) / y->lines; if (elem_bits * y->lines / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->a, elem_bits); y->a->used = n >> (4 - y->a->var); } if (y->outliers) { y->outlier = alloc_nbuf(x->u.izipped.outliers, 1); if (y->outlier == NULL) return RC(rcXF, rcFunction, rcExecuting, rcMemory, rcExhausted); y->outlier->min = x->u.izipped.min_outlier; if (FLAG_OUTLIER(x->u.izipped) == DATA_CONSTANT) { y->outlier->used = y->outliers; memset(y->outlier->data.raw, 0, nbuf_size(y->outlier)); } else { if (FLAG_OUTLIER(x->u.izipped) == DATA_ZIPPED) { rc = zlib_decompress(y->outlier->data.u8, y->outlier->size * 8, &n, x->u.izipped.outlier, x->u.izipped.outlier_size); if (rc) return rc; } else { n = x->u.izipped.outlier_size; memcpy(y->outlier->data.u8, x->u.izipped.outlier, n); } elem_bits = (n * 8) / x->u.izipped.outliers; if (elem_bits * x->u.izipped.outliers / 8 != n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInvalid); BITS_TO_VARIANT(y->outlier, elem_bits); y->outlier->used = n >> (4 - y->outlier->var); } }
static rc_t deserialize_encoded(struct encoded *y, const uint8_t src[], unsigned n, int swap) { unsigned i = 0; memset(y, 0, sizeof(*y)); if (i + 1 > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->flags = src[i]; ++i; if (i + 4 > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); memcpy(&y->data_count, &src[i], 4); i += 4; if (swap) y->data_count = bswap_32(y->data_count); switch (y->flags & 0x03) { case 2: case 3: if (i + 8 > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); memcpy(&y->u.packed.min, &src[i], 8); i += 8; if (swap) y->u.packed.min = bswap_64(y->u.packed.min); case 1: y->u.zipped.data_size = n - i; y->u.zipped.data = &src[i]; return 0; default: break; } DESERIAL32(data_flags); DESERIAL32(segments); DESERIAL32(outliers); DESERIAL32(type_size); DESERIAL32(diff_size); DESERIAL32(length_size); DESERIAL32(dy_size); DESERIAL32(dx_size); DESERIAL32(a_size); DESERIAL32(outlier_size); DESERIAL64(min_diff); DESERIAL64(min_length); DESERIAL64(min_dy); DESERIAL64(min_dx); DESERIAL64(min_a); DESERIAL64(min_outlier); if (FLAG_TYPE(y->u.izipped) != DATA_ABSENT && FLAG_TYPE(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.type_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.type = &src[i]; i += y->u.izipped.type_size; } if (FLAG_DIFF(y->u.izipped) != DATA_ABSENT && FLAG_DIFF(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.diff_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.diff = &src[i]; i += y->u.izipped.diff_size; } if (FLAG_LENGTH(y->u.izipped) != DATA_ABSENT && FLAG_LENGTH(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.length_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.length = &src[i]; i += y->u.izipped.length_size; } if (FLAG_DY(y->u.izipped) != DATA_ABSENT && FLAG_DY(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.dy_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.dy = &src[i]; i += y->u.izipped.dy_size; } if (FLAG_DX(y->u.izipped) != DATA_ABSENT && FLAG_DX(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.dx_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.dx = &src[i]; i += y->u.izipped.dx_size; } if (FLAG_A(y->u.izipped) != DATA_ABSENT && FLAG_A(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.a_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.a = &src[i]; i += y->u.izipped.a_size; } if (FLAG_OUTLIER(y->u.izipped) != DATA_ABSENT && FLAG_OUTLIER(y->u.izipped) != DATA_CONSTANT) { if (i + y->u.izipped.outlier_size > n) return RC(rcXF, rcFunction, rcExecuting, rcData, rcInsufficient); y->u.izipped.outlier = &src[i]; /* i += y->u.izipped.outlier_size; */ } return 0; }