Skip to content

Commit 1653edc

Browse files
mnisslerSteve Kondik
authored and
Steve Kondik
committed
Switch to BoringSSL for crypto.
Adjust code and dependencies to use BoringSSL + libcrypto_utils instead of libmincrypt. Change-Id: Id1b4ee538923e9dac68a7db2521b70007119c268
1 parent 5d5c65b commit 1653edc

File tree

7 files changed

+19
-68
lines changed

7 files changed

+19
-68
lines changed

Diff for: libfec/Android.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ common_src_files := \
1717
fec_process.cpp
1818

1919
common_static_libraries := \
20-
libmincrypt \
20+
libcrypto_utils_static \
2121
libcrypto_static \
2222
libcutils \
2323
libbase

Diff for: libfec/fec_private.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
#include <new>
2424
#include <pthread.h>
2525
#include <stdio.h>
26-
#include <string>
2726
#include <string.h>
27+
#include <string>
2828
#include <sys/syscall.h>
2929
#include <unistd.h>
3030
#include <vector>
3131

32-
#include <utils/Compat.h>
33-
#include <mincrypt/rsa.h>
34-
#include <openssl/sha.h>
35-
#include <fec/io.h>
32+
#include <crypto_utils/android_pubkey.h>
3633
#include <fec/ecc.h>
34+
#include <fec/io.h>
35+
#include <openssl/sha.h>
36+
#include <utils/Compat.h>
3737

3838
/* processing parameters */
3939
#define WORK_MIN_THREADS 1
@@ -59,7 +59,7 @@
5959
struct verity_header {
6060
uint32_t magic;
6161
uint32_t version;
62-
uint8_t signature[RSANUMBYTES];
62+
uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE];
6363
uint32_t length;
6464
};
6565

Diff for: libfec/include/fec/io.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include <stdio.h>
2525
#include <sys/types.h>
2626
#include <unistd.h>
27-
#include <mincrypt/rsa.h>
27+
28+
#include <crypto_utils/android_pubkey.h>
2829

