示例#1
0
/**
 * Store calculated hash into the given array.
 *
 * @param ctx the algorithm context containing current hashing state
 * @param result calculated hash in binary form
 */
void whirlpool_final(whirlpool_ctx *ctx, unsigned char* result)
{
  unsigned index = (unsigned)ctx->length & 63;
  uint64_t* msg64 = (uint64_t*)ctx->message;

  /* pad message and run for last block */
  ctx->message[index++] = 0x80;

  /* if no room left in the message to store 256-bit message length */
  if(index>32) {
    /* then pad the rest with zeros and process it */
    while(index < 64) {
      ctx->message[index++] = 0;
    }
    whirlpool_process_block(ctx->hash, msg64);
    index = 0;
  }
  /* due to optimization actually only 64-bit of message length are stored */
  while(index < 56) {
    ctx->message[index++] = 0;
  }
  msg64[7] = be2me_64(ctx->length << 3);
  whirlpool_process_block(ctx->hash, msg64);
  
  /* save result hash */
  be64_copy(result, 0, ctx->hash, 64);
}
示例#2
0
/**
 * Reads a 8-byte big-endian word from the input stream
 */
static inline uint64_t
read8be(PcmFile *pf)
{
    uint64_t x;
    if (byteio_read(&x, 8, &pf->io) != 8)
        return 0;
    pf->filepos += 8;
    return be2me_64(x);
}
示例#3
0
void av_sha1_final(AVSHA1* ctx, uint8_t digest[20]){
    int i;
    uint64_t finalcount= be2me_64(ctx->count<<3);

    av_sha1_update(ctx, "\200", 1);
    while ((ctx->count & 63) != 56) {
        av_sha1_update(ctx, "", 1);
    }
    av_sha1_update(ctx, &finalcount, 8);  /* Should cause a transform() */
    for(i=0; i<5; i++)
        ((uint32_t*)digest)[i]= be2me_32(ctx->state[i]);
}
示例#4
0
static int aac_sync(uint64_t state, AACAC3ParseContext *hdr_info,
        int *need_next_header, int *new_frame_start)
{
    GetBitContext bits;
    int size, rdb, ch, sr;
    union {
        uint64_t u64;
        uint8_t  u8[8];
    } tmp;

    tmp.u64 = be2me_64(state);
    init_get_bits(&bits, tmp.u8+8-AAC_HEADER_SIZE, AAC_HEADER_SIZE * 8);

    if(get_bits(&bits, 12) != 0xfff)
        return 0;

    skip_bits1(&bits);          /* id */
    skip_bits(&bits, 2);        /* layer */
    skip_bits1(&bits);          /* protection_absent */
    skip_bits(&bits, 2);        /* profile_objecttype */
    sr = get_bits(&bits, 4);    /* sample_frequency_index */
    if(!ff_mpeg4audio_sample_rates[sr])
        return 0;
    skip_bits1(&bits);          /* private_bit */
    ch = get_bits(&bits, 3);    /* channel_configuration */
    if(!ff_mpeg4audio_channels[ch])
        return 0;
    skip_bits1(&bits);          /* original/copy */
    skip_bits1(&bits);          /* home */

    /* adts_variable_header */
    skip_bits1(&bits);          /* copyright_identification_bit */
    skip_bits1(&bits);          /* copyright_identification_start */
    size = get_bits(&bits, 13); /* aac_frame_length */
    if(size < AAC_HEADER_SIZE)
        return 0;

    skip_bits(&bits, 11);       /* adts_buffer_fullness */
    rdb = get_bits(&bits, 2);   /* number_of_raw_data_blocks_in_frame */

    hdr_info->channels = ff_mpeg4audio_channels[ch];
    hdr_info->sample_rate = ff_mpeg4audio_sample_rates[sr];
    hdr_info->samples = (rdb + 1) * 1024;
    hdr_info->bit_rate = size * 8 * hdr_info->sample_rate / hdr_info->samples;

    *need_next_header = 0;
    *new_frame_start  = 1;
    return size;
}
示例#5
0
/**
 * The core transformation. Process a 512-bit block.
 *
 * @param hash algorithm state
 * @param block the message block to process
 */
