This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FCL_Webauthn.sol
178 lines (155 loc) · 7.08 KB
/
FCL_Webauthn.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//********************************************************************************************/
// ___ _ ___ _ _ _ _
// | __| _ ___ __| |_ / __|_ _ _ _ _ __| |_ ___ | | (_) |__
// | _| '_/ -_|_-< ' \ | (__| '_| || | '_ \ _/ _ \ | |__| | '_ \
// |_||_| \___/__/_||_| \___|_| \_, | .__/\__\___/ |____|_|_.__/
///* Copyright (C) 2022 - Renaud Dubois - This file is part of FCL (Fresh CryptoLib) project
///* License: This software is licensed under MIT License
///* This Code may be reused including license and copyright notice.
///* See LICENSE file at the root folder of the project.
///* FILE: FCL_elliptic.sol
///*
///*
///* DESCRIPTION: Implementation of the WebAuthn Authentication mechanism
///* https://www.w3.org/TR/webauthn-2/#sctn-intro
///* Original code extracted from https://github.com/btchip/Webauthn.sol
//**************************************************************************************/
//* WARNING: this code SHALL not be used for non prime order curves for security reasons.
// Code is optimized for a=-3 only curves with prime order, constant like -1, -2 shall be replaced
// if ever used for other curve than sec256R1
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
import {Base64URL} from "./Base64URL.sol";
import {FCL_Elliptic_ZZ} from "./FCL_elliptic.sol";
library FCL_WebAuthn {
error InvalidAuthenticatorData();
error InvalidClientData();
error InvalidSignature();
/** @notice Modified from original Fresh Crypto Lib code to use memory instead of calldata */
function WebAuthn_format(
bytes memory authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes memory clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] memory // rs
) internal pure returns (bytes32 result) {
// Let the caller check if User Presence (0x01) or User Verification (0x04) are set
{
if ((authenticatorData[32] & authenticatorDataFlagMask) != authenticatorDataFlagMask) {
revert InvalidAuthenticatorData();
}
// Verify that clientData commits to the expected client challenge
string memory challengeEncoded = Base64URL.encode32(abi.encodePacked(clientChallenge));
bytes memory challengeExtracted = new bytes(
bytes(challengeEncoded).length
);
// TODO: wax#68 Calldata decoding and stack limit
// Remove use of copyBytes function, and use commented inline assembly instead to extract the client challenge
// assembly {
// calldatacopy(
// add(challengeExtracted, 32),
// add(clientData.offset, clientChallengeDataOffset),
// mload(challengeExtracted)
// )
// }
// bytes32 moreData; //=keccak256(abi.encodePacked(challengeExtracted));
// assembly {
// moreData := keccak256(add(challengeExtracted, 32), mload(challengeExtracted))
// }
// if (keccak256(abi.encodePacked(bytes(challengeEncoded))) != moreData) {
// revert InvalidClientData();
// }
copyBytes(
clientData,
clientChallengeDataOffset,
challengeExtracted.length,
challengeExtracted,
0
);
if (
keccak256(abi.encodePacked(bytes(challengeEncoded))) !=
keccak256(abi.encodePacked(challengeExtracted))
) {
revert InvalidClientData();
}
} //avoid stack full
// Verify the signature over sha256(authenticatorData || sha256(clientData))
bytes32 more = sha256(clientData);
bytes memory verifyData = abi.encodePacked(authenticatorData, more);
return sha256(verifyData);
}
// TODO: wax#68 Calldata decoding and stack limit - remove this function once #68 is complete
/* The following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license */
function copyBytes(
bytes memory _from,
uint _fromOffset,
uint _length,
bytes memory _to,
uint _toOffset
) internal pure returns (bytes memory _copiedBytes) {
uint minLength = _length + _toOffset;
require(_to.length >= minLength); // Buffer too small. Should be a better way?
uint i = 32 + _fromOffset; // NOTE: the offset 32 is added to skip the `size` field of both bytes variables
uint j = 32 + _toOffset;
while (i < (32 + _fromOffset + _length)) {
assembly {
let tmp := mload(add(_from, i))
mstore(add(_to, j), tmp)
}
i += 32;
j += 32;
}
return _to;
}
/** @notice Modified from original Fresh Crypto Lib code to use memory instead of calldata */
function checkSignature(
bytes memory authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes memory clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] memory rs,
uint256[2] memory Q
) internal returns (bool) {
// Let the caller check if User Presence (0x01) or User Verification (0x04) are set
bytes32 message = FCL_WebAuthn.WebAuthn_format(
authenticatorData, authenticatorDataFlagMask, clientData, clientChallenge, clientChallengeDataOffset, rs
);
bool result = FCL_Elliptic_ZZ.ecdsa_verify_memory(message, rs, Q);
return result;
}
function checkSignature_prec(
bytes calldata authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes calldata clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] calldata rs,
address dataPointer
) internal returns (bool) {
// Let the caller check if User Presence (0x01) or User Verification (0x04) are set
bytes32 message = FCL_WebAuthn.WebAuthn_format(
authenticatorData, authenticatorDataFlagMask, clientData, clientChallenge, clientChallengeDataOffset, rs
);
bool result = FCL_Elliptic_ZZ.ecdsa_precomputed_verify(message, rs, dataPointer);
return result;
}
//beware that this implementation will not be compliant with EOF
function checkSignature_hackmem(
bytes calldata authenticatorData,
bytes1 authenticatorDataFlagMask,
bytes calldata clientData,
bytes32 clientChallenge,
uint256 clientChallengeDataOffset,
uint256[2] calldata rs,
uint256 dataPointer
) internal returns (bool) {
// Let the caller check if User Presence (0x01) or User Verification (0x04) are set
bytes32 message = FCL_WebAuthn.WebAuthn_format(
authenticatorData, authenticatorDataFlagMask, clientData, clientChallenge, clientChallengeDataOffset, rs
);
bool result = FCL_Elliptic_ZZ.ecdsa_precomputed_hackmem(message, rs, dataPointer);
return result;
}
}