OLD | NEW |
(Empty) | |
| 1 /* ==================================================================== |
| 2 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions |
| 6 * are met: |
| 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * |
| 11 * 2. Redistributions in binary form must reproduce the above copyright |
| 12 * notice, this list of conditions and the following disclaimer in |
| 13 * the documentation and/or other materials provided with the |
| 14 * distribution. |
| 15 * |
| 16 * 3. All advertising materials mentioning features or use of this |
| 17 * software must display the following acknowledgment: |
| 18 * "This product includes software developed by the OpenSSL Project |
| 19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 20 * |
| 21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 * endorse or promote products derived from this software without |
| 23 * prior written permission. For written permission, please contact |
| 24 * licensing@OpenSSL.org. |
| 25 * |
| 26 * 5. Products derived from this software may not be called "OpenSSL" |
| 27 * nor may "OpenSSL" appear in their names without prior written |
| 28 * permission of the OpenSSL Project. |
| 29 * |
| 30 * 6. Redistributions of any form whatsoever must retain the following |
| 31 * acknowledgment: |
| 32 * "This product includes software developed by the OpenSSL Project |
| 33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 34 * |
| 35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 * ==================================================================== |
| 48 */ |
| 49 |
| 50 /* This implementation of poly1305 is by Andrew Moon |
| 51 * (https://github.com/floodyberry/poly1305-donna) and released as public |
| 52 * domain. */ |
| 53 |
| 54 #include <string.h> |
| 55 #include <stdint.h> |
| 56 #include <openssl/opensslconf.h> |
| 57 |
| 58 #if !defined(OPENSSL_NO_POLY1305) |
| 59 |
| 60 #include <openssl/poly1305.h> |
| 61 |
| 62 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_6
4__) |
| 63 /* We can assume little-endian. */ |
| 64 static uint32_t U8TO32_LE(const unsigned char *m) |
| 65 { |
| 66 uint32_t r; |
| 67 memcpy(&r, m, sizeof(r)); |
| 68 return r; |
| 69 } |
| 70 |
| 71 static void U32TO8_LE(unsigned char *m, uint32_t v) |
| 72 { |
| 73 memcpy(m, &v, sizeof(v)); |
| 74 } |
| 75 #else |
| 76 static uint32_t U8TO32_LE(const unsigned char *m) |
| 77 { |
| 78 return (uint32_t)m[0] | |
| 79 (uint32_t)m[1] << 8 | |
| 80 (uint32_t)m[2] << 16 | |
| 81 (uint32_t)m[3] << 24; |
| 82 } |
| 83 |
| 84 static void U32TO8_LE(unsigned char *m, uint32_t v) |
| 85 { |
| 86 m[0] = v; |
| 87 m[1] = v >> 8; |
| 88 m[2] = v >> 16; |
| 89 m[3] = v >> 24; |
| 90 } |
| 91 #endif |
| 92 |
| 93 #if __arm__ |
| 94 void CRYPTO_poly1305_init_neon(poly1305_state* state, |
| 95 const unsigned char key[32]); |
| 96 |
| 97 void CRYPTO_poly1305_update_neon(poly1305_state* state, |
| 98 const unsigned char *in, |
| 99 size_t in_len); |
| 100 |
| 101 void CRYPTO_poly1305_finish_neon(poly1305_state* state, unsigned char mac[16]); |
| 102 #endif |
| 103 |
| 104 static uint64_t |
| 105 mul32x32_64(uint32_t a, uint32_t b) |
| 106 { |
| 107 return (uint64_t)a * b; |
| 108 } |
| 109 |
| 110 |
| 111 struct poly1305_state_st |
| 112 { |
| 113 uint32_t r0,r1,r2,r3,r4; |
| 114 uint32_t s1,s2,s3,s4; |
| 115 uint32_t h0,h1,h2,h3,h4; |
| 116 unsigned char buf[16]; |
| 117 unsigned int buf_used; |
| 118 unsigned char key[16]; |
| 119 }; |
| 120 |
| 121 /* poly1305_blocks updates |state| given some amount of input data. This |
| 122 * function may only be called with a |len| that is not a multiple of 16 at the |
| 123 * end of the data. Otherwise the input must be buffered into 16 byte blocks. |
| 124 * */ |
| 125 static void poly1305_update(struct poly1305_state_st *state, |
| 126 const unsigned char *in, size_t len) |
| 127 { |
| 128 uint32_t t0,t1,t2,t3; |
| 129 uint64_t t[5]; |
| 130 uint32_t b; |
| 131 uint64_t c; |
| 132 size_t j; |
| 133 unsigned char mp[16]; |
| 134 |
| 135 if (len < 16) |
| 136 goto poly1305_donna_atmost15bytes; |
| 137 |
| 138 poly1305_donna_16bytes: |
| 139 t0 = U8TO32_LE(in); |
| 140 t1 = U8TO32_LE(in+4); |
| 141 t2 = U8TO32_LE(in+8); |
| 142 t3 = U8TO32_LE(in+12); |
| 143 |
| 144 in += 16; |
| 145 len -= 16; |
| 146 |
| 147 state->h0 += t0 & 0x3ffffff; |
| 148 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; |
| 149 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; |
| 150 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; |
| 151 state->h4 += (t3 >> 8) | (1 << 24); |
| 152 |
| 153 poly1305_donna_mul: |
| 154 t[0] = mul32x32_64(state->h0,state->r0) + |
| 155 mul32x32_64(state->h1,state->s4) + |
| 156 mul32x32_64(state->h2,state->s3) + |
| 157 mul32x32_64(state->h3,state->s2) + |
| 158 mul32x32_64(state->h4,state->s1); |
| 159 t[1] = mul32x32_64(state->h0,state->r1) + |
| 160 mul32x32_64(state->h1,state->r0) + |
| 161 mul32x32_64(state->h2,state->s4) + |
| 162 mul32x32_64(state->h3,state->s3) + |
| 163 mul32x32_64(state->h4,state->s2); |
| 164 t[2] = mul32x32_64(state->h0,state->r2) + |
| 165 mul32x32_64(state->h1,state->r1) + |
| 166 mul32x32_64(state->h2,state->r0) + |
| 167 mul32x32_64(state->h3,state->s4) + |
| 168 mul32x32_64(state->h4,state->s3); |
| 169 t[3] = mul32x32_64(state->h0,state->r3) + |
| 170 mul32x32_64(state->h1,state->r2) + |
| 171 mul32x32_64(state->h2,state->r1) + |
| 172 mul32x32_64(state->h3,state->r0) + |
| 173 mul32x32_64(state->h4,state->s4); |
| 174 t[4] = mul32x32_64(state->h0,state->r4) + |
| 175 mul32x32_64(state->h1,state->r3) + |
| 176 mul32x32_64(state->h2,state->r2) + |
| 177 mul32x32_64(state->h3,state->r1) + |
| 178 mul32x32_64(state->h4,state->r0); |
| 179 |
| 180 state->h0 = (uint32_t)t[0] & 0x3ffffff; c = (t[0] >
> 26); |
| 181 t[1] += c; state->h1 = (uint32_t)t[1] & 0x3ffffff; b = (uint32_t)(t[1] >
> 26); |
| 182 t[2] += b; state->h2 = (uint32_t)t[2] & 0x3ffffff; b = (uint32_t)(t[2] >
> 26); |
| 183 t[3] += b; state->h3 = (uint32_t)t[3] & 0x3ffffff; b = (uint32_t)(t[3] >
> 26); |
| 184 t[4] += b; state->h4 = (uint32_t)t[4] & 0x3ffffff; b = (uint32_t)(t[4] >
> 26); |
| 185 state->h0 += b * 5; |
| 186 |
| 187 if (len >= 16) |
| 188 goto poly1305_donna_16bytes; |
| 189 |
| 190 /* final bytes */ |
| 191 poly1305_donna_atmost15bytes: |
| 192 if (!len) |
| 193 return; |
| 194 |
| 195 for (j = 0; j < len; j++) |
| 196 mp[j] = in[j]; |
| 197 mp[j++] = 1; |
| 198 for (; j < 16; j++) |
| 199 mp[j] = 0; |
| 200 len = 0; |
| 201 |
| 202 t0 = U8TO32_LE(mp+0); |
| 203 t1 = U8TO32_LE(mp+4); |
| 204 t2 = U8TO32_LE(mp+8); |
| 205 t3 = U8TO32_LE(mp+12); |
| 206 |
| 207 state->h0 += t0 & 0x3ffffff; |
| 208 state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff; |
| 209 state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff; |
| 210 state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff; |
| 211 state->h4 += (t3 >> 8); |
| 212 |
| 213 goto poly1305_donna_mul; |
| 214 } |
| 215 |
| 216 void CRYPTO_poly1305_init(poly1305_state *statep, const unsigned char key[32]) |
| 217 { |
| 218 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; |
| 219 uint32_t t0,t1,t2,t3; |
| 220 |
| 221 #if __arm__ |
| 222 if (CRYPTO_is_NEON_capable()) |
| 223 { |
| 224 CRYPTO_poly1305_init_neon(statep, key); |
| 225 return; |
| 226 } |
| 227 #endif |
| 228 |
| 229 t0 = U8TO32_LE(key+0); |
| 230 t1 = U8TO32_LE(key+4); |
| 231 t2 = U8TO32_LE(key+8); |
| 232 t3 = U8TO32_LE(key+12); |
| 233 |
| 234 /* precompute multipliers */ |
| 235 state->r0 = t0 & 0x3ffffff; t0 >>= 26; t0 |= t1 << 6; |
| 236 state->r1 = t0 & 0x3ffff03; t1 >>= 20; t1 |= t2 << 12; |
| 237 state->r2 = t1 & 0x3ffc0ff; t2 >>= 14; t2 |= t3 << 18; |
| 238 state->r3 = t2 & 0x3f03fff; t3 >>= 8; |
| 239 state->r4 = t3 & 0x00fffff; |
| 240 |
| 241 state->s1 = state->r1 * 5; |
| 242 state->s2 = state->r2 * 5; |
| 243 state->s3 = state->r3 * 5; |
| 244 state->s4 = state->r4 * 5; |
| 245 |
| 246 /* init state */ |
| 247 state->h0 = 0; |
| 248 state->h1 = 0; |
| 249 state->h2 = 0; |
| 250 state->h3 = 0; |
| 251 state->h4 = 0; |
| 252 |
| 253 state->buf_used = 0; |
| 254 memcpy(state->key, key + 16, sizeof(state->key)); |
| 255 } |
| 256 |
| 257 void CRYPTO_poly1305_update(poly1305_state *statep, const unsigned char *in, |
| 258 size_t in_len) |
| 259 { |
| 260 unsigned int i; |
| 261 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; |
| 262 |
| 263 #if __arm__ |
| 264 if (CRYPTO_is_NEON_capable()) |
| 265 { |
| 266 CRYPTO_poly1305_update_neon(statep, in, in_len); |
| 267 return; |
| 268 } |
| 269 #endif |
| 270 |
| 271 if (state->buf_used) |
| 272 { |
| 273 unsigned int todo = 16 - state->buf_used; |
| 274 if (todo > in_len) |
| 275 todo = in_len; |
| 276 for (i = 0; i < todo; i++) |
| 277 state->buf[state->buf_used + i] = in[i]; |
| 278 state->buf_used += todo; |
| 279 in_len -= todo; |
| 280 in += todo; |
| 281 |
| 282 if (state->buf_used == 16) |
| 283 { |
| 284 poly1305_update(state, state->buf, 16); |
| 285 state->buf_used = 0; |
| 286 } |
| 287 } |
| 288 |
| 289 if (in_len >= 16) |
| 290 { |
| 291 size_t todo = in_len & ~0xf; |
| 292 poly1305_update(state, in, todo); |
| 293 in += todo; |
| 294 in_len &= 0xf; |
| 295 } |
| 296 |
| 297 if (in_len) |
| 298 { |
| 299 for (i = 0; i < in_len; i++) |
| 300 state->buf[i] = in[i]; |
| 301 state->buf_used = in_len; |
| 302 } |
| 303 } |
| 304 |
| 305 void CRYPTO_poly1305_finish(poly1305_state *statep, unsigned char mac[16]) |
| 306 { |
| 307 struct poly1305_state_st *state = (struct poly1305_state_st*) statep; |
| 308 uint64_t f0,f1,f2,f3; |
| 309 uint32_t g0,g1,g2,g3,g4; |
| 310 uint32_t b, nb; |
| 311 |
| 312 #if __arm__ |
| 313 if (CRYPTO_is_NEON_capable()) |
| 314 { |
| 315 CRYPTO_poly1305_finish_neon(statep, mac); |
| 316 return; |
| 317 } |
| 318 #endif |
| 319 |
| 320 if (state->buf_used) |
| 321 poly1305_update(state, state->buf, state->buf_used); |
| 322 |
| 323 b = state->h0 >> 26; state->h0 = state->h0 & 0x3ffff
ff; |
| 324 state->h1 += b; b = state->h1 >> 26; state->h1 = state->h1 & 0x3ffff
ff; |
| 325 state->h2 += b; b = state->h2 >> 26; state->h2 = state->h2 & 0x3ffff
ff; |
| 326 state->h3 += b; b = state->h3 >> 26; state->h3 = state->h3 & 0x3ffff
ff; |
| 327 state->h4 += b; b = state->h4 >> 26; state->h4 = state->h4 & 0x3ffff
ff; |
| 328 state->h0 += b * 5; |
| 329 |
| 330 g0 = state->h0 + 5; b = g0 >> 26; g0 &= 0x3ffffff; |
| 331 g1 = state->h1 + b; b = g1 >> 26; g1 &= 0x3ffffff; |
| 332 g2 = state->h2 + b; b = g2 >> 26; g2 &= 0x3ffffff; |
| 333 g3 = state->h3 + b; b = g3 >> 26; g3 &= 0x3ffffff; |
| 334 g4 = state->h4 + b - (1 << 26); |
| 335 |
| 336 b = (g4 >> 31) - 1; |
| 337 nb = ~b; |
| 338 state->h0 = (state->h0 & nb) | (g0 & b); |
| 339 state->h1 = (state->h1 & nb) | (g1 & b); |
| 340 state->h2 = (state->h2 & nb) | (g2 & b); |
| 341 state->h3 = (state->h3 & nb) | (g3 & b); |
| 342 state->h4 = (state->h4 & nb) | (g4 & b); |
| 343 |
| 344 f0 = ((state->h0 ) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&stat
e->key[0]); |
| 345 f1 = ((state->h1 >> 6) | (state->h2 << 20)) + (uint64_t)U8TO32_LE(&stat
e->key[4]); |
| 346 f2 = ((state->h2 >> 12) | (state->h3 << 14)) + (uint64_t)U8TO32_LE(&stat
e->key[8]); |
| 347 f3 = ((state->h3 >> 18) | (state->h4 << 8)) + (uint64_t)U8TO32_LE(&stat
e->key[12]); |
| 348 |
| 349 U32TO8_LE(&mac[ 0], f0); f1 += (f0 >> 32); |
| 350 U32TO8_LE(&mac[ 4], f1); f2 += (f1 >> 32); |
| 351 U32TO8_LE(&mac[ 8], f2); f3 += (f2 >> 32); |
| 352 U32TO8_LE(&mac[12], f3); |
| 353 } |
| 354 |
| 355 #endif /* !OPENSSL_NO_POLY1305 */ |
OLD | NEW |