2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2018-03-13 16:33:44 +08:00
|
|
|
* Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
* Copyright (c) 2004, EdelKey Project. All Rights Reserved.
|
2011-03-13 01:01:19 +08:00
|
|
|
*
|
2018-12-06 20:56:57 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2018-03-13 16:33:44 +08:00
|
|
|
*
|
|
|
|
* Originally written by Christophe Renou and Peter Sylvester,
|
|
|
|
* for the EdelKey project.
|
2011-03-13 01:01:19 +08:00
|
|
|
*/
|
2016-05-18 03:38:09 +08:00
|
|
|
|
2011-03-13 01:01:19 +08:00
|
|
|
#ifndef OPENSSL_NO_SRP
|
2015-05-14 22:56:48 +08:00
|
|
|
# include "internal/cryptlib.h"
|
2015-02-04 00:20:56 +08:00
|
|
|
# include <openssl/sha.h>
|
2015-01-22 11:40:55 +08:00
|
|
|
# include <openssl/srp.h>
|
|
|
|
# include <openssl/evp.h>
|
|
|
|
# include "internal/bn_srp.h"
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-09-30 02:24:26 +08:00
|
|
|
/* calculate = SHA1(PAD(x) || PAD(y)) */
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-09-30 02:24:26 +08:00
|
|
|
static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char digest[SHA_DIGEST_LENGTH];
|
2016-08-23 06:18:50 +08:00
|
|
|
unsigned char *tmp = NULL;
|
2016-09-30 02:24:26 +08:00
|
|
|
int numN = BN_num_bytes(N);
|
2015-11-27 21:02:12 +08:00
|
|
|
BIGNUM *res = NULL;
|
2019-01-14 19:22:42 +08:00
|
|
|
|
2016-09-30 02:24:26 +08:00
|
|
|
if (x != N && BN_ucmp(x, N) >= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2016-09-30 02:24:26 +08:00
|
|
|
if (y != N && BN_ucmp(y, N) >= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2016-09-30 02:24:26 +08:00
|
|
|
if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
|
2016-06-18 22:46:13 +08:00
|
|
|
goto err;
|
2016-09-30 02:24:26 +08:00
|
|
|
if (BN_bn2binpad(x, tmp, numN) < 0
|
|
|
|
|| BN_bn2binpad(y, tmp + numN, numN) < 0
|
|
|
|
|| !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))
|
2016-06-18 22:46:13 +08:00
|
|
|
goto err;
|
2015-11-27 21:02:12 +08:00
|
|
|
res = BN_bin2bn(digest, sizeof(digest), NULL);
|
|
|
|
err:
|
2016-08-23 06:18:50 +08:00
|
|
|
OPENSSL_free(tmp);
|
2015-11-27 21:02:12 +08:00
|
|
|
return res;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-09-30 02:24:26 +08:00
|
|
|
static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
|
|
|
|
{
|
2018-10-26 06:43:35 +08:00
|
|
|
/* k = SHA1(N | PAD(g)) -- tls-srp RFC 5054 */
|
2016-09-30 02:24:26 +08:00
|
|
|
return srp_Calc_xy(N, g, N);
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2018-10-26 06:43:35 +08:00
|
|
|
/* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
|
2016-09-30 02:24:26 +08:00
|
|
|
return srp_Calc_xy(A, B, N);
|
2011-03-13 01:01:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
|
|
|
|
const BIGNUM *b, const BIGNUM *N)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIGNUM *tmp = NULL, *S = NULL;
|
|
|
|
BN_CTX *bn_ctx;
|
|
|
|
|
|
|
|
if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2016-04-25 23:22:31 +08:00
|
|
|
if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* S = (A*v**u) ** b */
|
|
|
|
|
|
|
|
if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
|
|
|
|
goto err;
|
2016-04-25 23:22:31 +08:00
|
|
|
|
|
|
|
S = BN_new();
|
|
|
|
if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
|
|
|
|
BN_free(S);
|
|
|
|
S = NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
BN_CTX_free(bn_ctx);
|
|
|
|
BN_clear_free(tmp);
|
|
|
|
return S;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
|
|
|
|
const BIGNUM *v)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIGNUM *kv = NULL, *gb = NULL;
|
|
|
|
BIGNUM *B = NULL, *k = NULL;
|
|
|
|
BN_CTX *bn_ctx;
|
|
|
|
|
|
|
|
if (b == NULL || N == NULL || g == NULL || v == NULL ||
|
|
|
|
(bn_ctx = BN_CTX_new()) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((kv = BN_new()) == NULL ||
|
|
|
|
(gb = BN_new()) == NULL || (B = BN_new()) == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* B = g**b + k*v */
|
|
|
|
|
2015-05-07 01:43:59 +08:00
|
|
|
if (!BN_mod_exp(gb, g, b, N, bn_ctx)
|
|
|
|
|| (k = srp_Calc_k(N, g)) == NULL
|
|
|
|
|| !BN_mod_mul(kv, v, k, N, bn_ctx)
|
|
|
|
|| !BN_mod_add(B, gb, kv, N, bn_ctx)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_free(B);
|
|
|
|
B = NULL;
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
BN_CTX_free(bn_ctx);
|
|
|
|
BN_clear_free(kv);
|
|
|
|
BN_clear_free(gb);
|
|
|
|
BN_free(k);
|
|
|
|
return B;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char dig[SHA_DIGEST_LENGTH];
|
2015-11-27 21:02:12 +08:00
|
|
|
EVP_MD_CTX *ctxt;
|
2016-08-23 06:23:31 +08:00
|
|
|
unsigned char *cs = NULL;
|
2015-11-27 21:02:12 +08:00
|
|
|
BIGNUM *res = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
if ((s == NULL) || (user == NULL) || (pass == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-02 07:49:35 +08:00
|
|
|
ctxt = EVP_MD_CTX_new();
|
2015-11-27 21:02:12 +08:00
|
|
|
if (ctxt == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2015-11-27 21:02:12 +08:00
|
|
|
if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2016-06-18 22:46:13 +08:00
|
|
|
if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
|
|
|
|
|| !EVP_DigestUpdate(ctxt, user, strlen(user))
|
|
|
|
|| !EVP_DigestUpdate(ctxt, ":", 1)
|
|
|
|
|| !EVP_DigestUpdate(ctxt, pass, strlen(pass))
|
|
|
|
|| !EVP_DigestFinal_ex(ctxt, dig, NULL)
|
|
|
|
|| !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
|
|
|
|
goto err;
|
2019-01-14 19:22:42 +08:00
|
|
|
if (BN_bn2bin(s, cs) < 0)
|
|
|
|
goto err;
|
2016-06-18 22:46:13 +08:00
|
|
|
if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
|
|
|
|
goto err;
|
2016-08-23 06:23:31 +08:00
|
|
|
|
2016-06-18 22:46:13 +08:00
|
|
|
if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
|
|
|
|
|| !EVP_DigestFinal_ex(ctxt, dig, NULL))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
res = BN_bin2bn(dig, sizeof(dig), NULL);
|
2016-08-23 06:23:31 +08:00
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
err:
|
2016-08-23 06:23:31 +08:00
|
|
|
OPENSSL_free(cs);
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(ctxt);
|
2015-11-27 21:02:12 +08:00
|
|
|
return res;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BN_CTX *bn_ctx;
|
|
|
|
BIGNUM *A = NULL;
|
|
|
|
|
2015-05-01 09:37:06 +08:00
|
|
|
if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
|
|
|
|
BN_free(A);
|
|
|
|
A = NULL;
|
|
|
|
}
|
|
|
|
BN_CTX_free(bn_ctx);
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
|
|
|
|
const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
|
|
|
|
BN_CTX *bn_ctx;
|
|
|
|
|
|
|
|
if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
|
|
|
|
|| a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((tmp = BN_new()) == NULL ||
|
|
|
|
(tmp2 = BN_new()) == NULL ||
|
2016-04-25 23:22:31 +08:00
|
|
|
(tmp3 = BN_new()) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
|
|
|
|
goto err;
|
2015-05-07 01:43:59 +08:00
|
|
|
if ((k = srp_Calc_k(N, g)) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
|
|
|
|
goto err;
|
|
|
|
if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
|
|
|
|
goto err;
|
2016-05-01 21:59:43 +08:00
|
|
|
if (!BN_mul(tmp3, u, x, bn_ctx))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-05-01 21:59:43 +08:00
|
|
|
if (!BN_add(tmp2, a, tmp3))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2016-04-25 23:22:31 +08:00
|
|
|
K = BN_new();
|
|
|
|
if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
|
|
|
|
BN_free(K);
|
|
|
|
K = NULL;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
err:
|
|
|
|
BN_CTX_free(bn_ctx);
|
|
|
|
BN_clear_free(tmp);
|
|
|
|
BN_clear_free(tmp2);
|
|
|
|
BN_clear_free(tmp3);
|
|
|
|
BN_free(k);
|
|
|
|
return K;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIGNUM *r;
|
|
|
|
BN_CTX *bn_ctx;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((r = BN_new()) == NULL)
|
|
|
|
goto err;
|
|
|
|
/* Checks if B % N == 0 */
|
|
|
|
if (!BN_nnmod(r, B, N, bn_ctx))
|
|
|
|
goto err;
|
|
|
|
ret = !BN_is_zero(r);
|
|
|
|
err:
|
|
|
|
BN_CTX_free(bn_ctx);
|
|
|
|
BN_free(r);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-06-15 22:17:50 +08:00
|
|
|
int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
/* Checks if A % N == 0 */
|
|
|
|
return SRP_Verify_B_mod_N(A, N);
|
|
|
|
}
|
|
|
|
|
2015-09-12 01:16:23 +08:00
|
|
|
static SRP_gN knowngN[] = {
|
2016-06-15 22:17:50 +08:00
|
|
|
{"8192", &bn_generator_19, &bn_group_8192},
|
|
|
|
{"6144", &bn_generator_5, &bn_group_6144},
|
|
|
|
{"4096", &bn_generator_5, &bn_group_4096},
|
|
|
|
{"3072", &bn_generator_5, &bn_group_3072},
|
|
|
|
{"2048", &bn_generator_2, &bn_group_2048},
|
|
|
|
{"1536", &bn_generator_2, &bn_group_1536},
|
|
|
|
{"1024", &bn_generator_2, &bn_group_1024},
|
2015-09-12 01:16:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
# define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2016-02-06 04:23:54 +08:00
|
|
|
* Check if G and N are known parameters. The values have been generated
|
2018-10-26 06:43:35 +08:00
|
|
|
* from the IETF RFC 5054
|
2015-01-22 11:40:55 +08:00
|
|
|
*/
|
2016-06-15 22:17:50 +08:00
|
|
|
char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if ((g == NULL) || (N == NULL))
|
2018-10-26 06:43:35 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
for (i = 0; i < KNOWN_GN_NUMBER; i++) {
|
|
|
|
if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
|
|
|
|
return knowngN[i].id;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2012-06-04 06:00:21 +08:00
|
|
|
SRP_gN *SRP_get_default_gN(const char *id)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (id == NULL)
|
|
|
|
return knowngN;
|
|
|
|
for (i = 0; i < KNOWN_GN_NUMBER; i++) {
|
|
|
|
if (strcmp(knowngN[i].id, id) == 0)
|
|
|
|
return knowngN + i;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
#endif
|