diff options
| -rw-r--r-- | src/lib/libcrypto/crypto/Makefile | 7 | ||||
| -rw-r--r-- | src/lib/libcrypto/poly1305/poly1305-donna.c | 263 | ||||
| -rw-r--r-- | src/lib/libcrypto/poly1305/poly1305.c | 37 | ||||
| -rw-r--r-- | src/lib/libcrypto/poly1305/poly1305.h | 48 | ||||
| -rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305-donna.c | 263 | ||||
| -rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305.c | 37 | ||||
| -rw-r--r-- | src/lib/libssl/src/crypto/poly1305/poly1305.h | 48 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/Makefile | 3 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/poly1305/Makefile | 7 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/poly1305/poly1305test.c | 166 |
10 files changed, 877 insertions, 2 deletions
diff --git a/src/lib/libcrypto/crypto/Makefile b/src/lib/libcrypto/crypto/Makefile index ce04ac9e61..6d9eac1383 100644 --- a/src/lib/libcrypto/crypto/Makefile +++ b/src/lib/libcrypto/crypto/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.32 2014/05/12 19:14:14 miod Exp $ | 1 | # $OpenBSD: Makefile,v 1.33 2014/05/14 14:46:35 jsing Exp $ |
| 2 | 2 | ||
| 3 | LIB= crypto | 3 | LIB= crypto |
| 4 | 4 | ||
| @@ -198,6 +198,9 @@ SRCS+= p12_utl.c p12_npas.c pk12err.c p12_p8d.c p12_p8e.c | |||
| 198 | SRCS+= pk7_asn1.c pk7_lib.c pkcs7err.c pk7_doit.c pk7_smime.c pk7_attr.c | 198 | SRCS+= pk7_asn1.c pk7_lib.c pkcs7err.c pk7_doit.c pk7_smime.c pk7_attr.c |
| 199 | SRCS+= pk7_mime.c bio_pk7.c | 199 | SRCS+= pk7_mime.c bio_pk7.c |
| 200 | 200 | ||
| 201 | # poly1305/ | ||
| 202 | SRCS+= poly1305.c | ||
| 203 | |||
| 201 | # rand/ | 204 | # rand/ |
| 202 | SRCS+= randfile.c rand_lib.c rand_err.c | 205 | SRCS+= randfile.c rand_lib.c rand_err.c |
| 203 | 206 | ||
| @@ -301,6 +304,7 @@ SRCS+= v3_asid.c v3_addr.c | |||
| 301 | ${LCRYPTO_SRC}/perlasm \ | 304 | ${LCRYPTO_SRC}/perlasm \ |
| 302 | ${LCRYPTO_SRC}/pkcs12 \ | 305 | ${LCRYPTO_SRC}/pkcs12 \ |
| 303 | ${LCRYPTO_SRC}/pkcs7 \ | 306 | ${LCRYPTO_SRC}/pkcs7 \ |
| 307 | ${LCRYPTO_SRC}/poly1305 \ | ||
| 304 | ${LCRYPTO_SRC}/rand \ | 308 | ${LCRYPTO_SRC}/rand \ |
| 305 | ${LCRYPTO_SRC}/rc2 \ | 309 | ${LCRYPTO_SRC}/rc2 \ |
| 306 | ${LCRYPTO_SRC}/rc4 \ | 310 | ${LCRYPTO_SRC}/rc4 \ |
| @@ -363,6 +367,7 @@ HDRS=\ | |||
| 363 | crypto/pem/pem2.h \ | 367 | crypto/pem/pem2.h \ |
| 364 | crypto/pkcs12/pkcs12.h \ | 368 | crypto/pkcs12/pkcs12.h \ |
| 365 | crypto/pkcs7/pkcs7.h \ | 369 | crypto/pkcs7/pkcs7.h \ |
| 370 | crypto/poly1305/poly1305.h \ | ||
| 366 | crypto/rand/rand.h \ | 371 | crypto/rand/rand.h \ |
| 367 | crypto/rc2/rc2.h \ | 372 | crypto/rc2/rc2.h \ |
| 368 | crypto/rc4/rc4.h \ | 373 | crypto/rc4/rc4.h \ |
diff --git a/src/lib/libcrypto/poly1305/poly1305-donna.c b/src/lib/libcrypto/poly1305/poly1305-donna.c new file mode 100644 index 0000000000..642a30b376 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305-donna.c | |||
| @@ -0,0 +1,263 @@ | |||
| 1 | /* | ||
| 2 | * Public Domain poly1305 from Andrew Moon | ||
| 3 | * Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: | ||
| 4 | * https://github.com/floodyberry/poly1305-donna | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <stddef.h> | ||
| 8 | |||
| 9 | static inline void poly1305_init(poly1305_context *ctx, | ||
| 10 | const unsigned char key[32]); | ||
| 11 | static inline void poly1305_update(poly1305_context *ctx, | ||
| 12 | const unsigned char *m, size_t bytes); | ||
| 13 | static inline void poly1305_finish(poly1305_context *ctx, | ||
| 14 | unsigned char mac[16]); | ||
| 15 | |||
| 16 | /* | ||
| 17 | * poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication | ||
| 18 | * and 64 bit addition. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #define poly1305_block_size 16 | ||
| 22 | |||
| 23 | /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ | ||
| 24 | typedef struct poly1305_state_internal_t { | ||
| 25 | unsigned long r[5]; | ||
| 26 | unsigned long h[5]; | ||
| 27 | unsigned long pad[4]; | ||
| 28 | size_t leftover; | ||
| 29 | unsigned char buffer[poly1305_block_size]; | ||
| 30 | unsigned char final; | ||
| 31 | } poly1305_state_internal_t; | ||
| 32 | |||
| 33 | /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ | ||
| 34 | static unsigned long | ||
| 35 | U8TO32(const unsigned char *p) { | ||
| 36 | return | ||
| 37 | (((unsigned long)(p[0] & 0xff) ) | | ||
| 38 | ((unsigned long)(p[1] & 0xff) << 8) | | ||
| 39 | ((unsigned long)(p[2] & 0xff) << 16) | | ||
| 40 | ((unsigned long)(p[3] & 0xff) << 24)); | ||
| 41 | } | ||
| 42 | |||
| 43 | /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ | ||
| 44 | static void | ||
| 45 | U32TO8(unsigned char *p, unsigned long v) { | ||
| 46 | p[0] = (v ) & 0xff; | ||
| 47 | p[1] = (v >> 8) & 0xff; | ||
| 48 | p[2] = (v >> 16) & 0xff; | ||
| 49 | p[3] = (v >> 24) & 0xff; | ||
| 50 | } | ||
| 51 | |||
| 52 | static inline void | ||
| 53 | poly1305_init(poly1305_context *ctx, const unsigned char key[32]) { | ||
| 54 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 55 | |||
| 56 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ | ||
| 57 | st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff; | ||
| 58 | st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03; | ||
| 59 | st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff; | ||
| 60 | st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff; | ||
| 61 | st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; | ||
| 62 | |||
| 63 | /* h = 0 */ | ||
| 64 | st->h[0] = 0; | ||
| 65 | st->h[1] = 0; | ||
| 66 | st->h[2] = 0; | ||
| 67 | st->h[3] = 0; | ||
| 68 | st->h[4] = 0; | ||
| 69 | |||
| 70 | /* save pad for later */ | ||
| 71 | st->pad[0] = U8TO32(&key[16]); | ||
| 72 | st->pad[1] = U8TO32(&key[20]); | ||
| 73 | st->pad[2] = U8TO32(&key[24]); | ||
| 74 | st->pad[3] = U8TO32(&key[28]); | ||
| 75 | |||
| 76 | st->leftover = 0; | ||
| 77 | st->final = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | static void | ||
| 81 | poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) { | ||
| 82 | const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */ | ||
| 83 | unsigned long r0,r1,r2,r3,r4; | ||
| 84 | unsigned long s1,s2,s3,s4; | ||
| 85 | unsigned long h0,h1,h2,h3,h4; | ||
| 86 | unsigned long long d0,d1,d2,d3,d4; | ||
| 87 | unsigned long c; | ||
| 88 | |||
| 89 | r0 = st->r[0]; | ||
| 90 | r1 = st->r[1]; | ||
| 91 | r2 = st->r[2]; | ||
| 92 | r3 = st->r[3]; | ||
| 93 | r4 = st->r[4]; | ||
| 94 | |||
| 95 | s1 = r1 * 5; | ||
| 96 | s2 = r2 * 5; | ||
| 97 | s3 = r3 * 5; | ||
| 98 | s4 = r4 * 5; | ||
| 99 | |||
| 100 | h0 = st->h[0]; | ||
| 101 | h1 = st->h[1]; | ||
| 102 | h2 = st->h[2]; | ||
| 103 | h3 = st->h[3]; | ||
| 104 | h4 = st->h[4]; | ||
| 105 | |||
| 106 | while (bytes >= poly1305_block_size) { | ||
| 107 | /* h += m[i] */ | ||
| 108 | h0 += (U8TO32(m+ 0) ) & 0x3ffffff; | ||
| 109 | h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff; | ||
| 110 | h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff; | ||
| 111 | h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff; | ||
| 112 | h4 += (U8TO32(m+12) >> 8) | hibit; | ||
| 113 | |||
| 114 | /* h *= r */ | ||
| 115 | d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1); | ||
| 116 | d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2); | ||
| 117 | d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3); | ||
| 118 | d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4); | ||
| 119 | d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0); | ||
| 120 | |||
| 121 | /* (partial) h %= p */ | ||
| 122 | c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff; | ||
| 123 | d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff; | ||
| 124 | d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff; | ||
| 125 | d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff; | ||
| 126 | d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff; | ||
| 127 | h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; | ||
| 128 | h1 += c; | ||
| 129 | |||
| 130 | m += poly1305_block_size; | ||
| 131 | bytes -= poly1305_block_size; | ||
| 132 | } | ||
| 133 | |||
| 134 | st->h[0] = h0; | ||
| 135 | st->h[1] = h1; | ||
| 136 | st->h[2] = h2; | ||
| 137 | st->h[3] = h3; | ||
| 138 | st->h[4] = h4; | ||
| 139 | } | ||
| 140 | |||
| 141 | static inline void | ||
| 142 | poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) { | ||
| 143 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 144 | size_t i; | ||
| 145 | |||
| 146 | /* handle leftover */ | ||
| 147 | if (st->leftover) { | ||
| 148 | size_t want = (poly1305_block_size - st->leftover); | ||
| 149 | if (want > bytes) | ||
| 150 | want = bytes; | ||
| 151 | for (i = 0; i < want; i++) | ||
| 152 | st->buffer[st->leftover + i] = m[i]; | ||
| 153 | bytes -= want; | ||
| 154 | m += want; | ||
| 155 | st->leftover += want; | ||
| 156 | if (st->leftover < poly1305_block_size) | ||
| 157 | return; | ||
| 158 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
| 159 | st->leftover = 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | /* process full blocks */ | ||
| 163 | if (bytes >= poly1305_block_size) { | ||
| 164 | size_t want = (bytes & ~(poly1305_block_size - 1)); | ||
| 165 | poly1305_blocks(st, m, want); | ||
| 166 | m += want; | ||
| 167 | bytes -= want; | ||
| 168 | } | ||
| 169 | |||
| 170 | /* store leftover */ | ||
| 171 | if (bytes) { | ||
| 172 | for (i = 0; i < bytes; i++) | ||
| 173 | st->buffer[st->leftover + i] = m[i]; | ||
| 174 | st->leftover += bytes; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | static inline void | ||
| 179 | poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) { | ||
| 180 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 181 | unsigned long h0,h1,h2,h3,h4,c; | ||
| 182 | unsigned long g0,g1,g2,g3,g4; | ||
| 183 | unsigned long long f; | ||
| 184 | unsigned long mask; | ||
| 185 | |||
| 186 | /* process the remaining block */ | ||
| 187 | if (st->leftover) { | ||
| 188 | size_t i = st->leftover; | ||
| 189 | st->buffer[i++] = 1; | ||
| 190 | for (; i < poly1305_block_size; i++) | ||
| 191 | st->buffer[i] = 0; | ||
| 192 | st->final = 1; | ||
| 193 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
| 194 | } | ||
| 195 | |||
| 196 | /* fully carry h */ | ||
| 197 | h0 = st->h[0]; | ||
| 198 | h1 = st->h[1]; | ||
| 199 | h2 = st->h[2]; | ||
| 200 | h3 = st->h[3]; | ||
| 201 | h4 = st->h[4]; | ||
| 202 | |||
| 203 | c = h1 >> 26; h1 = h1 & 0x3ffffff; | ||
| 204 | h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; | ||
| 205 | h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; | ||
| 206 | h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; | ||
| 207 | h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; | ||
| 208 | h1 += c; | ||
| 209 | |||
| 210 | /* compute h + -p */ | ||
| 211 | g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; | ||
| 212 | g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; | ||
| 213 | g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; | ||
| 214 | g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; | ||
| 215 | g4 = h4 + c - (1 << 26); | ||
| 216 | |||
| 217 | /* select h if h < p, or h + -p if h >= p */ | ||
| 218 | mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1; | ||
| 219 | g0 &= mask; | ||
| 220 | g1 &= mask; | ||
| 221 | g2 &= mask; | ||
| 222 | g3 &= mask; | ||
| 223 | g4 &= mask; | ||
| 224 | mask = ~mask; | ||
| 225 | h0 = (h0 & mask) | g0; | ||
| 226 | h1 = (h1 & mask) | g1; | ||
| 227 | h2 = (h2 & mask) | g2; | ||
| 228 | h3 = (h3 & mask) | g3; | ||
| 229 | h4 = (h4 & mask) | g4; | ||
| 230 | |||
| 231 | /* h = h % (2^128) */ | ||
| 232 | h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; | ||
| 233 | h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; | ||
| 234 | h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; | ||
| 235 | h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; | ||
| 236 | |||
| 237 | /* mac = (h + pad) % (2^128) */ | ||
| 238 | f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f; | ||
| 239 | f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f; | ||
| 240 | f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f; | ||
| 241 | f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f; | ||
| 242 | |||
| 243 | U32TO8(mac + 0, h0); | ||
| 244 | U32TO8(mac + 4, h1); | ||
| 245 | U32TO8(mac + 8, h2); | ||
| 246 | U32TO8(mac + 12, h3); | ||
| 247 | |||
| 248 | /* zero out the state */ | ||
| 249 | st->h[0] = 0; | ||
| 250 | st->h[1] = 0; | ||
| 251 | st->h[2] = 0; | ||
| 252 | st->h[3] = 0; | ||
| 253 | st->h[4] = 0; | ||
| 254 | st->r[0] = 0; | ||
| 255 | st->r[1] = 0; | ||
| 256 | st->r[2] = 0; | ||
| 257 | st->r[3] = 0; | ||
| 258 | st->r[4] = 0; | ||
| 259 | st->pad[0] = 0; | ||
| 260 | st->pad[1] = 0; | ||
| 261 | st->pad[2] = 0; | ||
| 262 | st->pad[3] = 0; | ||
| 263 | } | ||
diff --git a/src/lib/libcrypto/poly1305/poly1305.c b/src/lib/libcrypto/poly1305/poly1305.c new file mode 100644 index 0000000000..96083fe7e9 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305.c | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "poly1305.h" | ||
| 18 | #include "poly1305-donna.c" | ||
| 19 | |||
| 20 | void | ||
| 21 | CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]) | ||
| 22 | { | ||
| 23 | poly1305_init(ctx, key); | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
| 28 | size_t len) | ||
| 29 | { | ||
| 30 | poly1305_update(ctx, in, len); | ||
| 31 | } | ||
| 32 | |||
| 33 | void | ||
| 34 | CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) | ||
| 35 | { | ||
| 36 | poly1305_finish(ctx, mac); | ||
| 37 | } | ||
diff --git a/src/lib/libcrypto/poly1305/poly1305.h b/src/lib/libcrypto/poly1305/poly1305.h new file mode 100644 index 0000000000..a4ccdd5d80 --- /dev/null +++ b/src/lib/libcrypto/poly1305/poly1305.h | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) Joel Sing <jsing@openbsd.org> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #ifndef HEADER_POLY1305_H | ||
| 18 | #define HEADER_POLY1305_H | ||
| 19 | |||
| 20 | #include <openssl/opensslconf.h> | ||
| 21 | |||
| 22 | #if defined(OPENSSL_NO_POLY1305) | ||
| 23 | #error Poly1305 is disabled. | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <stddef.h> | ||
| 27 | |||
| 28 | #ifdef __cplusplus | ||
| 29 | extern "C" { | ||
| 30 | #endif | ||
| 31 | |||
| 32 | typedef struct poly1305_context { | ||
| 33 | size_t aligner; | ||
| 34 | unsigned char opaque[136]; | ||
| 35 | } poly1305_context; | ||
| 36 | |||
| 37 | typedef struct poly1305_context poly1305_state; | ||
| 38 | |||
| 39 | void CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]); | ||
| 40 | void CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
| 41 | size_t len); | ||
| 42 | void CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]); | ||
| 43 | |||
| 44 | #ifdef __cplusplus | ||
| 45 | } | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #endif /* HEADER_POLY1305_H */ | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c b/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c new file mode 100644 index 0000000000..642a30b376 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305-donna.c | |||
| @@ -0,0 +1,263 @@ | |||
| 1 | /* | ||
| 2 | * Public Domain poly1305 from Andrew Moon | ||
| 3 | * Based on poly1305-donna.c, poly1305-donna-32.h and poly1305-donna.h from: | ||
| 4 | * https://github.com/floodyberry/poly1305-donna | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <stddef.h> | ||
| 8 | |||
| 9 | static inline void poly1305_init(poly1305_context *ctx, | ||
| 10 | const unsigned char key[32]); | ||
| 11 | static inline void poly1305_update(poly1305_context *ctx, | ||
| 12 | const unsigned char *m, size_t bytes); | ||
| 13 | static inline void poly1305_finish(poly1305_context *ctx, | ||
| 14 | unsigned char mac[16]); | ||
| 15 | |||
| 16 | /* | ||
| 17 | * poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication | ||
| 18 | * and 64 bit addition. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #define poly1305_block_size 16 | ||
| 22 | |||
| 23 | /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ | ||
| 24 | typedef struct poly1305_state_internal_t { | ||
| 25 | unsigned long r[5]; | ||
| 26 | unsigned long h[5]; | ||
| 27 | unsigned long pad[4]; | ||
| 28 | size_t leftover; | ||
| 29 | unsigned char buffer[poly1305_block_size]; | ||
| 30 | unsigned char final; | ||
| 31 | } poly1305_state_internal_t; | ||
| 32 | |||
| 33 | /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */ | ||
| 34 | static unsigned long | ||
| 35 | U8TO32(const unsigned char *p) { | ||
| 36 | return | ||
| 37 | (((unsigned long)(p[0] & 0xff) ) | | ||
| 38 | ((unsigned long)(p[1] & 0xff) << 8) | | ||
| 39 | ((unsigned long)(p[2] & 0xff) << 16) | | ||
| 40 | ((unsigned long)(p[3] & 0xff) << 24)); | ||
| 41 | } | ||
| 42 | |||
| 43 | /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */ | ||
| 44 | static void | ||
| 45 | U32TO8(unsigned char *p, unsigned long v) { | ||
| 46 | p[0] = (v ) & 0xff; | ||
| 47 | p[1] = (v >> 8) & 0xff; | ||
| 48 | p[2] = (v >> 16) & 0xff; | ||
| 49 | p[3] = (v >> 24) & 0xff; | ||
| 50 | } | ||
| 51 | |||
| 52 | static inline void | ||
| 53 | poly1305_init(poly1305_context *ctx, const unsigned char key[32]) { | ||
| 54 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 55 | |||
| 56 | /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ | ||
| 57 | st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff; | ||
| 58 | st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03; | ||
| 59 | st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff; | ||
| 60 | st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff; | ||
| 61 | st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff; | ||
| 62 | |||
| 63 | /* h = 0 */ | ||
| 64 | st->h[0] = 0; | ||
| 65 | st->h[1] = 0; | ||
| 66 | st->h[2] = 0; | ||
| 67 | st->h[3] = 0; | ||
| 68 | st->h[4] = 0; | ||
| 69 | |||
| 70 | /* save pad for later */ | ||
| 71 | st->pad[0] = U8TO32(&key[16]); | ||
| 72 | st->pad[1] = U8TO32(&key[20]); | ||
| 73 | st->pad[2] = U8TO32(&key[24]); | ||
| 74 | st->pad[3] = U8TO32(&key[28]); | ||
| 75 | |||
| 76 | st->leftover = 0; | ||
| 77 | st->final = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | static void | ||
| 81 | poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) { | ||
| 82 | const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */ | ||
| 83 | unsigned long r0,r1,r2,r3,r4; | ||
| 84 | unsigned long s1,s2,s3,s4; | ||
| 85 | unsigned long h0,h1,h2,h3,h4; | ||
| 86 | unsigned long long d0,d1,d2,d3,d4; | ||
| 87 | unsigned long c; | ||
| 88 | |||
| 89 | r0 = st->r[0]; | ||
| 90 | r1 = st->r[1]; | ||
| 91 | r2 = st->r[2]; | ||
| 92 | r3 = st->r[3]; | ||
| 93 | r4 = st->r[4]; | ||
| 94 | |||
| 95 | s1 = r1 * 5; | ||
| 96 | s2 = r2 * 5; | ||
| 97 | s3 = r3 * 5; | ||
| 98 | s4 = r4 * 5; | ||
| 99 | |||
| 100 | h0 = st->h[0]; | ||
| 101 | h1 = st->h[1]; | ||
| 102 | h2 = st->h[2]; | ||
| 103 | h3 = st->h[3]; | ||
| 104 | h4 = st->h[4]; | ||
| 105 | |||
| 106 | while (bytes >= poly1305_block_size) { | ||
| 107 | /* h += m[i] */ | ||
| 108 | h0 += (U8TO32(m+ 0) ) & 0x3ffffff; | ||
| 109 | h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff; | ||
| 110 | h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff; | ||
| 111 | h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff; | ||
| 112 | h4 += (U8TO32(m+12) >> 8) | hibit; | ||
| 113 | |||
| 114 | /* h *= r */ | ||
| 115 | d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1); | ||
| 116 | d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2); | ||
| 117 | d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3); | ||
| 118 | d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4); | ||
| 119 | d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0); | ||
| 120 | |||
| 121 | /* (partial) h %= p */ | ||
| 122 | c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff; | ||
| 123 | d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff; | ||
| 124 | d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff; | ||
| 125 | d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff; | ||
| 126 | d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff; | ||
| 127 | h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; | ||
| 128 | h1 += c; | ||
| 129 | |||
| 130 | m += poly1305_block_size; | ||
| 131 | bytes -= poly1305_block_size; | ||
| 132 | } | ||
| 133 | |||
| 134 | st->h[0] = h0; | ||
| 135 | st->h[1] = h1; | ||
| 136 | st->h[2] = h2; | ||
| 137 | st->h[3] = h3; | ||
| 138 | st->h[4] = h4; | ||
| 139 | } | ||
| 140 | |||
| 141 | static inline void | ||
| 142 | poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) { | ||
| 143 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 144 | size_t i; | ||
| 145 | |||
| 146 | /* handle leftover */ | ||
| 147 | if (st->leftover) { | ||
| 148 | size_t want = (poly1305_block_size - st->leftover); | ||
| 149 | if (want > bytes) | ||
| 150 | want = bytes; | ||
| 151 | for (i = 0; i < want; i++) | ||
| 152 | st->buffer[st->leftover + i] = m[i]; | ||
| 153 | bytes -= want; | ||
| 154 | m += want; | ||
| 155 | st->leftover += want; | ||
| 156 | if (st->leftover < poly1305_block_size) | ||
| 157 | return; | ||
| 158 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
| 159 | st->leftover = 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | /* process full blocks */ | ||
| 163 | if (bytes >= poly1305_block_size) { | ||
| 164 | size_t want = (bytes & ~(poly1305_block_size - 1)); | ||
| 165 | poly1305_blocks(st, m, want); | ||
| 166 | m += want; | ||
| 167 | bytes -= want; | ||
| 168 | } | ||
| 169 | |||
| 170 | /* store leftover */ | ||
| 171 | if (bytes) { | ||
| 172 | for (i = 0; i < bytes; i++) | ||
| 173 | st->buffer[st->leftover + i] = m[i]; | ||
| 174 | st->leftover += bytes; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | static inline void | ||
| 179 | poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) { | ||
| 180 | poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; | ||
| 181 | unsigned long h0,h1,h2,h3,h4,c; | ||
| 182 | unsigned long g0,g1,g2,g3,g4; | ||
| 183 | unsigned long long f; | ||
| 184 | unsigned long mask; | ||
| 185 | |||
| 186 | /* process the remaining block */ | ||
| 187 | if (st->leftover) { | ||
| 188 | size_t i = st->leftover; | ||
| 189 | st->buffer[i++] = 1; | ||
| 190 | for (; i < poly1305_block_size; i++) | ||
| 191 | st->buffer[i] = 0; | ||
| 192 | st->final = 1; | ||
| 193 | poly1305_blocks(st, st->buffer, poly1305_block_size); | ||
| 194 | } | ||
| 195 | |||
| 196 | /* fully carry h */ | ||
| 197 | h0 = st->h[0]; | ||
| 198 | h1 = st->h[1]; | ||
| 199 | h2 = st->h[2]; | ||
| 200 | h3 = st->h[3]; | ||
| 201 | h4 = st->h[4]; | ||
| 202 | |||
| 203 | c = h1 >> 26; h1 = h1 & 0x3ffffff; | ||
| 204 | h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; | ||
| 205 | h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; | ||
| 206 | h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; | ||
| 207 | h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; | ||
| 208 | h1 += c; | ||
| 209 | |||
| 210 | /* compute h + -p */ | ||
| 211 | g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff; | ||
| 212 | g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff; | ||
| 213 | g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff; | ||
| 214 | g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff; | ||
| 215 | g4 = h4 + c - (1 << 26); | ||
| 216 | |||
| 217 | /* select h if h < p, or h + -p if h >= p */ | ||
| 218 | mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1; | ||
| 219 | g0 &= mask; | ||
| 220 | g1 &= mask; | ||
| 221 | g2 &= mask; | ||
| 222 | g3 &= mask; | ||
| 223 | g4 &= mask; | ||
| 224 | mask = ~mask; | ||
| 225 | h0 = (h0 & mask) | g0; | ||
| 226 | h1 = (h1 & mask) | g1; | ||
| 227 | h2 = (h2 & mask) | g2; | ||
| 228 | h3 = (h3 & mask) | g3; | ||
| 229 | h4 = (h4 & mask) | g4; | ||
| 230 | |||
| 231 | /* h = h % (2^128) */ | ||
| 232 | h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; | ||
| 233 | h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; | ||
| 234 | h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; | ||
| 235 | h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; | ||
| 236 | |||
| 237 | /* mac = (h + pad) % (2^128) */ | ||
| 238 | f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f; | ||
| 239 | f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f; | ||
| 240 | f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f; | ||
| 241 | f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f; | ||
| 242 | |||
| 243 | U32TO8(mac + 0, h0); | ||
| 244 | U32TO8(mac + 4, h1); | ||
| 245 | U32TO8(mac + 8, h2); | ||
| 246 | U32TO8(mac + 12, h3); | ||
| 247 | |||
| 248 | /* zero out the state */ | ||
| 249 | st->h[0] = 0; | ||
| 250 | st->h[1] = 0; | ||
| 251 | st->h[2] = 0; | ||
| 252 | st->h[3] = 0; | ||
| 253 | st->h[4] = 0; | ||
| 254 | st->r[0] = 0; | ||
| 255 | st->r[1] = 0; | ||
| 256 | st->r[2] = 0; | ||
| 257 | st->r[3] = 0; | ||
| 258 | st->r[4] = 0; | ||
| 259 | st->pad[0] = 0; | ||
| 260 | st->pad[1] = 0; | ||
| 261 | st->pad[2] = 0; | ||
| 262 | st->pad[3] = 0; | ||
| 263 | } | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305.c b/src/lib/libssl/src/crypto/poly1305/poly1305.c new file mode 100644 index 0000000000..96083fe7e9 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305.c | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "poly1305.h" | ||
| 18 | #include "poly1305-donna.c" | ||
| 19 | |||
| 20 | void | ||
| 21 | CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]) | ||
| 22 | { | ||
| 23 | poly1305_init(ctx, key); | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
| 28 | size_t len) | ||
| 29 | { | ||
| 30 | poly1305_update(ctx, in, len); | ||
| 31 | } | ||
| 32 | |||
| 33 | void | ||
| 34 | CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) | ||
| 35 | { | ||
| 36 | poly1305_finish(ctx, mac); | ||
| 37 | } | ||
diff --git a/src/lib/libssl/src/crypto/poly1305/poly1305.h b/src/lib/libssl/src/crypto/poly1305/poly1305.h new file mode 100644 index 0000000000..a4ccdd5d80 --- /dev/null +++ b/src/lib/libssl/src/crypto/poly1305/poly1305.h | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) Joel Sing <jsing@openbsd.org> | ||
| 3 | * | ||
| 4 | * Permission to use, copy, modify, and distribute this software for any | ||
| 5 | * purpose with or without fee is hereby granted, provided that the above | ||
| 6 | * copyright notice and this permission notice appear in all copies. | ||
| 7 | * | ||
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #ifndef HEADER_POLY1305_H | ||
| 18 | #define HEADER_POLY1305_H | ||
| 19 | |||
| 20 | #include <openssl/opensslconf.h> | ||
| 21 | |||
| 22 | #if defined(OPENSSL_NO_POLY1305) | ||
| 23 | #error Poly1305 is disabled. | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #include <stddef.h> | ||
| 27 | |||
| 28 | #ifdef __cplusplus | ||
| 29 | extern "C" { | ||
| 30 | #endif | ||
| 31 | |||
| 32 | typedef struct poly1305_context { | ||
| 33 | size_t aligner; | ||
| 34 | unsigned char opaque[136]; | ||
| 35 | } poly1305_context; | ||
| 36 | |||
| 37 | typedef struct poly1305_context poly1305_state; | ||
| 38 | |||
| 39 | void CRYPTO_poly1305_init(poly1305_context *ctx, const unsigned char key[32]); | ||
| 40 | void CRYPTO_poly1305_update(poly1305_context *ctx, const unsigned char *in, | ||
| 41 | size_t len); | ||
| 42 | void CRYPTO_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]); | ||
| 43 | |||
| 44 | #ifdef __cplusplus | ||
| 45 | } | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #endif /* HEADER_POLY1305_H */ | ||
diff --git a/src/regress/lib/libcrypto/Makefile b/src/regress/lib/libcrypto/Makefile index 22737367a3..6cf7191cd7 100644 --- a/src/regress/lib/libcrypto/Makefile +++ b/src/regress/lib/libcrypto/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.8 2014/05/06 20:27:17 miod Exp $ | 1 | # $OpenBSD: Makefile,v 1.9 2014/05/14 14:46:35 jsing Exp $ |
| 2 | 2 | ||
| 3 | SUBDIR= \ | 3 | SUBDIR= \ |
| 4 | aeswrap \ | 4 | aeswrap \ |
| @@ -23,6 +23,7 @@ SUBDIR= \ | |||
| 23 | md4 \ | 23 | md4 \ |
| 24 | md5 \ | 24 | md5 \ |
| 25 | mdc2 \ | 25 | mdc2 \ |
| 26 | poly1305 \ | ||
| 26 | pqueue \ | 27 | pqueue \ |
| 27 | rand \ | 28 | rand \ |
| 28 | rc2 \ | 29 | rc2 \ |
diff --git a/src/regress/lib/libcrypto/poly1305/Makefile b/src/regress/lib/libcrypto/poly1305/Makefile new file mode 100644 index 0000000000..956b46973a --- /dev/null +++ b/src/regress/lib/libcrypto/poly1305/Makefile | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.1 2014/05/14 14:46:35 jsing Exp $ | ||
| 2 | |||
| 3 | PROG= poly1305test | ||
| 4 | LDADD= -lcrypto | ||
| 5 | DPADD= ${LIBCRYPTO} | ||
| 6 | |||
| 7 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libcrypto/poly1305/poly1305test.c b/src/regress/lib/libcrypto/poly1305/poly1305test.c new file mode 100644 index 0000000000..96b34c0218 --- /dev/null +++ b/src/regress/lib/libcrypto/poly1305/poly1305test.c | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | /* | ||
| 2 | * Public Domain poly1305 from Andrew Moon | ||
| 3 | * Based on poly1305-donna.c from: | ||
| 4 | * https://github.com/floodyberry/poly1305-donna | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <stdio.h> | ||
| 8 | |||
| 9 | #include <openssl/poly1305.h> | ||
| 10 | |||
| 11 | void poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, | ||
| 12 | const unsigned char key[32]); | ||
| 13 | |||
| 14 | int poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]); | ||
| 15 | int poly1305_power_on_self_test(void); | ||
| 16 | |||
| 17 | void | ||
| 18 | poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, | ||
| 19 | const unsigned char key[32]) { | ||
| 20 | poly1305_context ctx; | ||
| 21 | CRYPTO_poly1305_init(&ctx, key); | ||
| 22 | CRYPTO_poly1305_update(&ctx, m, bytes); | ||
| 23 | CRYPTO_poly1305_finish(&ctx, mac); | ||
| 24 | } | ||
| 25 | |||
| 26 | int | ||
| 27 | poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]) { | ||
| 28 | size_t i; | ||
| 29 | unsigned int dif = 0; | ||
| 30 | for (i = 0; i < 16; i++) | ||
| 31 | dif |= (mac1[i] ^ mac2[i]); | ||
| 32 | dif = (dif - 1) >> ((sizeof(unsigned int) * 8) - 1); | ||
| 33 | return (dif & 1); | ||
| 34 | } | ||
| 35 | |||
| 36 | /* test a few basic operations */ | ||
| 37 | int | ||
| 38 | poly1305_power_on_self_test(void) { | ||
| 39 | /* example from nacl */ | ||
| 40 | static const unsigned char nacl_key[32] = { | ||
| 41 | 0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91, | ||
| 42 | 0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25, | ||
| 43 | 0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65, | ||
| 44 | 0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80, | ||
| 45 | }; | ||
| 46 | |||
| 47 | static const unsigned char nacl_msg[131] = { | ||
| 48 | 0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73, | ||
| 49 | 0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce, | ||
| 50 | 0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4, | ||
| 51 | 0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a, | ||
| 52 | 0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b, | ||
| 53 | 0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72, | ||
| 54 | 0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2, | ||
| 55 | 0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38, | ||
| 56 | 0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a, | ||
| 57 | 0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae, | ||
| 58 | 0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea, | ||
| 59 | 0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda, | ||
| 60 | 0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde, | ||
| 61 | 0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3, | ||
| 62 | 0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6, | ||
| 63 | 0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74, | ||
| 64 | 0xe3,0x55,0xa5 | ||
| 65 | }; | ||
| 66 | |||
| 67 | static const unsigned char nacl_mac[16] = { | ||
| 68 | 0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5, | ||
| 69 | 0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9 | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* generates a final value of (2^130 - 2) == 3 */ | ||
| 73 | static const unsigned char wrap_key[32] = { | ||
| 74 | 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 75 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 76 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 77 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 78 | }; | ||
| 79 | |||
| 80 | static const unsigned char wrap_msg[16] = { | ||
| 81 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, | ||
| 82 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff | ||
| 83 | }; | ||
| 84 | |||
| 85 | static const unsigned char wrap_mac[16] = { | ||
| 86 | 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 87 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | ||
| 88 | }; | ||
| 89 | |||
| 90 | /* | ||
| 91 | mac of the macs of messages of length 0 to 256, where the key and messages | ||
| 92 | have all their values set to the length | ||
| 93 | */ | ||
| 94 | static const unsigned char total_key[32] = { | ||
| 95 | 0x01,0x02,0x03,0x04,0x05,0x06,0x07, | ||
| 96 | 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9, | ||
| 97 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, | ||
| 98 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff | ||
| 99 | }; | ||
| 100 | |||
| 101 | static const unsigned char total_mac[16] = { | ||
| 102 | 0x64,0xaf,0xe2,0xe8,0xd6,0xad,0x7b,0xbd, | ||
| 103 | 0xd2,0x87,0xf9,0x7c,0x44,0x62,0x3d,0x39 | ||
| 104 | }; | ||
| 105 | |||
| 106 | poly1305_context ctx; | ||
| 107 | poly1305_context total_ctx; | ||
| 108 | unsigned char all_key[32]; | ||
| 109 | unsigned char all_msg[256]; | ||
| 110 | unsigned char mac[16]; | ||
| 111 | size_t i, j; | ||
| 112 | int result = 1; | ||
| 113 | |||
| 114 | for (i = 0; i < sizeof(mac); i++) | ||
| 115 | mac[i] = 0; | ||
| 116 | poly1305_auth(mac, nacl_msg, sizeof(nacl_msg), nacl_key); | ||
| 117 | result &= poly1305_verify(nacl_mac, mac); | ||
| 118 | |||
| 119 | for (i = 0; i < sizeof(mac); i++) | ||
| 120 | mac[i] = 0; | ||
| 121 | CRYPTO_poly1305_init(&ctx, nacl_key); | ||
| 122 | CRYPTO_poly1305_update(&ctx, nacl_msg + 0, 32); | ||
| 123 | CRYPTO_poly1305_update(&ctx, nacl_msg + 32, 64); | ||
| 124 | CRYPTO_poly1305_update(&ctx, nacl_msg + 96, 16); | ||
| 125 | CRYPTO_poly1305_update(&ctx, nacl_msg + 112, 8); | ||
| 126 | CRYPTO_poly1305_update(&ctx, nacl_msg + 120, 4); | ||
| 127 | CRYPTO_poly1305_update(&ctx, nacl_msg + 124, 2); | ||
| 128 | CRYPTO_poly1305_update(&ctx, nacl_msg + 126, 1); | ||
| 129 | CRYPTO_poly1305_update(&ctx, nacl_msg + 127, 1); | ||
| 130 | CRYPTO_poly1305_update(&ctx, nacl_msg + 128, 1); | ||
| 131 | CRYPTO_poly1305_update(&ctx, nacl_msg + 129, 1); | ||
| 132 | CRYPTO_poly1305_update(&ctx, nacl_msg + 130, 1); | ||
| 133 | CRYPTO_poly1305_finish(&ctx, mac); | ||
| 134 | result &= poly1305_verify(nacl_mac, mac); | ||
| 135 | |||
| 136 | for (i = 0; i < sizeof(mac); i++) | ||
| 137 | mac[i] = 0; | ||
| 138 | poly1305_auth(mac, wrap_msg, sizeof(wrap_msg), wrap_key); | ||
| 139 | result &= poly1305_verify(wrap_mac, mac); | ||
| 140 | |||
| 141 | CRYPTO_poly1305_init(&total_ctx, total_key); | ||
| 142 | for (i = 0; i < 256; i++) { | ||
| 143 | /* set key and message to 'i,i,i..' */ | ||
| 144 | for (j = 0; j < sizeof(all_key); j++) | ||
| 145 | all_key[j] = i; | ||
| 146 | for (j = 0; j < i; j++) | ||
| 147 | all_msg[j] = i; | ||
| 148 | poly1305_auth(mac, all_msg, i, all_key); | ||
| 149 | CRYPTO_poly1305_update(&total_ctx, mac, 16); | ||
| 150 | } | ||
| 151 | CRYPTO_poly1305_finish(&total_ctx, mac); | ||
| 152 | result &= poly1305_verify(total_mac, mac); | ||
| 153 | |||
| 154 | return result; | ||
| 155 | } | ||
| 156 | |||
| 157 | int | ||
| 158 | main(int argc, char **argv) | ||
| 159 | { | ||
| 160 | if (!poly1305_power_on_self_test()) { | ||
| 161 | fprintf(stderr, "One or more self tests failed!\n"); | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | return 0; | ||
| 166 | } | ||
