diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/ecdh/ecdh_kdf.c | 137 |
1 files changed, 72 insertions, 65 deletions
diff --git a/src/lib/libcrypto/ecdh/ecdh_kdf.c b/src/lib/libcrypto/ecdh/ecdh_kdf.c index 3d9081244f..f7ada7b40b 100644 --- a/src/lib/libcrypto/ecdh/ecdh_kdf.c +++ b/src/lib/libcrypto/ecdh/ecdh_kdf.c | |||
| @@ -9,31 +9,31 @@ | |||
| 9 | * are met: | 9 | * are met: |
| 10 | * | 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright | 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. | 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * | 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in | 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the | 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. | 17 | * distribution. |
| 18 | * | 18 | * |
| 19 | * 3. All advertising materials mentioning features or use of this | 19 | * 3. All advertising materials mentioning features or use of this |
| 20 | * software must display the following acknowledgment: | 20 | * software must display the following acknowledgment: |
| 21 | * "This product includes software developed by the OpenSSL Project | 21 | * "This product includes software developed by the OpenSSL Project |
| 22 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | 22 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 23 | * | 23 | * |
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 25 | * endorse or promote products derived from this software without | 25 | * endorse or promote products derived from this software without |
| 26 | * prior written permission. For written permission, please contact | 26 | * prior written permission. For written permission, please contact |
| 27 | * openssl-core@openssl.org. | 27 | * openssl-core@openssl.org. |
| 28 | * | 28 | * |
| 29 | * 5. Products derived from this software may not be called "OpenSSL" | 29 | * 5. Products derived from this software may not be called "OpenSSL" |
| 30 | * nor may "OpenSSL" appear in their names without prior written | 30 | * nor may "OpenSSL" appear in their names without prior written |
| 31 | * permission of the OpenSSL Project. | 31 | * permission of the OpenSSL Project. |
| 32 | * | 32 | * |
| 33 | * 6. Redistributions of any form whatsoever must retain the following | 33 | * 6. Redistributions of any form whatsoever must retain the following |
| 34 | * acknowledgment: | 34 | * acknowledgment: |
| 35 | * "This product includes software developed by the OpenSSL Project | 35 | * "This product includes software developed by the OpenSSL Project |
| 36 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | 36 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 37 | * | 37 | * |
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| @@ -51,62 +51,69 @@ | |||
| 51 | */ | 51 | */ |
| 52 | 52 | ||
| 53 | #include <string.h> | 53 | #include <string.h> |
| 54 | |||
| 54 | #include <openssl/ec.h> | 55 | #include <openssl/ec.h> |
| 55 | #include <openssl/evp.h> | 56 | #include <openssl/evp.h> |
| 57 | |||
| 56 | #include "ec_lcl.h" | 58 | #include "ec_lcl.h" |
| 57 | 59 | ||
| 58 | /* Key derivation function from X9.63/SECG */ | 60 | /* |
| 61 | * Key derivation function from X9.63/SECG. | ||
| 62 | */ | ||
| 63 | |||
| 59 | /* Way more than we will ever need */ | 64 | /* Way more than we will ever need */ |
| 60 | #define ECDH_KDF_MAX (1 << 30) | 65 | #define ECDH_KDF_MAX (1 << 30) |
| 61 | 66 | ||
| 62 | int ecdh_KDF_X9_63(unsigned char *out, size_t outlen, | 67 | int |
| 63 | const unsigned char *Z, size_t Zlen, | 68 | ecdh_KDF_X9_63(unsigned char *out, size_t outlen, const unsigned char *Z, |
| 64 | const unsigned char *sinfo, size_t sinfolen, | 69 | size_t Zlen, const unsigned char *sinfo, size_t sinfolen, const EVP_MD *md) |
| 65 | const EVP_MD *md) | ||
| 66 | { | 70 | { |
| 67 | EVP_MD_CTX *mctx = NULL; | 71 | EVP_MD_CTX *mctx = NULL; |
| 68 | int rv = 0; | 72 | unsigned int i; |
| 69 | unsigned int i; | 73 | size_t mdlen; |
| 70 | size_t mdlen; | 74 | unsigned char ctr[4]; |
| 71 | unsigned char ctr[4]; | 75 | int rv = 0; |
| 72 | if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX | 76 | |
| 73 | || Zlen > ECDH_KDF_MAX) | 77 | if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX || |
| 74 | return 0; | 78 | Zlen > ECDH_KDF_MAX) |
| 75 | mctx = EVP_MD_CTX_new(); | 79 | return 0; |
| 76 | if (mctx == NULL) | 80 | mctx = EVP_MD_CTX_new(); |
| 77 | return 0; | 81 | if (mctx == NULL) |
| 78 | mdlen = EVP_MD_size(md); | 82 | return 0; |
| 79 | for (i = 1;; i++) { | 83 | mdlen = EVP_MD_size(md); |
| 80 | unsigned char mtmp[EVP_MAX_MD_SIZE]; | 84 | for (i = 1;; i++) { |
| 81 | if (!EVP_DigestInit_ex(mctx, md, NULL)) | 85 | unsigned char mtmp[EVP_MAX_MD_SIZE]; |
| 82 | goto err; | 86 | if (!EVP_DigestInit_ex(mctx, md, NULL)) |
| 83 | ctr[3] = i & 0xFF; | 87 | goto err; |
| 84 | ctr[2] = (i >> 8) & 0xFF; | 88 | ctr[3] = i & 0xFF; |
| 85 | ctr[1] = (i >> 16) & 0xFF; | 89 | ctr[2] = (i >> 8) & 0xFF; |
| 86 | ctr[0] = (i >> 24) & 0xFF; | 90 | ctr[1] = (i >> 16) & 0xFF; |
| 87 | if (!EVP_DigestUpdate(mctx, Z, Zlen)) | 91 | ctr[0] = (i >> 24) & 0xFF; |
| 88 | goto err; | 92 | if (!EVP_DigestUpdate(mctx, Z, Zlen)) |
| 89 | if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr))) | 93 | goto err; |
| 90 | goto err; | 94 | if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr))) |
| 91 | if (!EVP_DigestUpdate(mctx, sinfo, sinfolen)) | 95 | goto err; |
| 92 | goto err; | 96 | if (!EVP_DigestUpdate(mctx, sinfo, sinfolen)) |
| 93 | if (outlen >= mdlen) { | 97 | goto err; |
| 94 | if (!EVP_DigestFinal(mctx, out, NULL)) | 98 | if (outlen >= mdlen) { |
| 95 | goto err; | 99 | if (!EVP_DigestFinal(mctx, out, NULL)) |
| 96 | outlen -= mdlen; | 100 | goto err; |
| 97 | if (outlen == 0) | 101 | outlen -= mdlen; |
| 98 | break; | 102 | if (outlen == 0) |
| 99 | out += mdlen; | 103 | break; |
| 100 | } else { | 104 | out += mdlen; |
| 101 | if (!EVP_DigestFinal(mctx, mtmp, NULL)) | 105 | } else { |
| 102 | goto err; | 106 | if (!EVP_DigestFinal(mctx, mtmp, NULL)) |
| 103 | memcpy(out, mtmp, outlen); | 107 | goto err; |
| 104 | OPENSSL_cleanse(mtmp, mdlen); | 108 | memcpy(out, mtmp, outlen); |
| 105 | break; | 109 | OPENSSL_cleanse(mtmp, mdlen); |
| 106 | } | 110 | break; |
| 107 | } | 111 | } |
| 108 | rv = 1; | 112 | } |
| 113 | rv = 1; | ||
| 114 | |||
| 109 | err: | 115 | err: |
| 110 | EVP_MD_CTX_free(mctx); | 116 | EVP_MD_CTX_free(mctx); |
| 111 | return rv; | 117 | |
| 118 | return rv; | ||
| 112 | } | 119 | } |
