mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbedtls/source/ecdsa.c@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Elliptic curve DSA |
mbedAustin | 11:cada08fc8a70 | 3 | * |
mbedAustin | 11:cada08fc8a70 | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
mbedAustin | 11:cada08fc8a70 | 5 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 6 | * |
mbedAustin | 11:cada08fc8a70 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
mbedAustin | 11:cada08fc8a70 | 8 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 9 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 10 | * |
mbedAustin | 11:cada08fc8a70 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 12 | * |
mbedAustin | 11:cada08fc8a70 | 13 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 16 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 17 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 18 | * |
mbedAustin | 11:cada08fc8a70 | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
mbedAustin | 11:cada08fc8a70 | 20 | */ |
mbedAustin | 11:cada08fc8a70 | 21 | |
mbedAustin | 11:cada08fc8a70 | 22 | /* |
mbedAustin | 11:cada08fc8a70 | 23 | * References: |
mbedAustin | 11:cada08fc8a70 | 24 | * |
mbedAustin | 11:cada08fc8a70 | 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
mbedAustin | 11:cada08fc8a70 | 26 | */ |
mbedAustin | 11:cada08fc8a70 | 27 | |
mbedAustin | 11:cada08fc8a70 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
mbedAustin | 11:cada08fc8a70 | 29 | #include "mbedtls/config.h" |
mbedAustin | 11:cada08fc8a70 | 30 | #else |
mbedAustin | 11:cada08fc8a70 | 31 | #include MBEDTLS_CONFIG_FILE |
mbedAustin | 11:cada08fc8a70 | 32 | #endif |
mbedAustin | 11:cada08fc8a70 | 33 | |
mbedAustin | 11:cada08fc8a70 | 34 | #if defined(MBEDTLS_ECDSA_C) |
mbedAustin | 11:cada08fc8a70 | 35 | |
mbedAustin | 11:cada08fc8a70 | 36 | #include "mbedtls/ecdsa.h" |
mbedAustin | 11:cada08fc8a70 | 37 | #include "mbedtls/asn1write.h" |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | #include <string.h> |
mbedAustin | 11:cada08fc8a70 | 40 | |
mbedAustin | 11:cada08fc8a70 | 41 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
mbedAustin | 11:cada08fc8a70 | 42 | #include "mbedtls/hmac_drbg.h" |
mbedAustin | 11:cada08fc8a70 | 43 | #endif |
mbedAustin | 11:cada08fc8a70 | 44 | |
mbedAustin | 11:cada08fc8a70 | 45 | /* |
mbedAustin | 11:cada08fc8a70 | 46 | * Derive a suitable integer for group grp from a buffer of length len |
mbedAustin | 11:cada08fc8a70 | 47 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
mbedAustin | 11:cada08fc8a70 | 48 | */ |
mbedAustin | 11:cada08fc8a70 | 49 | static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x, |
mbedAustin | 11:cada08fc8a70 | 50 | const unsigned char *buf, size_t blen ) |
mbedAustin | 11:cada08fc8a70 | 51 | { |
mbedAustin | 11:cada08fc8a70 | 52 | int ret; |
mbedAustin | 11:cada08fc8a70 | 53 | size_t n_size = ( grp->nbits + 7 ) / 8; |
mbedAustin | 11:cada08fc8a70 | 54 | size_t use_size = blen > n_size ? n_size : blen; |
mbedAustin | 11:cada08fc8a70 | 55 | |
mbedAustin | 11:cada08fc8a70 | 56 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) ); |
mbedAustin | 11:cada08fc8a70 | 57 | if( use_size * 8 > grp->nbits ) |
mbedAustin | 11:cada08fc8a70 | 58 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) ); |
mbedAustin | 11:cada08fc8a70 | 59 | |
mbedAustin | 11:cada08fc8a70 | 60 | /* While at it, reduce modulo N */ |
mbedAustin | 11:cada08fc8a70 | 61 | if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 ) |
mbedAustin | 11:cada08fc8a70 | 62 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 63 | |
mbedAustin | 11:cada08fc8a70 | 64 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 65 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 66 | } |
mbedAustin | 11:cada08fc8a70 | 67 | |
mbedAustin | 11:cada08fc8a70 | 68 | /* |
mbedAustin | 11:cada08fc8a70 | 69 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
mbedAustin | 11:cada08fc8a70 | 70 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
mbedAustin | 11:cada08fc8a70 | 71 | */ |
mbedAustin | 11:cada08fc8a70 | 72 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
mbedAustin | 11:cada08fc8a70 | 73 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
mbedAustin | 11:cada08fc8a70 | 74 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
mbedAustin | 11:cada08fc8a70 | 75 | { |
mbedAustin | 11:cada08fc8a70 | 76 | int ret, key_tries, sign_tries, blind_tries; |
mbedAustin | 11:cada08fc8a70 | 77 | mbedtls_ecp_point R; |
mbedAustin | 11:cada08fc8a70 | 78 | mbedtls_mpi k, e, t; |
mbedAustin | 11:cada08fc8a70 | 79 | |
mbedAustin | 11:cada08fc8a70 | 80 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
mbedAustin | 11:cada08fc8a70 | 81 | if( grp->N.p == NULL ) |
mbedAustin | 11:cada08fc8a70 | 82 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
mbedAustin | 11:cada08fc8a70 | 83 | |
mbedAustin | 11:cada08fc8a70 | 84 | mbedtls_ecp_point_init( &R ); |
mbedAustin | 11:cada08fc8a70 | 85 | mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t ); |
mbedAustin | 11:cada08fc8a70 | 86 | |
mbedAustin | 11:cada08fc8a70 | 87 | sign_tries = 0; |
mbedAustin | 11:cada08fc8a70 | 88 | do |
mbedAustin | 11:cada08fc8a70 | 89 | { |
mbedAustin | 11:cada08fc8a70 | 90 | /* |
mbedAustin | 11:cada08fc8a70 | 91 | * Steps 1-3: generate a suitable ephemeral keypair |
mbedAustin | 11:cada08fc8a70 | 92 | * and set r = xR mod n |
mbedAustin | 11:cada08fc8a70 | 93 | */ |
mbedAustin | 11:cada08fc8a70 | 94 | key_tries = 0; |
mbedAustin | 11:cada08fc8a70 | 95 | do |
mbedAustin | 11:cada08fc8a70 | 96 | { |
mbedAustin | 11:cada08fc8a70 | 97 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
mbedAustin | 11:cada08fc8a70 | 98 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 99 | |
mbedAustin | 11:cada08fc8a70 | 100 | if( key_tries++ > 10 ) |
mbedAustin | 11:cada08fc8a70 | 101 | { |
mbedAustin | 11:cada08fc8a70 | 102 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
mbedAustin | 11:cada08fc8a70 | 103 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 104 | } |
mbedAustin | 11:cada08fc8a70 | 105 | } |
mbedAustin | 11:cada08fc8a70 | 106 | while( mbedtls_mpi_cmp_int( r, 0 ) == 0 ); |
mbedAustin | 11:cada08fc8a70 | 107 | |
mbedAustin | 11:cada08fc8a70 | 108 | /* |
mbedAustin | 11:cada08fc8a70 | 109 | * Step 5: derive MPI from hashed message |
mbedAustin | 11:cada08fc8a70 | 110 | */ |
mbedAustin | 11:cada08fc8a70 | 111 | MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
mbedAustin | 11:cada08fc8a70 | 112 | |
mbedAustin | 11:cada08fc8a70 | 113 | /* |
mbedAustin | 11:cada08fc8a70 | 114 | * Generate a random value to blind inv_mod in next step, |
mbedAustin | 11:cada08fc8a70 | 115 | * avoiding a potential timing leak. |
mbedAustin | 11:cada08fc8a70 | 116 | */ |
mbedAustin | 11:cada08fc8a70 | 117 | blind_tries = 0; |
mbedAustin | 11:cada08fc8a70 | 118 | do |
mbedAustin | 11:cada08fc8a70 | 119 | { |
mbedAustin | 11:cada08fc8a70 | 120 | size_t n_size = ( grp->nbits + 7 ) / 8; |
mbedAustin | 11:cada08fc8a70 | 121 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) ); |
mbedAustin | 11:cada08fc8a70 | 122 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) ); |
mbedAustin | 11:cada08fc8a70 | 123 | |
mbedAustin | 11:cada08fc8a70 | 124 | /* See mbedtls_ecp_gen_keypair() */ |
mbedAustin | 11:cada08fc8a70 | 125 | if( ++blind_tries > 30 ) |
mbedAustin | 11:cada08fc8a70 | 126 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
mbedAustin | 11:cada08fc8a70 | 127 | } |
mbedAustin | 11:cada08fc8a70 | 128 | while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 || |
mbedAustin | 11:cada08fc8a70 | 129 | mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 ); |
mbedAustin | 11:cada08fc8a70 | 130 | |
mbedAustin | 11:cada08fc8a70 | 131 | /* |
mbedAustin | 11:cada08fc8a70 | 132 | * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n |
mbedAustin | 11:cada08fc8a70 | 133 | */ |
mbedAustin | 11:cada08fc8a70 | 134 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) ); |
mbedAustin | 11:cada08fc8a70 | 135 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) ); |
mbedAustin | 11:cada08fc8a70 | 136 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) ); |
mbedAustin | 11:cada08fc8a70 | 137 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) ); |
mbedAustin | 11:cada08fc8a70 | 138 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 139 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) ); |
mbedAustin | 11:cada08fc8a70 | 140 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 141 | |
mbedAustin | 11:cada08fc8a70 | 142 | if( sign_tries++ > 10 ) |
mbedAustin | 11:cada08fc8a70 | 143 | { |
mbedAustin | 11:cada08fc8a70 | 144 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
mbedAustin | 11:cada08fc8a70 | 145 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 146 | } |
mbedAustin | 11:cada08fc8a70 | 147 | } |
mbedAustin | 11:cada08fc8a70 | 148 | while( mbedtls_mpi_cmp_int( s, 0 ) == 0 ); |
mbedAustin | 11:cada08fc8a70 | 149 | |
mbedAustin | 11:cada08fc8a70 | 150 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 151 | mbedtls_ecp_point_free( &R ); |
mbedAustin | 11:cada08fc8a70 | 152 | mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t ); |
mbedAustin | 11:cada08fc8a70 | 153 | |
mbedAustin | 11:cada08fc8a70 | 154 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 155 | } |
mbedAustin | 11:cada08fc8a70 | 156 | |
mbedAustin | 11:cada08fc8a70 | 157 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
mbedAustin | 11:cada08fc8a70 | 158 | /* |
mbedAustin | 11:cada08fc8a70 | 159 | * Deterministic signature wrapper |
mbedAustin | 11:cada08fc8a70 | 160 | */ |
mbedAustin | 11:cada08fc8a70 | 161 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
mbedAustin | 11:cada08fc8a70 | 162 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
mbedAustin | 11:cada08fc8a70 | 163 | mbedtls_md_type_t md_alg ) |
mbedAustin | 11:cada08fc8a70 | 164 | { |
mbedAustin | 11:cada08fc8a70 | 165 | int ret; |
mbedAustin | 11:cada08fc8a70 | 166 | mbedtls_hmac_drbg_context rng_ctx; |
mbedAustin | 11:cada08fc8a70 | 167 | unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; |
mbedAustin | 11:cada08fc8a70 | 168 | size_t grp_len = ( grp->nbits + 7 ) / 8; |
mbedAustin | 11:cada08fc8a70 | 169 | const mbedtls_md_info_t *md_info; |
mbedAustin | 11:cada08fc8a70 | 170 | mbedtls_mpi h; |
mbedAustin | 11:cada08fc8a70 | 171 | |
mbedAustin | 11:cada08fc8a70 | 172 | if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) |
mbedAustin | 11:cada08fc8a70 | 173 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
mbedAustin | 11:cada08fc8a70 | 174 | |
mbedAustin | 11:cada08fc8a70 | 175 | mbedtls_mpi_init( &h ); |
mbedAustin | 11:cada08fc8a70 | 176 | mbedtls_hmac_drbg_init( &rng_ctx ); |
mbedAustin | 11:cada08fc8a70 | 177 | |
mbedAustin | 11:cada08fc8a70 | 178 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
mbedAustin | 11:cada08fc8a70 | 179 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) ); |
mbedAustin | 11:cada08fc8a70 | 180 | MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); |
mbedAustin | 11:cada08fc8a70 | 181 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) ); |
mbedAustin | 11:cada08fc8a70 | 182 | mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len ); |
mbedAustin | 11:cada08fc8a70 | 183 | |
mbedAustin | 11:cada08fc8a70 | 184 | ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen, |
mbedAustin | 11:cada08fc8a70 | 185 | mbedtls_hmac_drbg_random, &rng_ctx ); |
mbedAustin | 11:cada08fc8a70 | 186 | |
mbedAustin | 11:cada08fc8a70 | 187 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 188 | mbedtls_hmac_drbg_free( &rng_ctx ); |
mbedAustin | 11:cada08fc8a70 | 189 | mbedtls_mpi_free( &h ); |
mbedAustin | 11:cada08fc8a70 | 190 | |
mbedAustin | 11:cada08fc8a70 | 191 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 192 | } |
mbedAustin | 11:cada08fc8a70 | 193 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
mbedAustin | 11:cada08fc8a70 | 194 | |
mbedAustin | 11:cada08fc8a70 | 195 | /* |
mbedAustin | 11:cada08fc8a70 | 196 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
mbedAustin | 11:cada08fc8a70 | 197 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
mbedAustin | 11:cada08fc8a70 | 198 | */ |
mbedAustin | 11:cada08fc8a70 | 199 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
mbedAustin | 11:cada08fc8a70 | 200 | const unsigned char *buf, size_t blen, |
mbedAustin | 11:cada08fc8a70 | 201 | const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) |
mbedAustin | 11:cada08fc8a70 | 202 | { |
mbedAustin | 11:cada08fc8a70 | 203 | int ret; |
mbedAustin | 11:cada08fc8a70 | 204 | mbedtls_mpi e, s_inv, u1, u2; |
mbedAustin | 11:cada08fc8a70 | 205 | mbedtls_ecp_point R; |
mbedAustin | 11:cada08fc8a70 | 206 | |
mbedAustin | 11:cada08fc8a70 | 207 | mbedtls_ecp_point_init( &R ); |
mbedAustin | 11:cada08fc8a70 | 208 | mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 ); |
mbedAustin | 11:cada08fc8a70 | 209 | |
mbedAustin | 11:cada08fc8a70 | 210 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
mbedAustin | 11:cada08fc8a70 | 211 | if( grp->N.p == NULL ) |
mbedAustin | 11:cada08fc8a70 | 212 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
mbedAustin | 11:cada08fc8a70 | 213 | |
mbedAustin | 11:cada08fc8a70 | 214 | /* |
mbedAustin | 11:cada08fc8a70 | 215 | * Step 1: make sure r and s are in range 1..n-1 |
mbedAustin | 11:cada08fc8a70 | 216 | */ |
mbedAustin | 11:cada08fc8a70 | 217 | if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 || |
mbedAustin | 11:cada08fc8a70 | 218 | mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
mbedAustin | 11:cada08fc8a70 | 219 | { |
mbedAustin | 11:cada08fc8a70 | 220 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
mbedAustin | 11:cada08fc8a70 | 221 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 222 | } |
mbedAustin | 11:cada08fc8a70 | 223 | |
mbedAustin | 11:cada08fc8a70 | 224 | /* |
mbedAustin | 11:cada08fc8a70 | 225 | * Additional precaution: make sure Q is valid |
mbedAustin | 11:cada08fc8a70 | 226 | */ |
mbedAustin | 11:cada08fc8a70 | 227 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); |
mbedAustin | 11:cada08fc8a70 | 228 | |
mbedAustin | 11:cada08fc8a70 | 229 | /* |
mbedAustin | 11:cada08fc8a70 | 230 | * Step 3: derive MPI from hashed message |
mbedAustin | 11:cada08fc8a70 | 231 | */ |
mbedAustin | 11:cada08fc8a70 | 232 | MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
mbedAustin | 11:cada08fc8a70 | 233 | |
mbedAustin | 11:cada08fc8a70 | 234 | /* |
mbedAustin | 11:cada08fc8a70 | 235 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
mbedAustin | 11:cada08fc8a70 | 236 | */ |
mbedAustin | 11:cada08fc8a70 | 237 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 238 | |
mbedAustin | 11:cada08fc8a70 | 239 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) ); |
mbedAustin | 11:cada08fc8a70 | 240 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 241 | |
mbedAustin | 11:cada08fc8a70 | 242 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) ); |
mbedAustin | 11:cada08fc8a70 | 243 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 244 | |
mbedAustin | 11:cada08fc8a70 | 245 | /* |
mbedAustin | 11:cada08fc8a70 | 246 | * Step 5: R = u1 G + u2 Q |
mbedAustin | 11:cada08fc8a70 | 247 | * |
mbedAustin | 11:cada08fc8a70 | 248 | * Since we're not using any secret data, no need to pass a RNG to |
mbedAustin | 11:cada08fc8a70 | 249 | * mbedtls_ecp_mul() for countermesures. |
mbedAustin | 11:cada08fc8a70 | 250 | */ |
mbedAustin | 11:cada08fc8a70 | 251 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) ); |
mbedAustin | 11:cada08fc8a70 | 252 | |
mbedAustin | 11:cada08fc8a70 | 253 | if( mbedtls_ecp_is_zero( &R ) ) |
mbedAustin | 11:cada08fc8a70 | 254 | { |
mbedAustin | 11:cada08fc8a70 | 255 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
mbedAustin | 11:cada08fc8a70 | 256 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 257 | } |
mbedAustin | 11:cada08fc8a70 | 258 | |
mbedAustin | 11:cada08fc8a70 | 259 | /* |
mbedAustin | 11:cada08fc8a70 | 260 | * Step 6: convert xR to an integer (no-op) |
mbedAustin | 11:cada08fc8a70 | 261 | * Step 7: reduce xR mod n (gives v) |
mbedAustin | 11:cada08fc8a70 | 262 | */ |
mbedAustin | 11:cada08fc8a70 | 263 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); |
mbedAustin | 11:cada08fc8a70 | 264 | |
mbedAustin | 11:cada08fc8a70 | 265 | /* |
mbedAustin | 11:cada08fc8a70 | 266 | * Step 8: check if v (that is, R.X) is equal to r |
mbedAustin | 11:cada08fc8a70 | 267 | */ |
mbedAustin | 11:cada08fc8a70 | 268 | if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 269 | { |
mbedAustin | 11:cada08fc8a70 | 270 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
mbedAustin | 11:cada08fc8a70 | 271 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 272 | } |
mbedAustin | 11:cada08fc8a70 | 273 | |
mbedAustin | 11:cada08fc8a70 | 274 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 275 | mbedtls_ecp_point_free( &R ); |
mbedAustin | 11:cada08fc8a70 | 276 | mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 ); |
mbedAustin | 11:cada08fc8a70 | 277 | |
mbedAustin | 11:cada08fc8a70 | 278 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 279 | } |
mbedAustin | 11:cada08fc8a70 | 280 | |
mbedAustin | 11:cada08fc8a70 | 281 | /* |
mbedAustin | 11:cada08fc8a70 | 282 | * Convert a signature (given by context) to ASN.1 |
mbedAustin | 11:cada08fc8a70 | 283 | */ |
mbedAustin | 11:cada08fc8a70 | 284 | static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s, |
mbedAustin | 11:cada08fc8a70 | 285 | unsigned char *sig, size_t *slen ) |
mbedAustin | 11:cada08fc8a70 | 286 | { |
mbedAustin | 11:cada08fc8a70 | 287 | int ret; |
mbedAustin | 11:cada08fc8a70 | 288 | unsigned char buf[MBEDTLS_ECDSA_MAX_LEN]; |
mbedAustin | 11:cada08fc8a70 | 289 | unsigned char *p = buf + sizeof( buf ); |
mbedAustin | 11:cada08fc8a70 | 290 | size_t len = 0; |
mbedAustin | 11:cada08fc8a70 | 291 | |
mbedAustin | 11:cada08fc8a70 | 292 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) ); |
mbedAustin | 11:cada08fc8a70 | 293 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) ); |
mbedAustin | 11:cada08fc8a70 | 294 | |
mbedAustin | 11:cada08fc8a70 | 295 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) ); |
mbedAustin | 11:cada08fc8a70 | 296 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf, |
mbedAustin | 11:cada08fc8a70 | 297 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
mbedAustin | 11:cada08fc8a70 | 298 | |
mbedAustin | 11:cada08fc8a70 | 299 | memcpy( sig, p, len ); |
mbedAustin | 11:cada08fc8a70 | 300 | *slen = len; |
mbedAustin | 11:cada08fc8a70 | 301 | |
mbedAustin | 11:cada08fc8a70 | 302 | return( 0 ); |
mbedAustin | 11:cada08fc8a70 | 303 | } |
mbedAustin | 11:cada08fc8a70 | 304 | |
mbedAustin | 11:cada08fc8a70 | 305 | /* |
mbedAustin | 11:cada08fc8a70 | 306 | * Compute and write signature |
mbedAustin | 11:cada08fc8a70 | 307 | */ |
mbedAustin | 11:cada08fc8a70 | 308 | int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, |
mbedAustin | 11:cada08fc8a70 | 309 | const unsigned char *hash, size_t hlen, |
mbedAustin | 11:cada08fc8a70 | 310 | unsigned char *sig, size_t *slen, |
mbedAustin | 11:cada08fc8a70 | 311 | int (*f_rng)(void *, unsigned char *, size_t), |
mbedAustin | 11:cada08fc8a70 | 312 | void *p_rng ) |
mbedAustin | 11:cada08fc8a70 | 313 | { |
mbedAustin | 11:cada08fc8a70 | 314 | int ret; |
mbedAustin | 11:cada08fc8a70 | 315 | mbedtls_mpi r, s; |
mbedAustin | 11:cada08fc8a70 | 316 | |
mbedAustin | 11:cada08fc8a70 | 317 | mbedtls_mpi_init( &r ); |
mbedAustin | 11:cada08fc8a70 | 318 | mbedtls_mpi_init( &s ); |
mbedAustin | 11:cada08fc8a70 | 319 | |
mbedAustin | 11:cada08fc8a70 | 320 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
mbedAustin | 11:cada08fc8a70 | 321 | (void) f_rng; |
mbedAustin | 11:cada08fc8a70 | 322 | (void) p_rng; |
mbedAustin | 11:cada08fc8a70 | 323 | |
mbedAustin | 11:cada08fc8a70 | 324 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d, |
mbedAustin | 11:cada08fc8a70 | 325 | hash, hlen, md_alg ) ); |
mbedAustin | 11:cada08fc8a70 | 326 | #else |
mbedAustin | 11:cada08fc8a70 | 327 | (void) md_alg; |
mbedAustin | 11:cada08fc8a70 | 328 | |
mbedAustin | 11:cada08fc8a70 | 329 | MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d, |
mbedAustin | 11:cada08fc8a70 | 330 | hash, hlen, f_rng, p_rng ) ); |
mbedAustin | 11:cada08fc8a70 | 331 | #endif |
mbedAustin | 11:cada08fc8a70 | 332 | |
mbedAustin | 11:cada08fc8a70 | 333 | MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) ); |
mbedAustin | 11:cada08fc8a70 | 334 | |
mbedAustin | 11:cada08fc8a70 | 335 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 336 | mbedtls_mpi_free( &r ); |
mbedAustin | 11:cada08fc8a70 | 337 | mbedtls_mpi_free( &s ); |
mbedAustin | 11:cada08fc8a70 | 338 | |
mbedAustin | 11:cada08fc8a70 | 339 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 340 | } |
mbedAustin | 11:cada08fc8a70 | 341 | |
mbedAustin | 11:cada08fc8a70 | 342 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \ |
mbedAustin | 11:cada08fc8a70 | 343 | defined(MBEDTLS_ECDSA_DETERMINISTIC) |
mbedAustin | 11:cada08fc8a70 | 344 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 345 | const unsigned char *hash, size_t hlen, |
mbedAustin | 11:cada08fc8a70 | 346 | unsigned char *sig, size_t *slen, |
mbedAustin | 11:cada08fc8a70 | 347 | mbedtls_md_type_t md_alg ) |
mbedAustin | 11:cada08fc8a70 | 348 | { |
mbedAustin | 11:cada08fc8a70 | 349 | return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, |
mbedAustin | 11:cada08fc8a70 | 350 | NULL, NULL ) ); |
mbedAustin | 11:cada08fc8a70 | 351 | } |
mbedAustin | 11:cada08fc8a70 | 352 | #endif |
mbedAustin | 11:cada08fc8a70 | 353 | |
mbedAustin | 11:cada08fc8a70 | 354 | /* |
mbedAustin | 11:cada08fc8a70 | 355 | * Read and check signature |
mbedAustin | 11:cada08fc8a70 | 356 | */ |
mbedAustin | 11:cada08fc8a70 | 357 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
mbedAustin | 11:cada08fc8a70 | 358 | const unsigned char *hash, size_t hlen, |
mbedAustin | 11:cada08fc8a70 | 359 | const unsigned char *sig, size_t slen ) |
mbedAustin | 11:cada08fc8a70 | 360 | { |
mbedAustin | 11:cada08fc8a70 | 361 | int ret; |
mbedAustin | 11:cada08fc8a70 | 362 | unsigned char *p = (unsigned char *) sig; |
mbedAustin | 11:cada08fc8a70 | 363 | const unsigned char *end = sig + slen; |
mbedAustin | 11:cada08fc8a70 | 364 | size_t len; |
mbedAustin | 11:cada08fc8a70 | 365 | mbedtls_mpi r, s; |
mbedAustin | 11:cada08fc8a70 | 366 | |
mbedAustin | 11:cada08fc8a70 | 367 | mbedtls_mpi_init( &r ); |
mbedAustin | 11:cada08fc8a70 | 368 | mbedtls_mpi_init( &s ); |
mbedAustin | 11:cada08fc8a70 | 369 | |
mbedAustin | 11:cada08fc8a70 | 370 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
mbedAustin | 11:cada08fc8a70 | 371 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 372 | { |
mbedAustin | 11:cada08fc8a70 | 373 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
mbedAustin | 11:cada08fc8a70 | 374 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 375 | } |
mbedAustin | 11:cada08fc8a70 | 376 | |
mbedAustin | 11:cada08fc8a70 | 377 | if( p + len != end ) |
mbedAustin | 11:cada08fc8a70 | 378 | { |
mbedAustin | 11:cada08fc8a70 | 379 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA + |
mbedAustin | 11:cada08fc8a70 | 380 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
mbedAustin | 11:cada08fc8a70 | 381 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 382 | } |
mbedAustin | 11:cada08fc8a70 | 383 | |
mbedAustin | 11:cada08fc8a70 | 384 | if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 || |
mbedAustin | 11:cada08fc8a70 | 385 | ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 386 | { |
mbedAustin | 11:cada08fc8a70 | 387 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
mbedAustin | 11:cada08fc8a70 | 388 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 389 | } |
mbedAustin | 11:cada08fc8a70 | 390 | |
mbedAustin | 11:cada08fc8a70 | 391 | if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen, |
mbedAustin | 11:cada08fc8a70 | 392 | &ctx->Q, &r, &s ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 393 | goto cleanup; |
mbedAustin | 11:cada08fc8a70 | 394 | |
mbedAustin | 11:cada08fc8a70 | 395 | if( p != end ) |
mbedAustin | 11:cada08fc8a70 | 396 | ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; |
mbedAustin | 11:cada08fc8a70 | 397 | |
mbedAustin | 11:cada08fc8a70 | 398 | cleanup: |
mbedAustin | 11:cada08fc8a70 | 399 | mbedtls_mpi_free( &r ); |
mbedAustin | 11:cada08fc8a70 | 400 | mbedtls_mpi_free( &s ); |
mbedAustin | 11:cada08fc8a70 | 401 | |
mbedAustin | 11:cada08fc8a70 | 402 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 403 | } |
mbedAustin | 11:cada08fc8a70 | 404 | |
mbedAustin | 11:cada08fc8a70 | 405 | /* |
mbedAustin | 11:cada08fc8a70 | 406 | * Generate key pair |
mbedAustin | 11:cada08fc8a70 | 407 | */ |
mbedAustin | 11:cada08fc8a70 | 408 | int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
mbedAustin | 11:cada08fc8a70 | 409 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
mbedAustin | 11:cada08fc8a70 | 410 | { |
mbedAustin | 11:cada08fc8a70 | 411 | return( mbedtls_ecp_group_load( &ctx->grp, gid ) || |
mbedAustin | 11:cada08fc8a70 | 412 | mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); |
mbedAustin | 11:cada08fc8a70 | 413 | } |
mbedAustin | 11:cada08fc8a70 | 414 | |
mbedAustin | 11:cada08fc8a70 | 415 | /* |
mbedAustin | 11:cada08fc8a70 | 416 | * Set context from an mbedtls_ecp_keypair |
mbedAustin | 11:cada08fc8a70 | 417 | */ |
mbedAustin | 11:cada08fc8a70 | 418 | int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) |
mbedAustin | 11:cada08fc8a70 | 419 | { |
mbedAustin | 11:cada08fc8a70 | 420 | int ret; |
mbedAustin | 11:cada08fc8a70 | 421 | |
mbedAustin | 11:cada08fc8a70 | 422 | if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || |
mbedAustin | 11:cada08fc8a70 | 423 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || |
mbedAustin | 11:cada08fc8a70 | 424 | ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) |
mbedAustin | 11:cada08fc8a70 | 425 | { |
mbedAustin | 11:cada08fc8a70 | 426 | mbedtls_ecdsa_free( ctx ); |
mbedAustin | 11:cada08fc8a70 | 427 | } |
mbedAustin | 11:cada08fc8a70 | 428 | |
mbedAustin | 11:cada08fc8a70 | 429 | return( ret ); |
mbedAustin | 11:cada08fc8a70 | 430 | } |
mbedAustin | 11:cada08fc8a70 | 431 | |
mbedAustin | 11:cada08fc8a70 | 432 | /* |
mbedAustin | 11:cada08fc8a70 | 433 | * Initialize context |
mbedAustin | 11:cada08fc8a70 | 434 | */ |
mbedAustin | 11:cada08fc8a70 | 435 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 436 | { |
mbedAustin | 11:cada08fc8a70 | 437 | mbedtls_ecp_keypair_init( ctx ); |
mbedAustin | 11:cada08fc8a70 | 438 | } |
mbedAustin | 11:cada08fc8a70 | 439 | |
mbedAustin | 11:cada08fc8a70 | 440 | /* |
mbedAustin | 11:cada08fc8a70 | 441 | * Free context |
mbedAustin | 11:cada08fc8a70 | 442 | */ |
mbedAustin | 11:cada08fc8a70 | 443 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) |
mbedAustin | 11:cada08fc8a70 | 444 | { |
mbedAustin | 11:cada08fc8a70 | 445 | mbedtls_ecp_keypair_free( ctx ); |
mbedAustin | 11:cada08fc8a70 | 446 | } |
mbedAustin | 11:cada08fc8a70 | 447 | |
mbedAustin | 11:cada08fc8a70 | 448 | #endif /* MBEDTLS_ECDSA_C */ |