Fix some undefined behaviour in the Curve448 code (2nd attempt)
[openssl.git] / crypto / evp / m_sha3.c
1 /*
2  * Copyright 2017 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 <stdio.h>
11 #include <string.h>
12
13 #include <openssl/evp.h>
14 #include <openssl/objects.h>
15 #include "internal/evp_int.h"
16 #include "evp_locl.h"
17
18 size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
19                    size_t r);
20 void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
21
22 #define KECCAK1600_WIDTH 1600
23
24 typedef struct {
25     uint64_t A[5][5];
26     size_t block_size;          /* cached ctx->digest->block_size */
27     size_t md_size;             /* output length, variable in XOF */
28     size_t num;                 /* used bytes in below buffer */
29     unsigned char buf[KECCAK1600_WIDTH / 8 - 32];
30     unsigned char pad;
31 } KECCAK1600_CTX;
32
33 static int init(EVP_MD_CTX *evp_ctx, unsigned char pad)
34 {
35     KECCAK1600_CTX *ctx = evp_ctx->md_data;
36     size_t bsz = evp_ctx->digest->block_size;
37
38     if (bsz <= sizeof(ctx->buf)) {
39         memset(ctx->A, 0, sizeof(ctx->A));
40
41         ctx->num = 0;
42         ctx->block_size = bsz;
43         ctx->md_size = evp_ctx->digest->md_size;
44         ctx->pad = pad;
45
46         return 1;
47     }
48
49     return 0;
50 }
51
52 static int sha3_init(EVP_MD_CTX *evp_ctx)
53 {
54     return init(evp_ctx, '\x06');
55 }
56
57 static int shake_init(EVP_MD_CTX *evp_ctx)
58 {
59     return init(evp_ctx, '\x1f');
60 }
61
62 static int sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
63 {
64     KECCAK1600_CTX *ctx = evp_ctx->md_data;
65     const unsigned char *inp = _inp;
66     size_t bsz = ctx->block_size;
67     size_t num, rem;
68
69     if (len == 0)
70         return 1;
71
72     if ((num = ctx->num) != 0) {      /* process intermediate buffer? */
73         rem = bsz - num;
74
75         if (len < rem) {
76             memcpy(ctx->buf + num, inp, len);
77             ctx->num += len;
78             return 1;
79         }
80         /*
81          * We have enough data to fill or overflow the intermediate
82          * buffer. So we append |rem| bytes and process the block,
83          * leaving the rest for later processing...
84          */
85         memcpy(ctx->buf + num, inp, rem);
86         inp += rem, len -= rem;
87         (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
88         ctx->num = 0;
89         /* ctx->buf is processed, ctx->num is guaranteed to be zero */
90     }
91
92     if (len >= bsz)
93         rem = SHA3_absorb(ctx->A, inp, len, bsz);
94     else
95         rem = len;
96
97     if (rem) {
98         memcpy(ctx->buf, inp + len - rem, rem);
99         ctx->num = rem;
100     }
101
102     return 1;
103 }
104
105 static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
106 {
107     KECCAK1600_CTX *ctx = evp_ctx->md_data;
108     size_t bsz = ctx->block_size;
109     size_t num = ctx->num;
110
111     /*
112      * Pad the data with 10*1. Note that |num| can be |bsz - 1|
113      * in which case both byte operations below are performed on
114      * same byte...
115      */
116     memset(ctx->buf + num, 0, bsz - num);
117     ctx->buf[num] = ctx->pad;
118     ctx->buf[bsz - 1] |= 0x80;
119
120     (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
121
122     SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
123
124     return 1;
125 }
126
127 static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
128 {
129     KECCAK1600_CTX *ctx = evp_ctx->md_data;
130
131     switch (cmd) {
132     case EVP_MD_CTRL_XOF_LEN:
133         ctx->md_size = p1;
134         return 1;
135     default:
136         return 0;
137     }
138 }
139
140 #define EVP_MD_SHA3(bitlen)                     \
141 const EVP_MD *EVP_sha3_##bitlen(void)           \
142 {                                               \
143     static const EVP_MD sha3_##bitlen##_md = {  \
144         NID_sha3_##bitlen,                      \
145         NID_RSA_SHA3_##bitlen,                  \
146         bitlen / 8,                             \
147         EVP_MD_FLAG_DIGALGID_ABSENT,            \
148         sha3_init,                              \
149         sha3_update,                            \
150         sha3_final,                             \
151         NULL,                                   \
152         NULL,                                   \
153         (KECCAK1600_WIDTH - bitlen * 2) / 8,    \
154         sizeof(KECCAK1600_CTX),                 \
155     };                                          \
156     return &sha3_##bitlen##_md;                 \
157 }
158
159 EVP_MD_SHA3(224)
160 EVP_MD_SHA3(256)
161 EVP_MD_SHA3(384)
162 EVP_MD_SHA3(512)
163
164 #define EVP_MD_SHAKE(bitlen)                    \
165 const EVP_MD *EVP_shake##bitlen(void)           \
166 {                                               \
167     static const EVP_MD shake##bitlen##_md = {  \
168         NID_shake##bitlen,                      \
169         0,                                      \
170         bitlen / 8,                             \
171         EVP_MD_FLAG_XOF,                        \
172         shake_init,                             \
173         sha3_update,                            \
174         sha3_final,                             \
175         NULL,                                   \
176         NULL,                                   \
177         (KECCAK1600_WIDTH - bitlen * 2) / 8,    \
178         sizeof(KECCAK1600_CTX),                 \
179         shake_ctrl                              \
180     };                                          \
181     return &shake##bitlen##_md;                 \
182 }
183
184 EVP_MD_SHAKE(128)
185 EVP_MD_SHAKE(256)