1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery #include <string.h>
11*b077aed3SPierre Pronchery #include <stdlib.h>
12*b077aed3SPierre Pronchery #include <openssl/crypto.h>
13*b077aed3SPierre Pronchery #include <openssl/evp.h>
14*b077aed3SPierre Pronchery #include <openssl/core_names.h>
15*b077aed3SPierre Pronchery #include <openssl/params.h>
16*b077aed3SPierre Pronchery #include "internal/endian.h"
17*b077aed3SPierre Pronchery #include "crypto/modes.h"
18*b077aed3SPierre Pronchery #include "crypto/siv.h"
19*b077aed3SPierre Pronchery
20*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SIV
21*b077aed3SPierre Pronchery
rotl8(uint32_t x)22*b077aed3SPierre Pronchery __owur static ossl_inline uint32_t rotl8(uint32_t x)
23*b077aed3SPierre Pronchery {
24*b077aed3SPierre Pronchery return (x << 8) | (x >> 24);
25*b077aed3SPierre Pronchery }
26*b077aed3SPierre Pronchery
rotr8(uint32_t x)27*b077aed3SPierre Pronchery __owur static ossl_inline uint32_t rotr8(uint32_t x)
28*b077aed3SPierre Pronchery {
29*b077aed3SPierre Pronchery return (x >> 8) | (x << 24);
30*b077aed3SPierre Pronchery }
31*b077aed3SPierre Pronchery
byteswap8(uint64_t x)32*b077aed3SPierre Pronchery __owur static ossl_inline uint64_t byteswap8(uint64_t x)
33*b077aed3SPierre Pronchery {
34*b077aed3SPierre Pronchery uint32_t high = (uint32_t)(x >> 32);
35*b077aed3SPierre Pronchery uint32_t low = (uint32_t)x;
36*b077aed3SPierre Pronchery
37*b077aed3SPierre Pronchery high = (rotl8(high) & 0x00ff00ff) | (rotr8(high) & 0xff00ff00);
38*b077aed3SPierre Pronchery low = (rotl8(low) & 0x00ff00ff) | (rotr8(low) & 0xff00ff00);
39*b077aed3SPierre Pronchery return ((uint64_t)low) << 32 | (uint64_t)high;
40*b077aed3SPierre Pronchery }
41*b077aed3SPierre Pronchery
siv128_getword(SIV_BLOCK const * b,size_t i)42*b077aed3SPierre Pronchery __owur static ossl_inline uint64_t siv128_getword(SIV_BLOCK const *b, size_t i)
43*b077aed3SPierre Pronchery {
44*b077aed3SPierre Pronchery DECLARE_IS_ENDIAN;
45*b077aed3SPierre Pronchery
46*b077aed3SPierre Pronchery if (IS_LITTLE_ENDIAN)
47*b077aed3SPierre Pronchery return byteswap8(b->word[i]);
48*b077aed3SPierre Pronchery return b->word[i];
49*b077aed3SPierre Pronchery }
50*b077aed3SPierre Pronchery
siv128_putword(SIV_BLOCK * b,size_t i,uint64_t x)51*b077aed3SPierre Pronchery static ossl_inline void siv128_putword(SIV_BLOCK *b, size_t i, uint64_t x)
52*b077aed3SPierre Pronchery {
53*b077aed3SPierre Pronchery DECLARE_IS_ENDIAN;
54*b077aed3SPierre Pronchery
55*b077aed3SPierre Pronchery if (IS_LITTLE_ENDIAN)
56*b077aed3SPierre Pronchery b->word[i] = byteswap8(x);
57*b077aed3SPierre Pronchery else
58*b077aed3SPierre Pronchery b->word[i] = x;
59*b077aed3SPierre Pronchery }
60*b077aed3SPierre Pronchery
siv128_xorblock(SIV_BLOCK * x,SIV_BLOCK const * y)61*b077aed3SPierre Pronchery static ossl_inline void siv128_xorblock(SIV_BLOCK *x,
62*b077aed3SPierre Pronchery SIV_BLOCK const *y)
63*b077aed3SPierre Pronchery {
64*b077aed3SPierre Pronchery x->word[0] ^= y->word[0];
65*b077aed3SPierre Pronchery x->word[1] ^= y->word[1];
66*b077aed3SPierre Pronchery }
67*b077aed3SPierre Pronchery
68*b077aed3SPierre Pronchery /*
69*b077aed3SPierre Pronchery * Doubles |b|, which is 16 bytes representing an element
70*b077aed3SPierre Pronchery * of GF(2**128) modulo the irreducible polynomial
71*b077aed3SPierre Pronchery * x**128 + x**7 + x**2 + x + 1.
72*b077aed3SPierre Pronchery * Assumes two's-complement arithmetic
73*b077aed3SPierre Pronchery */
siv128_dbl(SIV_BLOCK * b)74*b077aed3SPierre Pronchery static ossl_inline void siv128_dbl(SIV_BLOCK *b)
75*b077aed3SPierre Pronchery {
76*b077aed3SPierre Pronchery uint64_t high = siv128_getword(b, 0);
77*b077aed3SPierre Pronchery uint64_t low = siv128_getword(b, 1);
78*b077aed3SPierre Pronchery uint64_t high_carry = high & (((uint64_t)1) << 63);
79*b077aed3SPierre Pronchery uint64_t low_carry = low & (((uint64_t)1) << 63);
80*b077aed3SPierre Pronchery int64_t low_mask = -((int64_t)(high_carry >> 63)) & 0x87;
81*b077aed3SPierre Pronchery uint64_t high_mask = low_carry >> 63;
82*b077aed3SPierre Pronchery
83*b077aed3SPierre Pronchery high = (high << 1) | high_mask;
84*b077aed3SPierre Pronchery low = (low << 1) ^ (uint64_t)low_mask;
85*b077aed3SPierre Pronchery siv128_putword(b, 0, high);
86*b077aed3SPierre Pronchery siv128_putword(b, 1, low);
87*b077aed3SPierre Pronchery }
88*b077aed3SPierre Pronchery
siv128_do_s2v_p(SIV128_CONTEXT * ctx,SIV_BLOCK * out,unsigned char const * in,size_t len)89*b077aed3SPierre Pronchery __owur static ossl_inline int siv128_do_s2v_p(SIV128_CONTEXT *ctx, SIV_BLOCK *out,
90*b077aed3SPierre Pronchery unsigned char const* in, size_t len)
91*b077aed3SPierre Pronchery {
92*b077aed3SPierre Pronchery SIV_BLOCK t;
93*b077aed3SPierre Pronchery size_t out_len = sizeof(out->byte);
94*b077aed3SPierre Pronchery EVP_MAC_CTX *mac_ctx;
95*b077aed3SPierre Pronchery int ret = 0;
96*b077aed3SPierre Pronchery
97*b077aed3SPierre Pronchery mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init);
98*b077aed3SPierre Pronchery if (mac_ctx == NULL)
99*b077aed3SPierre Pronchery return 0;
100*b077aed3SPierre Pronchery
101*b077aed3SPierre Pronchery if (len >= SIV_LEN) {
102*b077aed3SPierre Pronchery if (!EVP_MAC_update(mac_ctx, in, len - SIV_LEN))
103*b077aed3SPierre Pronchery goto err;
104*b077aed3SPierre Pronchery memcpy(&t, in + (len-SIV_LEN), SIV_LEN);
105*b077aed3SPierre Pronchery siv128_xorblock(&t, &ctx->d);
106*b077aed3SPierre Pronchery if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
107*b077aed3SPierre Pronchery goto err;
108*b077aed3SPierre Pronchery } else {
109*b077aed3SPierre Pronchery memset(&t, 0, sizeof(t));
110*b077aed3SPierre Pronchery memcpy(&t, in, len);
111*b077aed3SPierre Pronchery t.byte[len] = 0x80;
112*b077aed3SPierre Pronchery siv128_dbl(&ctx->d);
113*b077aed3SPierre Pronchery siv128_xorblock(&t, &ctx->d);
114*b077aed3SPierre Pronchery if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
115*b077aed3SPierre Pronchery goto err;
116*b077aed3SPierre Pronchery }
117*b077aed3SPierre Pronchery if (!EVP_MAC_final(mac_ctx, out->byte, &out_len, sizeof(out->byte))
118*b077aed3SPierre Pronchery || out_len != SIV_LEN)
119*b077aed3SPierre Pronchery goto err;
120*b077aed3SPierre Pronchery
121*b077aed3SPierre Pronchery ret = 1;
122*b077aed3SPierre Pronchery
123*b077aed3SPierre Pronchery err:
124*b077aed3SPierre Pronchery EVP_MAC_CTX_free(mac_ctx);
125*b077aed3SPierre Pronchery return ret;
126*b077aed3SPierre Pronchery }
127*b077aed3SPierre Pronchery
128*b077aed3SPierre Pronchery
siv128_do_encrypt(EVP_CIPHER_CTX * ctx,unsigned char * out,unsigned char const * in,size_t len,SIV_BLOCK * icv)129*b077aed3SPierre Pronchery __owur static ossl_inline int siv128_do_encrypt(EVP_CIPHER_CTX *ctx, unsigned char *out,
130*b077aed3SPierre Pronchery unsigned char const *in, size_t len,
131*b077aed3SPierre Pronchery SIV_BLOCK *icv)
132*b077aed3SPierre Pronchery {
133*b077aed3SPierre Pronchery int out_len = (int)len;
134*b077aed3SPierre Pronchery
135*b077aed3SPierre Pronchery if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, icv->byte, 1))
136*b077aed3SPierre Pronchery return 0;
137*b077aed3SPierre Pronchery return EVP_EncryptUpdate(ctx, out, &out_len, in, out_len);
138*b077aed3SPierre Pronchery }
139*b077aed3SPierre Pronchery
140*b077aed3SPierre Pronchery /*
141*b077aed3SPierre Pronchery * Create a new SIV128_CONTEXT
142*b077aed3SPierre Pronchery */
ossl_siv128_new(const unsigned char * key,int klen,EVP_CIPHER * cbc,EVP_CIPHER * ctr,OSSL_LIB_CTX * libctx,const char * propq)143*b077aed3SPierre Pronchery SIV128_CONTEXT *ossl_siv128_new(const unsigned char *key, int klen,
144*b077aed3SPierre Pronchery EVP_CIPHER *cbc, EVP_CIPHER *ctr,
145*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq)
146*b077aed3SPierre Pronchery {
147*b077aed3SPierre Pronchery SIV128_CONTEXT *ctx;
148*b077aed3SPierre Pronchery int ret;
149*b077aed3SPierre Pronchery
150*b077aed3SPierre Pronchery if ((ctx = OPENSSL_malloc(sizeof(*ctx))) != NULL) {
151*b077aed3SPierre Pronchery ret = ossl_siv128_init(ctx, key, klen, cbc, ctr, libctx, propq);
152*b077aed3SPierre Pronchery if (ret)
153*b077aed3SPierre Pronchery return ctx;
154*b077aed3SPierre Pronchery OPENSSL_free(ctx);
155*b077aed3SPierre Pronchery }
156*b077aed3SPierre Pronchery
157*b077aed3SPierre Pronchery return NULL;
158*b077aed3SPierre Pronchery }
159*b077aed3SPierre Pronchery
160*b077aed3SPierre Pronchery /*
161*b077aed3SPierre Pronchery * Initialise an existing SIV128_CONTEXT
162*b077aed3SPierre Pronchery */
ossl_siv128_init(SIV128_CONTEXT * ctx,const unsigned char * key,int klen,const EVP_CIPHER * cbc,const EVP_CIPHER * ctr,OSSL_LIB_CTX * libctx,const char * propq)163*b077aed3SPierre Pronchery int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
164*b077aed3SPierre Pronchery const EVP_CIPHER *cbc, const EVP_CIPHER *ctr,
165*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq)
166*b077aed3SPierre Pronchery {
167*b077aed3SPierre Pronchery static const unsigned char zero[SIV_LEN] = { 0 };
168*b077aed3SPierre Pronchery size_t out_len = SIV_LEN;
169*b077aed3SPierre Pronchery EVP_MAC_CTX *mac_ctx = NULL;
170*b077aed3SPierre Pronchery OSSL_PARAM params[3];
171*b077aed3SPierre Pronchery const char *cbc_name;
172*b077aed3SPierre Pronchery
173*b077aed3SPierre Pronchery if (ctx == NULL)
174*b077aed3SPierre Pronchery return 0;
175*b077aed3SPierre Pronchery
176*b077aed3SPierre Pronchery memset(&ctx->d, 0, sizeof(ctx->d));
177*b077aed3SPierre Pronchery EVP_CIPHER_CTX_free(ctx->cipher_ctx);
178*b077aed3SPierre Pronchery EVP_MAC_CTX_free(ctx->mac_ctx_init);
179*b077aed3SPierre Pronchery EVP_MAC_free(ctx->mac);
180*b077aed3SPierre Pronchery ctx->mac = NULL;
181*b077aed3SPierre Pronchery ctx->cipher_ctx = NULL;
182*b077aed3SPierre Pronchery ctx->mac_ctx_init = NULL;
183*b077aed3SPierre Pronchery
184*b077aed3SPierre Pronchery if (key == NULL || cbc == NULL || ctr == NULL)
185*b077aed3SPierre Pronchery return 0;
186*b077aed3SPierre Pronchery
187*b077aed3SPierre Pronchery cbc_name = EVP_CIPHER_get0_name(cbc);
188*b077aed3SPierre Pronchery params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
189*b077aed3SPierre Pronchery (char *)cbc_name, 0);
190*b077aed3SPierre Pronchery params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
191*b077aed3SPierre Pronchery (void *)key, klen);
192*b077aed3SPierre Pronchery params[2] = OSSL_PARAM_construct_end();
193*b077aed3SPierre Pronchery
194*b077aed3SPierre Pronchery if ((ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
195*b077aed3SPierre Pronchery || (ctx->mac =
196*b077aed3SPierre Pronchery EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, propq)) == NULL
197*b077aed3SPierre Pronchery || (ctx->mac_ctx_init = EVP_MAC_CTX_new(ctx->mac)) == NULL
198*b077aed3SPierre Pronchery || !EVP_MAC_CTX_set_params(ctx->mac_ctx_init, params)
199*b077aed3SPierre Pronchery || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
200*b077aed3SPierre Pronchery || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
201*b077aed3SPierre Pronchery || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
202*b077aed3SPierre Pronchery || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len,
203*b077aed3SPierre Pronchery sizeof(ctx->d.byte))) {
204*b077aed3SPierre Pronchery EVP_CIPHER_CTX_free(ctx->cipher_ctx);
205*b077aed3SPierre Pronchery EVP_MAC_CTX_free(ctx->mac_ctx_init);
206*b077aed3SPierre Pronchery EVP_MAC_CTX_free(mac_ctx);
207*b077aed3SPierre Pronchery EVP_MAC_free(ctx->mac);
208*b077aed3SPierre Pronchery return 0;
209*b077aed3SPierre Pronchery }
210*b077aed3SPierre Pronchery EVP_MAC_CTX_free(mac_ctx);
211*b077aed3SPierre Pronchery
212*b077aed3SPierre Pronchery ctx->final_ret = -1;
213*b077aed3SPierre Pronchery ctx->crypto_ok = 1;
214*b077aed3SPierre Pronchery
215*b077aed3SPierre Pronchery return 1;
216*b077aed3SPierre Pronchery }
217*b077aed3SPierre Pronchery
218*b077aed3SPierre Pronchery /*
219*b077aed3SPierre Pronchery * Copy an SIV128_CONTEXT object
220*b077aed3SPierre Pronchery */
ossl_siv128_copy_ctx(SIV128_CONTEXT * dest,SIV128_CONTEXT * src)221*b077aed3SPierre Pronchery int ossl_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
222*b077aed3SPierre Pronchery {
223*b077aed3SPierre Pronchery memcpy(&dest->d, &src->d, sizeof(src->d));
224*b077aed3SPierre Pronchery if (dest->cipher_ctx == NULL) {
225*b077aed3SPierre Pronchery dest->cipher_ctx = EVP_CIPHER_CTX_new();
226*b077aed3SPierre Pronchery if (dest->cipher_ctx == NULL)
227*b077aed3SPierre Pronchery return 0;
228*b077aed3SPierre Pronchery }
229*b077aed3SPierre Pronchery if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
230*b077aed3SPierre Pronchery return 0;
231*b077aed3SPierre Pronchery EVP_MAC_CTX_free(dest->mac_ctx_init);
232*b077aed3SPierre Pronchery dest->mac_ctx_init = EVP_MAC_CTX_dup(src->mac_ctx_init);
233*b077aed3SPierre Pronchery if (dest->mac_ctx_init == NULL)
234*b077aed3SPierre Pronchery return 0;
235*b077aed3SPierre Pronchery dest->mac = src->mac;
236*b077aed3SPierre Pronchery if (dest->mac != NULL)
237*b077aed3SPierre Pronchery EVP_MAC_up_ref(dest->mac);
238*b077aed3SPierre Pronchery return 1;
239*b077aed3SPierre Pronchery }
240*b077aed3SPierre Pronchery
241*b077aed3SPierre Pronchery /*
242*b077aed3SPierre Pronchery * Provide any AAD. This can be called multiple times.
243*b077aed3SPierre Pronchery * Per RFC5297, the last piece of associated data
244*b077aed3SPierre Pronchery * is the nonce, but it's not treated special
245*b077aed3SPierre Pronchery */
ossl_siv128_aad(SIV128_CONTEXT * ctx,const unsigned char * aad,size_t len)246*b077aed3SPierre Pronchery int ossl_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad,
247*b077aed3SPierre Pronchery size_t len)
248*b077aed3SPierre Pronchery {
249*b077aed3SPierre Pronchery SIV_BLOCK mac_out;
250*b077aed3SPierre Pronchery size_t out_len = SIV_LEN;
251*b077aed3SPierre Pronchery EVP_MAC_CTX *mac_ctx;
252*b077aed3SPierre Pronchery
253*b077aed3SPierre Pronchery siv128_dbl(&ctx->d);
254*b077aed3SPierre Pronchery
255*b077aed3SPierre Pronchery if ((mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
256*b077aed3SPierre Pronchery || !EVP_MAC_update(mac_ctx, aad, len)
257*b077aed3SPierre Pronchery || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len,
258*b077aed3SPierre Pronchery sizeof(mac_out.byte))
259*b077aed3SPierre Pronchery || out_len != SIV_LEN) {
260*b077aed3SPierre Pronchery EVP_MAC_CTX_free(mac_ctx);
261*b077aed3SPierre Pronchery return 0;
262*b077aed3SPierre Pronchery }
263*b077aed3SPierre Pronchery EVP_MAC_CTX_free(mac_ctx);
264*b077aed3SPierre Pronchery
265*b077aed3SPierre Pronchery siv128_xorblock(&ctx->d, &mac_out);
266*b077aed3SPierre Pronchery
267*b077aed3SPierre Pronchery return 1;
268*b077aed3SPierre Pronchery }
269*b077aed3SPierre Pronchery
270*b077aed3SPierre Pronchery /*
271*b077aed3SPierre Pronchery * Provide any data to be encrypted. This can be called once.
272*b077aed3SPierre Pronchery */
ossl_siv128_encrypt(SIV128_CONTEXT * ctx,const unsigned char * in,unsigned char * out,size_t len)273*b077aed3SPierre Pronchery int ossl_siv128_encrypt(SIV128_CONTEXT *ctx,
274*b077aed3SPierre Pronchery const unsigned char *in, unsigned char *out,
275*b077aed3SPierre Pronchery size_t len)
276*b077aed3SPierre Pronchery {
277*b077aed3SPierre Pronchery SIV_BLOCK q;
278*b077aed3SPierre Pronchery
279*b077aed3SPierre Pronchery /* can only do one crypto operation */
280*b077aed3SPierre Pronchery if (ctx->crypto_ok == 0)
281*b077aed3SPierre Pronchery return 0;
282*b077aed3SPierre Pronchery ctx->crypto_ok--;
283*b077aed3SPierre Pronchery
284*b077aed3SPierre Pronchery if (!siv128_do_s2v_p(ctx, &q, in, len))
285*b077aed3SPierre Pronchery return 0;
286*b077aed3SPierre Pronchery
287*b077aed3SPierre Pronchery memcpy(ctx->tag.byte, &q, SIV_LEN);
288*b077aed3SPierre Pronchery q.byte[8] &= 0x7f;
289*b077aed3SPierre Pronchery q.byte[12] &= 0x7f;
290*b077aed3SPierre Pronchery
291*b077aed3SPierre Pronchery if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q))
292*b077aed3SPierre Pronchery return 0;
293*b077aed3SPierre Pronchery ctx->final_ret = 0;
294*b077aed3SPierre Pronchery return len;
295*b077aed3SPierre Pronchery }
296*b077aed3SPierre Pronchery
297*b077aed3SPierre Pronchery /*
298*b077aed3SPierre Pronchery * Provide any data to be decrypted. This can be called once.
299*b077aed3SPierre Pronchery */
ossl_siv128_decrypt(SIV128_CONTEXT * ctx,const unsigned char * in,unsigned char * out,size_t len)300*b077aed3SPierre Pronchery int ossl_siv128_decrypt(SIV128_CONTEXT *ctx,
301*b077aed3SPierre Pronchery const unsigned char *in, unsigned char *out,
302*b077aed3SPierre Pronchery size_t len)
303*b077aed3SPierre Pronchery {
304*b077aed3SPierre Pronchery unsigned char* p;
305*b077aed3SPierre Pronchery SIV_BLOCK t, q;
306*b077aed3SPierre Pronchery int i;
307*b077aed3SPierre Pronchery
308*b077aed3SPierre Pronchery /* can only do one crypto operation */
309*b077aed3SPierre Pronchery if (ctx->crypto_ok == 0)
310*b077aed3SPierre Pronchery return 0;
311*b077aed3SPierre Pronchery ctx->crypto_ok--;
312*b077aed3SPierre Pronchery
313*b077aed3SPierre Pronchery memcpy(&q, ctx->tag.byte, SIV_LEN);
314*b077aed3SPierre Pronchery q.byte[8] &= 0x7f;
315*b077aed3SPierre Pronchery q.byte[12] &= 0x7f;
316*b077aed3SPierre Pronchery
317*b077aed3SPierre Pronchery if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q)
318*b077aed3SPierre Pronchery || !siv128_do_s2v_p(ctx, &t, out, len))
319*b077aed3SPierre Pronchery return 0;
320*b077aed3SPierre Pronchery
321*b077aed3SPierre Pronchery p = ctx->tag.byte;
322*b077aed3SPierre Pronchery for (i = 0; i < SIV_LEN; i++)
323*b077aed3SPierre Pronchery t.byte[i] ^= p[i];
324*b077aed3SPierre Pronchery
325*b077aed3SPierre Pronchery if ((t.word[0] | t.word[1]) != 0) {
326*b077aed3SPierre Pronchery OPENSSL_cleanse(out, len);
327*b077aed3SPierre Pronchery return 0;
328*b077aed3SPierre Pronchery }
329*b077aed3SPierre Pronchery ctx->final_ret = 0;
330*b077aed3SPierre Pronchery return len;
331*b077aed3SPierre Pronchery }
332*b077aed3SPierre Pronchery
333*b077aed3SPierre Pronchery /*
334*b077aed3SPierre Pronchery * Return the already calculated final result.
335*b077aed3SPierre Pronchery */
ossl_siv128_finish(SIV128_CONTEXT * ctx)336*b077aed3SPierre Pronchery int ossl_siv128_finish(SIV128_CONTEXT *ctx)
337*b077aed3SPierre Pronchery {
338*b077aed3SPierre Pronchery return ctx->final_ret;
339*b077aed3SPierre Pronchery }
340*b077aed3SPierre Pronchery
341*b077aed3SPierre Pronchery /*
342*b077aed3SPierre Pronchery * Set the tag
343*b077aed3SPierre Pronchery */
ossl_siv128_set_tag(SIV128_CONTEXT * ctx,const unsigned char * tag,size_t len)344*b077aed3SPierre Pronchery int ossl_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len)
345*b077aed3SPierre Pronchery {
346*b077aed3SPierre Pronchery if (len != SIV_LEN)
347*b077aed3SPierre Pronchery return 0;
348*b077aed3SPierre Pronchery
349*b077aed3SPierre Pronchery /* Copy the tag from the supplied buffer */
350*b077aed3SPierre Pronchery memcpy(ctx->tag.byte, tag, len);
351*b077aed3SPierre Pronchery return 1;
352*b077aed3SPierre Pronchery }
353*b077aed3SPierre Pronchery
354*b077aed3SPierre Pronchery /*
355*b077aed3SPierre Pronchery * Retrieve the calculated tag
356*b077aed3SPierre Pronchery */
ossl_siv128_get_tag(SIV128_CONTEXT * ctx,unsigned char * tag,size_t len)357*b077aed3SPierre Pronchery int ossl_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len)
358*b077aed3SPierre Pronchery {
359*b077aed3SPierre Pronchery if (len != SIV_LEN)
360*b077aed3SPierre Pronchery return 0;
361*b077aed3SPierre Pronchery
362*b077aed3SPierre Pronchery /* Copy the tag into the supplied buffer */
363*b077aed3SPierre Pronchery memcpy(tag, ctx->tag.byte, len);
364*b077aed3SPierre Pronchery return 1;
365*b077aed3SPierre Pronchery }
366*b077aed3SPierre Pronchery
367*b077aed3SPierre Pronchery /*
368*b077aed3SPierre Pronchery * Release all resources
369*b077aed3SPierre Pronchery */
ossl_siv128_cleanup(SIV128_CONTEXT * ctx)370*b077aed3SPierre Pronchery int ossl_siv128_cleanup(SIV128_CONTEXT *ctx)
371*b077aed3SPierre Pronchery {
372*b077aed3SPierre Pronchery if (ctx != NULL) {
373*b077aed3SPierre Pronchery EVP_CIPHER_CTX_free(ctx->cipher_ctx);
374*b077aed3SPierre Pronchery ctx->cipher_ctx = NULL;
375*b077aed3SPierre Pronchery EVP_MAC_CTX_free(ctx->mac_ctx_init);
376*b077aed3SPierre Pronchery ctx->mac_ctx_init = NULL;
377*b077aed3SPierre Pronchery EVP_MAC_free(ctx->mac);
378*b077aed3SPierre Pronchery ctx->mac = NULL;
379*b077aed3SPierre Pronchery OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
380*b077aed3SPierre Pronchery OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
381*b077aed3SPierre Pronchery ctx->final_ret = -1;
382*b077aed3SPierre Pronchery ctx->crypto_ok = 1;
383*b077aed3SPierre Pronchery }
384*b077aed3SPierre Pronchery return 1;
385*b077aed3SPierre Pronchery }
386*b077aed3SPierre Pronchery
ossl_siv128_speed(SIV128_CONTEXT * ctx,int arg)387*b077aed3SPierre Pronchery int ossl_siv128_speed(SIV128_CONTEXT *ctx, int arg)
388*b077aed3SPierre Pronchery {
389*b077aed3SPierre Pronchery ctx->crypto_ok = (arg == 1) ? -1 : 1;
390*b077aed3SPierre Pronchery return 1;
391*b077aed3SPierre Pronchery }
392*b077aed3SPierre Pronchery
393*b077aed3SPierre Pronchery #endif /* OPENSSL_NO_SIV */
394