Rate This Document
Findability
Accuracy
Completeness
Readability

Setting AAD in SM4-GCM

The SM4-GCM mode supports setting user additional authentication data (AAD). The setting method is as follows: When Update is called for the first time, set out to NULL and in to the buffer containing the AAD. The following is a code example:
    EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ectx, gcm, g_engine, g_key16, g_iv16);
    int len;
    // Set out to NULL in the first Update call. This indicates to set the AAD.
    ASSERT(EVP_EncryptUpdate(ectx, NULL, &len, aad, sizeof(aad)) == 1, "succeed set aad in");// Set the AAD.
    // Perform the second Update call.
    EVP_EncryptUpdate(ectx, ct, &outl, pt, 32);
    totl = outl;
    // After the second Update call, the AAD cannot be updated.
    ASSERT(EVP_EncryptUpdate(ectx, NULL, &len, aad, sizeof(aad)) == 0, "can't set add in during update");
    EVP_EncryptFinal_ex(ectx, ct + totl, &outl);

The AAD setting is the same as that in the native OpenSSL AES-GCM mode.