1a079e38bSJohn Baldwin /*
2a079e38bSJohn Baldwin * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
3a079e38bSJohn Baldwin *
4a079e38bSJohn Baldwin * Licensed under the OpenSSL license (the "License"). You may not use
5a079e38bSJohn Baldwin * this file except in compliance with the License. You can obtain a copy
6a079e38bSJohn Baldwin * in the file LICENSE in the source distribution or at
7a079e38bSJohn Baldwin * https://www.openssl.org/source/license.html
8a079e38bSJohn Baldwin */
9a079e38bSJohn Baldwin
10a079e38bSJohn Baldwin #include <sys/libkern.h>
11a079e38bSJohn Baldwin #include <sys/malloc.h>
12a079e38bSJohn Baldwin
13a079e38bSJohn Baldwin #include <opencrypto/cryptodev.h>
14a079e38bSJohn Baldwin #include <opencrypto/xform_auth.h>
15a079e38bSJohn Baldwin
16a079e38bSJohn Baldwin #include <crypto/openssl/ossl.h>
17a079e38bSJohn Baldwin #include <crypto/openssl/ossl_poly1305.h>
18a079e38bSJohn Baldwin
19a079e38bSJohn Baldwin #define POLY1305_ASM
20a079e38bSJohn Baldwin
21a079e38bSJohn Baldwin /* From crypto/poly1305/poly1305.c */
22a079e38bSJohn Baldwin
23a079e38bSJohn Baldwin /* pick 32-bit unsigned integer in little endian order */
U8TOU32(const unsigned char * p)24a079e38bSJohn Baldwin static unsigned int U8TOU32(const unsigned char *p)
25a079e38bSJohn Baldwin {
26a079e38bSJohn Baldwin return (((unsigned int)(p[0] & 0xff)) |
27a079e38bSJohn Baldwin ((unsigned int)(p[1] & 0xff) << 8) |
28a079e38bSJohn Baldwin ((unsigned int)(p[2] & 0xff) << 16) |
29a079e38bSJohn Baldwin ((unsigned int)(p[3] & 0xff) << 24));
30a079e38bSJohn Baldwin }
31a079e38bSJohn Baldwin
32a079e38bSJohn Baldwin /*
33a079e38bSJohn Baldwin * Implementations can be classified by amount of significant bits in
34a079e38bSJohn Baldwin * words making up the multi-precision value, or in other words radix
35a079e38bSJohn Baldwin * or base of numerical representation, e.g. base 2^64, base 2^32,
36a079e38bSJohn Baldwin * base 2^26. Complementary characteristic is how wide is the result of
37a079e38bSJohn Baldwin * multiplication of pair of digits, e.g. it would take 128 bits to
38a079e38bSJohn Baldwin * accommodate multiplication result in base 2^64 case. These are used
39a079e38bSJohn Baldwin * interchangeably. To describe implementation that is. But interface
40a079e38bSJohn Baldwin * is designed to isolate this so that low-level primitives implemented
41a079e38bSJohn Baldwin * in assembly can be self-contained/self-coherent.
42a079e38bSJohn Baldwin */
43a079e38bSJohn Baldwin int poly1305_init(void *ctx, const unsigned char key[16], void *func);
44a079e38bSJohn Baldwin void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
45a079e38bSJohn Baldwin unsigned int padbit);
46a079e38bSJohn Baldwin void poly1305_emit(void *ctx, unsigned char mac[16],
47a079e38bSJohn Baldwin const unsigned int nonce[4]);
48a079e38bSJohn Baldwin
Poly1305_Init(POLY1305 * ctx,const unsigned char key[32])49*78991a93SJohn Baldwin void Poly1305_Init(POLY1305 *ctx, const unsigned char key[32])
50a079e38bSJohn Baldwin {
51a079e38bSJohn Baldwin ctx->nonce[0] = U8TOU32(&key[16]);
52a079e38bSJohn Baldwin ctx->nonce[1] = U8TOU32(&key[20]);
53a079e38bSJohn Baldwin ctx->nonce[2] = U8TOU32(&key[24]);
54a079e38bSJohn Baldwin ctx->nonce[3] = U8TOU32(&key[28]);
55a079e38bSJohn Baldwin
56a079e38bSJohn Baldwin /*
57a079e38bSJohn Baldwin * Unlike reference poly1305_init assembly counterpart is expected
58a079e38bSJohn Baldwin * to return a value: non-zero if it initializes ctx->func, and zero
59a079e38bSJohn Baldwin * otherwise. Latter is to simplify assembly in cases when there no
60a079e38bSJohn Baldwin * multiple code paths to switch between.
61a079e38bSJohn Baldwin */
62a079e38bSJohn Baldwin if (!poly1305_init(ctx->opaque, key, &ctx->func)) {
63a079e38bSJohn Baldwin ctx->func.blocks = poly1305_blocks;
64a079e38bSJohn Baldwin ctx->func.emit = poly1305_emit;
65a079e38bSJohn Baldwin }
66a079e38bSJohn Baldwin
67a079e38bSJohn Baldwin ctx->num = 0;
68a079e38bSJohn Baldwin
69a079e38bSJohn Baldwin }
70a079e38bSJohn Baldwin
71a079e38bSJohn Baldwin #ifdef POLY1305_ASM
72a079e38bSJohn Baldwin /*
73a079e38bSJohn Baldwin * This "eclipses" poly1305_blocks and poly1305_emit, but it's
74a079e38bSJohn Baldwin * conscious choice imposed by -Wshadow compiler warnings.
75a079e38bSJohn Baldwin */
76a079e38bSJohn Baldwin # define poly1305_blocks (*poly1305_blocks_p)
77a079e38bSJohn Baldwin # define poly1305_emit (*poly1305_emit_p)
78a079e38bSJohn Baldwin #endif
79a079e38bSJohn Baldwin
Poly1305_Update(POLY1305 * ctx,const unsigned char * inp,size_t len)80*78991a93SJohn Baldwin void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len)
81a079e38bSJohn Baldwin {
82a079e38bSJohn Baldwin #ifdef POLY1305_ASM
83a079e38bSJohn Baldwin /*
84a079e38bSJohn Baldwin * As documented, poly1305_blocks is never called with input
85a079e38bSJohn Baldwin * longer than single block and padbit argument set to 0. This
86a079e38bSJohn Baldwin * property is fluently used in assembly modules to optimize
87a079e38bSJohn Baldwin * padbit handling on loop boundary.
88a079e38bSJohn Baldwin */
89a079e38bSJohn Baldwin poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
90a079e38bSJohn Baldwin #endif
91a079e38bSJohn Baldwin size_t rem, num;
92a079e38bSJohn Baldwin
93a079e38bSJohn Baldwin if ((num = ctx->num)) {
94a079e38bSJohn Baldwin rem = POLY1305_BLOCK_SIZE - num;
95a079e38bSJohn Baldwin if (len >= rem) {
96a079e38bSJohn Baldwin memcpy(ctx->data + num, inp, rem);
97a079e38bSJohn Baldwin poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1);
98a079e38bSJohn Baldwin inp += rem;
99a079e38bSJohn Baldwin len -= rem;
100a079e38bSJohn Baldwin } else {
101a079e38bSJohn Baldwin /* Still not enough data to process a block. */
102a079e38bSJohn Baldwin memcpy(ctx->data + num, inp, len);
103a079e38bSJohn Baldwin ctx->num = num + len;
104a079e38bSJohn Baldwin return;
105a079e38bSJohn Baldwin }
106a079e38bSJohn Baldwin }
107a079e38bSJohn Baldwin
108a079e38bSJohn Baldwin rem = len % POLY1305_BLOCK_SIZE;
109a079e38bSJohn Baldwin len -= rem;
110a079e38bSJohn Baldwin
111a079e38bSJohn Baldwin if (len >= POLY1305_BLOCK_SIZE) {
112a079e38bSJohn Baldwin poly1305_blocks(ctx->opaque, inp, len, 1);
113a079e38bSJohn Baldwin inp += len;
114a079e38bSJohn Baldwin }
115a079e38bSJohn Baldwin
116a079e38bSJohn Baldwin if (rem)
117a079e38bSJohn Baldwin memcpy(ctx->data, inp, rem);
118a079e38bSJohn Baldwin
119a079e38bSJohn Baldwin ctx->num = rem;
120a079e38bSJohn Baldwin }
121a079e38bSJohn Baldwin
Poly1305_Final(POLY1305 * ctx,unsigned char mac[16])122*78991a93SJohn Baldwin void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16])
123a079e38bSJohn Baldwin {
124a079e38bSJohn Baldwin #ifdef POLY1305_ASM
125a079e38bSJohn Baldwin poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
126a079e38bSJohn Baldwin poly1305_emit_f poly1305_emit_p = ctx->func.emit;
127a079e38bSJohn Baldwin #endif
128a079e38bSJohn Baldwin size_t num;
129a079e38bSJohn Baldwin
130a079e38bSJohn Baldwin if ((num = ctx->num)) {
131a079e38bSJohn Baldwin ctx->data[num++] = 1; /* pad bit */
132a079e38bSJohn Baldwin while (num < POLY1305_BLOCK_SIZE)
133a079e38bSJohn Baldwin ctx->data[num++] = 0;
134a079e38bSJohn Baldwin poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 0);
135a079e38bSJohn Baldwin }
136a079e38bSJohn Baldwin
137a079e38bSJohn Baldwin poly1305_emit(ctx->opaque, mac, ctx->nonce);
138a079e38bSJohn Baldwin
139a079e38bSJohn Baldwin /* zero out the state */
140a079e38bSJohn Baldwin OPENSSL_cleanse(ctx, sizeof(*ctx));
141a079e38bSJohn Baldwin }
142a079e38bSJohn Baldwin
143a079e38bSJohn Baldwin static void
ossl_poly1305_init(void * vctx)144a079e38bSJohn Baldwin ossl_poly1305_init(void *vctx)
145a079e38bSJohn Baldwin {
146a079e38bSJohn Baldwin }
147a079e38bSJohn Baldwin
148a079e38bSJohn Baldwin static void
ossl_poly1305_setkey(void * vctx,const uint8_t * key,u_int klen)149a079e38bSJohn Baldwin ossl_poly1305_setkey(void *vctx, const uint8_t *key, u_int klen)
150a079e38bSJohn Baldwin {
151a079e38bSJohn Baldwin MPASS(klen == 32);
152a079e38bSJohn Baldwin Poly1305_Init(vctx, key);
153a079e38bSJohn Baldwin }
154a079e38bSJohn Baldwin
155*78991a93SJohn Baldwin int
ossl_poly1305_update(void * vctx,const void * buf,u_int len)156a079e38bSJohn Baldwin ossl_poly1305_update(void *vctx, const void *buf, u_int len)
157a079e38bSJohn Baldwin {
158a079e38bSJohn Baldwin Poly1305_Update(vctx, buf, len);
159a079e38bSJohn Baldwin return (0);
160a079e38bSJohn Baldwin }
161a079e38bSJohn Baldwin
162a079e38bSJohn Baldwin static void
ossl_poly1305_final(uint8_t * digest,void * vctx)163a079e38bSJohn Baldwin ossl_poly1305_final(uint8_t *digest, void *vctx)
164a079e38bSJohn Baldwin {
165a079e38bSJohn Baldwin Poly1305_Final(vctx, digest);
166a079e38bSJohn Baldwin }
167a079e38bSJohn Baldwin
168a079e38bSJohn Baldwin struct auth_hash ossl_hash_poly1305 = {
169a079e38bSJohn Baldwin .type = CRYPTO_POLY1305,
170a079e38bSJohn Baldwin .name = "OpenSSL-Poly1305",
171a079e38bSJohn Baldwin .hashsize = POLY1305_HASH_LEN,
172a079e38bSJohn Baldwin .ctxsize = sizeof(struct poly1305_context),
173a079e38bSJohn Baldwin .blocksize = POLY1305_BLOCK_SIZE,
174a079e38bSJohn Baldwin .Init = ossl_poly1305_init,
175a079e38bSJohn Baldwin .Setkey = ossl_poly1305_setkey,
176a079e38bSJohn Baldwin .Update = ossl_poly1305_update,
177a079e38bSJohn Baldwin .Final = ossl_poly1305_final,
178a079e38bSJohn Baldwin };
179a079e38bSJohn Baldwin
180a079e38bSJohn Baldwin _Static_assert(sizeof(struct poly1305_context) <=
181a079e38bSJohn Baldwin sizeof(struct ossl_hash_context), "ossl_hash_context too small");
182