1 /* $OpenBSD: m_sha1.c,v 1.18 2021/12/12 21:30:13 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #ifndef OPENSSL_NO_SHA 64 65 #include <openssl/evp.h> 66 #include <openssl/objects.h> 67 #include <openssl/sha.h> 68 69 #ifndef OPENSSL_NO_RSA 70 #include <openssl/rsa.h> 71 #endif 72 73 #include "evp_locl.h" 74 75 static int 76 init(EVP_MD_CTX *ctx) 77 { 78 return SHA1_Init(ctx->md_data); 79 } 80 81 static int 82 update(EVP_MD_CTX *ctx, const void *data, size_t count) 83 { 84 return SHA1_Update(ctx->md_data, data, count); 85 } 86 87 static int 88 final(EVP_MD_CTX *ctx, unsigned char *md) 89 { 90 return SHA1_Final(md, ctx->md_data); 91 } 92 93 static const EVP_MD sha1_md = { 94 .type = NID_sha1, 95 .pkey_type = NID_sha1WithRSAEncryption, 96 .md_size = SHA_DIGEST_LENGTH, 97 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 98 .init = init, 99 .update = update, 100 .final = final, 101 .copy = NULL, 102 .cleanup = NULL, 103 #ifndef OPENSSL_NO_RSA 104 .sign = (evp_sign_method *)RSA_sign, 105 .verify = (evp_verify_method *)RSA_verify, 106 .required_pkey_type = { 107 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 108 }, 109 #endif 110 .block_size = SHA_CBLOCK, 111 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA_CTX), 112 }; 113 114 const EVP_MD * 115 EVP_sha1(void) 116 { 117 return (&sha1_md); 118 } 119 #endif 120 121 #ifndef OPENSSL_NO_SHA256 122 static int 123 init224(EVP_MD_CTX *ctx) 124 { 125 return SHA224_Init(ctx->md_data); 126 } 127 128 static int 129 init256(EVP_MD_CTX *ctx) 130 { 131 return SHA256_Init(ctx->md_data); 132 } 133 /* 134 * Even though there're separate SHA224_[Update|Final], we call 135 * SHA256 functions even in SHA224 context. This is what happens 136 * there anyway, so we can spare few CPU cycles:-) 137 */ 138 static int 139 update256(EVP_MD_CTX *ctx, const void *data, size_t count) 140 { 141 return SHA256_Update(ctx->md_data, data, count); 142 } 143 144 static int 145 final256(EVP_MD_CTX *ctx, unsigned char *md) 146 { 147 return SHA256_Final(md, ctx->md_data); 148 } 149 150 static const EVP_MD sha224_md = { 151 .type = NID_sha224, 152 .pkey_type = NID_sha224WithRSAEncryption, 153 .md_size = SHA224_DIGEST_LENGTH, 154 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 155 .init = init224, 156 .update = update256, 157 .final = final256, 158 .copy = NULL, 159 .cleanup = NULL, 160 #ifndef OPENSSL_NO_RSA 161 .sign = (evp_sign_method *)RSA_sign, 162 .verify = (evp_verify_method *)RSA_verify, 163 .required_pkey_type = { 164 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 165 }, 166 #endif 167 .block_size = SHA256_CBLOCK, 168 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA256_CTX), 169 }; 170 171 const EVP_MD * 172 EVP_sha224(void) 173 { 174 return (&sha224_md); 175 } 176 177 static const EVP_MD sha256_md = { 178 .type = NID_sha256, 179 .pkey_type = NID_sha256WithRSAEncryption, 180 .md_size = SHA256_DIGEST_LENGTH, 181 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 182 .init = init256, 183 .update = update256, 184 .final = final256, 185 .copy = NULL, 186 .cleanup = NULL, 187 #ifndef OPENSSL_NO_RSA 188 .sign = (evp_sign_method *)RSA_sign, 189 .verify = (evp_verify_method *)RSA_verify, 190 .required_pkey_type = { 191 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 192 }, 193 #endif 194 .block_size = SHA256_CBLOCK, 195 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA256_CTX), 196 }; 197 198 const EVP_MD * 199 EVP_sha256(void) 200 { 201 return (&sha256_md); 202 } 203 #endif /* ifndef OPENSSL_NO_SHA256 */ 204 205 #ifndef OPENSSL_NO_SHA512 206 static int 207 init384(EVP_MD_CTX *ctx) 208 { 209 return SHA384_Init(ctx->md_data); 210 } 211 212 static int 213 init512(EVP_MD_CTX *ctx) 214 { 215 return SHA512_Init(ctx->md_data); 216 } 217 /* See comment in SHA224/256 section */ 218 static int 219 update512(EVP_MD_CTX *ctx, const void *data, size_t count) 220 { 221 return SHA512_Update(ctx->md_data, data, count); 222 } 223 224 static int 225 final512(EVP_MD_CTX *ctx, unsigned char *md) 226 { 227 return SHA512_Final(md, ctx->md_data); 228 } 229 230 static const EVP_MD sha384_md = { 231 .type = NID_sha384, 232 .pkey_type = NID_sha384WithRSAEncryption, 233 .md_size = SHA384_DIGEST_LENGTH, 234 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 235 .init = init384, 236 .update = update512, 237 .final = final512, 238 .copy = NULL, 239 .cleanup = NULL, 240 #ifndef OPENSSL_NO_RSA 241 .sign = (evp_sign_method *)RSA_sign, 242 .verify = (evp_verify_method *)RSA_verify, 243 .required_pkey_type = { 244 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 245 }, 246 #endif 247 .block_size = SHA512_CBLOCK, 248 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 249 }; 250 251 const EVP_MD * 252 EVP_sha384(void) 253 { 254 return (&sha384_md); 255 } 256 257 static const EVP_MD sha512_md = { 258 .type = NID_sha512, 259 .pkey_type = NID_sha512WithRSAEncryption, 260 .md_size = SHA512_DIGEST_LENGTH, 261 .flags = EVP_MD_FLAG_PKEY_METHOD_SIGNATURE|EVP_MD_FLAG_DIGALGID_ABSENT, 262 .init = init512, 263 .update = update512, 264 .final = final512, 265 .copy = NULL, 266 .cleanup = NULL, 267 #ifndef OPENSSL_NO_RSA 268 .sign = (evp_sign_method *)RSA_sign, 269 .verify = (evp_verify_method *)RSA_verify, 270 .required_pkey_type = { 271 EVP_PKEY_RSA, EVP_PKEY_RSA2, 0, 0, 272 }, 273 #endif 274 .block_size = SHA512_CBLOCK, 275 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 276 }; 277 278 const EVP_MD * 279 EVP_sha512(void) 280 { 281 return (&sha512_md); 282 } 283 #endif /* ifndef OPENSSL_NO_SHA512 */ 284