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