1e41f4b71Sopenharmony_ci# Signature Verification with an RSA Key Pair (PSS 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 **'RSA2048|PSS|SHA256|MGF1_SHA256'** to create a **Verify** instance. As indicated by the string parameter, the asymmetric key type is **RSA2048**, the padding mode is **PSS**, the MD algorithm is **SHA256**, and mask algorithm is **MGF1_SHA256**.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci2. Use [OH_CryptoVerify_SetParam](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_setparam) to set parameters. The parameter values must be the same as those set for signing.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci3. 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**).
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci4. Use [OH_CryptoVerify_Update](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_update) to pass in the data to be verified.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci   Currently, the amount of data to be passed in by a single **OH_CryptoVerify_Update()** is not limited. You can determine how to pass in data based on the data volume.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci5. Use [OH_CryptoVerify_Final](../../reference/apis-crypto-architecture-kit/_crypto_signature_api.md#oh_cryptoverify_final) to verify the signature.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**Example**
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci```c++
31e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_common.h"
32e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_signature.h"
33e41f4b71Sopenharmony_ci#include "CryptoArchitectureKit/crypto_asym_key.h"
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_cistatic bool doTestRsaPssSignatureSeg()
36e41f4b71Sopenharmony_ci{
37e41f4b71Sopenharmony_ci   OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
38e41f4b71Sopenharmony_ci   OH_CryptoKeyPair *keyPair = nullptr;
39e41f4b71Sopenharmony_ci   OH_CryptoVerify *verify = nullptr;
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci   uint8_t plainText[] = {
42e41f4b71Sopenharmony_ci      0x13, 0xa7, 0x73, 0xe8, 0xb8, 0x22, 0x99, 0x72, 0x98, 0x29, 0xae, 0x74, 0xa8, 0x4a, 0xea, 0xa9,
43e41f4b71Sopenharmony_ci   };
44e41f4b71Sopenharmony_ci   Crypto_DataBlob msgBlob = {
45e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(plainText),
46e41f4b71Sopenharmony_ci      .len = sizeof(plainText)
47e41f4b71Sopenharmony_ci   };
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci   uint8_t pubKeyText[] = {
50e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50,
51e41f4b71Sopenharmony_ci      0x55, 0x42, 0x4c, 0x49, 0x43, 0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a, 0x4d,
52e41f4b71Sopenharmony_ci      0x49, 0x49, 0x42, 0x43, 0x67, 0x4b, 0x43, 0x41, 0x51, 0x45, 0x41, 0x76, 0x6a, 0x6c, 0x59, 0x35,
53e41f4b71Sopenharmony_ci      0x53, 0x72, 0x54, 0x57, 0x32, 0x43, 0x78, 0x74, 0x47, 0x32, 0x54, 0x67, 0x54, 0x54, 0x39, 0x39,
54e41f4b71Sopenharmony_ci      0x78, 0x71, 0x37, 0x62, 0x4e, 0x41, 0x6b, 0x54, 0x2b, 0x65, 0x6a, 0x75, 0x65, 0x7a, 0x37, 0x39,
55e41f4b71Sopenharmony_ci      0x37, 0x2f, 0x65, 0x63, 0x56, 0x4b, 0x34, 0x78, 0x37, 0x58, 0x41, 0x4d, 0x6d, 0x73, 0x4a, 0x0a,
56e41f4b71Sopenharmony_ci      0x4a, 0x63, 0x66, 0x49, 0x36, 0x73, 0x54, 0x4d, 0x4e, 0x68, 0x45, 0x6b, 0x70, 0x79, 0x63, 0x31,
57e41f4b71Sopenharmony_ci      0x4b, 0x32, 0x46, 0x6e, 0x30, 0x74, 0x59, 0x47, 0x2f, 0x6d, 0x4d, 0x37, 0x72, 0x71, 0x6d, 0x6a,
58e41f4b71Sopenharmony_ci      0x6c, 0x6b, 0x75, 0x72, 0x34, 0x72, 0x74, 0x6a, 0x4a, 0x4a, 0x75, 0x66, 0x34, 0x35, 0x45, 0x42,
59e41f4b71Sopenharmony_ci      0x30, 0x79, 0x6c, 0x55, 0x65, 0x47, 0x61, 0x39, 0x6d, 0x44, 0x4a, 0x57, 0x76, 0x62, 0x2b, 0x73,
60e41f4b71Sopenharmony_ci      0x0a, 0x41, 0x4a, 0x78, 0x33, 0x41, 0x44, 0x78, 0x70, 0x50, 0x31, 0x59, 0x36, 0x46, 0x61, 0x71,
61e41f4b71Sopenharmony_ci      0x54, 0x44, 0x6e, 0x64, 0x47, 0x41, 0x6e, 0x6b, 0x65, 0x4d, 0x53, 0x2f, 0x56, 0x71, 0x53, 0x45,
62e41f4b71Sopenharmony_ci      0x65, 0x75, 0x43, 0x36, 0x4d, 0x42, 0x38, 0x52, 0x53, 0x65, 0x6f, 0x31, 0x4f, 0x59, 0x4c, 0x53,
63e41f4b71Sopenharmony_ci      0x73, 0x7a, 0x36, 0x43, 0x76, 0x38, 0x34, 0x76, 0x76, 0x53, 0x69, 0x32, 0x37, 0x32, 0x51, 0x44,
64e41f4b71Sopenharmony_ci      0x6e, 0x0a, 0x6f, 0x4b, 0x4f, 0x4d, 0x34, 0x43, 0x78, 0x6d, 0x6e, 0x32, 0x31, 0x58, 0x5a, 0x43,
65e41f4b71Sopenharmony_ci      0x5a, 0x2f, 0x59, 0x50, 0x32, 0x35, 0x67, 0x5a, 0x6e, 0x57, 0x4f, 0x61, 0x42, 0x4c, 0x50, 0x57,
66e41f4b71Sopenharmony_ci      0x79, 0x6f, 0x48, 0x46, 0x65, 0x49, 0x55, 0x42, 0x48, 0x4c, 0x50, 0x69, 0x4a, 0x2b, 0x72, 0x58,
67e41f4b71Sopenharmony_ci      0x48, 0x4e, 0x65, 0x4f, 0x38, 0x2b, 0x70, 0x6c, 0x37, 0x49, 0x42, 0x74, 0x66, 0x35, 0x67, 0x70,
68e41f4b71Sopenharmony_ci      0x4a, 0x76, 0x0a, 0x31, 0x6e, 0x78, 0x72, 0x45, 0x4b, 0x73, 0x75, 0x2b, 0x6e, 0x64, 0x48, 0x43,
69e41f4b71Sopenharmony_ci      0x6e, 0x46, 0x64, 0x6f, 0x38, 0x2f, 0x49, 0x46, 0x46, 0x4a, 0x6a, 0x70, 0x36, 0x73, 0x6f, 0x55,
70e41f4b71Sopenharmony_ci      0x4a, 0x4f, 0x5a, 0x52, 0x4b, 0x6e, 0x6f, 0x41, 0x4b, 0x34, 0x67, 0x6a, 0x34, 0x48, 0x30, 0x50,
71e41f4b71Sopenharmony_ci      0x76, 0x49, 0x79, 0x4d, 0x67, 0x4b, 0x61, 0x43, 0x43, 0x41, 0x55, 0x57, 0x70, 0x4a, 0x65, 0x76,
72e41f4b71Sopenharmony_ci      0x35, 0x42, 0x52, 0x0a, 0x42, 0x4f, 0x56, 0x38, 0x4f, 0x59, 0x34, 0x48, 0x48, 0x6f, 0x42, 0x6b,
73e41f4b71Sopenharmony_ci      0x47, 0x4d, 0x6e, 0x32, 0x71, 0x6a, 0x4d, 0x48, 0x78, 0x49, 0x6c, 0x71, 0x48, 0x50, 0x67, 0x59,
74e41f4b71Sopenharmony_ci      0x70, 0x41, 0x53, 0x50, 0x51, 0x77, 0x49, 0x44, 0x41, 0x51, 0x41, 0x42, 0x0a, 0x2d, 0x2d, 0x2d,
75e41f4b71Sopenharmony_ci      0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x52, 0x53, 0x41, 0x20, 0x50, 0x55, 0x42, 0x4c, 0x49, 0x43,
76e41f4b71Sopenharmony_ci      0x20, 0x4b, 0x45, 0x59, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x0a,
77e41f4b71Sopenharmony_ci   };
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci   Crypto_DataBlob keyBlob = {
80e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(pubKeyText),
81e41f4b71Sopenharmony_ci      .len = sizeof(pubKeyText)
82e41f4b71Sopenharmony_ci   };
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci   uint8_t signText[] = {
85e41f4b71Sopenharmony_ci      0xac, 0x2b, 0x12, 0x56, 0x1c, 0xe1, 0x60, 0x49, 0xc2, 0xd9, 0x87, 0x89, 0xfb, 0xa3, 0xc5, 0x41,
86e41f4b71Sopenharmony_ci      0x64, 0x7f, 0x6f, 0x80, 0xc8, 0xdb, 0xb3, 0xdf, 0x25, 0x76, 0x4b, 0x1e, 0x51, 0xaa, 0x0a, 0x6d,
87e41f4b71Sopenharmony_ci      0x83, 0x49, 0xae, 0x00, 0x7a, 0x99, 0xf4, 0xc8, 0x98, 0x45, 0x71, 0xfc, 0x5e, 0xdb, 0xed, 0x31,
88e41f4b71Sopenharmony_ci      0xad, 0xf2, 0x35, 0x05, 0xe2, 0x3e, 0xf1, 0xcb, 0x96, 0xb2, 0xb9, 0x59, 0xaf, 0x30, 0x25, 0xb0,
89e41f4b71Sopenharmony_ci      0xda, 0x83, 0x18, 0x2b, 0x11, 0xa4, 0x93, 0x2d, 0x9e, 0x93, 0x99, 0x62, 0xdd, 0xea, 0x1b, 0xfa,
90e41f4b71Sopenharmony_ci      0x60, 0xb8, 0xea, 0x9c, 0xef, 0x4f, 0x2b, 0x9d, 0xd1, 0x3e, 0xe1, 0x6b, 0x24, 0x98, 0x9d, 0x32,
91e41f4b71Sopenharmony_ci      0xa3, 0x1e, 0x9d, 0x45, 0xe7, 0x3d, 0x51, 0x7e, 0x3b, 0x0c, 0xee, 0x3f, 0xca, 0x29, 0xd9, 0x02,
92e41f4b71Sopenharmony_ci      0xe5, 0xb8, 0xf5, 0x89, 0x06, 0xf4, 0xfe, 0x27, 0x44, 0xff, 0x38, 0xed, 0x5a, 0x0e, 0x89, 0x16,
93e41f4b71Sopenharmony_ci      0x15, 0x26, 0xf0, 0xb2, 0x4c, 0x95, 0xee, 0x0a, 0xd3, 0x61, 0xc7, 0xb2, 0x4b, 0xfd, 0x20, 0xb9,
94e41f4b71Sopenharmony_ci      0x83, 0x25, 0x43, 0x4d, 0xa0, 0x3d, 0xaa, 0x40, 0x7b, 0xac, 0x01, 0x48, 0x8e, 0x2a, 0x96, 0x11,
95e41f4b71Sopenharmony_ci      0xc0, 0x31, 0x51, 0x5b, 0xaf, 0xeb, 0x8b, 0xaf, 0xb5, 0x88, 0xcb, 0xe0, 0x97, 0x45, 0x36, 0xe9,
96e41f4b71Sopenharmony_ci      0x6e, 0x6e, 0xe0, 0x55, 0xea, 0xf4, 0xd2, 0x88, 0xbb, 0xc9, 0x85, 0x94, 0xd5, 0x65, 0xeb, 0xa3,
97e41f4b71Sopenharmony_ci      0x1c, 0xd1, 0xd6, 0xf5, 0x22, 0x29, 0xf1, 0x16, 0xa5, 0x53, 0x1b, 0xd0, 0x6c, 0xf6, 0x0d, 0xa8,
98e41f4b71Sopenharmony_ci      0xd4, 0xe4, 0xb2, 0x0a, 0x92, 0x64, 0x7a, 0x6d, 0xf2, 0x76, 0xf3, 0xb0, 0x08, 0x44, 0x31, 0x31,
99e41f4b71Sopenharmony_ci      0x90, 0x48, 0x9e, 0x2e, 0x03, 0xc7, 0xab, 0x5d, 0x7a, 0x07, 0x1f, 0x1d, 0x10, 0x21, 0x54, 0x60,
100e41f4b71Sopenharmony_ci      0x0d, 0x26, 0xe4, 0x1c, 0xc7, 0x82, 0x03, 0x65, 0x64, 0x70, 0x41, 0x68, 0x0f, 0xfa, 0x64, 0x3c,
101e41f4b71Sopenharmony_ci   };
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci   Crypto_DataBlob signBlob = {
104e41f4b71Sopenharmony_ci      .data = reinterpret_cast<uint8_t *>(signText),
105e41f4b71Sopenharmony_ci      .len = sizeof(signText)
106e41f4b71Sopenharmony_ci   };
107e41f4b71Sopenharmony_ci   
108e41f4b71Sopenharmony_ci   // keypair
109e41f4b71Sopenharmony_ci   OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
110e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Create((const char *)"RSA2048", &keyCtx);
111e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
112e41f4b71Sopenharmony_ci      return false;
113e41f4b71Sopenharmony_ci   }
114e41f4b71Sopenharmony_ci   ret = OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_PEM, &keyBlob, nullptr, &keyPair);
115e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
116e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
117e41f4b71Sopenharmony_ci      return false;
118e41f4b71Sopenharmony_ci   }
119e41f4b71Sopenharmony_ci   OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
120e41f4b71Sopenharmony_ci   // verify
121e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Create((const char *)"RSA2048|PSS|SHA256|MGF1_SHA256", &verify);
122e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
123e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
124e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
125e41f4b71Sopenharmony_ci      return false;
126e41f4b71Sopenharmony_ci   }
127e41f4b71Sopenharmony_ci   ret = OH_CryptoVerify_Init(verify, pubKey);
128e41f4b71Sopenharmony_ci   if (ret != CRYPTO_SUCCESS) {
129e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
130e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
131e41f4b71Sopenharmony_ci      return false;
132e41f4b71Sopenharmony_ci   }
133e41f4b71Sopenharmony_ci   bool res = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob);
134e41f4b71Sopenharmony_ci   if (ret != true) {
135e41f4b71Sopenharmony_ci      OH_CryptoVerify_Destroy(verify);
136e41f4b71Sopenharmony_ci      OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
137e41f4b71Sopenharmony_ci      return false;
138e41f4b71Sopenharmony_ci   }
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