Make SRP library context aware
[openssl.git] / crypto / srp / srp_lib.c
index 9efad9352fb2168c59c13d0eacd2c970f6d8b302..063f966f96f8ee308bce8dc485919f675c625974 100644 (file)
@@ -1,10 +1,14 @@
 /*
- * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * 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
+ *
+ * Originally written by Christophe Renou and Peter Sylvester,
+ * for the EdelKey project.
  */
 
 #ifndef OPENSSL_NO_SRP
 # include <openssl/sha.h>
 # include <openssl/srp.h>
 # include <openssl/evp.h>
-# include "internal/bn_srp.h"
+# include "crypto/bn_srp.h"
 
-static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
-{
-    /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
+/* calculate = SHA1(PAD(x) || PAD(y)) */
 
+static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N,
+                           OPENSSL_CTX *libctx, const char *propq)
+{
     unsigned char digest[SHA_DIGEST_LENGTH];
     unsigned char *tmp = NULL;
-    EVP_MD_CTX *ctxt = NULL;
-    int longg;
-    int longN = BN_num_bytes(N);
+    int numN = BN_num_bytes(N);
     BIGNUM *res = NULL;
+    EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
 
-    if (BN_ucmp(g, N) >= 0)
+    if (sha1 == NULL)
         return NULL;
 
-    ctxt = EVP_MD_CTX_new();
-    if (ctxt == NULL)
-        return NULL;
-    if ((tmp = OPENSSL_malloc(longN)) == NULL)
+    if (x != N && BN_ucmp(x, N) >= 0)
         goto err;
-    BN_bn2bin(N, tmp);
-
-    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
-        || !EVP_DigestUpdate(ctxt, tmp, longN))
+    if (y != N && BN_ucmp(y, N) >= 0)
         goto err;
-
-    memset(tmp, 0, longN);
-    longg = BN_bn2bin(g, tmp);
-    /* use the zeros behind to pad on left */
-    if (!EVP_DigestUpdate(ctxt, tmp + longg, longN - longg)
-        || !EVP_DigestUpdate(ctxt, tmp, longg))
+    if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
         goto err;
-
-    if (!EVP_DigestFinal_ex(ctxt, digest, NULL))
+    if (BN_bn2binpad(x, tmp, numN) < 0
+        || BN_bn2binpad(y, tmp + numN, numN) < 0
+        || !EVP_Digest(tmp, numN * 2, digest, NULL, sha1, NULL))
         goto err;
     res = BN_bin2bn(digest, sizeof(digest), NULL);
  err:
+    EVP_MD_free(sha1);
     OPENSSL_free(tmp);
-    EVP_MD_CTX_free(ctxt);
     return res;
 }
 
-BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
+static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g, OPENSSL_CTX *libctx,
+                          const char *propq)
 {
-    /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
-
-    BIGNUM *u = NULL;
-    unsigned char cu[SHA_DIGEST_LENGTH];
-    unsigned char *cAB = NULL;
-    EVP_MD_CTX *ctxt = NULL;
-    int longN;
-    if ((A == NULL) || (B == NULL) || (N == NULL))
-        return NULL;
-
-    if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
-        return NULL;
-
-    longN = BN_num_bytes(N);
-
-    ctxt = EVP_MD_CTX_new();
-    if (ctxt == NULL)
-        return NULL;
-    if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
-        goto err;
-
-    memset(cAB, 0, longN);
-
-    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
-        || !EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(A, cAB + longN), longN)
-        || !EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(B, cAB + longN), longN))
-        goto err;
-    OPENSSL_free(cAB);
-    if (!EVP_DigestFinal_ex(ctxt, cu, NULL))
-        goto err;
+    /* k = SHA1(N | PAD(g)) -- tls-srp RFC 5054 */
+    return srp_Calc_xy(N, g, N, libctx, propq);
+}
 
-    if ((u = BN_bin2bn(cu, sizeof(cu), NULL)) == NULL)
-        goto err;
-    if (BN_is_zero(u)) {
-        BN_free(u);
-        u = NULL;
-    }
- err:
-    EVP_MD_CTX_free(ctxt);
+BIGNUM *SRP_Calc_u_ex(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N,
+                      OPENSSL_CTX *libctx, const char *propq)
+{
+    /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
+    return srp_Calc_xy(A, B, N, libctx, propq);
+}
 
