xref: /freebsd/crypto/openssl/crypto/sha/sha1_one.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * SHA-1 low level APIs are deprecated for public use, but still ok for
12*b077aed3SPierre Pronchery  * internal use.
13*b077aed3SPierre Pronchery  */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery 
1674664626SKris Kennaway #include <stdio.h>
1774664626SKris Kennaway #include <string.h>
185c87c606SMark Murray #include <openssl/crypto.h>
1909286989SJung-uk Kim #include <openssl/sha.h>
20*b077aed3SPierre Pronchery #include <openssl/evp.h>
21*b077aed3SPierre Pronchery #include "crypto/sha.h"
2274664626SKris Kennaway 
ossl_sha1(const unsigned char * d,size_t n,unsigned char * md)23*b077aed3SPierre Pronchery unsigned char *ossl_sha1(const unsigned char *d, size_t n, unsigned char *md)
2474664626SKris Kennaway {
2574664626SKris Kennaway     SHA_CTX c;
2674664626SKris Kennaway     static unsigned char m[SHA_DIGEST_LENGTH];
2774664626SKris Kennaway 
286f9291ceSJung-uk Kim     if (md == NULL)
296f9291ceSJung-uk Kim         md = m;
303b4e3dcbSSimon L. B. Nielsen     if (!SHA1_Init(&c))
313b4e3dcbSSimon L. B. Nielsen         return NULL;
3274664626SKris Kennaway     SHA1_Update(&c, d, n);
3374664626SKris Kennaway     SHA1_Final(md, &c);
345c87c606SMark Murray     OPENSSL_cleanse(&c, sizeof(c));
35e71b7053SJung-uk Kim     return md;
3674664626SKris Kennaway }
37*b077aed3SPierre Pronchery 
SHA1(const unsigned char * d,size_t n,unsigned char * md)38*b077aed3SPierre Pronchery unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md)
39*b077aed3SPierre Pronchery {
40*b077aed3SPierre Pronchery     static unsigned char m[SHA_DIGEST_LENGTH];
41*b077aed3SPierre Pronchery 
42*b077aed3SPierre Pronchery     if (md == NULL)
43*b077aed3SPierre Pronchery         md = m;
44*b077aed3SPierre Pronchery     return EVP_Q_digest(NULL, "SHA1", NULL, d, n, md, NULL) ? md : NULL;
45*b077aed3SPierre Pronchery }
46*b077aed3SPierre Pronchery 
SHA224(const unsigned char * d,size_t n,unsigned char * md)47*b077aed3SPierre Pronchery unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md)
48*b077aed3SPierre Pronchery {
49*b077aed3SPierre Pronchery     static unsigned char m[SHA224_DIGEST_LENGTH];
50*b077aed3SPierre Pronchery 
51*b077aed3SPierre Pronchery     if (md == NULL)
52*b077aed3SPierre Pronchery         md = m;
53*b077aed3SPierre Pronchery     return EVP_Q_digest(NULL, "SHA224", NULL, d, n, md, NULL) ? md : NULL;
54*b077aed3SPierre Pronchery }
55*b077aed3SPierre Pronchery 
SHA256(const unsigned char * d,size_t n,unsigned char * md)56*b077aed3SPierre Pronchery unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
57*b077aed3SPierre Pronchery {
58*b077aed3SPierre Pronchery     static unsigned char m[SHA256_DIGEST_LENGTH];
59*b077aed3SPierre Pronchery 
60*b077aed3SPierre Pronchery     if (md == NULL)
61*b077aed3SPierre Pronchery         md = m;
62*b077aed3SPierre Pronchery     return EVP_Q_digest(NULL, "SHA256", NULL, d, n, md, NULL) ? md : NULL;
63*b077aed3SPierre Pronchery }
64*b077aed3SPierre Pronchery 
SHA384(const unsigned char * d,size_t n,unsigned char * md)65*b077aed3SPierre Pronchery unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
66*b077aed3SPierre Pronchery {
67*b077aed3SPierre Pronchery     static unsigned char m[SHA384_DIGEST_LENGTH];
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     if (md == NULL)
70*b077aed3SPierre Pronchery         md = m;
71*b077aed3SPierre Pronchery     return EVP_Q_digest(NULL, "SHA384", NULL, d, n, md, NULL) ? md : NULL;
72*b077aed3SPierre Pronchery }
73*b077aed3SPierre Pronchery 
SHA512(const unsigned char * d,size_t n,unsigned char * md)74*b077aed3SPierre Pronchery unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
75*b077aed3SPierre Pronchery {
76*b077aed3SPierre Pronchery     static unsigned char m[SHA512_DIGEST_LENGTH];
77*b077aed3SPierre Pronchery 
78*b077aed3SPierre Pronchery     if (md == NULL)
79*b077aed3SPierre Pronchery         md = m;
80*b077aed3SPierre Pronchery     return EVP_Q_digest(NULL, "SHA512", NULL, d, n, md, NULL) ? md : NULL;
81*b077aed3SPierre Pronchery }
82