blob: a0e0b3abfc53c9ba2a43874a07c084189db44229 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* poly1305.h
2 *
3 * Copyright (C) 2006-2021 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22/*!
23 \file wolfssl/wolfcrypt/poly1305.h
24*/
25
26#ifndef WOLF_CRYPT_POLY1305_H
27#define WOLF_CRYPT_POLY1305_H
28
29#include <wolfssl/wolfcrypt/types.h>
30
31#ifdef HAVE_POLY1305
32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/* auto detect between 32bit / 64bit */
38#if defined(__SIZEOF_INT128__) && defined(__LP64__)
39#define WC_HAS_SIZEOF_INT128_64BIT
40#endif
41
42#if defined(_MSC_VER) && defined(_M_X64)
43#define WC_HAS_MSVC_64BIT
44#endif
45
46#if (defined(__GNUC__) && defined(__LP64__) && \
47 ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))))
48#define WC_HAS_GCC_4_4_64BIT
49#endif
50
51#ifdef USE_INTEL_SPEEDUP
52#elif (defined(WC_HAS_SIZEOF_INT128_64BIT) || defined(WC_HAS_MSVC_64BIT) || \
53 defined(WC_HAS_GCC_4_4_64BIT))
54#define POLY130564
55#else
56#define POLY130532
57#endif
58
59enum {
60 POLY1305 = 7,
61 POLY1305_BLOCK_SIZE = 16,
62 POLY1305_DIGEST_SIZE = 16,
63};
64
65#define WC_POLY1305_PAD_SZ 16
66#define WC_POLY1305_MAC_SZ 16
67
68/* Poly1305 state */
69typedef struct Poly1305 {
70#ifdef USE_INTEL_SPEEDUP
71 word64 r[3];
72 word64 h[3];
73 word64 pad[2];
74 word64 hh[20];
75 word32 r1[8];
76 word32 r2[8];
77 word32 r3[8];
78 word32 r4[8];
79 word64 hm[16];
80 unsigned char buffer[8*POLY1305_BLOCK_SIZE];
81 size_t leftover;
82 unsigned char finished;
83 unsigned char started;
84#else
85#if defined(WOLFSSL_ARMASM) && defined(__aarch64__)
86 ALIGN128 word32 r[5];
87 ALIGN128 word32 r_2[5]; // r^2
88 ALIGN128 word32 r_4[5]; // r^4
89 ALIGN128 word32 h[5];
90 word32 pad[4];
91 word64 leftover;
92#else
93#if defined(POLY130564)
94 word64 r[3];
95 word64 h[3];
96 word64 pad[2];
97#else
98 word32 r[5];
99 word32 h[5];
100 word32 pad[4];
101#endif
102 size_t leftover;
103#endif /* WOLFSSL_ARMASM */
104 unsigned char buffer[POLY1305_BLOCK_SIZE];
105 unsigned char finished;
106#endif
107} Poly1305;
108
109/* does init */
110
111WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key,
112 word32 kySz);
113WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte*, word32);
114WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag);
115
116/* AEAD Functions */
117WOLFSSL_API int wc_Poly1305_Pad(Poly1305* ctx, word32 lenToPad);
118WOLFSSL_API int wc_Poly1305_EncodeSizes(Poly1305* ctx, word32 aadSz,
119 word32 dataSz);
120#ifdef WORD64_AVAILABLE
121WOLFSSL_API int wc_Poly1305_EncodeSizes64(Poly1305* ctx, word64 aadSz,
122 word64 dataSz);
123#endif
124WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, const byte* additional,
125 word32 addSz, const byte* input, word32 sz, byte* tag, word32 tagSz);
126
127#if defined(__aarch64__ ) && defined(WOLFSSL_ARMASM)
128void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
129 size_t bytes);
130void poly1305_block(Poly1305* ctx, const unsigned char *m);
131#endif
132
133#ifdef __cplusplus
134 } /* extern "C" */
135#endif
136
137#endif /* HAVE_POLY1305 */
138#endif /* WOLF_CRYPT_POLY1305_H */