boot; first cut at adding support for signatures with ECSDA using NIST P-256.
diff --git a/boot/bootutil/src/image_ec256.c b/boot/bootutil/src/image_ec256.c
new file mode 100644
index 0000000..b27a70b
--- /dev/null
+++ b/boot/bootutil/src/image_ec256.c
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BOOTUTIL_SIGN_EC256)
+#include "bootutil/sign_key.h"
+
+#include "mbedtls/oid.h"
+#include "mbedtls/asn1.h"
+
+#include "tinycrypt/ecc_dsa.h"
+#include "bootutil_priv.h"
+
+/*
+ * Declaring these like this adds NULL termination.
+ */
+static const uint8_t ec_pubkey_oid[] = MBEDTLS_OID_EC_ALG_UNRESTRICTED;
+static const uint8_t ec_secp256r1_oid[] = MBEDTLS_OID_EC_GRP_SECP256R1;
+
+/*
+ * Parse the public key used for signing.
+ */
+static int
+tinycrypt_import_key(EccPoint *pubkey, uint8_t *cp, uint8_t *end)
+{
+    size_t len;
+    mbedtls_asn1_buf alg;
+    mbedtls_asn1_buf param;
+
+    if (mbedtls_asn1_get_tag(&cp, end, &len,
+        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
+        return -1;
+    }
+    end = cp + len;
+
+    if (mbedtls_asn1_get_alg(&cp, end, &alg, &param)) {
+        return -2;
+    }
+    if (alg.len != sizeof(ec_pubkey_oid) - 1 ||
+        memcmp(alg.p, ec_pubkey_oid, sizeof(ec_pubkey_oid) - 1)) {
+        return -3;
+    }
+    if (param.len != sizeof(ec_secp256r1_oid) - 1 ||
+        memcmp(param.p, ec_secp256r1_oid, sizeof(ec_secp256r1_oid) - 1)) {
+        return -4;
+    }
+    if (mbedtls_asn1_get_bitstring_null(&cp, end, &len)) {
+        return -6;
+    }
+    if (cp + len != end) {
+        return -7;
+    }
+
+    if (len != 2 * NUM_ECC_BYTES + 1) {
+        return -8;
+    }
+    if (cp[0] != 0x04) {
+        return -9;
+    }
+
+    ecc_bytes2native(pubkey->x, cp + 1);
+    ecc_bytes2native(pubkey->y, cp + 1 + NUM_ECC_BYTES);
+
+    return 0;
+}
+
+/*
+ * cp points to ASN1 string containing an integer.
+ * Verify the tag, and that the length is 32 bytes.
+ */
+static int
+tinycrypt_read_bigint(uint32_t i[NUM_ECC_DIGITS], uint8_t **cp, uint8_t *end)
+{
+    size_t len;
+
+    if (mbedtls_asn1_get_tag(cp, end, &len, MBEDTLS_ASN1_INTEGER)) {
+        return -3;
+    }
+
+    for (; *cp < end; *cp = *cp + 1, len--) {
+        if (**cp != 0) {
+            break;
+        }
+    }
+    if (len != NUM_ECC_BYTES) {
+        return -1;
+    }
+    ecc_bytes2native(i, *cp);
+    *cp += len;
+    return 0;
+}
+
+/*
+ * Read in signature. Signature has r and s encoded as integers.
+ */
+static int
+tinycrypt_decode_sig(uint32_t r[NUM_ECC_DIGITS], uint32_t s[NUM_ECC_DIGITS],
+                     uint8_t *cp, uint8_t *end)
+{
+    int rc;
+    size_t len;
+
+    rc = mbedtls_asn1_get_tag(&cp, end, &len,
+                              MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
+    if (rc) {
+        return -1;
+    }
+    if (cp + len != end) {
+        return -2;
+    }
+    rc = tinycrypt_read_bigint(r, &cp, end);
+    if (rc) {
+        return -3;
+    }
+    rc = tinycrypt_read_bigint(s, &cp, end);
+    if (rc) {
+        return -4;
+    }
+    return 0;
+}
+
+int
+bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
+  uint8_t key_id)
+{
+    int rc;
+    uint8_t *cp;
+    uint8_t *end;
+    EccPoint ctx;
+    uint32_t r[NUM_ECC_DIGITS];
+    uint32_t s[NUM_ECC_DIGITS];
+    uint32_t hash_t[NUM_ECC_DIGITS];
+
+    cp = (uint8_t *)bootutil_keys[key_id].key;
+    end = cp + *bootutil_keys[key_id].len;
+
+    rc = tinycrypt_import_key(&ctx, cp, end);
+    if (rc) {
+        return -1;
+    }
+
+    while (sig[slen - 1] == '\0') {
+        slen--;
+    }
+
+    rc = tinycrypt_decode_sig(r, s, sig, sig + slen);
+    if (rc) {
+        return -1;
+    }
+
+    /*
+     * This is simplified, as the hash length is also 32 bytes.
+     */
+    if (hlen != NUM_ECC_BYTES) {
+        return -1;
+    }
+
+    ecc_bytes2native(hash_t, hash);
+    rc = ecdsa_verify(&ctx, hash_t, r, s);
+    if (rc == 1) {
+        return 0;
+    } else {
+        return -2;
+    }
+}
+#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_EC256) */