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