C
parameter 실수
GunwooYun
2022. 11. 8. 22:32
먼저 작성한 코드를 보자.
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
|
U2 ARIA_Enc_Update(IN U1 padding_flag, IN U1 *plain_text, IN U4 plain_len, OUT U1 *cipher, OUT U4 *cipher_len)
{
U2 ret = 0x0000;
U4 outl = 0;
U1 *cipher_buf = NULL;
U4 cipher_buf_len = 0;
int nBytesWritten = 0;
ret = EVP_CIPHER_CTX_set_padding(evp_ctx_enc, padding_flag);
if(!ret)
{
printf("EVP_CIPHER_CTX_set_padding ERROR\n");
return 0xffff;
}
cipher_buf_len = plain_len + EVP_CIPHER_CTX_block_size(evp_ctx_enc);
cipher_buf = (U1 *)malloc(cipher_buf_len);
if(cipher_buf == NULL)
{
printf("cipher buf malloc failed\n");
return 0xffff;
}
EVP_EncryptUpdate(evp_ctx_enc, &cipher_buf[outl], &nBytesWritten, plain_text, plain_len);
outl += nBytesWritten;
EVP_EncryptFinal(evp_ctx_enc, &cipher_buf[outl], &nBytesWritten);
outl += nBytesWritten;
//cipher = cipher_buf;
memcpy(cipher, cipher_buf, outl);
*cipher_len = outl;
/*
for(int i = 0; i < outl; i++)
printf("%#x ", cipher_buf[i]);
*/
EVP_CIPHER_CTX_free(evp_ctx_enc);
free(cipher_buf);
return 0x9000;
}
|
cs |
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
|
int main(void)
{
U2 ret = 0;
U1 key_short[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
U1 iv[] = { 0x0f, 0x02, 0x05, 0x03, 0x08, 0x05, 0x07, 0xaa, 0xbb, 0xcc, 0xda, 0xfb, 0xcc, 0xd0, 0xe0, 0xf0 };
U1 plain_text_short[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x0f, 0x02, 0x05, 0x03, 0x08, 0x05, 0x07, 0xaa, 0xbb, 0xcc, 0xda, 0xfb, 0xcc, 0xd0, 0xe0, 0xf0 };
U1 cipher_text[100] = {0x00, };
U4 cipher_len = 0;
ret = ARIA_Enc_Init(key_short, MODE_ECB, sizeof(iv), iv);
if(ret == SUCCESS)
printf("init success\n");
ret = ARIA_Enc_Update(PADDING_BLOCK, plain_text_short, sizeof(plain_text_short), cipher_text, &cipher_len);
if(ret == SUCCESS)
printf("init success\n");
printf("cipher_len : %d\n", cipher_len);
for(int i = 0; i < cipher_len; i++)
printf("%#x ", cipher_text[i]);
printf("\n");
return 0;
}
|
cs |
원래 생각한건 암호문이 저장된 cipher_buf 의 값을 매개변수 cipher로 저장하려고 했다. 다시 말하면 포인터로 연결하려고 했다. 문제는 이렇게 하고 cipher를 출력하면 0으로 나온다.
main 문에서 선언한 cipher_text 를 update문에 매개변수로 넣어 주소값을 갖게 하려는게 목적이었다. 근데 자꾸 0이 출력된다. 도대체 왜 그럴까..아...잠깐만.. 내가 무슨 짓을 하고 있는거냐..
첫번째, 만약 주소값을 갖게 하려면 포인터 변수를 선언해서 넣었어야 했는데.. 예를 들면 이런 느낌이다.
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "add.h"
void func(int **a)
{
int *p = malloc(sizeof(int) * 3);
memset(p, 0, 12);
*a = p;
}
int main()
{
int *p;
func(&p);
printf("%d %d %d\n", p[0], p[1], p[2]);
return 0;
}
|
cs |
두번째, 배열에 넣으려면 memcpy를 했어야 했는데...
역시 C는 어렵다. 정진하자.