• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "huks_cipher_rsa_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
HksRsaCipherTestEncryptAbnormal(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * encryptParamSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * cipherText)20 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestEncryptAbnormal(const struct OH_Huks_Blob *keyAlias,
21                                                                     const struct OH_Huks_ParamSet *encryptParamSet,
22                                                                     const struct OH_Huks_Blob *inData,
23                                                                     struct OH_Huks_Blob *cipherText)
24 {
25     uint8_t handleE[sizeof(uint64_t)] = {0};
26     struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
27     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
28     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
29     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
30         return ret;
31     }
32 
33     ret = TestUpdateFinish(&handleEncrypt, encryptParamSet, OH_HUKS_KEY_PURPOSE_ENCRYPT, inData, cipherText);
34     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT) << "TestUpdateFinish should failed.";
35     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
36         OH_Huks_Result abortRet = OH_Huks_AbortSession(&handleEncrypt, encryptParamSet);
37         EXPECT_EQ(abortRet.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Abort failed.";
38         return ret;
39     }
40     int32_t ret1 = HksMemCmp(inData->data, cipherText->data, inData->size);
41     EXPECT_NE(ret1, (int32_t)OH_HUKS_SUCCESS) << "cipherText equals inData";
42 
43     return ret;
44 }
45 
HksRsaCipherTestEncrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * encryptParamSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * cipherText)46 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestEncrypt(const struct OH_Huks_Blob *keyAlias,
47                                                             const struct OH_Huks_ParamSet *encryptParamSet,
48                                                             const struct OH_Huks_Blob *inData,
49                                                             struct OH_Huks_Blob *cipherText)
50 {
51     uint8_t handleE[sizeof(uint64_t)] = {0};
52     struct OH_Huks_Blob handleEncrypt = {sizeof(uint64_t), handleE};
53     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
54     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
55     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
56         return ret;
57     }
58 
59     ret = TestUpdateFinish(&handleEncrypt, encryptParamSet, OH_HUKS_KEY_PURPOSE_ENCRYPT, inData, cipherText);
60     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
61     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
62         return ret;
63     }
64     int32_t ret1 = HksMemCmp(inData->data, cipherText->data, inData->size);
65     EXPECT_NE(ret1, (int32_t)OH_HUKS_SUCCESS) << "cipherText equals inData";
66     return ret;
67 }
68 
HksRsaCipherTestDecrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * decryptParamSet,const struct OH_Huks_Blob * cipherText,struct OH_Huks_Blob * plainText,const struct OH_Huks_Blob * inData)69 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestDecrypt(const struct OH_Huks_Blob *keyAlias,
70                                                             const struct OH_Huks_ParamSet *decryptParamSet,
71                                                             const struct OH_Huks_Blob *cipherText,
72                                                             struct OH_Huks_Blob *plainText,
73                                                             const struct OH_Huks_Blob *inData)
74 {
75     uint8_t handleD[sizeof(uint64_t)] = {0};
76     struct OH_Huks_Blob handleDecrypt = {sizeof(uint64_t), handleD};
77     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
78     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
79     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
80         return ret;
81     }
82 
83     ret = TestUpdateFinish(&handleDecrypt, decryptParamSet, OH_HUKS_KEY_PURPOSE_DECRYPT, cipherText, plainText);
84     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
85     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
86         return ret;
87     }
88     int32_t ret1 = HksMemCmp(inData->data, plainText->data, inData->size);
89     EXPECT_EQ(ret1, (int32_t)OH_HUKS_SUCCESS) << "plainText not equals inData";
90     return ret;
91 }
92 
HksRsaCipherTestCase(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * encryptParamSet,struct OH_Huks_ParamSet * decryptParamSet,const struct OH_Huks_Blob * inData)93 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestCase(const struct OH_Huks_Blob *keyAlias,
94                                                          struct OH_Huks_ParamSet *genParamSet,
95                                                          struct OH_Huks_ParamSet *encryptParamSet,
96                                                          struct OH_Huks_ParamSet *decryptParamSet,
97                                                          const struct OH_Huks_Blob *inData)
98 {
99     /* 1. Generate Key */
100     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
101     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
102 
103     /* 2. Export Public Key */
104     uint8_t tmpPublicKey[OH_HUKS_RSA_KEY_SIZE_1024] = {0};
105     struct OH_Huks_Blob publicKey = {OH_HUKS_RSA_KEY_SIZE_1024, (uint8_t *)tmpPublicKey};
106     ret = OH_Huks_ExportPublicKeyItem(keyAlias, genParamSet, &publicKey);
107 
108     /* 3. Import Key */
109     char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
110     struct OH_Huks_Blob newKeyAlias = {.size = strlen(tmpKey), .data = (uint8_t *)tmpKey};
111     ret = OH_Huks_ImportKeyItem(&newKeyAlias, encryptParamSet, &publicKey);
112 
113     /* 4. Encrypt Three Stage */
114     uint8_t cipher[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
115     struct OH_Huks_Blob cipherText = {Unittest::RsaCipher::RSA_COMMON_SIZE, cipher};
116     ret = HksRsaCipherTestEncrypt(&newKeyAlias, encryptParamSet, inData, &cipherText);
117     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksRsaCipherTestEncrypt failed.";
118 
119     /* 5. Decrypt Three Stage */
120     uint8_t plain[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
121     struct OH_Huks_Blob plainText = {Unittest::RsaCipher::RSA_COMMON_SIZE, plain};
122     ret = HksRsaCipherTestDecrypt(keyAlias, decryptParamSet, &cipherText, &plainText, inData);
123     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksRsaCipherTestDecrypt failed.";
124 
125     /* 6. Delete Key */
126     EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
127     EXPECT_EQ(OH_Huks_DeleteKeyItem(&newKeyAlias, encryptParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS)
128         << "Delete ImportKey failed.";
129     return ret;
130 }
131 
HksRsaCipherTestCaseAbnormal(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * encryptParamSet,struct OH_Huks_ParamSet * decryptParamSet,const struct OH_Huks_Blob * inData)132 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestCaseAbnormal(const struct OH_Huks_Blob *keyAlias,
133                                                                  struct OH_Huks_ParamSet *genParamSet,
134                                                                  struct OH_Huks_ParamSet *encryptParamSet,
135                                                                  struct OH_Huks_ParamSet *decryptParamSet,
136                                                                  const struct OH_Huks_Blob *inData)
137 {
138     /* 1. Generate Key */
139     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
140     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
141 
142     /* 2. Export Public Key */
143     uint8_t tmpPublicKey[OH_HUKS_RSA_KEY_SIZE_1024] = {0};
144     struct OH_Huks_Blob publicKey = {OH_HUKS_RSA_KEY_SIZE_1024, (uint8_t *)tmpPublicKey};
145     ret = OH_Huks_ExportPublicKeyItem(keyAlias, genParamSet, &publicKey);
146 
147     /* 3. Import Key */
148     char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
149     struct OH_Huks_Blob newKeyAlias = {.size = strlen(tmpKey), .data = (uint8_t *)tmpKey};
150     ret = OH_Huks_ImportKeyItem(&newKeyAlias, encryptParamSet, &publicKey);
151 
152     /* 4. Encrypt Three Stage */
153     uint8_t cipher[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
154     struct OH_Huks_Blob cipherText = {Unittest::RsaCipher::RSA_COMMON_SIZE, cipher};
155     ret = HksRsaCipherTestEncryptAbnormal(&newKeyAlias, encryptParamSet, inData, &cipherText);
156     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT) << "HksRsaCipherTestEncrypt should failed.";
157 
158     /* 6. Delete Key */
159     EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
160     EXPECT_EQ(OH_Huks_DeleteKeyItem(&newKeyAlias, encryptParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS)
161         << "Delete ImportKey failed.";
162     return ret;
163 }