| 6 | // |
| 7 | // https://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 |
| 15 | #include <openssl/sha.h> |
| 16 |
| 17 | #include <openssl/mem.h> |
| 18 |
| 19 | #include "../fipsmodule/bcm_interface.h" |
| 20 |
| 21 |
| 22 | int SHA224_Init(SHA256_CTX *sha) { |
| 23 | BCM_sha224_init(sha); |
| 24 | return 1; |
| 25 | } |
| 26 |
| 27 | int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) { |
| 28 | BCM_sha224_update(sha, data, len); |
| 29 | return 1; |
| 30 | } |
| 31 |
| 32 | int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) { |
| 33 | BCM_sha224_final(out, sha); |
| 34 | return 1; |
| 35 | } |
| 36 |
| 37 | uint8_t *SHA224(const uint8_t *data, size_t len, |
| 38 | uint8_t out[SHA224_DIGEST_LENGTH]) { |
| 39 | SHA256_CTX ctx; |
| 40 | BCM_sha224_init(&ctx); |
| 41 | BCM_sha224_update(&ctx, data, len); |
| 42 | BCM_sha224_final(out, &ctx); |
| 43 | OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 44 | return out; |
| 45 | } |
| 46 |
| 47 | int SHA256_Init(SHA256_CTX *sha) { |
| 48 | BCM_sha256_init(sha); |
| 49 | return 1; |
| 50 | } |
| 51 |
| 52 | int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) { |
| 53 | BCM_sha256_update(sha, data, len); |
| 54 | return 1; |
| 55 | } |
| 56 |
| 57 | int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha) { |
| 58 | // TODO(bbe): This overflow check one of the few places a low-level hash |
| 59 | // 'final' function can fail. SHA-512 does not have a corresponding check. |
| 60 | // The BCM function is infallible and will abort if this is done incorrectly. |
| 61 | // we should verify nothing crashes with this removed and eliminate the 0 |
| 62 | // return. |
| 63 | if (sha->md_len > SHA256_DIGEST_LENGTH) { |
| 64 | return 0; |
| 65 | } |
| 66 | BCM_sha256_final(out, sha); |
| 67 | return 1; |
| 68 | } |
| 69 |
| 70 | uint8_t *SHA256(const uint8_t *data, size_t len, |
| 71 | uint8_t out[SHA256_DIGEST_LENGTH]) { |
| 72 | SHA256_CTX ctx; |
| 73 | BCM_sha256_init(&ctx); |
| 74 | BCM_sha256_update(&ctx, data, len); |
| 75 | BCM_sha256_final(out, &ctx); |
| 76 | OPENSSL_cleanse(&ctx, sizeof(ctx)); |
| 77 | return out; |
| 78 | } |
| 79 |
| 80 | void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) { |
| 81 | BCM_sha256_transform(sha, block); |
| 82 | } |
| 83 |
| 84 | void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data, |
| 85 | size_t num_blocks) { |
| 86 | BCM_sha256_transform_blocks(state, data, num_blocks); |
| 87 | } |
| 88 |