Version 0.5.0 of tinydtls
Dependents: tinydtls_test_cellular tinydtls_test_ethernet tiny-dtls
ecc/ecc.h@1:598a56fe116e, 2014-02-12 (annotated)
- Committer:
- ashleymills
- Date:
- Wed Feb 12 09:30:16 2014 +0000
- Revision:
- 1:598a56fe116e
- Parent:
- 0:ff9ebe0cf0e9
Explicitly removed something instead of relying on MACRO to disable it. Mbed can't use it.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:ff9ebe0cf0e9 | 1 | /* |
ashleymills | 0:ff9ebe0cf0e9 | 2 | * Copyright (c) 2009 Chris K Cockrum <ckc@cockrum.net> |
ashleymills | 0:ff9ebe0cf0e9 | 3 | * |
ashleymills | 0:ff9ebe0cf0e9 | 4 | * Copyright (c) 2013 Jens Trillmann <jtrillma@tzi.de> |
ashleymills | 0:ff9ebe0cf0e9 | 5 | * Copyright (c) 2013 Marc Müller-Weinhardt <muewei@tzi.de> |
ashleymills | 0:ff9ebe0cf0e9 | 6 | * Copyright (c) 2013 Lars Schmertmann <lars@tzi.de> |
ashleymills | 0:ff9ebe0cf0e9 | 7 | * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de> |
ashleymills | 0:ff9ebe0cf0e9 | 8 | * |
ashleymills | 0:ff9ebe0cf0e9 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
ashleymills | 0:ff9ebe0cf0e9 | 10 | * of this software and associated documentation files (the "Software"), to deal |
ashleymills | 0:ff9ebe0cf0e9 | 11 | * in the Software without restriction, including without limitation the rights |
ashleymills | 0:ff9ebe0cf0e9 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ashleymills | 0:ff9ebe0cf0e9 | 13 | * copies of the Software, and to permit persons to whom the Software is |
ashleymills | 0:ff9ebe0cf0e9 | 14 | * furnished to do so, subject to the following conditions: |
ashleymills | 0:ff9ebe0cf0e9 | 15 | * |
ashleymills | 0:ff9ebe0cf0e9 | 16 | * The above copyright notice and this permission notice shall be included in |
ashleymills | 0:ff9ebe0cf0e9 | 17 | * all copies or substantial portions of the Software. |
ashleymills | 0:ff9ebe0cf0e9 | 18 | * |
ashleymills | 0:ff9ebe0cf0e9 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ashleymills | 0:ff9ebe0cf0e9 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ashleymills | 0:ff9ebe0cf0e9 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ashleymills | 0:ff9ebe0cf0e9 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ashleymills | 0:ff9ebe0cf0e9 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ashleymills | 0:ff9ebe0cf0e9 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ashleymills | 0:ff9ebe0cf0e9 | 25 | * THE SOFTWARE. |
ashleymills | 0:ff9ebe0cf0e9 | 26 | * |
ashleymills | 0:ff9ebe0cf0e9 | 27 | * |
ashleymills | 0:ff9ebe0cf0e9 | 28 | * This implementation is based in part on the paper Implementation of an |
ashleymills | 0:ff9ebe0cf0e9 | 29 | * Elliptic Curve Cryptosystem on an 8-bit Microcontroller [0] by |
ashleymills | 0:ff9ebe0cf0e9 | 30 | * Chris K Cockrum <ckc@cockrum.net>. |
ashleymills | 0:ff9ebe0cf0e9 | 31 | * |
ashleymills | 0:ff9ebe0cf0e9 | 32 | * [0]: http://cockrum.net/Implementation_of_ECC_on_an_8-bit_microcontroller.pdf |
ashleymills | 0:ff9ebe0cf0e9 | 33 | * |
ashleymills | 0:ff9ebe0cf0e9 | 34 | * This is a efficient ECC implementation on the secp256r1 curve for 32 Bit CPU |
ashleymills | 0:ff9ebe0cf0e9 | 35 | * architectures. It provides basic operations on the secp256r1 curve and support |
ashleymills | 0:ff9ebe0cf0e9 | 36 | * for ECDH and ECDSA. |
ashleymills | 0:ff9ebe0cf0e9 | 37 | */ |
ashleymills | 0:ff9ebe0cf0e9 | 38 | #include <inttypes.h> |
ashleymills | 0:ff9ebe0cf0e9 | 39 | |
ashleymills | 0:ff9ebe0cf0e9 | 40 | #define keyLengthInBytes 32 |
ashleymills | 0:ff9ebe0cf0e9 | 41 | #define arrayLength 8 |
ashleymills | 0:ff9ebe0cf0e9 | 42 | |
ashleymills | 0:ff9ebe0cf0e9 | 43 | extern const uint32_t ecc_g_point_x[8]; |
ashleymills | 0:ff9ebe0cf0e9 | 44 | extern const uint32_t ecc_g_point_y[8]; |
ashleymills | 0:ff9ebe0cf0e9 | 45 | |
ashleymills | 0:ff9ebe0cf0e9 | 46 | //ec Functions |
ashleymills | 0:ff9ebe0cf0e9 | 47 | void ecc_ec_mult(const uint32_t *px, const uint32_t *py, const uint32_t *secret, uint32_t *resultx, uint32_t *resulty); |
ashleymills | 0:ff9ebe0cf0e9 | 48 | |
ashleymills | 0:ff9ebe0cf0e9 | 49 | static inline void ecc_ecdh(const uint32_t *px, const uint32_t *py, const uint32_t *secret, uint32_t *resultx, uint32_t *resulty) { |
ashleymills | 0:ff9ebe0cf0e9 | 50 | ecc_ec_mult(px, py, secret, resultx, resulty); |
ashleymills | 0:ff9ebe0cf0e9 | 51 | } |
ashleymills | 0:ff9ebe0cf0e9 | 52 | int ecc_ecdsa_validate(const uint32_t *x, const uint32_t *y, const uint32_t *e, const uint32_t *r, const uint32_t *s); |
ashleymills | 0:ff9ebe0cf0e9 | 53 | int ecc_ecdsa_sign(const uint32_t *d, const uint32_t *e, const uint32_t *k, uint32_t *r, uint32_t *s); |
ashleymills | 0:ff9ebe0cf0e9 | 54 | |
ashleymills | 0:ff9ebe0cf0e9 | 55 | int ecc_is_valid_key(const uint32_t * priv_key); |
ashleymills | 0:ff9ebe0cf0e9 | 56 | static inline void ecc_gen_pub_key(const uint32_t *priv_key, uint32_t *pub_x, uint32_t *pub_y) |
ashleymills | 0:ff9ebe0cf0e9 | 57 | { |
ashleymills | 0:ff9ebe0cf0e9 | 58 | ecc_ec_mult(ecc_g_point_x, ecc_g_point_y, priv_key, pub_x, pub_y); |
ashleymills | 0:ff9ebe0cf0e9 | 59 | } |
ashleymills | 0:ff9ebe0cf0e9 | 60 | |
ashleymills | 0:ff9ebe0cf0e9 | 61 | #ifdef TEST_INCLUDE |
ashleymills | 0:ff9ebe0cf0e9 | 62 | //ec Functions |
ashleymills | 0:ff9ebe0cf0e9 | 63 | void ecc_ec_add(const uint32_t *px, const uint32_t *py, const uint32_t *qx, const uint32_t *qy, uint32_t *Sx, uint32_t *Sy); |
ashleymills | 0:ff9ebe0cf0e9 | 64 | void ecc_ec_double(const uint32_t *px, const uint32_t *py, uint32_t *Dx, uint32_t *Dy); |
ashleymills | 0:ff9ebe0cf0e9 | 65 | |
ashleymills | 0:ff9ebe0cf0e9 | 66 | //simple Functions for addition and substraction of big numbers |
ashleymills | 0:ff9ebe0cf0e9 | 67 | uint32_t ecc_add( const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 68 | uint32_t ecc_sub( const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 69 | |
ashleymills | 0:ff9ebe0cf0e9 | 70 | //field functions for big numbers |
ashleymills | 0:ff9ebe0cf0e9 | 71 | int ecc_fieldAdd(const uint32_t *x, const uint32_t *y, const uint32_t *reducer, uint32_t *result); |
ashleymills | 0:ff9ebe0cf0e9 | 72 | int ecc_fieldSub(const uint32_t *x, const uint32_t *y, const uint32_t *modulus, uint32_t *result); |
ashleymills | 0:ff9ebe0cf0e9 | 73 | int ecc_fieldMult(const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 74 | void ecc_fieldModP(uint32_t *A, const uint32_t *B); |
ashleymills | 0:ff9ebe0cf0e9 | 75 | void ecc_fieldModO(const uint32_t *A, uint32_t *result, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 76 | void ecc_fieldInv(const uint32_t *A, const uint32_t *modulus, const uint32_t *reducer, uint32_t *B); |
ashleymills | 0:ff9ebe0cf0e9 | 77 | |
ashleymills | 0:ff9ebe0cf0e9 | 78 | //simple functions to work with the big numbers |
ashleymills | 0:ff9ebe0cf0e9 | 79 | void ecc_copy(const uint32_t *from, uint32_t *to, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 80 | int ecc_isSame(const uint32_t *A, const uint32_t *B, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 81 | void ecc_setZero(uint32_t *A, const int length); |
ashleymills | 0:ff9ebe0cf0e9 | 82 | int ecc_isOne(const uint32_t* A); |
ashleymills | 0:ff9ebe0cf0e9 | 83 | void ecc_rshift(uint32_t* A); |
ashleymills | 0:ff9ebe0cf0e9 | 84 | int ecc_isGreater(const uint32_t *A, const uint32_t *B, uint8_t length); |
ashleymills | 0:ff9ebe0cf0e9 | 85 | |
ashleymills | 0:ff9ebe0cf0e9 | 86 | #endif /* TEST_INCLUDE */ |