1 /* $OpenBSD: m_sha1.c,v 1.23 2023/04/16 16:42:06 jsing 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_local.h" 74 #include "sha_internal.h" 75 76 static int 77 sha1_init(EVP_MD_CTX *ctx) 78 { 79 return SHA1_Init(ctx->md_data); 80 } 81 82 static int 83 sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) 84 { 85 return SHA1_Update(ctx->md_data, data, count); 86 } 87 88 static int 89 sha1_final(EVP_MD_CTX *ctx, unsigned char *md) 90 { 91 return SHA1_Final(md, ctx->md_data); 92 } 93 94 static const EVP_MD sha1_md = { 95 .type = NID_sha1, 96 .pkey_type = NID_sha1WithRSAEncryption, 97 .md_size = SHA_DIGEST_LENGTH, 98 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 99 .init = sha1_init, 100 .update = sha1_update, 101 .final = sha1_final, 102 .copy = NULL, 103 .cleanup = NULL, 104 .block_size = SHA_CBLOCK, 105 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA_CTX), 106 }; 107 108 const EVP_MD * 109 EVP_sha1(void) 110 { 111 return &sha1_md; 112 } 113 #endif 114 115 #ifndef OPENSSL_NO_SHA256 116 static int 117 sha224_init(EVP_MD_CTX *ctx) 118 { 119 return SHA224_Init(ctx->md_data); 120 } 121 122 static int 123 sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) 124 { 125 /* 126 * Even though there're separate SHA224_[Update|Final], we call 127 * SHA256 functions even in SHA224 context. This is what happens 128 * there anyway, so we can spare few CPU cycles:-) 129 */ 130 return SHA256_Update(ctx->md_data, data, count); 131 } 132 133 static int 134 sha224_final(EVP_MD_CTX *ctx, unsigned char *md) 135 { 136 return SHA224_Final(md, ctx->md_data); 137 } 138 139 static const EVP_MD sha224_md = { 140 .type = NID_sha224, 141 .pkey_type = NID_sha224WithRSAEncryption, 142 .md_size = SHA224_DIGEST_LENGTH, 143 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 144 .init = sha224_init, 145 .update = sha224_update, 146 .final = sha224_final, 147 .copy = NULL, 148 .cleanup = NULL, 149 .block_size = SHA256_CBLOCK, 150 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA256_CTX), 151 }; 152 153 const EVP_MD * 154 EVP_sha224(void) 155 { 156 return &sha224_md; 157 } 158 159 static int 160 sha256_init(EVP_MD_CTX *ctx) 161 { 162 return SHA256_Init(ctx->md_data); 163 } 164 165 static int 166 sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) 167 { 168 return SHA256_Update(ctx->md_data, data, count); 169 } 170 171 static int 172 sha256_final(EVP_MD_CTX *ctx, unsigned char *md) 173 { 174 return SHA256_Final(md, ctx->md_data); 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_DIGALGID_ABSENT, 182 .init = sha256_init, 183 .update = sha256_update, 184 .final = sha256_final, 185 .copy = NULL, 186 .cleanup = NULL, 187 .block_size = SHA256_CBLOCK, 188 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA256_CTX), 189 }; 190 191 const EVP_MD * 192 EVP_sha256(void) 193 { 194 return &sha256_md; 195 } 196 #endif /* ifndef OPENSSL_NO_SHA256 */ 197 198 #ifndef OPENSSL_NO_SHA512 199 static int 200 sha384_init(EVP_MD_CTX *ctx) 201 { 202 return SHA384_Init(ctx->md_data); 203 } 204 205 static int 206 sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) 207 { 208 /* See comment in SHA224/256 section */ 209 return SHA512_Update(ctx->md_data, data, count); 210 } 211 212 static int 213 sha384_final(EVP_MD_CTX *ctx, unsigned char *md) 214 { 215 return SHA384_Final(md, ctx->md_data); 216 } 217 218 static const EVP_MD sha384_md = { 219 .type = NID_sha384, 220 .pkey_type = NID_sha384WithRSAEncryption, 221 .md_size = SHA384_DIGEST_LENGTH, 222 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 223 .init = sha384_init, 224 .update = sha384_update, 225 .final = sha384_final, 226 .copy = NULL, 227 .cleanup = NULL, 228 .block_size = SHA512_CBLOCK, 229 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 230 }; 231 232 const EVP_MD * 233 EVP_sha384(void) 234 { 235 return &sha384_md; 236 } 237 238 static int 239 sha512_init(EVP_MD_CTX *ctx) 240 { 241 return SHA512_Init(ctx->md_data); 242 } 243 244 static int 245 sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) 246 { 247 return SHA512_Update(ctx->md_data, data, count); 248 } 249 250 static int 251 sha512_final(EVP_MD_CTX *ctx, unsigned char *md) 252 { 253 return SHA512_Final(md, ctx->md_data); 254 } 255 256 static const EVP_MD sha512_md = { 257 .type = NID_sha512, 258 .pkey_type = NID_sha512WithRSAEncryption, 259 .md_size = SHA512_DIGEST_LENGTH, 260 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 261 .init = sha512_init, 262 .update = sha512_update, 263 .final = sha512_final, 264 .copy = NULL, 265 .cleanup = NULL, 266 .block_size = SHA512_CBLOCK, 267 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 268 }; 269 270 const EVP_MD * 271 EVP_sha512(void) 272 { 273 return &sha512_md; 274 } 275 276 static int 277 sha512_224_init(EVP_MD_CTX *ctx) 278 { 279 return SHA512_224_Init(ctx->md_data); 280 } 281 282 static int 283 sha512_224_update(EVP_MD_CTX *ctx, const void *data, size_t count) 284 { 285 return SHA512_224_Update(ctx->md_data, data, count); 286 } 287 288 static int 289 sha512_224_final(EVP_MD_CTX *ctx, unsigned char *md) 290 { 291 return SHA512_224_Final(md, ctx->md_data); 292 } 293 294 static const EVP_MD sha512_224_md = { 295 .type = NID_sha512_224, 296 .pkey_type = NID_sha512_224WithRSAEncryption, 297 .md_size = SHA512_224_DIGEST_LENGTH, 298 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 299 .init = sha512_224_init, 300 .update = sha512_224_update, 301 .final = sha512_224_final, 302 .copy = NULL, 303 .cleanup = NULL, 304 .block_size = SHA512_CBLOCK, 305 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 306 }; 307 308 const EVP_MD * 309 EVP_sha512_224(void) 310 { 311 return &sha512_224_md; 312 } 313 314 static int 315 sha512_256_init(EVP_MD_CTX *ctx) 316 { 317 return SHA512_256_Init(ctx->md_data); 318 } 319 320 static int 321 sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) 322 { 323 return SHA512_256_Update(ctx->md_data, data, count); 324 } 325 326 static int 327 sha512_256_final(EVP_MD_CTX *ctx, unsigned char *md) 328 { 329 return SHA512_256_Final(md, ctx->md_data); 330 } 331 332 static const EVP_MD sha512_256_md = { 333 .type = NID_sha512_256, 334 .pkey_type = NID_sha512_256WithRSAEncryption, 335 .md_size = SHA512_256_DIGEST_LENGTH, 336 .flags = EVP_MD_FLAG_DIGALGID_ABSENT, 337 .init = sha512_256_init, 338 .update = sha512_256_update, 339 .final = sha512_256_final, 340 .copy = NULL, 341 .cleanup = NULL, 342 .block_size = SHA512_CBLOCK, 343 .ctx_size = sizeof(EVP_MD *) + sizeof(SHA512_CTX), 344 }; 345 346 const EVP_MD * 347 EVP_sha512_256(void) 348 { 349 return &sha512_256_md; 350 } 351 #endif /* ifndef OPENSSL_NO_SHA512 */ 352