1b51722b4e2c31355971100c21628a9e881756c3abowgotsai/*
2b51722b4e2c31355971100c21628a9e881756c3abowgotsai * Copyright (C) 2016 The Android Open Source Project
3b51722b4e2c31355971100c21628a9e881756c3abowgotsai *
4b51722b4e2c31355971100c21628a9e881756c3abowgotsai * Licensed under the Apache License, Version 2.0 (the "License");
5b51722b4e2c31355971100c21628a9e881756c3abowgotsai * you may not use this file except in compliance with the License.
6b51722b4e2c31355971100c21628a9e881756c3abowgotsai * You may obtain a copy of the License at
7b51722b4e2c31355971100c21628a9e881756c3abowgotsai *
8b51722b4e2c31355971100c21628a9e881756c3abowgotsai *      http://www.apache.org/licenses/LICENSE-2.0
9b51722b4e2c31355971100c21628a9e881756c3abowgotsai *
10b51722b4e2c31355971100c21628a9e881756c3abowgotsai * Unless required by applicable law or agreed to in writing, software
11b51722b4e2c31355971100c21628a9e881756c3abowgotsai * distributed under the License is distributed on an "AS IS" BASIS,
12b51722b4e2c31355971100c21628a9e881756c3abowgotsai * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b51722b4e2c31355971100c21628a9e881756c3abowgotsai * See the License for the specific language governing permissions and
14b51722b4e2c31355971100c21628a9e881756c3abowgotsai * limitations under the License.
15b51722b4e2c31355971100c21628a9e881756c3abowgotsai */
16b51722b4e2c31355971100c21628a9e881756c3abowgotsai
17b51722b4e2c31355971100c21628a9e881756c3abowgotsai#ifndef __CORE_FS_MGR_PRIV_SHA_H
18b51722b4e2c31355971100c21628a9e881756c3abowgotsai#define __CORE_FS_MGR_PRIV_SHA_H
19b51722b4e2c31355971100c21628a9e881756c3abowgotsai
20b51722b4e2c31355971100c21628a9e881756c3abowgotsai#include <openssl/sha.h>
21b51722b4e2c31355971100c21628a9e881756c3abowgotsai
224caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsaiclass SHA256Hasher {
2395c966a8599a069c40707c933c31155d625bd355Bowgo Tsai  private:
24b51722b4e2c31355971100c21628a9e881756c3abowgotsai    SHA256_CTX sha256_ctx;
25b51722b4e2c31355971100c21628a9e881756c3abowgotsai    uint8_t hash[SHA256_DIGEST_LENGTH];
26b51722b4e2c31355971100c21628a9e881756c3abowgotsai
2795c966a8599a069c40707c933c31155d625bd355Bowgo Tsai  public:
28b51722b4e2c31355971100c21628a9e881756c3abowgotsai    enum { DIGEST_SIZE = SHA256_DIGEST_LENGTH };
29b51722b4e2c31355971100c21628a9e881756c3abowgotsai
304caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsai    SHA256Hasher() { SHA256_Init(&sha256_ctx); }
31b51722b4e2c31355971100c21628a9e881756c3abowgotsai
3295c966a8599a069c40707c933c31155d625bd355Bowgo Tsai    void update(const uint8_t* data, size_t data_size) {
3395c966a8599a069c40707c933c31155d625bd355Bowgo Tsai        SHA256_Update(&sha256_ctx, data, data_size);
3495c966a8599a069c40707c933c31155d625bd355Bowgo Tsai    }
35b51722b4e2c31355971100c21628a9e881756c3abowgotsai
364caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsai    const uint8_t* finalize() {
37b51722b4e2c31355971100c21628a9e881756c3abowgotsai        SHA256_Final(hash, &sha256_ctx);
38b51722b4e2c31355971100c21628a9e881756c3abowgotsai        return hash;
39b51722b4e2c31355971100c21628a9e881756c3abowgotsai    }
40b51722b4e2c31355971100c21628a9e881756c3abowgotsai};
41b51722b4e2c31355971100c21628a9e881756c3abowgotsai
424caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsaiclass SHA512Hasher {
4395c966a8599a069c40707c933c31155d625bd355Bowgo Tsai  private:
44b51722b4e2c31355971100c21628a9e881756c3abowgotsai    SHA512_CTX sha512_ctx;
45b51722b4e2c31355971100c21628a9e881756c3abowgotsai    uint8_t hash[SHA512_DIGEST_LENGTH];
46b51722b4e2c31355971100c21628a9e881756c3abowgotsai
4795c966a8599a069c40707c933c31155d625bd355Bowgo Tsai  public:
48b51722b4e2c31355971100c21628a9e881756c3abowgotsai    enum { DIGEST_SIZE = SHA512_DIGEST_LENGTH };
49b51722b4e2c31355971100c21628a9e881756c3abowgotsai
504caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsai    SHA512Hasher() { SHA512_Init(&sha512_ctx); }
51b51722b4e2c31355971100c21628a9e881756c3abowgotsai
524caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsai    void update(const uint8_t* data, size_t data_size) {
53b51722b4e2c31355971100c21628a9e881756c3abowgotsai        SHA512_Update(&sha512_ctx, data, data_size);
54b51722b4e2c31355971100c21628a9e881756c3abowgotsai    }
55b51722b4e2c31355971100c21628a9e881756c3abowgotsai
564caf4c03c12cff5d52bd9e8d2810be0b47e40f4bBowgo Tsai    const uint8_t* finalize() {
57b51722b4e2c31355971100c21628a9e881756c3abowgotsai        SHA512_Final(hash, &sha512_ctx);
58b51722b4e2c31355971100c21628a9e881756c3abowgotsai        return hash;
59b51722b4e2c31355971100c21628a9e881756c3abowgotsai    }
60b51722b4e2c31355971100c21628a9e881756c3abowgotsai};
61b51722b4e2c31355971100c21628a9e881756c3abowgotsai
62b51722b4e2c31355971100c21628a9e881756c3abowgotsai#endif /* __CORE_FS_MGR_PRIV_SHA_H */
63