2930
#ifdef __cplusplus
3031
extern "C" {
@@ -70,8 +71,8 @@ struct fec_ecc_metadata {
7071
struct fec_verity_metadata {
7172
bool disabled;
7273
uint64_t data_size;
73-
uint8_t signature[RSANUMBYTES];
74-
uint8_t ecc_signature[RSANUMBYTES];
74+
uint8_t signature[ANDROID_PUBKEY_MODULUS_SIZE];
75+
uint8_t ecc_signature[ANDROID_PUBKEY_MODULUS_SIZE];
7576
const char *table;
7677
uint32_t table_length;
7778
};

Diff for: libfec/test/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LOCAL_MODULE_TAGS := optional
1111
LOCAL_STATIC_LIBRARIES := \
1212
libfec_host \
1313
libfec_rs_host \
14+
libcrypto_utils_static \
1415
libcrypto_static \
1516
libext4_utils_host \
1617
libsquashfs_utils_host \

Diff for: verity/Android.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LOCAL_MODULE := generate_verity_key
1818
LOCAL_SRC_FILES := generate_verity_key.c
1919
LOCAL_MODULE_CLASS := EXECUTABLES
2020
LOCAL_MODULE_TAGS := optional
21-
LOCAL_SHARED_LIBRARIES := libcrypto-host
21+
LOCAL_SHARED_LIBRARIES := libcrypto_utils libcrypto-host
2222
include $(BUILD_HOST_EXECUTABLE)
2323

2424
include $(CLEAR_VARS)

Diff for: verity/fec/Android.mk

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LOCAL_MODULE_TAGS := optional
1111
LOCAL_STATIC_LIBRARIES := \
1212
libsparse_host \
1313
libz \
14+
libcrypto_utils_static \
1415
libcrypto_static \
1516
libfec_host \
1617
libfec_rs_host \
@@ -29,6 +30,7 @@ LOCAL_FORCE_STATIC_EXECUTABLE := true
2930
LOCAL_SRC_FILES := main.cpp image.cpp
3031
LOCAL_MODULE_TAGS := optional
3132
LOCAL_STATIC_LIBRARIES := \
33+
libcrypto_utils_static \
3234
libcrypto_static \
3335
libfec \
3436
libfec_rs \

Diff for: verity/generate_verity_key.c

+4-57
Original file line numberDiff line numberDiff line change
@@ -23,85 +23,32 @@
2323
#include <sys/types.h>
2424
#include <unistd.h>
2525

26-
/* HACK: we need the RSAPublicKey struct
27-
* but RSA_verify conflits with openssl */
28-
#define RSA_verify RSA_verify_mincrypt
29-
#include "mincrypt/rsa.h"
30-
#undef RSA_verify
26+
#include <crypto_utils/android_pubkey.h>
3127

3228
#include <openssl/evp.h>
3329
#include <openssl/objects.h>
3430
#include <openssl/pem.h>
3531
#include <openssl/rsa.h>
3632
#include <openssl/sha.h>
3733

38-
// Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format.
39-
// Lifted from secure adb's mincrypt key generation.
40-
static int convert_to_mincrypt_format(RSA *rsa, RSAPublicKey *pkey)
41-
{
42-
int ret = -1;
43-
unsigned int i;
44-
45-
if (RSA_size(rsa) != RSANUMBYTES)
46-
goto out;
47-
48-
BN_CTX* ctx = BN_CTX_new();
49-
BIGNUM* r32 = BN_new();
50-
BIGNUM* rr = BN_new();
51-
BIGNUM* r = BN_new();
52-
BIGNUM* rem = BN_new();
53-
BIGNUM* n = BN_new();
54-
BIGNUM* n0inv = BN_new();
55-
56-
BN_set_bit(r32, 32);
57-
BN_copy(n, rsa->n);
58-
BN_set_bit(r, RSANUMWORDS * 32);
59-
BN_mod_sqr(rr, r, n, ctx);
60-
BN_div(NULL, rem, n, r32, ctx);
61-
BN_mod_inverse(n0inv, rem, r32, ctx);
62-
63-
pkey->len = RSANUMWORDS;
64-
pkey->n0inv = 0 - BN_get_word(n0inv);
65-
for (i = 0; i < RSANUMWORDS; i++) {
66-
BN_div(rr, rem, rr, r32, ctx);
67-
pkey->rr[i] = BN_get_word(rem);
68-
BN_div(n, rem, n, r32, ctx);
69-
pkey->n[i] = BN_get_word(rem);
70-
}
71-
pkey->exponent = BN_get_word(rsa->e);
72-
73-
ret = 0;
74-
75-
BN_free(n0inv);
76-
BN_free(n);
77-
BN_free(rem);
78-
BN_free(r);
79-
BN_free(rr);
80-
BN_free(r32);
81-
BN_CTX_free(ctx);
82-
83-
out:
84-
return ret;
85-
}
86-
8734
static int write_public_keyfile(RSA *private_key, const char *private_key_path)
8835
{
89-
RSAPublicKey pkey;
36+
uint8_t key_data[ANDROID_PUBKEY_ENCODED_SIZE];
9037
BIO *bfile = NULL;
9138
char *path = NULL;
9239
int ret = -1;
9340

9441
if (asprintf(&path, "%s.pub", private_key_path) < 0)
9542
goto out;
9643

97-
if (convert_to_mincrypt_format(private_key, &pkey) < 0)
44+
if (!android_pubkey_encode(private_key, key_data, sizeof(key_data)))
9845
goto out;
9946

10047
bfile = BIO_new_file(path, "w");
10148
if (!bfile)
10249
goto out;
10350

104-
BIO_write(bfile, &pkey, sizeof(pkey));
51+
BIO_write(bfile, key_data, sizeof(key_data));
10552
BIO_flush(bfile);
10653

10754
ret = 0;

0 commit comments

Comments
 (0)