Commit 2e41876f by Enrico Pozzobon

fixes example submissions

parent 5bc8fb62
......@@ -97,18 +97,19 @@ int crypto_aead_encrypt(
mbedtls_gcm_context ctx;
mbedtls_aes_context aes;
int ret;
unsigned long long mask = 15;
unsigned long long mlenp = (mlen + mask) & (~mask);
unsigned char tag_buf[CRYPTO_ABYTES];
*clen = mlenp + CRYPTO_ABYTES;
*clen = mlen + CRYPTO_ABYTES;
mbedtls_gcm_init( &ctx );
ctx.cipher_ctx.cipher_ctx = &aes;
ret = mbedtls_gcm_setkey( &ctx, k, 128);
if (ret != 0) {
return ret;
}
ret = mbedtls_gcm_crypt_and_tag( &ctx, 1, mlen, npub, 12, ad, adlen, m, c, 16, tag_buf );
mbedtls_gcm_free( &ctx );
mbedtls_platform_zeroize( &aes, sizeof( aes ) );
memcpy(c + mlenp, tag_buf, CRYPTO_ABYTES);
memcpy(c + mlen, tag_buf, CRYPTO_ABYTES);
return ret;
}
......
......@@ -73,10 +73,9 @@ int generate_test_vectors()
unsigned char key[CRYPTO_KEYBYTES];
unsigned char nonce[CRYPTO_NPUBBYTES];
unsigned char msg[MAX_MESSAGE_LENGTH];
unsigned char msgbuf[MAX_MESSAGE_LENGTH];
unsigned char ct[MAX_MESSAGE_LENGTH + CRYPTO_ABYTES];
unsigned char msg2[MAX_MESSAGE_LENGTH];
unsigned char ad[MAX_ASSOCIATED_DATA_LENGTH];
unsigned char ct[MAX_MESSAGE_LENGTH + CRYPTO_ABYTES];
unsigned long long clen, mlen2;
int count = 1;
int func_ret, ret_val = KAT_SUCCESS;
......@@ -97,23 +96,17 @@ int generate_test_vectors()
for (unsigned long long adlen = 0; adlen <= MAX_ASSOCIATED_DATA_LENGTH; adlen++) {
fprintf(fp, "Count = %d\n", count++);
fprint_bstr(fp, "Key = ", key, CRYPTO_KEYBYTES);
fprint_bstr(fp, "Nonce = ", nonce, CRYPTO_NPUBBYTES);
unsigned long long mlenp = ((mlen + 15) >> 4) << 4;
memset(msgbuf, 0, mlenp);
memcpy(msgbuf, msg, mlen);
fprint_bstr(fp, "PT = ", msgbuf, mlenp);
fprint_bstr(fp, "PT = ", msg, mlen);
fprint_bstr(fp, "AD = ", ad, adlen);
if ((func_ret = crypto_aead_encrypt(ct, &clen, msgbuf, mlenp, ad, adlen, NULL, nonce, key)) != 0) {
if ((func_ret = crypto_aead_encrypt(ct, &clen, msg, mlen, ad, adlen, NULL, nonce, key)) != 0) {
fprintf(fp, "crypto_aead_encrypt returned <%d>\n", func_ret);
ret_val = KAT_CRYPTO_FAILURE;
break;
......@@ -123,21 +116,22 @@ int generate_test_vectors()
fprintf(fp, "\n");
if ((func_ret = crypto_aead_decrypt(msg2, &mlen2, NULL, ct, clen, ad, adlen, nonce, key)) != 0) {
fprint_bstr(fp, "PT = ", msg2, mlen2);
fprintf(fp, "\n");
fprintf(fp, "crypto_aead_decrypt returned <%d>\n", func_ret);
ret_val = KAT_CRYPTO_FAILURE;
fprint_bstr(fp, "PT = ", msg2, mlen2);
break;
}
if (mlenp != mlen2) {
fprintf(fp, "crypto_aead_decrypt returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen2, mlenp);
if (mlen != mlen2) {
fprintf(fp, "crypto_aead_decrypt returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen2, mlen);
ret_val = KAT_CRYPTO_FAILURE;
break;
}
if (memcmp(msgbuf, msg2, mlenp)) {
if (memcmp(msg, msg2, mlen)) {
fprintf(fp, "crypto_aead_decrypt did not recover the plaintext\n");
ret_val = KAT_CRYPTO_FAILURE;
break;
......@@ -166,3 +160,4 @@ void init_buffer(unsigned char *buffer, unsigned long long numbytes)
for (unsigned long long i = 0; i < numbytes; i++)
buffer[i] = (unsigned char)i;
}
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment