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