Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Utility functions for message digest functions. |
| 6 | */ |
| 7 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 8 | #include "cryptolib.h" |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 9 | #include "utility.h" |
| 10 | |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 11 | void DigestInit(DigestContext* ctx, int sig_algorithm) { |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 12 | ctx->algorithm = hash_type_map[sig_algorithm]; |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 13 | switch(ctx->algorithm) { |
| 14 | case SHA1_DIGEST_ALGORITHM: |
| 15 | ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX)); |
| 16 | SHA1_init(ctx->sha1_ctx); |
| 17 | break; |
| 18 | case SHA256_DIGEST_ALGORITHM: |
| 19 | ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX)); |
| 20 | SHA256_init(ctx->sha256_ctx); |
| 21 | break; |
| 22 | case SHA512_DIGEST_ALGORITHM: |
| 23 | ctx->sha512_ctx = (SHA512_CTX*) Malloc(sizeof(SHA512_CTX)); |
| 24 | SHA512_init(ctx->sha512_ctx); |
| 25 | break; |
| 26 | }; |
| 27 | } |
| 28 | |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 29 | void DigestUpdate(DigestContext* ctx, const uint8_t* data, uint64_t len) { |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 30 | switch(ctx->algorithm) { |
| 31 | case SHA1_DIGEST_ALGORITHM: |
| 32 | SHA1_update(ctx->sha1_ctx, data, len); |
| 33 | break; |
| 34 | case SHA256_DIGEST_ALGORITHM: |
| 35 | SHA256_update(ctx->sha256_ctx, data, len); |
| 36 | break; |
| 37 | case SHA512_DIGEST_ALGORITHM: |
| 38 | SHA512_update(ctx->sha512_ctx, data, len); |
| 39 | break; |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | uint8_t* DigestFinal(DigestContext* ctx) { |
| 44 | uint8_t* digest = NULL; |
| 45 | switch(ctx->algorithm) { |
| 46 | case SHA1_DIGEST_ALGORITHM: |
| 47 | digest = (uint8_t*) Malloc(SHA1_DIGEST_SIZE); |
| 48 | Memcpy(digest, SHA1_final(ctx->sha1_ctx), SHA1_DIGEST_SIZE); |
| 49 | Free(ctx->sha1_ctx); |
| 50 | break; |
| 51 | case SHA256_DIGEST_ALGORITHM: |
| 52 | digest = (uint8_t*) Malloc(SHA256_DIGEST_SIZE); |
| 53 | Memcpy(digest, SHA256_final(ctx->sha256_ctx), SHA256_DIGEST_SIZE); |
| 54 | Free(ctx->sha256_ctx); |
| 55 | break; |
| 56 | case SHA512_DIGEST_ALGORITHM: |
| 57 | digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); |
| 58 | Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE); |
| 59 | Free(ctx->sha512_ctx); |
| 60 | break; |
| 61 | }; |
| 62 | return digest; |
| 63 | } |
| 64 | |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 65 | uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) { |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 66 | uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */ |
| 67 | /* Define an array mapping [sig_algorithm] to function pointers to the |
| 68 | * SHA{1|256|512} functions. |
| 69 | */ |
Gaurav Shah | 456678b | 2010-03-10 18:38:45 -0800 | [diff] [blame] | 70 | typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*); |
Gaurav Shah | 290e078 | 2010-02-05 14:37:30 -0800 | [diff] [blame] | 71 | Hash_ptr hash[] = { |
| 72 | SHA1, /* RSA 1024 */ |
| 73 | SHA256, |
| 74 | SHA512, |
| 75 | SHA1, /* RSA 2048 */ |
| 76 | SHA256, |
| 77 | SHA512, |
| 78 | SHA1, /* RSA 4096 */ |
| 79 | SHA256, |
| 80 | SHA512, |
| 81 | SHA1, /* RSA 8192 */ |
| 82 | SHA256, |
| 83 | SHA512, |
| 84 | }; |
| 85 | /* Call the appropriate hash function. */ |
| 86 | return hash[sig_algorithm](buf, len, digest); |
| 87 | } |