Skip to content

Commit

Permalink
Add libcrypto_utils.
Browse files Browse the repository at this point in the history
This provides a tiny library implementing encode/decode functionality
for Android's custom RSA public key binary format. Keys are encoded
from and decoded to BoringSSL RSA key objects.

Change-Id: I55e5522d557e0e9f35927a87b6581f020ee34e7a
  • Loading branch information
mnissler authored and enh-google committed Apr 12, 2016
1 parent 979ce0e commit b62146d
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 0 deletions.
56 changes: 56 additions & 0 deletions libcrypto_utils/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto_utils
LOCAL_SRC_FILES := android_pubkey.c
LOCAL_CFLAGS := -Wall -Werror -Wextra -std=c99
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := libcrypto
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto_utils
LOCAL_SRC_FILES := android_pubkey.c
LOCAL_CFLAGS := -Wall -Werror -Wextra -std=c99
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := libcrypto-host
include $(BUILD_HOST_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto_utils_static
LOCAL_SRC_FILES := android_pubkey.c
LOCAL_CFLAGS := -Wall -Werror -Wextra -std=c99
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := libcrypto_static
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto_utils_static
LOCAL_MODULE_HOST_OS := darwin linux windows
LOCAL_SRC_FILES := android_pubkey.c
LOCAL_CFLAGS := -Wall -Werror -Wextra -std=c99
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := libcrypto_static
include $(BUILD_HOST_STATIC_LIBRARY)

include $(call all-makefiles-under,$(LOCAL_PATH))
167 changes: 167 additions & 0 deletions libcrypto_utils/android_pubkey.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <crypto_utils/android_pubkey.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>

// Better safe than sorry.
#if (ANDROID_PUBKEY_MODULUS_SIZE % 4) != 0
#error RSA modulus size must be multiple of the word size!
#endif

// Size of the RSA modulus in words.
#define ANDROID_PUBKEY_MODULUS_SIZE_WORDS (ANDROID_PUBKEY_MODULUS_SIZE / 4)

// This file implements encoding and decoding logic for Android's custom RSA
// public key binary format. Public keys are stored as a sequence of
// little-endian 32 bit words. Note that Android only supports little-endian
// processors, so we don't do any byte order conversions when parsing the binary
// struct.
typedef struct RSAPublicKey {
// Modulus length. This must be ANDROID_PUBKEY_MODULUS_SIZE.
uint32_t modulus_size_words;

// Precomputed montgomery parameter: -1 / n[0] mod 2^32
uint32_t n0inv;

// RSA modulus as a little-endian array.
uint8_t modulus[ANDROID_PUBKEY_MODULUS_SIZE];

// Montgomery parameter R^2 as a little-endian array of little-endian words.
uint8_t rr[ANDROID_PUBKEY_MODULUS_SIZE];

// RSA modulus: 3 or 65537
uint32_t exponent;
} RSAPublicKey;

// Reverses byte order in |buffer|.
static void reverse_bytes(uint8_t* buffer, size_t size) {
for (size_t i = 0; i < (size + 1) / 2; ++i) {
uint8_t tmp = buffer[i];
buffer[i] = buffer[size - i - 1];
buffer[size - i - 1] = tmp;
}
}

bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key) {
const RSAPublicKey* key_struct = (RSAPublicKey*)key_buffer;
bool ret = false;
uint8_t modulus_buffer[ANDROID_PUBKEY_MODULUS_SIZE];
RSA* new_key = RSA_new();
if (!new_key) {
goto cleanup;
}

// Check |size| is large enough and the modulus size is correct.
if (size < sizeof(RSAPublicKey)) {
goto cleanup;
}
if (key_struct->modulus_size_words != ANDROID_PUBKEY_MODULUS_SIZE_WORDS) {
goto cleanup;
}

// Convert the modulus to big-endian byte order as expected by BN_bin2bn.
memcpy(modulus_buffer, key_struct->modulus, sizeof(modulus_buffer));
reverse_bytes(modulus_buffer, sizeof(modulus_buffer));
new_key->n = BN_bin2bn(modulus_buffer, sizeof(modulus_buffer), NULL);
if (!new_key->n) {
goto cleanup;
}

// Read the exponent.
new_key->e = BN_new();
if (!new_key->e || !BN_set_word(new_key->e, key_struct->exponent)) {
goto cleanup;
}

// Note that we don't extract the montgomery parameters n0inv and rr from
// the RSAPublicKey structure. They assume a word size of 32 bits, but
// BoringSSL may use a word size of 64 bits internally, so we're lacking the
// top 32 bits of n0inv in general. For now, we just ignore the parameters
// and have BoringSSL recompute them internally. More sophisticated logic can
// be added here if/when we want the additional speedup from using the
// pre-computed montgomery parameters.

*key = new_key;
ret = true;

cleanup:
if (!ret && new_key) {
RSA_free(new_key);
}
return ret;
}

