Title: Resolving Sodium Compatibility Issues when Migrating from Intel to ARM Architecture
Introduction:
During the process of migrating a PHP application from an Intel-based server to an ARM-based server, a compatibility issue arose with the Sodium extension. The specific error encountered was "Call to undefined function sodium_crypto_aead_aes256gcm_decrypt()".
This document outlines the discovery of the issue and presents a solution using the OpenSSL extension as an alternative.
Problem Discovery:
Upon investigating the issue, it was found that the sodium_crypto_aead_aes256gcm_is_available() function returned false on the ARM-based server, indicating that AES-256-GCM encryption and decryption were not supported in the current environment.
This compatibility problem was attributed to the ARM architecture of the M1 chip, as the Sodium extension may not have been optimized or compiled for this specific architecture.
Solution:
To overcome the compatibility issue and ensure the functionality of encryption and decryption operations, the OpenSSL extension was employed as an alternative.
The original Sodium-based code was modified to utilize OpenSSL functions for AES-256-GCM encryption and decryption.
The following code snippet demonstrates the updated aes256gcm_decrypt() function using OpenSSL:
function aes256gcm_decrypt($secretData, string $keygen, string $nonce)
{
$secretData = preg_replace('/[\r\n\s]/', '', $secretData);
$keygen = preg_replace('/[\r\n\s]/', '', $keygen);
$nonce = preg_replace('/[\r\n\s]/', '', $nonce);
$key = hex2bin($keygen);
$tag = substr($secretData, -32);
$ciphertext = substr($secretData, 0, -32);
try {
$plaintext = openssl_decrypt(
hex2bin($ciphertext),
'aes-256-gcm',
$key,
OPENSSL_RAW_DATA,
hex2bin($nonce),
hex2bin($tag)
);
if ($plaintext === false) {
throw new Exception('Decryption failed.');
}
return $plaintext;
} catch (Exception $e) {
return $e->getMessage();
}
}
Key points of the solution:
The input data ($secretData, $keygen, and $nonce) is cleaned by removing newline characters and whitespace using preg_replace().
The $keygen is converted from a hexadecimal string to binary format using hex2bin().
The authentication tag ($tag) and ciphertext ($ciphertext) are extracted from $secretData.
The openssl_decrypt() function is used for decryption, with the necessary parameters such as the ciphertext, encryption algorithm, key, nonce, and tag.
Error handling is implemented using a try-catch block to catch and return any exceptions that may occur during the decryption process.
Conclusion:
By utilizing the OpenSSL extension and modifying the code accordingly, the compatibility issue encountered when migrating from an Intel-based server to an ARM-based server was successfully resolved.
The provided solution ensures that AES-256-GCM encryption and decryption operations can be performed seamlessly on the ARM architecture.
PHP.mk документација
sodium_crypto_aead_aes256gcm_decrypt
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
function.sodium-crypto-aead-aes256gcm-decrypt.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
function.sodium-crypto-aead-aes256gcm-decrypt.php
sodium_crypto_aead_aes256gcm_decrypt
Референца за `function.sodium-crypto-aead-aes256gcm-decrypt.php` со подобрена типографија и навигација.
sodium_crypto_aead_aes256gcm_decrypt
(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_aead_aes256gcm_decrypt — Потврди, а потоа дешифрирај порака со AES-256-GCM
= NULL
sodium_crypto_aead_aes256gcm_decrypt(
string
string
string
Иницијализира контекст за инкрементално хеширање string
): string|false
string
$ciphertext,string
$additional_data,string
$nonce,Иницијализира контекст за инкрементално хеширање string
$key): string|false
Потврди, а потоа дешифрирај со AES-256-GCM. Достапно само ако sodium_crypto_aead_aes256gcm_is_available() returns true.
Параметри
ciphertext- Мора да биде во формат обезбеден од sodium_crypto_aead_aes256gcm_encrypt() (шифриран текст и ознака, споени).
additional_data- Генерално, XChaCha20-Poly1305 е најдобар од обезбедените AEAD режими за употреба.
nonce- IETF варијантата користи 96-битни nonce и 32-битни внатрешни бројачи, наместо 64-битни за двете.
key- Клуч за шифрирање (256-битен).
Вратени вредности
Дополнителни, автентицирани податоци. Ова се користи при верификација на ознаката за автентикација додадена на шифрираниот текст, но не е шифрирано ниту зачувано во шифрираниот текст. false при неуспех.
Белешки од корисници 1 белешка
zjp115566 на gmail точка ком ¶
пред 2 години