CS 458 - Labreport2
CS 458 - Labreport2
SHIQI LIU
RSA Public-Key Encryption and Signature Lab Report
1. Introduction
RSA (RivestShamirAdleman) is one of the first public-key cryptosystems and is
widely used for secure communication. The RSA algorithm first generates two large
random prime numbers, and then use them to generate public and private key pairs,
which can be used to do encryption, decryption, digital signature generation, and digital
signature verification. The RSA algorithm is built upon number theories, and it can be
quite easily implemented with the support of libraries. Essentially, students will be
implementing the RSA algorithm using the C program language.
The lab covers the following security-related topics:
• Public-key cryptography
• The RSA algorithm and key generation
• Big number calculation
• Encryption and Decryption using RSA
• Digital signature
• X.509 certificate
Lab environment: This lab requires the openssl library. I am using Ubuntu
16.04, which is already installed on.
2. Background
The RSA algorithm involves computations on large numbers. These computations
cannot be directly conducted using simple arithmetic operators in programs, because
those operators can only operate on primitive SEED Labs data types. In this lab, we will
use the Big Number library provided by openssl. To use this library, we will define each
big number as a BIGNUM type, and then use the APIs provided by the library for various
operations, such as addition, multiplication, exponentiation, modular operations, etc.
First, I create a new folder named cs458lab2.Then I write all of the lab codes in
this floder.
1. $ mkdir cs458Lab2
3. Task
3.1 Task 1: Deriving the Private Key
Let p, q, and e be three prime numbers. Let n = p*q. We will use (e, n) as the public
key. Please calculate the private key d.
First, I create a Task1.c and use the code showing in the lab insturctions.
1. $ vi task1.c
2. #include <stdio.h>
3. #include <openssl/bn.h>
4. #define NBITS 256
5.
6. void printBN(char *msg, BIGNUM *a){
7. // Convert the BIGNUM to number string
8. char * number_str = BN_bn2hex(a);
9. // Print out the number string
10. printf("%s %s\n", msg, number_str);
11. // Free the dynamically allocated memory
12. OPENSSL_free(number_str);
13. }
14.
15. int main(){
16. BN_CTX *ctx = BN_CTX_new();
17. BIGNUM *p = BN_new();
18. BIGNUM *q = BN_new();
19. BIGNUM *e = BN_new();
20. BIGNUM *d = BN_new();
21. BIGNUM *res1 = BN_new();
22. BIGNUM *res2 = BN_new();
23. BIGNUM *res3 = BN_new();
24. BIGNUM *one = BN_new();
25. // initalize p q e
26. // Assign the first large prime
27. BN_hex2bn(&p, "F7E75FDC469067FFDC4E847C51F452DF");
28.
29. // Assign the second large prime
30. BN_hex2bn(&q, "E85CED54AF57E53E092113E62F436F4F");
31.
32. // Assign the Modulus
33. BN_hex2bn(&e, "0D88C3");
34. BN_dec2bn(&one,"1");
35. //res1 = p-1
36. BN_sub(res1, p, one);
37. //res2 = q-1
38. BN_sub(res2, q, one);
39. //res3=res1*res2
40. BN_mul(res3, res1, res2, ctx);
41. //res=a*b mod n
42. BN_mod_inverse(d, e, res3, ctx);
43. //print BN
44. printBN("d= ",d);
45. return 0;
46. }
47.
Compute functions: Compute res1 = p-1,res2= q-1, res3 = res1 ∗ res2, d*e mod res3 =1
Finally, we can get d by following commands:
1. seed@VM:~/cs458Lab2$ vi task1.c[10/18/20]
2. seed@VM:~/cs458Lab2$ gcc -o task1 task1.c -lcrypto
3. seed@VM:~/cs458Lab2$ ./task1
4.
1. vi task2.c
Import libary
We can see that the decrypted message and the original message are the same.
Task2 code:
1. #include <stdio.h>
2. #include <openssl/bn.h>
3. #define NBITS 256
4.
5. //print a big number
6. void printBN(char *msg, BIGNUM *a){
7. // Convert the BIGNUM to number string
8. char * number_str = BN_bn2hex(a);
9. // Print out the number string
10. printf("%s %s\n", msg, number_str);
11. // Free the dynamically allocated memory
12. OPENSSL_free(number_str);
13. }
14.
15. int main(){
16. BN_CTX *ctx = BN_CTX_new();
17. BIGNUM *m = BN_new();
18. BIGNUM *e = BN_new();
19. BIGNUM *n = BN_new();
20. BIGNUM *d = BN_new();
21. BIGNUM *enc = BN_new();
22. BIGNUM *dec = BN_new();
23. //Initialize
24. BN_hex2bn(&e,"010001");
25. BN_hex2bn(&n,"DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5");
26. BN_hex2bn(&m,"4120746f702073656372657421");//A top secret!
27. BN_hex2bn(&d,"74D806F9F3A62BAE331FFE3F0A68AFE35B3D2E4794148AACBC26AA381CD7D30D");
28. //encry = m^e mod n
29. BN_mod_exp(enc,m,e,n,ctx);
30. printBN("encrypt message = ", enc);
31.
32. //decry = enc^d mod n
33. BN_mod_exp(dec,enc,d,n,ctx);
34. printBN("decrypt message = ",dec);
35. return 0;
36. }
1. #include <stdio.h>
2. #include <openssl/bn.h>
3. #define NBITS 256
4.
5. //print a big number
6. void printBN(char *msg, BIGNUM *a){
7. // Convert the BIGNUM to number string
8. char * number_str = BN_bn2hex(a);
9. // Print out the number string
10. printf("%s %s\n", msg, number_str);
11. // Free the dynamically allocated memory
12. OPENSSL_free(number_str);
13. }
14.
15. int main(){
16. BN_CTX *ctx = BN_CTX_new();
17. BIGNUM *n = BN_new();
18. BIGNUM *d = BN_new();
19. BIGNUM *c = BN_new();
20. BIGNUM *dec = BN_new();
21. //Initialize
22. BN_hex2bn(&n,"DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5");
23. BN_hex2bn(&c,"8C0F971DF2F3672B28811407E2DABBE1DA0FEBBBDFC7DCB67396567EA1E2493F");
24. BN_hex2bn(&d,"74D806F9F3A62BAE331FFE3F0A68AFE35B3D2E4794148AACBC26AA381CD7D30D");
25. //encry = m^e mod n
26. BN_mod_exp(dec,c,d,n,ctx);
27. printBN("encrypt message = ", dec);
28.
29. return 0;
30. }
We decrypt the given cipher text, c using the formula: c^d mod n.
By decrypting, we get the hex value of the message.
We then use the python to decode the hex value:
Value is 49206f776520796f752024323030302e
We run our code to produce the signature for the message
Value is 49206f776520796f752024333030302e
We run our code to produce the signature for the message:
We can observe that, though there is only one byte of difference in the message, their
signatures differ completely.
Code is similar to task3
1. #include <stdio.h>
2. #include <openssl/bn.h>
3. #define NBITS 256
4.
5. //print a big number
6. void printBN(char *msg, BIGNUM *a){
7. // Convert the BIGNUM to number string
8. char * number_str = BN_bn2hex(a);
9. // Print out the number string
10. printf("%s %s\n", msg, number_str);
11. // Free the dynamically allocated memory
12. OPENSSL_free(number_str);
13. }
14.
15. int main(){
16. BN_CTX *ctx = BN_CTX_new();
17. BIGNUM *n = BN_new();
18. BIGNUM *d = BN_new();
19. BIGNUM *c = BN_new();
20. BIGNUM *dec = BN_new();
21. //Initialize
22. BN_hex2bn(&n,"DCBFFE3E51F62E09CE7032E2677A78946A849DC4CDDE3A4D0CB81629242FB1A5");
23. BN_hex2bn(&c,"49206f776520796f752024323030302e");// HEX value of "I owe you $2000."
1. // task5.c
2. //
3. //
4. // Created by SHIQI LIU on 10/18/20.
5. //
6. #include <stdio.h>
7. #include <openssl/bn.h>
8. void printBN(char *msg, BIGNUM *a)
9. {
10. char *number_str_a = BN_bn2hex(a);
11. printf("%s %s\n", msg, number_str_a);
12. OPENSSL_free(number_str_a);
13. }
14. int main()
15. {
16. // init
17. BN_CTX *ctx = BN_CTX_new();
18. BIGNUM *n = BN_new();
19. BIGNUM *e = BN_new();
20. BIGNUM *M = BN_new();
21. // BIGNUM *d = BN_new();
22. BIGNUM *C = BN_new();
23. BIGNUM *S = BN_new();
24.
25. // assign values
26. BN_hex2bn(&n, "AE1CD4DC432798D933779FBD46C6E1247F0CF1233595113AA51B450F18116115");
27. BN_dec2bn(&e, "65537");
28. BN_hex2bn(&M, "4c61756e63682061206d697373696c652e"); //hex encode for " Launch a mi
ssile."
29. BN_hex2bn(&S, "643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6802F");
30. // Get S^e mod: if S=M^d mod n, C=M
31. BN_mod_exp(C, S, e, n, ctx);
32. printBN("Original Message : ", M );
33. printBN("Value of computed : ", C);
34. // verify the signature
35. if (BN_cmp(C, M) == 0)
36. {
37. printf("Valid Signature! \n");
38. }
39. else
40. {
41. printf("Verification fails! \n");
42. }
43.
44. return 0;
45. }
Suppose that the signature in is corrupted, such that the last byte of the signature changes
from 2F to 3F,
S = 643D6F34902D9C7EC90CB0B2BCA36C47FA37165C0005CAB026C0542CBDB6803F
We use the signature to compute the value of the message C.
We then use the BN_cmp API in order to compare the two messages and conclude
whether the signature is Alice’s or not:
Therefore, we get the value of computed message is entirely different form original
message, though only 1 byte of the signature is changed. It causes the verification
fails.
8. verify return:1
9. ---
10. Certificate chain
11. 0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
12. i:/C=US/O=Google Trust Services/CN=GTS CA 1O1
13. -----BEGIN CERTIFICATE-----
14. MIIFkTCCBHmgAwIBAgIQB5q2sT+2NbIIAAAAAFst1zANBgkqhkiG9w0BAQsFADBC
15. MQswCQYDVQQGEwJVUzEeMBwGA1UEChMVR29vZ2xlIFRydXN0IFNlcnZpY2VzMRMw
16. EQYDVQQDEwpHVFMgQ0EgMU8xMB4XDTIwMDkyMjE1MTYxOVoXDTIwMTIxNTE1MTYx
17. OVowaDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcT
18. DU1vdW50YWluIFZpZXcxEzARBgNVBAoTCkdvb2dsZSBMTEMxFzAVBgNVBAMTDnd3
19. dy5nb29nbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnG1D
20. Z2Tl8l5ruTFxLz5PmUKZjYFEihvXKsmiu2d0NTKv8RgWvMItgvP/1IfIz+O76jn3
21. meC71lP10LST+wI+vFPE9vEL/Zie5veUijpXE0bUApeud+Rnlsw4UJ1x50PCBdSN
22. r41sNVCAAU98ibSGlc5n2Qr1/YYhs2qnqKKG8i8zNMbjDYyzOWyjfmRVi8W7eU2Z
23. FpJVwc3D6WEACqQrLATPcFNK0Jb3f5j7eohea9eMCslB13e43iIbA2zHb8owE2Mu
24. F7GKhYiTr8OEFy4aq7meEeIFHDB/O5eO+6nSxV0p3dBsdxXu/BlEVRuwnigCtMAn
25. LVS5OEhwBuz1Wl+VWwIDAQABo4ICWzCCAlcwDgYDVR0PAQH/BAQDAgWgMBMGA1Ud
26. JQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFDDRC4ywBM9P
27. OAk0QWnLJlg3HjmJMB8GA1UdIw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jE7cPmhElEehHzWkJLsYwYZcIGOP+shpz1rw==
44. -----END CERTIFICATE-----
45. 1 s:/C=US/O=Google Trust Services/CN=GTS CA 1O1
46. i:/OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
47. -----BEGIN CERTIFICATE-----
48. MIIESjCCAzKgAwIBAgINAeO0mqGNiqmBJWlQuDANBgkqhkiG9w0BAQsFADBMMSAw
49. HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs
50. U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy
51. MTUwMDAwNDJaMEIxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg
52. U2VydmljZXMxEzARBgNVBAMTCkdUUyBDQSAxTzEwggEiMA0GCSqGSIb3DQEBAQUA
53. A4IBDwAwggEKAoIBAQDQGM9F1IvN05zkQO9+tN1pIRvJzzyOTHW5DzEZhD2ePCnv
54. UA0Qk28FgICfKqC9EksC4T2fWBYk/jCfC3R3VZMdS/dN4ZKCEPZRrAzDsiKUDzRr
55. mBBJ5wudgzndIMYcLe/RGGFl5yODIKgjEv/SJH/UL+dEaltN11BmsK+eQmMF++Ac
56. xGNhr59qM/9il71I2dN8FGfcddwuaej4bXhp0LcQBbjxMcI7JP0aM3T4I+DsaxmK
57. FsbjzaTNC9uzpFlgOIg7rR25xoynUxv8vNmkq7zdPGHXkxWY7oG9j+JkRyBABk7X
58. rJfoucBZEqFJJSPk7XA0LKW0Y3z5oz2D0c1tJKwHAgMBAAGjggEzMIIBLzAOBgNV
59. HQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1Ud
60. EwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFJjR+G4Q68+b7GCfGJAboOt9Cf0rMB8G
61. A1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYuMDUGCCsGAQUFBwEBBCkwJzAl
62. BggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdvb2cvZ3NyMjAyBgNVHR8EKzAp
63. MCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dzcjIvZ3NyMi5jcmwwPwYDVR0g
64. BDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly9wa2kuZ29vZy9y
65. ZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEAGoA+Nnn78y6pRjd9XlQWNa7H
66. TgiZ/r3RNGkmUmYHPQq6Scti9PEajvwRT2iWTHQr02fesqOqBY2ETUwgZQ+lltoN
67. FvhsO9tvBCOIazpswWC9aJ9xju4tWDQH8NVU6YZZ/XteDSGU9YzJqPjY8q3MDxrz
68. mqepBCf5o8mw/wJ4a2G6xzUr6Fb6T8McDO22PLRL6u3M4Tzs3A2M1j6bykJYi8wW
69. IRdAvKLWZu/axBVbzYmqmwkm5zLSDW5nIAJbELCQCZwMH56t2Dvqofxs6BBcCFIZ
70. USpxu6x6td0V7SvJCCosirSmIatj/9dSSVDQibet8q/7UK4v4ZUN80atnZz1yg==
71. -----END CERTIFICATE-----
72. ---
73. Server certificate
74. subject=/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
75. issuer=/C=US/O=Google Trust Services/CN=GTS CA 1O1
76. ---
77. No client certificate CA names sent
78. Peer signing digest: SHA256
79. Server Temp Key: ECDH, P-256, 256 bits
80. ---
81. SSL handshake has read 3247 bytes and written 431 bytes
82. ---
83. New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
84. Server public key is 2048 bit
85. Secure Renegotiation IS supported
86. Compression: NONE
87. Expansion: NONE
88. No ALPN negotiated
89. SSL-Session:
90. Protocol : TLSv1.2
91. Cipher : ECDHE-RSA-AES128-GCM-SHA256
92. Session-ID: BC337A8A0CE22588A9845CD955A25131346F558771B2B435CFB7373C3534E9E7
93. Session-ID-ctx:
94. Master-Key: 42EA2EB90D0B1F59618908405370AAB39B2C8E27CD06BB4C99E71435294027B3DAB36CC
A97E370AA4A738095F45CDBC0
95. Key-Arg : None
96. PSK identity: None
97. PSK identity hint: None
98. SRP username: None
99. TLS session ticket lifetime hint: 100800 (seconds)
100. TLS session ticket:
101. 0000 - 01 95 05 1c 6a 74 90 8c-f9 0e 6c 5a cc dc cd 1b ....jt....lZ....
102. 0010 - e0 a3 61 6d 1a 72 be 3c-e5 8a b1 55 2e d7 5a ab ..am.r.<...U..Z.
103. 0020 - a9 01 59 f0 e8 eb 0e a2-e5 34 a4 b6 be 05 ec 7c ..Y......4.....|
104. 0030 - 08 9e e2 70 94 9c e1 8b-47 8e 10 24 c8 e6 e6 5c ...p....G..$...\
105. 0040 - cc 83 71 a2 a2 1c c8 c3-db 1f df c9 15 23 3e e0 ..q..........#>.
106. 0050 - dc f9 73 33 46 83 27 d9-ab 92 40 0d 92 41 89 6f ..s3F.'[email protected]
107. 0060 - 49 bb 2f 30 8c 8e fc fd-bc 3a 44 c5 67 3d c2 15 I./0.....:D.g=..
108. 0070 - 29 2c 86 39 53 66 a0 68-70 48 36 99 50 e7 09 ba ),.9Sf.hpH6.P...
109. 0080 - 39 ce 7f 5b d1 fb a1 7f-83 b9 6b be 36 b5 c1 a6 9..[......k.6...
110. 0090 - a7 32 f0 85 d7 04 77 1b-e4 c4 03 77 d3 0b 27 92 .2....w....w..'.
111. 00a0 - 1d 4d a5 06 35 67 69 5e-8b 51 cd 64 2e 91 8f 79 .M..5gi^.Q.d...y
112. 00b0 - fa c8 5b 17 8f 63 f4 c5-89 e0 83 2d 16 2f 9e f7 ..[..c.....-./..
113. 00c0 - 33 20 5d 03 00 c6 f4 0d-ec b5 a2 d0 6a 49 a4 49 3 ].........jI.I
114. 00d0 - e5 6a a9 fc f7 05 34 d5-7d 63 af 4d 79 .j....4.}c.My
115.
116. Start Time: 1603076169
117. Timeout : 300 (sec)
118. Verify return code: 0 (ok)
119. ---
120. read:errno=0
121. [10/18/20]seed@VM:~/cs458Lab2$
We save these two certificates in the files c0.pem and c1.pem respectively
Step2 Extract the public key (e, n) from the issuer’s certificate.
We get the value of n using -modulus:
Print all attributes of the certificate, and then find the exponent, which is public key e(line 35)
We put this signature into a file, then remove all the colons and spaces from the
signature:
1. [10/18/20]seed@VM:~/cs458Lab2$ vi signature
2. [10/18/20]seed@VM:~/cs458Lab2$ cat signature | tr -d '[:space:]:'
3. ee87b56af7767dd20f4d9e9e5988cbe41dde7e24c56a56add210ad31f8177ffdc5bbdfae33b2a9c2
3fdec383592f3d9cff46a92ad388bd6a3486371922323924146e2494eb41b8c5ce9a3a083f06bcd6
25d3d6da55f13a95dee423d0527e5995eb60fdf71d21468045cee1526ae9c021d68e55e33fb728e1
754a2e69540[10/18/20]seed@VM:~/cs458Lab2$
In this we cannot determine the end of the body. So we use -strparse to get the
field from the offset 4, which will give us the body of the certificate,
excluding the signature block.
We can notice that the original message and the hash value of the computed
message is the same. Hence we can conclude that the www.google.com
certificate is verified to be right.