1e41f4b71Sopenharmony_ci# Signing and Signature Verification by Segment with an RSA Key Pair (PKCS1 Mode) (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-sign-sig-verify-overview.md#rsa).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Adding the Dynamic Library in the CMake Script
8e41f4b71Sopenharmony_ci```txt
9e41f4b71Sopenharmony_ci   target_link_libraries(entry PUBLIC libohcrypto.so)
10e41f4b71Sopenharmony_ci```
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## How to Develop
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci1. Use [OH_CryptoVerify_Create](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_create) with the string parameter **'RSA1024|PKCS1|SHA256'** to create a **Verify** instance. The string parameter must be the same as that used to create the **Sign** instance.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci2. Use [OH_CryptoVerify_Init](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_init) to initialize the **Verify** instance by using the public key (**OH_CryptoPubKey**).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci3. Use [OH_CryptoVerify_Update](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_update) to pass in the data to be verified.
20e41f4b71Sopenharmony_ci   
21e41f4b71Sopenharmony_ciCurrently, the amount of data to be passed in by a single **OH_CryptoSymCipher_Update()** is not limited. You can determine how to pass in data based on the data volume.
22e41f4b71Sopenharmony_ci   
23e41f4b71Sopenharmony_ci4. Use [OH_CryptoVerify_Final](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_final) to verify the signature.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Example**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci```c++
28e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h"
29e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asy_key.h"
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_cistatic bool doTestRsaSignatureSeg()
32e41f4b71Sopenharmony_ci{
33e41f4b71Sopenharmony_ci   OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
34e41f4b71Sopenharmony_ci   OH_CryptoKeyPair *keyPair = nullptr;
35e41f4b71Sopenharmony_ci   OH_CryptoVerify *verify = nullptr;
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci   uint8_t plainText[] = {
38e41f4b71Sopenharmony_ci      0x43, 0x31, 0x7d, 0xb5, 0x85, 0x2e, 0xd4, 0xef, 0x08, 0x7a, 0x17, 0x96, 0xbc, 0x7c, 0x8f, 0x80,
39e41f4b71Sopenharmony_ci      0x8c, 0xa7, 0x63, 0x7f, 0x26, 0x89, 0x8f, 0xf0, 0xfa, 0xa7, 0x51, 0xbd, 0x9c, 0x69, 0x17, 0xf3,
40e41f4b71Sopenharmony_ci      0xd1, 0xb5, 0xc7, 0x12, 0xbf, 0xcf, 0x91, 0x25, 0x82, 0x23, 0x6b, 0xd6, 0x64, 0x52, 0x77, 0x93,
41e41f4b71Sopenharmony_ci      0x01, 0x9d, 0x70, 0xa3, 0xf4, 0x92, 0x16, 0xec, 0x3f, 0xa7, 0x3c, 0x83, 0x8d, 0x40, 0x41, 0xfc,
42e41f4b71Sopenharmony_ci   };
43e41f4b71Sopenharmony_ci   Crypto_DataBlob msgBlob = {
44e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(plainText),
45e41f4b71Sopenharmony_ci      .len = sizeof(plainText)
46e41f4b71Sopenharmony_ci   };
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci   uint8_t pubKeyText[] = {
49e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50,
50e41f4b71Sopenharmony_ci      0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d,
51e41f4b71Sopenharmony_ci      0x49, 0x47, 0x4a, 0x41, 0x6f, 0x47, 0x42, 0x41, 0x4d, 0x78, 0x63, 0x44, 0x4d, 0x6f, 0x61, 0x59,
52e41f4b71Sopenharmony_ci      0x52, 0x58, 0x6f, 0x78, 0x65, 0x69, 0x33, 0x49, 0x6d, 0x33, 0x33, 0x78, 0x4a, 0x76, 0x61, 0x73,
53e41f4b71Sopenharmony_ci      0x63, 0x43, 0x62, 0x77, 0x31, 0x6f, 0x73, 0x63, 0x32, 0x56, 0x56, 0x69, 0x47, 0x6a, 0x56, 0x47,
54e41f4b71Sopenharmony_ci      0x47, 0x4a, 0x37, 0x6c, 0x75, 0x4e, 0x41, 0x58, 0x6b, 0x6a, 0x73, 0x56, 0x46, 0x64, 0x35, 0x0a,
55e41f4b71Sopenharmony_ci      0x58, 0x37, 0x4c, 0x4d, 0x6c, 0x46, 0x34, 0x63, 0x35, 0x5a, 0x75, 0x59, 0x2f, 0x61, 0x69, 0x57,
56e41f4b71Sopenharmony_ci      0x77, 0x70, 0x54, 0x69, 0x63, 0x62, 0x67, 0x49, 0x33, 0x43, 0x66, 0x50, 0x6f, 0x32, 0x6a, 0x6c,
57e41f4b71Sopenharmony_ci      0x52, 0x74, 0x67, 0x41, 0x46, 0x6b, 0x44, 0x71, 0x7a, 0x4b, 0x53, 0x46, 0x62, 0x46, 0x47, 0x51,
58e41f4b71Sopenharmony_ci      0x6b, 0x43, 0x6e, 0x64, 0x63, 0x2b, 0x54, 0x59, 0x6b, 0x5a, 0x42, 0x32, 0x70, 0x45, 0x6f, 0x72,
59e41f4b71Sopenharmony_ci      0x0a, 0x7a, 0x73, 0x61, 0x56, 0x58, 0x77, 0x5a, 0x47, 0x45, 0x34, 0x41, 0x43, 0x70, 0x59, 0x35,
60e41f4b71Sopenharmony_ci      0x79, 0x65, 0x66, 0x49, 0x44, 0x6c, 0x45, 0x57, 0x49, 0x51, 0x4f, 0x6a, 0x59, 0x4b, 0x2f, 0x6c,
61e41f4b71Sopenharmony_ci      0x58, 0x71, 0x7a, 0x48, 0x47, 0x69, 0x4f, 0x69, 0x32, 0x75, 0x4a, 0x45, 0x75, 0x44, 0x43, 0x50,
62e41f4b71Sopenharmony_ci      0x6a, 0x51, 0x64, 0x6a, 0x54, 0x41, 0x67, 0x4d, 0x42, 0x41, 0x41, 0x45, 0x3d, 0x0a, 0x2d, 0x2d,
63e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x55, 0x42, 0x4c, 0x49,
64e41f4b71Sopenharmony_ci      0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a,
65e41f4b71Sopenharmony_ci   };
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci   Crypto_DataBlob keyBlob = {
68e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(pubKeyText),
69e41f4b71Sopenharmony_ci      .len = sizeof(pubKeyText)
70e41f4b71Sopenharmony_ci   };
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci   uint8_t signText[] = {
73e41f4b71Sopenharmony_ci      0x68, 0x2f, 0x3b, 0xe6, 0xa6, 0x5c, 0xb8, 0x60, 0xd4, 0xe1, 0x64, 0xa7, 0xd8, 0x0c, 0x9c, 0x89,
74e41f4b71Sopenharmony_ci      0x39, 0xb4, 0xf0, 0xb7, 0xad, 0xb5, 0x8a, 0x71, 0x04, 0xf1, 0xa5, 0x63, 0xdd, 0x32, 0x6a, 0x44,
75e41f4b71Sopenharmony_ci      0xeb, 0xff, 0xb7, 0xe6, 0x85, 0xe5, 0xa5, 0x55, 0x5d, 0x5b, 0x28, 0x53, 0x63, 0xe4, 0xb3, 0xb9,
76e41f4b71Sopenharmony_ci      0xa8, 0x70, 0xc8, 0x8f, 0xcd, 0x21, 0x8d, 0xe6, 0x1f, 0xe5, 0x78, 0x34, 0xd3, 0x45, 0x0c, 0x9c,
77e41f4b71Sopenharmony_ci      0x7a, 0x22, 0x1b, 0x63, 0x55, 0xca, 0x14, 0xa5, 0x0c, 0x7a, 0x40, 0x8e, 0xa1, 0x14, 0x78, 0xa1,
78e41f4b71Sopenharmony_ci      0xf1, 0x36, 0x78, 0xbd, 0xba, 0x37, 0x3b, 0x5b, 0xb0, 0x8e, 0xb3, 0x4a, 0x9b, 0x1b, 0x0c, 0xfa,
79e41f4b71Sopenharmony_ci      0xfa, 0xc7, 0x9f, 0xb1, 0x35, 0x48, 0x82, 0x73, 0xf8, 0x6b, 0xd4, 0x76, 0x33, 0x5c, 0xed, 0x9c,
80e41f4b71Sopenharmony_ci      0xd8, 0x4b, 0xc9, 0x92, 0xa0, 0x3f, 0x6e, 0xba, 0x78, 0x2e, 0x80, 0x78, 0x1e, 0x74, 0xa0, 0x47,
81e41f4b71Sopenharmony_ci   };
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci   Crypto_DataBlob signBlob = {
84e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(signText),
85e41f4b71Sopenharmony_ci      .len = sizeof(signText)
86e41f4b71Sopenharmony_ci   };
87e41f4b71Sopenharmony_ci   
88e41f4b71Sopenharmony_ci   // keypair
89e41f4b71Sopenharmony_ci   OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
90e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Create((const char *)"RSA2048", &keyCtx);
91e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
92e41f4b71Sopenharmony_ci      return false;
93e41f4b71Sopenharmony_ci   }
94e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_PEM, &keyBlob, nullptr, &keyPair);
95e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
96e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
97e41f4b71Sopenharmony_ci      return false;
98e41f4b71Sopenharmony_ci   }
99e41f4b71Sopenharmony_ci   OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
100e41f4b71Sopenharmony_ci   // verify
101e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Create((const char *)"RSA1024|PKCS1|SHA256", &verify);
102e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
103e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
104e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
105e41f4b71Sopenharmony_ci      return false;
106e41f4b71Sopenharmony_ci   }
107e41f4b71Sopenharmony_ci   int blockSize = 20;
108e41f4b71Sopenharmony_ci   int cnt_s = 64 / blockSize;
109e41f4b71Sopenharmony_ci   int rem_s = 64 % blockSize;
110e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Init(verify, pubKey);
111e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
112e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
113e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
114e41f4b71Sopenharmony_ci      return false;
115e41f4b71Sopenharmony_ci   }
116e41f4b71Sopenharmony_ci   for (int i = 0; i < cnt_s; i++) {
117e41f4b71Sopenharmony_ci      msgBlob.len = blockSize;
118e41f4b71Sopenharmony_ci      ret = OH_CryptoVerify_Update(verify, (Crypto_DataBlob *)&msgBlob);
119e41f4b71Sopenharmony_ci      if (ret != CRYPTO_SUCCESS) {
120e41f4b71Sopenharmony_ci         OH_CryptoVerify_Destroy(verify);
121e41f4b71Sopenharmony_ci         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
122e41f4b71Sopenharmony_ci         return false;
123e41f4b71Sopenharmony_ci      }
124e41f4b71Sopenharmony_ci      msgBlob.data += blockSize;
125e41f4b71Sopenharmony_ci   }
126e41f4b71Sopenharmony_ci   bool res = false;
127e41f4b71Sopenharmony_ci   if (rem_s > 0) {
128e41f4b71Sopenharmony_ci      msgBlob.len = rem_s;
129e41f4b71Sopenharmony_ci      res = OH_CryptoVerify_Final(verify, (Crypto_DataBlob *)&msgBlob, (Crypto_DataBlob *)&signBlob);
130e41f4b71Sopenharmony_ci      if (res != true) {
131e41f4b71Sopenharmony_ci         OH_CryptoVerify_Destroy(verify);
132e41f4b71Sopenharmony_ci         OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
133e41f4b71Sopenharmony_ci         return false;
134e41f4b71Sopenharmony_ci      }
135e41f4b71Sopenharmony_ci   }
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci   msgBlob.data -=  64 - rem_s;
138e41f4b71Sopenharmony_ci   msgBlob.len = 64;
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci   OH_CryptoVerify_Destroy(verify);
141e41f4b71Sopenharmony_ci   OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
142e41f4b71Sopenharmony_ci   OH_CryptoKeyPair_Destroy(keyPair);
143e41f4b71Sopenharmony_ci   return res;
144e41f4b71Sopenharmony_ci}
145e41f4b71Sopenharmony_ci```
146