LEA Cipher 암호화/복호화 과정 이해하기
블록암호 LEA(Lightweight Encryption Algorithm)는 128비트 데이터 블록을 암호화하는 알고리즘으로 128, 192, 256비트 비밀키를 사용할 수 있으며 요구되는 안전성 기준에 따라 용도가 구분될 수 있다 1. LEA ��
cryptosecurity.tistory.com
LEA Cipher 암호화와 복호화 규격과 구조, 과정을 이해하고 보시길 바랍니다.
LEA.h
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#ifndef _LEA_H_
#define _LEA_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE; //1byte
typedef unsigned long int WORD; //4byte
int ROL(int i, WORD value);
int ROR(int i, WORD value);
void KeySchedule_128(BYTE *K, WORD *RK);
void KeySchedule_192(BYTE *K, WORD *RK);
void KeySchedule_256(BYTE *K, WORD *RK);
void Encrypt(int Nr, WORD *RK, BYTE *P, BYTE *C);
void Decrypt(int Nr, WORD *RK, BYTE *D, BYTE *C);
#else
#endif
|
cs |
LEA.cpp
delta 상수 정하기
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#include "LEA.h"
WORD delta[8] = {
0xc3efe9db,
0x44626b02,
0x79e27c8a,
0x78df30ec,
0x715ea49e,
0xc785da0a,
0xe04ef22a,
0xe5c40957
};
|
cs |
Rotate Left & Rotate Right 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
|
//32비트 비트열 x의 i비트 좌측 순환이동
int ROL(int i, WORD value)
{
return (value << i) | (value >> (32 - i));
}
//32비트 비트열 x의 i비트 우측 순환이동
int ROR(int i, WORD value)
{
return (value >> i) | (value << (32 - i));
}
|
cs |
LEA-128 키 스케줄링 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
void KeySchedule_128(BYTE *K, WORD *RK)
{
WORD T[4];
memcpy(T, K, 16); //8*16 = 128
int i;
for (i = 0; i < 24; i++)
{
T[0] = ROL(1, T[0] + ROL(i, delta[i % 4]));
T[1] = ROL(3, T[1] + ROL(i + 1, delta[i % 4]));
T[2] = ROL(6, T[2] + ROL(i + 2, delta[i % 4]));
T[3] = ROL(11, T[3] + ROL(i + 3, delta[i % 4]));
RK[i * 6 + 0] = T[0];
RK[i * 6 + 1] = T[1];
RK[i * 6 + 2] = T[2];
RK[i * 6 + 3] = T[1];
RK[i * 6 + 4] = T[3];
RK[i * 6 + 5] = T[1];
}
}
|
cs |
LEA-192 키 스케줄링 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void KeySchedule_192(BYTE *K, WORD *RK)
{
WORD T[6];
memcpy(T, K, 24); //8*24 = 192
int i;
for (i = 0; i < 28; i++)
{
T[0] = ROL(1, T[0] + ROL(i, delta[i % 6]));
T[1] = ROL(3, T[1] + ROL(i + 1, delta[i % 6]));
T[2] = ROL(6, T[2] + ROL(i + 2, delta[i % 6]));
T[3] = ROL(11, T[3] + ROL(i + 3, delta[i % 6]));
T[4] = ROL(13, T[4] + ROL(i + 4, delta[i % 6]));
T[5] = ROL(17, T[5] + ROL(i + 5, delta[i % 6]));
RK[i * 6 + 0] = T[0];
RK[i * 6 + 1] = T[1];
RK[i * 6 + 2] = T[2];
RK[i * 6 + 3] = T[3];
RK[i * 6 + 4] = T[4];
RK[i * 6 + 5] = T[5];
}
}
|
cs |
LEA-256 키 스케줄링 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void KeySchedule_256(BYTE *K, WORD *RK)
{
WORD T[8];
memcpy(T, K, 32); //8*32 = 256
int i;
for (i = 0; i < 32; i++)
{
T[(6 * i + 0) % 8] = ROL(1, T[(6 * i + 0) % 8] + ROL(i, delta[i % 8]));
T[(6 * i + 1) % 8] = ROL(3, T[(6 * i + 1) % 8] + ROL(i + 1, delta[i % 8]));
T[(6 * i + 2) % 8] = ROL(6, T[(6 * i + 2) % 8] + ROL(i + 2, delta[i % 8]));
T[(6 * i + 3) % 8] = ROL(11, T[(6 * i + 3) % 8] + ROL(i + 3, delta[i % 8]));
T[(6 * i + 4) % 8] = ROL(13, T[(6 * i + 4) % 8] + ROL(i + 4, delta[i % 8]));
T[(6 * i + 5) % 8] = ROL(17, T[(6 * i + 5) % 8] + ROL(i + 5, delta[i % 8]));
RK[i * 6 + 0] = T[(i * 6 + 0) % 8];
RK[i * 6 + 1] = T[(i * 6 + 1) % 8];
RK[i * 6 + 2] = T[(i * 6 + 2) % 8];
RK[i * 6 + 3] = T[(i * 6 + 3) % 8];
RK[i * 6 + 4] = T[(i * 6 + 4) % 8];
RK[i * 6 + 5] = T[(i * 6 + 5) % 8];
}
}
|
cs |
LEA 암호화 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void Encrypt(int Nr, WORD *RK, BYTE *P, BYTE *C)
{
WORD X_Round[4]; //32 * 4 =128BIT
WORD X_NextRound[4];
memcpy(X_Round, P, 16);
int i, j, k;
for (i = 0; i < Nr; i++)
{
X_NextRound[0] = ROL(9, (X_Round[0] ^ RK[i * 6 + 0]) + (X_Round[1] ^ RK[i * 6 + 1]));
X_NextRound[1] = ROR(5, (X_Round[1] ^ RK[i * 6 + 2]) + (X_Round[2] ^ RK[i * 6 + 3]));
X_NextRound[2] = ROR(3, (X_Round[2] ^ RK[i * 6 + 4]) + (X_Round[3] ^ RK[i * 6 + 5]));
X_NextRound[3] = X_Round[0];
memcpy(X_Round, X_NextRound, 16);
}
memcpy(C, X_NextRound, 16);
}
|
cs |
LEA 복호화 함수 구현
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void Decrypt(int Nr, WORD *RK, BYTE *D, BYTE *C)
{
WORD X_Round[4];
WORD X_NextRound[4];
memcpy(X_Round, C, 16);
int i;
for (i = 0; i < Nr; i++)
{
X_NextRound[0] = X_Round[3];
X_NextRound[1] = (ROR(9, X_Round[0]) - (X_NextRound[0] ^ RK[((Nr - i - 1) * 6) + 0])) ^ RK[((Nr - i - 1) * 6) + 1];
X_NextRound[2] = (ROL(5, X_Round[1]) - (X_NextRound[1] ^ RK[((Nr - i - 1) * 6) + 2])) ^ RK[((Nr - i - 1) * 6) + 3];
X_NextRound[3] = (ROL(3, X_Round[2]) - (X_NextRound[2] ^ RK[((Nr - i - 1) * 6) + 4])) ^ RK[((Nr - i - 1) * 6) + 5];
memcpy(X_Round, X_NextRound, 16);
}
memcpy(D, X_Round, 16);
}
|
cs |
main.cpp
LEA 암호화 복호화 테스트 해보기
1. LEA-128 암호화, 복호화 테스트하기
Nb : 16, Nk : 16, Nr : 24
Key : 0f 1e 2d 3c 4b 5a 69 78 87 96 a5 b4 c3 d2 e1 f0
Plaintext : 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
다음의 Key로 Plaintext를 암호화 해서 Ciphertext를 구하고, 다시 Ciphertext를 복호화해서 Plaintext를 구해보자.
|
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
|
#include "LEA.h"
int main()
{
int i;
BYTE K[16] =
{
0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0
};
BYTE P[16] =
{
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
WORD RoundKey[144] = { 0, };
BYTE C[16] = { 0 };
BYTE D[16] = { 0 };
KeySchedule_128(K, RoundKey);
printf("Key : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", K[i]);
}
printf("\n\n");
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", P[i]);
}
printf("\n\n");
Encrypt(24, RoundKey, P, C);
printf("Ciphertext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", C[i]);
}
printf("\n\n");
Decrypt(24, RoundKey, D, C);
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", D[i]);
}
printf("\n");
return 0;
}
|
cs |
==실행결과==

