Пример #1
0
void blake_small_lastBlock(blake_small_ctx_t *ctx, const void *msg,
        uint16_t length_b)
{
    while (length_b >= BLAKE_SMALL_BLOCKSIZE) {
        blake_small_nextBlock(ctx, msg);
        msg = (uint8_t*) msg + BLAKE_SMALL_BLOCKSIZE_B;
        length_b -= BLAKE_SMALL_BLOCKSIZE;
    }
    union {
        uint8_t v8[64];
        uint32_t v32[16];
    } buffer;
    uint32_t v[16];
    union {
        uint64_t v64;
        uint32_t v32[2];
    } ctr;
    ctr.v64 = ctx->counter * 512 + length_b;
    memset(buffer.v8, 0, 64);
    memcpy(buffer.v8, msg, (length_b + 7) / 8);
    buffer.v8[length_b / 8] |= 0x80 >> (length_b & 0x7);
    blake_small_changeendian(buffer.v8, buffer.v8);
    blake_small_expand(v, ctx);
    if (length_b > 512 - 64 - 2) {
        v[12] ^= ctr.v32[0];
        v[13] ^= ctr.v32[0];
        v[14] ^= ctr.v32[1];
        v[15] ^= ctr.v32[1];
        blake_small_compress(v, buffer.v8);
        blake_small_collapse(ctx, v);
        memset(buffer.v8, 0, 64 - 8);
        blake_small_expand(v, ctx);
    } else {
        if (length_b) {
            v[12] ^= ctr.v32[0];
            v[13] ^= ctr.v32[0];
            v[14] ^= ctr.v32[1];
            v[15] ^= ctr.v32[1];
        }
    }
    if (ctx->appendone)
        buffer.v8[64 - 8 - 4] |= 0x01;
    buffer.v32[14] = ctr.v32[1];
    buffer.v32[15] = ctr.v32[0];
    blake_small_compress(v, buffer.v8);
    blake_small_collapse(ctx, v);

}
void blake_small_nextBlock(blake_small_ctx_t* ctx, const void* msg){
	uint32_t v[16];
	uint32_t m[16];
	union {
		uint64_t v64;
		uint32_t v32[2];
	}ctr;
	blake_small_expand(v,ctx);
	ctx->counter++;
	ctr.v64 = ctx->counter*512;
	v[12] ^= ctr.v32[0];
	v[13] ^= ctr.v32[0];
	v[14] ^= ctr.v32[1];
	v[15] ^= ctr.v32[1];
	blake_small_changeendian(m, msg);
	blake_small_compress(v, m);
	blake_small_collapse(ctx, v);
}