OpenCores
URL https://opencores.org/ocsvn/csa/csa/trunk

Subversion Repositories csa

[/] [csa/] [trunk/] [sw_sim/] [group_decrypt.c] - Blame information for rev 49

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 49 simon111
/*
2
 * =====================================================================================
3
 *
4
 *       Filename:  group_decrypt.c
5
 *
6
 *    Description:  test group decryp module
7
 *
8
 *        Version:  1.0
9
 *        Created:  04/25/2009 11:56:00 AM
10
 *       Revision:  none
11
 *       Compiler:  gcc
12
 *
13
 *         Author:  mengxipeng@gmail.com
14
 *
15
 * =====================================================================================
16
 */
17
 
18
#include <stdio.h>
19
#include <string.h>
20
#include "misc.h"
21
#include "csa.h"
22
 
23
void key_schedule(unsigned char *CK, int *kk) ;
24
void stream_cypher(int init, unsigned char *CK, unsigned char *sb, unsigned char *cb) ;
25
void block_decypher(int *kk, unsigned char *ib, unsigned char *bd) ;
26
 
27
int main()
28
{
29
        unsigned char ck[8];
30
        int kk[57];
31
        unsigned char encrypted[184];
32
        unsigned char decrypted[184];
33
        READ_DATA(ck,8);
34
        key_schedule(ck,kk);
35
        READ_DATA(encrypted,184);
36
        {
37
                int i,j,offset=0,N;
38
                unsigned char stream[8];
39
                unsigned char ib[8];
40
                unsigned char block[8];
41
                int residue;
42
 
43
                N = (184 - offset) / 8;
44
                residue = (184 - offset) % 8;
45
 
46
                /*  1st 8 bytes of initialisation */
47
                stream_cypher(1, ck, &encrypted[offset], ib);
48
 
49
 
50
                for(j=1; j<(N+1); j++) {
51
                        block_decypher(kk, ib, block);
52
                        DEBUG_OUTPUT_ARR(block,8);
53
 
54
                        if (j != N) {
55
                                stream_cypher(0, ck, NULL, stream);
56
 
57
                                /*  xor sb x stream */
58
                                for(i=0; i<8; i++)
59
                                        ib[i] = encrypted[offset+8*j+i] ^ stream[i];
60
                        }
61
                        else {
62
                                /*  last block - sb[N+1] = IV(initialisation vetor)(=0) */
63
                                for(i=0; i<8; i++)  ib[i] = 0;
64
                        }
65
 
66
                        /*  xor ib x block */
67
                        for(i=0; i<8; i++)
68
                                decrypted[offset+8*(j-1)+i] = ib[i] ^ block[i];
69
                        DEBUG_OUTPUT_ARR(&decrypted[offset+8*(j-1)+0],8);
70
                } /* for(j=1; j<(N+1); j++) */
71
 
72
                if (residue) {
73
                        stream_cypher(0, ck, NULL, stream);
74
                        for (i=0;i<residue;i++)
75
                                decrypted[184-residue+i] = encrypted[184-residue+i] ^ stream[i];
76
                }
77
        }
78
        WRITE_DATA(decrypted,184);
79
        return 0;
80
}
81
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.