Implement public key extraction in TZ backend
[platform/core/security/key-manager.git] / src / manager / crypto / tz-backend / internals.cpp
index aca0124..2cbdf23 100644 (file)
@@ -337,9 +337,9 @@ AlgoType generateAKey(const CryptoAlgorithm &alg,
        return keyType;
 }
 
-void destroyKey(const RawBuffer &key)
+void destroyKey(const RawBuffer &keyId)
 {
-       TrustZoneContext::Instance().executeDestroy(key);
+       TrustZoneContext::Instance().executeDestroy(keyId);
 }
 
 void importData(const Data &data,
@@ -364,7 +364,7 @@ void importData(const Data &data,
                                                                                hash);
 }
 
-void importWrappedKey(const RawBuffer &wrappingKey,
+void importWrappedKey(const RawBuffer &wrappingKeyId,
                                          const Pwd &wrappingKeyPwd,
                                          const CryptoAlgorithm &alg,
                                          const Data &encryptedKey,
@@ -382,7 +382,7 @@ void importWrappedKey(const RawBuffer &wrappingKey,
        decompose(alg, algo, ctrLenOrTagSizeBits, iv, aad);
 
        // TODO it is awful!
-       TrustZoneContext::Instance().importWrappedKey(wrappingKey,
+       TrustZoneContext::Instance().importWrappedKey(wrappingKeyId,
                                                                                                  wrappingKeyPwd,
                                                                                                  getAlgType(algo),
                                                                                                  iv,
@@ -396,7 +396,7 @@ void importWrappedKey(const RawBuffer &wrappingKey,
                                                                                                  encryptedKeyId);
 }
 
-RawBuffer exportWrappedKey(const RawBuffer &wrappingKey,
+RawBuffer exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                   const Pwd &wrappingKeyPwd,
                                                   const CryptoAlgorithm &alg,
                                                   const RawBuffer &keyToWrapId,
@@ -409,7 +409,7 @@ RawBuffer exportWrappedKey(const RawBuffer &wrappingKey,
        decompose(alg, algo, ctrLenOrTagSizeBits, iv, aad);
 
        // TODO it is awful!
-       return TrustZoneContext::Instance().exportWrappedKey(wrappingKey,
+       return TrustZoneContext::Instance().exportWrappedKey(wrappingKeyId,
                                                                                                                 wrappingKeyPwd,
                                                                                                                 getAlgType(algo),
                                                                                                                 iv,
@@ -434,7 +434,7 @@ void destroyData(const RawBuffer &dataId)
        TrustZoneContext::Instance().destroyData(dataId);
 }
 
-BufferPair encryptDataAesGcm(const RawBuffer &key,
+BufferPair encryptDataAesGcm(const RawBuffer &keyId,
                                                        const Pwd &pwd,
                                                        const RawBuffer &iv,
                                                        int tagSize,
@@ -444,26 +444,26 @@ BufferPair encryptDataAesGcm(const RawBuffer &key,
        RawBuffer result;
        RawBuffer tag;
 
-       TrustZoneContext::Instance().executeEncryptAE(key, pwd, iv, tagSize,
+       TrustZoneContext::Instance().executeEncryptAE(keyId, pwd, iv, tagSize,
                                                                                                aad, data, result, tag);
 
        return std::make_pair(result, tag);
 }
 
-RawBuffer encryptDataAesGcmPacked(const RawBuffer &key,
+RawBuffer encryptDataAesGcmPacked(const RawBuffer &keyId,
                                                                const Pwd &pwd,
                                                                const RawBuffer &iv,
                                                                int tagSize,
                                                                const RawBuffer &data,
                                                                const RawBuffer &aad)
 {
-       auto pair = encryptDataAesGcm(key, pwd, iv, tagSize, data, aad);
+       auto pair = encryptDataAesGcm(keyId, pwd, iv, tagSize, data, aad);
        std::copy(pair.second.begin(), pair.second.end(),
                        std::back_inserter(pair.first));
        return pair.first;
 }
 
-RawBuffer decryptDataAesGcm(const RawBuffer &key,
+RawBuffer decryptDataAesGcm(const RawBuffer &keyId,
                                                        const Pwd &pwd,
                                                        const RawBuffer &iv,
                                                        int tagSizeBits,
@@ -473,13 +473,13 @@ RawBuffer decryptDataAesGcm(const RawBuffer &key,
 {
        RawBuffer result;
 
-       TrustZoneContext::Instance().executeDecryptAE(key, pwd, iv, tagSizeBits,
+       TrustZoneContext::Instance().executeDecryptAE(keyId, pwd, iv, tagSizeBits,
                                                                                                tag, aad, data, result);
 
        return result;
 }
 
-RawBuffer decryptDataAesGcmPacked(const RawBuffer &key,
+RawBuffer decryptDataAesGcmPacked(const RawBuffer &keyId,
                                                                const Pwd &pwd,
                                                                const RawBuffer &iv,
                                                                int tagSizeBits,
@@ -491,7 +491,7 @@ RawBuffer decryptDataAesGcmPacked(const RawBuffer &key,
                ThrowErr(Exc::Crypto::InputParam, "Wrong size of tag");
 
        auto tagPos = data.data() + data.size() - tagSizeBytes;
-       return decryptDataAesGcm(key,
+       return decryptDataAesGcm(keyId,
                                                        pwd,
                                                        iv,
                                                        tagSizeBits,
@@ -501,7 +501,7 @@ RawBuffer decryptDataAesGcmPacked(const RawBuffer &key,
 }
 
 
-RawBuffer symmetricEncrypt(const RawBuffer &key,
+RawBuffer symmetricEncrypt(const RawBuffer &keyId,
                                                const Pwd &pwd,
                                                const CryptoAlgorithm &alg,
                                                const RawBuffer &data)
@@ -525,7 +525,7 @@ RawBuffer symmetricEncrypt(const RawBuffer &key,
                        RawBuffer result;
                        TrustZoneContext::Instance().executeCrypt(CMD_ENCRYPT,
                                                                                                        getAlgType(algo),
-                                                                                                       key,
+                                                                                                       keyId,
                                                                                                        pwd,
                                                                                                        unpack<RawBuffer>(alg, ParamName::ED_IV),
                                                                                                        data,
@@ -537,7 +537,7 @@ RawBuffer symmetricEncrypt(const RawBuffer &key,
                        alg.getParam(ParamName::ED_TAG_LEN, tagLenBits);
                        RawBuffer aad;
                        alg.getParam(ParamName::ED_AAD, aad);
-                       return encryptDataAesGcmPacked(key,
+                       return encryptDataAesGcmPacked(keyId,
                                                                                pwd,
                                                                                unpack<RawBuffer>(alg, ParamName::ED_IV),
                                                                                tagLenBits,
@@ -552,7 +552,7 @@ RawBuffer symmetricEncrypt(const RawBuffer &key,
                                "Incorrect algorithm provided for symmetric crypto operation");
 }
 
-RawBuffer symmetricDecrypt(const RawBuffer &key,
+RawBuffer symmetricDecrypt(const RawBuffer &keyId,
                                                const Pwd &pwd,
                                                const CryptoAlgorithm &alg,
                                                const RawBuffer &data)
@@ -576,7 +576,7 @@ RawBuffer symmetricDecrypt(const RawBuffer &key,
                        RawBuffer result;
                        TrustZoneContext::Instance().executeCrypt(CMD_DECRYPT,
                                                                                                        getAlgType(algo),
-                                                                                                       key,
+                                                                                                       keyId,
                                                                                                        pwd,
                                                                                                        unpack<RawBuffer>(alg, ParamName::ED_IV),
                                                                                                        data,
@@ -588,7 +588,7 @@ RawBuffer symmetricDecrypt(const RawBuffer &key,
                        alg.getParam(ParamName::ED_TAG_LEN, tagSizeBits);
                        RawBuffer aad;
                        alg.getParam(ParamName::ED_AAD, aad);
-                       return decryptDataAesGcmPacked(key,
+                       return decryptDataAesGcmPacked(keyId,
                                                                                pwd,
                                                                                unpack<RawBuffer>(alg, ParamName::ED_IV),
                                                                                tagSizeBits,
@@ -603,7 +603,7 @@ RawBuffer symmetricDecrypt(const RawBuffer &key,
                                "Incorrect algorithm provided for symmetric crypto operation");
 }
 
-RawBuffer asymmetricEncrypt(const RawBuffer &key,
+RawBuffer asymmetricEncrypt(const RawBuffer &keyId,
                                                        const Pwd &pwd,
                                                        const CryptoAlgorithm &alg,
                                                        const RawBuffer &data)
@@ -616,7 +616,7 @@ RawBuffer asymmetricEncrypt(const RawBuffer &key,
        case AlgoType::RSA_OAEP: {
                TrustZoneContext::Instance().executeCrypt(CMD_ENCRYPT,
                                                                                                getAlgType(algo),
-                                                                                               key,
+                                                                                               keyId,
                                                                                                pwd,
                                                                                                result, // unused dummy
                                                                                                data,
@@ -631,7 +631,7 @@ RawBuffer asymmetricEncrypt(const RawBuffer &key,
                                "Incorrect algorithm provided for asymmetric crypto operation");
 }
 
-RawBuffer asymmetricDecrypt(const RawBuffer &key,
+RawBuffer asymmetricDecrypt(const RawBuffer &keyId,
                                                        const Pwd &pwd,
                                                        const CryptoAlgorithm &alg,
                                                        const RawBuffer &cipher)
@@ -644,7 +644,7 @@ RawBuffer asymmetricDecrypt(const RawBuffer &key,
        case AlgoType::RSA_OAEP: {
                TrustZoneContext::Instance().executeCrypt(CMD_DECRYPT,
                                                                                                getAlgType(algo),
-                                                                                               key,
+                                                                                               keyId,
                                                                                                pwd,
                                                                                                result, // unused dummy
                                                                                                cipher,
@@ -659,7 +659,7 @@ RawBuffer asymmetricDecrypt(const RawBuffer &key,
                                "Incorrect algorithm provided for asymmetric crypto operation");
 }
 
-uint32_t initCipher(const RawBuffer &key,
+uint32_t initCipher(const RawBuffer &keyId,
                                        const Pwd &pwd,
                                        const CryptoAlgorithm &alg,
                                        bool encrypt)
@@ -674,7 +674,7 @@ uint32_t initCipher(const RawBuffer &key,
                RawBuffer aad;
                alg.getParam(ParamName::ED_AAD, aad);
                return TrustZoneContext::Instance().initGcmCipher(encrypt ? CIPHER_ENCRYPT : CIPHER_DECRYPT,
-                                                                                                                 key,
+                                                                                                                 keyId,
                                                                                                                  pwd,
                                                                                                                  unpack<RawBuffer>(alg, ParamName::ED_IV),
                                                                                                                  tagSizeBits,
@@ -710,7 +710,7 @@ RawBuffer finalizeCipher(uint32_t opId,
        return TrustZoneContext::Instance().finalizeGcmCipher(opId, data);
 }
 
-RawBuffer sign(const RawBuffer &pkey,
+RawBuffer sign(const RawBuffer &pkeyId,
                        const Pwd &pwd,
                        const CryptoAlgorithm &alg,
                        const RawBuffer &message)
@@ -723,14 +723,14 @@ RawBuffer sign(const RawBuffer &pkey,
        RawBuffer signature;
        TrustZoneContext::Instance().executeSign(getAlgType(algo),
                                                                                        getHashType(hash),
-                                                                                       pkey,
+                                                                                       pkeyId,
                                                                                        pwd,
                                                                                        message,
                                                                                        signature);
        return signature;
 }
 
-int verify(const RawBuffer &pkey,
+int verify(const RawBuffer &pkeyId,
                const Pwd &pwd,
                const CryptoAlgorithm &alg,
                const RawBuffer &message,
@@ -743,13 +743,13 @@ int verify(const RawBuffer &pkey,
 
        return TrustZoneContext::Instance().executeVerify(getAlgType(algo),
                                                                                                        getHashType(hash),
-                                                                                                       pkey,
+                                                                                                       pkeyId,
                                                                                                        pwd,
                                                                                                        message,
                                                                                                        signature);
 }
 
-void deriveECDH(const RawBuffer &prvKey,
+void deriveECDH(const RawBuffer &prvKeyId,
                                const Pwd &prvKeyPwd,
                                const RawBuffer &pubKey,
                                const Password &secretPwd,
@@ -789,7 +789,7 @@ void deriveECDH(const RawBuffer &prvKey,
 
        RawBuffer secretPwdBuf(secretPwd.begin(), secretPwd.end());
 
-       TrustZoneContext::Instance().executeEcdh(prvKey,
+       TrustZoneContext::Instance().executeEcdh(prvKeyId,
                                                                                         prvKeyPwd,
                                                                                         xBuf,
                                                                                         yBuf,
@@ -799,7 +799,7 @@ void deriveECDH(const RawBuffer &prvKey,
                                                                                         secretHash);
 }
 
-void deriveKBKDF(const RawBuffer &secret,
+void deriveKBKDF(const RawBuffer &secretId,
                                 const CryptoAlgorithm &alg,
                                 const Password &keyPwd,
                                 const RawBuffer &keyPwdIV,
@@ -821,7 +821,7 @@ void deriveKBKDF(const RawBuffer &secret,
 
        RawBuffer keyPwdBuf(keyPwd.begin(), keyPwd.end());
 
-       TrustZoneContext::Instance().executeKbkdf(secret,
+       TrustZoneContext::Instance().executeKbkdf(secretId,
                                                                                          label,
                                                                                          context,
                                                                                          fixed,