Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Update base64url library to match latest FCL lib
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnGuilding committed Oct 2, 2023
1 parent 96add0b commit fd24668
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 108 deletions.
105 changes: 0 additions & 105 deletions primitives/src/lib/Base64URL.sol

This file was deleted.

76 changes: 76 additions & 0 deletions primitives/src/lib/Base64Url.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

/**
* @dev Encode (without '=' padding)
* @author evmbrahmin, adapted from hiromin's Base64URL libraries
*/
library Base64Url {
/**
* @dev Base64Url Encoding Table
*/
string internal constant ENCODING_TABLE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

function encode(bytes memory data) internal pure returns (string memory) {
if (data.length == 0) return "";

// Load the table into memory
string memory table = ENCODING_TABLE;

string memory result = new string(4 * ((data.length + 2) / 3));

// @solidity memory-safe-assembly
assembly {
let tablePtr := add(table, 1)
let resultPtr := add(result, 32)

for {
let dataPtr := data
let endPtr := add(data, mload(data))
} lt(dataPtr, endPtr) {

} {
dataPtr := add(dataPtr, 3)
let input := mload(dataPtr)

mstore8(
resultPtr,
mload(add(tablePtr, and(shr(18, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)

mstore8(
resultPtr,
mload(add(tablePtr, and(shr(12, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)

mstore8(
resultPtr,
mload(add(tablePtr, and(shr(6, input), 0x3F)))
)
resultPtr := add(resultPtr, 1)

mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
resultPtr := add(resultPtr, 1)
}

// Remove the padding adjustment logic
switch mod(mload(data), 3)
case 1 {
// Adjust for the last byte of data
resultPtr := sub(resultPtr, 2)
}
case 2 {
// Adjust for the last two bytes of data
resultPtr := sub(resultPtr, 1)
}

// Set the correct length of the result string
mstore(result, sub(resultPtr, add(result, 32)))
}

return result;
}
}
7 changes: 4 additions & 3 deletions primitives/src/lib/FCL_Webauthn.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import {Base64URL} from "./Base64URL.sol";
import {Base64Url} from "./Base64Url.sol";
import {FCL_Elliptic_ZZ} from "./FCL_elliptic.sol";

library FCL_WebAuthn {
Expand All @@ -42,7 +42,8 @@ library FCL_WebAuthn {
revert InvalidAuthenticatorData();
}
// Verify that clientData commits to the expected client challenge
string memory challengeEncoded = Base64URL.encode32(abi.encodePacked(clientChallenge));
// Use the Base64Url encoding which omits padding characters to match WebAuthn Specification
string memory challengeEncoded = Base64Url.encode(abi.encodePacked(clientChallenge));
bytes memory challengeExtracted = new bytes(
bytes(challengeEncoded).length
);
Expand Down Expand Up @@ -140,4 +141,4 @@ library FCL_WebAuthn {

return result;
}
}
}

0 comments on commit fd24668

Please sign in to comment.