1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <stdlib.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include <openssl/core_names.h>
14e1051a39Sopenharmony_ci#include <openssl/evp.h>
15e1051a39Sopenharmony_ci#include <openssl/params.h>
16e1051a39Sopenharmony_ci#include <openssl/err.h>
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ci/*
19e1051a39Sopenharmony_ci * This is a demonstration of how to compute Poly1305-AES using the OpenSSL
20e1051a39Sopenharmony_ci * Poly1305 and AES providers and the EVP API.
21e1051a39Sopenharmony_ci *
22e1051a39Sopenharmony_ci * Please note that:
23e1051a39Sopenharmony_ci *
24e1051a39Sopenharmony_ci *   - Poly1305 must never be used alone and must be used in conjunction with
25e1051a39Sopenharmony_ci *     another primitive which processes the input nonce to be secure;
26e1051a39Sopenharmony_ci *
27e1051a39Sopenharmony_ci *   - you must never pass a nonce to the Poly1305 primitive directly;
28e1051a39Sopenharmony_ci *
29e1051a39Sopenharmony_ci *   - Poly1305 exhibits catastrophic failure (that is, can be broken) if a
30e1051a39Sopenharmony_ci *     nonce is ever reused for a given key.
31e1051a39Sopenharmony_ci *
32e1051a39Sopenharmony_ci * If you are looking for a general purpose MAC, you should consider using a
33e1051a39Sopenharmony_ci * different MAC and looking at one of the other examples, unless you have a
34e1051a39Sopenharmony_ci * good familiarity with the details and caveats of Poly1305.
35e1051a39Sopenharmony_ci *
36e1051a39Sopenharmony_ci * This example uses AES, as described in the original paper, "The Poly1305-AES
37e1051a39Sopenharmony_ci * message authentication code":
38e1051a39Sopenharmony_ci *   https://cr.yp.to/mac/poly1305-20050329.pdf
39e1051a39Sopenharmony_ci *
40e1051a39Sopenharmony_ci * The test vectors below are from that paper.
41e1051a39Sopenharmony_ci */
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci/*
44e1051a39Sopenharmony_ci * Hard coding the key into an application is very bad.
45e1051a39Sopenharmony_ci * It is done here solely for educational purposes.
46e1051a39Sopenharmony_ci * These are the "r" and "k" inputs to Poly1305-AES.
47e1051a39Sopenharmony_ci */
48e1051a39Sopenharmony_cistatic const unsigned char test_r[] = {
49e1051a39Sopenharmony_ci    0x85, 0x1f, 0xc4, 0x0c, 0x34, 0x67, 0xac, 0x0b,
50e1051a39Sopenharmony_ci    0xe0, 0x5c, 0xc2, 0x04, 0x04, 0xf3, 0xf7, 0x00
51e1051a39Sopenharmony_ci};
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_cistatic const unsigned char test_k[] = {
54e1051a39Sopenharmony_ci    0xec, 0x07, 0x4c, 0x83, 0x55, 0x80, 0x74, 0x17,
55e1051a39Sopenharmony_ci    0x01, 0x42, 0x5b, 0x62, 0x32, 0x35, 0xad, 0xd6
56e1051a39Sopenharmony_ci};
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci/*
59e1051a39Sopenharmony_ci * Hard coding a nonce must not be done under any circumstances and is done here
60e1051a39Sopenharmony_ci * purely for demonstration purposes. Please note that Poly1305 exhibits
61e1051a39Sopenharmony_ci * catastrophic failure (that is, can be broken) if a nonce is ever reused for a
62e1051a39Sopenharmony_ci * given key.
63e1051a39Sopenharmony_ci */
64e1051a39Sopenharmony_cistatic const unsigned char test_n[] = {
65e1051a39Sopenharmony_ci    0xfb, 0x44, 0x73, 0x50, 0xc4, 0xe8, 0x68, 0xc5,
66e1051a39Sopenharmony_ci    0x2a, 0xc3, 0x27, 0x5c, 0xf9, 0xd4, 0x32, 0x7e
67e1051a39Sopenharmony_ci};
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_ci/* Input message. */
70e1051a39Sopenharmony_cistatic const unsigned char test_m[] = {
71e1051a39Sopenharmony_ci    0xf3, 0xf6
72e1051a39Sopenharmony_ci};
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_cistatic const unsigned char expected_output[] = {
75e1051a39Sopenharmony_ci    0xf4, 0xc6, 0x33, 0xc3, 0x04, 0x4f, 0xc1, 0x45,
76e1051a39Sopenharmony_ci    0xf8, 0x4f, 0x33, 0x5c, 0xb8, 0x19, 0x53, 0xde
77e1051a39Sopenharmony_ci};
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci/*
80e1051a39Sopenharmony_ci * A property query used for selecting the POLY1305 implementation.
81e1051a39Sopenharmony_ci */
82e1051a39Sopenharmony_cistatic char *propq = NULL;
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ciint main(int argc, char **argv)
85e1051a39Sopenharmony_ci{
86e1051a39Sopenharmony_ci    int rv = EXIT_FAILURE;
87e1051a39Sopenharmony_ci    EVP_CIPHER *aes = NULL;
88e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *aesctx = NULL;
89e1051a39Sopenharmony_ci    EVP_MAC *mac = NULL;
90e1051a39Sopenharmony_ci    EVP_MAC_CTX *mctx = NULL;
91e1051a39Sopenharmony_ci    unsigned char composite_key[32];
92e1051a39Sopenharmony_ci    unsigned char out[16];
93e1051a39Sopenharmony_ci    OSSL_LIB_CTX *library_context = NULL;
94e1051a39Sopenharmony_ci    size_t out_len = 0;
95e1051a39Sopenharmony_ci    int aes_len = 0;
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ci    library_context = OSSL_LIB_CTX_new();
98e1051a39Sopenharmony_ci    if (library_context == NULL) {
99e1051a39Sopenharmony_ci        fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
100e1051a39Sopenharmony_ci        goto end;
101e1051a39Sopenharmony_ci    }
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci    /* Fetch the Poly1305 implementation */
104e1051a39Sopenharmony_ci    mac = EVP_MAC_fetch(library_context, "POLY1305", propq);
105e1051a39Sopenharmony_ci    if (mac == NULL) {
106e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
107e1051a39Sopenharmony_ci        goto end;
108e1051a39Sopenharmony_ci    }
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci    /* Create a context for the Poly1305 operation */
111e1051a39Sopenharmony_ci    mctx = EVP_MAC_CTX_new(mac);
112e1051a39Sopenharmony_ci    if (mctx == NULL) {
113e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
114e1051a39Sopenharmony_ci        goto end;
115e1051a39Sopenharmony_ci    }
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_ci    /* Fetch the AES implementation */
118e1051a39Sopenharmony_ci    aes = EVP_CIPHER_fetch(library_context, "AES-128-ECB", propq);
119e1051a39Sopenharmony_ci    if (aes == NULL) {
120e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_CIPHER_fetch() returned NULL\n");
121e1051a39Sopenharmony_ci        goto end;
122e1051a39Sopenharmony_ci    }
123e1051a39Sopenharmony_ci
124e1051a39Sopenharmony_ci    /* Create a context for AES */
125e1051a39Sopenharmony_ci    aesctx = EVP_CIPHER_CTX_new();
126e1051a39Sopenharmony_ci    if (aesctx == NULL) {
127e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_CIPHER_CTX_new() returned NULL\n");
128e1051a39Sopenharmony_ci        goto end;
129e1051a39Sopenharmony_ci    }
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci    /* Initialize the AES cipher with the 128-bit key k */
132e1051a39Sopenharmony_ci    if (!EVP_EncryptInit_ex(aesctx, aes, NULL, test_k, NULL)) {
133e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_EncryptInit_ex() failed\n");
134e1051a39Sopenharmony_ci        goto end;
135e1051a39Sopenharmony_ci    }
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci    /*
138e1051a39Sopenharmony_ci     * Disable padding for the AES cipher. We do not strictly need to do this as
139e1051a39Sopenharmony_ci     * we are encrypting a single block and thus there are no alignment or
140e1051a39Sopenharmony_ci     * padding concerns, but this ensures that the operation below fails if
141e1051a39Sopenharmony_ci     * padding would be required for some reason, which in this circumstance
142e1051a39Sopenharmony_ci     * would indicate an implementation bug.
143e1051a39Sopenharmony_ci     */
144e1051a39Sopenharmony_ci    if (!EVP_CIPHER_CTX_set_padding(aesctx, 0)) {
145e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_CIPHER_CTX_set_padding() failed\n");
146e1051a39Sopenharmony_ci        goto end;
147e1051a39Sopenharmony_ci    }
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci    /*
150e1051a39Sopenharmony_ci     * Computes the value AES_k(n) which we need for our Poly1305-AES
151e1051a39Sopenharmony_ci     * computation below.
152e1051a39Sopenharmony_ci     */
153e1051a39Sopenharmony_ci    if (!EVP_EncryptUpdate(aesctx, composite_key + 16, &aes_len,
154e1051a39Sopenharmony_ci                           test_n, sizeof(test_n))) {
155e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_EncryptUpdate() failed\n");
156e1051a39Sopenharmony_ci        goto end;
157e1051a39Sopenharmony_ci    }
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci    /*
160e1051a39Sopenharmony_ci     * The Poly1305 provider expects the key r to be passed as the first 16
161e1051a39Sopenharmony_ci     * bytes of the "key" and the processed nonce (that is, AES_k(n)) to be
162e1051a39Sopenharmony_ci     * passed as the second 16 bytes of the "key". We already put the processed
163e1051a39Sopenharmony_ci     * nonce in the correct place above, so copy r into place.
164e1051a39Sopenharmony_ci     */
165e1051a39Sopenharmony_ci    memcpy(composite_key, test_r, 16);
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci    /* Initialise the Poly1305 operation */
168e1051a39Sopenharmony_ci    if (!EVP_MAC_init(mctx, composite_key, sizeof(composite_key), NULL)) {
169e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_MAC_init() failed\n");
170e1051a39Sopenharmony_ci        goto end;
171e1051a39Sopenharmony_ci    }
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_ci    /* Make one or more calls to process the data to be authenticated */
174e1051a39Sopenharmony_ci    if (!EVP_MAC_update(mctx, test_m, sizeof(test_m))) {
175e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_MAC_update() failed\n");
176e1051a39Sopenharmony_ci        goto end;
177e1051a39Sopenharmony_ci    }
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci    /* Make one call to the final to get the MAC */
180e1051a39Sopenharmony_ci    if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
181e1051a39Sopenharmony_ci        fprintf(stderr, "EVP_MAC_final() failed\n");
182e1051a39Sopenharmony_ci        goto end;
183e1051a39Sopenharmony_ci    }
184e1051a39Sopenharmony_ci
185e1051a39Sopenharmony_ci    printf("Generated MAC:\n");
186e1051a39Sopenharmony_ci    BIO_dump_indent_fp(stdout, out, out_len, 2);
187e1051a39Sopenharmony_ci    putchar('\n');
188e1051a39Sopenharmony_ci
189e1051a39Sopenharmony_ci    if (out_len != sizeof(expected_output)) {
190e1051a39Sopenharmony_ci        fprintf(stderr, "Generated MAC has an unexpected length\n");
191e1051a39Sopenharmony_ci        goto end;
192e1051a39Sopenharmony_ci    }
193e1051a39Sopenharmony_ci
194e1051a39Sopenharmony_ci    if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
195e1051a39Sopenharmony_ci        fprintf(stderr, "Generated MAC does not match expected value\n");
196e1051a39Sopenharmony_ci        goto end;
197e1051a39Sopenharmony_ci    }
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_ci    rv = EXIT_SUCCESS;
200e1051a39Sopenharmony_ciend:
201e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(aesctx);
202e1051a39Sopenharmony_ci    EVP_CIPHER_free(aes);
203e1051a39Sopenharmony_ci    EVP_MAC_CTX_free(mctx);
204e1051a39Sopenharmony_ci    EVP_MAC_free(mac);
205e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(library_context);
206e1051a39Sopenharmony_ci    if (rv != EXIT_SUCCESS)
207e1051a39Sopenharmony_ci        ERR_print_errors_fp(stderr);
208e1051a39Sopenharmony_ci    return rv;
209e1051a39Sopenharmony_ci}
210