Add OIDs for HMAC SHA512/224 and HMAC SHA512/256.
[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 ((num = ctx->num) != 0) {      /* process intermediate buffer? */
70         rem = bsz - num;
71
72         if (len < rem) {
73             memcpy(ctx->buf + num, inp, len);
74             ctx->num += len;
75             return 1;
76         }
77         /*
78          * We have enough data to fill or overflow the intermediate
79          * buffer. So we append |rem| bytes and process the block,
80          * leaving the rest for later processing...
81          */
82         memcpy(ctx->buf + num, inp, rem);
83         inp += rem, len -= rem;
84         (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
85         ctx->num = 0;
86         /* ctx->buf is processed, ctx->num is guaranteed to be zero */
87     }
88
89     if (len >= bsz)
90         rem = SHA3_absorb(ctx->A, inp, len, bsz);
91     else
92         rem = len;
93
94     if (rem) {
95         memcpy(ctx->buf, inp + len - rem, rem);
96         ctx->num = rem;
97     }
98
99     return 1;
100 }
101
102 static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
103 {
104     KECCAK1600_CTX *ctx = evp_ctx->md_data;
105     size_t bsz = ctx->block_size;
106     size_t num = ctx->num;
107
108     /*
109      * Pad the data with 10*1. Note that |num| can be |bsz - 1|
110      * in which case both byte operations below are performed on
111      * same byte...
112      */
113     memset(ctx->buf + num, 0, bsz - num);
114     ctx->buf[num] = ctx->pad;
115     ctx->buf[bsz - 1] |= 0x80;
116
117     (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
118
119     SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
120
121     return 1;
122 }
123
124 static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
125 {
126     KECCAK1600_CTX *ctx = evp_ctx->md_data;
127
128     switch (cmd) {
129     case EVP_MD_CTRL_XOF_LEN:
130         ctx->md_size = p1;
131         return 1;
132     default:
133         return 0;
134     }
135 }
136
137 #define EVP_MD_SHA3(bitlen)                     \
138 const EVP_MD *EVP_sha3_##bitlen(void)           \
139 {                                               \
140     static const EVP_MD sha3_##bitlen##_md = {  \
141         NID_sha3_##bitlen,                      \
142         NID_RSA_SHA3_##bitlen,                  \
143         bitlen / 8,                             \
144         EVP_MD_FLAG_DIGALGID_ABSENT,            \
145         sha3_init,                              \
146         sha3_update,                            \
147         sha3_final,                             \
148         NULL,                                   \
149         NULL,                                   \
150         (KECCAK1600_WIDTH - bitlen * 2) / 8,    \
151         sizeof(KECCAK1600_CTX),                 \
152     };                                          \
153     return &sha3_##bitlen##_md;                 \
154 }
155
156 EVP_MD_SHA3(224)
157 EVP_MD_SHA3(256)
158 EVP_MD_SHA3(384)
159 EVP_MD_SHA3(512)
160
161 #define EVP_MD_SHAKE(bitlen)                    \
162 const EVP_MD *EVP_shake##bitlen(void)           \
163 {                                               \
164     static const EVP_MD shake##bitlen##_md = {  \
165         NID_shake##bitlen,                      \
166         0,                                      \
167         bitlen / 8,                             \
168         EVP_MD_FLAG_XOF,                        \
169         shake_init,                             \
170         sha3_update,                            \
171         sha3_final,                             \
172         NULL,                                   \
173         NULL,                                   \
174         (KECCAK1600_WIDTH - bitlen * 2) / 8,    \
175         sizeof(KECCAK1600_CTX),                 \
176         shake_ctrl                              \
177     };                                          \
178     return &shake##bitlen##_md;                 \
179 }
180
181 EVP_MD_SHAKE(128)
182 EVP_MD_SHAKE(256)