Use C++ to create authToken in Android

The fingerprint identification call in the android system needs to provide a hw_auth_token_t structure, hw_auth_token_t is the authToken data, if this structure is filled incorrectly, it will cause the following interface calls to be abnormal.

The authToken needs hardware to generate. In fact, the key is the calculation of hmac. There is currently no open source code for this calculation. The general principle is to calculate a key and use the key to make a hash.

In the Android system, calling the verify interface of the gatekeeper can generate authToken data.

The following example code demonstrates how to call ("hat" in the code is the output authToken data):

static bool generateHat(hw_auth_token_t& hat, int userid, uint64_t challenge, const tpassworddata* pswdata) {
    if (!pswdata) {
        return false;
    }
    memset(&hat,0,sizeof(hat));
    bool result = false;
    android::sp<IGatekeeper> bio = IGatekeeper::getService();
    if (bio == nullptr) {
        return false;
    }
    android::hardware::hidl_vec<uint8_t> newPwd;
    newPwd.setToExternal((uint8_t*)(pswdata->pCurrentPasswordText), ::strlen(pswdata->pCurrentPasswordText));
    android::hardware::hidl_vec<uint8_t> enrolledPasswordHandle;
    enrolledPasswordHandle.setToExternal(const_cast<uint8_t*>(pswdata->pCurrentPasswordData), (uint32_t)pswdata->currentPasswordDataLen);
    Return<void> hwRes = bio->verify(userid, challenge, enrolledPasswordHandle, newPwd,
                [&hat](const GatekeeperResponse& rsp) {
		    if (rsp.code >= GatekeeperStatusCode::STATUS_OK) {
                               (int32_t)rsp.data.size());
			    if (rsp.data.size() == sizeof(hat)) {
				memcpy(&hat, rsp.data.data(), rsp.data.size());
                            } else
                                (int32_t)rsp.data.size());				
       			    }
		});

    return hwRes.isOk();
}

 

Guess you like

Origin blog.csdn.net/henysugar/article/details/113624724