/src/openssl30/crypto/evp/legacy_sha.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2019-2021 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 |  | /* | 
| 11 |  |  * All SHA low level APIs are deprecated for public use, but still ok for | 
| 12 |  |  * internal use. | 
| 13 |  |  */ | 
| 14 |  | #include "internal/deprecated.h" | 
| 15 |  |  | 
| 16 |  | #include <openssl/sha.h>         /* diverse SHA macros */ | 
| 17 |  | #include "internal/sha3.h"       /* KECCAK1600_WIDTH */ | 
| 18 |  | #include "crypto/evp.h" | 
| 19 |  | /* Used by legacy methods */ | 
| 20 |  | #include "crypto/sha.h" | 
| 21 |  | #include "legacy_meth.h" | 
| 22 |  | #include "evp_local.h" | 
| 23 |  |  | 
| 24 |  | /*- | 
| 25 |  |  * LEGACY methods for SHA. | 
| 26 |  |  * These only remain to support engines that can get these methods. | 
| 27 |  |  * Hardware support for SHA3 has been removed from these legacy cases. | 
| 28 |  |  */ | 
| 29 |  | #define IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(nm, fn, tag)                         \ | 
| 30 | 0 | static int nm##_init(EVP_MD_CTX *ctx)                                          \ | 
| 31 | 0 | {                                                                              \ | 
| 32 | 0 |     return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \ | 
| 33 | 0 | }                                                                              \ | 
| 34 | 0 | static int nm##_update(EVP_MD_CTX *ctx, const void *data, size_t count)        \ | 
| 35 | 0 | {                                                                              \ | 
| 36 | 0 |     return fn##_update(EVP_MD_CTX_get0_md_data(ctx), data, count);             \ | 
| 37 | 0 | }                                                                              \ | 
| 38 | 0 | static int nm##_final(EVP_MD_CTX *ctx, unsigned char *md)                      \ | 
| 39 | 0 | {                                                                              \ | 
| 40 | 0 |     return fn##_final(md, EVP_MD_CTX_get0_md_data(ctx));                       \ | 
| 41 | 0 | } | 
| 42 |  | #define IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(nm, fn, tag)                        \ | 
| 43 | 0 | static int nm##_init(EVP_MD_CTX *ctx)                                          \ | 
| 44 | 0 | {                                                                              \ | 
| 45 | 0 |     return fn##_init(EVP_MD_CTX_get0_md_data(ctx), tag, ctx->digest->md_size * 8); \ | 
| 46 | 0 | }                                                                              \ | 
| 47 |  |  | 
| 48 | 0 | #define sha512_224_Init    sha512_224_init | 
| 49 | 0 | #define sha512_256_Init    sha512_256_init | 
| 50 |  |  | 
| 51 | 0 | #define sha512_224_Update  SHA512_Update | 
| 52 | 0 | #define sha512_224_Final   SHA512_Final | 
| 53 | 0 | #define sha512_256_Update  SHA512_Update | 
| 54 | 0 | #define sha512_256_Final   SHA512_Final | 
| 55 |  |  | 
| 56 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha1, SHA1) | 
| 57 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha224, SHA224) | 
| 58 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha256, SHA256) | 
| 59 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha384, SHA384) | 
| 60 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha512, SHA512) | 
| 61 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha512_224_int, sha512_224) | 
| 62 |  | IMPLEMENT_LEGACY_EVP_MD_METH(sha512_256_int, sha512_256) | 
| 63 |  | IMPLEMENT_LEGACY_EVP_MD_METH_SHA3(sha3_int, ossl_sha3, '\x06') | 
| 64 |  | IMPLEMENT_LEGACY_EVP_MD_METH_SHAKE(shake, ossl_sha3, '\x1f') | 
| 65 |  |  | 
| 66 |  | static int sha1_int_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void *p2) | 
| 67 | 0 | { | 
| 68 | 0 |     return ossl_sha1_ctrl(ctx != NULL ? EVP_MD_CTX_get0_md_data(ctx) : NULL, | 
| 69 | 0 |                           cmd, p1, p2); | 
| 70 | 0 | } | 
| 71 |  |  | 
| 72 |  | static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2) | 
| 73 | 0 | { | 
| 74 | 0 |     KECCAK1600_CTX *ctx = evp_ctx->md_data; | 
| 75 |  | 
 | 
| 76 | 0 |     switch (cmd) { | 
| 77 | 0 |     case EVP_MD_CTRL_XOF_LEN: | 
| 78 | 0 |         ctx->md_size = p1; | 
| 79 | 0 |         return 1; | 
| 80 | 0 |     default: | 
| 81 | 0 |         return 0; | 
| 82 | 0 |     } | 
| 83 | 0 | } | 
| 84 |  |  | 
| 85 |  |  | 
| 86 |  |  | 
| 87 |  | static const EVP_MD sha1_md = { | 
| 88 |  |     NID_sha1, | 
| 89 |  |     NID_sha1WithRSAEncryption, | 
| 90 |  |     SHA_DIGEST_LENGTH, | 
| 91 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 92 |  |     EVP_ORIG_GLOBAL, | 
| 93 |  |     LEGACY_EVP_MD_METH_TABLE(sha1_init, sha1_update, sha1_final, sha1_int_ctrl, | 
| 94 |  |                              SHA_CBLOCK), | 
| 95 |  | }; | 
| 96 |  |  | 
| 97 |  | const EVP_MD *EVP_sha1(void) | 
| 98 | 297k | { | 
| 99 | 297k |     return &sha1_md; | 
| 100 | 297k | } | 
| 101 |  |  | 
| 102 |  | static const EVP_MD sha224_md = { | 
| 103 |  |     NID_sha224, | 
| 104 |  |     NID_sha224WithRSAEncryption, | 
| 105 |  |     SHA224_DIGEST_LENGTH, | 
| 106 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 107 |  |     EVP_ORIG_GLOBAL, | 
| 108 |  |     LEGACY_EVP_MD_METH_TABLE(sha224_init, sha224_update, sha224_final, NULL, | 
| 109 |  |                              SHA256_CBLOCK), | 
| 110 |  | }; | 
| 111 |  |  | 
| 112 |  | const EVP_MD *EVP_sha224(void) | 
| 113 | 37 | { | 
| 114 | 37 |     return &sha224_md; | 
| 115 | 37 | } | 
| 116 |  |  | 
| 117 |  | static const EVP_MD sha256_md = { | 
| 118 |  |     NID_sha256, | 
| 119 |  |     NID_sha256WithRSAEncryption, | 
| 120 |  |     SHA256_DIGEST_LENGTH, | 
| 121 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 122 |  |     EVP_ORIG_GLOBAL, | 
| 123 |  |     LEGACY_EVP_MD_METH_TABLE(sha256_init, sha256_update, sha256_final, NULL, | 
| 124 |  |                              SHA256_CBLOCK), | 
| 125 |  | }; | 
| 126 |  |  | 
| 127 |  | const EVP_MD *EVP_sha256(void) | 
| 128 | 4.51k | { | 
| 129 | 4.51k |     return &sha256_md; | 
| 130 | 4.51k | } | 
| 131 |  |  | 
| 132 |  | static const EVP_MD sha512_224_md = { | 
| 133 |  |     NID_sha512_224, | 
| 134 |  |     NID_sha512_224WithRSAEncryption, | 
| 135 |  |     SHA224_DIGEST_LENGTH, | 
| 136 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 137 |  |     EVP_ORIG_GLOBAL, | 
| 138 |  |     LEGACY_EVP_MD_METH_TABLE(sha512_224_int_init, sha512_224_int_update, | 
| 139 |  |                              sha512_224_int_final, NULL, SHA512_CBLOCK), | 
| 140 |  | }; | 
| 141 |  |  | 
| 142 |  | const EVP_MD *EVP_sha512_224(void) | 
| 143 | 31 | { | 
| 144 | 31 |     return &sha512_224_md; | 
| 145 | 31 | } | 
| 146 |  |  | 
| 147 |  | static const EVP_MD sha512_256_md = { | 
| 148 |  |     NID_sha512_256, | 
| 149 |  |     NID_sha512_256WithRSAEncryption, | 
| 150 |  |     SHA256_DIGEST_LENGTH, | 
| 151 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 152 |  |     EVP_ORIG_GLOBAL, | 
| 153 |  |     LEGACY_EVP_MD_METH_TABLE(sha512_256_int_init, sha512_256_int_update, | 
| 154 |  |                              sha512_256_int_final, NULL, SHA512_CBLOCK), | 
| 155 |  | }; | 
| 156 |  |  | 
| 157 |  | const EVP_MD *EVP_sha512_256(void) | 
| 158 | 31 | { | 
| 159 | 31 |     return &sha512_256_md; | 
| 160 | 31 | } | 
| 161 |  |  | 
| 162 |  | static const EVP_MD sha384_md = { | 
| 163 |  |     NID_sha384, | 
| 164 |  |     NID_sha384WithRSAEncryption, | 
| 165 |  |     SHA384_DIGEST_LENGTH, | 
| 166 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 167 |  |     EVP_ORIG_GLOBAL, | 
| 168 |  |     LEGACY_EVP_MD_METH_TABLE(sha384_init, sha384_update, sha384_final, NULL, | 
| 169 |  |                              SHA512_CBLOCK), | 
| 170 |  | }; | 
| 171 |  |  | 
| 172 |  | const EVP_MD *EVP_sha384(void) | 
| 173 | 37 | { | 
| 174 | 37 |     return &sha384_md; | 
| 175 | 37 | } | 
| 176 |  |  | 
| 177 |  | static const EVP_MD sha512_md = { | 
| 178 |  |     NID_sha512, | 
| 179 |  |     NID_sha512WithRSAEncryption, | 
| 180 |  |     SHA512_DIGEST_LENGTH, | 
| 181 |  |     EVP_MD_FLAG_DIGALGID_ABSENT, | 
| 182 |  |     EVP_ORIG_GLOBAL, | 
| 183 |  |     LEGACY_EVP_MD_METH_TABLE(sha512_init, sha512_update, sha512_final, NULL, | 
| 184 |  |                              SHA512_CBLOCK), | 
| 185 |  | }; | 
| 186 |  |  | 
| 187 |  | const EVP_MD *EVP_sha512(void) | 
| 188 | 37 | { | 
| 189 | 37 |     return &sha512_md; | 
| 190 | 37 | } | 
| 191 |  |  | 
| 192 |  | #define EVP_MD_SHA3(bitlen)                                                    \ | 
| 193 | 124 | const EVP_MD *EVP_sha3_##bitlen(void)                                          \ | 
| 194 | 124 | {                                                                              \ | 
| 195 | 124 |     static const EVP_MD sha3_##bitlen##_md = {                                 \ | 
| 196 | 124 |         NID_sha3_##bitlen,                                                     \ | 
| 197 | 124 |         NID_RSA_SHA3_##bitlen,                                                 \ | 
| 198 | 124 |         bitlen / 8,                                                            \ | 
| 199 | 124 |         EVP_MD_FLAG_DIGALGID_ABSENT,                                           \ | 
| 200 | 124 |         EVP_ORIG_GLOBAL,                                                       \ | 
| 201 | 124 |         LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \ | 
| 202 | 124 |                                  sha3_int_final, NULL,                         \ | 
| 203 | 124 |                                  (KECCAK1600_WIDTH - bitlen * 2) / 8),         \ | 
| 204 | 124 |     };                                                                         \ | 
| 205 | 124 |     return &sha3_##bitlen##_md;                                                \ | 
| 206 | 124 | } | Line | Count | Source |  | 193 | 31 | const EVP_MD *EVP_sha3_##bitlen(void)                                          \ |  | 194 | 31 | {                                                                              \ |  | 195 | 31 |     static const EVP_MD sha3_##bitlen##_md = {                                 \ |  | 196 | 31 |         NID_sha3_##bitlen,                                                     \ |  | 197 | 31 |         NID_RSA_SHA3_##bitlen,                                                 \ |  | 198 | 31 |         bitlen / 8,                                                            \ |  | 199 | 31 |         EVP_MD_FLAG_DIGALGID_ABSENT,                                           \ |  | 200 | 31 |         EVP_ORIG_GLOBAL,                                                       \ |  | 201 | 31 |         LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \ |  | 202 | 31 |                                  sha3_int_final, NULL,                         \ |  | 203 | 31 |                                  (KECCAK1600_WIDTH - bitlen * 2) / 8),         \ |  | 204 | 31 |     };                                                                         \ |  | 205 | 31 |     return &sha3_##bitlen##_md;                                                \ |  | 206 | 31 | } | 
| Line | Count | Source |  | 193 | 31 | const EVP_MD *EVP_sha3_##bitlen(void)                                          \ |  | 194 | 31 | {                                                                              \ |  | 195 | 31 |     static const EVP_MD sha3_##bitlen##_md = {                                 \ |  | 196 | 31 |         NID_sha3_##bitlen,                                                     \ |  | 197 | 31 |         NID_RSA_SHA3_##bitlen,                                                 \ |  | 198 | 31 |         bitlen / 8,                                                            \ |  | 199 | 31 |         EVP_MD_FLAG_DIGALGID_ABSENT,                                           \ |  | 200 | 31 |         EVP_ORIG_GLOBAL,                                                       \ |  | 201 | 31 |         LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \ |  | 202 | 31 |                                  sha3_int_final, NULL,                         \ |  | 203 | 31 |                                  (KECCAK1600_WIDTH - bitlen * 2) / 8),         \ |  | 204 | 31 |     };                                                                         \ |  | 205 | 31 |     return &sha3_##bitlen##_md;                                                \ |  | 206 | 31 | } | 
| Line | Count | Source |  | 193 | 31 | const EVP_MD *EVP_sha3_##bitlen(void)                                          \ |  | 194 | 31 | {                                                                              \ |  | 195 | 31 |     static const EVP_MD sha3_##bitlen##_md = {                                 \ |  | 196 | 31 |         NID_sha3_##bitlen,                                                     \ |  | 197 | 31 |         NID_RSA_SHA3_##bitlen,                                                 \ |  | 198 | 31 |         bitlen / 8,                                                            \ |  | 199 | 31 |         EVP_MD_FLAG_DIGALGID_ABSENT,                                           \ |  | 200 | 31 |         EVP_ORIG_GLOBAL,                                                       \ |  | 201 | 31 |         LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \ |  | 202 | 31 |                                  sha3_int_final, NULL,                         \ |  | 203 | 31 |                                  (KECCAK1600_WIDTH - bitlen * 2) / 8),         \ |  | 204 | 31 |     };                                                                         \ |  | 205 | 31 |     return &sha3_##bitlen##_md;                                                \ |  | 206 | 31 | } | 
| Line | Count | Source |  | 193 | 31 | const EVP_MD *EVP_sha3_##bitlen(void)                                          \ |  | 194 | 31 | {                                                                              \ |  | 195 | 31 |     static const EVP_MD sha3_##bitlen##_md = {                                 \ |  | 196 | 31 |         NID_sha3_##bitlen,                                                     \ |  | 197 | 31 |         NID_RSA_SHA3_##bitlen,                                                 \ |  | 198 | 31 |         bitlen / 8,                                                            \ |  | 199 | 31 |         EVP_MD_FLAG_DIGALGID_ABSENT,                                           \ |  | 200 | 31 |         EVP_ORIG_GLOBAL,                                                       \ |  | 201 | 31 |         LEGACY_EVP_MD_METH_TABLE(sha3_int_init, sha3_int_update,               \ |  | 202 | 31 |                                  sha3_int_final, NULL,                         \ |  | 203 | 31 |                                  (KECCAK1600_WIDTH - bitlen * 2) / 8),         \ |  | 204 | 31 |     };                                                                         \ |  | 205 | 31 |     return &sha3_##bitlen##_md;                                                \ |  | 206 | 31 | } | 
 | 
