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