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