nagendra modadugu | c0fe836 | 2016-12-08 14:43:06 -0800 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include "cryptoc/sha384.h" |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include "cryptoc/sha512.h" |
| 20 | |
| 21 | #ifndef SHA512_SUPPORT |
| 22 | #error "-DSHA512_SUPPORT must be specified to enable SHA-384/512" |
| 23 | #endif |
| 24 | |
| 25 | static const HASH_VTAB SHA384_VTAB = { |
| 26 | SHA384_init, |
| 27 | SHA512_update, |
| 28 | SHA512_final, |
| 29 | SHA384_hash, |
nagendra modadugu | 793cf59 | 2019-10-09 14:07:05 -0700 | [diff] [blame] | 30 | SHA384_DIGEST_SIZE, |
| 31 | SHA384_BLOCK_SIZE |
nagendra modadugu | c0fe836 | 2016-12-08 14:43:06 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | void SHA384_init(LITE_SHA384_CTX* ctx) { |
| 35 | ctx->f = &SHA384_VTAB; |
| 36 | ctx->state[0] = 0xcbbb9d5dc1059ed8ll; |
| 37 | ctx->state[1] = 0x629a292a367cd507ll; |
| 38 | ctx->state[2] = 0x9159015a3070dd17ll; |
| 39 | ctx->state[3] = 0x152fecd8f70e5939ll; |
| 40 | ctx->state[4] = 0x67332667ffc00b31ll; |
| 41 | ctx->state[5] = 0x8eb44a8768581511ll; |
| 42 | ctx->state[6] = 0xdb0c2e0d64f98fa7ll; |
| 43 | ctx->state[7] = 0x47b5481dbefa4fa4ll; |
| 44 | ctx->count = 0; |
| 45 | } |
| 46 | |
| 47 | void SHA384_update(LITE_SHA384_CTX* ctx, const void* data, size_t len) { |
| 48 | SHA512_update(ctx, data, len); |
| 49 | } |
| 50 | |
| 51 | const uint8_t* SHA384_final(LITE_SHA384_CTX* ctx) { |
| 52 | return SHA512_final(ctx); |
| 53 | } |
| 54 | |
| 55 | const uint8_t* SHA384_hash(const void* data, size_t len, uint8_t* digest) { |
| 56 | LITE_SHA384_CTX ctx; |
| 57 | SHA384_init(&ctx); |
| 58 | SHA384_update(&ctx, data, len); |
| 59 | memcpy(digest, SHA384_final(&ctx), SHA384_DIGEST_SIZE); |
| 60 | return digest; |
| 61 | } |