2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/crypto.h>
13 #include <openssl/cmac.h>
14 #include "modes_lcl.h"
16 #ifndef OPENSSL_NO_SIV
18 __owur static ossl_inline uint32_t rotl8(uint32_t x)
20 return (x << 8) | (x >> 24);
23 __owur static ossl_inline uint32_t rotr8(uint32_t x)
25 return (x >> 8) | (x << 24);
28 __owur static ossl_inline uint64_t byteswap8(uint64_t x)
30 uint32_t high = (uint32_t)(x >> 32);
31 uint32_t low = (uint32_t)x;
33 high = (rotl8(high) & 0x00ff00ff) | (rotr8(high) & 0xff00ff00);
34 low = (rotl8(low) & 0x00ff00ff) | (rotr8(low) & 0xff00ff00);
35 return ((uint64_t)low) << 32 | (uint64_t)high;
38 __owur static ossl_inline uint64_t siv128_getword(SIV_BLOCK const *b, size_t i)
46 return byteswap8(b->word[i]);
50 static ossl_inline void siv128_putword(SIV_BLOCK *b, size_t i, uint64_t x)
58 b->word[i] = byteswap8(x);
63 static ossl_inline void siv128_xorblock(SIV_BLOCK *x,
66 x->word[0] ^= y->word[0];
67 x->word[1] ^= y->word[1];
71 * Doubles |b|, which is 16 bytes representing an element
72 * of GF(2**128) modulo the irreducible polynomial
73 * x**128 + x**7 + x**2 + x + 1.
74 * Assumes two's-complement arithmetic
76 static ossl_inline void siv128_dbl(SIV_BLOCK *b)
78 uint64_t high = siv128_getword(b, 0);
79 uint64_t low = siv128_getword(b, 1);
80 uint64_t high_carry = high & (((uint64_t)1) << 63);
81 uint64_t low_carry = low & (((uint64_t)1) << 63);
82 int64_t low_mask = -((int64_t)(high_carry >> 63)) & 0x87;
83 uint64_t high_mask = low_carry >> 63;
85 high = (high << 1) | high_mask;
86 low = (low << 1) ^ (uint64_t)low_mask;
87 siv128_putword(b, 0, high);
88 siv128_putword(b, 1, low);
91 __owur static ossl_inline int siv128_do_s2v_p(SIV128_CONTEXT *ctx, SIV_BLOCK *out,
92 unsigned char const* in, size_t len)
95 size_t out_len = sizeof(out->byte);
97 if (!CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init))
100 if (len >= SIV_LEN) {
101 if (!CMAC_Update(ctx->cmac_ctx, in, len - SIV_LEN))
103 memcpy(&t, in + (len-SIV_LEN), SIV_LEN);
104 siv128_xorblock(&t, &ctx->d);
105 if (!CMAC_Update(ctx->cmac_ctx, t.byte, SIV_LEN))
108 memset(&t, 0, sizeof(t));
112 siv128_xorblock(&t, &ctx->d);
113 if (!CMAC_Update(ctx->cmac_ctx, t.byte, SIV_LEN))
116 if (!CMAC_Final(ctx->cmac_ctx, out->byte, &out_len)
117 || out_len != SIV_LEN)
123 __owur static ossl_inline int siv128_do_encrypt(EVP_CIPHER_CTX *ctx, unsigned char *out,
124 unsigned char const *in, size_t len,
127 int out_len = (int)len;
129 if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, icv->byte, 1))
131 return EVP_EncryptUpdate(ctx, out, &out_len, in, out_len);
135 * Create a new SIV128_CONTEXT
137 SIV128_CONTEXT *CRYPTO_siv128_new(const unsigned char *key, int klen, EVP_CIPHER* cbc, EVP_CIPHER* ctr)
142 if ((ctx = OPENSSL_malloc(sizeof(*ctx))) != NULL) {
143 ret = CRYPTO_siv128_init(ctx, key, klen, cbc, ctr);
153 * Initialise an existing SIV128_CONTEXT
155 int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
156 const EVP_CIPHER* cbc, const EVP_CIPHER* ctr)
158 static const unsigned char zero[SIV_LEN] = { 0 };
159 size_t out_len = SIV_LEN;
161 memset(&ctx->d, 0, sizeof(ctx->d));
162 ctx->cipher_ctx = NULL;
163 ctx->cmac_ctx = NULL;
164 ctx->cmac_ctx_init = NULL;
166 if (key == NULL || cbc == NULL || ctr == NULL
167 || (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
168 || (ctx->cmac_ctx_init = CMAC_CTX_new()) == NULL
169 || (ctx->cmac_ctx = CMAC_CTX_new()) == NULL
170 || !CMAC_Init(ctx->cmac_ctx_init, key, klen, cbc, NULL)
171 || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
172 || !CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init)
173 || !CMAC_Update(ctx->cmac_ctx, zero, sizeof(zero))
174 || !CMAC_Final(ctx->cmac_ctx, ctx->d.byte, &out_len)) {
175 EVP_CIPHER_CTX_free(ctx->cipher_ctx);
176 CMAC_CTX_free(ctx->cmac_ctx_init);
177 CMAC_CTX_free(ctx->cmac_ctx);
188 * Copy an SIV128_CONTEXT object
190 int CRYPTO_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
192 memcpy(&dest->d, &src->d, sizeof(src->d));
193 if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
195 if (!CMAC_CTX_copy(dest->cmac_ctx_init, src->cmac_ctx_init))
197 /* no need to copy cmac_ctx since it's temp storage */
202 * Provide any AAD. This can be called multiple times.
203 * Per RFC5297, the last piece of associated data
204 * is the nonce, but it's not treated special
206 int CRYPTO_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad,
210 size_t out_len = SIV_LEN;
214 if (!CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init)
215 || !CMAC_Update(ctx->cmac_ctx, aad, len)
216 || !CMAC_Final(ctx->cmac_ctx, cmac_out.byte, &out_len)
217 || out_len != SIV_LEN)
220 siv128_xorblock(&ctx->d, &cmac_out);
227 * Provide any data to be encrypted. This can be called once.
229 int CRYPTO_siv128_encrypt(SIV128_CONTEXT *ctx,
230 const unsigned char *in, unsigned char *out,
235 /* can only do one crypto operation */
236 if (ctx->crypto_ok == 0)
240 if (!siv128_do_s2v_p(ctx, &q, in, len))
243 memcpy(ctx->tag.byte, &q, SIV_LEN);
247 if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q))
254 * Provide any data to be decrypted. This can be called once.
256 int CRYPTO_siv128_decrypt(SIV128_CONTEXT *ctx,
257 const unsigned char *in, unsigned char *out,
264 /* can only do one crypto operation */
265 if (ctx->crypto_ok == 0)
269 memcpy(&q, ctx->tag.byte, SIV_LEN);
273 if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q)
274 || !siv128_do_s2v_p(ctx, &t, out, len))
278 for (i = 0; i < SIV_LEN; i++)
281 if ((t.word[0] | t.word[1]) != 0) {
282 OPENSSL_cleanse(out, len);
290 * Return the already calculated final result.
292 int CRYPTO_siv128_finish(SIV128_CONTEXT *ctx)
294 return ctx->final_ret;
300 int CRYPTO_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len)
305 /* Copy the tag from the supplied buffer */
306 memcpy(ctx->tag.byte, tag, len);
311 * Retrieve the calculated tag
313 int CRYPTO_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len)
318 /* Copy the tag into the supplied buffer */
319 memcpy(tag, ctx->tag.byte, len);
324 * Release all resources
326 int CRYPTO_siv128_cleanup(SIV128_CONTEXT *ctx)
329 EVP_CIPHER_CTX_free(ctx->cipher_ctx);
330 ctx->cipher_ctx = NULL;
331 CMAC_CTX_free(ctx->cmac_ctx_init);
332 ctx->cmac_ctx_init = NULL;
333 CMAC_CTX_free(ctx->cmac_ctx);
334 ctx->cmac_ctx = NULL;
335 OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
336 OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
343 int CRYPTO_siv128_speed(SIV128_CONTEXT *ctx, int arg)
345 ctx->crypto_ok = (arg == 1) ? -1 : 1;
349 #endif /* OPENSSL_NO_SIV */