Trusted Design

OpenSSLでRSA鍵ペア生成

概要

OpenSSLが提供しているC言語のAPIを使って公開鍵暗号方式であるRSAの鍵ペア生成をします。RSAの暗号処理は公開鍵と秘密鍵の2種類の鍵を利用します。秘密鍵は自分で文字通り秘密に保管します。一方の公開鍵は通信相手に送ります。公開鍵暗号方式の特徴は,公開鍵を利用して作成した暗号文を秘密鍵で復号できることです。
暗号文を作成するときは,通信相手の公開鍵を使って暗号化します。そして,暗号文を受け取った受信者は自分の秘密鍵を使って暗号文を復号し,平文を取得することができます。
ここでは,RSA鍵ペアの生成を行います。

利用するAPI

RSA鍵生成を実行するためのAPI

  • BN_new()
    RSAの鍵生成で大きな整数を利用するため,BIGNUM構造体を初期化します。
  • BN_set_word()
    BIGNUMを生成します。
  • BN_free()
    BIGNUM構造体を開放します。
  • RSA_new()
    RSA構造体を初期化します。
  • RSA_generate_key_ex()
    RSA鍵ペアを生成します。
  • RSA_free()
    RSA構造体を解放します。
  • i2d_RSAPublicKey()
    RSA公開鍵をPKCS#1形式で出力します。
  • i2d_RSAPrivateKey()
    RSA秘密鍵をPKCS#1形式で出力します。

サンプルプログラム

RSA鍵ペアを生成する。

この例では1024bitの鍵ペアを生成し,PKCS#1のDER形式で表示します。


#include "openssl/rsa.h"
#include "openssl/err.h"
#include "openssl/x509.h"

/*
 バイト列を出力する関数
*/
int PrintBytes(unsigned char* bytes, unsigned int bytesLen)
{
	for (int i = 1; i <= bytesLen; i++) {
		printf("%02x ", *bytes++);
		if (i % 16 == 0) printf("\n");
	}
	printf("\n");
	return 1;
}

int main()
{
	RSA* rsa = NULL;
	int bits = 1024;		// 1024bitの鍵ペアを生成する

	BIGNUM*	bn = NULL;
	unsigned long long e = RSA_F4; // 65537

	unsigned char* outbuf;
	unsigned char* work;
	int	outbufLen = 0;

	/* RSA鍵生成で必要になるBIGNUMを生成する */
	bn = BN_new();
	BN_set_word(bn, e);

	/* RSA鍵生成を行う */
	rsa = RSA_new();
	if (!RSA_generate_key_ex(rsa, bits, bn, NULL))
	{
		/* Error */
		printf("Error: %s\n", ERR_reason_error_string(ERR_get_error()));
		return 0;
	}

	/* RSA公開鍵を出力する */
	outbufLen = i2d_RSAPublicKey(rsa, NULL);
	outbuf = work = (unsigned char*)malloc(outbufLen);
	if(!i2d_RSAPublicKey(rsa, &work))
	{
		/* Error */
		printf("Error: %s\n", ERR_reason_error_string(ERR_get_error()));
		free(outbuf);
		return 0;
	}
	printf("RSA Public Key : \n");
	PrintBytes(outbuf, outbufLen);
	printf("\n");
	free(outbuf);

	/* RSA秘密鍵を出力する */
	outbufLen = i2d_RSAPrivateKey(rsa, NULL);
	outbuf = work = (unsigned char*)malloc(outbufLen);
	if (!i2d_RSAPrivateKey(rsa, &work))
	{
		/* Error */
		printf("Error: %s\n", ERR_reason_error_string(ERR_get_error()));
		free(outbuf);
		return 0;
	}
	printf("RSA Private Key : \n");
	PrintBytes(outbuf, outbufLen);
	free(outbuf);

	/* 解放処理 */
	BN_free(bn);
	RSA_free(rsa);
}

実行結果

公開鍵と秘密鍵の順番に出力されます。