static void whirlpool_process_block(uint64_t *hash, uint64_t* p_block)
{
    int i;                /* loop counter */
    uint64_t K1[8];       /* key used in even rounds */
    uint64_t K2[8];       /* key used in odd  rounds */
    uint64_t state1[8];    /* state used in even rounds */
    uint64_t state2[8];    /* state used in odd rounds */

    /* the number of rounds of the internal dedicated block cipher */
    const int number_of_rounds = 10;

    /* array used in the rounds */
    static const uint64_t rc[10] = {
      I64(0x1823c6e887b8014f),
      I64(0x36a6d2f5796f9152),
      I64(0x60bc9b8ea30c7b35),
      I64(0x1de0d7c22e4bfe57),
      I64(0x157737e59ff04ada),
      I64(0x58c9290ab1a06b85),
      I64(0xbd5d10f4cb3e0567),
      I64(0xe427418ba77d95d8),
      I64(0xfbee7c66dd17479e),
      I64(0xca2dbf07ad5a8333)
    };

    /* map the message buffer to a block */
    for(i = 0; i < 8; i++) {
      /* store K^0 and xor it with intermediate hash state */
      state1[i] = hash[i] = be2me_64(p_block[i]) ^ (K1[i] = hash[i]);
    }

    /* iterate over algorithm rounds */
    for(i = 0; i < number_of_rounds; i++)
    {
        /* compute K^i from K^{i-1} */
        K2[0] = WHIRLPOOL_OP(K1, 0) ^ rc[i];
        K2[1] = WHIRLPOOL_OP(K1, 1);
        K2[2] = WHIRLPOOL_OP(K1, 2);
        K2[3] = WHIRLPOOL_OP(K1, 3);
        K2[4] = WHIRLPOOL_OP(K1, 4);
        K2[5] = WHIRLPOOL_OP(K1, 5);
        K2[6] = WHIRLPOOL_OP(K1, 6);
        K2[7] = WHIRLPOOL_OP(K1, 7);

        /* apply the i-th round transformation */
        state2[0] = WHIRLPOOL_OP(state1, 0) ^ K2[0];
        state2[1] = WHIRLPOOL_OP(state1, 1) ^ K2[1];
        state2[2] = WHIRLPOOL_OP(state1, 2) ^ K2[2];
        state2[3] = WHIRLPOOL_OP(state1, 3) ^ K2[3];
        state2[4] = WHIRLPOOL_OP(state1, 4) ^ K2[4];
        state2[5] = WHIRLPOOL_OP(state1, 5) ^ K2[5];
        state2[6] = WHIRLPOOL_OP(state1, 6) ^ K2[6];
        state2[7] = WHIRLPOOL_OP(state1, 7) ^ K2[7];
        i++;

        /* compute K^i from K^{i-1} */
        K1[0] = WHIRLPOOL_OP(K2, 0) ^ rc[i];
        K1[1] = WHIRLPOOL_OP(K2, 1);
        K1[2] = WHIRLPOOL_OP(K2, 2);
        K1[3] = WHIRLPOOL_OP(K2, 3);
        K1[4] = WHIRLPOOL_OP(K2, 4);
        K1[5] = WHIRLPOOL_OP(K2, 5);
        K1[6] = WHIRLPOOL_OP(K2, 6);
        K1[7] = WHIRLPOOL_OP(K2, 7);

        /* apply the i-th round transformation */
        state1[0] = WHIRLPOOL_OP(state2, 0) ^ K1[0];
        state1[1] = WHIRLPOOL_OP(state2, 1) ^ K1[1];
        state1[2] = WHIRLPOOL_OP(state2, 2) ^ K1[2];
        state1[3] = WHIRLPOOL_OP(state2, 3) ^ K1[3];
        state1[4] = WHIRLPOOL_OP(state2, 4) ^ K1[4];
        state1[5] = WHIRLPOOL_OP(state2, 5) ^ K1[5];
        state1[6] = WHIRLPOOL_OP(state2, 6) ^ K1[6];
        state1[7] = WHIRLPOOL_OP(state2, 7) ^ K1[7];
    }

    /* apply the Miyaguchi-Preneel compression function */
    hash[0] ^= state1[0];
    hash[1] ^= state1[1];
    hash[2] ^= state1[2];
    hash[3] ^= state1[3];
    hash[4] ^= state1[4];
    hash[5] ^= state1[5];
    hash[6] ^= state1[6];
    hash[7] ^= state1[7];
}