1  /*
2  * Copyright (C) 2022-2024 Huawei Device Co., Ltd.
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  */
15 
16 #ifndef USERIAM_USER_SIGN_CENTRE_H
17 #define USERIAM_USER_SIGN_CENTRE_H
18 
19 #include <stdint.h>
20 
21 #include "adaptor_algorithm.h"
22 #include "buffer.h"
23 #include "defines.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define AUTH_TOKEN_LEN sizeof(UserAuthTokenHal)
30 #define AUTH_TOKEN_DATA_LEN (AUTH_TOKEN_LEN - SHA256_DIGEST_SIZE)
31 #define AUTH_TOKEN_CIPHER_LEN sizeof(TokenDataToEncrypt)
32 #define TOKEN_VERSION 0
33 #define UDID_LEN 64
34 
35 typedef struct {
36     uint8_t challenge[CHALLENGE_LEN];
37     uint64_t time;
38     uint32_t authTrustLevel;
39     uint32_t authType;
40     uint32_t authMode;
41     uint32_t securityLevel;
42     uint32_t tokenType;
43 } __attribute__((__packed__)) TokenDataPlain;
44 
45 typedef struct {
46     int32_t userId;
47     uint64_t secureUid;
48     uint64_t enrolledId;
49     uint64_t credentialId;
50     uint8_t collectorUdid[UDID_LEN];
51     uint8_t verifierUdid[UDID_LEN];
52 } __attribute__((__packed__)) TokenDataToEncrypt;
53 
54 typedef struct {
55     uint32_t version;
56     TokenDataPlain tokenDataPlain;
57     uint8_t tokenDataCipher[AUTH_TOKEN_CIPHER_LEN];
58     uint8_t tag[AES_GCM_TAG_SIZE];
59     uint8_t iv[AES_GCM_IV_SIZE];
60     uint8_t sign[SHA256_DIGEST_SIZE];
61 } __attribute__((__packed__)) UserAuthTokenHal;
62 
63 typedef struct {
64     TokenDataPlain tokenDataPlain;
65     TokenDataToEncrypt tokenDataToEncrypt;
66 } __attribute__((__packed__)) UserAuthTokenPlain;
67 
68 ResultCode UserAuthTokenSign(UserAuthTokenPlain *tokenPlain, UserAuthTokenHal *authToken);
69 ResultCode UserAuthTokenVerify(UserAuthTokenHal *userAuthToken, UserAuthTokenPlain *tokenPlain);
70 ResultCode ReuseUnlockTokenSign(UserAuthTokenHal *reuseToken);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #endif // USERIAM_USER_SIGN_CENTRE_H