1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: srp lib head.
15 * This file should be changed only infrequently and with great care.
16 */
17
18 #ifndef __SRP_H__
19 #define __SRP_H__
20 #include "mbedtls/bignum.h"
21 #include "errcode.h"
22 #include "dfx_adapt_layer.h"
23
24 #define SRP_SUCC 1
25 #define SRP_FAIL 0
26 #define SHA256_DIGEST_LENGTH 32
27 #define SRP_RANDOM_SALT_LEN 20
28 #define SRP_HASH_LEN 32
29 #define srp_err_printf(fmt...) dfx_log_err(fmt)
30
31 typedef mbedtls_mpi bignum;
32 typedef enum {
33 SRP_GN_1024,
34 SRP_GN_3072,
35 SRP_GN_MAX
36 } srp_gntype;
37 typedef struct {
38 bignum *g;
39 bignum *n;
40 } srp_gn;
41
bn_num_bytes(const mbedtls_mpi * x)42 static inline uint32_t bn_num_bytes(const mbedtls_mpi *x)
43 {
44 return mbedtls_mpi_size(x);
45 }
bn_ucmp(const mbedtls_mpi * x,const mbedtls_mpi * y)46 static inline int32_t bn_ucmp(const mbedtls_mpi *x, const mbedtls_mpi *y)
47 {
48 return mbedtls_mpi_cmp_abs(x, y);
49 }
bn_cmp(const mbedtls_mpi * x,const mbedtls_mpi * y)50 static inline int32_t bn_cmp(const mbedtls_mpi *x, const mbedtls_mpi *y)
51 {
52 return mbedtls_mpi_cmp_mpi(x, y);
53 }
54
55 uint32_t uapi_hash_start(uint32_t *handle);
56 uint32_t uapi_hash_update(uint32_t handle, const uint8_t *input_data, uint32_t input_data_len);
57 uint32_t uapi_hash_final(uint32_t handle, uint8_t *output_hash, uint32_t output_hash_len);
58 uint32_t uapi_hash_sha256(uint8_t *input_data, uint32_t input_data_len, uint8_t *output_hash);
59
60 bignum *bn_new(void);
61 void bn_free(bignum *a);
62 bignum *bn_bin2bn(const uint8_t *s, uint32_t len, bignum *ret);
63 int bn_bn2bin(const bignum *a, unsigned char *to);
64
65 uint32_t srp_get_random_data(uint8_t *random_data, uint16_t length);
66 srp_gn *srp_get_default_gn(srp_gntype type);
67 bignum *srp_calc_b(bignum *b, bignum *n, bignum *g, bignum *v);
68 int srp_verify_b_mod_n(bignum *b, bignum *n);
69 bignum *srp_calc_u(bignum *a, bignum *b, bignum *n);
70 bignum *srp_calc_server_key(bignum *a, bignum *v, bignum *u, bignum *b, bignum *n);
71 int srp_create_verifier_bn(const char *user, const char *pass,
72 bignum **salt, bignum **verifier, srp_gn *gn);
73
74 #endif
75