summaryrefslogtreecommitdiff
path: root/cp/secu/src/secu_p_run.c
blob: 39616a5aefc5fb312d05b0dba0a7bc0c8af395ae (plain)
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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp/secu/secu_lib.c
 * \brief   all protocol run related function
 * \ingroup cp_secu
 *
 */

#include "common/std.h"
#include "cp/secu/inc/secu_p_run.h"

static protocol_run_t m_p_run[MAX_STA_NB];    // variables du protocol run


void
secu_p_run_init(void)
{
    memset(m_p_run, 0, sizeof(m_p_run));
}

void 
secu_gen_nonce (const tei_t tei)
{
    u8 nonce[AES_KEY_SIZE];

    dbg_assert( tei != 0 && tei != 0xFF);

    secu_gen_aes_key (nonce);
    memcpy(&m_p_run[tei].nonce, nonce, sizeof(m_p_run[tei].nonce));
}



void 
secu_start_new_protocol_run(const tei_t tei, const pid_t pid, protocol_run_t *p_run)
{
    u8 rand_num[AES_KEY_SIZE];

    dbg_assert( tei != 0 && tei != 0xFF);
    dbg_assert (p_run);

    m_p_run[tei].pid = pid; // Protocol ID

    secu_gen_aes_key(rand_num);
    memcpy(&m_p_run[tei].prn, rand_num, sizeof(m_p_run[tei].prn));

    m_p_run[tei].pmn = 0; // Protocol Message Number

    memcpy(p_run, &m_p_run[tei], sizeof(p_run));
}

E_ErrCode 
secu_check_protocol_run_param (const tei_t tei, const protocol_run_t to_check)
{
    dbg_assert( tei != 0 && tei != 0xFF);

    // check the global parameters
    if (to_check.pid > HLE_PRO)
        return PID_Unknown;
    // check parameters specific to this run
    if(to_check.nonce != m_p_run[tei].nonce)
    {
        return NONCE_INCORRECT;
    }
    if (to_check.pmn == 1)
    {
        // we start a run, so initialize station context
        memcpy (&m_p_run[tei], &to_check, sizeof(protocol_run_t));
        return Success;
    }
    else
    {
        if (to_check.pid != m_p_run[tei].pid)
            return PID_Wrong;
        if (to_check.prn != m_p_run[tei].prn)
            return PRN_Wrong;
        if (to_check.pmn == PMN_LAST_MSG)
        {
            // the run is over
            m_p_run[tei].pmn = PMN_LAST_MSG;
            m_p_run[tei].prn = 0;
        }
        else if (to_check.pmn != ++m_p_run[tei].pmn)
            return PMN_Wrong;
        return Success;
    }
}

E_ErrCode
secu_gen_protocol_run_param (const tei_t tei, const bool last_msg, protocol_run_t *gen)
{
    dbg_assert( tei != 0 && tei != 0xFF);
    dbg_assert (gen);

    if (last_msg)
        m_p_run[tei].pmn = PMN_LAST_MSG;
    else
    {
        if ( (m_p_run[tei].pmn == PMN_LAST_MSG)
           || (m_p_run[tei].pmn == PMN_LAST_MSG - 1) )
        {
            return PRN_NotInitialised;
        }
        m_p_run[tei].pmn ++;
    }
    memcpy (gen, &m_p_run[tei], sizeof(protocol_run_t));
    return Success;
}