US0
[OpenSSL/RSA] RSA Sructure & Function 본문
[OpenSSL/RSA] RSA Sructure & Function
∙ RSA 자료구조
struct
{
BIGNUM *n; // public modulus
BIGNUM *e; // public exponent
BIGNUM *d; // private exponent
BIGNUM *p; // secret prime factor
BIGNUM *q; // secret prime factor
BIGNUM *dmp1; // d mod (p-1)
BIGNUM *dmq1; // d mod (q-1)
BIGNUM *iqmp; // q^-1 mod p
// ...
}RSA;
: RSA에서 키를 보관하는 자료구조이다.
n, e 는 공개키 n, d 는 개인키
//RSA 자료구조 생성
∙ RSA_new()
- synopsis
#include <openssl/rsa.h>
RSA * RSA_new(void);
void RSA_free(RSA *rsa);
- description
: RSA_new()함수는 RSA자료구조를 메모리상에 확보하는 함수이고
RSA_free는 확보된 자료구조를 메모리에서 제거하는 함수이다.
//RSA_Key생성
∙ RSA_generate_key()
- synopsis
#include <openssl/rsa.h>
RSA *RSA_generate_key(int num, unsigned long e, void (*callback)(int,int,void *), void *cb_arg);
- description
: RSA의 키를 생성하는 함수이다.
num은 키의 크기(bit)를 말하며 1024(bit)까지 쓸 수 있다.
그리고 e는 public exponent를 말하며 3 ~ 65535까지의 prime number(소수)를 넣으면 된다.
callback 함수는 키 생성과정에 대한 feedback을 제공하는데 사용되는 함수이다.
return값으로 RSA 구조체의 pointer형을 받아와서 키가 저장된다.
//RSA_Key확인
∙ RSA_check_key()
- synopsis
#include <openssl/rsa.h>
int RSA_check_key(RSA *rsa);
- description
: 해당하는 키(rsa)가 정당한 키를 가지고 있는지 검사를 하는 함수이다. return값이 1이면 validate키를 가지고 있다는 것을 의미한다.
//RSA 암호화/복호화 ( 암호화<publlic key>/ 복호화<private key> )
∙ RSA_public_encrypt()
- synopsis
#include <openssl/rsa.h>
int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
- description
: RSA의 암호화와 복호화를 수행하는 함수이다.
RSA_public_encrypt()함수는 form이라는 평문을 rsa라는 키를 사용하여 flen 만큼의 길이(Byte)를 암호화 작업을 수행하여 to라는
암호문을 생성하는 함수이다.
<padding은 여러 가지가 있지만 현재 대부분은 RSA_PKCS1_PADDING을 사용한다. RSA_private_decrypt()함수는 RSA_public_encrypt()함수와는 반대로 from이라는 암호문을 rsa키를 사용하여 flen만큼 복호화 하여 to에 평문을 만들어 내는 함수이다. padding역시 같은 것을 사용하면 된다. >
ps. return값의 경우, RSA_public_encrypt()함수 에서는 생성된 암호문의 길이(byte) 를 넘겨주고
RSA_private_decrypt()함수에서는 복호화된 평문의 길이(byte)를 넘겨준다.
//RSA 암호화/복호화 ( 암호화<publlic key>/ 복호화<private key> )
∙ RSA_private_encrypt()
- synopsis
#include <openssl/rsa.h>
int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);
- description
: RSA의 인증(서명)을 수행하는 함수들이다. 함수 인자들은 위의 RSA_public_encrypt(), RSA_private_decrypt()함수와 같다.
∙ RSA를 이용한 암호화 복호화의 예
#include<openssl/rsa.h>
int main()
{
RSA *key;
unsigned char cipher_text[256];
unsigned char plain_text_sender[]="This is RSA test Program";
unsigned char plain_text_receiver[256];
unsigned int num;
//RSA자료구조 생성과 key생성
key=RSA_new();
key=RSA_generate_key(1024,3,NULL,NULL);
//key유효성 검사
if(RSA_check_key(key)==1)
printf("validate key\n");
printf("plaintext=%s\n",plain_text_sender);
// 암/복호화 실행
num=RSA_public_encrypt(sizeof(plain_text_sender)-1, plain_text_sender, cipher_text, key, RSA_PKCS1_PADDING);
num=RSA_private_decrypt(num, cipher_text, plain_text_receiver, key, RSA_PKCS1_PADDING);
plain_text_receiver[num]='\0';
printf("plaintext=%s\n",plain_text_receiver);
}
'Security > Cryptography' 카테고리의 다른 글
[OpenSSL/RSA] 나눴던 Private Key로 다시 RSA구조체 만들고 암/복호화 하기!!(Deserialization) (0) | 2018.11.24 |
---|---|
[OpenSSL/RSA] RSA Private key와 Public key 나누기!!(Serialization) (0) | 2018.11.21 |
CH.5 Openssl를 활용한 암호화 프로그래밍<EVP API를 활용한 AES_CBC 암호화 ver.1.> (1) | 2018.11.10 |
[OpenSSL] <openssl/evp.h>EVP 함수 정리 (0) | 2018.11.10 |
CH.4 Openssl를 활용한 암호화 프로그래밍<EVP API를 활용한 DES_CBC 암.복호화 실습. OP2.> (0) | 2018.11.08 |