poly1305.h
Go to the documentation of this file.
1 /* poly1305.h
2  *
3  * Copyright (C) 2006-2020 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 
26 #ifndef WOLF_CRYPT_POLY1305_H
27 #define WOLF_CRYPT_POLY1305_H
28 
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 
59 enum {
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 */
69 typedef 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 
111 WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key,
112  word32 kySz);
113 WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte*, word32);
114 WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag);
115 
116 /* AEAD Functions */
117 WOLFSSL_API int wc_Poly1305_Pad(Poly1305* ctx, word32 lenToPad);
118 WOLFSSL_API int wc_Poly1305_EncodeSizes(Poly1305* ctx, word32 aadSz, word32 dataSz);
119 WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, byte* additional, word32 addSz,
120  byte* input, word32 sz, byte* tag, word32 tagSz);
121 
122 void poly1305_block(Poly1305* ctx, const unsigned char *m);
123 void poly1305_blocks(Poly1305* ctx, const unsigned char *m,
124  size_t bytes);
125 #ifdef __cplusplus
126  } /* extern "C" */
127 #endif
128 
129 #endif /* HAVE_POLY1305 */
130 #endif /* WOLF_CRYPT_POLY1305_H */
Definition: poly1305.h:69
WOLFSSL_API int wc_Poly1305Update(Poly1305 *poly1305, const byte *, word32)
This function updates the message to hash with the Poly1305 structure.
Definition: poly1305.c:662
WOLFSSL_API int wc_Poly1305Final(Poly1305 *poly1305, byte *tag)
This function calculates the hash of the input messages and stores the result in mac. After this is called, the key should be reset.
Definition: poly1305.c:479
WOLFSSL_API int wc_Poly1305SetKey(Poly1305 *poly1305, const byte *key, word32 kySz)
This function sets the key for a Poly1305 context structure, initializing it for hashing. Note: A new key should be set after generating a message hash with wc_Poly1305Final to ensure security.
Definition: poly1305.c:393
WOLFSSL_API int wc_Poly1305_MAC(Poly1305 *ctx, byte *additional, word32 addSz, byte *input, word32 sz, byte *tag, word32 tagSz)
Takes in an initialized Poly1305 struct that has a key loaded and creates a MAC (tag) using recent TL...
Definition: poly1305.c:822