static bool android_pubkey_encode_bignum(const BIGNUM* num, uint8_t* buffer) {
if (!BN_bn2bin_padded(buffer, ANDROID_PUBKEY_MODULUS_SIZE, num)) {
return false;
}

reverse_bytes(buffer, ANDROID_PUBKEY_MODULUS_SIZE);
return true;
}

bool android_pubkey_encode(const RSA* key, uint8_t* key_buffer, size_t size) {
RSAPublicKey* key_struct = (RSAPublicKey*)key_buffer;
bool ret = false;
BN_CTX* ctx = BN_CTX_new();
BIGNUM* r32 = BN_new();
BIGNUM* n0inv = BN_new();
BIGNUM* rr = BN_new();

if (sizeof(RSAPublicKey) > size ||
RSA_size(key) != ANDROID_PUBKEY_MODULUS_SIZE) {
goto cleanup;
}

// Store the modulus size.
key_struct->modulus_size_words = ANDROID_PUBKEY_MODULUS_SIZE_WORDS;

// Compute and store n0inv = -1 / N[0] mod 2^32.
if (!ctx || !r32 || !n0inv || !BN_set_bit(r32, 32) ||
!BN_mod(n0inv, key->n, r32, ctx) ||
!BN_mod_inverse(n0inv, n0inv, r32, ctx) || !BN_sub(n0inv, r32, n0inv)) {
goto cleanup;
}
key_struct->n0inv = (uint32_t)BN_get_word(n0inv);

// Store the modulus.
if (!android_pubkey_encode_bignum(key->n, key_struct->modulus)) {
goto cleanup;
}

// Compute and store rr = (2^(rsa_size)) ^ 2 mod N.
if (!ctx || !rr || !BN_set_bit(rr, ANDROID_PUBKEY_MODULUS_SIZE * 8) ||
!BN_mod_sqr(rr, rr, key->n, ctx) ||
!android_pubkey_encode_bignum(rr, key_struct->rr)) {
goto cleanup;
}

// Store the exponent.
key_struct->exponent = (uint32_t)BN_get_word(key->e);

ret = true;

cleanup:
BN_free(rr);
BN_free(n0inv);
BN_free(r32);
BN_CTX_free(ctx);
return ret;
}
61 changes: 61 additions & 0 deletions libcrypto_utils/include/crypto_utils/android_pubkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CRYPTO_UTILS_ANDROID_PUBKEY_H
#define CRYPTO_UTILS_ANDROID_PUBKEY_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <openssl/rsa.h>

#ifdef __cplusplus
extern "C" {
#endif

// Size of an RSA modulus such as an encrypted block or a signature.
#define ANDROID_PUBKEY_MODULUS_SIZE (2048 / 8)

// Size of an encoded RSA key.
#define ANDROID_PUBKEY_ENCODED_SIZE \
(3 * sizeof(uint32_t) + 2 * ANDROID_PUBKEY_MODULUS_SIZE)

/* Allocates a new RSA |key| object, decodes a public RSA key stored in
* Android's custom binary format from |key_buffer| and sets the key parameters
* in |key|. |size| specifies the size of the key buffer and must be at least
* |ANDROID_PUBKEY_ENCODED_SIZE|. The resulting |*key| can be used with the
* standard BoringSSL API to perform public operations.
*
* Returns true if successful, in which case the caller receives ownership of
* the |*key| object, i.e. needs to call RSA_free() when done with it. If there
* is an error, |key| is left untouched and the return value will be false.
*/
bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key);

/* Encodes |key| in the Android RSA public key binary format and stores the
* bytes in |key_buffer|. |key_buffer| should be of size at least
* |ANDROID_PUBKEY_ENCODED_SIZE|.
*
* Returns true if successful, false on error.
*/
bool android_pubkey_encode(const RSA* key, uint8_t* key_buffer, size_t size);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // CRYPTO_UTILS_ANDROID_PUBKEY_H
24 changes: 24 additions & 0 deletions libcrypto_utils/tests/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto_utils_test
LOCAL_SRC_FILES := android_pubkey_test.cpp
LOCAL_CFLAGS := -Wall -Werror -Wextra -std=c++11
LOCAL_SHARED_LIBRARIES := libcrypto_utils libcrypto-host
include $(BUILD_HOST_NATIVE_TEST)
Loading

0 comments on commit b62146d

Please sign in to comment.