blob: 5e3f7e07c4b663b6148f9ec0be29aa68747db4c7 [file] [log] [blame]
Gaurav Shah290e0782010-02-05 14:37:30 -08001/* 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
8#include "sha_utility.h"
9
10#include <fcntl.h>
11#include <unistd.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include "sha.h"
19#include "utility.h"
20
21int digest_type_map[] = {
22 SHA1_DIGEST_ALGORITHM, /* RSA 1024 */
23 SHA256_DIGEST_ALGORITHM,
24 SHA512_DIGEST_ALGORITHM,
25 SHA1_DIGEST_ALGORITHM, /* RSA 2048 */
26 SHA256_DIGEST_ALGORITHM,
27 SHA512_DIGEST_ALGORITHM,
28 SHA1_DIGEST_ALGORITHM, /* RSA 4096 */
29 SHA256_DIGEST_ALGORITHM,
30 SHA512_DIGEST_ALGORITHM,
31 SHA1_DIGEST_ALGORITHM, /* RSA 8192 */
32 SHA256_DIGEST_ALGORITHM,
33 SHA512_DIGEST_ALGORITHM,
34};
35
36void DigestInit(DigestContext* ctx, int sig_algorithm) {
37 ctx->algorithm = digest_type_map[sig_algorithm];
38 switch(ctx->algorithm) {
39 case SHA1_DIGEST_ALGORITHM:
40 ctx->sha1_ctx = (SHA1_CTX*) Malloc(sizeof(SHA1_CTX));
41 SHA1_init(ctx->sha1_ctx);
42 break;
43 case SHA256_DIGEST_ALGORITHM:
44 ctx->sha256_ctx = (SHA256_CTX*) Malloc(sizeof(SHA256_CTX));
45 SHA256_init(ctx->sha256_ctx);
46 break;
47 case SHA512_DIGEST_ALGORITHM:
48 ctx->sha512_ctx = (SHA512_CTX*) Malloc(sizeof(SHA512_CTX));
49 SHA512_init(ctx->sha512_ctx);
50 break;
51 };
52}
53
54void DigestUpdate(DigestContext* ctx, const uint8_t* data, int len) {
55 switch(ctx->algorithm) {
56 case SHA1_DIGEST_ALGORITHM:
57 SHA1_update(ctx->sha1_ctx, data, len);
58 break;
59 case SHA256_DIGEST_ALGORITHM:
60 SHA256_update(ctx->sha256_ctx, data, len);
61 break;
62 case SHA512_DIGEST_ALGORITHM:
63 SHA512_update(ctx->sha512_ctx, data, len);
64 break;
65 };
66}
67
68uint8_t* DigestFinal(DigestContext* ctx) {
69 uint8_t* digest = NULL;
70 switch(ctx->algorithm) {
71 case SHA1_DIGEST_ALGORITHM:
72 digest = (uint8_t*) Malloc(SHA1_DIGEST_SIZE);
73 Memcpy(digest, SHA1_final(ctx->sha1_ctx), SHA1_DIGEST_SIZE);
74 Free(ctx->sha1_ctx);
75 break;
76 case SHA256_DIGEST_ALGORITHM:
77 digest = (uint8_t*) Malloc(SHA256_DIGEST_SIZE);
78 Memcpy(digest, SHA256_final(ctx->sha256_ctx), SHA256_DIGEST_SIZE);
79 Free(ctx->sha256_ctx);
80 break;
81 case SHA512_DIGEST_ALGORITHM:
82 digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE);
83 Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE);
84 Free(ctx->sha512_ctx);
85 break;
86 };
87 return digest;
88}
89
90uint8_t* DigestFile(char* input_file, int sig_algorithm) {
91 int input_fd, len;
92 uint8_t data[SHA1_BLOCK_SIZE];
93 uint8_t* digest = NULL;
94 DigestContext ctx;
95
96 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
97 fprintf(stderr, "Couldn't open input file.\n");
98 return NULL;
99 }
100 DigestInit(&ctx, sig_algorithm);
101 while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) ==
102 SHA1_BLOCK_SIZE)
103 DigestUpdate(&ctx, data, len);
104 if (len != -1)
105 DigestUpdate(&ctx, data, len);
106 digest = DigestFinal(&ctx);
107 close(input_fd);
108 return digest;
109}
110
111uint8_t* DigestBuf(uint8_t* buf, int len, int sig_algorithm) {
112 uint8_t* digest = (uint8_t*) Malloc(SHA512_DIGEST_SIZE); /* Use the max. */
113 /* Define an array mapping [sig_algorithm] to function pointers to the
114 * SHA{1|256|512} functions.
115 */
116 typedef uint8_t* (*Hash_ptr) (const uint8_t*, int, uint8_t*);
117 Hash_ptr hash[] = {
118 SHA1, /* RSA 1024 */
119 SHA256,
120 SHA512,
121 SHA1, /* RSA 2048 */
122 SHA256,
123 SHA512,
124 SHA1, /* RSA 4096 */
125 SHA256,
126 SHA512,
127 SHA1, /* RSA 8192 */
128 SHA256,
129 SHA512,
130 };
131 /* Call the appropriate hash function. */
132 return hash[sig_algorithm](buf, len, digest);
133}