xref: /freebsd/crypto/openssl/crypto/sha/sha1_one.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  * Copyright 1995-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  * SHA-1 low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <openssl/crypto.h>
19 #include <openssl/sha.h>
20 #include <openssl/evp.h>
21 #include "crypto/sha.h"
22 
ossl_sha1(const unsigned char * d,size_t n,unsigned char * md)23 unsigned char *ossl_sha1(const unsigned char *d, size_t n, unsigned char *md)
24 {
25     SHA_CTX c;
26     static unsigned char m[SHA_DIGEST_LENGTH];
27 
28     if (md == NULL)
29         md = m;
30     if (!SHA1_Init(&c))
31         return NULL;
32     SHA1_Update(&c, d, n);
33     SHA1_Final(md, &c);
34     OPENSSL_cleanse(&c, sizeof(c));
35     return md;
36 }
37 
SHA1(const unsigned char * d,size_t n,unsigned char * md)38 unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md)
39 {
40     static unsigned char m[SHA_DIGEST_LENGTH];
41 
42     if (md == NULL)
43         md = m;
44     return EVP_Q_digest(NULL, "SHA1", NULL, d, n, md, NULL) ? md : NULL;
45 }
46 
SHA224(const unsigned char * d,size_t n,unsigned char * md)47 unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md)
48 {
49     static unsigned char m[SHA224_DIGEST_LENGTH];
50 
51     if (md == NULL)
52         md = m;
53     return EVP_Q_digest(NULL, "SHA224", NULL, d, n, md, NULL) ? md : NULL;
54 }
55 
SHA256(const unsigned char * d,size_t n,unsigned char * md)56 unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
57 {
58     static unsigned char m[SHA256_DIGEST_LENGTH];
59 
60     if (md == NULL)
61         md = m;
62     return EVP_Q_digest(NULL, "SHA256", NULL, d, n, md, NULL) ? md : NULL;
63 }
64 
SHA384(const unsigned char * d,size_t n,unsigned char * md)65 unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
66 {
67     static unsigned char m[SHA384_DIGEST_LENGTH];
68 
69     if (md == NULL)
70         md = m;
71     return EVP_Q_digest(NULL, "SHA384", NULL, d, n, md, NULL) ? md : NULL;
72 }
73 
SHA512(const unsigned char * d,size_t n,unsigned char * md)74 unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
75 {
76     static unsigned char m[SHA512_DIGEST_LENGTH];
77 
78     if (md == NULL)
79         md = m;
80     return EVP_Q_digest(NULL, "SHA512", NULL, d, n, md, NULL) ? md : NULL;
81 }
82