1 | #include "crypto.h"
|
---|
2 |
|
---|
3 | #include "KISA_SHA256.h"
|
---|
4 |
|
---|
5 | #define ARIA_BLOCKSIZE 16
|
---|
6 |
|
---|
7 | crypto::crypto()
|
---|
8 | {
|
---|
9 | }
|
---|
10 |
|
---|
11 | void crypto::setKey(cf::bin & key)
|
---|
12 | {
|
---|
13 | CHEV_ARIA_Initialize(&info, CHEV_ARIA_256, key.buffer(), key.size());
|
---|
14 | }
|
---|
15 |
|
---|
16 | cf::bin crypto::sha256(cf::bin & input)
|
---|
17 | {
|
---|
18 | cf::bin out;
|
---|
19 |
|
---|
20 | out.resize(SHA256_DIGEST_VALUELEN);
|
---|
21 |
|
---|
22 | SHA256_Encrpyt(input.buffer(), input.size(), out.buffer());
|
---|
23 |
|
---|
24 | return out;
|
---|
25 | }
|
---|
26 |
|
---|
27 | cf::bin crypto::encrypt(cf::bin & input)
|
---|
28 | {
|
---|
29 | cf::bin plain;
|
---|
30 | cf::bin out;
|
---|
31 | int outSize;
|
---|
32 |
|
---|
33 | plain = input + getPaddingBytes(ARIA_BLOCKSIZE, input.size());
|
---|
34 |
|
---|
35 | out.resize(plain.size());
|
---|
36 |
|
---|
37 | CHEV_ARIA_Encrypt(&info, plain.buffer(), plain.size(), out.buffer(), &outSize);
|
---|
38 |
|
---|
39 | return out;
|
---|
40 | }
|
---|
41 |
|
---|
42 | cf::bin crypto::decrypt(cf::bin & input)
|
---|
43 | {
|
---|
44 | int plainSize;
|
---|
45 | int paddingSize;
|
---|
46 | cf::bin out;
|
---|
47 | int outSize;
|
---|
48 |
|
---|
49 | out.resize(input.size());
|
---|
50 |
|
---|
51 | CHEV_ARIA_Decrypt(&info, input.buffer(), input.size(), out.buffer(), &outSize);
|
---|
52 |
|
---|
53 | paddingSize = out[outSize - 1];
|
---|
54 |
|
---|
55 | plainSize = outSize - paddingSize;
|
---|
56 | out.resize(plainSize);
|
---|
57 |
|
---|
58 | return out;
|
---|
59 | }
|
---|
60 |
|
---|
61 | cf::bin crypto::getPaddingBytes(int blockSize, int inputSize)
|
---|
62 | {
|
---|
63 | int paddingSize = blockSize - (inputSize % blockSize);
|
---|
64 | cf::bin result;
|
---|
65 |
|
---|
66 | result.resize(paddingSize);
|
---|
67 |
|
---|
68 | memset(result.buffer(), paddingSize, paddingSize);
|
---|
69 |
|
---|
70 | return result;
|
---|
71 | }
|
---|