RSA Public Key :
30 81 89 02 81 81 00 c4 3e 77 98 45 a7 99 31 1d
7d 06 1c e4 95 64 55 6a 44 e3 37 7d 98 5a 74 e5
3f b0 bf 8c 5d 70 29 d2 e8 93 9b 77 b7 a7 a5 37
20 bf 81 3e 66 84 72 04 4c c1 dc 90 78 ff 1d 93
fe 98 7c f4 86 cf 4f 82 1d 81 ee cc 35 d2 bd aa
15 f1 2a 4f 53 7d 14 26 4d 91 af 69 ee 7b d0 54
42 c1 11 7b 7d 1a a3 d3 1c a0 c3 63 fe 3b e1 ac
ef a3 b5 ef 35 03 2b 51 65 79 b2 4f 1c 7b e6 3e
d6 25 b3 89 57 ba 55 02 03 01 00 01

RSA Private Key :
30 82 02 5e 02 01 00 02 81 81 00 c4 3e 77 98 45
a7 99 31 1d 7d 06 1c e4 95 64 55 6a 44 e3 37 7d
98 5a 74 e5 3f b0 bf 8c 5d 70 29 d2 e8 93 9b 77
b7 a7 a5 37 20 bf 81 3e 66 84 72 04 4c c1 dc 90
78 ff 1d 93 fe 98 7c f4 86 cf 4f 82 1d 81 ee cc
35 d2 bd aa 15 f1 2a 4f 53 7d 14 26 4d 91 af 69
ee 7b d0 54 42 c1 11 7b 7d 1a a3 d3 1c a0 c3 63
fe 3b e1 ac ef a3 b5 ef 35 03 2b 51 65 79 b2 4f
1c 7b e6 3e d6 25 b3 89 57 ba 55 02 03 01 00 01
02 81 81 00 bb 3b 1a 21 76 bb 6b 86 17 ab 53 ec
b7 0d ed 57 2c f7 a2 8d 2b cf f2 e5 ba 5f 64 c3
ac 28 ed bd 89 93 0a 59 77 04 0a f7 af 3c 18 5a
37 ed d9 89 6a 9e cd 0b 21 ef be ff 3a 56 da a7
c5 28 e5 9a 38 d6 98 e4 36 88 24 53 02 79 db b8
f1 f1 10 8a ba d0 b3 d0 4f 78 c7 ed 22 5a 6c 9f
61 55 14 47 b7 0b b0 0b 7b fb a5 9b 67 ea 6f c3
42 d3 41 c8 fa b6 7f 1e a9 a2 af d6 b6 85 3c 12
4d c2 e4 21 02 41 00 f9 6c 3d 35 f2 bb 01 e8 a3
ad b0 ef 7b 1f 3e ec fa 1f c1 be 1c 01 93 23 b2
73 b5 c4 23 03 19 c3 5d 2f 9a a5 ce 6c bc 16 25
f0 7e ba 56 b6 75 d4 09 3b a0 df 2f e1 ea 80 e9
b7 83 81 d4 85 76 29 02 41 00 c9 6b 3c db 9a 8d
d6 90 3d 19 38 19 76 5a 1a b0 8b 7b 92 49 9a 7f
dc 8b 21 c5 8c ba d2 4a 54 ef b1 a6 9f 8a c5 ef
e3 a8 3e c5 5e 34 7b 8c d3 81 c5 10 71 65 44 3e
48 98 3c 0f 20 54 c5 1f b0 4d 02 41 00 89 e0 af
a5 96 dc 91 9d 8c 7d 83 85 9e c6 12 fe e4 f5 91
20 0a 53 44 87 43 2a 7b 9c 44 ce 7d fb 65 13 df
a5 30 8b 23 1e 0b 32 fa 9a ff 3c 35 be 54 ac 45
ad 9a c6 83 df 67 53 2e bb 22 43 f4 a1 02 40 70
ce c7 d1 f2 b8 d7 e0 06 e8 35 fc 44 c5 e8 46 d2
88 0e 01 7d 2f 79 74 cf 20 56 56 57 ea db b1 37
5d 2b e3 cc 42 33 ce 0a 01 fe 66 04 80 76 01 2b
3e af 53 d9 29 cc fc be 3e 76 8c 23 af 40 cd 02
41 00 8b d7 2c f1 1e e5 a4 62 64 d6 59 e1 36 1a
8d 90 e3 61 05 8b e6 be b5 7d b4 cf d8 11 68 7e
28 55 38 63 5f 1e 09 e8 f3 cb ac fe 71 63 16 29
7b e1 c5 4e 50 02 79 c0 ef ce 4b 95 f0 85 cb 77
41 b7