| 207 |  | #define EVP_MD_SHAKE(bitlen)                                                   \ | 
| 208 | 85 | const EVP_MD *EVP_shake##bitlen(void)                                          \ | 
| 209 | 85 | {                                                                              \ | 
| 210 | 85 |     static const EVP_MD shake##bitlen##_md = {                                 \ | 
| 211 | 85 |         NID_shake##bitlen,                                                     \ | 
| 212 | 85 |         0,                                                                     \ | 
| 213 | 85 |         bitlen / 8,                                                            \ | 
| 214 | 85 |         EVP_MD_FLAG_XOF,                                                       \ | 
| 215 | 85 |         EVP_ORIG_GLOBAL,                                                       \ | 
| 216 | 85 |         LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final,  \ | 
| 217 | 85 |                         shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8),      \ | 
| 218 | 85 |     };                                                                         \ | 
| 219 | 85 |     return &shake##bitlen##_md;                                                \ | 
| 220 | 85 | } | Line | Count | Source |  | 208 | 31 | const EVP_MD *EVP_shake##bitlen(void)                                          \ |  | 209 | 31 | {                                                                              \ |  | 210 | 31 |     static const EVP_MD shake##bitlen##_md = {                                 \ |  | 211 | 31 |         NID_shake##bitlen,                                                     \ |  | 212 | 31 |         0,                                                                     \ |  | 213 | 31 |         bitlen / 8,                                                            \ |  | 214 | 31 |         EVP_MD_FLAG_XOF,                                                       \ |  | 215 | 31 |         EVP_ORIG_GLOBAL,                                                       \ |  | 216 | 31 |         LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final,  \ |  | 217 | 31 |                         shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8),      \ |  | 218 | 31 |     };                                                                         \ |  | 219 | 31 |     return &shake##bitlen##_md;                                                \ |  | 220 | 31 | } | 
| Line | Count | Source |  | 208 | 54 | const EVP_MD *EVP_shake##bitlen(void)                                          \ |  | 209 | 54 | {                                                                              \ |  | 210 | 54 |     static const EVP_MD shake##bitlen##_md = {                                 \ |  | 211 | 54 |         NID_shake##bitlen,                                                     \ |  | 212 | 54 |         0,                                                                     \ |  | 213 | 54 |         bitlen / 8,                                                            \ |  | 214 | 54 |         EVP_MD_FLAG_XOF,                                                       \ |  | 215 | 54 |         EVP_ORIG_GLOBAL,                                                       \ |  | 216 | 54 |         LEGACY_EVP_MD_METH_TABLE(shake_init, sha3_int_update, sha3_int_final,  \ |  | 217 | 54 |                         shake_ctrl, (KECCAK1600_WIDTH - bitlen * 2) / 8),      \ |  | 218 | 54 |     };                                                                         \ |  | 219 | 54 |     return &shake##bitlen##_md;                                                \ |  | 220 | 54 | } | 
 | 
| 221 |  |  | 
| 222 |  | EVP_MD_SHA3(224) | 
| 223 |  | EVP_MD_SHA3(256) | 
| 224 |  | EVP_MD_SHA3(384) | 
| 225 |  | EVP_MD_SHA3(512) | 
| 226 |  |  | 
| 227 |  | EVP_MD_SHAKE(128) | 
| 228 |  | EVP_MD_SHAKE(256) |