/*-
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
for (i = 0; i < 32; i++)
key[i] = i;
for (i = 0; i < 16; i++)
iv0[i] = 0x20 ^ i;
for (i = 0; i < 144; i++)
in[i] = 0x80 ^ i;
for (i = 0; i < 3; i++) {
impl->ai_setenckey(&enc, key, aes_nrounds[i]);
impl->ai_setdeckey(&dec, key, aes_nrounds[i]);
/* Try one swell foop. */
memcpy(iv, iv0, 16);
impl->ai_cbc_enc(&enc, in, out, 144, iv, aes_nrounds[i]);
if (memcmp(out, expected[i], 144))
return aes_selftest_fail(impl, out, expected[i], 144,
"AES-%u-CBC enc", aes_keybits[i]);
memcpy(iv, iv0, 16);
impl->ai_cbc_dec(&dec, out, out, 144, iv, aes_nrounds[i]);
if (memcmp(out, in, 144))
return aes_selftest_fail(impl, out, in, 144,
"AES-%u-CBC dec", aes_keybits[i]);
/* Try incrementally, with IV update. */
for (j = 0; j < 144; j += 16) {
memcpy(iv, iv0, 16);
impl->ai_cbc_enc(&enc, in, out, j, iv, aes_nrounds[i]);
impl->ai_cbc_enc(&enc, in + j, out + j, 144 - j, iv,
aes_nrounds[i]);
if (memcmp(out, expected[i], 144))
return aes_selftest_fail(impl, out,
expected[i], 144, "AES-%u-CBC enc inc %u",
aes_keybits[i], j);
memcpy(iv, iv0, 16);
impl->ai_cbc_dec(&dec, out, out, j, iv,
aes_nrounds[i]);
impl->ai_cbc_dec(&dec, out + j, out + j, 144 - j, iv,
aes_nrounds[i]);
if (memcmp(out, in, 144))
return aes_selftest_fail(impl, out,
in, 144, "AES-%u-CBC dec inc %u",
aes_keybits[i], j);
}
}
/* Load the data encryption key. */
impl->ai_setenckey(&enc, key1, aes_nrounds[i]);
impl->ai_setdeckey(&dec, key1, aes_nrounds[i]);
/* Try one swell foop. */
memcpy(iv, iv0, 16);
impl->ai_xts_enc(&enc, in, out, 144, iv, aes_nrounds[i]);
if (memcmp(out, expected[i], 144))
return aes_selftest_fail(impl, out, expected[i], 144,
"AES-%u-XTS enc", aes_keybits[i]);
memcpy(iv, iv0, 16);
impl->ai_xts_dec(&dec, out, out, 144, iv, aes_nrounds[i]);
if (memcmp(out, in, 144))
return aes_selftest_fail(impl, out, in, 144,
"AES-%u-XTS dec", aes_keybits[i]);
/* Try incrementally, with IV update. */
memcpy(iv, iv0, 16);
impl->ai_xts_enc(&enc, in, out, 16, iv, aes_nrounds[i]);
impl->ai_xts_enc(&enc, in + 16, out + 16, 128, iv,
aes_nrounds[i]);
if (memcmp(out, expected[i], 144))
return aes_selftest_fail(impl, out, expected[i], 144,
"AES-%u-XTS enc incremental", aes_keybits[i]);
memcpy(iv, iv0, 16);
impl->ai_xts_dec(&dec, out, out, 128, iv, aes_nrounds[i]);
impl->ai_xts_dec(&dec, out + 128, out + 128, 16, iv,
aes_nrounds[i]);
if (memcmp(out, in, 144))
return aes_selftest_fail(impl, out, in, 144,
"AES-%u-XTS dec incremental", aes_keybits[i]);
}
if (memcmp(buf, ptxt, 32))
result |= aes_selftest_fail(impl, buf, ptxt, 48,
"AES-128 CCM plaintext");
return result;
}
int
aes_selftest(const struct aes_impl *impl)
{
int result = 0;
if (impl->ai_probe())
return -1;
if (aes_selftest_encdec(impl))
result = -1;
if (aes_selftest_encdec_cbc(impl))
result = -1;
if (aes_selftest_encdec_xts(impl))
result = -1;
if (aes_selftest_cbcmac(impl))
result = -1;
if (aes_selftest_ccm(impl))
result = -1;