OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #include "signature_digest.h" | 6 #include "signature_digest.h" |
7 #define OPENSSL_NO_SHA | 7 #define OPENSSL_NO_SHA |
8 #include <openssl/engine.h> | 8 #include <openssl/engine.h> |
9 #include <openssl/pem.h> | 9 #include <openssl/pem.h> |
10 #include <openssl/rsa.h> | 10 #include <openssl/rsa.h> |
(...skipping 10 matching lines...) Expand all Loading... |
21 uint8_t* PrependDigestInfo(int algorithm, uint8_t* digest) { | 21 uint8_t* PrependDigestInfo(int algorithm, uint8_t* digest) { |
22 const int digest_size = hash_size_map[algorithm]; | 22 const int digest_size = hash_size_map[algorithm]; |
23 const int digestinfo_size = digestinfo_size_map[algorithm]; | 23 const int digestinfo_size = digestinfo_size_map[algorithm]; |
24 const uint8_t* digestinfo = hash_digestinfo_map[algorithm]; | 24 const uint8_t* digestinfo = hash_digestinfo_map[algorithm]; |
25 uint8_t* p = Malloc(digestinfo_size + digest_size); | 25 uint8_t* p = Malloc(digestinfo_size + digest_size); |
26 Memcpy(p, digestinfo, digestinfo_size); | 26 Memcpy(p, digestinfo, digestinfo_size); |
27 Memcpy(p + digestinfo_size, digest, digest_size); | 27 Memcpy(p + digestinfo_size, digest, digest_size); |
28 return p; | 28 return p; |
29 } | 29 } |
30 | 30 |
31 uint8_t* SignatureDigest(const uint8_t* buf, int len, int algorithm) { | 31 uint8_t* SignatureDigest(const uint8_t* buf, uint64_t len, int algorithm) { |
32 uint8_t* info_digest = NULL; | 32 uint8_t* info_digest = NULL; |
33 uint8_t* digest = NULL; | 33 uint8_t* digest = NULL; |
34 | 34 |
35 if (algorithm >= kNumAlgorithms) { | 35 if (algorithm >= kNumAlgorithms) { |
36 fprintf(stderr, "SignatureDigest() called with invalid algorithm!\n"); | 36 fprintf(stderr, "SignatureDigest() called with invalid algorithm!\n"); |
37 } else if ((digest = DigestBuf(buf, len, algorithm))) { | 37 } else if ((digest = DigestBuf(buf, len, algorithm))) { |
38 info_digest = PrependDigestInfo(algorithm, digest); | 38 info_digest = PrependDigestInfo(algorithm, digest); |
39 } | 39 } |
40 Free(digest); | 40 Free(digest); |
41 return info_digest; | 41 return info_digest; |
42 } | 42 } |
43 | 43 |
44 uint8_t* SignatureBuf(const uint8_t* buf, int len, const char* key_file, | 44 uint8_t* SignatureBuf(const uint8_t* buf, uint64_t len, const char* key_file, |
45 int algorithm) { | 45 int algorithm) { |
46 FILE* key_fp = NULL; | 46 FILE* key_fp = NULL; |
47 RSA* key = NULL; | 47 RSA* key = NULL; |
48 uint8_t* signature = NULL; | 48 uint8_t* signature = NULL; |
49 uint8_t* signature_digest = SignatureDigest(buf, len, algorithm); | 49 uint8_t* signature_digest = SignatureDigest(buf, len, algorithm); |
50 int signature_digest_len = (hash_size_map[algorithm] + | 50 int signature_digest_len = (hash_size_map[algorithm] + |
51 digestinfo_size_map[algorithm]); | 51 digestinfo_size_map[algorithm]); |
52 key_fp = fopen(key_file, "r"); | 52 key_fp = fopen(key_file, "r"); |
53 if (!key_fp) { | 53 if (!key_fp) { |
54 fprintf(stderr, "SignatureBuf(): Couldn't open key file: %s\n", key_file); | 54 fprintf(stderr, "SignatureBuf(): Couldn't open key file: %s\n", key_file); |
(...skipping 10 matching lines...) Expand all Loading... |
65 signature, /* Output signature. */ | 65 signature, /* Output signature. */ |
66 key, /* Key to use. */ | 66 key, /* Key to use. */ |
67 RSA_PKCS1_PADDING)) /* Padding to use. */ | 67 RSA_PKCS1_PADDING)) /* Padding to use. */ |
68 fprintf(stderr, "SignatureBuf(): RSA_private_encrypt() failed.\n"); | 68 fprintf(stderr, "SignatureBuf(): RSA_private_encrypt() failed.\n"); |
69 } | 69 } |
70 if (key) | 70 if (key) |
71 RSA_free(key); | 71 RSA_free(key); |
72 Free(signature_digest); | 72 Free(signature_digest); |
73 return signature; | 73 return signature; |
74 } | 74 } |
OLD | NEW |