Exemplo n.º 1
0
void speck128_encryptx(
    const void *key, 
    void *in)
{
  uint64_t i, t, k0, k1, k2, k3, x0, x1;
  
  // copy 256-bit key to local space
  k0 = ((uint64_t*)key)[0];
  k1 = ((uint64_t*)key)[1];
  k2 = ((uint64_t*)key)[2];
  k3 = ((uint64_t*)key)[3];
  
  // copy input to local space
  x0 = ((uint64_t*)in)[0];
  x1 = ((uint64_t*)in)[1];
  
  for (i=0; i<34; i++)
  {
    // encrypt block
    x1 = (ROTR64(x1, 8) + x0) ^ k0;
    x0 =  ROTL64(x0, 3) ^ x1;
    
    // create next subkey
    k1 = (ROTR64(k1, 8) + k0) ^ i;
    k0 =  ROTL64(k0, 3) ^ k1;

    XCHG(k3, k2, t);
    XCHG(k3, k1, t);    
  }
  // save result
  ((uint64_t*)in)[0] = x0;
  ((uint64_t*)in)[1] = x1;
}
Exemplo n.º 2
0
void speck128_encryptx(const void *key, void *in)
{
  uint32_t i, t, k0, k1, k2, k3, x0, x1;
  bc_blk   *x=(bc_blk*)in;
  bc_blk   *k=(bc_blk*)key;
  
  // copy 128-bit key to local registers
  k0 = k->w[0]; k1 = k->w[1];
  k2 = k->w[2]; k3 = k->w[3];
  
  // copy M to local space
  x0 = x->w[0]; x1 = x->w[1];
  
  for (i=0; i<27; i++)
  {
    // encrypt block
    x0 = (ROTR32(x0, 8) + x1) ^ k0;
    x1 =  ROTL32(x1, 3) ^ x0;
    
    // create next subkey
    k1 = (ROTR32(k1, 8) + k0) ^ i;
    k0 =  ROTL32(k0, 3) ^ k1;
    
    XCHG(k3, k2, t);
    XCHG(k3, k1, t);    
  }
  // save result
  x->w[0] = x0; x->w[1] = x1;
}
Exemplo n.º 3
0
void speck32_encryptx(const void *key, void *data)
{
    uint16_t i, t, k0, k1, k2, k3, x0, x1;
    uint16_t *k=(uint16_t*)key;
    uint16_t *x=(uint16_t*)data;
    
    // copy 128-bit key to local space
    k0 = k[0]; k1 = k[1];
    k2 = k[2]; k3 = k[3];
    
    // copy input to local space
    x0 = x[0]; x1 = x[1];
    
    for (i=0; i<22; i++)
    {
      // encrypt block
      x0 = (ROTR16(x0, 7) + x1) ^ k0;
      x1 =  ROTL16(x1, 2) ^ x0;
      
      // create next subkey
      k1 = (ROTR16(k1, 7) + k0) ^ i;
      k0 =  ROTL16(k0, 2) ^ k1;
      
      XCHG(k3, k2);
      XCHG(k3, k1);    
    }
    // save result
    x[0] = x0; x[1] = x1;
}
Exemplo n.º 4
0
void speck128_setkey(
    const void *in, 
    void *out)
{
  uint64_t i, t, k0, k1, k2, k3;

  // copy 256-bit key to local space
  k0 = ((uint64_t*)in)[0];
  k1 = ((uint64_t*)in)[1];
  k2 = ((uint64_t*)in)[2];
  k3 = ((uint64_t*)in)[3];

  // expand 256-bit key into round keys
  for (i=0; i<34; i++)
  {
    ((uint64_t*)out)[i] = k0;
    
    k1 = (ROTR64(k1, 8) + k0) ^ i;
    k0 = ROTL64(k0,  3) ^ k1;
    
    // rotate left 64-bits
    XCHG(k3, k2, t);
    XCHG(k3, k1, t);
  }
}
Exemplo n.º 5
0
void xoodoo(void *state) {
    uint32_t e[4], a0, a1, a2, a3, t;
    int      i, x, y, j;
    uint32_t *p, *q;
    uint32_t (*A)[4] = (uint32_t(*)[4])state; 
    
    p=(uint32_t*)A[0];
      
    // 12 rounds by default
    for (i=0; i<12; i++) {
      // θ
      for (x=0; x<4; x++) {
        e[x] = ROTR32(p[x] ^ p[x+4] ^ p[x+8], 18);
        e[x]^= ROTR32(e[x], 9);
      }

      for (x=0; x<12; x++) {
        p[x] ^= e[(x - 1) & 3];
      }

      // ρ_west
      XCHG(p[7], p[4]);
      XCHG(p[7], p[5]);
      XCHG(p[7], p[6]);

      for (x=0; x<4; x++) {
        p[x+8] = ROTR32(p[x+8], 21); 
      }
  
      // ι
      p[0] ^= (uint32_t)rc[i];

      // χ
      for (x=0; x<4; x++) {
        a0 = p[x+0];
        a1 = p[x+4];
        a2 = p[x+8];

        p[x+0] ^= ~a1 & a2;        
        p[x+4] ^= ~a2 & a0;        
        p[x+8] ^= ~a0 & a1;                
      }

      // ******
      // ρ_east
      // ******
      for (x=0;x<4; x++) {       
        p[x+4] = ROTR32(p[x+4], 31); 
        p[x+8] = ROTR32(p[x+8], 24);
      } 
      XCHG(p[8], p[10]);
      XCHG(p[9], p[11]); 
    }
}
Exemplo n.º 6
0
static void transpose( uint8_t *buf, int w )
{
    int i, j;
    for( i = 0; i < w; i++ )
        for( j = 0; j < i; j++ )
            XCHG( uint8_t, buf[w*i+j], buf[w*j+i] );
}
Exemplo n.º 7
0
static int read_frame( cli_pic_t *p_pic, hnd_t handle, int i_frame )
{
    thread_hnd_t *h = handle;
    int ret = 0;

    if( h->next_frame >= 0 )
    {
        x264_threadpool_wait( h->pool, h->next_args );
        ret |= h->next_args->status;
    }

    if( h->next_frame == i_frame )
        XCHG( cli_pic_t, *p_pic, h->pic );
    else
    {
        if( h->next_frame >= 0 )
            thread_input.release_frame( &h->pic, handle );
        ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );
    }

    if( !h->frame_total || i_frame+1 < h->frame_total )
    {
        h->next_frame =
            h->next_args->i_frame = i_frame+1;
        h->next_args->pic = &h->pic;
        x264_threadpool_run( h->pool, (void*)read_frame_thread_int, h->next_args );
    }
    else
        h->next_frame = -1;

    return ret;
}
Exemplo n.º 8
0
static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
{
    thread_hnd_t *h = handle;
    int ret = 0;

    if( h->next_frame >= 0 )
    {
        x264_pthread_join( h->tid, NULL );
        ret |= h->next_args->status;
        h->in_progress = 0;
    }

    if( h->next_frame == i_frame )
        XCHG( x264_picture_t, *p_pic, h->pic );
    else
        ret |= h->input.read_frame( p_pic, h->p_handle, i_frame );

    if( !h->frame_total || i_frame+1 < h->frame_total )
    {
        h->next_frame =
        h->next_args->i_frame = i_frame+1;
        h->next_args->pic = &h->pic;
        if( x264_pthread_create( &h->tid, NULL, (void*)read_frame_thread_int, h->next_args ) )
            return -1;
        h->in_progress = 1;
    }
    else
        h->next_frame = -1;

    return ret;
}
Exemplo n.º 9
0
void speck32_setkey(const void *in, void *out)
{
    uint16_t i, t, k0, k1, k2, k3;
    uint16_t *k=(uint16_t*)in;
    uint16_t *ks=(uint16_t*)out;
    
    // copy 64-bit key to local space
    k0 = k[0]; k1 = k[1];
    k2 = k[2]; k3 = k[3];

    // expand 64-bit key into round keys
    for (i=0; i<22; i++)
    {
      ks[i] = k0;
      
      k1 = (ROTR16(k1, 7) + k0) ^ i;
      k0 = ROTL16(k0, 2) ^ k1;
      
      // rotate left 32-bits
      XCHG(k3, k2);
      XCHG(k3, k1);
    }
}
Exemplo n.º 10
0
void rc5_encryptx(void *key, void *data)
{
    uint32_t A, B, t;
    uint32_t L[4], S[RC5_KR];
    int      i, j,r;    
    
    uint32_t *k=(uint32_t*)key;
    uint32_t *x=(uint32_t*)data;
    
    // initialize L with 128-bit key
    memcpy(&L, key, 16);

    A = 0xC983AE2D; //RC5_P;
    
    // initialize S with constants
    for (i=RC5_KR-1; i>=0; i--) {
      A -= RC5_Q;     
      S[i] = A;
    }
    
    A = B = 0;
    r = (RC5_KR*3) - 1;
    
    // create subkeys
    for (i=0, j=0; r>0; i++, j++, r--) {
      // i needs reset?
      if (i==RC5_KR) i=0;
      
      A = S[i%RC5_KR] = ROTR32(S[i%RC5_KR] + A+B, 32-3);  
      B = L[j%4] = ROTR32(L[j%4] + A+B, 32-U8V(A+B));
    }
    
    // assign subkeys to k ptr 
    k = S;
    
    // load plaintext
    A = x[0] + *k; k++; 
    B = x[1] + *k; k++;

    // apply encryption
    for (i=RC5_KR-2; i>0; i--) {
      // original spec uses ROTL32
      A = ROTR32(A ^ B, 32 - U8V(B)) + *k; k++;   
      // rotate
      XCHG(A, B);
    }
    // save
    x[0] = A; x[1] = B;
}
Exemplo n.º 11
0
void gimlix(void *state)
{
    int      r, j;
    uint32_t t, x, y, z;
    uint32_t *s=(uint32_t*)state;

    for (r=24; r>0; --r) {
      // apply SP-box
      for (j=0; j<4; j++) {
        x = ROTR32(s[    j],  8);
        y = ROTR32(s[4 + j], 23);
        z =        s[8 + j];

        s[8 + j] = x ^ (z << 1) ^ ((y & z) << 2);
        s[4 + j] = y ^ x        ^ ((x | z) << 1);
        s[j]     = z ^ y        ^ ((x & y) << 3);
      }

      // apply Linear layer
      t = r & 3;

      // if zero, do small swap
      if (t == 0) {
        XCHG(s[0], s[1]);
        XCHG(s[2], s[3]);

        // add constant
        s[0] ^= (0x9e377900 | r);
      }
      // if 2, do big swap
      if (t == 2) {
        XCHG(s[0], s[2]);
        XCHG(s[1], s[3]);
      }
    }
}
Exemplo n.º 12
0
static void
p_sort_(uint* base, uint* l, uint* h)
{
  uint* i;
  for(i = l; i < h; i++)
  {  
    uint* elm = base + *(i);

    uint* j;
    for(j = i+1; j < h; j++)
    {
      if(*elm > *(base + *(j)))
      {
        XCHG(*(i), *(j));
        elm = base + *(i);
      }
    }
  }
}
Exemplo n.º 13
0
static int read_frame_internal( cli_pic_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
{
    if( h->first_pic && !info )
    {
        /* see if the frame we are requesting is the frame we have already read and stored.
         * if so, retrieve the pts and image data before freeing it. */
        if( !i_frame )
        {
            XCHG( cli_image_t, p_pic->img, h->first_pic->img );
            p_pic->pts = h->first_pic->pts;
            XCHG( void*, p_pic->opaque, h->first_pic->opaque );
        }
        lavf_input.release_frame( h->first_pic, NULL );
        lavf_input.picture_clean( h->first_pic );
        free( h->first_pic );
        h->first_pic = NULL;
        if( !i_frame )
            return 0;
    }
Exemplo n.º 14
0
static void __chfsnprb_tree_insert_color(CHFSNPRB_POOL *pool, const uint32_t node_pos, uint32_t *root_pos)
{
    CHFSNPRB_NODE *node;
    CHFSNPRB_NODE *root;
    CHFSNPRB_NODE *parent;    
    
    uint32_t  node_pos_t;

    node_pos_t = node_pos;
    node  = CHFSNPRB_POOL_NODE(pool, node_pos_t);

    while (NULL_PTR != (parent = CHFSNPRB_POOL_NODE(pool, CHFSNPRB_NODE_PARENT_POS(node))) /*parent is valid*/
         && CHFSNPRB_RED == CHFSNPRB_NODE_COLOR(parent))
    {
        uint32_t  parent_pos;
        uint32_t  gparent_pos;
        CHFSNPRB_NODE *gparent;

        parent_pos = CHFSNPRB_NODE_PARENT_POS(node);

        gparent_pos = CHFSNPRB_NODE_PARENT_POS(parent);
        ASSERT(CHFSNPRB_ERR_POS != gparent_pos);
        gparent = CHFSNPRB_POOL_NODE(pool, gparent_pos);

        if (parent_pos == CHFSNPRB_NODE_LEFT_POS(gparent))
        {
            {
                CHFSNPRB_NODE *uncle;
                if (NULL_PTR != (uncle = CHFSNPRB_POOL_NODE(pool, CHFSNPRB_NODE_RIGHT_POS(gparent))) /*uncle is valid*/
                   && CHFSNPRB_RED == CHFSNPRB_NODE_COLOR(uncle))
                {
                    CHFSNPRB_NODE_COLOR(uncle)   = CHFSNPRB_BLACK;
                    CHFSNPRB_NODE_COLOR(parent)  = CHFSNPRB_BLACK;
                    CHFSNPRB_NODE_COLOR(gparent) = CHFSNPRB_RED;
                    
                    node = gparent;
                    node_pos_t = gparent_pos;
                    continue;
                }
            }

            if (CHFSNPRB_NODE_RIGHT_POS(parent) == node_pos_t)
            {
                __chfsnprb_tree_rotate_left(pool, parent_pos, root_pos);
                XCHG(CHFSNPRB_NODE *, parent, node);
                XCHG(uint32_t, parent_pos, node_pos_t);
            }

            CHFSNPRB_NODE_COLOR(parent)  = CHFSNPRB_BLACK;
            CHFSNPRB_NODE_COLOR(gparent) = CHFSNPRB_RED;
            __chfsnprb_tree_rotate_right(pool, gparent_pos, root_pos);
         }
         else 
         {        
            {
                CHFSNPRB_NODE *uncle;
                if (NULL_PTR != (uncle = CHFSNPRB_POOL_NODE(pool, CHFSNPRB_NODE_LEFT_POS(gparent))) /*uncle is valid*/
                    && CHFSNPRB_RED == CHFSNPRB_NODE_COLOR(uncle))
                {
                    CHFSNPRB_NODE_COLOR(uncle)   = CHFSNPRB_BLACK;
                    CHFSNPRB_NODE_COLOR(parent)  = CHFSNPRB_BLACK;
                    CHFSNPRB_NODE_COLOR(gparent) = CHFSNPRB_RED;
                    
                    node = gparent;
                    node_pos_t = gparent_pos;
                    continue;
                }
            }

            if (CHFSNPRB_NODE_LEFT_POS(parent) == node_pos_t)
            {
                __chfsnprb_tree_rotate_right(pool, parent_pos, root_pos);
                XCHG(CHFSNPRB_NODE *, parent, node);
                XCHG(uint32_t, parent_pos, node_pos_t);
            }

            CHFSNPRB_NODE_COLOR(parent)  = CHFSNPRB_BLACK;
            CHFSNPRB_NODE_COLOR(gparent) = CHFSNPRB_RED;
            __chfsnprb_tree_rotate_left(pool, gparent_pos, root_pos);
        }
    }

    root = CHFSNPRB_POOL_NODE(pool, *root_pos);
    CHFSNPRB_NODE_COLOR(root) = CHFSNPRB_BLACK;
    return;
}
Exemplo n.º 15
0
Arquivo: lavf.c Projeto: tufei/x264c64
static int read_frame_internal( x264_picture_t *p_pic, lavf_hnd_t *h, int i_frame, video_info_t *info )
{
    if( h->first_pic && !info )
    {
        /* see if the frame we are requesting is the frame we have already read and stored.
         * if so, retrieve the pts and image data before freeing it. */
        if( !i_frame )
        {
            XCHG( x264_image_t, p_pic->img, h->first_pic->img );
            p_pic->i_pts = h->first_pic->i_pts;
        }
        lavf_input.picture_clean( h->first_pic );
        free( h->first_pic );
        h->first_pic = NULL;
        if( !i_frame )
            return 0;
    }

    AVCodecContext *c = h->lavf->streams[h->stream_id]->codec;
    lavf_pic_t *pic_h = p_pic->opaque;
    AVPacket *pkt = &pic_h->packet;
    AVFrame *frame = &pic_h->frame;

    while( i_frame >= h->next_frame )
    {
        int finished = 0;
        while( !finished && av_read_frame( h->lavf, pkt ) >= 0 )
            if( pkt->stream_index == h->stream_id )
            {
                c->reordered_opaque = pkt->pts;
                if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
                    fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
            }
        if( !finished )
        {
            if( avcodec_decode_video2( c, frame, &finished, pkt ) < 0 )
                fprintf( stderr, "lavf [warning]: video decoding failed on frame %d\n", h->next_frame );
            if( !finished )
                return -1;
        }
        h->next_frame++;
    }

    if( check_swscale( h, c, i_frame ) )
        return -1;
    /* FIXME: avoid sws_scale where possible (no colorspace conversion). */
    sws_scale( h->scaler, frame->data, frame->linesize, 0, c->height, p_pic->img.plane, p_pic->img.i_stride );

    if( info )
        info->interlaced = frame->interlaced_frame;

    if( h->vfr_input )
    {
        p_pic->i_pts = 0;
        if( frame->reordered_opaque != AV_NOPTS_VALUE )
            p_pic->i_pts = frame->reordered_opaque;
        else if( pkt->dts != AV_NOPTS_VALUE )
            p_pic->i_pts = pkt->dts; // for AVI files
        else if( info )
        {
            h->vfr_input = info->vfr = 0;
            goto exit;
        }
        if( !h->pts_offset_flag )
        {
            h->pts_offset = p_pic->i_pts;
            h->pts_offset_flag = 1;
        }
        p_pic->i_pts -= h->pts_offset;
    }

exit:
    if( pkt->destruct )
        pkt->destruct( pkt );
    avcodec_get_frame_defaults( frame );
    return 0;
}