blob: 5ac2db4b628fc0c12831af92da883f3816f48112 [file] [log] [blame]
Gaurav Shahe178fd92010-02-05 11:44:58 -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 "padding.h"
9#include "rsa_utility.h"
Gaurav Shah08df9b82010-02-23 16:16:23 -080010#include "sha_utility.h"
Gaurav Shahe178fd92010-02-05 11:44:58 -080011#include "utility.h"
12
13int RSAProcessedKeySize(int algorithm) {
Gaurav Shahcae5fa62010-02-28 20:02:29 -080014 int key_len = siglen_map[algorithm]; /* Key length in
15 * bytes. */
Gaurav Shahe178fd92010-02-05 11:44:58 -080016 /* Total size needed by a RSAPublicKey structure is =
17 * 2 * key_len bytes for the n and rr arrays
18 * + sizeof len + sizeof n0inv.
19 */
20 return (2 * key_len + sizeof(int) + sizeof(uint32_t));
21}
22
Gaurav Shah259de402010-03-12 17:42:03 -080023RSAPublicKey* RSAPublicKeyNew(void) {
24 RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
25 key->n = NULL;
26 key->rr = NULL;
27 return key;
28}
29
Gaurav Shah08df9b82010-02-23 16:16:23 -080030void RSAPublicKeyFree(RSAPublicKey* key) {
31 if (key) {
32 Free(key->n);
33 Free(key->rr);
34 Free(key);
35 }
36}
37
38RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) {
Gaurav Shah259de402010-03-12 17:42:03 -080039 RSAPublicKey* key = RSAPublicKeyNew();
Gaurav Shahe178fd92010-02-05 11:44:58 -080040 MemcpyState st;
41 int key_len;
42
Gaurav Shah08df9b82010-02-23 16:16:23 -080043 st.remaining_buf = (uint8_t*) buf;
Gaurav Shahe178fd92010-02-05 11:44:58 -080044 st.remaining_len = len;
Gaurav Shahe450be42010-03-29 21:27:08 -070045 st.overrun = 0;
46
Gaurav Shahe178fd92010-02-05 11:44:58 -080047 StatefulMemcpy(&st, &key->len, sizeof(key->len));
48 key_len = key->len * sizeof(uint32_t); /* key length in bytes. */
Gaurav Shah259de402010-03-12 17:42:03 -080049
50 /* Sanity Check the key length. */
51 if (RSA1024NUMBYTES != key_len &&
52 RSA2048NUMBYTES != key_len &&
53 RSA4096NUMBYTES != key_len &&
54 RSA8192NUMBYTES != key_len) {
55 RSAPublicKeyFree(key);
56 return NULL;
57 }
58
Gaurav Shahe178fd92010-02-05 11:44:58 -080059 key->n = (uint32_t*) Malloc(key_len);
60 key->rr = (uint32_t*) Malloc(key_len);
61
62 StatefulMemcpy(&st, &key->n0inv, sizeof(key->n0inv));
63 StatefulMemcpy(&st, key->n, key_len);
64 StatefulMemcpy(&st, key->rr, key_len);
Gaurav Shahe450be42010-03-29 21:27:08 -070065 if (st.overrun || st.remaining_len != 0) { /* Underrun or overrun. */
Gaurav Shah259de402010-03-12 17:42:03 -080066 RSAPublicKeyFree(key);
Gaurav Shahe178fd92010-02-05 11:44:58 -080067 return NULL;
68 }
69
70 return key;
71}
Gaurav Shah08df9b82010-02-23 16:16:23 -080072
73int RSAVerifyBinary_f(const uint8_t* key_blob,
74 const RSAPublicKey* key,
75 const uint8_t* buf,
Gaurav Shahe450be42010-03-29 21:27:08 -070076 uint64_t len,
Gaurav Shah08df9b82010-02-23 16:16:23 -080077 const uint8_t* sig,
78 int algorithm) {
79 RSAPublicKey* verification_key = NULL;
80 uint8_t* digest = NULL;
81 int key_size;
82 int sig_size;
83 int success;
84
85 if (algorithm >= kNumAlgorithms)
86 return 0; /* Invalid algorithm. */
87 key_size = RSAProcessedKeySize(algorithm);
Gaurav Shahcae5fa62010-02-28 20:02:29 -080088 sig_size = siglen_map[algorithm];
Gaurav Shah08df9b82010-02-23 16:16:23 -080089
90 if (key_blob && !key)
91 verification_key = RSAPublicKeyFromBuf(key_blob, key_size);
92 else if (!key_blob && key)
93 verification_key = (RSAPublicKey*) key; /* Supress const warning. */
94 else
95 return 0; /* Both can't be NULL or non-NULL. */
96
97 digest = DigestBuf(buf, len, algorithm);
Gaurav Shahf5564fa2010-03-02 15:40:01 -080098 success = RSAVerify(verification_key, sig, sig_size, algorithm, digest);
Gaurav Shah08df9b82010-02-23 16:16:23 -080099
100 Free(digest);
101 if (!key)
102 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */
103 return success;
104}
Gaurav Shah463be3f2010-03-29 16:13:45 -0700105
106/* Version of RSAVerifyBinary_f() where instead of the raw binary blob
107 * of data, its digest is passed as the argument. */
108int RSAVerifyBinaryWithDigest_f(const uint8_t* key_blob,
109 const RSAPublicKey* key,
110 const uint8_t* digest,
111 const uint8_t* sig,
112 int algorithm) {
113 RSAPublicKey* verification_key = NULL;
114 int key_size;
115 int sig_size;
116 int success;
117
118 if (algorithm >= kNumAlgorithms)
119 return 0; /* Invalid algorithm. */
120 key_size = RSAProcessedKeySize(algorithm);
121 sig_size = siglen_map[algorithm];
122
123 if (key_blob && !key)
124 verification_key = RSAPublicKeyFromBuf(key_blob, key_size);
125 else if (!key_blob && key)
126 verification_key = (RSAPublicKey*) key; /* Supress const warning. */
127 else
128 return 0; /* Both can't be NULL or non-NULL. */
129
130 success = RSAVerify(verification_key, sig, sig_size, algorithm, digest);
131
132 if (!key)
133 RSAPublicKeyFree(verification_key); /* Only free if we allocated it. */
134 return success;
135}