am 4ff9c3f7: am aab1670b: Merge "Add support for ECDSA P-256 with SHA256"
* commit '4ff9c3f7d89fe418290288d0925f257f0fe15f20': Add support for ECDSA P-256 with SHA256
This commit is contained in:
commit
33d1e6294f
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_DSA_SIG_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_DSA_SIG_H_
|
||||
|
||||
#include "mincrypt/p256.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Returns 0 if input sig is not a valid ASN.1 sequence
|
||||
int dsa_sig_unpack(unsigned char* sig, int sig_len, p256_int* r_int, p256_int* s_int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SYSTEM_CORE_INCLUDE_MINCRYPT_DSA_SIG_H_ */
|
|
@ -1,8 +1,31 @@
|
|||
// Copyright 2007 Google Inc. All Rights Reserved.
|
||||
// Author: mschilder@google.com (Marius Schilder)
|
||||
/*
|
||||
* Copyright 2007 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SECURITY_UTIL_LITE_HASH_INTERNAL_H__
|
||||
#define SECURITY_UTIL_LITE_HASH_INTERNAL_H__
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_HASH_INTERNAL_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_HASH_INTERNAL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -37,4 +60,4 @@ typedef struct HASH_CTX {
|
|||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // SECURITY_UTIL_LITE_HASH_INTERNAL_H__
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_HASH_INTERNAL_H_
|
||||
|
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_LITE_P256_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_LITE_P256_H_
|
||||
|
||||
// Collection of routines manipulating 256 bit unsigned integers.
|
||||
// Just enough to implement ecdsa-p256 and related algorithms.
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define P256_BITSPERDIGIT 32
|
||||
#define P256_NDIGITS 8
|
||||
#define P256_NBYTES 32
|
||||
|
||||
typedef int p256_err;
|
||||
typedef uint32_t p256_digit;
|
||||
typedef int32_t p256_sdigit;
|
||||
typedef uint64_t p256_ddigit;
|
||||
typedef int64_t p256_sddigit;
|
||||
|
||||
// Defining p256_int as struct to leverage struct assigment.
|
||||
typedef struct {
|
||||
p256_digit a[P256_NDIGITS];
|
||||
} p256_int;
|
||||
|
||||
extern const p256_int SECP256r1_n; // Curve order
|
||||
extern const p256_int SECP256r1_p; // Curve prime
|
||||
extern const p256_int SECP256r1_b; // Curve param
|
||||
|
||||
// Initialize a p256_int to zero.
|
||||
void p256_init(p256_int* a);
|
||||
|
||||
// Clear a p256_int to zero.
|
||||
void p256_clear(p256_int* a);
|
||||
|
||||
// Return bit. Index 0 is least significant.
|
||||
int p256_get_bit(const p256_int* a, int index);
|
||||
|
||||
// b := a % MOD
|
||||
void p256_mod(
|
||||
const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
p256_int* b);
|
||||
|
||||
// c := a * (top_b | b) % MOD
|
||||
void p256_modmul(
|
||||
const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
const p256_digit top_b,
|
||||
const p256_int* b,
|
||||
p256_int* c);
|
||||
|
||||
// b := 1 / a % MOD
|
||||
// MOD best be SECP256r1_n
|
||||
void p256_modinv(
|
||||
const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
p256_int* b);
|
||||
|
||||
// b := 1 / a % MOD
|
||||
// MOD best be SECP256r1_n
|
||||
// Faster than p256_modinv()
|
||||
void p256_modinv_vartime(
|
||||
const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
p256_int* b);
|
||||
|
||||
// b := a << (n % P256_BITSPERDIGIT)
|
||||
// Returns the bits shifted out of most significant digit.
|
||||
p256_digit p256_shl(const p256_int* a, int n, p256_int* b);
|
||||
|
||||
// b := a >> (n % P256_BITSPERDIGIT)
|
||||
void p256_shr(const p256_int* a, int n, p256_int* b);
|
||||
|
||||
int p256_is_zero(const p256_int* a);
|
||||
int p256_is_odd(const p256_int* a);
|
||||
int p256_is_even(const p256_int* a);
|
||||
|
||||
// Returns -1, 0 or 1.
|
||||
int p256_cmp(const p256_int* a, const p256_int *b);
|
||||
|
||||
// c: = a - b
|
||||
// Returns -1 on borrow.
|
||||
int p256_sub(const p256_int* a, const p256_int* b, p256_int* c);
|
||||
|
||||
// c := a + b
|
||||
// Returns 1 on carry.
|
||||
int p256_add(const p256_int* a, const p256_int* b, p256_int* c);
|
||||
|
||||
// c := a + (single digit)b
|
||||
// Returns carry 1 on carry.
|
||||
int p256_add_d(const p256_int* a, p256_digit b, p256_int* c);
|
||||
|
||||
// ec routines.
|
||||
|
||||
// {out_x,out_y} := nG
|
||||
void p256_base_point_mul(const p256_int *n,
|
||||
p256_int *out_x,
|
||||
p256_int *out_y);
|
||||
|
||||
// {out_x,out_y} := n{in_x,in_y}
|
||||
void p256_point_mul(const p256_int *n,
|
||||
const p256_int *in_x,
|
||||
const p256_int *in_y,
|
||||
p256_int *out_x,
|
||||
p256_int *out_y);
|
||||
|
||||
// {out_x,out_y} := n1G + n2{in_x,in_y}
|
||||
void p256_points_mul_vartime(
|
||||
const p256_int *n1, const p256_int *n2,
|
||||
const p256_int *in_x, const p256_int *in_y,
|
||||
p256_int *out_x, p256_int *out_y);
|
||||
|
||||
// Return whether point {x,y} is on curve.
|
||||
int p256_is_valid_point(const p256_int* x, const p256_int* y);
|
||||
|
||||
// Outputs big-endian binary form. No leading zero skips.
|
||||
void p256_to_bin(const p256_int* src, uint8_t dst[P256_NBYTES]);
|
||||
|
||||
// Reads from big-endian binary form,
|
||||
// thus pre-pad with leading zeros if short.
|
||||
void p256_from_bin(const uint8_t src[P256_NBYTES], p256_int* dst);
|
||||
|
||||
#define P256_DIGITS(x) ((x)->a)
|
||||
#define P256_DIGIT(x,y) ((x)->a[y])
|
||||
|
||||
#define P256_ZERO {{0}}
|
||||
#define P256_ONE {{1}}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_LITE_P256_H_
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_P256_ECDSA_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_P256_ECDSA_H_
|
||||
|
||||
// Using current directory as relative include path here since
|
||||
// this code typically gets lifted into a variety of build systems
|
||||
// and directory structures.
|
||||
#include "p256.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Returns 0 if {r,s} is not a signature on message for
|
||||
// public key {key_x,key_y}.
|
||||
//
|
||||
// Note: message is a p256_int.
|
||||
// Convert from a binary string using p256_from_bin().
|
||||
int p256_ecdsa_verify(const p256_int* key_x,
|
||||
const p256_int* key_y,
|
||||
const p256_int* message,
|
||||
const p256_int* r, const p256_int* s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_P256_ECDSA_H_
|
|
@ -25,8 +25,8 @@
|
|||
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _EMBEDDED_RSA_H_
|
||||
#define _EMBEDDED_RSA_H_
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_RSA_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_RSA_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
|
@ -55,4 +55,4 @@ int RSA_verify(const RSAPublicKey *key,
|
|||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_RSA_H_
|
||||
|
|
|
@ -1,8 +1,30 @@
|
|||
// Copyright 2005 Google Inc. All Rights Reserved.
|
||||
// Author: mschilder@google.com (Marius Schilder)
|
||||
|
||||
#ifndef SECURITY_UTIL_LITE_SHA1_H__
|
||||
#define SECURITY_UTIL_LITE_SHA1_H__
|
||||
/*
|
||||
* Copyright 2005 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_SHA1_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_SHA1_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hash-internal.h"
|
||||
|
@ -27,4 +49,4 @@ const uint8_t* SHA_hash(const void* data, int len, uint8_t* digest);
|
|||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // SECURITY_UTIL_LITE_SHA1_H__
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_SHA1_H_
|
||||
|
|
|
@ -1,8 +1,31 @@
|
|||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
// Author: mschilder@google.com (Marius Schilder)
|
||||
/*
|
||||
* Copyright 2011 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SECURITY_UTIL_LITE_SHA256_H__
|
||||
#define SECURITY_UTIL_LITE_SHA256_H__
|
||||
#ifndef SYSTEM_CORE_INCLUDE_MINCRYPT_SHA256_H_
|
||||
#define SYSTEM_CORE_INCLUDE_MINCRYPT_SHA256_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "hash-internal.h"
|
||||
|
@ -26,4 +49,4 @@ const uint8_t* SHA256_hash(const void* data, int len, uint8_t* digest);
|
|||
}
|
||||
#endif // __cplusplus
|
||||
|
||||
#endif // SECURITY_UTIL_LITE_SHA256_H__
|
||||
#endif // SYSTEM_CORE_INCLUDE_MINCRYPT_SHA256_H_
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
# Copyright 2008 The Android Open Source Project
|
||||
#
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := libmincrypt
|
||||
LOCAL_SRC_FILES := rsa.c sha.c sha256.c
|
||||
LOCAL_SRC_FILES := dsa_sig.c p256.c p256_ec.c p256_ecdsa.c rsa.c sha.c sha256.c
|
||||
LOCAL_CFLAGS := -Wall -Werror
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE := libmincrypt
|
||||
LOCAL_SRC_FILES := rsa.c sha.c sha256.c
|
||||
LOCAL_SRC_FILES := dsa_sig.c p256.c p256_ec.c p256_ecdsa.c rsa.c sha.c sha256.c
|
||||
LOCAL_CFLAGS := -Wall -Werror
|
||||
include $(BUILD_HOST_STATIC_LIBRARY)
|
||||
|
||||
|
||||
include $(LOCAL_PATH)/tools/Android.mk \
|
||||
$(LOCAL_PATH)/test/Android.mk
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mincrypt/p256.h"
|
||||
|
||||
/**
|
||||
* Trims off the leading zero bytes and copy it to a buffer aligning it to the end.
|
||||
*/
|
||||
static inline int trim_to_p256_bytes(unsigned char dst[P256_NBYTES], unsigned char *src,
|
||||
int src_len) {
|
||||
int dst_offset;
|
||||
while (*src == '\0' && src_len > 0) {
|
||||
src++;
|
||||
src_len--;
|
||||
}
|
||||
if (src_len > P256_NBYTES || src_len < 1) {
|
||||
return 0;
|
||||
}
|
||||
dst_offset = P256_NBYTES - src_len;
|
||||
memset(dst, 0, dst_offset);
|
||||
memcpy(dst + dst_offset, src, src_len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks the ASN.1 DSA signature sequence.
|
||||
*/
|
||||
int dsa_sig_unpack(unsigned char* sig, int sig_len, p256_int* r_int, p256_int* s_int) {
|
||||
/*
|
||||
* Structure is:
|
||||
* 0x30 0xNN SEQUENCE + s_length
|
||||
* 0x02 0xNN INTEGER + r_length
|
||||
* 0xAA 0xBB .. r_length bytes of "r" (offset 4)
|
||||
* 0x02 0xNN INTEGER + s_length
|
||||
* 0xMM 0xNN .. s_length bytes of "s" (offset 6 + r_len)
|
||||
*/
|
||||
int seq_len;
|
||||
unsigned char r_bytes[P256_NBYTES];
|
||||
unsigned char s_bytes[P256_NBYTES];
|
||||
int r_len;
|
||||
int s_len;
|
||||
|
||||
memset(r_bytes, 0, sizeof(r_bytes));
|
||||
memset(s_bytes, 0, sizeof(s_bytes));
|
||||
|
||||
/*
|
||||
* Must have at least:
|
||||
* 2 bytes sequence header and length
|
||||
* 2 bytes R integer header and length
|
||||
* 1 byte of R
|
||||
* 2 bytes S integer header and length
|
||||
* 1 byte of S
|
||||
*
|
||||
* 8 bytes total
|
||||
*/
|
||||
if (sig_len < 8 || sig[0] != 0x30 || sig[2] != 0x02) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
seq_len = sig[1];
|
||||
if ((seq_len <= 0) || (seq_len + 2 != sig_len)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
r_len = sig[3];
|
||||
/*
|
||||
* Must have at least:
|
||||
* 2 bytes for R header and length
|
||||
* 2 bytes S integer header and length
|
||||
* 1 byte of S
|
||||
*/
|
||||
if ((r_len < 1) || (r_len > seq_len - 5) || (sig[4 + r_len] != 0x02)) {
|
||||
return 0;
|
||||
}
|
||||
s_len = sig[5 + r_len];
|
||||
|
||||
/**
|
||||
* Must have:
|
||||
* 2 bytes for R header and length
|
||||
* r_len bytes for R
|
||||
* 2 bytes S integer header and length
|
||||
*/
|
||||
if ((s_len < 1) || (s_len != seq_len - 4 - r_len)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* ASN.1 encoded integers are zero-padded for positive integers. Make sure we have
|
||||
* a correctly-sized buffer and that the resulting integer isn't too large.
|
||||
*/
|
||||
if (!trim_to_p256_bytes(r_bytes, &sig[4], r_len)
|
||||
|| !trim_to_p256_bytes(s_bytes, &sig[6 + r_len], s_len)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
p256_from_bin(r_bytes, r_int);
|
||||
p256_from_bin(s_bytes, s_int);
|
||||
|
||||
return 1;
|
||||
}
|
|
@ -0,0 +1,375 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// This is an implementation of the P256 elliptic curve group. It's written to
|
||||
// be portable 32-bit, although it's still constant-time.
|
||||
//
|
||||
// WARNING: Implementing these functions in a constant-time manner is far from
|
||||
// obvious. Be careful when touching this code.
|
||||
//
|
||||
// See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mincrypt/p256.h"
|
||||
|
||||
const p256_int SECP256r1_n = // curve order
|
||||
{{0xfc632551, 0xf3b9cac2, 0xa7179e84, 0xbce6faad, -1, -1, 0, -1}};
|
||||
|
||||
const p256_int SECP256r1_p = // curve field size
|
||||
{{-1, -1, -1, 0, 0, 0, 1, -1 }};
|
||||
|
||||
const p256_int SECP256r1_b = // curve b
|
||||
{{0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
|
||||
0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8}};
|
||||
|
||||
static const p256_int p256_one = P256_ONE;
|
||||
|
||||
void p256_init(p256_int* a) {
|
||||
memset(a, 0, sizeof(*a));
|
||||
}
|
||||
|
||||
void p256_clear(p256_int* a) { p256_init(a); }
|
||||
|
||||
int p256_get_bit(const p256_int* scalar, int bit) {
|
||||
return (P256_DIGIT(scalar, bit / P256_BITSPERDIGIT)
|
||||
>> (bit & (P256_BITSPERDIGIT - 1))) & 1;
|
||||
}
|
||||
|
||||
int p256_is_zero(const p256_int* a) {
|
||||
int i, result = 0;
|
||||
for (i = 0; i < P256_NDIGITS; ++i) result |= P256_DIGIT(a, i);
|
||||
return !result;
|
||||
}
|
||||
|
||||
// top, c[] += a[] * b
|
||||
// Returns new top
|
||||
static p256_digit mulAdd(const p256_int* a,
|
||||
p256_digit b,
|
||||
p256_digit top,
|
||||
p256_digit* c) {
|
||||
int i;
|
||||
p256_ddigit carry = 0;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
carry += *c;
|
||||
carry += (p256_ddigit)P256_DIGIT(a, i) * b;
|
||||
*c++ = (p256_digit)carry;
|
||||
carry >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return top + (p256_digit)carry;
|
||||
}
|
||||
|
||||
// top, c[] -= top_a, a[]
|
||||
static p256_digit subTop(p256_digit top_a,
|
||||
const p256_digit* a,
|
||||
p256_digit top_c,
|
||||
p256_digit* c) {
|
||||
int i;
|
||||
p256_sddigit borrow = 0;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
borrow += *c;
|
||||
borrow -= *a++;
|
||||
*c++ = (p256_digit)borrow;
|
||||
borrow >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
borrow += top_c;
|
||||
borrow -= top_a;
|
||||
top_c = (p256_digit)borrow;
|
||||
assert((borrow >> P256_BITSPERDIGIT) == 0);
|
||||
return top_c;
|
||||
}
|
||||
|
||||
// top, c[] -= MOD[] & mask (0 or -1)
|
||||
// returns new top.
|
||||
static p256_digit subM(const p256_int* MOD,
|
||||
p256_digit top,
|
||||
p256_digit* c,
|
||||
p256_digit mask) {
|
||||
int i;
|
||||
p256_sddigit borrow = 0;
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
borrow += *c;
|
||||
borrow -= P256_DIGIT(MOD, i) & mask;
|
||||
*c++ = (p256_digit)borrow;
|
||||
borrow >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return top + (p256_digit)borrow;
|
||||
}
|
||||
|
||||
// top, c[] += MOD[] & mask (0 or -1)
|
||||
// returns new top.
|
||||
static p256_digit addM(const p256_int* MOD,
|
||||
p256_digit top,
|
||||
p256_digit* c,
|
||||
p256_digit mask) {
|
||||
int i;
|
||||
p256_ddigit carry = 0;
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
carry += *c;
|
||||
carry += P256_DIGIT(MOD, i) & mask;
|
||||
*c++ = (p256_digit)carry;
|
||||
carry >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return top + (p256_digit)carry;
|
||||
}
|
||||
|
||||
// c = a * b mod MOD. c can be a and/or b.
|
||||
void p256_modmul(const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
const p256_digit top_b,
|
||||
const p256_int* b,
|
||||
p256_int* c) {
|
||||
p256_digit tmp[P256_NDIGITS * 2 + 1] = { 0 };
|
||||
p256_digit top = 0;
|
||||
int i;
|
||||
|
||||
// Multiply/add into tmp.
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
if (i) tmp[i + P256_NDIGITS - 1] = top;
|
||||
top = mulAdd(a, P256_DIGIT(b, i), 0, tmp + i);
|
||||
}
|
||||
|
||||
// Multiply/add top digit
|
||||
tmp[i + P256_NDIGITS - 1] = top;
|
||||
top = mulAdd(a, top_b, 0, tmp + i);
|
||||
|
||||
// Reduce tmp, digit by digit.
|
||||
for (; i >= 0; --i) {
|
||||
p256_digit reducer[P256_NDIGITS] = { 0 };
|
||||
p256_digit top_reducer;
|
||||
|
||||
// top can be any value at this point.
|
||||
// Guestimate reducer as top * MOD, since msw of MOD is -1.
|
||||
top_reducer = mulAdd(MOD, top, 0, reducer);
|
||||
|
||||
// Subtract reducer from top | tmp.
|
||||
top = subTop(top_reducer, reducer, top, tmp + i);
|
||||
|
||||
// top is now either 0 or 1. Make it 0, fixed-timing.
|
||||
assert(top <= 1);
|
||||
|
||||
top = subM(MOD, top, tmp + i, ~(top - 1));
|
||||
|
||||
assert(top == 0);
|
||||
|
||||
// We have now reduced the top digit off tmp. Fetch new top digit.
|
||||
top = tmp[i + P256_NDIGITS - 1];
|
||||
}
|
||||
|
||||
// tmp might still be larger than MOD, yet same bit length.
|
||||
// Make sure it is less, fixed-timing.
|
||||
addM(MOD, 0, tmp, subM(MOD, 0, tmp, -1));
|
||||
|
||||
memcpy(c, tmp, P256_NBYTES);
|
||||
}
|
||||
int p256_is_odd(const p256_int* a) { return P256_DIGIT(a, 0) & 1; }
|
||||
int p256_is_even(const p256_int* a) { return !(P256_DIGIT(a, 0) & 1); }
|
||||
|
||||
p256_digit p256_shl(const p256_int* a, int n, p256_int* b) {
|
||||
int i;
|
||||
p256_digit top = P256_DIGIT(a, P256_NDIGITS - 1);
|
||||
|
||||
n %= P256_BITSPERDIGIT;
|
||||
for (i = P256_NDIGITS - 1; i > 0; --i) {
|
||||
p256_digit accu = (P256_DIGIT(a, i) << n);
|
||||
accu |= (P256_DIGIT(a, i - 1) >> (P256_BITSPERDIGIT - n));
|
||||
P256_DIGIT(b, i) = accu;
|
||||
}
|
||||
P256_DIGIT(b, i) = (P256_DIGIT(a, i) << n);
|
||||
|
||||
top = (p256_digit)((((p256_ddigit)top) << n) >> P256_BITSPERDIGIT);
|
||||
|
||||
return top;
|
||||
}
|
||||
|
||||
void p256_shr(const p256_int* a, int n, p256_int* b) {
|
||||
int i;
|
||||
|
||||
n %= P256_BITSPERDIGIT;
|
||||
for (i = 0; i < P256_NDIGITS - 1; ++i) {
|
||||
p256_digit accu = (P256_DIGIT(a, i) >> n);
|
||||
accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - n));
|
||||
P256_DIGIT(b, i) = accu;
|
||||
}
|
||||
P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> n);
|
||||
}
|
||||
|
||||
static void p256_shr1(const p256_int* a, int highbit, p256_int* b) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS - 1; ++i) {
|
||||
p256_digit accu = (P256_DIGIT(a, i) >> 1);
|
||||
accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - 1));
|
||||
P256_DIGIT(b, i) = accu;
|
||||
}
|
||||
P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> 1) |
|
||||
(highbit << (P256_BITSPERDIGIT - 1));
|
||||
}
|
||||
|
||||
// Return -1, 0, 1 for a < b, a == b or a > b respectively.
|
||||
int p256_cmp(const p256_int* a, const p256_int* b) {
|
||||
int i;
|
||||
p256_sddigit borrow = 0;
|
||||
p256_digit notzero = 0;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
|
||||
// Track whether any result digit is ever not zero.
|
||||
// Relies on !!(non-zero) evaluating to 1, e.g., !!(-1) evaluating to 1.
|
||||
notzero |= !!((p256_digit)borrow);
|
||||
borrow >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return (int)borrow | notzero;
|
||||
}
|
||||
|
||||
// c = a - b. Returns borrow: 0 or -1.
|
||||
int p256_sub(const p256_int* a, const p256_int* b, p256_int* c) {
|
||||
int i;
|
||||
p256_sddigit borrow = 0;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
|
||||
if (c) P256_DIGIT(c, i) = (p256_digit)borrow;
|
||||
borrow >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return (int)borrow;
|
||||
}
|
||||
|
||||
// c = a + b. Returns carry: 0 or 1.
|
||||
int p256_add(const p256_int* a, const p256_int* b, p256_int* c) {
|
||||
int i;
|
||||
p256_ddigit carry = 0;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
carry += (p256_ddigit)P256_DIGIT(a, i) + P256_DIGIT(b, i);
|
||||
if (c) P256_DIGIT(c, i) = (p256_digit)carry;
|
||||
carry >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return (int)carry;
|
||||
}
|
||||
|
||||
// b = a + d. Returns carry, 0 or 1.
|
||||
int p256_add_d(const p256_int* a, p256_digit d, p256_int* b) {
|
||||
int i;
|
||||
p256_ddigit carry = d;
|
||||
|
||||
for (i = 0; i < P256_NDIGITS; ++i) {
|
||||
carry += (p256_ddigit)P256_DIGIT(a, i);
|
||||
if (b) P256_DIGIT(b, i) = (p256_digit)carry;
|
||||
carry >>= P256_BITSPERDIGIT;
|
||||
}
|
||||
return (int)carry;
|
||||
}
|
||||
|
||||
// b = 1/a mod MOD, binary euclid.
|
||||
void p256_modinv_vartime(const p256_int* MOD,
|
||||
const p256_int* a,
|
||||
p256_int* b) {
|
||||
p256_int R = P256_ZERO;
|
||||
p256_int S = P256_ONE;
|
||||
p256_int U = *MOD;
|
||||
p256_int V = *a;
|
||||
|
||||
for (;;) {
|
||||
if (p256_is_even(&U)) {
|
||||
p256_shr1(&U, 0, &U);
|
||||
if (p256_is_even(&R)) {
|
||||
p256_shr1(&R, 0, &R);
|
||||
} else {
|
||||
// R = (R+MOD)/2
|
||||
p256_shr1(&R, p256_add(&R, MOD, &R), &R);
|
||||
}
|
||||
} else if (p256_is_even(&V)) {
|
||||
p256_shr1(&V, 0, &V);
|
||||
if (p256_is_even(&S)) {
|
||||
p256_shr1(&S, 0, &S);
|
||||
} else {
|
||||
// S = (S+MOD)/2
|
||||
p256_shr1(&S, p256_add(&S, MOD, &S) , &S);
|
||||
}
|
||||
} else { // U,V both odd.
|
||||
if (!p256_sub(&V, &U, NULL)) {
|
||||
p256_sub(&V, &U, &V);
|
||||
if (p256_sub(&S, &R, &S)) p256_add(&S, MOD, &S);
|
||||
if (p256_is_zero(&V)) break; // done.
|
||||
} else {
|
||||
p256_sub(&U, &V, &U);
|
||||
if (p256_sub(&R, &S, &R)) p256_add(&R, MOD, &R);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p256_mod(MOD, &R, b);
|
||||
}
|
||||
|
||||
void p256_mod(const p256_int* MOD,
|
||||
const p256_int* in,
|
||||
p256_int* out) {
|
||||
if (out != in) *out = *in;
|
||||
addM(MOD, 0, P256_DIGITS(out), subM(MOD, 0, P256_DIGITS(out), -1));
|
||||
}
|
||||
|
||||
// Verify y^2 == x^3 - 3x + b mod p
|
||||
// and 0 < x < p and 0 < y < p
|
||||
int p256_is_valid_point(const p256_int* x, const p256_int* y) {
|
||||
p256_int y2, x3;
|
||||
|
||||
if (p256_cmp(&SECP256r1_p, x) <= 0 ||
|
||||
p256_cmp(&SECP256r1_p, y) <= 0 ||
|
||||
p256_is_zero(x) ||
|
||||
p256_is_zero(y)) return 0;
|
||||
|
||||
p256_modmul(&SECP256r1_p, y, 0, y, &y2); // y^2
|
||||
|
||||
p256_modmul(&SECP256r1_p, x, 0, x, &x3); // x^2
|
||||
p256_modmul(&SECP256r1_p, x, 0, &x3, &x3); // x^3
|
||||
if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - x
|
||||
if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 2x
|
||||
if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 3x
|
||||
if (p256_add(&x3, &SECP256r1_b, &x3)) // x^3 - 3x + b
|
||||
p256_sub(&x3, &SECP256r1_p, &x3);
|
||||
|
||||
return p256_cmp(&y2, &x3) == 0;
|
||||
}
|
||||
|
||||
void p256_from_bin(const uint8_t src[P256_NBYTES], p256_int* dst) {
|
||||
int i;
|
||||
const uint8_t* p = &src[0];
|
||||
|
||||
for (i = P256_NDIGITS - 1; i >= 0; --i) {
|
||||
P256_DIGIT(dst, i) =
|
||||
(p[0] << 24) |
|
||||
(p[1] << 16) |
|
||||
(p[2] << 8) |
|
||||
p[3];
|
||||
p += 4;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mincrypt/p256_ecdsa.h"
|
||||
#include "mincrypt/p256.h"
|
||||
|
||||
int p256_ecdsa_verify(const p256_int* key_x, const p256_int* key_y,
|
||||
const p256_int* message,
|
||||
const p256_int* r, const p256_int* s) {
|
||||
p256_int u, v;
|
||||
|
||||
// Check public key.
|
||||
if (!p256_is_valid_point(key_x, key_y)) return 0;
|
||||
|
||||
// Check r and s are != 0 % n.
|
||||
p256_mod(&SECP256r1_n, r, &u);
|
||||
p256_mod(&SECP256r1_n, s, &v);
|
||||
if (p256_is_zero(&u) || p256_is_zero(&v)) return 0;
|
||||
|
||||
p256_modinv_vartime(&SECP256r1_n, s, &v);
|
||||
p256_modmul(&SECP256r1_n, message, 0, &v, &u); // message / s % n
|
||||
p256_modmul(&SECP256r1_n, r, 0, &v, &v); // r / s % n
|
||||
|
||||
p256_points_mul_vartime(&u, &v,
|
||||
key_x, key_y,
|
||||
&u, &v);
|
||||
|
||||
p256_mod(&SECP256r1_n, &u, &u); // (x coord % p) % n
|
||||
return p256_cmp(r, &u) == 0;
|
||||
}
|
||||
|
|
@ -1,10 +1,15 @@
|
|||
# Copyright 2013 The Android Open Source Project
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := rsa_test
|
||||
LOCAL_SRC_FILES := rsa_test.c
|
||||
LOCAL_STATIC_LIBRARIES := libmincrypt
|
||||
include $(BUILD_HOST_EXECUTABLE)
|
||||
include $(BUILD_HOST_NATIVE_TEST)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := ecdsa_test
|
||||
LOCAL_SRC_FILES := ecdsa_test.c
|
||||
LOCAL_STATIC_LIBRARIES := libmincrypt
|
||||
include $(BUILD_HOST_NATIVE_TEST)
|
||||
|
|
|
@ -0,0 +1,278 @@
|
|||
/*
|
||||
* Copyright 2013 The Android Open Source Project
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Google Inc. nor the names of its contributors may
|
||||
* be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mincrypt/p256.h"
|
||||
#include "mincrypt/p256_ecdsa.h"
|
||||
#include "mincrypt/sha256.h"
|
||||
|
||||
/**
|
||||
* Messages signed using:
|
||||
*
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIDw6UiziVMbjlfSpOAIpA2tcL+v1OlznZLnpadO8BGi1oAoGCCqGSM49
|
||||
AwEHoUQDQgAEZw7VAOjAXYRFuhZWYBgjahdOvkwcAnjGkxQWytZW+iS1hI3ZGE24
|
||||
6XmNka9IGxAgj2n/ip+MuZJMFoJ9DRea3g==
|
||||
-----END EC PRIVATE KEY-----
|
||||
*/
|
||||
|
||||
p256_int key_x = {
|
||||
.a = {0xd656fa24u, 0x931416cau, 0x1c0278c6u, 0x174ebe4cu,
|
||||
0x6018236au, 0x45ba1656u, 0xe8c05d84u, 0x670ed500u}
|
||||
};
|
||||
p256_int key_y = {
|
||||
.a = {0x0d179adeu, 0x4c16827du, 0x9f8cb992u, 0x8f69ff8au,
|
||||
0x481b1020u, 0x798d91afu, 0x184db8e9u, 0xb5848dd9u}
|
||||
};
|
||||
|
||||
char* message_1 =
|
||||
"f4 5d 55 f3 55 51 e9 75 d6 a8 dc 7e a9 f4 88 59"
|
||||
"39 40 cc 75 69 4a 27 8f 27 e5 78 a1 63 d8 39 b3"
|
||||
"40 40 84 18 08 cf 9c 58 c9 b8 72 8b f5 f9 ce 8e"
|
||||
"e8 11 ea 91 71 4f 47 ba b9 2d 0f 6d 5a 26 fc fe"
|
||||
"ea 6c d9 3b 91 0c 0a 2c 96 3e 64 eb 18 23 f1 02"
|
||||
"75 3d 41 f0 33 59 10 ad 3a 97 71 04 f1 aa f6 c3"
|
||||
"74 27 16 a9 75 5d 11 b8 ee d6 90 47 7f 44 5c 5d"
|
||||
"27 20 8b 2e 28 43 30 fa 3d 30 14 23 fa 7f 2d 08"
|
||||
"6e 0a d0 b8 92 b9 db 54 4e 45 6d 3f 0d ab 85 d9"
|
||||
"53 c1 2d 34 0a a8 73 ed a7 27 c8 a6 49 db 7f a6"
|
||||
"37 40 e2 5e 9a f1 53 3b 30 7e 61 32 99 93 11 0e"
|
||||
"95 19 4e 03 93 99 c3 82 4d 24 c5 1f 22 b2 6b de"
|
||||
"10 24 cd 39 59 58 a2 df eb 48 16 a6 e8 ad ed b5"
|
||||
"0b 1f 6b 56 d0 b3 06 0f f0 f1 c4 cb 0d 0e 00 1d"
|
||||
"d5 9d 73 be 12";
|
||||
|
||||
char* signature_1 =
|
||||
"30 44 02 20 43 18 fc eb 3b a8 3a a8 a3 cf 41 b7"
|
||||
"81 4a f9 01 e1 8b 6e 95 c1 3a 83 25 9e a5 2e 66"
|
||||
"7c 98 25 d9 02 20 54 f3 7f 5a e9 36 9c a2 f0 51"
|
||||
"e0 6e 78 48 60 a3 f9 8a d5 2c 37 5a 0a 29 c9 f7"
|
||||
"ea 57 7e 88 46 12";
|
||||
|
||||
// Same as signature 1, but with leading zeroes.
|
||||
char* message_2 =
|
||||
"f4 5d 55 f3 55 51 e9 75 d6 a8 dc 7e a9 f4 88 59"
|
||||
"39 40 cc 75 69 4a 27 8f 27 e5 78 a1 63 d8 39 b3"
|
||||
"40 40 84 18 08 cf 9c 58 c9 b8 72 8b f5 f9 ce 8e"
|
||||
"e8 11 ea 91 71 4f 47 ba b9 2d 0f 6d 5a 26 fc fe"
|
||||
"ea 6c d9 3b 91 0c 0a 2c 96 3e 64 eb 18 23 f1 02"
|
||||
"75 3d 41 f0 33 59 10 ad 3a 97 71 04 f1 aa f6 c3"
|
||||
"74 27 16 a9 75 5d 11 b8 ee d6 90 47 7f 44 5c 5d"
|
||||
"27 20 8b 2e 28 43 30 fa 3d 30 14 23 fa 7f 2d 08"
|
||||
"6e 0a d0 b8 92 b9 db 54 4e 45 6d 3f 0d ab 85 d9"
|
||||
"53 c1 2d 34 0a a8 73 ed a7 27 c8 a6 49 db 7f a6"
|
||||
"37 40 e2 5e 9a f1 53 3b 30 7e 61 32 99 93 11 0e"
|
||||
"95 19 4e 03 93 99 c3 82 4d 24 c5 1f 22 b2 6b de"
|
||||
"10 24 cd 39 59 58 a2 df eb 48 16 a6 e8 ad ed b5"
|
||||
"0b 1f 6b 56 d0 b3 06 0f f0 f1 c4 cb 0d 0e 00 1d"
|
||||
"d5 9d 73 be 12";
|
||||
|
||||
char* signature_2 =
|
||||
"30 46 02 21 00 43 18 fc eb 3b a8 3a a8 a3 cf 41 b7"
|
||||
"81 4a f9 01 e1 8b 6e 95 c1 3a 83 25 9e a5 2e 66"
|
||||
"7c 98 25 d9 02 21 00 54 f3 7f 5a e9 36 9c a2 f0 51"
|
||||
"e0 6e 78 48 60 a3 f9 8a d5 2c 37 5a 0a 29 c9 f7"
|
||||
"ea 57 7e 88 46 12";
|
||||
|
||||
// Excessive zeroes on the signature
|
||||
char* message_3 =
|
||||
"f4 5d 55 f3 55 51 e9 75 d6 a8 dc 7e a9 f4 88 59"
|
||||
"39 40 cc 75 69 4a 27 8f 27 e5 78 a1 63 d8 39 b3"
|
||||
"40 40 84 18 08 cf 9c 58 c9 b8 72 8b f5 f9 ce 8e"
|
||||
"e8 11 ea 91 71 4f 47 ba b9 2d 0f 6d 5a 26 fc fe"
|
||||
"ea 6c d9 3b 91 0c 0a 2c 96 3e 64 eb 18 23 f1 02"
|
||||
"75 3d 41 f0 33 59 10 ad 3a 97 71 04 f1 aa f6 c3"
|
||||
"74 27 16 a9 75 5d 11 b8 ee d6 90 47 7f 44 5c 5d"
|
||||
"27 20 8b 2e 28 43 30 fa 3d 30 14 23 fa 7f 2d 08"
|
||||
"6e 0a d0 b8 92 b9 db 54 4e 45 6d 3f 0d ab 85 d9"
|
||||
"53 c1 2d 34 0a a8 73 ed a7 27 c8 a6 49 db 7f a6"
|
||||
"37 40 e2 5e 9a f1 53 3b 30 7e 61 32 99 93 11 0e"
|
||||
"95 19 4e 03 93 99 c3 82 4d 24 c5 1f 22 b2 6b de"
|
||||
"10 24 cd 39 59 58 a2 df eb 48 16 a6 e8 ad ed b5"
|
||||
"0b 1f 6b 56 d0 b3 06 0f f0 f1 c4 cb 0d 0e 00 1d"
|
||||
"d5 9d 73 be 12";
|
||||
|
||||
char* signature_3 =
|
||||
"30 4c 02 24 00 00 00 00 43 18 fc eb 3b a8 3a a8 a3 cf 41 b7"
|
||||
"81 4a f9 01 e1 8b 6e 95 c1 3a 83 25 9e a5 2e 66"
|
||||
"7c 98 25 d9 02 24 00 00 00 00 54 f3 7f 5a e9 36 9c a2 f0 51"
|
||||
"e0 6e 78 48 60 a3 f9 8a d5 2c 37 5a 0a 29 c9 f7"
|
||||
"ea 57 7e 88 46 12";
|
||||
|
||||
|
||||
char* good_dsa_signature_1 =
|
||||
"30 0D 02 01 01 02 08 00 A5 55 5A 01 FF A5 01";
|
||||
p256_int good_dsa_signature_1_r = {
|
||||
.a = {0x00000001U, 0x00000000U, 0x00000000U, 0x00000000U,
|
||||
0x00000000U, 0x00000000U, 0x00000000U, 0x00000000U}
|
||||
};
|
||||
p256_int good_dsa_signature_1_s = {
|
||||
.a = {0x01FFA501U, 0x00A5555AU, 0x00000000U, 0x00000000U,
|
||||
0x00000000U, 0x00000000U, 0x00000000U, 0x00000000U}
|
||||
};
|
||||
|
||||
|
||||
char* bad_dsa_signature_1 =
|
||||
"a0 06 02 01 01 02 01 01";
|
||||
|
||||
char* bad_dsa_signature_2 =
|
||||
"30 07 02 01 01 02 01 01";
|
||||
|
||||
char* bad_dsa_signature_3 =
|
||||
"30 06 82 01 01 02 01 01";
|
||||
|
||||
char* bad_dsa_signature_4 =
|
||||
"30 06 02 00 01 02 01 01";
|
||||
|
||||
char* bad_dsa_signature_5 =
|
||||
"30 06 02 01 01 82 01 01";
|
||||
|
||||
char* bad_dsa_signature_6 =
|
||||
"30 05 02 01 01 02 00";
|
||||
|
||||
char* bad_dsa_signature_7 =
|
||||
"30 06 02 01 01 02 00 01";
|
||||
|
||||
unsigned char* parsehex(char* str, int* len) {
|
||||
// result can't be longer than input
|
||||
unsigned char* result = malloc(strlen(str));
|
||||
|
||||
unsigned char* p = result;
|
||||
*len = 0;
|
||||
|
||||
while (*str) {
|
||||
int b;
|
||||
|
||||
while (isspace(*str)) str++;
|
||||
|
||||
switch (*str) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
b = (*str - '0') << 4; break;
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
b = (*str - 'a' + 10) << 4; break;
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
b = (*str - 'A' + 10) << 4; break;
|
||||
case '\0':
|
||||
return result;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
str++;
|
||||
|
||||
while (isspace(*str)) str++;
|
||||
|
||||
switch (*str) {
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
b |= *str - '0'; break;
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
b |= *str - 'a' + 10; break;
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
b |= *str - 'A' + 10; break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
str++;
|
||||
|
||||
*p++ = b;
|
||||
++*len;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int arg, char** argv) {
|
||||
|
||||
unsigned char hash_buf[SHA256_DIGEST_SIZE];
|
||||
|
||||
unsigned char* message;
|
||||
int mlen;
|
||||
unsigned char* signature;
|
||||
int slen;
|
||||
|
||||
p256_int hash;
|
||||
p256_int r;
|
||||
p256_int s;
|
||||
|
||||
int success = 1;
|
||||
|
||||
#define CHECK_DSA_SIG(sig, good) do {\
|
||||
message = parsehex(sig, &mlen); \
|
||||
int result = dsa_sig_unpack(message, mlen, &r, &s); \
|
||||
printf(#sig ": %s\n", result ? "good" : "bad"); \
|
||||
success = success && !(good ^ result); \
|
||||
free(message); \
|
||||
} while(0)
|
||||
#define CHECK_GOOD_DSA_SIG(n) do {\
|
||||
CHECK_DSA_SIG(good_dsa_signature_##n, 1); \
|
||||
int result = !memcmp(P256_DIGITS(&good_dsa_signature_##n##_r), P256_DIGITS(&r), \
|
||||
P256_NBYTES); \
|
||||
success = success && result; \
|
||||
printf(" R value %s\n", result ? "good" : "bad"); \
|
||||
result = !memcmp(P256_DIGITS(&good_dsa_signature_##n##_s), P256_DIGITS(&s), \
|
||||
P256_NBYTES); \
|
||||
success = success && result; \
|
||||
printf(" S value %s\n", result ? "good" : "bad"); \
|
||||
} while (0)
|
||||
#define CHECK_BAD_DSA_SIG(n) \
|
||||
CHECK_DSA_SIG(bad_dsa_signature_##n, 0)
|
||||
|
||||
CHECK_GOOD_DSA_SIG(1);
|
||||
|
||||
CHECK_BAD_DSA_SIG(1);
|
||||
CHECK_BAD_DSA_SIG(2);
|
||||
CHECK_BAD_DSA_SIG(3);
|
||||
CHECK_BAD_DSA_SIG(4);
|
||||
CHECK_BAD_DSA_SIG(5);
|
||||
CHECK_BAD_DSA_SIG(6);
|
||||
CHECK_BAD_DSA_SIG(7);
|
||||
|
||||
|
||||
#define TEST_MESSAGE(n) do {\
|
||||
message = parsehex(message_##n, &mlen); \
|
||||
SHA256_hash(message, mlen, hash_buf); \
|
||||
p256_from_bin(hash_buf, &hash); \
|
||||
signature = parsehex(signature_##n, &slen); \
|
||||
int result = dsa_sig_unpack(signature, slen, &r, &s); \
|
||||
if (result) { result = p256_ecdsa_verify(&key_x, &key_y, &hash, &r, &s); } \
|
||||
printf("message %d: %s\n", n, result ? "verified" : "not verified"); \
|
||||
success = success && result; \
|
||||
free(signature); \
|
||||
} while(0)
|
||||
|
||||
TEST_MESSAGE(1);
|
||||
TEST_MESSAGE(2);
|
||||
TEST_MESSAGE(3);
|
||||
|
||||
printf("\n%s\n\n", success ? "PASS" : "FAIL");
|
||||
|
||||
return !success;
|
||||
}
|
|
@ -13,9 +13,10 @@
|
|||
# limitations under the License.
|
||||
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := dumpkey
|
||||
LOCAL_SRC_FILES := DumpPublicKey.java
|
||||
LOCAL_JAR_MANIFEST := DumpPublicKey.mf
|
||||
LOCAL_STATIC_JAVA_LIBRARIES := bouncycastle-host
|
||||
include $(BUILD_HOST_JAVA_LIBRARY)
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package com.android.dumpkey;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.security.cert.CertificateFactory;
|
||||
|
@ -23,7 +25,10 @@ import java.security.cert.X509Certificate;
|
|||
import java.security.KeyStore;
|
||||
import java.security.Key;
|
||||
import java.security.PublicKey;
|
||||
import java.security.Security;
|
||||
import java.security.interfaces.ECPublicKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.ECPoint;
|
||||
|
||||
/**
|
||||
* Command line tool to extract RSA public keys from X.509 certificates
|
||||
|
@ -39,9 +44,8 @@ class DumpPublicKey {
|
|||
* 3: 2048-bit RSA key with e=3 and SHA-256 hash
|
||||
* 4: 2048-bit RSA key with e=65537 and SHA-256 hash
|
||||
* @throws Exception if the key has the wrong size or public exponent
|
||||
|
||||
*/
|
||||
static int check(RSAPublicKey key, boolean useSHA256) throws Exception {
|
||||
static int checkRSA(RSAPublicKey key, boolean useSHA256) throws Exception {
|
||||
BigInteger pubexp = key.getPublicExponent();
|
||||
BigInteger modulus = key.getModulus();
|
||||
int version;
|
||||
|
@ -63,13 +67,43 @@ class DumpPublicKey {
|
|||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key to perform sanity checks on
|
||||
* @return version number of key. Supported versions are:
|
||||
* 5: 256-bit EC key with curve NIST P-256
|
||||
* @throws Exception if the key has the wrong size or public exponent
|
||||
*/
|
||||
static int checkEC(ECPublicKey key) throws Exception {
|
||||
if (key.getParams().getCurve().getField().getFieldSize() != 256) {
|
||||
throw new Exception("Curve must be NIST P-256");
|
||||
}
|
||||
|
||||
return 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform sanity check on public key.
|
||||
*/
|
||||
static int check(PublicKey key, boolean useSHA256) throws Exception {
|
||||
if (key instanceof RSAPublicKey) {
|
||||
return checkRSA((RSAPublicKey) key, useSHA256);
|
||||
} else if (key instanceof ECPublicKey) {
|
||||
if (!useSHA256) {
|
||||
throw new Exception("Must use SHA-256 with EC keys!");
|
||||
}
|
||||
return checkEC((ECPublicKey) key);
|
||||
} else {
|
||||
throw new Exception("Unsupported key class: " + key.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key to output
|
||||
* @return a String representing this public key. If the key is a
|
||||
* version 1 key, the string will be a C initializer; this is
|
||||
* not true for newer key versions.
|
||||
*/
|
||||
static String print(RSAPublicKey key, boolean useSHA256) throws Exception {
|
||||
static String printRSA(RSAPublicKey key, boolean useSHA256) throws Exception {
|
||||
int version = check(key, useSHA256);
|
||||
|
||||
BigInteger N = key.getModulus();
|
||||
|
@ -128,11 +162,78 @@ class DumpPublicKey {
|
|||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key to output
|
||||
* @return a String representing this public key. If the key is a
|
||||
* version 1 key, the string will be a C initializer; this is
|
||||
* not true for newer key versions.
|
||||
*/
|
||||
static String printEC(ECPublicKey key) throws Exception {
|
||||
int version = checkEC(key);
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
result.append("v");
|
||||
result.append(Integer.toString(version));
|
||||
result.append(" ");
|
||||
|
||||
BigInteger X = key.getW().getAffineX();
|
||||
BigInteger Y = key.getW().getAffineY();
|
||||
int nbytes = key.getParams().getCurve().getField().getFieldSize() / 8; // # of 32 bit integers in X coordinate
|
||||
|
||||
result.append("{");
|
||||
result.append(nbytes);
|
||||
|
||||
BigInteger B = BigInteger.valueOf(0x100L); // 2^8
|
||||
|
||||
// Write out Y coordinate as array of characters.
|
||||
result.append(",{");
|
||||
for (int i = 0; i < nbytes; ++i) {
|
||||
long n = X.mod(B).longValue();
|
||||
result.append(n);
|
||||
|
||||
if (i != nbytes - 1) {
|
||||
result.append(",");
|
||||
}
|
||||
|
||||
X = X.divide(B);
|
||||
}
|
||||
result.append("}");
|
||||
|
||||
// Write out Y coordinate as array of characters.
|
||||
result.append(",{");
|
||||
for (int i = 0; i < nbytes; ++i) {
|
||||
long n = Y.mod(B).longValue();
|
||||
result.append(n);
|
||||
|
||||
if (i != nbytes - 1) {
|
||||
result.append(",");
|
||||
}
|
||||
|
||||
Y = Y.divide(B);
|
||||
}
|
||||
result.append("}");
|
||||
|
||||
result.append("}");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
static String print(PublicKey key, boolean useSHA256) throws Exception {
|
||||
if (key instanceof RSAPublicKey) {
|
||||
return printRSA((RSAPublicKey) key, useSHA256);
|
||||
} else if (key instanceof ECPublicKey) {
|
||||
return printEC((ECPublicKey) key);
|
||||
} else {
|
||||
throw new Exception("Unsupported key class: " + key.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 1) {
|
||||
System.err.println("Usage: DumpPublicKey certfile ... > source.c");
|
||||
System.exit(1);
|
||||
}
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
try {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
FileInputStream input = new FileInputStream(args[i]);
|
||||
|
@ -147,7 +248,7 @@ class DumpPublicKey {
|
|||
// anyway. Continue to do so for backwards
|
||||
// compatibility.
|
||||
useSHA256 = false;
|
||||
} else if ("SHA256withRSA".equals(sigAlg)) {
|
||||
} else if ("SHA256withRSA".equals(sigAlg) || "SHA256withECDSA".equals(sigAlg)) {
|
||||
useSHA256 = true;
|
||||
} else {
|
||||
System.err.println(args[i] + ": unsupported signature algorithm \"" +
|
||||
|
@ -155,7 +256,7 @@ class DumpPublicKey {
|
|||
System.exit(1);
|
||||
}
|
||||
|
||||
RSAPublicKey key = (RSAPublicKey) (cert.getPublicKey());
|
||||
PublicKey key = cert.getPublicKey();
|
||||
check(key, useSHA256);
|
||||
System.out.print(print(key, useSHA256));
|
||||
System.out.println(i < args.length - 1 ? "," : "");
|
||||
|
|
Loading…
Reference in New Issue