You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-07 12:30:27 +00:00
Implement RSA encryption/decryption.
This commit is contained in:
@@ -362,3 +362,30 @@ bool CryptoMbedTLS::verify(HashingContext::HashType p_hash_type, Vector<uint8_t>
|
||||
ERR_FAIL_COND_V_MSG(!key.is_valid(), false, "Invalid key provided.");
|
||||
return mbedtls_pk_verify(&(key->pkey), type, p_hash.ptr(), size, p_signature.ptr(), p_signature.size()) == 0;
|
||||
}
|
||||
|
||||
Vector<uint8_t> CryptoMbedTLS::encrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_plaintext) {
|
||||
Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
|
||||
ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
|
||||
uint8_t buf[1024];
|
||||
size_t size;
|
||||
Vector<uint8_t> out;
|
||||
int ret = mbedtls_pk_encrypt(&(key->pkey), p_plaintext.ptr(), p_plaintext.size(), buf, &size, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
ERR_FAIL_COND_V_MSG(ret, out, "Error while encrypting: " + itos(ret));
|
||||
out.resize(size);
|
||||
copymem(out.ptrw(), buf, size);
|
||||
return out;
|
||||
}
|
||||
|
||||
Vector<uint8_t> CryptoMbedTLS::decrypt(Ref<CryptoKey> p_key, Vector<uint8_t> p_ciphertext) {
|
||||
Ref<CryptoKeyMbedTLS> key = static_cast<Ref<CryptoKeyMbedTLS>>(p_key);
|
||||
ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
|
||||
ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot decrypt using a public_only key.");
|
||||
uint8_t buf[2048];
|
||||
size_t size;
|
||||
Vector<uint8_t> out;
|
||||
int ret = mbedtls_pk_decrypt(&(key->pkey), p_ciphertext.ptr(), p_ciphertext.size(), buf, &size, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
ERR_FAIL_COND_V_MSG(ret, out, "Error while decrypting: " + itos(ret));
|
||||
out.resize(size);
|
||||
copymem(out.ptrw(), buf, size);
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user