2. LEA-192 암호화 복호화 테스트하기
Nb : 16, Nk : 24, Nr : 28
Key : 0f 1e 2d 3c 4b 5a 69 78 87 96 a5 b4 c3 d2 e1 f0 f0 e1 d2 c3 b4 a5 96 87
Plaintext : 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
다음의 Key로 Plaintext를 암호화 해서 Ciphertext를 구하고, 다시 Ciphertext를 복호화해서 Plaintext를 구해보자.
|
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
|
#include "lea.h"
int main()
{
int i;
BYTE K[24] =
{
0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87
};
BYTE P[16] =
{
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
};
WORD RoundKey[168] = { 0, };
BYTE C[16] = { 0 };
BYTE D[16] = { 0 };
KeySchedule_192(K, RoundKey);
printf("Key : ");
for (i = 0; i < 24; i++)
{
printf("0x%02x ", K[i]);
}
printf("\n\n");
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", P[i]);
}
printf("\n\n");
Encrypt(28, RoundKey, P, C);
printf("Ciphertext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", C[i]);
}
printf("\n\n");
Decrypt(28, RoundKey, D, C);
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", D[i]);
}
printf("\n");
return 0;
}
|
cs |
==실행결과==

3. LEA-256 암호화, 복호화 테스트하기
Nb : 16, Nk : 32, Nr : 32
Key : 0f 1e 2d 3c 4b 5a 69 78 87 96 a5 b4 c3 d2 e1 f0 f0 e1 d2 c3 b4 a5 96 87
Plaintext : 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
다음의 Key로 Plaintext를 암호화 해서 Ciphertext를 구하고, 다시 Ciphertext를 복호화해서 Plaintext를 구해보자.
|
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
|
#include "lea.h"
int main()
{
int i;
BYTE K[32] =
{
0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f
};
BYTE P[16] =
{
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
};
WORD RoundKey[192] = { 0, };
BYTE C[16] = { 0 };
BYTE D[16] = { 0 };
KeySchedule_256(K, RoundKey);
printf("Key : ");
for (i = 0; i < 32; i++)
{
printf("0x%02x ", K[i]);
}
printf("\n\n");
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", P[i]);
}
printf("\n\n");
Encrypt(32, RoundKey, P, C);
printf("Ciphertext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", C[i]);
}
printf("\n\n");
Decrypt(32, RoundKey, D, C);
printf("Plaintext : ");
for (i = 0; i < 16; i++)
{
printf("0x%02x ", D[i]);
}
printf("\n");
return 0;
}
|
cs |
==실행결과==

LEA 암호화, 복호화 전체 c언어 코드 보기
'Cipher analysis > LEA' 카테고리의 다른 글
| LEA Cipher 암호화/복호화 과정 이해하기 (0) | 2020.10.14 |
|---|

























