-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_elgamal.c
84 lines (48 loc) · 6.11 KB
/
main_elgamal.c
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
// Result c1 at 0000:0080 and c2 at 0000:0090
// Result message at 0000:0000
#include <8051.h>
#include "lib.h"
void encrypt(__xdata unsigned char *mt, __xdata unsigned char *gt, __xdata unsigned char *x, __xdata unsigned char *y, __xdata unsigned char *R, __xdata unsigned char *n, __xdata unsigned char *c1, __xdata unsigned char *c2) {
modexp(gt, y, c1, R);
modexp(c1, x, c2, R);
montpro(mt, c2, c2, n);
}
void decrypt(__xdata unsigned char *c1, __xdata unsigned char *c2, __xdata unsigned char *x, __xdata unsigned char *R, __xdata unsigned char *n, __xdata unsigned char *m) {
__xdata __at (0x280) unsigned char one[SIZE];
one[SIZE-1] = 0x01;
modexp(c1, x, c1, R);
montpro(c1, one, c1, n);
montinv(c1, n, c1);
montpro(c1, c2, m, n);
}
void main() {
__xdata __at (0x000) unsigned char m[SIZE] = {0xA1,0xB0,0x5A,0x36,0x72,0x2C,0x18,0xF0,0xFC,0x5E,0x1B,0x6C,0x79,0x28,0x5C,0xF3,0x3A,0xC0,0x1E,0x0E,0x64,0x99,0xFD,0xCF,0x1F,0x6F,0x4B,0xD3,0x3E,0xC2,0xC8,0x45,0x51,0x15,0xCE,0xE8,0xF9,0x1A,0x63,0x68,0x87,0x87,0xE1,0xBC,0x01,0xBF,0x70,0x20,0x1A,0xC8,0x4C,0x9D,0xEB,0xA9,0xCC,0xED,0x89,0x19,0x34,0xB7,0xEE,0x66,0xA1,0x9A,0xDB,0x6D,0xA3,0xBE,0xDB,0x15,0x8A,0x11,0x83,0x62,0x22,0x53,0x04,0xA1,0x00,0x51,0x26,0xE8,0xEE,0xD8,0x25,0x21,0x89,0xF2,0x67,0xC7,0x04,0x44,0x83,0x97,0x4E,0x34,0x21,0xFE,0x0E,0xFA,0xBB,0x6F,0x37,0x64,0x0A,0x26,0x30,0x05,0x7D,0xAC,0x1C,0x8E,0xA5,0x99,0x39,0xDF,0x5A,0xA9,0xE7,0x11,0x93,0xCE,0x87,0xBC,0x94,0x7B,0x58,0x5F};
__xdata __at (0x080) unsigned char n[SIZE] = {0xC8,0x63,0x16,0x32,0xFE,0x9E,0xA1,0x68,0x6F,0xCD,0x8F,0x50,0x48,0x55,0x58,0xB7,0xB5,0x64,0x0D,0x87,0xAC,0x6F,0xF8,0x00,0x9C,0x6B,0x1F,0x69,0x0B,0x54,0xCF,0x84,0x77,0xB0,0xF1,0xE8,0x17,0xE4,0x88,0x57,0x2D,0xB6,0x40,0xA9,0x9D,0xB8,0x1F,0x60,0xD9,0xE1,0x4B,0xDB,0x45,0x06,0x02,0x4F,0x5A,0x06,0xAD,0x1A,0xFD,0x45,0x8D,0x59,0x9D,0x79,0x46,0xDC,0xC7,0x97,0x02,0x50,0x1A,0x2C,0xF8,0x27,0xAD,0xEC,0x04,0xD7,0x01,0x8F,0x68,0x42,0x99,0xA3,0x4F,0xCF,0xA2,0xEB,0x19,0x41,0x50,0x79,0x69,0x76,0x47,0x8D,0x64,0x89,0x53,0x95,0x33,0x7A,0x13,0xB3,0x10,0xBF,0xF6,0x0A,0x1E,0xC2,0x50,0x38,0xC9,0x31,0x03,0x36,0x03,0x6C,0x2F,0x60,0xAA,0x9C,0xBA,0xC2,0x09,0x01};
__xdata __at (0x100) unsigned char R[SIZE] = { 0x37,0x9C,0xE9,0xCD,0x01,0x61,0x5E,0x97,0x90,0x32,0x70,0xAF,0xB7,0xAA,0xA7,0x48,0x4A,0x9B,0xF2,0x78,0x53,0x90,0x07,0xFF,0x63,0x94,0xE0,0x96,0xF4,0xAB,0x30,0x7B,0x88,0x4F,0x0E,0x17,0xE8,0x1B,0x77,0xA8,0xD2,0x49,0xBF,0x56,0x62,0x47,0xE0,0x9F,0x26,0x1E,0xB4,0x24,0xBA,0xF9,0xFD,0xB0,0xA5,0xF9,0x52,0xE5,0x02,0xBA,0x72,0xA6,0x62,0x86,0xB9,0x23,0x38,0x68,0xFD,0xAF,0xE5,0xD3,0x07,0xD8,0x52,0x13,0xFB,0x28,0xFE,0x70,0x97,0xBD,0x66,0x5C,0xB0,0x30,0x5D,0x14,0xE6,0xBE,0xAF,0x86,0x96,0x89,0xB8,0x72,0x9B,0x76,0xAC,0x6A,0xCC,0x85,0xEC,0x4C,0xEF,0x40,0x09,0xF5,0xE1,0x3D,0xAF,0xC7,0x36,0xCE,0xFC,0xC9,0xFC,0x93,0xD0,0x9F,0x55,0x63,0x45,0x3D,0xF6,0xFF};
__xdata __at (0x180) unsigned char R_2[SIZE] = {
0x6B,0xF5,0x1A,0xA2,0xD3,0x00,0x84,0x1E,0x7F,0xA9,0xFC,0x47,0xDF,0x78,0x68,0x4E,0xBC,0x4A,0xED,0x49,0x61,0x88,0x59,0x83,0x64,0x8D,0xE3,0x0F,0xC5,0x56,0xF6,0x08,0x89,0xEF,0xE9,0x31,0xCD,0x74,0xA3,0xFA,0x0F,0xF8,0x43,0x13,0xA5,0xB5,0x4F,0x13,0x37,0x1D,0x04,0x6F,0x6A,0x74,0x97,0x1B,0x30,0x58,0x47,0x2E,0x30,0x08,0x79,0xD8,0x51,0x45,0x64,0xE4,0x48,0x6C,0x42,0xF1,0x28,0xBC,0xD7,0xCC,0x2C,0x43,0xD7,0x35,0x29,0x53,0x86,0xED,0xB4,0x10,0x1C,0xC5,0xE3,0x70,0x1F,0xA2,0xE3,0x2F,0x8F,0xBC,0x25,0x76,0x73,0x0C,0xBA,0x05,0x3D,0xF8,0x11,0x1A,0xD5,0x14,0x52,0xE3,0xAE,0x4B,0xB2,0x8E,0xF3,0xE4,0x93,0x84,0x76,0x73,0x6E,0xF0,0x45,0xE2,0xA4,0x5A,0xE9,0x8A};
__xdata __at (0x200) unsigned char g[SIZE] = {
0xC2,0xCE,0x9A,0x67,0x9A,0x29,0xCF,0x15,0xBB,0x5B,0x59,0x43,0xD0,0x33,0x16,0x14,0x4E,0xB4,0x87,0xD8,0xBB,0xB8,0x2C,0xE0,0xD3,0x17,0x73,0xBD,0x1B,0x71,0xDA,0xC6,0x18,0xCC,0x48,0x73,0xC9,0xE9,0x5B,0xDA,0xE8,0xA5,0x29,0x41,0xCB,0xA4,0xE5,0x74,0x9C,0x9E,0xDD,0xE4,0xD2,0xD7,0x27,0xED,0xC8,0xD3,0xA6,0x47,0x19,0x60,0x34,0x28,0xB0,0x24,0xA5,0x78,0xBC,0xB1,0xF4,0x5D,0x12,0xF2,0xB1,0xF0,0x3E,0x68,0x30,0xFA,0x44,0xF8,0x3C,0x96,0x30,0x65,0x13,0x89,0xA7,0x7B,0x64,0xC5,0x2A,0x42,0x1B,0x51,0x4E,0xE1,0x6B,0x0C,0x4D,0xD7,0xE7,0x19,0x50,0x3C,0x49,0x3F,0xDE,0xA1,0xA5,0xC2,0x7E,0x0B,0x6E,0x17,0x30,0x79,0xFF,0x0D,0x6A,0x65,0xAE,0x27,0x9F,0x33,0x5B,0x29};
__xdata __at (0x280) unsigned char one[SIZE] = {0};
__xdata __at (0x300) unsigned char c1[SIZE];
__xdata __at (0x380) unsigned char c2[SIZE];
__xdata __at (0x400) unsigned char x[SIZE] = {0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x22,0x99,0x06,0x29,0xD7,0x60,0x8A,0xF4,0x75,0x30,0x1E,0xA6,0xCE,0x2B,0x27,0xBA};
__xdata __at (0x480) unsigned char y[SIZE] = {0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x2E,0x20,0x35,0xA4,0x49,0xDD,0x1C,0x08,0x5A,0x99,0xA4,0xED,0x11,0x04,0x3C};
__xdata __at (0x500) unsigned char mt[SIZE];
__xdata __at (0x580) unsigned char gt[SIZE];
one[SIZE-1] = 1;
montpro(m, R_2, mt, n);
montpro(g, R_2, gt, n);
encrypt(mt, gt, x, y, R, n, c1, c2);
montpro(c1, one, c1, n);
display_result();
montpro(c2, one, c2, n);
display_result();
/*P3 = 0x55;*/ /* Encryption done */
montpro(c1, R_2, c1, n);
montpro(c2, R_2, c2, n);
decrypt(c1, c2, x, R, n, m);
montpro(m, one, m, n);
display_result();
P3 = 0x55; /* Decryption done */
}