-    return u;
+BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
+{
+    /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
+    return srp_Calc_xy(A, B, N, NULL, NULL);
 }
 
 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
@@ -130,15 +99,15 @@ BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
     return S;
 }
 
-BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
-                   const BIGNUM *v)
+BIGNUM *SRP_Calc_B_ex(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
+                      const BIGNUM *v, OPENSSL_CTX *libctx, const char *propq)
 {
     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)
+        (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
         return NULL;
 
     if ((kv = BN_new()) == NULL ||
@@ -148,7 +117,7 @@ BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
     /* B = g**b + k*v */
 
     if (!BN_mod_exp(gb, g, b, N, bn_ctx)
-        || (k = srp_Calc_k(N, g)) == NULL
+        || (k = srp_Calc_k(N, g, libctx, propq)) == NULL
         || !BN_mod_mul(kv, v, k, N, bn_ctx)
         || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
         BN_free(B);
@@ -162,12 +131,20 @@ BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
     return B;
 }
 
-BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
+BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
+                   const BIGNUM *v)
+{
+    return SRP_Calc_B_ex(b, N, g, v, NULL, NULL);
+}
+
+BIGNUM *SRP_Calc_x_ex(const BIGNUM *s, const char *user, const char *pass,
+                      OPENSSL_CTX *libctx, const char *propq)
 {
     unsigned char dig[SHA_DIGEST_LENGTH];
     EVP_MD_CTX *ctxt;
-    unsigned char *cs;
+    unsigned char *cs = NULL;
     BIGNUM *res = NULL;
+    EVP_MD *sha1 = NULL;
 
     if ((s == NULL) || (user == NULL) || (pass == NULL))
         return NULL;
@@ -178,27 +155,40 @@ BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
     if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
         goto err;
 
-    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
+    sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
+    if (sha1 == NULL)
+        goto err;
+
+    if (!EVP_DigestInit_ex(ctxt, 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))
+        || !EVP_DigestInit_ex(ctxt, sha1, NULL))
+        goto err;
+    if (BN_bn2bin(s, cs) < 0)
         goto err;
-    BN_bn2bin(s, cs);
     if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
         goto err;
-    OPENSSL_free(cs);
+
     if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
         || !EVP_DigestFinal_ex(ctxt, dig, NULL))
         goto err;
 
     res = BN_bin2bn(dig, sizeof(dig), NULL);
+
  err:
+    EVP_MD_free(sha1);
+    OPENSSL_free(cs);
     EVP_MD_CTX_free(ctxt);
     return res;
 }
 
+BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
+{
+    return SRP_Calc_x_ex(s, user, pass, NULL, NULL);
+}
+
 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
 {
     BN_CTX *bn_ctx;
@@ -215,14 +205,15 @@ BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
     return A;
 }
 
-BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
-                            const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
+BIGNUM *SRP_Calc_client_key_ex(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
+                            const BIGNUM *x, const BIGNUM *a, const BIGNUM *u,
+                            OPENSSL_CTX *libctx, const char *propq)
 {
     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)
+        || a == NULL || (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
         return NULL;
 
     if ((tmp = BN_new()) == NULL ||
@@ -232,7 +223,7 @@ BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
 
     if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
         goto err;
-    if ((k = srp_Calc_k(N, g)) == NULL)
+    if ((k = srp_Calc_k(N, g, libctx, propq)) == NULL)
         goto err;
     if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
         goto err;
@@ -257,6 +248,12 @@ BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
     return K;
 }
 
+BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
+                            const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
+{
+    return SRP_Calc_client_key_ex(N, B, g, x, a, u, NULL, NULL);
+}
+
 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
 {
     BIGNUM *r;
@@ -298,13 +295,13 @@ static SRP_gN knowngN[] = {
 
 /*
  * Check if G and N are known parameters. The values have been generated
- * from the ietf-tls-srp draft version 8
+ * from the IETF RFC 5054
  */
 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
 {
     size_t i;
     if ((g == NULL) || (N == NULL))
-        return 0;
+        return NULL;
 
     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
         if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)