• 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, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText)
22 {
23     uint8_t handleE[sizeof(uint64_t)] = {0};
24     struct OH_Huks_Blob handleEncrypt = { sizeof(uint64_t), handleE };
25     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
26     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
27     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
28         return ret;
29     }
30 
31     ret = TestUpdateFinish(&handleEncrypt, encryptParamSet, OH_HUKS_KEY_PURPOSE_ENCRYPT, inData, cipherText);
32     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT) << "TestUpdateFinish should failed.";
33     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
34         OH_Huks_Result abortRet = OH_Huks_AbortSession(&handleEncrypt, encryptParamSet);
35         EXPECT_EQ(abortRet.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Abort failed.";
36         return ret;
37     }
38     int32_t ret1 = HksMemCmp(inData->data, cipherText->data, inData->size);
39     EXPECT_NE(ret1, (int32_t)OH_HUKS_SUCCESS) << "cipherText equals inData";
40 
41     return ret;
42 }
43 
HksRsaCipherTestEncrypt(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * encryptParamSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * cipherText)44 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestEncrypt(const struct OH_Huks_Blob *keyAlias,
45     const struct OH_Huks_ParamSet *encryptParamSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *cipherText)
46 {
47     uint8_t handleE[sizeof(uint64_t)] = {0};
48     struct OH_Huks_Blob handleEncrypt = { sizeof(uint64_t), handleE };
49     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
50     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
51     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
52         return ret;
53     }
54 
55     ret = TestUpdateFinish(&handleEncrypt, encryptParamSet, OH_HUKS_KEY_PURPOSE_ENCRYPT, inData, cipherText);
56     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
57     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
58         return ret;
59     }
60     int32_t ret1 = HksMemCmp(inData->data, cipherText->data, inData->size);
61     EXPECT_NE(ret1, (int32_t)OH_HUKS_SUCCESS) << "cipherText equals inData";
62     return ret;
63 }
64 
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)65 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestDecrypt(const struct OH_Huks_Blob *keyAlias,
66     const struct OH_Huks_ParamSet *decryptParamSet, const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText,
67     const struct OH_Huks_Blob *inData)
68 {
69     uint8_t handleD[sizeof(uint64_t)] = {0};
70     struct OH_Huks_Blob handleDecrypt = { sizeof(uint64_t), handleD };
71     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
72     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
73     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
74         return ret;
75     }
76 
77     ret = TestUpdateFinish(&handleDecrypt, decryptParamSet, OH_HUKS_KEY_PURPOSE_DECRYPT, cipherText, plainText);
78     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
79     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
80         return ret;
81     }
82     int32_t ret1 = HksMemCmp(inData->data, plainText->data, inData->size);
83     EXPECT_EQ(ret1, (int32_t)OH_HUKS_SUCCESS) << "plainText not equals inData";
84     return ret;
85 }
86 
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)87 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestCase(const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
88     struct OH_Huks_ParamSet *encryptParamSet, struct OH_Huks_ParamSet *decryptParamSet, const struct OH_Huks_Blob *inData)
89 {
90     /* 1. Generate Key */
91     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
92     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
93 
94     /* 2. Export Public Key */
95     uint8_t tmpPublicKey[OH_HUKS_RSA_KEY_SIZE_1024] = {0};
96     struct OH_Huks_Blob publicKey = { OH_HUKS_RSA_KEY_SIZE_1024, (uint8_t *)tmpPublicKey };
97     ret = OH_Huks_ExportPublicKeyItem(keyAlias, genParamSet, &publicKey);
98 
99     /* 3. Import Key */
100     char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
101     struct OH_Huks_Blob newKeyAlias = { .size = strlen(tmpKey), .data = (uint8_t *)tmpKey };
102     ret = OH_Huks_ImportKeyItem(&newKeyAlias, encryptParamSet, &publicKey);
103 
104     /* 4. Encrypt Three Stage */
105     uint8_t cipher[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
106     struct OH_Huks_Blob cipherText = { Unittest::RsaCipher::RSA_COMMON_SIZE, cipher };
107     ret = HksRsaCipherTestEncrypt(&newKeyAlias, encryptParamSet, inData, &cipherText);
108     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksRsaCipherTestEncrypt failed.";
109 
110     /* 5. Decrypt Three Stage */
111     uint8_t plain[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
112     struct OH_Huks_Blob plainText = { Unittest::RsaCipher::RSA_COMMON_SIZE, plain };
113     ret = HksRsaCipherTestDecrypt(keyAlias, decryptParamSet, &cipherText, &plainText, inData);
114     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksRsaCipherTestDecrypt failed.";
115 
116     /* 6. Delete Key */
117     EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
118     EXPECT_EQ(OH_Huks_DeleteKeyItem(&newKeyAlias, encryptParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
119     return ret;
120 }
121 
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)122 OH_Huks_Result Unittest::RsaCipher::HksRsaCipherTestCaseAbnormal(const struct OH_Huks_Blob *keyAlias,
123     struct OH_Huks_ParamSet *genParamSet, struct OH_Huks_ParamSet *encryptParamSet, struct OH_Huks_ParamSet *decryptParamSet,
124     const struct OH_Huks_Blob *inData)
125 {
126     /* 1. Generate Key */
127     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
128     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
129 
130     /* 2. Export Public Key */
131     uint8_t tmpPublicKey[OH_HUKS_RSA_KEY_SIZE_1024] = {0};
132     struct OH_Huks_Blob publicKey = { OH_HUKS_RSA_KEY_SIZE_1024, (uint8_t *)tmpPublicKey };
133     ret = OH_Huks_ExportPublicKeyItem(keyAlias, genParamSet, &publicKey);
134 
135     /* 3. Import Key */
136     char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
137     struct OH_Huks_Blob newKeyAlias = { .size = strlen(tmpKey), .data = (uint8_t *)tmpKey };
138     ret = OH_Huks_ImportKeyItem(&newKeyAlias, encryptParamSet, &publicKey);
139 
140     /* 4. Encrypt Three Stage */
141     uint8_t cipher[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
142     struct OH_Huks_Blob cipherText = { Unittest::RsaCipher::RSA_COMMON_SIZE, cipher };
143     ret = HksRsaCipherTestEncryptAbnormal(&newKeyAlias, encryptParamSet, inData, &cipherText);
144     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT) << "HksRsaCipherTestEncrypt should failed.";
145 
146     /* 6. Delete Key */
147     EXPECT_EQ(OH_Huks_DeleteKeyItem(keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
148     EXPECT_EQ(OH_Huks_DeleteKeyItem(&newKeyAlias, encryptParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
149     return ret;
150 }