drygascon128_apb.h 3.3 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#ifndef __DRYGASCON128_APB_H__
#define __DRYGASCON128_APB_H__

#include <stdint.h>

typedef struct {
  volatile uint32_t CTRL;
  volatile uint32_t C;
  volatile uint32_t IO;
  volatile uint32_t X;
} Drygascon128_Regs;

#define DRYGASCON128_ROUNDS_O (0)
#define DRYGASCON128_DS_O (4)
#define DRYGASCON128_START_O (8)
#define DRYGASCON128_IDLE_O (31)

#define DRYGASCON128_START_MASK (1<<DRYGASCON128_START_O)
#define DRYGASCON128_IDLE_MASK (1<<DRYGASCON128_IDLE_O)

static unsigned int drygascon128hw_idle(Drygascon128_Regs *hw){
    return hw->CTRL & DRYGASCON128_IDLE_MASK;
}

static unsigned int drygascon128hw_wait_idle(Drygascon128_Regs *hw){
    while(!drygascon128hw_idle(hw));
}

static void drygascon128hw_start(Drygascon128_Regs *hw, unsigned int ds, unsigned int rounds){
    hw->CTRL = DRYGASCON128_START_MASK | (ds << DRYGASCON128_DS_O) | (rounds<<DRYGASCON128_ROUNDS_O);
}

static void drygascon128hw_set_x(Drygascon128_Regs *hw, const uint32_t *const x){
    hw->X = x[0];
    hw->X = x[1];
    hw->X = x[2];
    hw->X = x[3];
}

static void drygascon128hw_set_io(Drygascon128_Regs *hw, const uint32_t *const i){
    hw->IO = i[0];
    hw->IO = i[1];
    hw->IO = i[2];
    hw->IO = i[3];
}

static void drygascon128hw_get_io(Drygascon128_Regs *hw, uint32_t *const o){
    o[0] = hw->IO;
    o[1] = hw->IO;
    o[2] = hw->IO;
    o[3] = hw->IO;
}

static void drygascon128hw_set_c(Drygascon128_Regs *hw, const uint32_t *const c){
    for(unsigned int i=0;i<10;i++){
        hw->C = c[i];
    }
}

static void drygascon128hw_get_c(Drygascon128_Regs *hw, uint32_t *const c){
    for(unsigned int i=0;i<10;i++){
        c[i] = hw->C;
    }
}

static void drygascon128hw_g(Drygascon128_Regs *hw, uint32_t*const r, unsigned int rounds){
    drygascon128hw_start(hw, 0, rounds);
    drygascon128hw_wait_idle(hw);
    drygascon128hw_get_io(hw,r);
}

static void drygascon128hw_f(Drygascon128_Regs *hw, uint32_t*const r, const uint32_t*const in, uint32_t ds, unsigned int rounds){
    drygascon128hw_set_io(hw,in);
    drygascon128hw_start(hw, ds, rounds);
    drygascon128hw_wait_idle(hw);
    drygascon128hw_get_io(hw,r);
}

//return 0 if success or error code
static unsigned int drygascon128hw_test_ctrl(Drygascon128_Regs *hw){
    if(!drygascon128hw_idle(hw)) return 0x2;
    for(unsigned int i=0;i<256;i++){
        hw->CTRL = i;
        if(i!=(hw->CTRL & ~DRYGASCON128_IDLE_MASK)) return 0x01000000 | i;
    }
    hw->CTRL = 0xFFFFFE00;
    uint32_t z = hw->CTRL & ~DRYGASCON128_IDLE_MASK;
    if(z) return z;
    uint32_t p = 0;
    uint32_t q = 0;
    uint32_t c[10];
    for(unsigned int i=0;i<256;i++){
        for(unsigned int j=0;j<10;j++){
            p = i*16 + j;
            c[j] = p;
        }
        drygascon128hw_set_c(hw,c);
        drygascon128hw_get_c(hw,c);
        for(unsigned int j=0;j<10;j++){
            q = i*16 + j;
            if(c[j] != q) return 0x02000000 | i;
        }
    }
    for(unsigned int i=0;i<256;i++){
        for(unsigned int j=0;j<4;j++){
            p = i*16 + j;
            c[j] = p;
        }
        drygascon128hw_set_io(hw,c);
        drygascon128hw_get_io(hw,c);
        for(unsigned int j=0;j<4;j++){
            q = i*16 + j;
            if(c[j] != q) return 0x03000000 | i;
        }
    }
    //purge the internal state
    drygascon128hw_start(hw, 0, 1);
    drygascon128hw_wait_idle(hw);
    return 0;
}
#endif