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