poly1305.h (6040B)
1 /* poly1305.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 WOLFSSL_X86_64_BUILD 52 #if defined(USE_INTEL_SPEEDUP) && !defined(NO_POLY1305_ASM) 53 #define USE_INTEL_POLY1305_SPEEDUP 54 #define HAVE_INTEL_AVX1 55 #endif 56 #endif 57 58 #if defined(USE_INTEL_POLY1305_SPEEDUP) 59 #elif (defined(WC_HAS_SIZEOF_INT128_64BIT) || defined(WC_HAS_MSVC_64BIT) || \ 60 defined(WC_HAS_GCC_4_4_64BIT)) && !defined(WOLFSSL_W64_WRAPPER_TEST) 61 #define POLY130564 62 #else 63 #define POLY130532 64 #endif 65 66 enum { 67 POLY1305 = 7, 68 POLY1305_BLOCK_SIZE = 16, 69 POLY1305_DIGEST_SIZE = 16 70 }; 71 72 #define WC_POLY1305_PAD_SZ 16 73 #define WC_POLY1305_MAC_SZ 16 74 75 /* Poly1305 state */ 76 typedef struct Poly1305 { 77 #ifdef USE_INTEL_POLY1305_SPEEDUP 78 word64 r[3]; 79 word64 h[3]; 80 word64 pad[2]; 81 word64 hh[20]; 82 word32 r1[8]; 83 word32 r2[8]; 84 word32 r3[8]; 85 word32 r4[8]; 86 word64 hm[16]; 87 unsigned char buffer[8*POLY1305_BLOCK_SIZE]; 88 size_t leftover; 89 unsigned char finished; 90 unsigned char started; 91 #elif defined(WOLFSSL_ARMASM) && defined(__aarch64__) 92 ALIGN128 word64 r64[2]; 93 ALIGN128 word32 r[5]; 94 ALIGN128 word32 r_2[5]; /* r^2 */ 95 ALIGN128 word32 r_4[5]; /* r^4 */ 96 ALIGN128 word32 h[5]; 97 word32 pad[4]; 98 word64 leftover; 99 unsigned char buffer[POLY1305_BLOCK_SIZE]; 100 unsigned char finished; 101 #elif defined(WOLFSSL_ARMASM) && !defined(WOLFSSL_ARMASM_THUMB2) && \ 102 !defined(WOLFSSL_ARMASM_NO_NEON) 103 /* NEON implementation for ARM32 */ 104 word32 r[4]; 105 word32 h[6]; 106 word32 pad[4]; 107 word32 leftover; 108 unsigned char buffer[4*POLY1305_BLOCK_SIZE]; 109 word32 r_21[10]; 110 word32 r_43[10]; 111 #elif defined(WOLFSSL_ARMASM) 112 /* ARM32 (non-NEON) and Thumb2 */ 113 word32 r[4]; 114 word32 h[5]; 115 word32 pad[4]; 116 word32 leftover; 117 unsigned char buffer[POLY1305_BLOCK_SIZE]; 118 #elif defined(WOLFSSL_RISCV_ASM) 119 word64 r[2]; 120 #ifdef WOLFSSL_RISCV_VECTOR 121 word64 r2[6]; 122 #endif 123 word64 h[3]; 124 word64 pad[2]; 125 size_t leftover; 126 unsigned char buffer[POLY1305_BLOCK_SIZE]; 127 #else 128 #if defined(POLY130564) 129 word64 r[3]; 130 word64 h[3]; 131 word64 pad[2]; 132 #else 133 word32 r[5]; 134 word32 h[5]; 135 word32 pad[4]; 136 #endif 137 size_t leftover; 138 unsigned char buffer[POLY1305_BLOCK_SIZE]; 139 unsigned char finished; 140 #endif /* WOLFSSL_ARMASM */ 141 } Poly1305; 142 143 /* does init */ 144 145 WOLFSSL_API int wc_Poly1305SetKey(Poly1305* poly1305, const byte* key, 146 word32 kySz); 147 WOLFSSL_API int wc_Poly1305Update(Poly1305* poly1305, const byte* m, word32 bytes); 148 WOLFSSL_API int wc_Poly1305Final(Poly1305* poly1305, byte* tag); 149 150 /* AEAD Functions */ 151 WOLFSSL_API int wc_Poly1305_Pad(Poly1305* ctx, word32 lenToPad); 152 WOLFSSL_API int wc_Poly1305_EncodeSizes(Poly1305* ctx, word32 aadSz, 153 word32 dataSz); 154 #ifdef WORD64_AVAILABLE 155 WOLFSSL_API int wc_Poly1305_EncodeSizes64(Poly1305* ctx, word64 aadSz, 156 word64 dataSz); 157 #endif 158 WOLFSSL_API int wc_Poly1305_MAC(Poly1305* ctx, const byte* additional, 159 word32 addSz, const byte* input, word32 sz, byte* tag, word32 tagSz); 160 161 #if defined(WOLFSSL_ARMASM) 162 #if defined(__aarch64__ ) 163 #define poly1305_blocks poly1305_blocks_aarch64 164 #define poly1305_block poly1305_block_aarch64 165 166 void poly1305_blocks_aarch64(Poly1305* ctx, const unsigned char *m, 167 size_t bytes); 168 void poly1305_block_aarch64(Poly1305* ctx, const unsigned char *m); 169 #else 170 #if defined(WOLFSSL_ARMASM_THUMB2) 171 #define poly1305_blocks poly1305_blocks_thumb2 172 #define poly1305_block poly1305_block_thumb2 173 174 void poly1305_blocks_thumb2(Poly1305* ctx, const unsigned char *m, 175 size_t bytes); 176 void poly1305_block_thumb2(Poly1305* ctx, const unsigned char *m); 177 178 void poly1305_blocks_thumb2_16(Poly1305* ctx, const unsigned char* m, 179 word32 len, int notLast); 180 #else 181 #define poly1305_blocks poly1305_blocks_arm32 182 #define poly1305_block poly1305_block_arm32 183 184 void poly1305_blocks_arm32(Poly1305* ctx, const unsigned char *m, size_t bytes); 185 void poly1305_block_arm32(Poly1305* ctx, const unsigned char *m); 186 187 void poly1305_arm32_blocks(Poly1305* ctx, const unsigned char* m, word32 len); 188 void poly1305_arm32_blocks_16(Poly1305* ctx, const unsigned char* m, word32 len, 189 int notLast); 190 #endif 191 void poly1305_set_key(Poly1305* ctx, const byte* key); 192 void poly1305_final(Poly1305* ctx, byte* mac); 193 #endif 194 #endif /* WOLFSSL_ARMASM */ 195 196 #if defined(WOLFSSL_RISCV_ASM) 197 #define poly1305_blocks poly1305_blocks_riscv64 198 #define poly1305_block poly1305_block_riscv64 199 200 void poly1305_blocks_riscv64(Poly1305* ctx, const unsigned char *m, 201 size_t bytes); 202 void poly1305_block_riscv64(Poly1305* ctx, const unsigned char *m); 203 #endif 204 205 #ifdef __cplusplus 206 } /* extern "C" */ 207 #endif 208 209 #endif /* HAVE_POLY1305 */ 210 #endif /* WOLF_CRYPT_POLY1305_H */