1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci/* 11e1051a39Sopenharmony_ci * SHA-1 low level APIs are deprecated for public use, but still ok for 12e1051a39Sopenharmony_ci * internal use. 13e1051a39Sopenharmony_ci */ 14e1051a39Sopenharmony_ci#include "internal/deprecated.h" 15e1051a39Sopenharmony_ci 16e1051a39Sopenharmony_ci#include <stdio.h> 17e1051a39Sopenharmony_ci#include <string.h> 18e1051a39Sopenharmony_ci#include <openssl/crypto.h> 19e1051a39Sopenharmony_ci#include <openssl/sha.h> 20e1051a39Sopenharmony_ci#include <openssl/evp.h> 21e1051a39Sopenharmony_ci#include "crypto/sha.h" 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ciunsigned char *ossl_sha1(const unsigned char *d, size_t n, unsigned char *md) 24e1051a39Sopenharmony_ci{ 25e1051a39Sopenharmony_ci SHA_CTX c; 26e1051a39Sopenharmony_ci static unsigned char m[SHA_DIGEST_LENGTH]; 27e1051a39Sopenharmony_ci 28e1051a39Sopenharmony_ci if (md == NULL) 29e1051a39Sopenharmony_ci md = m; 30e1051a39Sopenharmony_ci if (!SHA1_Init(&c)) 31e1051a39Sopenharmony_ci return NULL; 32e1051a39Sopenharmony_ci SHA1_Update(&c, d, n); 33e1051a39Sopenharmony_ci SHA1_Final(md, &c); 34e1051a39Sopenharmony_ci OPENSSL_cleanse(&c, sizeof(c)); 35e1051a39Sopenharmony_ci return md; 36e1051a39Sopenharmony_ci} 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciunsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md) 39e1051a39Sopenharmony_ci{ 40e1051a39Sopenharmony_ci static unsigned char m[SHA_DIGEST_LENGTH]; 41e1051a39Sopenharmony_ci 42e1051a39Sopenharmony_ci if (md == NULL) 43e1051a39Sopenharmony_ci md = m; 44e1051a39Sopenharmony_ci return EVP_Q_digest(NULL, "SHA1", NULL, d, n, md, NULL) ? md : NULL; 45e1051a39Sopenharmony_ci} 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_ciunsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md) 48e1051a39Sopenharmony_ci{ 49e1051a39Sopenharmony_ci static unsigned char m[SHA224_DIGEST_LENGTH]; 50e1051a39Sopenharmony_ci 51e1051a39Sopenharmony_ci if (md == NULL) 52e1051a39Sopenharmony_ci md = m; 53e1051a39Sopenharmony_ci return EVP_Q_digest(NULL, "SHA224", NULL, d, n, md, NULL) ? md : NULL; 54e1051a39Sopenharmony_ci} 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ciunsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md) 57e1051a39Sopenharmony_ci{ 58e1051a39Sopenharmony_ci static unsigned char m[SHA256_DIGEST_LENGTH]; 59e1051a39Sopenharmony_ci 60e1051a39Sopenharmony_ci if (md == NULL) 61e1051a39Sopenharmony_ci md = m; 62e1051a39Sopenharmony_ci return EVP_Q_digest(NULL, "SHA256", NULL, d, n, md, NULL) ? md : NULL; 63e1051a39Sopenharmony_ci} 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ciunsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md) 66e1051a39Sopenharmony_ci{ 67e1051a39Sopenharmony_ci static unsigned char m[SHA384_DIGEST_LENGTH]; 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_ci if (md == NULL) 70e1051a39Sopenharmony_ci md = m; 71e1051a39Sopenharmony_ci return EVP_Q_digest(NULL, "SHA384", NULL, d, n, md, NULL) ? md : NULL; 72e1051a39Sopenharmony_ci} 73e1051a39Sopenharmony_ci 74e1051a39Sopenharmony_ciunsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md) 75e1051a39Sopenharmony_ci{ 76e1051a39Sopenharmony_ci static unsigned char m[SHA512_DIGEST_LENGTH]; 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ci if (md == NULL) 79e1051a39Sopenharmony_ci md = m; 80e1051a39Sopenharmony_ci return EVP_Q_digest(NULL, "SHA512", NULL, d, n, md, NULL) ? md : NULL; 81e1051a39Sopenharmony_ci} 82